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 */
507 /* Only if storage == ArgOnStack */
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 static AMD64_Reg_No param_regs [] = { AMD64_RCX, AMD64_RDX, AMD64_R8, AMD64_R9 };
529 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
531 static AMD64_Reg_No param_regs [] = { AMD64_RDI, AMD64_RSI, AMD64_RDX, AMD64_RCX, AMD64_R8, AMD64_R9 };
533 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
537 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
539 ainfo->offset = *stack_size;
541 if (*gr >= PARAM_REGS) {
542 ainfo->storage = ArgOnStack;
543 ainfo->arg_size = sizeof (mgreg_t);
544 /* Since the same stack slot size is used for all arg */
545 /* types, it needs to be big enough to hold them all */
546 (*stack_size) += sizeof(mgreg_t);
549 ainfo->storage = ArgInIReg;
550 ainfo->reg = param_regs [*gr];
556 #define FLOAT_PARAM_REGS 4
558 #define FLOAT_PARAM_REGS 8
562 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
564 ainfo->offset = *stack_size;
566 if (*gr >= FLOAT_PARAM_REGS) {
567 ainfo->storage = ArgOnStack;
568 ainfo->arg_size = sizeof (mgreg_t);
569 /* Since the same stack slot size is used for both float */
570 /* types, it needs to be big enough to hold them both */
571 (*stack_size) += sizeof(mgreg_t);
574 /* A double register */
576 ainfo->storage = ArgInDoubleSSEReg;
578 ainfo->storage = ArgInFloatSSEReg;
584 typedef enum ArgumentClass {
592 merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
594 ArgumentClass class2 = ARG_CLASS_NO_CLASS;
597 ptype = mini_get_underlying_type (type);
598 switch (ptype->type) {
607 case MONO_TYPE_STRING:
608 case MONO_TYPE_OBJECT:
609 case MONO_TYPE_CLASS:
610 case MONO_TYPE_SZARRAY:
612 case MONO_TYPE_FNPTR:
613 case MONO_TYPE_ARRAY:
616 class2 = ARG_CLASS_INTEGER;
621 class2 = ARG_CLASS_INTEGER;
623 class2 = ARG_CLASS_SSE;
627 case MONO_TYPE_TYPEDBYREF:
628 g_assert_not_reached ();
630 case MONO_TYPE_GENERICINST:
631 if (!mono_type_generic_inst_is_valuetype (ptype)) {
632 class2 = ARG_CLASS_INTEGER;
636 case MONO_TYPE_VALUETYPE: {
637 MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
640 for (i = 0; i < info->num_fields; ++i) {
642 class2 = merge_argument_class_from_type (info->fields [i].field->type, class2);
647 g_assert_not_reached ();
651 if (class1 == class2)
653 else if (class1 == ARG_CLASS_NO_CLASS)
655 else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
656 class1 = ARG_CLASS_MEMORY;
657 else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
658 class1 = ARG_CLASS_INTEGER;
660 class1 = ARG_CLASS_SSE;
664 #ifdef __native_client_codegen__
666 /* Default alignment for Native Client is 32-byte. */
667 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
669 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
670 /* Check that alignment doesn't cross an alignment boundary. */
672 mono_arch_nacl_pad(guint8 *code, int pad)
674 const int kMaxPadding = 8; /* see amd64-codegen.h:amd64_padding_size() */
676 if (pad == 0) return code;
677 /* assertion: alignment cannot cross a block boundary */
678 g_assert (((uintptr_t)code & (~kNaClAlignmentMask)) ==
679 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
680 while (pad >= kMaxPadding) {
681 amd64_padding (code, kMaxPadding);
684 if (pad != 0) amd64_padding (code, pad);
690 count_fields_nested (MonoClass *klass)
692 MonoMarshalType *info;
695 info = mono_marshal_load_type_info (klass);
698 for (i = 0; i < info->num_fields; ++i) {
699 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type))
700 count += count_fields_nested (mono_class_from_mono_type (info->fields [i].field->type));
708 collect_field_info_nested (MonoClass *klass, MonoMarshalField *fields, int index, int offset)
710 MonoMarshalType *info;
713 info = mono_marshal_load_type_info (klass);
715 for (i = 0; i < info->num_fields; ++i) {
716 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type)) {
717 index = collect_field_info_nested (mono_class_from_mono_type (info->fields [i].field->type), fields, index, info->fields [i].offset);
719 memcpy (&fields [index], &info->fields [i], sizeof (MonoMarshalField));
720 fields [index].offset += offset;
729 add_valuetype_win64 (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
731 guint32 *gr, guint32 *fr, guint32 *stack_size)
733 guint32 size, i, nfields;
735 ArgumentClass arg_class;
736 MonoMarshalType *info = NULL;
737 MonoMarshalField *fields = NULL;
739 gboolean pass_on_stack = FALSE;
741 klass = mono_class_from_mono_type (type);
742 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
744 pass_on_stack = TRUE;
746 /* If this struct can't be split up naturally into 8-byte */
747 /* chunks (registers), pass it on the stack. */
748 if (sig->pinvoke && !pass_on_stack) {
752 info = mono_marshal_load_type_info (klass);
756 * Collect field information recursively to be able to
757 * handle nested structures.
759 nfields = count_fields_nested (klass);
760 fields = g_new0 (MonoMarshalField, nfields);
761 collect_field_info_nested (klass, fields, 0, 0);
763 for (i = 0; i < nfields; ++i) {
764 field_size = mono_marshal_type_size (fields [i].field->type,
766 &align, TRUE, klass->unicode);
767 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
768 pass_on_stack = TRUE;
775 /* Allways pass in memory */
776 ainfo->offset = *stack_size;
777 *stack_size += ALIGN_TO (size, 8);
778 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
780 ainfo->arg_size = ALIGN_TO (size, 8);
787 int n = mono_class_value_size (klass, NULL);
792 arg_class = ARG_CLASS_MEMORY;
794 /* Always pass in 1 integer register */
795 arg_class = ARG_CLASS_INTEGER;
800 ainfo->storage = ArgValuetypeInReg;
801 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
805 switch (info->native_size) {
806 case 1: case 2: case 4: case 8:
810 ainfo->storage = ArgValuetypeAddrInIReg;
811 ainfo->offset = *stack_size;
812 *stack_size += ALIGN_TO (info->native_size, 8);
815 ainfo->storage = ArgValuetypeAddrInIReg;
817 if (*gr < PARAM_REGS) {
818 ainfo->pair_storage [0] = ArgInIReg;
819 ainfo->pair_regs [0] = param_regs [*gr];
823 ainfo->pair_storage [0] = ArgOnStack;
824 ainfo->offset = *stack_size;
825 ainfo->arg_size = sizeof (mgreg_t);
836 ArgumentClass class1;
839 class1 = ARG_CLASS_MEMORY;
841 class1 = ARG_CLASS_NO_CLASS;
842 for (i = 0; i < nfields; ++i) {
843 size = mono_marshal_type_size (fields [i].field->type,
845 &align, TRUE, klass->unicode);
846 /* How far into this quad this data extends.*/
847 /* (8 is size of quad) */
848 argsize = fields [i].offset + size;
850 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
852 g_assert (class1 != ARG_CLASS_NO_CLASS);
858 /* Allocate registers */
863 while (argsize != 1 && argsize != 2 && argsize != 4 && argsize != 8)
866 ainfo->storage = ArgValuetypeInReg;
867 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
868 ainfo->pair_size [0] = argsize;
869 ainfo->pair_size [1] = 0;
872 case ARG_CLASS_INTEGER:
873 if (*gr >= PARAM_REGS)
874 arg_class = ARG_CLASS_MEMORY;
876 ainfo->pair_storage [0] = ArgInIReg;
878 ainfo->pair_regs [0] = return_regs [*gr];
880 ainfo->pair_regs [0] = param_regs [*gr];
885 if (*fr >= FLOAT_PARAM_REGS)
886 arg_class = ARG_CLASS_MEMORY;
889 ainfo->pair_storage [0] = ArgInFloatSSEReg;
891 ainfo->pair_storage [0] = ArgInDoubleSSEReg;
892 ainfo->pair_regs [0] = *fr;
896 case ARG_CLASS_MEMORY:
899 g_assert_not_reached ();
902 if (arg_class == ARG_CLASS_MEMORY) {
903 /* Revert possible register assignments */
907 ainfo->offset = *stack_size;
908 *stack_size += sizeof (mgreg_t);
909 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
911 ainfo->arg_size = sizeof (mgreg_t);
915 #endif /* TARGET_WIN32 */
918 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
920 guint32 *gr, guint32 *fr, guint32 *stack_size)
923 add_valuetype_win64 (sig, ainfo, type, is_return, gr, fr, stack_size);
925 guint32 size, quad, nquads, i, nfields;
926 /* Keep track of the size used in each quad so we can */
927 /* use the right size when copying args/return vars. */
928 guint32 quadsize [2] = {8, 8};
929 ArgumentClass args [2];
930 MonoMarshalType *info = NULL;
931 MonoMarshalField *fields = NULL;
933 gboolean pass_on_stack = FALSE;
935 klass = mono_class_from_mono_type (type);
936 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
937 if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
938 /* We pass and return vtypes of size 8 in a register */
939 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
940 pass_on_stack = TRUE;
943 /* If this struct can't be split up naturally into 8-byte */
944 /* chunks (registers), pass it on the stack. */
945 if (sig->pinvoke && !pass_on_stack) {
949 info = mono_marshal_load_type_info (klass);
953 * Collect field information recursively to be able to
954 * handle nested structures.
956 nfields = count_fields_nested (klass);
957 fields = g_new0 (MonoMarshalField, nfields);
958 collect_field_info_nested (klass, fields, 0, 0);
960 for (i = 0; i < nfields; ++i) {
961 field_size = mono_marshal_type_size (fields [i].field->type,
963 &align, TRUE, klass->unicode);
964 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
965 pass_on_stack = TRUE;
972 ainfo->storage = ArgValuetypeInReg;
973 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
978 /* Allways pass in memory */
979 ainfo->offset = *stack_size;
980 *stack_size += ALIGN_TO (size, 8);
981 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
983 ainfo->arg_size = ALIGN_TO (size, 8);
995 int n = mono_class_value_size (klass, NULL);
997 quadsize [0] = n >= 8 ? 8 : n;
998 quadsize [1] = n >= 8 ? MAX (n - 8, 8) : 0;
1000 /* Always pass in 1 or 2 integer registers */
1001 args [0] = ARG_CLASS_INTEGER;
1002 args [1] = ARG_CLASS_INTEGER;
1003 /* Only the simplest cases are supported */
1004 if (is_return && nquads != 1) {
1005 args [0] = ARG_CLASS_MEMORY;
1006 args [1] = ARG_CLASS_MEMORY;
1010 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
1011 * The X87 and SSEUP stuff is left out since there are no such types in
1017 ainfo->storage = ArgValuetypeInReg;
1018 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
1022 if (info->native_size > 16) {
1023 ainfo->offset = *stack_size;
1024 *stack_size += ALIGN_TO (info->native_size, 8);
1025 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
1027 ainfo->arg_size = ALIGN_TO (info->native_size, 8);
1033 args [0] = ARG_CLASS_NO_CLASS;
1034 args [1] = ARG_CLASS_NO_CLASS;
1035 for (quad = 0; quad < nquads; ++quad) {
1038 ArgumentClass class1;
1041 class1 = ARG_CLASS_MEMORY;
1043 class1 = ARG_CLASS_NO_CLASS;
1044 for (i = 0; i < nfields; ++i) {
1045 size = mono_marshal_type_size (fields [i].field->type,
1047 &align, TRUE, klass->unicode);
1048 if ((fields [i].offset < 8) && (fields [i].offset + size) > 8) {
1049 /* Unaligned field */
1053 /* Skip fields in other quad */
1054 if ((quad == 0) && (fields [i].offset >= 8))
1056 if ((quad == 1) && (fields [i].offset < 8))
1059 /* How far into this quad this data extends.*/
1060 /* (8 is size of quad) */
1061 quadsize [quad] = fields [i].offset + size - (quad * 8);
1063 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
1065 g_assert (class1 != ARG_CLASS_NO_CLASS);
1066 args [quad] = class1;
1072 /* Post merger cleanup */
1073 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
1074 args [0] = args [1] = ARG_CLASS_MEMORY;
1076 /* Allocate registers */
1081 while (quadsize [0] != 1 && quadsize [0] != 2 && quadsize [0] != 4 && quadsize [0] != 8)
1083 while (quadsize [1] != 0 && quadsize [1] != 1 && quadsize [1] != 2 && quadsize [1] != 4 && quadsize [1] != 8)
1086 ainfo->storage = ArgValuetypeInReg;
1087 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
1088 g_assert (quadsize [0] <= 8);
1089 g_assert (quadsize [1] <= 8);
1090 ainfo->pair_size [0] = quadsize [0];
1091 ainfo->pair_size [1] = quadsize [1];
1092 ainfo->nregs = nquads;
1093 for (quad = 0; quad < nquads; ++quad) {
1094 switch (args [quad]) {
1095 case ARG_CLASS_INTEGER:
1096 if (*gr >= PARAM_REGS)
1097 args [quad] = ARG_CLASS_MEMORY;
1099 ainfo->pair_storage [quad] = ArgInIReg;
1101 ainfo->pair_regs [quad] = return_regs [*gr];
1103 ainfo->pair_regs [quad] = param_regs [*gr];
1108 if (*fr >= FLOAT_PARAM_REGS)
1109 args [quad] = ARG_CLASS_MEMORY;
1111 if (quadsize[quad] <= 4)
1112 ainfo->pair_storage [quad] = ArgInFloatSSEReg;
1113 else ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
1114 ainfo->pair_regs [quad] = *fr;
1118 case ARG_CLASS_MEMORY:
1121 g_assert_not_reached ();
1125 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
1127 /* Revert possible register assignments */
1131 ainfo->offset = *stack_size;
1133 arg_size = ALIGN_TO (info->native_size, 8);
1135 arg_size = nquads * sizeof(mgreg_t);
1136 *stack_size += arg_size;
1137 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
1139 ainfo->arg_size = arg_size;
1142 #endif /* !TARGET_WIN32 */
1148 * Obtain information about a call according to the calling convention.
1149 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
1150 * Draft Version 0.23" document for more information.
1153 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
1155 guint32 i, gr, fr, pstart;
1157 int n = sig->hasthis + sig->param_count;
1158 guint32 stack_size = 0;
1160 gboolean is_pinvoke = sig->pinvoke;
1163 cinfo = (CallInfo *)mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1165 cinfo = (CallInfo *)g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1173 /* Reserve space where the callee can save the argument registers */
1174 stack_size = 4 * sizeof (mgreg_t);
1178 ret_type = mini_get_underlying_type (sig->ret);
1179 switch (ret_type->type) {
1189 case MONO_TYPE_FNPTR:
1190 case MONO_TYPE_CLASS:
1191 case MONO_TYPE_OBJECT:
1192 case MONO_TYPE_SZARRAY:
1193 case MONO_TYPE_ARRAY:
1194 case MONO_TYPE_STRING:
1195 cinfo->ret.storage = ArgInIReg;
1196 cinfo->ret.reg = AMD64_RAX;
1200 cinfo->ret.storage = ArgInIReg;
1201 cinfo->ret.reg = AMD64_RAX;
1204 cinfo->ret.storage = ArgInFloatSSEReg;
1205 cinfo->ret.reg = AMD64_XMM0;
1208 cinfo->ret.storage = ArgInDoubleSSEReg;
1209 cinfo->ret.reg = AMD64_XMM0;
1211 case MONO_TYPE_GENERICINST:
1212 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1213 cinfo->ret.storage = ArgInIReg;
1214 cinfo->ret.reg = AMD64_RAX;
1217 if (mini_is_gsharedvt_type (ret_type)) {
1218 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1219 cinfo->ret.is_gsharedvt_return_value = 1;
1223 case MONO_TYPE_VALUETYPE:
1224 case MONO_TYPE_TYPEDBYREF: {
1225 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
1227 add_valuetype (sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
1228 g_assert (cinfo->ret.storage != ArgInIReg);
1232 case MONO_TYPE_MVAR:
1233 g_assert (mini_is_gsharedvt_type (ret_type));
1234 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1235 cinfo->ret.is_gsharedvt_return_value = 1;
1237 case MONO_TYPE_VOID:
1240 g_error ("Can't handle as return value 0x%x", ret_type->type);
1245 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1246 * the first argument, allowing 'this' to be always passed in the first arg reg.
1247 * Also do this if the first argument is a reference type, since virtual calls
1248 * are sometimes made using calli without sig->hasthis set, like in the delegate
1251 if (cinfo->ret.storage == ArgValuetypeAddrInIReg && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
1253 add_general (&gr, &stack_size, cinfo->args + 0);
1255 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
1258 add_general (&gr, &stack_size, &cinfo->ret);
1259 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1260 cinfo->vret_arg_index = 1;
1264 add_general (&gr, &stack_size, cinfo->args + 0);
1266 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
1267 add_general (&gr, &stack_size, &cinfo->ret);
1268 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1272 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
1274 fr = FLOAT_PARAM_REGS;
1276 /* Emit the signature cookie just before the implicit arguments */
1277 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1280 for (i = pstart; i < sig->param_count; ++i) {
1281 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
1285 /* The float param registers and other param registers must be the same index on Windows x64.*/
1292 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1293 /* We allways pass the sig cookie on the stack for simplicity */
1295 * Prevent implicit arguments + the sig cookie from being passed
1299 fr = FLOAT_PARAM_REGS;
1301 /* Emit the signature cookie just before the implicit arguments */
1302 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1305 ptype = mini_get_underlying_type (sig->params [i]);
1306 switch (ptype->type) {
1309 add_general (&gr, &stack_size, ainfo);
1313 add_general (&gr, &stack_size, ainfo);
1317 add_general (&gr, &stack_size, ainfo);
1322 case MONO_TYPE_FNPTR:
1323 case MONO_TYPE_CLASS:
1324 case MONO_TYPE_OBJECT:
1325 case MONO_TYPE_STRING:
1326 case MONO_TYPE_SZARRAY:
1327 case MONO_TYPE_ARRAY:
1328 add_general (&gr, &stack_size, ainfo);
1330 case MONO_TYPE_GENERICINST:
1331 if (!mono_type_generic_inst_is_valuetype (ptype)) {
1332 add_general (&gr, &stack_size, ainfo);
1335 if (mini_is_gsharedvt_variable_type (ptype)) {
1336 /* gsharedvt arguments are passed by ref */
1337 add_general (&gr, &stack_size, ainfo);
1338 if (ainfo->storage == ArgInIReg)
1339 ainfo->storage = ArgGSharedVtInReg;
1341 ainfo->storage = ArgGSharedVtOnStack;
1345 case MONO_TYPE_VALUETYPE:
1346 case MONO_TYPE_TYPEDBYREF:
1347 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
1352 add_general (&gr, &stack_size, ainfo);
1355 add_float (&fr, &stack_size, ainfo, FALSE);
1358 add_float (&fr, &stack_size, ainfo, TRUE);
1361 case MONO_TYPE_MVAR:
1362 /* gsharedvt arguments are passed by ref */
1363 g_assert (mini_is_gsharedvt_type (ptype));
1364 add_general (&gr, &stack_size, ainfo);
1365 if (ainfo->storage == ArgInIReg)
1366 ainfo->storage = ArgGSharedVtInReg;
1368 ainfo->storage = ArgGSharedVtOnStack;
1371 g_assert_not_reached ();
1375 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
1377 fr = FLOAT_PARAM_REGS;
1379 /* Emit the signature cookie just before the implicit arguments */
1380 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1383 cinfo->stack_usage = stack_size;
1384 cinfo->reg_usage = gr;
1385 cinfo->freg_usage = fr;
1390 * mono_arch_get_argument_info:
1391 * @csig: a method signature
1392 * @param_count: the number of parameters to consider
1393 * @arg_info: an array to store the result infos
1395 * Gathers information on parameters such as size, alignment and
1396 * padding. arg_info should be large enought to hold param_count + 1 entries.
1398 * Returns the size of the argument area on the stack.
1401 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
1404 CallInfo *cinfo = get_call_info (NULL, csig);
1405 guint32 args_size = cinfo->stack_usage;
1407 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
1408 if (csig->hasthis) {
1409 arg_info [0].offset = 0;
1412 for (k = 0; k < param_count; k++) {
1413 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
1415 arg_info [k + 1].size = 0;
1424 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
1428 MonoType *callee_ret;
1430 c1 = get_call_info (NULL, caller_sig);
1431 c2 = get_call_info (NULL, callee_sig);
1432 res = c1->stack_usage >= c2->stack_usage;
1433 callee_ret = mini_get_underlying_type (callee_sig->ret);
1434 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
1435 /* An address on the callee's stack is passed as the first argument */
1445 * Initialize the cpu to execute managed code.
1448 mono_arch_cpu_init (void)
1453 /* spec compliance requires running with double precision */
1454 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1455 fpcw &= ~X86_FPCW_PRECC_MASK;
1456 fpcw |= X86_FPCW_PREC_DOUBLE;
1457 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
1458 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1460 /* TODO: This is crashing on Win64 right now.
1461 * _control87 (_PC_53, MCW_PC);
1467 * Initialize architecture specific code.
1470 mono_arch_init (void)
1472 mono_os_mutex_init_recursive (&mini_arch_mutex);
1473 #if defined(__native_client_codegen__)
1474 mono_native_tls_alloc (&nacl_instruction_depth, NULL);
1475 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)0);
1476 mono_native_tls_alloc (&nacl_rex_tag, NULL);
1477 mono_native_tls_alloc (&nacl_legacy_prefix_tag, NULL);
1480 mono_aot_register_jit_icall ("mono_amd64_throw_exception", mono_amd64_throw_exception);
1481 mono_aot_register_jit_icall ("mono_amd64_throw_corlib_exception", mono_amd64_throw_corlib_exception);
1482 mono_aot_register_jit_icall ("mono_amd64_resume_unwind", mono_amd64_resume_unwind);
1483 mono_aot_register_jit_icall ("mono_amd64_get_original_ip", mono_amd64_get_original_ip);
1484 #if defined(ENABLE_GSHAREDVT)
1485 mono_aot_register_jit_icall ("mono_amd64_start_gsharedvt_call", mono_amd64_start_gsharedvt_call);
1489 bp_trampoline = mini_get_breakpoint_trampoline ();
1493 * Cleanup architecture specific code.
1496 mono_arch_cleanup (void)
1498 mono_os_mutex_destroy (&mini_arch_mutex);
1499 #if defined(__native_client_codegen__)
1500 mono_native_tls_free (nacl_instruction_depth);
1501 mono_native_tls_free (nacl_rex_tag);
1502 mono_native_tls_free (nacl_legacy_prefix_tag);
1507 * This function returns the optimizations supported on this cpu.
1510 mono_arch_cpu_optimizations (guint32 *exclude_mask)
1516 if (mono_hwcap_x86_has_cmov) {
1517 opts |= MONO_OPT_CMOV;
1519 if (mono_hwcap_x86_has_fcmov)
1520 opts |= MONO_OPT_FCMOV;
1522 *exclude_mask |= MONO_OPT_FCMOV;
1524 *exclude_mask |= MONO_OPT_CMOV;
1531 * This function test for all SSE functions supported.
1533 * Returns a bitmask corresponding to all supported versions.
1537 mono_arch_cpu_enumerate_simd_versions (void)
1539 guint32 sse_opts = 0;
1541 if (mono_hwcap_x86_has_sse1)
1542 sse_opts |= SIMD_VERSION_SSE1;
1544 if (mono_hwcap_x86_has_sse2)
1545 sse_opts |= SIMD_VERSION_SSE2;
1547 if (mono_hwcap_x86_has_sse3)
1548 sse_opts |= SIMD_VERSION_SSE3;
1550 if (mono_hwcap_x86_has_ssse3)
1551 sse_opts |= SIMD_VERSION_SSSE3;
1553 if (mono_hwcap_x86_has_sse41)
1554 sse_opts |= SIMD_VERSION_SSE41;
1556 if (mono_hwcap_x86_has_sse42)
1557 sse_opts |= SIMD_VERSION_SSE42;
1559 if (mono_hwcap_x86_has_sse4a)
1560 sse_opts |= SIMD_VERSION_SSE4a;
1568 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1573 for (i = 0; i < cfg->num_varinfo; i++) {
1574 MonoInst *ins = cfg->varinfo [i];
1575 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1578 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1581 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
1582 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1585 if (mono_is_regsize_var (ins->inst_vtype)) {
1586 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1587 g_assert (i == vmv->idx);
1588 vars = g_list_prepend (vars, vmv);
1592 vars = mono_varlist_sort (cfg, vars, 0);
1598 * mono_arch_compute_omit_fp:
1600 * Determine whenever the frame pointer can be eliminated.
1603 mono_arch_compute_omit_fp (MonoCompile *cfg)
1605 MonoMethodSignature *sig;
1606 MonoMethodHeader *header;
1610 if (cfg->arch.omit_fp_computed)
1613 header = cfg->header;
1615 sig = mono_method_signature (cfg->method);
1617 if (!cfg->arch.cinfo)
1618 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1619 cinfo = (CallInfo *)cfg->arch.cinfo;
1622 * FIXME: Remove some of the restrictions.
1624 cfg->arch.omit_fp = TRUE;
1625 cfg->arch.omit_fp_computed = TRUE;
1627 #ifdef __native_client_codegen__
1628 /* NaCl modules may not change the value of RBP, so it cannot be */
1629 /* used as a normal register, but it can be used as a frame pointer*/
1630 cfg->disable_omit_fp = TRUE;
1631 cfg->arch.omit_fp = FALSE;
1634 if (cfg->disable_omit_fp)
1635 cfg->arch.omit_fp = FALSE;
1637 if (!debug_omit_fp ())
1638 cfg->arch.omit_fp = FALSE;
1640 if (cfg->method->save_lmf)
1641 cfg->arch.omit_fp = FALSE;
1643 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1644 cfg->arch.omit_fp = FALSE;
1645 if (header->num_clauses)
1646 cfg->arch.omit_fp = FALSE;
1647 if (cfg->param_area)
1648 cfg->arch.omit_fp = FALSE;
1649 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1650 cfg->arch.omit_fp = FALSE;
1651 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1652 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1653 cfg->arch.omit_fp = FALSE;
1654 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1655 ArgInfo *ainfo = &cinfo->args [i];
1657 if (ainfo->storage == ArgOnStack) {
1659 * The stack offset can only be determined when the frame
1662 cfg->arch.omit_fp = FALSE;
1667 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1668 MonoInst *ins = cfg->varinfo [i];
1671 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1676 mono_arch_get_global_int_regs (MonoCompile *cfg)
1680 mono_arch_compute_omit_fp (cfg);
1682 if (cfg->arch.omit_fp)
1683 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1685 /* We use the callee saved registers for global allocation */
1686 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1687 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1688 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1689 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1690 #ifndef __native_client_codegen__
1691 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1694 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1695 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1702 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1707 /* All XMM registers */
1708 for (i = 0; i < 16; ++i)
1709 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1715 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1717 static GList *r = NULL;
1722 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1723 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1724 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1725 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1726 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1727 #ifndef __native_client_codegen__
1728 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1731 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1732 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1733 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1734 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1735 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1736 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1737 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1738 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1740 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1747 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1750 static GList *r = NULL;
1755 for (i = 0; i < AMD64_XMM_NREG; ++i)
1756 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1758 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1765 * mono_arch_regalloc_cost:
1767 * Return the cost, in number of memory references, of the action of
1768 * allocating the variable VMV into a register during global register
1772 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1774 MonoInst *ins = cfg->varinfo [vmv->idx];
1776 if (cfg->method->save_lmf)
1777 /* The register is already saved */
1778 /* substract 1 for the invisible store in the prolog */
1779 return (ins->opcode == OP_ARG) ? 0 : 1;
1782 return (ins->opcode == OP_ARG) ? 1 : 2;
1786 * mono_arch_fill_argument_info:
1788 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1792 mono_arch_fill_argument_info (MonoCompile *cfg)
1795 MonoMethodSignature *sig;
1800 sig = mono_method_signature (cfg->method);
1802 cinfo = (CallInfo *)cfg->arch.cinfo;
1803 sig_ret = mini_get_underlying_type (sig->ret);
1806 * Contrary to mono_arch_allocate_vars (), the information should describe
1807 * where the arguments are at the beginning of the method, not where they can be
1808 * accessed during the execution of the method. The later makes no sense for the
1809 * global register allocator, since a variable can be in more than one location.
1811 switch (cinfo->ret.storage) {
1813 case ArgInFloatSSEReg:
1814 case ArgInDoubleSSEReg:
1815 cfg->ret->opcode = OP_REGVAR;
1816 cfg->ret->inst_c0 = cinfo->ret.reg;
1818 case ArgValuetypeInReg:
1819 cfg->ret->opcode = OP_REGOFFSET;
1820 cfg->ret->inst_basereg = -1;
1821 cfg->ret->inst_offset = -1;
1826 g_assert_not_reached ();
1829 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1830 ArgInfo *ainfo = &cinfo->args [i];
1832 ins = cfg->args [i];
1834 switch (ainfo->storage) {
1836 case ArgInFloatSSEReg:
1837 case ArgInDoubleSSEReg:
1838 ins->opcode = OP_REGVAR;
1839 ins->inst_c0 = ainfo->reg;
1842 ins->opcode = OP_REGOFFSET;
1843 ins->inst_basereg = -1;
1844 ins->inst_offset = -1;
1846 case ArgValuetypeInReg:
1848 ins->opcode = OP_NOP;
1851 g_assert_not_reached ();
1857 mono_arch_allocate_vars (MonoCompile *cfg)
1860 MonoMethodSignature *sig;
1863 guint32 locals_stack_size, locals_stack_align;
1867 sig = mono_method_signature (cfg->method);
1869 cinfo = (CallInfo *)cfg->arch.cinfo;
1870 sig_ret = mini_get_underlying_type (sig->ret);
1872 mono_arch_compute_omit_fp (cfg);
1875 * We use the ABI calling conventions for managed code as well.
1876 * Exception: valuetypes are only sometimes passed or returned in registers.
1880 * The stack looks like this:
1881 * <incoming arguments passed on the stack>
1883 * <lmf/caller saved registers>
1886 * <localloc area> -> grows dynamically
1890 if (cfg->arch.omit_fp) {
1891 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1892 cfg->frame_reg = AMD64_RSP;
1895 /* Locals are allocated backwards from %fp */
1896 cfg->frame_reg = AMD64_RBP;
1900 cfg->arch.saved_iregs = cfg->used_int_regs;
1901 if (cfg->method->save_lmf)
1902 /* Save all callee-saved registers normally, and restore them when unwinding through an LMF */
1903 cfg->arch.saved_iregs |= (1 << AMD64_RBX) | (1 << AMD64_R12) | (1 << AMD64_R13) | (1 << AMD64_R14) | (1 << AMD64_R15);
1905 if (cfg->arch.omit_fp)
1906 cfg->arch.reg_save_area_offset = offset;
1907 /* Reserve space for callee saved registers */
1908 for (i = 0; i < AMD64_NREG; ++i)
1909 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
1910 offset += sizeof(mgreg_t);
1912 if (!cfg->arch.omit_fp)
1913 cfg->arch.reg_save_area_offset = -offset;
1915 if (sig_ret->type != MONO_TYPE_VOID) {
1916 switch (cinfo->ret.storage) {
1918 case ArgInFloatSSEReg:
1919 case ArgInDoubleSSEReg:
1920 cfg->ret->opcode = OP_REGVAR;
1921 cfg->ret->inst_c0 = cinfo->ret.reg;
1922 cfg->ret->dreg = cinfo->ret.reg;
1924 case ArgValuetypeAddrInIReg:
1925 /* The register is volatile */
1926 cfg->vret_addr->opcode = OP_REGOFFSET;
1927 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1928 if (cfg->arch.omit_fp) {
1929 cfg->vret_addr->inst_offset = offset;
1933 cfg->vret_addr->inst_offset = -offset;
1935 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1936 printf ("vret_addr =");
1937 mono_print_ins (cfg->vret_addr);
1940 case ArgValuetypeInReg:
1941 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1942 cfg->ret->opcode = OP_REGOFFSET;
1943 cfg->ret->inst_basereg = cfg->frame_reg;
1944 if (cfg->arch.omit_fp) {
1945 cfg->ret->inst_offset = offset;
1946 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1948 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1949 cfg->ret->inst_offset = - offset;
1953 g_assert_not_reached ();
1957 /* Allocate locals */
1958 offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1959 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1960 char *mname = mono_method_full_name (cfg->method, TRUE);
1961 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s stack is too big.", mname));
1966 if (locals_stack_align) {
1967 offset += (locals_stack_align - 1);
1968 offset &= ~(locals_stack_align - 1);
1970 if (cfg->arch.omit_fp) {
1971 cfg->locals_min_stack_offset = offset;
1972 cfg->locals_max_stack_offset = offset + locals_stack_size;
1974 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1975 cfg->locals_max_stack_offset = - offset;
1978 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1979 if (offsets [i] != -1) {
1980 MonoInst *ins = cfg->varinfo [i];
1981 ins->opcode = OP_REGOFFSET;
1982 ins->inst_basereg = cfg->frame_reg;
1983 if (cfg->arch.omit_fp)
1984 ins->inst_offset = (offset + offsets [i]);
1986 ins->inst_offset = - (offset + offsets [i]);
1987 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1990 offset += locals_stack_size;
1992 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1993 g_assert (!cfg->arch.omit_fp);
1994 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1995 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1998 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1999 ins = cfg->args [i];
2000 if (ins->opcode != OP_REGVAR) {
2001 ArgInfo *ainfo = &cinfo->args [i];
2002 gboolean inreg = TRUE;
2004 /* FIXME: Allocate volatile arguments to registers */
2005 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
2009 * Under AMD64, all registers used to pass arguments to functions
2010 * are volatile across calls.
2011 * FIXME: Optimize this.
2013 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg) || (ainfo->storage == ArgGSharedVtInReg))
2016 ins->opcode = OP_REGOFFSET;
2018 switch (ainfo->storage) {
2020 case ArgInFloatSSEReg:
2021 case ArgInDoubleSSEReg:
2022 case ArgGSharedVtInReg:
2024 ins->opcode = OP_REGVAR;
2025 ins->dreg = ainfo->reg;
2029 case ArgGSharedVtOnStack:
2030 g_assert (!cfg->arch.omit_fp);
2031 ins->opcode = OP_REGOFFSET;
2032 ins->inst_basereg = cfg->frame_reg;
2033 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
2035 case ArgValuetypeInReg:
2037 case ArgValuetypeAddrInIReg: {
2039 g_assert (!cfg->arch.omit_fp);
2041 MONO_INST_NEW (cfg, indir, 0);
2042 indir->opcode = OP_REGOFFSET;
2043 if (ainfo->pair_storage [0] == ArgInIReg) {
2044 indir->inst_basereg = cfg->frame_reg;
2045 offset = ALIGN_TO (offset, sizeof (gpointer));
2046 offset += (sizeof (gpointer));
2047 indir->inst_offset = - offset;
2050 indir->inst_basereg = cfg->frame_reg;
2051 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
2054 ins->opcode = OP_VTARG_ADDR;
2055 ins->inst_left = indir;
2063 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg) && (ainfo->storage != ArgGSharedVtOnStack)) {
2064 ins->opcode = OP_REGOFFSET;
2065 ins->inst_basereg = cfg->frame_reg;
2066 /* These arguments are saved to the stack in the prolog */
2067 offset = ALIGN_TO (offset, sizeof(mgreg_t));
2068 if (cfg->arch.omit_fp) {
2069 ins->inst_offset = offset;
2070 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
2071 // Arguments are yet supported by the stack map creation code
2072 //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
2074 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
2075 ins->inst_offset = - offset;
2076 //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
2082 cfg->stack_offset = offset;
2086 mono_arch_create_vars (MonoCompile *cfg)
2088 MonoMethodSignature *sig;
2092 sig = mono_method_signature (cfg->method);
2094 if (!cfg->arch.cinfo)
2095 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
2096 cinfo = (CallInfo *)cfg->arch.cinfo;
2098 if (cinfo->ret.storage == ArgValuetypeInReg)
2099 cfg->ret_var_is_local = TRUE;
2101 sig_ret = mini_get_underlying_type (sig->ret);
2102 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
2103 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2104 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2105 printf ("vret_addr = ");
2106 mono_print_ins (cfg->vret_addr);
2110 if (cfg->gen_sdb_seq_points) {
2113 if (cfg->compile_aot) {
2114 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2115 ins->flags |= MONO_INST_VOLATILE;
2116 cfg->arch.seq_point_info_var = ins;
2118 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2119 ins->flags |= MONO_INST_VOLATILE;
2120 cfg->arch.ss_tramp_var = ins;
2122 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2123 ins->flags |= MONO_INST_VOLATILE;
2124 cfg->arch.bp_tramp_var = ins;
2127 if (cfg->method->save_lmf)
2128 cfg->create_lmf_var = TRUE;
2130 if (cfg->method->save_lmf) {
2132 #if !defined(TARGET_WIN32)
2133 if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
2134 cfg->lmf_ir_mono_lmf = TRUE;
2140 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2146 MONO_INST_NEW (cfg, ins, OP_MOVE);
2147 ins->dreg = mono_alloc_ireg_copy (cfg, tree->dreg);
2148 ins->sreg1 = tree->dreg;
2149 MONO_ADD_INS (cfg->cbb, ins);
2150 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2152 case ArgInFloatSSEReg:
2153 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
2154 ins->dreg = mono_alloc_freg (cfg);
2155 ins->sreg1 = tree->dreg;
2156 MONO_ADD_INS (cfg->cbb, ins);
2158 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2160 case ArgInDoubleSSEReg:
2161 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2162 ins->dreg = mono_alloc_freg (cfg);
2163 ins->sreg1 = tree->dreg;
2164 MONO_ADD_INS (cfg->cbb, ins);
2166 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2170 g_assert_not_reached ();
2175 arg_storage_to_load_membase (ArgStorage storage)
2179 #if defined(__mono_ilp32__)
2180 return OP_LOADI8_MEMBASE;
2182 return OP_LOAD_MEMBASE;
2184 case ArgInDoubleSSEReg:
2185 return OP_LOADR8_MEMBASE;
2186 case ArgInFloatSSEReg:
2187 return OP_LOADR4_MEMBASE;
2189 g_assert_not_reached ();
2196 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2198 MonoMethodSignature *tmp_sig;
2201 if (call->tail_call)
2204 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
2207 * mono_ArgIterator_Setup assumes the signature cookie is
2208 * passed first and all the arguments which were before it are
2209 * passed on the stack after the signature. So compensate by
2210 * passing a different signature.
2212 tmp_sig = mono_metadata_signature_dup_full (cfg->method->klass->image, call->signature);
2213 tmp_sig->param_count -= call->signature->sentinelpos;
2214 tmp_sig->sentinelpos = 0;
2215 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2217 sig_reg = mono_alloc_ireg (cfg);
2218 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2220 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, cinfo->sig_cookie.offset, sig_reg);
2224 static inline LLVMArgStorage
2225 arg_storage_to_llvm_arg_storage (MonoCompile *cfg, ArgStorage storage)
2229 return LLVMArgInIReg;
2232 case ArgGSharedVtInReg:
2233 case ArgGSharedVtOnStack:
2234 return LLVMArgGSharedVt;
2236 g_assert_not_reached ();
2242 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2248 LLVMCallInfo *linfo;
2249 MonoType *t, *sig_ret;
2251 n = sig->param_count + sig->hasthis;
2252 sig_ret = mini_get_underlying_type (sig->ret);
2254 cinfo = get_call_info (cfg->mempool, sig);
2256 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2259 * LLVM always uses the native ABI while we use our own ABI, the
2260 * only difference is the handling of vtypes:
2261 * - we only pass/receive them in registers in some cases, and only
2262 * in 1 or 2 integer registers.
2264 switch (cinfo->ret.storage) {
2266 linfo->ret.storage = LLVMArgNone;
2269 case ArgInFloatSSEReg:
2270 case ArgInDoubleSSEReg:
2271 linfo->ret.storage = LLVMArgNormal;
2273 case ArgValuetypeInReg: {
2274 ainfo = &cinfo->ret;
2277 (ainfo->pair_storage [0] == ArgInFloatSSEReg || ainfo->pair_storage [0] == ArgInDoubleSSEReg ||
2278 ainfo->pair_storage [1] == ArgInFloatSSEReg || ainfo->pair_storage [1] == ArgInDoubleSSEReg)) {
2279 cfg->exception_message = g_strdup ("pinvoke + vtype ret");
2280 cfg->disable_llvm = TRUE;
2284 linfo->ret.storage = LLVMArgVtypeInReg;
2285 for (j = 0; j < 2; ++j)
2286 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2289 case ArgValuetypeAddrInIReg:
2290 /* Vtype returned using a hidden argument */
2291 linfo->ret.storage = LLVMArgVtypeRetAddr;
2292 linfo->vret_arg_index = cinfo->vret_arg_index;
2295 g_assert_not_reached ();
2299 for (i = 0; i < n; ++i) {
2300 ainfo = cinfo->args + i;
2302 if (i >= sig->hasthis)
2303 t = sig->params [i - sig->hasthis];
2305 t = &mono_defaults.int_class->byval_arg;
2307 linfo->args [i].storage = LLVMArgNone;
2309 switch (ainfo->storage) {
2311 linfo->args [i].storage = LLVMArgNormal;
2313 case ArgInDoubleSSEReg:
2314 case ArgInFloatSSEReg:
2315 linfo->args [i].storage = LLVMArgNormal;
2318 if (MONO_TYPE_ISSTRUCT (t))
2319 linfo->args [i].storage = LLVMArgVtypeByVal;
2321 linfo->args [i].storage = LLVMArgNormal;
2323 case ArgValuetypeInReg:
2325 (ainfo->pair_storage [0] == ArgInFloatSSEReg || ainfo->pair_storage [0] == ArgInDoubleSSEReg ||
2326 ainfo->pair_storage [1] == ArgInFloatSSEReg || ainfo->pair_storage [1] == ArgInDoubleSSEReg)) {
2327 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2328 cfg->disable_llvm = TRUE;
2332 linfo->args [i].storage = LLVMArgVtypeInReg;
2333 for (j = 0; j < 2; ++j)
2334 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2336 case ArgGSharedVtInReg:
2337 case ArgGSharedVtOnStack:
2338 linfo->args [i].storage = LLVMArgGSharedVt;
2341 cfg->exception_message = g_strdup ("ainfo->storage");
2342 cfg->disable_llvm = TRUE;
2352 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2355 MonoMethodSignature *sig;
2361 sig = call->signature;
2362 n = sig->param_count + sig->hasthis;
2364 cinfo = get_call_info (cfg->mempool, sig);
2368 if (COMPILE_LLVM (cfg)) {
2369 /* We shouldn't be called in the llvm case */
2370 cfg->disable_llvm = TRUE;
2375 * Emit all arguments which are passed on the stack to prevent register
2376 * allocation problems.
2378 for (i = 0; i < n; ++i) {
2380 ainfo = cinfo->args + i;
2382 in = call->args [i];
2384 if (sig->hasthis && i == 0)
2385 t = &mono_defaults.object_class->byval_arg;
2387 t = sig->params [i - sig->hasthis];
2389 t = mini_get_underlying_type (t);
2390 //XXX what about ArgGSharedVtOnStack here?
2391 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call) {
2393 if (t->type == MONO_TYPE_R4)
2394 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2395 else if (t->type == MONO_TYPE_R8)
2396 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2398 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2400 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2402 if (cfg->compute_gc_maps) {
2405 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2411 * Emit all parameters passed in registers in non-reverse order for better readability
2412 * and to help the optimization in emit_prolog ().
2414 for (i = 0; i < n; ++i) {
2415 ainfo = cinfo->args + i;
2417 in = call->args [i];
2419 if (ainfo->storage == ArgInIReg)
2420 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2423 for (i = n - 1; i >= 0; --i) {
2426 ainfo = cinfo->args + i;
2428 in = call->args [i];
2430 if (sig->hasthis && i == 0)
2431 t = &mono_defaults.object_class->byval_arg;
2433 t = sig->params [i - sig->hasthis];
2434 t = mini_get_underlying_type (t);
2436 switch (ainfo->storage) {
2440 case ArgInFloatSSEReg:
2441 case ArgInDoubleSSEReg:
2442 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2445 case ArgValuetypeInReg:
2446 case ArgValuetypeAddrInIReg:
2447 case ArgGSharedVtInReg:
2448 case ArgGSharedVtOnStack: {
2449 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call)
2450 /* Already emitted above */
2452 //FIXME what about ArgGSharedVtOnStack ?
2453 if (ainfo->storage == ArgOnStack && call->tail_call) {
2454 MonoInst *call_inst = (MonoInst*)call;
2455 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2456 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2464 size = mono_type_native_stack_size (t, &align);
2467 * Other backends use mono_type_stack_size (), but that
2468 * aligns the size to 8, which is larger than the size of
2469 * the source, leading to reads of invalid memory if the
2470 * source is at the end of address space.
2472 size = mono_class_value_size (mono_class_from_mono_type (t), &align);
2475 if (size >= 10000) {
2476 /* Avoid asserts in emit_memcpy () */
2477 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Passing an argument of size '%d'.", size));
2478 /* Continue normally */
2482 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2483 arg->sreg1 = in->dreg;
2484 arg->klass = mono_class_from_mono_type (t);
2485 arg->backend.size = size;
2486 arg->inst_p0 = call;
2487 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2488 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2490 MONO_ADD_INS (cfg->cbb, arg);
2495 g_assert_not_reached ();
2498 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
2499 /* Emit the signature cookie just before the implicit arguments */
2500 emit_sig_cookie (cfg, call, cinfo);
2503 /* Handle the case where there are no implicit arguments */
2504 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2505 emit_sig_cookie (cfg, call, cinfo);
2507 switch (cinfo->ret.storage) {
2508 case ArgValuetypeInReg:
2509 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2511 * Tell the JIT to use a more efficient calling convention: call using
2512 * OP_CALL, compute the result location after the call, and save the
2515 call->vret_in_reg = TRUE;
2517 * Nullify the instruction computing the vret addr to enable
2518 * future optimizations.
2521 NULLIFY_INS (call->vret_var);
2523 if (call->tail_call)
2526 * The valuetype is in RAX:RDX after the call, need to be copied to
2527 * the stack. Push the address here, so the call instruction can
2530 if (!cfg->arch.vret_addr_loc) {
2531 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2532 /* Prevent it from being register allocated or optimized away */
2533 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2536 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2539 case ArgValuetypeAddrInIReg: {
2541 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2542 vtarg->sreg1 = call->vret_var->dreg;
2543 vtarg->dreg = mono_alloc_preg (cfg);
2544 MONO_ADD_INS (cfg->cbb, vtarg);
2546 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2553 if (cfg->method->save_lmf) {
2554 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2555 MONO_ADD_INS (cfg->cbb, arg);
2558 call->stack_usage = cinfo->stack_usage;
2562 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2565 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2566 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2567 int size = ins->backend.size;
2569 switch (ainfo->storage) {
2570 case ArgValuetypeInReg: {
2574 for (part = 0; part < 2; ++part) {
2575 if (ainfo->pair_storage [part] == ArgNone)
2578 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2579 load->inst_basereg = src->dreg;
2580 load->inst_offset = part * sizeof(mgreg_t);
2582 switch (ainfo->pair_storage [part]) {
2584 load->dreg = mono_alloc_ireg (cfg);
2586 case ArgInDoubleSSEReg:
2587 case ArgInFloatSSEReg:
2588 load->dreg = mono_alloc_freg (cfg);
2591 g_assert_not_reached ();
2593 MONO_ADD_INS (cfg->cbb, load);
2595 add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2599 case ArgValuetypeAddrInIReg: {
2600 MonoInst *vtaddr, *load;
2601 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2603 MONO_INST_NEW (cfg, load, OP_LDADDR);
2604 cfg->has_indirection = TRUE;
2605 load->inst_p0 = vtaddr;
2606 vtaddr->flags |= MONO_INST_INDIRECT;
2607 load->type = STACK_MP;
2608 load->klass = vtaddr->klass;
2609 load->dreg = mono_alloc_ireg (cfg);
2610 MONO_ADD_INS (cfg->cbb, load);
2611 mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2613 if (ainfo->pair_storage [0] == ArgInIReg) {
2614 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2615 arg->dreg = mono_alloc_ireg (cfg);
2616 arg->sreg1 = load->dreg;
2618 MONO_ADD_INS (cfg->cbb, arg);
2619 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2621 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, load->dreg);
2625 case ArgGSharedVtInReg:
2627 mono_call_inst_add_outarg_reg (cfg, call, src->dreg, ainfo->reg, FALSE);
2629 case ArgGSharedVtOnStack:
2630 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, src->dreg);
2634 int dreg = mono_alloc_ireg (cfg);
2636 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
2637 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, dreg);
2638 } else if (size <= 40) {
2639 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2641 // FIXME: Code growth
2642 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2645 if (cfg->compute_gc_maps) {
2647 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2653 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2655 MonoType *ret = mini_get_underlying_type (mono_method_signature (method)->ret);
2657 if (ret->type == MONO_TYPE_R4) {
2658 if (COMPILE_LLVM (cfg))
2659 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2661 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2663 } else if (ret->type == MONO_TYPE_R8) {
2664 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2668 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2671 #endif /* DISABLE_JIT */
2673 #define EMIT_COND_BRANCH(ins,cond,sign) \
2674 if (ins->inst_true_bb->native_offset) { \
2675 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2677 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2678 if ((cfg->opt & MONO_OPT_BRANCH) && \
2679 x86_is_imm8 (ins->inst_true_bb->max_offset - offset)) \
2680 x86_branch8 (code, cond, 0, sign); \
2682 x86_branch32 (code, cond, 0, sign); \
2686 MonoMethodSignature *sig;
2691 dyn_call_supported (MonoMethodSignature *sig, CallInfo *cinfo)
2699 switch (cinfo->ret.storage) {
2702 case ArgInFloatSSEReg:
2703 case ArgInDoubleSSEReg:
2705 case ArgValuetypeInReg: {
2706 ArgInfo *ainfo = &cinfo->ret;
2708 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2710 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2718 for (i = 0; i < cinfo->nargs; ++i) {
2719 ArgInfo *ainfo = &cinfo->args [i];
2720 switch (ainfo->storage) {
2722 case ArgInFloatSSEReg:
2723 case ArgInDoubleSSEReg:
2725 case ArgValuetypeInReg:
2726 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2728 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2740 * mono_arch_dyn_call_prepare:
2742 * Return a pointer to an arch-specific structure which contains information
2743 * needed by mono_arch_get_dyn_call_args (). Return NULL if OP_DYN_CALL is not
2744 * supported for SIG.
2745 * This function is equivalent to ffi_prep_cif in libffi.
2748 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2750 ArchDynCallInfo *info;
2753 cinfo = get_call_info (NULL, sig);
2755 if (!dyn_call_supported (sig, cinfo)) {
2760 info = g_new0 (ArchDynCallInfo, 1);
2761 // FIXME: Preprocess the info to speed up get_dyn_call_args ().
2763 info->cinfo = cinfo;
2765 return (MonoDynCallInfo*)info;
2769 * mono_arch_dyn_call_free:
2771 * Free a MonoDynCallInfo structure.
2774 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2776 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2778 g_free (ainfo->cinfo);
2782 #if !defined(__native_client__)
2783 #define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
2784 #define GREG_TO_PTR(greg) (gpointer)(greg)
2786 /* Correctly handle casts to/from 32-bit pointers without compiler warnings */
2787 #define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
2788 #define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
2792 * mono_arch_get_start_dyn_call:
2794 * Convert the arguments ARGS to a format which can be passed to OP_DYN_CALL, and
2795 * store the result into BUF.
2796 * ARGS should be an array of pointers pointing to the arguments.
2797 * RET should point to a memory buffer large enought to hold the result of the
2799 * This function should be as fast as possible, any work which does not depend
2800 * on the actual values of the arguments should be done in
2801 * mono_arch_dyn_call_prepare ().
2802 * start_dyn_call + OP_DYN_CALL + finish_dyn_call is equivalent to ffi_call in
2806 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2808 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2809 DynCallArgs *p = (DynCallArgs*)buf;
2810 int arg_index, greg, freg, i, pindex;
2811 MonoMethodSignature *sig = dinfo->sig;
2812 int buffer_offset = 0;
2814 g_assert (buf_len >= sizeof (DynCallArgs));
2824 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2825 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2830 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg)
2831 p->regs [greg ++] = PTR_TO_GREG(ret);
2833 for (i = pindex; i < sig->param_count; i++) {
2834 MonoType *t = mini_get_underlying_type (sig->params [i]);
2835 gpointer *arg = args [arg_index ++];
2838 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2843 case MONO_TYPE_STRING:
2844 case MONO_TYPE_CLASS:
2845 case MONO_TYPE_ARRAY:
2846 case MONO_TYPE_SZARRAY:
2847 case MONO_TYPE_OBJECT:
2851 #if !defined(__mono_ilp32__)
2855 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2856 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2858 #if defined(__mono_ilp32__)
2861 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2862 p->regs [greg ++] = *(guint64*)(arg);
2866 p->regs [greg ++] = *(guint8*)(arg);
2869 p->regs [greg ++] = *(gint8*)(arg);
2872 p->regs [greg ++] = *(gint16*)(arg);
2875 p->regs [greg ++] = *(guint16*)(arg);
2878 p->regs [greg ++] = *(gint32*)(arg);
2881 p->regs [greg ++] = *(guint32*)(arg);
2883 case MONO_TYPE_R4: {
2886 *(float*)&d = *(float*)(arg);
2888 p->fregs [freg ++] = d;
2893 p->fregs [freg ++] = *(double*)(arg);
2895 case MONO_TYPE_GENERICINST:
2896 if (MONO_TYPE_IS_REFERENCE (t)) {
2897 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2899 } else if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
2900 MonoClass *klass = mono_class_from_mono_type (t);
2901 guint8 *nullable_buf;
2904 size = mono_class_value_size (klass, NULL);
2905 nullable_buf = p->buffer + buffer_offset;
2906 buffer_offset += size;
2907 g_assert (buffer_offset <= 256);
2909 /* The argument pointed to by arg is either a boxed vtype or null */
2910 mono_nullable_init (nullable_buf, (MonoObject*)arg, klass);
2912 arg = (gpointer*)nullable_buf;
2918 case MONO_TYPE_VALUETYPE: {
2919 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2921 g_assert (ainfo->storage == ArgValuetypeInReg);
2922 if (ainfo->pair_storage [0] != ArgNone) {
2923 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2924 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2926 if (ainfo->pair_storage [1] != ArgNone) {
2927 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2928 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2933 g_assert_not_reached ();
2937 g_assert (greg <= PARAM_REGS);
2941 * mono_arch_finish_dyn_call:
2943 * Store the result of a dyn call into the return value buffer passed to
2944 * start_dyn_call ().
2945 * This function should be as fast as possible, any work which does not depend
2946 * on the actual values of the arguments should be done in
2947 * mono_arch_dyn_call_prepare ().
2950 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2952 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2953 MonoMethodSignature *sig = dinfo->sig;
2954 DynCallArgs *dargs = (DynCallArgs*)buf;
2955 guint8 *ret = dargs->ret;
2956 mgreg_t res = dargs->res;
2957 MonoType *sig_ret = mini_get_underlying_type (sig->ret);
2959 switch (sig_ret->type) {
2960 case MONO_TYPE_VOID:
2961 *(gpointer*)ret = NULL;
2963 case MONO_TYPE_STRING:
2964 case MONO_TYPE_CLASS:
2965 case MONO_TYPE_ARRAY:
2966 case MONO_TYPE_SZARRAY:
2967 case MONO_TYPE_OBJECT:
2971 *(gpointer*)ret = GREG_TO_PTR(res);
2977 *(guint8*)ret = res;
2980 *(gint16*)ret = res;
2983 *(guint16*)ret = res;
2986 *(gint32*)ret = res;
2989 *(guint32*)ret = res;
2992 *(gint64*)ret = res;
2995 *(guint64*)ret = res;
2998 *(float*)ret = *(float*)&(dargs->fregs [0]);
3001 *(double*)ret = dargs->fregs [0];
3003 case MONO_TYPE_GENERICINST:
3004 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
3005 *(gpointer*)ret = GREG_TO_PTR(res);
3010 case MONO_TYPE_VALUETYPE:
3011 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg) {
3014 ArgInfo *ainfo = &dinfo->cinfo->ret;
3016 g_assert (ainfo->storage == ArgValuetypeInReg);
3018 if (ainfo->pair_storage [0] != ArgNone) {
3019 g_assert (ainfo->pair_storage [0] == ArgInIReg);
3020 ((mgreg_t*)ret)[0] = res;
3023 g_assert (ainfo->pair_storage [1] == ArgNone);
3027 g_assert_not_reached ();
3031 /* emit an exception if condition is fail */
3032 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
3034 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
3035 if (tins == NULL) { \
3036 mono_add_patch_info (cfg, code - cfg->native_code, \
3037 MONO_PATCH_INFO_EXC, exc_name); \
3038 x86_branch32 (code, cond, 0, signed); \
3040 EMIT_COND_BRANCH (tins, cond, signed); \
3044 #define EMIT_FPCOMPARE(code) do { \
3045 amd64_fcompp (code); \
3046 amd64_fnstsw (code); \
3049 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
3050 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
3051 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
3052 amd64_ ##op (code); \
3053 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
3054 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
3058 emit_call_body (MonoCompile *cfg, guint8 *code, MonoJumpInfoType patch_type, gconstpointer data)
3060 gboolean no_patch = FALSE;
3063 * FIXME: Add support for thunks
3066 gboolean near_call = FALSE;
3069 * Indirect calls are expensive so try to make a near call if possible.
3070 * The caller memory is allocated by the code manager so it is
3071 * guaranteed to be at a 32 bit offset.
3074 if (patch_type != MONO_PATCH_INFO_ABS) {
3075 /* The target is in memory allocated using the code manager */
3078 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
3079 if (((MonoMethod*)data)->klass->image->aot_module)
3080 /* The callee might be an AOT method */
3082 if (((MonoMethod*)data)->dynamic)
3083 /* The target is in malloc-ed memory */
3087 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
3089 * The call might go directly to a native function without
3092 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ((const char *)data);
3094 gconstpointer target = mono_icall_get_wrapper (mi);
3095 if ((((guint64)target) >> 32) != 0)
3101 MonoJumpInfo *jinfo = NULL;
3103 if (cfg->abs_patches)
3104 jinfo = (MonoJumpInfo *)g_hash_table_lookup (cfg->abs_patches, data);
3106 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3107 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
3108 if (mi && (((guint64)mi->func) >> 32) == 0)
3113 * This is not really an optimization, but required because the
3114 * generic class init trampolines use R11 to pass the vtable.
3119 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
3121 if (info->func == info->wrapper) {
3123 if ((((guint64)info->func) >> 32) == 0)
3127 /* See the comment in mono_codegen () */
3128 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
3132 else if ((((guint64)data) >> 32) == 0) {
3139 if (cfg->method->dynamic)
3140 /* These methods are allocated using malloc */
3143 #ifdef MONO_ARCH_NOMAP32BIT
3146 #if defined(__native_client__)
3147 /* Always use near_call == TRUE for Native Client */
3150 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
3151 if (optimize_for_xen)
3154 if (cfg->compile_aot) {
3161 * Align the call displacement to an address divisible by 4 so it does
3162 * not span cache lines. This is required for code patching to work on SMP
3165 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
3166 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
3167 amd64_padding (code, pad_size);
3169 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3170 amd64_call_code (code, 0);
3173 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3174 amd64_set_reg_template (code, GP_SCRATCH_REG);
3175 amd64_call_reg (code, GP_SCRATCH_REG);
3182 static inline guint8*
3183 emit_call (MonoCompile *cfg, guint8 *code, MonoJumpInfoType patch_type, gconstpointer data, gboolean win64_adjust_stack)
3186 if (win64_adjust_stack)
3187 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
3189 code = emit_call_body (cfg, code, patch_type, data);
3191 if (win64_adjust_stack)
3192 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
3199 store_membase_imm_to_store_membase_reg (int opcode)
3202 case OP_STORE_MEMBASE_IMM:
3203 return OP_STORE_MEMBASE_REG;
3204 case OP_STOREI4_MEMBASE_IMM:
3205 return OP_STOREI4_MEMBASE_REG;
3206 case OP_STOREI8_MEMBASE_IMM:
3207 return OP_STOREI8_MEMBASE_REG;
3215 #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)))
3218 * mono_arch_peephole_pass_1:
3220 * Perform peephole opts which should/can be performed before local regalloc
3223 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3227 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3228 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3230 switch (ins->opcode) {
3234 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
3236 * X86_LEA is like ADD, but doesn't have the
3237 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
3238 * its operand to 64 bit.
3240 ins->opcode = OP_X86_LEA_MEMBASE;
3241 ins->inst_basereg = ins->sreg1;
3246 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3250 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3251 * the latter has length 2-3 instead of 6 (reverse constant
3252 * propagation). These instruction sequences are very common
3253 * in the initlocals bblock.
3255 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3256 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)) {
3257 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3258 ins2->sreg1 = ins->dreg;
3259 } 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)) {
3261 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3264 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3272 case OP_COMPARE_IMM:
3273 case OP_LCOMPARE_IMM:
3274 /* OP_COMPARE_IMM (reg, 0)
3276 * OP_AMD64_TEST_NULL (reg)
3279 ins->opcode = OP_AMD64_TEST_NULL;
3281 case OP_ICOMPARE_IMM:
3283 ins->opcode = OP_X86_TEST_NULL;
3285 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3287 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3288 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3290 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3291 * OP_COMPARE_IMM reg, imm
3293 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3295 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3296 ins->inst_basereg == last_ins->inst_destbasereg &&
3297 ins->inst_offset == last_ins->inst_offset) {
3298 ins->opcode = OP_ICOMPARE_IMM;
3299 ins->sreg1 = last_ins->sreg1;
3301 /* check if we can remove cmp reg,0 with test null */
3303 ins->opcode = OP_X86_TEST_NULL;
3309 mono_peephole_ins (bb, ins);
3314 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3318 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3319 switch (ins->opcode) {
3322 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3323 /* reg = 0 -> XOR (reg, reg) */
3324 /* XOR sets cflags on x86, so we cant do it always */
3325 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3326 ins->opcode = OP_LXOR;
3327 ins->sreg1 = ins->dreg;
3328 ins->sreg2 = ins->dreg;
3336 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3337 * 0 result into 64 bits.
3339 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3340 ins->opcode = OP_IXOR;
3344 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3348 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3349 * the latter has length 2-3 instead of 6 (reverse constant
3350 * propagation). These instruction sequences are very common
3351 * in the initlocals bblock.
3353 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3354 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)) {
3355 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3356 ins2->sreg1 = ins->dreg;
3357 } 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)) {
3359 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3362 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3371 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3372 ins->opcode = OP_X86_INC_REG;
3375 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3376 ins->opcode = OP_X86_DEC_REG;
3380 mono_peephole_ins (bb, ins);
3384 #define NEW_INS(cfg,ins,dest,op) do { \
3385 MONO_INST_NEW ((cfg), (dest), (op)); \
3386 (dest)->cil_code = (ins)->cil_code; \
3387 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3391 * mono_arch_lowering_pass:
3393 * Converts complex opcodes into simpler ones so that each IR instruction
3394 * corresponds to one machine instruction.
3397 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3399 MonoInst *ins, *n, *temp;
3402 * FIXME: Need to add more instructions, but the current machine
3403 * description can't model some parts of the composite instructions like
3406 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3407 switch (ins->opcode) {
3411 case OP_IDIV_UN_IMM:
3412 case OP_IREM_UN_IMM:
3415 mono_decompose_op_imm (cfg, bb, ins);
3417 case OP_COMPARE_IMM:
3418 case OP_LCOMPARE_IMM:
3419 if (!amd64_use_imm32 (ins->inst_imm)) {
3420 NEW_INS (cfg, ins, temp, OP_I8CONST);
3421 temp->inst_c0 = ins->inst_imm;
3422 temp->dreg = mono_alloc_ireg (cfg);
3423 ins->opcode = OP_COMPARE;
3424 ins->sreg2 = temp->dreg;
3427 #ifndef __mono_ilp32__
3428 case OP_LOAD_MEMBASE:
3430 case OP_LOADI8_MEMBASE:
3431 #ifndef __native_client_codegen__
3432 /* Don't generate memindex opcodes (to simplify */
3433 /* read sandboxing) */
3434 if (!amd64_use_imm32 (ins->inst_offset)) {
3435 NEW_INS (cfg, ins, temp, OP_I8CONST);
3436 temp->inst_c0 = ins->inst_offset;
3437 temp->dreg = mono_alloc_ireg (cfg);
3438 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3439 ins->inst_indexreg = temp->dreg;
3443 #ifndef __mono_ilp32__
3444 case OP_STORE_MEMBASE_IMM:
3446 case OP_STOREI8_MEMBASE_IMM:
3447 if (!amd64_use_imm32 (ins->inst_imm)) {
3448 NEW_INS (cfg, ins, temp, OP_I8CONST);
3449 temp->inst_c0 = ins->inst_imm;
3450 temp->dreg = mono_alloc_ireg (cfg);
3451 ins->opcode = OP_STOREI8_MEMBASE_REG;
3452 ins->sreg1 = temp->dreg;
3455 #ifdef MONO_ARCH_SIMD_INTRINSICS
3456 case OP_EXPAND_I1: {
3457 int temp_reg1 = mono_alloc_ireg (cfg);
3458 int temp_reg2 = mono_alloc_ireg (cfg);
3459 int original_reg = ins->sreg1;
3461 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3462 temp->sreg1 = original_reg;
3463 temp->dreg = temp_reg1;
3465 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3466 temp->sreg1 = temp_reg1;
3467 temp->dreg = temp_reg2;
3470 NEW_INS (cfg, ins, temp, OP_LOR);
3471 temp->sreg1 = temp->dreg = temp_reg2;
3472 temp->sreg2 = temp_reg1;
3474 ins->opcode = OP_EXPAND_I2;
3475 ins->sreg1 = temp_reg2;
3484 bb->max_vreg = cfg->next_vreg;
3488 branch_cc_table [] = {
3489 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3490 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3491 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3494 /* Maps CMP_... constants to X86_CC_... constants */
3497 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3498 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3502 cc_signed_table [] = {
3503 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3504 FALSE, FALSE, FALSE, FALSE
3507 /*#include "cprop.c"*/
3509 static unsigned char*
3510 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3513 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3515 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3518 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3520 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3524 static unsigned char*
3525 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3527 int sreg = tree->sreg1;
3528 int need_touch = FALSE;
3530 #if defined(TARGET_WIN32)
3532 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3533 if (!tree->flags & MONO_INST_INIT)
3542 * If requested stack size is larger than one page,
3543 * perform stack-touch operation
3546 * Generate stack probe code.
3547 * Under Windows, it is necessary to allocate one page at a time,
3548 * "touching" stack after each successful sub-allocation. This is
3549 * because of the way stack growth is implemented - there is a
3550 * guard page before the lowest stack page that is currently commited.
3551 * Stack normally grows sequentially so OS traps access to the
3552 * guard page and commits more pages when needed.
3554 amd64_test_reg_imm (code, sreg, ~0xFFF);
3555 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3557 br[2] = code; /* loop */
3558 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3559 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3560 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3561 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3562 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3563 amd64_patch (br[3], br[2]);
3564 amd64_test_reg_reg (code, sreg, sreg);
3565 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3566 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3568 br[1] = code; x86_jump8 (code, 0);
3570 amd64_patch (br[0], code);
3571 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3572 amd64_patch (br[1], code);
3573 amd64_patch (br[4], code);
3576 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3578 if (tree->flags & MONO_INST_INIT) {
3580 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3581 amd64_push_reg (code, AMD64_RAX);
3584 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3585 amd64_push_reg (code, AMD64_RCX);
3588 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3589 amd64_push_reg (code, AMD64_RDI);
3593 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3594 if (sreg != AMD64_RCX)
3595 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3596 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3598 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3599 if (cfg->param_area)
3600 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3602 #if defined(__default_codegen__)
3603 amd64_prefix (code, X86_REP_PREFIX);
3605 #elif defined(__native_client_codegen__)
3606 /* NaCl stos pseudo-instruction */
3607 amd64_codegen_pre(code);
3608 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3609 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3610 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3611 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3612 amd64_prefix (code, X86_REP_PREFIX);
3614 amd64_codegen_post(code);
3615 #endif /* __native_client_codegen__ */
3617 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3618 amd64_pop_reg (code, AMD64_RDI);
3619 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3620 amd64_pop_reg (code, AMD64_RCX);
3621 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3622 amd64_pop_reg (code, AMD64_RAX);
3628 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3633 /* Move return value to the target register */
3634 /* FIXME: do this in the local reg allocator */
3635 switch (ins->opcode) {
3638 case OP_CALL_MEMBASE:
3641 case OP_LCALL_MEMBASE:
3642 g_assert (ins->dreg == AMD64_RAX);
3646 case OP_FCALL_MEMBASE: {
3647 MonoType *rtype = mini_get_underlying_type (((MonoCallInst*)ins)->signature->ret);
3648 if (rtype->type == MONO_TYPE_R4) {
3649 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3652 if (ins->dreg != AMD64_XMM0)
3653 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3659 case OP_RCALL_MEMBASE:
3660 if (ins->dreg != AMD64_XMM0)
3661 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3665 case OP_VCALL_MEMBASE:
3668 case OP_VCALL2_MEMBASE:
3669 cinfo = get_call_info (cfg->mempool, ((MonoCallInst*)ins)->signature);
3670 if (cinfo->ret.storage == ArgValuetypeInReg) {
3671 MonoInst *loc = (MonoInst *)cfg->arch.vret_addr_loc;
3673 /* Load the destination address */
3674 g_assert (loc->opcode == OP_REGOFFSET);
3675 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3677 for (quad = 0; quad < 2; quad ++) {
3678 switch (cinfo->ret.pair_storage [quad]) {
3680 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3682 case ArgInFloatSSEReg:
3683 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3685 case ArgInDoubleSSEReg:
3686 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3701 #endif /* DISABLE_JIT */
3704 static int tls_gs_offset;
3708 mono_amd64_have_tls_get (void)
3711 static gboolean have_tls_get = FALSE;
3712 static gboolean inited = FALSE;
3715 return have_tls_get;
3717 #if MONO_HAVE_FAST_TLS
3718 guint8 *ins = (guint8*)pthread_getspecific;
3721 * We're looking for these two instructions:
3723 * mov %gs:[offset](,%rdi,8),%rax
3726 have_tls_get = ins [0] == 0x65 &&
3736 tls_gs_offset = ins[5];
3739 * Apple now loads a different version of pthread_getspecific when launched from Xcode
3740 * For that version we're looking for these instructions:
3744 * mov %gs:[offset](,%rdi,8),%rax
3748 if (!have_tls_get) {
3749 have_tls_get = ins [0] == 0x55 &&
3764 tls_gs_offset = ins[9];
3770 return have_tls_get;
3771 #elif defined(TARGET_ANDROID)
3779 mono_amd64_get_tls_gs_offset (void)
3782 return tls_gs_offset;
3784 g_assert_not_reached ();
3790 * mono_amd64_emit_tls_get:
3791 * @code: buffer to store code to
3792 * @dreg: hard register where to place the result
3793 * @tls_offset: offset info
3795 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3796 * the dreg register the item in the thread local storage identified
3799 * Returns: a pointer to the end of the stored code
3802 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3805 if (tls_offset < 64) {
3806 x86_prefix (code, X86_GS_PREFIX);
3807 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3811 g_assert (tls_offset < 0x440);
3812 /* Load TEB->TlsExpansionSlots */
3813 x86_prefix (code, X86_GS_PREFIX);
3814 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3815 amd64_test_reg_reg (code, dreg, dreg);
3817 amd64_branch (code, X86_CC_EQ, code, TRUE);
3818 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3819 amd64_patch (buf [0], code);
3821 #elif defined(__APPLE__)
3822 x86_prefix (code, X86_GS_PREFIX);
3823 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3825 if (optimize_for_xen) {
3826 x86_prefix (code, X86_FS_PREFIX);
3827 amd64_mov_reg_mem (code, dreg, 0, 8);
3828 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3830 x86_prefix (code, X86_FS_PREFIX);
3831 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3838 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3840 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3842 if (dreg != offset_reg)
3843 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3844 amd64_prefix (code, X86_GS_PREFIX);
3845 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3846 #elif defined(__linux__)
3849 if (dreg == offset_reg) {
3850 /* Use a temporary reg by saving it to the redzone */
3851 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3852 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3853 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3854 offset_reg = tmpreg;
3856 x86_prefix (code, X86_FS_PREFIX);
3857 amd64_mov_reg_mem (code, dreg, 0, 8);
3858 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3860 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3862 g_assert_not_reached ();
3868 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3871 g_assert_not_reached ();
3872 #elif defined(__APPLE__)
3873 x86_prefix (code, X86_GS_PREFIX);
3874 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3876 g_assert (!optimize_for_xen);
3877 x86_prefix (code, X86_FS_PREFIX);
3878 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3884 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3886 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3888 g_assert_not_reached ();
3889 #elif defined(__APPLE__)
3890 x86_prefix (code, X86_GS_PREFIX);
3891 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3893 x86_prefix (code, X86_FS_PREFIX);
3894 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3900 * mono_arch_translate_tls_offset:
3902 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3905 mono_arch_translate_tls_offset (int offset)
3908 return tls_gs_offset + (offset * 8);
3917 * Emit code to initialize an LMF structure at LMF_OFFSET.
3920 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3923 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3926 * sp is saved right before calls but we need to save it here too so
3927 * async stack walks would work.
3929 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3931 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3932 if (cfg->arch.omit_fp && cfa_offset != -1)
3933 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3935 /* These can't contain refs */
3936 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3937 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3938 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3939 /* These are handled automatically by the stack marking code */
3940 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3945 #define REAL_PRINT_REG(text,reg) \
3946 mono_assert (reg >= 0); \
3947 amd64_push_reg (code, AMD64_RAX); \
3948 amd64_push_reg (code, AMD64_RDX); \
3949 amd64_push_reg (code, AMD64_RCX); \
3950 amd64_push_reg (code, reg); \
3951 amd64_push_imm (code, reg); \
3952 amd64_push_imm (code, text " %d %p\n"); \
3953 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
3954 amd64_call_reg (code, AMD64_RAX); \
3955 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
3956 amd64_pop_reg (code, AMD64_RCX); \
3957 amd64_pop_reg (code, AMD64_RDX); \
3958 amd64_pop_reg (code, AMD64_RAX);
3960 /* benchmark and set based on cpu */
3961 #define LOOP_ALIGNMENT 8
3962 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3966 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3971 guint8 *code = cfg->native_code + cfg->code_len;
3974 /* Fix max_offset estimate for each successor bb */
3975 if (cfg->opt & MONO_OPT_BRANCH) {
3976 int current_offset = cfg->code_len;
3977 MonoBasicBlock *current_bb;
3978 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3979 current_bb->max_offset = current_offset;
3980 current_offset += current_bb->max_length;
3984 if (cfg->opt & MONO_OPT_LOOP) {
3985 int pad, align = LOOP_ALIGNMENT;
3986 /* set alignment depending on cpu */
3987 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3989 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3990 amd64_padding (code, pad);
3991 cfg->code_len += pad;
3992 bb->native_offset = cfg->code_len;
3996 #if defined(__native_client_codegen__)
3997 /* For Native Client, all indirect call/jump targets must be */
3998 /* 32-byte aligned. Exception handler blocks are jumped to */
3999 /* indirectly as well. */
4000 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
4001 (bb->flags & BB_EXCEPTION_HANDLER);
4003 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
4004 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
4005 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
4006 cfg->code_len += pad;
4007 bb->native_offset = cfg->code_len;
4009 #endif /*__native_client_codegen__*/
4011 if (cfg->verbose_level > 2)
4012 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
4014 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
4015 MonoProfileCoverageInfo *cov = cfg->coverage_info;
4016 g_assert (!cfg->compile_aot);
4018 cov->data [bb->dfn].cil_code = bb->cil_code;
4019 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
4020 /* this is not thread save, but good enough */
4021 amd64_inc_membase (code, AMD64_R11, 0);
4024 offset = code - cfg->native_code;
4026 mono_debug_open_block (cfg, bb, offset);
4028 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)
4029 x86_breakpoint (code);
4031 MONO_BB_FOR_EACH_INS (bb, ins) {
4032 offset = code - cfg->native_code;
4034 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4036 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
4038 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
4039 cfg->code_size *= 2;
4040 cfg->native_code = (unsigned char *)mono_realloc_native_code(cfg);
4041 code = cfg->native_code + offset;
4042 cfg->stat_code_reallocs++;
4045 if (cfg->debug_info)
4046 mono_debug_record_line_number (cfg, ins, offset);
4048 switch (ins->opcode) {
4050 amd64_mul_reg (code, ins->sreg2, TRUE);
4053 amd64_mul_reg (code, ins->sreg2, FALSE);
4055 case OP_X86_SETEQ_MEMBASE:
4056 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
4058 case OP_STOREI1_MEMBASE_IMM:
4059 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
4061 case OP_STOREI2_MEMBASE_IMM:
4062 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
4064 case OP_STOREI4_MEMBASE_IMM:
4065 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
4067 case OP_STOREI1_MEMBASE_REG:
4068 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
4070 case OP_STOREI2_MEMBASE_REG:
4071 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
4073 /* In AMD64 NaCl, pointers are 4 bytes, */
4074 /* so STORE_* != STOREI8_*. Likewise below. */
4075 case OP_STORE_MEMBASE_REG:
4076 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
4078 case OP_STOREI8_MEMBASE_REG:
4079 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
4081 case OP_STOREI4_MEMBASE_REG:
4082 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
4084 case OP_STORE_MEMBASE_IMM:
4085 #ifndef __native_client_codegen__
4086 /* In NaCl, this could be a PCONST type, which could */
4087 /* mean a pointer type was copied directly into the */
4088 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
4089 /* the value would be 0x00000000FFFFFFFF which is */
4090 /* not proper for an imm32 unless you cast it. */
4091 g_assert (amd64_is_imm32 (ins->inst_imm));
4093 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
4095 case OP_STOREI8_MEMBASE_IMM:
4096 g_assert (amd64_is_imm32 (ins->inst_imm));
4097 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
4100 #ifdef __mono_ilp32__
4101 /* In ILP32, pointers are 4 bytes, so separate these */
4102 /* cases, use literal 8 below where we really want 8 */
4103 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4104 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
4108 // FIXME: Decompose this earlier
4109 if (amd64_use_imm32 (ins->inst_imm))
4110 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
4112 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4113 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
4117 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4118 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
4121 // FIXME: Decompose this earlier
4122 if (amd64_use_imm32 (ins->inst_imm))
4123 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
4125 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4126 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
4130 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4131 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
4134 /* For NaCl, pointers are 4 bytes, so separate these */
4135 /* cases, use literal 8 below where we really want 8 */
4136 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4137 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
4139 case OP_LOAD_MEMBASE:
4140 g_assert (amd64_is_imm32 (ins->inst_offset));
4141 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
4143 case OP_LOADI8_MEMBASE:
4144 /* Use literal 8 instead of sizeof pointer or */
4145 /* register, we really want 8 for this opcode */
4146 g_assert (amd64_is_imm32 (ins->inst_offset));
4147 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
4149 case OP_LOADI4_MEMBASE:
4150 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4152 case OP_LOADU4_MEMBASE:
4153 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
4155 case OP_LOADU1_MEMBASE:
4156 /* The cpu zero extends the result into 64 bits */
4157 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
4159 case OP_LOADI1_MEMBASE:
4160 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
4162 case OP_LOADU2_MEMBASE:
4163 /* The cpu zero extends the result into 64 bits */
4164 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
4166 case OP_LOADI2_MEMBASE:
4167 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
4169 case OP_AMD64_LOADI8_MEMINDEX:
4170 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
4172 case OP_LCONV_TO_I1:
4173 case OP_ICONV_TO_I1:
4175 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
4177 case OP_LCONV_TO_I2:
4178 case OP_ICONV_TO_I2:
4180 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
4182 case OP_LCONV_TO_U1:
4183 case OP_ICONV_TO_U1:
4184 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
4186 case OP_LCONV_TO_U2:
4187 case OP_ICONV_TO_U2:
4188 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
4191 /* Clean out the upper word */
4192 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4195 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
4199 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4201 case OP_COMPARE_IMM:
4202 #if defined(__mono_ilp32__)
4203 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
4204 g_assert (amd64_is_imm32 (ins->inst_imm));
4205 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4208 case OP_LCOMPARE_IMM:
4209 g_assert (amd64_is_imm32 (ins->inst_imm));
4210 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
4212 case OP_X86_COMPARE_REG_MEMBASE:
4213 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
4215 case OP_X86_TEST_NULL:
4216 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
4218 case OP_AMD64_TEST_NULL:
4219 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4222 case OP_X86_ADD_REG_MEMBASE:
4223 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4225 case OP_X86_SUB_REG_MEMBASE:
4226 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4228 case OP_X86_AND_REG_MEMBASE:
4229 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4231 case OP_X86_OR_REG_MEMBASE:
4232 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4234 case OP_X86_XOR_REG_MEMBASE:
4235 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4238 case OP_X86_ADD_MEMBASE_IMM:
4239 /* FIXME: Make a 64 version too */
4240 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4242 case OP_X86_SUB_MEMBASE_IMM:
4243 g_assert (amd64_is_imm32 (ins->inst_imm));
4244 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4246 case OP_X86_AND_MEMBASE_IMM:
4247 g_assert (amd64_is_imm32 (ins->inst_imm));
4248 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4250 case OP_X86_OR_MEMBASE_IMM:
4251 g_assert (amd64_is_imm32 (ins->inst_imm));
4252 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4254 case OP_X86_XOR_MEMBASE_IMM:
4255 g_assert (amd64_is_imm32 (ins->inst_imm));
4256 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4258 case OP_X86_ADD_MEMBASE_REG:
4259 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4261 case OP_X86_SUB_MEMBASE_REG:
4262 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4264 case OP_X86_AND_MEMBASE_REG:
4265 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4267 case OP_X86_OR_MEMBASE_REG:
4268 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4270 case OP_X86_XOR_MEMBASE_REG:
4271 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4273 case OP_X86_INC_MEMBASE:
4274 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4276 case OP_X86_INC_REG:
4277 amd64_inc_reg_size (code, ins->dreg, 4);
4279 case OP_X86_DEC_MEMBASE:
4280 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4282 case OP_X86_DEC_REG:
4283 amd64_dec_reg_size (code, ins->dreg, 4);
4285 case OP_X86_MUL_REG_MEMBASE:
4286 case OP_X86_MUL_MEMBASE_REG:
4287 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4289 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4290 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4292 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4293 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4295 case OP_AMD64_COMPARE_MEMBASE_REG:
4296 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4298 case OP_AMD64_COMPARE_MEMBASE_IMM:
4299 g_assert (amd64_is_imm32 (ins->inst_imm));
4300 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4302 case OP_X86_COMPARE_MEMBASE8_IMM:
4303 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4305 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4306 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4308 case OP_AMD64_COMPARE_REG_MEMBASE:
4309 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4312 case OP_AMD64_ADD_REG_MEMBASE:
4313 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4315 case OP_AMD64_SUB_REG_MEMBASE:
4316 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4318 case OP_AMD64_AND_REG_MEMBASE:
4319 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4321 case OP_AMD64_OR_REG_MEMBASE:
4322 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4324 case OP_AMD64_XOR_REG_MEMBASE:
4325 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4328 case OP_AMD64_ADD_MEMBASE_REG:
4329 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4331 case OP_AMD64_SUB_MEMBASE_REG:
4332 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4334 case OP_AMD64_AND_MEMBASE_REG:
4335 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4337 case OP_AMD64_OR_MEMBASE_REG:
4338 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4340 case OP_AMD64_XOR_MEMBASE_REG:
4341 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4344 case OP_AMD64_ADD_MEMBASE_IMM:
4345 g_assert (amd64_is_imm32 (ins->inst_imm));
4346 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4348 case OP_AMD64_SUB_MEMBASE_IMM:
4349 g_assert (amd64_is_imm32 (ins->inst_imm));
4350 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4352 case OP_AMD64_AND_MEMBASE_IMM:
4353 g_assert (amd64_is_imm32 (ins->inst_imm));
4354 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4356 case OP_AMD64_OR_MEMBASE_IMM:
4357 g_assert (amd64_is_imm32 (ins->inst_imm));
4358 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4360 case OP_AMD64_XOR_MEMBASE_IMM:
4361 g_assert (amd64_is_imm32 (ins->inst_imm));
4362 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4366 amd64_breakpoint (code);
4368 case OP_RELAXED_NOP:
4369 x86_prefix (code, X86_REP_PREFIX);
4377 case OP_DUMMY_STORE:
4378 case OP_DUMMY_ICONST:
4379 case OP_DUMMY_R8CONST:
4380 case OP_NOT_REACHED:
4383 case OP_IL_SEQ_POINT:
4384 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4386 case OP_SEQ_POINT: {
4387 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4388 MonoInst *var = (MonoInst *)cfg->arch.ss_tramp_var;
4391 /* Load ss_tramp_var */
4392 /* This is equal to &ss_trampoline */
4393 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4394 /* Load the trampoline address */
4395 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4396 /* Call it if it is non-null */
4397 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4399 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4400 amd64_call_reg (code, AMD64_R11);
4401 amd64_patch (label, code);
4405 * This is the address which is saved in seq points,
4407 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4409 if (cfg->compile_aot) {
4410 guint32 offset = code - cfg->native_code;
4412 MonoInst *info_var = (MonoInst *)cfg->arch.seq_point_info_var;
4416 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4417 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4418 /* Load the info->bp_addrs [offset], which is either NULL or the address of the breakpoint trampoline */
4419 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4420 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4422 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4423 /* Call the trampoline */
4424 amd64_call_reg (code, AMD64_R11);
4425 amd64_patch (label, code);
4427 MonoInst *var = (MonoInst *)cfg->arch.bp_tramp_var;
4431 * Emit a test+branch against a constant, the constant will be overwritten
4432 * by mono_arch_set_breakpoint () to cause the test to fail.
4434 amd64_mov_reg_imm (code, AMD64_R11, 0);
4435 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4437 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4440 g_assert (var->opcode == OP_REGOFFSET);
4441 /* Load bp_tramp_var */
4442 /* This is equal to &bp_trampoline */
4443 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4444 /* Call the trampoline */
4445 amd64_call_membase (code, AMD64_R11, 0);
4446 amd64_patch (label, code);
4449 * Add an additional nop so skipping the bp doesn't cause the ip to point
4450 * to another IL offset.
4458 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4461 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4465 g_assert (amd64_is_imm32 (ins->inst_imm));
4466 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4469 g_assert (amd64_is_imm32 (ins->inst_imm));
4470 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4475 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4478 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4482 g_assert (amd64_is_imm32 (ins->inst_imm));
4483 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4486 g_assert (amd64_is_imm32 (ins->inst_imm));
4487 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4490 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4494 g_assert (amd64_is_imm32 (ins->inst_imm));
4495 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4498 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4503 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4505 switch (ins->inst_imm) {
4509 if (ins->dreg != ins->sreg1)
4510 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4511 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4514 /* LEA r1, [r2 + r2*2] */
4515 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4518 /* LEA r1, [r2 + r2*4] */
4519 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4522 /* LEA r1, [r2 + r2*2] */
4524 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4525 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4528 /* LEA r1, [r2 + r2*8] */
4529 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4532 /* LEA r1, [r2 + r2*4] */
4534 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4535 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4538 /* LEA r1, [r2 + r2*2] */
4540 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4541 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4544 /* LEA r1, [r2 + r2*4] */
4545 /* LEA r1, [r1 + r1*4] */
4546 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4547 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4550 /* LEA r1, [r2 + r2*4] */
4552 /* LEA r1, [r1 + r1*4] */
4553 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4554 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4555 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4558 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
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 /* Regalloc magic makes the div/rem cases the same */
4570 if (ins->sreg2 == AMD64_RDX) {
4571 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4573 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4576 amd64_div_reg (code, ins->sreg2, TRUE);
4581 #if defined( __native_client_codegen__ )
4582 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4583 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4585 if (ins->sreg2 == AMD64_RDX) {
4586 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4587 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4588 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4590 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4591 amd64_div_reg (code, ins->sreg2, FALSE);
4596 #if defined( __native_client_codegen__ )
4597 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4598 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4600 if (ins->sreg2 == AMD64_RDX) {
4601 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4602 amd64_cdq_size (code, 4);
4603 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4605 amd64_cdq_size (code, 4);
4606 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4611 #if defined( __native_client_codegen__ )
4612 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4613 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4615 if (ins->sreg2 == AMD64_RDX) {
4616 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4617 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4618 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4620 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4621 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4625 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4626 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4629 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4633 g_assert (amd64_is_imm32 (ins->inst_imm));
4634 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4637 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4641 g_assert (amd64_is_imm32 (ins->inst_imm));
4642 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4645 g_assert (ins->sreg2 == AMD64_RCX);
4646 amd64_shift_reg (code, X86_SHL, ins->dreg);
4649 g_assert (ins->sreg2 == AMD64_RCX);
4650 amd64_shift_reg (code, X86_SAR, ins->dreg);
4654 g_assert (amd64_is_imm32 (ins->inst_imm));
4655 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4658 g_assert (amd64_is_imm32 (ins->inst_imm));
4659 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4661 case OP_LSHR_UN_IMM:
4662 g_assert (amd64_is_imm32 (ins->inst_imm));
4663 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4666 g_assert (ins->sreg2 == AMD64_RCX);
4667 amd64_shift_reg (code, X86_SHR, ins->dreg);
4671 g_assert (amd64_is_imm32 (ins->inst_imm));
4672 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4677 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4680 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4683 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4686 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4690 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4693 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4696 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4699 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4702 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4705 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4708 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4711 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4714 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4717 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4720 amd64_neg_reg_size (code, ins->sreg1, 4);
4723 amd64_not_reg_size (code, ins->sreg1, 4);
4726 g_assert (ins->sreg2 == AMD64_RCX);
4727 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4730 g_assert (ins->sreg2 == AMD64_RCX);
4731 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4734 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4736 case OP_ISHR_UN_IMM:
4737 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4740 g_assert (ins->sreg2 == AMD64_RCX);
4741 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4744 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4747 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4750 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4751 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4753 case OP_IMUL_OVF_UN:
4754 case OP_LMUL_OVF_UN: {
4755 /* the mul operation and the exception check should most likely be split */
4756 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4757 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4758 /*g_assert (ins->sreg2 == X86_EAX);
4759 g_assert (ins->dreg == X86_EAX);*/
4760 if (ins->sreg2 == X86_EAX) {
4761 non_eax_reg = ins->sreg1;
4762 } else if (ins->sreg1 == X86_EAX) {
4763 non_eax_reg = ins->sreg2;
4765 /* no need to save since we're going to store to it anyway */
4766 if (ins->dreg != X86_EAX) {
4768 amd64_push_reg (code, X86_EAX);
4770 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4771 non_eax_reg = ins->sreg2;
4773 if (ins->dreg == X86_EDX) {
4776 amd64_push_reg (code, X86_EAX);
4780 amd64_push_reg (code, X86_EDX);
4782 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4783 /* save before the check since pop and mov don't change the flags */
4784 if (ins->dreg != X86_EAX)
4785 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4787 amd64_pop_reg (code, X86_EDX);
4789 amd64_pop_reg (code, X86_EAX);
4790 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4794 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4796 case OP_ICOMPARE_IMM:
4797 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4819 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4827 case OP_CMOV_INE_UN:
4828 case OP_CMOV_IGE_UN:
4829 case OP_CMOV_IGT_UN:
4830 case OP_CMOV_ILE_UN:
4831 case OP_CMOV_ILT_UN:
4837 case OP_CMOV_LNE_UN:
4838 case OP_CMOV_LGE_UN:
4839 case OP_CMOV_LGT_UN:
4840 case OP_CMOV_LLE_UN:
4841 case OP_CMOV_LLT_UN:
4842 g_assert (ins->dreg == ins->sreg1);
4843 /* This needs to operate on 64 bit values */
4844 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);
4848 amd64_not_reg (code, ins->sreg1);
4851 amd64_neg_reg (code, ins->sreg1);
4856 if ((((guint64)ins->inst_c0) >> 32) == 0 && !mini_get_debug_options()->single_imm_size)
4857 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4859 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4862 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4863 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4866 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4867 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4870 if (ins->dreg != ins->sreg1)
4871 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4873 case OP_AMD64_SET_XMMREG_R4: {
4875 if (ins->dreg != ins->sreg1)
4876 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4878 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4882 case OP_AMD64_SET_XMMREG_R8: {
4883 if (ins->dreg != ins->sreg1)
4884 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4888 MonoCallInst *call = (MonoCallInst*)ins;
4889 int i, save_area_offset;
4891 g_assert (!cfg->method->save_lmf);
4893 /* Restore callee saved registers */
4894 save_area_offset = cfg->arch.reg_save_area_offset;
4895 for (i = 0; i < AMD64_NREG; ++i)
4896 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4897 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4898 save_area_offset += 8;
4901 if (cfg->arch.omit_fp) {
4902 if (cfg->arch.stack_alloc_size)
4903 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4905 if (call->stack_usage)
4908 /* Copy arguments on the stack to our argument area */
4909 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4910 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4911 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4917 offset = code - cfg->native_code;
4918 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4919 if (cfg->compile_aot)
4920 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4922 amd64_set_reg_template (code, AMD64_R11);
4923 amd64_jump_reg (code, AMD64_R11);
4924 ins->flags |= MONO_INST_GC_CALLSITE;
4925 ins->backend.pc_offset = code - cfg->native_code;
4929 /* ensure ins->sreg1 is not NULL */
4930 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4933 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4934 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4944 call = (MonoCallInst*)ins;
4946 * The AMD64 ABI forces callers to know about varargs.
4948 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4949 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4950 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4952 * Since the unmanaged calling convention doesn't contain a
4953 * 'vararg' entry, we have to treat every pinvoke call as a
4954 * potential vararg call.
4958 for (i = 0; i < AMD64_XMM_NREG; ++i)
4959 if (call->used_fregs & (1 << i))
4962 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4964 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4967 if (ins->flags & MONO_INST_HAS_METHOD)
4968 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4970 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
4971 ins->flags |= MONO_INST_GC_CALLSITE;
4972 ins->backend.pc_offset = code - cfg->native_code;
4973 code = emit_move_return_value (cfg, ins, code);
4980 case OP_VOIDCALL_REG:
4982 call = (MonoCallInst*)ins;
4984 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4985 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4986 ins->sreg1 = AMD64_R11;
4990 * The AMD64 ABI forces callers to know about varargs.
4992 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4993 if (ins->sreg1 == AMD64_RAX) {
4994 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4995 ins->sreg1 = AMD64_R11;
4997 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4998 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
5000 * Since the unmanaged calling convention doesn't contain a
5001 * 'vararg' entry, we have to treat every pinvoke call as a
5002 * potential vararg call.
5006 for (i = 0; i < AMD64_XMM_NREG; ++i)
5007 if (call->used_fregs & (1 << i))
5009 if (ins->sreg1 == AMD64_RAX) {
5010 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
5011 ins->sreg1 = AMD64_R11;
5014 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
5016 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
5019 amd64_call_reg (code, ins->sreg1);
5020 ins->flags |= MONO_INST_GC_CALLSITE;
5021 ins->backend.pc_offset = code - cfg->native_code;
5022 code = emit_move_return_value (cfg, ins, code);
5024 case OP_FCALL_MEMBASE:
5025 case OP_RCALL_MEMBASE:
5026 case OP_LCALL_MEMBASE:
5027 case OP_VCALL_MEMBASE:
5028 case OP_VCALL2_MEMBASE:
5029 case OP_VOIDCALL_MEMBASE:
5030 case OP_CALL_MEMBASE:
5031 call = (MonoCallInst*)ins;
5033 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
5034 ins->flags |= MONO_INST_GC_CALLSITE;
5035 ins->backend.pc_offset = code - cfg->native_code;
5036 code = emit_move_return_value (cfg, ins, code);
5040 MonoInst *var = cfg->dyn_call_var;
5043 g_assert (var->opcode == OP_REGOFFSET);
5045 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
5046 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
5048 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
5050 /* Save args buffer */
5051 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
5053 /* Set fp arg regs */
5054 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, has_fp), sizeof (mgreg_t));
5055 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
5057 amd64_branch8 (code, X86_CC_Z, -1, 1);
5058 for (i = 0; i < FLOAT_PARAM_REGS; ++i)
5059 amd64_sse_movsd_reg_membase (code, i, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, fregs) + (i * sizeof (double)));
5060 amd64_patch (label, code);
5062 /* Set argument registers */
5063 for (i = 0; i < PARAM_REGS; ++i)
5064 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
5067 amd64_call_reg (code, AMD64_R10);
5069 ins->flags |= MONO_INST_GC_CALLSITE;
5070 ins->backend.pc_offset = code - cfg->native_code;
5073 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
5074 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
5075 amd64_sse_movsd_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, fregs), AMD64_XMM0);
5078 case OP_AMD64_SAVE_SP_TO_LMF: {
5079 MonoInst *lmf_var = cfg->lmf_var;
5080 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
5084 g_assert_not_reached ();
5085 amd64_push_reg (code, ins->sreg1);
5087 case OP_X86_PUSH_IMM:
5088 g_assert_not_reached ();
5089 g_assert (amd64_is_imm32 (ins->inst_imm));
5090 amd64_push_imm (code, ins->inst_imm);
5092 case OP_X86_PUSH_MEMBASE:
5093 g_assert_not_reached ();
5094 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
5096 case OP_X86_PUSH_OBJ: {
5097 int size = ALIGN_TO (ins->inst_imm, 8);
5099 g_assert_not_reached ();
5101 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5102 amd64_push_reg (code, AMD64_RDI);
5103 amd64_push_reg (code, AMD64_RSI);
5104 amd64_push_reg (code, AMD64_RCX);
5105 if (ins->inst_offset)
5106 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
5108 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
5109 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
5110 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
5112 amd64_prefix (code, X86_REP_PREFIX);
5114 amd64_pop_reg (code, AMD64_RCX);
5115 amd64_pop_reg (code, AMD64_RSI);
5116 amd64_pop_reg (code, AMD64_RDI);
5119 case OP_GENERIC_CLASS_INIT: {
5120 static int byte_offset = -1;
5121 static guint8 bitmask;
5124 g_assert (ins->sreg1 == MONO_AMD64_ARG_REG1);
5126 if (byte_offset < 0)
5127 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
5129 amd64_test_membase_imm_size (code, ins->sreg1, byte_offset, bitmask, 1);
5131 amd64_branch8 (code, X86_CC_NZ, -1, 1);
5133 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_generic_class_init", FALSE);
5134 ins->flags |= MONO_INST_GC_CALLSITE;
5135 ins->backend.pc_offset = code - cfg->native_code;
5137 x86_patch (jump, code);
5142 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
5144 case OP_X86_LEA_MEMBASE:
5145 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
5148 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
5151 /* keep alignment */
5152 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
5153 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
5154 code = mono_emit_stack_alloc (cfg, code, ins);
5155 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5156 if (cfg->param_area)
5157 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5159 case OP_LOCALLOC_IMM: {
5160 guint32 size = ins->inst_imm;
5161 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
5163 if (ins->flags & MONO_INST_INIT) {
5167 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5168 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5170 for (i = 0; i < size; i += 8)
5171 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
5172 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5174 amd64_mov_reg_imm (code, ins->dreg, size);
5175 ins->sreg1 = ins->dreg;
5177 code = mono_emit_stack_alloc (cfg, code, ins);
5178 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5181 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5182 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5184 if (cfg->param_area)
5185 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5189 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5190 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5191 (gpointer)"mono_arch_throw_exception", FALSE);
5192 ins->flags |= MONO_INST_GC_CALLSITE;
5193 ins->backend.pc_offset = code - cfg->native_code;
5197 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5198 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5199 (gpointer)"mono_arch_rethrow_exception", FALSE);
5200 ins->flags |= MONO_INST_GC_CALLSITE;
5201 ins->backend.pc_offset = code - cfg->native_code;
5204 case OP_CALL_HANDLER:
5206 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5207 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5208 amd64_call_imm (code, 0);
5209 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
5210 /* Restore stack alignment */
5211 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5213 case OP_START_HANDLER: {
5214 /* Even though we're saving RSP, use sizeof */
5215 /* gpointer because spvar is of type IntPtr */
5216 /* see: mono_create_spvar_for_region */
5217 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5218 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
5220 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
5221 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
5223 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
5227 case OP_ENDFINALLY: {
5228 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5229 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5233 case OP_ENDFILTER: {
5234 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5235 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5236 /* The local allocator will put the result into RAX */
5241 if (ins->dreg != AMD64_RAX)
5242 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, sizeof (gpointer));
5245 ins->inst_c0 = code - cfg->native_code;
5248 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
5249 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
5251 if (ins->inst_target_bb->native_offset) {
5252 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
5254 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5255 if ((cfg->opt & MONO_OPT_BRANCH) &&
5256 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
5257 x86_jump8 (code, 0);
5259 x86_jump32 (code, 0);
5263 amd64_jump_reg (code, ins->sreg1);
5286 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5287 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5289 case OP_COND_EXC_EQ:
5290 case OP_COND_EXC_NE_UN:
5291 case OP_COND_EXC_LT:
5292 case OP_COND_EXC_LT_UN:
5293 case OP_COND_EXC_GT:
5294 case OP_COND_EXC_GT_UN:
5295 case OP_COND_EXC_GE:
5296 case OP_COND_EXC_GE_UN:
5297 case OP_COND_EXC_LE:
5298 case OP_COND_EXC_LE_UN:
5299 case OP_COND_EXC_IEQ:
5300 case OP_COND_EXC_INE_UN:
5301 case OP_COND_EXC_ILT:
5302 case OP_COND_EXC_ILT_UN:
5303 case OP_COND_EXC_IGT:
5304 case OP_COND_EXC_IGT_UN:
5305 case OP_COND_EXC_IGE:
5306 case OP_COND_EXC_IGE_UN:
5307 case OP_COND_EXC_ILE:
5308 case OP_COND_EXC_ILE_UN:
5309 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);
5311 case OP_COND_EXC_OV:
5312 case OP_COND_EXC_NO:
5314 case OP_COND_EXC_NC:
5315 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5316 (ins->opcode < OP_COND_EXC_NE_UN), (const char *)ins->inst_p1);
5318 case OP_COND_EXC_IOV:
5319 case OP_COND_EXC_INO:
5320 case OP_COND_EXC_IC:
5321 case OP_COND_EXC_INC:
5322 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5323 (ins->opcode < OP_COND_EXC_INE_UN), (const char *)ins->inst_p1);
5326 /* floating point opcodes */
5328 double d = *(double *)ins->inst_p0;
5330 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5331 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5334 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5335 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5340 float f = *(float *)ins->inst_p0;
5342 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5344 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5346 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5349 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5350 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5352 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5356 case OP_STORER8_MEMBASE_REG:
5357 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5359 case OP_LOADR8_MEMBASE:
5360 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5362 case OP_STORER4_MEMBASE_REG:
5364 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5366 /* This requires a double->single conversion */
5367 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5368 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5371 case OP_LOADR4_MEMBASE:
5373 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5375 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5376 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5379 case OP_ICONV_TO_R4:
5381 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5383 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5384 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5387 case OP_ICONV_TO_R8:
5388 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5390 case OP_LCONV_TO_R4:
5392 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5394 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5395 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5398 case OP_LCONV_TO_R8:
5399 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5401 case OP_FCONV_TO_R4:
5403 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5405 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5406 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5409 case OP_FCONV_TO_I1:
5410 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5412 case OP_FCONV_TO_U1:
5413 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5415 case OP_FCONV_TO_I2:
5416 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5418 case OP_FCONV_TO_U2:
5419 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5421 case OP_FCONV_TO_U4:
5422 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5424 case OP_FCONV_TO_I4:
5426 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5428 case OP_FCONV_TO_I8:
5429 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5432 case OP_RCONV_TO_I1:
5433 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5434 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5436 case OP_RCONV_TO_U1:
5437 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5438 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5440 case OP_RCONV_TO_I2:
5441 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5442 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5444 case OP_RCONV_TO_U2:
5445 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5446 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5448 case OP_RCONV_TO_I4:
5449 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5451 case OP_RCONV_TO_U4:
5452 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5454 case OP_RCONV_TO_I8:
5455 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5457 case OP_RCONV_TO_R8:
5458 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5460 case OP_RCONV_TO_R4:
5461 if (ins->dreg != ins->sreg1)
5462 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5465 case OP_LCONV_TO_R_UN: {
5468 /* Based on gcc code */
5469 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5470 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5473 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5474 br [1] = code; x86_jump8 (code, 0);
5475 amd64_patch (br [0], code);
5478 /* Save to the red zone */
5479 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5480 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5481 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5482 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5483 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5484 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5485 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5486 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5487 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5489 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5490 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5491 amd64_patch (br [1], code);
5494 case OP_LCONV_TO_OVF_U4:
5495 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5496 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5497 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5499 case OP_LCONV_TO_OVF_I4_UN:
5500 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5501 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5502 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5505 if (ins->dreg != ins->sreg1)
5506 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5509 if (ins->dreg != ins->sreg1)
5510 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5512 case OP_MOVE_F_TO_I4:
5514 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5516 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5517 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5520 case OP_MOVE_I4_TO_F:
5521 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5523 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5525 case OP_MOVE_F_TO_I8:
5526 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5528 case OP_MOVE_I8_TO_F:
5529 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5532 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5535 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5538 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5541 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5544 static double r8_0 = -0.0;
5546 g_assert (ins->sreg1 == ins->dreg);
5548 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5549 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5553 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5556 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5559 static guint64 d = 0x7fffffffffffffffUL;
5561 g_assert (ins->sreg1 == ins->dreg);
5563 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5564 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5568 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5572 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5575 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5578 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5581 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5584 static float r4_0 = -0.0;
5586 g_assert (ins->sreg1 == ins->dreg);
5588 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5589 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5590 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
5595 g_assert (cfg->opt & MONO_OPT_CMOV);
5596 g_assert (ins->dreg == ins->sreg1);
5597 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5598 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
5601 g_assert (cfg->opt & MONO_OPT_CMOV);
5602 g_assert (ins->dreg == ins->sreg1);
5603 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5604 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
5607 g_assert (cfg->opt & MONO_OPT_CMOV);
5608 g_assert (ins->dreg == ins->sreg1);
5609 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5610 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
5613 g_assert (cfg->opt & MONO_OPT_CMOV);
5614 g_assert (ins->dreg == ins->sreg1);
5615 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5616 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
5619 g_assert (cfg->opt & MONO_OPT_CMOV);
5620 g_assert (ins->dreg == ins->sreg1);
5621 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5622 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
5625 g_assert (cfg->opt & MONO_OPT_CMOV);
5626 g_assert (ins->dreg == ins->sreg1);
5627 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5628 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
5631 g_assert (cfg->opt & MONO_OPT_CMOV);
5632 g_assert (ins->dreg == ins->sreg1);
5633 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5634 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
5637 g_assert (cfg->opt & MONO_OPT_CMOV);
5638 g_assert (ins->dreg == ins->sreg1);
5639 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5640 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5646 * The two arguments are swapped because the fbranch instructions
5647 * depend on this for the non-sse case to work.
5649 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5653 * FIXME: Get rid of this.
5654 * The two arguments are swapped because the fbranch instructions
5655 * depend on this for the non-sse case to work.
5657 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5661 /* zeroing the register at the start results in
5662 * shorter and faster code (we can also remove the widening op)
5664 guchar *unordered_check;
5666 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5667 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5668 unordered_check = code;
5669 x86_branch8 (code, X86_CC_P, 0, FALSE);
5671 if (ins->opcode == OP_FCEQ) {
5672 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5673 amd64_patch (unordered_check, code);
5675 guchar *jump_to_end;
5676 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5678 x86_jump8 (code, 0);
5679 amd64_patch (unordered_check, code);
5680 amd64_inc_reg (code, ins->dreg);
5681 amd64_patch (jump_to_end, code);
5687 /* zeroing the register at the start results in
5688 * shorter and faster code (we can also remove the widening op)
5690 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5691 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5692 if (ins->opcode == OP_FCLT_UN) {
5693 guchar *unordered_check = code;
5694 guchar *jump_to_end;
5695 x86_branch8 (code, X86_CC_P, 0, FALSE);
5696 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5698 x86_jump8 (code, 0);
5699 amd64_patch (unordered_check, code);
5700 amd64_inc_reg (code, ins->dreg);
5701 amd64_patch (jump_to_end, code);
5703 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5708 guchar *unordered_check;
5709 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5710 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5711 unordered_check = code;
5712 x86_branch8 (code, X86_CC_P, 0, FALSE);
5713 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5714 amd64_patch (unordered_check, code);
5719 /* zeroing the register at the start results in
5720 * shorter and faster code (we can also remove the widening op)
5722 guchar *unordered_check;
5724 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5725 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5726 if (ins->opcode == OP_FCGT) {
5727 unordered_check = code;
5728 x86_branch8 (code, X86_CC_P, 0, FALSE);
5729 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5730 amd64_patch (unordered_check, code);
5732 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5737 guchar *unordered_check;
5738 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5739 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5740 unordered_check = code;
5741 x86_branch8 (code, X86_CC_P, 0, FALSE);
5742 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5743 amd64_patch (unordered_check, code);
5753 gboolean unordered = FALSE;
5755 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5756 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5758 switch (ins->opcode) {
5760 x86_cond = X86_CC_EQ;
5763 x86_cond = X86_CC_LT;
5766 x86_cond = X86_CC_GT;
5769 x86_cond = X86_CC_GT;
5773 x86_cond = X86_CC_LT;
5777 g_assert_not_reached ();
5782 guchar *unordered_check;
5783 guchar *jump_to_end;
5785 unordered_check = code;
5786 x86_branch8 (code, X86_CC_P, 0, FALSE);
5787 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5789 x86_jump8 (code, 0);
5790 amd64_patch (unordered_check, code);
5791 amd64_inc_reg (code, ins->dreg);
5792 amd64_patch (jump_to_end, code);
5794 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5798 case OP_FCLT_MEMBASE:
5799 case OP_FCGT_MEMBASE:
5800 case OP_FCLT_UN_MEMBASE:
5801 case OP_FCGT_UN_MEMBASE:
5802 case OP_FCEQ_MEMBASE: {
5803 guchar *unordered_check, *jump_to_end;
5806 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5807 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5809 switch (ins->opcode) {
5810 case OP_FCEQ_MEMBASE:
5811 x86_cond = X86_CC_EQ;
5813 case OP_FCLT_MEMBASE:
5814 case OP_FCLT_UN_MEMBASE:
5815 x86_cond = X86_CC_LT;
5817 case OP_FCGT_MEMBASE:
5818 case OP_FCGT_UN_MEMBASE:
5819 x86_cond = X86_CC_GT;
5822 g_assert_not_reached ();
5825 unordered_check = code;
5826 x86_branch8 (code, X86_CC_P, 0, FALSE);
5827 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5829 switch (ins->opcode) {
5830 case OP_FCEQ_MEMBASE:
5831 case OP_FCLT_MEMBASE:
5832 case OP_FCGT_MEMBASE:
5833 amd64_patch (unordered_check, code);
5835 case OP_FCLT_UN_MEMBASE:
5836 case OP_FCGT_UN_MEMBASE:
5838 x86_jump8 (code, 0);
5839 amd64_patch (unordered_check, code);
5840 amd64_inc_reg (code, ins->dreg);
5841 amd64_patch (jump_to_end, code);
5849 guchar *jump = code;
5850 x86_branch8 (code, X86_CC_P, 0, TRUE);
5851 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5852 amd64_patch (jump, code);
5856 /* Branch if C013 != 100 */
5857 /* branch if !ZF or (PF|CF) */
5858 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5859 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5860 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5863 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5866 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5867 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5871 if (ins->opcode == OP_FBGT) {
5874 /* skip branch if C1=1 */
5876 x86_branch8 (code, X86_CC_P, 0, FALSE);
5877 /* branch if (C0 | C3) = 1 */
5878 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5879 amd64_patch (br1, code);
5882 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5886 /* Branch if C013 == 100 or 001 */
5889 /* skip branch if C1=1 */
5891 x86_branch8 (code, X86_CC_P, 0, FALSE);
5892 /* branch if (C0 | C3) = 1 */
5893 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5894 amd64_patch (br1, code);
5898 /* Branch if C013 == 000 */
5899 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5902 /* Branch if C013=000 or 100 */
5905 /* skip branch if C1=1 */
5907 x86_branch8 (code, X86_CC_P, 0, FALSE);
5908 /* branch if C0=0 */
5909 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5910 amd64_patch (br1, code);
5914 /* Branch if C013 != 001 */
5915 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5916 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5919 /* Transfer value to the fp stack */
5920 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5921 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5922 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5924 amd64_push_reg (code, AMD64_RAX);
5926 amd64_fnstsw (code);
5927 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5928 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5929 amd64_pop_reg (code, AMD64_RAX);
5930 amd64_fstp (code, 0);
5931 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
5932 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5935 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5938 case OP_TLS_GET_REG:
5939 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5942 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5945 case OP_TLS_SET_REG: {
5946 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5949 case OP_MEMORY_BARRIER: {
5950 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5954 case OP_ATOMIC_ADD_I4:
5955 case OP_ATOMIC_ADD_I8: {
5956 int dreg = ins->dreg;
5957 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5959 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5962 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5963 amd64_prefix (code, X86_LOCK_PREFIX);
5964 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5965 /* dreg contains the old value, add with sreg2 value */
5966 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5968 if (ins->dreg != dreg)
5969 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5973 case OP_ATOMIC_EXCHANGE_I4:
5974 case OP_ATOMIC_EXCHANGE_I8: {
5975 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5977 /* LOCK prefix is implied. */
5978 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5979 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5980 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5983 case OP_ATOMIC_CAS_I4:
5984 case OP_ATOMIC_CAS_I8: {
5987 if (ins->opcode == OP_ATOMIC_CAS_I8)
5993 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5994 * an explanation of how this works.
5996 g_assert (ins->sreg3 == AMD64_RAX);
5997 g_assert (ins->sreg1 != AMD64_RAX);
5998 g_assert (ins->sreg1 != ins->sreg2);
6000 amd64_prefix (code, X86_LOCK_PREFIX);
6001 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
6003 if (ins->dreg != AMD64_RAX)
6004 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
6007 case OP_ATOMIC_LOAD_I1: {
6008 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
6011 case OP_ATOMIC_LOAD_U1: {
6012 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
6015 case OP_ATOMIC_LOAD_I2: {
6016 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
6019 case OP_ATOMIC_LOAD_U2: {
6020 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
6023 case OP_ATOMIC_LOAD_I4: {
6024 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
6027 case OP_ATOMIC_LOAD_U4:
6028 case OP_ATOMIC_LOAD_I8:
6029 case OP_ATOMIC_LOAD_U8: {
6030 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
6033 case OP_ATOMIC_LOAD_R4: {
6034 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
6035 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
6038 case OP_ATOMIC_LOAD_R8: {
6039 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
6042 case OP_ATOMIC_STORE_I1:
6043 case OP_ATOMIC_STORE_U1:
6044 case OP_ATOMIC_STORE_I2:
6045 case OP_ATOMIC_STORE_U2:
6046 case OP_ATOMIC_STORE_I4:
6047 case OP_ATOMIC_STORE_U4:
6048 case OP_ATOMIC_STORE_I8:
6049 case OP_ATOMIC_STORE_U8: {
6052 switch (ins->opcode) {
6053 case OP_ATOMIC_STORE_I1:
6054 case OP_ATOMIC_STORE_U1:
6057 case OP_ATOMIC_STORE_I2:
6058 case OP_ATOMIC_STORE_U2:
6061 case OP_ATOMIC_STORE_I4:
6062 case OP_ATOMIC_STORE_U4:
6065 case OP_ATOMIC_STORE_I8:
6066 case OP_ATOMIC_STORE_U8:
6071 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
6073 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6077 case OP_ATOMIC_STORE_R4: {
6078 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6079 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
6081 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6085 case OP_ATOMIC_STORE_R8: {
6088 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
6092 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6096 case OP_CARD_TABLE_WBARRIER: {
6097 int ptr = ins->sreg1;
6098 int value = ins->sreg2;
6100 int nursery_shift, card_table_shift;
6101 gpointer card_table_mask;
6102 size_t nursery_size;
6104 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
6105 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
6106 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
6108 /*If either point to the stack we can simply avoid the WB. This happens due to
6109 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
6111 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
6115 * We need one register we can clobber, we choose EDX and make sreg1
6116 * fixed EAX to work around limitations in the local register allocator.
6117 * sreg2 might get allocated to EDX, but that is not a problem since
6118 * we use it before clobbering EDX.
6120 g_assert (ins->sreg1 == AMD64_RAX);
6123 * This is the code we produce:
6126 * edx >>= nursery_shift
6127 * cmp edx, (nursery_start >> nursery_shift)
6130 * edx >>= card_table_shift
6136 if (mono_gc_card_table_nursery_check ()) {
6137 if (value != AMD64_RDX)
6138 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
6139 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
6140 if (shifted_nursery_start >> 31) {
6142 * The value we need to compare against is 64 bits, so we need
6143 * another spare register. We use RBX, which we save and
6146 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
6147 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
6148 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
6149 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
6151 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
6153 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
6155 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
6156 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
6157 if (card_table_mask)
6158 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
6160 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
6161 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
6163 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
6165 if (mono_gc_card_table_nursery_check ())
6166 x86_patch (br, code);
6169 #ifdef MONO_ARCH_SIMD_INTRINSICS
6170 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
6172 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
6175 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
6178 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
6181 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
6184 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
6187 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
6190 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6191 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6194 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
6197 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
6200 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
6203 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
6206 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6209 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6212 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
6215 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6218 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
6221 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6224 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
6227 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
6230 case OP_PSHUFLEW_HIGH:
6231 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6232 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6234 case OP_PSHUFLEW_LOW:
6235 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6236 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6239 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6240 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6243 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6244 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6247 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
6248 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6252 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
6255 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
6258 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
6261 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
6264 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
6267 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
6270 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6271 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6274 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
6277 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
6280 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
6283 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6286 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6289 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6292 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6295 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6298 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6301 case OP_EXTRACT_MASK:
6302 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6306 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6309 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6312 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6316 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6319 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6322 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6325 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6329 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6332 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6335 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6338 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6342 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6345 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6348 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6352 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6355 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6358 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6362 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6365 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6369 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6372 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6375 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6379 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6382 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6385 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6389 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6392 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6395 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6398 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6402 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6405 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6408 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6411 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6414 case OP_PSUM_ABS_DIFF:
6415 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6418 case OP_UNPACK_LOWB:
6419 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6421 case OP_UNPACK_LOWW:
6422 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6424 case OP_UNPACK_LOWD:
6425 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6427 case OP_UNPACK_LOWQ:
6428 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6430 case OP_UNPACK_LOWPS:
6431 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6433 case OP_UNPACK_LOWPD:
6434 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6437 case OP_UNPACK_HIGHB:
6438 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6440 case OP_UNPACK_HIGHW:
6441 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6443 case OP_UNPACK_HIGHD:
6444 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6446 case OP_UNPACK_HIGHQ:
6447 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6449 case OP_UNPACK_HIGHPS:
6450 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6452 case OP_UNPACK_HIGHPD:
6453 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6457 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6460 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6463 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6466 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6469 case OP_PADDB_SAT_UN:
6470 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6472 case OP_PSUBB_SAT_UN:
6473 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6475 case OP_PADDW_SAT_UN:
6476 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6478 case OP_PSUBW_SAT_UN:
6479 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6483 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6486 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6489 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6492 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6496 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6499 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6502 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6504 case OP_PMULW_HIGH_UN:
6505 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6508 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6512 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6515 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6519 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6522 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6526 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6529 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6533 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6536 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6540 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6543 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6547 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6550 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6554 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6557 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6560 /*TODO: This is appart of the sse spec but not added
6562 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6565 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6570 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6573 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6576 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6579 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6582 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6585 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6588 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6591 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6594 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6597 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6601 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6604 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6608 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6609 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6611 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6616 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6618 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6619 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6623 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6625 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6626 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6627 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6631 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6633 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6636 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6638 case OP_EXTRACTX_U2:
6639 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6641 case OP_INSERTX_U1_SLOW:
6642 /*sreg1 is the extracted ireg (scratch)
6643 /sreg2 is the to be inserted ireg (scratch)
6644 /dreg is the xreg to receive the value*/
6646 /*clear the bits from the extracted word*/
6647 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6648 /*shift the value to insert if needed*/
6649 if (ins->inst_c0 & 1)
6650 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6651 /*join them together*/
6652 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6653 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6655 case OP_INSERTX_I4_SLOW:
6656 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6657 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6658 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6660 case OP_INSERTX_I8_SLOW:
6661 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6663 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6665 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6668 case OP_INSERTX_R4_SLOW:
6669 switch (ins->inst_c0) {
6672 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6674 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6677 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
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(1, 0, 2, 3));
6685 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6687 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6689 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6690 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6693 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6695 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6697 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6698 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6702 case OP_INSERTX_R8_SLOW:
6704 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6706 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6708 case OP_STOREX_MEMBASE_REG:
6709 case OP_STOREX_MEMBASE:
6710 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6712 case OP_LOADX_MEMBASE:
6713 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6715 case OP_LOADX_ALIGNED_MEMBASE:
6716 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6718 case OP_STOREX_ALIGNED_MEMBASE_REG:
6719 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6721 case OP_STOREX_NTA_MEMBASE_REG:
6722 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6724 case OP_PREFETCH_MEMBASE:
6725 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6729 /*FIXME the peephole pass should have killed this*/
6730 if (ins->dreg != ins->sreg1)
6731 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6734 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6736 case OP_ICONV_TO_R4_RAW:
6737 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6740 case OP_FCONV_TO_R8_X:
6741 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6744 case OP_XCONV_R8_TO_I4:
6745 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6746 switch (ins->backend.source_opcode) {
6747 case OP_FCONV_TO_I1:
6748 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6750 case OP_FCONV_TO_U1:
6751 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6753 case OP_FCONV_TO_I2:
6754 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6756 case OP_FCONV_TO_U2:
6757 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6763 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6764 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6765 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6768 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6769 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6772 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6773 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6777 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6779 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6780 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6782 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6785 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6786 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6789 case OP_LIVERANGE_START: {
6790 if (cfg->verbose_level > 1)
6791 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6792 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6795 case OP_LIVERANGE_END: {
6796 if (cfg->verbose_level > 1)
6797 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6798 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6801 case OP_GC_SAFE_POINT: {
6802 const char *polling_func = NULL;
6803 int compare_val = 0;
6806 #if defined(__native_client_codegen__) && defined(__native_client_gc__)
6807 polling_func = "mono_nacl_gc";
6808 compare_val = 0xFFFFFFFF;
6810 g_assert (mono_threads_is_coop_enabled ());
6811 polling_func = "mono_threads_state_poll";
6815 amd64_test_membase_imm_size (code, ins->sreg1, 0, compare_val, 4);
6816 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6817 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func, FALSE);
6818 amd64_patch (br[0], code);
6822 case OP_GC_LIVENESS_DEF:
6823 case OP_GC_LIVENESS_USE:
6824 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6825 ins->backend.pc_offset = code - cfg->native_code;
6827 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6828 ins->backend.pc_offset = code - cfg->native_code;
6829 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6832 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6833 g_assert_not_reached ();
6836 if ((code - cfg->native_code - offset) > max_len) {
6837 #if !defined(__native_client_codegen__)
6838 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6839 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6840 g_assert_not_reached ();
6845 cfg->code_len = code - cfg->native_code;
6848 #endif /* DISABLE_JIT */
6851 mono_arch_register_lowlevel_calls (void)
6853 /* The signature doesn't matter */
6854 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6858 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
6860 unsigned char *ip = ji->ip.i + code;
6863 * Debug code to help track down problems where the target of a near call is
6866 if (amd64_is_near_call (ip)) {
6867 gint64 disp = (guint8*)target - (guint8*)ip;
6869 if (!amd64_is_imm32 (disp)) {
6870 printf ("TYPE: %d\n", ji->type);
6872 case MONO_PATCH_INFO_INTERNAL_METHOD:
6873 printf ("V: %s\n", ji->data.name);
6875 case MONO_PATCH_INFO_METHOD_JUMP:
6876 case MONO_PATCH_INFO_METHOD:
6877 printf ("V: %s\n", ji->data.method->name);
6885 amd64_patch (ip, (gpointer)target);
6891 get_max_epilog_size (MonoCompile *cfg)
6893 int max_epilog_size = 16;
6895 if (cfg->method->save_lmf)
6896 max_epilog_size += 256;
6898 if (mono_jit_trace_calls != NULL)
6899 max_epilog_size += 50;
6901 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6902 max_epilog_size += 50;
6904 max_epilog_size += (AMD64_NREG * 2);
6906 return max_epilog_size;
6910 * This macro is used for testing whenever the unwinder works correctly at every point
6911 * where an async exception can happen.
6913 /* This will generate a SIGSEGV at the given point in the code */
6914 #define async_exc_point(code) do { \
6915 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6916 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6917 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6918 cfg->arch.async_point_count ++; \
6923 mono_arch_emit_prolog (MonoCompile *cfg)
6925 MonoMethod *method = cfg->method;
6927 MonoMethodSignature *sig;
6929 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6932 MonoInst *lmf_var = cfg->lmf_var;
6933 gboolean args_clobbered = FALSE;
6934 gboolean trace = FALSE;
6935 #ifdef __native_client_codegen__
6936 guint alignment_check;
6939 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6941 #if defined(__default_codegen__)
6942 code = cfg->native_code = (unsigned char *)g_malloc (cfg->code_size);
6943 #elif defined(__native_client_codegen__)
6944 /* native_code_alloc is not 32-byte aligned, native_code is. */
6945 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6947 /* Align native_code to next nearest kNaclAlignment byte. */
6948 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6949 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6951 code = cfg->native_code;
6953 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6954 g_assert (alignment_check == 0);
6957 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6960 /* Amount of stack space allocated by register saving code */
6963 /* Offset between RSP and the CFA */
6967 * The prolog consists of the following parts:
6969 * - push rbp, mov rbp, rsp
6970 * - save callee saved regs using pushes
6972 * - save rgctx if needed
6973 * - save lmf if needed
6976 * - save rgctx if needed
6977 * - save lmf if needed
6978 * - save callee saved regs using moves
6983 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6984 // IP saved at CFA - 8
6985 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6986 async_exc_point (code);
6987 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6989 if (!cfg->arch.omit_fp) {
6990 amd64_push_reg (code, AMD64_RBP);
6992 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6993 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6994 async_exc_point (code);
6996 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6998 /* These are handled automatically by the stack marking code */
6999 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
7001 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
7002 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
7003 async_exc_point (code);
7005 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
7009 /* The param area is always at offset 0 from sp */
7010 /* This needs to be allocated here, since it has to come after the spill area */
7011 if (cfg->param_area) {
7012 if (cfg->arch.omit_fp)
7014 g_assert_not_reached ();
7015 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
7018 if (cfg->arch.omit_fp) {
7020 * On enter, the stack is misaligned by the pushing of the return
7021 * address. It is either made aligned by the pushing of %rbp, or by
7024 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
7025 if ((alloc_size % 16) == 0) {
7027 /* Mark the padding slot as NOREF */
7028 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
7031 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
7032 if (cfg->stack_offset != alloc_size) {
7033 /* Mark the padding slot as NOREF */
7034 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
7036 cfg->arch.sp_fp_offset = alloc_size;
7040 cfg->arch.stack_alloc_size = alloc_size;
7042 /* Allocate stack frame */
7044 /* See mono_emit_stack_alloc */
7045 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
7046 guint32 remaining_size = alloc_size;
7047 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
7048 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
7049 guint32 offset = code - cfg->native_code;
7050 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
7051 while (required_code_size >= (cfg->code_size - offset))
7052 cfg->code_size *= 2;
7053 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7054 code = cfg->native_code + offset;
7055 cfg->stat_code_reallocs++;
7058 while (remaining_size >= 0x1000) {
7059 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
7060 if (cfg->arch.omit_fp) {
7061 cfa_offset += 0x1000;
7062 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7064 async_exc_point (code);
7066 if (cfg->arch.omit_fp)
7067 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
7070 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
7071 remaining_size -= 0x1000;
7073 if (remaining_size) {
7074 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
7075 if (cfg->arch.omit_fp) {
7076 cfa_offset += remaining_size;
7077 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7078 async_exc_point (code);
7081 if (cfg->arch.omit_fp)
7082 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
7086 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
7087 if (cfg->arch.omit_fp) {
7088 cfa_offset += alloc_size;
7089 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7090 async_exc_point (code);
7095 /* Stack alignment check */
7100 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
7101 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
7102 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
7104 x86_branch8 (code, X86_CC_EQ, 1, FALSE);
7105 amd64_breakpoint (code);
7106 amd64_patch (buf, code);
7110 if (mini_get_debug_options ()->init_stacks) {
7111 /* Fill the stack frame with a dummy value to force deterministic behavior */
7113 /* Save registers to the red zone */
7114 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
7115 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
7117 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
7118 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
7119 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
7122 #if defined(__default_codegen__)
7123 amd64_prefix (code, X86_REP_PREFIX);
7125 #elif defined(__native_client_codegen__)
7126 /* NaCl stos pseudo-instruction */
7127 amd64_codegen_pre (code);
7128 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
7129 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
7130 /* Add %r15 to %rdi using lea, condition flags unaffected. */
7131 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
7132 amd64_prefix (code, X86_REP_PREFIX);
7134 amd64_codegen_post (code);
7135 #endif /* __native_client_codegen__ */
7137 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
7138 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
7142 if (method->save_lmf)
7143 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
7145 /* Save callee saved registers */
7146 if (cfg->arch.omit_fp) {
7147 save_area_offset = cfg->arch.reg_save_area_offset;
7148 /* Save caller saved registers after sp is adjusted */
7149 /* The registers are saved at the bottom of the frame */
7150 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
7152 /* The registers are saved just below the saved rbp */
7153 save_area_offset = cfg->arch.reg_save_area_offset;
7156 for (i = 0; i < AMD64_NREG; ++i) {
7157 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7158 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
7160 if (cfg->arch.omit_fp) {
7161 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
7162 /* These are handled automatically by the stack marking code */
7163 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
7165 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
7169 save_area_offset += 8;
7170 async_exc_point (code);
7174 /* store runtime generic context */
7175 if (cfg->rgctx_var) {
7176 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
7177 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
7179 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
7181 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
7182 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
7185 /* compute max_length in order to use short forward jumps */
7186 max_epilog_size = get_max_epilog_size (cfg);
7187 if (cfg->opt & MONO_OPT_BRANCH) {
7188 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7192 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7194 /* max alignment for loops */
7195 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
7196 max_length += LOOP_ALIGNMENT;
7197 #ifdef __native_client_codegen__
7198 /* max alignment for native client */
7199 max_length += kNaClAlignment;
7202 MONO_BB_FOR_EACH_INS (bb, ins) {
7203 #ifdef __native_client_codegen__
7205 int space_in_block = kNaClAlignment -
7206 ((max_length + cfg->code_len) & kNaClAlignmentMask);
7207 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7208 if (space_in_block < max_len && max_len < kNaClAlignment) {
7209 max_length += space_in_block;
7212 #endif /*__native_client_codegen__*/
7213 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7216 /* Take prolog and epilog instrumentation into account */
7217 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
7218 max_length += max_epilog_size;
7220 bb->max_length = max_length;
7224 sig = mono_method_signature (method);
7227 cinfo = (CallInfo *)cfg->arch.cinfo;
7229 if (sig->ret->type != MONO_TYPE_VOID) {
7230 /* Save volatile arguments to the stack */
7231 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
7232 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
7235 /* Keep this in sync with emit_load_volatile_arguments */
7236 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
7237 ArgInfo *ainfo = cinfo->args + i;
7239 ins = cfg->args [i];
7241 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
7242 /* Unused arguments */
7245 /* Save volatile arguments to the stack */
7246 if (ins->opcode != OP_REGVAR) {
7247 switch (ainfo->storage) {
7253 if (stack_offset & 0x1)
7255 else if (stack_offset & 0x2)
7257 else if (stack_offset & 0x4)
7262 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
7265 * Save the original location of 'this',
7266 * get_generic_info_from_stack_frame () needs this to properly look up
7267 * the argument value during the handling of async exceptions.
7269 if (ins == cfg->args [0]) {
7270 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7271 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
7275 case ArgInFloatSSEReg:
7276 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7278 case ArgInDoubleSSEReg:
7279 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7281 case ArgValuetypeInReg:
7282 for (quad = 0; quad < 2; quad ++) {
7283 switch (ainfo->pair_storage [quad]) {
7285 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7287 case ArgInFloatSSEReg:
7288 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7290 case ArgInDoubleSSEReg:
7291 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7296 g_assert_not_reached ();
7300 case ArgValuetypeAddrInIReg:
7301 if (ainfo->pair_storage [0] == ArgInIReg)
7302 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7304 case ArgGSharedVtInReg:
7305 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, 8);
7311 /* Argument allocated to (non-volatile) register */
7312 switch (ainfo->storage) {
7314 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7317 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7320 g_assert_not_reached ();
7323 if (ins == cfg->args [0]) {
7324 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7325 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7330 if (cfg->method->save_lmf)
7331 args_clobbered = TRUE;
7334 args_clobbered = TRUE;
7335 code = (guint8 *)mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7338 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7339 args_clobbered = TRUE;
7342 * Optimize the common case of the first bblock making a call with the same
7343 * arguments as the method. This works because the arguments are still in their
7344 * original argument registers.
7345 * FIXME: Generalize this
7347 if (!args_clobbered) {
7348 MonoBasicBlock *first_bb = cfg->bb_entry;
7350 int filter = FILTER_IL_SEQ_POINT;
7352 next = mono_bb_first_inst (first_bb, filter);
7353 if (!next && first_bb->next_bb) {
7354 first_bb = first_bb->next_bb;
7355 next = mono_bb_first_inst (first_bb, filter);
7358 if (first_bb->in_count > 1)
7361 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7362 ArgInfo *ainfo = cinfo->args + i;
7363 gboolean match = FALSE;
7365 ins = cfg->args [i];
7366 if (ins->opcode != OP_REGVAR) {
7367 switch (ainfo->storage) {
7369 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7370 if (next->dreg == ainfo->reg) {
7374 next->opcode = OP_MOVE;
7375 next->sreg1 = ainfo->reg;
7376 /* Only continue if the instruction doesn't change argument regs */
7377 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7387 /* Argument allocated to (non-volatile) register */
7388 switch (ainfo->storage) {
7390 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7401 next = mono_inst_next (next, filter);
7402 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7409 if (cfg->gen_sdb_seq_points) {
7410 MonoInst *info_var = (MonoInst *)cfg->arch.seq_point_info_var;
7412 /* Initialize seq_point_info_var */
7413 if (cfg->compile_aot) {
7414 /* Initialize the variable from a GOT slot */
7415 /* Same as OP_AOTCONST */
7416 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7417 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7418 g_assert (info_var->opcode == OP_REGOFFSET);
7419 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7422 if (cfg->compile_aot) {
7423 /* Initialize ss_tramp_var */
7424 ins = (MonoInst *)cfg->arch.ss_tramp_var;
7425 g_assert (ins->opcode == OP_REGOFFSET);
7427 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7428 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr), 8);
7429 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7431 /* Initialize ss_tramp_var */
7432 ins = (MonoInst *)cfg->arch.ss_tramp_var;
7433 g_assert (ins->opcode == OP_REGOFFSET);
7435 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&ss_trampoline);
7436 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7438 /* Initialize bp_tramp_var */
7439 ins = (MonoInst *)cfg->arch.bp_tramp_var;
7440 g_assert (ins->opcode == OP_REGOFFSET);
7442 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&bp_trampoline);
7443 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7447 cfg->code_len = code - cfg->native_code;
7449 g_assert (cfg->code_len < cfg->code_size);
7455 mono_arch_emit_epilog (MonoCompile *cfg)
7457 MonoMethod *method = cfg->method;
7460 int max_epilog_size;
7462 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7463 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7465 max_epilog_size = get_max_epilog_size (cfg);
7467 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7468 cfg->code_size *= 2;
7469 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7470 cfg->stat_code_reallocs++;
7472 code = cfg->native_code + cfg->code_len;
7474 cfg->has_unwind_info_for_epilog = TRUE;
7476 /* Mark the start of the epilog */
7477 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7479 /* Save the uwind state which is needed by the out-of-line code */
7480 mono_emit_unwind_op_remember_state (cfg, code);
7482 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7483 code = (guint8 *)mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7485 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7487 if (method->save_lmf) {
7488 /* check if we need to restore protection of the stack after a stack overflow */
7489 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7491 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7492 /* we load the value in a separate instruction: this mechanism may be
7493 * used later as a safer way to do thread interruption
7495 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7496 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7498 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7499 /* note that the call trampoline will preserve eax/edx */
7500 x86_call_reg (code, X86_ECX);
7501 x86_patch (patch, code);
7503 /* FIXME: maybe save the jit tls in the prolog */
7505 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7506 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7510 /* Restore callee saved regs */
7511 for (i = 0; i < AMD64_NREG; ++i) {
7512 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7513 /* Restore only used_int_regs, not arch.saved_iregs */
7514 if (cfg->used_int_regs & (1 << i)) {
7515 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7516 mono_emit_unwind_op_same_value (cfg, code, i);
7517 async_exc_point (code);
7519 save_area_offset += 8;
7523 /* Load returned vtypes into registers if needed */
7524 cinfo = (CallInfo *)cfg->arch.cinfo;
7525 if (cinfo->ret.storage == ArgValuetypeInReg) {
7526 ArgInfo *ainfo = &cinfo->ret;
7527 MonoInst *inst = cfg->ret;
7529 for (quad = 0; quad < 2; quad ++) {
7530 switch (ainfo->pair_storage [quad]) {
7532 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7534 case ArgInFloatSSEReg:
7535 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7537 case ArgInDoubleSSEReg:
7538 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7543 g_assert_not_reached ();
7548 if (cfg->arch.omit_fp) {
7549 if (cfg->arch.stack_alloc_size) {
7550 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7554 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7556 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7557 async_exc_point (code);
7560 /* Restore the unwind state to be the same as before the epilog */
7561 mono_emit_unwind_op_restore_state (cfg, code);
7563 cfg->code_len = code - cfg->native_code;
7565 g_assert (cfg->code_len < cfg->code_size);
7569 mono_arch_emit_exceptions (MonoCompile *cfg)
7571 MonoJumpInfo *patch_info;
7574 MonoClass *exc_classes [16];
7575 guint8 *exc_throw_start [16], *exc_throw_end [16];
7576 guint32 code_size = 0;
7578 /* Compute needed space */
7579 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7580 if (patch_info->type == MONO_PATCH_INFO_EXC)
7582 if (patch_info->type == MONO_PATCH_INFO_R8)
7583 code_size += 8 + 15; /* sizeof (double) + alignment */
7584 if (patch_info->type == MONO_PATCH_INFO_R4)
7585 code_size += 4 + 15; /* sizeof (float) + alignment */
7586 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7587 code_size += 8 + 7; /*sizeof (void*) + alignment */
7590 #ifdef __native_client_codegen__
7591 /* Give us extra room on Native Client. This could be */
7592 /* more carefully calculated, but bundle alignment makes */
7593 /* it much trickier, so *2 like other places is good. */
7597 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7598 cfg->code_size *= 2;
7599 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7600 cfg->stat_code_reallocs++;
7603 code = cfg->native_code + cfg->code_len;
7605 /* add code to raise exceptions */
7607 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7608 switch (patch_info->type) {
7609 case MONO_PATCH_INFO_EXC: {
7610 MonoClass *exc_class;
7614 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7616 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7617 g_assert (exc_class);
7618 throw_ip = patch_info->ip.i;
7620 //x86_breakpoint (code);
7621 /* Find a throw sequence for the same exception class */
7622 for (i = 0; i < nthrows; ++i)
7623 if (exc_classes [i] == exc_class)
7626 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7627 x86_jump_code (code, exc_throw_start [i]);
7628 patch_info->type = MONO_PATCH_INFO_NONE;
7632 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7636 exc_classes [nthrows] = exc_class;
7637 exc_throw_start [nthrows] = code;
7639 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7641 patch_info->type = MONO_PATCH_INFO_NONE;
7643 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7645 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7650 exc_throw_end [nthrows] = code;
7660 g_assert(code < cfg->native_code + cfg->code_size);
7663 /* Handle relocations with RIP relative addressing */
7664 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7665 gboolean remove = FALSE;
7666 guint8 *orig_code = code;
7668 switch (patch_info->type) {
7669 case MONO_PATCH_INFO_R8:
7670 case MONO_PATCH_INFO_R4: {
7671 guint8 *pos, *patch_pos;
7674 /* The SSE opcodes require a 16 byte alignment */
7675 #if defined(__default_codegen__)
7676 code = (guint8*)ALIGN_TO (code, 16);
7677 #elif defined(__native_client_codegen__)
7679 /* Pad this out with HLT instructions */
7680 /* or we can get garbage bytes emitted */
7681 /* which will fail validation */
7682 guint8 *aligned_code;
7683 /* extra align to make room for */
7684 /* mov/push below */
7685 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7686 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7687 /* The technique of hiding data in an */
7688 /* instruction has a problem here: we */
7689 /* need the data aligned to a 16-byte */
7690 /* boundary but the instruction cannot */
7691 /* cross the bundle boundary. so only */
7692 /* odd multiples of 16 can be used */
7693 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7696 while (code < aligned_code) {
7697 *(code++) = 0xf4; /* hlt */
7702 pos = cfg->native_code + patch_info->ip.i;
7703 if (IS_REX (pos [1])) {
7704 patch_pos = pos + 5;
7705 target_pos = code - pos - 9;
7708 patch_pos = pos + 4;
7709 target_pos = code - pos - 8;
7712 if (patch_info->type == MONO_PATCH_INFO_R8) {
7713 #ifdef __native_client_codegen__
7714 /* Hide 64-bit data in a */
7715 /* "mov imm64, r11" instruction. */
7716 /* write it before the start of */
7718 *(code-2) = 0x49; /* prefix */
7719 *(code-1) = 0xbb; /* mov X, %r11 */
7721 *(double*)code = *(double*)patch_info->data.target;
7722 code += sizeof (double);
7724 #ifdef __native_client_codegen__
7725 /* Hide 32-bit data in a */
7726 /* "push imm32" instruction. */
7727 *(code-1) = 0x68; /* push */
7729 *(float*)code = *(float*)patch_info->data.target;
7730 code += sizeof (float);
7733 *(guint32*)(patch_pos) = target_pos;
7738 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7741 if (cfg->compile_aot)
7744 /*loading is faster against aligned addresses.*/
7745 code = (guint8*)ALIGN_TO (code, 8);
7746 memset (orig_code, 0, code - orig_code);
7748 pos = cfg->native_code + patch_info->ip.i;
7750 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7751 if (IS_REX (pos [1]))
7752 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7754 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7756 *(gpointer*)code = (gpointer)patch_info->data.target;
7757 code += sizeof (gpointer);
7767 if (patch_info == cfg->patch_info)
7768 cfg->patch_info = patch_info->next;
7772 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7774 tmp->next = patch_info->next;
7777 g_assert (code < cfg->native_code + cfg->code_size);
7780 cfg->code_len = code - cfg->native_code;
7782 g_assert (cfg->code_len < cfg->code_size);
7786 #endif /* DISABLE_JIT */
7789 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7791 guchar *code = (guchar *)p;
7792 MonoMethodSignature *sig;
7794 int i, n, stack_area = 0;
7796 /* Keep this in sync with mono_arch_get_argument_info */
7798 if (enable_arguments) {
7799 /* Allocate a new area on the stack and save arguments there */
7800 sig = mono_method_signature (cfg->method);
7802 n = sig->param_count + sig->hasthis;
7804 stack_area = ALIGN_TO (n * 8, 16);
7806 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7808 for (i = 0; i < n; ++i) {
7809 inst = cfg->args [i];
7811 if (inst->opcode == OP_REGVAR)
7812 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7814 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7815 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7820 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7821 amd64_set_reg_template (code, AMD64_ARG_REG1);
7822 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7823 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7825 if (enable_arguments)
7826 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7840 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7842 guchar *code = (guchar *)p;
7843 int save_mode = SAVE_NONE;
7844 MonoMethod *method = cfg->method;
7845 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
7848 switch (ret_type->type) {
7849 case MONO_TYPE_VOID:
7850 /* special case string .ctor icall */
7851 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7852 save_mode = SAVE_EAX;
7854 save_mode = SAVE_NONE;
7858 save_mode = SAVE_EAX;
7862 save_mode = SAVE_XMM;
7864 case MONO_TYPE_GENERICINST:
7865 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7866 save_mode = SAVE_EAX;
7870 case MONO_TYPE_VALUETYPE:
7871 save_mode = SAVE_STRUCT;
7874 save_mode = SAVE_EAX;
7878 /* Save the result and copy it into the proper argument register */
7879 switch (save_mode) {
7881 amd64_push_reg (code, AMD64_RAX);
7883 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7884 if (enable_arguments)
7885 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7889 if (enable_arguments)
7890 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7893 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7894 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7896 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7898 * The result is already in the proper argument register so no copying
7905 g_assert_not_reached ();
7908 /* Set %al since this is a varargs call */
7909 if (save_mode == SAVE_XMM)
7910 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7912 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7914 if (preserve_argument_registers) {
7915 for (i = 0; i < PARAM_REGS; ++i)
7916 amd64_push_reg (code, param_regs [i]);
7919 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7920 amd64_set_reg_template (code, AMD64_ARG_REG1);
7921 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7923 if (preserve_argument_registers) {
7924 for (i = PARAM_REGS - 1; i >= 0; --i)
7925 amd64_pop_reg (code, param_regs [i]);
7928 /* Restore result */
7929 switch (save_mode) {
7931 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7932 amd64_pop_reg (code, AMD64_RAX);
7938 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7939 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7940 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7945 g_assert_not_reached ();
7952 mono_arch_flush_icache (guint8 *code, gint size)
7958 mono_arch_flush_register_windows (void)
7963 mono_arch_is_inst_imm (gint64 imm)
7965 return amd64_use_imm32 (imm);
7969 * Determine whenever the trap whose info is in SIGINFO is caused by
7973 mono_arch_is_int_overflow (void *sigctx, void *info)
7980 mono_sigctx_to_monoctx (sigctx, &ctx);
7982 rip = (guint8*)ctx.gregs [AMD64_RIP];
7984 if (IS_REX (rip [0])) {
7985 reg = amd64_rex_b (rip [0]);
7991 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7993 reg += x86_modrm_rm (rip [1]);
7995 value = ctx.gregs [reg];
8005 mono_arch_get_patch_offset (guint8 *code)
8011 * mono_breakpoint_clean_code:
8013 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
8014 * breakpoints in the original code, they are removed in the copy.
8016 * Returns TRUE if no sw breakpoint was present.
8019 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
8022 * If method_start is non-NULL we need to perform bound checks, since we access memory
8023 * at code - offset we could go before the start of the method and end up in a different
8024 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
8027 if (!method_start || code - offset >= method_start) {
8028 memcpy (buf, code - offset, size);
8030 int diff = code - method_start;
8031 memset (buf, 0, size);
8032 memcpy (buf + offset - diff, method_start, diff + size - offset);
8037 #if defined(__native_client_codegen__)
8038 /* For membase calls, we want the base register. for Native Client, */
8039 /* all indirect calls have the following sequence with the given sizes: */
8040 /* mov %eXX,%eXX [2-3] */
8041 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
8042 /* and $0xffffffffffffffe0,%r11d [4] */
8043 /* add %r15,%r11 [3] */
8044 /* callq *%r11 [3] */
8047 /* Determine if code points to a NaCl call-through-register sequence, */
8048 /* (i.e., the last 3 instructions listed above) */
8050 is_nacl_call_reg_sequence(guint8* code)
8052 const char *sequence = "\x41\x83\xe3\xe0" /* and */
8053 "\x4d\x03\xdf" /* add */
8054 "\x41\xff\xd3"; /* call */
8055 return memcmp(code, sequence, 10) == 0;
8058 /* Determine if code points to the first opcode of the mov membase component */
8059 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
8060 /* (there could be a REX prefix before the opcode but it is ignored) */
8062 is_nacl_indirect_call_membase_sequence(guint8* code)
8064 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
8065 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
8066 /* and that src reg = dest reg */
8067 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
8068 /* Check that next inst is mov, uses SIB byte (rm = 4), */
8070 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
8071 /* and has dst of r11 and base of r15 */
8072 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
8073 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
8075 #endif /* __native_client_codegen__ */
8078 mono_arch_get_this_arg_reg (guint8 *code)
8080 return AMD64_ARG_REG1;
8084 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
8086 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
8089 #define MAX_ARCH_DELEGATE_PARAMS 10
8092 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, guint32 param_count)
8094 guint8 *code, *start;
8095 GSList *unwind_ops = NULL;
8098 unwind_ops = mono_arch_get_cie_program ();
8101 start = code = (guint8 *)mono_global_codeman_reserve (64);
8103 /* Replace the this argument with the target */
8104 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8105 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8106 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8108 g_assert ((code - start) < 64);
8110 start = code = (guint8 *)mono_global_codeman_reserve (64);
8112 if (param_count == 0) {
8113 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8115 /* We have to shift the arguments left */
8116 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8117 for (i = 0; i < param_count; ++i) {
8120 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8122 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
8124 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8128 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8130 g_assert ((code - start) < 64);
8133 nacl_global_codeman_validate (&start, 64, &code);
8134 mono_arch_flush_icache (start, code - start);
8137 *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
8139 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
8140 *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
8144 if (mono_jit_map_is_enabled ()) {
8147 buff = (char*)"delegate_invoke_has_target";
8149 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
8150 mono_emit_jit_tramp (start, code - start, buff);
8154 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8159 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
8162 get_delegate_virtual_invoke_impl (MonoTrampInfo **info, gboolean load_imt_reg, int offset)
8164 guint8 *code, *start;
8169 if (offset / (int)sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
8172 start = code = (guint8 *)mono_global_codeman_reserve (size);
8174 unwind_ops = mono_arch_get_cie_program ();
8176 /* Replace the this argument with the target */
8177 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8178 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8181 /* Load the IMT reg */
8182 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
8185 /* Load the vtable */
8186 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
8187 amd64_jump_membase (code, AMD64_RAX, offset);
8188 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8191 tramp_name = g_strdup_printf ("delegate_virtual_invoke_imt_%d", - offset / sizeof (gpointer));
8193 tramp_name = g_strdup_printf ("delegate_virtual_invoke_%d", offset / sizeof (gpointer));
8194 *info = mono_tramp_info_create (tramp_name, start, code - start, NULL, unwind_ops);
8195 g_free (tramp_name);
8201 * mono_arch_get_delegate_invoke_impls:
8203 * Return a list of MonoTrampInfo structures for the delegate invoke impl
8207 mono_arch_get_delegate_invoke_impls (void)
8210 MonoTrampInfo *info;
8213 get_delegate_invoke_impl (&info, TRUE, 0);
8214 res = g_slist_prepend (res, info);
8216 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
8217 get_delegate_invoke_impl (&info, FALSE, i);
8218 res = g_slist_prepend (res, info);
8221 for (i = 0; i <= MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
8222 get_delegate_virtual_invoke_impl (&info, TRUE, - i * SIZEOF_VOID_P);
8223 res = g_slist_prepend (res, info);
8225 get_delegate_virtual_invoke_impl (&info, FALSE, i * SIZEOF_VOID_P);
8226 res = g_slist_prepend (res, info);
8233 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
8235 guint8 *code, *start;
8238 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
8241 /* FIXME: Support more cases */
8242 if (MONO_TYPE_ISSTRUCT (mini_get_underlying_type (sig->ret)))
8246 static guint8* cached = NULL;
8251 if (mono_aot_only) {
8252 start = (guint8 *)mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
8254 MonoTrampInfo *info;
8255 start = (guint8 *)get_delegate_invoke_impl (&info, TRUE, 0);
8256 mono_tramp_info_register (info, NULL);
8259 mono_memory_barrier ();
8263 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
8264 for (i = 0; i < sig->param_count; ++i)
8265 if (!mono_is_regsize_var (sig->params [i]))
8267 if (sig->param_count > 4)
8270 code = cache [sig->param_count];
8274 if (mono_aot_only) {
8275 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
8276 start = (guint8 *)mono_aot_get_trampoline (name);
8279 MonoTrampInfo *info;
8280 start = (guint8 *)get_delegate_invoke_impl (&info, FALSE, sig->param_count);
8281 mono_tramp_info_register (info, NULL);
8284 mono_memory_barrier ();
8286 cache [sig->param_count] = start;
8293 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
8295 MonoTrampInfo *info;
8298 code = get_delegate_virtual_invoke_impl (&info, load_imt_reg, offset);
8300 mono_tramp_info_register (info, NULL);
8305 mono_arch_finish_init (void)
8307 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
8308 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
8313 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8317 #if defined(__default_codegen__)
8318 #define CMP_SIZE (6 + 1)
8319 #define CMP_REG_REG_SIZE (4 + 1)
8320 #define BR_SMALL_SIZE 2
8321 #define BR_LARGE_SIZE 6
8322 #define MOV_REG_IMM_SIZE 10
8323 #define MOV_REG_IMM_32BIT_SIZE 6
8324 #define JUMP_REG_SIZE (2 + 1)
8325 #elif defined(__native_client_codegen__)
8326 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8327 #define CMP_SIZE ((6 + 1) * 2 - 1)
8328 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8329 #define BR_SMALL_SIZE (2 * 2 - 1)
8330 #define BR_LARGE_SIZE (6 * 2 - 1)
8331 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8332 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8333 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8334 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8335 /* Jump membase's size is large and unpredictable */
8336 /* in native client, just pad it out a whole bundle. */
8337 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8341 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8343 int i, distance = 0;
8344 for (i = start; i < target; ++i)
8345 distance += imt_entries [i]->chunk_size;
8350 * LOCKING: called with the domain lock held
8353 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8354 gpointer fail_tramp)
8358 guint8 *code, *start;
8359 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8362 for (i = 0; i < count; ++i) {
8363 MonoIMTCheckItem *item = imt_entries [i];
8364 if (item->is_equals) {
8365 if (item->check_target_idx) {
8366 if (!item->compare_done) {
8367 if (amd64_use_imm32 ((gint64)item->key))
8368 item->chunk_size += CMP_SIZE;
8370 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8372 if (item->has_target_code) {
8373 item->chunk_size += MOV_REG_IMM_SIZE;
8375 if (vtable_is_32bit)
8376 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8378 item->chunk_size += MOV_REG_IMM_SIZE;
8379 #ifdef __native_client_codegen__
8380 item->chunk_size += JUMP_MEMBASE_SIZE;
8383 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8386 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8387 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8389 if (vtable_is_32bit)
8390 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8392 item->chunk_size += MOV_REG_IMM_SIZE;
8393 item->chunk_size += JUMP_REG_SIZE;
8394 /* with assert below:
8395 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8397 #ifdef __native_client_codegen__
8398 item->chunk_size += JUMP_MEMBASE_SIZE;
8403 if (amd64_use_imm32 ((gint64)item->key))
8404 item->chunk_size += CMP_SIZE;
8406 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8407 item->chunk_size += BR_LARGE_SIZE;
8408 imt_entries [item->check_target_idx]->compare_done = TRUE;
8410 size += item->chunk_size;
8412 #if defined(__native_client__) && defined(__native_client_codegen__)
8413 /* In Native Client, we don't re-use thunks, allocate from the */
8414 /* normal code manager paths. */
8415 code = mono_domain_code_reserve (domain, size);
8418 code = (guint8 *)mono_method_alloc_generic_virtual_thunk (domain, size);
8420 code = (guint8 *)mono_domain_code_reserve (domain, size);
8424 unwind_ops = mono_arch_get_cie_program ();
8426 for (i = 0; i < count; ++i) {
8427 MonoIMTCheckItem *item = imt_entries [i];
8428 item->code_target = code;
8429 if (item->is_equals) {
8430 gboolean fail_case = !item->check_target_idx && fail_tramp;
8432 if (item->check_target_idx || fail_case) {
8433 if (!item->compare_done || fail_case) {
8434 if (amd64_use_imm32 ((gint64)item->key))
8435 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8437 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof(gpointer));
8438 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8441 item->jmp_code = code;
8442 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8443 if (item->has_target_code) {
8444 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8445 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8447 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8448 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8452 amd64_patch (item->jmp_code, code);
8453 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8454 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8455 item->jmp_code = NULL;
8458 /* enable the commented code to assert on wrong method */
8460 if (amd64_is_imm32 (item->key))
8461 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8463 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8464 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8466 item->jmp_code = code;
8467 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8468 /* See the comment below about R10 */
8469 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8470 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8471 amd64_patch (item->jmp_code, code);
8472 amd64_breakpoint (code);
8473 item->jmp_code = NULL;
8475 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8476 needs to be preserved. R10 needs
8477 to be preserved for calls which
8478 require a runtime generic context,
8479 but interface calls don't. */
8480 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8481 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8485 if (amd64_use_imm32 ((gint64)item->key))
8486 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8488 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof (gpointer));
8489 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8491 item->jmp_code = code;
8492 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8493 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8495 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8497 g_assert (code - item->code_target <= item->chunk_size);
8499 /* patch the branches to get to the target items */
8500 for (i = 0; i < count; ++i) {
8501 MonoIMTCheckItem *item = imt_entries [i];
8502 if (item->jmp_code) {
8503 if (item->check_target_idx) {
8504 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8510 mono_stats.imt_thunks_size += code - start;
8511 g_assert (code - start <= size);
8513 nacl_domain_code_validate(domain, &start, size, &code);
8514 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8516 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
8522 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8524 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8528 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8530 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8534 mono_arch_get_cie_program (void)
8538 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8539 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8547 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8549 MonoInst *ins = NULL;
8552 if (cmethod->klass == mono_defaults.math_class) {
8553 if (strcmp (cmethod->name, "Sin") == 0) {
8555 } else if (strcmp (cmethod->name, "Cos") == 0) {
8557 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8559 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8563 if (opcode && fsig->param_count == 1) {
8564 MONO_INST_NEW (cfg, ins, opcode);
8565 ins->type = STACK_R8;
8566 ins->dreg = mono_alloc_freg (cfg);
8567 ins->sreg1 = args [0]->dreg;
8568 MONO_ADD_INS (cfg->cbb, ins);
8572 if (cfg->opt & MONO_OPT_CMOV) {
8573 if (strcmp (cmethod->name, "Min") == 0) {
8574 if (fsig->params [0]->type == MONO_TYPE_I4)
8576 if (fsig->params [0]->type == MONO_TYPE_U4)
8577 opcode = OP_IMIN_UN;
8578 else if (fsig->params [0]->type == MONO_TYPE_I8)
8580 else if (fsig->params [0]->type == MONO_TYPE_U8)
8581 opcode = OP_LMIN_UN;
8582 } else if (strcmp (cmethod->name, "Max") == 0) {
8583 if (fsig->params [0]->type == MONO_TYPE_I4)
8585 if (fsig->params [0]->type == MONO_TYPE_U4)
8586 opcode = OP_IMAX_UN;
8587 else if (fsig->params [0]->type == MONO_TYPE_I8)
8589 else if (fsig->params [0]->type == MONO_TYPE_U8)
8590 opcode = OP_LMAX_UN;
8594 if (opcode && fsig->param_count == 2) {
8595 MONO_INST_NEW (cfg, ins, opcode);
8596 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8597 ins->dreg = mono_alloc_ireg (cfg);
8598 ins->sreg1 = args [0]->dreg;
8599 ins->sreg2 = args [1]->dreg;
8600 MONO_ADD_INS (cfg->cbb, ins);
8604 /* OP_FREM is not IEEE compatible */
8605 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8606 MONO_INST_NEW (cfg, ins, OP_FREM);
8607 ins->inst_i0 = args [0];
8608 ins->inst_i1 = args [1];
8618 mono_arch_print_tree (MonoInst *tree, int arity)
8624 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8626 return ctx->gregs [reg];
8630 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8632 ctx->gregs [reg] = val;
8636 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8638 gpointer *sp, old_value;
8642 bp = (char *)MONO_CONTEXT_GET_BP (ctx);
8643 sp = (gpointer *)*(gpointer*)(bp + clause->exvar_offset);
8646 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8655 * mono_arch_emit_load_aotconst:
8657 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8658 * TARGET from the mscorlib GOT in full-aot code.
8659 * On AMD64, the result is placed into R11.
8662 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, MonoJumpInfoType tramp_type, gconstpointer target)
8664 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8665 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8671 * mono_arch_get_trampolines:
8673 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8677 mono_arch_get_trampolines (gboolean aot)
8679 return mono_amd64_get_exception_trampolines (aot);
8682 /* Soft Debug support */
8683 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8686 * mono_arch_set_breakpoint:
8688 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8689 * The location should contain code emitted by OP_SEQ_POINT.
8692 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8697 guint32 native_offset = ip - (guint8*)ji->code_start;
8698 SeqPointInfo *info = (SeqPointInfo *)mono_arch_get_seq_point_info (mono_domain_get (), (guint8 *)ji->code_start);
8700 g_assert (info->bp_addrs [native_offset] == 0);
8701 info->bp_addrs [native_offset] = mini_get_breakpoint_trampoline ();
8703 /* ip points to a mov r11, 0 */
8704 g_assert (code [0] == 0x41);
8705 g_assert (code [1] == 0xbb);
8706 amd64_mov_reg_imm (code, AMD64_R11, 1);
8711 * mono_arch_clear_breakpoint:
8713 * Clear the breakpoint at IP.
8716 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8721 guint32 native_offset = ip - (guint8*)ji->code_start;
8722 SeqPointInfo *info = (SeqPointInfo *)mono_arch_get_seq_point_info (mono_domain_get (), (guint8 *)ji->code_start);
8724 info->bp_addrs [native_offset] = NULL;
8726 amd64_mov_reg_imm (code, AMD64_R11, 0);
8731 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8733 /* We use soft breakpoints on amd64 */
8738 * mono_arch_skip_breakpoint:
8740 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8741 * we resume, the instruction is not executed again.
8744 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8746 g_assert_not_reached ();
8750 * mono_arch_start_single_stepping:
8752 * Start single stepping.
8755 mono_arch_start_single_stepping (void)
8757 ss_trampoline = mini_get_single_step_trampoline ();
8761 * mono_arch_stop_single_stepping:
8763 * Stop single stepping.
8766 mono_arch_stop_single_stepping (void)
8768 ss_trampoline = NULL;
8772 * mono_arch_is_single_step_event:
8774 * Return whenever the machine state in SIGCTX corresponds to a single
8778 mono_arch_is_single_step_event (void *info, void *sigctx)
8780 /* We use soft breakpoints on amd64 */
8785 * mono_arch_skip_single_step:
8787 * Modify CTX so the ip is placed after the single step trigger instruction,
8788 * we resume, the instruction is not executed again.
8791 mono_arch_skip_single_step (MonoContext *ctx)
8793 g_assert_not_reached ();
8797 * mono_arch_create_seq_point_info:
8799 * Return a pointer to a data structure which is used by the sequence
8800 * point implementation in AOTed code.
8803 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8808 // FIXME: Add a free function
8810 mono_domain_lock (domain);
8811 info = (SeqPointInfo *)g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8813 mono_domain_unlock (domain);
8816 ji = mono_jit_info_table_find (domain, (char*)code);
8819 // FIXME: Optimize the size
8820 info = (SeqPointInfo *)g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8822 info->ss_tramp_addr = &ss_trampoline;
8824 mono_domain_lock (domain);
8825 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8827 mono_domain_unlock (domain);
8834 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8836 ext->lmf.previous_lmf = prev_lmf;
8837 /* Mark that this is a MonoLMFExt */
8838 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8839 ext->lmf.rsp = (gssize)ext;
8845 mono_arch_opcode_supported (int opcode)
8848 case OP_ATOMIC_ADD_I4:
8849 case OP_ATOMIC_ADD_I8:
8850 case OP_ATOMIC_EXCHANGE_I4:
8851 case OP_ATOMIC_EXCHANGE_I8:
8852 case OP_ATOMIC_CAS_I4:
8853 case OP_ATOMIC_CAS_I8:
8854 case OP_ATOMIC_LOAD_I1:
8855 case OP_ATOMIC_LOAD_I2:
8856 case OP_ATOMIC_LOAD_I4:
8857 case OP_ATOMIC_LOAD_I8:
8858 case OP_ATOMIC_LOAD_U1:
8859 case OP_ATOMIC_LOAD_U2:
8860 case OP_ATOMIC_LOAD_U4:
8861 case OP_ATOMIC_LOAD_U8:
8862 case OP_ATOMIC_LOAD_R4:
8863 case OP_ATOMIC_LOAD_R8:
8864 case OP_ATOMIC_STORE_I1:
8865 case OP_ATOMIC_STORE_I2:
8866 case OP_ATOMIC_STORE_I4:
8867 case OP_ATOMIC_STORE_I8:
8868 case OP_ATOMIC_STORE_U1:
8869 case OP_ATOMIC_STORE_U2:
8870 case OP_ATOMIC_STORE_U4:
8871 case OP_ATOMIC_STORE_U8:
8872 case OP_ATOMIC_STORE_R4:
8873 case OP_ATOMIC_STORE_R8:
8880 #if defined(ENABLE_GSHAREDVT) && defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
8882 #include "../../../mono-extensions/mono/mini/mini-amd64-gsharedvt.c"
8884 #endif /* !ENABLE_GSHAREDVT */