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.
13 * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
14 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
23 #include <mono/metadata/abi-details.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/debug-helpers.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/profiler-private.h>
28 #include <mono/metadata/mono-debug.h>
29 #include <mono/metadata/gc-internals.h>
30 #include <mono/utils/mono-math.h>
31 #include <mono/utils/mono-mmap.h>
32 #include <mono/utils/mono-memory-model.h>
33 #include <mono/utils/mono-tls.h>
34 #include <mono/utils/mono-hwcap-x86.h>
35 #include <mono/utils/mono-threads.h>
39 #include "mini-amd64.h"
40 #include "cpu-amd64.h"
41 #include "debugger-agent.h"
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() mono_os_mutex_lock (&mini_arch_mutex)
65 #define mono_mini_arch_unlock() mono_os_mutex_unlock (&mini_arch_mutex)
66 static mono_mutex_t mini_arch_mutex;
68 /* The single step trampoline */
69 static gpointer ss_trampoline;
71 /* The breakpoint trampoline */
72 static gpointer bp_trampoline;
74 /* Offset between fp and the first argument in the callee */
75 #define ARGS_OFFSET 16
76 #define GP_SCRATCH_REG AMD64_R11
79 * AMD64 register usage:
80 * - callee saved registers are used for global register allocation
81 * - %r11 is used for materializing 64 bit constants in opcodes
82 * - the rest is used for local allocation
86 * Floating point comparison results:
96 mono_arch_regname (int reg)
99 case AMD64_RAX: return "%rax";
100 case AMD64_RBX: return "%rbx";
101 case AMD64_RCX: return "%rcx";
102 case AMD64_RDX: return "%rdx";
103 case AMD64_RSP: return "%rsp";
104 case AMD64_RBP: return "%rbp";
105 case AMD64_RDI: return "%rdi";
106 case AMD64_RSI: return "%rsi";
107 case AMD64_R8: return "%r8";
108 case AMD64_R9: return "%r9";
109 case AMD64_R10: return "%r10";
110 case AMD64_R11: return "%r11";
111 case AMD64_R12: return "%r12";
112 case AMD64_R13: return "%r13";
113 case AMD64_R14: return "%r14";
114 case AMD64_R15: return "%r15";
119 static const char * packed_xmmregs [] = {
120 "p:xmm0", "p:xmm1", "p:xmm2", "p:xmm3", "p:xmm4", "p:xmm5", "p:xmm6", "p:xmm7", "p:xmm8",
121 "p:xmm9", "p:xmm10", "p:xmm11", "p:xmm12", "p:xmm13", "p:xmm14", "p:xmm15"
124 static const char * single_xmmregs [] = {
125 "s:xmm0", "s:xmm1", "s:xmm2", "s:xmm3", "s:xmm4", "s:xmm5", "s:xmm6", "s:xmm7", "s:xmm8",
126 "s:xmm9", "s:xmm10", "s:xmm11", "s:xmm12", "s:xmm13", "s:xmm14", "s:xmm15"
130 mono_arch_fregname (int reg)
132 if (reg < AMD64_XMM_NREG)
133 return single_xmmregs [reg];
139 mono_arch_xregname (int reg)
141 if (reg < AMD64_XMM_NREG)
142 return packed_xmmregs [reg];
151 return mono_debug_count ();
157 static inline gboolean
158 amd64_is_near_call (guint8 *code)
161 if ((code [0] >= 0x40) && (code [0] <= 0x4f))
164 return code [0] == 0xe8;
167 static inline gboolean
168 amd64_use_imm32 (gint64 val)
170 if (mini_get_debug_options()->single_imm_size)
173 return amd64_is_imm32 (val);
176 #ifdef __native_client_codegen__
178 /* Keep track of instruction "depth", that is, the level of sub-instruction */
179 /* for any given instruction. For instance, amd64_call_reg resolves to */
180 /* amd64_call_reg_internal, which uses amd64_alu_* macros, etc. */
181 /* We only want to force bundle alignment for the top level instruction, */
182 /* so NaCl pseudo-instructions can be implemented with sub instructions. */
183 static MonoNativeTlsKey nacl_instruction_depth;
185 static MonoNativeTlsKey nacl_rex_tag;
186 static MonoNativeTlsKey nacl_legacy_prefix_tag;
189 amd64_nacl_clear_legacy_prefix_tag ()
191 mono_native_tls_set_value (nacl_legacy_prefix_tag, NULL);
195 amd64_nacl_tag_legacy_prefix (guint8* code)
197 if (mono_native_tls_get_value (nacl_legacy_prefix_tag) == NULL)
198 mono_native_tls_set_value (nacl_legacy_prefix_tag, code);
202 amd64_nacl_tag_rex (guint8* code)
204 mono_native_tls_set_value (nacl_rex_tag, code);
208 amd64_nacl_get_legacy_prefix_tag ()
210 return (guint8*)mono_native_tls_get_value (nacl_legacy_prefix_tag);
214 amd64_nacl_get_rex_tag ()
216 return (guint8*)mono_native_tls_get_value (nacl_rex_tag);
219 /* Increment the instruction "depth" described above */
221 amd64_nacl_instruction_pre ()
223 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
225 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)depth);
228 /* amd64_nacl_instruction_post: Decrement instruction "depth", force bundle */
229 /* alignment if depth == 0 (top level instruction) */
230 /* IN: start, end pointers to instruction beginning and end */
231 /* OUT: start, end pointers to beginning and end after possible alignment */
232 /* GLOBALS: nacl_instruction_depth defined above */
234 amd64_nacl_instruction_post (guint8 **start, guint8 **end)
236 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
238 mono_native_tls_set_value (nacl_instruction_depth, (void*)depth);
240 g_assert ( depth >= 0 );
242 uintptr_t space_in_block;
244 guint8 *prefix = amd64_nacl_get_legacy_prefix_tag ();
245 /* if legacy prefix is present, and if it was emitted before */
246 /* the start of the instruction sequence, adjust the start */
247 if (prefix != NULL && prefix < *start) {
248 g_assert (*start - prefix <= 3);/* only 3 are allowed */
251 space_in_block = kNaClAlignment - ((uintptr_t)(*start) & kNaClAlignmentMask);
252 instlen = (uintptr_t)(*end - *start);
253 /* Only check for instructions which are less than */
254 /* kNaClAlignment. The only instructions that should ever */
255 /* be that long are call sequences, which are already */
256 /* padded out to align the return to the next bundle. */
257 if (instlen > space_in_block && instlen < kNaClAlignment) {
258 const size_t MAX_NACL_INST_LENGTH = kNaClAlignment;
259 guint8 copy_of_instruction[MAX_NACL_INST_LENGTH];
260 const size_t length = (size_t)((*end)-(*start));
261 g_assert (length < MAX_NACL_INST_LENGTH);
263 memcpy (copy_of_instruction, *start, length);
264 *start = mono_arch_nacl_pad (*start, space_in_block);
265 memcpy (*start, copy_of_instruction, length);
266 *end = *start + length;
268 amd64_nacl_clear_legacy_prefix_tag ();
269 amd64_nacl_tag_rex (NULL);
273 /* amd64_nacl_membase_handler: ensure all access to memory of the form */
274 /* OFFSET(%rXX) is sandboxed. For allowable base registers %rip, %rbp, */
275 /* %rsp, and %r15, emit the membase as usual. For all other registers, */
276 /* make sure the upper 32-bits are cleared, and use that register in the */
277 /* index field of a new address of this form: OFFSET(%r15,%eXX,1) */
279 /* pointer to current instruction stream (in the */
280 /* middle of an instruction, after opcode is emitted) */
281 /* basereg/offset/dreg */
282 /* operands of normal membase address */
284 /* pointer to the end of the membase/memindex emit */
285 /* GLOBALS: nacl_rex_tag */
286 /* position in instruction stream that rex prefix was emitted */
287 /* nacl_legacy_prefix_tag */
288 /* (possibly NULL) position in instruction of legacy x86 prefix */
290 amd64_nacl_membase_handler (guint8** code, gint8 basereg, gint32 offset, gint8 dreg)
292 gint8 true_basereg = basereg;
294 /* Cache these values, they might change */
295 /* as new instructions are emitted below. */
296 guint8* rex_tag = amd64_nacl_get_rex_tag ();
297 guint8* legacy_prefix_tag = amd64_nacl_get_legacy_prefix_tag ();
299 /* 'basereg' is given masked to 0x7 at this point, so check */
300 /* the rex prefix to see if this is an extended register. */
301 if ((rex_tag != NULL) && IS_REX(*rex_tag) && (*rex_tag & AMD64_REX_B)) {
305 #define X86_LEA_OPCODE (0x8D)
307 if (!amd64_is_valid_nacl_base (true_basereg) && (*(*code-1) != X86_LEA_OPCODE)) {
308 guint8* old_instruction_start;
310 /* This will hold the 'mov %eXX, %eXX' that clears the upper */
311 /* 32-bits of the old base register (new index register) */
313 guint8* buf_ptr = buf;
316 g_assert (rex_tag != NULL);
318 if (IS_REX(*rex_tag)) {
319 /* The old rex.B should be the new rex.X */
320 if (*rex_tag & AMD64_REX_B) {
321 *rex_tag |= AMD64_REX_X;
323 /* Since our new base is %r15 set rex.B */
324 *rex_tag |= AMD64_REX_B;
326 /* Shift the instruction by one byte */
327 /* so we can insert a rex prefix */
328 memmove (rex_tag + 1, rex_tag, (size_t)(*code - rex_tag));
330 /* New rex prefix only needs rex.B for %r15 base */
331 *rex_tag = AMD64_REX(AMD64_REX_B);
334 if (legacy_prefix_tag) {
335 old_instruction_start = legacy_prefix_tag;
337 old_instruction_start = rex_tag;
340 /* Clears the upper 32-bits of the previous base register */
341 amd64_mov_reg_reg_size (buf_ptr, true_basereg, true_basereg, 4);
342 insert_len = buf_ptr - buf;
344 /* Move the old instruction forward to make */
345 /* room for 'mov' stored in 'buf_ptr' */
346 memmove (old_instruction_start + insert_len, old_instruction_start, (size_t)(*code - old_instruction_start));
348 memcpy (old_instruction_start, buf, insert_len);
350 /* Sandboxed replacement for the normal membase_emit */
351 x86_memindex_emit (*code, dreg, AMD64_R15, offset, basereg, 0);
354 /* Normal default behavior, emit membase memory location */
355 x86_membase_emit_body (*code, dreg, basereg, offset);
360 static inline unsigned char*
361 amd64_skip_nops (unsigned char* code)
366 if ( code[0] == 0x90) {
370 if ( code[0] == 0x66 && code[1] == 0x90) {
374 if (code[0] == 0x0f && code[1] == 0x1f
375 && code[2] == 0x00) {
379 if (code[0] == 0x0f && code[1] == 0x1f
380 && code[2] == 0x40 && code[3] == 0x00) {
384 if (code[0] == 0x0f && code[1] == 0x1f
385 && code[2] == 0x44 && code[3] == 0x00
386 && code[4] == 0x00) {
390 if (code[0] == 0x66 && code[1] == 0x0f
391 && code[2] == 0x1f && code[3] == 0x44
392 && code[4] == 0x00 && code[5] == 0x00) {
396 if (code[0] == 0x0f && code[1] == 0x1f
397 && code[2] == 0x80 && code[3] == 0x00
398 && code[4] == 0x00 && code[5] == 0x00
399 && code[6] == 0x00) {
403 if (code[0] == 0x0f && code[1] == 0x1f
404 && code[2] == 0x84 && code[3] == 0x00
405 && code[4] == 0x00 && code[5] == 0x00
406 && code[6] == 0x00 && code[7] == 0x00) {
415 mono_arch_nacl_skip_nops (guint8* code)
417 return amd64_skip_nops(code);
420 #endif /*__native_client_codegen__*/
423 amd64_patch (unsigned char* code, gpointer target)
427 #ifdef __native_client_codegen__
428 code = amd64_skip_nops (code);
430 #if defined(__native_client_codegen__) && defined(__native_client__)
431 if (nacl_is_code_address (code)) {
432 /* For tail calls, code is patched after being installed */
433 /* but not through the normal "patch callsite" method. */
434 unsigned char buf[kNaClAlignment];
435 unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
437 memcpy (buf, aligned_code, kNaClAlignment);
438 /* Patch a temp buffer of bundle size, */
439 /* then install to actual location. */
440 amd64_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), target);
441 ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
445 target = nacl_modify_patch_target (target);
449 if ((code [0] >= 0x40) && (code [0] <= 0x4f)) {
454 if ((code [0] & 0xf8) == 0xb8) {
455 /* amd64_set_reg_template */
456 *(guint64*)(code + 1) = (guint64)target;
458 else if ((code [0] == 0x8b) && rex && x86_modrm_mod (code [1]) == 0 && x86_modrm_rm (code [1]) == 5) {
459 /* mov 0(%rip), %dreg */
460 *(guint32*)(code + 2) = (guint32)(guint64)target - 7;
462 else if ((code [0] == 0xff) && (code [1] == 0x15)) {
463 /* call *<OFFSET>(%rip) */
464 *(guint32*)(code + 2) = ((guint32)(guint64)target) - 7;
466 else if (code [0] == 0xe8) {
468 gint64 disp = (guint8*)target - (guint8*)code;
469 g_assert (amd64_is_imm32 (disp));
470 x86_patch (code, (unsigned char*)target);
473 x86_patch (code, (unsigned char*)target);
477 mono_amd64_patch (unsigned char* code, gpointer target)
479 amd64_patch (code, target);
488 ArgValuetypeAddrInIReg,
489 /* gsharedvt argument passed by addr */
492 ArgNone /* only in pair_storage */
498 ArgStorage storage : 8;
499 gboolean is_gsharedvt_return_value : 1;
501 /* Only if storage == ArgValuetypeInReg */
502 ArgStorage pair_storage [2];
504 /* The size of each pair (bytes) */
507 /* Only if storage == ArgOnStack */
508 int arg_size; // Bytes, will always be rounded up/aligned to 8 byte boundary
516 gboolean need_stack_align;
517 /* The index of the vret arg in the argument list */
524 #define DEBUG(a) if (cfg->verbose_level > 1) a
527 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
529 ainfo->offset = *stack_size;
531 if (*gr >= PARAM_REGS) {
532 ainfo->storage = ArgOnStack;
533 ainfo->arg_size = sizeof (mgreg_t);
534 /* Since the same stack slot size is used for all arg */
535 /* types, it needs to be big enough to hold them all */
536 (*stack_size) += sizeof(mgreg_t);
539 ainfo->storage = ArgInIReg;
540 ainfo->reg = param_regs [*gr];
546 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
548 ainfo->offset = *stack_size;
550 if (*gr >= FLOAT_PARAM_REGS) {
551 ainfo->storage = ArgOnStack;
552 ainfo->arg_size = sizeof (mgreg_t);
553 /* Since the same stack slot size is used for both float */
554 /* types, it needs to be big enough to hold them both */
555 (*stack_size) += sizeof(mgreg_t);
558 /* A double register */
560 ainfo->storage = ArgInDoubleSSEReg;
562 ainfo->storage = ArgInFloatSSEReg;
568 typedef enum ArgumentClass {
576 merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
578 ArgumentClass class2 = ARG_CLASS_NO_CLASS;
581 ptype = mini_get_underlying_type (type);
582 switch (ptype->type) {
591 case MONO_TYPE_STRING:
592 case MONO_TYPE_OBJECT:
593 case MONO_TYPE_CLASS:
594 case MONO_TYPE_SZARRAY:
596 case MONO_TYPE_FNPTR:
597 case MONO_TYPE_ARRAY:
600 class2 = ARG_CLASS_INTEGER;
605 class2 = ARG_CLASS_INTEGER;
607 class2 = ARG_CLASS_SSE;
611 case MONO_TYPE_TYPEDBYREF:
612 g_assert_not_reached ();
614 case MONO_TYPE_GENERICINST:
615 if (!mono_type_generic_inst_is_valuetype (ptype)) {
616 class2 = ARG_CLASS_INTEGER;
620 case MONO_TYPE_VALUETYPE: {
621 MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
624 for (i = 0; i < info->num_fields; ++i) {
626 class2 = merge_argument_class_from_type (info->fields [i].field->type, class2);
631 g_assert_not_reached ();
635 if (class1 == class2)
637 else if (class1 == ARG_CLASS_NO_CLASS)
639 else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
640 class1 = ARG_CLASS_MEMORY;
641 else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
642 class1 = ARG_CLASS_INTEGER;
644 class1 = ARG_CLASS_SSE;
648 #ifdef __native_client_codegen__
650 /* Default alignment for Native Client is 32-byte. */
651 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
653 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
654 /* Check that alignment doesn't cross an alignment boundary. */
656 mono_arch_nacl_pad(guint8 *code, int pad)
658 const int kMaxPadding = 8; /* see amd64-codegen.h:amd64_padding_size() */
660 if (pad == 0) return code;
661 /* assertion: alignment cannot cross a block boundary */
662 g_assert (((uintptr_t)code & (~kNaClAlignmentMask)) ==
663 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
664 while (pad >= kMaxPadding) {
665 amd64_padding (code, kMaxPadding);
668 if (pad != 0) amd64_padding (code, pad);
674 count_fields_nested (MonoClass *klass)
676 MonoMarshalType *info;
679 info = mono_marshal_load_type_info (klass);
682 for (i = 0; i < info->num_fields; ++i) {
683 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type))
684 count += count_fields_nested (mono_class_from_mono_type (info->fields [i].field->type));
692 collect_field_info_nested (MonoClass *klass, MonoMarshalField *fields, int index, int offset)
694 MonoMarshalType *info;
697 info = mono_marshal_load_type_info (klass);
699 for (i = 0; i < info->num_fields; ++i) {
700 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type)) {
701 index = collect_field_info_nested (mono_class_from_mono_type (info->fields [i].field->type), fields, index, info->fields [i].offset);
703 memcpy (&fields [index], &info->fields [i], sizeof (MonoMarshalField));
704 fields [index].offset += offset;
713 add_valuetype_win64 (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
715 guint32 *gr, guint32 *fr, guint32 *stack_size)
717 guint32 size, i, nfields;
719 ArgumentClass arg_class;
720 MonoMarshalType *info = NULL;
721 MonoMarshalField *fields = NULL;
723 gboolean pass_on_stack = FALSE;
725 klass = mono_class_from_mono_type (type);
726 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
728 pass_on_stack = TRUE;
730 /* If this struct can't be split up naturally into 8-byte */
731 /* chunks (registers), pass it on the stack. */
732 if (sig->pinvoke && !pass_on_stack) {
736 info = mono_marshal_load_type_info (klass);
740 * Collect field information recursively to be able to
741 * handle nested structures.
743 nfields = count_fields_nested (klass);
744 fields = g_new0 (MonoMarshalField, nfields);
745 collect_field_info_nested (klass, fields, 0, 0);
747 for (i = 0; i < nfields; ++i) {
748 field_size = mono_marshal_type_size (fields [i].field->type,
750 &align, TRUE, klass->unicode);
751 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
752 pass_on_stack = TRUE;
759 /* Allways pass in memory */
760 ainfo->offset = *stack_size;
761 *stack_size += ALIGN_TO (size, 8);
762 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
764 ainfo->arg_size = ALIGN_TO (size, 8);
771 int n = mono_class_value_size (klass, NULL);
776 arg_class = ARG_CLASS_MEMORY;
778 /* Always pass in 1 integer register */
779 arg_class = ARG_CLASS_INTEGER;
784 ainfo->storage = ArgValuetypeInReg;
785 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
789 switch (info->native_size) {
790 case 1: case 2: case 4: case 8:
794 ainfo->storage = ArgValuetypeAddrInIReg;
795 ainfo->offset = *stack_size;
796 *stack_size += ALIGN_TO (info->native_size, 8);
799 ainfo->storage = ArgValuetypeAddrInIReg;
801 if (*gr < PARAM_REGS) {
802 ainfo->pair_storage [0] = ArgInIReg;
803 ainfo->pair_regs [0] = param_regs [*gr];
807 ainfo->pair_storage [0] = ArgOnStack;
808 ainfo->offset = *stack_size;
809 ainfo->arg_size = sizeof (mgreg_t);
820 ArgumentClass class1;
823 class1 = ARG_CLASS_MEMORY;
825 class1 = ARG_CLASS_NO_CLASS;
826 for (i = 0; i < nfields; ++i) {
827 size = mono_marshal_type_size (fields [i].field->type,
829 &align, TRUE, klass->unicode);
830 /* How far into this quad this data extends.*/
831 /* (8 is size of quad) */
832 argsize = fields [i].offset + size;
834 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
836 g_assert (class1 != ARG_CLASS_NO_CLASS);
842 /* Allocate registers */
847 while (argsize != 1 && argsize != 2 && argsize != 4 && argsize != 8)
850 ainfo->storage = ArgValuetypeInReg;
851 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
852 ainfo->pair_size [0] = argsize;
853 ainfo->pair_size [1] = 0;
856 case ARG_CLASS_INTEGER:
857 if (*gr >= PARAM_REGS)
858 arg_class = ARG_CLASS_MEMORY;
860 ainfo->pair_storage [0] = ArgInIReg;
862 ainfo->pair_regs [0] = return_regs [*gr];
864 ainfo->pair_regs [0] = param_regs [*gr];
869 if (*fr >= FLOAT_PARAM_REGS)
870 arg_class = ARG_CLASS_MEMORY;
873 ainfo->pair_storage [0] = ArgInFloatSSEReg;
875 ainfo->pair_storage [0] = ArgInDoubleSSEReg;
876 ainfo->pair_regs [0] = *fr;
880 case ARG_CLASS_MEMORY:
883 g_assert_not_reached ();
886 if (arg_class == ARG_CLASS_MEMORY) {
887 /* Revert possible register assignments */
891 ainfo->offset = *stack_size;
892 *stack_size += sizeof (mgreg_t);
893 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
895 ainfo->arg_size = sizeof (mgreg_t);
899 #endif /* TARGET_WIN32 */
902 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
904 guint32 *gr, guint32 *fr, guint32 *stack_size)
907 add_valuetype_win64 (sig, ainfo, type, is_return, gr, fr, stack_size);
909 guint32 size, quad, nquads, i, nfields;
910 /* Keep track of the size used in each quad so we can */
911 /* use the right size when copying args/return vars. */
912 guint32 quadsize [2] = {8, 8};
913 ArgumentClass args [2];
914 MonoMarshalType *info = NULL;
915 MonoMarshalField *fields = NULL;
917 gboolean pass_on_stack = FALSE;
919 klass = mono_class_from_mono_type (type);
920 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
921 if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
922 /* We pass and return vtypes of size 8 in a register */
923 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
924 pass_on_stack = TRUE;
927 /* If this struct can't be split up naturally into 8-byte */
928 /* chunks (registers), pass it on the stack. */
929 if (sig->pinvoke && !pass_on_stack) {
933 info = mono_marshal_load_type_info (klass);
937 * Collect field information recursively to be able to
938 * handle nested structures.
940 nfields = count_fields_nested (klass);
941 fields = g_new0 (MonoMarshalField, nfields);
942 collect_field_info_nested (klass, fields, 0, 0);
944 for (i = 0; i < nfields; ++i) {
945 field_size = mono_marshal_type_size (fields [i].field->type,
947 &align, TRUE, klass->unicode);
948 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
949 pass_on_stack = TRUE;
956 ainfo->storage = ArgValuetypeInReg;
957 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
962 /* Allways pass in memory */
963 ainfo->offset = *stack_size;
964 *stack_size += ALIGN_TO (size, 8);
965 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
967 ainfo->arg_size = ALIGN_TO (size, 8);
979 int n = mono_class_value_size (klass, NULL);
981 quadsize [0] = n >= 8 ? 8 : n;
982 quadsize [1] = n >= 8 ? MAX (n - 8, 8) : 0;
984 /* Always pass in 1 or 2 integer registers */
985 args [0] = ARG_CLASS_INTEGER;
986 args [1] = ARG_CLASS_INTEGER;
987 /* Only the simplest cases are supported */
988 if (is_return && nquads != 1) {
989 args [0] = ARG_CLASS_MEMORY;
990 args [1] = ARG_CLASS_MEMORY;
994 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
995 * The X87 and SSEUP stuff is left out since there are no such types in
1001 ainfo->storage = ArgValuetypeInReg;
1002 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
1006 if (info->native_size > 16) {
1007 ainfo->offset = *stack_size;
1008 *stack_size += ALIGN_TO (info->native_size, 8);
1009 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
1011 ainfo->arg_size = ALIGN_TO (info->native_size, 8);
1017 args [0] = ARG_CLASS_NO_CLASS;
1018 args [1] = ARG_CLASS_NO_CLASS;
1019 for (quad = 0; quad < nquads; ++quad) {
1022 ArgumentClass class1;
1025 class1 = ARG_CLASS_MEMORY;
1027 class1 = ARG_CLASS_NO_CLASS;
1028 for (i = 0; i < nfields; ++i) {
1029 size = mono_marshal_type_size (fields [i].field->type,
1031 &align, TRUE, klass->unicode);
1032 if ((fields [i].offset < 8) && (fields [i].offset + size) > 8) {
1033 /* Unaligned field */
1037 /* Skip fields in other quad */
1038 if ((quad == 0) && (fields [i].offset >= 8))
1040 if ((quad == 1) && (fields [i].offset < 8))
1043 /* How far into this quad this data extends.*/
1044 /* (8 is size of quad) */
1045 quadsize [quad] = fields [i].offset + size - (quad * 8);
1047 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
1049 g_assert (class1 != ARG_CLASS_NO_CLASS);
1050 args [quad] = class1;
1056 /* Post merger cleanup */
1057 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
1058 args [0] = args [1] = ARG_CLASS_MEMORY;
1060 /* Allocate registers */
1065 while (quadsize [0] != 1 && quadsize [0] != 2 && quadsize [0] != 4 && quadsize [0] != 8)
1067 while (quadsize [1] != 0 && quadsize [1] != 1 && quadsize [1] != 2 && quadsize [1] != 4 && quadsize [1] != 8)
1070 ainfo->storage = ArgValuetypeInReg;
1071 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
1072 g_assert (quadsize [0] <= 8);
1073 g_assert (quadsize [1] <= 8);
1074 ainfo->pair_size [0] = quadsize [0];
1075 ainfo->pair_size [1] = quadsize [1];
1076 ainfo->nregs = nquads;
1077 for (quad = 0; quad < nquads; ++quad) {
1078 switch (args [quad]) {
1079 case ARG_CLASS_INTEGER:
1080 if (*gr >= PARAM_REGS)
1081 args [quad] = ARG_CLASS_MEMORY;
1083 ainfo->pair_storage [quad] = ArgInIReg;
1085 ainfo->pair_regs [quad] = return_regs [*gr];
1087 ainfo->pair_regs [quad] = param_regs [*gr];
1092 if (*fr >= FLOAT_PARAM_REGS)
1093 args [quad] = ARG_CLASS_MEMORY;
1095 if (quadsize[quad] <= 4)
1096 ainfo->pair_storage [quad] = ArgInFloatSSEReg;
1097 else ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
1098 ainfo->pair_regs [quad] = *fr;
1102 case ARG_CLASS_MEMORY:
1105 g_assert_not_reached ();
1109 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
1111 /* Revert possible register assignments */
1115 ainfo->offset = *stack_size;
1117 arg_size = ALIGN_TO (info->native_size, 8);
1119 arg_size = nquads * sizeof(mgreg_t);
1120 *stack_size += arg_size;
1121 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
1123 ainfo->arg_size = arg_size;
1126 #endif /* !TARGET_WIN32 */
1132 * Obtain information about a call according to the calling convention.
1133 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
1134 * Draft Version 0.23" document for more information.
1137 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
1139 guint32 i, gr, fr, pstart;
1141 int n = sig->hasthis + sig->param_count;
1142 guint32 stack_size = 0;
1144 gboolean is_pinvoke = sig->pinvoke;
1147 cinfo = (CallInfo *)mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1149 cinfo = (CallInfo *)g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1157 /* Reserve space where the callee can save the argument registers */
1158 stack_size = 4 * sizeof (mgreg_t);
1162 ret_type = mini_get_underlying_type (sig->ret);
1163 switch (ret_type->type) {
1173 case MONO_TYPE_FNPTR:
1174 case MONO_TYPE_CLASS:
1175 case MONO_TYPE_OBJECT:
1176 case MONO_TYPE_SZARRAY:
1177 case MONO_TYPE_ARRAY:
1178 case MONO_TYPE_STRING:
1179 cinfo->ret.storage = ArgInIReg;
1180 cinfo->ret.reg = AMD64_RAX;
1184 cinfo->ret.storage = ArgInIReg;
1185 cinfo->ret.reg = AMD64_RAX;
1188 cinfo->ret.storage = ArgInFloatSSEReg;
1189 cinfo->ret.reg = AMD64_XMM0;
1192 cinfo->ret.storage = ArgInDoubleSSEReg;
1193 cinfo->ret.reg = AMD64_XMM0;
1195 case MONO_TYPE_GENERICINST:
1196 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1197 cinfo->ret.storage = ArgInIReg;
1198 cinfo->ret.reg = AMD64_RAX;
1201 if (mini_is_gsharedvt_type (ret_type)) {
1202 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1203 cinfo->ret.is_gsharedvt_return_value = 1;
1207 case MONO_TYPE_VALUETYPE:
1208 case MONO_TYPE_TYPEDBYREF: {
1209 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
1211 add_valuetype (sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
1212 g_assert (cinfo->ret.storage != ArgInIReg);
1216 case MONO_TYPE_MVAR:
1217 g_assert (mini_is_gsharedvt_type (ret_type));
1218 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1219 cinfo->ret.is_gsharedvt_return_value = 1;
1221 case MONO_TYPE_VOID:
1224 g_error ("Can't handle as return value 0x%x", ret_type->type);
1229 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1230 * the first argument, allowing 'this' to be always passed in the first arg reg.
1231 * Also do this if the first argument is a reference type, since virtual calls
1232 * are sometimes made using calli without sig->hasthis set, like in the delegate
1235 if (cinfo->ret.storage == ArgValuetypeAddrInIReg && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
1237 add_general (&gr, &stack_size, cinfo->args + 0);
1239 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
1242 add_general (&gr, &stack_size, &cinfo->ret);
1243 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1244 cinfo->vret_arg_index = 1;
1248 add_general (&gr, &stack_size, cinfo->args + 0);
1250 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
1251 add_general (&gr, &stack_size, &cinfo->ret);
1252 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1256 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
1258 fr = FLOAT_PARAM_REGS;
1260 /* Emit the signature cookie just before the implicit arguments */
1261 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1264 for (i = pstart; i < sig->param_count; ++i) {
1265 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
1269 /* The float param registers and other param registers must be the same index on Windows x64.*/
1276 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1277 /* We allways pass the sig cookie on the stack for simplicity */
1279 * Prevent implicit arguments + the sig cookie from being passed
1283 fr = FLOAT_PARAM_REGS;
1285 /* Emit the signature cookie just before the implicit arguments */
1286 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1289 ptype = mini_get_underlying_type (sig->params [i]);
1290 switch (ptype->type) {
1293 add_general (&gr, &stack_size, ainfo);
1297 add_general (&gr, &stack_size, ainfo);
1301 add_general (&gr, &stack_size, ainfo);
1306 case MONO_TYPE_FNPTR:
1307 case MONO_TYPE_CLASS:
1308 case MONO_TYPE_OBJECT:
1309 case MONO_TYPE_STRING:
1310 case MONO_TYPE_SZARRAY:
1311 case MONO_TYPE_ARRAY:
1312 add_general (&gr, &stack_size, ainfo);
1314 case MONO_TYPE_GENERICINST:
1315 if (!mono_type_generic_inst_is_valuetype (ptype)) {
1316 add_general (&gr, &stack_size, ainfo);
1319 if (mini_is_gsharedvt_variable_type (ptype)) {
1320 /* gsharedvt arguments are passed by ref */
1321 add_general (&gr, &stack_size, ainfo);
1322 if (ainfo->storage == ArgInIReg)
1323 ainfo->storage = ArgGSharedVtInReg;
1325 ainfo->storage = ArgGSharedVtOnStack;
1329 case MONO_TYPE_VALUETYPE:
1330 case MONO_TYPE_TYPEDBYREF:
1331 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
1336 add_general (&gr, &stack_size, ainfo);
1339 add_float (&fr, &stack_size, ainfo, FALSE);
1342 add_float (&fr, &stack_size, ainfo, TRUE);
1345 case MONO_TYPE_MVAR:
1346 /* gsharedvt arguments are passed by ref */
1347 g_assert (mini_is_gsharedvt_type (ptype));
1348 add_general (&gr, &stack_size, ainfo);
1349 if (ainfo->storage == ArgInIReg)
1350 ainfo->storage = ArgGSharedVtInReg;
1352 ainfo->storage = ArgGSharedVtOnStack;
1355 g_assert_not_reached ();
1359 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
1361 fr = FLOAT_PARAM_REGS;
1363 /* Emit the signature cookie just before the implicit arguments */
1364 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1367 cinfo->stack_usage = stack_size;
1368 cinfo->reg_usage = gr;
1369 cinfo->freg_usage = fr;
1374 * mono_arch_get_argument_info:
1375 * @csig: a method signature
1376 * @param_count: the number of parameters to consider
1377 * @arg_info: an array to store the result infos
1379 * Gathers information on parameters such as size, alignment and
1380 * padding. arg_info should be large enought to hold param_count + 1 entries.
1382 * Returns the size of the argument area on the stack.
1385 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
1388 CallInfo *cinfo = get_call_info (NULL, csig);
1389 guint32 args_size = cinfo->stack_usage;
1391 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
1392 if (csig->hasthis) {
1393 arg_info [0].offset = 0;
1396 for (k = 0; k < param_count; k++) {
1397 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
1399 arg_info [k + 1].size = 0;
1408 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
1412 MonoType *callee_ret;
1414 c1 = get_call_info (NULL, caller_sig);
1415 c2 = get_call_info (NULL, callee_sig);
1416 res = c1->stack_usage >= c2->stack_usage;
1417 callee_ret = mini_get_underlying_type (callee_sig->ret);
1418 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
1419 /* An address on the callee's stack is passed as the first argument */
1429 * Initialize the cpu to execute managed code.
1432 mono_arch_cpu_init (void)
1437 /* spec compliance requires running with double precision */
1438 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1439 fpcw &= ~X86_FPCW_PRECC_MASK;
1440 fpcw |= X86_FPCW_PREC_DOUBLE;
1441 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
1442 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1444 /* TODO: This is crashing on Win64 right now.
1445 * _control87 (_PC_53, MCW_PC);
1451 * Initialize architecture specific code.
1454 mono_arch_init (void)
1456 mono_os_mutex_init_recursive (&mini_arch_mutex);
1457 #if defined(__native_client_codegen__)
1458 mono_native_tls_alloc (&nacl_instruction_depth, NULL);
1459 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)0);
1460 mono_native_tls_alloc (&nacl_rex_tag, NULL);
1461 mono_native_tls_alloc (&nacl_legacy_prefix_tag, NULL);
1464 mono_aot_register_jit_icall ("mono_amd64_throw_exception", mono_amd64_throw_exception);
1465 mono_aot_register_jit_icall ("mono_amd64_throw_corlib_exception", mono_amd64_throw_corlib_exception);
1466 mono_aot_register_jit_icall ("mono_amd64_resume_unwind", mono_amd64_resume_unwind);
1467 mono_aot_register_jit_icall ("mono_amd64_get_original_ip", mono_amd64_get_original_ip);
1468 #if defined(ENABLE_GSHAREDVT)
1469 mono_aot_register_jit_icall ("mono_amd64_start_gsharedvt_call", mono_amd64_start_gsharedvt_call);
1473 bp_trampoline = mini_get_breakpoint_trampoline ();
1477 * Cleanup architecture specific code.
1480 mono_arch_cleanup (void)
1482 mono_os_mutex_destroy (&mini_arch_mutex);
1483 #if defined(__native_client_codegen__)
1484 mono_native_tls_free (nacl_instruction_depth);
1485 mono_native_tls_free (nacl_rex_tag);
1486 mono_native_tls_free (nacl_legacy_prefix_tag);
1491 * This function returns the optimizations supported on this cpu.
1494 mono_arch_cpu_optimizations (guint32 *exclude_mask)
1500 if (mono_hwcap_x86_has_cmov) {
1501 opts |= MONO_OPT_CMOV;
1503 if (mono_hwcap_x86_has_fcmov)
1504 opts |= MONO_OPT_FCMOV;
1506 *exclude_mask |= MONO_OPT_FCMOV;
1508 *exclude_mask |= MONO_OPT_CMOV;
1515 * This function test for all SSE functions supported.
1517 * Returns a bitmask corresponding to all supported versions.
1521 mono_arch_cpu_enumerate_simd_versions (void)
1523 guint32 sse_opts = 0;
1525 if (mono_hwcap_x86_has_sse1)
1526 sse_opts |= SIMD_VERSION_SSE1;
1528 if (mono_hwcap_x86_has_sse2)
1529 sse_opts |= SIMD_VERSION_SSE2;
1531 if (mono_hwcap_x86_has_sse3)
1532 sse_opts |= SIMD_VERSION_SSE3;
1534 if (mono_hwcap_x86_has_ssse3)
1535 sse_opts |= SIMD_VERSION_SSSE3;
1537 if (mono_hwcap_x86_has_sse41)
1538 sse_opts |= SIMD_VERSION_SSE41;
1540 if (mono_hwcap_x86_has_sse42)
1541 sse_opts |= SIMD_VERSION_SSE42;
1543 if (mono_hwcap_x86_has_sse4a)
1544 sse_opts |= SIMD_VERSION_SSE4a;
1552 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1557 for (i = 0; i < cfg->num_varinfo; i++) {
1558 MonoInst *ins = cfg->varinfo [i];
1559 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1562 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1565 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
1566 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1569 if (mono_is_regsize_var (ins->inst_vtype)) {
1570 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1571 g_assert (i == vmv->idx);
1572 vars = g_list_prepend (vars, vmv);
1576 vars = mono_varlist_sort (cfg, vars, 0);
1582 * mono_arch_compute_omit_fp:
1584 * Determine whenever the frame pointer can be eliminated.
1587 mono_arch_compute_omit_fp (MonoCompile *cfg)
1589 MonoMethodSignature *sig;
1590 MonoMethodHeader *header;
1594 if (cfg->arch.omit_fp_computed)
1597 header = cfg->header;
1599 sig = mono_method_signature (cfg->method);
1601 if (!cfg->arch.cinfo)
1602 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1603 cinfo = (CallInfo *)cfg->arch.cinfo;
1606 * FIXME: Remove some of the restrictions.
1608 cfg->arch.omit_fp = TRUE;
1609 cfg->arch.omit_fp_computed = TRUE;
1611 #ifdef __native_client_codegen__
1612 /* NaCl modules may not change the value of RBP, so it cannot be */
1613 /* used as a normal register, but it can be used as a frame pointer*/
1614 cfg->disable_omit_fp = TRUE;
1615 cfg->arch.omit_fp = FALSE;
1618 if (cfg->disable_omit_fp)
1619 cfg->arch.omit_fp = FALSE;
1621 if (!debug_omit_fp ())
1622 cfg->arch.omit_fp = FALSE;
1624 if (cfg->method->save_lmf)
1625 cfg->arch.omit_fp = FALSE;
1627 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1628 cfg->arch.omit_fp = FALSE;
1629 if (header->num_clauses)
1630 cfg->arch.omit_fp = FALSE;
1631 if (cfg->param_area)
1632 cfg->arch.omit_fp = FALSE;
1633 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1634 cfg->arch.omit_fp = FALSE;
1635 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1636 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1637 cfg->arch.omit_fp = FALSE;
1638 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1639 ArgInfo *ainfo = &cinfo->args [i];
1641 if (ainfo->storage == ArgOnStack) {
1643 * The stack offset can only be determined when the frame
1646 cfg->arch.omit_fp = FALSE;
1651 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1652 MonoInst *ins = cfg->varinfo [i];
1655 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1660 mono_arch_get_global_int_regs (MonoCompile *cfg)
1664 mono_arch_compute_omit_fp (cfg);
1666 if (cfg->arch.omit_fp)
1667 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1669 /* We use the callee saved registers for global allocation */
1670 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1671 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1672 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1673 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1674 #ifndef __native_client_codegen__
1675 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1678 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1679 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1686 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1691 /* All XMM registers */
1692 for (i = 0; i < 16; ++i)
1693 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1699 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1701 static GList *r = NULL;
1706 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1707 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1708 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1709 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1710 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1711 #ifndef __native_client_codegen__
1712 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1715 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1716 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1717 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1718 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1719 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1720 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1721 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1722 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1724 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1731 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1734 static GList *r = NULL;
1739 for (i = 0; i < AMD64_XMM_NREG; ++i)
1740 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1742 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1749 * mono_arch_regalloc_cost:
1751 * Return the cost, in number of memory references, of the action of
1752 * allocating the variable VMV into a register during global register
1756 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1758 MonoInst *ins = cfg->varinfo [vmv->idx];
1760 if (cfg->method->save_lmf)
1761 /* The register is already saved */
1762 /* substract 1 for the invisible store in the prolog */
1763 return (ins->opcode == OP_ARG) ? 0 : 1;
1766 return (ins->opcode == OP_ARG) ? 1 : 2;
1770 * mono_arch_fill_argument_info:
1772 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1776 mono_arch_fill_argument_info (MonoCompile *cfg)
1779 MonoMethodSignature *sig;
1784 sig = mono_method_signature (cfg->method);
1786 cinfo = (CallInfo *)cfg->arch.cinfo;
1787 sig_ret = mini_get_underlying_type (sig->ret);
1790 * Contrary to mono_arch_allocate_vars (), the information should describe
1791 * where the arguments are at the beginning of the method, not where they can be
1792 * accessed during the execution of the method. The later makes no sense for the
1793 * global register allocator, since a variable can be in more than one location.
1795 switch (cinfo->ret.storage) {
1797 case ArgInFloatSSEReg:
1798 case ArgInDoubleSSEReg:
1799 cfg->ret->opcode = OP_REGVAR;
1800 cfg->ret->inst_c0 = cinfo->ret.reg;
1802 case ArgValuetypeInReg:
1803 cfg->ret->opcode = OP_REGOFFSET;
1804 cfg->ret->inst_basereg = -1;
1805 cfg->ret->inst_offset = -1;
1810 g_assert_not_reached ();
1813 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1814 ArgInfo *ainfo = &cinfo->args [i];
1816 ins = cfg->args [i];
1818 switch (ainfo->storage) {
1820 case ArgInFloatSSEReg:
1821 case ArgInDoubleSSEReg:
1822 ins->opcode = OP_REGVAR;
1823 ins->inst_c0 = ainfo->reg;
1826 ins->opcode = OP_REGOFFSET;
1827 ins->inst_basereg = -1;
1828 ins->inst_offset = -1;
1830 case ArgValuetypeInReg:
1832 ins->opcode = OP_NOP;
1835 g_assert_not_reached ();
1841 mono_arch_allocate_vars (MonoCompile *cfg)
1844 MonoMethodSignature *sig;
1847 guint32 locals_stack_size, locals_stack_align;
1851 sig = mono_method_signature (cfg->method);
1853 cinfo = (CallInfo *)cfg->arch.cinfo;
1854 sig_ret = mini_get_underlying_type (sig->ret);
1856 mono_arch_compute_omit_fp (cfg);
1859 * We use the ABI calling conventions for managed code as well.
1860 * Exception: valuetypes are only sometimes passed or returned in registers.
1864 * The stack looks like this:
1865 * <incoming arguments passed on the stack>
1867 * <lmf/caller saved registers>
1870 * <localloc area> -> grows dynamically
1874 if (cfg->arch.omit_fp) {
1875 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1876 cfg->frame_reg = AMD64_RSP;
1879 /* Locals are allocated backwards from %fp */
1880 cfg->frame_reg = AMD64_RBP;
1884 cfg->arch.saved_iregs = cfg->used_int_regs;
1885 if (cfg->method->save_lmf)
1886 /* Save all callee-saved registers normally, and restore them when unwinding through an LMF */
1887 cfg->arch.saved_iregs |= (1 << AMD64_RBX) | (1 << AMD64_R12) | (1 << AMD64_R13) | (1 << AMD64_R14) | (1 << AMD64_R15);
1889 if (cfg->arch.omit_fp)
1890 cfg->arch.reg_save_area_offset = offset;
1891 /* Reserve space for callee saved registers */
1892 for (i = 0; i < AMD64_NREG; ++i)
1893 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
1894 offset += sizeof(mgreg_t);
1896 if (!cfg->arch.omit_fp)
1897 cfg->arch.reg_save_area_offset = -offset;
1899 if (sig_ret->type != MONO_TYPE_VOID) {
1900 switch (cinfo->ret.storage) {
1902 case ArgInFloatSSEReg:
1903 case ArgInDoubleSSEReg:
1904 cfg->ret->opcode = OP_REGVAR;
1905 cfg->ret->inst_c0 = cinfo->ret.reg;
1906 cfg->ret->dreg = cinfo->ret.reg;
1908 case ArgValuetypeAddrInIReg:
1909 /* The register is volatile */
1910 cfg->vret_addr->opcode = OP_REGOFFSET;
1911 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1912 if (cfg->arch.omit_fp) {
1913 cfg->vret_addr->inst_offset = offset;
1917 cfg->vret_addr->inst_offset = -offset;
1919 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1920 printf ("vret_addr =");
1921 mono_print_ins (cfg->vret_addr);
1924 case ArgValuetypeInReg:
1925 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1926 cfg->ret->opcode = OP_REGOFFSET;
1927 cfg->ret->inst_basereg = cfg->frame_reg;
1928 if (cfg->arch.omit_fp) {
1929 cfg->ret->inst_offset = offset;
1930 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1932 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1933 cfg->ret->inst_offset = - offset;
1937 g_assert_not_reached ();
1941 /* Allocate locals */
1942 offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1943 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1944 char *mname = mono_method_full_name (cfg->method, TRUE);
1945 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s stack is too big.", mname));
1950 if (locals_stack_align) {
1951 offset += (locals_stack_align - 1);
1952 offset &= ~(locals_stack_align - 1);
1954 if (cfg->arch.omit_fp) {
1955 cfg->locals_min_stack_offset = offset;
1956 cfg->locals_max_stack_offset = offset + locals_stack_size;
1958 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1959 cfg->locals_max_stack_offset = - offset;
1962 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1963 if (offsets [i] != -1) {
1964 MonoInst *ins = cfg->varinfo [i];
1965 ins->opcode = OP_REGOFFSET;
1966 ins->inst_basereg = cfg->frame_reg;
1967 if (cfg->arch.omit_fp)
1968 ins->inst_offset = (offset + offsets [i]);
1970 ins->inst_offset = - (offset + offsets [i]);
1971 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1974 offset += locals_stack_size;
1976 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1977 g_assert (!cfg->arch.omit_fp);
1978 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1979 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1982 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1983 ins = cfg->args [i];
1984 if (ins->opcode != OP_REGVAR) {
1985 ArgInfo *ainfo = &cinfo->args [i];
1986 gboolean inreg = TRUE;
1988 /* FIXME: Allocate volatile arguments to registers */
1989 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1993 * Under AMD64, all registers used to pass arguments to functions
1994 * are volatile across calls.
1995 * FIXME: Optimize this.
1997 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg) || (ainfo->storage == ArgGSharedVtInReg))
2000 ins->opcode = OP_REGOFFSET;
2002 switch (ainfo->storage) {
2004 case ArgInFloatSSEReg:
2005 case ArgInDoubleSSEReg:
2006 case ArgGSharedVtInReg:
2008 ins->opcode = OP_REGVAR;
2009 ins->dreg = ainfo->reg;
2013 case ArgGSharedVtOnStack:
2014 g_assert (!cfg->arch.omit_fp);
2015 ins->opcode = OP_REGOFFSET;
2016 ins->inst_basereg = cfg->frame_reg;
2017 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
2019 case ArgValuetypeInReg:
2021 case ArgValuetypeAddrInIReg: {
2023 g_assert (!cfg->arch.omit_fp);
2025 MONO_INST_NEW (cfg, indir, 0);
2026 indir->opcode = OP_REGOFFSET;
2027 if (ainfo->pair_storage [0] == ArgInIReg) {
2028 indir->inst_basereg = cfg->frame_reg;
2029 offset = ALIGN_TO (offset, sizeof (gpointer));
2030 offset += (sizeof (gpointer));
2031 indir->inst_offset = - offset;
2034 indir->inst_basereg = cfg->frame_reg;
2035 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
2038 ins->opcode = OP_VTARG_ADDR;
2039 ins->inst_left = indir;
2047 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg) && (ainfo->storage != ArgGSharedVtOnStack)) {
2048 ins->opcode = OP_REGOFFSET;
2049 ins->inst_basereg = cfg->frame_reg;
2050 /* These arguments are saved to the stack in the prolog */
2051 offset = ALIGN_TO (offset, sizeof(mgreg_t));
2052 if (cfg->arch.omit_fp) {
2053 ins->inst_offset = offset;
2054 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
2055 // Arguments are yet supported by the stack map creation code
2056 //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
2058 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
2059 ins->inst_offset = - offset;
2060 //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
2066 cfg->stack_offset = offset;
2070 mono_arch_create_vars (MonoCompile *cfg)
2072 MonoMethodSignature *sig;
2076 sig = mono_method_signature (cfg->method);
2078 if (!cfg->arch.cinfo)
2079 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
2080 cinfo = (CallInfo *)cfg->arch.cinfo;
2082 if (cinfo->ret.storage == ArgValuetypeInReg)
2083 cfg->ret_var_is_local = TRUE;
2085 sig_ret = mini_get_underlying_type (sig->ret);
2086 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
2087 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2088 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2089 printf ("vret_addr = ");
2090 mono_print_ins (cfg->vret_addr);
2094 if (cfg->gen_sdb_seq_points) {
2097 if (cfg->compile_aot) {
2098 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2099 ins->flags |= MONO_INST_VOLATILE;
2100 cfg->arch.seq_point_info_var = ins;
2102 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2103 ins->flags |= MONO_INST_VOLATILE;
2104 cfg->arch.ss_tramp_var = ins;
2106 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2107 ins->flags |= MONO_INST_VOLATILE;
2108 cfg->arch.bp_tramp_var = ins;
2111 if (cfg->method->save_lmf)
2112 cfg->create_lmf_var = TRUE;
2114 if (cfg->method->save_lmf) {
2116 #if !defined(TARGET_WIN32)
2117 if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
2118 cfg->lmf_ir_mono_lmf = TRUE;
2124 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2130 MONO_INST_NEW (cfg, ins, OP_MOVE);
2131 ins->dreg = mono_alloc_ireg_copy (cfg, tree->dreg);
2132 ins->sreg1 = tree->dreg;
2133 MONO_ADD_INS (cfg->cbb, ins);
2134 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2136 case ArgInFloatSSEReg:
2137 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
2138 ins->dreg = mono_alloc_freg (cfg);
2139 ins->sreg1 = tree->dreg;
2140 MONO_ADD_INS (cfg->cbb, ins);
2142 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2144 case ArgInDoubleSSEReg:
2145 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2146 ins->dreg = mono_alloc_freg (cfg);
2147 ins->sreg1 = tree->dreg;
2148 MONO_ADD_INS (cfg->cbb, ins);
2150 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2154 g_assert_not_reached ();
2159 arg_storage_to_load_membase (ArgStorage storage)
2163 #if defined(__mono_ilp32__)
2164 return OP_LOADI8_MEMBASE;
2166 return OP_LOAD_MEMBASE;
2168 case ArgInDoubleSSEReg:
2169 return OP_LOADR8_MEMBASE;
2170 case ArgInFloatSSEReg:
2171 return OP_LOADR4_MEMBASE;
2173 g_assert_not_reached ();
2180 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2182 MonoMethodSignature *tmp_sig;
2185 if (call->tail_call)
2188 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
2191 * mono_ArgIterator_Setup assumes the signature cookie is
2192 * passed first and all the arguments which were before it are
2193 * passed on the stack after the signature. So compensate by
2194 * passing a different signature.
2196 tmp_sig = mono_metadata_signature_dup_full (cfg->method->klass->image, call->signature);
2197 tmp_sig->param_count -= call->signature->sentinelpos;
2198 tmp_sig->sentinelpos = 0;
2199 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2201 sig_reg = mono_alloc_ireg (cfg);
2202 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2204 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, cinfo->sig_cookie.offset, sig_reg);
2208 static inline LLVMArgStorage
2209 arg_storage_to_llvm_arg_storage (MonoCompile *cfg, ArgStorage storage)
2213 return LLVMArgInIReg;
2216 case ArgGSharedVtInReg:
2217 case ArgGSharedVtOnStack:
2218 return LLVMArgGSharedVt;
2220 g_assert_not_reached ();
2226 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2232 LLVMCallInfo *linfo;
2233 MonoType *t, *sig_ret;
2235 n = sig->param_count + sig->hasthis;
2236 sig_ret = mini_get_underlying_type (sig->ret);
2238 cinfo = get_call_info (cfg->mempool, sig);
2240 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2243 * LLVM always uses the native ABI while we use our own ABI, the
2244 * only difference is the handling of vtypes:
2245 * - we only pass/receive them in registers in some cases, and only
2246 * in 1 or 2 integer registers.
2248 switch (cinfo->ret.storage) {
2250 linfo->ret.storage = LLVMArgNone;
2253 case ArgInFloatSSEReg:
2254 case ArgInDoubleSSEReg:
2255 linfo->ret.storage = LLVMArgNormal;
2257 case ArgValuetypeInReg: {
2258 ainfo = &cinfo->ret;
2261 (ainfo->pair_storage [0] == ArgInFloatSSEReg || ainfo->pair_storage [0] == ArgInDoubleSSEReg ||
2262 ainfo->pair_storage [1] == ArgInFloatSSEReg || ainfo->pair_storage [1] == ArgInDoubleSSEReg)) {
2263 cfg->exception_message = g_strdup ("pinvoke + vtype ret");
2264 cfg->disable_llvm = TRUE;
2268 linfo->ret.storage = LLVMArgVtypeInReg;
2269 for (j = 0; j < 2; ++j)
2270 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2273 case ArgValuetypeAddrInIReg:
2274 /* Vtype returned using a hidden argument */
2275 linfo->ret.storage = LLVMArgVtypeRetAddr;
2276 linfo->vret_arg_index = cinfo->vret_arg_index;
2279 g_assert_not_reached ();
2283 for (i = 0; i < n; ++i) {
2284 ainfo = cinfo->args + i;
2286 if (i >= sig->hasthis)
2287 t = sig->params [i - sig->hasthis];
2289 t = &mono_defaults.int_class->byval_arg;
2291 linfo->args [i].storage = LLVMArgNone;
2293 switch (ainfo->storage) {
2295 linfo->args [i].storage = LLVMArgNormal;
2297 case ArgInDoubleSSEReg:
2298 case ArgInFloatSSEReg:
2299 linfo->args [i].storage = LLVMArgNormal;
2302 if (MONO_TYPE_ISSTRUCT (t))
2303 linfo->args [i].storage = LLVMArgVtypeByVal;
2305 linfo->args [i].storage = LLVMArgNormal;
2307 case ArgValuetypeInReg:
2309 (ainfo->pair_storage [0] == ArgInFloatSSEReg || ainfo->pair_storage [0] == ArgInDoubleSSEReg ||
2310 ainfo->pair_storage [1] == ArgInFloatSSEReg || ainfo->pair_storage [1] == ArgInDoubleSSEReg)) {
2311 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2312 cfg->disable_llvm = TRUE;
2316 linfo->args [i].storage = LLVMArgVtypeInReg;
2317 for (j = 0; j < 2; ++j)
2318 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2320 case ArgGSharedVtInReg:
2321 case ArgGSharedVtOnStack:
2322 linfo->args [i].storage = LLVMArgGSharedVt;
2325 cfg->exception_message = g_strdup ("ainfo->storage");
2326 cfg->disable_llvm = TRUE;
2336 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2339 MonoMethodSignature *sig;
2345 sig = call->signature;
2346 n = sig->param_count + sig->hasthis;
2348 cinfo = get_call_info (cfg->mempool, sig);
2352 if (COMPILE_LLVM (cfg)) {
2353 /* We shouldn't be called in the llvm case */
2354 cfg->disable_llvm = TRUE;
2359 * Emit all arguments which are passed on the stack to prevent register
2360 * allocation problems.
2362 for (i = 0; i < n; ++i) {
2364 ainfo = cinfo->args + i;
2366 in = call->args [i];
2368 if (sig->hasthis && i == 0)
2369 t = &mono_defaults.object_class->byval_arg;
2371 t = sig->params [i - sig->hasthis];
2373 t = mini_get_underlying_type (t);
2374 //XXX what about ArgGSharedVtOnStack here?
2375 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call) {
2377 if (t->type == MONO_TYPE_R4)
2378 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2379 else if (t->type == MONO_TYPE_R8)
2380 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2382 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2384 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2386 if (cfg->compute_gc_maps) {
2389 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2395 * Emit all parameters passed in registers in non-reverse order for better readability
2396 * and to help the optimization in emit_prolog ().
2398 for (i = 0; i < n; ++i) {
2399 ainfo = cinfo->args + i;
2401 in = call->args [i];
2403 if (ainfo->storage == ArgInIReg)
2404 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2407 for (i = n - 1; i >= 0; --i) {
2410 ainfo = cinfo->args + i;
2412 in = call->args [i];
2414 if (sig->hasthis && i == 0)
2415 t = &mono_defaults.object_class->byval_arg;
2417 t = sig->params [i - sig->hasthis];
2418 t = mini_get_underlying_type (t);
2420 switch (ainfo->storage) {
2424 case ArgInFloatSSEReg:
2425 case ArgInDoubleSSEReg:
2426 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2429 case ArgValuetypeInReg:
2430 case ArgValuetypeAddrInIReg:
2431 case ArgGSharedVtInReg:
2432 case ArgGSharedVtOnStack: {
2433 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call)
2434 /* Already emitted above */
2436 //FIXME what about ArgGSharedVtOnStack ?
2437 if (ainfo->storage == ArgOnStack && call->tail_call) {
2438 MonoInst *call_inst = (MonoInst*)call;
2439 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2440 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2448 size = mono_type_native_stack_size (t, &align);
2451 * Other backends use mono_type_stack_size (), but that
2452 * aligns the size to 8, which is larger than the size of
2453 * the source, leading to reads of invalid memory if the
2454 * source is at the end of address space.
2456 size = mono_class_value_size (mono_class_from_mono_type (t), &align);
2459 if (size >= 10000) {
2460 /* Avoid asserts in emit_memcpy () */
2461 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Passing an argument of size '%d'.", size));
2462 /* Continue normally */
2466 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2467 arg->sreg1 = in->dreg;
2468 arg->klass = mono_class_from_mono_type (t);
2469 arg->backend.size = size;
2470 arg->inst_p0 = call;
2471 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2472 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2474 MONO_ADD_INS (cfg->cbb, arg);
2479 g_assert_not_reached ();
2482 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
2483 /* Emit the signature cookie just before the implicit arguments */
2484 emit_sig_cookie (cfg, call, cinfo);
2487 /* Handle the case where there are no implicit arguments */
2488 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2489 emit_sig_cookie (cfg, call, cinfo);
2491 switch (cinfo->ret.storage) {
2492 case ArgValuetypeInReg:
2493 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2495 * Tell the JIT to use a more efficient calling convention: call using
2496 * OP_CALL, compute the result location after the call, and save the
2499 call->vret_in_reg = TRUE;
2501 * Nullify the instruction computing the vret addr to enable
2502 * future optimizations.
2505 NULLIFY_INS (call->vret_var);
2507 if (call->tail_call)
2510 * The valuetype is in RAX:RDX after the call, need to be copied to
2511 * the stack. Push the address here, so the call instruction can
2514 if (!cfg->arch.vret_addr_loc) {
2515 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2516 /* Prevent it from being register allocated or optimized away */
2517 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2520 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2523 case ArgValuetypeAddrInIReg: {
2525 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2526 vtarg->sreg1 = call->vret_var->dreg;
2527 vtarg->dreg = mono_alloc_preg (cfg);
2528 MONO_ADD_INS (cfg->cbb, vtarg);
2530 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2537 if (cfg->method->save_lmf) {
2538 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2539 MONO_ADD_INS (cfg->cbb, arg);
2542 call->stack_usage = cinfo->stack_usage;
2546 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2549 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2550 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2551 int size = ins->backend.size;
2553 switch (ainfo->storage) {
2554 case ArgValuetypeInReg: {
2558 for (part = 0; part < 2; ++part) {
2559 if (ainfo->pair_storage [part] == ArgNone)
2562 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2563 load->inst_basereg = src->dreg;
2564 load->inst_offset = part * sizeof(mgreg_t);
2566 switch (ainfo->pair_storage [part]) {
2568 load->dreg = mono_alloc_ireg (cfg);
2570 case ArgInDoubleSSEReg:
2571 case ArgInFloatSSEReg:
2572 load->dreg = mono_alloc_freg (cfg);
2575 g_assert_not_reached ();
2577 MONO_ADD_INS (cfg->cbb, load);
2579 add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2583 case ArgValuetypeAddrInIReg: {
2584 MonoInst *vtaddr, *load;
2585 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2587 MONO_INST_NEW (cfg, load, OP_LDADDR);
2588 cfg->has_indirection = TRUE;
2589 load->inst_p0 = vtaddr;
2590 vtaddr->flags |= MONO_INST_INDIRECT;
2591 load->type = STACK_MP;
2592 load->klass = vtaddr->klass;
2593 load->dreg = mono_alloc_ireg (cfg);
2594 MONO_ADD_INS (cfg->cbb, load);
2595 mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2597 if (ainfo->pair_storage [0] == ArgInIReg) {
2598 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2599 arg->dreg = mono_alloc_ireg (cfg);
2600 arg->sreg1 = load->dreg;
2602 MONO_ADD_INS (cfg->cbb, arg);
2603 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2605 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, load->dreg);
2609 case ArgGSharedVtInReg:
2611 mono_call_inst_add_outarg_reg (cfg, call, src->dreg, ainfo->reg, FALSE);
2613 case ArgGSharedVtOnStack:
2614 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, src->dreg);
2618 int dreg = mono_alloc_ireg (cfg);
2620 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
2621 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, dreg);
2622 } else if (size <= 40) {
2623 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2625 // FIXME: Code growth
2626 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2629 if (cfg->compute_gc_maps) {
2631 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2637 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2639 MonoType *ret = mini_get_underlying_type (mono_method_signature (method)->ret);
2641 if (ret->type == MONO_TYPE_R4) {
2642 if (COMPILE_LLVM (cfg))
2643 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2645 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2647 } else if (ret->type == MONO_TYPE_R8) {
2648 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2652 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2655 #endif /* DISABLE_JIT */
2657 #define EMIT_COND_BRANCH(ins,cond,sign) \
2658 if (ins->inst_true_bb->native_offset) { \
2659 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2661 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2662 if ((cfg->opt & MONO_OPT_BRANCH) && \
2663 x86_is_imm8 (ins->inst_true_bb->max_offset - offset)) \
2664 x86_branch8 (code, cond, 0, sign); \
2666 x86_branch32 (code, cond, 0, sign); \
2670 MonoMethodSignature *sig;
2675 dyn_call_supported (MonoMethodSignature *sig, CallInfo *cinfo)
2683 switch (cinfo->ret.storage) {
2686 case ArgInFloatSSEReg:
2687 case ArgInDoubleSSEReg:
2689 case ArgValuetypeInReg: {
2690 ArgInfo *ainfo = &cinfo->ret;
2692 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2694 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2702 for (i = 0; i < cinfo->nargs; ++i) {
2703 ArgInfo *ainfo = &cinfo->args [i];
2704 switch (ainfo->storage) {
2706 case ArgInFloatSSEReg:
2707 case ArgInDoubleSSEReg:
2709 case ArgValuetypeInReg:
2710 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2712 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2724 * mono_arch_dyn_call_prepare:
2726 * Return a pointer to an arch-specific structure which contains information
2727 * needed by mono_arch_get_dyn_call_args (). Return NULL if OP_DYN_CALL is not
2728 * supported for SIG.
2729 * This function is equivalent to ffi_prep_cif in libffi.
2732 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2734 ArchDynCallInfo *info;
2737 cinfo = get_call_info (NULL, sig);
2739 if (!dyn_call_supported (sig, cinfo)) {
2744 info = g_new0 (ArchDynCallInfo, 1);
2745 // FIXME: Preprocess the info to speed up get_dyn_call_args ().
2747 info->cinfo = cinfo;
2749 return (MonoDynCallInfo*)info;
2753 * mono_arch_dyn_call_free:
2755 * Free a MonoDynCallInfo structure.
2758 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2760 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2762 g_free (ainfo->cinfo);
2766 #if !defined(__native_client__)
2767 #define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
2768 #define GREG_TO_PTR(greg) (gpointer)(greg)
2770 /* Correctly handle casts to/from 32-bit pointers without compiler warnings */
2771 #define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
2772 #define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
2776 * mono_arch_get_start_dyn_call:
2778 * Convert the arguments ARGS to a format which can be passed to OP_DYN_CALL, and
2779 * store the result into BUF.
2780 * ARGS should be an array of pointers pointing to the arguments.
2781 * RET should point to a memory buffer large enought to hold the result of the
2783 * This function should be as fast as possible, any work which does not depend
2784 * on the actual values of the arguments should be done in
2785 * mono_arch_dyn_call_prepare ().
2786 * start_dyn_call + OP_DYN_CALL + finish_dyn_call is equivalent to ffi_call in
2790 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2792 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2793 DynCallArgs *p = (DynCallArgs*)buf;
2794 int arg_index, greg, freg, i, pindex;
2795 MonoMethodSignature *sig = dinfo->sig;
2796 int buffer_offset = 0;
2798 g_assert (buf_len >= sizeof (DynCallArgs));
2808 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2809 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2814 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg)
2815 p->regs [greg ++] = PTR_TO_GREG(ret);
2817 for (i = pindex; i < sig->param_count; i++) {
2818 MonoType *t = mini_get_underlying_type (sig->params [i]);
2819 gpointer *arg = args [arg_index ++];
2822 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2827 case MONO_TYPE_STRING:
2828 case MONO_TYPE_CLASS:
2829 case MONO_TYPE_ARRAY:
2830 case MONO_TYPE_SZARRAY:
2831 case MONO_TYPE_OBJECT:
2835 #if !defined(__mono_ilp32__)
2839 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2840 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2842 #if defined(__mono_ilp32__)
2845 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2846 p->regs [greg ++] = *(guint64*)(arg);
2850 p->regs [greg ++] = *(guint8*)(arg);
2853 p->regs [greg ++] = *(gint8*)(arg);
2856 p->regs [greg ++] = *(gint16*)(arg);
2859 p->regs [greg ++] = *(guint16*)(arg);
2862 p->regs [greg ++] = *(gint32*)(arg);
2865 p->regs [greg ++] = *(guint32*)(arg);
2867 case MONO_TYPE_R4: {
2870 *(float*)&d = *(float*)(arg);
2872 p->fregs [freg ++] = d;
2877 p->fregs [freg ++] = *(double*)(arg);
2879 case MONO_TYPE_GENERICINST:
2880 if (MONO_TYPE_IS_REFERENCE (t)) {
2881 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2883 } else if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
2884 MonoClass *klass = mono_class_from_mono_type (t);
2885 guint8 *nullable_buf;
2888 size = mono_class_value_size (klass, NULL);
2889 nullable_buf = p->buffer + buffer_offset;
2890 buffer_offset += size;
2891 g_assert (buffer_offset <= 256);
2893 /* The argument pointed to by arg is either a boxed vtype or null */
2894 mono_nullable_init (nullable_buf, (MonoObject*)arg, klass);
2896 arg = (gpointer*)nullable_buf;
2902 case MONO_TYPE_VALUETYPE: {
2903 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2905 g_assert (ainfo->storage == ArgValuetypeInReg);
2906 if (ainfo->pair_storage [0] != ArgNone) {
2907 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2908 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2910 if (ainfo->pair_storage [1] != ArgNone) {
2911 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2912 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2917 g_assert_not_reached ();
2921 g_assert (greg <= PARAM_REGS);
2925 * mono_arch_finish_dyn_call:
2927 * Store the result of a dyn call into the return value buffer passed to
2928 * start_dyn_call ().
2929 * This function should be as fast as possible, any work which does not depend
2930 * on the actual values of the arguments should be done in
2931 * mono_arch_dyn_call_prepare ().
2934 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2936 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2937 MonoMethodSignature *sig = dinfo->sig;
2938 DynCallArgs *dargs = (DynCallArgs*)buf;
2939 guint8 *ret = dargs->ret;
2940 mgreg_t res = dargs->res;
2941 MonoType *sig_ret = mini_get_underlying_type (sig->ret);
2943 switch (sig_ret->type) {
2944 case MONO_TYPE_VOID:
2945 *(gpointer*)ret = NULL;
2947 case MONO_TYPE_STRING:
2948 case MONO_TYPE_CLASS:
2949 case MONO_TYPE_ARRAY:
2950 case MONO_TYPE_SZARRAY:
2951 case MONO_TYPE_OBJECT:
2955 *(gpointer*)ret = GREG_TO_PTR(res);
2961 *(guint8*)ret = res;
2964 *(gint16*)ret = res;
2967 *(guint16*)ret = res;
2970 *(gint32*)ret = res;
2973 *(guint32*)ret = res;
2976 *(gint64*)ret = res;
2979 *(guint64*)ret = res;
2982 *(float*)ret = *(float*)&(dargs->fregs [0]);
2985 *(double*)ret = dargs->fregs [0];
2987 case MONO_TYPE_GENERICINST:
2988 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
2989 *(gpointer*)ret = GREG_TO_PTR(res);
2994 case MONO_TYPE_VALUETYPE:
2995 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg) {
2998 ArgInfo *ainfo = &dinfo->cinfo->ret;
3000 g_assert (ainfo->storage == ArgValuetypeInReg);
3002 if (ainfo->pair_storage [0] != ArgNone) {
3003 g_assert (ainfo->pair_storage [0] == ArgInIReg);
3004 ((mgreg_t*)ret)[0] = res;
3007 g_assert (ainfo->pair_storage [1] == ArgNone);
3011 g_assert_not_reached ();
3015 /* emit an exception if condition is fail */
3016 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
3018 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
3019 if (tins == NULL) { \
3020 mono_add_patch_info (cfg, code - cfg->native_code, \
3021 MONO_PATCH_INFO_EXC, exc_name); \
3022 x86_branch32 (code, cond, 0, signed); \
3024 EMIT_COND_BRANCH (tins, cond, signed); \
3028 #define EMIT_FPCOMPARE(code) do { \
3029 amd64_fcompp (code); \
3030 amd64_fnstsw (code); \
3033 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
3034 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
3035 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
3036 amd64_ ##op (code); \
3037 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
3038 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
3042 emit_call_body (MonoCompile *cfg, guint8 *code, MonoJumpInfoType patch_type, gconstpointer data)
3044 gboolean no_patch = FALSE;
3047 * FIXME: Add support for thunks
3050 gboolean near_call = FALSE;
3053 * Indirect calls are expensive so try to make a near call if possible.
3054 * The caller memory is allocated by the code manager so it is
3055 * guaranteed to be at a 32 bit offset.
3058 if (patch_type != MONO_PATCH_INFO_ABS) {
3059 /* The target is in memory allocated using the code manager */
3062 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
3063 if (((MonoMethod*)data)->klass->image->aot_module)
3064 /* The callee might be an AOT method */
3066 if (((MonoMethod*)data)->dynamic)
3067 /* The target is in malloc-ed memory */
3071 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
3073 * The call might go directly to a native function without
3076 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ((const char *)data);
3078 gconstpointer target = mono_icall_get_wrapper (mi);
3079 if ((((guint64)target) >> 32) != 0)
3085 MonoJumpInfo *jinfo = NULL;
3087 if (cfg->abs_patches)
3088 jinfo = (MonoJumpInfo *)g_hash_table_lookup (cfg->abs_patches, data);
3090 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3091 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
3092 if (mi && (((guint64)mi->func) >> 32) == 0)
3097 * This is not really an optimization, but required because the
3098 * generic class init trampolines use R11 to pass the vtable.
3103 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
3105 if (info->func == info->wrapper) {
3107 if ((((guint64)info->func) >> 32) == 0)
3111 /* See the comment in mono_codegen () */
3112 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
3116 else if ((((guint64)data) >> 32) == 0) {
3123 if (cfg->method->dynamic)
3124 /* These methods are allocated using malloc */
3127 #ifdef MONO_ARCH_NOMAP32BIT
3130 #if defined(__native_client__)
3131 /* Always use near_call == TRUE for Native Client */
3134 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
3135 if (optimize_for_xen)
3138 if (cfg->compile_aot) {
3145 * Align the call displacement to an address divisible by 4 so it does
3146 * not span cache lines. This is required for code patching to work on SMP
3149 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
3150 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
3151 amd64_padding (code, pad_size);
3153 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3154 amd64_call_code (code, 0);
3157 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3158 amd64_set_reg_template (code, GP_SCRATCH_REG);
3159 amd64_call_reg (code, GP_SCRATCH_REG);
3166 static inline guint8*
3167 emit_call (MonoCompile *cfg, guint8 *code, MonoJumpInfoType patch_type, gconstpointer data, gboolean win64_adjust_stack)
3170 if (win64_adjust_stack)
3171 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
3173 code = emit_call_body (cfg, code, patch_type, data);
3175 if (win64_adjust_stack)
3176 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
3183 store_membase_imm_to_store_membase_reg (int opcode)
3186 case OP_STORE_MEMBASE_IMM:
3187 return OP_STORE_MEMBASE_REG;
3188 case OP_STOREI4_MEMBASE_IMM:
3189 return OP_STOREI4_MEMBASE_REG;
3190 case OP_STOREI8_MEMBASE_IMM:
3191 return OP_STOREI8_MEMBASE_REG;
3199 #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)))
3202 * mono_arch_peephole_pass_1:
3204 * Perform peephole opts which should/can be performed before local regalloc
3207 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3211 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3212 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3214 switch (ins->opcode) {
3218 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
3220 * X86_LEA is like ADD, but doesn't have the
3221 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
3222 * its operand to 64 bit.
3224 ins->opcode = OP_X86_LEA_MEMBASE;
3225 ins->inst_basereg = ins->sreg1;
3230 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3234 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3235 * the latter has length 2-3 instead of 6 (reverse constant
3236 * propagation). These instruction sequences are very common
3237 * in the initlocals bblock.
3239 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3240 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)) {
3241 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3242 ins2->sreg1 = ins->dreg;
3243 } 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)) {
3245 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3248 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3256 case OP_COMPARE_IMM:
3257 case OP_LCOMPARE_IMM:
3258 /* OP_COMPARE_IMM (reg, 0)
3260 * OP_AMD64_TEST_NULL (reg)
3263 ins->opcode = OP_AMD64_TEST_NULL;
3265 case OP_ICOMPARE_IMM:
3267 ins->opcode = OP_X86_TEST_NULL;
3269 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3271 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3272 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3274 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3275 * OP_COMPARE_IMM reg, imm
3277 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3279 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3280 ins->inst_basereg == last_ins->inst_destbasereg &&
3281 ins->inst_offset == last_ins->inst_offset) {
3282 ins->opcode = OP_ICOMPARE_IMM;
3283 ins->sreg1 = last_ins->sreg1;
3285 /* check if we can remove cmp reg,0 with test null */
3287 ins->opcode = OP_X86_TEST_NULL;
3293 mono_peephole_ins (bb, ins);
3298 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3302 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3303 switch (ins->opcode) {
3306 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3307 /* reg = 0 -> XOR (reg, reg) */
3308 /* XOR sets cflags on x86, so we cant do it always */
3309 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3310 ins->opcode = OP_LXOR;
3311 ins->sreg1 = ins->dreg;
3312 ins->sreg2 = ins->dreg;
3320 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3321 * 0 result into 64 bits.
3323 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3324 ins->opcode = OP_IXOR;
3328 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3332 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3333 * the latter has length 2-3 instead of 6 (reverse constant
3334 * propagation). These instruction sequences are very common
3335 * in the initlocals bblock.
3337 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3338 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)) {
3339 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3340 ins2->sreg1 = ins->dreg;
3341 } 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) || (ins2->opcode == OP_LIVERANGE_START) || (ins2->opcode == OP_GC_LIVENESS_DEF) || (ins2->opcode == OP_GC_LIVENESS_USE)) {
3343 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3346 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3355 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3356 ins->opcode = OP_X86_INC_REG;
3359 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3360 ins->opcode = OP_X86_DEC_REG;
3364 mono_peephole_ins (bb, ins);
3368 #define NEW_INS(cfg,ins,dest,op) do { \
3369 MONO_INST_NEW ((cfg), (dest), (op)); \
3370 (dest)->cil_code = (ins)->cil_code; \
3371 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3375 * mono_arch_lowering_pass:
3377 * Converts complex opcodes into simpler ones so that each IR instruction
3378 * corresponds to one machine instruction.
3381 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3383 MonoInst *ins, *n, *temp;
3386 * FIXME: Need to add more instructions, but the current machine
3387 * description can't model some parts of the composite instructions like
3390 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3391 switch (ins->opcode) {
3395 case OP_IDIV_UN_IMM:
3396 case OP_IREM_UN_IMM:
3399 mono_decompose_op_imm (cfg, bb, ins);
3401 case OP_COMPARE_IMM:
3402 case OP_LCOMPARE_IMM:
3403 if (!amd64_use_imm32 (ins->inst_imm)) {
3404 NEW_INS (cfg, ins, temp, OP_I8CONST);
3405 temp->inst_c0 = ins->inst_imm;
3406 temp->dreg = mono_alloc_ireg (cfg);
3407 ins->opcode = OP_COMPARE;
3408 ins->sreg2 = temp->dreg;
3411 #ifndef __mono_ilp32__
3412 case OP_LOAD_MEMBASE:
3414 case OP_LOADI8_MEMBASE:
3415 #ifndef __native_client_codegen__
3416 /* Don't generate memindex opcodes (to simplify */
3417 /* read sandboxing) */
3418 if (!amd64_use_imm32 (ins->inst_offset)) {
3419 NEW_INS (cfg, ins, temp, OP_I8CONST);
3420 temp->inst_c0 = ins->inst_offset;
3421 temp->dreg = mono_alloc_ireg (cfg);
3422 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3423 ins->inst_indexreg = temp->dreg;
3427 #ifndef __mono_ilp32__
3428 case OP_STORE_MEMBASE_IMM:
3430 case OP_STOREI8_MEMBASE_IMM:
3431 if (!amd64_use_imm32 (ins->inst_imm)) {
3432 NEW_INS (cfg, ins, temp, OP_I8CONST);
3433 temp->inst_c0 = ins->inst_imm;
3434 temp->dreg = mono_alloc_ireg (cfg);
3435 ins->opcode = OP_STOREI8_MEMBASE_REG;
3436 ins->sreg1 = temp->dreg;
3439 #ifdef MONO_ARCH_SIMD_INTRINSICS
3440 case OP_EXPAND_I1: {
3441 int temp_reg1 = mono_alloc_ireg (cfg);
3442 int temp_reg2 = mono_alloc_ireg (cfg);
3443 int original_reg = ins->sreg1;
3445 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3446 temp->sreg1 = original_reg;
3447 temp->dreg = temp_reg1;
3449 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3450 temp->sreg1 = temp_reg1;
3451 temp->dreg = temp_reg2;
3454 NEW_INS (cfg, ins, temp, OP_LOR);
3455 temp->sreg1 = temp->dreg = temp_reg2;
3456 temp->sreg2 = temp_reg1;
3458 ins->opcode = OP_EXPAND_I2;
3459 ins->sreg1 = temp_reg2;
3468 bb->max_vreg = cfg->next_vreg;
3472 branch_cc_table [] = {
3473 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3474 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3475 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3478 /* Maps CMP_... constants to X86_CC_... constants */
3481 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3482 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3486 cc_signed_table [] = {
3487 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3488 FALSE, FALSE, FALSE, FALSE
3491 /*#include "cprop.c"*/
3493 static unsigned char*
3494 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3497 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3499 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3502 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3504 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3508 static unsigned char*
3509 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3511 int sreg = tree->sreg1;
3512 int need_touch = FALSE;
3514 #if defined(TARGET_WIN32)
3516 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3517 if (!tree->flags & MONO_INST_INIT)
3526 * If requested stack size is larger than one page,
3527 * perform stack-touch operation
3530 * Generate stack probe code.
3531 * Under Windows, it is necessary to allocate one page at a time,
3532 * "touching" stack after each successful sub-allocation. This is
3533 * because of the way stack growth is implemented - there is a
3534 * guard page before the lowest stack page that is currently commited.
3535 * Stack normally grows sequentially so OS traps access to the
3536 * guard page and commits more pages when needed.
3538 amd64_test_reg_imm (code, sreg, ~0xFFF);
3539 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3541 br[2] = code; /* loop */
3542 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3543 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3544 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3545 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3546 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3547 amd64_patch (br[3], br[2]);
3548 amd64_test_reg_reg (code, sreg, sreg);
3549 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3550 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3552 br[1] = code; x86_jump8 (code, 0);
3554 amd64_patch (br[0], code);
3555 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3556 amd64_patch (br[1], code);
3557 amd64_patch (br[4], code);
3560 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3562 if (tree->flags & MONO_INST_INIT) {
3564 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3565 amd64_push_reg (code, AMD64_RAX);
3568 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3569 amd64_push_reg (code, AMD64_RCX);
3572 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3573 amd64_push_reg (code, AMD64_RDI);
3577 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3578 if (sreg != AMD64_RCX)
3579 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3580 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3582 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3583 if (cfg->param_area)
3584 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3586 #if defined(__default_codegen__)
3587 amd64_prefix (code, X86_REP_PREFIX);
3589 #elif defined(__native_client_codegen__)
3590 /* NaCl stos pseudo-instruction */
3591 amd64_codegen_pre(code);
3592 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3593 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3594 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3595 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3596 amd64_prefix (code, X86_REP_PREFIX);
3598 amd64_codegen_post(code);
3599 #endif /* __native_client_codegen__ */
3601 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3602 amd64_pop_reg (code, AMD64_RDI);
3603 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3604 amd64_pop_reg (code, AMD64_RCX);
3605 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3606 amd64_pop_reg (code, AMD64_RAX);
3612 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3617 /* Move return value to the target register */
3618 /* FIXME: do this in the local reg allocator */
3619 switch (ins->opcode) {
3622 case OP_CALL_MEMBASE:
3625 case OP_LCALL_MEMBASE:
3626 g_assert (ins->dreg == AMD64_RAX);
3630 case OP_FCALL_MEMBASE: {
3631 MonoType *rtype = mini_get_underlying_type (((MonoCallInst*)ins)->signature->ret);
3632 if (rtype->type == MONO_TYPE_R4) {
3633 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3636 if (ins->dreg != AMD64_XMM0)
3637 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3643 case OP_RCALL_MEMBASE:
3644 if (ins->dreg != AMD64_XMM0)
3645 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3649 case OP_VCALL_MEMBASE:
3652 case OP_VCALL2_MEMBASE:
3653 cinfo = get_call_info (cfg->mempool, ((MonoCallInst*)ins)->signature);
3654 if (cinfo->ret.storage == ArgValuetypeInReg) {
3655 MonoInst *loc = (MonoInst *)cfg->arch.vret_addr_loc;
3657 /* Load the destination address */
3658 g_assert (loc->opcode == OP_REGOFFSET);
3659 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3661 for (quad = 0; quad < 2; quad ++) {
3662 switch (cinfo->ret.pair_storage [quad]) {
3664 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3666 case ArgInFloatSSEReg:
3667 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3669 case ArgInDoubleSSEReg:
3670 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3685 #endif /* DISABLE_JIT */
3688 static int tls_gs_offset;
3692 mono_amd64_have_tls_get (void)
3695 static gboolean have_tls_get = FALSE;
3696 static gboolean inited = FALSE;
3699 return have_tls_get;
3701 #if MONO_HAVE_FAST_TLS
3702 guint8 *ins = (guint8*)pthread_getspecific;
3705 * We're looking for these two instructions:
3707 * mov %gs:[offset](,%rdi,8),%rax
3710 have_tls_get = ins [0] == 0x65 &&
3720 tls_gs_offset = ins[5];
3723 * Apple now loads a different version of pthread_getspecific when launched from Xcode
3724 * For that version we're looking for these instructions:
3728 * mov %gs:[offset](,%rdi,8),%rax
3732 if (!have_tls_get) {
3733 have_tls_get = ins [0] == 0x55 &&
3748 tls_gs_offset = ins[9];
3754 return have_tls_get;
3755 #elif defined(TARGET_ANDROID)
3763 mono_amd64_get_tls_gs_offset (void)
3766 return tls_gs_offset;
3768 g_assert_not_reached ();
3774 * mono_amd64_emit_tls_get:
3775 * @code: buffer to store code to
3776 * @dreg: hard register where to place the result
3777 * @tls_offset: offset info
3779 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3780 * the dreg register the item in the thread local storage identified
3783 * Returns: a pointer to the end of the stored code
3786 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3789 if (tls_offset < 64) {
3790 x86_prefix (code, X86_GS_PREFIX);
3791 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3795 g_assert (tls_offset < 0x440);
3796 /* Load TEB->TlsExpansionSlots */
3797 x86_prefix (code, X86_GS_PREFIX);
3798 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3799 amd64_test_reg_reg (code, dreg, dreg);
3801 amd64_branch (code, X86_CC_EQ, code, TRUE);
3802 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3803 amd64_patch (buf [0], code);
3805 #elif defined(__APPLE__)
3806 x86_prefix (code, X86_GS_PREFIX);
3807 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3809 if (optimize_for_xen) {
3810 x86_prefix (code, X86_FS_PREFIX);
3811 amd64_mov_reg_mem (code, dreg, 0, 8);
3812 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3814 x86_prefix (code, X86_FS_PREFIX);
3815 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3822 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3824 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3826 if (dreg != offset_reg)
3827 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3828 amd64_prefix (code, X86_GS_PREFIX);
3829 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3830 #elif defined(__linux__)
3833 if (dreg == offset_reg) {
3834 /* Use a temporary reg by saving it to the redzone */
3835 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3836 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3837 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3838 offset_reg = tmpreg;
3840 x86_prefix (code, X86_FS_PREFIX);
3841 amd64_mov_reg_mem (code, dreg, 0, 8);
3842 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3844 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3846 g_assert_not_reached ();
3852 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3855 g_assert_not_reached ();
3856 #elif defined(__APPLE__)
3857 x86_prefix (code, X86_GS_PREFIX);
3858 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3860 g_assert (!optimize_for_xen);
3861 x86_prefix (code, X86_FS_PREFIX);
3862 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3868 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3870 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3872 g_assert_not_reached ();
3873 #elif defined(__APPLE__)
3874 x86_prefix (code, X86_GS_PREFIX);
3875 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3877 x86_prefix (code, X86_FS_PREFIX);
3878 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3884 * mono_arch_translate_tls_offset:
3886 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3889 mono_arch_translate_tls_offset (int offset)
3892 return tls_gs_offset + (offset * 8);
3901 * Emit code to initialize an LMF structure at LMF_OFFSET.
3904 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3907 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3910 * sp is saved right before calls but we need to save it here too so
3911 * async stack walks would work.
3913 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3915 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3916 if (cfg->arch.omit_fp && cfa_offset != -1)
3917 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3919 /* These can't contain refs */
3920 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3921 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3922 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3923 /* These are handled automatically by the stack marking code */
3924 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3929 #define REAL_PRINT_REG(text,reg) \
3930 mono_assert (reg >= 0); \
3931 amd64_push_reg (code, AMD64_RAX); \
3932 amd64_push_reg (code, AMD64_RDX); \
3933 amd64_push_reg (code, AMD64_RCX); \
3934 amd64_push_reg (code, reg); \
3935 amd64_push_imm (code, reg); \
3936 amd64_push_imm (code, text " %d %p\n"); \
3937 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
3938 amd64_call_reg (code, AMD64_RAX); \
3939 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
3940 amd64_pop_reg (code, AMD64_RCX); \
3941 amd64_pop_reg (code, AMD64_RDX); \
3942 amd64_pop_reg (code, AMD64_RAX);
3944 /* benchmark and set based on cpu */
3945 #define LOOP_ALIGNMENT 8
3946 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3950 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3955 guint8 *code = cfg->native_code + cfg->code_len;
3958 /* Fix max_offset estimate for each successor bb */
3959 if (cfg->opt & MONO_OPT_BRANCH) {
3960 int current_offset = cfg->code_len;
3961 MonoBasicBlock *current_bb;
3962 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3963 current_bb->max_offset = current_offset;
3964 current_offset += current_bb->max_length;
3968 if (cfg->opt & MONO_OPT_LOOP) {
3969 int pad, align = LOOP_ALIGNMENT;
3970 /* set alignment depending on cpu */
3971 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3973 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3974 amd64_padding (code, pad);
3975 cfg->code_len += pad;
3976 bb->native_offset = cfg->code_len;
3980 #if defined(__native_client_codegen__)
3981 /* For Native Client, all indirect call/jump targets must be */
3982 /* 32-byte aligned. Exception handler blocks are jumped to */
3983 /* indirectly as well. */
3984 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
3985 (bb->flags & BB_EXCEPTION_HANDLER);
3987 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
3988 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
3989 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
3990 cfg->code_len += pad;
3991 bb->native_offset = cfg->code_len;
3993 #endif /*__native_client_codegen__*/
3995 if (cfg->verbose_level > 2)
3996 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3998 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
3999 MonoProfileCoverageInfo *cov = cfg->coverage_info;
4000 g_assert (!cfg->compile_aot);
4002 cov->data [bb->dfn].cil_code = bb->cil_code;
4003 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
4004 /* this is not thread save, but good enough */
4005 amd64_inc_membase (code, AMD64_R11, 0);
4008 offset = code - cfg->native_code;
4010 mono_debug_open_block (cfg, bb, offset);
4012 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)
4013 x86_breakpoint (code);
4015 MONO_BB_FOR_EACH_INS (bb, ins) {
4016 offset = code - cfg->native_code;
4018 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4020 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
4022 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
4023 cfg->code_size *= 2;
4024 cfg->native_code = (unsigned char *)mono_realloc_native_code(cfg);
4025 code = cfg->native_code + offset;
4026 cfg->stat_code_reallocs++;
4029 if (cfg->debug_info)
4030 mono_debug_record_line_number (cfg, ins, offset);
4032 switch (ins->opcode) {
4034 amd64_mul_reg (code, ins->sreg2, TRUE);
4037 amd64_mul_reg (code, ins->sreg2, FALSE);
4039 case OP_X86_SETEQ_MEMBASE:
4040 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
4042 case OP_STOREI1_MEMBASE_IMM:
4043 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
4045 case OP_STOREI2_MEMBASE_IMM:
4046 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
4048 case OP_STOREI4_MEMBASE_IMM:
4049 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
4051 case OP_STOREI1_MEMBASE_REG:
4052 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
4054 case OP_STOREI2_MEMBASE_REG:
4055 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
4057 /* In AMD64 NaCl, pointers are 4 bytes, */
4058 /* so STORE_* != STOREI8_*. Likewise below. */
4059 case OP_STORE_MEMBASE_REG:
4060 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
4062 case OP_STOREI8_MEMBASE_REG:
4063 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
4065 case OP_STOREI4_MEMBASE_REG:
4066 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
4068 case OP_STORE_MEMBASE_IMM:
4069 #ifndef __native_client_codegen__
4070 /* In NaCl, this could be a PCONST type, which could */
4071 /* mean a pointer type was copied directly into the */
4072 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
4073 /* the value would be 0x00000000FFFFFFFF which is */
4074 /* not proper for an imm32 unless you cast it. */
4075 g_assert (amd64_is_imm32 (ins->inst_imm));
4077 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
4079 case OP_STOREI8_MEMBASE_IMM:
4080 g_assert (amd64_is_imm32 (ins->inst_imm));
4081 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
4084 #ifdef __mono_ilp32__
4085 /* In ILP32, pointers are 4 bytes, so separate these */
4086 /* cases, use literal 8 below where we really want 8 */
4087 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4088 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
4092 // FIXME: Decompose this earlier
4093 if (amd64_use_imm32 (ins->inst_imm))
4094 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
4096 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4097 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
4101 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4102 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
4105 // FIXME: Decompose this earlier
4106 if (amd64_use_imm32 (ins->inst_imm))
4107 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
4109 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4110 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
4114 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4115 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
4118 /* For NaCl, pointers are 4 bytes, so separate these */
4119 /* cases, use literal 8 below where we really want 8 */
4120 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4121 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
4123 case OP_LOAD_MEMBASE:
4124 g_assert (amd64_is_imm32 (ins->inst_offset));
4125 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
4127 case OP_LOADI8_MEMBASE:
4128 /* Use literal 8 instead of sizeof pointer or */
4129 /* register, we really want 8 for this opcode */
4130 g_assert (amd64_is_imm32 (ins->inst_offset));
4131 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
4133 case OP_LOADI4_MEMBASE:
4134 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4136 case OP_LOADU4_MEMBASE:
4137 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
4139 case OP_LOADU1_MEMBASE:
4140 /* The cpu zero extends the result into 64 bits */
4141 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
4143 case OP_LOADI1_MEMBASE:
4144 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
4146 case OP_LOADU2_MEMBASE:
4147 /* The cpu zero extends the result into 64 bits */
4148 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
4150 case OP_LOADI2_MEMBASE:
4151 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
4153 case OP_AMD64_LOADI8_MEMINDEX:
4154 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
4156 case OP_LCONV_TO_I1:
4157 case OP_ICONV_TO_I1:
4159 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
4161 case OP_LCONV_TO_I2:
4162 case OP_ICONV_TO_I2:
4164 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
4166 case OP_LCONV_TO_U1:
4167 case OP_ICONV_TO_U1:
4168 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
4170 case OP_LCONV_TO_U2:
4171 case OP_ICONV_TO_U2:
4172 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
4175 /* Clean out the upper word */
4176 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4179 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
4183 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4185 case OP_COMPARE_IMM:
4186 #if defined(__mono_ilp32__)
4187 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
4188 g_assert (amd64_is_imm32 (ins->inst_imm));
4189 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4192 case OP_LCOMPARE_IMM:
4193 g_assert (amd64_is_imm32 (ins->inst_imm));
4194 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
4196 case OP_X86_COMPARE_REG_MEMBASE:
4197 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
4199 case OP_X86_TEST_NULL:
4200 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
4202 case OP_AMD64_TEST_NULL:
4203 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4206 case OP_X86_ADD_REG_MEMBASE:
4207 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4209 case OP_X86_SUB_REG_MEMBASE:
4210 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4212 case OP_X86_AND_REG_MEMBASE:
4213 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4215 case OP_X86_OR_REG_MEMBASE:
4216 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4218 case OP_X86_XOR_REG_MEMBASE:
4219 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4222 case OP_X86_ADD_MEMBASE_IMM:
4223 /* FIXME: Make a 64 version too */
4224 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4226 case OP_X86_SUB_MEMBASE_IMM:
4227 g_assert (amd64_is_imm32 (ins->inst_imm));
4228 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4230 case OP_X86_AND_MEMBASE_IMM:
4231 g_assert (amd64_is_imm32 (ins->inst_imm));
4232 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4234 case OP_X86_OR_MEMBASE_IMM:
4235 g_assert (amd64_is_imm32 (ins->inst_imm));
4236 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4238 case OP_X86_XOR_MEMBASE_IMM:
4239 g_assert (amd64_is_imm32 (ins->inst_imm));
4240 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4242 case OP_X86_ADD_MEMBASE_REG:
4243 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4245 case OP_X86_SUB_MEMBASE_REG:
4246 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4248 case OP_X86_AND_MEMBASE_REG:
4249 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4251 case OP_X86_OR_MEMBASE_REG:
4252 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4254 case OP_X86_XOR_MEMBASE_REG:
4255 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4257 case OP_X86_INC_MEMBASE:
4258 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4260 case OP_X86_INC_REG:
4261 amd64_inc_reg_size (code, ins->dreg, 4);
4263 case OP_X86_DEC_MEMBASE:
4264 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4266 case OP_X86_DEC_REG:
4267 amd64_dec_reg_size (code, ins->dreg, 4);
4269 case OP_X86_MUL_REG_MEMBASE:
4270 case OP_X86_MUL_MEMBASE_REG:
4271 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4273 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4274 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4276 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4277 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4279 case OP_AMD64_COMPARE_MEMBASE_REG:
4280 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4282 case OP_AMD64_COMPARE_MEMBASE_IMM:
4283 g_assert (amd64_is_imm32 (ins->inst_imm));
4284 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4286 case OP_X86_COMPARE_MEMBASE8_IMM:
4287 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4289 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4290 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4292 case OP_AMD64_COMPARE_REG_MEMBASE:
4293 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4296 case OP_AMD64_ADD_REG_MEMBASE:
4297 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4299 case OP_AMD64_SUB_REG_MEMBASE:
4300 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4302 case OP_AMD64_AND_REG_MEMBASE:
4303 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4305 case OP_AMD64_OR_REG_MEMBASE:
4306 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4308 case OP_AMD64_XOR_REG_MEMBASE:
4309 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4312 case OP_AMD64_ADD_MEMBASE_REG:
4313 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4315 case OP_AMD64_SUB_MEMBASE_REG:
4316 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4318 case OP_AMD64_AND_MEMBASE_REG:
4319 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4321 case OP_AMD64_OR_MEMBASE_REG:
4322 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4324 case OP_AMD64_XOR_MEMBASE_REG:
4325 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4328 case OP_AMD64_ADD_MEMBASE_IMM:
4329 g_assert (amd64_is_imm32 (ins->inst_imm));
4330 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4332 case OP_AMD64_SUB_MEMBASE_IMM:
4333 g_assert (amd64_is_imm32 (ins->inst_imm));
4334 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4336 case OP_AMD64_AND_MEMBASE_IMM:
4337 g_assert (amd64_is_imm32 (ins->inst_imm));
4338 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4340 case OP_AMD64_OR_MEMBASE_IMM:
4341 g_assert (amd64_is_imm32 (ins->inst_imm));
4342 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4344 case OP_AMD64_XOR_MEMBASE_IMM:
4345 g_assert (amd64_is_imm32 (ins->inst_imm));
4346 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4350 amd64_breakpoint (code);
4352 case OP_RELAXED_NOP:
4353 x86_prefix (code, X86_REP_PREFIX);
4361 case OP_DUMMY_STORE:
4362 case OP_DUMMY_ICONST:
4363 case OP_DUMMY_R8CONST:
4364 case OP_NOT_REACHED:
4367 case OP_IL_SEQ_POINT:
4368 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4370 case OP_SEQ_POINT: {
4371 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4372 MonoInst *var = (MonoInst *)cfg->arch.ss_tramp_var;
4375 /* Load ss_tramp_var */
4376 /* This is equal to &ss_trampoline */
4377 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4378 /* Load the trampoline address */
4379 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4380 /* Call it if it is non-null */
4381 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4383 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4384 amd64_call_reg (code, AMD64_R11);
4385 amd64_patch (label, code);
4389 * This is the address which is saved in seq points,
4391 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4393 if (cfg->compile_aot) {
4394 guint32 offset = code - cfg->native_code;
4396 MonoInst *info_var = (MonoInst *)cfg->arch.seq_point_info_var;
4400 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4401 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4402 /* Load the info->bp_addrs [offset], which is either NULL or the address of the breakpoint trampoline */
4403 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4404 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4406 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4407 /* Call the trampoline */
4408 amd64_call_reg (code, AMD64_R11);
4409 amd64_patch (label, code);
4411 MonoInst *var = (MonoInst *)cfg->arch.bp_tramp_var;
4415 * Emit a test+branch against a constant, the constant will be overwritten
4416 * by mono_arch_set_breakpoint () to cause the test to fail.
4418 amd64_mov_reg_imm (code, AMD64_R11, 0);
4419 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4421 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4424 g_assert (var->opcode == OP_REGOFFSET);
4425 /* Load bp_tramp_var */
4426 /* This is equal to &bp_trampoline */
4427 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4428 /* Call the trampoline */
4429 amd64_call_membase (code, AMD64_R11, 0);
4430 amd64_patch (label, code);
4433 * Add an additional nop so skipping the bp doesn't cause the ip to point
4434 * to another IL offset.
4442 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4445 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4449 g_assert (amd64_is_imm32 (ins->inst_imm));
4450 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4453 g_assert (amd64_is_imm32 (ins->inst_imm));
4454 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4459 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4462 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4466 g_assert (amd64_is_imm32 (ins->inst_imm));
4467 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4470 g_assert (amd64_is_imm32 (ins->inst_imm));
4471 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4474 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4478 g_assert (amd64_is_imm32 (ins->inst_imm));
4479 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4482 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4487 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4489 switch (ins->inst_imm) {
4493 if (ins->dreg != ins->sreg1)
4494 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4495 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4498 /* LEA r1, [r2 + r2*2] */
4499 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4502 /* LEA r1, [r2 + r2*4] */
4503 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4506 /* LEA r1, [r2 + r2*2] */
4508 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4509 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4512 /* LEA r1, [r2 + r2*8] */
4513 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4516 /* LEA r1, [r2 + r2*4] */
4518 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4519 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4522 /* LEA r1, [r2 + r2*2] */
4524 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4525 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4528 /* LEA r1, [r2 + r2*4] */
4529 /* LEA r1, [r1 + r1*4] */
4530 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4531 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4534 /* LEA r1, [r2 + r2*4] */
4536 /* LEA r1, [r1 + r1*4] */
4537 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4538 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4539 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4542 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
4549 #if defined( __native_client_codegen__ )
4550 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4551 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4553 /* Regalloc magic makes the div/rem cases the same */
4554 if (ins->sreg2 == AMD64_RDX) {
4555 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4557 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4560 amd64_div_reg (code, ins->sreg2, TRUE);
4565 #if defined( __native_client_codegen__ )
4566 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4567 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4569 if (ins->sreg2 == AMD64_RDX) {
4570 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4571 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4572 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4574 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4575 amd64_div_reg (code, ins->sreg2, FALSE);
4580 #if defined( __native_client_codegen__ )
4581 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4582 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4584 if (ins->sreg2 == AMD64_RDX) {
4585 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4586 amd64_cdq_size (code, 4);
4587 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4589 amd64_cdq_size (code, 4);
4590 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4595 #if defined( __native_client_codegen__ )
4596 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4597 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4599 if (ins->sreg2 == AMD64_RDX) {
4600 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4601 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4602 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4604 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4605 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4609 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4610 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4613 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4617 g_assert (amd64_is_imm32 (ins->inst_imm));
4618 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4621 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4625 g_assert (amd64_is_imm32 (ins->inst_imm));
4626 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4629 g_assert (ins->sreg2 == AMD64_RCX);
4630 amd64_shift_reg (code, X86_SHL, ins->dreg);
4633 g_assert (ins->sreg2 == AMD64_RCX);
4634 amd64_shift_reg (code, X86_SAR, ins->dreg);
4638 g_assert (amd64_is_imm32 (ins->inst_imm));
4639 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4642 g_assert (amd64_is_imm32 (ins->inst_imm));
4643 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4645 case OP_LSHR_UN_IMM:
4646 g_assert (amd64_is_imm32 (ins->inst_imm));
4647 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4650 g_assert (ins->sreg2 == AMD64_RCX);
4651 amd64_shift_reg (code, X86_SHR, ins->dreg);
4655 g_assert (amd64_is_imm32 (ins->inst_imm));
4656 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4661 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4664 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4667 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4670 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4674 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4677 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4680 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4683 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4686 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4689 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4692 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4695 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4698 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4701 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4704 amd64_neg_reg_size (code, ins->sreg1, 4);
4707 amd64_not_reg_size (code, ins->sreg1, 4);
4710 g_assert (ins->sreg2 == AMD64_RCX);
4711 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4714 g_assert (ins->sreg2 == AMD64_RCX);
4715 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4718 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4720 case OP_ISHR_UN_IMM:
4721 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4724 g_assert (ins->sreg2 == AMD64_RCX);
4725 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4728 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4731 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4734 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4735 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4737 case OP_IMUL_OVF_UN:
4738 case OP_LMUL_OVF_UN: {
4739 /* the mul operation and the exception check should most likely be split */
4740 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4741 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4742 /*g_assert (ins->sreg2 == X86_EAX);
4743 g_assert (ins->dreg == X86_EAX);*/
4744 if (ins->sreg2 == X86_EAX) {
4745 non_eax_reg = ins->sreg1;
4746 } else if (ins->sreg1 == X86_EAX) {
4747 non_eax_reg = ins->sreg2;
4749 /* no need to save since we're going to store to it anyway */
4750 if (ins->dreg != X86_EAX) {
4752 amd64_push_reg (code, X86_EAX);
4754 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4755 non_eax_reg = ins->sreg2;
4757 if (ins->dreg == X86_EDX) {
4760 amd64_push_reg (code, X86_EAX);
4764 amd64_push_reg (code, X86_EDX);
4766 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4767 /* save before the check since pop and mov don't change the flags */
4768 if (ins->dreg != X86_EAX)
4769 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4771 amd64_pop_reg (code, X86_EDX);
4773 amd64_pop_reg (code, X86_EAX);
4774 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4778 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4780 case OP_ICOMPARE_IMM:
4781 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4803 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4811 case OP_CMOV_INE_UN:
4812 case OP_CMOV_IGE_UN:
4813 case OP_CMOV_IGT_UN:
4814 case OP_CMOV_ILE_UN:
4815 case OP_CMOV_ILT_UN:
4821 case OP_CMOV_LNE_UN:
4822 case OP_CMOV_LGE_UN:
4823 case OP_CMOV_LGT_UN:
4824 case OP_CMOV_LLE_UN:
4825 case OP_CMOV_LLT_UN:
4826 g_assert (ins->dreg == ins->sreg1);
4827 /* This needs to operate on 64 bit values */
4828 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);
4832 amd64_not_reg (code, ins->sreg1);
4835 amd64_neg_reg (code, ins->sreg1);
4840 if ((((guint64)ins->inst_c0) >> 32) == 0 && !mini_get_debug_options()->single_imm_size)
4841 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4843 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4846 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4847 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4850 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4851 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4854 if (ins->dreg != ins->sreg1)
4855 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4857 case OP_AMD64_SET_XMMREG_R4: {
4859 if (ins->dreg != ins->sreg1)
4860 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4862 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4866 case OP_AMD64_SET_XMMREG_R8: {
4867 if (ins->dreg != ins->sreg1)
4868 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4872 MonoCallInst *call = (MonoCallInst*)ins;
4873 int i, save_area_offset;
4875 g_assert (!cfg->method->save_lmf);
4877 /* Restore callee saved registers */
4878 save_area_offset = cfg->arch.reg_save_area_offset;
4879 for (i = 0; i < AMD64_NREG; ++i)
4880 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4881 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4882 save_area_offset += 8;
4885 if (cfg->arch.omit_fp) {
4886 if (cfg->arch.stack_alloc_size)
4887 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4889 if (call->stack_usage)
4892 /* Copy arguments on the stack to our argument area */
4893 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4894 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4895 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4901 offset = code - cfg->native_code;
4902 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4903 if (cfg->compile_aot)
4904 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4906 amd64_set_reg_template (code, AMD64_R11);
4907 amd64_jump_reg (code, AMD64_R11);
4908 ins->flags |= MONO_INST_GC_CALLSITE;
4909 ins->backend.pc_offset = code - cfg->native_code;
4913 /* ensure ins->sreg1 is not NULL */
4914 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4917 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4918 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4928 call = (MonoCallInst*)ins;
4930 * The AMD64 ABI forces callers to know about varargs.
4932 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4933 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4934 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4936 * Since the unmanaged calling convention doesn't contain a
4937 * 'vararg' entry, we have to treat every pinvoke call as a
4938 * potential vararg call.
4942 for (i = 0; i < AMD64_XMM_NREG; ++i)
4943 if (call->used_fregs & (1 << i))
4946 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4948 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4951 if (ins->flags & MONO_INST_HAS_METHOD)
4952 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4954 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
4955 ins->flags |= MONO_INST_GC_CALLSITE;
4956 ins->backend.pc_offset = code - cfg->native_code;
4957 code = emit_move_return_value (cfg, ins, code);
4964 case OP_VOIDCALL_REG:
4966 call = (MonoCallInst*)ins;
4968 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4969 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4970 ins->sreg1 = AMD64_R11;
4974 * The AMD64 ABI forces callers to know about varargs.
4976 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4977 if (ins->sreg1 == AMD64_RAX) {
4978 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4979 ins->sreg1 = AMD64_R11;
4981 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4982 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4984 * Since the unmanaged calling convention doesn't contain a
4985 * 'vararg' entry, we have to treat every pinvoke call as a
4986 * potential vararg call.
4990 for (i = 0; i < AMD64_XMM_NREG; ++i)
4991 if (call->used_fregs & (1 << i))
4993 if (ins->sreg1 == AMD64_RAX) {
4994 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4995 ins->sreg1 = AMD64_R11;
4998 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
5000 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
5003 amd64_call_reg (code, ins->sreg1);
5004 ins->flags |= MONO_INST_GC_CALLSITE;
5005 ins->backend.pc_offset = code - cfg->native_code;
5006 code = emit_move_return_value (cfg, ins, code);
5008 case OP_FCALL_MEMBASE:
5009 case OP_RCALL_MEMBASE:
5010 case OP_LCALL_MEMBASE:
5011 case OP_VCALL_MEMBASE:
5012 case OP_VCALL2_MEMBASE:
5013 case OP_VOIDCALL_MEMBASE:
5014 case OP_CALL_MEMBASE:
5015 call = (MonoCallInst*)ins;
5017 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
5018 ins->flags |= MONO_INST_GC_CALLSITE;
5019 ins->backend.pc_offset = code - cfg->native_code;
5020 code = emit_move_return_value (cfg, ins, code);
5024 MonoInst *var = cfg->dyn_call_var;
5027 g_assert (var->opcode == OP_REGOFFSET);
5029 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
5030 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
5032 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
5034 /* Save args buffer */
5035 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
5037 /* Set fp arg regs */
5038 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, has_fp), sizeof (mgreg_t));
5039 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
5041 amd64_branch8 (code, X86_CC_Z, -1, 1);
5042 for (i = 0; i < FLOAT_PARAM_REGS; ++i)
5043 amd64_sse_movsd_reg_membase (code, i, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, fregs) + (i * sizeof (double)));
5044 amd64_patch (label, code);
5046 /* Set argument registers */
5047 for (i = 0; i < PARAM_REGS; ++i)
5048 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
5051 amd64_call_reg (code, AMD64_R10);
5053 ins->flags |= MONO_INST_GC_CALLSITE;
5054 ins->backend.pc_offset = code - cfg->native_code;
5057 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
5058 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
5059 amd64_sse_movsd_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, fregs), AMD64_XMM0);
5062 case OP_AMD64_SAVE_SP_TO_LMF: {
5063 MonoInst *lmf_var = cfg->lmf_var;
5064 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
5068 g_assert_not_reached ();
5069 amd64_push_reg (code, ins->sreg1);
5071 case OP_X86_PUSH_IMM:
5072 g_assert_not_reached ();
5073 g_assert (amd64_is_imm32 (ins->inst_imm));
5074 amd64_push_imm (code, ins->inst_imm);
5076 case OP_X86_PUSH_MEMBASE:
5077 g_assert_not_reached ();
5078 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
5080 case OP_X86_PUSH_OBJ: {
5081 int size = ALIGN_TO (ins->inst_imm, 8);
5083 g_assert_not_reached ();
5085 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5086 amd64_push_reg (code, AMD64_RDI);
5087 amd64_push_reg (code, AMD64_RSI);
5088 amd64_push_reg (code, AMD64_RCX);
5089 if (ins->inst_offset)
5090 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
5092 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
5093 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
5094 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
5096 amd64_prefix (code, X86_REP_PREFIX);
5098 amd64_pop_reg (code, AMD64_RCX);
5099 amd64_pop_reg (code, AMD64_RSI);
5100 amd64_pop_reg (code, AMD64_RDI);
5103 case OP_GENERIC_CLASS_INIT: {
5104 static int byte_offset = -1;
5105 static guint8 bitmask;
5108 g_assert (ins->sreg1 == MONO_AMD64_ARG_REG1);
5110 if (byte_offset < 0)
5111 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
5113 amd64_test_membase_imm_size (code, ins->sreg1, byte_offset, bitmask, 1);
5115 amd64_branch8 (code, X86_CC_NZ, -1, 1);
5117 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_generic_class_init", FALSE);
5118 ins->flags |= MONO_INST_GC_CALLSITE;
5119 ins->backend.pc_offset = code - cfg->native_code;
5121 x86_patch (jump, code);
5126 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
5128 case OP_X86_LEA_MEMBASE:
5129 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
5132 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
5135 /* keep alignment */
5136 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
5137 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
5138 code = mono_emit_stack_alloc (cfg, code, ins);
5139 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5140 if (cfg->param_area)
5141 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5143 case OP_LOCALLOC_IMM: {
5144 guint32 size = ins->inst_imm;
5145 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
5147 if (ins->flags & MONO_INST_INIT) {
5151 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5152 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5154 for (i = 0; i < size; i += 8)
5155 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
5156 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5158 amd64_mov_reg_imm (code, ins->dreg, size);
5159 ins->sreg1 = ins->dreg;
5161 code = mono_emit_stack_alloc (cfg, code, ins);
5162 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5165 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5166 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5168 if (cfg->param_area)
5169 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5173 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5174 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5175 (gpointer)"mono_arch_throw_exception", FALSE);
5176 ins->flags |= MONO_INST_GC_CALLSITE;
5177 ins->backend.pc_offset = code - cfg->native_code;
5181 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5182 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5183 (gpointer)"mono_arch_rethrow_exception", FALSE);
5184 ins->flags |= MONO_INST_GC_CALLSITE;
5185 ins->backend.pc_offset = code - cfg->native_code;
5188 case OP_CALL_HANDLER:
5190 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5191 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5192 amd64_call_imm (code, 0);
5193 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
5194 /* Restore stack alignment */
5195 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5197 case OP_START_HANDLER: {
5198 /* Even though we're saving RSP, use sizeof */
5199 /* gpointer because spvar is of type IntPtr */
5200 /* see: mono_create_spvar_for_region */
5201 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5202 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
5204 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
5205 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
5207 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
5211 case OP_ENDFINALLY: {
5212 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5213 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5217 case OP_ENDFILTER: {
5218 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5219 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5220 /* The local allocator will put the result into RAX */
5225 if (ins->dreg != AMD64_RAX)
5226 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, sizeof (gpointer));
5229 ins->inst_c0 = code - cfg->native_code;
5232 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
5233 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
5235 if (ins->inst_target_bb->native_offset) {
5236 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
5238 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5239 if ((cfg->opt & MONO_OPT_BRANCH) &&
5240 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
5241 x86_jump8 (code, 0);
5243 x86_jump32 (code, 0);
5247 amd64_jump_reg (code, ins->sreg1);
5270 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5271 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5273 case OP_COND_EXC_EQ:
5274 case OP_COND_EXC_NE_UN:
5275 case OP_COND_EXC_LT:
5276 case OP_COND_EXC_LT_UN:
5277 case OP_COND_EXC_GT:
5278 case OP_COND_EXC_GT_UN:
5279 case OP_COND_EXC_GE:
5280 case OP_COND_EXC_GE_UN:
5281 case OP_COND_EXC_LE:
5282 case OP_COND_EXC_LE_UN:
5283 case OP_COND_EXC_IEQ:
5284 case OP_COND_EXC_INE_UN:
5285 case OP_COND_EXC_ILT:
5286 case OP_COND_EXC_ILT_UN:
5287 case OP_COND_EXC_IGT:
5288 case OP_COND_EXC_IGT_UN:
5289 case OP_COND_EXC_IGE:
5290 case OP_COND_EXC_IGE_UN:
5291 case OP_COND_EXC_ILE:
5292 case OP_COND_EXC_ILE_UN:
5293 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], (const char *)ins->inst_p1);
5295 case OP_COND_EXC_OV:
5296 case OP_COND_EXC_NO:
5298 case OP_COND_EXC_NC:
5299 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5300 (ins->opcode < OP_COND_EXC_NE_UN), (const char *)ins->inst_p1);
5302 case OP_COND_EXC_IOV:
5303 case OP_COND_EXC_INO:
5304 case OP_COND_EXC_IC:
5305 case OP_COND_EXC_INC:
5306 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5307 (ins->opcode < OP_COND_EXC_INE_UN), (const char *)ins->inst_p1);
5310 /* floating point opcodes */
5312 double d = *(double *)ins->inst_p0;
5314 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5315 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5318 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5319 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5324 float f = *(float *)ins->inst_p0;
5326 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5328 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5330 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5333 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5334 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5336 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5340 case OP_STORER8_MEMBASE_REG:
5341 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5343 case OP_LOADR8_MEMBASE:
5344 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5346 case OP_STORER4_MEMBASE_REG:
5348 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5350 /* This requires a double->single conversion */
5351 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5352 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5355 case OP_LOADR4_MEMBASE:
5357 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5359 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5360 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5363 case OP_ICONV_TO_R4:
5365 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5367 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5368 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5371 case OP_ICONV_TO_R8:
5372 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5374 case OP_LCONV_TO_R4:
5376 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5378 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5379 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5382 case OP_LCONV_TO_R8:
5383 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5385 case OP_FCONV_TO_R4:
5387 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5389 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5390 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5393 case OP_FCONV_TO_I1:
5394 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5396 case OP_FCONV_TO_U1:
5397 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5399 case OP_FCONV_TO_I2:
5400 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5402 case OP_FCONV_TO_U2:
5403 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5405 case OP_FCONV_TO_U4:
5406 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5408 case OP_FCONV_TO_I4:
5410 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5412 case OP_FCONV_TO_I8:
5413 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5416 case OP_RCONV_TO_I1:
5417 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5418 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5420 case OP_RCONV_TO_U1:
5421 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5422 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5424 case OP_RCONV_TO_I2:
5425 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5426 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5428 case OP_RCONV_TO_U2:
5429 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5430 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5432 case OP_RCONV_TO_I4:
5433 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5435 case OP_RCONV_TO_U4:
5436 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5438 case OP_RCONV_TO_I8:
5439 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5441 case OP_RCONV_TO_R8:
5442 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5444 case OP_RCONV_TO_R4:
5445 if (ins->dreg != ins->sreg1)
5446 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5449 case OP_LCONV_TO_R_UN: {
5452 /* Based on gcc code */
5453 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5454 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5457 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5458 br [1] = code; x86_jump8 (code, 0);
5459 amd64_patch (br [0], code);
5462 /* Save to the red zone */
5463 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5464 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5465 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5466 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5467 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5468 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5469 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5470 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5471 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5473 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5474 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5475 amd64_patch (br [1], code);
5478 case OP_LCONV_TO_OVF_U4:
5479 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5480 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5481 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5483 case OP_LCONV_TO_OVF_I4_UN:
5484 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5485 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5486 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5489 if (ins->dreg != ins->sreg1)
5490 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5493 if (ins->dreg != ins->sreg1)
5494 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5496 case OP_MOVE_F_TO_I4:
5498 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5500 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5501 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5504 case OP_MOVE_I4_TO_F:
5505 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5507 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5509 case OP_MOVE_F_TO_I8:
5510 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5512 case OP_MOVE_I8_TO_F:
5513 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5516 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5519 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5522 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5525 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5528 static double r8_0 = -0.0;
5530 g_assert (ins->sreg1 == ins->dreg);
5532 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5533 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5537 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5540 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5543 static guint64 d = 0x7fffffffffffffffUL;
5545 g_assert (ins->sreg1 == ins->dreg);
5547 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5548 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5552 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5556 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5559 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5562 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5565 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5568 static float r4_0 = -0.0;
5570 g_assert (ins->sreg1 == ins->dreg);
5572 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5573 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5574 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
5579 g_assert (cfg->opt & MONO_OPT_CMOV);
5580 g_assert (ins->dreg == ins->sreg1);
5581 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5582 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
5585 g_assert (cfg->opt & MONO_OPT_CMOV);
5586 g_assert (ins->dreg == ins->sreg1);
5587 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5588 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
5591 g_assert (cfg->opt & MONO_OPT_CMOV);
5592 g_assert (ins->dreg == ins->sreg1);
5593 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5594 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
5597 g_assert (cfg->opt & MONO_OPT_CMOV);
5598 g_assert (ins->dreg == ins->sreg1);
5599 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5600 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
5603 g_assert (cfg->opt & MONO_OPT_CMOV);
5604 g_assert (ins->dreg == ins->sreg1);
5605 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5606 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
5609 g_assert (cfg->opt & MONO_OPT_CMOV);
5610 g_assert (ins->dreg == ins->sreg1);
5611 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5612 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
5615 g_assert (cfg->opt & MONO_OPT_CMOV);
5616 g_assert (ins->dreg == ins->sreg1);
5617 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5618 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
5621 g_assert (cfg->opt & MONO_OPT_CMOV);
5622 g_assert (ins->dreg == ins->sreg1);
5623 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5624 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5630 * The two arguments are swapped because the fbranch instructions
5631 * depend on this for the non-sse case to work.
5633 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5637 * FIXME: Get rid of this.
5638 * The two arguments are swapped because the fbranch instructions
5639 * depend on this for the non-sse case to work.
5641 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5645 /* zeroing the register at the start results in
5646 * shorter and faster code (we can also remove the widening op)
5648 guchar *unordered_check;
5650 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5651 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5652 unordered_check = code;
5653 x86_branch8 (code, X86_CC_P, 0, FALSE);
5655 if (ins->opcode == OP_FCEQ) {
5656 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5657 amd64_patch (unordered_check, code);
5659 guchar *jump_to_end;
5660 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5662 x86_jump8 (code, 0);
5663 amd64_patch (unordered_check, code);
5664 amd64_inc_reg (code, ins->dreg);
5665 amd64_patch (jump_to_end, code);
5671 /* zeroing the register at the start results in
5672 * shorter and faster code (we can also remove the widening op)
5674 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5675 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5676 if (ins->opcode == OP_FCLT_UN) {
5677 guchar *unordered_check = code;
5678 guchar *jump_to_end;
5679 x86_branch8 (code, X86_CC_P, 0, FALSE);
5680 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5682 x86_jump8 (code, 0);
5683 amd64_patch (unordered_check, code);
5684 amd64_inc_reg (code, ins->dreg);
5685 amd64_patch (jump_to_end, code);
5687 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5692 guchar *unordered_check;
5693 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5694 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5695 unordered_check = code;
5696 x86_branch8 (code, X86_CC_P, 0, FALSE);
5697 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5698 amd64_patch (unordered_check, code);
5703 /* zeroing the register at the start results in
5704 * shorter and faster code (we can also remove the widening op)
5706 guchar *unordered_check;
5708 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5709 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5710 if (ins->opcode == OP_FCGT) {
5711 unordered_check = code;
5712 x86_branch8 (code, X86_CC_P, 0, FALSE);
5713 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5714 amd64_patch (unordered_check, code);
5716 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5721 guchar *unordered_check;
5722 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5723 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5724 unordered_check = code;
5725 x86_branch8 (code, X86_CC_P, 0, FALSE);
5726 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5727 amd64_patch (unordered_check, code);
5737 gboolean unordered = FALSE;
5739 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5740 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5742 switch (ins->opcode) {
5744 x86_cond = X86_CC_EQ;
5747 x86_cond = X86_CC_LT;
5750 x86_cond = X86_CC_GT;
5753 x86_cond = X86_CC_GT;
5757 x86_cond = X86_CC_LT;
5761 g_assert_not_reached ();
5766 guchar *unordered_check;
5767 guchar *jump_to_end;
5769 unordered_check = code;
5770 x86_branch8 (code, X86_CC_P, 0, FALSE);
5771 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5773 x86_jump8 (code, 0);
5774 amd64_patch (unordered_check, code);
5775 amd64_inc_reg (code, ins->dreg);
5776 amd64_patch (jump_to_end, code);
5778 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5782 case OP_FCLT_MEMBASE:
5783 case OP_FCGT_MEMBASE:
5784 case OP_FCLT_UN_MEMBASE:
5785 case OP_FCGT_UN_MEMBASE:
5786 case OP_FCEQ_MEMBASE: {
5787 guchar *unordered_check, *jump_to_end;
5790 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5791 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5793 switch (ins->opcode) {
5794 case OP_FCEQ_MEMBASE:
5795 x86_cond = X86_CC_EQ;
5797 case OP_FCLT_MEMBASE:
5798 case OP_FCLT_UN_MEMBASE:
5799 x86_cond = X86_CC_LT;
5801 case OP_FCGT_MEMBASE:
5802 case OP_FCGT_UN_MEMBASE:
5803 x86_cond = X86_CC_GT;
5806 g_assert_not_reached ();
5809 unordered_check = code;
5810 x86_branch8 (code, X86_CC_P, 0, FALSE);
5811 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5813 switch (ins->opcode) {
5814 case OP_FCEQ_MEMBASE:
5815 case OP_FCLT_MEMBASE:
5816 case OP_FCGT_MEMBASE:
5817 amd64_patch (unordered_check, code);
5819 case OP_FCLT_UN_MEMBASE:
5820 case OP_FCGT_UN_MEMBASE:
5822 x86_jump8 (code, 0);
5823 amd64_patch (unordered_check, code);
5824 amd64_inc_reg (code, ins->dreg);
5825 amd64_patch (jump_to_end, code);
5833 guchar *jump = code;
5834 x86_branch8 (code, X86_CC_P, 0, TRUE);
5835 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5836 amd64_patch (jump, code);
5840 /* Branch if C013 != 100 */
5841 /* branch if !ZF or (PF|CF) */
5842 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5843 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5844 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5847 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5850 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5851 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5855 if (ins->opcode == OP_FBGT) {
5858 /* skip branch if C1=1 */
5860 x86_branch8 (code, X86_CC_P, 0, FALSE);
5861 /* branch if (C0 | C3) = 1 */
5862 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5863 amd64_patch (br1, code);
5866 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5870 /* Branch if C013 == 100 or 001 */
5873 /* skip branch if C1=1 */
5875 x86_branch8 (code, X86_CC_P, 0, FALSE);
5876 /* branch if (C0 | C3) = 1 */
5877 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5878 amd64_patch (br1, code);
5882 /* Branch if C013 == 000 */
5883 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5886 /* Branch if C013=000 or 100 */
5889 /* skip branch if C1=1 */
5891 x86_branch8 (code, X86_CC_P, 0, FALSE);
5892 /* branch if C0=0 */
5893 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5894 amd64_patch (br1, code);
5898 /* Branch if C013 != 001 */
5899 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5900 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5903 /* Transfer value to the fp stack */
5904 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5905 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5906 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5908 amd64_push_reg (code, AMD64_RAX);
5910 amd64_fnstsw (code);
5911 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5912 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5913 amd64_pop_reg (code, AMD64_RAX);
5914 amd64_fstp (code, 0);
5915 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "OverflowException");
5916 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5919 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5922 case OP_TLS_GET_REG:
5923 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5926 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5929 case OP_TLS_SET_REG: {
5930 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5933 case OP_MEMORY_BARRIER: {
5934 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5938 case OP_ATOMIC_ADD_I4:
5939 case OP_ATOMIC_ADD_I8: {
5940 int dreg = ins->dreg;
5941 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5943 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5946 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5947 amd64_prefix (code, X86_LOCK_PREFIX);
5948 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5949 /* dreg contains the old value, add with sreg2 value */
5950 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5952 if (ins->dreg != dreg)
5953 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5957 case OP_ATOMIC_EXCHANGE_I4:
5958 case OP_ATOMIC_EXCHANGE_I8: {
5959 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5961 /* LOCK prefix is implied. */
5962 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5963 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5964 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5967 case OP_ATOMIC_CAS_I4:
5968 case OP_ATOMIC_CAS_I8: {
5971 if (ins->opcode == OP_ATOMIC_CAS_I8)
5977 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5978 * an explanation of how this works.
5980 g_assert (ins->sreg3 == AMD64_RAX);
5981 g_assert (ins->sreg1 != AMD64_RAX);
5982 g_assert (ins->sreg1 != ins->sreg2);
5984 amd64_prefix (code, X86_LOCK_PREFIX);
5985 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
5987 if (ins->dreg != AMD64_RAX)
5988 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
5991 case OP_ATOMIC_LOAD_I1: {
5992 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
5995 case OP_ATOMIC_LOAD_U1: {
5996 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
5999 case OP_ATOMIC_LOAD_I2: {
6000 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
6003 case OP_ATOMIC_LOAD_U2: {
6004 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
6007 case OP_ATOMIC_LOAD_I4: {
6008 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
6011 case OP_ATOMIC_LOAD_U4:
6012 case OP_ATOMIC_LOAD_I8:
6013 case OP_ATOMIC_LOAD_U8: {
6014 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
6017 case OP_ATOMIC_LOAD_R4: {
6018 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
6019 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
6022 case OP_ATOMIC_LOAD_R8: {
6023 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
6026 case OP_ATOMIC_STORE_I1:
6027 case OP_ATOMIC_STORE_U1:
6028 case OP_ATOMIC_STORE_I2:
6029 case OP_ATOMIC_STORE_U2:
6030 case OP_ATOMIC_STORE_I4:
6031 case OP_ATOMIC_STORE_U4:
6032 case OP_ATOMIC_STORE_I8:
6033 case OP_ATOMIC_STORE_U8: {
6036 switch (ins->opcode) {
6037 case OP_ATOMIC_STORE_I1:
6038 case OP_ATOMIC_STORE_U1:
6041 case OP_ATOMIC_STORE_I2:
6042 case OP_ATOMIC_STORE_U2:
6045 case OP_ATOMIC_STORE_I4:
6046 case OP_ATOMIC_STORE_U4:
6049 case OP_ATOMIC_STORE_I8:
6050 case OP_ATOMIC_STORE_U8:
6055 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
6057 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6061 case OP_ATOMIC_STORE_R4: {
6062 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6063 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
6065 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6069 case OP_ATOMIC_STORE_R8: {
6072 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
6076 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6080 case OP_CARD_TABLE_WBARRIER: {
6081 int ptr = ins->sreg1;
6082 int value = ins->sreg2;
6084 int nursery_shift, card_table_shift;
6085 gpointer card_table_mask;
6086 size_t nursery_size;
6088 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
6089 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
6090 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
6092 /*If either point to the stack we can simply avoid the WB. This happens due to
6093 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
6095 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
6099 * We need one register we can clobber, we choose EDX and make sreg1
6100 * fixed EAX to work around limitations in the local register allocator.
6101 * sreg2 might get allocated to EDX, but that is not a problem since
6102 * we use it before clobbering EDX.
6104 g_assert (ins->sreg1 == AMD64_RAX);
6107 * This is the code we produce:
6110 * edx >>= nursery_shift
6111 * cmp edx, (nursery_start >> nursery_shift)
6114 * edx >>= card_table_shift
6120 if (mono_gc_card_table_nursery_check ()) {
6121 if (value != AMD64_RDX)
6122 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
6123 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
6124 if (shifted_nursery_start >> 31) {
6126 * The value we need to compare against is 64 bits, so we need
6127 * another spare register. We use RBX, which we save and
6130 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
6131 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
6132 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
6133 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
6135 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
6137 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
6139 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
6140 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
6141 if (card_table_mask)
6142 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
6144 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
6145 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
6147 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
6149 if (mono_gc_card_table_nursery_check ())
6150 x86_patch (br, code);
6153 #ifdef MONO_ARCH_SIMD_INTRINSICS
6154 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
6156 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
6159 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
6162 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
6165 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
6168 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
6171 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
6174 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6175 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6178 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
6181 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
6184 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
6187 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
6190 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6193 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6196 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
6199 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6202 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
6205 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6208 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
6211 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
6214 case OP_PSHUFLEW_HIGH:
6215 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6216 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6218 case OP_PSHUFLEW_LOW:
6219 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6220 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6223 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6224 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6227 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6228 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6231 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
6232 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6236 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
6239 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
6242 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
6245 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
6248 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
6251 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
6254 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6255 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6258 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
6261 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
6264 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
6267 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6270 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6273 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6276 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6279 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6282 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6285 case OP_EXTRACT_MASK:
6286 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6290 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6293 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6296 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6300 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6303 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6306 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6309 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6313 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6316 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6319 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6322 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6326 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6329 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6332 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6336 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6339 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6342 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6346 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6349 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6353 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6356 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6359 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6363 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6366 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6369 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6373 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6376 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6379 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6382 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6386 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6389 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6392 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6395 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6398 case OP_PSUM_ABS_DIFF:
6399 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6402 case OP_UNPACK_LOWB:
6403 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6405 case OP_UNPACK_LOWW:
6406 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6408 case OP_UNPACK_LOWD:
6409 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6411 case OP_UNPACK_LOWQ:
6412 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6414 case OP_UNPACK_LOWPS:
6415 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6417 case OP_UNPACK_LOWPD:
6418 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6421 case OP_UNPACK_HIGHB:
6422 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6424 case OP_UNPACK_HIGHW:
6425 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6427 case OP_UNPACK_HIGHD:
6428 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6430 case OP_UNPACK_HIGHQ:
6431 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6433 case OP_UNPACK_HIGHPS:
6434 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6436 case OP_UNPACK_HIGHPD:
6437 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6441 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6444 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6447 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6450 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6453 case OP_PADDB_SAT_UN:
6454 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6456 case OP_PSUBB_SAT_UN:
6457 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6459 case OP_PADDW_SAT_UN:
6460 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6462 case OP_PSUBW_SAT_UN:
6463 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6467 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6470 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6473 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6476 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6480 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6483 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6486 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6488 case OP_PMULW_HIGH_UN:
6489 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6492 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6496 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6499 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6503 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6506 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6510 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6513 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6517 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6520 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6524 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6527 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6531 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6534 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6538 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6541 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6544 /*TODO: This is appart of the sse spec but not added
6546 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6549 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6554 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6557 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6560 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6563 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6566 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6569 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6572 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6575 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6578 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6581 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6585 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6588 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6592 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6593 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6595 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6600 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6602 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6603 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6607 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6609 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6610 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6611 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6615 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6617 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6620 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6622 case OP_EXTRACTX_U2:
6623 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6625 case OP_INSERTX_U1_SLOW:
6626 /*sreg1 is the extracted ireg (scratch)
6627 /sreg2 is the to be inserted ireg (scratch)
6628 /dreg is the xreg to receive the value*/
6630 /*clear the bits from the extracted word*/
6631 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6632 /*shift the value to insert if needed*/
6633 if (ins->inst_c0 & 1)
6634 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6635 /*join them together*/
6636 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6637 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6639 case OP_INSERTX_I4_SLOW:
6640 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6641 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6642 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6644 case OP_INSERTX_I8_SLOW:
6645 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6647 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6649 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6652 case OP_INSERTX_R4_SLOW:
6653 switch (ins->inst_c0) {
6656 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6658 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6661 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6663 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6665 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6666 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6669 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6671 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6673 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6674 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6677 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6679 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6681 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6682 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6686 case OP_INSERTX_R8_SLOW:
6688 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6690 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6692 case OP_STOREX_MEMBASE_REG:
6693 case OP_STOREX_MEMBASE:
6694 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6696 case OP_LOADX_MEMBASE:
6697 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6699 case OP_LOADX_ALIGNED_MEMBASE:
6700 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6702 case OP_STOREX_ALIGNED_MEMBASE_REG:
6703 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6705 case OP_STOREX_NTA_MEMBASE_REG:
6706 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6708 case OP_PREFETCH_MEMBASE:
6709 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6713 /*FIXME the peephole pass should have killed this*/
6714 if (ins->dreg != ins->sreg1)
6715 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6718 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6720 case OP_ICONV_TO_R4_RAW:
6721 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6724 case OP_FCONV_TO_R8_X:
6725 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6728 case OP_XCONV_R8_TO_I4:
6729 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6730 switch (ins->backend.source_opcode) {
6731 case OP_FCONV_TO_I1:
6732 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6734 case OP_FCONV_TO_U1:
6735 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6737 case OP_FCONV_TO_I2:
6738 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6740 case OP_FCONV_TO_U2:
6741 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6747 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6748 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6749 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6752 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6753 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6756 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6757 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6761 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6763 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6764 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6766 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6769 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6770 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6773 case OP_LIVERANGE_START: {
6774 if (cfg->verbose_level > 1)
6775 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6776 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6779 case OP_LIVERANGE_END: {
6780 if (cfg->verbose_level > 1)
6781 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6782 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6785 case OP_GC_SAFE_POINT: {
6786 const char *polling_func = NULL;
6787 int compare_val = 0;
6790 #if defined(__native_client_codegen__) && defined(__native_client_gc__)
6791 polling_func = "mono_nacl_gc";
6792 compare_val = 0xFFFFFFFF;
6794 g_assert (mono_threads_is_coop_enabled ());
6795 polling_func = "mono_threads_state_poll";
6799 amd64_test_membase_imm_size (code, ins->sreg1, 0, compare_val, 4);
6800 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6801 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func, FALSE);
6802 amd64_patch (br[0], code);
6806 case OP_GC_LIVENESS_DEF:
6807 case OP_GC_LIVENESS_USE:
6808 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6809 ins->backend.pc_offset = code - cfg->native_code;
6811 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6812 ins->backend.pc_offset = code - cfg->native_code;
6813 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6816 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6817 g_assert_not_reached ();
6820 if ((code - cfg->native_code - offset) > max_len) {
6821 #if !defined(__native_client_codegen__)
6822 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6823 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6824 g_assert_not_reached ();
6829 cfg->code_len = code - cfg->native_code;
6832 #endif /* DISABLE_JIT */
6835 mono_arch_register_lowlevel_calls (void)
6837 /* The signature doesn't matter */
6838 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6842 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
6844 unsigned char *ip = ji->ip.i + code;
6847 * Debug code to help track down problems where the target of a near call is
6850 if (amd64_is_near_call (ip)) {
6851 gint64 disp = (guint8*)target - (guint8*)ip;
6853 if (!amd64_is_imm32 (disp)) {
6854 printf ("TYPE: %d\n", ji->type);
6856 case MONO_PATCH_INFO_INTERNAL_METHOD:
6857 printf ("V: %s\n", ji->data.name);
6859 case MONO_PATCH_INFO_METHOD_JUMP:
6860 case MONO_PATCH_INFO_METHOD:
6861 printf ("V: %s\n", ji->data.method->name);
6869 amd64_patch (ip, (gpointer)target);
6875 get_max_epilog_size (MonoCompile *cfg)
6877 int max_epilog_size = 16;
6879 if (cfg->method->save_lmf)
6880 max_epilog_size += 256;
6882 if (mono_jit_trace_calls != NULL)
6883 max_epilog_size += 50;
6885 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6886 max_epilog_size += 50;
6888 max_epilog_size += (AMD64_NREG * 2);
6890 return max_epilog_size;
6894 * This macro is used for testing whenever the unwinder works correctly at every point
6895 * where an async exception can happen.
6897 /* This will generate a SIGSEGV at the given point in the code */
6898 #define async_exc_point(code) do { \
6899 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6900 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6901 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6902 cfg->arch.async_point_count ++; \
6907 mono_arch_emit_prolog (MonoCompile *cfg)
6909 MonoMethod *method = cfg->method;
6911 MonoMethodSignature *sig;
6913 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6916 MonoInst *lmf_var = cfg->lmf_var;
6917 gboolean args_clobbered = FALSE;
6918 gboolean trace = FALSE;
6919 #ifdef __native_client_codegen__
6920 guint alignment_check;
6923 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6925 #if defined(__default_codegen__)
6926 code = cfg->native_code = (unsigned char *)g_malloc (cfg->code_size);
6927 #elif defined(__native_client_codegen__)
6928 /* native_code_alloc is not 32-byte aligned, native_code is. */
6929 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6931 /* Align native_code to next nearest kNaclAlignment byte. */
6932 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6933 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6935 code = cfg->native_code;
6937 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6938 g_assert (alignment_check == 0);
6941 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6944 /* Amount of stack space allocated by register saving code */
6947 /* Offset between RSP and the CFA */
6951 * The prolog consists of the following parts:
6953 * - push rbp, mov rbp, rsp
6954 * - save callee saved regs using pushes
6956 * - save rgctx if needed
6957 * - save lmf if needed
6960 * - save rgctx if needed
6961 * - save lmf if needed
6962 * - save callee saved regs using moves
6967 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6968 // IP saved at CFA - 8
6969 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6970 async_exc_point (code);
6971 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6973 if (!cfg->arch.omit_fp) {
6974 amd64_push_reg (code, AMD64_RBP);
6976 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6977 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6978 async_exc_point (code);
6980 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6982 /* These are handled automatically by the stack marking code */
6983 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6985 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
6986 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
6987 async_exc_point (code);
6989 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6993 /* The param area is always at offset 0 from sp */
6994 /* This needs to be allocated here, since it has to come after the spill area */
6995 if (cfg->param_area) {
6996 if (cfg->arch.omit_fp)
6998 g_assert_not_reached ();
6999 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
7002 if (cfg->arch.omit_fp) {
7004 * On enter, the stack is misaligned by the pushing of the return
7005 * address. It is either made aligned by the pushing of %rbp, or by
7008 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
7009 if ((alloc_size % 16) == 0) {
7011 /* Mark the padding slot as NOREF */
7012 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
7015 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
7016 if (cfg->stack_offset != alloc_size) {
7017 /* Mark the padding slot as NOREF */
7018 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
7020 cfg->arch.sp_fp_offset = alloc_size;
7024 cfg->arch.stack_alloc_size = alloc_size;
7026 /* Allocate stack frame */
7028 /* See mono_emit_stack_alloc */
7029 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
7030 guint32 remaining_size = alloc_size;
7031 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
7032 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
7033 guint32 offset = code - cfg->native_code;
7034 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
7035 while (required_code_size >= (cfg->code_size - offset))
7036 cfg->code_size *= 2;
7037 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7038 code = cfg->native_code + offset;
7039 cfg->stat_code_reallocs++;
7042 while (remaining_size >= 0x1000) {
7043 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
7044 if (cfg->arch.omit_fp) {
7045 cfa_offset += 0x1000;
7046 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7048 async_exc_point (code);
7050 if (cfg->arch.omit_fp)
7051 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
7054 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
7055 remaining_size -= 0x1000;
7057 if (remaining_size) {
7058 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
7059 if (cfg->arch.omit_fp) {
7060 cfa_offset += remaining_size;
7061 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7062 async_exc_point (code);
7065 if (cfg->arch.omit_fp)
7066 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
7070 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
7071 if (cfg->arch.omit_fp) {
7072 cfa_offset += alloc_size;
7073 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7074 async_exc_point (code);
7079 /* Stack alignment check */
7084 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
7085 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
7086 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
7088 x86_branch8 (code, X86_CC_EQ, 1, FALSE);
7089 amd64_breakpoint (code);
7090 amd64_patch (buf, code);
7094 if (mini_get_debug_options ()->init_stacks) {
7095 /* Fill the stack frame with a dummy value to force deterministic behavior */
7097 /* Save registers to the red zone */
7098 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
7099 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
7101 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
7102 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
7103 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
7106 #if defined(__default_codegen__)
7107 amd64_prefix (code, X86_REP_PREFIX);
7109 #elif defined(__native_client_codegen__)
7110 /* NaCl stos pseudo-instruction */
7111 amd64_codegen_pre (code);
7112 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
7113 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
7114 /* Add %r15 to %rdi using lea, condition flags unaffected. */
7115 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
7116 amd64_prefix (code, X86_REP_PREFIX);
7118 amd64_codegen_post (code);
7119 #endif /* __native_client_codegen__ */
7121 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
7122 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
7126 if (method->save_lmf)
7127 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
7129 /* Save callee saved registers */
7130 if (cfg->arch.omit_fp) {
7131 save_area_offset = cfg->arch.reg_save_area_offset;
7132 /* Save caller saved registers after sp is adjusted */
7133 /* The registers are saved at the bottom of the frame */
7134 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
7136 /* The registers are saved just below the saved rbp */
7137 save_area_offset = cfg->arch.reg_save_area_offset;
7140 for (i = 0; i < AMD64_NREG; ++i) {
7141 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7142 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
7144 if (cfg->arch.omit_fp) {
7145 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
7146 /* These are handled automatically by the stack marking code */
7147 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
7149 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
7153 save_area_offset += 8;
7154 async_exc_point (code);
7158 /* store runtime generic context */
7159 if (cfg->rgctx_var) {
7160 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
7161 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
7163 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
7165 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
7166 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
7169 /* compute max_length in order to use short forward jumps */
7170 max_epilog_size = get_max_epilog_size (cfg);
7171 if (cfg->opt & MONO_OPT_BRANCH) {
7172 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7176 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7178 /* max alignment for loops */
7179 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
7180 max_length += LOOP_ALIGNMENT;
7181 #ifdef __native_client_codegen__
7182 /* max alignment for native client */
7183 max_length += kNaClAlignment;
7186 MONO_BB_FOR_EACH_INS (bb, ins) {
7187 #ifdef __native_client_codegen__
7189 int space_in_block = kNaClAlignment -
7190 ((max_length + cfg->code_len) & kNaClAlignmentMask);
7191 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7192 if (space_in_block < max_len && max_len < kNaClAlignment) {
7193 max_length += space_in_block;
7196 #endif /*__native_client_codegen__*/
7197 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7200 /* Take prolog and epilog instrumentation into account */
7201 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
7202 max_length += max_epilog_size;
7204 bb->max_length = max_length;
7208 sig = mono_method_signature (method);
7211 cinfo = (CallInfo *)cfg->arch.cinfo;
7213 if (sig->ret->type != MONO_TYPE_VOID) {
7214 /* Save volatile arguments to the stack */
7215 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
7216 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
7219 /* Keep this in sync with emit_load_volatile_arguments */
7220 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
7221 ArgInfo *ainfo = cinfo->args + i;
7223 ins = cfg->args [i];
7225 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
7226 /* Unused arguments */
7229 /* Save volatile arguments to the stack */
7230 if (ins->opcode != OP_REGVAR) {
7231 switch (ainfo->storage) {
7237 if (stack_offset & 0x1)
7239 else if (stack_offset & 0x2)
7241 else if (stack_offset & 0x4)
7246 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
7249 * Save the original location of 'this',
7250 * get_generic_info_from_stack_frame () needs this to properly look up
7251 * the argument value during the handling of async exceptions.
7253 if (ins == cfg->args [0]) {
7254 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7255 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
7259 case ArgInFloatSSEReg:
7260 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7262 case ArgInDoubleSSEReg:
7263 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7265 case ArgValuetypeInReg:
7266 for (quad = 0; quad < 2; quad ++) {
7267 switch (ainfo->pair_storage [quad]) {
7269 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7271 case ArgInFloatSSEReg:
7272 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7274 case ArgInDoubleSSEReg:
7275 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7280 g_assert_not_reached ();
7284 case ArgValuetypeAddrInIReg:
7285 if (ainfo->pair_storage [0] == ArgInIReg)
7286 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7288 case ArgGSharedVtInReg:
7289 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, 8);
7295 /* Argument allocated to (non-volatile) register */
7296 switch (ainfo->storage) {
7298 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7301 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7304 g_assert_not_reached ();
7307 if (ins == cfg->args [0]) {
7308 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7309 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7314 if (cfg->method->save_lmf)
7315 args_clobbered = TRUE;
7318 args_clobbered = TRUE;
7319 code = (guint8 *)mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7322 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7323 args_clobbered = TRUE;
7326 * Optimize the common case of the first bblock making a call with the same
7327 * arguments as the method. This works because the arguments are still in their
7328 * original argument registers.
7329 * FIXME: Generalize this
7331 if (!args_clobbered) {
7332 MonoBasicBlock *first_bb = cfg->bb_entry;
7334 int filter = FILTER_IL_SEQ_POINT;
7336 next = mono_bb_first_inst (first_bb, filter);
7337 if (!next && first_bb->next_bb) {
7338 first_bb = first_bb->next_bb;
7339 next = mono_bb_first_inst (first_bb, filter);
7342 if (first_bb->in_count > 1)
7345 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7346 ArgInfo *ainfo = cinfo->args + i;
7347 gboolean match = FALSE;
7349 ins = cfg->args [i];
7350 if (ins->opcode != OP_REGVAR) {
7351 switch (ainfo->storage) {
7353 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7354 if (next->dreg == ainfo->reg) {
7358 next->opcode = OP_MOVE;
7359 next->sreg1 = ainfo->reg;
7360 /* Only continue if the instruction doesn't change argument regs */
7361 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7371 /* Argument allocated to (non-volatile) register */
7372 switch (ainfo->storage) {
7374 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7385 next = mono_inst_next (next, filter);
7386 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7393 if (cfg->gen_sdb_seq_points) {
7394 MonoInst *info_var = (MonoInst *)cfg->arch.seq_point_info_var;
7396 /* Initialize seq_point_info_var */
7397 if (cfg->compile_aot) {
7398 /* Initialize the variable from a GOT slot */
7399 /* Same as OP_AOTCONST */
7400 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7401 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7402 g_assert (info_var->opcode == OP_REGOFFSET);
7403 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7406 if (cfg->compile_aot) {
7407 /* Initialize ss_tramp_var */
7408 ins = (MonoInst *)cfg->arch.ss_tramp_var;
7409 g_assert (ins->opcode == OP_REGOFFSET);
7411 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7412 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr), 8);
7413 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7415 /* Initialize ss_tramp_var */
7416 ins = (MonoInst *)cfg->arch.ss_tramp_var;
7417 g_assert (ins->opcode == OP_REGOFFSET);
7419 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&ss_trampoline);
7420 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7422 /* Initialize bp_tramp_var */
7423 ins = (MonoInst *)cfg->arch.bp_tramp_var;
7424 g_assert (ins->opcode == OP_REGOFFSET);
7426 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&bp_trampoline);
7427 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7431 cfg->code_len = code - cfg->native_code;
7433 g_assert (cfg->code_len < cfg->code_size);
7439 mono_arch_emit_epilog (MonoCompile *cfg)
7441 MonoMethod *method = cfg->method;
7444 int max_epilog_size;
7446 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7447 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7449 max_epilog_size = get_max_epilog_size (cfg);
7451 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7452 cfg->code_size *= 2;
7453 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7454 cfg->stat_code_reallocs++;
7456 code = cfg->native_code + cfg->code_len;
7458 cfg->has_unwind_info_for_epilog = TRUE;
7460 /* Mark the start of the epilog */
7461 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7463 /* Save the uwind state which is needed by the out-of-line code */
7464 mono_emit_unwind_op_remember_state (cfg, code);
7466 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7467 code = (guint8 *)mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7469 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7471 if (method->save_lmf) {
7472 /* check if we need to restore protection of the stack after a stack overflow */
7473 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7475 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7476 /* we load the value in a separate instruction: this mechanism may be
7477 * used later as a safer way to do thread interruption
7479 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7480 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7482 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7483 /* note that the call trampoline will preserve eax/edx */
7484 x86_call_reg (code, X86_ECX);
7485 x86_patch (patch, code);
7487 /* FIXME: maybe save the jit tls in the prolog */
7489 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7490 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7494 /* Restore callee saved regs */
7495 for (i = 0; i < AMD64_NREG; ++i) {
7496 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7497 /* Restore only used_int_regs, not arch.saved_iregs */
7498 if (cfg->used_int_regs & (1 << i)) {
7499 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7500 mono_emit_unwind_op_same_value (cfg, code, i);
7501 async_exc_point (code);
7503 save_area_offset += 8;
7507 /* Load returned vtypes into registers if needed */
7508 cinfo = (CallInfo *)cfg->arch.cinfo;
7509 if (cinfo->ret.storage == ArgValuetypeInReg) {
7510 ArgInfo *ainfo = &cinfo->ret;
7511 MonoInst *inst = cfg->ret;
7513 for (quad = 0; quad < 2; quad ++) {
7514 switch (ainfo->pair_storage [quad]) {
7516 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7518 case ArgInFloatSSEReg:
7519 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7521 case ArgInDoubleSSEReg:
7522 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7527 g_assert_not_reached ();
7532 if (cfg->arch.omit_fp) {
7533 if (cfg->arch.stack_alloc_size) {
7534 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7538 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7540 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7541 async_exc_point (code);
7544 /* Restore the unwind state to be the same as before the epilog */
7545 mono_emit_unwind_op_restore_state (cfg, code);
7547 cfg->code_len = code - cfg->native_code;
7549 g_assert (cfg->code_len < cfg->code_size);
7553 mono_arch_emit_exceptions (MonoCompile *cfg)
7555 MonoJumpInfo *patch_info;
7558 MonoClass *exc_classes [16];
7559 guint8 *exc_throw_start [16], *exc_throw_end [16];
7560 guint32 code_size = 0;
7562 /* Compute needed space */
7563 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7564 if (patch_info->type == MONO_PATCH_INFO_EXC)
7566 if (patch_info->type == MONO_PATCH_INFO_R8)
7567 code_size += 8 + 15; /* sizeof (double) + alignment */
7568 if (patch_info->type == MONO_PATCH_INFO_R4)
7569 code_size += 4 + 15; /* sizeof (float) + alignment */
7570 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7571 code_size += 8 + 7; /*sizeof (void*) + alignment */
7574 #ifdef __native_client_codegen__
7575 /* Give us extra room on Native Client. This could be */
7576 /* more carefully calculated, but bundle alignment makes */
7577 /* it much trickier, so *2 like other places is good. */
7581 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7582 cfg->code_size *= 2;
7583 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7584 cfg->stat_code_reallocs++;
7587 code = cfg->native_code + cfg->code_len;
7589 /* add code to raise exceptions */
7591 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7592 switch (patch_info->type) {
7593 case MONO_PATCH_INFO_EXC: {
7594 MonoClass *exc_class;
7598 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7600 exc_class = mono_class_load_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7601 throw_ip = patch_info->ip.i;
7603 //x86_breakpoint (code);
7604 /* Find a throw sequence for the same exception class */
7605 for (i = 0; i < nthrows; ++i)
7606 if (exc_classes [i] == exc_class)
7609 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7610 x86_jump_code (code, exc_throw_start [i]);
7611 patch_info->type = MONO_PATCH_INFO_NONE;
7615 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7619 exc_classes [nthrows] = exc_class;
7620 exc_throw_start [nthrows] = code;
7622 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7624 patch_info->type = MONO_PATCH_INFO_NONE;
7626 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7628 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7633 exc_throw_end [nthrows] = code;
7643 g_assert(code < cfg->native_code + cfg->code_size);
7646 /* Handle relocations with RIP relative addressing */
7647 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7648 gboolean remove = FALSE;
7649 guint8 *orig_code = code;
7651 switch (patch_info->type) {
7652 case MONO_PATCH_INFO_R8:
7653 case MONO_PATCH_INFO_R4: {
7654 guint8 *pos, *patch_pos;
7657 /* The SSE opcodes require a 16 byte alignment */
7658 #if defined(__default_codegen__)
7659 code = (guint8*)ALIGN_TO (code, 16);
7660 #elif defined(__native_client_codegen__)
7662 /* Pad this out with HLT instructions */
7663 /* or we can get garbage bytes emitted */
7664 /* which will fail validation */
7665 guint8 *aligned_code;
7666 /* extra align to make room for */
7667 /* mov/push below */
7668 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7669 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7670 /* The technique of hiding data in an */
7671 /* instruction has a problem here: we */
7672 /* need the data aligned to a 16-byte */
7673 /* boundary but the instruction cannot */
7674 /* cross the bundle boundary. so only */
7675 /* odd multiples of 16 can be used */
7676 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7679 while (code < aligned_code) {
7680 *(code++) = 0xf4; /* hlt */
7685 pos = cfg->native_code + patch_info->ip.i;
7686 if (IS_REX (pos [1])) {
7687 patch_pos = pos + 5;
7688 target_pos = code - pos - 9;
7691 patch_pos = pos + 4;
7692 target_pos = code - pos - 8;
7695 if (patch_info->type == MONO_PATCH_INFO_R8) {
7696 #ifdef __native_client_codegen__
7697 /* Hide 64-bit data in a */
7698 /* "mov imm64, r11" instruction. */
7699 /* write it before the start of */
7701 *(code-2) = 0x49; /* prefix */
7702 *(code-1) = 0xbb; /* mov X, %r11 */
7704 *(double*)code = *(double*)patch_info->data.target;
7705 code += sizeof (double);
7707 #ifdef __native_client_codegen__
7708 /* Hide 32-bit data in a */
7709 /* "push imm32" instruction. */
7710 *(code-1) = 0x68; /* push */
7712 *(float*)code = *(float*)patch_info->data.target;
7713 code += sizeof (float);
7716 *(guint32*)(patch_pos) = target_pos;
7721 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7724 if (cfg->compile_aot)
7727 /*loading is faster against aligned addresses.*/
7728 code = (guint8*)ALIGN_TO (code, 8);
7729 memset (orig_code, 0, code - orig_code);
7731 pos = cfg->native_code + patch_info->ip.i;
7733 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7734 if (IS_REX (pos [1]))
7735 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7737 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7739 *(gpointer*)code = (gpointer)patch_info->data.target;
7740 code += sizeof (gpointer);
7750 if (patch_info == cfg->patch_info)
7751 cfg->patch_info = patch_info->next;
7755 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7757 tmp->next = patch_info->next;
7760 g_assert (code < cfg->native_code + cfg->code_size);
7763 cfg->code_len = code - cfg->native_code;
7765 g_assert (cfg->code_len < cfg->code_size);
7769 #endif /* DISABLE_JIT */
7772 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7774 guchar *code = (guchar *)p;
7775 MonoMethodSignature *sig;
7777 int i, n, stack_area = 0;
7779 /* Keep this in sync with mono_arch_get_argument_info */
7781 if (enable_arguments) {
7782 /* Allocate a new area on the stack and save arguments there */
7783 sig = mono_method_signature (cfg->method);
7785 n = sig->param_count + sig->hasthis;
7787 stack_area = ALIGN_TO (n * 8, 16);
7789 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7791 for (i = 0; i < n; ++i) {
7792 inst = cfg->args [i];
7794 if (inst->opcode == OP_REGVAR)
7795 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7797 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7798 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7803 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7804 amd64_set_reg_template (code, AMD64_ARG_REG1);
7805 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7806 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7808 if (enable_arguments)
7809 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7823 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7825 guchar *code = (guchar *)p;
7826 int save_mode = SAVE_NONE;
7827 MonoMethod *method = cfg->method;
7828 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
7831 switch (ret_type->type) {
7832 case MONO_TYPE_VOID:
7833 /* special case string .ctor icall */
7834 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7835 save_mode = SAVE_EAX;
7837 save_mode = SAVE_NONE;
7841 save_mode = SAVE_EAX;
7845 save_mode = SAVE_XMM;
7847 case MONO_TYPE_GENERICINST:
7848 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7849 save_mode = SAVE_EAX;
7853 case MONO_TYPE_VALUETYPE:
7854 save_mode = SAVE_STRUCT;
7857 save_mode = SAVE_EAX;
7861 /* Save the result and copy it into the proper argument register */
7862 switch (save_mode) {
7864 amd64_push_reg (code, AMD64_RAX);
7866 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7867 if (enable_arguments)
7868 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7872 if (enable_arguments)
7873 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7876 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7877 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7879 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7881 * The result is already in the proper argument register so no copying
7888 g_assert_not_reached ();
7891 /* Set %al since this is a varargs call */
7892 if (save_mode == SAVE_XMM)
7893 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7895 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7897 if (preserve_argument_registers) {
7898 for (i = 0; i < PARAM_REGS; ++i)
7899 amd64_push_reg (code, param_regs [i]);
7902 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7903 amd64_set_reg_template (code, AMD64_ARG_REG1);
7904 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7906 if (preserve_argument_registers) {
7907 for (i = PARAM_REGS - 1; i >= 0; --i)
7908 amd64_pop_reg (code, param_regs [i]);
7911 /* Restore result */
7912 switch (save_mode) {
7914 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7915 amd64_pop_reg (code, AMD64_RAX);
7921 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7922 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7923 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7928 g_assert_not_reached ();
7935 mono_arch_flush_icache (guint8 *code, gint size)
7941 mono_arch_flush_register_windows (void)
7946 mono_arch_is_inst_imm (gint64 imm)
7948 return amd64_use_imm32 (imm);
7952 * Determine whenever the trap whose info is in SIGINFO is caused by
7956 mono_arch_is_int_overflow (void *sigctx, void *info)
7963 mono_sigctx_to_monoctx (sigctx, &ctx);
7965 rip = (guint8*)ctx.gregs [AMD64_RIP];
7967 if (IS_REX (rip [0])) {
7968 reg = amd64_rex_b (rip [0]);
7974 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7976 reg += x86_modrm_rm (rip [1]);
7978 value = ctx.gregs [reg];
7988 mono_arch_get_patch_offset (guint8 *code)
7994 * mono_breakpoint_clean_code:
7996 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
7997 * breakpoints in the original code, they are removed in the copy.
7999 * Returns TRUE if no sw breakpoint was present.
8002 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
8005 * If method_start is non-NULL we need to perform bound checks, since we access memory
8006 * at code - offset we could go before the start of the method and end up in a different
8007 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
8010 if (!method_start || code - offset >= method_start) {
8011 memcpy (buf, code - offset, size);
8013 int diff = code - method_start;
8014 memset (buf, 0, size);
8015 memcpy (buf + offset - diff, method_start, diff + size - offset);
8020 #if defined(__native_client_codegen__)
8021 /* For membase calls, we want the base register. for Native Client, */
8022 /* all indirect calls have the following sequence with the given sizes: */
8023 /* mov %eXX,%eXX [2-3] */
8024 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
8025 /* and $0xffffffffffffffe0,%r11d [4] */
8026 /* add %r15,%r11 [3] */
8027 /* callq *%r11 [3] */
8030 /* Determine if code points to a NaCl call-through-register sequence, */
8031 /* (i.e., the last 3 instructions listed above) */
8033 is_nacl_call_reg_sequence(guint8* code)
8035 const char *sequence = "\x41\x83\xe3\xe0" /* and */
8036 "\x4d\x03\xdf" /* add */
8037 "\x41\xff\xd3"; /* call */
8038 return memcmp(code, sequence, 10) == 0;
8041 /* Determine if code points to the first opcode of the mov membase component */
8042 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
8043 /* (there could be a REX prefix before the opcode but it is ignored) */
8045 is_nacl_indirect_call_membase_sequence(guint8* code)
8047 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
8048 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
8049 /* and that src reg = dest reg */
8050 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
8051 /* Check that next inst is mov, uses SIB byte (rm = 4), */
8053 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
8054 /* and has dst of r11 and base of r15 */
8055 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
8056 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
8058 #endif /* __native_client_codegen__ */
8061 mono_arch_get_this_arg_reg (guint8 *code)
8063 return AMD64_ARG_REG1;
8067 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
8069 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
8072 #define MAX_ARCH_DELEGATE_PARAMS 10
8075 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, guint32 param_count)
8077 guint8 *code, *start;
8078 GSList *unwind_ops = NULL;
8081 unwind_ops = mono_arch_get_cie_program ();
8084 start = code = (guint8 *)mono_global_codeman_reserve (64);
8086 /* Replace the this argument with the target */
8087 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8088 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8089 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8091 g_assert ((code - start) < 64);
8093 start = code = (guint8 *)mono_global_codeman_reserve (64);
8095 if (param_count == 0) {
8096 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8098 /* We have to shift the arguments left */
8099 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8100 for (i = 0; i < param_count; ++i) {
8103 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8105 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
8107 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8111 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8113 g_assert ((code - start) < 64);
8116 nacl_global_codeman_validate (&start, 64, &code);
8117 mono_arch_flush_icache (start, code - start);
8120 *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
8122 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
8123 *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
8127 if (mono_jit_map_is_enabled ()) {
8130 buff = (char*)"delegate_invoke_has_target";
8132 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
8133 mono_emit_jit_tramp (start, code - start, buff);
8137 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8142 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
8145 get_delegate_virtual_invoke_impl (MonoTrampInfo **info, gboolean load_imt_reg, int offset)
8147 guint8 *code, *start;
8152 if (offset / (int)sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
8155 start = code = (guint8 *)mono_global_codeman_reserve (size);
8157 unwind_ops = mono_arch_get_cie_program ();
8159 /* Replace the this argument with the target */
8160 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8161 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8164 /* Load the IMT reg */
8165 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
8168 /* Load the vtable */
8169 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
8170 amd64_jump_membase (code, AMD64_RAX, offset);
8171 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8174 tramp_name = g_strdup_printf ("delegate_virtual_invoke_imt_%d", - offset / sizeof (gpointer));
8176 tramp_name = g_strdup_printf ("delegate_virtual_invoke_%d", offset / sizeof (gpointer));
8177 *info = mono_tramp_info_create (tramp_name, start, code - start, NULL, unwind_ops);
8178 g_free (tramp_name);
8184 * mono_arch_get_delegate_invoke_impls:
8186 * Return a list of MonoTrampInfo structures for the delegate invoke impl
8190 mono_arch_get_delegate_invoke_impls (void)
8193 MonoTrampInfo *info;
8196 get_delegate_invoke_impl (&info, TRUE, 0);
8197 res = g_slist_prepend (res, info);
8199 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
8200 get_delegate_invoke_impl (&info, FALSE, i);
8201 res = g_slist_prepend (res, info);
8204 for (i = 0; i <= MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
8205 get_delegate_virtual_invoke_impl (&info, TRUE, - i * SIZEOF_VOID_P);
8206 res = g_slist_prepend (res, info);
8208 get_delegate_virtual_invoke_impl (&info, FALSE, i * SIZEOF_VOID_P);
8209 res = g_slist_prepend (res, info);
8216 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
8218 guint8 *code, *start;
8221 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
8224 /* FIXME: Support more cases */
8225 if (MONO_TYPE_ISSTRUCT (mini_get_underlying_type (sig->ret)))
8229 static guint8* cached = NULL;
8234 if (mono_aot_only) {
8235 start = (guint8 *)mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
8237 MonoTrampInfo *info;
8238 start = (guint8 *)get_delegate_invoke_impl (&info, TRUE, 0);
8239 mono_tramp_info_register (info, NULL);
8242 mono_memory_barrier ();
8246 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
8247 for (i = 0; i < sig->param_count; ++i)
8248 if (!mono_is_regsize_var (sig->params [i]))
8250 if (sig->param_count > 4)
8253 code = cache [sig->param_count];
8257 if (mono_aot_only) {
8258 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
8259 start = (guint8 *)mono_aot_get_trampoline (name);
8262 MonoTrampInfo *info;
8263 start = (guint8 *)get_delegate_invoke_impl (&info, FALSE, sig->param_count);
8264 mono_tramp_info_register (info, NULL);
8267 mono_memory_barrier ();
8269 cache [sig->param_count] = start;
8276 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
8278 MonoTrampInfo *info;
8281 code = get_delegate_virtual_invoke_impl (&info, load_imt_reg, offset);
8283 mono_tramp_info_register (info, NULL);
8288 mono_arch_finish_init (void)
8290 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
8291 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
8296 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8300 #if defined(__default_codegen__)
8301 #define CMP_SIZE (6 + 1)
8302 #define CMP_REG_REG_SIZE (4 + 1)
8303 #define BR_SMALL_SIZE 2
8304 #define BR_LARGE_SIZE 6
8305 #define MOV_REG_IMM_SIZE 10
8306 #define MOV_REG_IMM_32BIT_SIZE 6
8307 #define JUMP_REG_SIZE (2 + 1)
8308 #elif defined(__native_client_codegen__)
8309 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8310 #define CMP_SIZE ((6 + 1) * 2 - 1)
8311 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8312 #define BR_SMALL_SIZE (2 * 2 - 1)
8313 #define BR_LARGE_SIZE (6 * 2 - 1)
8314 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8315 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8316 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8317 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8318 /* Jump membase's size is large and unpredictable */
8319 /* in native client, just pad it out a whole bundle. */
8320 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8324 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8326 int i, distance = 0;
8327 for (i = start; i < target; ++i)
8328 distance += imt_entries [i]->chunk_size;
8333 * LOCKING: called with the domain lock held
8336 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8337 gpointer fail_tramp)
8341 guint8 *code, *start;
8342 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8345 for (i = 0; i < count; ++i) {
8346 MonoIMTCheckItem *item = imt_entries [i];
8347 if (item->is_equals) {
8348 if (item->check_target_idx) {
8349 if (!item->compare_done) {
8350 if (amd64_use_imm32 ((gint64)item->key))
8351 item->chunk_size += CMP_SIZE;
8353 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8355 if (item->has_target_code) {
8356 item->chunk_size += MOV_REG_IMM_SIZE;
8358 if (vtable_is_32bit)
8359 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8361 item->chunk_size += MOV_REG_IMM_SIZE;
8362 #ifdef __native_client_codegen__
8363 item->chunk_size += JUMP_MEMBASE_SIZE;
8366 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8369 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8370 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8372 if (vtable_is_32bit)
8373 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8375 item->chunk_size += MOV_REG_IMM_SIZE;
8376 item->chunk_size += JUMP_REG_SIZE;
8377 /* with assert below:
8378 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8380 #ifdef __native_client_codegen__
8381 item->chunk_size += JUMP_MEMBASE_SIZE;
8386 if (amd64_use_imm32 ((gint64)item->key))
8387 item->chunk_size += CMP_SIZE;
8389 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8390 item->chunk_size += BR_LARGE_SIZE;
8391 imt_entries [item->check_target_idx]->compare_done = TRUE;
8393 size += item->chunk_size;
8395 #if defined(__native_client__) && defined(__native_client_codegen__)
8396 /* In Native Client, we don't re-use thunks, allocate from the */
8397 /* normal code manager paths. */
8398 code = mono_domain_code_reserve (domain, size);
8401 code = (guint8 *)mono_method_alloc_generic_virtual_thunk (domain, size);
8403 code = (guint8 *)mono_domain_code_reserve (domain, size);
8407 unwind_ops = mono_arch_get_cie_program ();
8409 for (i = 0; i < count; ++i) {
8410 MonoIMTCheckItem *item = imt_entries [i];
8411 item->code_target = code;
8412 if (item->is_equals) {
8413 gboolean fail_case = !item->check_target_idx && fail_tramp;
8415 if (item->check_target_idx || fail_case) {
8416 if (!item->compare_done || fail_case) {
8417 if (amd64_use_imm32 ((gint64)item->key))
8418 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8420 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof(gpointer));
8421 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8424 item->jmp_code = code;
8425 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8426 if (item->has_target_code) {
8427 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8428 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8430 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8431 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8435 amd64_patch (item->jmp_code, code);
8436 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8437 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8438 item->jmp_code = NULL;
8441 /* enable the commented code to assert on wrong method */
8443 if (amd64_is_imm32 (item->key))
8444 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8446 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8447 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8449 item->jmp_code = code;
8450 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8451 /* See the comment below about R10 */
8452 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8453 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8454 amd64_patch (item->jmp_code, code);
8455 amd64_breakpoint (code);
8456 item->jmp_code = NULL;
8458 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8459 needs to be preserved. R10 needs
8460 to be preserved for calls which
8461 require a runtime generic context,
8462 but interface calls don't. */
8463 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8464 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8468 if (amd64_use_imm32 ((gint64)item->key))
8469 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8471 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof (gpointer));
8472 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8474 item->jmp_code = code;
8475 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8476 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8478 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8480 g_assert (code - item->code_target <= item->chunk_size);
8482 /* patch the branches to get to the target items */
8483 for (i = 0; i < count; ++i) {
8484 MonoIMTCheckItem *item = imt_entries [i];
8485 if (item->jmp_code) {
8486 if (item->check_target_idx) {
8487 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8493 mono_stats.imt_thunks_size += code - start;
8494 g_assert (code - start <= size);
8496 nacl_domain_code_validate(domain, &start, size, &code);
8497 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8499 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
8505 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8507 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8511 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8513 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8517 mono_arch_get_cie_program (void)
8521 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8522 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8530 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8532 MonoInst *ins = NULL;
8535 if (cmethod->klass == mono_defaults.math_class) {
8536 if (strcmp (cmethod->name, "Sin") == 0) {
8538 } else if (strcmp (cmethod->name, "Cos") == 0) {
8540 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8542 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8546 if (opcode && fsig->param_count == 1) {
8547 MONO_INST_NEW (cfg, ins, opcode);
8548 ins->type = STACK_R8;
8549 ins->dreg = mono_alloc_freg (cfg);
8550 ins->sreg1 = args [0]->dreg;
8551 MONO_ADD_INS (cfg->cbb, ins);
8555 if (cfg->opt & MONO_OPT_CMOV) {
8556 if (strcmp (cmethod->name, "Min") == 0) {
8557 if (fsig->params [0]->type == MONO_TYPE_I4)
8559 if (fsig->params [0]->type == MONO_TYPE_U4)
8560 opcode = OP_IMIN_UN;
8561 else if (fsig->params [0]->type == MONO_TYPE_I8)
8563 else if (fsig->params [0]->type == MONO_TYPE_U8)
8564 opcode = OP_LMIN_UN;
8565 } else if (strcmp (cmethod->name, "Max") == 0) {
8566 if (fsig->params [0]->type == MONO_TYPE_I4)
8568 if (fsig->params [0]->type == MONO_TYPE_U4)
8569 opcode = OP_IMAX_UN;
8570 else if (fsig->params [0]->type == MONO_TYPE_I8)
8572 else if (fsig->params [0]->type == MONO_TYPE_U8)
8573 opcode = OP_LMAX_UN;
8577 if (opcode && fsig->param_count == 2) {
8578 MONO_INST_NEW (cfg, ins, opcode);
8579 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8580 ins->dreg = mono_alloc_ireg (cfg);
8581 ins->sreg1 = args [0]->dreg;
8582 ins->sreg2 = args [1]->dreg;
8583 MONO_ADD_INS (cfg->cbb, ins);
8587 /* OP_FREM is not IEEE compatible */
8588 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8589 MONO_INST_NEW (cfg, ins, OP_FREM);
8590 ins->inst_i0 = args [0];
8591 ins->inst_i1 = args [1];
8601 mono_arch_print_tree (MonoInst *tree, int arity)
8607 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8609 return ctx->gregs [reg];
8613 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8615 ctx->gregs [reg] = val;
8619 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8621 gpointer *sp, old_value;
8625 bp = (char *)MONO_CONTEXT_GET_BP (ctx);
8626 sp = (gpointer *)*(gpointer*)(bp + clause->exvar_offset);
8629 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8638 * mono_arch_emit_load_aotconst:
8640 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8641 * TARGET from the mscorlib GOT in full-aot code.
8642 * On AMD64, the result is placed into R11.
8645 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, MonoJumpInfoType tramp_type, gconstpointer target)
8647 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8648 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8654 * mono_arch_get_trampolines:
8656 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8660 mono_arch_get_trampolines (gboolean aot)
8662 return mono_amd64_get_exception_trampolines (aot);
8665 /* Soft Debug support */
8666 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8669 * mono_arch_set_breakpoint:
8671 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8672 * The location should contain code emitted by OP_SEQ_POINT.
8675 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8680 guint32 native_offset = ip - (guint8*)ji->code_start;
8681 SeqPointInfo *info = (SeqPointInfo *)mono_arch_get_seq_point_info (mono_domain_get (), (guint8 *)ji->code_start);
8683 g_assert (info->bp_addrs [native_offset] == 0);
8684 info->bp_addrs [native_offset] = mini_get_breakpoint_trampoline ();
8686 /* ip points to a mov r11, 0 */
8687 g_assert (code [0] == 0x41);
8688 g_assert (code [1] == 0xbb);
8689 amd64_mov_reg_imm (code, AMD64_R11, 1);
8694 * mono_arch_clear_breakpoint:
8696 * Clear the breakpoint at IP.
8699 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8704 guint32 native_offset = ip - (guint8*)ji->code_start;
8705 SeqPointInfo *info = (SeqPointInfo *)mono_arch_get_seq_point_info (mono_domain_get (), (guint8 *)ji->code_start);
8707 info->bp_addrs [native_offset] = NULL;
8709 amd64_mov_reg_imm (code, AMD64_R11, 0);
8714 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8716 /* We use soft breakpoints on amd64 */
8721 * mono_arch_skip_breakpoint:
8723 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8724 * we resume, the instruction is not executed again.
8727 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8729 g_assert_not_reached ();
8733 * mono_arch_start_single_stepping:
8735 * Start single stepping.
8738 mono_arch_start_single_stepping (void)
8740 ss_trampoline = mini_get_single_step_trampoline ();
8744 * mono_arch_stop_single_stepping:
8746 * Stop single stepping.
8749 mono_arch_stop_single_stepping (void)
8751 ss_trampoline = NULL;
8755 * mono_arch_is_single_step_event:
8757 * Return whenever the machine state in SIGCTX corresponds to a single
8761 mono_arch_is_single_step_event (void *info, void *sigctx)
8763 /* We use soft breakpoints on amd64 */
8768 * mono_arch_skip_single_step:
8770 * Modify CTX so the ip is placed after the single step trigger instruction,
8771 * we resume, the instruction is not executed again.
8774 mono_arch_skip_single_step (MonoContext *ctx)
8776 g_assert_not_reached ();
8780 * mono_arch_create_seq_point_info:
8782 * Return a pointer to a data structure which is used by the sequence
8783 * point implementation in AOTed code.
8786 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8791 // FIXME: Add a free function
8793 mono_domain_lock (domain);
8794 info = (SeqPointInfo *)g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8796 mono_domain_unlock (domain);
8799 ji = mono_jit_info_table_find (domain, (char*)code);
8802 // FIXME: Optimize the size
8803 info = (SeqPointInfo *)g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8805 info->ss_tramp_addr = &ss_trampoline;
8807 mono_domain_lock (domain);
8808 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8810 mono_domain_unlock (domain);
8817 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8819 ext->lmf.previous_lmf = prev_lmf;
8820 /* Mark that this is a MonoLMFExt */
8821 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8822 ext->lmf.rsp = (gssize)ext;
8828 mono_arch_opcode_supported (int opcode)
8831 case OP_ATOMIC_ADD_I4:
8832 case OP_ATOMIC_ADD_I8:
8833 case OP_ATOMIC_EXCHANGE_I4:
8834 case OP_ATOMIC_EXCHANGE_I8:
8835 case OP_ATOMIC_CAS_I4:
8836 case OP_ATOMIC_CAS_I8:
8837 case OP_ATOMIC_LOAD_I1:
8838 case OP_ATOMIC_LOAD_I2:
8839 case OP_ATOMIC_LOAD_I4:
8840 case OP_ATOMIC_LOAD_I8:
8841 case OP_ATOMIC_LOAD_U1:
8842 case OP_ATOMIC_LOAD_U2:
8843 case OP_ATOMIC_LOAD_U4:
8844 case OP_ATOMIC_LOAD_U8:
8845 case OP_ATOMIC_LOAD_R4:
8846 case OP_ATOMIC_LOAD_R8:
8847 case OP_ATOMIC_STORE_I1:
8848 case OP_ATOMIC_STORE_I2:
8849 case OP_ATOMIC_STORE_I4:
8850 case OP_ATOMIC_STORE_I8:
8851 case OP_ATOMIC_STORE_U1:
8852 case OP_ATOMIC_STORE_U2:
8853 case OP_ATOMIC_STORE_U4:
8854 case OP_ATOMIC_STORE_U8:
8855 case OP_ATOMIC_STORE_R4:
8856 case OP_ATOMIC_STORE_R8:
8863 #if defined(ENABLE_GSHAREDVT) && defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
8865 #include "../../../mono-extensions/mono/mini/mini-amd64-gsharedvt.c"
8867 #endif /* !ENABLE_GSHAREDVT */