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)
15 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
24 #include <mono/metadata/abi-details.h>
25 #include <mono/metadata/appdomain.h>
26 #include <mono/metadata/debug-helpers.h>
27 #include <mono/metadata/threads.h>
28 #include <mono/metadata/profiler-private.h>
29 #include <mono/metadata/mono-debug.h>
30 #include <mono/metadata/gc-internals.h>
31 #include <mono/utils/mono-math.h>
32 #include <mono/utils/mono-mmap.h>
33 #include <mono/utils/mono-memory-model.h>
34 #include <mono/utils/mono-tls.h>
35 #include <mono/utils/mono-hwcap-x86.h>
36 #include <mono/utils/mono-threads.h>
40 #include "mini-amd64.h"
41 #include "cpu-amd64.h"
42 #include "debugger-agent.h"
46 static gboolean optimize_for_xen = TRUE;
48 #define optimize_for_xen 0
51 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
53 #define IS_IMM32(val) ((((guint64)val) >> 32) == 0)
55 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
58 /* Under windows, the calling convention is never stdcall */
59 #define CALLCONV_IS_STDCALL(call_conv) (FALSE)
61 #define CALLCONV_IS_STDCALL(call_conv) ((call_conv) == MONO_CALL_STDCALL)
64 /* This mutex protects architecture specific caches */
65 #define mono_mini_arch_lock() mono_os_mutex_lock (&mini_arch_mutex)
66 #define mono_mini_arch_unlock() mono_os_mutex_unlock (&mini_arch_mutex)
67 static mono_mutex_t mini_arch_mutex;
69 /* The single step trampoline */
70 static gpointer ss_trampoline;
72 /* The breakpoint trampoline */
73 static gpointer bp_trampoline;
75 /* Offset between fp and the first argument in the callee */
76 #define ARGS_OFFSET 16
77 #define GP_SCRATCH_REG AMD64_R11
80 * AMD64 register usage:
81 * - callee saved registers are used for global register allocation
82 * - %r11 is used for materializing 64 bit constants in opcodes
83 * - the rest is used for local allocation
87 * Floating point comparison results:
97 mono_arch_regname (int reg)
100 case AMD64_RAX: return "%rax";
101 case AMD64_RBX: return "%rbx";
102 case AMD64_RCX: return "%rcx";
103 case AMD64_RDX: return "%rdx";
104 case AMD64_RSP: return "%rsp";
105 case AMD64_RBP: return "%rbp";
106 case AMD64_RDI: return "%rdi";
107 case AMD64_RSI: return "%rsi";
108 case AMD64_R8: return "%r8";
109 case AMD64_R9: return "%r9";
110 case AMD64_R10: return "%r10";
111 case AMD64_R11: return "%r11";
112 case AMD64_R12: return "%r12";
113 case AMD64_R13: return "%r13";
114 case AMD64_R14: return "%r14";
115 case AMD64_R15: return "%r15";
120 static const char * packed_xmmregs [] = {
121 "p:xmm0", "p:xmm1", "p:xmm2", "p:xmm3", "p:xmm4", "p:xmm5", "p:xmm6", "p:xmm7", "p:xmm8",
122 "p:xmm9", "p:xmm10", "p:xmm11", "p:xmm12", "p:xmm13", "p:xmm14", "p:xmm15"
125 static const char * single_xmmregs [] = {
126 "s:xmm0", "s:xmm1", "s:xmm2", "s:xmm3", "s:xmm4", "s:xmm5", "s:xmm6", "s:xmm7", "s:xmm8",
127 "s:xmm9", "s:xmm10", "s:xmm11", "s:xmm12", "s:xmm13", "s:xmm14", "s:xmm15"
131 mono_arch_fregname (int reg)
133 if (reg < AMD64_XMM_NREG)
134 return single_xmmregs [reg];
140 mono_arch_xregname (int reg)
142 if (reg < AMD64_XMM_NREG)
143 return packed_xmmregs [reg];
152 return mono_debug_count ();
158 static inline gboolean
159 amd64_is_near_call (guint8 *code)
162 if ((code [0] >= 0x40) && (code [0] <= 0x4f))
165 return code [0] == 0xe8;
168 static inline gboolean
169 amd64_use_imm32 (gint64 val)
171 if (mini_get_debug_options()->single_imm_size)
174 return amd64_is_imm32 (val);
177 #ifdef __native_client_codegen__
179 /* Keep track of instruction "depth", that is, the level of sub-instruction */
180 /* for any given instruction. For instance, amd64_call_reg resolves to */
181 /* amd64_call_reg_internal, which uses amd64_alu_* macros, etc. */
182 /* We only want to force bundle alignment for the top level instruction, */
183 /* so NaCl pseudo-instructions can be implemented with sub instructions. */
184 static MonoNativeTlsKey nacl_instruction_depth;
186 static MonoNativeTlsKey nacl_rex_tag;
187 static MonoNativeTlsKey nacl_legacy_prefix_tag;
190 amd64_nacl_clear_legacy_prefix_tag ()
192 mono_native_tls_set_value (nacl_legacy_prefix_tag, NULL);
196 amd64_nacl_tag_legacy_prefix (guint8* code)
198 if (mono_native_tls_get_value (nacl_legacy_prefix_tag) == NULL)
199 mono_native_tls_set_value (nacl_legacy_prefix_tag, code);
203 amd64_nacl_tag_rex (guint8* code)
205 mono_native_tls_set_value (nacl_rex_tag, code);
209 amd64_nacl_get_legacy_prefix_tag ()
211 return (guint8*)mono_native_tls_get_value (nacl_legacy_prefix_tag);
215 amd64_nacl_get_rex_tag ()
217 return (guint8*)mono_native_tls_get_value (nacl_rex_tag);
220 /* Increment the instruction "depth" described above */
222 amd64_nacl_instruction_pre ()
224 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
226 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)depth);
229 /* amd64_nacl_instruction_post: Decrement instruction "depth", force bundle */
230 /* alignment if depth == 0 (top level instruction) */
231 /* IN: start, end pointers to instruction beginning and end */
232 /* OUT: start, end pointers to beginning and end after possible alignment */
233 /* GLOBALS: nacl_instruction_depth defined above */
235 amd64_nacl_instruction_post (guint8 **start, guint8 **end)
237 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
239 mono_native_tls_set_value (nacl_instruction_depth, (void*)depth);
241 g_assert ( depth >= 0 );
243 uintptr_t space_in_block;
245 guint8 *prefix = amd64_nacl_get_legacy_prefix_tag ();
246 /* if legacy prefix is present, and if it was emitted before */
247 /* the start of the instruction sequence, adjust the start */
248 if (prefix != NULL && prefix < *start) {
249 g_assert (*start - prefix <= 3);/* only 3 are allowed */
252 space_in_block = kNaClAlignment - ((uintptr_t)(*start) & kNaClAlignmentMask);
253 instlen = (uintptr_t)(*end - *start);
254 /* Only check for instructions which are less than */
255 /* kNaClAlignment. The only instructions that should ever */
256 /* be that long are call sequences, which are already */
257 /* padded out to align the return to the next bundle. */
258 if (instlen > space_in_block && instlen < kNaClAlignment) {
259 const size_t MAX_NACL_INST_LENGTH = kNaClAlignment;
260 guint8 copy_of_instruction[MAX_NACL_INST_LENGTH];
261 const size_t length = (size_t)((*end)-(*start));
262 g_assert (length < MAX_NACL_INST_LENGTH);
264 memcpy (copy_of_instruction, *start, length);
265 *start = mono_arch_nacl_pad (*start, space_in_block);
266 memcpy (*start, copy_of_instruction, length);
267 *end = *start + length;
269 amd64_nacl_clear_legacy_prefix_tag ();
270 amd64_nacl_tag_rex (NULL);
274 /* amd64_nacl_membase_handler: ensure all access to memory of the form */
275 /* OFFSET(%rXX) is sandboxed. For allowable base registers %rip, %rbp, */
276 /* %rsp, and %r15, emit the membase as usual. For all other registers, */
277 /* make sure the upper 32-bits are cleared, and use that register in the */
278 /* index field of a new address of this form: OFFSET(%r15,%eXX,1) */
280 /* pointer to current instruction stream (in the */
281 /* middle of an instruction, after opcode is emitted) */
282 /* basereg/offset/dreg */
283 /* operands of normal membase address */
285 /* pointer to the end of the membase/memindex emit */
286 /* GLOBALS: nacl_rex_tag */
287 /* position in instruction stream that rex prefix was emitted */
288 /* nacl_legacy_prefix_tag */
289 /* (possibly NULL) position in instruction of legacy x86 prefix */
291 amd64_nacl_membase_handler (guint8** code, gint8 basereg, gint32 offset, gint8 dreg)
293 gint8 true_basereg = basereg;
295 /* Cache these values, they might change */
296 /* as new instructions are emitted below. */
297 guint8* rex_tag = amd64_nacl_get_rex_tag ();
298 guint8* legacy_prefix_tag = amd64_nacl_get_legacy_prefix_tag ();
300 /* 'basereg' is given masked to 0x7 at this point, so check */
301 /* the rex prefix to see if this is an extended register. */
302 if ((rex_tag != NULL) && IS_REX(*rex_tag) && (*rex_tag & AMD64_REX_B)) {
306 #define X86_LEA_OPCODE (0x8D)
308 if (!amd64_is_valid_nacl_base (true_basereg) && (*(*code-1) != X86_LEA_OPCODE)) {
309 guint8* old_instruction_start;
311 /* This will hold the 'mov %eXX, %eXX' that clears the upper */
312 /* 32-bits of the old base register (new index register) */
314 guint8* buf_ptr = buf;
317 g_assert (rex_tag != NULL);
319 if (IS_REX(*rex_tag)) {
320 /* The old rex.B should be the new rex.X */
321 if (*rex_tag & AMD64_REX_B) {
322 *rex_tag |= AMD64_REX_X;
324 /* Since our new base is %r15 set rex.B */
325 *rex_tag |= AMD64_REX_B;
327 /* Shift the instruction by one byte */
328 /* so we can insert a rex prefix */
329 memmove (rex_tag + 1, rex_tag, (size_t)(*code - rex_tag));
331 /* New rex prefix only needs rex.B for %r15 base */
332 *rex_tag = AMD64_REX(AMD64_REX_B);
335 if (legacy_prefix_tag) {
336 old_instruction_start = legacy_prefix_tag;
338 old_instruction_start = rex_tag;
341 /* Clears the upper 32-bits of the previous base register */
342 amd64_mov_reg_reg_size (buf_ptr, true_basereg, true_basereg, 4);
343 insert_len = buf_ptr - buf;
345 /* Move the old instruction forward to make */
346 /* room for 'mov' stored in 'buf_ptr' */
347 memmove (old_instruction_start + insert_len, old_instruction_start, (size_t)(*code - old_instruction_start));
349 memcpy (old_instruction_start, buf, insert_len);
351 /* Sandboxed replacement for the normal membase_emit */
352 x86_memindex_emit (*code, dreg, AMD64_R15, offset, basereg, 0);
355 /* Normal default behavior, emit membase memory location */
356 x86_membase_emit_body (*code, dreg, basereg, offset);
361 static inline unsigned char*
362 amd64_skip_nops (unsigned char* code)
367 if ( code[0] == 0x90) {
371 if ( code[0] == 0x66 && code[1] == 0x90) {
375 if (code[0] == 0x0f && code[1] == 0x1f
376 && code[2] == 0x00) {
380 if (code[0] == 0x0f && code[1] == 0x1f
381 && code[2] == 0x40 && code[3] == 0x00) {
385 if (code[0] == 0x0f && code[1] == 0x1f
386 && code[2] == 0x44 && code[3] == 0x00
387 && code[4] == 0x00) {
391 if (code[0] == 0x66 && code[1] == 0x0f
392 && code[2] == 0x1f && code[3] == 0x44
393 && code[4] == 0x00 && code[5] == 0x00) {
397 if (code[0] == 0x0f && code[1] == 0x1f
398 && code[2] == 0x80 && code[3] == 0x00
399 && code[4] == 0x00 && code[5] == 0x00
400 && code[6] == 0x00) {
404 if (code[0] == 0x0f && code[1] == 0x1f
405 && code[2] == 0x84 && code[3] == 0x00
406 && code[4] == 0x00 && code[5] == 0x00
407 && code[6] == 0x00 && code[7] == 0x00) {
416 mono_arch_nacl_skip_nops (guint8* code)
418 return amd64_skip_nops(code);
421 #endif /*__native_client_codegen__*/
424 amd64_patch (unsigned char* code, gpointer target)
428 #ifdef __native_client_codegen__
429 code = amd64_skip_nops (code);
431 #if defined(__native_client_codegen__) && defined(__native_client__)
432 if (nacl_is_code_address (code)) {
433 /* For tail calls, code is patched after being installed */
434 /* but not through the normal "patch callsite" method. */
435 unsigned char buf[kNaClAlignment];
436 unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
438 memcpy (buf, aligned_code, kNaClAlignment);
439 /* Patch a temp buffer of bundle size, */
440 /* then install to actual location. */
441 amd64_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), target);
442 ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
446 target = nacl_modify_patch_target (target);
450 if ((code [0] >= 0x40) && (code [0] <= 0x4f)) {
455 if ((code [0] & 0xf8) == 0xb8) {
456 /* amd64_set_reg_template */
457 *(guint64*)(code + 1) = (guint64)target;
459 else if ((code [0] == 0x8b) && rex && x86_modrm_mod (code [1]) == 0 && x86_modrm_rm (code [1]) == 5) {
460 /* mov 0(%rip), %dreg */
461 *(guint32*)(code + 2) = (guint32)(guint64)target - 7;
463 else if ((code [0] == 0xff) && (code [1] == 0x15)) {
464 /* call *<OFFSET>(%rip) */
465 *(guint32*)(code + 2) = ((guint32)(guint64)target) - 7;
467 else if (code [0] == 0xe8) {
469 gint64 disp = (guint8*)target - (guint8*)code;
470 g_assert (amd64_is_imm32 (disp));
471 x86_patch (code, (unsigned char*)target);
474 x86_patch (code, (unsigned char*)target);
478 mono_amd64_patch (unsigned char* code, gpointer target)
480 amd64_patch (code, target);
483 #define DEBUG(a) if (cfg->verbose_level > 1) a
486 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
488 ainfo->offset = *stack_size;
490 if (*gr >= PARAM_REGS) {
491 ainfo->storage = ArgOnStack;
492 ainfo->arg_size = sizeof (mgreg_t);
493 /* Since the same stack slot size is used for all arg */
494 /* types, it needs to be big enough to hold them all */
495 (*stack_size) += sizeof(mgreg_t);
498 ainfo->storage = ArgInIReg;
499 ainfo->reg = param_regs [*gr];
505 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
507 ainfo->offset = *stack_size;
509 if (*gr >= FLOAT_PARAM_REGS) {
510 ainfo->storage = ArgOnStack;
511 ainfo->arg_size = sizeof (mgreg_t);
512 /* Since the same stack slot size is used for both float */
513 /* types, it needs to be big enough to hold them both */
514 (*stack_size) += sizeof(mgreg_t);
517 /* A double register */
519 ainfo->storage = ArgInDoubleSSEReg;
521 ainfo->storage = ArgInFloatSSEReg;
527 typedef enum ArgumentClass {
535 merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
537 ArgumentClass class2 = ARG_CLASS_NO_CLASS;
540 ptype = mini_get_underlying_type (type);
541 switch (ptype->type) {
550 case MONO_TYPE_STRING:
551 case MONO_TYPE_OBJECT:
552 case MONO_TYPE_CLASS:
553 case MONO_TYPE_SZARRAY:
555 case MONO_TYPE_FNPTR:
556 case MONO_TYPE_ARRAY:
559 class2 = ARG_CLASS_INTEGER;
564 class2 = ARG_CLASS_INTEGER;
566 class2 = ARG_CLASS_SSE;
570 case MONO_TYPE_TYPEDBYREF:
571 g_assert_not_reached ();
573 case MONO_TYPE_GENERICINST:
574 if (!mono_type_generic_inst_is_valuetype (ptype)) {
575 class2 = ARG_CLASS_INTEGER;
579 case MONO_TYPE_VALUETYPE: {
580 MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
583 for (i = 0; i < info->num_fields; ++i) {
585 class2 = merge_argument_class_from_type (info->fields [i].field->type, class2);
590 g_assert_not_reached ();
594 if (class1 == class2)
596 else if (class1 == ARG_CLASS_NO_CLASS)
598 else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
599 class1 = ARG_CLASS_MEMORY;
600 else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
601 class1 = ARG_CLASS_INTEGER;
603 class1 = ARG_CLASS_SSE;
607 #ifdef __native_client_codegen__
609 /* Default alignment for Native Client is 32-byte. */
610 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
612 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
613 /* Check that alignment doesn't cross an alignment boundary. */
615 mono_arch_nacl_pad(guint8 *code, int pad)
617 const int kMaxPadding = 8; /* see amd64-codegen.h:amd64_padding_size() */
619 if (pad == 0) return code;
620 /* assertion: alignment cannot cross a block boundary */
621 g_assert (((uintptr_t)code & (~kNaClAlignmentMask)) ==
622 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
623 while (pad >= kMaxPadding) {
624 amd64_padding (code, kMaxPadding);
627 if (pad != 0) amd64_padding (code, pad);
633 count_fields_nested (MonoClass *klass)
635 MonoMarshalType *info;
638 info = mono_marshal_load_type_info (klass);
641 for (i = 0; i < info->num_fields; ++i) {
642 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type))
643 count += count_fields_nested (mono_class_from_mono_type (info->fields [i].field->type));
651 collect_field_info_nested (MonoClass *klass, MonoMarshalField *fields, int index, int offset)
653 MonoMarshalType *info;
656 info = mono_marshal_load_type_info (klass);
658 for (i = 0; i < info->num_fields; ++i) {
659 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type)) {
660 index = collect_field_info_nested (mono_class_from_mono_type (info->fields [i].field->type), fields, index, info->fields [i].offset);
662 memcpy (&fields [index], &info->fields [i], sizeof (MonoMarshalField));
663 fields [index].offset += offset;
672 add_valuetype_win64 (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
674 guint32 *gr, guint32 *fr, guint32 *stack_size)
676 guint32 size, i, nfields;
678 ArgumentClass arg_class;
679 MonoMarshalType *info = NULL;
680 MonoMarshalField *fields = NULL;
682 gboolean pass_on_stack = FALSE;
684 klass = mono_class_from_mono_type (type);
685 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
687 pass_on_stack = TRUE;
689 /* If this struct can't be split up naturally into 8-byte */
690 /* chunks (registers), pass it on the stack. */
691 if (sig->pinvoke && !pass_on_stack) {
695 info = mono_marshal_load_type_info (klass);
699 * Collect field information recursively to be able to
700 * handle nested structures.
702 nfields = count_fields_nested (klass);
703 fields = g_new0 (MonoMarshalField, nfields);
704 collect_field_info_nested (klass, fields, 0, 0);
706 for (i = 0; i < nfields; ++i) {
707 field_size = mono_marshal_type_size (fields [i].field->type,
709 &align, TRUE, klass->unicode);
710 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
711 pass_on_stack = TRUE;
718 /* Allways pass in memory */
719 ainfo->offset = *stack_size;
720 *stack_size += ALIGN_TO (size, 8);
721 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
723 ainfo->arg_size = ALIGN_TO (size, 8);
730 int n = mono_class_value_size (klass, NULL);
735 arg_class = ARG_CLASS_MEMORY;
737 /* Always pass in 1 integer register */
738 arg_class = ARG_CLASS_INTEGER;
743 ainfo->storage = ArgValuetypeInReg;
744 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
748 switch (info->native_size) {
749 case 1: case 2: case 4: case 8:
753 ainfo->storage = ArgValuetypeAddrInIReg;
754 ainfo->offset = *stack_size;
755 *stack_size += ALIGN_TO (info->native_size, 8);
758 ainfo->storage = ArgValuetypeAddrInIReg;
760 if (*gr < PARAM_REGS) {
761 ainfo->pair_storage [0] = ArgInIReg;
762 ainfo->pair_regs [0] = param_regs [*gr];
766 ainfo->pair_storage [0] = ArgOnStack;
767 ainfo->offset = *stack_size;
768 ainfo->arg_size = sizeof (mgreg_t);
779 ArgumentClass class1;
782 class1 = ARG_CLASS_MEMORY;
784 class1 = ARG_CLASS_NO_CLASS;
785 for (i = 0; i < nfields; ++i) {
786 size = mono_marshal_type_size (fields [i].field->type,
788 &align, TRUE, klass->unicode);
789 /* How far into this quad this data extends.*/
790 /* (8 is size of quad) */
791 argsize = fields [i].offset + size;
793 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
795 g_assert (class1 != ARG_CLASS_NO_CLASS);
801 /* Allocate registers */
806 while (argsize != 1 && argsize != 2 && argsize != 4 && argsize != 8)
809 ainfo->storage = ArgValuetypeInReg;
810 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
811 ainfo->pair_size [0] = argsize;
812 ainfo->pair_size [1] = 0;
815 case ARG_CLASS_INTEGER:
816 if (*gr >= PARAM_REGS)
817 arg_class = ARG_CLASS_MEMORY;
819 ainfo->pair_storage [0] = ArgInIReg;
821 ainfo->pair_regs [0] = return_regs [*gr];
823 ainfo->pair_regs [0] = param_regs [*gr];
828 if (*fr >= FLOAT_PARAM_REGS)
829 arg_class = ARG_CLASS_MEMORY;
832 ainfo->pair_storage [0] = ArgInFloatSSEReg;
834 ainfo->pair_storage [0] = ArgInDoubleSSEReg;
835 ainfo->pair_regs [0] = *fr;
839 case ARG_CLASS_MEMORY:
842 g_assert_not_reached ();
845 if (arg_class == ARG_CLASS_MEMORY) {
846 /* Revert possible register assignments */
850 ainfo->offset = *stack_size;
851 *stack_size += sizeof (mgreg_t);
852 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
854 ainfo->arg_size = sizeof (mgreg_t);
858 #endif /* TARGET_WIN32 */
861 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
863 guint32 *gr, guint32 *fr, guint32 *stack_size)
866 add_valuetype_win64 (sig, ainfo, type, is_return, gr, fr, stack_size);
868 guint32 size, quad, nquads, i, nfields;
869 /* Keep track of the size used in each quad so we can */
870 /* use the right size when copying args/return vars. */
871 guint32 quadsize [2] = {8, 8};
872 ArgumentClass args [2];
873 MonoMarshalType *info = NULL;
874 MonoMarshalField *fields = NULL;
876 gboolean pass_on_stack = FALSE;
878 klass = mono_class_from_mono_type (type);
879 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
880 if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
881 /* We pass and return vtypes of size 8 in a register */
882 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
883 pass_on_stack = TRUE;
886 /* If this struct can't be split up naturally into 8-byte */
887 /* chunks (registers), pass it on the stack. */
888 if (sig->pinvoke && !pass_on_stack) {
892 info = mono_marshal_load_type_info (klass);
896 * Collect field information recursively to be able to
897 * handle nested structures.
899 nfields = count_fields_nested (klass);
900 fields = g_new0 (MonoMarshalField, nfields);
901 collect_field_info_nested (klass, fields, 0, 0);
903 for (i = 0; i < nfields; ++i) {
904 field_size = mono_marshal_type_size (fields [i].field->type,
906 &align, TRUE, klass->unicode);
907 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
908 pass_on_stack = TRUE;
915 ainfo->storage = ArgValuetypeInReg;
916 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
921 /* Allways pass in memory */
922 ainfo->offset = *stack_size;
923 *stack_size += ALIGN_TO (size, 8);
924 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
926 ainfo->arg_size = ALIGN_TO (size, 8);
938 int n = mono_class_value_size (klass, NULL);
940 quadsize [0] = n >= 8 ? 8 : n;
941 quadsize [1] = n >= 8 ? MAX (n - 8, 8) : 0;
943 /* Always pass in 1 or 2 integer registers */
944 args [0] = ARG_CLASS_INTEGER;
945 args [1] = ARG_CLASS_INTEGER;
946 /* Only the simplest cases are supported */
947 if (is_return && nquads != 1) {
948 args [0] = ARG_CLASS_MEMORY;
949 args [1] = ARG_CLASS_MEMORY;
953 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
954 * The X87 and SSEUP stuff is left out since there are no such types in
960 ainfo->storage = ArgValuetypeInReg;
961 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
965 if (info->native_size > 16) {
966 ainfo->offset = *stack_size;
967 *stack_size += ALIGN_TO (info->native_size, 8);
968 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
970 ainfo->arg_size = ALIGN_TO (info->native_size, 8);
976 args [0] = ARG_CLASS_NO_CLASS;
977 args [1] = ARG_CLASS_NO_CLASS;
978 for (quad = 0; quad < nquads; ++quad) {
981 ArgumentClass class1;
984 class1 = ARG_CLASS_MEMORY;
986 class1 = ARG_CLASS_NO_CLASS;
987 for (i = 0; i < nfields; ++i) {
988 size = mono_marshal_type_size (fields [i].field->type,
990 &align, TRUE, klass->unicode);
991 if ((fields [i].offset < 8) && (fields [i].offset + size) > 8) {
992 /* Unaligned field */
996 /* Skip fields in other quad */
997 if ((quad == 0) && (fields [i].offset >= 8))
999 if ((quad == 1) && (fields [i].offset < 8))
1002 /* How far into this quad this data extends.*/
1003 /* (8 is size of quad) */
1004 quadsize [quad] = fields [i].offset + size - (quad * 8);
1006 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
1008 g_assert (class1 != ARG_CLASS_NO_CLASS);
1009 args [quad] = class1;
1015 /* Post merger cleanup */
1016 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
1017 args [0] = args [1] = ARG_CLASS_MEMORY;
1019 /* Allocate registers */
1024 while (quadsize [0] != 1 && quadsize [0] != 2 && quadsize [0] != 4 && quadsize [0] != 8)
1026 while (quadsize [1] != 0 && quadsize [1] != 1 && quadsize [1] != 2 && quadsize [1] != 4 && quadsize [1] != 8)
1029 ainfo->storage = ArgValuetypeInReg;
1030 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
1031 g_assert (quadsize [0] <= 8);
1032 g_assert (quadsize [1] <= 8);
1033 ainfo->pair_size [0] = quadsize [0];
1034 ainfo->pair_size [1] = quadsize [1];
1035 ainfo->nregs = nquads;
1036 for (quad = 0; quad < nquads; ++quad) {
1037 switch (args [quad]) {
1038 case ARG_CLASS_INTEGER:
1039 if (*gr >= PARAM_REGS)
1040 args [quad] = ARG_CLASS_MEMORY;
1042 ainfo->pair_storage [quad] = ArgInIReg;
1044 ainfo->pair_regs [quad] = return_regs [*gr];
1046 ainfo->pair_regs [quad] = param_regs [*gr];
1051 if (*fr >= FLOAT_PARAM_REGS)
1052 args [quad] = ARG_CLASS_MEMORY;
1054 if (quadsize[quad] <= 4)
1055 ainfo->pair_storage [quad] = ArgInFloatSSEReg;
1056 else ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
1057 ainfo->pair_regs [quad] = *fr;
1061 case ARG_CLASS_MEMORY:
1064 g_assert_not_reached ();
1068 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
1070 /* Revert possible register assignments */
1074 ainfo->offset = *stack_size;
1076 arg_size = ALIGN_TO (info->native_size, 8);
1078 arg_size = nquads * sizeof(mgreg_t);
1079 *stack_size += arg_size;
1080 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
1082 ainfo->arg_size = arg_size;
1085 #endif /* !TARGET_WIN32 */
1091 * Obtain information about a call according to the calling convention.
1092 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
1093 * Draft Version 0.23" document for more information.
1096 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
1098 guint32 i, gr, fr, pstart;
1100 int n = sig->hasthis + sig->param_count;
1101 guint32 stack_size = 0;
1103 gboolean is_pinvoke = sig->pinvoke;
1106 cinfo = (CallInfo *)mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1108 cinfo = (CallInfo *)g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1111 cinfo->gsharedvt = mini_is_gsharedvt_variable_signature (sig);
1117 /* Reserve space where the callee can save the argument registers */
1118 stack_size = 4 * sizeof (mgreg_t);
1122 ret_type = mini_get_underlying_type (sig->ret);
1123 switch (ret_type->type) {
1133 case MONO_TYPE_FNPTR:
1134 case MONO_TYPE_CLASS:
1135 case MONO_TYPE_OBJECT:
1136 case MONO_TYPE_SZARRAY:
1137 case MONO_TYPE_ARRAY:
1138 case MONO_TYPE_STRING:
1139 cinfo->ret.storage = ArgInIReg;
1140 cinfo->ret.reg = AMD64_RAX;
1144 cinfo->ret.storage = ArgInIReg;
1145 cinfo->ret.reg = AMD64_RAX;
1148 cinfo->ret.storage = ArgInFloatSSEReg;
1149 cinfo->ret.reg = AMD64_XMM0;
1152 cinfo->ret.storage = ArgInDoubleSSEReg;
1153 cinfo->ret.reg = AMD64_XMM0;
1155 case MONO_TYPE_GENERICINST:
1156 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1157 cinfo->ret.storage = ArgInIReg;
1158 cinfo->ret.reg = AMD64_RAX;
1161 if (mini_is_gsharedvt_type (ret_type)) {
1162 cinfo->ret.storage = ArgGsharedvtVariableInReg;
1166 case MONO_TYPE_VALUETYPE:
1167 case MONO_TYPE_TYPEDBYREF: {
1168 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
1170 add_valuetype (sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
1171 g_assert (cinfo->ret.storage != ArgInIReg);
1175 case MONO_TYPE_MVAR:
1176 g_assert (mini_is_gsharedvt_type (ret_type));
1177 cinfo->ret.storage = ArgGsharedvtVariableInReg;
1179 case MONO_TYPE_VOID:
1182 g_error ("Can't handle as return value 0x%x", ret_type->type);
1187 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1188 * the first argument, allowing 'this' to be always passed in the first arg reg.
1189 * Also do this if the first argument is a reference type, since virtual calls
1190 * are sometimes made using calli without sig->hasthis set, like in the delegate
1193 ArgStorage ret_storage = cinfo->ret.storage;
1194 if ((ret_storage == ArgValuetypeAddrInIReg || ret_storage == ArgGsharedvtVariableInReg) && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
1196 add_general (&gr, &stack_size, cinfo->args + 0);
1198 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
1201 add_general (&gr, &stack_size, &cinfo->ret);
1202 cinfo->ret.storage = ret_storage;
1203 cinfo->vret_arg_index = 1;
1207 add_general (&gr, &stack_size, cinfo->args + 0);
1209 if (ret_storage == ArgValuetypeAddrInIReg || ret_storage == ArgGsharedvtVariableInReg) {
1210 add_general (&gr, &stack_size, &cinfo->ret);
1211 cinfo->ret.storage = ret_storage;
1215 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
1217 fr = FLOAT_PARAM_REGS;
1219 /* Emit the signature cookie just before the implicit arguments */
1220 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1223 for (i = pstart; i < sig->param_count; ++i) {
1224 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
1228 /* The float param registers and other param registers must be the same index on Windows x64.*/
1235 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1236 /* We allways pass the sig cookie on the stack for simplicity */
1238 * Prevent implicit arguments + the sig cookie from being passed
1242 fr = FLOAT_PARAM_REGS;
1244 /* Emit the signature cookie just before the implicit arguments */
1245 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1248 ptype = mini_get_underlying_type (sig->params [i]);
1249 switch (ptype->type) {
1252 add_general (&gr, &stack_size, ainfo);
1256 add_general (&gr, &stack_size, ainfo);
1260 add_general (&gr, &stack_size, ainfo);
1265 case MONO_TYPE_FNPTR:
1266 case MONO_TYPE_CLASS:
1267 case MONO_TYPE_OBJECT:
1268 case MONO_TYPE_STRING:
1269 case MONO_TYPE_SZARRAY:
1270 case MONO_TYPE_ARRAY:
1271 add_general (&gr, &stack_size, ainfo);
1273 case MONO_TYPE_GENERICINST:
1274 if (!mono_type_generic_inst_is_valuetype (ptype)) {
1275 add_general (&gr, &stack_size, ainfo);
1278 if (mini_is_gsharedvt_variable_type (ptype)) {
1279 /* gsharedvt arguments are passed by ref */
1280 add_general (&gr, &stack_size, ainfo);
1281 if (ainfo->storage == ArgInIReg)
1282 ainfo->storage = ArgGSharedVtInReg;
1284 ainfo->storage = ArgGSharedVtOnStack;
1288 case MONO_TYPE_VALUETYPE:
1289 case MONO_TYPE_TYPEDBYREF:
1290 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
1295 add_general (&gr, &stack_size, ainfo);
1298 add_float (&fr, &stack_size, ainfo, FALSE);
1301 add_float (&fr, &stack_size, ainfo, TRUE);
1304 case MONO_TYPE_MVAR:
1305 /* gsharedvt arguments are passed by ref */
1306 g_assert (mini_is_gsharedvt_type (ptype));
1307 add_general (&gr, &stack_size, ainfo);
1308 if (ainfo->storage == ArgInIReg)
1309 ainfo->storage = ArgGSharedVtInReg;
1311 ainfo->storage = ArgGSharedVtOnStack;
1314 g_assert_not_reached ();
1318 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
1320 fr = FLOAT_PARAM_REGS;
1322 /* Emit the signature cookie just before the implicit arguments */
1323 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1326 cinfo->stack_usage = stack_size;
1327 cinfo->reg_usage = gr;
1328 cinfo->freg_usage = fr;
1333 * mono_arch_get_argument_info:
1334 * @csig: a method signature
1335 * @param_count: the number of parameters to consider
1336 * @arg_info: an array to store the result infos
1338 * Gathers information on parameters such as size, alignment and
1339 * padding. arg_info should be large enought to hold param_count + 1 entries.
1341 * Returns the size of the argument area on the stack.
1344 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
1347 CallInfo *cinfo = get_call_info (NULL, csig);
1348 guint32 args_size = cinfo->stack_usage;
1350 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
1351 if (csig->hasthis) {
1352 arg_info [0].offset = 0;
1355 for (k = 0; k < param_count; k++) {
1356 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
1358 arg_info [k + 1].size = 0;
1367 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
1371 MonoType *callee_ret;
1373 c1 = get_call_info (NULL, caller_sig);
1374 c2 = get_call_info (NULL, callee_sig);
1375 res = c1->stack_usage >= c2->stack_usage;
1376 callee_ret = mini_get_underlying_type (callee_sig->ret);
1377 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
1378 /* An address on the callee's stack is passed as the first argument */
1388 * Initialize the cpu to execute managed code.
1391 mono_arch_cpu_init (void)
1396 /* spec compliance requires running with double precision */
1397 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1398 fpcw &= ~X86_FPCW_PRECC_MASK;
1399 fpcw |= X86_FPCW_PREC_DOUBLE;
1400 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
1401 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1403 /* TODO: This is crashing on Win64 right now.
1404 * _control87 (_PC_53, MCW_PC);
1410 * Initialize architecture specific code.
1413 mono_arch_init (void)
1415 mono_os_mutex_init_recursive (&mini_arch_mutex);
1416 #if defined(__native_client_codegen__)
1417 mono_native_tls_alloc (&nacl_instruction_depth, NULL);
1418 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)0);
1419 mono_native_tls_alloc (&nacl_rex_tag, NULL);
1420 mono_native_tls_alloc (&nacl_legacy_prefix_tag, NULL);
1423 mono_aot_register_jit_icall ("mono_amd64_throw_exception", mono_amd64_throw_exception);
1424 mono_aot_register_jit_icall ("mono_amd64_throw_corlib_exception", mono_amd64_throw_corlib_exception);
1425 mono_aot_register_jit_icall ("mono_amd64_resume_unwind", mono_amd64_resume_unwind);
1426 mono_aot_register_jit_icall ("mono_amd64_get_original_ip", mono_amd64_get_original_ip);
1427 #if defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
1428 mono_aot_register_jit_icall ("mono_amd64_start_gsharedvt_call", mono_amd64_start_gsharedvt_call);
1432 bp_trampoline = mini_get_breakpoint_trampoline ();
1436 * Cleanup architecture specific code.
1439 mono_arch_cleanup (void)
1441 mono_os_mutex_destroy (&mini_arch_mutex);
1442 #if defined(__native_client_codegen__)
1443 mono_native_tls_free (nacl_instruction_depth);
1444 mono_native_tls_free (nacl_rex_tag);
1445 mono_native_tls_free (nacl_legacy_prefix_tag);
1450 * This function returns the optimizations supported on this cpu.
1453 mono_arch_cpu_optimizations (guint32 *exclude_mask)
1459 if (mono_hwcap_x86_has_cmov) {
1460 opts |= MONO_OPT_CMOV;
1462 if (mono_hwcap_x86_has_fcmov)
1463 opts |= MONO_OPT_FCMOV;
1465 *exclude_mask |= MONO_OPT_FCMOV;
1467 *exclude_mask |= MONO_OPT_CMOV;
1474 * This function test for all SSE functions supported.
1476 * Returns a bitmask corresponding to all supported versions.
1480 mono_arch_cpu_enumerate_simd_versions (void)
1482 guint32 sse_opts = 0;
1484 if (mono_hwcap_x86_has_sse1)
1485 sse_opts |= SIMD_VERSION_SSE1;
1487 if (mono_hwcap_x86_has_sse2)
1488 sse_opts |= SIMD_VERSION_SSE2;
1490 if (mono_hwcap_x86_has_sse3)
1491 sse_opts |= SIMD_VERSION_SSE3;
1493 if (mono_hwcap_x86_has_ssse3)
1494 sse_opts |= SIMD_VERSION_SSSE3;
1496 if (mono_hwcap_x86_has_sse41)
1497 sse_opts |= SIMD_VERSION_SSE41;
1499 if (mono_hwcap_x86_has_sse42)
1500 sse_opts |= SIMD_VERSION_SSE42;
1502 if (mono_hwcap_x86_has_sse4a)
1503 sse_opts |= SIMD_VERSION_SSE4a;
1511 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1516 for (i = 0; i < cfg->num_varinfo; i++) {
1517 MonoInst *ins = cfg->varinfo [i];
1518 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1521 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1524 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
1525 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1528 if (mono_is_regsize_var (ins->inst_vtype)) {
1529 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1530 g_assert (i == vmv->idx);
1531 vars = g_list_prepend (vars, vmv);
1535 vars = mono_varlist_sort (cfg, vars, 0);
1541 * mono_arch_compute_omit_fp:
1543 * Determine whenever the frame pointer can be eliminated.
1546 mono_arch_compute_omit_fp (MonoCompile *cfg)
1548 MonoMethodSignature *sig;
1549 MonoMethodHeader *header;
1553 if (cfg->arch.omit_fp_computed)
1556 header = cfg->header;
1558 sig = mono_method_signature (cfg->method);
1560 if (!cfg->arch.cinfo)
1561 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1562 cinfo = (CallInfo *)cfg->arch.cinfo;
1565 * FIXME: Remove some of the restrictions.
1567 cfg->arch.omit_fp = TRUE;
1568 cfg->arch.omit_fp_computed = TRUE;
1570 #ifdef __native_client_codegen__
1571 /* NaCl modules may not change the value of RBP, so it cannot be */
1572 /* used as a normal register, but it can be used as a frame pointer*/
1573 cfg->disable_omit_fp = TRUE;
1574 cfg->arch.omit_fp = FALSE;
1577 if (cfg->disable_omit_fp)
1578 cfg->arch.omit_fp = FALSE;
1580 if (!debug_omit_fp ())
1581 cfg->arch.omit_fp = FALSE;
1583 if (cfg->method->save_lmf)
1584 cfg->arch.omit_fp = FALSE;
1586 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1587 cfg->arch.omit_fp = FALSE;
1588 if (header->num_clauses)
1589 cfg->arch.omit_fp = FALSE;
1590 if (cfg->param_area)
1591 cfg->arch.omit_fp = FALSE;
1592 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1593 cfg->arch.omit_fp = FALSE;
1594 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1595 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1596 cfg->arch.omit_fp = FALSE;
1597 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1598 ArgInfo *ainfo = &cinfo->args [i];
1600 if (ainfo->storage == ArgOnStack) {
1602 * The stack offset can only be determined when the frame
1605 cfg->arch.omit_fp = FALSE;
1610 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1611 MonoInst *ins = cfg->varinfo [i];
1614 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1619 mono_arch_get_global_int_regs (MonoCompile *cfg)
1623 mono_arch_compute_omit_fp (cfg);
1625 if (cfg->arch.omit_fp)
1626 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1628 /* We use the callee saved registers for global allocation */
1629 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1630 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1631 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1632 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1633 #ifndef __native_client_codegen__
1634 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1637 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1638 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1645 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1650 /* All XMM registers */
1651 for (i = 0; i < 16; ++i)
1652 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1658 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1660 static GList *r = NULL;
1665 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1666 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1667 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1668 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1669 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1670 #ifndef __native_client_codegen__
1671 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1674 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1675 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1676 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1677 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1678 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1679 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1680 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1681 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1683 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1690 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1693 static GList *r = NULL;
1698 for (i = 0; i < AMD64_XMM_NREG; ++i)
1699 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1701 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1708 * mono_arch_regalloc_cost:
1710 * Return the cost, in number of memory references, of the action of
1711 * allocating the variable VMV into a register during global register
1715 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1717 MonoInst *ins = cfg->varinfo [vmv->idx];
1719 if (cfg->method->save_lmf)
1720 /* The register is already saved */
1721 /* substract 1 for the invisible store in the prolog */
1722 return (ins->opcode == OP_ARG) ? 0 : 1;
1725 return (ins->opcode == OP_ARG) ? 1 : 2;
1729 * mono_arch_fill_argument_info:
1731 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1735 mono_arch_fill_argument_info (MonoCompile *cfg)
1738 MonoMethodSignature *sig;
1743 sig = mono_method_signature (cfg->method);
1745 cinfo = (CallInfo *)cfg->arch.cinfo;
1746 sig_ret = mini_get_underlying_type (sig->ret);
1749 * Contrary to mono_arch_allocate_vars (), the information should describe
1750 * where the arguments are at the beginning of the method, not where they can be
1751 * accessed during the execution of the method. The later makes no sense for the
1752 * global register allocator, since a variable can be in more than one location.
1754 switch (cinfo->ret.storage) {
1756 case ArgInFloatSSEReg:
1757 case ArgInDoubleSSEReg:
1758 cfg->ret->opcode = OP_REGVAR;
1759 cfg->ret->inst_c0 = cinfo->ret.reg;
1761 case ArgValuetypeInReg:
1762 cfg->ret->opcode = OP_REGOFFSET;
1763 cfg->ret->inst_basereg = -1;
1764 cfg->ret->inst_offset = -1;
1769 g_assert_not_reached ();
1772 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1773 ArgInfo *ainfo = &cinfo->args [i];
1775 ins = cfg->args [i];
1777 switch (ainfo->storage) {
1779 case ArgInFloatSSEReg:
1780 case ArgInDoubleSSEReg:
1781 ins->opcode = OP_REGVAR;
1782 ins->inst_c0 = ainfo->reg;
1785 ins->opcode = OP_REGOFFSET;
1786 ins->inst_basereg = -1;
1787 ins->inst_offset = -1;
1789 case ArgValuetypeInReg:
1791 ins->opcode = OP_NOP;
1794 g_assert_not_reached ();
1800 mono_arch_allocate_vars (MonoCompile *cfg)
1803 MonoMethodSignature *sig;
1806 guint32 locals_stack_size, locals_stack_align;
1810 sig = mono_method_signature (cfg->method);
1812 cinfo = (CallInfo *)cfg->arch.cinfo;
1813 sig_ret = mini_get_underlying_type (sig->ret);
1815 mono_arch_compute_omit_fp (cfg);
1818 * We use the ABI calling conventions for managed code as well.
1819 * Exception: valuetypes are only sometimes passed or returned in registers.
1823 * The stack looks like this:
1824 * <incoming arguments passed on the stack>
1826 * <lmf/caller saved registers>
1829 * <localloc area> -> grows dynamically
1833 if (cfg->arch.omit_fp) {
1834 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1835 cfg->frame_reg = AMD64_RSP;
1838 /* Locals are allocated backwards from %fp */
1839 cfg->frame_reg = AMD64_RBP;
1843 cfg->arch.saved_iregs = cfg->used_int_regs;
1844 if (cfg->method->save_lmf)
1845 /* Save all callee-saved registers normally, and restore them when unwinding through an LMF */
1846 cfg->arch.saved_iregs |= (1 << AMD64_RBX) | (1 << AMD64_R12) | (1 << AMD64_R13) | (1 << AMD64_R14) | (1 << AMD64_R15);
1848 if (cfg->arch.omit_fp)
1849 cfg->arch.reg_save_area_offset = offset;
1850 /* Reserve space for callee saved registers */
1851 for (i = 0; i < AMD64_NREG; ++i)
1852 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
1853 offset += sizeof(mgreg_t);
1855 if (!cfg->arch.omit_fp)
1856 cfg->arch.reg_save_area_offset = -offset;
1858 if (sig_ret->type != MONO_TYPE_VOID) {
1859 switch (cinfo->ret.storage) {
1861 case ArgInFloatSSEReg:
1862 case ArgInDoubleSSEReg:
1863 cfg->ret->opcode = OP_REGVAR;
1864 cfg->ret->inst_c0 = cinfo->ret.reg;
1865 cfg->ret->dreg = cinfo->ret.reg;
1867 case ArgValuetypeAddrInIReg:
1868 case ArgGsharedvtVariableInReg:
1869 /* The register is volatile */
1870 cfg->vret_addr->opcode = OP_REGOFFSET;
1871 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1872 if (cfg->arch.omit_fp) {
1873 cfg->vret_addr->inst_offset = offset;
1877 cfg->vret_addr->inst_offset = -offset;
1879 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1880 printf ("vret_addr =");
1881 mono_print_ins (cfg->vret_addr);
1884 case ArgValuetypeInReg:
1885 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1886 cfg->ret->opcode = OP_REGOFFSET;
1887 cfg->ret->inst_basereg = cfg->frame_reg;
1888 if (cfg->arch.omit_fp) {
1889 cfg->ret->inst_offset = offset;
1890 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1892 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1893 cfg->ret->inst_offset = - offset;
1897 g_assert_not_reached ();
1901 /* Allocate locals */
1902 offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1903 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1904 char *mname = mono_method_full_name (cfg->method, TRUE);
1905 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Method %s stack is too big.", mname));
1910 if (locals_stack_align) {
1911 offset += (locals_stack_align - 1);
1912 offset &= ~(locals_stack_align - 1);
1914 if (cfg->arch.omit_fp) {
1915 cfg->locals_min_stack_offset = offset;
1916 cfg->locals_max_stack_offset = offset + locals_stack_size;
1918 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1919 cfg->locals_max_stack_offset = - offset;
1922 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1923 if (offsets [i] != -1) {
1924 MonoInst *ins = cfg->varinfo [i];
1925 ins->opcode = OP_REGOFFSET;
1926 ins->inst_basereg = cfg->frame_reg;
1927 if (cfg->arch.omit_fp)
1928 ins->inst_offset = (offset + offsets [i]);
1930 ins->inst_offset = - (offset + offsets [i]);
1931 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1934 offset += locals_stack_size;
1936 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1937 g_assert (!cfg->arch.omit_fp);
1938 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1939 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1942 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1943 ins = cfg->args [i];
1944 if (ins->opcode != OP_REGVAR) {
1945 ArgInfo *ainfo = &cinfo->args [i];
1946 gboolean inreg = TRUE;
1948 /* FIXME: Allocate volatile arguments to registers */
1949 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1953 * Under AMD64, all registers used to pass arguments to functions
1954 * are volatile across calls.
1955 * FIXME: Optimize this.
1957 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg) || (ainfo->storage == ArgGSharedVtInReg))
1960 ins->opcode = OP_REGOFFSET;
1962 switch (ainfo->storage) {
1964 case ArgInFloatSSEReg:
1965 case ArgInDoubleSSEReg:
1966 case ArgGSharedVtInReg:
1968 ins->opcode = OP_REGVAR;
1969 ins->dreg = ainfo->reg;
1973 case ArgGSharedVtOnStack:
1974 g_assert (!cfg->arch.omit_fp);
1975 ins->opcode = OP_REGOFFSET;
1976 ins->inst_basereg = cfg->frame_reg;
1977 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1979 case ArgValuetypeInReg:
1981 case ArgValuetypeAddrInIReg: {
1983 g_assert (!cfg->arch.omit_fp);
1985 MONO_INST_NEW (cfg, indir, 0);
1986 indir->opcode = OP_REGOFFSET;
1987 if (ainfo->pair_storage [0] == ArgInIReg) {
1988 indir->inst_basereg = cfg->frame_reg;
1989 offset = ALIGN_TO (offset, sizeof (gpointer));
1990 offset += (sizeof (gpointer));
1991 indir->inst_offset = - offset;
1994 indir->inst_basereg = cfg->frame_reg;
1995 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
1998 ins->opcode = OP_VTARG_ADDR;
1999 ins->inst_left = indir;
2007 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg) && (ainfo->storage != ArgGSharedVtOnStack)) {
2008 ins->opcode = OP_REGOFFSET;
2009 ins->inst_basereg = cfg->frame_reg;
2010 /* These arguments are saved to the stack in the prolog */
2011 offset = ALIGN_TO (offset, sizeof(mgreg_t));
2012 if (cfg->arch.omit_fp) {
2013 ins->inst_offset = offset;
2014 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
2015 // Arguments are yet supported by the stack map creation code
2016 //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
2018 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
2019 ins->inst_offset = - offset;
2020 //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
2026 cfg->stack_offset = offset;
2030 mono_arch_create_vars (MonoCompile *cfg)
2032 MonoMethodSignature *sig;
2036 sig = mono_method_signature (cfg->method);
2038 if (!cfg->arch.cinfo)
2039 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
2040 cinfo = (CallInfo *)cfg->arch.cinfo;
2042 if (cinfo->ret.storage == ArgValuetypeInReg)
2043 cfg->ret_var_is_local = TRUE;
2045 sig_ret = mini_get_underlying_type (sig->ret);
2046 if (cinfo->ret.storage == ArgValuetypeAddrInIReg || cinfo->ret.storage == ArgGsharedvtVariableInReg) {
2047 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2048 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2049 printf ("vret_addr = ");
2050 mono_print_ins (cfg->vret_addr);
2054 if (cfg->gen_sdb_seq_points) {
2057 if (cfg->compile_aot) {
2058 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2059 ins->flags |= MONO_INST_VOLATILE;
2060 cfg->arch.seq_point_info_var = ins;
2062 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2063 ins->flags |= MONO_INST_VOLATILE;
2064 cfg->arch.ss_tramp_var = ins;
2066 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2067 ins->flags |= MONO_INST_VOLATILE;
2068 cfg->arch.bp_tramp_var = ins;
2071 if (cfg->method->save_lmf)
2072 cfg->create_lmf_var = TRUE;
2074 if (cfg->method->save_lmf) {
2076 #if !defined(TARGET_WIN32)
2077 if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
2078 cfg->lmf_ir_mono_lmf = TRUE;
2084 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2090 MONO_INST_NEW (cfg, ins, OP_MOVE);
2091 ins->dreg = mono_alloc_ireg_copy (cfg, tree->dreg);
2092 ins->sreg1 = tree->dreg;
2093 MONO_ADD_INS (cfg->cbb, ins);
2094 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2096 case ArgInFloatSSEReg:
2097 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
2098 ins->dreg = mono_alloc_freg (cfg);
2099 ins->sreg1 = tree->dreg;
2100 MONO_ADD_INS (cfg->cbb, ins);
2102 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2104 case ArgInDoubleSSEReg:
2105 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2106 ins->dreg = mono_alloc_freg (cfg);
2107 ins->sreg1 = tree->dreg;
2108 MONO_ADD_INS (cfg->cbb, ins);
2110 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2114 g_assert_not_reached ();
2119 arg_storage_to_load_membase (ArgStorage storage)
2123 #if defined(__mono_ilp32__)
2124 return OP_LOADI8_MEMBASE;
2126 return OP_LOAD_MEMBASE;
2128 case ArgInDoubleSSEReg:
2129 return OP_LOADR8_MEMBASE;
2130 case ArgInFloatSSEReg:
2131 return OP_LOADR4_MEMBASE;
2133 g_assert_not_reached ();
2140 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2142 MonoMethodSignature *tmp_sig;
2145 if (call->tail_call)
2148 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
2151 * mono_ArgIterator_Setup assumes the signature cookie is
2152 * passed first and all the arguments which were before it are
2153 * passed on the stack after the signature. So compensate by
2154 * passing a different signature.
2156 tmp_sig = mono_metadata_signature_dup_full (cfg->method->klass->image, call->signature);
2157 tmp_sig->param_count -= call->signature->sentinelpos;
2158 tmp_sig->sentinelpos = 0;
2159 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2161 sig_reg = mono_alloc_ireg (cfg);
2162 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2164 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, cinfo->sig_cookie.offset, sig_reg);
2168 static inline LLVMArgStorage
2169 arg_storage_to_llvm_arg_storage (MonoCompile *cfg, ArgStorage storage)
2173 return LLVMArgInIReg;
2176 case ArgGSharedVtInReg:
2177 case ArgGSharedVtOnStack:
2178 return LLVMArgGSharedVt;
2180 g_assert_not_reached ();
2186 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2192 LLVMCallInfo *linfo;
2193 MonoType *t, *sig_ret;
2195 n = sig->param_count + sig->hasthis;
2196 sig_ret = mini_get_underlying_type (sig->ret);
2198 cinfo = get_call_info (cfg->mempool, sig);
2200 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2203 * LLVM always uses the native ABI while we use our own ABI, the
2204 * only difference is the handling of vtypes:
2205 * - we only pass/receive them in registers in some cases, and only
2206 * in 1 or 2 integer registers.
2208 switch (cinfo->ret.storage) {
2210 linfo->ret.storage = LLVMArgNone;
2213 case ArgInFloatSSEReg:
2214 case ArgInDoubleSSEReg:
2215 linfo->ret.storage = LLVMArgNormal;
2217 case ArgValuetypeInReg: {
2218 ainfo = &cinfo->ret;
2221 (ainfo->pair_storage [0] == ArgInFloatSSEReg || ainfo->pair_storage [0] == ArgInDoubleSSEReg ||
2222 ainfo->pair_storage [1] == ArgInFloatSSEReg || ainfo->pair_storage [1] == ArgInDoubleSSEReg)) {
2223 cfg->exception_message = g_strdup ("pinvoke + vtype ret");
2224 cfg->disable_llvm = TRUE;
2228 linfo->ret.storage = LLVMArgVtypeInReg;
2229 for (j = 0; j < 2; ++j)
2230 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2233 case ArgValuetypeAddrInIReg:
2234 case ArgGsharedvtVariableInReg:
2235 /* Vtype returned using a hidden argument */
2236 linfo->ret.storage = LLVMArgVtypeRetAddr;
2237 linfo->vret_arg_index = cinfo->vret_arg_index;
2240 g_assert_not_reached ();
2244 for (i = 0; i < n; ++i) {
2245 ainfo = cinfo->args + i;
2247 if (i >= sig->hasthis)
2248 t = sig->params [i - sig->hasthis];
2250 t = &mono_defaults.int_class->byval_arg;
2252 linfo->args [i].storage = LLVMArgNone;
2254 switch (ainfo->storage) {
2256 linfo->args [i].storage = LLVMArgNormal;
2258 case ArgInDoubleSSEReg:
2259 case ArgInFloatSSEReg:
2260 linfo->args [i].storage = LLVMArgNormal;
2263 if (MONO_TYPE_ISSTRUCT (t))
2264 linfo->args [i].storage = LLVMArgVtypeByVal;
2266 linfo->args [i].storage = LLVMArgNormal;
2268 case ArgValuetypeInReg:
2270 (ainfo->pair_storage [0] == ArgInFloatSSEReg || ainfo->pair_storage [0] == ArgInDoubleSSEReg ||
2271 ainfo->pair_storage [1] == ArgInFloatSSEReg || ainfo->pair_storage [1] == ArgInDoubleSSEReg)) {
2272 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2273 cfg->disable_llvm = TRUE;
2277 linfo->args [i].storage = LLVMArgVtypeInReg;
2278 for (j = 0; j < 2; ++j)
2279 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2281 case ArgGSharedVtInReg:
2282 case ArgGSharedVtOnStack:
2283 linfo->args [i].storage = LLVMArgGSharedVt;
2286 cfg->exception_message = g_strdup ("ainfo->storage");
2287 cfg->disable_llvm = TRUE;
2297 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2300 MonoMethodSignature *sig;
2306 sig = call->signature;
2307 n = sig->param_count + sig->hasthis;
2309 cinfo = get_call_info (cfg->mempool, sig);
2313 if (COMPILE_LLVM (cfg)) {
2314 /* We shouldn't be called in the llvm case */
2315 cfg->disable_llvm = TRUE;
2320 * Emit all arguments which are passed on the stack to prevent register
2321 * allocation problems.
2323 for (i = 0; i < n; ++i) {
2325 ainfo = cinfo->args + i;
2327 in = call->args [i];
2329 if (sig->hasthis && i == 0)
2330 t = &mono_defaults.object_class->byval_arg;
2332 t = sig->params [i - sig->hasthis];
2334 t = mini_get_underlying_type (t);
2335 //XXX what about ArgGSharedVtOnStack here?
2336 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call) {
2338 if (t->type == MONO_TYPE_R4)
2339 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2340 else if (t->type == MONO_TYPE_R8)
2341 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2343 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2345 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2347 if (cfg->compute_gc_maps) {
2350 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2356 * Emit all parameters passed in registers in non-reverse order for better readability
2357 * and to help the optimization in emit_prolog ().
2359 for (i = 0; i < n; ++i) {
2360 ainfo = cinfo->args + i;
2362 in = call->args [i];
2364 if (ainfo->storage == ArgInIReg)
2365 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2368 for (i = n - 1; i >= 0; --i) {
2371 ainfo = cinfo->args + i;
2373 in = call->args [i];
2375 if (sig->hasthis && i == 0)
2376 t = &mono_defaults.object_class->byval_arg;
2378 t = sig->params [i - sig->hasthis];
2379 t = mini_get_underlying_type (t);
2381 switch (ainfo->storage) {
2385 case ArgInFloatSSEReg:
2386 case ArgInDoubleSSEReg:
2387 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2390 case ArgValuetypeInReg:
2391 case ArgValuetypeAddrInIReg:
2392 case ArgGSharedVtInReg:
2393 case ArgGSharedVtOnStack: {
2394 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call)
2395 /* Already emitted above */
2397 //FIXME what about ArgGSharedVtOnStack ?
2398 if (ainfo->storage == ArgOnStack && call->tail_call) {
2399 MonoInst *call_inst = (MonoInst*)call;
2400 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2401 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2409 size = mono_type_native_stack_size (t, &align);
2412 * Other backends use mono_type_stack_size (), but that
2413 * aligns the size to 8, which is larger than the size of
2414 * the source, leading to reads of invalid memory if the
2415 * source is at the end of address space.
2417 size = mono_class_value_size (mono_class_from_mono_type (t), &align);
2420 if (size >= 10000) {
2421 /* Avoid asserts in emit_memcpy () */
2422 mono_cfg_set_exception_invalid_program (cfg, g_strdup_printf ("Passing an argument of size '%d'.", size));
2423 /* Continue normally */
2427 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2428 arg->sreg1 = in->dreg;
2429 arg->klass = mono_class_from_mono_type (t);
2430 arg->backend.size = size;
2431 arg->inst_p0 = call;
2432 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2433 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2435 MONO_ADD_INS (cfg->cbb, arg);
2440 g_assert_not_reached ();
2443 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
2444 /* Emit the signature cookie just before the implicit arguments */
2445 emit_sig_cookie (cfg, call, cinfo);
2448 /* Handle the case where there are no implicit arguments */
2449 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2450 emit_sig_cookie (cfg, call, cinfo);
2452 switch (cinfo->ret.storage) {
2453 case ArgValuetypeInReg:
2454 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2456 * Tell the JIT to use a more efficient calling convention: call using
2457 * OP_CALL, compute the result location after the call, and save the
2460 call->vret_in_reg = TRUE;
2462 * Nullify the instruction computing the vret addr to enable
2463 * future optimizations.
2466 NULLIFY_INS (call->vret_var);
2468 if (call->tail_call)
2471 * The valuetype is in RAX:RDX after the call, need to be copied to
2472 * the stack. Push the address here, so the call instruction can
2475 if (!cfg->arch.vret_addr_loc) {
2476 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2477 /* Prevent it from being register allocated or optimized away */
2478 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2481 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2484 case ArgValuetypeAddrInIReg:
2485 case ArgGsharedvtVariableInReg: {
2487 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2488 vtarg->sreg1 = call->vret_var->dreg;
2489 vtarg->dreg = mono_alloc_preg (cfg);
2490 MONO_ADD_INS (cfg->cbb, vtarg);
2492 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2499 if (cfg->method->save_lmf) {
2500 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2501 MONO_ADD_INS (cfg->cbb, arg);
2504 call->stack_usage = cinfo->stack_usage;
2508 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2511 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2512 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2513 int size = ins->backend.size;
2515 switch (ainfo->storage) {
2516 case ArgValuetypeInReg: {
2520 for (part = 0; part < 2; ++part) {
2521 if (ainfo->pair_storage [part] == ArgNone)
2524 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2525 load->inst_basereg = src->dreg;
2526 load->inst_offset = part * sizeof(mgreg_t);
2528 switch (ainfo->pair_storage [part]) {
2530 load->dreg = mono_alloc_ireg (cfg);
2532 case ArgInDoubleSSEReg:
2533 case ArgInFloatSSEReg:
2534 load->dreg = mono_alloc_freg (cfg);
2537 g_assert_not_reached ();
2539 MONO_ADD_INS (cfg->cbb, load);
2541 add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2545 case ArgValuetypeAddrInIReg: {
2546 MonoInst *vtaddr, *load;
2547 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2549 MONO_INST_NEW (cfg, load, OP_LDADDR);
2550 cfg->has_indirection = TRUE;
2551 load->inst_p0 = vtaddr;
2552 vtaddr->flags |= MONO_INST_INDIRECT;
2553 load->type = STACK_MP;
2554 load->klass = vtaddr->klass;
2555 load->dreg = mono_alloc_ireg (cfg);
2556 MONO_ADD_INS (cfg->cbb, load);
2557 mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2559 if (ainfo->pair_storage [0] == ArgInIReg) {
2560 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2561 arg->dreg = mono_alloc_ireg (cfg);
2562 arg->sreg1 = load->dreg;
2564 MONO_ADD_INS (cfg->cbb, arg);
2565 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2567 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, load->dreg);
2571 case ArgGSharedVtInReg:
2573 mono_call_inst_add_outarg_reg (cfg, call, src->dreg, ainfo->reg, FALSE);
2575 case ArgGSharedVtOnStack:
2576 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, src->dreg);
2580 int dreg = mono_alloc_ireg (cfg);
2582 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
2583 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, dreg);
2584 } else if (size <= 40) {
2585 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2587 // FIXME: Code growth
2588 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2591 if (cfg->compute_gc_maps) {
2593 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2599 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2601 MonoType *ret = mini_get_underlying_type (mono_method_signature (method)->ret);
2603 if (ret->type == MONO_TYPE_R4) {
2604 if (COMPILE_LLVM (cfg))
2605 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2607 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2609 } else if (ret->type == MONO_TYPE_R8) {
2610 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2614 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2617 #endif /* DISABLE_JIT */
2619 #define EMIT_COND_BRANCH(ins,cond,sign) \
2620 if (ins->inst_true_bb->native_offset) { \
2621 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2623 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2624 if ((cfg->opt & MONO_OPT_BRANCH) && \
2625 x86_is_imm8 (ins->inst_true_bb->max_offset - offset)) \
2626 x86_branch8 (code, cond, 0, sign); \
2628 x86_branch32 (code, cond, 0, sign); \
2632 MonoMethodSignature *sig;
2637 dyn_call_supported (MonoMethodSignature *sig, CallInfo *cinfo)
2645 switch (cinfo->ret.storage) {
2648 case ArgInFloatSSEReg:
2649 case ArgInDoubleSSEReg:
2651 case ArgValuetypeInReg: {
2652 ArgInfo *ainfo = &cinfo->ret;
2654 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2656 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2664 for (i = 0; i < cinfo->nargs; ++i) {
2665 ArgInfo *ainfo = &cinfo->args [i];
2666 switch (ainfo->storage) {
2668 case ArgInFloatSSEReg:
2669 case ArgInDoubleSSEReg:
2671 case ArgValuetypeInReg:
2672 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2674 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2686 * mono_arch_dyn_call_prepare:
2688 * Return a pointer to an arch-specific structure which contains information
2689 * needed by mono_arch_get_dyn_call_args (). Return NULL if OP_DYN_CALL is not
2690 * supported for SIG.
2691 * This function is equivalent to ffi_prep_cif in libffi.
2694 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2696 ArchDynCallInfo *info;
2699 cinfo = get_call_info (NULL, sig);
2701 if (!dyn_call_supported (sig, cinfo)) {
2706 info = g_new0 (ArchDynCallInfo, 1);
2707 // FIXME: Preprocess the info to speed up get_dyn_call_args ().
2709 info->cinfo = cinfo;
2711 return (MonoDynCallInfo*)info;
2715 * mono_arch_dyn_call_free:
2717 * Free a MonoDynCallInfo structure.
2720 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2722 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2724 g_free (ainfo->cinfo);
2728 #if !defined(__native_client__)
2729 #define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
2730 #define GREG_TO_PTR(greg) (gpointer)(greg)
2732 /* Correctly handle casts to/from 32-bit pointers without compiler warnings */
2733 #define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
2734 #define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
2738 * mono_arch_get_start_dyn_call:
2740 * Convert the arguments ARGS to a format which can be passed to OP_DYN_CALL, and
2741 * store the result into BUF.
2742 * ARGS should be an array of pointers pointing to the arguments.
2743 * RET should point to a memory buffer large enought to hold the result of the
2745 * This function should be as fast as possible, any work which does not depend
2746 * on the actual values of the arguments should be done in
2747 * mono_arch_dyn_call_prepare ().
2748 * start_dyn_call + OP_DYN_CALL + finish_dyn_call is equivalent to ffi_call in
2752 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2754 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2755 DynCallArgs *p = (DynCallArgs*)buf;
2756 int arg_index, greg, freg, i, pindex;
2757 MonoMethodSignature *sig = dinfo->sig;
2758 int buffer_offset = 0;
2760 g_assert (buf_len >= sizeof (DynCallArgs));
2770 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2771 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2776 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg || dinfo->cinfo->ret.storage == ArgGsharedvtVariableInReg)
2777 p->regs [greg ++] = PTR_TO_GREG(ret);
2779 for (i = pindex; i < sig->param_count; i++) {
2780 MonoType *t = mini_get_underlying_type (sig->params [i]);
2781 gpointer *arg = args [arg_index ++];
2784 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2789 case MONO_TYPE_STRING:
2790 case MONO_TYPE_CLASS:
2791 case MONO_TYPE_ARRAY:
2792 case MONO_TYPE_SZARRAY:
2793 case MONO_TYPE_OBJECT:
2797 #if !defined(__mono_ilp32__)
2801 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2802 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2804 #if defined(__mono_ilp32__)
2807 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2808 p->regs [greg ++] = *(guint64*)(arg);
2812 p->regs [greg ++] = *(guint8*)(arg);
2815 p->regs [greg ++] = *(gint8*)(arg);
2818 p->regs [greg ++] = *(gint16*)(arg);
2821 p->regs [greg ++] = *(guint16*)(arg);
2824 p->regs [greg ++] = *(gint32*)(arg);
2827 p->regs [greg ++] = *(guint32*)(arg);
2829 case MONO_TYPE_R4: {
2832 *(float*)&d = *(float*)(arg);
2834 p->fregs [freg ++] = d;
2839 p->fregs [freg ++] = *(double*)(arg);
2841 case MONO_TYPE_GENERICINST:
2842 if (MONO_TYPE_IS_REFERENCE (t)) {
2843 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2845 } else if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
2846 MonoClass *klass = mono_class_from_mono_type (t);
2847 guint8 *nullable_buf;
2850 size = mono_class_value_size (klass, NULL);
2851 nullable_buf = p->buffer + buffer_offset;
2852 buffer_offset += size;
2853 g_assert (buffer_offset <= 256);
2855 /* The argument pointed to by arg is either a boxed vtype or null */
2856 mono_nullable_init (nullable_buf, (MonoObject*)arg, klass);
2858 arg = (gpointer*)nullable_buf;
2864 case MONO_TYPE_VALUETYPE: {
2865 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2867 g_assert (ainfo->storage == ArgValuetypeInReg);
2868 if (ainfo->pair_storage [0] != ArgNone) {
2869 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2870 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2872 if (ainfo->pair_storage [1] != ArgNone) {
2873 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2874 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2879 g_assert_not_reached ();
2883 g_assert (greg <= PARAM_REGS);
2887 * mono_arch_finish_dyn_call:
2889 * Store the result of a dyn call into the return value buffer passed to
2890 * start_dyn_call ().
2891 * This function should be as fast as possible, any work which does not depend
2892 * on the actual values of the arguments should be done in
2893 * mono_arch_dyn_call_prepare ().
2896 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2898 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2899 MonoMethodSignature *sig = dinfo->sig;
2900 DynCallArgs *dargs = (DynCallArgs*)buf;
2901 guint8 *ret = dargs->ret;
2902 mgreg_t res = dargs->res;
2903 MonoType *sig_ret = mini_get_underlying_type (sig->ret);
2905 switch (sig_ret->type) {
2906 case MONO_TYPE_VOID:
2907 *(gpointer*)ret = NULL;
2909 case MONO_TYPE_STRING:
2910 case MONO_TYPE_CLASS:
2911 case MONO_TYPE_ARRAY:
2912 case MONO_TYPE_SZARRAY:
2913 case MONO_TYPE_OBJECT:
2917 *(gpointer*)ret = GREG_TO_PTR(res);
2923 *(guint8*)ret = res;
2926 *(gint16*)ret = res;
2929 *(guint16*)ret = res;
2932 *(gint32*)ret = res;
2935 *(guint32*)ret = res;
2938 *(gint64*)ret = res;
2941 *(guint64*)ret = res;
2944 *(float*)ret = *(float*)&(dargs->fregs [0]);
2947 *(double*)ret = dargs->fregs [0];
2949 case MONO_TYPE_GENERICINST:
2950 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
2951 *(gpointer*)ret = GREG_TO_PTR(res);
2956 case MONO_TYPE_VALUETYPE:
2957 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg || dinfo->cinfo->ret.storage == ArgGsharedvtVariableInReg) {
2960 ArgInfo *ainfo = &dinfo->cinfo->ret;
2962 g_assert (ainfo->storage == ArgValuetypeInReg);
2964 if (ainfo->pair_storage [0] != ArgNone) {
2965 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2966 ((mgreg_t*)ret)[0] = res;
2969 g_assert (ainfo->pair_storage [1] == ArgNone);
2973 g_assert_not_reached ();
2977 /* emit an exception if condition is fail */
2978 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2980 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2981 if (tins == NULL) { \
2982 mono_add_patch_info (cfg, code - cfg->native_code, \
2983 MONO_PATCH_INFO_EXC, exc_name); \
2984 x86_branch32 (code, cond, 0, signed); \
2986 EMIT_COND_BRANCH (tins, cond, signed); \
2990 #define EMIT_FPCOMPARE(code) do { \
2991 amd64_fcompp (code); \
2992 amd64_fnstsw (code); \
2995 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
2996 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
2997 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
2998 amd64_ ##op (code); \
2999 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
3000 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
3004 emit_call_body (MonoCompile *cfg, guint8 *code, MonoJumpInfoType patch_type, gconstpointer data)
3006 gboolean no_patch = FALSE;
3009 * FIXME: Add support for thunks
3012 gboolean near_call = FALSE;
3015 * Indirect calls are expensive so try to make a near call if possible.
3016 * The caller memory is allocated by the code manager so it is
3017 * guaranteed to be at a 32 bit offset.
3020 if (patch_type != MONO_PATCH_INFO_ABS) {
3021 /* The target is in memory allocated using the code manager */
3024 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
3025 if (((MonoMethod*)data)->klass->image->aot_module)
3026 /* The callee might be an AOT method */
3028 if (((MonoMethod*)data)->dynamic)
3029 /* The target is in malloc-ed memory */
3033 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
3035 * The call might go directly to a native function without
3038 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ((const char *)data);
3040 gconstpointer target = mono_icall_get_wrapper (mi);
3041 if ((((guint64)target) >> 32) != 0)
3047 MonoJumpInfo *jinfo = NULL;
3049 if (cfg->abs_patches)
3050 jinfo = (MonoJumpInfo *)g_hash_table_lookup (cfg->abs_patches, data);
3052 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3053 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
3054 if (mi && (((guint64)mi->func) >> 32) == 0)
3059 * This is not really an optimization, but required because the
3060 * generic class init trampolines use R11 to pass the vtable.
3065 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
3067 if (info->func == info->wrapper) {
3069 if ((((guint64)info->func) >> 32) == 0)
3073 /* See the comment in mono_codegen () */
3074 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
3078 else if ((((guint64)data) >> 32) == 0) {
3085 if (cfg->method->dynamic)
3086 /* These methods are allocated using malloc */
3089 #ifdef MONO_ARCH_NOMAP32BIT
3092 #if defined(__native_client__)
3093 /* Always use near_call == TRUE for Native Client */
3096 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
3097 if (optimize_for_xen)
3100 if (cfg->compile_aot) {
3107 * Align the call displacement to an address divisible by 4 so it does
3108 * not span cache lines. This is required for code patching to work on SMP
3111 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
3112 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
3113 amd64_padding (code, pad_size);
3115 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3116 amd64_call_code (code, 0);
3119 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3120 amd64_set_reg_template (code, GP_SCRATCH_REG);
3121 amd64_call_reg (code, GP_SCRATCH_REG);
3128 static inline guint8*
3129 emit_call (MonoCompile *cfg, guint8 *code, MonoJumpInfoType patch_type, gconstpointer data, gboolean win64_adjust_stack)
3132 if (win64_adjust_stack)
3133 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
3135 code = emit_call_body (cfg, code, patch_type, data);
3137 if (win64_adjust_stack)
3138 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
3145 store_membase_imm_to_store_membase_reg (int opcode)
3148 case OP_STORE_MEMBASE_IMM:
3149 return OP_STORE_MEMBASE_REG;
3150 case OP_STOREI4_MEMBASE_IMM:
3151 return OP_STOREI4_MEMBASE_REG;
3152 case OP_STOREI8_MEMBASE_IMM:
3153 return OP_STOREI8_MEMBASE_REG;
3161 #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)))
3164 * mono_arch_peephole_pass_1:
3166 * Perform peephole opts which should/can be performed before local regalloc
3169 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3173 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3174 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3176 switch (ins->opcode) {
3180 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
3182 * X86_LEA is like ADD, but doesn't have the
3183 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
3184 * its operand to 64 bit.
3186 ins->opcode = OP_X86_LEA_MEMBASE;
3187 ins->inst_basereg = ins->sreg1;
3192 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3196 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3197 * the latter has length 2-3 instead of 6 (reverse constant
3198 * propagation). These instruction sequences are very common
3199 * in the initlocals bblock.
3201 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3202 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)) {
3203 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3204 ins2->sreg1 = ins->dreg;
3205 } 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)) {
3207 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3210 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3218 case OP_COMPARE_IMM:
3219 case OP_LCOMPARE_IMM:
3220 /* OP_COMPARE_IMM (reg, 0)
3222 * OP_AMD64_TEST_NULL (reg)
3225 ins->opcode = OP_AMD64_TEST_NULL;
3227 case OP_ICOMPARE_IMM:
3229 ins->opcode = OP_X86_TEST_NULL;
3231 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3233 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3234 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3236 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3237 * OP_COMPARE_IMM reg, imm
3239 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3241 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3242 ins->inst_basereg == last_ins->inst_destbasereg &&
3243 ins->inst_offset == last_ins->inst_offset) {
3244 ins->opcode = OP_ICOMPARE_IMM;
3245 ins->sreg1 = last_ins->sreg1;
3247 /* check if we can remove cmp reg,0 with test null */
3249 ins->opcode = OP_X86_TEST_NULL;
3255 mono_peephole_ins (bb, ins);
3260 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3264 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3265 switch (ins->opcode) {
3268 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3269 /* reg = 0 -> XOR (reg, reg) */
3270 /* XOR sets cflags on x86, so we cant do it always */
3271 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3272 ins->opcode = OP_LXOR;
3273 ins->sreg1 = ins->dreg;
3274 ins->sreg2 = ins->dreg;
3282 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3283 * 0 result into 64 bits.
3285 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3286 ins->opcode = OP_IXOR;
3290 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3294 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3295 * the latter has length 2-3 instead of 6 (reverse constant
3296 * propagation). These instruction sequences are very common
3297 * in the initlocals bblock.
3299 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3300 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)) {
3301 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3302 ins2->sreg1 = ins->dreg;
3303 } 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)) {
3305 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3308 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3317 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3318 ins->opcode = OP_X86_INC_REG;
3321 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3322 ins->opcode = OP_X86_DEC_REG;
3326 mono_peephole_ins (bb, ins);
3330 #define NEW_INS(cfg,ins,dest,op) do { \
3331 MONO_INST_NEW ((cfg), (dest), (op)); \
3332 (dest)->cil_code = (ins)->cil_code; \
3333 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3337 * mono_arch_lowering_pass:
3339 * Converts complex opcodes into simpler ones so that each IR instruction
3340 * corresponds to one machine instruction.
3343 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3345 MonoInst *ins, *n, *temp;
3348 * FIXME: Need to add more instructions, but the current machine
3349 * description can't model some parts of the composite instructions like
3352 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3353 switch (ins->opcode) {
3357 case OP_IDIV_UN_IMM:
3358 case OP_IREM_UN_IMM:
3361 mono_decompose_op_imm (cfg, bb, ins);
3363 case OP_COMPARE_IMM:
3364 case OP_LCOMPARE_IMM:
3365 if (!amd64_use_imm32 (ins->inst_imm)) {
3366 NEW_INS (cfg, ins, temp, OP_I8CONST);
3367 temp->inst_c0 = ins->inst_imm;
3368 temp->dreg = mono_alloc_ireg (cfg);
3369 ins->opcode = OP_COMPARE;
3370 ins->sreg2 = temp->dreg;
3373 #ifndef __mono_ilp32__
3374 case OP_LOAD_MEMBASE:
3376 case OP_LOADI8_MEMBASE:
3377 #ifndef __native_client_codegen__
3378 /* Don't generate memindex opcodes (to simplify */
3379 /* read sandboxing) */
3380 if (!amd64_use_imm32 (ins->inst_offset)) {
3381 NEW_INS (cfg, ins, temp, OP_I8CONST);
3382 temp->inst_c0 = ins->inst_offset;
3383 temp->dreg = mono_alloc_ireg (cfg);
3384 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3385 ins->inst_indexreg = temp->dreg;
3389 #ifndef __mono_ilp32__
3390 case OP_STORE_MEMBASE_IMM:
3392 case OP_STOREI8_MEMBASE_IMM:
3393 if (!amd64_use_imm32 (ins->inst_imm)) {
3394 NEW_INS (cfg, ins, temp, OP_I8CONST);
3395 temp->inst_c0 = ins->inst_imm;
3396 temp->dreg = mono_alloc_ireg (cfg);
3397 ins->opcode = OP_STOREI8_MEMBASE_REG;
3398 ins->sreg1 = temp->dreg;
3401 #ifdef MONO_ARCH_SIMD_INTRINSICS
3402 case OP_EXPAND_I1: {
3403 int temp_reg1 = mono_alloc_ireg (cfg);
3404 int temp_reg2 = mono_alloc_ireg (cfg);
3405 int original_reg = ins->sreg1;
3407 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3408 temp->sreg1 = original_reg;
3409 temp->dreg = temp_reg1;
3411 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3412 temp->sreg1 = temp_reg1;
3413 temp->dreg = temp_reg2;
3416 NEW_INS (cfg, ins, temp, OP_LOR);
3417 temp->sreg1 = temp->dreg = temp_reg2;
3418 temp->sreg2 = temp_reg1;
3420 ins->opcode = OP_EXPAND_I2;
3421 ins->sreg1 = temp_reg2;
3430 bb->max_vreg = cfg->next_vreg;
3434 branch_cc_table [] = {
3435 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3436 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3437 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3440 /* Maps CMP_... constants to X86_CC_... constants */
3443 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3444 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3448 cc_signed_table [] = {
3449 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3450 FALSE, FALSE, FALSE, FALSE
3453 /*#include "cprop.c"*/
3455 static unsigned char*
3456 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3459 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3461 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3464 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3466 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3470 static unsigned char*
3471 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3473 int sreg = tree->sreg1;
3474 int need_touch = FALSE;
3476 #if defined(TARGET_WIN32)
3478 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3479 if (!tree->flags & MONO_INST_INIT)
3488 * If requested stack size is larger than one page,
3489 * perform stack-touch operation
3492 * Generate stack probe code.
3493 * Under Windows, it is necessary to allocate one page at a time,
3494 * "touching" stack after each successful sub-allocation. This is
3495 * because of the way stack growth is implemented - there is a
3496 * guard page before the lowest stack page that is currently commited.
3497 * Stack normally grows sequentially so OS traps access to the
3498 * guard page and commits more pages when needed.
3500 amd64_test_reg_imm (code, sreg, ~0xFFF);
3501 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3503 br[2] = code; /* loop */
3504 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3505 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3506 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3507 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3508 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3509 amd64_patch (br[3], br[2]);
3510 amd64_test_reg_reg (code, sreg, sreg);
3511 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3512 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3514 br[1] = code; x86_jump8 (code, 0);
3516 amd64_patch (br[0], code);
3517 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3518 amd64_patch (br[1], code);
3519 amd64_patch (br[4], code);
3522 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3524 if (tree->flags & MONO_INST_INIT) {
3526 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3527 amd64_push_reg (code, AMD64_RAX);
3530 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3531 amd64_push_reg (code, AMD64_RCX);
3534 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3535 amd64_push_reg (code, AMD64_RDI);
3539 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3540 if (sreg != AMD64_RCX)
3541 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3542 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3544 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3545 if (cfg->param_area)
3546 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3548 #if defined(__default_codegen__)
3549 amd64_prefix (code, X86_REP_PREFIX);
3551 #elif defined(__native_client_codegen__)
3552 /* NaCl stos pseudo-instruction */
3553 amd64_codegen_pre(code);
3554 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3555 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3556 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3557 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3558 amd64_prefix (code, X86_REP_PREFIX);
3560 amd64_codegen_post(code);
3561 #endif /* __native_client_codegen__ */
3563 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3564 amd64_pop_reg (code, AMD64_RDI);
3565 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3566 amd64_pop_reg (code, AMD64_RCX);
3567 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3568 amd64_pop_reg (code, AMD64_RAX);
3574 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3579 /* Move return value to the target register */
3580 /* FIXME: do this in the local reg allocator */
3581 switch (ins->opcode) {
3584 case OP_CALL_MEMBASE:
3587 case OP_LCALL_MEMBASE:
3588 g_assert (ins->dreg == AMD64_RAX);
3592 case OP_FCALL_MEMBASE: {
3593 MonoType *rtype = mini_get_underlying_type (((MonoCallInst*)ins)->signature->ret);
3594 if (rtype->type == MONO_TYPE_R4) {
3595 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3598 if (ins->dreg != AMD64_XMM0)
3599 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3605 case OP_RCALL_MEMBASE:
3606 if (ins->dreg != AMD64_XMM0)
3607 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3611 case OP_VCALL_MEMBASE:
3614 case OP_VCALL2_MEMBASE:
3615 cinfo = get_call_info (cfg->mempool, ((MonoCallInst*)ins)->signature);
3616 if (cinfo->ret.storage == ArgValuetypeInReg) {
3617 MonoInst *loc = (MonoInst *)cfg->arch.vret_addr_loc;
3619 /* Load the destination address */
3620 g_assert (loc->opcode == OP_REGOFFSET);
3621 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3623 for (quad = 0; quad < 2; quad ++) {
3624 switch (cinfo->ret.pair_storage [quad]) {
3626 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3628 case ArgInFloatSSEReg:
3629 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3631 case ArgInDoubleSSEReg:
3632 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3647 #endif /* DISABLE_JIT */
3650 static int tls_gs_offset;
3654 mono_amd64_have_tls_get (void)
3657 static gboolean have_tls_get = FALSE;
3658 static gboolean inited = FALSE;
3661 return have_tls_get;
3663 #if MONO_HAVE_FAST_TLS
3664 guint8 *ins = (guint8*)pthread_getspecific;
3667 * We're looking for these two instructions:
3669 * mov %gs:[offset](,%rdi,8),%rax
3672 have_tls_get = ins [0] == 0x65 &&
3682 tls_gs_offset = ins[5];
3685 * Apple now loads a different version of pthread_getspecific when launched from Xcode
3686 * For that version we're looking for these instructions:
3690 * mov %gs:[offset](,%rdi,8),%rax
3694 if (!have_tls_get) {
3695 have_tls_get = ins [0] == 0x55 &&
3710 tls_gs_offset = ins[9];
3716 return have_tls_get;
3717 #elif defined(TARGET_ANDROID)
3725 mono_amd64_get_tls_gs_offset (void)
3728 return tls_gs_offset;
3730 g_assert_not_reached ();
3736 * mono_amd64_emit_tls_get:
3737 * @code: buffer to store code to
3738 * @dreg: hard register where to place the result
3739 * @tls_offset: offset info
3741 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3742 * the dreg register the item in the thread local storage identified
3745 * Returns: a pointer to the end of the stored code
3748 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3751 if (tls_offset < 64) {
3752 x86_prefix (code, X86_GS_PREFIX);
3753 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3757 g_assert (tls_offset < 0x440);
3758 /* Load TEB->TlsExpansionSlots */
3759 x86_prefix (code, X86_GS_PREFIX);
3760 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3761 amd64_test_reg_reg (code, dreg, dreg);
3763 amd64_branch (code, X86_CC_EQ, code, TRUE);
3764 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3765 amd64_patch (buf [0], code);
3767 #elif defined(__APPLE__)
3768 x86_prefix (code, X86_GS_PREFIX);
3769 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3771 if (optimize_for_xen) {
3772 x86_prefix (code, X86_FS_PREFIX);
3773 amd64_mov_reg_mem (code, dreg, 0, 8);
3774 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3776 x86_prefix (code, X86_FS_PREFIX);
3777 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3784 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3786 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3788 if (dreg != offset_reg)
3789 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3790 amd64_prefix (code, X86_GS_PREFIX);
3791 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3792 #elif defined(__linux__)
3795 if (dreg == offset_reg) {
3796 /* Use a temporary reg by saving it to the redzone */
3797 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3798 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3799 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3800 offset_reg = tmpreg;
3802 x86_prefix (code, X86_FS_PREFIX);
3803 amd64_mov_reg_mem (code, dreg, 0, 8);
3804 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3806 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3808 g_assert_not_reached ();
3814 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3817 g_assert_not_reached ();
3818 #elif defined(__APPLE__)
3819 x86_prefix (code, X86_GS_PREFIX);
3820 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3822 g_assert (!optimize_for_xen);
3823 x86_prefix (code, X86_FS_PREFIX);
3824 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3830 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3832 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3834 g_assert_not_reached ();
3835 #elif defined(__APPLE__)
3836 x86_prefix (code, X86_GS_PREFIX);
3837 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3839 x86_prefix (code, X86_FS_PREFIX);
3840 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3846 * mono_arch_translate_tls_offset:
3848 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3851 mono_arch_translate_tls_offset (int offset)
3854 return tls_gs_offset + (offset * 8);
3863 * Emit code to initialize an LMF structure at LMF_OFFSET.
3866 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3869 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3872 * sp is saved right before calls but we need to save it here too so
3873 * async stack walks would work.
3875 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3877 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3878 if (cfg->arch.omit_fp && cfa_offset != -1)
3879 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3881 /* These can't contain refs */
3882 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3883 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3884 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3885 /* These are handled automatically by the stack marking code */
3886 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3891 /* benchmark and set based on cpu */
3892 #define LOOP_ALIGNMENT 8
3893 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3897 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3902 guint8 *code = cfg->native_code + cfg->code_len;
3905 /* Fix max_offset estimate for each successor bb */
3906 if (cfg->opt & MONO_OPT_BRANCH) {
3907 int current_offset = cfg->code_len;
3908 MonoBasicBlock *current_bb;
3909 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3910 current_bb->max_offset = current_offset;
3911 current_offset += current_bb->max_length;
3915 if (cfg->opt & MONO_OPT_LOOP) {
3916 int pad, align = LOOP_ALIGNMENT;
3917 /* set alignment depending on cpu */
3918 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3920 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3921 amd64_padding (code, pad);
3922 cfg->code_len += pad;
3923 bb->native_offset = cfg->code_len;
3927 #if defined(__native_client_codegen__)
3928 /* For Native Client, all indirect call/jump targets must be */
3929 /* 32-byte aligned. Exception handler blocks are jumped to */
3930 /* indirectly as well. */
3931 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
3932 (bb->flags & BB_EXCEPTION_HANDLER);
3934 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
3935 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
3936 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
3937 cfg->code_len += pad;
3938 bb->native_offset = cfg->code_len;
3940 #endif /*__native_client_codegen__*/
3942 if (cfg->verbose_level > 2)
3943 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3945 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
3946 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3947 g_assert (!cfg->compile_aot);
3949 cov->data [bb->dfn].cil_code = bb->cil_code;
3950 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3951 /* this is not thread save, but good enough */
3952 amd64_inc_membase (code, AMD64_R11, 0);
3955 offset = code - cfg->native_code;
3957 mono_debug_open_block (cfg, bb, offset);
3959 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)
3960 x86_breakpoint (code);
3962 MONO_BB_FOR_EACH_INS (bb, ins) {
3963 offset = code - cfg->native_code;
3965 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3967 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
3969 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
3970 cfg->code_size *= 2;
3971 cfg->native_code = (unsigned char *)mono_realloc_native_code(cfg);
3972 code = cfg->native_code + offset;
3973 cfg->stat_code_reallocs++;
3976 if (cfg->debug_info)
3977 mono_debug_record_line_number (cfg, ins, offset);
3979 switch (ins->opcode) {
3981 amd64_mul_reg (code, ins->sreg2, TRUE);
3984 amd64_mul_reg (code, ins->sreg2, FALSE);
3986 case OP_X86_SETEQ_MEMBASE:
3987 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3989 case OP_STOREI1_MEMBASE_IMM:
3990 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3992 case OP_STOREI2_MEMBASE_IMM:
3993 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
3995 case OP_STOREI4_MEMBASE_IMM:
3996 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
3998 case OP_STOREI1_MEMBASE_REG:
3999 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
4001 case OP_STOREI2_MEMBASE_REG:
4002 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
4004 /* In AMD64 NaCl, pointers are 4 bytes, */
4005 /* so STORE_* != STOREI8_*. Likewise below. */
4006 case OP_STORE_MEMBASE_REG:
4007 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
4009 case OP_STOREI8_MEMBASE_REG:
4010 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
4012 case OP_STOREI4_MEMBASE_REG:
4013 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
4015 case OP_STORE_MEMBASE_IMM:
4016 #ifndef __native_client_codegen__
4017 /* In NaCl, this could be a PCONST type, which could */
4018 /* mean a pointer type was copied directly into the */
4019 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
4020 /* the value would be 0x00000000FFFFFFFF which is */
4021 /* not proper for an imm32 unless you cast it. */
4022 g_assert (amd64_is_imm32 (ins->inst_imm));
4024 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
4026 case OP_STOREI8_MEMBASE_IMM:
4027 g_assert (amd64_is_imm32 (ins->inst_imm));
4028 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
4031 #ifdef __mono_ilp32__
4032 /* In ILP32, pointers are 4 bytes, so separate these */
4033 /* cases, use literal 8 below where we really want 8 */
4034 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4035 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
4039 // FIXME: Decompose this earlier
4040 if (amd64_use_imm32 (ins->inst_imm))
4041 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
4043 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4044 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
4048 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4049 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
4052 // FIXME: Decompose this earlier
4053 if (amd64_use_imm32 (ins->inst_imm))
4054 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
4056 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
4057 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
4061 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4062 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
4065 /* For NaCl, pointers are 4 bytes, so separate these */
4066 /* cases, use literal 8 below where we really want 8 */
4067 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
4068 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
4070 case OP_LOAD_MEMBASE:
4071 g_assert (amd64_is_imm32 (ins->inst_offset));
4072 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
4074 case OP_LOADI8_MEMBASE:
4075 /* Use literal 8 instead of sizeof pointer or */
4076 /* register, we really want 8 for this opcode */
4077 g_assert (amd64_is_imm32 (ins->inst_offset));
4078 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
4080 case OP_LOADI4_MEMBASE:
4081 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4083 case OP_LOADU4_MEMBASE:
4084 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
4086 case OP_LOADU1_MEMBASE:
4087 /* The cpu zero extends the result into 64 bits */
4088 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
4090 case OP_LOADI1_MEMBASE:
4091 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
4093 case OP_LOADU2_MEMBASE:
4094 /* The cpu zero extends the result into 64 bits */
4095 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
4097 case OP_LOADI2_MEMBASE:
4098 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
4100 case OP_AMD64_LOADI8_MEMINDEX:
4101 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
4103 case OP_LCONV_TO_I1:
4104 case OP_ICONV_TO_I1:
4106 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
4108 case OP_LCONV_TO_I2:
4109 case OP_ICONV_TO_I2:
4111 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
4113 case OP_LCONV_TO_U1:
4114 case OP_ICONV_TO_U1:
4115 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
4117 case OP_LCONV_TO_U2:
4118 case OP_ICONV_TO_U2:
4119 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
4122 /* Clean out the upper word */
4123 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4126 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
4130 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4132 case OP_COMPARE_IMM:
4133 #if defined(__mono_ilp32__)
4134 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
4135 g_assert (amd64_is_imm32 (ins->inst_imm));
4136 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4139 case OP_LCOMPARE_IMM:
4140 g_assert (amd64_is_imm32 (ins->inst_imm));
4141 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
4143 case OP_X86_COMPARE_REG_MEMBASE:
4144 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
4146 case OP_X86_TEST_NULL:
4147 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
4149 case OP_AMD64_TEST_NULL:
4150 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4153 case OP_X86_ADD_REG_MEMBASE:
4154 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4156 case OP_X86_SUB_REG_MEMBASE:
4157 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4159 case OP_X86_AND_REG_MEMBASE:
4160 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4162 case OP_X86_OR_REG_MEMBASE:
4163 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4165 case OP_X86_XOR_REG_MEMBASE:
4166 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4169 case OP_X86_ADD_MEMBASE_IMM:
4170 /* FIXME: Make a 64 version too */
4171 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4173 case OP_X86_SUB_MEMBASE_IMM:
4174 g_assert (amd64_is_imm32 (ins->inst_imm));
4175 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4177 case OP_X86_AND_MEMBASE_IMM:
4178 g_assert (amd64_is_imm32 (ins->inst_imm));
4179 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4181 case OP_X86_OR_MEMBASE_IMM:
4182 g_assert (amd64_is_imm32 (ins->inst_imm));
4183 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4185 case OP_X86_XOR_MEMBASE_IMM:
4186 g_assert (amd64_is_imm32 (ins->inst_imm));
4187 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4189 case OP_X86_ADD_MEMBASE_REG:
4190 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4192 case OP_X86_SUB_MEMBASE_REG:
4193 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4195 case OP_X86_AND_MEMBASE_REG:
4196 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4198 case OP_X86_OR_MEMBASE_REG:
4199 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4201 case OP_X86_XOR_MEMBASE_REG:
4202 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4204 case OP_X86_INC_MEMBASE:
4205 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4207 case OP_X86_INC_REG:
4208 amd64_inc_reg_size (code, ins->dreg, 4);
4210 case OP_X86_DEC_MEMBASE:
4211 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4213 case OP_X86_DEC_REG:
4214 amd64_dec_reg_size (code, ins->dreg, 4);
4216 case OP_X86_MUL_REG_MEMBASE:
4217 case OP_X86_MUL_MEMBASE_REG:
4218 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4220 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4221 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4223 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4224 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4226 case OP_AMD64_COMPARE_MEMBASE_REG:
4227 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4229 case OP_AMD64_COMPARE_MEMBASE_IMM:
4230 g_assert (amd64_is_imm32 (ins->inst_imm));
4231 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4233 case OP_X86_COMPARE_MEMBASE8_IMM:
4234 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4236 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4237 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4239 case OP_AMD64_COMPARE_REG_MEMBASE:
4240 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4243 case OP_AMD64_ADD_REG_MEMBASE:
4244 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4246 case OP_AMD64_SUB_REG_MEMBASE:
4247 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4249 case OP_AMD64_AND_REG_MEMBASE:
4250 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4252 case OP_AMD64_OR_REG_MEMBASE:
4253 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4255 case OP_AMD64_XOR_REG_MEMBASE:
4256 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4259 case OP_AMD64_ADD_MEMBASE_REG:
4260 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4262 case OP_AMD64_SUB_MEMBASE_REG:
4263 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4265 case OP_AMD64_AND_MEMBASE_REG:
4266 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4268 case OP_AMD64_OR_MEMBASE_REG:
4269 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4271 case OP_AMD64_XOR_MEMBASE_REG:
4272 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4275 case OP_AMD64_ADD_MEMBASE_IMM:
4276 g_assert (amd64_is_imm32 (ins->inst_imm));
4277 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4279 case OP_AMD64_SUB_MEMBASE_IMM:
4280 g_assert (amd64_is_imm32 (ins->inst_imm));
4281 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4283 case OP_AMD64_AND_MEMBASE_IMM:
4284 g_assert (amd64_is_imm32 (ins->inst_imm));
4285 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4287 case OP_AMD64_OR_MEMBASE_IMM:
4288 g_assert (amd64_is_imm32 (ins->inst_imm));
4289 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4291 case OP_AMD64_XOR_MEMBASE_IMM:
4292 g_assert (amd64_is_imm32 (ins->inst_imm));
4293 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4297 amd64_breakpoint (code);
4299 case OP_RELAXED_NOP:
4300 x86_prefix (code, X86_REP_PREFIX);
4308 case OP_DUMMY_STORE:
4309 case OP_DUMMY_ICONST:
4310 case OP_DUMMY_R8CONST:
4311 case OP_NOT_REACHED:
4314 case OP_IL_SEQ_POINT:
4315 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4317 case OP_SEQ_POINT: {
4318 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4319 MonoInst *var = (MonoInst *)cfg->arch.ss_tramp_var;
4322 /* Load ss_tramp_var */
4323 /* This is equal to &ss_trampoline */
4324 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4325 /* Load the trampoline address */
4326 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4327 /* Call it if it is non-null */
4328 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4330 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4331 amd64_call_reg (code, AMD64_R11);
4332 amd64_patch (label, code);
4336 * This is the address which is saved in seq points,
4338 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4340 if (cfg->compile_aot) {
4341 guint32 offset = code - cfg->native_code;
4343 MonoInst *info_var = (MonoInst *)cfg->arch.seq_point_info_var;
4347 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4348 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4349 /* Load the info->bp_addrs [offset], which is either NULL or the address of the breakpoint trampoline */
4350 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4351 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4353 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4354 /* Call the trampoline */
4355 amd64_call_reg (code, AMD64_R11);
4356 amd64_patch (label, code);
4358 MonoInst *var = (MonoInst *)cfg->arch.bp_tramp_var;
4362 * Emit a test+branch against a constant, the constant will be overwritten
4363 * by mono_arch_set_breakpoint () to cause the test to fail.
4365 amd64_mov_reg_imm (code, AMD64_R11, 0);
4366 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4368 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4371 g_assert (var->opcode == OP_REGOFFSET);
4372 /* Load bp_tramp_var */
4373 /* This is equal to &bp_trampoline */
4374 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4375 /* Call the trampoline */
4376 amd64_call_membase (code, AMD64_R11, 0);
4377 amd64_patch (label, code);
4380 * Add an additional nop so skipping the bp doesn't cause the ip to point
4381 * to another IL offset.
4389 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4392 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4396 g_assert (amd64_is_imm32 (ins->inst_imm));
4397 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4400 g_assert (amd64_is_imm32 (ins->inst_imm));
4401 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4406 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4409 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4413 g_assert (amd64_is_imm32 (ins->inst_imm));
4414 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4417 g_assert (amd64_is_imm32 (ins->inst_imm));
4418 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4421 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4425 g_assert (amd64_is_imm32 (ins->inst_imm));
4426 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4429 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4434 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4436 switch (ins->inst_imm) {
4440 if (ins->dreg != ins->sreg1)
4441 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4442 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4445 /* LEA r1, [r2 + r2*2] */
4446 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4449 /* LEA r1, [r2 + r2*4] */
4450 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4453 /* LEA r1, [r2 + r2*2] */
4455 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4456 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4459 /* LEA r1, [r2 + r2*8] */
4460 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4463 /* LEA r1, [r2 + r2*4] */
4465 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4466 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4469 /* LEA r1, [r2 + r2*2] */
4471 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4472 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4475 /* LEA r1, [r2 + r2*4] */
4476 /* LEA r1, [r1 + r1*4] */
4477 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4478 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4481 /* LEA r1, [r2 + r2*4] */
4483 /* LEA r1, [r1 + r1*4] */
4484 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4485 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4486 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4489 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
4496 #if defined( __native_client_codegen__ )
4497 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4498 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4500 /* Regalloc magic makes the div/rem cases the same */
4501 if (ins->sreg2 == AMD64_RDX) {
4502 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4504 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4507 amd64_div_reg (code, ins->sreg2, TRUE);
4512 #if defined( __native_client_codegen__ )
4513 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4514 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4516 if (ins->sreg2 == AMD64_RDX) {
4517 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4518 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4519 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4521 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4522 amd64_div_reg (code, ins->sreg2, FALSE);
4527 #if defined( __native_client_codegen__ )
4528 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4529 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4531 if (ins->sreg2 == AMD64_RDX) {
4532 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4533 amd64_cdq_size (code, 4);
4534 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4536 amd64_cdq_size (code, 4);
4537 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4542 #if defined( __native_client_codegen__ )
4543 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4544 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4546 if (ins->sreg2 == AMD64_RDX) {
4547 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4548 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4549 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4551 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4552 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4556 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4557 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4560 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4564 g_assert (amd64_is_imm32 (ins->inst_imm));
4565 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4568 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4572 g_assert (amd64_is_imm32 (ins->inst_imm));
4573 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4576 g_assert (ins->sreg2 == AMD64_RCX);
4577 amd64_shift_reg (code, X86_SHL, ins->dreg);
4580 g_assert (ins->sreg2 == AMD64_RCX);
4581 amd64_shift_reg (code, X86_SAR, ins->dreg);
4585 g_assert (amd64_is_imm32 (ins->inst_imm));
4586 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4589 g_assert (amd64_is_imm32 (ins->inst_imm));
4590 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4592 case OP_LSHR_UN_IMM:
4593 g_assert (amd64_is_imm32 (ins->inst_imm));
4594 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4597 g_assert (ins->sreg2 == AMD64_RCX);
4598 amd64_shift_reg (code, X86_SHR, ins->dreg);
4602 g_assert (amd64_is_imm32 (ins->inst_imm));
4603 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4608 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4611 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4614 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4617 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4621 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4624 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4627 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4630 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4633 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4636 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4639 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4642 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4645 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4648 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4651 amd64_neg_reg_size (code, ins->sreg1, 4);
4654 amd64_not_reg_size (code, ins->sreg1, 4);
4657 g_assert (ins->sreg2 == AMD64_RCX);
4658 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4661 g_assert (ins->sreg2 == AMD64_RCX);
4662 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4665 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4667 case OP_ISHR_UN_IMM:
4668 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4671 g_assert (ins->sreg2 == AMD64_RCX);
4672 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4675 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4678 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4681 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4682 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4684 case OP_IMUL_OVF_UN:
4685 case OP_LMUL_OVF_UN: {
4686 /* the mul operation and the exception check should most likely be split */
4687 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4688 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4689 /*g_assert (ins->sreg2 == X86_EAX);
4690 g_assert (ins->dreg == X86_EAX);*/
4691 if (ins->sreg2 == X86_EAX) {
4692 non_eax_reg = ins->sreg1;
4693 } else if (ins->sreg1 == X86_EAX) {
4694 non_eax_reg = ins->sreg2;
4696 /* no need to save since we're going to store to it anyway */
4697 if (ins->dreg != X86_EAX) {
4699 amd64_push_reg (code, X86_EAX);
4701 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4702 non_eax_reg = ins->sreg2;
4704 if (ins->dreg == X86_EDX) {
4707 amd64_push_reg (code, X86_EAX);
4711 amd64_push_reg (code, X86_EDX);
4713 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4714 /* save before the check since pop and mov don't change the flags */
4715 if (ins->dreg != X86_EAX)
4716 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4718 amd64_pop_reg (code, X86_EDX);
4720 amd64_pop_reg (code, X86_EAX);
4721 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4725 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4727 case OP_ICOMPARE_IMM:
4728 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4750 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4758 case OP_CMOV_INE_UN:
4759 case OP_CMOV_IGE_UN:
4760 case OP_CMOV_IGT_UN:
4761 case OP_CMOV_ILE_UN:
4762 case OP_CMOV_ILT_UN:
4768 case OP_CMOV_LNE_UN:
4769 case OP_CMOV_LGE_UN:
4770 case OP_CMOV_LGT_UN:
4771 case OP_CMOV_LLE_UN:
4772 case OP_CMOV_LLT_UN:
4773 g_assert (ins->dreg == ins->sreg1);
4774 /* This needs to operate on 64 bit values */
4775 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);
4779 amd64_not_reg (code, ins->sreg1);
4782 amd64_neg_reg (code, ins->sreg1);
4787 if ((((guint64)ins->inst_c0) >> 32) == 0 && !mini_get_debug_options()->single_imm_size)
4788 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4790 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4793 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4794 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4797 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4798 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4801 if (ins->dreg != ins->sreg1)
4802 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4804 case OP_AMD64_SET_XMMREG_R4: {
4806 if (ins->dreg != ins->sreg1)
4807 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4809 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4813 case OP_AMD64_SET_XMMREG_R8: {
4814 if (ins->dreg != ins->sreg1)
4815 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4819 MonoCallInst *call = (MonoCallInst*)ins;
4820 int i, save_area_offset;
4822 g_assert (!cfg->method->save_lmf);
4824 /* Restore callee saved registers */
4825 save_area_offset = cfg->arch.reg_save_area_offset;
4826 for (i = 0; i < AMD64_NREG; ++i)
4827 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4828 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4829 save_area_offset += 8;
4832 if (cfg->arch.omit_fp) {
4833 if (cfg->arch.stack_alloc_size)
4834 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4836 if (call->stack_usage)
4839 /* Copy arguments on the stack to our argument area */
4840 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4841 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4842 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4848 offset = code - cfg->native_code;
4849 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4850 if (cfg->compile_aot)
4851 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4853 amd64_set_reg_template (code, AMD64_R11);
4854 amd64_jump_reg (code, AMD64_R11);
4855 ins->flags |= MONO_INST_GC_CALLSITE;
4856 ins->backend.pc_offset = code - cfg->native_code;
4860 /* ensure ins->sreg1 is not NULL */
4861 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4864 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4865 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4875 call = (MonoCallInst*)ins;
4877 * The AMD64 ABI forces callers to know about varargs.
4879 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4880 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4881 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4883 * Since the unmanaged calling convention doesn't contain a
4884 * 'vararg' entry, we have to treat every pinvoke call as a
4885 * potential vararg call.
4889 for (i = 0; i < AMD64_XMM_NREG; ++i)
4890 if (call->used_fregs & (1 << i))
4893 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4895 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4898 if (ins->flags & MONO_INST_HAS_METHOD)
4899 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4901 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
4902 ins->flags |= MONO_INST_GC_CALLSITE;
4903 ins->backend.pc_offset = code - cfg->native_code;
4904 code = emit_move_return_value (cfg, ins, code);
4911 case OP_VOIDCALL_REG:
4913 call = (MonoCallInst*)ins;
4915 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4916 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4917 ins->sreg1 = AMD64_R11;
4921 * The AMD64 ABI forces callers to know about varargs.
4923 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4924 if (ins->sreg1 == AMD64_RAX) {
4925 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4926 ins->sreg1 = AMD64_R11;
4928 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4929 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4931 * Since the unmanaged calling convention doesn't contain a
4932 * 'vararg' entry, we have to treat every pinvoke call as a
4933 * potential vararg call.
4937 for (i = 0; i < AMD64_XMM_NREG; ++i)
4938 if (call->used_fregs & (1 << i))
4940 if (ins->sreg1 == AMD64_RAX) {
4941 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4942 ins->sreg1 = AMD64_R11;
4945 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4947 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4950 amd64_call_reg (code, ins->sreg1);
4951 ins->flags |= MONO_INST_GC_CALLSITE;
4952 ins->backend.pc_offset = code - cfg->native_code;
4953 code = emit_move_return_value (cfg, ins, code);
4955 case OP_FCALL_MEMBASE:
4956 case OP_RCALL_MEMBASE:
4957 case OP_LCALL_MEMBASE:
4958 case OP_VCALL_MEMBASE:
4959 case OP_VCALL2_MEMBASE:
4960 case OP_VOIDCALL_MEMBASE:
4961 case OP_CALL_MEMBASE:
4962 call = (MonoCallInst*)ins;
4964 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
4965 ins->flags |= MONO_INST_GC_CALLSITE;
4966 ins->backend.pc_offset = code - cfg->native_code;
4967 code = emit_move_return_value (cfg, ins, code);
4971 MonoInst *var = cfg->dyn_call_var;
4974 g_assert (var->opcode == OP_REGOFFSET);
4976 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
4977 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4979 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
4981 /* Save args buffer */
4982 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
4984 /* Set fp arg regs */
4985 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, has_fp), sizeof (mgreg_t));
4986 amd64_test_reg_reg (code, AMD64_RAX, AMD64_RAX);
4988 amd64_branch8 (code, X86_CC_Z, -1, 1);
4989 for (i = 0; i < FLOAT_PARAM_REGS; ++i)
4990 amd64_sse_movsd_reg_membase (code, i, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, fregs) + (i * sizeof (double)));
4991 amd64_patch (label, code);
4993 /* Set argument registers */
4994 for (i = 0; i < PARAM_REGS; ++i)
4995 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
4998 amd64_call_reg (code, AMD64_R10);
5000 ins->flags |= MONO_INST_GC_CALLSITE;
5001 ins->backend.pc_offset = code - cfg->native_code;
5004 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
5005 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
5006 amd64_sse_movsd_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, fregs), AMD64_XMM0);
5009 case OP_AMD64_SAVE_SP_TO_LMF: {
5010 MonoInst *lmf_var = cfg->lmf_var;
5011 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
5015 g_assert_not_reached ();
5016 amd64_push_reg (code, ins->sreg1);
5018 case OP_X86_PUSH_IMM:
5019 g_assert_not_reached ();
5020 g_assert (amd64_is_imm32 (ins->inst_imm));
5021 amd64_push_imm (code, ins->inst_imm);
5023 case OP_X86_PUSH_MEMBASE:
5024 g_assert_not_reached ();
5025 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
5027 case OP_X86_PUSH_OBJ: {
5028 int size = ALIGN_TO (ins->inst_imm, 8);
5030 g_assert_not_reached ();
5032 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5033 amd64_push_reg (code, AMD64_RDI);
5034 amd64_push_reg (code, AMD64_RSI);
5035 amd64_push_reg (code, AMD64_RCX);
5036 if (ins->inst_offset)
5037 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
5039 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
5040 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
5041 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
5043 amd64_prefix (code, X86_REP_PREFIX);
5045 amd64_pop_reg (code, AMD64_RCX);
5046 amd64_pop_reg (code, AMD64_RSI);
5047 amd64_pop_reg (code, AMD64_RDI);
5050 case OP_GENERIC_CLASS_INIT: {
5051 static int byte_offset = -1;
5052 static guint8 bitmask;
5055 g_assert (ins->sreg1 == MONO_AMD64_ARG_REG1);
5057 if (byte_offset < 0)
5058 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
5060 amd64_test_membase_imm_size (code, ins->sreg1, byte_offset, bitmask, 1);
5062 amd64_branch8 (code, X86_CC_NZ, -1, 1);
5064 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_generic_class_init", FALSE);
5065 ins->flags |= MONO_INST_GC_CALLSITE;
5066 ins->backend.pc_offset = code - cfg->native_code;
5068 x86_patch (jump, code);
5073 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
5075 case OP_X86_LEA_MEMBASE:
5076 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
5079 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
5082 /* keep alignment */
5083 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
5084 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
5085 code = mono_emit_stack_alloc (cfg, code, ins);
5086 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5087 if (cfg->param_area)
5088 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5090 case OP_LOCALLOC_IMM: {
5091 guint32 size = ins->inst_imm;
5092 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
5094 if (ins->flags & MONO_INST_INIT) {
5098 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5099 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5101 for (i = 0; i < size; i += 8)
5102 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
5103 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5105 amd64_mov_reg_imm (code, ins->dreg, size);
5106 ins->sreg1 = ins->dreg;
5108 code = mono_emit_stack_alloc (cfg, code, ins);
5109 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5112 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
5113 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
5115 if (cfg->param_area)
5116 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
5120 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5121 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5122 (gpointer)"mono_arch_throw_exception", FALSE);
5123 ins->flags |= MONO_INST_GC_CALLSITE;
5124 ins->backend.pc_offset = code - cfg->native_code;
5128 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5129 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5130 (gpointer)"mono_arch_rethrow_exception", FALSE);
5131 ins->flags |= MONO_INST_GC_CALLSITE;
5132 ins->backend.pc_offset = code - cfg->native_code;
5135 case OP_CALL_HANDLER:
5137 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5138 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5139 amd64_call_imm (code, 0);
5140 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
5141 /* Restore stack alignment */
5142 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5144 case OP_START_HANDLER: {
5145 /* Even though we're saving RSP, use sizeof */
5146 /* gpointer because spvar is of type IntPtr */
5147 /* see: mono_create_spvar_for_region */
5148 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5149 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
5151 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
5152 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
5154 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
5158 case OP_ENDFINALLY: {
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));
5164 case OP_ENDFILTER: {
5165 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5166 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5167 /* The local allocator will put the result into RAX */
5172 if (ins->dreg != AMD64_RAX)
5173 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, sizeof (gpointer));
5176 ins->inst_c0 = code - cfg->native_code;
5179 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
5180 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
5182 if (ins->inst_target_bb->native_offset) {
5183 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
5185 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5186 if ((cfg->opt & MONO_OPT_BRANCH) &&
5187 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
5188 x86_jump8 (code, 0);
5190 x86_jump32 (code, 0);
5194 amd64_jump_reg (code, ins->sreg1);
5217 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5218 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5220 case OP_COND_EXC_EQ:
5221 case OP_COND_EXC_NE_UN:
5222 case OP_COND_EXC_LT:
5223 case OP_COND_EXC_LT_UN:
5224 case OP_COND_EXC_GT:
5225 case OP_COND_EXC_GT_UN:
5226 case OP_COND_EXC_GE:
5227 case OP_COND_EXC_GE_UN:
5228 case OP_COND_EXC_LE:
5229 case OP_COND_EXC_LE_UN:
5230 case OP_COND_EXC_IEQ:
5231 case OP_COND_EXC_INE_UN:
5232 case OP_COND_EXC_ILT:
5233 case OP_COND_EXC_ILT_UN:
5234 case OP_COND_EXC_IGT:
5235 case OP_COND_EXC_IGT_UN:
5236 case OP_COND_EXC_IGE:
5237 case OP_COND_EXC_IGE_UN:
5238 case OP_COND_EXC_ILE:
5239 case OP_COND_EXC_ILE_UN:
5240 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], (const char *)ins->inst_p1);
5242 case OP_COND_EXC_OV:
5243 case OP_COND_EXC_NO:
5245 case OP_COND_EXC_NC:
5246 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5247 (ins->opcode < OP_COND_EXC_NE_UN), (const char *)ins->inst_p1);
5249 case OP_COND_EXC_IOV:
5250 case OP_COND_EXC_INO:
5251 case OP_COND_EXC_IC:
5252 case OP_COND_EXC_INC:
5253 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5254 (ins->opcode < OP_COND_EXC_INE_UN), (const char *)ins->inst_p1);
5257 /* floating point opcodes */
5259 double d = *(double *)ins->inst_p0;
5261 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5262 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5265 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5266 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5271 float f = *(float *)ins->inst_p0;
5273 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5275 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5277 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5280 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5281 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5283 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5287 case OP_STORER8_MEMBASE_REG:
5288 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5290 case OP_LOADR8_MEMBASE:
5291 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5293 case OP_STORER4_MEMBASE_REG:
5295 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5297 /* This requires a double->single conversion */
5298 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5299 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5302 case OP_LOADR4_MEMBASE:
5304 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5306 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5307 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5310 case OP_ICONV_TO_R4:
5312 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5314 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5315 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5318 case OP_ICONV_TO_R8:
5319 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5321 case OP_LCONV_TO_R4:
5323 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5325 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5326 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5329 case OP_LCONV_TO_R8:
5330 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5332 case OP_FCONV_TO_R4:
5334 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5336 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5337 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5340 case OP_FCONV_TO_I1:
5341 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5343 case OP_FCONV_TO_U1:
5344 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5346 case OP_FCONV_TO_I2:
5347 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5349 case OP_FCONV_TO_U2:
5350 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5352 case OP_FCONV_TO_U4:
5353 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5355 case OP_FCONV_TO_I4:
5357 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5359 case OP_FCONV_TO_I8:
5360 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5363 case OP_RCONV_TO_I1:
5364 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5365 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5367 case OP_RCONV_TO_U1:
5368 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5369 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5371 case OP_RCONV_TO_I2:
5372 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5373 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5375 case OP_RCONV_TO_U2:
5376 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5377 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5379 case OP_RCONV_TO_I4:
5380 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5382 case OP_RCONV_TO_U4:
5383 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5385 case OP_RCONV_TO_I8:
5386 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5388 case OP_RCONV_TO_R8:
5389 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5391 case OP_RCONV_TO_R4:
5392 if (ins->dreg != ins->sreg1)
5393 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5396 case OP_LCONV_TO_R_UN: {
5399 /* Based on gcc code */
5400 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5401 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5404 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5405 br [1] = code; x86_jump8 (code, 0);
5406 amd64_patch (br [0], code);
5409 /* Save to the red zone */
5410 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5411 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5412 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5413 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5414 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5415 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5416 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5417 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5418 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5420 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5421 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5422 amd64_patch (br [1], code);
5425 case OP_LCONV_TO_OVF_U4:
5426 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5427 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5428 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5430 case OP_LCONV_TO_OVF_I4_UN:
5431 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5432 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5433 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5436 if (ins->dreg != ins->sreg1)
5437 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5440 if (ins->dreg != ins->sreg1)
5441 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5443 case OP_MOVE_F_TO_I4:
5445 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5447 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5448 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5451 case OP_MOVE_I4_TO_F:
5452 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5454 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5456 case OP_MOVE_F_TO_I8:
5457 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5459 case OP_MOVE_I8_TO_F:
5460 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5463 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5466 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5469 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5472 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5475 static double r8_0 = -0.0;
5477 g_assert (ins->sreg1 == ins->dreg);
5479 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5480 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5484 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5487 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5490 static guint64 d = 0x7fffffffffffffffUL;
5492 g_assert (ins->sreg1 == ins->dreg);
5494 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5495 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5499 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5503 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5506 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5509 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5512 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5515 static float r4_0 = -0.0;
5517 g_assert (ins->sreg1 == ins->dreg);
5519 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5520 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5521 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
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, TRUE, 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_GT, FALSE, 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, TRUE, 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_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5547 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
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, TRUE, 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_GT, FALSE, 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, TRUE, ins->dreg, ins->sreg2);
5568 g_assert (cfg->opt & MONO_OPT_CMOV);
5569 g_assert (ins->dreg == ins->sreg1);
5570 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5571 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5577 * The two arguments are swapped because the fbranch instructions
5578 * depend on this for the non-sse case to work.
5580 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5584 * FIXME: Get rid of this.
5585 * The two arguments are swapped because the fbranch instructions
5586 * depend on this for the non-sse case to work.
5588 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5592 /* zeroing the register at the start results in
5593 * shorter and faster code (we can also remove the widening op)
5595 guchar *unordered_check;
5597 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5598 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5599 unordered_check = code;
5600 x86_branch8 (code, X86_CC_P, 0, FALSE);
5602 if (ins->opcode == OP_FCEQ) {
5603 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5604 amd64_patch (unordered_check, code);
5606 guchar *jump_to_end;
5607 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5609 x86_jump8 (code, 0);
5610 amd64_patch (unordered_check, code);
5611 amd64_inc_reg (code, ins->dreg);
5612 amd64_patch (jump_to_end, code);
5618 /* zeroing the register at the start results in
5619 * shorter and faster code (we can also remove the widening op)
5621 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5622 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5623 if (ins->opcode == OP_FCLT_UN) {
5624 guchar *unordered_check = code;
5625 guchar *jump_to_end;
5626 x86_branch8 (code, X86_CC_P, 0, FALSE);
5627 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5629 x86_jump8 (code, 0);
5630 amd64_patch (unordered_check, code);
5631 amd64_inc_reg (code, ins->dreg);
5632 amd64_patch (jump_to_end, code);
5634 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5639 guchar *unordered_check;
5640 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5641 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5642 unordered_check = code;
5643 x86_branch8 (code, X86_CC_P, 0, FALSE);
5644 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5645 amd64_patch (unordered_check, code);
5650 /* zeroing the register at the start results in
5651 * shorter and faster code (we can also remove the widening op)
5653 guchar *unordered_check;
5655 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5656 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5657 if (ins->opcode == OP_FCGT) {
5658 unordered_check = code;
5659 x86_branch8 (code, X86_CC_P, 0, FALSE);
5660 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5661 amd64_patch (unordered_check, code);
5663 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5668 guchar *unordered_check;
5669 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5670 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5671 unordered_check = code;
5672 x86_branch8 (code, X86_CC_P, 0, FALSE);
5673 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5674 amd64_patch (unordered_check, code);
5684 gboolean unordered = FALSE;
5686 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5687 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5689 switch (ins->opcode) {
5691 x86_cond = X86_CC_EQ;
5694 x86_cond = X86_CC_LT;
5697 x86_cond = X86_CC_GT;
5700 x86_cond = X86_CC_GT;
5704 x86_cond = X86_CC_LT;
5708 g_assert_not_reached ();
5713 guchar *unordered_check;
5714 guchar *jump_to_end;
5716 unordered_check = code;
5717 x86_branch8 (code, X86_CC_P, 0, FALSE);
5718 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5720 x86_jump8 (code, 0);
5721 amd64_patch (unordered_check, code);
5722 amd64_inc_reg (code, ins->dreg);
5723 amd64_patch (jump_to_end, code);
5725 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5729 case OP_FCLT_MEMBASE:
5730 case OP_FCGT_MEMBASE:
5731 case OP_FCLT_UN_MEMBASE:
5732 case OP_FCGT_UN_MEMBASE:
5733 case OP_FCEQ_MEMBASE: {
5734 guchar *unordered_check, *jump_to_end;
5737 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5738 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5740 switch (ins->opcode) {
5741 case OP_FCEQ_MEMBASE:
5742 x86_cond = X86_CC_EQ;
5744 case OP_FCLT_MEMBASE:
5745 case OP_FCLT_UN_MEMBASE:
5746 x86_cond = X86_CC_LT;
5748 case OP_FCGT_MEMBASE:
5749 case OP_FCGT_UN_MEMBASE:
5750 x86_cond = X86_CC_GT;
5753 g_assert_not_reached ();
5756 unordered_check = code;
5757 x86_branch8 (code, X86_CC_P, 0, FALSE);
5758 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5760 switch (ins->opcode) {
5761 case OP_FCEQ_MEMBASE:
5762 case OP_FCLT_MEMBASE:
5763 case OP_FCGT_MEMBASE:
5764 amd64_patch (unordered_check, code);
5766 case OP_FCLT_UN_MEMBASE:
5767 case OP_FCGT_UN_MEMBASE:
5769 x86_jump8 (code, 0);
5770 amd64_patch (unordered_check, code);
5771 amd64_inc_reg (code, ins->dreg);
5772 amd64_patch (jump_to_end, code);
5780 guchar *jump = code;
5781 x86_branch8 (code, X86_CC_P, 0, TRUE);
5782 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5783 amd64_patch (jump, code);
5787 /* Branch if C013 != 100 */
5788 /* branch if !ZF or (PF|CF) */
5789 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5790 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5791 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5794 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5797 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5798 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5802 if (ins->opcode == OP_FBGT) {
5805 /* skip branch if C1=1 */
5807 x86_branch8 (code, X86_CC_P, 0, FALSE);
5808 /* branch if (C0 | C3) = 1 */
5809 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5810 amd64_patch (br1, code);
5813 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5817 /* Branch if C013 == 100 or 001 */
5820 /* skip branch if C1=1 */
5822 x86_branch8 (code, X86_CC_P, 0, FALSE);
5823 /* branch if (C0 | C3) = 1 */
5824 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5825 amd64_patch (br1, code);
5829 /* Branch if C013 == 000 */
5830 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5833 /* Branch if C013=000 or 100 */
5836 /* skip branch if C1=1 */
5838 x86_branch8 (code, X86_CC_P, 0, FALSE);
5839 /* branch if C0=0 */
5840 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5841 amd64_patch (br1, code);
5845 /* Branch if C013 != 001 */
5846 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5847 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5850 /* Transfer value to the fp stack */
5851 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5852 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5853 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5855 amd64_push_reg (code, AMD64_RAX);
5857 amd64_fnstsw (code);
5858 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5859 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5860 amd64_pop_reg (code, AMD64_RAX);
5861 amd64_fstp (code, 0);
5862 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "OverflowException");
5863 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5866 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5869 case OP_TLS_GET_REG:
5870 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5873 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5876 case OP_TLS_SET_REG: {
5877 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5880 case OP_MEMORY_BARRIER: {
5881 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5885 case OP_ATOMIC_ADD_I4:
5886 case OP_ATOMIC_ADD_I8: {
5887 int dreg = ins->dreg;
5888 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5890 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5893 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5894 amd64_prefix (code, X86_LOCK_PREFIX);
5895 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5896 /* dreg contains the old value, add with sreg2 value */
5897 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5899 if (ins->dreg != dreg)
5900 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5904 case OP_ATOMIC_EXCHANGE_I4:
5905 case OP_ATOMIC_EXCHANGE_I8: {
5906 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5908 /* LOCK prefix is implied. */
5909 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5910 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5911 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5914 case OP_ATOMIC_CAS_I4:
5915 case OP_ATOMIC_CAS_I8: {
5918 if (ins->opcode == OP_ATOMIC_CAS_I8)
5924 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5925 * an explanation of how this works.
5927 g_assert (ins->sreg3 == AMD64_RAX);
5928 g_assert (ins->sreg1 != AMD64_RAX);
5929 g_assert (ins->sreg1 != ins->sreg2);
5931 amd64_prefix (code, X86_LOCK_PREFIX);
5932 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
5934 if (ins->dreg != AMD64_RAX)
5935 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
5938 case OP_ATOMIC_LOAD_I1: {
5939 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
5942 case OP_ATOMIC_LOAD_U1: {
5943 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
5946 case OP_ATOMIC_LOAD_I2: {
5947 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
5950 case OP_ATOMIC_LOAD_U2: {
5951 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
5954 case OP_ATOMIC_LOAD_I4: {
5955 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5958 case OP_ATOMIC_LOAD_U4:
5959 case OP_ATOMIC_LOAD_I8:
5960 case OP_ATOMIC_LOAD_U8: {
5961 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
5964 case OP_ATOMIC_LOAD_R4: {
5965 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5966 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5969 case OP_ATOMIC_LOAD_R8: {
5970 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5973 case OP_ATOMIC_STORE_I1:
5974 case OP_ATOMIC_STORE_U1:
5975 case OP_ATOMIC_STORE_I2:
5976 case OP_ATOMIC_STORE_U2:
5977 case OP_ATOMIC_STORE_I4:
5978 case OP_ATOMIC_STORE_U4:
5979 case OP_ATOMIC_STORE_I8:
5980 case OP_ATOMIC_STORE_U8: {
5983 switch (ins->opcode) {
5984 case OP_ATOMIC_STORE_I1:
5985 case OP_ATOMIC_STORE_U1:
5988 case OP_ATOMIC_STORE_I2:
5989 case OP_ATOMIC_STORE_U2:
5992 case OP_ATOMIC_STORE_I4:
5993 case OP_ATOMIC_STORE_U4:
5996 case OP_ATOMIC_STORE_I8:
5997 case OP_ATOMIC_STORE_U8:
6002 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
6004 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6008 case OP_ATOMIC_STORE_R4: {
6009 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6010 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
6012 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6016 case OP_ATOMIC_STORE_R8: {
6019 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
6023 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
6027 case OP_CARD_TABLE_WBARRIER: {
6028 int ptr = ins->sreg1;
6029 int value = ins->sreg2;
6031 int nursery_shift, card_table_shift;
6032 gpointer card_table_mask;
6033 size_t nursery_size;
6035 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
6036 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
6037 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
6039 /*If either point to the stack we can simply avoid the WB. This happens due to
6040 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
6042 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
6046 * We need one register we can clobber, we choose EDX and make sreg1
6047 * fixed EAX to work around limitations in the local register allocator.
6048 * sreg2 might get allocated to EDX, but that is not a problem since
6049 * we use it before clobbering EDX.
6051 g_assert (ins->sreg1 == AMD64_RAX);
6054 * This is the code we produce:
6057 * edx >>= nursery_shift
6058 * cmp edx, (nursery_start >> nursery_shift)
6061 * edx >>= card_table_shift
6067 if (mono_gc_card_table_nursery_check ()) {
6068 if (value != AMD64_RDX)
6069 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
6070 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
6071 if (shifted_nursery_start >> 31) {
6073 * The value we need to compare against is 64 bits, so we need
6074 * another spare register. We use RBX, which we save and
6077 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
6078 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
6079 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
6080 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
6082 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
6084 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
6086 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
6087 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
6088 if (card_table_mask)
6089 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
6091 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
6092 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
6094 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
6096 if (mono_gc_card_table_nursery_check ())
6097 x86_patch (br, code);
6100 #ifdef MONO_ARCH_SIMD_INTRINSICS
6101 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
6103 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
6106 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
6109 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
6112 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
6115 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
6118 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
6121 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6122 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6125 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
6128 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
6131 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
6134 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
6137 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6140 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6143 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
6146 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6149 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
6152 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6155 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
6158 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
6161 case OP_PSHUFLEW_HIGH:
6162 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6163 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6165 case OP_PSHUFLEW_LOW:
6166 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6167 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6170 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6171 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6174 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6175 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6178 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
6179 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6183 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
6186 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
6189 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
6192 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
6195 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
6198 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
6201 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6202 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6205 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
6208 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
6211 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
6214 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6217 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6220 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6223 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6226 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6229 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6232 case OP_EXTRACT_MASK:
6233 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6237 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6240 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6243 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6247 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6250 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6253 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6256 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6260 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6263 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6266 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6269 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6273 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6276 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6279 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6283 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6286 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6289 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6293 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6296 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6300 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6303 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6306 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6310 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6313 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6316 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6320 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6323 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6326 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6329 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6333 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6336 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6339 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6342 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6345 case OP_PSUM_ABS_DIFF:
6346 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6349 case OP_UNPACK_LOWB:
6350 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6352 case OP_UNPACK_LOWW:
6353 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6355 case OP_UNPACK_LOWD:
6356 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6358 case OP_UNPACK_LOWQ:
6359 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6361 case OP_UNPACK_LOWPS:
6362 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6364 case OP_UNPACK_LOWPD:
6365 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6368 case OP_UNPACK_HIGHB:
6369 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6371 case OP_UNPACK_HIGHW:
6372 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6374 case OP_UNPACK_HIGHD:
6375 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6377 case OP_UNPACK_HIGHQ:
6378 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6380 case OP_UNPACK_HIGHPS:
6381 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6383 case OP_UNPACK_HIGHPD:
6384 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6388 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6391 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6394 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6397 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6400 case OP_PADDB_SAT_UN:
6401 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6403 case OP_PSUBB_SAT_UN:
6404 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6406 case OP_PADDW_SAT_UN:
6407 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6409 case OP_PSUBW_SAT_UN:
6410 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6414 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6417 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6420 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6423 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6427 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6430 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6433 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6435 case OP_PMULW_HIGH_UN:
6436 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6439 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6443 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6446 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6450 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6453 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6457 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6460 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6464 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6467 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6471 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6474 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6478 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6481 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6485 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6488 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6491 /*TODO: This is appart of the sse spec but not added
6493 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6496 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6501 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6504 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6507 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6510 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6513 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6516 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6519 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6522 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6525 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6528 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6532 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6535 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6539 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6540 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6542 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6547 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6549 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6550 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6554 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6556 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6557 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6558 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6562 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6564 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6567 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6569 case OP_EXTRACTX_U2:
6570 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6572 case OP_INSERTX_U1_SLOW:
6573 /*sreg1 is the extracted ireg (scratch)
6574 /sreg2 is the to be inserted ireg (scratch)
6575 /dreg is the xreg to receive the value*/
6577 /*clear the bits from the extracted word*/
6578 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6579 /*shift the value to insert if needed*/
6580 if (ins->inst_c0 & 1)
6581 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6582 /*join them together*/
6583 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6584 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6586 case OP_INSERTX_I4_SLOW:
6587 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6588 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6589 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6591 case OP_INSERTX_I8_SLOW:
6592 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6594 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6596 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6599 case OP_INSERTX_R4_SLOW:
6600 switch (ins->inst_c0) {
6603 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6605 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6608 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6610 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6612 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6613 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6616 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6618 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6620 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6621 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6624 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6626 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6628 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6629 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6633 case OP_INSERTX_R8_SLOW:
6635 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6637 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6639 case OP_STOREX_MEMBASE_REG:
6640 case OP_STOREX_MEMBASE:
6641 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6643 case OP_LOADX_MEMBASE:
6644 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6646 case OP_LOADX_ALIGNED_MEMBASE:
6647 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6649 case OP_STOREX_ALIGNED_MEMBASE_REG:
6650 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6652 case OP_STOREX_NTA_MEMBASE_REG:
6653 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6655 case OP_PREFETCH_MEMBASE:
6656 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6660 /*FIXME the peephole pass should have killed this*/
6661 if (ins->dreg != ins->sreg1)
6662 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6665 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6667 case OP_ICONV_TO_R4_RAW:
6668 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6671 case OP_FCONV_TO_R8_X:
6672 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6675 case OP_XCONV_R8_TO_I4:
6676 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6677 switch (ins->backend.source_opcode) {
6678 case OP_FCONV_TO_I1:
6679 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6681 case OP_FCONV_TO_U1:
6682 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6684 case OP_FCONV_TO_I2:
6685 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6687 case OP_FCONV_TO_U2:
6688 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6694 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6695 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6696 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6699 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6700 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6703 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6704 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6708 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6710 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6711 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6713 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6716 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6717 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6720 case OP_LIVERANGE_START: {
6721 if (cfg->verbose_level > 1)
6722 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6723 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6726 case OP_LIVERANGE_END: {
6727 if (cfg->verbose_level > 1)
6728 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6729 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6732 case OP_GC_SAFE_POINT: {
6733 const char *polling_func = NULL;
6734 int compare_val = 0;
6737 #if defined(__native_client_codegen__) && defined(__native_client_gc__)
6738 polling_func = "mono_nacl_gc";
6739 compare_val = 0xFFFFFFFF;
6741 g_assert (mono_threads_is_coop_enabled ());
6742 polling_func = "mono_threads_state_poll";
6746 amd64_test_membase_imm_size (code, ins->sreg1, 0, compare_val, 4);
6747 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6748 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func, FALSE);
6749 amd64_patch (br[0], code);
6753 case OP_GC_LIVENESS_DEF:
6754 case OP_GC_LIVENESS_USE:
6755 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6756 ins->backend.pc_offset = code - cfg->native_code;
6758 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6759 ins->backend.pc_offset = code - cfg->native_code;
6760 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6763 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6764 g_assert_not_reached ();
6767 if ((code - cfg->native_code - offset) > max_len) {
6768 #if !defined(__native_client_codegen__)
6769 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6770 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6771 g_assert_not_reached ();
6776 cfg->code_len = code - cfg->native_code;
6779 #endif /* DISABLE_JIT */
6782 mono_arch_register_lowlevel_calls (void)
6784 /* The signature doesn't matter */
6785 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6789 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
6791 unsigned char *ip = ji->ip.i + code;
6794 * Debug code to help track down problems where the target of a near call is
6797 if (amd64_is_near_call (ip)) {
6798 gint64 disp = (guint8*)target - (guint8*)ip;
6800 if (!amd64_is_imm32 (disp)) {
6801 printf ("TYPE: %d\n", ji->type);
6803 case MONO_PATCH_INFO_INTERNAL_METHOD:
6804 printf ("V: %s\n", ji->data.name);
6806 case MONO_PATCH_INFO_METHOD_JUMP:
6807 case MONO_PATCH_INFO_METHOD:
6808 printf ("V: %s\n", ji->data.method->name);
6816 amd64_patch (ip, (gpointer)target);
6822 get_max_epilog_size (MonoCompile *cfg)
6824 int max_epilog_size = 16;
6826 if (cfg->method->save_lmf)
6827 max_epilog_size += 256;
6829 if (mono_jit_trace_calls != NULL)
6830 max_epilog_size += 50;
6832 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6833 max_epilog_size += 50;
6835 max_epilog_size += (AMD64_NREG * 2);
6837 return max_epilog_size;
6841 * This macro is used for testing whenever the unwinder works correctly at every point
6842 * where an async exception can happen.
6844 /* This will generate a SIGSEGV at the given point in the code */
6845 #define async_exc_point(code) do { \
6846 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6847 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6848 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6849 cfg->arch.async_point_count ++; \
6854 mono_arch_emit_prolog (MonoCompile *cfg)
6856 MonoMethod *method = cfg->method;
6858 MonoMethodSignature *sig;
6860 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6863 MonoInst *lmf_var = cfg->lmf_var;
6864 gboolean args_clobbered = FALSE;
6865 gboolean trace = FALSE;
6866 #ifdef __native_client_codegen__
6867 guint alignment_check;
6870 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6872 #if defined(__default_codegen__)
6873 code = cfg->native_code = (unsigned char *)g_malloc (cfg->code_size);
6874 #elif defined(__native_client_codegen__)
6875 /* native_code_alloc is not 32-byte aligned, native_code is. */
6876 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6878 /* Align native_code to next nearest kNaclAlignment byte. */
6879 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6880 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6882 code = cfg->native_code;
6884 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6885 g_assert (alignment_check == 0);
6888 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6891 /* Amount of stack space allocated by register saving code */
6894 /* Offset between RSP and the CFA */
6898 * The prolog consists of the following parts:
6900 * - push rbp, mov rbp, rsp
6901 * - save callee saved regs using pushes
6903 * - save rgctx if needed
6904 * - save lmf if needed
6907 * - save rgctx if needed
6908 * - save lmf if needed
6909 * - save callee saved regs using moves
6914 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6915 // IP saved at CFA - 8
6916 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6917 async_exc_point (code);
6918 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6920 if (!cfg->arch.omit_fp) {
6921 amd64_push_reg (code, AMD64_RBP);
6923 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6924 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6925 async_exc_point (code);
6927 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6929 /* These are handled automatically by the stack marking code */
6930 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6932 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
6933 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
6934 async_exc_point (code);
6936 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6940 /* The param area is always at offset 0 from sp */
6941 /* This needs to be allocated here, since it has to come after the spill area */
6942 if (cfg->param_area) {
6943 if (cfg->arch.omit_fp)
6945 g_assert_not_reached ();
6946 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
6949 if (cfg->arch.omit_fp) {
6951 * On enter, the stack is misaligned by the pushing of the return
6952 * address. It is either made aligned by the pushing of %rbp, or by
6955 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
6956 if ((alloc_size % 16) == 0) {
6958 /* Mark the padding slot as NOREF */
6959 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
6962 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
6963 if (cfg->stack_offset != alloc_size) {
6964 /* Mark the padding slot as NOREF */
6965 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
6967 cfg->arch.sp_fp_offset = alloc_size;
6971 cfg->arch.stack_alloc_size = alloc_size;
6973 /* Allocate stack frame */
6975 /* See mono_emit_stack_alloc */
6976 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
6977 guint32 remaining_size = alloc_size;
6978 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
6979 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
6980 guint32 offset = code - cfg->native_code;
6981 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
6982 while (required_code_size >= (cfg->code_size - offset))
6983 cfg->code_size *= 2;
6984 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
6985 code = cfg->native_code + offset;
6986 cfg->stat_code_reallocs++;
6989 while (remaining_size >= 0x1000) {
6990 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
6991 if (cfg->arch.omit_fp) {
6992 cfa_offset += 0x1000;
6993 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6995 async_exc_point (code);
6997 if (cfg->arch.omit_fp)
6998 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
7001 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
7002 remaining_size -= 0x1000;
7004 if (remaining_size) {
7005 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
7006 if (cfg->arch.omit_fp) {
7007 cfa_offset += remaining_size;
7008 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7009 async_exc_point (code);
7012 if (cfg->arch.omit_fp)
7013 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
7017 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
7018 if (cfg->arch.omit_fp) {
7019 cfa_offset += alloc_size;
7020 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
7021 async_exc_point (code);
7026 /* Stack alignment check */
7031 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
7032 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
7033 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
7035 x86_branch8 (code, X86_CC_EQ, 1, FALSE);
7036 amd64_breakpoint (code);
7037 amd64_patch (buf, code);
7041 if (mini_get_debug_options ()->init_stacks) {
7042 /* Fill the stack frame with a dummy value to force deterministic behavior */
7044 /* Save registers to the red zone */
7045 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
7046 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
7048 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
7049 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
7050 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
7053 #if defined(__default_codegen__)
7054 amd64_prefix (code, X86_REP_PREFIX);
7056 #elif defined(__native_client_codegen__)
7057 /* NaCl stos pseudo-instruction */
7058 amd64_codegen_pre (code);
7059 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
7060 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
7061 /* Add %r15 to %rdi using lea, condition flags unaffected. */
7062 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
7063 amd64_prefix (code, X86_REP_PREFIX);
7065 amd64_codegen_post (code);
7066 #endif /* __native_client_codegen__ */
7068 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
7069 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
7073 if (method->save_lmf)
7074 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
7076 /* Save callee saved registers */
7077 if (cfg->arch.omit_fp) {
7078 save_area_offset = cfg->arch.reg_save_area_offset;
7079 /* Save caller saved registers after sp is adjusted */
7080 /* The registers are saved at the bottom of the frame */
7081 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
7083 /* The registers are saved just below the saved rbp */
7084 save_area_offset = cfg->arch.reg_save_area_offset;
7087 for (i = 0; i < AMD64_NREG; ++i) {
7088 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7089 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
7091 if (cfg->arch.omit_fp) {
7092 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
7093 /* These are handled automatically by the stack marking code */
7094 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
7096 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
7100 save_area_offset += 8;
7101 async_exc_point (code);
7105 /* store runtime generic context */
7106 if (cfg->rgctx_var) {
7107 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
7108 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
7110 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
7112 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
7113 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
7116 /* compute max_length in order to use short forward jumps */
7117 max_epilog_size = get_max_epilog_size (cfg);
7118 if (cfg->opt & MONO_OPT_BRANCH) {
7119 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7123 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
7125 /* max alignment for loops */
7126 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
7127 max_length += LOOP_ALIGNMENT;
7128 #ifdef __native_client_codegen__
7129 /* max alignment for native client */
7130 max_length += kNaClAlignment;
7133 MONO_BB_FOR_EACH_INS (bb, ins) {
7134 #ifdef __native_client_codegen__
7136 int space_in_block = kNaClAlignment -
7137 ((max_length + cfg->code_len) & kNaClAlignmentMask);
7138 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7139 if (space_in_block < max_len && max_len < kNaClAlignment) {
7140 max_length += space_in_block;
7143 #endif /*__native_client_codegen__*/
7144 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7147 /* Take prolog and epilog instrumentation into account */
7148 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
7149 max_length += max_epilog_size;
7151 bb->max_length = max_length;
7155 sig = mono_method_signature (method);
7158 cinfo = (CallInfo *)cfg->arch.cinfo;
7160 if (sig->ret->type != MONO_TYPE_VOID) {
7161 /* Save volatile arguments to the stack */
7162 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
7163 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
7166 /* Keep this in sync with emit_load_volatile_arguments */
7167 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
7168 ArgInfo *ainfo = cinfo->args + i;
7170 ins = cfg->args [i];
7172 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
7173 /* Unused arguments */
7176 /* Save volatile arguments to the stack */
7177 if (ins->opcode != OP_REGVAR) {
7178 switch (ainfo->storage) {
7184 if (stack_offset & 0x1)
7186 else if (stack_offset & 0x2)
7188 else if (stack_offset & 0x4)
7193 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
7196 * Save the original location of 'this',
7197 * get_generic_info_from_stack_frame () needs this to properly look up
7198 * the argument value during the handling of async exceptions.
7200 if (ins == cfg->args [0]) {
7201 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7202 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
7206 case ArgInFloatSSEReg:
7207 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7209 case ArgInDoubleSSEReg:
7210 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7212 case ArgValuetypeInReg:
7213 for (quad = 0; quad < 2; quad ++) {
7214 switch (ainfo->pair_storage [quad]) {
7216 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7218 case ArgInFloatSSEReg:
7219 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7221 case ArgInDoubleSSEReg:
7222 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7227 g_assert_not_reached ();
7231 case ArgValuetypeAddrInIReg:
7232 if (ainfo->pair_storage [0] == ArgInIReg)
7233 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7235 case ArgGSharedVtInReg:
7236 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, 8);
7242 /* Argument allocated to (non-volatile) register */
7243 switch (ainfo->storage) {
7245 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7248 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7251 g_assert_not_reached ();
7254 if (ins == cfg->args [0]) {
7255 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7256 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7261 if (cfg->method->save_lmf)
7262 args_clobbered = TRUE;
7265 args_clobbered = TRUE;
7266 code = (guint8 *)mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7269 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7270 args_clobbered = TRUE;
7273 * Optimize the common case of the first bblock making a call with the same
7274 * arguments as the method. This works because the arguments are still in their
7275 * original argument registers.
7276 * FIXME: Generalize this
7278 if (!args_clobbered) {
7279 MonoBasicBlock *first_bb = cfg->bb_entry;
7281 int filter = FILTER_IL_SEQ_POINT;
7283 next = mono_bb_first_inst (first_bb, filter);
7284 if (!next && first_bb->next_bb) {
7285 first_bb = first_bb->next_bb;
7286 next = mono_bb_first_inst (first_bb, filter);
7289 if (first_bb->in_count > 1)
7292 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7293 ArgInfo *ainfo = cinfo->args + i;
7294 gboolean match = FALSE;
7296 ins = cfg->args [i];
7297 if (ins->opcode != OP_REGVAR) {
7298 switch (ainfo->storage) {
7300 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7301 if (next->dreg == ainfo->reg) {
7305 next->opcode = OP_MOVE;
7306 next->sreg1 = ainfo->reg;
7307 /* Only continue if the instruction doesn't change argument regs */
7308 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7318 /* Argument allocated to (non-volatile) register */
7319 switch (ainfo->storage) {
7321 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7332 next = mono_inst_next (next, filter);
7333 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7340 if (cfg->gen_sdb_seq_points) {
7341 MonoInst *info_var = (MonoInst *)cfg->arch.seq_point_info_var;
7343 /* Initialize seq_point_info_var */
7344 if (cfg->compile_aot) {
7345 /* Initialize the variable from a GOT slot */
7346 /* Same as OP_AOTCONST */
7347 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7348 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7349 g_assert (info_var->opcode == OP_REGOFFSET);
7350 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7353 if (cfg->compile_aot) {
7354 /* Initialize ss_tramp_var */
7355 ins = (MonoInst *)cfg->arch.ss_tramp_var;
7356 g_assert (ins->opcode == OP_REGOFFSET);
7358 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7359 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr), 8);
7360 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7362 /* Initialize ss_tramp_var */
7363 ins = (MonoInst *)cfg->arch.ss_tramp_var;
7364 g_assert (ins->opcode == OP_REGOFFSET);
7366 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&ss_trampoline);
7367 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7369 /* Initialize bp_tramp_var */
7370 ins = (MonoInst *)cfg->arch.bp_tramp_var;
7371 g_assert (ins->opcode == OP_REGOFFSET);
7373 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&bp_trampoline);
7374 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7378 cfg->code_len = code - cfg->native_code;
7380 g_assert (cfg->code_len < cfg->code_size);
7386 mono_arch_emit_epilog (MonoCompile *cfg)
7388 MonoMethod *method = cfg->method;
7391 int max_epilog_size;
7393 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7394 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7396 max_epilog_size = get_max_epilog_size (cfg);
7398 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7399 cfg->code_size *= 2;
7400 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7401 cfg->stat_code_reallocs++;
7403 code = cfg->native_code + cfg->code_len;
7405 cfg->has_unwind_info_for_epilog = TRUE;
7407 /* Mark the start of the epilog */
7408 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7410 /* Save the uwind state which is needed by the out-of-line code */
7411 mono_emit_unwind_op_remember_state (cfg, code);
7413 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7414 code = (guint8 *)mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7416 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7418 if (method->save_lmf) {
7419 /* check if we need to restore protection of the stack after a stack overflow */
7420 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7422 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7423 /* we load the value in a separate instruction: this mechanism may be
7424 * used later as a safer way to do thread interruption
7426 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7427 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7429 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7430 /* note that the call trampoline will preserve eax/edx */
7431 x86_call_reg (code, X86_ECX);
7432 x86_patch (patch, code);
7434 /* FIXME: maybe save the jit tls in the prolog */
7436 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7437 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7441 /* Restore callee saved regs */
7442 for (i = 0; i < AMD64_NREG; ++i) {
7443 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7444 /* Restore only used_int_regs, not arch.saved_iregs */
7445 if (cfg->used_int_regs & (1 << i)) {
7446 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7447 mono_emit_unwind_op_same_value (cfg, code, i);
7448 async_exc_point (code);
7450 save_area_offset += 8;
7454 /* Load returned vtypes into registers if needed */
7455 cinfo = (CallInfo *)cfg->arch.cinfo;
7456 if (cinfo->ret.storage == ArgValuetypeInReg) {
7457 ArgInfo *ainfo = &cinfo->ret;
7458 MonoInst *inst = cfg->ret;
7460 for (quad = 0; quad < 2; quad ++) {
7461 switch (ainfo->pair_storage [quad]) {
7463 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7465 case ArgInFloatSSEReg:
7466 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7468 case ArgInDoubleSSEReg:
7469 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7474 g_assert_not_reached ();
7479 if (cfg->arch.omit_fp) {
7480 if (cfg->arch.stack_alloc_size) {
7481 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7485 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7487 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7488 async_exc_point (code);
7491 /* Restore the unwind state to be the same as before the epilog */
7492 mono_emit_unwind_op_restore_state (cfg, code);
7494 cfg->code_len = code - cfg->native_code;
7496 g_assert (cfg->code_len < cfg->code_size);
7500 mono_arch_emit_exceptions (MonoCompile *cfg)
7502 MonoJumpInfo *patch_info;
7505 MonoClass *exc_classes [16];
7506 guint8 *exc_throw_start [16], *exc_throw_end [16];
7507 guint32 code_size = 0;
7509 /* Compute needed space */
7510 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7511 if (patch_info->type == MONO_PATCH_INFO_EXC)
7513 if (patch_info->type == MONO_PATCH_INFO_R8)
7514 code_size += 8 + 15; /* sizeof (double) + alignment */
7515 if (patch_info->type == MONO_PATCH_INFO_R4)
7516 code_size += 4 + 15; /* sizeof (float) + alignment */
7517 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7518 code_size += 8 + 7; /*sizeof (void*) + alignment */
7521 #ifdef __native_client_codegen__
7522 /* Give us extra room on Native Client. This could be */
7523 /* more carefully calculated, but bundle alignment makes */
7524 /* it much trickier, so *2 like other places is good. */
7528 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7529 cfg->code_size *= 2;
7530 cfg->native_code = (unsigned char *)mono_realloc_native_code (cfg);
7531 cfg->stat_code_reallocs++;
7534 code = cfg->native_code + cfg->code_len;
7536 /* add code to raise exceptions */
7538 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7539 switch (patch_info->type) {
7540 case MONO_PATCH_INFO_EXC: {
7541 MonoClass *exc_class;
7545 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7547 exc_class = mono_class_load_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7548 throw_ip = patch_info->ip.i;
7550 //x86_breakpoint (code);
7551 /* Find a throw sequence for the same exception class */
7552 for (i = 0; i < nthrows; ++i)
7553 if (exc_classes [i] == exc_class)
7556 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7557 x86_jump_code (code, exc_throw_start [i]);
7558 patch_info->type = MONO_PATCH_INFO_NONE;
7562 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7566 exc_classes [nthrows] = exc_class;
7567 exc_throw_start [nthrows] = code;
7569 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7571 patch_info->type = MONO_PATCH_INFO_NONE;
7573 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7575 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7580 exc_throw_end [nthrows] = code;
7590 g_assert(code < cfg->native_code + cfg->code_size);
7593 /* Handle relocations with RIP relative addressing */
7594 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7595 gboolean remove = FALSE;
7596 guint8 *orig_code = code;
7598 switch (patch_info->type) {
7599 case MONO_PATCH_INFO_R8:
7600 case MONO_PATCH_INFO_R4: {
7601 guint8 *pos, *patch_pos;
7604 /* The SSE opcodes require a 16 byte alignment */
7605 #if defined(__default_codegen__)
7606 code = (guint8*)ALIGN_TO (code, 16);
7607 #elif defined(__native_client_codegen__)
7609 /* Pad this out with HLT instructions */
7610 /* or we can get garbage bytes emitted */
7611 /* which will fail validation */
7612 guint8 *aligned_code;
7613 /* extra align to make room for */
7614 /* mov/push below */
7615 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7616 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7617 /* The technique of hiding data in an */
7618 /* instruction has a problem here: we */
7619 /* need the data aligned to a 16-byte */
7620 /* boundary but the instruction cannot */
7621 /* cross the bundle boundary. so only */
7622 /* odd multiples of 16 can be used */
7623 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7626 while (code < aligned_code) {
7627 *(code++) = 0xf4; /* hlt */
7632 pos = cfg->native_code + patch_info->ip.i;
7633 if (IS_REX (pos [1])) {
7634 patch_pos = pos + 5;
7635 target_pos = code - pos - 9;
7638 patch_pos = pos + 4;
7639 target_pos = code - pos - 8;
7642 if (patch_info->type == MONO_PATCH_INFO_R8) {
7643 #ifdef __native_client_codegen__
7644 /* Hide 64-bit data in a */
7645 /* "mov imm64, r11" instruction. */
7646 /* write it before the start of */
7648 *(code-2) = 0x49; /* prefix */
7649 *(code-1) = 0xbb; /* mov X, %r11 */
7651 *(double*)code = *(double*)patch_info->data.target;
7652 code += sizeof (double);
7654 #ifdef __native_client_codegen__
7655 /* Hide 32-bit data in a */
7656 /* "push imm32" instruction. */
7657 *(code-1) = 0x68; /* push */
7659 *(float*)code = *(float*)patch_info->data.target;
7660 code += sizeof (float);
7663 *(guint32*)(patch_pos) = target_pos;
7668 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7671 if (cfg->compile_aot)
7674 /*loading is faster against aligned addresses.*/
7675 code = (guint8*)ALIGN_TO (code, 8);
7676 memset (orig_code, 0, code - orig_code);
7678 pos = cfg->native_code + patch_info->ip.i;
7680 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7681 if (IS_REX (pos [1]))
7682 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7684 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7686 *(gpointer*)code = (gpointer)patch_info->data.target;
7687 code += sizeof (gpointer);
7697 if (patch_info == cfg->patch_info)
7698 cfg->patch_info = patch_info->next;
7702 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7704 tmp->next = patch_info->next;
7707 g_assert (code < cfg->native_code + cfg->code_size);
7710 cfg->code_len = code - cfg->native_code;
7712 g_assert (cfg->code_len < cfg->code_size);
7716 #endif /* DISABLE_JIT */
7719 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7721 guchar *code = (guchar *)p;
7722 MonoMethodSignature *sig;
7724 int i, n, stack_area = 0;
7726 /* Keep this in sync with mono_arch_get_argument_info */
7728 if (enable_arguments) {
7729 /* Allocate a new area on the stack and save arguments there */
7730 sig = mono_method_signature (cfg->method);
7732 n = sig->param_count + sig->hasthis;
7734 stack_area = ALIGN_TO (n * 8, 16);
7736 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7738 for (i = 0; i < n; ++i) {
7739 inst = cfg->args [i];
7741 if (inst->opcode == OP_REGVAR)
7742 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7744 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7745 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7750 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7751 amd64_set_reg_template (code, AMD64_ARG_REG1);
7752 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7753 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7755 if (enable_arguments)
7756 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7770 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7772 guchar *code = (guchar *)p;
7773 int save_mode = SAVE_NONE;
7774 MonoMethod *method = cfg->method;
7775 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
7778 switch (ret_type->type) {
7779 case MONO_TYPE_VOID:
7780 /* special case string .ctor icall */
7781 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7782 save_mode = SAVE_EAX;
7784 save_mode = SAVE_NONE;
7788 save_mode = SAVE_EAX;
7792 save_mode = SAVE_XMM;
7794 case MONO_TYPE_GENERICINST:
7795 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7796 save_mode = SAVE_EAX;
7800 case MONO_TYPE_VALUETYPE:
7801 save_mode = SAVE_STRUCT;
7804 save_mode = SAVE_EAX;
7808 /* Save the result and copy it into the proper argument register */
7809 switch (save_mode) {
7811 amd64_push_reg (code, AMD64_RAX);
7813 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7814 if (enable_arguments)
7815 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7819 if (enable_arguments)
7820 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7823 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7824 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7826 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7828 * The result is already in the proper argument register so no copying
7835 g_assert_not_reached ();
7838 /* Set %al since this is a varargs call */
7839 if (save_mode == SAVE_XMM)
7840 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7842 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7844 if (preserve_argument_registers) {
7845 for (i = 0; i < PARAM_REGS; ++i)
7846 amd64_push_reg (code, param_regs [i]);
7849 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7850 amd64_set_reg_template (code, AMD64_ARG_REG1);
7851 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7853 if (preserve_argument_registers) {
7854 for (i = PARAM_REGS - 1; i >= 0; --i)
7855 amd64_pop_reg (code, param_regs [i]);
7858 /* Restore result */
7859 switch (save_mode) {
7861 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7862 amd64_pop_reg (code, AMD64_RAX);
7868 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7869 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7870 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7875 g_assert_not_reached ();
7882 mono_arch_flush_icache (guint8 *code, gint size)
7888 mono_arch_flush_register_windows (void)
7893 mono_arch_is_inst_imm (gint64 imm)
7895 return amd64_use_imm32 (imm);
7899 * Determine whenever the trap whose info is in SIGINFO is caused by
7903 mono_arch_is_int_overflow (void *sigctx, void *info)
7910 mono_sigctx_to_monoctx (sigctx, &ctx);
7912 rip = (guint8*)ctx.gregs [AMD64_RIP];
7914 if (IS_REX (rip [0])) {
7915 reg = amd64_rex_b (rip [0]);
7921 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7923 reg += x86_modrm_rm (rip [1]);
7925 value = ctx.gregs [reg];
7935 mono_arch_get_patch_offset (guint8 *code)
7941 * mono_breakpoint_clean_code:
7943 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
7944 * breakpoints in the original code, they are removed in the copy.
7946 * Returns TRUE if no sw breakpoint was present.
7949 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
7952 * If method_start is non-NULL we need to perform bound checks, since we access memory
7953 * at code - offset we could go before the start of the method and end up in a different
7954 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
7957 if (!method_start || code - offset >= method_start) {
7958 memcpy (buf, code - offset, size);
7960 int diff = code - method_start;
7961 memset (buf, 0, size);
7962 memcpy (buf + offset - diff, method_start, diff + size - offset);
7967 #if defined(__native_client_codegen__)
7968 /* For membase calls, we want the base register. for Native Client, */
7969 /* all indirect calls have the following sequence with the given sizes: */
7970 /* mov %eXX,%eXX [2-3] */
7971 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
7972 /* and $0xffffffffffffffe0,%r11d [4] */
7973 /* add %r15,%r11 [3] */
7974 /* callq *%r11 [3] */
7977 /* Determine if code points to a NaCl call-through-register sequence, */
7978 /* (i.e., the last 3 instructions listed above) */
7980 is_nacl_call_reg_sequence(guint8* code)
7982 const char *sequence = "\x41\x83\xe3\xe0" /* and */
7983 "\x4d\x03\xdf" /* add */
7984 "\x41\xff\xd3"; /* call */
7985 return memcmp(code, sequence, 10) == 0;
7988 /* Determine if code points to the first opcode of the mov membase component */
7989 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
7990 /* (there could be a REX prefix before the opcode but it is ignored) */
7992 is_nacl_indirect_call_membase_sequence(guint8* code)
7994 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
7995 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
7996 /* and that src reg = dest reg */
7997 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
7998 /* Check that next inst is mov, uses SIB byte (rm = 4), */
8000 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
8001 /* and has dst of r11 and base of r15 */
8002 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
8003 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
8005 #endif /* __native_client_codegen__ */
8008 mono_arch_get_this_arg_reg (guint8 *code)
8010 return AMD64_ARG_REG1;
8014 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
8016 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
8019 #define MAX_ARCH_DELEGATE_PARAMS 10
8022 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, guint32 param_count)
8024 guint8 *code, *start;
8025 GSList *unwind_ops = NULL;
8028 unwind_ops = mono_arch_get_cie_program ();
8031 start = code = (guint8 *)mono_global_codeman_reserve (64);
8033 /* Replace the this argument with the target */
8034 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8035 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8036 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8038 g_assert ((code - start) < 64);
8040 start = code = (guint8 *)mono_global_codeman_reserve (64);
8042 if (param_count == 0) {
8043 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8045 /* We have to shift the arguments left */
8046 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8047 for (i = 0; i < param_count; ++i) {
8050 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8052 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
8054 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
8058 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
8060 g_assert ((code - start) < 64);
8063 nacl_global_codeman_validate (&start, 64, &code);
8064 mono_arch_flush_icache (start, code - start);
8067 *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
8069 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
8070 *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
8074 if (mono_jit_map_is_enabled ()) {
8077 buff = (char*)"delegate_invoke_has_target";
8079 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
8080 mono_emit_jit_tramp (start, code - start, buff);
8084 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8089 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
8092 get_delegate_virtual_invoke_impl (MonoTrampInfo **info, gboolean load_imt_reg, int offset)
8094 guint8 *code, *start;
8099 if (offset / (int)sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
8102 start = code = (guint8 *)mono_global_codeman_reserve (size);
8104 unwind_ops = mono_arch_get_cie_program ();
8106 /* Replace the this argument with the target */
8107 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8108 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8111 /* Load the IMT reg */
8112 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
8115 /* Load the vtable */
8116 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
8117 amd64_jump_membase (code, AMD64_RAX, offset);
8118 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8121 tramp_name = g_strdup_printf ("delegate_virtual_invoke_imt_%d", - offset / sizeof (gpointer));
8123 tramp_name = g_strdup_printf ("delegate_virtual_invoke_%d", offset / sizeof (gpointer));
8124 *info = mono_tramp_info_create (tramp_name, start, code - start, NULL, unwind_ops);
8125 g_free (tramp_name);
8131 * mono_arch_get_delegate_invoke_impls:
8133 * Return a list of MonoTrampInfo structures for the delegate invoke impl
8137 mono_arch_get_delegate_invoke_impls (void)
8140 MonoTrampInfo *info;
8143 get_delegate_invoke_impl (&info, TRUE, 0);
8144 res = g_slist_prepend (res, info);
8146 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
8147 get_delegate_invoke_impl (&info, FALSE, i);
8148 res = g_slist_prepend (res, info);
8151 for (i = 0; i <= MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
8152 get_delegate_virtual_invoke_impl (&info, TRUE, - i * SIZEOF_VOID_P);
8153 res = g_slist_prepend (res, info);
8155 get_delegate_virtual_invoke_impl (&info, FALSE, i * SIZEOF_VOID_P);
8156 res = g_slist_prepend (res, info);
8163 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
8165 guint8 *code, *start;
8168 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
8171 /* FIXME: Support more cases */
8172 if (MONO_TYPE_ISSTRUCT (mini_get_underlying_type (sig->ret)))
8176 static guint8* cached = NULL;
8181 if (mono_aot_only) {
8182 start = (guint8 *)mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
8184 MonoTrampInfo *info;
8185 start = (guint8 *)get_delegate_invoke_impl (&info, TRUE, 0);
8186 mono_tramp_info_register (info, NULL);
8189 mono_memory_barrier ();
8193 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
8194 for (i = 0; i < sig->param_count; ++i)
8195 if (!mono_is_regsize_var (sig->params [i]))
8197 if (sig->param_count > 4)
8200 code = cache [sig->param_count];
8204 if (mono_aot_only) {
8205 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
8206 start = (guint8 *)mono_aot_get_trampoline (name);
8209 MonoTrampInfo *info;
8210 start = (guint8 *)get_delegate_invoke_impl (&info, FALSE, sig->param_count);
8211 mono_tramp_info_register (info, NULL);
8214 mono_memory_barrier ();
8216 cache [sig->param_count] = start;
8223 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
8225 MonoTrampInfo *info;
8228 code = get_delegate_virtual_invoke_impl (&info, load_imt_reg, offset);
8230 mono_tramp_info_register (info, NULL);
8235 mono_arch_finish_init (void)
8237 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
8238 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
8243 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8247 #if defined(__default_codegen__)
8248 #define CMP_SIZE (6 + 1)
8249 #define CMP_REG_REG_SIZE (4 + 1)
8250 #define BR_SMALL_SIZE 2
8251 #define BR_LARGE_SIZE 6
8252 #define MOV_REG_IMM_SIZE 10
8253 #define MOV_REG_IMM_32BIT_SIZE 6
8254 #define JUMP_REG_SIZE (2 + 1)
8255 #elif defined(__native_client_codegen__)
8256 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8257 #define CMP_SIZE ((6 + 1) * 2 - 1)
8258 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8259 #define BR_SMALL_SIZE (2 * 2 - 1)
8260 #define BR_LARGE_SIZE (6 * 2 - 1)
8261 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8262 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8263 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8264 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8265 /* Jump membase's size is large and unpredictable */
8266 /* in native client, just pad it out a whole bundle. */
8267 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8271 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8273 int i, distance = 0;
8274 for (i = start; i < target; ++i)
8275 distance += imt_entries [i]->chunk_size;
8280 * LOCKING: called with the domain lock held
8283 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8284 gpointer fail_tramp)
8288 guint8 *code, *start;
8289 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8292 for (i = 0; i < count; ++i) {
8293 MonoIMTCheckItem *item = imt_entries [i];
8294 if (item->is_equals) {
8295 if (item->check_target_idx) {
8296 if (!item->compare_done) {
8297 if (amd64_use_imm32 ((gint64)item->key))
8298 item->chunk_size += CMP_SIZE;
8300 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8302 if (item->has_target_code) {
8303 item->chunk_size += MOV_REG_IMM_SIZE;
8305 if (vtable_is_32bit)
8306 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8308 item->chunk_size += MOV_REG_IMM_SIZE;
8309 #ifdef __native_client_codegen__
8310 item->chunk_size += JUMP_MEMBASE_SIZE;
8313 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8316 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8317 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8319 if (vtable_is_32bit)
8320 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8322 item->chunk_size += MOV_REG_IMM_SIZE;
8323 item->chunk_size += JUMP_REG_SIZE;
8324 /* with assert below:
8325 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8327 #ifdef __native_client_codegen__
8328 item->chunk_size += JUMP_MEMBASE_SIZE;
8333 if (amd64_use_imm32 ((gint64)item->key))
8334 item->chunk_size += CMP_SIZE;
8336 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8337 item->chunk_size += BR_LARGE_SIZE;
8338 imt_entries [item->check_target_idx]->compare_done = TRUE;
8340 size += item->chunk_size;
8342 #if defined(__native_client__) && defined(__native_client_codegen__)
8343 /* In Native Client, we don't re-use thunks, allocate from the */
8344 /* normal code manager paths. */
8345 code = mono_domain_code_reserve (domain, size);
8348 code = (guint8 *)mono_method_alloc_generic_virtual_thunk (domain, size);
8350 code = (guint8 *)mono_domain_code_reserve (domain, size);
8354 unwind_ops = mono_arch_get_cie_program ();
8356 for (i = 0; i < count; ++i) {
8357 MonoIMTCheckItem *item = imt_entries [i];
8358 item->code_target = code;
8359 if (item->is_equals) {
8360 gboolean fail_case = !item->check_target_idx && fail_tramp;
8362 if (item->check_target_idx || fail_case) {
8363 if (!item->compare_done || fail_case) {
8364 if (amd64_use_imm32 ((gint64)item->key))
8365 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8367 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof(gpointer));
8368 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8371 item->jmp_code = code;
8372 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8373 if (item->has_target_code) {
8374 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8375 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8377 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8378 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8382 amd64_patch (item->jmp_code, code);
8383 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8384 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8385 item->jmp_code = NULL;
8388 /* enable the commented code to assert on wrong method */
8390 if (amd64_is_imm32 (item->key))
8391 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8393 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8394 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8396 item->jmp_code = code;
8397 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8398 /* See the comment below about R10 */
8399 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8400 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8401 amd64_patch (item->jmp_code, code);
8402 amd64_breakpoint (code);
8403 item->jmp_code = NULL;
8405 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8406 needs to be preserved. R10 needs
8407 to be preserved for calls which
8408 require a runtime generic context,
8409 but interface calls don't. */
8410 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8411 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8415 if (amd64_use_imm32 ((gint64)item->key))
8416 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8418 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof (gpointer));
8419 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8421 item->jmp_code = code;
8422 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8423 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8425 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8427 g_assert (code - item->code_target <= item->chunk_size);
8429 /* patch the branches to get to the target items */
8430 for (i = 0; i < count; ++i) {
8431 MonoIMTCheckItem *item = imt_entries [i];
8432 if (item->jmp_code) {
8433 if (item->check_target_idx) {
8434 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8440 mono_stats.imt_thunks_size += code - start;
8441 g_assert (code - start <= size);
8443 nacl_domain_code_validate(domain, &start, size, &code);
8444 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8446 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
8452 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8454 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8458 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8460 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8464 mono_arch_get_cie_program (void)
8468 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8469 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8477 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8479 MonoInst *ins = NULL;
8482 if (cmethod->klass == mono_defaults.math_class) {
8483 if (strcmp (cmethod->name, "Sin") == 0) {
8485 } else if (strcmp (cmethod->name, "Cos") == 0) {
8487 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8489 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8493 if (opcode && fsig->param_count == 1) {
8494 MONO_INST_NEW (cfg, ins, opcode);
8495 ins->type = STACK_R8;
8496 ins->dreg = mono_alloc_freg (cfg);
8497 ins->sreg1 = args [0]->dreg;
8498 MONO_ADD_INS (cfg->cbb, ins);
8502 if (cfg->opt & MONO_OPT_CMOV) {
8503 if (strcmp (cmethod->name, "Min") == 0) {
8504 if (fsig->params [0]->type == MONO_TYPE_I4)
8506 if (fsig->params [0]->type == MONO_TYPE_U4)
8507 opcode = OP_IMIN_UN;
8508 else if (fsig->params [0]->type == MONO_TYPE_I8)
8510 else if (fsig->params [0]->type == MONO_TYPE_U8)
8511 opcode = OP_LMIN_UN;
8512 } else if (strcmp (cmethod->name, "Max") == 0) {
8513 if (fsig->params [0]->type == MONO_TYPE_I4)
8515 if (fsig->params [0]->type == MONO_TYPE_U4)
8516 opcode = OP_IMAX_UN;
8517 else if (fsig->params [0]->type == MONO_TYPE_I8)
8519 else if (fsig->params [0]->type == MONO_TYPE_U8)
8520 opcode = OP_LMAX_UN;
8524 if (opcode && fsig->param_count == 2) {
8525 MONO_INST_NEW (cfg, ins, opcode);
8526 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8527 ins->dreg = mono_alloc_ireg (cfg);
8528 ins->sreg1 = args [0]->dreg;
8529 ins->sreg2 = args [1]->dreg;
8530 MONO_ADD_INS (cfg->cbb, ins);
8534 /* OP_FREM is not IEEE compatible */
8535 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8536 MONO_INST_NEW (cfg, ins, OP_FREM);
8537 ins->inst_i0 = args [0];
8538 ins->inst_i1 = args [1];
8548 mono_arch_print_tree (MonoInst *tree, int arity)
8554 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8556 return ctx->gregs [reg];
8560 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8562 ctx->gregs [reg] = val;
8566 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8568 gpointer *sp, old_value;
8572 bp = (char *)MONO_CONTEXT_GET_BP (ctx);
8573 sp = (gpointer *)*(gpointer*)(bp + clause->exvar_offset);
8576 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8585 * mono_arch_emit_load_aotconst:
8587 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8588 * TARGET from the mscorlib GOT in full-aot code.
8589 * On AMD64, the result is placed into R11.
8592 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, MonoJumpInfoType tramp_type, gconstpointer target)
8594 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8595 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8601 * mono_arch_get_trampolines:
8603 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8607 mono_arch_get_trampolines (gboolean aot)
8609 return mono_amd64_get_exception_trampolines (aot);
8612 /* Soft Debug support */
8613 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8616 * mono_arch_set_breakpoint:
8618 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8619 * The location should contain code emitted by OP_SEQ_POINT.
8622 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8627 guint32 native_offset = ip - (guint8*)ji->code_start;
8628 SeqPointInfo *info = (SeqPointInfo *)mono_arch_get_seq_point_info (mono_domain_get (), (guint8 *)ji->code_start);
8630 g_assert (info->bp_addrs [native_offset] == 0);
8631 info->bp_addrs [native_offset] = mini_get_breakpoint_trampoline ();
8633 /* ip points to a mov r11, 0 */
8634 g_assert (code [0] == 0x41);
8635 g_assert (code [1] == 0xbb);
8636 amd64_mov_reg_imm (code, AMD64_R11, 1);
8641 * mono_arch_clear_breakpoint:
8643 * Clear the breakpoint at IP.
8646 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8651 guint32 native_offset = ip - (guint8*)ji->code_start;
8652 SeqPointInfo *info = (SeqPointInfo *)mono_arch_get_seq_point_info (mono_domain_get (), (guint8 *)ji->code_start);
8654 info->bp_addrs [native_offset] = NULL;
8656 amd64_mov_reg_imm (code, AMD64_R11, 0);
8661 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8663 /* We use soft breakpoints on amd64 */
8668 * mono_arch_skip_breakpoint:
8670 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8671 * we resume, the instruction is not executed again.
8674 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8676 g_assert_not_reached ();
8680 * mono_arch_start_single_stepping:
8682 * Start single stepping.
8685 mono_arch_start_single_stepping (void)
8687 ss_trampoline = mini_get_single_step_trampoline ();
8691 * mono_arch_stop_single_stepping:
8693 * Stop single stepping.
8696 mono_arch_stop_single_stepping (void)
8698 ss_trampoline = NULL;
8702 * mono_arch_is_single_step_event:
8704 * Return whenever the machine state in SIGCTX corresponds to a single
8708 mono_arch_is_single_step_event (void *info, void *sigctx)
8710 /* We use soft breakpoints on amd64 */
8715 * mono_arch_skip_single_step:
8717 * Modify CTX so the ip is placed after the single step trigger instruction,
8718 * we resume, the instruction is not executed again.
8721 mono_arch_skip_single_step (MonoContext *ctx)
8723 g_assert_not_reached ();
8727 * mono_arch_create_seq_point_info:
8729 * Return a pointer to a data structure which is used by the sequence
8730 * point implementation in AOTed code.
8733 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8738 // FIXME: Add a free function
8740 mono_domain_lock (domain);
8741 info = (SeqPointInfo *)g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8743 mono_domain_unlock (domain);
8746 ji = mono_jit_info_table_find (domain, (char*)code);
8749 // FIXME: Optimize the size
8750 info = (SeqPointInfo *)g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8752 info->ss_tramp_addr = &ss_trampoline;
8754 mono_domain_lock (domain);
8755 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8757 mono_domain_unlock (domain);
8764 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8766 ext->lmf.previous_lmf = prev_lmf;
8767 /* Mark that this is a MonoLMFExt */
8768 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8769 ext->lmf.rsp = (gssize)ext;
8775 mono_arch_opcode_supported (int opcode)
8778 case OP_ATOMIC_ADD_I4:
8779 case OP_ATOMIC_ADD_I8:
8780 case OP_ATOMIC_EXCHANGE_I4:
8781 case OP_ATOMIC_EXCHANGE_I8:
8782 case OP_ATOMIC_CAS_I4:
8783 case OP_ATOMIC_CAS_I8:
8784 case OP_ATOMIC_LOAD_I1:
8785 case OP_ATOMIC_LOAD_I2:
8786 case OP_ATOMIC_LOAD_I4:
8787 case OP_ATOMIC_LOAD_I8:
8788 case OP_ATOMIC_LOAD_U1:
8789 case OP_ATOMIC_LOAD_U2:
8790 case OP_ATOMIC_LOAD_U4:
8791 case OP_ATOMIC_LOAD_U8:
8792 case OP_ATOMIC_LOAD_R4:
8793 case OP_ATOMIC_LOAD_R8:
8794 case OP_ATOMIC_STORE_I1:
8795 case OP_ATOMIC_STORE_I2:
8796 case OP_ATOMIC_STORE_I4:
8797 case OP_ATOMIC_STORE_I8:
8798 case OP_ATOMIC_STORE_U1:
8799 case OP_ATOMIC_STORE_U2:
8800 case OP_ATOMIC_STORE_U4:
8801 case OP_ATOMIC_STORE_U8:
8802 case OP_ATOMIC_STORE_R4:
8803 case OP_ATOMIC_STORE_R8:
8811 mono_arch_get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
8813 return get_call_info (mp, sig);