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-internal.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_mutex_lock (&mini_arch_mutex)
65 #define mono_mini_arch_unlock() mono_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 = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1147 cinfo = 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_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_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_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 = 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 = 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 = 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 = 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:
2253 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2254 cfg->disable_llvm = TRUE;
2258 linfo->ret.storage = LLVMArgVtypeInReg;
2259 for (j = 0; j < 2; ++j)
2260 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
2262 case ArgValuetypeAddrInIReg:
2263 /* Vtype returned using a hidden argument */
2264 linfo->ret.storage = LLVMArgVtypeRetAddr;
2265 linfo->vret_arg_index = cinfo->vret_arg_index;
2268 g_assert_not_reached ();
2272 for (i = 0; i < n; ++i) {
2273 ainfo = cinfo->args + i;
2275 if (i >= sig->hasthis)
2276 t = sig->params [i - sig->hasthis];
2278 t = &mono_defaults.int_class->byval_arg;
2280 linfo->args [i].storage = LLVMArgNone;
2282 switch (ainfo->storage) {
2284 linfo->args [i].storage = LLVMArgInIReg;
2286 case ArgInDoubleSSEReg:
2287 case ArgInFloatSSEReg:
2288 linfo->args [i].storage = LLVMArgInFPReg;
2291 if (MONO_TYPE_ISSTRUCT (t)) {
2292 linfo->args [i].storage = LLVMArgVtypeByVal;
2294 linfo->args [i].storage = LLVMArgInIReg;
2296 if (t->type == MONO_TYPE_R4)
2297 linfo->args [i].storage = LLVMArgInFPReg;
2298 else if (t->type == MONO_TYPE_R8)
2299 linfo->args [i].storage = LLVMArgInFPReg;
2303 case ArgValuetypeInReg:
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;
2786 g_assert (buf_len >= sizeof (DynCallArgs));
2795 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2796 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2801 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg)
2802 p->regs [greg ++] = PTR_TO_GREG(ret);
2804 for (i = pindex; i < sig->param_count; i++) {
2805 MonoType *t = mini_get_underlying_type (sig->params [i]);
2806 gpointer *arg = args [arg_index ++];
2809 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2814 case MONO_TYPE_STRING:
2815 case MONO_TYPE_CLASS:
2816 case MONO_TYPE_ARRAY:
2817 case MONO_TYPE_SZARRAY:
2818 case MONO_TYPE_OBJECT:
2822 #if !defined(__mono_ilp32__)
2826 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2827 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2829 #if defined(__mono_ilp32__)
2832 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2833 p->regs [greg ++] = *(guint64*)(arg);
2837 p->regs [greg ++] = *(guint8*)(arg);
2840 p->regs [greg ++] = *(gint8*)(arg);
2843 p->regs [greg ++] = *(gint16*)(arg);
2846 p->regs [greg ++] = *(guint16*)(arg);
2849 p->regs [greg ++] = *(gint32*)(arg);
2852 p->regs [greg ++] = *(guint32*)(arg);
2854 case MONO_TYPE_GENERICINST:
2855 if (MONO_TYPE_IS_REFERENCE (t)) {
2856 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2861 case MONO_TYPE_VALUETYPE: {
2862 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2864 g_assert (ainfo->storage == ArgValuetypeInReg);
2865 if (ainfo->pair_storage [0] != ArgNone) {
2866 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2867 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2869 if (ainfo->pair_storage [1] != ArgNone) {
2870 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2871 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2876 g_assert_not_reached ();
2880 g_assert (greg <= PARAM_REGS);
2884 * mono_arch_finish_dyn_call:
2886 * Store the result of a dyn call into the return value buffer passed to
2887 * start_dyn_call ().
2888 * This function should be as fast as possible, any work which does not depend
2889 * on the actual values of the arguments should be done in
2890 * mono_arch_dyn_call_prepare ().
2893 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2895 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2896 MonoMethodSignature *sig = dinfo->sig;
2897 guint8 *ret = ((DynCallArgs*)buf)->ret;
2898 mgreg_t res = ((DynCallArgs*)buf)->res;
2899 MonoType *sig_ret = mini_get_underlying_type (sig->ret);
2901 switch (sig_ret->type) {
2902 case MONO_TYPE_VOID:
2903 *(gpointer*)ret = NULL;
2905 case MONO_TYPE_STRING:
2906 case MONO_TYPE_CLASS:
2907 case MONO_TYPE_ARRAY:
2908 case MONO_TYPE_SZARRAY:
2909 case MONO_TYPE_OBJECT:
2913 *(gpointer*)ret = GREG_TO_PTR(res);
2919 *(guint8*)ret = res;
2922 *(gint16*)ret = res;
2925 *(guint16*)ret = res;
2928 *(gint32*)ret = res;
2931 *(guint32*)ret = res;
2934 *(gint64*)ret = res;
2937 *(guint64*)ret = res;
2939 case MONO_TYPE_GENERICINST:
2940 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
2941 *(gpointer*)ret = GREG_TO_PTR(res);
2946 case MONO_TYPE_VALUETYPE:
2947 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg) {
2950 ArgInfo *ainfo = &dinfo->cinfo->ret;
2952 g_assert (ainfo->storage == ArgValuetypeInReg);
2954 if (ainfo->pair_storage [0] != ArgNone) {
2955 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2956 ((mgreg_t*)ret)[0] = res;
2959 g_assert (ainfo->pair_storage [1] == ArgNone);
2963 g_assert_not_reached ();
2967 /* emit an exception if condition is fail */
2968 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2970 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2971 if (tins == NULL) { \
2972 mono_add_patch_info (cfg, code - cfg->native_code, \
2973 MONO_PATCH_INFO_EXC, exc_name); \
2974 x86_branch32 (code, cond, 0, signed); \
2976 EMIT_COND_BRANCH (tins, cond, signed); \
2980 #define EMIT_FPCOMPARE(code) do { \
2981 amd64_fcompp (code); \
2982 amd64_fnstsw (code); \
2985 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
2986 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
2987 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
2988 amd64_ ##op (code); \
2989 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
2990 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
2994 emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
2996 gboolean no_patch = FALSE;
2999 * FIXME: Add support for thunks
3002 gboolean near_call = FALSE;
3005 * Indirect calls are expensive so try to make a near call if possible.
3006 * The caller memory is allocated by the code manager so it is
3007 * guaranteed to be at a 32 bit offset.
3010 if (patch_type != MONO_PATCH_INFO_ABS) {
3011 /* The target is in memory allocated using the code manager */
3014 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
3015 if (((MonoMethod*)data)->klass->image->aot_module)
3016 /* The callee might be an AOT method */
3018 if (((MonoMethod*)data)->dynamic)
3019 /* The target is in malloc-ed memory */
3023 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
3025 * The call might go directly to a native function without
3028 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (data);
3030 gconstpointer target = mono_icall_get_wrapper (mi);
3031 if ((((guint64)target) >> 32) != 0)
3037 MonoJumpInfo *jinfo = NULL;
3039 if (cfg->abs_patches)
3040 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
3042 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3043 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
3044 if (mi && (((guint64)mi->func) >> 32) == 0)
3049 * This is not really an optimization, but required because the
3050 * generic class init trampolines use R11 to pass the vtable.
3055 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
3057 if (info->func == info->wrapper) {
3059 if ((((guint64)info->func) >> 32) == 0)
3063 /* See the comment in mono_codegen () */
3064 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
3068 else if ((((guint64)data) >> 32) == 0) {
3075 if (cfg->method->dynamic)
3076 /* These methods are allocated using malloc */
3079 #ifdef MONO_ARCH_NOMAP32BIT
3082 #if defined(__native_client__)
3083 /* Always use near_call == TRUE for Native Client */
3086 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
3087 if (optimize_for_xen)
3090 if (cfg->compile_aot) {
3097 * Align the call displacement to an address divisible by 4 so it does
3098 * not span cache lines. This is required for code patching to work on SMP
3101 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
3102 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
3103 amd64_padding (code, pad_size);
3105 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3106 amd64_call_code (code, 0);
3109 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3110 amd64_set_reg_template (code, GP_SCRATCH_REG);
3111 amd64_call_reg (code, GP_SCRATCH_REG);
3118 static inline guint8*
3119 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
3122 if (win64_adjust_stack)
3123 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
3125 code = emit_call_body (cfg, code, patch_type, data);
3127 if (win64_adjust_stack)
3128 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
3135 store_membase_imm_to_store_membase_reg (int opcode)
3138 case OP_STORE_MEMBASE_IMM:
3139 return OP_STORE_MEMBASE_REG;
3140 case OP_STOREI4_MEMBASE_IMM:
3141 return OP_STOREI4_MEMBASE_REG;
3142 case OP_STOREI8_MEMBASE_IMM:
3143 return OP_STOREI8_MEMBASE_REG;
3151 #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)))
3154 * mono_arch_peephole_pass_1:
3156 * Perform peephole opts which should/can be performed before local regalloc
3159 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3163 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3164 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3166 switch (ins->opcode) {
3170 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
3172 * X86_LEA is like ADD, but doesn't have the
3173 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
3174 * its operand to 64 bit.
3176 ins->opcode = OP_X86_LEA_MEMBASE;
3177 ins->inst_basereg = ins->sreg1;
3182 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3186 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3187 * the latter has length 2-3 instead of 6 (reverse constant
3188 * propagation). These instruction sequences are very common
3189 * in the initlocals bblock.
3191 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3192 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)) {
3193 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3194 ins2->sreg1 = ins->dreg;
3195 } 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)) {
3197 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3200 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3208 case OP_COMPARE_IMM:
3209 case OP_LCOMPARE_IMM:
3210 /* OP_COMPARE_IMM (reg, 0)
3212 * OP_AMD64_TEST_NULL (reg)
3215 ins->opcode = OP_AMD64_TEST_NULL;
3217 case OP_ICOMPARE_IMM:
3219 ins->opcode = OP_X86_TEST_NULL;
3221 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3223 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3224 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3226 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3227 * OP_COMPARE_IMM reg, imm
3229 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3231 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3232 ins->inst_basereg == last_ins->inst_destbasereg &&
3233 ins->inst_offset == last_ins->inst_offset) {
3234 ins->opcode = OP_ICOMPARE_IMM;
3235 ins->sreg1 = last_ins->sreg1;
3237 /* check if we can remove cmp reg,0 with test null */
3239 ins->opcode = OP_X86_TEST_NULL;
3245 mono_peephole_ins (bb, ins);
3250 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3254 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3255 switch (ins->opcode) {
3258 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3259 /* reg = 0 -> XOR (reg, reg) */
3260 /* XOR sets cflags on x86, so we cant do it always */
3261 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3262 ins->opcode = OP_LXOR;
3263 ins->sreg1 = ins->dreg;
3264 ins->sreg2 = ins->dreg;
3272 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3273 * 0 result into 64 bits.
3275 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3276 ins->opcode = OP_IXOR;
3280 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3284 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3285 * the latter has length 2-3 instead of 6 (reverse constant
3286 * propagation). These instruction sequences are very common
3287 * in the initlocals bblock.
3289 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3290 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)) {
3291 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3292 ins2->sreg1 = ins->dreg;
3293 } 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)) {
3295 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3298 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3307 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3308 ins->opcode = OP_X86_INC_REG;
3311 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3312 ins->opcode = OP_X86_DEC_REG;
3316 mono_peephole_ins (bb, ins);
3320 #define NEW_INS(cfg,ins,dest,op) do { \
3321 MONO_INST_NEW ((cfg), (dest), (op)); \
3322 (dest)->cil_code = (ins)->cil_code; \
3323 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3327 * mono_arch_lowering_pass:
3329 * Converts complex opcodes into simpler ones so that each IR instruction
3330 * corresponds to one machine instruction.
3333 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3335 MonoInst *ins, *n, *temp;
3338 * FIXME: Need to add more instructions, but the current machine
3339 * description can't model some parts of the composite instructions like
3342 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3343 switch (ins->opcode) {
3347 case OP_IDIV_UN_IMM:
3348 case OP_IREM_UN_IMM:
3351 mono_decompose_op_imm (cfg, bb, ins);
3353 case OP_COMPARE_IMM:
3354 case OP_LCOMPARE_IMM:
3355 if (!amd64_use_imm32 (ins->inst_imm)) {
3356 NEW_INS (cfg, ins, temp, OP_I8CONST);
3357 temp->inst_c0 = ins->inst_imm;
3358 temp->dreg = mono_alloc_ireg (cfg);
3359 ins->opcode = OP_COMPARE;
3360 ins->sreg2 = temp->dreg;
3363 #ifndef __mono_ilp32__
3364 case OP_LOAD_MEMBASE:
3366 case OP_LOADI8_MEMBASE:
3367 #ifndef __native_client_codegen__
3368 /* Don't generate memindex opcodes (to simplify */
3369 /* read sandboxing) */
3370 if (!amd64_use_imm32 (ins->inst_offset)) {
3371 NEW_INS (cfg, ins, temp, OP_I8CONST);
3372 temp->inst_c0 = ins->inst_offset;
3373 temp->dreg = mono_alloc_ireg (cfg);
3374 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3375 ins->inst_indexreg = temp->dreg;
3379 #ifndef __mono_ilp32__
3380 case OP_STORE_MEMBASE_IMM:
3382 case OP_STOREI8_MEMBASE_IMM:
3383 if (!amd64_use_imm32 (ins->inst_imm)) {
3384 NEW_INS (cfg, ins, temp, OP_I8CONST);
3385 temp->inst_c0 = ins->inst_imm;
3386 temp->dreg = mono_alloc_ireg (cfg);
3387 ins->opcode = OP_STOREI8_MEMBASE_REG;
3388 ins->sreg1 = temp->dreg;
3391 #ifdef MONO_ARCH_SIMD_INTRINSICS
3392 case OP_EXPAND_I1: {
3393 int temp_reg1 = mono_alloc_ireg (cfg);
3394 int temp_reg2 = mono_alloc_ireg (cfg);
3395 int original_reg = ins->sreg1;
3397 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3398 temp->sreg1 = original_reg;
3399 temp->dreg = temp_reg1;
3401 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3402 temp->sreg1 = temp_reg1;
3403 temp->dreg = temp_reg2;
3406 NEW_INS (cfg, ins, temp, OP_LOR);
3407 temp->sreg1 = temp->dreg = temp_reg2;
3408 temp->sreg2 = temp_reg1;
3410 ins->opcode = OP_EXPAND_I2;
3411 ins->sreg1 = temp_reg2;
3420 bb->max_vreg = cfg->next_vreg;
3424 branch_cc_table [] = {
3425 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3426 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3427 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3430 /* Maps CMP_... constants to X86_CC_... constants */
3433 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3434 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3438 cc_signed_table [] = {
3439 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3440 FALSE, FALSE, FALSE, FALSE
3443 /*#include "cprop.c"*/
3445 static unsigned char*
3446 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3449 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3451 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3454 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3456 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3460 static unsigned char*
3461 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3463 int sreg = tree->sreg1;
3464 int need_touch = FALSE;
3466 #if defined(TARGET_WIN32)
3468 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3469 if (!tree->flags & MONO_INST_INIT)
3478 * If requested stack size is larger than one page,
3479 * perform stack-touch operation
3482 * Generate stack probe code.
3483 * Under Windows, it is necessary to allocate one page at a time,
3484 * "touching" stack after each successful sub-allocation. This is
3485 * because of the way stack growth is implemented - there is a
3486 * guard page before the lowest stack page that is currently commited.
3487 * Stack normally grows sequentially so OS traps access to the
3488 * guard page and commits more pages when needed.
3490 amd64_test_reg_imm (code, sreg, ~0xFFF);
3491 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3493 br[2] = code; /* loop */
3494 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3495 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3496 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3497 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3498 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3499 amd64_patch (br[3], br[2]);
3500 amd64_test_reg_reg (code, sreg, sreg);
3501 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3502 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3504 br[1] = code; x86_jump8 (code, 0);
3506 amd64_patch (br[0], code);
3507 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3508 amd64_patch (br[1], code);
3509 amd64_patch (br[4], code);
3512 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3514 if (tree->flags & MONO_INST_INIT) {
3516 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3517 amd64_push_reg (code, AMD64_RAX);
3520 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3521 amd64_push_reg (code, AMD64_RCX);
3524 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3525 amd64_push_reg (code, AMD64_RDI);
3529 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3530 if (sreg != AMD64_RCX)
3531 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3532 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3534 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3535 if (cfg->param_area)
3536 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3538 #if defined(__default_codegen__)
3539 amd64_prefix (code, X86_REP_PREFIX);
3541 #elif defined(__native_client_codegen__)
3542 /* NaCl stos pseudo-instruction */
3543 amd64_codegen_pre(code);
3544 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3545 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3546 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3547 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3548 amd64_prefix (code, X86_REP_PREFIX);
3550 amd64_codegen_post(code);
3551 #endif /* __native_client_codegen__ */
3553 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3554 amd64_pop_reg (code, AMD64_RDI);
3555 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3556 amd64_pop_reg (code, AMD64_RCX);
3557 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3558 amd64_pop_reg (code, AMD64_RAX);
3564 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3569 /* Move return value to the target register */
3570 /* FIXME: do this in the local reg allocator */
3571 switch (ins->opcode) {
3574 case OP_CALL_MEMBASE:
3577 case OP_LCALL_MEMBASE:
3578 g_assert (ins->dreg == AMD64_RAX);
3582 case OP_FCALL_MEMBASE: {
3583 MonoType *rtype = mini_get_underlying_type (((MonoCallInst*)ins)->signature->ret);
3584 if (rtype->type == MONO_TYPE_R4) {
3585 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3588 if (ins->dreg != AMD64_XMM0)
3589 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3595 case OP_RCALL_MEMBASE:
3596 if (ins->dreg != AMD64_XMM0)
3597 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3601 case OP_VCALL_MEMBASE:
3604 case OP_VCALL2_MEMBASE:
3605 cinfo = get_call_info (cfg->mempool, ((MonoCallInst*)ins)->signature);
3606 if (cinfo->ret.storage == ArgValuetypeInReg) {
3607 MonoInst *loc = cfg->arch.vret_addr_loc;
3609 /* Load the destination address */
3610 g_assert (loc->opcode == OP_REGOFFSET);
3611 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3613 for (quad = 0; quad < 2; quad ++) {
3614 switch (cinfo->ret.pair_storage [quad]) {
3616 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3618 case ArgInFloatSSEReg:
3619 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3621 case ArgInDoubleSSEReg:
3622 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3637 #endif /* DISABLE_JIT */
3640 static int tls_gs_offset;
3644 mono_amd64_have_tls_get (void)
3647 static gboolean have_tls_get = FALSE;
3648 static gboolean inited = FALSE;
3651 return have_tls_get;
3653 #if MONO_HAVE_FAST_TLS
3654 guint8 *ins = (guint8*)pthread_getspecific;
3657 * We're looking for these two instructions:
3659 * mov %gs:[offset](,%rdi,8),%rax
3662 have_tls_get = ins [0] == 0x65 &&
3672 tls_gs_offset = ins[5];
3675 * Apple now loads a different version of pthread_getspecific when launched from Xcode
3676 * For that version we're looking for these instructions:
3680 * mov %gs:[offset](,%rdi,8),%rax
3684 if (!have_tls_get) {
3685 have_tls_get = ins [0] == 0x55 &&
3700 tls_gs_offset = ins[9];
3706 return have_tls_get;
3707 #elif defined(TARGET_ANDROID)
3715 mono_amd64_get_tls_gs_offset (void)
3718 return tls_gs_offset;
3720 g_assert_not_reached ();
3726 * mono_amd64_emit_tls_get:
3727 * @code: buffer to store code to
3728 * @dreg: hard register where to place the result
3729 * @tls_offset: offset info
3731 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3732 * the dreg register the item in the thread local storage identified
3735 * Returns: a pointer to the end of the stored code
3738 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3741 if (tls_offset < 64) {
3742 x86_prefix (code, X86_GS_PREFIX);
3743 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3747 g_assert (tls_offset < 0x440);
3748 /* Load TEB->TlsExpansionSlots */
3749 x86_prefix (code, X86_GS_PREFIX);
3750 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3751 amd64_test_reg_reg (code, dreg, dreg);
3753 amd64_branch (code, X86_CC_EQ, code, TRUE);
3754 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3755 amd64_patch (buf [0], code);
3757 #elif defined(__APPLE__)
3758 x86_prefix (code, X86_GS_PREFIX);
3759 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3761 if (optimize_for_xen) {
3762 x86_prefix (code, X86_FS_PREFIX);
3763 amd64_mov_reg_mem (code, dreg, 0, 8);
3764 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3766 x86_prefix (code, X86_FS_PREFIX);
3767 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3774 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3776 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3778 if (dreg != offset_reg)
3779 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3780 amd64_prefix (code, X86_GS_PREFIX);
3781 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3782 #elif defined(__linux__)
3785 if (dreg == offset_reg) {
3786 /* Use a temporary reg by saving it to the redzone */
3787 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3788 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3789 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3790 offset_reg = tmpreg;
3792 x86_prefix (code, X86_FS_PREFIX);
3793 amd64_mov_reg_mem (code, dreg, 0, 8);
3794 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3796 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3798 g_assert_not_reached ();
3804 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3807 g_assert_not_reached ();
3808 #elif defined(__APPLE__)
3809 x86_prefix (code, X86_GS_PREFIX);
3810 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3812 g_assert (!optimize_for_xen);
3813 x86_prefix (code, X86_FS_PREFIX);
3814 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3820 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3822 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3824 g_assert_not_reached ();
3825 #elif defined(__APPLE__)
3826 x86_prefix (code, X86_GS_PREFIX);
3827 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3829 x86_prefix (code, X86_FS_PREFIX);
3830 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3836 * mono_arch_translate_tls_offset:
3838 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3841 mono_arch_translate_tls_offset (int offset)
3844 return tls_gs_offset + (offset * 8);
3853 * Emit code to initialize an LMF structure at LMF_OFFSET.
3856 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3859 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3862 * sp is saved right before calls but we need to save it here too so
3863 * async stack walks would work.
3865 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3867 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3868 if (cfg->arch.omit_fp && cfa_offset != -1)
3869 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3871 /* These can't contain refs */
3872 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3873 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3874 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3875 /* These are handled automatically by the stack marking code */
3876 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3881 #define REAL_PRINT_REG(text,reg) \
3882 mono_assert (reg >= 0); \
3883 amd64_push_reg (code, AMD64_RAX); \
3884 amd64_push_reg (code, AMD64_RDX); \
3885 amd64_push_reg (code, AMD64_RCX); \
3886 amd64_push_reg (code, reg); \
3887 amd64_push_imm (code, reg); \
3888 amd64_push_imm (code, text " %d %p\n"); \
3889 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
3890 amd64_call_reg (code, AMD64_RAX); \
3891 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
3892 amd64_pop_reg (code, AMD64_RCX); \
3893 amd64_pop_reg (code, AMD64_RDX); \
3894 amd64_pop_reg (code, AMD64_RAX);
3896 /* benchmark and set based on cpu */
3897 #define LOOP_ALIGNMENT 8
3898 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3902 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3907 guint8 *code = cfg->native_code + cfg->code_len;
3910 /* Fix max_offset estimate for each successor bb */
3911 if (cfg->opt & MONO_OPT_BRANCH) {
3912 int current_offset = cfg->code_len;
3913 MonoBasicBlock *current_bb;
3914 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3915 current_bb->max_offset = current_offset;
3916 current_offset += current_bb->max_length;
3920 if (cfg->opt & MONO_OPT_LOOP) {
3921 int pad, align = LOOP_ALIGNMENT;
3922 /* set alignment depending on cpu */
3923 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3925 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3926 amd64_padding (code, pad);
3927 cfg->code_len += pad;
3928 bb->native_offset = cfg->code_len;
3932 #if defined(__native_client_codegen__)
3933 /* For Native Client, all indirect call/jump targets must be */
3934 /* 32-byte aligned. Exception handler blocks are jumped to */
3935 /* indirectly as well. */
3936 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
3937 (bb->flags & BB_EXCEPTION_HANDLER);
3939 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
3940 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
3941 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
3942 cfg->code_len += pad;
3943 bb->native_offset = cfg->code_len;
3945 #endif /*__native_client_codegen__*/
3947 if (cfg->verbose_level > 2)
3948 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3950 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
3951 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3952 g_assert (!cfg->compile_aot);
3954 cov->data [bb->dfn].cil_code = bb->cil_code;
3955 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3956 /* this is not thread save, but good enough */
3957 amd64_inc_membase (code, AMD64_R11, 0);
3960 offset = code - cfg->native_code;
3962 mono_debug_open_block (cfg, bb, offset);
3964 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)
3965 x86_breakpoint (code);
3967 MONO_BB_FOR_EACH_INS (bb, ins) {
3968 offset = code - cfg->native_code;
3970 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3972 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
3974 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
3975 cfg->code_size *= 2;
3976 cfg->native_code = mono_realloc_native_code(cfg);
3977 code = cfg->native_code + offset;
3978 cfg->stat_code_reallocs++;
3981 if (cfg->debug_info)
3982 mono_debug_record_line_number (cfg, ins, offset);
3984 switch (ins->opcode) {
3986 amd64_mul_reg (code, ins->sreg2, TRUE);
3989 amd64_mul_reg (code, ins->sreg2, FALSE);
3991 case OP_X86_SETEQ_MEMBASE:
3992 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3994 case OP_STOREI1_MEMBASE_IMM:
3995 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3997 case OP_STOREI2_MEMBASE_IMM:
3998 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
4000 case OP_STOREI4_MEMBASE_IMM:
4001 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
4003 case OP_STOREI1_MEMBASE_REG:
4004 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
4006 case OP_STOREI2_MEMBASE_REG:
4007 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
4009 /* In AMD64 NaCl, pointers are 4 bytes, */
4010 /* so STORE_* != STOREI8_*. Likewise below. */
4011 case OP_STORE_MEMBASE_REG:
4012 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
4014 case OP_STOREI8_MEMBASE_REG:
4015 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
4017 case OP_STOREI4_MEMBASE_REG:
4018 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
4020 case OP_STORE_MEMBASE_IMM:
4021 #ifndef __native_client_codegen__
4022 /* In NaCl, this could be a PCONST type, which could */
4023 /* mean a pointer type was copied directly into the */
4024 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
4025 /* the value would be 0x00000000FFFFFFFF which is */
4026 /* not proper for an imm32 unless you cast it. */
4027 g_assert (amd64_is_imm32 (ins->inst_imm));
4029 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
4031 case OP_STOREI8_MEMBASE_IMM:
4032 g_assert (amd64_is_imm32 (ins->inst_imm));
4033 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
4036 #ifdef __mono_ilp32__
4037 /* In ILP32, pointers are 4 bytes, so separate these */
4038 /* cases, use literal 8 below where we really want 8 */
4039 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4040 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
4044 // FIXME: Decompose this earlier
4045 if (amd64_use_imm32 (ins->inst_imm))
4046 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
4048 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4049 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
4053 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4054 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
4057 // FIXME: Decompose this earlier
4058 if (amd64_use_imm32 (ins->inst_imm))
4059 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
4061 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4062 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
4066 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4067 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
4070 /* For NaCl, pointers are 4 bytes, so separate these */
4071 /* cases, use literal 8 below where we really want 8 */
4072 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4073 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
4075 case OP_LOAD_MEMBASE:
4076 g_assert (amd64_is_imm32 (ins->inst_offset));
4077 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
4079 case OP_LOADI8_MEMBASE:
4080 /* Use literal 8 instead of sizeof pointer or */
4081 /* register, we really want 8 for this opcode */
4082 g_assert (amd64_is_imm32 (ins->inst_offset));
4083 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
4085 case OP_LOADI4_MEMBASE:
4086 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4088 case OP_LOADU4_MEMBASE:
4089 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
4091 case OP_LOADU1_MEMBASE:
4092 /* The cpu zero extends the result into 64 bits */
4093 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
4095 case OP_LOADI1_MEMBASE:
4096 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
4098 case OP_LOADU2_MEMBASE:
4099 /* The cpu zero extends the result into 64 bits */
4100 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
4102 case OP_LOADI2_MEMBASE:
4103 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
4105 case OP_AMD64_LOADI8_MEMINDEX:
4106 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
4108 case OP_LCONV_TO_I1:
4109 case OP_ICONV_TO_I1:
4111 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
4113 case OP_LCONV_TO_I2:
4114 case OP_ICONV_TO_I2:
4116 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
4118 case OP_LCONV_TO_U1:
4119 case OP_ICONV_TO_U1:
4120 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
4122 case OP_LCONV_TO_U2:
4123 case OP_ICONV_TO_U2:
4124 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
4127 /* Clean out the upper word */
4128 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4131 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
4135 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4137 case OP_COMPARE_IMM:
4138 #if defined(__mono_ilp32__)
4139 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
4140 g_assert (amd64_is_imm32 (ins->inst_imm));
4141 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4144 case OP_LCOMPARE_IMM:
4145 g_assert (amd64_is_imm32 (ins->inst_imm));
4146 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
4148 case OP_X86_COMPARE_REG_MEMBASE:
4149 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
4151 case OP_X86_TEST_NULL:
4152 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
4154 case OP_AMD64_TEST_NULL:
4155 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4158 case OP_X86_ADD_REG_MEMBASE:
4159 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4161 case OP_X86_SUB_REG_MEMBASE:
4162 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4164 case OP_X86_AND_REG_MEMBASE:
4165 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4167 case OP_X86_OR_REG_MEMBASE:
4168 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4170 case OP_X86_XOR_REG_MEMBASE:
4171 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4174 case OP_X86_ADD_MEMBASE_IMM:
4175 /* FIXME: Make a 64 version too */
4176 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4178 case OP_X86_SUB_MEMBASE_IMM:
4179 g_assert (amd64_is_imm32 (ins->inst_imm));
4180 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4182 case OP_X86_AND_MEMBASE_IMM:
4183 g_assert (amd64_is_imm32 (ins->inst_imm));
4184 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4186 case OP_X86_OR_MEMBASE_IMM:
4187 g_assert (amd64_is_imm32 (ins->inst_imm));
4188 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4190 case OP_X86_XOR_MEMBASE_IMM:
4191 g_assert (amd64_is_imm32 (ins->inst_imm));
4192 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4194 case OP_X86_ADD_MEMBASE_REG:
4195 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4197 case OP_X86_SUB_MEMBASE_REG:
4198 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4200 case OP_X86_AND_MEMBASE_REG:
4201 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4203 case OP_X86_OR_MEMBASE_REG:
4204 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4206 case OP_X86_XOR_MEMBASE_REG:
4207 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4209 case OP_X86_INC_MEMBASE:
4210 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4212 case OP_X86_INC_REG:
4213 amd64_inc_reg_size (code, ins->dreg, 4);
4215 case OP_X86_DEC_MEMBASE:
4216 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4218 case OP_X86_DEC_REG:
4219 amd64_dec_reg_size (code, ins->dreg, 4);
4221 case OP_X86_MUL_REG_MEMBASE:
4222 case OP_X86_MUL_MEMBASE_REG:
4223 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4225 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4226 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4228 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4229 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4231 case OP_AMD64_COMPARE_MEMBASE_REG:
4232 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4234 case OP_AMD64_COMPARE_MEMBASE_IMM:
4235 g_assert (amd64_is_imm32 (ins->inst_imm));
4236 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4238 case OP_X86_COMPARE_MEMBASE8_IMM:
4239 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4241 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4242 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4244 case OP_AMD64_COMPARE_REG_MEMBASE:
4245 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4248 case OP_AMD64_ADD_REG_MEMBASE:
4249 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4251 case OP_AMD64_SUB_REG_MEMBASE:
4252 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4254 case OP_AMD64_AND_REG_MEMBASE:
4255 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4257 case OP_AMD64_OR_REG_MEMBASE:
4258 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4260 case OP_AMD64_XOR_REG_MEMBASE:
4261 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4264 case OP_AMD64_ADD_MEMBASE_REG:
4265 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4267 case OP_AMD64_SUB_MEMBASE_REG:
4268 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4270 case OP_AMD64_AND_MEMBASE_REG:
4271 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4273 case OP_AMD64_OR_MEMBASE_REG:
4274 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4276 case OP_AMD64_XOR_MEMBASE_REG:
4277 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4280 case OP_AMD64_ADD_MEMBASE_IMM:
4281 g_assert (amd64_is_imm32 (ins->inst_imm));
4282 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4284 case OP_AMD64_SUB_MEMBASE_IMM:
4285 g_assert (amd64_is_imm32 (ins->inst_imm));
4286 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4288 case OP_AMD64_AND_MEMBASE_IMM:
4289 g_assert (amd64_is_imm32 (ins->inst_imm));
4290 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4292 case OP_AMD64_OR_MEMBASE_IMM:
4293 g_assert (amd64_is_imm32 (ins->inst_imm));
4294 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4296 case OP_AMD64_XOR_MEMBASE_IMM:
4297 g_assert (amd64_is_imm32 (ins->inst_imm));
4298 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4302 amd64_breakpoint (code);
4304 case OP_RELAXED_NOP:
4305 x86_prefix (code, X86_REP_PREFIX);
4313 case OP_DUMMY_STORE:
4314 case OP_DUMMY_ICONST:
4315 case OP_DUMMY_R8CONST:
4316 case OP_NOT_REACHED:
4319 case OP_IL_SEQ_POINT:
4320 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4322 case OP_SEQ_POINT: {
4323 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4324 MonoInst *var = cfg->arch.ss_tramp_var;
4327 /* Load ss_tramp_var */
4328 /* This is equal to &ss_trampoline */
4329 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4330 /* Load the trampoline address */
4331 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4332 /* Call it if it is non-null */
4333 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4335 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4336 amd64_call_reg (code, AMD64_R11);
4337 amd64_patch (label, code);
4341 * This is the address which is saved in seq points,
4343 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4345 if (cfg->compile_aot) {
4346 guint32 offset = code - cfg->native_code;
4348 MonoInst *info_var = cfg->arch.seq_point_info_var;
4352 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4353 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4354 /* Load the info->bp_addrs [offset], which is either NULL or the address of the breakpoint trampoline */
4355 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4356 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4358 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4359 /* Call the trampoline */
4360 amd64_call_reg (code, AMD64_R11);
4361 amd64_patch (label, code);
4363 MonoInst *var = cfg->arch.bp_tramp_var;
4367 * Emit a test+branch against a constant, the constant will be overwritten
4368 * by mono_arch_set_breakpoint () to cause the test to fail.
4370 amd64_mov_reg_imm (code, AMD64_R11, 0);
4371 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4373 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4376 g_assert (var->opcode == OP_REGOFFSET);
4377 /* Load bp_tramp_var */
4378 /* This is equal to &bp_trampoline */
4379 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4380 /* Call the trampoline */
4381 amd64_call_membase (code, AMD64_R11, 0);
4382 amd64_patch (label, code);
4385 * Add an additional nop so skipping the bp doesn't cause the ip to point
4386 * to another IL offset.
4394 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4397 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4401 g_assert (amd64_is_imm32 (ins->inst_imm));
4402 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4405 g_assert (amd64_is_imm32 (ins->inst_imm));
4406 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4411 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4414 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4418 g_assert (amd64_is_imm32 (ins->inst_imm));
4419 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4422 g_assert (amd64_is_imm32 (ins->inst_imm));
4423 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4426 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4430 g_assert (amd64_is_imm32 (ins->inst_imm));
4431 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4434 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4439 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4441 switch (ins->inst_imm) {
4445 if (ins->dreg != ins->sreg1)
4446 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4447 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4450 /* LEA r1, [r2 + r2*2] */
4451 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4454 /* LEA r1, [r2 + r2*4] */
4455 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4458 /* LEA r1, [r2 + r2*2] */
4460 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4461 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4464 /* LEA r1, [r2 + r2*8] */
4465 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4468 /* LEA r1, [r2 + r2*4] */
4470 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4471 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4474 /* LEA r1, [r2 + r2*2] */
4476 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4477 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4480 /* LEA r1, [r2 + r2*4] */
4481 /* LEA r1, [r1 + r1*4] */
4482 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4483 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4486 /* LEA r1, [r2 + r2*4] */
4488 /* LEA r1, [r1 + r1*4] */
4489 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4490 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4491 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4494 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
4501 #if defined( __native_client_codegen__ )
4502 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4503 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4505 /* Regalloc magic makes the div/rem cases the same */
4506 if (ins->sreg2 == AMD64_RDX) {
4507 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4509 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4512 amd64_div_reg (code, ins->sreg2, TRUE);
4517 #if defined( __native_client_codegen__ )
4518 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4519 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4521 if (ins->sreg2 == AMD64_RDX) {
4522 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4523 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4524 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4526 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4527 amd64_div_reg (code, ins->sreg2, FALSE);
4532 #if defined( __native_client_codegen__ )
4533 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4534 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4536 if (ins->sreg2 == AMD64_RDX) {
4537 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4538 amd64_cdq_size (code, 4);
4539 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4541 amd64_cdq_size (code, 4);
4542 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4547 #if defined( __native_client_codegen__ )
4548 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4549 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4551 if (ins->sreg2 == AMD64_RDX) {
4552 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4553 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4554 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4556 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4557 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4561 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4562 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4565 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4569 g_assert (amd64_is_imm32 (ins->inst_imm));
4570 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4573 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4577 g_assert (amd64_is_imm32 (ins->inst_imm));
4578 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4581 g_assert (ins->sreg2 == AMD64_RCX);
4582 amd64_shift_reg (code, X86_SHL, ins->dreg);
4585 g_assert (ins->sreg2 == AMD64_RCX);
4586 amd64_shift_reg (code, X86_SAR, ins->dreg);
4590 g_assert (amd64_is_imm32 (ins->inst_imm));
4591 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4594 g_assert (amd64_is_imm32 (ins->inst_imm));
4595 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4597 case OP_LSHR_UN_IMM:
4598 g_assert (amd64_is_imm32 (ins->inst_imm));
4599 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4602 g_assert (ins->sreg2 == AMD64_RCX);
4603 amd64_shift_reg (code, X86_SHR, ins->dreg);
4607 g_assert (amd64_is_imm32 (ins->inst_imm));
4608 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4613 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4616 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4619 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4622 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4626 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4629 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4632 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4635 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4638 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4641 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4644 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4647 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4650 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4653 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4656 amd64_neg_reg_size (code, ins->sreg1, 4);
4659 amd64_not_reg_size (code, ins->sreg1, 4);
4662 g_assert (ins->sreg2 == AMD64_RCX);
4663 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4666 g_assert (ins->sreg2 == AMD64_RCX);
4667 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4670 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4672 case OP_ISHR_UN_IMM:
4673 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4676 g_assert (ins->sreg2 == AMD64_RCX);
4677 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4680 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4683 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4686 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4687 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4689 case OP_IMUL_OVF_UN:
4690 case OP_LMUL_OVF_UN: {
4691 /* the mul operation and the exception check should most likely be split */
4692 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4693 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4694 /*g_assert (ins->sreg2 == X86_EAX);
4695 g_assert (ins->dreg == X86_EAX);*/
4696 if (ins->sreg2 == X86_EAX) {
4697 non_eax_reg = ins->sreg1;
4698 } else if (ins->sreg1 == X86_EAX) {
4699 non_eax_reg = ins->sreg2;
4701 /* no need to save since we're going to store to it anyway */
4702 if (ins->dreg != X86_EAX) {
4704 amd64_push_reg (code, X86_EAX);
4706 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4707 non_eax_reg = ins->sreg2;
4709 if (ins->dreg == X86_EDX) {
4712 amd64_push_reg (code, X86_EAX);
4716 amd64_push_reg (code, X86_EDX);
4718 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4719 /* save before the check since pop and mov don't change the flags */
4720 if (ins->dreg != X86_EAX)
4721 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4723 amd64_pop_reg (code, X86_EDX);
4725 amd64_pop_reg (code, X86_EAX);
4726 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4730 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4732 case OP_ICOMPARE_IMM:
4733 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4755 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4763 case OP_CMOV_INE_UN:
4764 case OP_CMOV_IGE_UN:
4765 case OP_CMOV_IGT_UN:
4766 case OP_CMOV_ILE_UN:
4767 case OP_CMOV_ILT_UN:
4773 case OP_CMOV_LNE_UN:
4774 case OP_CMOV_LGE_UN:
4775 case OP_CMOV_LGT_UN:
4776 case OP_CMOV_LLE_UN:
4777 case OP_CMOV_LLT_UN:
4778 g_assert (ins->dreg == ins->sreg1);
4779 /* This needs to operate on 64 bit values */
4780 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);
4784 amd64_not_reg (code, ins->sreg1);
4787 amd64_neg_reg (code, ins->sreg1);
4792 if ((((guint64)ins->inst_c0) >> 32) == 0 && !mini_get_debug_options()->single_imm_size)
4793 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4795 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4798 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4799 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4802 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4803 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4806 if (ins->dreg != ins->sreg1)
4807 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4809 case OP_AMD64_SET_XMMREG_R4: {
4811 if (ins->dreg != ins->sreg1)
4812 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4814 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4818 case OP_AMD64_SET_XMMREG_R8: {
4819 if (ins->dreg != ins->sreg1)
4820 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4824 MonoCallInst *call = (MonoCallInst*)ins;
4825 int i, save_area_offset;
4827 g_assert (!cfg->method->save_lmf);
4829 /* Restore callee saved registers */
4830 save_area_offset = cfg->arch.reg_save_area_offset;
4831 for (i = 0; i < AMD64_NREG; ++i)
4832 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4833 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4834 save_area_offset += 8;
4837 if (cfg->arch.omit_fp) {
4838 if (cfg->arch.stack_alloc_size)
4839 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4841 if (call->stack_usage)
4844 /* Copy arguments on the stack to our argument area */
4845 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4846 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4847 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4853 offset = code - cfg->native_code;
4854 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4855 if (cfg->compile_aot)
4856 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4858 amd64_set_reg_template (code, AMD64_R11);
4859 amd64_jump_reg (code, AMD64_R11);
4860 ins->flags |= MONO_INST_GC_CALLSITE;
4861 ins->backend.pc_offset = code - cfg->native_code;
4865 /* ensure ins->sreg1 is not NULL */
4866 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4869 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4870 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4880 call = (MonoCallInst*)ins;
4882 * The AMD64 ABI forces callers to know about varargs.
4884 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4885 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4886 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4888 * Since the unmanaged calling convention doesn't contain a
4889 * 'vararg' entry, we have to treat every pinvoke call as a
4890 * potential vararg call.
4894 for (i = 0; i < AMD64_XMM_NREG; ++i)
4895 if (call->used_fregs & (1 << i))
4898 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4900 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4903 if (ins->flags & MONO_INST_HAS_METHOD)
4904 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4906 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
4907 ins->flags |= MONO_INST_GC_CALLSITE;
4908 ins->backend.pc_offset = code - cfg->native_code;
4909 code = emit_move_return_value (cfg, ins, code);
4916 case OP_VOIDCALL_REG:
4918 call = (MonoCallInst*)ins;
4920 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4921 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4922 ins->sreg1 = AMD64_R11;
4926 * The AMD64 ABI forces callers to know about varargs.
4928 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4929 if (ins->sreg1 == AMD64_RAX) {
4930 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4931 ins->sreg1 = AMD64_R11;
4933 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4934 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4936 * Since the unmanaged calling convention doesn't contain a
4937 * 'vararg' entry, we have to treat every pinvoke call as a
4938 * potential vararg call.
4942 for (i = 0; i < AMD64_XMM_NREG; ++i)
4943 if (call->used_fregs & (1 << i))
4945 if (ins->sreg1 == AMD64_RAX) {
4946 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4947 ins->sreg1 = AMD64_R11;
4950 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4952 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4955 amd64_call_reg (code, ins->sreg1);
4956 ins->flags |= MONO_INST_GC_CALLSITE;
4957 ins->backend.pc_offset = code - cfg->native_code;
4958 code = emit_move_return_value (cfg, ins, code);
4960 case OP_FCALL_MEMBASE:
4961 case OP_RCALL_MEMBASE:
4962 case OP_LCALL_MEMBASE:
4963 case OP_VCALL_MEMBASE:
4964 case OP_VCALL2_MEMBASE:
4965 case OP_VOIDCALL_MEMBASE:
4966 case OP_CALL_MEMBASE:
4967 call = (MonoCallInst*)ins;
4969 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
4970 ins->flags |= MONO_INST_GC_CALLSITE;
4971 ins->backend.pc_offset = code - cfg->native_code;
4972 code = emit_move_return_value (cfg, ins, code);
4976 MonoInst *var = cfg->dyn_call_var;
4978 g_assert (var->opcode == OP_REGOFFSET);
4980 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
4981 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4983 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
4985 /* Save args buffer */
4986 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
4988 /* Set argument registers */
4989 for (i = 0; i < PARAM_REGS; ++i)
4990 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
4993 amd64_call_reg (code, AMD64_R10);
4995 ins->flags |= MONO_INST_GC_CALLSITE;
4996 ins->backend.pc_offset = code - cfg->native_code;
4999 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
5000 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
5003 case OP_AMD64_SAVE_SP_TO_LMF: {
5004 MonoInst *lmf_var = cfg->lmf_var;
5005 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
5009 g_assert_not_reached ();
5010 amd64_push_reg (code, ins->sreg1);
5012 case OP_X86_PUSH_IMM:
5013 g_assert_not_reached ();
5014 g_assert (amd64_is_imm32 (ins->inst_imm));
5015 amd64_push_imm (code, ins->inst_imm);
5017 case OP_X86_PUSH_MEMBASE:
5018 g_assert_not_reached ();
5019 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
5021 case OP_X86_PUSH_OBJ: {
5022 int size = ALIGN_TO (ins->inst_imm, 8);
5024 g_assert_not_reached ();
5026 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5027 amd64_push_reg (code, AMD64_RDI);
5028 amd64_push_reg (code, AMD64_RSI);
5029 amd64_push_reg (code, AMD64_RCX);
5030 if (ins->inst_offset)
5031 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
5033 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
5034 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
5035 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
5037 amd64_prefix (code, X86_REP_PREFIX);
5039 amd64_pop_reg (code, AMD64_RCX);
5040 amd64_pop_reg (code, AMD64_RSI);
5041 amd64_pop_reg (code, AMD64_RDI);
5044 case OP_GENERIC_CLASS_INIT: {
5045 static int byte_offset = -1;
5046 static guint8 bitmask;
5049 g_assert (ins->sreg1 == MONO_AMD64_ARG_REG1);
5051 if (byte_offset < 0)
5052 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
5054 amd64_test_membase_imm_size (code, ins->sreg1, byte_offset, bitmask, 1);
5056 amd64_branch8 (code, X86_CC_NZ, -1, 1);
5058 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_generic_class_init", FALSE);
5059 ins->flags |= MONO_INST_GC_CALLSITE;
5060 ins->backend.pc_offset = code - cfg->native_code;
5062 x86_patch (jump, code);
5067 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
5069 case OP_X86_LEA_MEMBASE:
5070 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
5073 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
5076 /* keep alignment */
5077 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
5078 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
5079 code = mono_emit_stack_alloc (cfg, code, ins);
5080 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5081 if (cfg->param_area)
5082 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5084 case OP_LOCALLOC_IMM: {
5085 guint32 size = ins->inst_imm;
5086 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
5088 if (ins->flags & MONO_INST_INIT) {
5092 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5093 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5095 for (i = 0; i < size; i += 8)
5096 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
5097 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5099 amd64_mov_reg_imm (code, ins->dreg, size);
5100 ins->sreg1 = ins->dreg;
5102 code = mono_emit_stack_alloc (cfg, code, ins);
5103 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5106 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5107 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5109 if (cfg->param_area)
5110 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5114 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5115 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5116 (gpointer)"mono_arch_throw_exception", FALSE);
5117 ins->flags |= MONO_INST_GC_CALLSITE;
5118 ins->backend.pc_offset = code - cfg->native_code;
5122 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5123 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5124 (gpointer)"mono_arch_rethrow_exception", FALSE);
5125 ins->flags |= MONO_INST_GC_CALLSITE;
5126 ins->backend.pc_offset = code - cfg->native_code;
5129 case OP_CALL_HANDLER:
5131 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5132 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5133 amd64_call_imm (code, 0);
5134 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
5135 /* Restore stack alignment */
5136 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5138 case OP_START_HANDLER: {
5139 /* Even though we're saving RSP, use sizeof */
5140 /* gpointer because spvar is of type IntPtr */
5141 /* see: mono_create_spvar_for_region */
5142 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5143 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
5145 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
5146 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
5148 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
5152 case OP_ENDFINALLY: {
5153 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5154 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5158 case OP_ENDFILTER: {
5159 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5160 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5161 /* The local allocator will put the result into RAX */
5166 if (ins->dreg != AMD64_RAX)
5167 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, sizeof (gpointer));
5170 ins->inst_c0 = code - cfg->native_code;
5173 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
5174 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
5176 if (ins->inst_target_bb->native_offset) {
5177 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
5179 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5180 if ((cfg->opt & MONO_OPT_BRANCH) &&
5181 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
5182 x86_jump8 (code, 0);
5184 x86_jump32 (code, 0);
5188 amd64_jump_reg (code, ins->sreg1);
5211 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5212 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5214 case OP_COND_EXC_EQ:
5215 case OP_COND_EXC_NE_UN:
5216 case OP_COND_EXC_LT:
5217 case OP_COND_EXC_LT_UN:
5218 case OP_COND_EXC_GT:
5219 case OP_COND_EXC_GT_UN:
5220 case OP_COND_EXC_GE:
5221 case OP_COND_EXC_GE_UN:
5222 case OP_COND_EXC_LE:
5223 case OP_COND_EXC_LE_UN:
5224 case OP_COND_EXC_IEQ:
5225 case OP_COND_EXC_INE_UN:
5226 case OP_COND_EXC_ILT:
5227 case OP_COND_EXC_ILT_UN:
5228 case OP_COND_EXC_IGT:
5229 case OP_COND_EXC_IGT_UN:
5230 case OP_COND_EXC_IGE:
5231 case OP_COND_EXC_IGE_UN:
5232 case OP_COND_EXC_ILE:
5233 case OP_COND_EXC_ILE_UN:
5234 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
5236 case OP_COND_EXC_OV:
5237 case OP_COND_EXC_NO:
5239 case OP_COND_EXC_NC:
5240 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5241 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
5243 case OP_COND_EXC_IOV:
5244 case OP_COND_EXC_INO:
5245 case OP_COND_EXC_IC:
5246 case OP_COND_EXC_INC:
5247 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5248 (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
5251 /* floating point opcodes */
5253 double d = *(double *)ins->inst_p0;
5255 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5256 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5259 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5260 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5265 float f = *(float *)ins->inst_p0;
5267 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5269 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5271 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5274 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5275 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5277 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5281 case OP_STORER8_MEMBASE_REG:
5282 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5284 case OP_LOADR8_MEMBASE:
5285 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5287 case OP_STORER4_MEMBASE_REG:
5289 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5291 /* This requires a double->single conversion */
5292 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5293 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5296 case OP_LOADR4_MEMBASE:
5298 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5300 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5301 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5304 case OP_ICONV_TO_R4:
5306 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5308 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5309 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5312 case OP_ICONV_TO_R8:
5313 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5315 case OP_LCONV_TO_R4:
5317 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5319 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5320 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5323 case OP_LCONV_TO_R8:
5324 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5326 case OP_FCONV_TO_R4:
5328 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5330 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5331 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5334 case OP_FCONV_TO_I1:
5335 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5337 case OP_FCONV_TO_U1:
5338 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5340 case OP_FCONV_TO_I2:
5341 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5343 case OP_FCONV_TO_U2:
5344 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5346 case OP_FCONV_TO_U4:
5347 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5349 case OP_FCONV_TO_I4:
5351 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5353 case OP_FCONV_TO_I8:
5354 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5357 case OP_RCONV_TO_I1:
5358 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5359 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5361 case OP_RCONV_TO_U1:
5362 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5363 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5365 case OP_RCONV_TO_I2:
5366 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5367 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5369 case OP_RCONV_TO_U2:
5370 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5371 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5373 case OP_RCONV_TO_I4:
5374 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5376 case OP_RCONV_TO_U4:
5377 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5379 case OP_RCONV_TO_I8:
5380 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5382 case OP_RCONV_TO_R8:
5383 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5385 case OP_RCONV_TO_R4:
5386 if (ins->dreg != ins->sreg1)
5387 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5390 case OP_LCONV_TO_R_UN: {
5393 /* Based on gcc code */
5394 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5395 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5398 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5399 br [1] = code; x86_jump8 (code, 0);
5400 amd64_patch (br [0], code);
5403 /* Save to the red zone */
5404 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5405 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5406 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5407 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5408 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5409 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5410 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5411 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5412 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5414 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5415 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5416 amd64_patch (br [1], code);
5419 case OP_LCONV_TO_OVF_U4:
5420 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5421 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5422 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5424 case OP_LCONV_TO_OVF_I4_UN:
5425 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5426 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5427 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5430 if (ins->dreg != ins->sreg1)
5431 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5434 if (ins->dreg != ins->sreg1)
5435 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5437 case OP_MOVE_F_TO_I4:
5439 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5441 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5442 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5445 case OP_MOVE_I4_TO_F:
5446 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5448 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5450 case OP_MOVE_F_TO_I8:
5451 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5453 case OP_MOVE_I8_TO_F:
5454 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5457 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5460 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5463 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5466 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5469 static double r8_0 = -0.0;
5471 g_assert (ins->sreg1 == ins->dreg);
5473 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5474 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5478 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5481 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5484 static guint64 d = 0x7fffffffffffffffUL;
5486 g_assert (ins->sreg1 == ins->dreg);
5488 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5489 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5493 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5497 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5500 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5503 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5506 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5509 static float r4_0 = -0.0;
5511 g_assert (ins->sreg1 == ins->dreg);
5513 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5514 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5515 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
5520 g_assert (cfg->opt & MONO_OPT_CMOV);
5521 g_assert (ins->dreg == ins->sreg1);
5522 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5523 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
5526 g_assert (cfg->opt & MONO_OPT_CMOV);
5527 g_assert (ins->dreg == ins->sreg1);
5528 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5529 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
5532 g_assert (cfg->opt & MONO_OPT_CMOV);
5533 g_assert (ins->dreg == ins->sreg1);
5534 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5535 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
5538 g_assert (cfg->opt & MONO_OPT_CMOV);
5539 g_assert (ins->dreg == ins->sreg1);
5540 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5541 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
5544 g_assert (cfg->opt & MONO_OPT_CMOV);
5545 g_assert (ins->dreg == ins->sreg1);
5546 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5547 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
5550 g_assert (cfg->opt & MONO_OPT_CMOV);
5551 g_assert (ins->dreg == ins->sreg1);
5552 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5553 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
5556 g_assert (cfg->opt & MONO_OPT_CMOV);
5557 g_assert (ins->dreg == ins->sreg1);
5558 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5559 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
5562 g_assert (cfg->opt & MONO_OPT_CMOV);
5563 g_assert (ins->dreg == ins->sreg1);
5564 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5565 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5571 * The two arguments are swapped because the fbranch instructions
5572 * depend on this for the non-sse case to work.
5574 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5578 * FIXME: Get rid of this.
5579 * The two arguments are swapped because the fbranch instructions
5580 * depend on this for the non-sse case to work.
5582 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5586 /* zeroing the register at the start results in
5587 * shorter and faster code (we can also remove the widening op)
5589 guchar *unordered_check;
5591 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5592 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5593 unordered_check = code;
5594 x86_branch8 (code, X86_CC_P, 0, FALSE);
5596 if (ins->opcode == OP_FCEQ) {
5597 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5598 amd64_patch (unordered_check, code);
5600 guchar *jump_to_end;
5601 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5603 x86_jump8 (code, 0);
5604 amd64_patch (unordered_check, code);
5605 amd64_inc_reg (code, ins->dreg);
5606 amd64_patch (jump_to_end, code);
5612 /* zeroing the register at the start results in
5613 * shorter and faster code (we can also remove the widening op)
5615 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5616 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5617 if (ins->opcode == OP_FCLT_UN) {
5618 guchar *unordered_check = code;
5619 guchar *jump_to_end;
5620 x86_branch8 (code, X86_CC_P, 0, FALSE);
5621 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5623 x86_jump8 (code, 0);
5624 amd64_patch (unordered_check, code);
5625 amd64_inc_reg (code, ins->dreg);
5626 amd64_patch (jump_to_end, code);
5628 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5633 guchar *unordered_check;
5634 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5635 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5636 unordered_check = code;
5637 x86_branch8 (code, X86_CC_P, 0, FALSE);
5638 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5639 amd64_patch (unordered_check, code);
5644 /* zeroing the register at the start results in
5645 * shorter and faster code (we can also remove the widening op)
5647 guchar *unordered_check;
5649 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5650 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5651 if (ins->opcode == OP_FCGT) {
5652 unordered_check = code;
5653 x86_branch8 (code, X86_CC_P, 0, FALSE);
5654 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5655 amd64_patch (unordered_check, code);
5657 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5662 guchar *unordered_check;
5663 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5664 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5665 unordered_check = code;
5666 x86_branch8 (code, X86_CC_P, 0, FALSE);
5667 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5668 amd64_patch (unordered_check, code);
5678 gboolean unordered = FALSE;
5680 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5681 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5683 switch (ins->opcode) {
5685 x86_cond = X86_CC_EQ;
5688 x86_cond = X86_CC_LT;
5691 x86_cond = X86_CC_GT;
5694 x86_cond = X86_CC_GT;
5698 x86_cond = X86_CC_LT;
5702 g_assert_not_reached ();
5707 guchar *unordered_check;
5708 guchar *jump_to_end;
5710 unordered_check = code;
5711 x86_branch8 (code, X86_CC_P, 0, FALSE);
5712 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5714 x86_jump8 (code, 0);
5715 amd64_patch (unordered_check, code);
5716 amd64_inc_reg (code, ins->dreg);
5717 amd64_patch (jump_to_end, code);
5719 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5723 case OP_FCLT_MEMBASE:
5724 case OP_FCGT_MEMBASE:
5725 case OP_FCLT_UN_MEMBASE:
5726 case OP_FCGT_UN_MEMBASE:
5727 case OP_FCEQ_MEMBASE: {
5728 guchar *unordered_check, *jump_to_end;
5731 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5732 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5734 switch (ins->opcode) {
5735 case OP_FCEQ_MEMBASE:
5736 x86_cond = X86_CC_EQ;
5738 case OP_FCLT_MEMBASE:
5739 case OP_FCLT_UN_MEMBASE:
5740 x86_cond = X86_CC_LT;
5742 case OP_FCGT_MEMBASE:
5743 case OP_FCGT_UN_MEMBASE:
5744 x86_cond = X86_CC_GT;
5747 g_assert_not_reached ();
5750 unordered_check = code;
5751 x86_branch8 (code, X86_CC_P, 0, FALSE);
5752 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5754 switch (ins->opcode) {
5755 case OP_FCEQ_MEMBASE:
5756 case OP_FCLT_MEMBASE:
5757 case OP_FCGT_MEMBASE:
5758 amd64_patch (unordered_check, code);
5760 case OP_FCLT_UN_MEMBASE:
5761 case OP_FCGT_UN_MEMBASE:
5763 x86_jump8 (code, 0);
5764 amd64_patch (unordered_check, code);
5765 amd64_inc_reg (code, ins->dreg);
5766 amd64_patch (jump_to_end, code);
5774 guchar *jump = code;
5775 x86_branch8 (code, X86_CC_P, 0, TRUE);
5776 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5777 amd64_patch (jump, code);
5781 /* Branch if C013 != 100 */
5782 /* branch if !ZF or (PF|CF) */
5783 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5784 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5785 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5788 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5791 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5792 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5796 if (ins->opcode == OP_FBGT) {
5799 /* skip branch if C1=1 */
5801 x86_branch8 (code, X86_CC_P, 0, FALSE);
5802 /* branch if (C0 | C3) = 1 */
5803 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5804 amd64_patch (br1, code);
5807 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5811 /* Branch if C013 == 100 or 001 */
5814 /* skip branch if C1=1 */
5816 x86_branch8 (code, X86_CC_P, 0, FALSE);
5817 /* branch if (C0 | C3) = 1 */
5818 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5819 amd64_patch (br1, code);
5823 /* Branch if C013 == 000 */
5824 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5827 /* Branch if C013=000 or 100 */
5830 /* skip branch if C1=1 */
5832 x86_branch8 (code, X86_CC_P, 0, FALSE);
5833 /* branch if C0=0 */
5834 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5835 amd64_patch (br1, code);
5839 /* Branch if C013 != 001 */
5840 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5841 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5844 /* Transfer value to the fp stack */
5845 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5846 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5847 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5849 amd64_push_reg (code, AMD64_RAX);
5851 amd64_fnstsw (code);
5852 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5853 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5854 amd64_pop_reg (code, AMD64_RAX);
5855 amd64_fstp (code, 0);
5856 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
5857 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5860 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5863 case OP_TLS_GET_REG:
5864 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5867 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5870 case OP_TLS_SET_REG: {
5871 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5874 case OP_MEMORY_BARRIER: {
5875 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5879 case OP_ATOMIC_ADD_I4:
5880 case OP_ATOMIC_ADD_I8: {
5881 int dreg = ins->dreg;
5882 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5884 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5887 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5888 amd64_prefix (code, X86_LOCK_PREFIX);
5889 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5890 /* dreg contains the old value, add with sreg2 value */
5891 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5893 if (ins->dreg != dreg)
5894 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5898 case OP_ATOMIC_EXCHANGE_I4:
5899 case OP_ATOMIC_EXCHANGE_I8: {
5900 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5902 /* LOCK prefix is implied. */
5903 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5904 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5905 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5908 case OP_ATOMIC_CAS_I4:
5909 case OP_ATOMIC_CAS_I8: {
5912 if (ins->opcode == OP_ATOMIC_CAS_I8)
5918 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5919 * an explanation of how this works.
5921 g_assert (ins->sreg3 == AMD64_RAX);
5922 g_assert (ins->sreg1 != AMD64_RAX);
5923 g_assert (ins->sreg1 != ins->sreg2);
5925 amd64_prefix (code, X86_LOCK_PREFIX);
5926 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
5928 if (ins->dreg != AMD64_RAX)
5929 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
5932 case OP_ATOMIC_LOAD_I1: {
5933 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
5936 case OP_ATOMIC_LOAD_U1: {
5937 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
5940 case OP_ATOMIC_LOAD_I2: {
5941 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
5944 case OP_ATOMIC_LOAD_U2: {
5945 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
5948 case OP_ATOMIC_LOAD_I4: {
5949 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5952 case OP_ATOMIC_LOAD_U4:
5953 case OP_ATOMIC_LOAD_I8:
5954 case OP_ATOMIC_LOAD_U8: {
5955 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
5958 case OP_ATOMIC_LOAD_R4: {
5959 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5960 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5963 case OP_ATOMIC_LOAD_R8: {
5964 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5967 case OP_ATOMIC_STORE_I1:
5968 case OP_ATOMIC_STORE_U1:
5969 case OP_ATOMIC_STORE_I2:
5970 case OP_ATOMIC_STORE_U2:
5971 case OP_ATOMIC_STORE_I4:
5972 case OP_ATOMIC_STORE_U4:
5973 case OP_ATOMIC_STORE_I8:
5974 case OP_ATOMIC_STORE_U8: {
5977 switch (ins->opcode) {
5978 case OP_ATOMIC_STORE_I1:
5979 case OP_ATOMIC_STORE_U1:
5982 case OP_ATOMIC_STORE_I2:
5983 case OP_ATOMIC_STORE_U2:
5986 case OP_ATOMIC_STORE_I4:
5987 case OP_ATOMIC_STORE_U4:
5990 case OP_ATOMIC_STORE_I8:
5991 case OP_ATOMIC_STORE_U8:
5996 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
5998 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6002 case OP_ATOMIC_STORE_R4: {
6003 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6004 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
6006 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6010 case OP_ATOMIC_STORE_R8: {
6013 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
6017 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6021 case OP_CARD_TABLE_WBARRIER: {
6022 int ptr = ins->sreg1;
6023 int value = ins->sreg2;
6025 int nursery_shift, card_table_shift;
6026 gpointer card_table_mask;
6027 size_t nursery_size;
6029 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
6030 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
6031 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
6033 /*If either point to the stack we can simply avoid the WB. This happens due to
6034 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
6036 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
6040 * We need one register we can clobber, we choose EDX and make sreg1
6041 * fixed EAX to work around limitations in the local register allocator.
6042 * sreg2 might get allocated to EDX, but that is not a problem since
6043 * we use it before clobbering EDX.
6045 g_assert (ins->sreg1 == AMD64_RAX);
6048 * This is the code we produce:
6051 * edx >>= nursery_shift
6052 * cmp edx, (nursery_start >> nursery_shift)
6055 * edx >>= card_table_shift
6061 if (mono_gc_card_table_nursery_check ()) {
6062 if (value != AMD64_RDX)
6063 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
6064 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
6065 if (shifted_nursery_start >> 31) {
6067 * The value we need to compare against is 64 bits, so we need
6068 * another spare register. We use RBX, which we save and
6071 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
6072 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
6073 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
6074 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
6076 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
6078 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
6080 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
6081 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
6082 if (card_table_mask)
6083 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
6085 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
6086 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
6088 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
6090 if (mono_gc_card_table_nursery_check ())
6091 x86_patch (br, code);
6094 #ifdef MONO_ARCH_SIMD_INTRINSICS
6095 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
6097 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
6100 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
6103 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
6106 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
6109 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
6112 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
6115 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6116 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6119 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
6122 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
6125 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
6128 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
6131 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6134 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6137 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
6140 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6143 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
6146 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6149 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
6152 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
6155 case OP_PSHUFLEW_HIGH:
6156 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6157 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6159 case OP_PSHUFLEW_LOW:
6160 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6161 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6164 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6165 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6168 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6169 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6172 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
6173 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6177 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
6180 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
6183 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
6186 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
6189 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
6192 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
6195 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6196 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6199 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
6202 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
6205 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
6208 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6211 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6214 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6217 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6220 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6223 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6226 case OP_EXTRACT_MASK:
6227 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6231 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6234 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6237 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6241 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6244 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6247 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6250 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6254 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6257 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6260 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6263 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6267 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6270 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6273 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6277 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6280 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6283 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6287 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6290 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6294 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6297 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6300 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6304 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6307 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6310 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6314 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6317 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6320 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6323 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6327 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6330 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6333 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6336 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6339 case OP_PSUM_ABS_DIFF:
6340 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6343 case OP_UNPACK_LOWB:
6344 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6346 case OP_UNPACK_LOWW:
6347 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6349 case OP_UNPACK_LOWD:
6350 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6352 case OP_UNPACK_LOWQ:
6353 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6355 case OP_UNPACK_LOWPS:
6356 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6358 case OP_UNPACK_LOWPD:
6359 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6362 case OP_UNPACK_HIGHB:
6363 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6365 case OP_UNPACK_HIGHW:
6366 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6368 case OP_UNPACK_HIGHD:
6369 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6371 case OP_UNPACK_HIGHQ:
6372 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6374 case OP_UNPACK_HIGHPS:
6375 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6377 case OP_UNPACK_HIGHPD:
6378 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6382 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6385 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6388 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6391 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6394 case OP_PADDB_SAT_UN:
6395 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6397 case OP_PSUBB_SAT_UN:
6398 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6400 case OP_PADDW_SAT_UN:
6401 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6403 case OP_PSUBW_SAT_UN:
6404 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6408 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6411 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6414 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6417 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6421 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6424 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6427 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6429 case OP_PMULW_HIGH_UN:
6430 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6433 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6437 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6440 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6444 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6447 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6451 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6454 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6458 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6461 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6465 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6468 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6472 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6475 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6479 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6482 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6485 /*TODO: This is appart of the sse spec but not added
6487 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6490 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6495 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6498 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6501 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6504 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6507 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6510 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6513 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6516 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6519 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6522 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6526 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6529 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6533 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6534 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6536 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6541 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6543 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6544 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6548 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6550 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6551 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6552 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6556 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6558 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6561 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6563 case OP_EXTRACTX_U2:
6564 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6566 case OP_INSERTX_U1_SLOW:
6567 /*sreg1 is the extracted ireg (scratch)
6568 /sreg2 is the to be inserted ireg (scratch)
6569 /dreg is the xreg to receive the value*/
6571 /*clear the bits from the extracted word*/
6572 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6573 /*shift the value to insert if needed*/
6574 if (ins->inst_c0 & 1)
6575 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6576 /*join them together*/
6577 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6578 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6580 case OP_INSERTX_I4_SLOW:
6581 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6582 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6583 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6585 case OP_INSERTX_I8_SLOW:
6586 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6588 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6590 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6593 case OP_INSERTX_R4_SLOW:
6594 switch (ins->inst_c0) {
6597 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6599 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6602 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6604 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6606 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6607 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6610 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6612 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6614 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6615 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6618 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6620 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6622 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6623 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6627 case OP_INSERTX_R8_SLOW:
6629 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6631 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6633 case OP_STOREX_MEMBASE_REG:
6634 case OP_STOREX_MEMBASE:
6635 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6637 case OP_LOADX_MEMBASE:
6638 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6640 case OP_LOADX_ALIGNED_MEMBASE:
6641 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6643 case OP_STOREX_ALIGNED_MEMBASE_REG:
6644 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6646 case OP_STOREX_NTA_MEMBASE_REG:
6647 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6649 case OP_PREFETCH_MEMBASE:
6650 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6654 /*FIXME the peephole pass should have killed this*/
6655 if (ins->dreg != ins->sreg1)
6656 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6659 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6661 case OP_ICONV_TO_R4_RAW:
6662 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6665 case OP_FCONV_TO_R8_X:
6666 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6669 case OP_XCONV_R8_TO_I4:
6670 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6671 switch (ins->backend.source_opcode) {
6672 case OP_FCONV_TO_I1:
6673 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6675 case OP_FCONV_TO_U1:
6676 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6678 case OP_FCONV_TO_I2:
6679 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6681 case OP_FCONV_TO_U2:
6682 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6688 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6689 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6690 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6693 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6694 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6697 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6698 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6702 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6704 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6705 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6707 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6710 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6711 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6714 case OP_LIVERANGE_START: {
6715 if (cfg->verbose_level > 1)
6716 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6717 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6720 case OP_LIVERANGE_END: {
6721 if (cfg->verbose_level > 1)
6722 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6723 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6726 case OP_GC_SAFE_POINT: {
6727 const char *polling_func = NULL;
6728 int compare_val = 0;
6731 #if defined(__native_client_codegen__) && defined(__native_client_gc__)
6732 polling_func = "mono_nacl_gc";
6733 compare_val = 0xFFFFFFFF;
6735 g_assert (mono_threads_is_coop_enabled ());
6736 polling_func = "mono_threads_state_poll";
6740 amd64_test_membase_imm_size (code, ins->sreg1, 0, compare_val, 4);
6741 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6742 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func, FALSE);
6743 amd64_patch (br[0], code);
6747 case OP_GC_LIVENESS_DEF:
6748 case OP_GC_LIVENESS_USE:
6749 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6750 ins->backend.pc_offset = code - cfg->native_code;
6752 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6753 ins->backend.pc_offset = code - cfg->native_code;
6754 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6757 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6758 g_assert_not_reached ();
6761 if ((code - cfg->native_code - offset) > max_len) {
6762 #if !defined(__native_client_codegen__)
6763 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6764 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6765 g_assert_not_reached ();
6770 cfg->code_len = code - cfg->native_code;
6773 #endif /* DISABLE_JIT */
6776 mono_arch_register_lowlevel_calls (void)
6778 /* The signature doesn't matter */
6779 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6783 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
6785 unsigned char *ip = ji->ip.i + code;
6788 * Debug code to help track down problems where the target of a near call is
6791 if (amd64_is_near_call (ip)) {
6792 gint64 disp = (guint8*)target - (guint8*)ip;
6794 if (!amd64_is_imm32 (disp)) {
6795 printf ("TYPE: %d\n", ji->type);
6797 case MONO_PATCH_INFO_INTERNAL_METHOD:
6798 printf ("V: %s\n", ji->data.name);
6800 case MONO_PATCH_INFO_METHOD_JUMP:
6801 case MONO_PATCH_INFO_METHOD:
6802 printf ("V: %s\n", ji->data.method->name);
6810 amd64_patch (ip, (gpointer)target);
6816 get_max_epilog_size (MonoCompile *cfg)
6818 int max_epilog_size = 16;
6820 if (cfg->method->save_lmf)
6821 max_epilog_size += 256;
6823 if (mono_jit_trace_calls != NULL)
6824 max_epilog_size += 50;
6826 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6827 max_epilog_size += 50;
6829 max_epilog_size += (AMD64_NREG * 2);
6831 return max_epilog_size;
6835 * This macro is used for testing whenever the unwinder works correctly at every point
6836 * where an async exception can happen.
6838 /* This will generate a SIGSEGV at the given point in the code */
6839 #define async_exc_point(code) do { \
6840 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6841 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6842 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6843 cfg->arch.async_point_count ++; \
6848 mono_arch_emit_prolog (MonoCompile *cfg)
6850 MonoMethod *method = cfg->method;
6852 MonoMethodSignature *sig;
6854 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6857 MonoInst *lmf_var = cfg->lmf_var;
6858 gboolean args_clobbered = FALSE;
6859 gboolean trace = FALSE;
6860 #ifdef __native_client_codegen__
6861 guint alignment_check;
6864 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6866 #if defined(__default_codegen__)
6867 code = cfg->native_code = g_malloc (cfg->code_size);
6868 #elif defined(__native_client_codegen__)
6869 /* native_code_alloc is not 32-byte aligned, native_code is. */
6870 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6872 /* Align native_code to next nearest kNaclAlignment byte. */
6873 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6874 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6876 code = cfg->native_code;
6878 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6879 g_assert (alignment_check == 0);
6882 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6885 /* Amount of stack space allocated by register saving code */
6888 /* Offset between RSP and the CFA */
6892 * The prolog consists of the following parts:
6894 * - push rbp, mov rbp, rsp
6895 * - save callee saved regs using pushes
6897 * - save rgctx if needed
6898 * - save lmf if needed
6901 * - save rgctx if needed
6902 * - save lmf if needed
6903 * - save callee saved regs using moves
6908 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6909 // IP saved at CFA - 8
6910 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6911 async_exc_point (code);
6912 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6914 if (!cfg->arch.omit_fp) {
6915 amd64_push_reg (code, AMD64_RBP);
6917 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6918 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6919 async_exc_point (code);
6921 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6923 /* These are handled automatically by the stack marking code */
6924 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6926 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
6927 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
6928 async_exc_point (code);
6930 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6934 /* The param area is always at offset 0 from sp */
6935 /* This needs to be allocated here, since it has to come after the spill area */
6936 if (cfg->param_area) {
6937 if (cfg->arch.omit_fp)
6939 g_assert_not_reached ();
6940 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
6943 if (cfg->arch.omit_fp) {
6945 * On enter, the stack is misaligned by the pushing of the return
6946 * address. It is either made aligned by the pushing of %rbp, or by
6949 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
6950 if ((alloc_size % 16) == 0) {
6952 /* Mark the padding slot as NOREF */
6953 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
6956 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
6957 if (cfg->stack_offset != alloc_size) {
6958 /* Mark the padding slot as NOREF */
6959 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
6961 cfg->arch.sp_fp_offset = alloc_size;
6965 cfg->arch.stack_alloc_size = alloc_size;
6967 /* Allocate stack frame */
6969 /* See mono_emit_stack_alloc */
6970 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
6971 guint32 remaining_size = alloc_size;
6972 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
6973 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
6974 guint32 offset = code - cfg->native_code;
6975 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
6976 while (required_code_size >= (cfg->code_size - offset))
6977 cfg->code_size *= 2;
6978 cfg->native_code = mono_realloc_native_code (cfg);
6979 code = cfg->native_code + offset;
6980 cfg->stat_code_reallocs++;
6983 while (remaining_size >= 0x1000) {
6984 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
6985 if (cfg->arch.omit_fp) {
6986 cfa_offset += 0x1000;
6987 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6989 async_exc_point (code);
6991 if (cfg->arch.omit_fp)
6992 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
6995 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
6996 remaining_size -= 0x1000;
6998 if (remaining_size) {
6999 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
7000 if (cfg->arch.omit_fp) {
7001 cfa_offset += remaining_size;
7002 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7003 async_exc_point (code);
7006 if (cfg->arch.omit_fp)
7007 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
7011 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
7012 if (cfg->arch.omit_fp) {
7013 cfa_offset += alloc_size;
7014 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7015 async_exc_point (code);
7020 /* Stack alignment check */
7023 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
7024 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
7025 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
7026 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
7027 amd64_breakpoint (code);
7031 if (mini_get_debug_options ()->init_stacks) {
7032 /* Fill the stack frame with a dummy value to force deterministic behavior */
7034 /* Save registers to the red zone */
7035 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
7036 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
7038 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
7039 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
7040 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
7043 #if defined(__default_codegen__)
7044 amd64_prefix (code, X86_REP_PREFIX);
7046 #elif defined(__native_client_codegen__)
7047 /* NaCl stos pseudo-instruction */
7048 amd64_codegen_pre (code);
7049 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
7050 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
7051 /* Add %r15 to %rdi using lea, condition flags unaffected. */
7052 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
7053 amd64_prefix (code, X86_REP_PREFIX);
7055 amd64_codegen_post (code);
7056 #endif /* __native_client_codegen__ */
7058 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
7059 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
7063 if (method->save_lmf)
7064 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
7066 /* Save callee saved registers */
7067 if (cfg->arch.omit_fp) {
7068 save_area_offset = cfg->arch.reg_save_area_offset;
7069 /* Save caller saved registers after sp is adjusted */
7070 /* The registers are saved at the bottom of the frame */
7071 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
7073 /* The registers are saved just below the saved rbp */
7074 save_area_offset = cfg->arch.reg_save_area_offset;
7077 for (i = 0; i < AMD64_NREG; ++i) {
7078 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7079 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
7081 if (cfg->arch.omit_fp) {
7082 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
7083 /* These are handled automatically by the stack marking code */
7084 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
7086 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
7090 save_area_offset += 8;
7091 async_exc_point (code);
7095 /* store runtime generic context */
7096 if (cfg->rgctx_var) {
7097 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
7098 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
7100 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
7102 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
7103 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
7106 /* compute max_length in order to use short forward jumps */
7107 max_epilog_size = get_max_epilog_size (cfg);
7108 if (cfg->opt & MONO_OPT_BRANCH) {
7109 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7113 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7115 /* max alignment for loops */
7116 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
7117 max_length += LOOP_ALIGNMENT;
7118 #ifdef __native_client_codegen__
7119 /* max alignment for native client */
7120 max_length += kNaClAlignment;
7123 MONO_BB_FOR_EACH_INS (bb, ins) {
7124 #ifdef __native_client_codegen__
7126 int space_in_block = kNaClAlignment -
7127 ((max_length + cfg->code_len) & kNaClAlignmentMask);
7128 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7129 if (space_in_block < max_len && max_len < kNaClAlignment) {
7130 max_length += space_in_block;
7133 #endif /*__native_client_codegen__*/
7134 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7137 /* Take prolog and epilog instrumentation into account */
7138 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
7139 max_length += max_epilog_size;
7141 bb->max_length = max_length;
7145 sig = mono_method_signature (method);
7148 cinfo = cfg->arch.cinfo;
7150 if (sig->ret->type != MONO_TYPE_VOID) {
7151 /* Save volatile arguments to the stack */
7152 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
7153 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
7156 /* Keep this in sync with emit_load_volatile_arguments */
7157 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
7158 ArgInfo *ainfo = cinfo->args + i;
7160 ins = cfg->args [i];
7162 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
7163 /* Unused arguments */
7166 /* Save volatile arguments to the stack */
7167 if (ins->opcode != OP_REGVAR) {
7168 switch (ainfo->storage) {
7174 if (stack_offset & 0x1)
7176 else if (stack_offset & 0x2)
7178 else if (stack_offset & 0x4)
7183 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
7186 * Save the original location of 'this',
7187 * get_generic_info_from_stack_frame () needs this to properly look up
7188 * the argument value during the handling of async exceptions.
7190 if (ins == cfg->args [0]) {
7191 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7192 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
7196 case ArgInFloatSSEReg:
7197 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7199 case ArgInDoubleSSEReg:
7200 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7202 case ArgValuetypeInReg:
7203 for (quad = 0; quad < 2; quad ++) {
7204 switch (ainfo->pair_storage [quad]) {
7206 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7208 case ArgInFloatSSEReg:
7209 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7211 case ArgInDoubleSSEReg:
7212 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7217 g_assert_not_reached ();
7221 case ArgValuetypeAddrInIReg:
7222 if (ainfo->pair_storage [0] == ArgInIReg)
7223 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7229 /* Argument allocated to (non-volatile) register */
7230 switch (ainfo->storage) {
7232 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7235 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7238 g_assert_not_reached ();
7241 if (ins == cfg->args [0]) {
7242 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7243 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7248 if (cfg->method->save_lmf)
7249 args_clobbered = TRUE;
7252 args_clobbered = TRUE;
7253 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7256 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7257 args_clobbered = TRUE;
7260 * Optimize the common case of the first bblock making a call with the same
7261 * arguments as the method. This works because the arguments are still in their
7262 * original argument registers.
7263 * FIXME: Generalize this
7265 if (!args_clobbered) {
7266 MonoBasicBlock *first_bb = cfg->bb_entry;
7268 int filter = FILTER_IL_SEQ_POINT;
7270 next = mono_bb_first_inst (first_bb, filter);
7271 if (!next && first_bb->next_bb) {
7272 first_bb = first_bb->next_bb;
7273 next = mono_bb_first_inst (first_bb, filter);
7276 if (first_bb->in_count > 1)
7279 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7280 ArgInfo *ainfo = cinfo->args + i;
7281 gboolean match = FALSE;
7283 ins = cfg->args [i];
7284 if (ins->opcode != OP_REGVAR) {
7285 switch (ainfo->storage) {
7287 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7288 if (next->dreg == ainfo->reg) {
7292 next->opcode = OP_MOVE;
7293 next->sreg1 = ainfo->reg;
7294 /* Only continue if the instruction doesn't change argument regs */
7295 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7305 /* Argument allocated to (non-volatile) register */
7306 switch (ainfo->storage) {
7308 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7319 next = mono_inst_next (next, filter);
7320 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7327 if (cfg->gen_sdb_seq_points) {
7328 MonoInst *info_var = cfg->arch.seq_point_info_var;
7330 /* Initialize seq_point_info_var */
7331 if (cfg->compile_aot) {
7332 /* Initialize the variable from a GOT slot */
7333 /* Same as OP_AOTCONST */
7334 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7335 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7336 g_assert (info_var->opcode == OP_REGOFFSET);
7337 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7340 if (cfg->compile_aot) {
7341 /* Initialize ss_tramp_var */
7342 ins = cfg->arch.ss_tramp_var;
7343 g_assert (ins->opcode == OP_REGOFFSET);
7345 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7346 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr), 8);
7347 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7349 /* Initialize ss_tramp_var */
7350 ins = cfg->arch.ss_tramp_var;
7351 g_assert (ins->opcode == OP_REGOFFSET);
7353 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&ss_trampoline);
7354 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7356 /* Initialize bp_tramp_var */
7357 ins = cfg->arch.bp_tramp_var;
7358 g_assert (ins->opcode == OP_REGOFFSET);
7360 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&bp_trampoline);
7361 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7365 cfg->code_len = code - cfg->native_code;
7367 g_assert (cfg->code_len < cfg->code_size);
7373 mono_arch_emit_epilog (MonoCompile *cfg)
7375 MonoMethod *method = cfg->method;
7378 int max_epilog_size;
7380 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7381 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7383 max_epilog_size = get_max_epilog_size (cfg);
7385 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7386 cfg->code_size *= 2;
7387 cfg->native_code = mono_realloc_native_code (cfg);
7388 cfg->stat_code_reallocs++;
7390 code = cfg->native_code + cfg->code_len;
7392 cfg->has_unwind_info_for_epilog = TRUE;
7394 /* Mark the start of the epilog */
7395 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7397 /* Save the uwind state which is needed by the out-of-line code */
7398 mono_emit_unwind_op_remember_state (cfg, code);
7400 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7401 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7403 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7405 if (method->save_lmf) {
7406 /* check if we need to restore protection of the stack after a stack overflow */
7407 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7409 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7410 /* we load the value in a separate instruction: this mechanism may be
7411 * used later as a safer way to do thread interruption
7413 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7414 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7416 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7417 /* note that the call trampoline will preserve eax/edx */
7418 x86_call_reg (code, X86_ECX);
7419 x86_patch (patch, code);
7421 /* FIXME: maybe save the jit tls in the prolog */
7423 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7424 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7428 /* Restore callee saved regs */
7429 for (i = 0; i < AMD64_NREG; ++i) {
7430 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7431 /* Restore only used_int_regs, not arch.saved_iregs */
7432 if (cfg->used_int_regs & (1 << i)) {
7433 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7434 mono_emit_unwind_op_same_value (cfg, code, i);
7435 async_exc_point (code);
7437 save_area_offset += 8;
7441 /* Load returned vtypes into registers if needed */
7442 cinfo = cfg->arch.cinfo;
7443 if (cinfo->ret.storage == ArgValuetypeInReg) {
7444 ArgInfo *ainfo = &cinfo->ret;
7445 MonoInst *inst = cfg->ret;
7447 for (quad = 0; quad < 2; quad ++) {
7448 switch (ainfo->pair_storage [quad]) {
7450 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7452 case ArgInFloatSSEReg:
7453 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7455 case ArgInDoubleSSEReg:
7456 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7461 g_assert_not_reached ();
7466 if (cfg->arch.omit_fp) {
7467 if (cfg->arch.stack_alloc_size) {
7468 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7472 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7474 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7475 async_exc_point (code);
7478 /* Restore the unwind state to be the same as before the epilog */
7479 mono_emit_unwind_op_restore_state (cfg, code);
7481 cfg->code_len = code - cfg->native_code;
7483 g_assert (cfg->code_len < cfg->code_size);
7487 mono_arch_emit_exceptions (MonoCompile *cfg)
7489 MonoJumpInfo *patch_info;
7492 MonoClass *exc_classes [16];
7493 guint8 *exc_throw_start [16], *exc_throw_end [16];
7494 guint32 code_size = 0;
7496 /* Compute needed space */
7497 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7498 if (patch_info->type == MONO_PATCH_INFO_EXC)
7500 if (patch_info->type == MONO_PATCH_INFO_R8)
7501 code_size += 8 + 15; /* sizeof (double) + alignment */
7502 if (patch_info->type == MONO_PATCH_INFO_R4)
7503 code_size += 4 + 15; /* sizeof (float) + alignment */
7504 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7505 code_size += 8 + 7; /*sizeof (void*) + alignment */
7508 #ifdef __native_client_codegen__
7509 /* Give us extra room on Native Client. This could be */
7510 /* more carefully calculated, but bundle alignment makes */
7511 /* it much trickier, so *2 like other places is good. */
7515 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7516 cfg->code_size *= 2;
7517 cfg->native_code = mono_realloc_native_code (cfg);
7518 cfg->stat_code_reallocs++;
7521 code = cfg->native_code + cfg->code_len;
7523 /* add code to raise exceptions */
7525 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7526 switch (patch_info->type) {
7527 case MONO_PATCH_INFO_EXC: {
7528 MonoClass *exc_class;
7532 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7534 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7535 g_assert (exc_class);
7536 throw_ip = patch_info->ip.i;
7538 //x86_breakpoint (code);
7539 /* Find a throw sequence for the same exception class */
7540 for (i = 0; i < nthrows; ++i)
7541 if (exc_classes [i] == exc_class)
7544 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7545 x86_jump_code (code, exc_throw_start [i]);
7546 patch_info->type = MONO_PATCH_INFO_NONE;
7550 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7554 exc_classes [nthrows] = exc_class;
7555 exc_throw_start [nthrows] = code;
7557 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7559 patch_info->type = MONO_PATCH_INFO_NONE;
7561 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7563 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7568 exc_throw_end [nthrows] = code;
7578 g_assert(code < cfg->native_code + cfg->code_size);
7581 /* Handle relocations with RIP relative addressing */
7582 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7583 gboolean remove = FALSE;
7584 guint8 *orig_code = code;
7586 switch (patch_info->type) {
7587 case MONO_PATCH_INFO_R8:
7588 case MONO_PATCH_INFO_R4: {
7589 guint8 *pos, *patch_pos;
7592 /* The SSE opcodes require a 16 byte alignment */
7593 #if defined(__default_codegen__)
7594 code = (guint8*)ALIGN_TO (code, 16);
7595 #elif defined(__native_client_codegen__)
7597 /* Pad this out with HLT instructions */
7598 /* or we can get garbage bytes emitted */
7599 /* which will fail validation */
7600 guint8 *aligned_code;
7601 /* extra align to make room for */
7602 /* mov/push below */
7603 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7604 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7605 /* The technique of hiding data in an */
7606 /* instruction has a problem here: we */
7607 /* need the data aligned to a 16-byte */
7608 /* boundary but the instruction cannot */
7609 /* cross the bundle boundary. so only */
7610 /* odd multiples of 16 can be used */
7611 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7614 while (code < aligned_code) {
7615 *(code++) = 0xf4; /* hlt */
7620 pos = cfg->native_code + patch_info->ip.i;
7621 if (IS_REX (pos [1])) {
7622 patch_pos = pos + 5;
7623 target_pos = code - pos - 9;
7626 patch_pos = pos + 4;
7627 target_pos = code - pos - 8;
7630 if (patch_info->type == MONO_PATCH_INFO_R8) {
7631 #ifdef __native_client_codegen__
7632 /* Hide 64-bit data in a */
7633 /* "mov imm64, r11" instruction. */
7634 /* write it before the start of */
7636 *(code-2) = 0x49; /* prefix */
7637 *(code-1) = 0xbb; /* mov X, %r11 */
7639 *(double*)code = *(double*)patch_info->data.target;
7640 code += sizeof (double);
7642 #ifdef __native_client_codegen__
7643 /* Hide 32-bit data in a */
7644 /* "push imm32" instruction. */
7645 *(code-1) = 0x68; /* push */
7647 *(float*)code = *(float*)patch_info->data.target;
7648 code += sizeof (float);
7651 *(guint32*)(patch_pos) = target_pos;
7656 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7659 if (cfg->compile_aot)
7662 /*loading is faster against aligned addresses.*/
7663 code = (guint8*)ALIGN_TO (code, 8);
7664 memset (orig_code, 0, code - orig_code);
7666 pos = cfg->native_code + patch_info->ip.i;
7668 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7669 if (IS_REX (pos [1]))
7670 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7672 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7674 *(gpointer*)code = (gpointer)patch_info->data.target;
7675 code += sizeof (gpointer);
7685 if (patch_info == cfg->patch_info)
7686 cfg->patch_info = patch_info->next;
7690 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7692 tmp->next = patch_info->next;
7695 g_assert (code < cfg->native_code + cfg->code_size);
7698 cfg->code_len = code - cfg->native_code;
7700 g_assert (cfg->code_len < cfg->code_size);
7704 #endif /* DISABLE_JIT */
7707 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7710 MonoMethodSignature *sig;
7712 int i, n, stack_area = 0;
7714 /* Keep this in sync with mono_arch_get_argument_info */
7716 if (enable_arguments) {
7717 /* Allocate a new area on the stack and save arguments there */
7718 sig = mono_method_signature (cfg->method);
7720 n = sig->param_count + sig->hasthis;
7722 stack_area = ALIGN_TO (n * 8, 16);
7724 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7726 for (i = 0; i < n; ++i) {
7727 inst = cfg->args [i];
7729 if (inst->opcode == OP_REGVAR)
7730 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7732 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7733 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7738 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7739 amd64_set_reg_template (code, AMD64_ARG_REG1);
7740 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7741 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7743 if (enable_arguments)
7744 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7758 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7761 int save_mode = SAVE_NONE;
7762 MonoMethod *method = cfg->method;
7763 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
7766 switch (ret_type->type) {
7767 case MONO_TYPE_VOID:
7768 /* special case string .ctor icall */
7769 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7770 save_mode = SAVE_EAX;
7772 save_mode = SAVE_NONE;
7776 save_mode = SAVE_EAX;
7780 save_mode = SAVE_XMM;
7782 case MONO_TYPE_GENERICINST:
7783 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7784 save_mode = SAVE_EAX;
7788 case MONO_TYPE_VALUETYPE:
7789 save_mode = SAVE_STRUCT;
7792 save_mode = SAVE_EAX;
7796 /* Save the result and copy it into the proper argument register */
7797 switch (save_mode) {
7799 amd64_push_reg (code, AMD64_RAX);
7801 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7802 if (enable_arguments)
7803 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7807 if (enable_arguments)
7808 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7811 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7812 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7814 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7816 * The result is already in the proper argument register so no copying
7823 g_assert_not_reached ();
7826 /* Set %al since this is a varargs call */
7827 if (save_mode == SAVE_XMM)
7828 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7830 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7832 if (preserve_argument_registers) {
7833 for (i = 0; i < PARAM_REGS; ++i)
7834 amd64_push_reg (code, param_regs [i]);
7837 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7838 amd64_set_reg_template (code, AMD64_ARG_REG1);
7839 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7841 if (preserve_argument_registers) {
7842 for (i = PARAM_REGS - 1; i >= 0; --i)
7843 amd64_pop_reg (code, param_regs [i]);
7846 /* Restore result */
7847 switch (save_mode) {
7849 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7850 amd64_pop_reg (code, AMD64_RAX);
7856 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7857 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7858 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7863 g_assert_not_reached ();
7870 mono_arch_flush_icache (guint8 *code, gint size)
7876 mono_arch_flush_register_windows (void)
7881 mono_arch_is_inst_imm (gint64 imm)
7883 return amd64_use_imm32 (imm);
7887 * Determine whenever the trap whose info is in SIGINFO is caused by
7891 mono_arch_is_int_overflow (void *sigctx, void *info)
7898 mono_sigctx_to_monoctx (sigctx, &ctx);
7900 rip = (guint8*)ctx.gregs [AMD64_RIP];
7902 if (IS_REX (rip [0])) {
7903 reg = amd64_rex_b (rip [0]);
7909 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7911 reg += x86_modrm_rm (rip [1]);
7913 value = ctx.gregs [reg];
7923 mono_arch_get_patch_offset (guint8 *code)
7929 * mono_breakpoint_clean_code:
7931 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
7932 * breakpoints in the original code, they are removed in the copy.
7934 * Returns TRUE if no sw breakpoint was present.
7937 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
7940 * If method_start is non-NULL we need to perform bound checks, since we access memory
7941 * at code - offset we could go before the start of the method and end up in a different
7942 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
7945 if (!method_start || code - offset >= method_start) {
7946 memcpy (buf, code - offset, size);
7948 int diff = code - method_start;
7949 memset (buf, 0, size);
7950 memcpy (buf + offset - diff, method_start, diff + size - offset);
7955 #if defined(__native_client_codegen__)
7956 /* For membase calls, we want the base register. for Native Client, */
7957 /* all indirect calls have the following sequence with the given sizes: */
7958 /* mov %eXX,%eXX [2-3] */
7959 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
7960 /* and $0xffffffffffffffe0,%r11d [4] */
7961 /* add %r15,%r11 [3] */
7962 /* callq *%r11 [3] */
7965 /* Determine if code points to a NaCl call-through-register sequence, */
7966 /* (i.e., the last 3 instructions listed above) */
7968 is_nacl_call_reg_sequence(guint8* code)
7970 const char *sequence = "\x41\x83\xe3\xe0" /* and */
7971 "\x4d\x03\xdf" /* add */
7972 "\x41\xff\xd3"; /* call */
7973 return memcmp(code, sequence, 10) == 0;
7976 /* Determine if code points to the first opcode of the mov membase component */
7977 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
7978 /* (there could be a REX prefix before the opcode but it is ignored) */
7980 is_nacl_indirect_call_membase_sequence(guint8* code)
7982 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
7983 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
7984 /* and that src reg = dest reg */
7985 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
7986 /* Check that next inst is mov, uses SIB byte (rm = 4), */
7988 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
7989 /* and has dst of r11 and base of r15 */
7990 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
7991 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
7993 #endif /* __native_client_codegen__ */
7996 mono_arch_get_this_arg_reg (guint8 *code)
7998 return AMD64_ARG_REG1;
8002 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
8004 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
8007 #define MAX_ARCH_DELEGATE_PARAMS 10
8010 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, guint32 param_count)
8012 guint8 *code, *start;
8013 GSList *unwind_ops = NULL;
8016 unwind_ops = mono_arch_get_cie_program ();
8019 start = code = mono_global_codeman_reserve (64);
8021 /* Replace the this argument with the target */
8022 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8023 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8024 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8026 g_assert ((code - start) < 64);
8028 start = code = mono_global_codeman_reserve (64);
8030 if (param_count == 0) {
8031 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8033 /* We have to shift the arguments left */
8034 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8035 for (i = 0; i < param_count; ++i) {
8038 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8040 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
8042 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8046 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8048 g_assert ((code - start) < 64);
8051 nacl_global_codeman_validate (&start, 64, &code);
8052 mono_arch_flush_icache (start, code - start);
8055 *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
8057 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
8058 *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
8062 if (mono_jit_map_is_enabled ()) {
8065 buff = (char*)"delegate_invoke_has_target";
8067 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
8068 mono_emit_jit_tramp (start, code - start, buff);
8072 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8077 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
8080 get_delegate_virtual_invoke_impl (MonoTrampInfo **info, gboolean load_imt_reg, int offset)
8082 guint8 *code, *start;
8087 if (offset / (int)sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
8090 start = code = mono_global_codeman_reserve (size);
8092 unwind_ops = mono_arch_get_cie_program ();
8094 /* Replace the this argument with the target */
8095 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8096 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8099 /* Load the IMT reg */
8100 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
8103 /* Load the vtable */
8104 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
8105 amd64_jump_membase (code, AMD64_RAX, offset);
8106 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8109 tramp_name = g_strdup_printf ("delegate_virtual_invoke_imt_%d", - offset / sizeof (gpointer));
8111 tramp_name = g_strdup_printf ("delegate_virtual_invoke_%d", offset / sizeof (gpointer));
8112 *info = mono_tramp_info_create (tramp_name, start, code - start, NULL, unwind_ops);
8113 g_free (tramp_name);
8119 * mono_arch_get_delegate_invoke_impls:
8121 * Return a list of MonoTrampInfo structures for the delegate invoke impl
8125 mono_arch_get_delegate_invoke_impls (void)
8128 MonoTrampInfo *info;
8131 get_delegate_invoke_impl (&info, TRUE, 0);
8132 res = g_slist_prepend (res, info);
8134 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
8135 get_delegate_invoke_impl (&info, FALSE, i);
8136 res = g_slist_prepend (res, info);
8139 for (i = 0; i <= MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
8140 get_delegate_virtual_invoke_impl (&info, TRUE, - i * SIZEOF_VOID_P);
8141 res = g_slist_prepend (res, info);
8143 get_delegate_virtual_invoke_impl (&info, FALSE, i * SIZEOF_VOID_P);
8144 res = g_slist_prepend (res, info);
8151 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
8153 guint8 *code, *start;
8156 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
8159 /* FIXME: Support more cases */
8160 if (MONO_TYPE_ISSTRUCT (mini_get_underlying_type (sig->ret)))
8164 static guint8* cached = NULL;
8169 if (mono_aot_only) {
8170 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
8172 MonoTrampInfo *info;
8173 start = get_delegate_invoke_impl (&info, TRUE, 0);
8174 mono_tramp_info_register (info, NULL);
8177 mono_memory_barrier ();
8181 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
8182 for (i = 0; i < sig->param_count; ++i)
8183 if (!mono_is_regsize_var (sig->params [i]))
8185 if (sig->param_count > 4)
8188 code = cache [sig->param_count];
8192 if (mono_aot_only) {
8193 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
8194 start = mono_aot_get_trampoline (name);
8197 MonoTrampInfo *info;
8198 start = get_delegate_invoke_impl (&info, FALSE, sig->param_count);
8199 mono_tramp_info_register (info, NULL);
8202 mono_memory_barrier ();
8204 cache [sig->param_count] = start;
8211 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
8213 MonoTrampInfo *info;
8216 code = get_delegate_virtual_invoke_impl (&info, load_imt_reg, offset);
8218 mono_tramp_info_register (info, NULL);
8223 mono_arch_finish_init (void)
8225 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
8226 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
8231 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8235 #if defined(__default_codegen__)
8236 #define CMP_SIZE (6 + 1)
8237 #define CMP_REG_REG_SIZE (4 + 1)
8238 #define BR_SMALL_SIZE 2
8239 #define BR_LARGE_SIZE 6
8240 #define MOV_REG_IMM_SIZE 10
8241 #define MOV_REG_IMM_32BIT_SIZE 6
8242 #define JUMP_REG_SIZE (2 + 1)
8243 #elif defined(__native_client_codegen__)
8244 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8245 #define CMP_SIZE ((6 + 1) * 2 - 1)
8246 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8247 #define BR_SMALL_SIZE (2 * 2 - 1)
8248 #define BR_LARGE_SIZE (6 * 2 - 1)
8249 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8250 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8251 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8252 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8253 /* Jump membase's size is large and unpredictable */
8254 /* in native client, just pad it out a whole bundle. */
8255 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8259 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8261 int i, distance = 0;
8262 for (i = start; i < target; ++i)
8263 distance += imt_entries [i]->chunk_size;
8268 * LOCKING: called with the domain lock held
8271 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8272 gpointer fail_tramp)
8276 guint8 *code, *start;
8277 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8280 for (i = 0; i < count; ++i) {
8281 MonoIMTCheckItem *item = imt_entries [i];
8282 if (item->is_equals) {
8283 if (item->check_target_idx) {
8284 if (!item->compare_done) {
8285 if (amd64_use_imm32 ((gint64)item->key))
8286 item->chunk_size += CMP_SIZE;
8288 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8290 if (item->has_target_code) {
8291 item->chunk_size += MOV_REG_IMM_SIZE;
8293 if (vtable_is_32bit)
8294 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8296 item->chunk_size += MOV_REG_IMM_SIZE;
8297 #ifdef __native_client_codegen__
8298 item->chunk_size += JUMP_MEMBASE_SIZE;
8301 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8304 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8305 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8307 if (vtable_is_32bit)
8308 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8310 item->chunk_size += MOV_REG_IMM_SIZE;
8311 item->chunk_size += JUMP_REG_SIZE;
8312 /* with assert below:
8313 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8315 #ifdef __native_client_codegen__
8316 item->chunk_size += JUMP_MEMBASE_SIZE;
8321 if (amd64_use_imm32 ((gint64)item->key))
8322 item->chunk_size += CMP_SIZE;
8324 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8325 item->chunk_size += BR_LARGE_SIZE;
8326 imt_entries [item->check_target_idx]->compare_done = TRUE;
8328 size += item->chunk_size;
8330 #if defined(__native_client__) && defined(__native_client_codegen__)
8331 /* In Native Client, we don't re-use thunks, allocate from the */
8332 /* normal code manager paths. */
8333 code = mono_domain_code_reserve (domain, size);
8336 code = mono_method_alloc_generic_virtual_thunk (domain, size);
8338 code = mono_domain_code_reserve (domain, size);
8342 unwind_ops = mono_arch_get_cie_program ();
8344 for (i = 0; i < count; ++i) {
8345 MonoIMTCheckItem *item = imt_entries [i];
8346 item->code_target = code;
8347 if (item->is_equals) {
8348 gboolean fail_case = !item->check_target_idx && fail_tramp;
8350 if (item->check_target_idx || fail_case) {
8351 if (!item->compare_done || fail_case) {
8352 if (amd64_use_imm32 ((gint64)item->key))
8353 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8355 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof(gpointer));
8356 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8359 item->jmp_code = code;
8360 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8361 if (item->has_target_code) {
8362 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8363 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8365 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8366 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8370 amd64_patch (item->jmp_code, code);
8371 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8372 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8373 item->jmp_code = NULL;
8376 /* enable the commented code to assert on wrong method */
8378 if (amd64_is_imm32 (item->key))
8379 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8381 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8382 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8384 item->jmp_code = code;
8385 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8386 /* See the comment below about R10 */
8387 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8388 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8389 amd64_patch (item->jmp_code, code);
8390 amd64_breakpoint (code);
8391 item->jmp_code = NULL;
8393 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8394 needs to be preserved. R10 needs
8395 to be preserved for calls which
8396 require a runtime generic context,
8397 but interface calls don't. */
8398 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8399 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8403 if (amd64_use_imm32 ((gint64)item->key))
8404 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8406 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof (gpointer));
8407 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8409 item->jmp_code = code;
8410 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8411 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8413 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8415 g_assert (code - item->code_target <= item->chunk_size);
8417 /* patch the branches to get to the target items */
8418 for (i = 0; i < count; ++i) {
8419 MonoIMTCheckItem *item = imt_entries [i];
8420 if (item->jmp_code) {
8421 if (item->check_target_idx) {
8422 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8428 mono_stats.imt_thunks_size += code - start;
8429 g_assert (code - start <= size);
8431 nacl_domain_code_validate(domain, &start, size, &code);
8432 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8434 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
8440 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8442 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8446 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8448 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8452 mono_arch_get_cie_program (void)
8456 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8457 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8465 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8467 MonoInst *ins = NULL;
8470 if (cmethod->klass == mono_defaults.math_class) {
8471 if (strcmp (cmethod->name, "Sin") == 0) {
8473 } else if (strcmp (cmethod->name, "Cos") == 0) {
8475 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8477 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8481 if (opcode && fsig->param_count == 1) {
8482 MONO_INST_NEW (cfg, ins, opcode);
8483 ins->type = STACK_R8;
8484 ins->dreg = mono_alloc_freg (cfg);
8485 ins->sreg1 = args [0]->dreg;
8486 MONO_ADD_INS (cfg->cbb, ins);
8490 if (cfg->opt & MONO_OPT_CMOV) {
8491 if (strcmp (cmethod->name, "Min") == 0) {
8492 if (fsig->params [0]->type == MONO_TYPE_I4)
8494 if (fsig->params [0]->type == MONO_TYPE_U4)
8495 opcode = OP_IMIN_UN;
8496 else if (fsig->params [0]->type == MONO_TYPE_I8)
8498 else if (fsig->params [0]->type == MONO_TYPE_U8)
8499 opcode = OP_LMIN_UN;
8500 } else if (strcmp (cmethod->name, "Max") == 0) {
8501 if (fsig->params [0]->type == MONO_TYPE_I4)
8503 if (fsig->params [0]->type == MONO_TYPE_U4)
8504 opcode = OP_IMAX_UN;
8505 else if (fsig->params [0]->type == MONO_TYPE_I8)
8507 else if (fsig->params [0]->type == MONO_TYPE_U8)
8508 opcode = OP_LMAX_UN;
8512 if (opcode && fsig->param_count == 2) {
8513 MONO_INST_NEW (cfg, ins, opcode);
8514 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8515 ins->dreg = mono_alloc_ireg (cfg);
8516 ins->sreg1 = args [0]->dreg;
8517 ins->sreg2 = args [1]->dreg;
8518 MONO_ADD_INS (cfg->cbb, ins);
8522 /* OP_FREM is not IEEE compatible */
8523 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8524 MONO_INST_NEW (cfg, ins, OP_FREM);
8525 ins->inst_i0 = args [0];
8526 ins->inst_i1 = args [1];
8536 mono_arch_print_tree (MonoInst *tree, int arity)
8542 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8544 return ctx->gregs [reg];
8548 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8550 ctx->gregs [reg] = val;
8554 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8556 gpointer *sp, old_value;
8560 bp = MONO_CONTEXT_GET_BP (ctx);
8561 sp = *(gpointer*)(bp + clause->exvar_offset);
8564 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8573 * mono_arch_emit_load_aotconst:
8575 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8576 * TARGET from the mscorlib GOT in full-aot code.
8577 * On AMD64, the result is placed into R11.
8580 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
8582 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8583 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8589 * mono_arch_get_trampolines:
8591 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8595 mono_arch_get_trampolines (gboolean aot)
8597 return mono_amd64_get_exception_trampolines (aot);
8600 /* Soft Debug support */
8601 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8604 * mono_arch_set_breakpoint:
8606 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8607 * The location should contain code emitted by OP_SEQ_POINT.
8610 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8615 guint32 native_offset = ip - (guint8*)ji->code_start;
8616 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8618 g_assert (info->bp_addrs [native_offset] == 0);
8619 info->bp_addrs [native_offset] = mini_get_breakpoint_trampoline ();
8621 /* ip points to a mov r11, 0 */
8622 g_assert (code [0] == 0x41);
8623 g_assert (code [1] == 0xbb);
8624 amd64_mov_reg_imm (code, AMD64_R11, 1);
8629 * mono_arch_clear_breakpoint:
8631 * Clear the breakpoint at IP.
8634 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8639 guint32 native_offset = ip - (guint8*)ji->code_start;
8640 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8642 info->bp_addrs [native_offset] = NULL;
8644 amd64_mov_reg_imm (code, AMD64_R11, 0);
8649 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8651 /* We use soft breakpoints on amd64 */
8656 * mono_arch_skip_breakpoint:
8658 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8659 * we resume, the instruction is not executed again.
8662 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8664 g_assert_not_reached ();
8668 * mono_arch_start_single_stepping:
8670 * Start single stepping.
8673 mono_arch_start_single_stepping (void)
8675 ss_trampoline = mini_get_single_step_trampoline ();
8679 * mono_arch_stop_single_stepping:
8681 * Stop single stepping.
8684 mono_arch_stop_single_stepping (void)
8686 ss_trampoline = NULL;
8690 * mono_arch_is_single_step_event:
8692 * Return whenever the machine state in SIGCTX corresponds to a single
8696 mono_arch_is_single_step_event (void *info, void *sigctx)
8698 /* We use soft breakpoints on amd64 */
8703 * mono_arch_skip_single_step:
8705 * Modify CTX so the ip is placed after the single step trigger instruction,
8706 * we resume, the instruction is not executed again.
8709 mono_arch_skip_single_step (MonoContext *ctx)
8711 g_assert_not_reached ();
8715 * mono_arch_create_seq_point_info:
8717 * Return a pointer to a data structure which is used by the sequence
8718 * point implementation in AOTed code.
8721 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8726 // FIXME: Add a free function
8728 mono_domain_lock (domain);
8729 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8731 mono_domain_unlock (domain);
8734 ji = mono_jit_info_table_find (domain, (char*)code);
8737 // FIXME: Optimize the size
8738 info = g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8740 info->ss_tramp_addr = &ss_trampoline;
8742 mono_domain_lock (domain);
8743 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8745 mono_domain_unlock (domain);
8752 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8754 ext->lmf.previous_lmf = prev_lmf;
8755 /* Mark that this is a MonoLMFExt */
8756 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8757 ext->lmf.rsp = (gssize)ext;
8763 mono_arch_opcode_supported (int opcode)
8766 case OP_ATOMIC_ADD_I4:
8767 case OP_ATOMIC_ADD_I8:
8768 case OP_ATOMIC_EXCHANGE_I4:
8769 case OP_ATOMIC_EXCHANGE_I8:
8770 case OP_ATOMIC_CAS_I4:
8771 case OP_ATOMIC_CAS_I8:
8772 case OP_ATOMIC_LOAD_I1:
8773 case OP_ATOMIC_LOAD_I2:
8774 case OP_ATOMIC_LOAD_I4:
8775 case OP_ATOMIC_LOAD_I8:
8776 case OP_ATOMIC_LOAD_U1:
8777 case OP_ATOMIC_LOAD_U2:
8778 case OP_ATOMIC_LOAD_U4:
8779 case OP_ATOMIC_LOAD_U8:
8780 case OP_ATOMIC_LOAD_R4:
8781 case OP_ATOMIC_LOAD_R8:
8782 case OP_ATOMIC_STORE_I1:
8783 case OP_ATOMIC_STORE_I2:
8784 case OP_ATOMIC_STORE_I4:
8785 case OP_ATOMIC_STORE_I8:
8786 case OP_ATOMIC_STORE_U1:
8787 case OP_ATOMIC_STORE_U2:
8788 case OP_ATOMIC_STORE_U4:
8789 case OP_ATOMIC_STORE_U8:
8790 case OP_ATOMIC_STORE_R4:
8791 case OP_ATOMIC_STORE_R8:
8798 #if defined(ENABLE_GSHAREDVT)
8800 #include "../../../mono-extensions/mono/mini/mini-amd64-gsharedvt.c"
8802 #endif /* !MONOTOUCH */