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 */
500 /* Only if storage == ArgValuetypeInReg */
501 ArgStorage pair_storage [2];
503 /* The size of each pair */
513 gboolean need_stack_align;
514 /* The index of the vret arg in the argument list */
521 #define DEBUG(a) if (cfg->verbose_level > 1) a
524 static AMD64_Reg_No param_regs [] = { AMD64_RCX, AMD64_RDX, AMD64_R8, AMD64_R9 };
526 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
528 static AMD64_Reg_No param_regs [] = { AMD64_RDI, AMD64_RSI, AMD64_RDX, AMD64_RCX, AMD64_R8, AMD64_R9 };
530 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
534 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
536 ainfo->offset = *stack_size;
538 if (*gr >= PARAM_REGS) {
539 ainfo->storage = ArgOnStack;
540 /* Since the same stack slot size is used for all arg */
541 /* types, it needs to be big enough to hold them all */
542 (*stack_size) += sizeof(mgreg_t);
545 ainfo->storage = ArgInIReg;
546 ainfo->reg = param_regs [*gr];
552 #define FLOAT_PARAM_REGS 4
554 #define FLOAT_PARAM_REGS 8
558 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
560 ainfo->offset = *stack_size;
562 if (*gr >= FLOAT_PARAM_REGS) {
563 ainfo->storage = ArgOnStack;
564 /* Since the same stack slot size is used for both float */
565 /* types, it needs to be big enough to hold them both */
566 (*stack_size) += sizeof(mgreg_t);
569 /* A double register */
571 ainfo->storage = ArgInDoubleSSEReg;
573 ainfo->storage = ArgInFloatSSEReg;
579 typedef enum ArgumentClass {
587 merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
589 ArgumentClass class2 = ARG_CLASS_NO_CLASS;
592 ptype = mini_get_underlying_type (type);
593 switch (ptype->type) {
602 case MONO_TYPE_STRING:
603 case MONO_TYPE_OBJECT:
604 case MONO_TYPE_CLASS:
605 case MONO_TYPE_SZARRAY:
607 case MONO_TYPE_FNPTR:
608 case MONO_TYPE_ARRAY:
611 class2 = ARG_CLASS_INTEGER;
616 class2 = ARG_CLASS_INTEGER;
618 class2 = ARG_CLASS_SSE;
622 case MONO_TYPE_TYPEDBYREF:
623 g_assert_not_reached ();
625 case MONO_TYPE_GENERICINST:
626 if (!mono_type_generic_inst_is_valuetype (ptype)) {
627 class2 = ARG_CLASS_INTEGER;
631 case MONO_TYPE_VALUETYPE: {
632 MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
635 for (i = 0; i < info->num_fields; ++i) {
637 class2 = merge_argument_class_from_type (info->fields [i].field->type, class2);
642 g_assert_not_reached ();
646 if (class1 == class2)
648 else if (class1 == ARG_CLASS_NO_CLASS)
650 else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
651 class1 = ARG_CLASS_MEMORY;
652 else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
653 class1 = ARG_CLASS_INTEGER;
655 class1 = ARG_CLASS_SSE;
659 #ifdef __native_client_codegen__
661 /* Default alignment for Native Client is 32-byte. */
662 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
664 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
665 /* Check that alignment doesn't cross an alignment boundary. */
667 mono_arch_nacl_pad(guint8 *code, int pad)
669 const int kMaxPadding = 8; /* see amd64-codegen.h:amd64_padding_size() */
671 if (pad == 0) return code;
672 /* assertion: alignment cannot cross a block boundary */
673 g_assert (((uintptr_t)code & (~kNaClAlignmentMask)) ==
674 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
675 while (pad >= kMaxPadding) {
676 amd64_padding (code, kMaxPadding);
679 if (pad != 0) amd64_padding (code, pad);
685 count_fields_nested (MonoClass *klass)
687 MonoMarshalType *info;
690 info = mono_marshal_load_type_info (klass);
693 for (i = 0; i < info->num_fields; ++i) {
694 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type))
695 count += count_fields_nested (mono_class_from_mono_type (info->fields [i].field->type));
703 collect_field_info_nested (MonoClass *klass, MonoMarshalField *fields, int index, int offset)
705 MonoMarshalType *info;
708 info = mono_marshal_load_type_info (klass);
710 for (i = 0; i < info->num_fields; ++i) {
711 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type)) {
712 index = collect_field_info_nested (mono_class_from_mono_type (info->fields [i].field->type), fields, index, info->fields [i].offset);
714 memcpy (&fields [index], &info->fields [i], sizeof (MonoMarshalField));
715 fields [index].offset += offset;
724 add_valuetype_win64 (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
726 guint32 *gr, guint32 *fr, guint32 *stack_size)
728 guint32 size, i, nfields;
730 ArgumentClass arg_class;
731 MonoMarshalType *info = NULL;
732 MonoMarshalField *fields = NULL;
734 gboolean pass_on_stack = FALSE;
736 klass = mono_class_from_mono_type (type);
737 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
739 pass_on_stack = TRUE;
741 /* If this struct can't be split up naturally into 8-byte */
742 /* chunks (registers), pass it on the stack. */
743 if (sig->pinvoke && !pass_on_stack) {
747 info = mono_marshal_load_type_info (klass);
751 * Collect field information recursively to be able to
752 * handle nested structures.
754 nfields = count_fields_nested (klass);
755 fields = g_new0 (MonoMarshalField, nfields);
756 collect_field_info_nested (klass, fields, 0, 0);
758 for (i = 0; i < nfields; ++i) {
759 field_size = mono_marshal_type_size (fields [i].field->type,
761 &align, TRUE, klass->unicode);
762 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
763 pass_on_stack = TRUE;
770 /* Allways pass in memory */
771 ainfo->offset = *stack_size;
772 *stack_size += ALIGN_TO (size, 8);
773 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
780 int n = mono_class_value_size (klass, NULL);
785 arg_class = ARG_CLASS_MEMORY;
787 /* Always pass in 1 integer register */
788 arg_class = ARG_CLASS_INTEGER;
793 ainfo->storage = ArgValuetypeInReg;
794 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
798 switch (info->native_size) {
799 case 1: case 2: case 4: case 8:
803 ainfo->storage = ArgValuetypeAddrInIReg;
804 ainfo->offset = *stack_size;
805 *stack_size += ALIGN_TO (info->native_size, 8);
808 ainfo->storage = ArgValuetypeAddrInIReg;
810 if (*gr < PARAM_REGS) {
811 ainfo->pair_storage [0] = ArgInIReg;
812 ainfo->pair_regs [0] = param_regs [*gr];
816 ainfo->pair_storage [0] = ArgOnStack;
817 ainfo->offset = *stack_size;
828 ArgumentClass class1;
831 class1 = ARG_CLASS_MEMORY;
833 class1 = ARG_CLASS_NO_CLASS;
834 for (i = 0; i < nfields; ++i) {
835 size = mono_marshal_type_size (fields [i].field->type,
837 &align, TRUE, klass->unicode);
838 /* How far into this quad this data extends.*/
839 /* (8 is size of quad) */
840 argsize = fields [i].offset + size;
842 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
844 g_assert (class1 != ARG_CLASS_NO_CLASS);
850 /* Allocate registers */
855 while (argsize != 1 && argsize != 2 && argsize != 4 && argsize != 8)
858 ainfo->storage = ArgValuetypeInReg;
859 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
860 ainfo->pair_size [0] = argsize;
861 ainfo->pair_size [1] = 0;
864 case ARG_CLASS_INTEGER:
865 if (*gr >= PARAM_REGS)
866 arg_class = ARG_CLASS_MEMORY;
868 ainfo->pair_storage [0] = ArgInIReg;
870 ainfo->pair_regs [0] = return_regs [*gr];
872 ainfo->pair_regs [0] = param_regs [*gr];
877 if (*fr >= FLOAT_PARAM_REGS)
878 arg_class = ARG_CLASS_MEMORY;
881 ainfo->pair_storage [0] = ArgInFloatSSEReg;
883 ainfo->pair_storage [0] = ArgInDoubleSSEReg;
884 ainfo->pair_regs [0] = *fr;
888 case ARG_CLASS_MEMORY:
891 g_assert_not_reached ();
894 if (arg_class == ARG_CLASS_MEMORY) {
895 /* Revert possible register assignments */
899 ainfo->offset = *stack_size;
900 *stack_size += sizeof (mgreg_t);
901 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
905 #endif /* TARGET_WIN32 */
908 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
910 guint32 *gr, guint32 *fr, guint32 *stack_size)
913 add_valuetype_win64 (sig, ainfo, type, is_return, gr, fr, stack_size);
915 guint32 size, quad, nquads, i, nfields;
916 /* Keep track of the size used in each quad so we can */
917 /* use the right size when copying args/return vars. */
918 guint32 quadsize [2] = {8, 8};
919 ArgumentClass args [2];
920 MonoMarshalType *info = NULL;
921 MonoMarshalField *fields = NULL;
923 gboolean pass_on_stack = FALSE;
925 klass = mono_class_from_mono_type (type);
926 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
927 if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
928 /* We pass and return vtypes of size 8 in a register */
929 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
930 pass_on_stack = TRUE;
933 /* If this struct can't be split up naturally into 8-byte */
934 /* chunks (registers), pass it on the stack. */
935 if (sig->pinvoke && !pass_on_stack) {
939 info = mono_marshal_load_type_info (klass);
943 * Collect field information recursively to be able to
944 * handle nested structures.
946 nfields = count_fields_nested (klass);
947 fields = g_new0 (MonoMarshalField, nfields);
948 collect_field_info_nested (klass, fields, 0, 0);
950 for (i = 0; i < nfields; ++i) {
951 field_size = mono_marshal_type_size (fields [i].field->type,
953 &align, TRUE, klass->unicode);
954 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
955 pass_on_stack = TRUE;
962 ainfo->storage = ArgValuetypeInReg;
963 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
968 /* Allways pass in memory */
969 ainfo->offset = *stack_size;
970 *stack_size += ALIGN_TO (size, 8);
971 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
983 int n = mono_class_value_size (klass, NULL);
985 quadsize [0] = n >= 8 ? 8 : n;
986 quadsize [1] = n >= 8 ? MAX (n - 8, 8) : 0;
988 /* Always pass in 1 or 2 integer registers */
989 args [0] = ARG_CLASS_INTEGER;
990 args [1] = ARG_CLASS_INTEGER;
991 /* Only the simplest cases are supported */
992 if (is_return && nquads != 1) {
993 args [0] = ARG_CLASS_MEMORY;
994 args [1] = ARG_CLASS_MEMORY;
998 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
999 * The X87 and SSEUP stuff is left out since there are no such types in
1005 ainfo->storage = ArgValuetypeInReg;
1006 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
1010 if (info->native_size > 16) {
1011 ainfo->offset = *stack_size;
1012 *stack_size += ALIGN_TO (info->native_size, 8);
1013 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
1019 args [0] = ARG_CLASS_NO_CLASS;
1020 args [1] = ARG_CLASS_NO_CLASS;
1021 for (quad = 0; quad < nquads; ++quad) {
1024 ArgumentClass class1;
1027 class1 = ARG_CLASS_MEMORY;
1029 class1 = ARG_CLASS_NO_CLASS;
1030 for (i = 0; i < nfields; ++i) {
1031 size = mono_marshal_type_size (fields [i].field->type,
1033 &align, TRUE, klass->unicode);
1034 if ((fields [i].offset < 8) && (fields [i].offset + size) > 8) {
1035 /* Unaligned field */
1039 /* Skip fields in other quad */
1040 if ((quad == 0) && (fields [i].offset >= 8))
1042 if ((quad == 1) && (fields [i].offset < 8))
1045 /* How far into this quad this data extends.*/
1046 /* (8 is size of quad) */
1047 quadsize [quad] = fields [i].offset + size - (quad * 8);
1049 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
1051 g_assert (class1 != ARG_CLASS_NO_CLASS);
1052 args [quad] = class1;
1058 /* Post merger cleanup */
1059 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
1060 args [0] = args [1] = ARG_CLASS_MEMORY;
1062 /* Allocate registers */
1067 while (quadsize [0] != 1 && quadsize [0] != 2 && quadsize [0] != 4 && quadsize [0] != 8)
1069 while (quadsize [1] != 1 && quadsize [1] != 2 && quadsize [1] != 4 && quadsize [1] != 8)
1072 ainfo->storage = ArgValuetypeInReg;
1073 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
1074 g_assert (quadsize [0] <= 8);
1075 g_assert (quadsize [1] <= 8);
1076 ainfo->pair_size [0] = quadsize [0];
1077 ainfo->pair_size [1] = quadsize [1];
1078 ainfo->nregs = nquads;
1079 for (quad = 0; quad < nquads; ++quad) {
1080 switch (args [quad]) {
1081 case ARG_CLASS_INTEGER:
1082 if (*gr >= PARAM_REGS)
1083 args [quad] = ARG_CLASS_MEMORY;
1085 ainfo->pair_storage [quad] = ArgInIReg;
1087 ainfo->pair_regs [quad] = return_regs [*gr];
1089 ainfo->pair_regs [quad] = param_regs [*gr];
1094 if (*fr >= FLOAT_PARAM_REGS)
1095 args [quad] = ARG_CLASS_MEMORY;
1097 if (quadsize[quad] <= 4)
1098 ainfo->pair_storage [quad] = ArgInFloatSSEReg;
1099 else ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
1100 ainfo->pair_regs [quad] = *fr;
1104 case ARG_CLASS_MEMORY:
1107 g_assert_not_reached ();
1111 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
1112 /* Revert possible register assignments */
1116 ainfo->offset = *stack_size;
1118 *stack_size += ALIGN_TO (info->native_size, 8);
1120 *stack_size += nquads * sizeof(mgreg_t);
1121 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
1124 #endif /* !TARGET_WIN32 */
1130 * Obtain information about a call according to the calling convention.
1131 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
1132 * Draft Version 0.23" document for more information.
1135 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
1137 guint32 i, gr, fr, pstart;
1139 int n = sig->hasthis + sig->param_count;
1140 guint32 stack_size = 0;
1142 gboolean is_pinvoke = sig->pinvoke;
1145 cinfo = (CallInfo *)mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1147 cinfo = (CallInfo *)g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1155 /* Reserve space where the callee can save the argument registers */
1156 stack_size = 4 * sizeof (mgreg_t);
1160 ret_type = mini_get_underlying_type (sig->ret);
1161 switch (ret_type->type) {
1171 case MONO_TYPE_FNPTR:
1172 case MONO_TYPE_CLASS:
1173 case MONO_TYPE_OBJECT:
1174 case MONO_TYPE_SZARRAY:
1175 case MONO_TYPE_ARRAY:
1176 case MONO_TYPE_STRING:
1177 cinfo->ret.storage = ArgInIReg;
1178 cinfo->ret.reg = AMD64_RAX;
1182 cinfo->ret.storage = ArgInIReg;
1183 cinfo->ret.reg = AMD64_RAX;
1186 cinfo->ret.storage = ArgInFloatSSEReg;
1187 cinfo->ret.reg = AMD64_XMM0;
1190 cinfo->ret.storage = ArgInDoubleSSEReg;
1191 cinfo->ret.reg = AMD64_XMM0;
1193 case MONO_TYPE_GENERICINST:
1194 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1195 cinfo->ret.storage = ArgInIReg;
1196 cinfo->ret.reg = AMD64_RAX;
1199 if (mini_is_gsharedvt_type (ret_type)) {
1200 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1204 case MONO_TYPE_VALUETYPE:
1205 case MONO_TYPE_TYPEDBYREF: {
1206 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
1208 add_valuetype (sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
1209 g_assert (cinfo->ret.storage != ArgInIReg);
1213 case MONO_TYPE_MVAR:
1214 g_assert (mini_is_gsharedvt_type (ret_type));
1215 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1217 case MONO_TYPE_VOID:
1220 g_error ("Can't handle as return value 0x%x", ret_type->type);
1225 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1226 * the first argument, allowing 'this' to be always passed in the first arg reg.
1227 * Also do this if the first argument is a reference type, since virtual calls
1228 * are sometimes made using calli without sig->hasthis set, like in the delegate
1231 if (cinfo->ret.storage == ArgValuetypeAddrInIReg && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
1233 add_general (&gr, &stack_size, cinfo->args + 0);
1235 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
1238 add_general (&gr, &stack_size, &cinfo->ret);
1239 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1240 cinfo->vret_arg_index = 1;
1244 add_general (&gr, &stack_size, cinfo->args + 0);
1246 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
1247 add_general (&gr, &stack_size, &cinfo->ret);
1248 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1252 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
1254 fr = FLOAT_PARAM_REGS;
1256 /* Emit the signature cookie just before the implicit arguments */
1257 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1260 for (i = pstart; i < sig->param_count; ++i) {
1261 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
1265 /* The float param registers and other param registers must be the same index on Windows x64.*/
1272 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1273 /* We allways pass the sig cookie on the stack for simplicity */
1275 * Prevent implicit arguments + the sig cookie from being passed
1279 fr = FLOAT_PARAM_REGS;
1281 /* Emit the signature cookie just before the implicit arguments */
1282 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1285 ptype = mini_get_underlying_type (sig->params [i]);
1286 switch (ptype->type) {
1289 add_general (&gr, &stack_size, ainfo);
1293 add_general (&gr, &stack_size, ainfo);
1297 add_general (&gr, &stack_size, ainfo);
1302 case MONO_TYPE_FNPTR:
1303 case MONO_TYPE_CLASS:
1304 case MONO_TYPE_OBJECT:
1305 case MONO_TYPE_STRING:
1306 case MONO_TYPE_SZARRAY:
1307 case MONO_TYPE_ARRAY:
1308 add_general (&gr, &stack_size, ainfo);
1310 case MONO_TYPE_GENERICINST:
1311 if (!mono_type_generic_inst_is_valuetype (ptype)) {
1312 add_general (&gr, &stack_size, ainfo);
1315 if (mini_is_gsharedvt_variable_type (ptype)) {
1316 /* gsharedvt arguments are passed by ref */
1317 add_general (&gr, &stack_size, ainfo);
1318 if (ainfo->storage == ArgInIReg)
1319 ainfo->storage = ArgGSharedVtInReg;
1321 ainfo->storage = ArgGSharedVtOnStack;
1325 case MONO_TYPE_VALUETYPE:
1326 case MONO_TYPE_TYPEDBYREF:
1327 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
1332 add_general (&gr, &stack_size, ainfo);
1335 add_float (&fr, &stack_size, ainfo, FALSE);
1338 add_float (&fr, &stack_size, ainfo, TRUE);
1341 case MONO_TYPE_MVAR:
1342 /* gsharedvt arguments are passed by ref */
1343 g_assert (mini_is_gsharedvt_type (ptype));
1344 add_general (&gr, &stack_size, ainfo);
1345 if (ainfo->storage == ArgInIReg)
1346 ainfo->storage = ArgGSharedVtInReg;
1348 ainfo->storage = ArgGSharedVtOnStack;
1351 g_assert_not_reached ();
1355 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
1357 fr = FLOAT_PARAM_REGS;
1359 /* Emit the signature cookie just before the implicit arguments */
1360 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1363 cinfo->stack_usage = stack_size;
1364 cinfo->reg_usage = gr;
1365 cinfo->freg_usage = fr;
1370 * mono_arch_get_argument_info:
1371 * @csig: a method signature
1372 * @param_count: the number of parameters to consider
1373 * @arg_info: an array to store the result infos
1375 * Gathers information on parameters such as size, alignment and
1376 * padding. arg_info should be large enought to hold param_count + 1 entries.
1378 * Returns the size of the argument area on the stack.
1381 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
1384 CallInfo *cinfo = get_call_info (NULL, csig);
1385 guint32 args_size = cinfo->stack_usage;
1387 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
1388 if (csig->hasthis) {
1389 arg_info [0].offset = 0;
1392 for (k = 0; k < param_count; k++) {
1393 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
1395 arg_info [k + 1].size = 0;
1404 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
1408 MonoType *callee_ret;
1410 c1 = get_call_info (NULL, caller_sig);
1411 c2 = get_call_info (NULL, callee_sig);
1412 res = c1->stack_usage >= c2->stack_usage;
1413 callee_ret = mini_get_underlying_type (callee_sig->ret);
1414 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
1415 /* An address on the callee's stack is passed as the first argument */
1425 * Initialize the cpu to execute managed code.
1428 mono_arch_cpu_init (void)
1433 /* spec compliance requires running with double precision */
1434 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1435 fpcw &= ~X86_FPCW_PRECC_MASK;
1436 fpcw |= X86_FPCW_PREC_DOUBLE;
1437 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
1438 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1440 /* TODO: This is crashing on Win64 right now.
1441 * _control87 (_PC_53, MCW_PC);
1447 * Initialize architecture specific code.
1450 mono_arch_init (void)
1452 mono_os_mutex_init_recursive (&mini_arch_mutex);
1453 #if defined(__native_client_codegen__)
1454 mono_native_tls_alloc (&nacl_instruction_depth, NULL);
1455 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)0);
1456 mono_native_tls_alloc (&nacl_rex_tag, NULL);
1457 mono_native_tls_alloc (&nacl_legacy_prefix_tag, NULL);
1460 mono_aot_register_jit_icall ("mono_amd64_throw_exception", mono_amd64_throw_exception);
1461 mono_aot_register_jit_icall ("mono_amd64_throw_corlib_exception", mono_amd64_throw_corlib_exception);
1462 mono_aot_register_jit_icall ("mono_amd64_resume_unwind", mono_amd64_resume_unwind);
1463 mono_aot_register_jit_icall ("mono_amd64_get_original_ip", mono_amd64_get_original_ip);
1466 bp_trampoline = mini_get_breakpoint_trampoline ();
1470 * Cleanup architecture specific code.
1473 mono_arch_cleanup (void)
1475 mono_os_mutex_destroy (&mini_arch_mutex);
1476 #if defined(__native_client_codegen__)
1477 mono_native_tls_free (nacl_instruction_depth);
1478 mono_native_tls_free (nacl_rex_tag);
1479 mono_native_tls_free (nacl_legacy_prefix_tag);
1484 * This function returns the optimizations supported on this cpu.
1487 mono_arch_cpu_optimizations (guint32 *exclude_mask)
1493 if (mono_hwcap_x86_has_cmov) {
1494 opts |= MONO_OPT_CMOV;
1496 if (mono_hwcap_x86_has_fcmov)
1497 opts |= MONO_OPT_FCMOV;
1499 *exclude_mask |= MONO_OPT_FCMOV;
1501 *exclude_mask |= MONO_OPT_CMOV;
1508 * This function test for all SSE functions supported.
1510 * Returns a bitmask corresponding to all supported versions.
1514 mono_arch_cpu_enumerate_simd_versions (void)
1516 guint32 sse_opts = 0;
1518 if (mono_hwcap_x86_has_sse1)
1519 sse_opts |= SIMD_VERSION_SSE1;
1521 if (mono_hwcap_x86_has_sse2)
1522 sse_opts |= SIMD_VERSION_SSE2;
1524 if (mono_hwcap_x86_has_sse3)
1525 sse_opts |= SIMD_VERSION_SSE3;
1527 if (mono_hwcap_x86_has_ssse3)
1528 sse_opts |= SIMD_VERSION_SSSE3;
1530 if (mono_hwcap_x86_has_sse41)
1531 sse_opts |= SIMD_VERSION_SSE41;
1533 if (mono_hwcap_x86_has_sse42)
1534 sse_opts |= SIMD_VERSION_SSE42;
1536 if (mono_hwcap_x86_has_sse4a)
1537 sse_opts |= SIMD_VERSION_SSE4a;
1545 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1550 for (i = 0; i < cfg->num_varinfo; i++) {
1551 MonoInst *ins = cfg->varinfo [i];
1552 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1555 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1558 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
1559 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1562 if (mono_is_regsize_var (ins->inst_vtype)) {
1563 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1564 g_assert (i == vmv->idx);
1565 vars = g_list_prepend (vars, vmv);
1569 vars = mono_varlist_sort (cfg, vars, 0);
1575 * mono_arch_compute_omit_fp:
1577 * Determine whenever the frame pointer can be eliminated.
1580 mono_arch_compute_omit_fp (MonoCompile *cfg)
1582 MonoMethodSignature *sig;
1583 MonoMethodHeader *header;
1587 if (cfg->arch.omit_fp_computed)
1590 header = cfg->header;
1592 sig = mono_method_signature (cfg->method);
1594 if (!cfg->arch.cinfo)
1595 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1596 cinfo = (CallInfo *)cfg->arch.cinfo;
1599 * FIXME: Remove some of the restrictions.
1601 cfg->arch.omit_fp = TRUE;
1602 cfg->arch.omit_fp_computed = TRUE;
1604 #ifdef __native_client_codegen__
1605 /* NaCl modules may not change the value of RBP, so it cannot be */
1606 /* used as a normal register, but it can be used as a frame pointer*/
1607 cfg->disable_omit_fp = TRUE;
1608 cfg->arch.omit_fp = FALSE;
1611 if (cfg->disable_omit_fp)
1612 cfg->arch.omit_fp = FALSE;
1614 if (!debug_omit_fp ())
1615 cfg->arch.omit_fp = FALSE;
1617 if (cfg->method->save_lmf)
1618 cfg->arch.omit_fp = FALSE;
1620 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1621 cfg->arch.omit_fp = FALSE;
1622 if (header->num_clauses)
1623 cfg->arch.omit_fp = FALSE;
1624 if (cfg->param_area)
1625 cfg->arch.omit_fp = FALSE;
1626 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1627 cfg->arch.omit_fp = FALSE;
1628 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1629 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1630 cfg->arch.omit_fp = FALSE;
1631 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1632 ArgInfo *ainfo = &cinfo->args [i];
1634 if (ainfo->storage == ArgOnStack) {
1636 * The stack offset can only be determined when the frame
1639 cfg->arch.omit_fp = FALSE;
1644 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1645 MonoInst *ins = cfg->varinfo [i];
1648 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1653 mono_arch_get_global_int_regs (MonoCompile *cfg)
1657 mono_arch_compute_omit_fp (cfg);
1659 if (cfg->arch.omit_fp)
1660 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1662 /* We use the callee saved registers for global allocation */
1663 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1664 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1665 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1666 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1667 #ifndef __native_client_codegen__
1668 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1671 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1672 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1679 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1684 /* All XMM registers */
1685 for (i = 0; i < 16; ++i)
1686 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1692 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1694 static GList *r = NULL;
1699 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1700 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1701 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1702 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1703 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1704 #ifndef __native_client_codegen__
1705 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1708 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1709 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1710 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1711 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1712 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1713 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1714 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1715 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1717 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1724 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1727 static GList *r = NULL;
1732 for (i = 0; i < AMD64_XMM_NREG; ++i)
1733 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1735 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1742 * mono_arch_regalloc_cost:
1744 * Return the cost, in number of memory references, of the action of
1745 * allocating the variable VMV into a register during global register
1749 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1751 MonoInst *ins = cfg->varinfo [vmv->idx];
1753 if (cfg->method->save_lmf)
1754 /* The register is already saved */
1755 /* substract 1 for the invisible store in the prolog */
1756 return (ins->opcode == OP_ARG) ? 0 : 1;
1759 return (ins->opcode == OP_ARG) ? 1 : 2;
1763 * mono_arch_fill_argument_info:
1765 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1769 mono_arch_fill_argument_info (MonoCompile *cfg)
1772 MonoMethodSignature *sig;
1777 sig = mono_method_signature (cfg->method);
1779 cinfo = (CallInfo *)cfg->arch.cinfo;
1780 sig_ret = mini_get_underlying_type (sig->ret);
1783 * Contrary to mono_arch_allocate_vars (), the information should describe
1784 * where the arguments are at the beginning of the method, not where they can be
1785 * accessed during the execution of the method. The later makes no sense for the
1786 * global register allocator, since a variable can be in more than one location.
1788 switch (cinfo->ret.storage) {
1790 case ArgInFloatSSEReg:
1791 case ArgInDoubleSSEReg:
1792 cfg->ret->opcode = OP_REGVAR;
1793 cfg->ret->inst_c0 = cinfo->ret.reg;
1795 case ArgValuetypeInReg:
1796 cfg->ret->opcode = OP_REGOFFSET;
1797 cfg->ret->inst_basereg = -1;
1798 cfg->ret->inst_offset = -1;
1803 g_assert_not_reached ();
1806 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1807 ArgInfo *ainfo = &cinfo->args [i];
1809 ins = cfg->args [i];
1811 switch (ainfo->storage) {
1813 case ArgInFloatSSEReg:
1814 case ArgInDoubleSSEReg:
1815 ins->opcode = OP_REGVAR;
1816 ins->inst_c0 = ainfo->reg;
1819 ins->opcode = OP_REGOFFSET;
1820 ins->inst_basereg = -1;
1821 ins->inst_offset = -1;
1823 case ArgValuetypeInReg:
1825 ins->opcode = OP_NOP;
1828 g_assert_not_reached ();
1834 mono_arch_allocate_vars (MonoCompile *cfg)
1837 MonoMethodSignature *sig;
1840 guint32 locals_stack_size, locals_stack_align;
1844 sig = mono_method_signature (cfg->method);
1846 cinfo = (CallInfo *)cfg->arch.cinfo;
1847 sig_ret = mini_get_underlying_type (sig->ret);
1849 mono_arch_compute_omit_fp (cfg);
1852 * We use the ABI calling conventions for managed code as well.
1853 * Exception: valuetypes are only sometimes passed or returned in registers.
1857 * The stack looks like this:
1858 * <incoming arguments passed on the stack>
1860 * <lmf/caller saved registers>
1863 * <localloc area> -> grows dynamically
1867 if (cfg->arch.omit_fp) {
1868 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1869 cfg->frame_reg = AMD64_RSP;
1872 /* Locals are allocated backwards from %fp */
1873 cfg->frame_reg = AMD64_RBP;
1877 cfg->arch.saved_iregs = cfg->used_int_regs;
1878 if (cfg->method->save_lmf)
1879 /* Save all callee-saved registers normally, and restore them when unwinding through an LMF */
1880 cfg->arch.saved_iregs |= (1 << AMD64_RBX) | (1 << AMD64_R12) | (1 << AMD64_R13) | (1 << AMD64_R14) | (1 << AMD64_R15);
1882 if (cfg->arch.omit_fp)
1883 cfg->arch.reg_save_area_offset = offset;
1884 /* Reserve space for callee saved registers */
1885 for (i = 0; i < AMD64_NREG; ++i)
1886 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
1887 offset += sizeof(mgreg_t);
1889 if (!cfg->arch.omit_fp)
1890 cfg->arch.reg_save_area_offset = -offset;
1892 if (sig_ret->type != MONO_TYPE_VOID) {
1893 switch (cinfo->ret.storage) {
1895 case ArgInFloatSSEReg:
1896 case ArgInDoubleSSEReg:
1897 cfg->ret->opcode = OP_REGVAR;
1898 cfg->ret->inst_c0 = cinfo->ret.reg;
1900 case ArgValuetypeAddrInIReg:
1901 /* The register is volatile */
1902 cfg->vret_addr->opcode = OP_REGOFFSET;
1903 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1904 if (cfg->arch.omit_fp) {
1905 cfg->vret_addr->inst_offset = offset;
1909 cfg->vret_addr->inst_offset = -offset;
1911 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1912 printf ("vret_addr =");
1913 mono_print_ins (cfg->vret_addr);
1916 case ArgValuetypeInReg:
1917 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1918 cfg->ret->opcode = OP_REGOFFSET;
1919 cfg->ret->inst_basereg = cfg->frame_reg;
1920 if (cfg->arch.omit_fp) {
1921 cfg->ret->inst_offset = offset;
1922 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1924 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1925 cfg->ret->inst_offset = - offset;
1929 g_assert_not_reached ();
1931 cfg->ret->dreg = cfg->ret->inst_c0;
1934 /* Allocate locals */
1935 offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1936 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1937 char *mname = mono_method_full_name (cfg->method, TRUE);
1938 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1939 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1944 if (locals_stack_align) {
1945 offset += (locals_stack_align - 1);
1946 offset &= ~(locals_stack_align - 1);
1948 if (cfg->arch.omit_fp) {
1949 cfg->locals_min_stack_offset = offset;
1950 cfg->locals_max_stack_offset = offset + locals_stack_size;
1952 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1953 cfg->locals_max_stack_offset = - offset;
1956 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1957 if (offsets [i] != -1) {
1958 MonoInst *ins = cfg->varinfo [i];
1959 ins->opcode = OP_REGOFFSET;
1960 ins->inst_basereg = cfg->frame_reg;
1961 if (cfg->arch.omit_fp)
1962 ins->inst_offset = (offset + offsets [i]);
1964 ins->inst_offset = - (offset + offsets [i]);
1965 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1968 offset += locals_stack_size;
1970 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1971 g_assert (!cfg->arch.omit_fp);
1972 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1973 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1976 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1977 ins = cfg->args [i];
1978 if (ins->opcode != OP_REGVAR) {
1979 ArgInfo *ainfo = &cinfo->args [i];
1980 gboolean inreg = TRUE;
1982 /* FIXME: Allocate volatile arguments to registers */
1983 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1987 * Under AMD64, all registers used to pass arguments to functions
1988 * are volatile across calls.
1989 * FIXME: Optimize this.
1991 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg) || (ainfo->storage == ArgGSharedVtInReg))
1994 ins->opcode = OP_REGOFFSET;
1996 switch (ainfo->storage) {
1998 case ArgInFloatSSEReg:
1999 case ArgInDoubleSSEReg:
2000 case ArgGSharedVtInReg:
2002 ins->opcode = OP_REGVAR;
2003 ins->dreg = ainfo->reg;
2007 case ArgGSharedVtOnStack:
2008 g_assert (!cfg->arch.omit_fp);
2009 ins->opcode = OP_REGOFFSET;
2010 ins->inst_basereg = cfg->frame_reg;
2011 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
2013 case ArgValuetypeInReg:
2015 case ArgValuetypeAddrInIReg: {
2017 g_assert (!cfg->arch.omit_fp);
2019 MONO_INST_NEW (cfg, indir, 0);
2020 indir->opcode = OP_REGOFFSET;
2021 if (ainfo->pair_storage [0] == ArgInIReg) {
2022 indir->inst_basereg = cfg->frame_reg;
2023 offset = ALIGN_TO (offset, sizeof (gpointer));
2024 offset += (sizeof (gpointer));
2025 indir->inst_offset = - offset;
2028 indir->inst_basereg = cfg->frame_reg;
2029 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
2032 ins->opcode = OP_VTARG_ADDR;
2033 ins->inst_left = indir;
2041 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg)) {
2042 ins->opcode = OP_REGOFFSET;
2043 ins->inst_basereg = cfg->frame_reg;
2044 /* These arguments are saved to the stack in the prolog */
2045 offset = ALIGN_TO (offset, sizeof(mgreg_t));
2046 if (cfg->arch.omit_fp) {
2047 ins->inst_offset = offset;
2048 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
2049 // Arguments are yet supported by the stack map creation code
2050 //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
2052 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
2053 ins->inst_offset = - offset;
2054 //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
2060 cfg->stack_offset = offset;
2064 mono_arch_create_vars (MonoCompile *cfg)
2066 MonoMethodSignature *sig;
2070 sig = mono_method_signature (cfg->method);
2072 if (!cfg->arch.cinfo)
2073 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
2074 cinfo = (CallInfo *)cfg->arch.cinfo;
2076 if (cinfo->ret.storage == ArgValuetypeInReg)
2077 cfg->ret_var_is_local = TRUE;
2079 sig_ret = mini_get_underlying_type (sig->ret);
2080 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
2081 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2082 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2083 printf ("vret_addr = ");
2084 mono_print_ins (cfg->vret_addr);
2088 if (cfg->gen_sdb_seq_points) {
2091 if (cfg->compile_aot) {
2092 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2093 ins->flags |= MONO_INST_VOLATILE;
2094 cfg->arch.seq_point_info_var = ins;
2096 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2097 ins->flags |= MONO_INST_VOLATILE;
2098 cfg->arch.ss_tramp_var = ins;
2100 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2101 ins->flags |= MONO_INST_VOLATILE;
2102 cfg->arch.bp_tramp_var = ins;
2105 if (cfg->method->save_lmf)
2106 cfg->create_lmf_var = TRUE;
2108 if (cfg->method->save_lmf) {
2110 #if !defined(TARGET_WIN32)
2111 if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
2112 cfg->lmf_ir_mono_lmf = TRUE;
2118 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2124 MONO_INST_NEW (cfg, ins, OP_MOVE);
2125 ins->dreg = mono_alloc_ireg_copy (cfg, tree->dreg);
2126 ins->sreg1 = tree->dreg;
2127 MONO_ADD_INS (cfg->cbb, ins);
2128 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2130 case ArgInFloatSSEReg:
2131 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
2132 ins->dreg = mono_alloc_freg (cfg);
2133 ins->sreg1 = tree->dreg;
2134 MONO_ADD_INS (cfg->cbb, ins);
2136 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2138 case ArgInDoubleSSEReg:
2139 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2140 ins->dreg = mono_alloc_freg (cfg);
2141 ins->sreg1 = tree->dreg;
2142 MONO_ADD_INS (cfg->cbb, ins);
2144 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2148 g_assert_not_reached ();
2153 arg_storage_to_load_membase (ArgStorage storage)
2157 #if defined(__mono_ilp32__)
2158 return OP_LOADI8_MEMBASE;
2160 return OP_LOAD_MEMBASE;
2162 case ArgInDoubleSSEReg:
2163 return OP_LOADR8_MEMBASE;
2164 case ArgInFloatSSEReg:
2165 return OP_LOADR4_MEMBASE;
2167 g_assert_not_reached ();
2174 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2176 MonoMethodSignature *tmp_sig;
2179 if (call->tail_call)
2182 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
2185 * mono_ArgIterator_Setup assumes the signature cookie is
2186 * passed first and all the arguments which were before it are
2187 * passed on the stack after the signature. So compensate by
2188 * passing a different signature.
2190 tmp_sig = mono_metadata_signature_dup_full (cfg->method->klass->image, call->signature);
2191 tmp_sig->param_count -= call->signature->sentinelpos;
2192 tmp_sig->sentinelpos = 0;
2193 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2195 sig_reg = mono_alloc_ireg (cfg);
2196 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2198 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, cinfo->sig_cookie.offset, sig_reg);
2202 static inline LLVMArgStorage
2203 arg_storage_to_llvm_arg_storage (MonoCompile *cfg, ArgStorage storage)
2207 return LLVMArgInIReg;
2210 case ArgGSharedVtInReg:
2211 case ArgGSharedVtOnStack:
2212 return LLVMArgGSharedVt;
2214 g_assert_not_reached ();
2220 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2226 LLVMCallInfo *linfo;
2227 MonoType *t, *sig_ret;
2229 n = sig->param_count + sig->hasthis;
2230 sig_ret = mini_get_underlying_type (sig->ret);
2232 cinfo = get_call_info (cfg->mempool, sig);
2234 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2237 * LLVM always uses the native ABI while we use our own ABI, the
2238 * only difference is the handling of vtypes:
2239 * - we only pass/receive them in registers in some cases, and only
2240 * in 1 or 2 integer registers.
2242 switch (cinfo->ret.storage) {
2244 linfo->ret.storage = LLVMArgNone;
2247 case ArgInFloatSSEReg:
2248 case ArgInDoubleSSEReg:
2249 linfo->ret.storage = LLVMArgNormal;
2251 case ArgValuetypeInReg: {
2252 ainfo = &cinfo->ret;
2255 (ainfo->pair_storage [0] == ArgInFloatSSEReg || ainfo->pair_storage [0] == ArgInDoubleSSEReg ||
2256 ainfo->pair_storage [1] == ArgInFloatSSEReg || ainfo->pair_storage [1] == ArgInDoubleSSEReg)) {
2257 cfg->exception_message = g_strdup ("pinvoke + vtype ret");
2258 cfg->disable_llvm = TRUE;
2262 linfo->ret.storage = LLVMArgVtypeInReg;
2263 for (j = 0; j < 2; ++j)
2264 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2267 case ArgValuetypeAddrInIReg:
2268 /* Vtype returned using a hidden argument */
2269 linfo->ret.storage = LLVMArgVtypeRetAddr;
2270 linfo->vret_arg_index = cinfo->vret_arg_index;
2273 g_assert_not_reached ();
2277 for (i = 0; i < n; ++i) {
2278 ainfo = cinfo->args + i;
2280 if (i >= sig->hasthis)
2281 t = sig->params [i - sig->hasthis];
2283 t = &mono_defaults.int_class->byval_arg;
2285 linfo->args [i].storage = LLVMArgNone;
2287 switch (ainfo->storage) {
2289 linfo->args [i].storage = LLVMArgNormal;
2291 case ArgInDoubleSSEReg:
2292 case ArgInFloatSSEReg:
2293 linfo->args [i].storage = LLVMArgNormal;
2296 if (MONO_TYPE_ISSTRUCT (t))
2297 linfo->args [i].storage = LLVMArgVtypeByVal;
2299 linfo->args [i].storage = LLVMArgNormal;
2301 case ArgValuetypeInReg:
2303 (ainfo->pair_storage [0] == ArgInFloatSSEReg || ainfo->pair_storage [0] == ArgInDoubleSSEReg ||
2304 ainfo->pair_storage [1] == ArgInFloatSSEReg || ainfo->pair_storage [1] == ArgInDoubleSSEReg)) {
2305 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2306 cfg->disable_llvm = TRUE;
2310 linfo->args [i].storage = LLVMArgVtypeInReg;
2311 for (j = 0; j < 2; ++j)
2312 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2314 case ArgGSharedVtInReg:
2315 case ArgGSharedVtOnStack:
2316 linfo->args [i].storage = LLVMArgGSharedVt;
2319 cfg->exception_message = g_strdup ("ainfo->storage");
2320 cfg->disable_llvm = TRUE;
2330 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2333 MonoMethodSignature *sig;
2339 sig = call->signature;
2340 n = sig->param_count + sig->hasthis;
2342 cinfo = get_call_info (cfg->mempool, sig);
2346 if (COMPILE_LLVM (cfg)) {
2347 /* We shouldn't be called in the llvm case */
2348 cfg->disable_llvm = TRUE;
2353 * Emit all arguments which are passed on the stack to prevent register
2354 * allocation problems.
2356 for (i = 0; i < n; ++i) {
2358 ainfo = cinfo->args + i;
2360 in = call->args [i];
2362 if (sig->hasthis && i == 0)
2363 t = &mono_defaults.object_class->byval_arg;
2365 t = sig->params [i - sig->hasthis];
2367 t = mini_get_underlying_type (t);
2368 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call) {
2370 if (t->type == MONO_TYPE_R4)
2371 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2372 else if (t->type == MONO_TYPE_R8)
2373 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2375 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2377 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2379 if (cfg->compute_gc_maps) {
2382 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2388 * Emit all parameters passed in registers in non-reverse order for better readability
2389 * and to help the optimization in emit_prolog ().
2391 for (i = 0; i < n; ++i) {
2392 ainfo = cinfo->args + i;
2394 in = call->args [i];
2396 if (ainfo->storage == ArgInIReg)
2397 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2400 for (i = n - 1; i >= 0; --i) {
2403 ainfo = cinfo->args + i;
2405 in = call->args [i];
2407 if (sig->hasthis && i == 0)
2408 t = &mono_defaults.object_class->byval_arg;
2410 t = sig->params [i - sig->hasthis];
2411 t = mini_get_underlying_type (t);
2413 switch (ainfo->storage) {
2417 case ArgInFloatSSEReg:
2418 case ArgInDoubleSSEReg:
2419 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2422 case ArgValuetypeInReg:
2423 case ArgValuetypeAddrInIReg:
2424 case ArgGSharedVtInReg:
2425 case ArgGSharedVtOnStack: {
2426 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call)
2427 /* Already emitted above */
2429 if (ainfo->storage == ArgOnStack && call->tail_call) {
2430 MonoInst *call_inst = (MonoInst*)call;
2431 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2432 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2440 size = mono_type_native_stack_size (t, &align);
2443 * Other backends use mono_type_stack_size (), but that
2444 * aligns the size to 8, which is larger than the size of
2445 * the source, leading to reads of invalid memory if the
2446 * source is at the end of address space.
2448 size = mono_class_value_size (mono_class_from_mono_type (t), &align);
2451 if (size >= 10000) {
2452 /* Avoid asserts in emit_memcpy () */
2453 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
2454 cfg->exception_message = g_strdup_printf ("Passing an argument of size '%d'.", size);
2455 /* Continue normally */
2459 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2460 arg->sreg1 = in->dreg;
2461 arg->klass = mono_class_from_mono_type (t);
2462 arg->backend.size = size;
2463 arg->inst_p0 = call;
2464 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2465 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2467 MONO_ADD_INS (cfg->cbb, arg);
2472 g_assert_not_reached ();
2475 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
2476 /* Emit the signature cookie just before the implicit arguments */
2477 emit_sig_cookie (cfg, call, cinfo);
2480 /* Handle the case where there are no implicit arguments */
2481 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2482 emit_sig_cookie (cfg, call, cinfo);
2484 switch (cinfo->ret.storage) {
2485 case ArgValuetypeInReg:
2486 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2488 * Tell the JIT to use a more efficient calling convention: call using
2489 * OP_CALL, compute the result location after the call, and save the
2492 call->vret_in_reg = TRUE;
2494 * Nullify the instruction computing the vret addr to enable
2495 * future optimizations.
2498 NULLIFY_INS (call->vret_var);
2500 if (call->tail_call)
2503 * The valuetype is in RAX:RDX after the call, need to be copied to
2504 * the stack. Push the address here, so the call instruction can
2507 if (!cfg->arch.vret_addr_loc) {
2508 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2509 /* Prevent it from being register allocated or optimized away */
2510 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2513 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2516 case ArgValuetypeAddrInIReg: {
2518 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2519 vtarg->sreg1 = call->vret_var->dreg;
2520 vtarg->dreg = mono_alloc_preg (cfg);
2521 MONO_ADD_INS (cfg->cbb, vtarg);
2523 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2530 if (cfg->method->save_lmf) {
2531 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2532 MONO_ADD_INS (cfg->cbb, arg);
2535 call->stack_usage = cinfo->stack_usage;
2539 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2542 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2543 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2544 int size = ins->backend.size;
2546 switch (ainfo->storage) {
2547 case ArgValuetypeInReg: {
2551 for (part = 0; part < 2; ++part) {
2552 if (ainfo->pair_storage [part] == ArgNone)
2555 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2556 load->inst_basereg = src->dreg;
2557 load->inst_offset = part * sizeof(mgreg_t);
2559 switch (ainfo->pair_storage [part]) {
2561 load->dreg = mono_alloc_ireg (cfg);
2563 case ArgInDoubleSSEReg:
2564 case ArgInFloatSSEReg:
2565 load->dreg = mono_alloc_freg (cfg);
2568 g_assert_not_reached ();
2570 MONO_ADD_INS (cfg->cbb, load);
2572 add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2576 case ArgValuetypeAddrInIReg: {
2577 MonoInst *vtaddr, *load;
2578 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2580 MONO_INST_NEW (cfg, load, OP_LDADDR);
2581 cfg->has_indirection = TRUE;
2582 load->inst_p0 = vtaddr;
2583 vtaddr->flags |= MONO_INST_INDIRECT;
2584 load->type = STACK_MP;
2585 load->klass = vtaddr->klass;
2586 load->dreg = mono_alloc_ireg (cfg);
2587 MONO_ADD_INS (cfg->cbb, load);
2588 mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2590 if (ainfo->pair_storage [0] == ArgInIReg) {
2591 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2592 arg->dreg = mono_alloc_ireg (cfg);
2593 arg->sreg1 = load->dreg;
2595 MONO_ADD_INS (cfg->cbb, arg);
2596 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2598 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, load->dreg);
2602 case ArgGSharedVtInReg:
2604 mono_call_inst_add_outarg_reg (cfg, call, src->dreg, ainfo->reg, FALSE);
2606 case ArgGSharedVtOnStack:
2607 g_assert_not_reached ();
2611 int dreg = mono_alloc_ireg (cfg);
2613 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
2614 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, dreg);
2615 } else if (size <= 40) {
2616 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2618 // FIXME: Code growth
2619 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2622 if (cfg->compute_gc_maps) {
2624 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2630 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2632 MonoType *ret = mini_get_underlying_type (mono_method_signature (method)->ret);
2634 if (ret->type == MONO_TYPE_R4) {
2635 if (COMPILE_LLVM (cfg))
2636 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2638 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2640 } else if (ret->type == MONO_TYPE_R8) {
2641 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2645 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2648 #endif /* DISABLE_JIT */
2650 #define EMIT_COND_BRANCH(ins,cond,sign) \
2651 if (ins->inst_true_bb->native_offset) { \
2652 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2654 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2655 if ((cfg->opt & MONO_OPT_BRANCH) && \
2656 x86_is_imm8 (ins->inst_true_bb->max_offset - offset)) \
2657 x86_branch8 (code, cond, 0, sign); \
2659 x86_branch32 (code, cond, 0, sign); \
2663 MonoMethodSignature *sig;
2668 dyn_call_supported (MonoMethodSignature *sig, CallInfo *cinfo)
2676 switch (cinfo->ret.storage) {
2680 case ArgValuetypeInReg: {
2681 ArgInfo *ainfo = &cinfo->ret;
2683 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2685 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2693 for (i = 0; i < cinfo->nargs; ++i) {
2694 ArgInfo *ainfo = &cinfo->args [i];
2695 switch (ainfo->storage) {
2698 case ArgValuetypeInReg:
2699 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2701 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2713 * mono_arch_dyn_call_prepare:
2715 * Return a pointer to an arch-specific structure which contains information
2716 * needed by mono_arch_get_dyn_call_args (). Return NULL if OP_DYN_CALL is not
2717 * supported for SIG.
2718 * This function is equivalent to ffi_prep_cif in libffi.
2721 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2723 ArchDynCallInfo *info;
2726 cinfo = get_call_info (NULL, sig);
2728 if (!dyn_call_supported (sig, cinfo)) {
2733 info = g_new0 (ArchDynCallInfo, 1);
2734 // FIXME: Preprocess the info to speed up get_dyn_call_args ().
2736 info->cinfo = cinfo;
2738 return (MonoDynCallInfo*)info;
2742 * mono_arch_dyn_call_free:
2744 * Free a MonoDynCallInfo structure.
2747 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2749 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2751 g_free (ainfo->cinfo);
2755 #if !defined(__native_client__)
2756 #define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
2757 #define GREG_TO_PTR(greg) (gpointer)(greg)
2759 /* Correctly handle casts to/from 32-bit pointers without compiler warnings */
2760 #define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
2761 #define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
2765 * mono_arch_get_start_dyn_call:
2767 * Convert the arguments ARGS to a format which can be passed to OP_DYN_CALL, and
2768 * store the result into BUF.
2769 * ARGS should be an array of pointers pointing to the arguments.
2770 * RET should point to a memory buffer large enought to hold the result of the
2772 * This function should be as fast as possible, any work which does not depend
2773 * on the actual values of the arguments should be done in
2774 * mono_arch_dyn_call_prepare ().
2775 * start_dyn_call + OP_DYN_CALL + finish_dyn_call is equivalent to ffi_call in
2779 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2781 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2782 DynCallArgs *p = (DynCallArgs*)buf;
2783 int arg_index, greg, i, pindex;
2784 MonoMethodSignature *sig = dinfo->sig;
2785 int buffer_offset = 0;
2787 g_assert (buf_len >= sizeof (DynCallArgs));
2796 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2797 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2802 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg)
2803 p->regs [greg ++] = PTR_TO_GREG(ret);
2805 for (i = pindex; i < sig->param_count; i++) {
2806 MonoType *t = mini_get_underlying_type (sig->params [i]);
2807 gpointer *arg = args [arg_index ++];
2810 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2815 case MONO_TYPE_STRING:
2816 case MONO_TYPE_CLASS:
2817 case MONO_TYPE_ARRAY:
2818 case MONO_TYPE_SZARRAY:
2819 case MONO_TYPE_OBJECT:
2823 #if !defined(__mono_ilp32__)
2827 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2828 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2830 #if defined(__mono_ilp32__)
2833 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2834 p->regs [greg ++] = *(guint64*)(arg);
2838 p->regs [greg ++] = *(guint8*)(arg);
2841 p->regs [greg ++] = *(gint8*)(arg);
2844 p->regs [greg ++] = *(gint16*)(arg);
2847 p->regs [greg ++] = *(guint16*)(arg);
2850 p->regs [greg ++] = *(gint32*)(arg);
2853 p->regs [greg ++] = *(guint32*)(arg);
2855 case MONO_TYPE_GENERICINST:
2856 if (MONO_TYPE_IS_REFERENCE (t)) {
2857 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2859 } else if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
2860 MonoClass *klass = mono_class_from_mono_type (t);
2861 guint8 *nullable_buf;
2864 size = mono_class_value_size (klass, NULL);
2865 nullable_buf = p->buffer + buffer_offset;
2866 buffer_offset += size;
2867 g_assert (buffer_offset <= 256);
2869 /* The argument pointed to by arg is either a boxed vtype or null */
2870 mono_nullable_init (nullable_buf, (MonoObject*)arg, klass);
2872 arg = (gpointer*)nullable_buf;
2878 case MONO_TYPE_VALUETYPE: {
2879 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2881 g_assert (ainfo->storage == ArgValuetypeInReg);
2882 if (ainfo->pair_storage [0] != ArgNone) {
2883 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2884 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2886 if (ainfo->pair_storage [1] != ArgNone) {
2887 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2888 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2893 g_assert_not_reached ();
2897 g_assert (greg <= PARAM_REGS);
2901 * mono_arch_finish_dyn_call:
2903 * Store the result of a dyn call into the return value buffer passed to
2904 * start_dyn_call ().
2905 * This function should be as fast as possible, any work which does not depend
2906 * on the actual values of the arguments should be done in
2907 * mono_arch_dyn_call_prepare ().
2910 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2912 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2913 MonoMethodSignature *sig = dinfo->sig;
2914 guint8 *ret = ((DynCallArgs*)buf)->ret;
2915 mgreg_t res = ((DynCallArgs*)buf)->res;
2916 MonoType *sig_ret = mini_get_underlying_type (sig->ret);
2918 switch (sig_ret->type) {
2919 case MONO_TYPE_VOID:
2920 *(gpointer*)ret = NULL;
2922 case MONO_TYPE_STRING:
2923 case MONO_TYPE_CLASS:
2924 case MONO_TYPE_ARRAY:
2925 case MONO_TYPE_SZARRAY:
2926 case MONO_TYPE_OBJECT:
2930 *(gpointer*)ret = GREG_TO_PTR(res);
2936 *(guint8*)ret = res;
2939 *(gint16*)ret = res;
2942 *(guint16*)ret = res;
2945 *(gint32*)ret = res;
2948 *(guint32*)ret = res;
2951 *(gint64*)ret = res;
2954 *(guint64*)ret = res;
2956 case MONO_TYPE_GENERICINST:
2957 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
2958 *(gpointer*)ret = GREG_TO_PTR(res);
2963 case MONO_TYPE_VALUETYPE:
2964 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg) {
2967 ArgInfo *ainfo = &dinfo->cinfo->ret;
2969 g_assert (ainfo->storage == ArgValuetypeInReg);
2971 if (ainfo->pair_storage [0] != ArgNone) {
2972 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2973 ((mgreg_t*)ret)[0] = res;
2976 g_assert (ainfo->pair_storage [1] == ArgNone);
2980 g_assert_not_reached ();
2984 /* emit an exception if condition is fail */
2985 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2987 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2988 if (tins == NULL) { \
2989 mono_add_patch_info (cfg, code - cfg->native_code, \
2990 MONO_PATCH_INFO_EXC, exc_name); \
2991 x86_branch32 (code, cond, 0, signed); \
2993 EMIT_COND_BRANCH (tins, cond, signed); \
2997 #define EMIT_FPCOMPARE(code) do { \
2998 amd64_fcompp (code); \
2999 amd64_fnstsw (code); \
3002 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
3003 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
3004 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
3005 amd64_ ##op (code); \
3006 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
3007 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
3011 emit_call_body (MonoCompile *cfg, guint8 *code, MonoJumpInfoType patch_type, gconstpointer data)
3013 gboolean no_patch = FALSE;
3016 * FIXME: Add support for thunks
3019 gboolean near_call = FALSE;
3022 * Indirect calls are expensive so try to make a near call if possible.
3023 * The caller memory is allocated by the code manager so it is
3024 * guaranteed to be at a 32 bit offset.
3027 if (patch_type != MONO_PATCH_INFO_ABS) {
3028 /* The target is in memory allocated using the code manager */
3031 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
3032 if (((MonoMethod*)data)->klass->image->aot_module)
3033 /* The callee might be an AOT method */
3035 if (((MonoMethod*)data)->dynamic)
3036 /* The target is in malloc-ed memory */
3040 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
3042 * The call might go directly to a native function without
3045 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ((const char *)data);
3047 gconstpointer target = mono_icall_get_wrapper (mi);
3048 if ((((guint64)target) >> 32) != 0)
3054 MonoJumpInfo *jinfo = NULL;
3056 if (cfg->abs_patches)
3057 jinfo = (MonoJumpInfo *)g_hash_table_lookup (cfg->abs_patches, data);
3059 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3060 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
3061 if (mi && (((guint64)mi->func) >> 32) == 0)
3066 * This is not really an optimization, but required because the
3067 * generic class init trampolines use R11 to pass the vtable.
3072 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
3074 if (info->func == info->wrapper) {
3076 if ((((guint64)info->func) >> 32) == 0)
3080 /* See the comment in mono_codegen () */
3081 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
3085 else if ((((guint64)data) >> 32) == 0) {
3092 if (cfg->method->dynamic)
3093 /* These methods are allocated using malloc */
3096 #ifdef MONO_ARCH_NOMAP32BIT
3099 #if defined(__native_client__)
3100 /* Always use near_call == TRUE for Native Client */
3103 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
3104 if (optimize_for_xen)
3107 if (cfg->compile_aot) {
3114 * Align the call displacement to an address divisible by 4 so it does
3115 * not span cache lines. This is required for code patching to work on SMP
3118 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
3119 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
3120 amd64_padding (code, pad_size);
3122 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3123 amd64_call_code (code, 0);
3126 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3127 amd64_set_reg_template (code, GP_SCRATCH_REG);
3128 amd64_call_reg (code, GP_SCRATCH_REG);
3135 static inline guint8*
3136 emit_call (MonoCompile *cfg, guint8 *code, MonoJumpInfoType patch_type, gconstpointer data, gboolean win64_adjust_stack)
3139 if (win64_adjust_stack)
3140 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
3142 code = emit_call_body (cfg, code, patch_type, data);
3144 if (win64_adjust_stack)
3145 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
3152 store_membase_imm_to_store_membase_reg (int opcode)
3155 case OP_STORE_MEMBASE_IMM:
3156 return OP_STORE_MEMBASE_REG;
3157 case OP_STOREI4_MEMBASE_IMM:
3158 return OP_STOREI4_MEMBASE_REG;
3159 case OP_STOREI8_MEMBASE_IMM:
3160 return OP_STOREI8_MEMBASE_REG;
3168 #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)))
3171 * mono_arch_peephole_pass_1:
3173 * Perform peephole opts which should/can be performed before local regalloc
3176 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3180 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3181 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3183 switch (ins->opcode) {
3187 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
3189 * X86_LEA is like ADD, but doesn't have the
3190 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
3191 * its operand to 64 bit.
3193 ins->opcode = OP_X86_LEA_MEMBASE;
3194 ins->inst_basereg = ins->sreg1;
3199 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3203 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3204 * the latter has length 2-3 instead of 6 (reverse constant
3205 * propagation). These instruction sequences are very common
3206 * in the initlocals bblock.
3208 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3209 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)) {
3210 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3211 ins2->sreg1 = ins->dreg;
3212 } 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)) {
3214 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3217 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3225 case OP_COMPARE_IMM:
3226 case OP_LCOMPARE_IMM:
3227 /* OP_COMPARE_IMM (reg, 0)
3229 * OP_AMD64_TEST_NULL (reg)
3232 ins->opcode = OP_AMD64_TEST_NULL;
3234 case OP_ICOMPARE_IMM:
3236 ins->opcode = OP_X86_TEST_NULL;
3238 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3240 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3241 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3243 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3244 * OP_COMPARE_IMM reg, imm
3246 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3248 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3249 ins->inst_basereg == last_ins->inst_destbasereg &&
3250 ins->inst_offset == last_ins->inst_offset) {
3251 ins->opcode = OP_ICOMPARE_IMM;
3252 ins->sreg1 = last_ins->sreg1;
3254 /* check if we can remove cmp reg,0 with test null */
3256 ins->opcode = OP_X86_TEST_NULL;
3262 mono_peephole_ins (bb, ins);
3267 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3271 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3272 switch (ins->opcode) {
3275 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3276 /* reg = 0 -> XOR (reg, reg) */
3277 /* XOR sets cflags on x86, so we cant do it always */
3278 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3279 ins->opcode = OP_LXOR;
3280 ins->sreg1 = ins->dreg;
3281 ins->sreg2 = ins->dreg;
3289 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3290 * 0 result into 64 bits.
3292 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3293 ins->opcode = OP_IXOR;
3297 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3301 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3302 * the latter has length 2-3 instead of 6 (reverse constant
3303 * propagation). These instruction sequences are very common
3304 * in the initlocals bblock.
3306 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3307 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)) {
3308 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3309 ins2->sreg1 = ins->dreg;
3310 } 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)) {
3312 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3315 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3324 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3325 ins->opcode = OP_X86_INC_REG;
3328 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3329 ins->opcode = OP_X86_DEC_REG;
3333 mono_peephole_ins (bb, ins);
3337 #define NEW_INS(cfg,ins,dest,op) do { \
3338 MONO_INST_NEW ((cfg), (dest), (op)); \
3339 (dest)->cil_code = (ins)->cil_code; \
3340 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3344 * mono_arch_lowering_pass:
3346 * Converts complex opcodes into simpler ones so that each IR instruction
3347 * corresponds to one machine instruction.
3350 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3352 MonoInst *ins, *n, *temp;
3355 * FIXME: Need to add more instructions, but the current machine
3356 * description can't model some parts of the composite instructions like
3359 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3360 switch (ins->opcode) {
3364 case OP_IDIV_UN_IMM:
3365 case OP_IREM_UN_IMM:
3368 mono_decompose_op_imm (cfg, bb, ins);
3370 case OP_COMPARE_IMM:
3371 case OP_LCOMPARE_IMM:
3372 if (!amd64_use_imm32 (ins->inst_imm)) {
3373 NEW_INS (cfg, ins, temp, OP_I8CONST);
3374 temp->inst_c0 = ins->inst_imm;
3375 temp->dreg = mono_alloc_ireg (cfg);
3376 ins->opcode = OP_COMPARE;
3377 ins->sreg2 = temp->dreg;
3380 #ifndef __mono_ilp32__
3381 case OP_LOAD_MEMBASE:
3383 case OP_LOADI8_MEMBASE:
3384 #ifndef __native_client_codegen__
3385 /* Don't generate memindex opcodes (to simplify */
3386 /* read sandboxing) */
3387 if (!amd64_use_imm32 (ins->inst_offset)) {
3388 NEW_INS (cfg, ins, temp, OP_I8CONST);
3389 temp->inst_c0 = ins->inst_offset;
3390 temp->dreg = mono_alloc_ireg (cfg);
3391 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3392 ins->inst_indexreg = temp->dreg;
3396 #ifndef __mono_ilp32__
3397 case OP_STORE_MEMBASE_IMM:
3399 case OP_STOREI8_MEMBASE_IMM:
3400 if (!amd64_use_imm32 (ins->inst_imm)) {
3401 NEW_INS (cfg, ins, temp, OP_I8CONST);
3402 temp->inst_c0 = ins->inst_imm;
3403 temp->dreg = mono_alloc_ireg (cfg);
3404 ins->opcode = OP_STOREI8_MEMBASE_REG;
3405 ins->sreg1 = temp->dreg;
3408 #ifdef MONO_ARCH_SIMD_INTRINSICS
3409 case OP_EXPAND_I1: {
3410 int temp_reg1 = mono_alloc_ireg (cfg);
3411 int temp_reg2 = mono_alloc_ireg (cfg);
3412 int original_reg = ins->sreg1;
3414 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3415 temp->sreg1 = original_reg;
3416 temp->dreg = temp_reg1;
3418 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3419 temp->sreg1 = temp_reg1;
3420 temp->dreg = temp_reg2;
3423 NEW_INS (cfg, ins, temp, OP_LOR);
3424 temp->sreg1 = temp->dreg = temp_reg2;
3425 temp->sreg2 = temp_reg1;
3427 ins->opcode = OP_EXPAND_I2;
3428 ins->sreg1 = temp_reg2;
3437 bb->max_vreg = cfg->next_vreg;
3441 branch_cc_table [] = {
3442 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3443 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3444 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3447 /* Maps CMP_... constants to X86_CC_... constants */
3450 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3451 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3455 cc_signed_table [] = {
3456 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3457 FALSE, FALSE, FALSE, FALSE
3460 /*#include "cprop.c"*/
3462 static unsigned char*
3463 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3466 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3468 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3471 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3473 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3477 static unsigned char*
3478 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3480 int sreg = tree->sreg1;
3481 int need_touch = FALSE;
3483 #if defined(TARGET_WIN32)
3485 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3486 if (!tree->flags & MONO_INST_INIT)
3495 * If requested stack size is larger than one page,
3496 * perform stack-touch operation
3499 * Generate stack probe code.
3500 * Under Windows, it is necessary to allocate one page at a time,
3501 * "touching" stack after each successful sub-allocation. This is
3502 * because of the way stack growth is implemented - there is a
3503 * guard page before the lowest stack page that is currently commited.
3504 * Stack normally grows sequentially so OS traps access to the
3505 * guard page and commits more pages when needed.
3507 amd64_test_reg_imm (code, sreg, ~0xFFF);
3508 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3510 br[2] = code; /* loop */
3511 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3512 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3513 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3514 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3515 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3516 amd64_patch (br[3], br[2]);
3517 amd64_test_reg_reg (code, sreg, sreg);
3518 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3519 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3521 br[1] = code; x86_jump8 (code, 0);
3523 amd64_patch (br[0], code);
3524 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3525 amd64_patch (br[1], code);
3526 amd64_patch (br[4], code);
3529 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3531 if (tree->flags & MONO_INST_INIT) {
3533 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3534 amd64_push_reg (code, AMD64_RAX);
3537 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3538 amd64_push_reg (code, AMD64_RCX);
3541 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3542 amd64_push_reg (code, AMD64_RDI);
3546 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3547 if (sreg != AMD64_RCX)
3548 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3549 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3551 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3552 if (cfg->param_area)
3553 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3555 #if defined(__default_codegen__)
3556 amd64_prefix (code, X86_REP_PREFIX);
3558 #elif defined(__native_client_codegen__)
3559 /* NaCl stos pseudo-instruction */
3560 amd64_codegen_pre(code);
3561 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3562 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3563 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3564 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3565 amd64_prefix (code, X86_REP_PREFIX);
3567 amd64_codegen_post(code);
3568 #endif /* __native_client_codegen__ */
3570 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3571 amd64_pop_reg (code, AMD64_RDI);
3572 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3573 amd64_pop_reg (code, AMD64_RCX);
3574 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3575 amd64_pop_reg (code, AMD64_RAX);
3581 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3586 /* Move return value to the target register */
3587 /* FIXME: do this in the local reg allocator */
3588 switch (ins->opcode) {
3591 case OP_CALL_MEMBASE:
3594 case OP_LCALL_MEMBASE:
3595 g_assert (ins->dreg == AMD64_RAX);
3599 case OP_FCALL_MEMBASE: {
3600 MonoType *rtype = mini_get_underlying_type (((MonoCallInst*)ins)->signature->ret);
3601 if (rtype->type == MONO_TYPE_R4) {
3602 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3605 if (ins->dreg != AMD64_XMM0)
3606 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3612 case OP_RCALL_MEMBASE:
3613 if (ins->dreg != AMD64_XMM0)
3614 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3618 case OP_VCALL_MEMBASE:
3621 case OP_VCALL2_MEMBASE:
3622 cinfo = get_call_info (cfg->mempool, ((MonoCallInst*)ins)->signature);
3623 if (cinfo->ret.storage == ArgValuetypeInReg) {
3624 MonoInst *loc = (MonoInst *)cfg->arch.vret_addr_loc;
3626 /* Load the destination address */
3627 g_assert (loc->opcode == OP_REGOFFSET);
3628 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3630 for (quad = 0; quad < 2; quad ++) {
3631 switch (cinfo->ret.pair_storage [quad]) {
3633 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3635 case ArgInFloatSSEReg:
3636 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3638 case ArgInDoubleSSEReg:
3639 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3654 #endif /* DISABLE_JIT */
3657 static int tls_gs_offset;
3661 mono_amd64_have_tls_get (void)
3664 static gboolean have_tls_get = FALSE;
3665 static gboolean inited = FALSE;
3668 return have_tls_get;
3670 #if MONO_HAVE_FAST_TLS
3671 guint8 *ins = (guint8*)pthread_getspecific;
3674 * We're looking for these two instructions:
3676 * mov %gs:[offset](,%rdi,8),%rax
3679 have_tls_get = ins [0] == 0x65 &&
3689 tls_gs_offset = ins[5];
3692 * Apple now loads a different version of pthread_getspecific when launched from Xcode
3693 * For that version we're looking for these instructions:
3697 * mov %gs:[offset](,%rdi,8),%rax
3701 if (!have_tls_get) {
3702 have_tls_get = ins [0] == 0x55 &&
3717 tls_gs_offset = ins[9];
3723 return have_tls_get;
3724 #elif defined(TARGET_ANDROID)
3732 mono_amd64_get_tls_gs_offset (void)
3735 return tls_gs_offset;
3737 g_assert_not_reached ();
3743 * mono_amd64_emit_tls_get:
3744 * @code: buffer to store code to
3745 * @dreg: hard register where to place the result
3746 * @tls_offset: offset info
3748 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3749 * the dreg register the item in the thread local storage identified
3752 * Returns: a pointer to the end of the stored code
3755 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3758 if (tls_offset < 64) {
3759 x86_prefix (code, X86_GS_PREFIX);
3760 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3764 g_assert (tls_offset < 0x440);
3765 /* Load TEB->TlsExpansionSlots */
3766 x86_prefix (code, X86_GS_PREFIX);
3767 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3768 amd64_test_reg_reg (code, dreg, dreg);
3770 amd64_branch (code, X86_CC_EQ, code, TRUE);
3771 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3772 amd64_patch (buf [0], code);
3774 #elif defined(__APPLE__)
3775 x86_prefix (code, X86_GS_PREFIX);
3776 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3778 if (optimize_for_xen) {
3779 x86_prefix (code, X86_FS_PREFIX);
3780 amd64_mov_reg_mem (code, dreg, 0, 8);
3781 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3783 x86_prefix (code, X86_FS_PREFIX);
3784 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3791 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3793 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3795 if (dreg != offset_reg)
3796 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3797 amd64_prefix (code, X86_GS_PREFIX);
3798 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3799 #elif defined(__linux__)
3802 if (dreg == offset_reg) {
3803 /* Use a temporary reg by saving it to the redzone */
3804 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3805 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3806 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3807 offset_reg = tmpreg;
3809 x86_prefix (code, X86_FS_PREFIX);
3810 amd64_mov_reg_mem (code, dreg, 0, 8);
3811 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3813 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3815 g_assert_not_reached ();
3821 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3824 g_assert_not_reached ();
3825 #elif defined(__APPLE__)
3826 x86_prefix (code, X86_GS_PREFIX);
3827 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3829 g_assert (!optimize_for_xen);
3830 x86_prefix (code, X86_FS_PREFIX);
3831 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3837 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3839 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3841 g_assert_not_reached ();
3842 #elif defined(__APPLE__)
3843 x86_prefix (code, X86_GS_PREFIX);
3844 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3846 x86_prefix (code, X86_FS_PREFIX);
3847 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3853 * mono_arch_translate_tls_offset:
3855 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3858 mono_arch_translate_tls_offset (int offset)
3861 return tls_gs_offset + (offset * 8);
3870 * Emit code to initialize an LMF structure at LMF_OFFSET.
3873 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3876 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3879 * sp is saved right before calls but we need to save it here too so
3880 * async stack walks would work.
3882 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3884 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3885 if (cfg->arch.omit_fp && cfa_offset != -1)
3886 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3888 /* These can't contain refs */
3889 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3890 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3891 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3892 /* These are handled automatically by the stack marking code */
3893 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3898 #define REAL_PRINT_REG(text,reg) \
3899 mono_assert (reg >= 0); \
3900 amd64_push_reg (code, AMD64_RAX); \
3901 amd64_push_reg (code, AMD64_RDX); \
3902 amd64_push_reg (code, AMD64_RCX); \
3903 amd64_push_reg (code, reg); \
3904 amd64_push_imm (code, reg); \
3905 amd64_push_imm (code, text " %d %p\n"); \
3906 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
3907 amd64_call_reg (code, AMD64_RAX); \
3908 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
3909 amd64_pop_reg (code, AMD64_RCX); \
3910 amd64_pop_reg (code, AMD64_RDX); \
3911 amd64_pop_reg (code, AMD64_RAX);
3913 /* benchmark and set based on cpu */
3914 #define LOOP_ALIGNMENT 8
3915 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3919 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3924 guint8 *code = cfg->native_code + cfg->code_len;
3927 /* Fix max_offset estimate for each successor bb */
3928 if (cfg->opt & MONO_OPT_BRANCH) {
3929 int current_offset = cfg->code_len;
3930 MonoBasicBlock *current_bb;
3931 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3932 current_bb->max_offset = current_offset;
3933 current_offset += current_bb->max_length;
3937 if (cfg->opt & MONO_OPT_LOOP) {
3938 int pad, align = LOOP_ALIGNMENT;
3939 /* set alignment depending on cpu */
3940 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3942 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3943 amd64_padding (code, pad);
3944 cfg->code_len += pad;
3945 bb->native_offset = cfg->code_len;
3949 #if defined(__native_client_codegen__)
3950 /* For Native Client, all indirect call/jump targets must be */
3951 /* 32-byte aligned. Exception handler blocks are jumped to */
3952 /* indirectly as well. */
3953 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
3954 (bb->flags & BB_EXCEPTION_HANDLER);
3956 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
3957 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
3958 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
3959 cfg->code_len += pad;
3960 bb->native_offset = cfg->code_len;
3962 #endif /*__native_client_codegen__*/
3964 if (cfg->verbose_level > 2)
3965 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3967 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
3968 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3969 g_assert (!cfg->compile_aot);
3971 cov->data [bb->dfn].cil_code = bb->cil_code;
3972 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3973 /* this is not thread save, but good enough */
3974 amd64_inc_membase (code, AMD64_R11, 0);
3977 offset = code - cfg->native_code;
3979 mono_debug_open_block (cfg, bb, offset);
3981 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)
3982 x86_breakpoint (code);
3984 MONO_BB_FOR_EACH_INS (bb, ins) {
3985 offset = code - cfg->native_code;
3987 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3989 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
3991 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
3992 cfg->code_size *= 2;
3993 cfg->native_code = (unsigned char *)mono_realloc_native_code(cfg);
3994 code = cfg->native_code + offset;
3995 cfg->stat_code_reallocs++;
3998 if (cfg->debug_info)
3999 mono_debug_record_line_number (cfg, ins, offset);
4001 switch (ins->opcode) {
4003 amd64_mul_reg (code, ins->sreg2, TRUE);
4006 amd64_mul_reg (code, ins->sreg2, FALSE);
4008 case OP_X86_SETEQ_MEMBASE:
4009 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
4011 case OP_STOREI1_MEMBASE_IMM:
4012 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
4014 case OP_STOREI2_MEMBASE_IMM:
4015 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
4017 case OP_STOREI4_MEMBASE_IMM:
4018 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
4020 case OP_STOREI1_MEMBASE_REG:
4021 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
4023 case OP_STOREI2_MEMBASE_REG:
4024 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
4026 /* In AMD64 NaCl, pointers are 4 bytes, */
4027 /* so STORE_* != STOREI8_*. Likewise below. */
4028 case OP_STORE_MEMBASE_REG:
4029 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
4031 case OP_STOREI8_MEMBASE_REG:
4032 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
4034 case OP_STOREI4_MEMBASE_REG:
4035 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
4037 case OP_STORE_MEMBASE_IMM:
4038 #ifndef __native_client_codegen__
4039 /* In NaCl, this could be a PCONST type, which could */
4040 /* mean a pointer type was copied directly into the */
4041 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
4042 /* the value would be 0x00000000FFFFFFFF which is */
4043 /* not proper for an imm32 unless you cast it. */
4044 g_assert (amd64_is_imm32 (ins->inst_imm));
4046 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
4048 case OP_STOREI8_MEMBASE_IMM:
4049 g_assert (amd64_is_imm32 (ins->inst_imm));
4050 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
4053 #ifdef __mono_ilp32__
4054 /* In ILP32, pointers are 4 bytes, so separate these */
4055 /* cases, use literal 8 below where we really want 8 */
4056 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4057 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
4061 // FIXME: Decompose this earlier
4062 if (amd64_use_imm32 (ins->inst_imm))
4063 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
4065 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4066 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
4070 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4071 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
4074 // FIXME: Decompose this earlier
4075 if (amd64_use_imm32 (ins->inst_imm))
4076 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
4078 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4079 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
4083 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4084 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
4087 /* For NaCl, pointers are 4 bytes, so separate these */
4088 /* cases, use literal 8 below where we really want 8 */
4089 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4090 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
4092 case OP_LOAD_MEMBASE:
4093 g_assert (amd64_is_imm32 (ins->inst_offset));
4094 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
4096 case OP_LOADI8_MEMBASE:
4097 /* Use literal 8 instead of sizeof pointer or */
4098 /* register, we really want 8 for this opcode */
4099 g_assert (amd64_is_imm32 (ins->inst_offset));
4100 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
4102 case OP_LOADI4_MEMBASE:
4103 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4105 case OP_LOADU4_MEMBASE:
4106 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
4108 case OP_LOADU1_MEMBASE:
4109 /* The cpu zero extends the result into 64 bits */
4110 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
4112 case OP_LOADI1_MEMBASE:
4113 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
4115 case OP_LOADU2_MEMBASE:
4116 /* The cpu zero extends the result into 64 bits */
4117 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
4119 case OP_LOADI2_MEMBASE:
4120 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
4122 case OP_AMD64_LOADI8_MEMINDEX:
4123 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
4125 case OP_LCONV_TO_I1:
4126 case OP_ICONV_TO_I1:
4128 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
4130 case OP_LCONV_TO_I2:
4131 case OP_ICONV_TO_I2:
4133 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
4135 case OP_LCONV_TO_U1:
4136 case OP_ICONV_TO_U1:
4137 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
4139 case OP_LCONV_TO_U2:
4140 case OP_ICONV_TO_U2:
4141 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
4144 /* Clean out the upper word */
4145 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4148 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
4152 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4154 case OP_COMPARE_IMM:
4155 #if defined(__mono_ilp32__)
4156 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
4157 g_assert (amd64_is_imm32 (ins->inst_imm));
4158 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4161 case OP_LCOMPARE_IMM:
4162 g_assert (amd64_is_imm32 (ins->inst_imm));
4163 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
4165 case OP_X86_COMPARE_REG_MEMBASE:
4166 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
4168 case OP_X86_TEST_NULL:
4169 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
4171 case OP_AMD64_TEST_NULL:
4172 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4175 case OP_X86_ADD_REG_MEMBASE:
4176 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4178 case OP_X86_SUB_REG_MEMBASE:
4179 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4181 case OP_X86_AND_REG_MEMBASE:
4182 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4184 case OP_X86_OR_REG_MEMBASE:
4185 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4187 case OP_X86_XOR_REG_MEMBASE:
4188 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4191 case OP_X86_ADD_MEMBASE_IMM:
4192 /* FIXME: Make a 64 version too */
4193 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4195 case OP_X86_SUB_MEMBASE_IMM:
4196 g_assert (amd64_is_imm32 (ins->inst_imm));
4197 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4199 case OP_X86_AND_MEMBASE_IMM:
4200 g_assert (amd64_is_imm32 (ins->inst_imm));
4201 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4203 case OP_X86_OR_MEMBASE_IMM:
4204 g_assert (amd64_is_imm32 (ins->inst_imm));
4205 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4207 case OP_X86_XOR_MEMBASE_IMM:
4208 g_assert (amd64_is_imm32 (ins->inst_imm));
4209 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4211 case OP_X86_ADD_MEMBASE_REG:
4212 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4214 case OP_X86_SUB_MEMBASE_REG:
4215 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4217 case OP_X86_AND_MEMBASE_REG:
4218 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4220 case OP_X86_OR_MEMBASE_REG:
4221 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4223 case OP_X86_XOR_MEMBASE_REG:
4224 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4226 case OP_X86_INC_MEMBASE:
4227 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4229 case OP_X86_INC_REG:
4230 amd64_inc_reg_size (code, ins->dreg, 4);
4232 case OP_X86_DEC_MEMBASE:
4233 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4235 case OP_X86_DEC_REG:
4236 amd64_dec_reg_size (code, ins->dreg, 4);
4238 case OP_X86_MUL_REG_MEMBASE:
4239 case OP_X86_MUL_MEMBASE_REG:
4240 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4242 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4243 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4245 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4246 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4248 case OP_AMD64_COMPARE_MEMBASE_REG:
4249 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4251 case OP_AMD64_COMPARE_MEMBASE_IMM:
4252 g_assert (amd64_is_imm32 (ins->inst_imm));
4253 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4255 case OP_X86_COMPARE_MEMBASE8_IMM:
4256 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4258 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4259 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4261 case OP_AMD64_COMPARE_REG_MEMBASE:
4262 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4265 case OP_AMD64_ADD_REG_MEMBASE:
4266 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4268 case OP_AMD64_SUB_REG_MEMBASE:
4269 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4271 case OP_AMD64_AND_REG_MEMBASE:
4272 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4274 case OP_AMD64_OR_REG_MEMBASE:
4275 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4277 case OP_AMD64_XOR_REG_MEMBASE:
4278 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4281 case OP_AMD64_ADD_MEMBASE_REG:
4282 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4284 case OP_AMD64_SUB_MEMBASE_REG:
4285 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4287 case OP_AMD64_AND_MEMBASE_REG:
4288 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4290 case OP_AMD64_OR_MEMBASE_REG:
4291 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4293 case OP_AMD64_XOR_MEMBASE_REG:
4294 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4297 case OP_AMD64_ADD_MEMBASE_IMM:
4298 g_assert (amd64_is_imm32 (ins->inst_imm));
4299 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4301 case OP_AMD64_SUB_MEMBASE_IMM:
4302 g_assert (amd64_is_imm32 (ins->inst_imm));
4303 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4305 case OP_AMD64_AND_MEMBASE_IMM:
4306 g_assert (amd64_is_imm32 (ins->inst_imm));
4307 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4309 case OP_AMD64_OR_MEMBASE_IMM:
4310 g_assert (amd64_is_imm32 (ins->inst_imm));
4311 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4313 case OP_AMD64_XOR_MEMBASE_IMM:
4314 g_assert (amd64_is_imm32 (ins->inst_imm));
4315 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4319 amd64_breakpoint (code);
4321 case OP_RELAXED_NOP:
4322 x86_prefix (code, X86_REP_PREFIX);
4330 case OP_DUMMY_STORE:
4331 case OP_DUMMY_ICONST:
4332 case OP_DUMMY_R8CONST:
4333 case OP_NOT_REACHED:
4336 case OP_IL_SEQ_POINT:
4337 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4339 case OP_SEQ_POINT: {
4340 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4341 MonoInst *var = (MonoInst *)cfg->arch.ss_tramp_var;
4344 /* Load ss_tramp_var */
4345 /* This is equal to &ss_trampoline */
4346 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4347 /* Load the trampoline address */
4348 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4349 /* Call it if it is non-null */
4350 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4352 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4353 amd64_call_reg (code, AMD64_R11);
4354 amd64_patch (label, code);
4358 * This is the address which is saved in seq points,
4360 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4362 if (cfg->compile_aot) {
4363 guint32 offset = code - cfg->native_code;
4365 MonoInst *info_var = (MonoInst *)cfg->arch.seq_point_info_var;
4369 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4370 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4371 /* Load the info->bp_addrs [offset], which is either NULL or the address of the breakpoint trampoline */
4372 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4373 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4375 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4376 /* Call the trampoline */
4377 amd64_call_reg (code, AMD64_R11);
4378 amd64_patch (label, code);
4380 MonoInst *var = (MonoInst *)cfg->arch.bp_tramp_var;
4384 * Emit a test+branch against a constant, the constant will be overwritten
4385 * by mono_arch_set_breakpoint () to cause the test to fail.
4387 amd64_mov_reg_imm (code, AMD64_R11, 0);
4388 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4390 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4393 g_assert (var->opcode == OP_REGOFFSET);
4394 /* Load bp_tramp_var */
4395 /* This is equal to &bp_trampoline */
4396 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4397 /* Call the trampoline */
4398 amd64_call_membase (code, AMD64_R11, 0);
4399 amd64_patch (label, code);
4402 * Add an additional nop so skipping the bp doesn't cause the ip to point
4403 * to another IL offset.
4411 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4414 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4418 g_assert (amd64_is_imm32 (ins->inst_imm));
4419 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4422 g_assert (amd64_is_imm32 (ins->inst_imm));
4423 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4428 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4431 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4435 g_assert (amd64_is_imm32 (ins->inst_imm));
4436 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4439 g_assert (amd64_is_imm32 (ins->inst_imm));
4440 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4443 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4447 g_assert (amd64_is_imm32 (ins->inst_imm));
4448 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4451 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4456 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4458 switch (ins->inst_imm) {
4462 if (ins->dreg != ins->sreg1)
4463 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4464 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4467 /* LEA r1, [r2 + r2*2] */
4468 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4471 /* LEA r1, [r2 + r2*4] */
4472 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4475 /* LEA r1, [r2 + r2*2] */
4477 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4478 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4481 /* LEA r1, [r2 + r2*8] */
4482 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4485 /* LEA r1, [r2 + r2*4] */
4487 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4488 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4491 /* LEA r1, [r2 + r2*2] */
4493 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4494 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4497 /* LEA r1, [r2 + r2*4] */
4498 /* LEA r1, [r1 + r1*4] */
4499 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4500 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4503 /* LEA r1, [r2 + r2*4] */
4505 /* LEA r1, [r1 + r1*4] */
4506 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4507 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4508 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4511 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
4518 #if defined( __native_client_codegen__ )
4519 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4520 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4522 /* Regalloc magic makes the div/rem cases the same */
4523 if (ins->sreg2 == AMD64_RDX) {
4524 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4526 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4529 amd64_div_reg (code, ins->sreg2, TRUE);
4534 #if defined( __native_client_codegen__ )
4535 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4536 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4538 if (ins->sreg2 == AMD64_RDX) {
4539 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4540 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4541 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4543 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4544 amd64_div_reg (code, ins->sreg2, FALSE);
4549 #if defined( __native_client_codegen__ )
4550 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4551 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4553 if (ins->sreg2 == AMD64_RDX) {
4554 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4555 amd64_cdq_size (code, 4);
4556 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4558 amd64_cdq_size (code, 4);
4559 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4564 #if defined( __native_client_codegen__ )
4565 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4566 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4568 if (ins->sreg2 == AMD64_RDX) {
4569 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4570 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4571 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4573 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4574 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4578 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4579 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4582 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4586 g_assert (amd64_is_imm32 (ins->inst_imm));
4587 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4590 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4594 g_assert (amd64_is_imm32 (ins->inst_imm));
4595 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4598 g_assert (ins->sreg2 == AMD64_RCX);
4599 amd64_shift_reg (code, X86_SHL, ins->dreg);
4602 g_assert (ins->sreg2 == AMD64_RCX);
4603 amd64_shift_reg (code, X86_SAR, ins->dreg);
4607 g_assert (amd64_is_imm32 (ins->inst_imm));
4608 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4611 g_assert (amd64_is_imm32 (ins->inst_imm));
4612 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4614 case OP_LSHR_UN_IMM:
4615 g_assert (amd64_is_imm32 (ins->inst_imm));
4616 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4619 g_assert (ins->sreg2 == AMD64_RCX);
4620 amd64_shift_reg (code, X86_SHR, ins->dreg);
4624 g_assert (amd64_is_imm32 (ins->inst_imm));
4625 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4630 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4633 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4636 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4639 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4643 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4646 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4649 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4652 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4655 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4658 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4661 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4664 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4667 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4670 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4673 amd64_neg_reg_size (code, ins->sreg1, 4);
4676 amd64_not_reg_size (code, ins->sreg1, 4);
4679 g_assert (ins->sreg2 == AMD64_RCX);
4680 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4683 g_assert (ins->sreg2 == AMD64_RCX);
4684 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4687 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4689 case OP_ISHR_UN_IMM:
4690 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4693 g_assert (ins->sreg2 == AMD64_RCX);
4694 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4697 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4700 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4703 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4704 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4706 case OP_IMUL_OVF_UN:
4707 case OP_LMUL_OVF_UN: {
4708 /* the mul operation and the exception check should most likely be split */
4709 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4710 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4711 /*g_assert (ins->sreg2 == X86_EAX);
4712 g_assert (ins->dreg == X86_EAX);*/
4713 if (ins->sreg2 == X86_EAX) {
4714 non_eax_reg = ins->sreg1;
4715 } else if (ins->sreg1 == X86_EAX) {
4716 non_eax_reg = ins->sreg2;
4718 /* no need to save since we're going to store to it anyway */
4719 if (ins->dreg != X86_EAX) {
4721 amd64_push_reg (code, X86_EAX);
4723 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4724 non_eax_reg = ins->sreg2;
4726 if (ins->dreg == X86_EDX) {
4729 amd64_push_reg (code, X86_EAX);
4733 amd64_push_reg (code, X86_EDX);
4735 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4736 /* save before the check since pop and mov don't change the flags */
4737 if (ins->dreg != X86_EAX)
4738 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4740 amd64_pop_reg (code, X86_EDX);
4742 amd64_pop_reg (code, X86_EAX);
4743 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4747 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4749 case OP_ICOMPARE_IMM:
4750 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4772 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4780 case OP_CMOV_INE_UN:
4781 case OP_CMOV_IGE_UN:
4782 case OP_CMOV_IGT_UN:
4783 case OP_CMOV_ILE_UN:
4784 case OP_CMOV_ILT_UN:
4790 case OP_CMOV_LNE_UN:
4791 case OP_CMOV_LGE_UN:
4792 case OP_CMOV_LGT_UN:
4793 case OP_CMOV_LLE_UN:
4794 case OP_CMOV_LLT_UN:
4795 g_assert (ins->dreg == ins->sreg1);
4796 /* This needs to operate on 64 bit values */
4797 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);
4801 amd64_not_reg (code, ins->sreg1);
4804 amd64_neg_reg (code, ins->sreg1);
4809 if ((((guint64)ins->inst_c0) >> 32) == 0 && !mini_get_debug_options()->single_imm_size)
4810 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4812 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4815 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4816 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4819 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4820 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4823 if (ins->dreg != ins->sreg1)
4824 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4826 case OP_AMD64_SET_XMMREG_R4: {
4828 if (ins->dreg != ins->sreg1)
4829 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4831 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4835 case OP_AMD64_SET_XMMREG_R8: {
4836 if (ins->dreg != ins->sreg1)
4837 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4841 MonoCallInst *call = (MonoCallInst*)ins;
4842 int i, save_area_offset;
4844 g_assert (!cfg->method->save_lmf);
4846 /* Restore callee saved registers */
4847 save_area_offset = cfg->arch.reg_save_area_offset;
4848 for (i = 0; i < AMD64_NREG; ++i)
4849 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4850 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4851 save_area_offset += 8;
4854 if (cfg->arch.omit_fp) {
4855 if (cfg->arch.stack_alloc_size)
4856 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4858 if (call->stack_usage)
4861 /* Copy arguments on the stack to our argument area */
4862 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4863 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4864 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4870 offset = code - cfg->native_code;
4871 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4872 if (cfg->compile_aot)
4873 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4875 amd64_set_reg_template (code, AMD64_R11);
4876 amd64_jump_reg (code, AMD64_R11);
4877 ins->flags |= MONO_INST_GC_CALLSITE;
4878 ins->backend.pc_offset = code - cfg->native_code;
4882 /* ensure ins->sreg1 is not NULL */
4883 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4886 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4887 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4897 call = (MonoCallInst*)ins;
4899 * The AMD64 ABI forces callers to know about varargs.
4901 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4902 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4903 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4905 * Since the unmanaged calling convention doesn't contain a
4906 * 'vararg' entry, we have to treat every pinvoke call as a
4907 * potential vararg call.
4911 for (i = 0; i < AMD64_XMM_NREG; ++i)
4912 if (call->used_fregs & (1 << i))
4915 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4917 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4920 if (ins->flags & MONO_INST_HAS_METHOD)
4921 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4923 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
4924 ins->flags |= MONO_INST_GC_CALLSITE;
4925 ins->backend.pc_offset = code - cfg->native_code;
4926 code = emit_move_return_value (cfg, ins, code);
4933 case OP_VOIDCALL_REG:
4935 call = (MonoCallInst*)ins;
4937 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4938 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4939 ins->sreg1 = AMD64_R11;
4943 * The AMD64 ABI forces callers to know about varargs.
4945 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4946 if (ins->sreg1 == AMD64_RAX) {
4947 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4948 ins->sreg1 = AMD64_R11;
4950 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4951 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4953 * Since the unmanaged calling convention doesn't contain a
4954 * 'vararg' entry, we have to treat every pinvoke call as a
4955 * potential vararg call.
4959 for (i = 0; i < AMD64_XMM_NREG; ++i)
4960 if (call->used_fregs & (1 << i))
4962 if (ins->sreg1 == AMD64_RAX) {
4963 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4964 ins->sreg1 = AMD64_R11;
4967 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4969 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4972 amd64_call_reg (code, ins->sreg1);
4973 ins->flags |= MONO_INST_GC_CALLSITE;
4974 ins->backend.pc_offset = code - cfg->native_code;
4975 code = emit_move_return_value (cfg, ins, code);
4977 case OP_FCALL_MEMBASE:
4978 case OP_RCALL_MEMBASE:
4979 case OP_LCALL_MEMBASE:
4980 case OP_VCALL_MEMBASE:
4981 case OP_VCALL2_MEMBASE:
4982 case OP_VOIDCALL_MEMBASE:
4983 case OP_CALL_MEMBASE:
4984 call = (MonoCallInst*)ins;
4986 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
4987 ins->flags |= MONO_INST_GC_CALLSITE;
4988 ins->backend.pc_offset = code - cfg->native_code;
4989 code = emit_move_return_value (cfg, ins, code);
4993 MonoInst *var = cfg->dyn_call_var;
4995 g_assert (var->opcode == OP_REGOFFSET);
4997 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
4998 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
5000 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
5002 /* Save args buffer */
5003 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
5005 /* Set argument registers */
5006 for (i = 0; i < PARAM_REGS; ++i)
5007 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
5010 amd64_call_reg (code, AMD64_R10);
5012 ins->flags |= MONO_INST_GC_CALLSITE;
5013 ins->backend.pc_offset = code - cfg->native_code;
5016 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
5017 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
5020 case OP_AMD64_SAVE_SP_TO_LMF: {
5021 MonoInst *lmf_var = cfg->lmf_var;
5022 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
5026 g_assert_not_reached ();
5027 amd64_push_reg (code, ins->sreg1);
5029 case OP_X86_PUSH_IMM:
5030 g_assert_not_reached ();
5031 g_assert (amd64_is_imm32 (ins->inst_imm));
5032 amd64_push_imm (code, ins->inst_imm);
5034 case OP_X86_PUSH_MEMBASE:
5035 g_assert_not_reached ();
5036 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
5038 case OP_X86_PUSH_OBJ: {
5039 int size = ALIGN_TO (ins->inst_imm, 8);
5041 g_assert_not_reached ();
5043 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5044 amd64_push_reg (code, AMD64_RDI);
5045 amd64_push_reg (code, AMD64_RSI);
5046 amd64_push_reg (code, AMD64_RCX);
5047 if (ins->inst_offset)
5048 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
5050 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
5051 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
5052 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
5054 amd64_prefix (code, X86_REP_PREFIX);
5056 amd64_pop_reg (code, AMD64_RCX);
5057 amd64_pop_reg (code, AMD64_RSI);
5058 amd64_pop_reg (code, AMD64_RDI);
5061 case OP_GENERIC_CLASS_INIT: {
5062 static int byte_offset = -1;
5063 static guint8 bitmask;
5066 g_assert (ins->sreg1 == MONO_AMD64_ARG_REG1);
5068 if (byte_offset < 0)
5069 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
5071 amd64_test_membase_imm_size (code, ins->sreg1, byte_offset, bitmask, 1);
5073 amd64_branch8 (code, X86_CC_NZ, -1, 1);
5075 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_generic_class_init", FALSE);
5076 ins->flags |= MONO_INST_GC_CALLSITE;
5077 ins->backend.pc_offset = code - cfg->native_code;
5079 x86_patch (jump, code);
5084 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
5086 case OP_X86_LEA_MEMBASE:
5087 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
5090 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
5093 /* keep alignment */
5094 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
5095 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
5096 code = mono_emit_stack_alloc (cfg, code, ins);
5097 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5098 if (cfg->param_area)
5099 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5101 case OP_LOCALLOC_IMM: {
5102 guint32 size = ins->inst_imm;
5103 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
5105 if (ins->flags & MONO_INST_INIT) {
5109 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5110 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5112 for (i = 0; i < size; i += 8)
5113 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
5114 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5116 amd64_mov_reg_imm (code, ins->dreg, size);
5117 ins->sreg1 = ins->dreg;
5119 code = mono_emit_stack_alloc (cfg, code, ins);
5120 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5123 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5124 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5126 if (cfg->param_area)
5127 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5131 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5132 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5133 (gpointer)"mono_arch_throw_exception", FALSE);
5134 ins->flags |= MONO_INST_GC_CALLSITE;
5135 ins->backend.pc_offset = code - cfg->native_code;
5139 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5140 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5141 (gpointer)"mono_arch_rethrow_exception", FALSE);
5142 ins->flags |= MONO_INST_GC_CALLSITE;
5143 ins->backend.pc_offset = code - cfg->native_code;
5146 case OP_CALL_HANDLER:
5148 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5149 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5150 amd64_call_imm (code, 0);
5151 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
5152 /* Restore stack alignment */
5153 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5155 case OP_START_HANDLER: {
5156 /* Even though we're saving RSP, use sizeof */
5157 /* gpointer because spvar is of type IntPtr */
5158 /* see: mono_create_spvar_for_region */
5159 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5160 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
5162 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
5163 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
5165 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
5169 case OP_ENDFINALLY: {
5170 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5171 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5175 case OP_ENDFILTER: {
5176 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5177 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5178 /* The local allocator will put the result into RAX */
5183 if (ins->dreg != AMD64_RAX)
5184 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, sizeof (gpointer));
5187 ins->inst_c0 = code - cfg->native_code;
5190 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
5191 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
5193 if (ins->inst_target_bb->native_offset) {
5194 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
5196 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5197 if ((cfg->opt & MONO_OPT_BRANCH) &&
5198 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
5199 x86_jump8 (code, 0);
5201 x86_jump32 (code, 0);
5205 amd64_jump_reg (code, ins->sreg1);
5228 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5229 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5231 case OP_COND_EXC_EQ:
5232 case OP_COND_EXC_NE_UN:
5233 case OP_COND_EXC_LT:
5234 case OP_COND_EXC_LT_UN:
5235 case OP_COND_EXC_GT:
5236 case OP_COND_EXC_GT_UN:
5237 case OP_COND_EXC_GE:
5238 case OP_COND_EXC_GE_UN:
5239 case OP_COND_EXC_LE:
5240 case OP_COND_EXC_LE_UN:
5241 case OP_COND_EXC_IEQ:
5242 case OP_COND_EXC_INE_UN:
5243 case OP_COND_EXC_ILT:
5244 case OP_COND_EXC_ILT_UN:
5245 case OP_COND_EXC_IGT:
5246 case OP_COND_EXC_IGT_UN:
5247 case OP_COND_EXC_IGE:
5248 case OP_COND_EXC_IGE_UN:
5249 case OP_COND_EXC_ILE:
5250 case OP_COND_EXC_ILE_UN:
5251 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);
5253 case OP_COND_EXC_OV:
5254 case OP_COND_EXC_NO:
5256 case OP_COND_EXC_NC:
5257 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5258 (ins->opcode < OP_COND_EXC_NE_UN), (const char *)ins->inst_p1);
5260 case OP_COND_EXC_IOV:
5261 case OP_COND_EXC_INO:
5262 case OP_COND_EXC_IC:
5263 case OP_COND_EXC_INC:
5264 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5265 (ins->opcode < OP_COND_EXC_INE_UN), (const char *)ins->inst_p1);
5268 /* floating point opcodes */
5270 double d = *(double *)ins->inst_p0;
5272 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5273 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5276 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5277 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5282 float f = *(float *)ins->inst_p0;
5284 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5286 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5288 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5291 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5292 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5294 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5298 case OP_STORER8_MEMBASE_REG:
5299 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5301 case OP_LOADR8_MEMBASE:
5302 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5304 case OP_STORER4_MEMBASE_REG:
5306 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5308 /* This requires a double->single conversion */
5309 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5310 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5313 case OP_LOADR4_MEMBASE:
5315 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5317 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5318 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5321 case OP_ICONV_TO_R4:
5323 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5325 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5326 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5329 case OP_ICONV_TO_R8:
5330 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5332 case OP_LCONV_TO_R4:
5334 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5336 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5337 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5340 case OP_LCONV_TO_R8:
5341 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5343 case OP_FCONV_TO_R4:
5345 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5347 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5348 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5351 case OP_FCONV_TO_I1:
5352 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5354 case OP_FCONV_TO_U1:
5355 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5357 case OP_FCONV_TO_I2:
5358 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5360 case OP_FCONV_TO_U2:
5361 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5363 case OP_FCONV_TO_U4:
5364 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5366 case OP_FCONV_TO_I4:
5368 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5370 case OP_FCONV_TO_I8:
5371 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5374 case OP_RCONV_TO_I1:
5375 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5376 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5378 case OP_RCONV_TO_U1:
5379 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5380 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5382 case OP_RCONV_TO_I2:
5383 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5384 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5386 case OP_RCONV_TO_U2:
5387 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5388 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5390 case OP_RCONV_TO_I4:
5391 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5393 case OP_RCONV_TO_U4:
5394 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5396 case OP_RCONV_TO_I8:
5397 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5399 case OP_RCONV_TO_R8:
5400 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5402 case OP_RCONV_TO_R4:
5403 if (ins->dreg != ins->sreg1)
5404 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5407 case OP_LCONV_TO_R_UN: {
5410 /* Based on gcc code */
5411 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5412 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5415 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5416 br [1] = code; x86_jump8 (code, 0);
5417 amd64_patch (br [0], code);
5420 /* Save to the red zone */
5421 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5422 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5423 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5424 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5425 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5426 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5427 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5428 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5429 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5431 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5432 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5433 amd64_patch (br [1], code);
5436 case OP_LCONV_TO_OVF_U4:
5437 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5438 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5439 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5441 case OP_LCONV_TO_OVF_I4_UN:
5442 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5443 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5444 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5447 if (ins->dreg != ins->sreg1)
5448 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5451 if (ins->dreg != ins->sreg1)
5452 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5454 case OP_MOVE_F_TO_I4:
5456 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5458 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5459 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5462 case OP_MOVE_I4_TO_F:
5463 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5465 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5467 case OP_MOVE_F_TO_I8:
5468 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5470 case OP_MOVE_I8_TO_F:
5471 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5474 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5477 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5480 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5483 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5486 static double r8_0 = -0.0;
5488 g_assert (ins->sreg1 == ins->dreg);
5490 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5491 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5495 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5498 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5501 static guint64 d = 0x7fffffffffffffffUL;
5503 g_assert (ins->sreg1 == ins->dreg);
5505 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5506 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5510 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5514 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5517 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5520 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5523 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5526 static float r4_0 = -0.0;
5528 g_assert (ins->sreg1 == ins->dreg);
5530 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5531 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5532 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
5537 g_assert (cfg->opt & MONO_OPT_CMOV);
5538 g_assert (ins->dreg == ins->sreg1);
5539 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5540 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
5543 g_assert (cfg->opt & MONO_OPT_CMOV);
5544 g_assert (ins->dreg == ins->sreg1);
5545 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5546 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
5549 g_assert (cfg->opt & MONO_OPT_CMOV);
5550 g_assert (ins->dreg == ins->sreg1);
5551 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5552 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
5555 g_assert (cfg->opt & MONO_OPT_CMOV);
5556 g_assert (ins->dreg == ins->sreg1);
5557 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5558 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
5561 g_assert (cfg->opt & MONO_OPT_CMOV);
5562 g_assert (ins->dreg == ins->sreg1);
5563 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5564 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
5567 g_assert (cfg->opt & MONO_OPT_CMOV);
5568 g_assert (ins->dreg == ins->sreg1);
5569 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5570 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
5573 g_assert (cfg->opt & MONO_OPT_CMOV);
5574 g_assert (ins->dreg == ins->sreg1);
5575 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5576 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
5579 g_assert (cfg->opt & MONO_OPT_CMOV);
5580 g_assert (ins->dreg == ins->sreg1);
5581 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5582 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5588 * The two arguments are swapped because the fbranch instructions
5589 * depend on this for the non-sse case to work.
5591 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5595 * FIXME: Get rid of this.
5596 * The two arguments are swapped because the fbranch instructions
5597 * depend on this for the non-sse case to work.
5599 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5603 /* zeroing the register at the start results in
5604 * shorter and faster code (we can also remove the widening op)
5606 guchar *unordered_check;
5608 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5609 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5610 unordered_check = code;
5611 x86_branch8 (code, X86_CC_P, 0, FALSE);
5613 if (ins->opcode == OP_FCEQ) {
5614 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5615 amd64_patch (unordered_check, code);
5617 guchar *jump_to_end;
5618 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5620 x86_jump8 (code, 0);
5621 amd64_patch (unordered_check, code);
5622 amd64_inc_reg (code, ins->dreg);
5623 amd64_patch (jump_to_end, code);
5629 /* zeroing the register at the start results in
5630 * shorter and faster code (we can also remove the widening op)
5632 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5633 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5634 if (ins->opcode == OP_FCLT_UN) {
5635 guchar *unordered_check = code;
5636 guchar *jump_to_end;
5637 x86_branch8 (code, X86_CC_P, 0, FALSE);
5638 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5640 x86_jump8 (code, 0);
5641 amd64_patch (unordered_check, code);
5642 amd64_inc_reg (code, ins->dreg);
5643 amd64_patch (jump_to_end, code);
5645 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5650 guchar *unordered_check;
5651 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5652 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5653 unordered_check = code;
5654 x86_branch8 (code, X86_CC_P, 0, FALSE);
5655 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5656 amd64_patch (unordered_check, code);
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->sreg2, ins->sreg1);
5668 if (ins->opcode == OP_FCGT) {
5669 unordered_check = code;
5670 x86_branch8 (code, X86_CC_P, 0, FALSE);
5671 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5672 amd64_patch (unordered_check, code);
5674 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5679 guchar *unordered_check;
5680 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5681 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5682 unordered_check = code;
5683 x86_branch8 (code, X86_CC_P, 0, FALSE);
5684 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5685 amd64_patch (unordered_check, code);
5695 gboolean unordered = FALSE;
5697 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5698 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5700 switch (ins->opcode) {
5702 x86_cond = X86_CC_EQ;
5705 x86_cond = X86_CC_LT;
5708 x86_cond = X86_CC_GT;
5711 x86_cond = X86_CC_GT;
5715 x86_cond = X86_CC_LT;
5719 g_assert_not_reached ();
5724 guchar *unordered_check;
5725 guchar *jump_to_end;
5727 unordered_check = code;
5728 x86_branch8 (code, X86_CC_P, 0, FALSE);
5729 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5731 x86_jump8 (code, 0);
5732 amd64_patch (unordered_check, code);
5733 amd64_inc_reg (code, ins->dreg);
5734 amd64_patch (jump_to_end, code);
5736 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5740 case OP_FCLT_MEMBASE:
5741 case OP_FCGT_MEMBASE:
5742 case OP_FCLT_UN_MEMBASE:
5743 case OP_FCGT_UN_MEMBASE:
5744 case OP_FCEQ_MEMBASE: {
5745 guchar *unordered_check, *jump_to_end;
5748 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5749 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5751 switch (ins->opcode) {
5752 case OP_FCEQ_MEMBASE:
5753 x86_cond = X86_CC_EQ;
5755 case OP_FCLT_MEMBASE:
5756 case OP_FCLT_UN_MEMBASE:
5757 x86_cond = X86_CC_LT;
5759 case OP_FCGT_MEMBASE:
5760 case OP_FCGT_UN_MEMBASE:
5761 x86_cond = X86_CC_GT;
5764 g_assert_not_reached ();
5767 unordered_check = code;
5768 x86_branch8 (code, X86_CC_P, 0, FALSE);
5769 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5771 switch (ins->opcode) {
5772 case OP_FCEQ_MEMBASE:
5773 case OP_FCLT_MEMBASE:
5774 case OP_FCGT_MEMBASE:
5775 amd64_patch (unordered_check, code);
5777 case OP_FCLT_UN_MEMBASE:
5778 case OP_FCGT_UN_MEMBASE:
5780 x86_jump8 (code, 0);
5781 amd64_patch (unordered_check, code);
5782 amd64_inc_reg (code, ins->dreg);
5783 amd64_patch (jump_to_end, code);
5791 guchar *jump = code;
5792 x86_branch8 (code, X86_CC_P, 0, TRUE);
5793 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5794 amd64_patch (jump, code);
5798 /* Branch if C013 != 100 */
5799 /* branch if !ZF or (PF|CF) */
5800 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5801 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5802 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5805 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5808 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5809 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5813 if (ins->opcode == OP_FBGT) {
5816 /* skip branch if C1=1 */
5818 x86_branch8 (code, X86_CC_P, 0, FALSE);
5819 /* branch if (C0 | C3) = 1 */
5820 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5821 amd64_patch (br1, code);
5824 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5828 /* Branch if C013 == 100 or 001 */
5831 /* skip branch if C1=1 */
5833 x86_branch8 (code, X86_CC_P, 0, FALSE);
5834 /* branch if (C0 | C3) = 1 */
5835 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5836 amd64_patch (br1, code);
5840 /* Branch if C013 == 000 */
5841 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5844 /* Branch if C013=000 or 100 */
5847 /* skip branch if C1=1 */
5849 x86_branch8 (code, X86_CC_P, 0, FALSE);
5850 /* branch if C0=0 */
5851 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5852 amd64_patch (br1, code);
5856 /* Branch if C013 != 001 */
5857 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5858 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5861 /* Transfer value to the fp stack */
5862 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5863 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5864 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5866 amd64_push_reg (code, AMD64_RAX);
5868 amd64_fnstsw (code);
5869 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5870 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5871 amd64_pop_reg (code, AMD64_RAX);
5872 amd64_fstp (code, 0);
5873 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
5874 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5877 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5880 case OP_TLS_GET_REG:
5881 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5884 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5887 case OP_TLS_SET_REG: {
5888 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5891 case OP_MEMORY_BARRIER: {
5892 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5896 case OP_ATOMIC_ADD_I4:
5897 case OP_ATOMIC_ADD_I8: {
5898 int dreg = ins->dreg;
5899 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5901 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5904 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5905 amd64_prefix (code, X86_LOCK_PREFIX);
5906 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5907 /* dreg contains the old value, add with sreg2 value */
5908 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5910 if (ins->dreg != dreg)
5911 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5915 case OP_ATOMIC_EXCHANGE_I4:
5916 case OP_ATOMIC_EXCHANGE_I8: {
5917 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5919 /* LOCK prefix is implied. */
5920 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5921 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5922 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5925 case OP_ATOMIC_CAS_I4:
5926 case OP_ATOMIC_CAS_I8: {
5929 if (ins->opcode == OP_ATOMIC_CAS_I8)
5935 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5936 * an explanation of how this works.
5938 g_assert (ins->sreg3 == AMD64_RAX);
5939 g_assert (ins->sreg1 != AMD64_RAX);
5940 g_assert (ins->sreg1 != ins->sreg2);
5942 amd64_prefix (code, X86_LOCK_PREFIX);
5943 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
5945 if (ins->dreg != AMD64_RAX)
5946 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
5949 case OP_ATOMIC_LOAD_I1: {
5950 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
5953 case OP_ATOMIC_LOAD_U1: {
5954 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
5957 case OP_ATOMIC_LOAD_I2: {
5958 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
5961 case OP_ATOMIC_LOAD_U2: {
5962 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
5965 case OP_ATOMIC_LOAD_I4: {
5966 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5969 case OP_ATOMIC_LOAD_U4:
5970 case OP_ATOMIC_LOAD_I8:
5971 case OP_ATOMIC_LOAD_U8: {
5972 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
5975 case OP_ATOMIC_LOAD_R4: {
5976 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5977 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5980 case OP_ATOMIC_LOAD_R8: {
5981 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5984 case OP_ATOMIC_STORE_I1:
5985 case OP_ATOMIC_STORE_U1:
5986 case OP_ATOMIC_STORE_I2:
5987 case OP_ATOMIC_STORE_U2:
5988 case OP_ATOMIC_STORE_I4:
5989 case OP_ATOMIC_STORE_U4:
5990 case OP_ATOMIC_STORE_I8:
5991 case OP_ATOMIC_STORE_U8: {
5994 switch (ins->opcode) {
5995 case OP_ATOMIC_STORE_I1:
5996 case OP_ATOMIC_STORE_U1:
5999 case OP_ATOMIC_STORE_I2:
6000 case OP_ATOMIC_STORE_U2:
6003 case OP_ATOMIC_STORE_I4:
6004 case OP_ATOMIC_STORE_U4:
6007 case OP_ATOMIC_STORE_I8:
6008 case OP_ATOMIC_STORE_U8:
6013 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
6015 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6019 case OP_ATOMIC_STORE_R4: {
6020 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6021 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
6023 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6027 case OP_ATOMIC_STORE_R8: {
6030 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
6034 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6038 case OP_CARD_TABLE_WBARRIER: {
6039 int ptr = ins->sreg1;
6040 int value = ins->sreg2;
6042 int nursery_shift, card_table_shift;
6043 gpointer card_table_mask;
6044 size_t nursery_size;
6046 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
6047 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
6048 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
6050 /*If either point to the stack we can simply avoid the WB. This happens due to
6051 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
6053 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
6057 * We need one register we can clobber, we choose EDX and make sreg1
6058 * fixed EAX to work around limitations in the local register allocator.
6059 * sreg2 might get allocated to EDX, but that is not a problem since
6060 * we use it before clobbering EDX.
6062 g_assert (ins->sreg1 == AMD64_RAX);
6065 * This is the code we produce:
6068 * edx >>= nursery_shift
6069 * cmp edx, (nursery_start >> nursery_shift)
6072 * edx >>= card_table_shift
6078 if (mono_gc_card_table_nursery_check ()) {
6079 if (value != AMD64_RDX)
6080 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
6081 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
6082 if (shifted_nursery_start >> 31) {
6084 * The value we need to compare against is 64 bits, so we need
6085 * another spare register. We use RBX, which we save and
6088 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
6089 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
6090 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
6091 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
6093 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
6095 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
6097 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
6098 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
6099 if (card_table_mask)
6100 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
6102 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
6103 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
6105 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
6107 if (mono_gc_card_table_nursery_check ())
6108 x86_patch (br, code);
6111 #ifdef MONO_ARCH_SIMD_INTRINSICS
6112 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
6114 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
6117 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
6120 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
6123 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
6126 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
6129 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
6132 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6133 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6136 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
6139 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
6142 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
6145 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
6148 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6151 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6154 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
6157 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6160 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
6163 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6166 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
6169 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
6172 case OP_PSHUFLEW_HIGH:
6173 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6174 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6176 case OP_PSHUFLEW_LOW:
6177 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6178 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6181 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6182 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6185 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6186 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6189 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
6190 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6194 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
6197 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
6200 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
6203 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
6206 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
6209 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
6212 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6213 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6216 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
6219 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
6222 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
6225 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6228 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6231 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6234 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6237 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6240 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6243 case OP_EXTRACT_MASK:
6244 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6248 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6251 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6254 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6258 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6261 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6264 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6267 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6271 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6274 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6277 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6280 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6284 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6287 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6290 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6294 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6297 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6300 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6304 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6307 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6311 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6314 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6317 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6321 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6324 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6327 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6331 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6334 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6337 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6340 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6344 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6347 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6350 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6353 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6356 case OP_PSUM_ABS_DIFF:
6357 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6360 case OP_UNPACK_LOWB:
6361 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6363 case OP_UNPACK_LOWW:
6364 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6366 case OP_UNPACK_LOWD:
6367 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6369 case OP_UNPACK_LOWQ:
6370 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6372 case OP_UNPACK_LOWPS:
6373 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6375 case OP_UNPACK_LOWPD:
6376 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6379 case OP_UNPACK_HIGHB:
6380 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6382 case OP_UNPACK_HIGHW:
6383 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6385 case OP_UNPACK_HIGHD:
6386 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6388 case OP_UNPACK_HIGHQ:
6389 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6391 case OP_UNPACK_HIGHPS:
6392 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6394 case OP_UNPACK_HIGHPD:
6395 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6399 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6402 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6405 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6408 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6411 case OP_PADDB_SAT_UN:
6412 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6414 case OP_PSUBB_SAT_UN:
6415 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6417 case OP_PADDW_SAT_UN:
6418 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6420 case OP_PSUBW_SAT_UN:
6421 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6425 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6428 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6431 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6434 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6438 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6441 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6444 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6446 case OP_PMULW_HIGH_UN:
6447 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6450 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6454 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6457 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6461 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6464 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6468 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6471 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6475 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6478 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6482 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6485 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6489 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6492 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6496 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6499 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6502 /*TODO: This is appart of the sse spec but not added
6504 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6507 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6512 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6515 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6518 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6521 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6524 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6527 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6530 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6533 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6536 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6539 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6543 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6546 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6550 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6551 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6553 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6558 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6560 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6561 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6565 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6567 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6568 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6569 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6573 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6575 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6578 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6580 case OP_EXTRACTX_U2:
6581 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6583 case OP_INSERTX_U1_SLOW:
6584 /*sreg1 is the extracted ireg (scratch)
6585 /sreg2 is the to be inserted ireg (scratch)
6586 /dreg is the xreg to receive the value*/
6588 /*clear the bits from the extracted word*/
6589 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6590 /*shift the value to insert if needed*/
6591 if (ins->inst_c0 & 1)
6592 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6593 /*join them together*/
6594 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6595 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6597 case OP_INSERTX_I4_SLOW:
6598 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6599 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6600 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6602 case OP_INSERTX_I8_SLOW:
6603 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6605 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6607 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6610 case OP_INSERTX_R4_SLOW:
6611 switch (ins->inst_c0) {
6614 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6616 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6619 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6621 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6623 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6624 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6627 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6629 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6631 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6632 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6635 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6637 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6639 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6640 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6644 case OP_INSERTX_R8_SLOW:
6646 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6648 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6650 case OP_STOREX_MEMBASE_REG:
6651 case OP_STOREX_MEMBASE:
6652 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6654 case OP_LOADX_MEMBASE:
6655 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6657 case OP_LOADX_ALIGNED_MEMBASE:
6658 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6660 case OP_STOREX_ALIGNED_MEMBASE_REG:
6661 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6663 case OP_STOREX_NTA_MEMBASE_REG:
6664 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6666 case OP_PREFETCH_MEMBASE:
6667 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6671 /*FIXME the peephole pass should have killed this*/
6672 if (ins->dreg != ins->sreg1)
6673 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6676 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6678 case OP_ICONV_TO_R4_RAW:
6679 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6682 case OP_FCONV_TO_R8_X:
6683 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6686 case OP_XCONV_R8_TO_I4:
6687 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6688 switch (ins->backend.source_opcode) {
6689 case OP_FCONV_TO_I1:
6690 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6692 case OP_FCONV_TO_U1:
6693 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6695 case OP_FCONV_TO_I2:
6696 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6698 case OP_FCONV_TO_U2:
6699 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6705 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6706 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6707 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6710 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6711 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6714 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6715 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6719 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6721 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6722 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6724 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6727 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6728 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6731 case OP_LIVERANGE_START: {
6732 if (cfg->verbose_level > 1)
6733 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6734 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6737 case OP_LIVERANGE_END: {
6738 if (cfg->verbose_level > 1)
6739 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6740 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6743 case OP_GC_SAFE_POINT: {
6744 const char *polling_func = NULL;
6745 int compare_val = 0;
6748 #if defined(__native_client_codegen__) && defined(__native_client_gc__)
6749 polling_func = "mono_nacl_gc";
6750 compare_val = 0xFFFFFFFF;
6752 g_assert (mono_threads_is_coop_enabled ());
6753 polling_func = "mono_threads_state_poll";
6757 amd64_test_membase_imm_size (code, ins->sreg1, 0, compare_val, 4);
6758 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6759 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func, FALSE);
6760 amd64_patch (br[0], code);
6764 case OP_GC_LIVENESS_DEF:
6765 case OP_GC_LIVENESS_USE:
6766 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6767 ins->backend.pc_offset = code - cfg->native_code;
6769 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6770 ins->backend.pc_offset = code - cfg->native_code;
6771 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6774 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6775 g_assert_not_reached ();
6778 if ((code - cfg->native_code - offset) > max_len) {
6779 #if !defined(__native_client_codegen__)
6780 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6781 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6782 g_assert_not_reached ();
6787 cfg->code_len = code - cfg->native_code;
6790 #endif /* DISABLE_JIT */
6793 mono_arch_register_lowlevel_calls (void)
6795 /* The signature doesn't matter */
6796 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6800 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
6802 unsigned char *ip = ji->ip.i + code;
6805 * Debug code to help track down problems where the target of a near call is
6808 if (amd64_is_near_call (ip)) {
6809 gint64 disp = (guint8*)target - (guint8*)ip;
6811 if (!amd64_is_imm32 (disp)) {
6812 printf ("TYPE: %d\n", ji->type);
6814 case MONO_PATCH_INFO_INTERNAL_METHOD:
6815 printf ("V: %s\n", ji->data.name);
6817 case MONO_PATCH_INFO_METHOD_JUMP:
6818 case MONO_PATCH_INFO_METHOD:
6819 printf ("V: %s\n", ji->data.method->name);
6827 amd64_patch (ip, (gpointer)target);
6833 get_max_epilog_size (MonoCompile *cfg)
6835 int max_epilog_size = 16;
6837 if (cfg->method->save_lmf)
6838 max_epilog_size += 256;
6840 if (mono_jit_trace_calls != NULL)
6841 max_epilog_size += 50;
6843 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6844 max_epilog_size += 50;
6846 max_epilog_size += (AMD64_NREG * 2);
6848 return max_epilog_size;
6852 * This macro is used for testing whenever the unwinder works correctly at every point
6853 * where an async exception can happen.
6855 /* This will generate a SIGSEGV at the given point in the code */
6856 #define async_exc_point(code) do { \
6857 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6858 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6859 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6860 cfg->arch.async_point_count ++; \
6865 mono_arch_emit_prolog (MonoCompile *cfg)
6867 MonoMethod *method = cfg->method;
6869 MonoMethodSignature *sig;
6871 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6874 MonoInst *lmf_var = cfg->lmf_var;
6875 gboolean args_clobbered = FALSE;
6876 gboolean trace = FALSE;
6877 #ifdef __native_client_codegen__
6878 guint alignment_check;
6881 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6883 #if defined(__default_codegen__)
6884 code = cfg->native_code = (unsigned char *)g_malloc (cfg->code_size);
6885 #elif defined(__native_client_codegen__)
6886 /* native_code_alloc is not 32-byte aligned, native_code is. */
6887 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6889 /* Align native_code to next nearest kNaclAlignment byte. */
6890 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6891 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6893 code = cfg->native_code;
6895 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6896 g_assert (alignment_check == 0);
6899 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6902 /* Amount of stack space allocated by register saving code */
6905 /* Offset between RSP and the CFA */
6909 * The prolog consists of the following parts:
6911 * - push rbp, mov rbp, rsp
6912 * - save callee saved regs using pushes
6914 * - save rgctx if needed
6915 * - save lmf if needed
6918 * - save rgctx if needed
6919 * - save lmf if needed
6920 * - save callee saved regs using moves
6925 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6926 // IP saved at CFA - 8
6927 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6928 async_exc_point (code);
6929 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6931 if (!cfg->arch.omit_fp) {
6932 amd64_push_reg (code, AMD64_RBP);
6934 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6935 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6936 async_exc_point (code);
6938 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6940 /* These are handled automatically by the stack marking code */
6941 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6943 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
6944 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
6945 async_exc_point (code);
6947 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6951 /* The param area is always at offset 0 from sp */
6952 /* This needs to be allocated here, since it has to come after the spill area */
6953 if (cfg->param_area) {
6954 if (cfg->arch.omit_fp)
6956 g_assert_not_reached ();
6957 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
6960 if (cfg->arch.omit_fp) {
6962 * On enter, the stack is misaligned by the pushing of the return
6963 * address. It is either made aligned by the pushing of %rbp, or by
6966 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
6967 if ((alloc_size % 16) == 0) {
6969 /* Mark the padding slot as NOREF */
6970 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
6973 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
6974 if (cfg->stack_offset != alloc_size) {
6975 /* Mark the padding slot as NOREF */
6976 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
6978 cfg->arch.sp_fp_offset = alloc_size;
6982 cfg->arch.stack_alloc_size = alloc_size;
6984 /* Allocate stack frame */
6986 /* See mono_emit_stack_alloc */
6987 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
6988 guint32 remaining_size = alloc_size;
6989 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
6990 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
6991 guint32 offset = code - cfg->native_code;
6992 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
6993 while (required_code_size >= (cfg->code_size - offset))
6994 cfg->code_size *= 2;
6995 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
6996 code = cfg->native_code + offset;
6997 cfg->stat_code_reallocs++;
7000 while (remaining_size >= 0x1000) {
7001 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
7002 if (cfg->arch.omit_fp) {
7003 cfa_offset += 0x1000;
7004 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7006 async_exc_point (code);
7008 if (cfg->arch.omit_fp)
7009 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
7012 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
7013 remaining_size -= 0x1000;
7015 if (remaining_size) {
7016 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
7017 if (cfg->arch.omit_fp) {
7018 cfa_offset += remaining_size;
7019 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7020 async_exc_point (code);
7023 if (cfg->arch.omit_fp)
7024 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
7028 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
7029 if (cfg->arch.omit_fp) {
7030 cfa_offset += alloc_size;
7031 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7032 async_exc_point (code);
7037 /* Stack alignment check */
7040 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
7041 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
7042 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
7043 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
7044 amd64_breakpoint (code);
7048 if (mini_get_debug_options ()->init_stacks) {
7049 /* Fill the stack frame with a dummy value to force deterministic behavior */
7051 /* Save registers to the red zone */
7052 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
7053 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
7055 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
7056 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
7057 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
7060 #if defined(__default_codegen__)
7061 amd64_prefix (code, X86_REP_PREFIX);
7063 #elif defined(__native_client_codegen__)
7064 /* NaCl stos pseudo-instruction */
7065 amd64_codegen_pre (code);
7066 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
7067 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
7068 /* Add %r15 to %rdi using lea, condition flags unaffected. */
7069 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
7070 amd64_prefix (code, X86_REP_PREFIX);
7072 amd64_codegen_post (code);
7073 #endif /* __native_client_codegen__ */
7075 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
7076 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
7080 if (method->save_lmf)
7081 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
7083 /* Save callee saved registers */
7084 if (cfg->arch.omit_fp) {
7085 save_area_offset = cfg->arch.reg_save_area_offset;
7086 /* Save caller saved registers after sp is adjusted */
7087 /* The registers are saved at the bottom of the frame */
7088 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
7090 /* The registers are saved just below the saved rbp */
7091 save_area_offset = cfg->arch.reg_save_area_offset;
7094 for (i = 0; i < AMD64_NREG; ++i) {
7095 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7096 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
7098 if (cfg->arch.omit_fp) {
7099 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
7100 /* These are handled automatically by the stack marking code */
7101 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
7103 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
7107 save_area_offset += 8;
7108 async_exc_point (code);
7112 /* store runtime generic context */
7113 if (cfg->rgctx_var) {
7114 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
7115 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
7117 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
7119 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
7120 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
7123 /* compute max_length in order to use short forward jumps */
7124 max_epilog_size = get_max_epilog_size (cfg);
7125 if (cfg->opt & MONO_OPT_BRANCH) {
7126 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7130 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7132 /* max alignment for loops */
7133 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
7134 max_length += LOOP_ALIGNMENT;
7135 #ifdef __native_client_codegen__
7136 /* max alignment for native client */
7137 max_length += kNaClAlignment;
7140 MONO_BB_FOR_EACH_INS (bb, ins) {
7141 #ifdef __native_client_codegen__
7143 int space_in_block = kNaClAlignment -
7144 ((max_length + cfg->code_len) & kNaClAlignmentMask);
7145 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7146 if (space_in_block < max_len && max_len < kNaClAlignment) {
7147 max_length += space_in_block;
7150 #endif /*__native_client_codegen__*/
7151 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7154 /* Take prolog and epilog instrumentation into account */
7155 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
7156 max_length += max_epilog_size;
7158 bb->max_length = max_length;
7162 sig = mono_method_signature (method);
7165 cinfo = (CallInfo *)cfg->arch.cinfo;
7167 if (sig->ret->type != MONO_TYPE_VOID) {
7168 /* Save volatile arguments to the stack */
7169 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
7170 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
7173 /* Keep this in sync with emit_load_volatile_arguments */
7174 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
7175 ArgInfo *ainfo = cinfo->args + i;
7177 ins = cfg->args [i];
7179 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
7180 /* Unused arguments */
7183 /* Save volatile arguments to the stack */
7184 if (ins->opcode != OP_REGVAR) {
7185 switch (ainfo->storage) {
7191 if (stack_offset & 0x1)
7193 else if (stack_offset & 0x2)
7195 else if (stack_offset & 0x4)
7200 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
7203 * Save the original location of 'this',
7204 * get_generic_info_from_stack_frame () needs this to properly look up
7205 * the argument value during the handling of async exceptions.
7207 if (ins == cfg->args [0]) {
7208 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7209 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
7213 case ArgInFloatSSEReg:
7214 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7216 case ArgInDoubleSSEReg:
7217 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7219 case ArgValuetypeInReg:
7220 for (quad = 0; quad < 2; quad ++) {
7221 switch (ainfo->pair_storage [quad]) {
7223 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7225 case ArgInFloatSSEReg:
7226 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7228 case ArgInDoubleSSEReg:
7229 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7234 g_assert_not_reached ();
7238 case ArgValuetypeAddrInIReg:
7239 if (ainfo->pair_storage [0] == ArgInIReg)
7240 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7246 /* Argument allocated to (non-volatile) register */
7247 switch (ainfo->storage) {
7249 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7252 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7255 g_assert_not_reached ();
7258 if (ins == cfg->args [0]) {
7259 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7260 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7265 if (cfg->method->save_lmf)
7266 args_clobbered = TRUE;
7269 args_clobbered = TRUE;
7270 code = (guint8 *)mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7273 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7274 args_clobbered = TRUE;
7277 * Optimize the common case of the first bblock making a call with the same
7278 * arguments as the method. This works because the arguments are still in their
7279 * original argument registers.
7280 * FIXME: Generalize this
7282 if (!args_clobbered) {
7283 MonoBasicBlock *first_bb = cfg->bb_entry;
7285 int filter = FILTER_IL_SEQ_POINT;
7287 next = mono_bb_first_inst (first_bb, filter);
7288 if (!next && first_bb->next_bb) {
7289 first_bb = first_bb->next_bb;
7290 next = mono_bb_first_inst (first_bb, filter);
7293 if (first_bb->in_count > 1)
7296 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7297 ArgInfo *ainfo = cinfo->args + i;
7298 gboolean match = FALSE;
7300 ins = cfg->args [i];
7301 if (ins->opcode != OP_REGVAR) {
7302 switch (ainfo->storage) {
7304 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7305 if (next->dreg == ainfo->reg) {
7309 next->opcode = OP_MOVE;
7310 next->sreg1 = ainfo->reg;
7311 /* Only continue if the instruction doesn't change argument regs */
7312 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7322 /* Argument allocated to (non-volatile) register */
7323 switch (ainfo->storage) {
7325 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7336 next = mono_inst_next (next, filter);
7337 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7344 if (cfg->gen_sdb_seq_points) {
7345 MonoInst *info_var = (MonoInst *)cfg->arch.seq_point_info_var;
7347 /* Initialize seq_point_info_var */
7348 if (cfg->compile_aot) {
7349 /* Initialize the variable from a GOT slot */
7350 /* Same as OP_AOTCONST */
7351 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7352 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7353 g_assert (info_var->opcode == OP_REGOFFSET);
7354 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7357 if (cfg->compile_aot) {
7358 /* Initialize ss_tramp_var */
7359 ins = (MonoInst *)cfg->arch.ss_tramp_var;
7360 g_assert (ins->opcode == OP_REGOFFSET);
7362 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7363 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr), 8);
7364 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7366 /* Initialize ss_tramp_var */
7367 ins = (MonoInst *)cfg->arch.ss_tramp_var;
7368 g_assert (ins->opcode == OP_REGOFFSET);
7370 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&ss_trampoline);
7371 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7373 /* Initialize bp_tramp_var */
7374 ins = (MonoInst *)cfg->arch.bp_tramp_var;
7375 g_assert (ins->opcode == OP_REGOFFSET);
7377 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&bp_trampoline);
7378 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7382 cfg->code_len = code - cfg->native_code;
7384 g_assert (cfg->code_len < cfg->code_size);
7390 mono_arch_emit_epilog (MonoCompile *cfg)
7392 MonoMethod *method = cfg->method;
7395 int max_epilog_size;
7397 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7398 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7400 max_epilog_size = get_max_epilog_size (cfg);
7402 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7403 cfg->code_size *= 2;
7404 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7405 cfg->stat_code_reallocs++;
7407 code = cfg->native_code + cfg->code_len;
7409 cfg->has_unwind_info_for_epilog = TRUE;
7411 /* Mark the start of the epilog */
7412 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7414 /* Save the uwind state which is needed by the out-of-line code */
7415 mono_emit_unwind_op_remember_state (cfg, code);
7417 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7418 code = (guint8 *)mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7420 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7422 if (method->save_lmf) {
7423 /* check if we need to restore protection of the stack after a stack overflow */
7424 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7426 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7427 /* we load the value in a separate instruction: this mechanism may be
7428 * used later as a safer way to do thread interruption
7430 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7431 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7433 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7434 /* note that the call trampoline will preserve eax/edx */
7435 x86_call_reg (code, X86_ECX);
7436 x86_patch (patch, code);
7438 /* FIXME: maybe save the jit tls in the prolog */
7440 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7441 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7445 /* Restore callee saved regs */
7446 for (i = 0; i < AMD64_NREG; ++i) {
7447 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7448 /* Restore only used_int_regs, not arch.saved_iregs */
7449 if (cfg->used_int_regs & (1 << i)) {
7450 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7451 mono_emit_unwind_op_same_value (cfg, code, i);
7452 async_exc_point (code);
7454 save_area_offset += 8;
7458 /* Load returned vtypes into registers if needed */
7459 cinfo = (CallInfo *)cfg->arch.cinfo;
7460 if (cinfo->ret.storage == ArgValuetypeInReg) {
7461 ArgInfo *ainfo = &cinfo->ret;
7462 MonoInst *inst = cfg->ret;
7464 for (quad = 0; quad < 2; quad ++) {
7465 switch (ainfo->pair_storage [quad]) {
7467 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7469 case ArgInFloatSSEReg:
7470 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7472 case ArgInDoubleSSEReg:
7473 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7478 g_assert_not_reached ();
7483 if (cfg->arch.omit_fp) {
7484 if (cfg->arch.stack_alloc_size) {
7485 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7489 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7491 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7492 async_exc_point (code);
7495 /* Restore the unwind state to be the same as before the epilog */
7496 mono_emit_unwind_op_restore_state (cfg, code);
7498 cfg->code_len = code - cfg->native_code;
7500 g_assert (cfg->code_len < cfg->code_size);
7504 mono_arch_emit_exceptions (MonoCompile *cfg)
7506 MonoJumpInfo *patch_info;
7509 MonoClass *exc_classes [16];
7510 guint8 *exc_throw_start [16], *exc_throw_end [16];
7511 guint32 code_size = 0;
7513 /* Compute needed space */
7514 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7515 if (patch_info->type == MONO_PATCH_INFO_EXC)
7517 if (patch_info->type == MONO_PATCH_INFO_R8)
7518 code_size += 8 + 15; /* sizeof (double) + alignment */
7519 if (patch_info->type == MONO_PATCH_INFO_R4)
7520 code_size += 4 + 15; /* sizeof (float) + alignment */
7521 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7522 code_size += 8 + 7; /*sizeof (void*) + alignment */
7525 #ifdef __native_client_codegen__
7526 /* Give us extra room on Native Client. This could be */
7527 /* more carefully calculated, but bundle alignment makes */
7528 /* it much trickier, so *2 like other places is good. */
7532 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7533 cfg->code_size *= 2;
7534 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7535 cfg->stat_code_reallocs++;
7538 code = cfg->native_code + cfg->code_len;
7540 /* add code to raise exceptions */
7542 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7543 switch (patch_info->type) {
7544 case MONO_PATCH_INFO_EXC: {
7545 MonoClass *exc_class;
7549 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7551 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7552 g_assert (exc_class);
7553 throw_ip = patch_info->ip.i;
7555 //x86_breakpoint (code);
7556 /* Find a throw sequence for the same exception class */
7557 for (i = 0; i < nthrows; ++i)
7558 if (exc_classes [i] == exc_class)
7561 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7562 x86_jump_code (code, exc_throw_start [i]);
7563 patch_info->type = MONO_PATCH_INFO_NONE;
7567 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7571 exc_classes [nthrows] = exc_class;
7572 exc_throw_start [nthrows] = code;
7574 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7576 patch_info->type = MONO_PATCH_INFO_NONE;
7578 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7580 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7585 exc_throw_end [nthrows] = code;
7595 g_assert(code < cfg->native_code + cfg->code_size);
7598 /* Handle relocations with RIP relative addressing */
7599 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7600 gboolean remove = FALSE;
7601 guint8 *orig_code = code;
7603 switch (patch_info->type) {
7604 case MONO_PATCH_INFO_R8:
7605 case MONO_PATCH_INFO_R4: {
7606 guint8 *pos, *patch_pos;
7609 /* The SSE opcodes require a 16 byte alignment */
7610 #if defined(__default_codegen__)
7611 code = (guint8*)ALIGN_TO (code, 16);
7612 #elif defined(__native_client_codegen__)
7614 /* Pad this out with HLT instructions */
7615 /* or we can get garbage bytes emitted */
7616 /* which will fail validation */
7617 guint8 *aligned_code;
7618 /* extra align to make room for */
7619 /* mov/push below */
7620 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7621 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7622 /* The technique of hiding data in an */
7623 /* instruction has a problem here: we */
7624 /* need the data aligned to a 16-byte */
7625 /* boundary but the instruction cannot */
7626 /* cross the bundle boundary. so only */
7627 /* odd multiples of 16 can be used */
7628 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7631 while (code < aligned_code) {
7632 *(code++) = 0xf4; /* hlt */
7637 pos = cfg->native_code + patch_info->ip.i;
7638 if (IS_REX (pos [1])) {
7639 patch_pos = pos + 5;
7640 target_pos = code - pos - 9;
7643 patch_pos = pos + 4;
7644 target_pos = code - pos - 8;
7647 if (patch_info->type == MONO_PATCH_INFO_R8) {
7648 #ifdef __native_client_codegen__
7649 /* Hide 64-bit data in a */
7650 /* "mov imm64, r11" instruction. */
7651 /* write it before the start of */
7653 *(code-2) = 0x49; /* prefix */
7654 *(code-1) = 0xbb; /* mov X, %r11 */
7656 *(double*)code = *(double*)patch_info->data.target;
7657 code += sizeof (double);
7659 #ifdef __native_client_codegen__
7660 /* Hide 32-bit data in a */
7661 /* "push imm32" instruction. */
7662 *(code-1) = 0x68; /* push */
7664 *(float*)code = *(float*)patch_info->data.target;
7665 code += sizeof (float);
7668 *(guint32*)(patch_pos) = target_pos;
7673 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7676 if (cfg->compile_aot)
7679 /*loading is faster against aligned addresses.*/
7680 code = (guint8*)ALIGN_TO (code, 8);
7681 memset (orig_code, 0, code - orig_code);
7683 pos = cfg->native_code + patch_info->ip.i;
7685 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7686 if (IS_REX (pos [1]))
7687 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7689 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7691 *(gpointer*)code = (gpointer)patch_info->data.target;
7692 code += sizeof (gpointer);
7702 if (patch_info == cfg->patch_info)
7703 cfg->patch_info = patch_info->next;
7707 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7709 tmp->next = patch_info->next;
7712 g_assert (code < cfg->native_code + cfg->code_size);
7715 cfg->code_len = code - cfg->native_code;
7717 g_assert (cfg->code_len < cfg->code_size);
7721 #endif /* DISABLE_JIT */
7724 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7726 guchar *code = (guchar *)p;
7727 MonoMethodSignature *sig;
7729 int i, n, stack_area = 0;
7731 /* Keep this in sync with mono_arch_get_argument_info */
7733 if (enable_arguments) {
7734 /* Allocate a new area on the stack and save arguments there */
7735 sig = mono_method_signature (cfg->method);
7737 n = sig->param_count + sig->hasthis;
7739 stack_area = ALIGN_TO (n * 8, 16);
7741 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7743 for (i = 0; i < n; ++i) {
7744 inst = cfg->args [i];
7746 if (inst->opcode == OP_REGVAR)
7747 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7749 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7750 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7755 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7756 amd64_set_reg_template (code, AMD64_ARG_REG1);
7757 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7758 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7760 if (enable_arguments)
7761 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7775 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7777 guchar *code = (guchar *)p;
7778 int save_mode = SAVE_NONE;
7779 MonoMethod *method = cfg->method;
7780 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
7783 switch (ret_type->type) {
7784 case MONO_TYPE_VOID:
7785 /* special case string .ctor icall */
7786 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7787 save_mode = SAVE_EAX;
7789 save_mode = SAVE_NONE;
7793 save_mode = SAVE_EAX;
7797 save_mode = SAVE_XMM;
7799 case MONO_TYPE_GENERICINST:
7800 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7801 save_mode = SAVE_EAX;
7805 case MONO_TYPE_VALUETYPE:
7806 save_mode = SAVE_STRUCT;
7809 save_mode = SAVE_EAX;
7813 /* Save the result and copy it into the proper argument register */
7814 switch (save_mode) {
7816 amd64_push_reg (code, AMD64_RAX);
7818 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7819 if (enable_arguments)
7820 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7824 if (enable_arguments)
7825 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7828 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7829 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7831 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7833 * The result is already in the proper argument register so no copying
7840 g_assert_not_reached ();
7843 /* Set %al since this is a varargs call */
7844 if (save_mode == SAVE_XMM)
7845 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7847 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7849 if (preserve_argument_registers) {
7850 for (i = 0; i < PARAM_REGS; ++i)
7851 amd64_push_reg (code, param_regs [i]);
7854 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7855 amd64_set_reg_template (code, AMD64_ARG_REG1);
7856 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7858 if (preserve_argument_registers) {
7859 for (i = PARAM_REGS - 1; i >= 0; --i)
7860 amd64_pop_reg (code, param_regs [i]);
7863 /* Restore result */
7864 switch (save_mode) {
7866 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7867 amd64_pop_reg (code, AMD64_RAX);
7873 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7874 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7875 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7880 g_assert_not_reached ();
7887 mono_arch_flush_icache (guint8 *code, gint size)
7893 mono_arch_flush_register_windows (void)
7898 mono_arch_is_inst_imm (gint64 imm)
7900 return amd64_use_imm32 (imm);
7904 * Determine whenever the trap whose info is in SIGINFO is caused by
7908 mono_arch_is_int_overflow (void *sigctx, void *info)
7915 mono_sigctx_to_monoctx (sigctx, &ctx);
7917 rip = (guint8*)ctx.gregs [AMD64_RIP];
7919 if (IS_REX (rip [0])) {
7920 reg = amd64_rex_b (rip [0]);
7926 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7928 reg += x86_modrm_rm (rip [1]);
7930 value = ctx.gregs [reg];
7940 mono_arch_get_patch_offset (guint8 *code)
7946 * mono_breakpoint_clean_code:
7948 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
7949 * breakpoints in the original code, they are removed in the copy.
7951 * Returns TRUE if no sw breakpoint was present.
7954 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
7957 * If method_start is non-NULL we need to perform bound checks, since we access memory
7958 * at code - offset we could go before the start of the method and end up in a different
7959 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
7962 if (!method_start || code - offset >= method_start) {
7963 memcpy (buf, code - offset, size);
7965 int diff = code - method_start;
7966 memset (buf, 0, size);
7967 memcpy (buf + offset - diff, method_start, diff + size - offset);
7972 #if defined(__native_client_codegen__)
7973 /* For membase calls, we want the base register. for Native Client, */
7974 /* all indirect calls have the following sequence with the given sizes: */
7975 /* mov %eXX,%eXX [2-3] */
7976 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
7977 /* and $0xffffffffffffffe0,%r11d [4] */
7978 /* add %r15,%r11 [3] */
7979 /* callq *%r11 [3] */
7982 /* Determine if code points to a NaCl call-through-register sequence, */
7983 /* (i.e., the last 3 instructions listed above) */
7985 is_nacl_call_reg_sequence(guint8* code)
7987 const char *sequence = "\x41\x83\xe3\xe0" /* and */
7988 "\x4d\x03\xdf" /* add */
7989 "\x41\xff\xd3"; /* call */
7990 return memcmp(code, sequence, 10) == 0;
7993 /* Determine if code points to the first opcode of the mov membase component */
7994 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
7995 /* (there could be a REX prefix before the opcode but it is ignored) */
7997 is_nacl_indirect_call_membase_sequence(guint8* code)
7999 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
8000 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
8001 /* and that src reg = dest reg */
8002 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
8003 /* Check that next inst is mov, uses SIB byte (rm = 4), */
8005 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
8006 /* and has dst of r11 and base of r15 */
8007 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
8008 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
8010 #endif /* __native_client_codegen__ */
8013 mono_arch_get_this_arg_reg (guint8 *code)
8015 return AMD64_ARG_REG1;
8019 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
8021 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
8024 #define MAX_ARCH_DELEGATE_PARAMS 10
8027 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, guint32 param_count)
8029 guint8 *code, *start;
8030 GSList *unwind_ops = NULL;
8033 unwind_ops = mono_arch_get_cie_program ();
8036 start = code = (guint8 *)mono_global_codeman_reserve (64);
8038 /* Replace the this argument with the target */
8039 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8040 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8041 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8043 g_assert ((code - start) < 64);
8045 start = code = (guint8 *)mono_global_codeman_reserve (64);
8047 if (param_count == 0) {
8048 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8050 /* We have to shift the arguments left */
8051 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8052 for (i = 0; i < param_count; ++i) {
8055 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8057 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
8059 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8063 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8065 g_assert ((code - start) < 64);
8068 nacl_global_codeman_validate (&start, 64, &code);
8069 mono_arch_flush_icache (start, code - start);
8072 *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
8074 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
8075 *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
8079 if (mono_jit_map_is_enabled ()) {
8082 buff = (char*)"delegate_invoke_has_target";
8084 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
8085 mono_emit_jit_tramp (start, code - start, buff);
8089 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8094 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
8097 get_delegate_virtual_invoke_impl (MonoTrampInfo **info, gboolean load_imt_reg, int offset)
8099 guint8 *code, *start;
8104 if (offset / (int)sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
8107 start = code = (guint8 *)mono_global_codeman_reserve (size);
8109 unwind_ops = mono_arch_get_cie_program ();
8111 /* Replace the this argument with the target */
8112 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8113 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8116 /* Load the IMT reg */
8117 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
8120 /* Load the vtable */
8121 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
8122 amd64_jump_membase (code, AMD64_RAX, offset);
8123 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8126 tramp_name = g_strdup_printf ("delegate_virtual_invoke_imt_%d", - offset / sizeof (gpointer));
8128 tramp_name = g_strdup_printf ("delegate_virtual_invoke_%d", offset / sizeof (gpointer));
8129 *info = mono_tramp_info_create (tramp_name, start, code - start, NULL, unwind_ops);
8130 g_free (tramp_name);
8136 * mono_arch_get_delegate_invoke_impls:
8138 * Return a list of MonoTrampInfo structures for the delegate invoke impl
8142 mono_arch_get_delegate_invoke_impls (void)
8145 MonoTrampInfo *info;
8148 get_delegate_invoke_impl (&info, TRUE, 0);
8149 res = g_slist_prepend (res, info);
8151 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
8152 get_delegate_invoke_impl (&info, FALSE, i);
8153 res = g_slist_prepend (res, info);
8156 for (i = 0; i <= MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
8157 get_delegate_virtual_invoke_impl (&info, TRUE, - i * SIZEOF_VOID_P);
8158 res = g_slist_prepend (res, info);
8160 get_delegate_virtual_invoke_impl (&info, FALSE, i * SIZEOF_VOID_P);
8161 res = g_slist_prepend (res, info);
8168 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
8170 guint8 *code, *start;
8173 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
8176 /* FIXME: Support more cases */
8177 if (MONO_TYPE_ISSTRUCT (mini_get_underlying_type (sig->ret)))
8181 static guint8* cached = NULL;
8186 if (mono_aot_only) {
8187 start = (guint8 *)mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
8189 MonoTrampInfo *info;
8190 start = (guint8 *)get_delegate_invoke_impl (&info, TRUE, 0);
8191 mono_tramp_info_register (info, NULL);
8194 mono_memory_barrier ();
8198 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
8199 for (i = 0; i < sig->param_count; ++i)
8200 if (!mono_is_regsize_var (sig->params [i]))
8202 if (sig->param_count > 4)
8205 code = cache [sig->param_count];
8209 if (mono_aot_only) {
8210 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
8211 start = (guint8 *)mono_aot_get_trampoline (name);
8214 MonoTrampInfo *info;
8215 start = (guint8 *)get_delegate_invoke_impl (&info, FALSE, sig->param_count);
8216 mono_tramp_info_register (info, NULL);
8219 mono_memory_barrier ();
8221 cache [sig->param_count] = start;
8228 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
8230 MonoTrampInfo *info;
8233 code = get_delegate_virtual_invoke_impl (&info, load_imt_reg, offset);
8235 mono_tramp_info_register (info, NULL);
8240 mono_arch_finish_init (void)
8242 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
8243 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
8248 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8252 #if defined(__default_codegen__)
8253 #define CMP_SIZE (6 + 1)
8254 #define CMP_REG_REG_SIZE (4 + 1)
8255 #define BR_SMALL_SIZE 2
8256 #define BR_LARGE_SIZE 6
8257 #define MOV_REG_IMM_SIZE 10
8258 #define MOV_REG_IMM_32BIT_SIZE 6
8259 #define JUMP_REG_SIZE (2 + 1)
8260 #elif defined(__native_client_codegen__)
8261 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8262 #define CMP_SIZE ((6 + 1) * 2 - 1)
8263 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8264 #define BR_SMALL_SIZE (2 * 2 - 1)
8265 #define BR_LARGE_SIZE (6 * 2 - 1)
8266 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8267 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8268 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8269 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8270 /* Jump membase's size is large and unpredictable */
8271 /* in native client, just pad it out a whole bundle. */
8272 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8276 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8278 int i, distance = 0;
8279 for (i = start; i < target; ++i)
8280 distance += imt_entries [i]->chunk_size;
8285 * LOCKING: called with the domain lock held
8288 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8289 gpointer fail_tramp)
8293 guint8 *code, *start;
8294 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8297 for (i = 0; i < count; ++i) {
8298 MonoIMTCheckItem *item = imt_entries [i];
8299 if (item->is_equals) {
8300 if (item->check_target_idx) {
8301 if (!item->compare_done) {
8302 if (amd64_use_imm32 ((gint64)item->key))
8303 item->chunk_size += CMP_SIZE;
8305 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8307 if (item->has_target_code) {
8308 item->chunk_size += MOV_REG_IMM_SIZE;
8310 if (vtable_is_32bit)
8311 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8313 item->chunk_size += MOV_REG_IMM_SIZE;
8314 #ifdef __native_client_codegen__
8315 item->chunk_size += JUMP_MEMBASE_SIZE;
8318 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8321 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8322 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8324 if (vtable_is_32bit)
8325 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8327 item->chunk_size += MOV_REG_IMM_SIZE;
8328 item->chunk_size += JUMP_REG_SIZE;
8329 /* with assert below:
8330 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8332 #ifdef __native_client_codegen__
8333 item->chunk_size += JUMP_MEMBASE_SIZE;
8338 if (amd64_use_imm32 ((gint64)item->key))
8339 item->chunk_size += CMP_SIZE;
8341 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8342 item->chunk_size += BR_LARGE_SIZE;
8343 imt_entries [item->check_target_idx]->compare_done = TRUE;
8345 size += item->chunk_size;
8347 #if defined(__native_client__) && defined(__native_client_codegen__)
8348 /* In Native Client, we don't re-use thunks, allocate from the */
8349 /* normal code manager paths. */
8350 code = mono_domain_code_reserve (domain, size);
8353 code = (guint8 *)mono_method_alloc_generic_virtual_thunk (domain, size);
8355 code = (guint8 *)mono_domain_code_reserve (domain, size);
8359 unwind_ops = mono_arch_get_cie_program ();
8361 for (i = 0; i < count; ++i) {
8362 MonoIMTCheckItem *item = imt_entries [i];
8363 item->code_target = code;
8364 if (item->is_equals) {
8365 gboolean fail_case = !item->check_target_idx && fail_tramp;
8367 if (item->check_target_idx || fail_case) {
8368 if (!item->compare_done || fail_case) {
8369 if (amd64_use_imm32 ((gint64)item->key))
8370 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8372 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof(gpointer));
8373 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8376 item->jmp_code = code;
8377 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8378 if (item->has_target_code) {
8379 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8380 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8382 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8383 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8387 amd64_patch (item->jmp_code, code);
8388 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8389 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8390 item->jmp_code = NULL;
8393 /* enable the commented code to assert on wrong method */
8395 if (amd64_is_imm32 (item->key))
8396 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8398 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8399 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8401 item->jmp_code = code;
8402 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8403 /* See the comment below about R10 */
8404 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8405 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8406 amd64_patch (item->jmp_code, code);
8407 amd64_breakpoint (code);
8408 item->jmp_code = NULL;
8410 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8411 needs to be preserved. R10 needs
8412 to be preserved for calls which
8413 require a runtime generic context,
8414 but interface calls don't. */
8415 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8416 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8420 if (amd64_use_imm32 ((gint64)item->key))
8421 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8423 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof (gpointer));
8424 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8426 item->jmp_code = code;
8427 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8428 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8430 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8432 g_assert (code - item->code_target <= item->chunk_size);
8434 /* patch the branches to get to the target items */
8435 for (i = 0; i < count; ++i) {
8436 MonoIMTCheckItem *item = imt_entries [i];
8437 if (item->jmp_code) {
8438 if (item->check_target_idx) {
8439 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8445 mono_stats.imt_thunks_size += code - start;
8446 g_assert (code - start <= size);
8448 nacl_domain_code_validate(domain, &start, size, &code);
8449 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8451 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
8457 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8459 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8463 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8465 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8469 mono_arch_get_cie_program (void)
8473 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8474 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8482 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8484 MonoInst *ins = NULL;
8487 if (cmethod->klass == mono_defaults.math_class) {
8488 if (strcmp (cmethod->name, "Sin") == 0) {
8490 } else if (strcmp (cmethod->name, "Cos") == 0) {
8492 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8494 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8498 if (opcode && fsig->param_count == 1) {
8499 MONO_INST_NEW (cfg, ins, opcode);
8500 ins->type = STACK_R8;
8501 ins->dreg = mono_alloc_freg (cfg);
8502 ins->sreg1 = args [0]->dreg;
8503 MONO_ADD_INS (cfg->cbb, ins);
8507 if (cfg->opt & MONO_OPT_CMOV) {
8508 if (strcmp (cmethod->name, "Min") == 0) {
8509 if (fsig->params [0]->type == MONO_TYPE_I4)
8511 if (fsig->params [0]->type == MONO_TYPE_U4)
8512 opcode = OP_IMIN_UN;
8513 else if (fsig->params [0]->type == MONO_TYPE_I8)
8515 else if (fsig->params [0]->type == MONO_TYPE_U8)
8516 opcode = OP_LMIN_UN;
8517 } else if (strcmp (cmethod->name, "Max") == 0) {
8518 if (fsig->params [0]->type == MONO_TYPE_I4)
8520 if (fsig->params [0]->type == MONO_TYPE_U4)
8521 opcode = OP_IMAX_UN;
8522 else if (fsig->params [0]->type == MONO_TYPE_I8)
8524 else if (fsig->params [0]->type == MONO_TYPE_U8)
8525 opcode = OP_LMAX_UN;
8529 if (opcode && fsig->param_count == 2) {
8530 MONO_INST_NEW (cfg, ins, opcode);
8531 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8532 ins->dreg = mono_alloc_ireg (cfg);
8533 ins->sreg1 = args [0]->dreg;
8534 ins->sreg2 = args [1]->dreg;
8535 MONO_ADD_INS (cfg->cbb, ins);
8539 /* OP_FREM is not IEEE compatible */
8540 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8541 MONO_INST_NEW (cfg, ins, OP_FREM);
8542 ins->inst_i0 = args [0];
8543 ins->inst_i1 = args [1];
8553 mono_arch_print_tree (MonoInst *tree, int arity)
8559 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8561 return ctx->gregs [reg];
8565 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8567 ctx->gregs [reg] = val;
8571 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8573 gpointer *sp, old_value;
8577 bp = (char *)MONO_CONTEXT_GET_BP (ctx);
8578 sp = (gpointer *)*(gpointer*)(bp + clause->exvar_offset);
8581 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8590 * mono_arch_emit_load_aotconst:
8592 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8593 * TARGET from the mscorlib GOT in full-aot code.
8594 * On AMD64, the result is placed into R11.
8597 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, MonoJumpInfoType tramp_type, gconstpointer target)
8599 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8600 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8606 * mono_arch_get_trampolines:
8608 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8612 mono_arch_get_trampolines (gboolean aot)
8614 return mono_amd64_get_exception_trampolines (aot);
8617 /* Soft Debug support */
8618 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8621 * mono_arch_set_breakpoint:
8623 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8624 * The location should contain code emitted by OP_SEQ_POINT.
8627 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8632 guint32 native_offset = ip - (guint8*)ji->code_start;
8633 SeqPointInfo *info = (SeqPointInfo *)mono_arch_get_seq_point_info (mono_domain_get (), (guint8 *)ji->code_start);
8635 g_assert (info->bp_addrs [native_offset] == 0);
8636 info->bp_addrs [native_offset] = mini_get_breakpoint_trampoline ();
8638 /* ip points to a mov r11, 0 */
8639 g_assert (code [0] == 0x41);
8640 g_assert (code [1] == 0xbb);
8641 amd64_mov_reg_imm (code, AMD64_R11, 1);
8646 * mono_arch_clear_breakpoint:
8648 * Clear the breakpoint at IP.
8651 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8656 guint32 native_offset = ip - (guint8*)ji->code_start;
8657 SeqPointInfo *info = (SeqPointInfo *)mono_arch_get_seq_point_info (mono_domain_get (), (guint8 *)ji->code_start);
8659 info->bp_addrs [native_offset] = NULL;
8661 amd64_mov_reg_imm (code, AMD64_R11, 0);
8666 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8668 /* We use soft breakpoints on amd64 */
8673 * mono_arch_skip_breakpoint:
8675 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8676 * we resume, the instruction is not executed again.
8679 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8681 g_assert_not_reached ();
8685 * mono_arch_start_single_stepping:
8687 * Start single stepping.
8690 mono_arch_start_single_stepping (void)
8692 ss_trampoline = mini_get_single_step_trampoline ();
8696 * mono_arch_stop_single_stepping:
8698 * Stop single stepping.
8701 mono_arch_stop_single_stepping (void)
8703 ss_trampoline = NULL;
8707 * mono_arch_is_single_step_event:
8709 * Return whenever the machine state in SIGCTX corresponds to a single
8713 mono_arch_is_single_step_event (void *info, void *sigctx)
8715 /* We use soft breakpoints on amd64 */
8720 * mono_arch_skip_single_step:
8722 * Modify CTX so the ip is placed after the single step trigger instruction,
8723 * we resume, the instruction is not executed again.
8726 mono_arch_skip_single_step (MonoContext *ctx)
8728 g_assert_not_reached ();
8732 * mono_arch_create_seq_point_info:
8734 * Return a pointer to a data structure which is used by the sequence
8735 * point implementation in AOTed code.
8738 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8743 // FIXME: Add a free function
8745 mono_domain_lock (domain);
8746 info = (SeqPointInfo *)g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8748 mono_domain_unlock (domain);
8751 ji = mono_jit_info_table_find (domain, (char*)code);
8754 // FIXME: Optimize the size
8755 info = (SeqPointInfo *)g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8757 info->ss_tramp_addr = &ss_trampoline;
8759 mono_domain_lock (domain);
8760 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8762 mono_domain_unlock (domain);
8769 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8771 ext->lmf.previous_lmf = prev_lmf;
8772 /* Mark that this is a MonoLMFExt */
8773 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8774 ext->lmf.rsp = (gssize)ext;
8780 mono_arch_opcode_supported (int opcode)
8783 case OP_ATOMIC_ADD_I4:
8784 case OP_ATOMIC_ADD_I8:
8785 case OP_ATOMIC_EXCHANGE_I4:
8786 case OP_ATOMIC_EXCHANGE_I8:
8787 case OP_ATOMIC_CAS_I4:
8788 case OP_ATOMIC_CAS_I8:
8789 case OP_ATOMIC_LOAD_I1:
8790 case OP_ATOMIC_LOAD_I2:
8791 case OP_ATOMIC_LOAD_I4:
8792 case OP_ATOMIC_LOAD_I8:
8793 case OP_ATOMIC_LOAD_U1:
8794 case OP_ATOMIC_LOAD_U2:
8795 case OP_ATOMIC_LOAD_U4:
8796 case OP_ATOMIC_LOAD_U8:
8797 case OP_ATOMIC_LOAD_R4:
8798 case OP_ATOMIC_LOAD_R8:
8799 case OP_ATOMIC_STORE_I1:
8800 case OP_ATOMIC_STORE_I2:
8801 case OP_ATOMIC_STORE_I4:
8802 case OP_ATOMIC_STORE_I8:
8803 case OP_ATOMIC_STORE_U1:
8804 case OP_ATOMIC_STORE_U2:
8805 case OP_ATOMIC_STORE_U4:
8806 case OP_ATOMIC_STORE_U8:
8807 case OP_ATOMIC_STORE_R4:
8808 case OP_ATOMIC_STORE_R8:
8815 #if defined(ENABLE_GSHAREDVT) && defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
8817 #include "../../../mono-extensions/mono/mini/mini-amd64-gsharedvt.c"
8819 #endif /* !MONOTOUCH */