2 * mini-amd64.c: AMD64 backend for the Mono code generator
7 * Paolo Molaro (lupus@ximian.com)
8 * Dietmar Maurer (dietmar@ximian.com)
10 * Zoltan Varga (vargaz@gmail.com)
12 * (C) 2003 Ximian, Inc.
13 * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
14 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
23 #include <mono/metadata/abi-details.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/debug-helpers.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/profiler-private.h>
28 #include <mono/metadata/mono-debug.h>
29 #include <mono/metadata/gc-internal.h>
30 #include <mono/utils/mono-math.h>
31 #include <mono/utils/mono-mmap.h>
32 #include <mono/utils/mono-memory-model.h>
33 #include <mono/utils/mono-tls.h>
34 #include <mono/utils/mono-hwcap-x86.h>
35 #include <mono/utils/mono-threads.h>
39 #include "mini-amd64.h"
40 #include "cpu-amd64.h"
41 #include "debugger-agent.h"
45 static gboolean optimize_for_xen = TRUE;
47 #define optimize_for_xen 0
50 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
52 #define IS_IMM32(val) ((((guint64)val) >> 32) == 0)
54 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
57 /* Under windows, the calling convention is never stdcall */
58 #define CALLCONV_IS_STDCALL(call_conv) (FALSE)
60 #define CALLCONV_IS_STDCALL(call_conv) ((call_conv) == MONO_CALL_STDCALL)
63 /* This mutex protects architecture specific caches */
64 #define mono_mini_arch_lock() mono_mutex_lock (&mini_arch_mutex)
65 #define mono_mini_arch_unlock() mono_mutex_unlock (&mini_arch_mutex)
66 static mono_mutex_t mini_arch_mutex;
69 * The code generated for sequence points reads from this location, which is
70 * made read-only when single stepping is enabled.
72 static gpointer ss_trigger_page;
74 /* Enabled breakpoints read from this trigger page */
75 static gpointer bp_trigger_page;
77 /* The size of the breakpoint sequence */
78 static int breakpoint_size;
80 /* The size of the breakpoint instruction causing the actual fault */
81 static int breakpoint_fault_size;
83 /* The size of the single step instruction causing the actual fault */
84 static int single_step_fault_size;
86 /* The single step trampoline */
87 static gpointer ss_trampoline;
89 /* Offset between fp and the first argument in the callee */
90 #define ARGS_OFFSET 16
91 #define GP_SCRATCH_REG AMD64_R11
94 * AMD64 register usage:
95 * - callee saved registers are used for global register allocation
96 * - %r11 is used for materializing 64 bit constants in opcodes
97 * - the rest is used for local allocation
101 * Floating point comparison results:
111 mono_arch_regname (int reg)
114 case AMD64_RAX: return "%rax";
115 case AMD64_RBX: return "%rbx";
116 case AMD64_RCX: return "%rcx";
117 case AMD64_RDX: return "%rdx";
118 case AMD64_RSP: return "%rsp";
119 case AMD64_RBP: return "%rbp";
120 case AMD64_RDI: return "%rdi";
121 case AMD64_RSI: return "%rsi";
122 case AMD64_R8: return "%r8";
123 case AMD64_R9: return "%r9";
124 case AMD64_R10: return "%r10";
125 case AMD64_R11: return "%r11";
126 case AMD64_R12: return "%r12";
127 case AMD64_R13: return "%r13";
128 case AMD64_R14: return "%r14";
129 case AMD64_R15: return "%r15";
134 static const char * packed_xmmregs [] = {
135 "p:xmm0", "p:xmm1", "p:xmm2", "p:xmm3", "p:xmm4", "p:xmm5", "p:xmm6", "p:xmm7", "p:xmm8",
136 "p:xmm9", "p:xmm10", "p:xmm11", "p:xmm12", "p:xmm13", "p:xmm14", "p:xmm15"
139 static const char * single_xmmregs [] = {
140 "s:xmm0", "s:xmm1", "s:xmm2", "s:xmm3", "s:xmm4", "s:xmm5", "s:xmm6", "s:xmm7", "s:xmm8",
141 "s:xmm9", "s:xmm10", "s:xmm11", "s:xmm12", "s:xmm13", "s:xmm14", "s:xmm15"
145 mono_arch_fregname (int reg)
147 if (reg < AMD64_XMM_NREG)
148 return single_xmmregs [reg];
154 mono_arch_xregname (int reg)
156 if (reg < AMD64_XMM_NREG)
157 return packed_xmmregs [reg];
166 return mono_debug_count ();
172 static inline gboolean
173 amd64_is_near_call (guint8 *code)
176 if ((code [0] >= 0x40) && (code [0] <= 0x4f))
179 return code [0] == 0xe8;
182 static inline gboolean
183 amd64_use_imm32 (gint64 val)
185 if (mini_get_debug_options()->single_imm_size)
188 return amd64_is_imm32 (val);
191 #ifdef __native_client_codegen__
193 /* Keep track of instruction "depth", that is, the level of sub-instruction */
194 /* for any given instruction. For instance, amd64_call_reg resolves to */
195 /* amd64_call_reg_internal, which uses amd64_alu_* macros, etc. */
196 /* We only want to force bundle alignment for the top level instruction, */
197 /* so NaCl pseudo-instructions can be implemented with sub instructions. */
198 static MonoNativeTlsKey nacl_instruction_depth;
200 static MonoNativeTlsKey nacl_rex_tag;
201 static MonoNativeTlsKey nacl_legacy_prefix_tag;
204 amd64_nacl_clear_legacy_prefix_tag ()
206 mono_native_tls_set_value (nacl_legacy_prefix_tag, NULL);
210 amd64_nacl_tag_legacy_prefix (guint8* code)
212 if (mono_native_tls_get_value (nacl_legacy_prefix_tag) == NULL)
213 mono_native_tls_set_value (nacl_legacy_prefix_tag, code);
217 amd64_nacl_tag_rex (guint8* code)
219 mono_native_tls_set_value (nacl_rex_tag, code);
223 amd64_nacl_get_legacy_prefix_tag ()
225 return (guint8*)mono_native_tls_get_value (nacl_legacy_prefix_tag);
229 amd64_nacl_get_rex_tag ()
231 return (guint8*)mono_native_tls_get_value (nacl_rex_tag);
234 /* Increment the instruction "depth" described above */
236 amd64_nacl_instruction_pre ()
238 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
240 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)depth);
243 /* amd64_nacl_instruction_post: Decrement instruction "depth", force bundle */
244 /* alignment if depth == 0 (top level instruction) */
245 /* IN: start, end pointers to instruction beginning and end */
246 /* OUT: start, end pointers to beginning and end after possible alignment */
247 /* GLOBALS: nacl_instruction_depth defined above */
249 amd64_nacl_instruction_post (guint8 **start, guint8 **end)
251 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
253 mono_native_tls_set_value (nacl_instruction_depth, (void*)depth);
255 g_assert ( depth >= 0 );
257 uintptr_t space_in_block;
259 guint8 *prefix = amd64_nacl_get_legacy_prefix_tag ();
260 /* if legacy prefix is present, and if it was emitted before */
261 /* the start of the instruction sequence, adjust the start */
262 if (prefix != NULL && prefix < *start) {
263 g_assert (*start - prefix <= 3);/* only 3 are allowed */
266 space_in_block = kNaClAlignment - ((uintptr_t)(*start) & kNaClAlignmentMask);
267 instlen = (uintptr_t)(*end - *start);
268 /* Only check for instructions which are less than */
269 /* kNaClAlignment. The only instructions that should ever */
270 /* be that long are call sequences, which are already */
271 /* padded out to align the return to the next bundle. */
272 if (instlen > space_in_block && instlen < kNaClAlignment) {
273 const size_t MAX_NACL_INST_LENGTH = kNaClAlignment;
274 guint8 copy_of_instruction[MAX_NACL_INST_LENGTH];
275 const size_t length = (size_t)((*end)-(*start));
276 g_assert (length < MAX_NACL_INST_LENGTH);
278 memcpy (copy_of_instruction, *start, length);
279 *start = mono_arch_nacl_pad (*start, space_in_block);
280 memcpy (*start, copy_of_instruction, length);
281 *end = *start + length;
283 amd64_nacl_clear_legacy_prefix_tag ();
284 amd64_nacl_tag_rex (NULL);
288 /* amd64_nacl_membase_handler: ensure all access to memory of the form */
289 /* OFFSET(%rXX) is sandboxed. For allowable base registers %rip, %rbp, */
290 /* %rsp, and %r15, emit the membase as usual. For all other registers, */
291 /* make sure the upper 32-bits are cleared, and use that register in the */
292 /* index field of a new address of this form: OFFSET(%r15,%eXX,1) */
294 /* pointer to current instruction stream (in the */
295 /* middle of an instruction, after opcode is emitted) */
296 /* basereg/offset/dreg */
297 /* operands of normal membase address */
299 /* pointer to the end of the membase/memindex emit */
300 /* GLOBALS: nacl_rex_tag */
301 /* position in instruction stream that rex prefix was emitted */
302 /* nacl_legacy_prefix_tag */
303 /* (possibly NULL) position in instruction of legacy x86 prefix */
305 amd64_nacl_membase_handler (guint8** code, gint8 basereg, gint32 offset, gint8 dreg)
307 gint8 true_basereg = basereg;
309 /* Cache these values, they might change */
310 /* as new instructions are emitted below. */
311 guint8* rex_tag = amd64_nacl_get_rex_tag ();
312 guint8* legacy_prefix_tag = amd64_nacl_get_legacy_prefix_tag ();
314 /* 'basereg' is given masked to 0x7 at this point, so check */
315 /* the rex prefix to see if this is an extended register. */
316 if ((rex_tag != NULL) && IS_REX(*rex_tag) && (*rex_tag & AMD64_REX_B)) {
320 #define X86_LEA_OPCODE (0x8D)
322 if (!amd64_is_valid_nacl_base (true_basereg) && (*(*code-1) != X86_LEA_OPCODE)) {
323 guint8* old_instruction_start;
325 /* This will hold the 'mov %eXX, %eXX' that clears the upper */
326 /* 32-bits of the old base register (new index register) */
328 guint8* buf_ptr = buf;
331 g_assert (rex_tag != NULL);
333 if (IS_REX(*rex_tag)) {
334 /* The old rex.B should be the new rex.X */
335 if (*rex_tag & AMD64_REX_B) {
336 *rex_tag |= AMD64_REX_X;
338 /* Since our new base is %r15 set rex.B */
339 *rex_tag |= AMD64_REX_B;
341 /* Shift the instruction by one byte */
342 /* so we can insert a rex prefix */
343 memmove (rex_tag + 1, rex_tag, (size_t)(*code - rex_tag));
345 /* New rex prefix only needs rex.B for %r15 base */
346 *rex_tag = AMD64_REX(AMD64_REX_B);
349 if (legacy_prefix_tag) {
350 old_instruction_start = legacy_prefix_tag;
352 old_instruction_start = rex_tag;
355 /* Clears the upper 32-bits of the previous base register */
356 amd64_mov_reg_reg_size (buf_ptr, true_basereg, true_basereg, 4);
357 insert_len = buf_ptr - buf;
359 /* Move the old instruction forward to make */
360 /* room for 'mov' stored in 'buf_ptr' */
361 memmove (old_instruction_start + insert_len, old_instruction_start, (size_t)(*code - old_instruction_start));
363 memcpy (old_instruction_start, buf, insert_len);
365 /* Sandboxed replacement for the normal membase_emit */
366 x86_memindex_emit (*code, dreg, AMD64_R15, offset, basereg, 0);
369 /* Normal default behavior, emit membase memory location */
370 x86_membase_emit_body (*code, dreg, basereg, offset);
375 static inline unsigned char*
376 amd64_skip_nops (unsigned char* code)
381 if ( code[0] == 0x90) {
385 if ( code[0] == 0x66 && code[1] == 0x90) {
389 if (code[0] == 0x0f && code[1] == 0x1f
390 && code[2] == 0x00) {
394 if (code[0] == 0x0f && code[1] == 0x1f
395 && code[2] == 0x40 && code[3] == 0x00) {
399 if (code[0] == 0x0f && code[1] == 0x1f
400 && code[2] == 0x44 && code[3] == 0x00
401 && code[4] == 0x00) {
405 if (code[0] == 0x66 && code[1] == 0x0f
406 && code[2] == 0x1f && code[3] == 0x44
407 && code[4] == 0x00 && code[5] == 0x00) {
411 if (code[0] == 0x0f && code[1] == 0x1f
412 && code[2] == 0x80 && code[3] == 0x00
413 && code[4] == 0x00 && code[5] == 0x00
414 && code[6] == 0x00) {
418 if (code[0] == 0x0f && code[1] == 0x1f
419 && code[2] == 0x84 && code[3] == 0x00
420 && code[4] == 0x00 && code[5] == 0x00
421 && code[6] == 0x00 && code[7] == 0x00) {
430 mono_arch_nacl_skip_nops (guint8* code)
432 return amd64_skip_nops(code);
435 #endif /*__native_client_codegen__*/
438 amd64_patch (unsigned char* code, gpointer target)
442 #ifdef __native_client_codegen__
443 code = amd64_skip_nops (code);
445 #if defined(__native_client_codegen__) && defined(__native_client__)
446 if (nacl_is_code_address (code)) {
447 /* For tail calls, code is patched after being installed */
448 /* but not through the normal "patch callsite" method. */
449 unsigned char buf[kNaClAlignment];
450 unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
452 memcpy (buf, aligned_code, kNaClAlignment);
453 /* Patch a temp buffer of bundle size, */
454 /* then install to actual location. */
455 amd64_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), target);
456 ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
460 target = nacl_modify_patch_target (target);
464 if ((code [0] >= 0x40) && (code [0] <= 0x4f)) {
469 if ((code [0] & 0xf8) == 0xb8) {
470 /* amd64_set_reg_template */
471 *(guint64*)(code + 1) = (guint64)target;
473 else if ((code [0] == 0x8b) && rex && x86_modrm_mod (code [1]) == 0 && x86_modrm_rm (code [1]) == 5) {
474 /* mov 0(%rip), %dreg */
475 *(guint32*)(code + 2) = (guint32)(guint64)target - 7;
477 else if ((code [0] == 0xff) && (code [1] == 0x15)) {
478 /* call *<OFFSET>(%rip) */
479 *(guint32*)(code + 2) = ((guint32)(guint64)target) - 7;
481 else if (code [0] == 0xe8) {
483 gint64 disp = (guint8*)target - (guint8*)code;
484 g_assert (amd64_is_imm32 (disp));
485 x86_patch (code, (unsigned char*)target);
488 x86_patch (code, (unsigned char*)target);
492 mono_amd64_patch (unsigned char* code, gpointer target)
494 amd64_patch (code, target);
503 ArgValuetypeAddrInIReg,
504 ArgNone /* only in pair_storage */
512 /* Only if storage == ArgValuetypeInReg */
513 ArgStorage pair_storage [2];
515 /* The size of each pair */
525 gboolean need_stack_align;
526 gboolean vtype_retaddr;
527 /* The index of the vret arg in the argument list */
534 #define DEBUG(a) if (cfg->verbose_level > 1) a
537 static AMD64_Reg_No param_regs [] = { AMD64_RCX, AMD64_RDX, AMD64_R8, AMD64_R9 };
539 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
541 static AMD64_Reg_No param_regs [] = { AMD64_RDI, AMD64_RSI, AMD64_RDX, AMD64_RCX, AMD64_R8, AMD64_R9 };
543 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
547 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
549 ainfo->offset = *stack_size;
551 if (*gr >= PARAM_REGS) {
552 ainfo->storage = ArgOnStack;
553 /* Since the same stack slot size is used for all arg */
554 /* types, it needs to be big enough to hold them all */
555 (*stack_size) += sizeof(mgreg_t);
558 ainfo->storage = ArgInIReg;
559 ainfo->reg = param_regs [*gr];
565 #define FLOAT_PARAM_REGS 4
567 #define FLOAT_PARAM_REGS 8
571 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
573 ainfo->offset = *stack_size;
575 if (*gr >= FLOAT_PARAM_REGS) {
576 ainfo->storage = ArgOnStack;
577 /* Since the same stack slot size is used for both float */
578 /* types, it needs to be big enough to hold them both */
579 (*stack_size) += sizeof(mgreg_t);
582 /* A double register */
584 ainfo->storage = ArgInDoubleSSEReg;
586 ainfo->storage = ArgInFloatSSEReg;
592 typedef enum ArgumentClass {
600 merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
602 ArgumentClass class2 = ARG_CLASS_NO_CLASS;
605 ptype = mini_get_underlying_type (type);
606 switch (ptype->type) {
615 case MONO_TYPE_STRING:
616 case MONO_TYPE_OBJECT:
617 case MONO_TYPE_CLASS:
618 case MONO_TYPE_SZARRAY:
620 case MONO_TYPE_FNPTR:
621 case MONO_TYPE_ARRAY:
624 class2 = ARG_CLASS_INTEGER;
629 class2 = ARG_CLASS_INTEGER;
631 class2 = ARG_CLASS_SSE;
635 case MONO_TYPE_TYPEDBYREF:
636 g_assert_not_reached ();
638 case MONO_TYPE_GENERICINST:
639 if (!mono_type_generic_inst_is_valuetype (ptype)) {
640 class2 = ARG_CLASS_INTEGER;
644 case MONO_TYPE_VALUETYPE: {
645 MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
648 for (i = 0; i < info->num_fields; ++i) {
650 class2 = merge_argument_class_from_type (info->fields [i].field->type, class2);
655 g_assert_not_reached ();
659 if (class1 == class2)
661 else if (class1 == ARG_CLASS_NO_CLASS)
663 else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
664 class1 = ARG_CLASS_MEMORY;
665 else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
666 class1 = ARG_CLASS_INTEGER;
668 class1 = ARG_CLASS_SSE;
672 #ifdef __native_client_codegen__
674 /* Default alignment for Native Client is 32-byte. */
675 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
677 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
678 /* Check that alignment doesn't cross an alignment boundary. */
680 mono_arch_nacl_pad(guint8 *code, int pad)
682 const int kMaxPadding = 8; /* see amd64-codegen.h:amd64_padding_size() */
684 if (pad == 0) return code;
685 /* assertion: alignment cannot cross a block boundary */
686 g_assert (((uintptr_t)code & (~kNaClAlignmentMask)) ==
687 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
688 while (pad >= kMaxPadding) {
689 amd64_padding (code, kMaxPadding);
692 if (pad != 0) amd64_padding (code, pad);
698 count_fields_nested (MonoClass *klass)
700 MonoMarshalType *info;
703 info = mono_marshal_load_type_info (klass);
706 for (i = 0; i < info->num_fields; ++i) {
707 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type))
708 count += count_fields_nested (mono_class_from_mono_type (info->fields [i].field->type));
716 collect_field_info_nested (MonoClass *klass, MonoMarshalField *fields, int index, int offset)
718 MonoMarshalType *info;
721 info = mono_marshal_load_type_info (klass);
723 for (i = 0; i < info->num_fields; ++i) {
724 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type)) {
725 index = collect_field_info_nested (mono_class_from_mono_type (info->fields [i].field->type), fields, index, info->fields [i].offset);
727 memcpy (&fields [index], &info->fields [i], sizeof (MonoMarshalField));
728 fields [index].offset += offset;
736 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
738 guint32 *gr, guint32 *fr, guint32 *stack_size)
740 guint32 size, quad, nquads, i, nfields;
741 /* Keep track of the size used in each quad so we can */
742 /* use the right size when copying args/return vars. */
743 guint32 quadsize [2] = {8, 8};
744 ArgumentClass args [2];
745 MonoMarshalType *info = NULL;
746 MonoMarshalField *fields = NULL;
748 gboolean pass_on_stack = FALSE;
750 klass = mono_class_from_mono_type (type);
751 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
753 if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
754 /* We pass and return vtypes of size 8 in a register */
755 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
756 pass_on_stack = TRUE;
760 pass_on_stack = TRUE;
764 /* If this struct can't be split up naturally into 8-byte */
765 /* chunks (registers), pass it on the stack. */
766 if (sig->pinvoke && !pass_on_stack) {
770 info = mono_marshal_load_type_info (klass);
774 * Collect field information recursively to be able to
775 * handle nested structures.
777 nfields = count_fields_nested (klass);
778 fields = g_new0 (MonoMarshalField, nfields);
779 collect_field_info_nested (klass, fields, 0, 0);
781 for (i = 0; i < nfields; ++i) {
782 field_size = mono_marshal_type_size (fields [i].field->type,
784 &align, TRUE, klass->unicode);
785 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
786 pass_on_stack = TRUE;
794 ainfo->storage = ArgValuetypeInReg;
795 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
801 /* Allways pass in memory */
802 ainfo->offset = *stack_size;
803 *stack_size += ALIGN_TO (size, 8);
804 ainfo->storage = ArgOnStack;
810 /* FIXME: Handle structs smaller than 8 bytes */
811 //if ((size % 8) != 0)
820 int n = mono_class_value_size (klass, NULL);
822 quadsize [0] = n >= 8 ? 8 : n;
823 quadsize [1] = n >= 8 ? MAX (n - 8, 8) : 0;
825 /* Always pass in 1 or 2 integer registers */
826 args [0] = ARG_CLASS_INTEGER;
827 args [1] = ARG_CLASS_INTEGER;
828 /* Only the simplest cases are supported */
829 if (is_return && nquads != 1) {
830 args [0] = ARG_CLASS_MEMORY;
831 args [1] = ARG_CLASS_MEMORY;
835 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
836 * The X87 and SSEUP stuff is left out since there are no such types in
842 ainfo->storage = ArgValuetypeInReg;
843 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
848 if (info->native_size > 16) {
849 ainfo->offset = *stack_size;
850 *stack_size += ALIGN_TO (info->native_size, 8);
851 ainfo->storage = ArgOnStack;
857 switch (info->native_size) {
858 case 1: case 2: case 4: case 8:
862 ainfo->storage = ArgOnStack;
863 ainfo->offset = *stack_size;
864 *stack_size += ALIGN_TO (info->native_size, 8);
867 ainfo->storage = ArgValuetypeAddrInIReg;
869 if (*gr < PARAM_REGS) {
870 ainfo->pair_storage [0] = ArgInIReg;
871 ainfo->pair_regs [0] = param_regs [*gr];
875 ainfo->pair_storage [0] = ArgOnStack;
876 ainfo->offset = *stack_size;
886 args [0] = ARG_CLASS_NO_CLASS;
887 args [1] = ARG_CLASS_NO_CLASS;
888 for (quad = 0; quad < nquads; ++quad) {
891 ArgumentClass class1;
894 class1 = ARG_CLASS_MEMORY;
896 class1 = ARG_CLASS_NO_CLASS;
897 for (i = 0; i < nfields; ++i) {
898 size = mono_marshal_type_size (fields [i].field->type,
900 &align, TRUE, klass->unicode);
901 if ((fields [i].offset < 8) && (fields [i].offset + size) > 8) {
902 /* Unaligned field */
906 /* Skip fields in other quad */
907 if ((quad == 0) && (fields [i].offset >= 8))
909 if ((quad == 1) && (fields [i].offset < 8))
912 /* How far into this quad this data extends.*/
913 /* (8 is size of quad) */
914 quadsize [quad] = fields [i].offset + size - (quad * 8);
916 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
918 g_assert (class1 != ARG_CLASS_NO_CLASS);
919 args [quad] = class1;
925 /* Post merger cleanup */
926 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
927 args [0] = args [1] = ARG_CLASS_MEMORY;
929 /* Allocate registers */
934 while (quadsize [0] != 1 && quadsize [0] != 2 && quadsize [0] != 4 && quadsize [0] != 8)
936 while (quadsize [1] != 1 && quadsize [1] != 2 && quadsize [1] != 4 && quadsize [1] != 8)
939 ainfo->storage = ArgValuetypeInReg;
940 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
941 g_assert (quadsize [0] <= 8);
942 g_assert (quadsize [1] <= 8);
943 ainfo->pair_size [0] = quadsize [0];
944 ainfo->pair_size [1] = quadsize [1];
945 ainfo->nregs = nquads;
946 for (quad = 0; quad < nquads; ++quad) {
947 switch (args [quad]) {
948 case ARG_CLASS_INTEGER:
949 if (*gr >= PARAM_REGS)
950 args [quad] = ARG_CLASS_MEMORY;
952 ainfo->pair_storage [quad] = ArgInIReg;
954 ainfo->pair_regs [quad] = return_regs [*gr];
956 ainfo->pair_regs [quad] = param_regs [*gr];
961 if (*fr >= FLOAT_PARAM_REGS)
962 args [quad] = ARG_CLASS_MEMORY;
964 if (quadsize[quad] <= 4)
965 ainfo->pair_storage [quad] = ArgInFloatSSEReg;
966 else ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
967 ainfo->pair_regs [quad] = *fr;
971 case ARG_CLASS_MEMORY:
974 g_assert_not_reached ();
978 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
979 /* Revert possible register assignments */
983 ainfo->offset = *stack_size;
985 *stack_size += ALIGN_TO (info->native_size, 8);
987 *stack_size += nquads * sizeof(mgreg_t);
988 ainfo->storage = ArgOnStack;
996 * Obtain information about a call according to the calling convention.
997 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
998 * Draft Version 0.23" document for more information.
1001 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
1003 guint32 i, gr, fr, pstart;
1005 int n = sig->hasthis + sig->param_count;
1006 guint32 stack_size = 0;
1008 gboolean is_pinvoke = sig->pinvoke;
1011 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1013 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1021 /* Reserve space where the callee can save the argument registers */
1022 stack_size = 4 * sizeof (mgreg_t);
1026 ret_type = mini_get_underlying_type (sig->ret);
1027 switch (ret_type->type) {
1037 case MONO_TYPE_FNPTR:
1038 case MONO_TYPE_CLASS:
1039 case MONO_TYPE_OBJECT:
1040 case MONO_TYPE_SZARRAY:
1041 case MONO_TYPE_ARRAY:
1042 case MONO_TYPE_STRING:
1043 cinfo->ret.storage = ArgInIReg;
1044 cinfo->ret.reg = AMD64_RAX;
1048 cinfo->ret.storage = ArgInIReg;
1049 cinfo->ret.reg = AMD64_RAX;
1052 cinfo->ret.storage = ArgInFloatSSEReg;
1053 cinfo->ret.reg = AMD64_XMM0;
1056 cinfo->ret.storage = ArgInDoubleSSEReg;
1057 cinfo->ret.reg = AMD64_XMM0;
1059 case MONO_TYPE_GENERICINST:
1060 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1061 cinfo->ret.storage = ArgInIReg;
1062 cinfo->ret.reg = AMD64_RAX;
1066 #if defined( __native_client_codegen__ )
1067 case MONO_TYPE_TYPEDBYREF:
1069 case MONO_TYPE_VALUETYPE: {
1070 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
1072 add_valuetype (sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
1073 if (cinfo->ret.storage == ArgOnStack) {
1074 cinfo->vtype_retaddr = TRUE;
1075 /* The caller passes the address where the value is stored */
1079 #if !defined( __native_client_codegen__ )
1080 case MONO_TYPE_TYPEDBYREF:
1081 /* Same as a valuetype with size 24 */
1082 cinfo->vtype_retaddr = TRUE;
1085 case MONO_TYPE_VOID:
1088 g_error ("Can't handle as return value 0x%x", ret_type->type);
1093 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1094 * the first argument, allowing 'this' to be always passed in the first arg reg.
1095 * Also do this if the first argument is a reference type, since virtual calls
1096 * are sometimes made using calli without sig->hasthis set, like in the delegate
1099 if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
1101 add_general (&gr, &stack_size, cinfo->args + 0);
1103 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
1106 add_general (&gr, &stack_size, &cinfo->ret);
1107 cinfo->vret_arg_index = 1;
1111 add_general (&gr, &stack_size, cinfo->args + 0);
1113 if (cinfo->vtype_retaddr)
1114 add_general (&gr, &stack_size, &cinfo->ret);
1117 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
1119 fr = FLOAT_PARAM_REGS;
1121 /* Emit the signature cookie just before the implicit arguments */
1122 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1125 for (i = pstart; i < sig->param_count; ++i) {
1126 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
1130 /* The float param registers and other param registers must be the same index on Windows x64.*/
1137 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1138 /* We allways pass the sig cookie on the stack for simplicity */
1140 * Prevent implicit arguments + the sig cookie from being passed
1144 fr = FLOAT_PARAM_REGS;
1146 /* Emit the signature cookie just before the implicit arguments */
1147 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1150 ptype = mini_get_underlying_type (sig->params [i]);
1151 switch (ptype->type) {
1154 add_general (&gr, &stack_size, ainfo);
1158 add_general (&gr, &stack_size, ainfo);
1162 add_general (&gr, &stack_size, ainfo);
1167 case MONO_TYPE_FNPTR:
1168 case MONO_TYPE_CLASS:
1169 case MONO_TYPE_OBJECT:
1170 case MONO_TYPE_STRING:
1171 case MONO_TYPE_SZARRAY:
1172 case MONO_TYPE_ARRAY:
1173 add_general (&gr, &stack_size, ainfo);
1175 case MONO_TYPE_GENERICINST:
1176 if (!mono_type_generic_inst_is_valuetype (ptype)) {
1177 add_general (&gr, &stack_size, ainfo);
1181 case MONO_TYPE_VALUETYPE:
1182 case MONO_TYPE_TYPEDBYREF:
1183 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
1188 add_general (&gr, &stack_size, ainfo);
1191 add_float (&fr, &stack_size, ainfo, FALSE);
1194 add_float (&fr, &stack_size, ainfo, TRUE);
1197 g_assert_not_reached ();
1201 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
1203 fr = FLOAT_PARAM_REGS;
1205 /* Emit the signature cookie just before the implicit arguments */
1206 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1209 cinfo->stack_usage = stack_size;
1210 cinfo->reg_usage = gr;
1211 cinfo->freg_usage = fr;
1216 * mono_arch_get_argument_info:
1217 * @csig: a method signature
1218 * @param_count: the number of parameters to consider
1219 * @arg_info: an array to store the result infos
1221 * Gathers information on parameters such as size, alignment and
1222 * padding. arg_info should be large enought to hold param_count + 1 entries.
1224 * Returns the size of the argument area on the stack.
1227 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
1230 CallInfo *cinfo = get_call_info (NULL, csig);
1231 guint32 args_size = cinfo->stack_usage;
1233 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
1234 if (csig->hasthis) {
1235 arg_info [0].offset = 0;
1238 for (k = 0; k < param_count; k++) {
1239 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
1241 arg_info [k + 1].size = 0;
1250 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
1254 MonoType *callee_ret;
1256 c1 = get_call_info (NULL, caller_sig);
1257 c2 = get_call_info (NULL, callee_sig);
1258 res = c1->stack_usage >= c2->stack_usage;
1259 callee_ret = mini_get_underlying_type (callee_sig->ret);
1260 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
1261 /* An address on the callee's stack is passed as the first argument */
1271 * Initialize the cpu to execute managed code.
1274 mono_arch_cpu_init (void)
1279 /* spec compliance requires running with double precision */
1280 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1281 fpcw &= ~X86_FPCW_PRECC_MASK;
1282 fpcw |= X86_FPCW_PREC_DOUBLE;
1283 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
1284 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1286 /* TODO: This is crashing on Win64 right now.
1287 * _control87 (_PC_53, MCW_PC);
1293 * Initialize architecture specific code.
1296 mono_arch_init (void)
1300 mono_mutex_init_recursive (&mini_arch_mutex);
1301 #if defined(__native_client_codegen__)
1302 mono_native_tls_alloc (&nacl_instruction_depth, NULL);
1303 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)0);
1304 mono_native_tls_alloc (&nacl_rex_tag, NULL);
1305 mono_native_tls_alloc (&nacl_legacy_prefix_tag, NULL);
1308 #ifdef MONO_ARCH_NOMAP32BIT
1309 flags = MONO_MMAP_READ;
1310 /* amd64_mov_reg_imm () + amd64_mov_reg_membase () */
1311 breakpoint_size = 13;
1312 breakpoint_fault_size = 3;
1314 flags = MONO_MMAP_READ|MONO_MMAP_32BIT;
1315 /* amd64_mov_reg_mem () */
1316 breakpoint_size = 8;
1317 breakpoint_fault_size = 8;
1320 /* amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4); */
1321 single_step_fault_size = 4;
1323 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), flags);
1324 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), flags);
1325 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
1327 mono_aot_register_jit_icall ("mono_amd64_throw_exception", mono_amd64_throw_exception);
1328 mono_aot_register_jit_icall ("mono_amd64_throw_corlib_exception", mono_amd64_throw_corlib_exception);
1329 mono_aot_register_jit_icall ("mono_amd64_resume_unwind", mono_amd64_resume_unwind);
1330 mono_aot_register_jit_icall ("mono_amd64_get_original_ip", mono_amd64_get_original_ip);
1334 * Cleanup architecture specific code.
1337 mono_arch_cleanup (void)
1339 mono_mutex_destroy (&mini_arch_mutex);
1340 #if defined(__native_client_codegen__)
1341 mono_native_tls_free (nacl_instruction_depth);
1342 mono_native_tls_free (nacl_rex_tag);
1343 mono_native_tls_free (nacl_legacy_prefix_tag);
1348 * This function returns the optimizations supported on this cpu.
1351 mono_arch_cpu_optimizations (guint32 *exclude_mask)
1357 if (mono_hwcap_x86_has_cmov) {
1358 opts |= MONO_OPT_CMOV;
1360 if (mono_hwcap_x86_has_fcmov)
1361 opts |= MONO_OPT_FCMOV;
1363 *exclude_mask |= MONO_OPT_FCMOV;
1365 *exclude_mask |= MONO_OPT_CMOV;
1372 * This function test for all SSE functions supported.
1374 * Returns a bitmask corresponding to all supported versions.
1378 mono_arch_cpu_enumerate_simd_versions (void)
1380 guint32 sse_opts = 0;
1382 if (mono_hwcap_x86_has_sse1)
1383 sse_opts |= SIMD_VERSION_SSE1;
1385 if (mono_hwcap_x86_has_sse2)
1386 sse_opts |= SIMD_VERSION_SSE2;
1388 if (mono_hwcap_x86_has_sse3)
1389 sse_opts |= SIMD_VERSION_SSE3;
1391 if (mono_hwcap_x86_has_ssse3)
1392 sse_opts |= SIMD_VERSION_SSSE3;
1394 if (mono_hwcap_x86_has_sse41)
1395 sse_opts |= SIMD_VERSION_SSE41;
1397 if (mono_hwcap_x86_has_sse42)
1398 sse_opts |= SIMD_VERSION_SSE42;
1400 if (mono_hwcap_x86_has_sse4a)
1401 sse_opts |= SIMD_VERSION_SSE4a;
1409 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1414 for (i = 0; i < cfg->num_varinfo; i++) {
1415 MonoInst *ins = cfg->varinfo [i];
1416 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1419 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1422 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
1423 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1426 if (mono_is_regsize_var (ins->inst_vtype)) {
1427 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1428 g_assert (i == vmv->idx);
1429 vars = g_list_prepend (vars, vmv);
1433 vars = mono_varlist_sort (cfg, vars, 0);
1439 * mono_arch_compute_omit_fp:
1441 * Determine whenever the frame pointer can be eliminated.
1444 mono_arch_compute_omit_fp (MonoCompile *cfg)
1446 MonoMethodSignature *sig;
1447 MonoMethodHeader *header;
1451 if (cfg->arch.omit_fp_computed)
1454 header = cfg->header;
1456 sig = mono_method_signature (cfg->method);
1458 if (!cfg->arch.cinfo)
1459 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1460 cinfo = cfg->arch.cinfo;
1463 * FIXME: Remove some of the restrictions.
1465 cfg->arch.omit_fp = TRUE;
1466 cfg->arch.omit_fp_computed = TRUE;
1468 #ifdef __native_client_codegen__
1469 /* NaCl modules may not change the value of RBP, so it cannot be */
1470 /* used as a normal register, but it can be used as a frame pointer*/
1471 cfg->disable_omit_fp = TRUE;
1472 cfg->arch.omit_fp = FALSE;
1475 if (cfg->disable_omit_fp)
1476 cfg->arch.omit_fp = FALSE;
1478 if (!debug_omit_fp ())
1479 cfg->arch.omit_fp = FALSE;
1481 if (cfg->method->save_lmf)
1482 cfg->arch.omit_fp = FALSE;
1484 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1485 cfg->arch.omit_fp = FALSE;
1486 if (header->num_clauses)
1487 cfg->arch.omit_fp = FALSE;
1488 if (cfg->param_area)
1489 cfg->arch.omit_fp = FALSE;
1490 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1491 cfg->arch.omit_fp = FALSE;
1492 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1493 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1494 cfg->arch.omit_fp = FALSE;
1495 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1496 ArgInfo *ainfo = &cinfo->args [i];
1498 if (ainfo->storage == ArgOnStack) {
1500 * The stack offset can only be determined when the frame
1503 cfg->arch.omit_fp = FALSE;
1508 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1509 MonoInst *ins = cfg->varinfo [i];
1512 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1517 mono_arch_get_global_int_regs (MonoCompile *cfg)
1521 mono_arch_compute_omit_fp (cfg);
1523 if (cfg->arch.omit_fp)
1524 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1526 /* We use the callee saved registers for global allocation */
1527 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1528 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1529 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1530 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1531 #ifndef __native_client_codegen__
1532 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1535 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1536 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1543 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1548 /* All XMM registers */
1549 for (i = 0; i < 16; ++i)
1550 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1556 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1558 static GList *r = NULL;
1563 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1564 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1565 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1566 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1567 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1568 #ifndef __native_client_codegen__
1569 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1572 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1573 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1574 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1575 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1576 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1577 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1578 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1579 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1581 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1588 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1591 static GList *r = NULL;
1596 for (i = 0; i < AMD64_XMM_NREG; ++i)
1597 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1599 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1606 * mono_arch_regalloc_cost:
1608 * Return the cost, in number of memory references, of the action of
1609 * allocating the variable VMV into a register during global register
1613 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1615 MonoInst *ins = cfg->varinfo [vmv->idx];
1617 if (cfg->method->save_lmf)
1618 /* The register is already saved */
1619 /* substract 1 for the invisible store in the prolog */
1620 return (ins->opcode == OP_ARG) ? 0 : 1;
1623 return (ins->opcode == OP_ARG) ? 1 : 2;
1627 * mono_arch_fill_argument_info:
1629 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1633 mono_arch_fill_argument_info (MonoCompile *cfg)
1636 MonoMethodSignature *sig;
1641 sig = mono_method_signature (cfg->method);
1643 cinfo = cfg->arch.cinfo;
1644 sig_ret = mini_get_underlying_type (sig->ret);
1647 * Contrary to mono_arch_allocate_vars (), the information should describe
1648 * where the arguments are at the beginning of the method, not where they can be
1649 * accessed during the execution of the method. The later makes no sense for the
1650 * global register allocator, since a variable can be in more than one location.
1652 if (sig_ret->type != MONO_TYPE_VOID) {
1653 switch (cinfo->ret.storage) {
1655 case ArgInFloatSSEReg:
1656 case ArgInDoubleSSEReg:
1657 if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->vtype_retaddr)) {
1658 cfg->vret_addr->opcode = OP_REGVAR;
1659 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1662 cfg->ret->opcode = OP_REGVAR;
1663 cfg->ret->inst_c0 = cinfo->ret.reg;
1666 case ArgValuetypeInReg:
1667 cfg->ret->opcode = OP_REGOFFSET;
1668 cfg->ret->inst_basereg = -1;
1669 cfg->ret->inst_offset = -1;
1672 g_assert_not_reached ();
1676 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1677 ArgInfo *ainfo = &cinfo->args [i];
1679 ins = cfg->args [i];
1681 switch (ainfo->storage) {
1683 case ArgInFloatSSEReg:
1684 case ArgInDoubleSSEReg:
1685 ins->opcode = OP_REGVAR;
1686 ins->inst_c0 = ainfo->reg;
1689 ins->opcode = OP_REGOFFSET;
1690 ins->inst_basereg = -1;
1691 ins->inst_offset = -1;
1693 case ArgValuetypeInReg:
1695 ins->opcode = OP_NOP;
1698 g_assert_not_reached ();
1704 mono_arch_allocate_vars (MonoCompile *cfg)
1707 MonoMethodSignature *sig;
1710 guint32 locals_stack_size, locals_stack_align;
1714 sig = mono_method_signature (cfg->method);
1716 cinfo = cfg->arch.cinfo;
1717 sig_ret = mini_get_underlying_type (sig->ret);
1719 mono_arch_compute_omit_fp (cfg);
1722 * We use the ABI calling conventions for managed code as well.
1723 * Exception: valuetypes are only sometimes passed or returned in registers.
1727 * The stack looks like this:
1728 * <incoming arguments passed on the stack>
1730 * <lmf/caller saved registers>
1733 * <localloc area> -> grows dynamically
1737 if (cfg->arch.omit_fp) {
1738 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1739 cfg->frame_reg = AMD64_RSP;
1742 /* Locals are allocated backwards from %fp */
1743 cfg->frame_reg = AMD64_RBP;
1747 cfg->arch.saved_iregs = cfg->used_int_regs;
1748 if (cfg->method->save_lmf)
1749 /* Save all callee-saved registers normally, and restore them when unwinding through an LMF */
1750 cfg->arch.saved_iregs |= (1 << AMD64_RBX) | (1 << AMD64_R12) | (1 << AMD64_R13) | (1 << AMD64_R14) | (1 << AMD64_R15);
1752 if (cfg->arch.omit_fp)
1753 cfg->arch.reg_save_area_offset = offset;
1754 /* Reserve space for callee saved registers */
1755 for (i = 0; i < AMD64_NREG; ++i)
1756 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
1757 offset += sizeof(mgreg_t);
1759 if (!cfg->arch.omit_fp)
1760 cfg->arch.reg_save_area_offset = -offset;
1762 if (sig_ret->type != MONO_TYPE_VOID) {
1763 switch (cinfo->ret.storage) {
1765 case ArgInFloatSSEReg:
1766 case ArgInDoubleSSEReg:
1767 if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->vtype_retaddr)) {
1768 /* The register is volatile */
1769 cfg->vret_addr->opcode = OP_REGOFFSET;
1770 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1771 if (cfg->arch.omit_fp) {
1772 cfg->vret_addr->inst_offset = offset;
1776 cfg->vret_addr->inst_offset = -offset;
1778 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1779 printf ("vret_addr =");
1780 mono_print_ins (cfg->vret_addr);
1784 cfg->ret->opcode = OP_REGVAR;
1785 cfg->ret->inst_c0 = cinfo->ret.reg;
1788 case ArgValuetypeInReg:
1789 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1790 cfg->ret->opcode = OP_REGOFFSET;
1791 cfg->ret->inst_basereg = cfg->frame_reg;
1792 if (cfg->arch.omit_fp) {
1793 cfg->ret->inst_offset = offset;
1794 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1796 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1797 cfg->ret->inst_offset = - offset;
1801 g_assert_not_reached ();
1803 cfg->ret->dreg = cfg->ret->inst_c0;
1806 /* Allocate locals */
1807 offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1808 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1809 char *mname = mono_method_full_name (cfg->method, TRUE);
1810 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1811 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1816 if (locals_stack_align) {
1817 offset += (locals_stack_align - 1);
1818 offset &= ~(locals_stack_align - 1);
1820 if (cfg->arch.omit_fp) {
1821 cfg->locals_min_stack_offset = offset;
1822 cfg->locals_max_stack_offset = offset + locals_stack_size;
1824 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1825 cfg->locals_max_stack_offset = - offset;
1828 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1829 if (offsets [i] != -1) {
1830 MonoInst *ins = cfg->varinfo [i];
1831 ins->opcode = OP_REGOFFSET;
1832 ins->inst_basereg = cfg->frame_reg;
1833 if (cfg->arch.omit_fp)
1834 ins->inst_offset = (offset + offsets [i]);
1836 ins->inst_offset = - (offset + offsets [i]);
1837 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1840 offset += locals_stack_size;
1842 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1843 g_assert (!cfg->arch.omit_fp);
1844 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1845 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1848 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1849 ins = cfg->args [i];
1850 if (ins->opcode != OP_REGVAR) {
1851 ArgInfo *ainfo = &cinfo->args [i];
1852 gboolean inreg = TRUE;
1854 /* FIXME: Allocate volatile arguments to registers */
1855 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1859 * Under AMD64, all registers used to pass arguments to functions
1860 * are volatile across calls.
1861 * FIXME: Optimize this.
1863 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg))
1866 ins->opcode = OP_REGOFFSET;
1868 switch (ainfo->storage) {
1870 case ArgInFloatSSEReg:
1871 case ArgInDoubleSSEReg:
1873 ins->opcode = OP_REGVAR;
1874 ins->dreg = ainfo->reg;
1878 g_assert (!cfg->arch.omit_fp);
1879 ins->opcode = OP_REGOFFSET;
1880 ins->inst_basereg = cfg->frame_reg;
1881 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1883 case ArgValuetypeInReg:
1885 case ArgValuetypeAddrInIReg: {
1887 g_assert (!cfg->arch.omit_fp);
1889 MONO_INST_NEW (cfg, indir, 0);
1890 indir->opcode = OP_REGOFFSET;
1891 if (ainfo->pair_storage [0] == ArgInIReg) {
1892 indir->inst_basereg = cfg->frame_reg;
1893 offset = ALIGN_TO (offset, sizeof (gpointer));
1894 offset += (sizeof (gpointer));
1895 indir->inst_offset = - offset;
1898 indir->inst_basereg = cfg->frame_reg;
1899 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
1902 ins->opcode = OP_VTARG_ADDR;
1903 ins->inst_left = indir;
1911 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg)) {
1912 ins->opcode = OP_REGOFFSET;
1913 ins->inst_basereg = cfg->frame_reg;
1914 /* These arguments are saved to the stack in the prolog */
1915 offset = ALIGN_TO (offset, sizeof(mgreg_t));
1916 if (cfg->arch.omit_fp) {
1917 ins->inst_offset = offset;
1918 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1919 // Arguments are yet supported by the stack map creation code
1920 //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
1922 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1923 ins->inst_offset = - offset;
1924 //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
1930 cfg->stack_offset = offset;
1934 mono_arch_create_vars (MonoCompile *cfg)
1936 MonoMethodSignature *sig;
1940 sig = mono_method_signature (cfg->method);
1942 if (!cfg->arch.cinfo)
1943 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1944 cinfo = cfg->arch.cinfo;
1946 if (cinfo->ret.storage == ArgValuetypeInReg)
1947 cfg->ret_var_is_local = TRUE;
1949 sig_ret = mini_get_underlying_type (sig->ret);
1950 if ((cinfo->ret.storage != ArgValuetypeInReg) && MONO_TYPE_ISSTRUCT (sig_ret)) {
1951 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1952 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1953 printf ("vret_addr = ");
1954 mono_print_ins (cfg->vret_addr);
1958 if (cfg->gen_sdb_seq_points) {
1961 if (cfg->compile_aot) {
1962 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1963 ins->flags |= MONO_INST_VOLATILE;
1964 cfg->arch.seq_point_info_var = ins;
1966 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1967 ins->flags |= MONO_INST_VOLATILE;
1968 cfg->arch.ss_tramp_var = ins;
1971 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1972 ins->flags |= MONO_INST_VOLATILE;
1973 cfg->arch.ss_trigger_page_var = ins;
1976 if (cfg->method->save_lmf)
1977 cfg->create_lmf_var = TRUE;
1979 if (cfg->method->save_lmf) {
1981 #if !defined(TARGET_WIN32)
1982 if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
1983 cfg->lmf_ir_mono_lmf = TRUE;
1989 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
1995 MONO_INST_NEW (cfg, ins, OP_MOVE);
1996 ins->dreg = mono_alloc_ireg_copy (cfg, tree->dreg);
1997 ins->sreg1 = tree->dreg;
1998 MONO_ADD_INS (cfg->cbb, ins);
1999 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2001 case ArgInFloatSSEReg:
2002 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
2003 ins->dreg = mono_alloc_freg (cfg);
2004 ins->sreg1 = tree->dreg;
2005 MONO_ADD_INS (cfg->cbb, ins);
2007 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2009 case ArgInDoubleSSEReg:
2010 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2011 ins->dreg = mono_alloc_freg (cfg);
2012 ins->sreg1 = tree->dreg;
2013 MONO_ADD_INS (cfg->cbb, ins);
2015 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2019 g_assert_not_reached ();
2024 arg_storage_to_load_membase (ArgStorage storage)
2028 #if defined(__mono_ilp32__)
2029 return OP_LOADI8_MEMBASE;
2031 return OP_LOAD_MEMBASE;
2033 case ArgInDoubleSSEReg:
2034 return OP_LOADR8_MEMBASE;
2035 case ArgInFloatSSEReg:
2036 return OP_LOADR4_MEMBASE;
2038 g_assert_not_reached ();
2045 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2047 MonoMethodSignature *tmp_sig;
2050 if (call->tail_call)
2053 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
2056 * mono_ArgIterator_Setup assumes the signature cookie is
2057 * passed first and all the arguments which were before it are
2058 * passed on the stack after the signature. So compensate by
2059 * passing a different signature.
2061 tmp_sig = mono_metadata_signature_dup_full (cfg->method->klass->image, call->signature);
2062 tmp_sig->param_count -= call->signature->sentinelpos;
2063 tmp_sig->sentinelpos = 0;
2064 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2066 sig_reg = mono_alloc_ireg (cfg);
2067 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2069 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, cinfo->sig_cookie.offset, sig_reg);
2073 static inline LLVMArgStorage
2074 arg_storage_to_llvm_arg_storage (MonoCompile *cfg, ArgStorage storage)
2078 return LLVMArgInIReg;
2082 g_assert_not_reached ();
2088 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2094 LLVMCallInfo *linfo;
2095 MonoType *t, *sig_ret;
2097 n = sig->param_count + sig->hasthis;
2098 sig_ret = mini_get_underlying_type (sig->ret);
2100 cinfo = get_call_info (cfg->mempool, sig);
2102 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2105 * LLVM always uses the native ABI while we use our own ABI, the
2106 * only difference is the handling of vtypes:
2107 * - we only pass/receive them in registers in some cases, and only
2108 * in 1 or 2 integer registers.
2110 if (cinfo->ret.storage == ArgValuetypeInReg) {
2112 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2113 cfg->disable_llvm = TRUE;
2117 linfo->ret.storage = LLVMArgVtypeInReg;
2118 for (j = 0; j < 2; ++j)
2119 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
2122 if (MONO_TYPE_ISSTRUCT (sig_ret) && cinfo->ret.storage == ArgInIReg) {
2123 /* Vtype returned using a hidden argument */
2124 linfo->ret.storage = LLVMArgVtypeRetAddr;
2125 linfo->vret_arg_index = cinfo->vret_arg_index;
2128 for (i = 0; i < n; ++i) {
2129 ainfo = cinfo->args + i;
2131 if (i >= sig->hasthis)
2132 t = sig->params [i - sig->hasthis];
2134 t = &mono_defaults.int_class->byval_arg;
2136 linfo->args [i].storage = LLVMArgNone;
2138 switch (ainfo->storage) {
2140 linfo->args [i].storage = LLVMArgInIReg;
2142 case ArgInDoubleSSEReg:
2143 case ArgInFloatSSEReg:
2144 linfo->args [i].storage = LLVMArgInFPReg;
2147 if (MONO_TYPE_ISSTRUCT (t)) {
2148 linfo->args [i].storage = LLVMArgVtypeByVal;
2150 linfo->args [i].storage = LLVMArgInIReg;
2152 if (t->type == MONO_TYPE_R4)
2153 linfo->args [i].storage = LLVMArgInFPReg;
2154 else if (t->type == MONO_TYPE_R8)
2155 linfo->args [i].storage = LLVMArgInFPReg;
2159 case ArgValuetypeInReg:
2161 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2162 cfg->disable_llvm = TRUE;
2166 linfo->args [i].storage = LLVMArgVtypeInReg;
2167 for (j = 0; j < 2; ++j)
2168 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2171 cfg->exception_message = g_strdup ("ainfo->storage");
2172 cfg->disable_llvm = TRUE;
2182 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2185 MonoMethodSignature *sig;
2191 sig = call->signature;
2192 n = sig->param_count + sig->hasthis;
2194 cinfo = get_call_info (cfg->mempool, sig);
2198 if (COMPILE_LLVM (cfg)) {
2199 /* We shouldn't be called in the llvm case */
2200 cfg->disable_llvm = TRUE;
2205 * Emit all arguments which are passed on the stack to prevent register
2206 * allocation problems.
2208 for (i = 0; i < n; ++i) {
2210 ainfo = cinfo->args + i;
2212 in = call->args [i];
2214 if (sig->hasthis && i == 0)
2215 t = &mono_defaults.object_class->byval_arg;
2217 t = sig->params [i - sig->hasthis];
2219 t = mini_get_underlying_type (t);
2220 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call) {
2222 if (t->type == MONO_TYPE_R4)
2223 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2224 else if (t->type == MONO_TYPE_R8)
2225 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2227 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2229 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2231 if (cfg->compute_gc_maps) {
2234 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2240 * Emit all parameters passed in registers in non-reverse order for better readability
2241 * and to help the optimization in emit_prolog ().
2243 for (i = 0; i < n; ++i) {
2244 ainfo = cinfo->args + i;
2246 in = call->args [i];
2248 if (ainfo->storage == ArgInIReg)
2249 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2252 for (i = n - 1; i >= 0; --i) {
2255 ainfo = cinfo->args + i;
2257 in = call->args [i];
2259 if (sig->hasthis && i == 0)
2260 t = &mono_defaults.object_class->byval_arg;
2262 t = sig->params [i - sig->hasthis];
2263 t = mini_get_underlying_type (t);
2265 switch (ainfo->storage) {
2269 case ArgInFloatSSEReg:
2270 case ArgInDoubleSSEReg:
2271 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2274 case ArgValuetypeInReg:
2275 case ArgValuetypeAddrInIReg:
2276 if (ainfo->storage == ArgOnStack && call->tail_call) {
2277 MonoInst *call_inst = (MonoInst*)call;
2278 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2279 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2280 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
2284 if (t->type == MONO_TYPE_TYPEDBYREF) {
2285 size = sizeof (MonoTypedRef);
2286 align = sizeof (gpointer);
2290 size = mono_type_native_stack_size (t, &align);
2293 * Other backends use mono_type_stack_size (), but that
2294 * aligns the size to 8, which is larger than the size of
2295 * the source, leading to reads of invalid memory if the
2296 * source is at the end of address space.
2298 size = mono_class_value_size (mono_class_from_mono_type (t), &align);
2301 g_assert (in->klass);
2303 if (ainfo->storage == ArgOnStack && size >= 10000) {
2304 /* Avoid asserts in emit_memcpy () */
2305 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
2306 cfg->exception_message = g_strdup_printf ("Passing an argument of size '%d'.", size);
2307 /* Continue normally */
2311 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2312 arg->sreg1 = in->dreg;
2313 arg->klass = mono_class_from_mono_type (t);
2314 arg->backend.size = size;
2315 arg->inst_p0 = call;
2316 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2317 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2319 MONO_ADD_INS (cfg->cbb, arg);
2324 g_assert_not_reached ();
2327 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
2328 /* Emit the signature cookie just before the implicit arguments */
2329 emit_sig_cookie (cfg, call, cinfo);
2332 /* Handle the case where there are no implicit arguments */
2333 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2334 emit_sig_cookie (cfg, call, cinfo);
2336 sig_ret = mini_get_underlying_type (sig->ret);
2337 if (sig_ret && MONO_TYPE_ISSTRUCT (sig_ret)) {
2340 if (cinfo->ret.storage == ArgValuetypeInReg) {
2341 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2343 * Tell the JIT to use a more efficient calling convention: call using
2344 * OP_CALL, compute the result location after the call, and save the
2347 call->vret_in_reg = TRUE;
2349 * Nullify the instruction computing the vret addr to enable
2350 * future optimizations.
2353 NULLIFY_INS (call->vret_var);
2355 if (call->tail_call)
2358 * The valuetype is in RAX:RDX after the call, need to be copied to
2359 * the stack. Push the address here, so the call instruction can
2362 if (!cfg->arch.vret_addr_loc) {
2363 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2364 /* Prevent it from being register allocated or optimized away */
2365 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2368 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2372 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2373 vtarg->sreg1 = call->vret_var->dreg;
2374 vtarg->dreg = mono_alloc_preg (cfg);
2375 MONO_ADD_INS (cfg->cbb, vtarg);
2377 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2381 if (cfg->method->save_lmf) {
2382 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2383 MONO_ADD_INS (cfg->cbb, arg);
2386 call->stack_usage = cinfo->stack_usage;
2390 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2393 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2394 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2395 int size = ins->backend.size;
2397 if (ainfo->storage == ArgValuetypeInReg) {
2401 for (part = 0; part < 2; ++part) {
2402 if (ainfo->pair_storage [part] == ArgNone)
2405 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2406 load->inst_basereg = src->dreg;
2407 load->inst_offset = part * sizeof(mgreg_t);
2409 switch (ainfo->pair_storage [part]) {
2411 load->dreg = mono_alloc_ireg (cfg);
2413 case ArgInDoubleSSEReg:
2414 case ArgInFloatSSEReg:
2415 load->dreg = mono_alloc_freg (cfg);
2418 g_assert_not_reached ();
2420 MONO_ADD_INS (cfg->cbb, load);
2422 add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2424 } else if (ainfo->storage == ArgValuetypeAddrInIReg) {
2425 MonoInst *vtaddr, *load;
2426 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2428 MONO_INST_NEW (cfg, load, OP_LDADDR);
2429 cfg->has_indirection = TRUE;
2430 load->inst_p0 = vtaddr;
2431 vtaddr->flags |= MONO_INST_INDIRECT;
2432 load->type = STACK_MP;
2433 load->klass = vtaddr->klass;
2434 load->dreg = mono_alloc_ireg (cfg);
2435 MONO_ADD_INS (cfg->cbb, load);
2436 mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2438 if (ainfo->pair_storage [0] == ArgInIReg) {
2439 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2440 arg->dreg = mono_alloc_ireg (cfg);
2441 arg->sreg1 = load->dreg;
2443 MONO_ADD_INS (cfg->cbb, arg);
2444 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2446 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, load->dreg);
2450 int dreg = mono_alloc_ireg (cfg);
2452 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
2453 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, dreg);
2454 } else if (size <= 40) {
2455 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2457 // FIXME: Code growth
2458 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2461 if (cfg->compute_gc_maps) {
2463 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2469 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2471 MonoType *ret = mini_get_underlying_type (mono_method_signature (method)->ret);
2473 if (ret->type == MONO_TYPE_R4) {
2474 if (COMPILE_LLVM (cfg))
2475 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2477 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2479 } else if (ret->type == MONO_TYPE_R8) {
2480 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2484 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2487 #endif /* DISABLE_JIT */
2489 #define EMIT_COND_BRANCH(ins,cond,sign) \
2490 if (ins->inst_true_bb->native_offset) { \
2491 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2493 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2494 if ((cfg->opt & MONO_OPT_BRANCH) && \
2495 x86_is_imm8 (ins->inst_true_bb->max_offset - offset)) \
2496 x86_branch8 (code, cond, 0, sign); \
2498 x86_branch32 (code, cond, 0, sign); \
2502 MonoMethodSignature *sig;
2507 dyn_call_supported (MonoMethodSignature *sig, CallInfo *cinfo)
2515 switch (cinfo->ret.storage) {
2519 case ArgValuetypeInReg: {
2520 ArgInfo *ainfo = &cinfo->ret;
2522 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2524 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2532 for (i = 0; i < cinfo->nargs; ++i) {
2533 ArgInfo *ainfo = &cinfo->args [i];
2534 switch (ainfo->storage) {
2537 case ArgValuetypeInReg:
2538 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2540 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2552 * mono_arch_dyn_call_prepare:
2554 * Return a pointer to an arch-specific structure which contains information
2555 * needed by mono_arch_get_dyn_call_args (). Return NULL if OP_DYN_CALL is not
2556 * supported for SIG.
2557 * This function is equivalent to ffi_prep_cif in libffi.
2560 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2562 ArchDynCallInfo *info;
2565 cinfo = get_call_info (NULL, sig);
2567 if (!dyn_call_supported (sig, cinfo)) {
2572 info = g_new0 (ArchDynCallInfo, 1);
2573 // FIXME: Preprocess the info to speed up get_dyn_call_args ().
2575 info->cinfo = cinfo;
2577 return (MonoDynCallInfo*)info;
2581 * mono_arch_dyn_call_free:
2583 * Free a MonoDynCallInfo structure.
2586 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2588 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2590 g_free (ainfo->cinfo);
2594 #if !defined(__native_client__)
2595 #define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
2596 #define GREG_TO_PTR(greg) (gpointer)(greg)
2598 /* Correctly handle casts to/from 32-bit pointers without compiler warnings */
2599 #define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
2600 #define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
2604 * mono_arch_get_start_dyn_call:
2606 * Convert the arguments ARGS to a format which can be passed to OP_DYN_CALL, and
2607 * store the result into BUF.
2608 * ARGS should be an array of pointers pointing to the arguments.
2609 * RET should point to a memory buffer large enought to hold the result of the
2611 * This function should be as fast as possible, any work which does not depend
2612 * on the actual values of the arguments should be done in
2613 * mono_arch_dyn_call_prepare ().
2614 * start_dyn_call + OP_DYN_CALL + finish_dyn_call is equivalent to ffi_call in
2618 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2620 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2621 DynCallArgs *p = (DynCallArgs*)buf;
2622 int arg_index, greg, i, pindex;
2623 MonoMethodSignature *sig = dinfo->sig;
2625 g_assert (buf_len >= sizeof (DynCallArgs));
2634 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2635 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2640 if (dinfo->cinfo->vtype_retaddr)
2641 p->regs [greg ++] = PTR_TO_GREG(ret);
2643 for (i = pindex; i < sig->param_count; i++) {
2644 MonoType *t = mini_get_underlying_type (sig->params [i]);
2645 gpointer *arg = args [arg_index ++];
2648 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2653 case MONO_TYPE_STRING:
2654 case MONO_TYPE_CLASS:
2655 case MONO_TYPE_ARRAY:
2656 case MONO_TYPE_SZARRAY:
2657 case MONO_TYPE_OBJECT:
2661 #if !defined(__mono_ilp32__)
2665 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2666 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2668 #if defined(__mono_ilp32__)
2671 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2672 p->regs [greg ++] = *(guint64*)(arg);
2676 p->regs [greg ++] = *(guint8*)(arg);
2679 p->regs [greg ++] = *(gint8*)(arg);
2682 p->regs [greg ++] = *(gint16*)(arg);
2685 p->regs [greg ++] = *(guint16*)(arg);
2688 p->regs [greg ++] = *(gint32*)(arg);
2691 p->regs [greg ++] = *(guint32*)(arg);
2693 case MONO_TYPE_GENERICINST:
2694 if (MONO_TYPE_IS_REFERENCE (t)) {
2695 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2700 case MONO_TYPE_VALUETYPE: {
2701 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2703 g_assert (ainfo->storage == ArgValuetypeInReg);
2704 if (ainfo->pair_storage [0] != ArgNone) {
2705 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2706 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2708 if (ainfo->pair_storage [1] != ArgNone) {
2709 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2710 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2715 g_assert_not_reached ();
2719 g_assert (greg <= PARAM_REGS);
2723 * mono_arch_finish_dyn_call:
2725 * Store the result of a dyn call into the return value buffer passed to
2726 * start_dyn_call ().
2727 * This function should be as fast as possible, any work which does not depend
2728 * on the actual values of the arguments should be done in
2729 * mono_arch_dyn_call_prepare ().
2732 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2734 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2735 MonoMethodSignature *sig = dinfo->sig;
2736 guint8 *ret = ((DynCallArgs*)buf)->ret;
2737 mgreg_t res = ((DynCallArgs*)buf)->res;
2738 MonoType *sig_ret = mini_get_underlying_type (sig->ret);
2740 switch (sig_ret->type) {
2741 case MONO_TYPE_VOID:
2742 *(gpointer*)ret = NULL;
2744 case MONO_TYPE_STRING:
2745 case MONO_TYPE_CLASS:
2746 case MONO_TYPE_ARRAY:
2747 case MONO_TYPE_SZARRAY:
2748 case MONO_TYPE_OBJECT:
2752 *(gpointer*)ret = GREG_TO_PTR(res);
2758 *(guint8*)ret = res;
2761 *(gint16*)ret = res;
2764 *(guint16*)ret = res;
2767 *(gint32*)ret = res;
2770 *(guint32*)ret = res;
2773 *(gint64*)ret = res;
2776 *(guint64*)ret = res;
2778 case MONO_TYPE_GENERICINST:
2779 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
2780 *(gpointer*)ret = GREG_TO_PTR(res);
2785 case MONO_TYPE_VALUETYPE:
2786 if (dinfo->cinfo->vtype_retaddr) {
2789 ArgInfo *ainfo = &dinfo->cinfo->ret;
2791 g_assert (ainfo->storage == ArgValuetypeInReg);
2793 if (ainfo->pair_storage [0] != ArgNone) {
2794 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2795 ((mgreg_t*)ret)[0] = res;
2798 g_assert (ainfo->pair_storage [1] == ArgNone);
2802 g_assert_not_reached ();
2806 /* emit an exception if condition is fail */
2807 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2809 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2810 if (tins == NULL) { \
2811 mono_add_patch_info (cfg, code - cfg->native_code, \
2812 MONO_PATCH_INFO_EXC, exc_name); \
2813 x86_branch32 (code, cond, 0, signed); \
2815 EMIT_COND_BRANCH (tins, cond, signed); \
2819 #define EMIT_FPCOMPARE(code) do { \
2820 amd64_fcompp (code); \
2821 amd64_fnstsw (code); \
2824 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
2825 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
2826 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
2827 amd64_ ##op (code); \
2828 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
2829 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
2833 emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
2835 gboolean no_patch = FALSE;
2838 * FIXME: Add support for thunks
2841 gboolean near_call = FALSE;
2844 * Indirect calls are expensive so try to make a near call if possible.
2845 * The caller memory is allocated by the code manager so it is
2846 * guaranteed to be at a 32 bit offset.
2849 if (patch_type != MONO_PATCH_INFO_ABS) {
2850 /* The target is in memory allocated using the code manager */
2853 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
2854 if (((MonoMethod*)data)->klass->image->aot_module)
2855 /* The callee might be an AOT method */
2857 if (((MonoMethod*)data)->dynamic)
2858 /* The target is in malloc-ed memory */
2862 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
2864 * The call might go directly to a native function without
2867 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (data);
2869 gconstpointer target = mono_icall_get_wrapper (mi);
2870 if ((((guint64)target) >> 32) != 0)
2876 MonoJumpInfo *jinfo = NULL;
2878 if (cfg->abs_patches)
2879 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
2881 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2882 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
2883 if (mi && (((guint64)mi->func) >> 32) == 0)
2888 * This is not really an optimization, but required because the
2889 * generic class init trampolines use R11 to pass the vtable.
2894 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
2896 if (info->func == info->wrapper) {
2898 if ((((guint64)info->func) >> 32) == 0)
2902 /* See the comment in mono_codegen () */
2903 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
2907 else if ((((guint64)data) >> 32) == 0) {
2914 if (cfg->method->dynamic)
2915 /* These methods are allocated using malloc */
2918 #ifdef MONO_ARCH_NOMAP32BIT
2921 #if defined(__native_client__)
2922 /* Always use near_call == TRUE for Native Client */
2925 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
2926 if (optimize_for_xen)
2929 if (cfg->compile_aot) {
2936 * Align the call displacement to an address divisible by 4 so it does
2937 * not span cache lines. This is required for code patching to work on SMP
2940 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
2941 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
2942 amd64_padding (code, pad_size);
2944 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2945 amd64_call_code (code, 0);
2948 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2949 amd64_set_reg_template (code, GP_SCRATCH_REG);
2950 amd64_call_reg (code, GP_SCRATCH_REG);
2957 static inline guint8*
2958 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
2961 if (win64_adjust_stack)
2962 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
2964 code = emit_call_body (cfg, code, patch_type, data);
2966 if (win64_adjust_stack)
2967 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
2974 store_membase_imm_to_store_membase_reg (int opcode)
2977 case OP_STORE_MEMBASE_IMM:
2978 return OP_STORE_MEMBASE_REG;
2979 case OP_STOREI4_MEMBASE_IMM:
2980 return OP_STOREI4_MEMBASE_REG;
2981 case OP_STOREI8_MEMBASE_IMM:
2982 return OP_STOREI8_MEMBASE_REG;
2990 #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)))
2993 * mono_arch_peephole_pass_1:
2995 * Perform peephole opts which should/can be performed before local regalloc
2998 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3002 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3003 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3005 switch (ins->opcode) {
3009 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
3011 * X86_LEA is like ADD, but doesn't have the
3012 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
3013 * its operand to 64 bit.
3015 ins->opcode = OP_X86_LEA_MEMBASE;
3016 ins->inst_basereg = ins->sreg1;
3021 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3025 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3026 * the latter has length 2-3 instead of 6 (reverse constant
3027 * propagation). These instruction sequences are very common
3028 * in the initlocals bblock.
3030 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3031 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)) {
3032 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3033 ins2->sreg1 = ins->dreg;
3034 } 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)) {
3036 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3039 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3047 case OP_COMPARE_IMM:
3048 case OP_LCOMPARE_IMM:
3049 /* OP_COMPARE_IMM (reg, 0)
3051 * OP_AMD64_TEST_NULL (reg)
3054 ins->opcode = OP_AMD64_TEST_NULL;
3056 case OP_ICOMPARE_IMM:
3058 ins->opcode = OP_X86_TEST_NULL;
3060 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3062 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3063 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3065 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3066 * OP_COMPARE_IMM reg, imm
3068 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3070 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3071 ins->inst_basereg == last_ins->inst_destbasereg &&
3072 ins->inst_offset == last_ins->inst_offset) {
3073 ins->opcode = OP_ICOMPARE_IMM;
3074 ins->sreg1 = last_ins->sreg1;
3076 /* check if we can remove cmp reg,0 with test null */
3078 ins->opcode = OP_X86_TEST_NULL;
3084 mono_peephole_ins (bb, ins);
3089 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3093 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3094 switch (ins->opcode) {
3097 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3098 /* reg = 0 -> XOR (reg, reg) */
3099 /* XOR sets cflags on x86, so we cant do it always */
3100 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3101 ins->opcode = OP_LXOR;
3102 ins->sreg1 = ins->dreg;
3103 ins->sreg2 = ins->dreg;
3111 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3112 * 0 result into 64 bits.
3114 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3115 ins->opcode = OP_IXOR;
3119 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3123 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3124 * the latter has length 2-3 instead of 6 (reverse constant
3125 * propagation). These instruction sequences are very common
3126 * in the initlocals bblock.
3128 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3129 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)) {
3130 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3131 ins2->sreg1 = ins->dreg;
3132 } 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)) {
3134 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3137 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3146 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3147 ins->opcode = OP_X86_INC_REG;
3150 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3151 ins->opcode = OP_X86_DEC_REG;
3155 mono_peephole_ins (bb, ins);
3159 #define NEW_INS(cfg,ins,dest,op) do { \
3160 MONO_INST_NEW ((cfg), (dest), (op)); \
3161 (dest)->cil_code = (ins)->cil_code; \
3162 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3166 * mono_arch_lowering_pass:
3168 * Converts complex opcodes into simpler ones so that each IR instruction
3169 * corresponds to one machine instruction.
3172 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3174 MonoInst *ins, *n, *temp;
3177 * FIXME: Need to add more instructions, but the current machine
3178 * description can't model some parts of the composite instructions like
3181 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3182 switch (ins->opcode) {
3186 case OP_IDIV_UN_IMM:
3187 case OP_IREM_UN_IMM:
3190 mono_decompose_op_imm (cfg, bb, ins);
3192 case OP_COMPARE_IMM:
3193 case OP_LCOMPARE_IMM:
3194 if (!amd64_use_imm32 (ins->inst_imm)) {
3195 NEW_INS (cfg, ins, temp, OP_I8CONST);
3196 temp->inst_c0 = ins->inst_imm;
3197 temp->dreg = mono_alloc_ireg (cfg);
3198 ins->opcode = OP_COMPARE;
3199 ins->sreg2 = temp->dreg;
3202 #ifndef __mono_ilp32__
3203 case OP_LOAD_MEMBASE:
3205 case OP_LOADI8_MEMBASE:
3206 #ifndef __native_client_codegen__
3207 /* Don't generate memindex opcodes (to simplify */
3208 /* read sandboxing) */
3209 if (!amd64_use_imm32 (ins->inst_offset)) {
3210 NEW_INS (cfg, ins, temp, OP_I8CONST);
3211 temp->inst_c0 = ins->inst_offset;
3212 temp->dreg = mono_alloc_ireg (cfg);
3213 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3214 ins->inst_indexreg = temp->dreg;
3218 #ifndef __mono_ilp32__
3219 case OP_STORE_MEMBASE_IMM:
3221 case OP_STOREI8_MEMBASE_IMM:
3222 if (!amd64_use_imm32 (ins->inst_imm)) {
3223 NEW_INS (cfg, ins, temp, OP_I8CONST);
3224 temp->inst_c0 = ins->inst_imm;
3225 temp->dreg = mono_alloc_ireg (cfg);
3226 ins->opcode = OP_STOREI8_MEMBASE_REG;
3227 ins->sreg1 = temp->dreg;
3230 #ifdef MONO_ARCH_SIMD_INTRINSICS
3231 case OP_EXPAND_I1: {
3232 int temp_reg1 = mono_alloc_ireg (cfg);
3233 int temp_reg2 = mono_alloc_ireg (cfg);
3234 int original_reg = ins->sreg1;
3236 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3237 temp->sreg1 = original_reg;
3238 temp->dreg = temp_reg1;
3240 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3241 temp->sreg1 = temp_reg1;
3242 temp->dreg = temp_reg2;
3245 NEW_INS (cfg, ins, temp, OP_LOR);
3246 temp->sreg1 = temp->dreg = temp_reg2;
3247 temp->sreg2 = temp_reg1;
3249 ins->opcode = OP_EXPAND_I2;
3250 ins->sreg1 = temp_reg2;
3259 bb->max_vreg = cfg->next_vreg;
3263 branch_cc_table [] = {
3264 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3265 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3266 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3269 /* Maps CMP_... constants to X86_CC_... constants */
3272 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3273 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3277 cc_signed_table [] = {
3278 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3279 FALSE, FALSE, FALSE, FALSE
3282 /*#include "cprop.c"*/
3284 static unsigned char*
3285 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3288 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3290 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3293 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3295 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3299 static unsigned char*
3300 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3302 int sreg = tree->sreg1;
3303 int need_touch = FALSE;
3305 #if defined(TARGET_WIN32)
3307 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3308 if (!tree->flags & MONO_INST_INIT)
3317 * If requested stack size is larger than one page,
3318 * perform stack-touch operation
3321 * Generate stack probe code.
3322 * Under Windows, it is necessary to allocate one page at a time,
3323 * "touching" stack after each successful sub-allocation. This is
3324 * because of the way stack growth is implemented - there is a
3325 * guard page before the lowest stack page that is currently commited.
3326 * Stack normally grows sequentially so OS traps access to the
3327 * guard page and commits more pages when needed.
3329 amd64_test_reg_imm (code, sreg, ~0xFFF);
3330 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3332 br[2] = code; /* loop */
3333 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3334 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3335 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3336 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3337 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3338 amd64_patch (br[3], br[2]);
3339 amd64_test_reg_reg (code, sreg, sreg);
3340 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3341 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3343 br[1] = code; x86_jump8 (code, 0);
3345 amd64_patch (br[0], code);
3346 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3347 amd64_patch (br[1], code);
3348 amd64_patch (br[4], code);
3351 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3353 if (tree->flags & MONO_INST_INIT) {
3355 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3356 amd64_push_reg (code, AMD64_RAX);
3359 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3360 amd64_push_reg (code, AMD64_RCX);
3363 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3364 amd64_push_reg (code, AMD64_RDI);
3368 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3369 if (sreg != AMD64_RCX)
3370 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3371 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3373 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3374 if (cfg->param_area)
3375 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3377 #if defined(__default_codegen__)
3378 amd64_prefix (code, X86_REP_PREFIX);
3380 #elif defined(__native_client_codegen__)
3381 /* NaCl stos pseudo-instruction */
3382 amd64_codegen_pre(code);
3383 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3384 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3385 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3386 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3387 amd64_prefix (code, X86_REP_PREFIX);
3389 amd64_codegen_post(code);
3390 #endif /* __native_client_codegen__ */
3392 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3393 amd64_pop_reg (code, AMD64_RDI);
3394 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3395 amd64_pop_reg (code, AMD64_RCX);
3396 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3397 amd64_pop_reg (code, AMD64_RAX);
3403 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3408 /* Move return value to the target register */
3409 /* FIXME: do this in the local reg allocator */
3410 switch (ins->opcode) {
3413 case OP_CALL_MEMBASE:
3416 case OP_LCALL_MEMBASE:
3417 g_assert (ins->dreg == AMD64_RAX);
3421 case OP_FCALL_MEMBASE: {
3422 MonoType *rtype = mini_get_underlying_type (((MonoCallInst*)ins)->signature->ret);
3423 if (rtype->type == MONO_TYPE_R4) {
3424 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3427 if (ins->dreg != AMD64_XMM0)
3428 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3434 case OP_RCALL_MEMBASE:
3435 if (ins->dreg != AMD64_XMM0)
3436 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3440 case OP_VCALL_MEMBASE:
3443 case OP_VCALL2_MEMBASE:
3444 cinfo = get_call_info (cfg->mempool, ((MonoCallInst*)ins)->signature);
3445 if (cinfo->ret.storage == ArgValuetypeInReg) {
3446 MonoInst *loc = cfg->arch.vret_addr_loc;
3448 /* Load the destination address */
3449 g_assert (loc->opcode == OP_REGOFFSET);
3450 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3452 for (quad = 0; quad < 2; quad ++) {
3453 switch (cinfo->ret.pair_storage [quad]) {
3455 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3457 case ArgInFloatSSEReg:
3458 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3460 case ArgInDoubleSSEReg:
3461 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3476 #endif /* DISABLE_JIT */
3479 static int tls_gs_offset;
3483 mono_amd64_have_tls_get (void)
3486 static gboolean have_tls_get = FALSE;
3487 static gboolean inited = FALSE;
3490 return have_tls_get;
3492 #if MONO_HAVE_FAST_TLS
3493 guint8 *ins = (guint8*)pthread_getspecific;
3496 * We're looking for these two instructions:
3498 * mov %gs:[offset](,%rdi,8),%rax
3501 have_tls_get = ins [0] == 0x65 &&
3511 tls_gs_offset = ins[5];
3516 return have_tls_get;
3517 #elif defined(TARGET_ANDROID)
3525 mono_amd64_get_tls_gs_offset (void)
3528 return tls_gs_offset;
3530 g_assert_not_reached ();
3536 * mono_amd64_emit_tls_get:
3537 * @code: buffer to store code to
3538 * @dreg: hard register where to place the result
3539 * @tls_offset: offset info
3541 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3542 * the dreg register the item in the thread local storage identified
3545 * Returns: a pointer to the end of the stored code
3548 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3551 if (tls_offset < 64) {
3552 x86_prefix (code, X86_GS_PREFIX);
3553 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3557 g_assert (tls_offset < 0x440);
3558 /* Load TEB->TlsExpansionSlots */
3559 x86_prefix (code, X86_GS_PREFIX);
3560 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3561 amd64_test_reg_reg (code, dreg, dreg);
3563 amd64_branch (code, X86_CC_EQ, code, TRUE);
3564 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3565 amd64_patch (buf [0], code);
3567 #elif defined(__APPLE__)
3568 x86_prefix (code, X86_GS_PREFIX);
3569 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3571 if (optimize_for_xen) {
3572 x86_prefix (code, X86_FS_PREFIX);
3573 amd64_mov_reg_mem (code, dreg, 0, 8);
3574 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3576 x86_prefix (code, X86_FS_PREFIX);
3577 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3584 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3586 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3588 if (dreg != offset_reg)
3589 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3590 amd64_prefix (code, X86_GS_PREFIX);
3591 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3592 #elif defined(__linux__)
3595 if (dreg == offset_reg) {
3596 /* Use a temporary reg by saving it to the redzone */
3597 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3598 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3599 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3600 offset_reg = tmpreg;
3602 x86_prefix (code, X86_FS_PREFIX);
3603 amd64_mov_reg_mem (code, dreg, 0, 8);
3604 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3606 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3608 g_assert_not_reached ();
3614 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3617 g_assert_not_reached ();
3618 #elif defined(__APPLE__)
3619 x86_prefix (code, X86_GS_PREFIX);
3620 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3622 g_assert (!optimize_for_xen);
3623 x86_prefix (code, X86_FS_PREFIX);
3624 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3630 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3632 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3634 g_assert_not_reached ();
3635 #elif defined(__APPLE__)
3636 x86_prefix (code, X86_GS_PREFIX);
3637 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3639 x86_prefix (code, X86_FS_PREFIX);
3640 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3646 * mono_arch_translate_tls_offset:
3648 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3651 mono_arch_translate_tls_offset (int offset)
3654 return tls_gs_offset + (offset * 8);
3663 * Emit code to initialize an LMF structure at LMF_OFFSET.
3666 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3669 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3672 * sp is saved right before calls but we need to save it here too so
3673 * async stack walks would work.
3675 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3677 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3678 if (cfg->arch.omit_fp && cfa_offset != -1)
3679 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3681 /* These can't contain refs */
3682 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3683 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3684 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3685 /* These are handled automatically by the stack marking code */
3686 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3691 #define REAL_PRINT_REG(text,reg) \
3692 mono_assert (reg >= 0); \
3693 amd64_push_reg (code, AMD64_RAX); \
3694 amd64_push_reg (code, AMD64_RDX); \
3695 amd64_push_reg (code, AMD64_RCX); \
3696 amd64_push_reg (code, reg); \
3697 amd64_push_imm (code, reg); \
3698 amd64_push_imm (code, text " %d %p\n"); \
3699 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
3700 amd64_call_reg (code, AMD64_RAX); \
3701 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
3702 amd64_pop_reg (code, AMD64_RCX); \
3703 amd64_pop_reg (code, AMD64_RDX); \
3704 amd64_pop_reg (code, AMD64_RAX);
3706 /* benchmark and set based on cpu */
3707 #define LOOP_ALIGNMENT 8
3708 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3712 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3717 guint8 *code = cfg->native_code + cfg->code_len;
3720 /* Fix max_offset estimate for each successor bb */
3721 if (cfg->opt & MONO_OPT_BRANCH) {
3722 int current_offset = cfg->code_len;
3723 MonoBasicBlock *current_bb;
3724 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3725 current_bb->max_offset = current_offset;
3726 current_offset += current_bb->max_length;
3730 if (cfg->opt & MONO_OPT_LOOP) {
3731 int pad, align = LOOP_ALIGNMENT;
3732 /* set alignment depending on cpu */
3733 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3735 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3736 amd64_padding (code, pad);
3737 cfg->code_len += pad;
3738 bb->native_offset = cfg->code_len;
3742 #if defined(__native_client_codegen__)
3743 /* For Native Client, all indirect call/jump targets must be */
3744 /* 32-byte aligned. Exception handler blocks are jumped to */
3745 /* indirectly as well. */
3746 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
3747 (bb->flags & BB_EXCEPTION_HANDLER);
3749 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
3750 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
3751 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
3752 cfg->code_len += pad;
3753 bb->native_offset = cfg->code_len;
3755 #endif /*__native_client_codegen__*/
3757 if (cfg->verbose_level > 2)
3758 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3760 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
3761 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3762 g_assert (!cfg->compile_aot);
3764 cov->data [bb->dfn].cil_code = bb->cil_code;
3765 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3766 /* this is not thread save, but good enough */
3767 amd64_inc_membase (code, AMD64_R11, 0);
3770 offset = code - cfg->native_code;
3772 mono_debug_open_block (cfg, bb, offset);
3774 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)
3775 x86_breakpoint (code);
3777 MONO_BB_FOR_EACH_INS (bb, ins) {
3778 offset = code - cfg->native_code;
3780 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3782 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
3784 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
3785 cfg->code_size *= 2;
3786 cfg->native_code = mono_realloc_native_code(cfg);
3787 code = cfg->native_code + offset;
3788 cfg->stat_code_reallocs++;
3791 if (cfg->debug_info)
3792 mono_debug_record_line_number (cfg, ins, offset);
3794 switch (ins->opcode) {
3796 amd64_mul_reg (code, ins->sreg2, TRUE);
3799 amd64_mul_reg (code, ins->sreg2, FALSE);
3801 case OP_X86_SETEQ_MEMBASE:
3802 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3804 case OP_STOREI1_MEMBASE_IMM:
3805 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3807 case OP_STOREI2_MEMBASE_IMM:
3808 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
3810 case OP_STOREI4_MEMBASE_IMM:
3811 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
3813 case OP_STOREI1_MEMBASE_REG:
3814 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
3816 case OP_STOREI2_MEMBASE_REG:
3817 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
3819 /* In AMD64 NaCl, pointers are 4 bytes, */
3820 /* so STORE_* != STOREI8_*. Likewise below. */
3821 case OP_STORE_MEMBASE_REG:
3822 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
3824 case OP_STOREI8_MEMBASE_REG:
3825 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
3827 case OP_STOREI4_MEMBASE_REG:
3828 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
3830 case OP_STORE_MEMBASE_IMM:
3831 #ifndef __native_client_codegen__
3832 /* In NaCl, this could be a PCONST type, which could */
3833 /* mean a pointer type was copied directly into the */
3834 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
3835 /* the value would be 0x00000000FFFFFFFF which is */
3836 /* not proper for an imm32 unless you cast it. */
3837 g_assert (amd64_is_imm32 (ins->inst_imm));
3839 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
3841 case OP_STOREI8_MEMBASE_IMM:
3842 g_assert (amd64_is_imm32 (ins->inst_imm));
3843 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
3846 #ifdef __mono_ilp32__
3847 /* In ILP32, pointers are 4 bytes, so separate these */
3848 /* cases, use literal 8 below where we really want 8 */
3849 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3850 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
3854 // FIXME: Decompose this earlier
3855 if (amd64_use_imm32 (ins->inst_imm))
3856 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
3858 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
3859 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
3863 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3864 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
3867 // FIXME: Decompose this earlier
3868 if (amd64_use_imm32 (ins->inst_imm))
3869 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
3871 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
3872 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3876 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3877 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
3880 /* For NaCl, pointers are 4 bytes, so separate these */
3881 /* cases, use literal 8 below where we really want 8 */
3882 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3883 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
3885 case OP_LOAD_MEMBASE:
3886 g_assert (amd64_is_imm32 (ins->inst_offset));
3887 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
3889 case OP_LOADI8_MEMBASE:
3890 /* Use literal 8 instead of sizeof pointer or */
3891 /* register, we really want 8 for this opcode */
3892 g_assert (amd64_is_imm32 (ins->inst_offset));
3893 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
3895 case OP_LOADI4_MEMBASE:
3896 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3898 case OP_LOADU4_MEMBASE:
3899 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
3901 case OP_LOADU1_MEMBASE:
3902 /* The cpu zero extends the result into 64 bits */
3903 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
3905 case OP_LOADI1_MEMBASE:
3906 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
3908 case OP_LOADU2_MEMBASE:
3909 /* The cpu zero extends the result into 64 bits */
3910 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
3912 case OP_LOADI2_MEMBASE:
3913 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
3915 case OP_AMD64_LOADI8_MEMINDEX:
3916 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
3918 case OP_LCONV_TO_I1:
3919 case OP_ICONV_TO_I1:
3921 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3923 case OP_LCONV_TO_I2:
3924 case OP_ICONV_TO_I2:
3926 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
3928 case OP_LCONV_TO_U1:
3929 case OP_ICONV_TO_U1:
3930 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
3932 case OP_LCONV_TO_U2:
3933 case OP_ICONV_TO_U2:
3934 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
3937 /* Clean out the upper word */
3938 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
3941 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
3945 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3947 case OP_COMPARE_IMM:
3948 #if defined(__mono_ilp32__)
3949 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
3950 g_assert (amd64_is_imm32 (ins->inst_imm));
3951 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
3954 case OP_LCOMPARE_IMM:
3955 g_assert (amd64_is_imm32 (ins->inst_imm));
3956 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
3958 case OP_X86_COMPARE_REG_MEMBASE:
3959 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
3961 case OP_X86_TEST_NULL:
3962 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
3964 case OP_AMD64_TEST_NULL:
3965 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
3968 case OP_X86_ADD_REG_MEMBASE:
3969 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3971 case OP_X86_SUB_REG_MEMBASE:
3972 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3974 case OP_X86_AND_REG_MEMBASE:
3975 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3977 case OP_X86_OR_REG_MEMBASE:
3978 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3980 case OP_X86_XOR_REG_MEMBASE:
3981 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3984 case OP_X86_ADD_MEMBASE_IMM:
3985 /* FIXME: Make a 64 version too */
3986 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3988 case OP_X86_SUB_MEMBASE_IMM:
3989 g_assert (amd64_is_imm32 (ins->inst_imm));
3990 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3992 case OP_X86_AND_MEMBASE_IMM:
3993 g_assert (amd64_is_imm32 (ins->inst_imm));
3994 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3996 case OP_X86_OR_MEMBASE_IMM:
3997 g_assert (amd64_is_imm32 (ins->inst_imm));
3998 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4000 case OP_X86_XOR_MEMBASE_IMM:
4001 g_assert (amd64_is_imm32 (ins->inst_imm));
4002 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4004 case OP_X86_ADD_MEMBASE_REG:
4005 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4007 case OP_X86_SUB_MEMBASE_REG:
4008 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4010 case OP_X86_AND_MEMBASE_REG:
4011 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4013 case OP_X86_OR_MEMBASE_REG:
4014 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4016 case OP_X86_XOR_MEMBASE_REG:
4017 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4019 case OP_X86_INC_MEMBASE:
4020 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4022 case OP_X86_INC_REG:
4023 amd64_inc_reg_size (code, ins->dreg, 4);
4025 case OP_X86_DEC_MEMBASE:
4026 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4028 case OP_X86_DEC_REG:
4029 amd64_dec_reg_size (code, ins->dreg, 4);
4031 case OP_X86_MUL_REG_MEMBASE:
4032 case OP_X86_MUL_MEMBASE_REG:
4033 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4035 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4036 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4038 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4039 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4041 case OP_AMD64_COMPARE_MEMBASE_REG:
4042 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4044 case OP_AMD64_COMPARE_MEMBASE_IMM:
4045 g_assert (amd64_is_imm32 (ins->inst_imm));
4046 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4048 case OP_X86_COMPARE_MEMBASE8_IMM:
4049 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4051 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4052 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4054 case OP_AMD64_COMPARE_REG_MEMBASE:
4055 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4058 case OP_AMD64_ADD_REG_MEMBASE:
4059 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4061 case OP_AMD64_SUB_REG_MEMBASE:
4062 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4064 case OP_AMD64_AND_REG_MEMBASE:
4065 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4067 case OP_AMD64_OR_REG_MEMBASE:
4068 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4070 case OP_AMD64_XOR_REG_MEMBASE:
4071 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4074 case OP_AMD64_ADD_MEMBASE_REG:
4075 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4077 case OP_AMD64_SUB_MEMBASE_REG:
4078 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4080 case OP_AMD64_AND_MEMBASE_REG:
4081 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4083 case OP_AMD64_OR_MEMBASE_REG:
4084 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4086 case OP_AMD64_XOR_MEMBASE_REG:
4087 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4090 case OP_AMD64_ADD_MEMBASE_IMM:
4091 g_assert (amd64_is_imm32 (ins->inst_imm));
4092 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4094 case OP_AMD64_SUB_MEMBASE_IMM:
4095 g_assert (amd64_is_imm32 (ins->inst_imm));
4096 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4098 case OP_AMD64_AND_MEMBASE_IMM:
4099 g_assert (amd64_is_imm32 (ins->inst_imm));
4100 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4102 case OP_AMD64_OR_MEMBASE_IMM:
4103 g_assert (amd64_is_imm32 (ins->inst_imm));
4104 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4106 case OP_AMD64_XOR_MEMBASE_IMM:
4107 g_assert (amd64_is_imm32 (ins->inst_imm));
4108 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4112 amd64_breakpoint (code);
4114 case OP_RELAXED_NOP:
4115 x86_prefix (code, X86_REP_PREFIX);
4123 case OP_DUMMY_STORE:
4124 case OP_DUMMY_ICONST:
4125 case OP_DUMMY_R8CONST:
4126 case OP_NOT_REACHED:
4129 case OP_IL_SEQ_POINT:
4130 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4132 case OP_SEQ_POINT: {
4135 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4136 if (cfg->compile_aot) {
4137 MonoInst *var = cfg->arch.ss_tramp_var;
4140 /* Load ss_tramp_var */
4141 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4142 /* Load the trampoline address */
4143 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4144 /* Call it if it is non-null */
4145 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4147 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4148 amd64_call_reg (code, AMD64_R11);
4149 amd64_patch (label, code);
4152 * Read from the single stepping trigger page. This will cause a
4153 * SIGSEGV when single stepping is enabled.
4154 * We do this _before_ the breakpoint, so single stepping after
4155 * a breakpoint is hit will step to the next IL offset.
4157 MonoInst *var = cfg->arch.ss_trigger_page_var;
4159 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4160 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4);
4165 * This is the address which is saved in seq points,
4167 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4169 if (cfg->compile_aot) {
4170 guint32 offset = code - cfg->native_code;
4172 MonoInst *info_var = cfg->arch.seq_point_info_var;
4176 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4177 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4178 /* Load the info->bp_addrs [offset], which is either NULL or the address of the breakpoint trampoline */
4179 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4180 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4182 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4183 /* Call the trampoline */
4184 amd64_call_reg (code, AMD64_R11);
4185 amd64_patch (label, code);
4188 * A placeholder for a possible breakpoint inserted by
4189 * mono_arch_set_breakpoint ().
4191 for (i = 0; i < breakpoint_size; ++i)
4195 * Add an additional nop so skipping the bp doesn't cause the ip to point
4196 * to another IL offset.
4204 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4207 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4211 g_assert (amd64_is_imm32 (ins->inst_imm));
4212 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4215 g_assert (amd64_is_imm32 (ins->inst_imm));
4216 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4221 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4224 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4228 g_assert (amd64_is_imm32 (ins->inst_imm));
4229 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4232 g_assert (amd64_is_imm32 (ins->inst_imm));
4233 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4236 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4240 g_assert (amd64_is_imm32 (ins->inst_imm));
4241 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4244 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4249 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4251 switch (ins->inst_imm) {
4255 if (ins->dreg != ins->sreg1)
4256 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4257 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4260 /* LEA r1, [r2 + r2*2] */
4261 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4264 /* LEA r1, [r2 + r2*4] */
4265 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4268 /* LEA r1, [r2 + r2*2] */
4270 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4271 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4274 /* LEA r1, [r2 + r2*8] */
4275 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4278 /* LEA r1, [r2 + r2*4] */
4280 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4281 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4284 /* LEA r1, [r2 + r2*2] */
4286 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4287 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4290 /* LEA r1, [r2 + r2*4] */
4291 /* LEA r1, [r1 + r1*4] */
4292 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4293 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4296 /* LEA r1, [r2 + r2*4] */
4298 /* LEA r1, [r1 + r1*4] */
4299 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4300 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4301 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4304 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
4311 #if defined( __native_client_codegen__ )
4312 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4313 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4315 /* Regalloc magic makes the div/rem cases the same */
4316 if (ins->sreg2 == AMD64_RDX) {
4317 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4319 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4322 amd64_div_reg (code, ins->sreg2, TRUE);
4327 #if defined( __native_client_codegen__ )
4328 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4329 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4331 if (ins->sreg2 == AMD64_RDX) {
4332 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4333 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4334 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4336 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4337 amd64_div_reg (code, ins->sreg2, FALSE);
4342 #if defined( __native_client_codegen__ )
4343 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4344 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4346 if (ins->sreg2 == AMD64_RDX) {
4347 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4348 amd64_cdq_size (code, 4);
4349 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4351 amd64_cdq_size (code, 4);
4352 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4357 #if defined( __native_client_codegen__ )
4358 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4359 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4361 if (ins->sreg2 == AMD64_RDX) {
4362 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4363 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4364 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4366 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4367 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4371 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4372 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4375 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4379 g_assert (amd64_is_imm32 (ins->inst_imm));
4380 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4383 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4387 g_assert (amd64_is_imm32 (ins->inst_imm));
4388 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4391 g_assert (ins->sreg2 == AMD64_RCX);
4392 amd64_shift_reg (code, X86_SHL, ins->dreg);
4395 g_assert (ins->sreg2 == AMD64_RCX);
4396 amd64_shift_reg (code, X86_SAR, ins->dreg);
4400 g_assert (amd64_is_imm32 (ins->inst_imm));
4401 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4404 g_assert (amd64_is_imm32 (ins->inst_imm));
4405 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4407 case OP_LSHR_UN_IMM:
4408 g_assert (amd64_is_imm32 (ins->inst_imm));
4409 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4412 g_assert (ins->sreg2 == AMD64_RCX);
4413 amd64_shift_reg (code, X86_SHR, ins->dreg);
4417 g_assert (amd64_is_imm32 (ins->inst_imm));
4418 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4423 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4426 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4429 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4432 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4436 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4439 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4442 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4445 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4448 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4451 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4454 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4457 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4460 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4463 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4466 amd64_neg_reg_size (code, ins->sreg1, 4);
4469 amd64_not_reg_size (code, ins->sreg1, 4);
4472 g_assert (ins->sreg2 == AMD64_RCX);
4473 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4476 g_assert (ins->sreg2 == AMD64_RCX);
4477 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4480 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4482 case OP_ISHR_UN_IMM:
4483 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4486 g_assert (ins->sreg2 == AMD64_RCX);
4487 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4490 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4493 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4496 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4497 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4499 case OP_IMUL_OVF_UN:
4500 case OP_LMUL_OVF_UN: {
4501 /* the mul operation and the exception check should most likely be split */
4502 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4503 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4504 /*g_assert (ins->sreg2 == X86_EAX);
4505 g_assert (ins->dreg == X86_EAX);*/
4506 if (ins->sreg2 == X86_EAX) {
4507 non_eax_reg = ins->sreg1;
4508 } else if (ins->sreg1 == X86_EAX) {
4509 non_eax_reg = ins->sreg2;
4511 /* no need to save since we're going to store to it anyway */
4512 if (ins->dreg != X86_EAX) {
4514 amd64_push_reg (code, X86_EAX);
4516 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4517 non_eax_reg = ins->sreg2;
4519 if (ins->dreg == X86_EDX) {
4522 amd64_push_reg (code, X86_EAX);
4526 amd64_push_reg (code, X86_EDX);
4528 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4529 /* save before the check since pop and mov don't change the flags */
4530 if (ins->dreg != X86_EAX)
4531 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4533 amd64_pop_reg (code, X86_EDX);
4535 amd64_pop_reg (code, X86_EAX);
4536 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4540 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4542 case OP_ICOMPARE_IMM:
4543 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4565 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4573 case OP_CMOV_INE_UN:
4574 case OP_CMOV_IGE_UN:
4575 case OP_CMOV_IGT_UN:
4576 case OP_CMOV_ILE_UN:
4577 case OP_CMOV_ILT_UN:
4583 case OP_CMOV_LNE_UN:
4584 case OP_CMOV_LGE_UN:
4585 case OP_CMOV_LGT_UN:
4586 case OP_CMOV_LLE_UN:
4587 case OP_CMOV_LLT_UN:
4588 g_assert (ins->dreg == ins->sreg1);
4589 /* This needs to operate on 64 bit values */
4590 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);
4594 amd64_not_reg (code, ins->sreg1);
4597 amd64_neg_reg (code, ins->sreg1);
4602 if (amd64_use_imm32 (ins->inst_c0))
4603 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4605 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4608 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4609 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4612 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4613 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4616 if (ins->dreg != ins->sreg1)
4617 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4619 case OP_AMD64_SET_XMMREG_R4: {
4621 if (ins->dreg != ins->sreg1)
4622 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4624 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4628 case OP_AMD64_SET_XMMREG_R8: {
4629 if (ins->dreg != ins->sreg1)
4630 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4634 MonoCallInst *call = (MonoCallInst*)ins;
4635 int i, save_area_offset;
4637 g_assert (!cfg->method->save_lmf);
4639 /* Restore callee saved registers */
4640 save_area_offset = cfg->arch.reg_save_area_offset;
4641 for (i = 0; i < AMD64_NREG; ++i)
4642 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4643 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4644 save_area_offset += 8;
4647 if (cfg->arch.omit_fp) {
4648 if (cfg->arch.stack_alloc_size)
4649 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4651 if (call->stack_usage)
4654 /* Copy arguments on the stack to our argument area */
4655 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4656 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4657 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4663 offset = code - cfg->native_code;
4664 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4665 if (cfg->compile_aot)
4666 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4668 amd64_set_reg_template (code, AMD64_R11);
4669 amd64_jump_reg (code, AMD64_R11);
4670 ins->flags |= MONO_INST_GC_CALLSITE;
4671 ins->backend.pc_offset = code - cfg->native_code;
4675 /* ensure ins->sreg1 is not NULL */
4676 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4679 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4680 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4690 call = (MonoCallInst*)ins;
4692 * The AMD64 ABI forces callers to know about varargs.
4694 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4695 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4696 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4698 * Since the unmanaged calling convention doesn't contain a
4699 * 'vararg' entry, we have to treat every pinvoke call as a
4700 * potential vararg call.
4704 for (i = 0; i < AMD64_XMM_NREG; ++i)
4705 if (call->used_fregs & (1 << i))
4708 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4710 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4713 if (ins->flags & MONO_INST_HAS_METHOD)
4714 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4716 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
4717 ins->flags |= MONO_INST_GC_CALLSITE;
4718 ins->backend.pc_offset = code - cfg->native_code;
4719 code = emit_move_return_value (cfg, ins, code);
4726 case OP_VOIDCALL_REG:
4728 call = (MonoCallInst*)ins;
4730 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4731 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4732 ins->sreg1 = AMD64_R11;
4736 * The AMD64 ABI forces callers to know about varargs.
4738 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4739 if (ins->sreg1 == AMD64_RAX) {
4740 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4741 ins->sreg1 = AMD64_R11;
4743 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4744 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4746 * Since the unmanaged calling convention doesn't contain a
4747 * 'vararg' entry, we have to treat every pinvoke call as a
4748 * potential vararg call.
4752 for (i = 0; i < AMD64_XMM_NREG; ++i)
4753 if (call->used_fregs & (1 << i))
4755 if (ins->sreg1 == AMD64_RAX) {
4756 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4757 ins->sreg1 = AMD64_R11;
4760 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4762 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4765 amd64_call_reg (code, ins->sreg1);
4766 ins->flags |= MONO_INST_GC_CALLSITE;
4767 ins->backend.pc_offset = code - cfg->native_code;
4768 code = emit_move_return_value (cfg, ins, code);
4770 case OP_FCALL_MEMBASE:
4771 case OP_RCALL_MEMBASE:
4772 case OP_LCALL_MEMBASE:
4773 case OP_VCALL_MEMBASE:
4774 case OP_VCALL2_MEMBASE:
4775 case OP_VOIDCALL_MEMBASE:
4776 case OP_CALL_MEMBASE:
4777 call = (MonoCallInst*)ins;
4779 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
4780 ins->flags |= MONO_INST_GC_CALLSITE;
4781 ins->backend.pc_offset = code - cfg->native_code;
4782 code = emit_move_return_value (cfg, ins, code);
4786 MonoInst *var = cfg->dyn_call_var;
4788 g_assert (var->opcode == OP_REGOFFSET);
4790 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
4791 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4793 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
4795 /* Save args buffer */
4796 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
4798 /* Set argument registers */
4799 for (i = 0; i < PARAM_REGS; ++i)
4800 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
4803 amd64_call_reg (code, AMD64_R10);
4805 ins->flags |= MONO_INST_GC_CALLSITE;
4806 ins->backend.pc_offset = code - cfg->native_code;
4809 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4810 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
4813 case OP_AMD64_SAVE_SP_TO_LMF: {
4814 MonoInst *lmf_var = cfg->lmf_var;
4815 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
4819 g_assert_not_reached ();
4820 amd64_push_reg (code, ins->sreg1);
4822 case OP_X86_PUSH_IMM:
4823 g_assert_not_reached ();
4824 g_assert (amd64_is_imm32 (ins->inst_imm));
4825 amd64_push_imm (code, ins->inst_imm);
4827 case OP_X86_PUSH_MEMBASE:
4828 g_assert_not_reached ();
4829 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
4831 case OP_X86_PUSH_OBJ: {
4832 int size = ALIGN_TO (ins->inst_imm, 8);
4834 g_assert_not_reached ();
4836 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4837 amd64_push_reg (code, AMD64_RDI);
4838 amd64_push_reg (code, AMD64_RSI);
4839 amd64_push_reg (code, AMD64_RCX);
4840 if (ins->inst_offset)
4841 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
4843 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
4844 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
4845 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
4847 amd64_prefix (code, X86_REP_PREFIX);
4849 amd64_pop_reg (code, AMD64_RCX);
4850 amd64_pop_reg (code, AMD64_RSI);
4851 amd64_pop_reg (code, AMD64_RDI);
4854 case OP_GENERIC_CLASS_INIT: {
4855 static int byte_offset = -1;
4856 static guint8 bitmask;
4859 g_assert (ins->sreg1 == MONO_AMD64_ARG_REG1);
4861 if (byte_offset < 0)
4862 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
4864 amd64_test_membase_imm_size (code, ins->sreg1, byte_offset, bitmask, 1);
4866 amd64_branch8 (code, X86_CC_NZ, -1, 1);
4868 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_generic_class_init", FALSE);
4869 ins->flags |= MONO_INST_GC_CALLSITE;
4870 ins->backend.pc_offset = code - cfg->native_code;
4872 x86_patch (jump, code);
4877 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
4879 case OP_X86_LEA_MEMBASE:
4880 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
4883 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
4886 /* keep alignment */
4887 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
4888 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
4889 code = mono_emit_stack_alloc (cfg, code, ins);
4890 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4891 if (cfg->param_area)
4892 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4894 case OP_LOCALLOC_IMM: {
4895 guint32 size = ins->inst_imm;
4896 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
4898 if (ins->flags & MONO_INST_INIT) {
4902 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4903 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4905 for (i = 0; i < size; i += 8)
4906 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
4907 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4909 amd64_mov_reg_imm (code, ins->dreg, size);
4910 ins->sreg1 = ins->dreg;
4912 code = mono_emit_stack_alloc (cfg, code, ins);
4913 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4916 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4917 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4919 if (cfg->param_area)
4920 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4924 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4925 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4926 (gpointer)"mono_arch_throw_exception", FALSE);
4927 ins->flags |= MONO_INST_GC_CALLSITE;
4928 ins->backend.pc_offset = code - cfg->native_code;
4932 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4933 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4934 (gpointer)"mono_arch_rethrow_exception", FALSE);
4935 ins->flags |= MONO_INST_GC_CALLSITE;
4936 ins->backend.pc_offset = code - cfg->native_code;
4939 case OP_CALL_HANDLER:
4941 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
4942 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4943 amd64_call_imm (code, 0);
4944 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4945 /* Restore stack alignment */
4946 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
4948 case OP_START_HANDLER: {
4949 /* Even though we're saving RSP, use sizeof */
4950 /* gpointer because spvar is of type IntPtr */
4951 /* see: mono_create_spvar_for_region */
4952 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4953 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
4955 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
4956 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
4958 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
4962 case OP_ENDFINALLY: {
4963 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4964 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
4968 case OP_ENDFILTER: {
4969 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4970 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
4971 /* The local allocator will put the result into RAX */
4976 if (ins->dreg != AMD64_RAX)
4977 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, sizeof (gpointer));
4980 ins->inst_c0 = code - cfg->native_code;
4983 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
4984 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
4986 if (ins->inst_target_bb->native_offset) {
4987 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
4989 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4990 if ((cfg->opt & MONO_OPT_BRANCH) &&
4991 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
4992 x86_jump8 (code, 0);
4994 x86_jump32 (code, 0);
4998 amd64_jump_reg (code, ins->sreg1);
5021 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5022 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5024 case OP_COND_EXC_EQ:
5025 case OP_COND_EXC_NE_UN:
5026 case OP_COND_EXC_LT:
5027 case OP_COND_EXC_LT_UN:
5028 case OP_COND_EXC_GT:
5029 case OP_COND_EXC_GT_UN:
5030 case OP_COND_EXC_GE:
5031 case OP_COND_EXC_GE_UN:
5032 case OP_COND_EXC_LE:
5033 case OP_COND_EXC_LE_UN:
5034 case OP_COND_EXC_IEQ:
5035 case OP_COND_EXC_INE_UN:
5036 case OP_COND_EXC_ILT:
5037 case OP_COND_EXC_ILT_UN:
5038 case OP_COND_EXC_IGT:
5039 case OP_COND_EXC_IGT_UN:
5040 case OP_COND_EXC_IGE:
5041 case OP_COND_EXC_IGE_UN:
5042 case OP_COND_EXC_ILE:
5043 case OP_COND_EXC_ILE_UN:
5044 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
5046 case OP_COND_EXC_OV:
5047 case OP_COND_EXC_NO:
5049 case OP_COND_EXC_NC:
5050 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5051 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
5053 case OP_COND_EXC_IOV:
5054 case OP_COND_EXC_INO:
5055 case OP_COND_EXC_IC:
5056 case OP_COND_EXC_INC:
5057 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5058 (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
5061 /* floating point opcodes */
5063 double d = *(double *)ins->inst_p0;
5065 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5066 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5069 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5070 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5075 float f = *(float *)ins->inst_p0;
5077 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5079 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5081 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5084 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5085 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5087 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5091 case OP_STORER8_MEMBASE_REG:
5092 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5094 case OP_LOADR8_MEMBASE:
5095 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5097 case OP_STORER4_MEMBASE_REG:
5099 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5101 /* This requires a double->single conversion */
5102 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5103 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5106 case OP_LOADR4_MEMBASE:
5108 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5110 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5111 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5114 case OP_ICONV_TO_R4:
5116 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5118 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5119 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5122 case OP_ICONV_TO_R8:
5123 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5125 case OP_LCONV_TO_R4:
5127 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5129 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5130 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5133 case OP_LCONV_TO_R8:
5134 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5136 case OP_FCONV_TO_R4:
5138 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5140 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5141 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5144 case OP_FCONV_TO_I1:
5145 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5147 case OP_FCONV_TO_U1:
5148 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5150 case OP_FCONV_TO_I2:
5151 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5153 case OP_FCONV_TO_U2:
5154 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5156 case OP_FCONV_TO_U4:
5157 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5159 case OP_FCONV_TO_I4:
5161 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5163 case OP_FCONV_TO_I8:
5164 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5167 case OP_RCONV_TO_I1:
5168 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5169 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5171 case OP_RCONV_TO_U1:
5172 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5173 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5175 case OP_RCONV_TO_I2:
5176 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5177 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5179 case OP_RCONV_TO_U2:
5180 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5181 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5183 case OP_RCONV_TO_I4:
5184 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5186 case OP_RCONV_TO_U4:
5187 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5189 case OP_RCONV_TO_I8:
5190 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5192 case OP_RCONV_TO_R8:
5193 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5195 case OP_RCONV_TO_R4:
5196 if (ins->dreg != ins->sreg1)
5197 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5200 case OP_LCONV_TO_R_UN: {
5203 /* Based on gcc code */
5204 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5205 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5208 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5209 br [1] = code; x86_jump8 (code, 0);
5210 amd64_patch (br [0], code);
5213 /* Save to the red zone */
5214 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5215 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5216 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5217 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5218 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5219 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5220 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5221 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5222 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5224 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5225 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5226 amd64_patch (br [1], code);
5229 case OP_LCONV_TO_OVF_U4:
5230 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5231 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5232 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5234 case OP_LCONV_TO_OVF_I4_UN:
5235 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5236 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5237 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5240 if (ins->dreg != ins->sreg1)
5241 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5244 if (ins->dreg != ins->sreg1)
5245 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5247 case OP_MOVE_F_TO_I4:
5249 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5251 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5252 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5255 case OP_MOVE_I4_TO_F:
5256 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5258 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5260 case OP_MOVE_F_TO_I8:
5261 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5263 case OP_MOVE_I8_TO_F:
5264 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5267 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5270 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5273 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5276 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5279 static double r8_0 = -0.0;
5281 g_assert (ins->sreg1 == ins->dreg);
5283 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5284 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5288 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5291 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5294 static guint64 d = 0x7fffffffffffffffUL;
5296 g_assert (ins->sreg1 == ins->dreg);
5298 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5299 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5303 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5307 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5310 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5313 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5316 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5319 static float r4_0 = -0.0;
5321 g_assert (ins->sreg1 == ins->dreg);
5323 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5324 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5325 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
5330 g_assert (cfg->opt & MONO_OPT_CMOV);
5331 g_assert (ins->dreg == ins->sreg1);
5332 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5333 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
5336 g_assert (cfg->opt & MONO_OPT_CMOV);
5337 g_assert (ins->dreg == ins->sreg1);
5338 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5339 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
5342 g_assert (cfg->opt & MONO_OPT_CMOV);
5343 g_assert (ins->dreg == ins->sreg1);
5344 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5345 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
5348 g_assert (cfg->opt & MONO_OPT_CMOV);
5349 g_assert (ins->dreg == ins->sreg1);
5350 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5351 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
5354 g_assert (cfg->opt & MONO_OPT_CMOV);
5355 g_assert (ins->dreg == ins->sreg1);
5356 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5357 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
5360 g_assert (cfg->opt & MONO_OPT_CMOV);
5361 g_assert (ins->dreg == ins->sreg1);
5362 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5363 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
5366 g_assert (cfg->opt & MONO_OPT_CMOV);
5367 g_assert (ins->dreg == ins->sreg1);
5368 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5369 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
5372 g_assert (cfg->opt & MONO_OPT_CMOV);
5373 g_assert (ins->dreg == ins->sreg1);
5374 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5375 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5381 * The two arguments are swapped because the fbranch instructions
5382 * depend on this for the non-sse case to work.
5384 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5388 * FIXME: Get rid of this.
5389 * The two arguments are swapped because the fbranch instructions
5390 * depend on this for the non-sse case to work.
5392 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5396 /* zeroing the register at the start results in
5397 * shorter and faster code (we can also remove the widening op)
5399 guchar *unordered_check;
5401 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5402 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5403 unordered_check = code;
5404 x86_branch8 (code, X86_CC_P, 0, FALSE);
5406 if (ins->opcode == OP_FCEQ) {
5407 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5408 amd64_patch (unordered_check, code);
5410 guchar *jump_to_end;
5411 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5413 x86_jump8 (code, 0);
5414 amd64_patch (unordered_check, code);
5415 amd64_inc_reg (code, ins->dreg);
5416 amd64_patch (jump_to_end, code);
5422 /* zeroing the register at the start results in
5423 * shorter and faster code (we can also remove the widening op)
5425 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5426 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5427 if (ins->opcode == OP_FCLT_UN) {
5428 guchar *unordered_check = code;
5429 guchar *jump_to_end;
5430 x86_branch8 (code, X86_CC_P, 0, FALSE);
5431 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5433 x86_jump8 (code, 0);
5434 amd64_patch (unordered_check, code);
5435 amd64_inc_reg (code, ins->dreg);
5436 amd64_patch (jump_to_end, code);
5438 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5443 guchar *unordered_check;
5444 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5445 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5446 unordered_check = code;
5447 x86_branch8 (code, X86_CC_P, 0, FALSE);
5448 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5449 amd64_patch (unordered_check, code);
5454 /* zeroing the register at the start results in
5455 * shorter and faster code (we can also remove the widening op)
5457 guchar *unordered_check;
5459 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5460 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5461 if (ins->opcode == OP_FCGT) {
5462 unordered_check = code;
5463 x86_branch8 (code, X86_CC_P, 0, FALSE);
5464 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5465 amd64_patch (unordered_check, code);
5467 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5472 guchar *unordered_check;
5473 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5474 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5475 unordered_check = code;
5476 x86_branch8 (code, X86_CC_P, 0, FALSE);
5477 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5478 amd64_patch (unordered_check, code);
5488 gboolean unordered = FALSE;
5490 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5491 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5493 switch (ins->opcode) {
5495 x86_cond = X86_CC_EQ;
5498 x86_cond = X86_CC_LT;
5501 x86_cond = X86_CC_GT;
5504 x86_cond = X86_CC_GT;
5508 x86_cond = X86_CC_LT;
5512 g_assert_not_reached ();
5517 guchar *unordered_check;
5518 guchar *jump_to_end;
5520 unordered_check = code;
5521 x86_branch8 (code, X86_CC_P, 0, FALSE);
5522 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5524 x86_jump8 (code, 0);
5525 amd64_patch (unordered_check, code);
5526 amd64_inc_reg (code, ins->dreg);
5527 amd64_patch (jump_to_end, code);
5529 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5533 case OP_FCLT_MEMBASE:
5534 case OP_FCGT_MEMBASE:
5535 case OP_FCLT_UN_MEMBASE:
5536 case OP_FCGT_UN_MEMBASE:
5537 case OP_FCEQ_MEMBASE: {
5538 guchar *unordered_check, *jump_to_end;
5541 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5542 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5544 switch (ins->opcode) {
5545 case OP_FCEQ_MEMBASE:
5546 x86_cond = X86_CC_EQ;
5548 case OP_FCLT_MEMBASE:
5549 case OP_FCLT_UN_MEMBASE:
5550 x86_cond = X86_CC_LT;
5552 case OP_FCGT_MEMBASE:
5553 case OP_FCGT_UN_MEMBASE:
5554 x86_cond = X86_CC_GT;
5557 g_assert_not_reached ();
5560 unordered_check = code;
5561 x86_branch8 (code, X86_CC_P, 0, FALSE);
5562 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5564 switch (ins->opcode) {
5565 case OP_FCEQ_MEMBASE:
5566 case OP_FCLT_MEMBASE:
5567 case OP_FCGT_MEMBASE:
5568 amd64_patch (unordered_check, code);
5570 case OP_FCLT_UN_MEMBASE:
5571 case OP_FCGT_UN_MEMBASE:
5573 x86_jump8 (code, 0);
5574 amd64_patch (unordered_check, code);
5575 amd64_inc_reg (code, ins->dreg);
5576 amd64_patch (jump_to_end, code);
5584 guchar *jump = code;
5585 x86_branch8 (code, X86_CC_P, 0, TRUE);
5586 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5587 amd64_patch (jump, code);
5591 /* Branch if C013 != 100 */
5592 /* branch if !ZF or (PF|CF) */
5593 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5594 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5595 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5598 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5601 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5602 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5606 if (ins->opcode == OP_FBGT) {
5609 /* skip branch if C1=1 */
5611 x86_branch8 (code, X86_CC_P, 0, FALSE);
5612 /* branch if (C0 | C3) = 1 */
5613 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5614 amd64_patch (br1, code);
5617 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5621 /* Branch if C013 == 100 or 001 */
5624 /* skip branch if C1=1 */
5626 x86_branch8 (code, X86_CC_P, 0, FALSE);
5627 /* branch if (C0 | C3) = 1 */
5628 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5629 amd64_patch (br1, code);
5633 /* Branch if C013 == 000 */
5634 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5637 /* Branch if C013=000 or 100 */
5640 /* skip branch if C1=1 */
5642 x86_branch8 (code, X86_CC_P, 0, FALSE);
5643 /* branch if C0=0 */
5644 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5645 amd64_patch (br1, code);
5649 /* Branch if C013 != 001 */
5650 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5651 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5654 /* Transfer value to the fp stack */
5655 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5656 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5657 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5659 amd64_push_reg (code, AMD64_RAX);
5661 amd64_fnstsw (code);
5662 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5663 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5664 amd64_pop_reg (code, AMD64_RAX);
5665 amd64_fstp (code, 0);
5666 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
5667 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5670 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5673 case OP_TLS_GET_REG:
5674 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5677 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5680 case OP_TLS_SET_REG: {
5681 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5684 case OP_MEMORY_BARRIER: {
5685 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5689 case OP_ATOMIC_ADD_I4:
5690 case OP_ATOMIC_ADD_I8: {
5691 int dreg = ins->dreg;
5692 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5694 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5697 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5698 amd64_prefix (code, X86_LOCK_PREFIX);
5699 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5700 /* dreg contains the old value, add with sreg2 value */
5701 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5703 if (ins->dreg != dreg)
5704 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5708 case OP_ATOMIC_EXCHANGE_I4:
5709 case OP_ATOMIC_EXCHANGE_I8: {
5710 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5712 /* LOCK prefix is implied. */
5713 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5714 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5715 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5718 case OP_ATOMIC_CAS_I4:
5719 case OP_ATOMIC_CAS_I8: {
5722 if (ins->opcode == OP_ATOMIC_CAS_I8)
5728 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5729 * an explanation of how this works.
5731 g_assert (ins->sreg3 == AMD64_RAX);
5732 g_assert (ins->sreg1 != AMD64_RAX);
5733 g_assert (ins->sreg1 != ins->sreg2);
5735 amd64_prefix (code, X86_LOCK_PREFIX);
5736 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
5738 if (ins->dreg != AMD64_RAX)
5739 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
5742 case OP_ATOMIC_LOAD_I1: {
5743 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
5746 case OP_ATOMIC_LOAD_U1: {
5747 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
5750 case OP_ATOMIC_LOAD_I2: {
5751 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
5754 case OP_ATOMIC_LOAD_U2: {
5755 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
5758 case OP_ATOMIC_LOAD_I4: {
5759 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5762 case OP_ATOMIC_LOAD_U4:
5763 case OP_ATOMIC_LOAD_I8:
5764 case OP_ATOMIC_LOAD_U8: {
5765 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
5768 case OP_ATOMIC_LOAD_R4: {
5769 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5770 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5773 case OP_ATOMIC_LOAD_R8: {
5774 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5777 case OP_ATOMIC_STORE_I1:
5778 case OP_ATOMIC_STORE_U1:
5779 case OP_ATOMIC_STORE_I2:
5780 case OP_ATOMIC_STORE_U2:
5781 case OP_ATOMIC_STORE_I4:
5782 case OP_ATOMIC_STORE_U4:
5783 case OP_ATOMIC_STORE_I8:
5784 case OP_ATOMIC_STORE_U8: {
5787 switch (ins->opcode) {
5788 case OP_ATOMIC_STORE_I1:
5789 case OP_ATOMIC_STORE_U1:
5792 case OP_ATOMIC_STORE_I2:
5793 case OP_ATOMIC_STORE_U2:
5796 case OP_ATOMIC_STORE_I4:
5797 case OP_ATOMIC_STORE_U4:
5800 case OP_ATOMIC_STORE_I8:
5801 case OP_ATOMIC_STORE_U8:
5806 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
5808 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5812 case OP_ATOMIC_STORE_R4: {
5813 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5814 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5816 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5820 case OP_ATOMIC_STORE_R8: {
5823 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5827 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5831 case OP_CARD_TABLE_WBARRIER: {
5832 int ptr = ins->sreg1;
5833 int value = ins->sreg2;
5835 int nursery_shift, card_table_shift;
5836 gpointer card_table_mask;
5837 size_t nursery_size;
5839 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
5840 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
5841 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
5843 /*If either point to the stack we can simply avoid the WB. This happens due to
5844 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
5846 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
5850 * We need one register we can clobber, we choose EDX and make sreg1
5851 * fixed EAX to work around limitations in the local register allocator.
5852 * sreg2 might get allocated to EDX, but that is not a problem since
5853 * we use it before clobbering EDX.
5855 g_assert (ins->sreg1 == AMD64_RAX);
5858 * This is the code we produce:
5861 * edx >>= nursery_shift
5862 * cmp edx, (nursery_start >> nursery_shift)
5865 * edx >>= card_table_shift
5871 if (mono_gc_card_table_nursery_check ()) {
5872 if (value != AMD64_RDX)
5873 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
5874 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
5875 if (shifted_nursery_start >> 31) {
5877 * The value we need to compare against is 64 bits, so we need
5878 * another spare register. We use RBX, which we save and
5881 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
5882 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
5883 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
5884 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
5886 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
5888 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
5890 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
5891 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
5892 if (card_table_mask)
5893 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
5895 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
5896 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
5898 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
5900 if (mono_gc_card_table_nursery_check ())
5901 x86_patch (br, code);
5904 #ifdef MONO_ARCH_SIMD_INTRINSICS
5905 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
5907 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
5910 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
5913 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
5916 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
5919 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
5922 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
5925 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
5926 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5929 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
5932 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
5935 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
5938 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
5941 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
5944 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
5947 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
5950 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
5953 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
5956 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
5959 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
5962 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
5965 case OP_PSHUFLEW_HIGH:
5966 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5967 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5969 case OP_PSHUFLEW_LOW:
5970 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5971 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5974 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5975 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5978 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5979 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5982 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
5983 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5987 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
5990 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
5993 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
5996 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
5999 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
6002 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
6005 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6006 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6009 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
6012 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
6015 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
6018 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6021 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6024 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6027 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6030 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6033 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6036 case OP_EXTRACT_MASK:
6037 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6041 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6044 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6047 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6051 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6054 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6057 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6060 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6064 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6067 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6070 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6073 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6077 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6080 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6083 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6087 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6090 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6093 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6097 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6100 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6104 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6107 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6110 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6114 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6117 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6120 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6124 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6127 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6130 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6133 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6137 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6140 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6143 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6146 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6149 case OP_PSUM_ABS_DIFF:
6150 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6153 case OP_UNPACK_LOWB:
6154 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6156 case OP_UNPACK_LOWW:
6157 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6159 case OP_UNPACK_LOWD:
6160 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6162 case OP_UNPACK_LOWQ:
6163 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6165 case OP_UNPACK_LOWPS:
6166 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6168 case OP_UNPACK_LOWPD:
6169 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6172 case OP_UNPACK_HIGHB:
6173 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6175 case OP_UNPACK_HIGHW:
6176 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6178 case OP_UNPACK_HIGHD:
6179 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6181 case OP_UNPACK_HIGHQ:
6182 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6184 case OP_UNPACK_HIGHPS:
6185 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6187 case OP_UNPACK_HIGHPD:
6188 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6192 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6195 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6198 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6201 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6204 case OP_PADDB_SAT_UN:
6205 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6207 case OP_PSUBB_SAT_UN:
6208 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6210 case OP_PADDW_SAT_UN:
6211 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6213 case OP_PSUBW_SAT_UN:
6214 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6218 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6221 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6224 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6227 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6231 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6234 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6237 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6239 case OP_PMULW_HIGH_UN:
6240 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6243 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6247 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6250 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6254 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6257 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6261 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6264 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6268 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6271 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6275 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6278 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6282 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6285 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6289 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6292 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6295 /*TODO: This is appart of the sse spec but not added
6297 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6300 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6305 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6308 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6311 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6314 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6317 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6320 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6323 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6326 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6329 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6332 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6336 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6339 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6343 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6344 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6346 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6351 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6353 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6354 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6358 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6360 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6361 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6362 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6366 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6368 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6371 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6373 case OP_EXTRACTX_U2:
6374 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6376 case OP_INSERTX_U1_SLOW:
6377 /*sreg1 is the extracted ireg (scratch)
6378 /sreg2 is the to be inserted ireg (scratch)
6379 /dreg is the xreg to receive the value*/
6381 /*clear the bits from the extracted word*/
6382 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6383 /*shift the value to insert if needed*/
6384 if (ins->inst_c0 & 1)
6385 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6386 /*join them together*/
6387 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6388 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6390 case OP_INSERTX_I4_SLOW:
6391 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6392 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6393 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6395 case OP_INSERTX_I8_SLOW:
6396 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6398 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6400 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6403 case OP_INSERTX_R4_SLOW:
6404 switch (ins->inst_c0) {
6407 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6409 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6412 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6414 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6416 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6417 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6420 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6422 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6424 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6425 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6428 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6430 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6432 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6433 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6437 case OP_INSERTX_R8_SLOW:
6439 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6441 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6443 case OP_STOREX_MEMBASE_REG:
6444 case OP_STOREX_MEMBASE:
6445 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6447 case OP_LOADX_MEMBASE:
6448 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6450 case OP_LOADX_ALIGNED_MEMBASE:
6451 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6453 case OP_STOREX_ALIGNED_MEMBASE_REG:
6454 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6456 case OP_STOREX_NTA_MEMBASE_REG:
6457 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6459 case OP_PREFETCH_MEMBASE:
6460 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6464 /*FIXME the peephole pass should have killed this*/
6465 if (ins->dreg != ins->sreg1)
6466 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6469 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6471 case OP_ICONV_TO_R4_RAW:
6472 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6475 case OP_FCONV_TO_R8_X:
6476 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6479 case OP_XCONV_R8_TO_I4:
6480 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6481 switch (ins->backend.source_opcode) {
6482 case OP_FCONV_TO_I1:
6483 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6485 case OP_FCONV_TO_U1:
6486 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6488 case OP_FCONV_TO_I2:
6489 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6491 case OP_FCONV_TO_U2:
6492 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6498 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6499 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6500 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6503 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6504 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6507 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6508 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6512 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6514 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6515 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6517 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6520 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6521 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6524 case OP_LIVERANGE_START: {
6525 if (cfg->verbose_level > 1)
6526 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6527 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6530 case OP_LIVERANGE_END: {
6531 if (cfg->verbose_level > 1)
6532 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6533 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6536 case OP_GC_SAFE_POINT: {
6537 const char *polling_func = NULL;
6538 int compare_val = 0;
6541 #if defined (USE_COOP_GC)
6542 polling_func = "mono_threads_state_poll";
6544 #elif defined(__native_client_codegen__) && defined(__native_client_gc__)
6545 polling_func = "mono_nacl_gc";
6546 compare_val = 0xFFFFFFFF;
6551 amd64_test_membase_imm_size (code, ins->sreg1, 0, compare_val, 4);
6552 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6553 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func, FALSE);
6554 amd64_patch (br[0], code);
6558 case OP_GC_LIVENESS_DEF:
6559 case OP_GC_LIVENESS_USE:
6560 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6561 ins->backend.pc_offset = code - cfg->native_code;
6563 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6564 ins->backend.pc_offset = code - cfg->native_code;
6565 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6568 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6569 g_assert_not_reached ();
6572 if ((code - cfg->native_code - offset) > max_len) {
6573 #if !defined(__native_client_codegen__)
6574 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6575 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6576 g_assert_not_reached ();
6581 cfg->code_len = code - cfg->native_code;
6584 #endif /* DISABLE_JIT */
6587 mono_arch_register_lowlevel_calls (void)
6589 /* The signature doesn't matter */
6590 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6594 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
6596 unsigned char *ip = ji->ip.i + code;
6599 * Debug code to help track down problems where the target of a near call is
6602 if (amd64_is_near_call (ip)) {
6603 gint64 disp = (guint8*)target - (guint8*)ip;
6605 if (!amd64_is_imm32 (disp)) {
6606 printf ("TYPE: %d\n", ji->type);
6608 case MONO_PATCH_INFO_INTERNAL_METHOD:
6609 printf ("V: %s\n", ji->data.name);
6611 case MONO_PATCH_INFO_METHOD_JUMP:
6612 case MONO_PATCH_INFO_METHOD:
6613 printf ("V: %s\n", ji->data.method->name);
6621 amd64_patch (ip, (gpointer)target);
6627 get_max_epilog_size (MonoCompile *cfg)
6629 int max_epilog_size = 16;
6631 if (cfg->method->save_lmf)
6632 max_epilog_size += 256;
6634 if (mono_jit_trace_calls != NULL)
6635 max_epilog_size += 50;
6637 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6638 max_epilog_size += 50;
6640 max_epilog_size += (AMD64_NREG * 2);
6642 return max_epilog_size;
6646 * This macro is used for testing whenever the unwinder works correctly at every point
6647 * where an async exception can happen.
6649 /* This will generate a SIGSEGV at the given point in the code */
6650 #define async_exc_point(code) do { \
6651 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6652 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6653 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6654 cfg->arch.async_point_count ++; \
6659 mono_arch_emit_prolog (MonoCompile *cfg)
6661 MonoMethod *method = cfg->method;
6663 MonoMethodSignature *sig;
6665 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6668 MonoInst *lmf_var = cfg->lmf_var;
6669 gboolean args_clobbered = FALSE;
6670 gboolean trace = FALSE;
6671 #ifdef __native_client_codegen__
6672 guint alignment_check;
6675 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6677 #if defined(__default_codegen__)
6678 code = cfg->native_code = g_malloc (cfg->code_size);
6679 #elif defined(__native_client_codegen__)
6680 /* native_code_alloc is not 32-byte aligned, native_code is. */
6681 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6683 /* Align native_code to next nearest kNaclAlignment byte. */
6684 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6685 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6687 code = cfg->native_code;
6689 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6690 g_assert (alignment_check == 0);
6693 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6696 /* Amount of stack space allocated by register saving code */
6699 /* Offset between RSP and the CFA */
6703 * The prolog consists of the following parts:
6705 * - push rbp, mov rbp, rsp
6706 * - save callee saved regs using pushes
6708 * - save rgctx if needed
6709 * - save lmf if needed
6712 * - save rgctx if needed
6713 * - save lmf if needed
6714 * - save callee saved regs using moves
6719 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6720 // IP saved at CFA - 8
6721 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6722 async_exc_point (code);
6723 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6725 if (!cfg->arch.omit_fp) {
6726 amd64_push_reg (code, AMD64_RBP);
6728 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6729 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6730 async_exc_point (code);
6732 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6734 /* These are handled automatically by the stack marking code */
6735 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6737 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
6738 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
6739 async_exc_point (code);
6741 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6745 /* The param area is always at offset 0 from sp */
6746 /* This needs to be allocated here, since it has to come after the spill area */
6747 if (cfg->param_area) {
6748 if (cfg->arch.omit_fp)
6750 g_assert_not_reached ();
6751 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
6754 if (cfg->arch.omit_fp) {
6756 * On enter, the stack is misaligned by the pushing of the return
6757 * address. It is either made aligned by the pushing of %rbp, or by
6760 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
6761 if ((alloc_size % 16) == 0) {
6763 /* Mark the padding slot as NOREF */
6764 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
6767 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
6768 if (cfg->stack_offset != alloc_size) {
6769 /* Mark the padding slot as NOREF */
6770 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
6772 cfg->arch.sp_fp_offset = alloc_size;
6776 cfg->arch.stack_alloc_size = alloc_size;
6778 /* Allocate stack frame */
6780 /* See mono_emit_stack_alloc */
6781 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
6782 guint32 remaining_size = alloc_size;
6783 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
6784 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
6785 guint32 offset = code - cfg->native_code;
6786 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
6787 while (required_code_size >= (cfg->code_size - offset))
6788 cfg->code_size *= 2;
6789 cfg->native_code = mono_realloc_native_code (cfg);
6790 code = cfg->native_code + offset;
6791 cfg->stat_code_reallocs++;
6794 while (remaining_size >= 0x1000) {
6795 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
6796 if (cfg->arch.omit_fp) {
6797 cfa_offset += 0x1000;
6798 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6800 async_exc_point (code);
6802 if (cfg->arch.omit_fp)
6803 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
6806 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
6807 remaining_size -= 0x1000;
6809 if (remaining_size) {
6810 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
6811 if (cfg->arch.omit_fp) {
6812 cfa_offset += remaining_size;
6813 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6814 async_exc_point (code);
6817 if (cfg->arch.omit_fp)
6818 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
6822 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
6823 if (cfg->arch.omit_fp) {
6824 cfa_offset += alloc_size;
6825 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6826 async_exc_point (code);
6831 /* Stack alignment check */
6834 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
6835 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
6836 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
6837 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
6838 amd64_breakpoint (code);
6842 if (mini_get_debug_options ()->init_stacks) {
6843 /* Fill the stack frame with a dummy value to force deterministic behavior */
6845 /* Save registers to the red zone */
6846 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
6847 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
6849 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
6850 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
6851 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
6854 #if defined(__default_codegen__)
6855 amd64_prefix (code, X86_REP_PREFIX);
6857 #elif defined(__native_client_codegen__)
6858 /* NaCl stos pseudo-instruction */
6859 amd64_codegen_pre (code);
6860 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
6861 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
6862 /* Add %r15 to %rdi using lea, condition flags unaffected. */
6863 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
6864 amd64_prefix (code, X86_REP_PREFIX);
6866 amd64_codegen_post (code);
6867 #endif /* __native_client_codegen__ */
6869 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
6870 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
6874 if (method->save_lmf)
6875 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
6877 /* Save callee saved registers */
6878 if (cfg->arch.omit_fp) {
6879 save_area_offset = cfg->arch.reg_save_area_offset;
6880 /* Save caller saved registers after sp is adjusted */
6881 /* The registers are saved at the bottom of the frame */
6882 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
6884 /* The registers are saved just below the saved rbp */
6885 save_area_offset = cfg->arch.reg_save_area_offset;
6888 for (i = 0; i < AMD64_NREG; ++i) {
6889 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
6890 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
6892 if (cfg->arch.omit_fp) {
6893 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
6894 /* These are handled automatically by the stack marking code */
6895 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
6897 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
6901 save_area_offset += 8;
6902 async_exc_point (code);
6906 /* store runtime generic context */
6907 if (cfg->rgctx_var) {
6908 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
6909 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
6911 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
6913 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
6914 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
6917 /* compute max_length in order to use short forward jumps */
6918 max_epilog_size = get_max_epilog_size (cfg);
6919 if (cfg->opt & MONO_OPT_BRANCH) {
6920 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6924 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6926 /* max alignment for loops */
6927 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
6928 max_length += LOOP_ALIGNMENT;
6929 #ifdef __native_client_codegen__
6930 /* max alignment for native client */
6931 max_length += kNaClAlignment;
6934 MONO_BB_FOR_EACH_INS (bb, ins) {
6935 #ifdef __native_client_codegen__
6937 int space_in_block = kNaClAlignment -
6938 ((max_length + cfg->code_len) & kNaClAlignmentMask);
6939 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
6940 if (space_in_block < max_len && max_len < kNaClAlignment) {
6941 max_length += space_in_block;
6944 #endif /*__native_client_codegen__*/
6945 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
6948 /* Take prolog and epilog instrumentation into account */
6949 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
6950 max_length += max_epilog_size;
6952 bb->max_length = max_length;
6956 sig = mono_method_signature (method);
6959 cinfo = cfg->arch.cinfo;
6961 if (sig->ret->type != MONO_TYPE_VOID) {
6962 /* Save volatile arguments to the stack */
6963 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
6964 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
6967 /* Keep this in sync with emit_load_volatile_arguments */
6968 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6969 ArgInfo *ainfo = cinfo->args + i;
6971 ins = cfg->args [i];
6973 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
6974 /* Unused arguments */
6977 /* Save volatile arguments to the stack */
6978 if (ins->opcode != OP_REGVAR) {
6979 switch (ainfo->storage) {
6985 if (stack_offset & 0x1)
6987 else if (stack_offset & 0x2)
6989 else if (stack_offset & 0x4)
6994 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
6997 * Save the original location of 'this',
6998 * get_generic_info_from_stack_frame () needs this to properly look up
6999 * the argument value during the handling of async exceptions.
7001 if (ins == cfg->args [0]) {
7002 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7003 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
7007 case ArgInFloatSSEReg:
7008 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7010 case ArgInDoubleSSEReg:
7011 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7013 case ArgValuetypeInReg:
7014 for (quad = 0; quad < 2; quad ++) {
7015 switch (ainfo->pair_storage [quad]) {
7017 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7019 case ArgInFloatSSEReg:
7020 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7022 case ArgInDoubleSSEReg:
7023 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7028 g_assert_not_reached ();
7032 case ArgValuetypeAddrInIReg:
7033 if (ainfo->pair_storage [0] == ArgInIReg)
7034 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7040 /* Argument allocated to (non-volatile) register */
7041 switch (ainfo->storage) {
7043 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7046 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7049 g_assert_not_reached ();
7052 if (ins == cfg->args [0]) {
7053 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7054 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7059 if (cfg->method->save_lmf)
7060 args_clobbered = TRUE;
7063 args_clobbered = TRUE;
7064 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7067 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7068 args_clobbered = TRUE;
7071 * Optimize the common case of the first bblock making a call with the same
7072 * arguments as the method. This works because the arguments are still in their
7073 * original argument registers.
7074 * FIXME: Generalize this
7076 if (!args_clobbered) {
7077 MonoBasicBlock *first_bb = cfg->bb_entry;
7079 int filter = FILTER_IL_SEQ_POINT;
7081 next = mono_bb_first_inst (first_bb, filter);
7082 if (!next && first_bb->next_bb) {
7083 first_bb = first_bb->next_bb;
7084 next = mono_bb_first_inst (first_bb, filter);
7087 if (first_bb->in_count > 1)
7090 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7091 ArgInfo *ainfo = cinfo->args + i;
7092 gboolean match = FALSE;
7094 ins = cfg->args [i];
7095 if (ins->opcode != OP_REGVAR) {
7096 switch (ainfo->storage) {
7098 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7099 if (next->dreg == ainfo->reg) {
7103 next->opcode = OP_MOVE;
7104 next->sreg1 = ainfo->reg;
7105 /* Only continue if the instruction doesn't change argument regs */
7106 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7116 /* Argument allocated to (non-volatile) register */
7117 switch (ainfo->storage) {
7119 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7130 next = mono_inst_next (next, filter);
7131 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7138 if (cfg->gen_sdb_seq_points) {
7139 MonoInst *info_var = cfg->arch.seq_point_info_var;
7141 /* Initialize seq_point_info_var */
7142 if (cfg->compile_aot) {
7143 /* Initialize the variable from a GOT slot */
7144 /* Same as OP_AOTCONST */
7145 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7146 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7147 g_assert (info_var->opcode == OP_REGOFFSET);
7148 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7151 if (cfg->compile_aot) {
7152 /* Initialize ss_tramp_var */
7153 ins = cfg->arch.ss_tramp_var;
7154 g_assert (ins->opcode == OP_REGOFFSET);
7156 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7157 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr), 8);
7158 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7160 /* Initialize ss_trigger_page_var */
7161 ins = cfg->arch.ss_trigger_page_var;
7163 g_assert (ins->opcode == OP_REGOFFSET);
7165 amd64_mov_reg_imm (code, AMD64_R11, (guint64)ss_trigger_page);
7166 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7170 cfg->code_len = code - cfg->native_code;
7172 g_assert (cfg->code_len < cfg->code_size);
7178 mono_arch_emit_epilog (MonoCompile *cfg)
7180 MonoMethod *method = cfg->method;
7183 int max_epilog_size;
7185 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7186 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7188 max_epilog_size = get_max_epilog_size (cfg);
7190 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7191 cfg->code_size *= 2;
7192 cfg->native_code = mono_realloc_native_code (cfg);
7193 cfg->stat_code_reallocs++;
7195 code = cfg->native_code + cfg->code_len;
7197 cfg->has_unwind_info_for_epilog = TRUE;
7199 /* Mark the start of the epilog */
7200 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7202 /* Save the uwind state which is needed by the out-of-line code */
7203 mono_emit_unwind_op_remember_state (cfg, code);
7205 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7206 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7208 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7210 if (method->save_lmf) {
7211 /* check if we need to restore protection of the stack after a stack overflow */
7212 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7214 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7215 /* we load the value in a separate instruction: this mechanism may be
7216 * used later as a safer way to do thread interruption
7218 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7219 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7221 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7222 /* note that the call trampoline will preserve eax/edx */
7223 x86_call_reg (code, X86_ECX);
7224 x86_patch (patch, code);
7226 /* FIXME: maybe save the jit tls in the prolog */
7228 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7229 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7233 /* Restore callee saved regs */
7234 for (i = 0; i < AMD64_NREG; ++i) {
7235 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7236 /* Restore only used_int_regs, not arch.saved_iregs */
7237 if (cfg->used_int_regs & (1 << i)) {
7238 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7239 mono_emit_unwind_op_same_value (cfg, code, i);
7240 async_exc_point (code);
7242 save_area_offset += 8;
7246 /* Load returned vtypes into registers if needed */
7247 cinfo = cfg->arch.cinfo;
7248 if (cinfo->ret.storage == ArgValuetypeInReg) {
7249 ArgInfo *ainfo = &cinfo->ret;
7250 MonoInst *inst = cfg->ret;
7252 for (quad = 0; quad < 2; quad ++) {
7253 switch (ainfo->pair_storage [quad]) {
7255 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7257 case ArgInFloatSSEReg:
7258 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7260 case ArgInDoubleSSEReg:
7261 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7266 g_assert_not_reached ();
7271 if (cfg->arch.omit_fp) {
7272 if (cfg->arch.stack_alloc_size) {
7273 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7277 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7279 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7280 async_exc_point (code);
7283 /* Restore the unwind state to be the same as before the epilog */
7284 mono_emit_unwind_op_restore_state (cfg, code);
7286 cfg->code_len = code - cfg->native_code;
7288 g_assert (cfg->code_len < cfg->code_size);
7292 mono_arch_emit_exceptions (MonoCompile *cfg)
7294 MonoJumpInfo *patch_info;
7297 MonoClass *exc_classes [16];
7298 guint8 *exc_throw_start [16], *exc_throw_end [16];
7299 guint32 code_size = 0;
7301 /* Compute needed space */
7302 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7303 if (patch_info->type == MONO_PATCH_INFO_EXC)
7305 if (patch_info->type == MONO_PATCH_INFO_R8)
7306 code_size += 8 + 15; /* sizeof (double) + alignment */
7307 if (patch_info->type == MONO_PATCH_INFO_R4)
7308 code_size += 4 + 15; /* sizeof (float) + alignment */
7309 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7310 code_size += 8 + 7; /*sizeof (void*) + alignment */
7313 #ifdef __native_client_codegen__
7314 /* Give us extra room on Native Client. This could be */
7315 /* more carefully calculated, but bundle alignment makes */
7316 /* it much trickier, so *2 like other places is good. */
7320 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7321 cfg->code_size *= 2;
7322 cfg->native_code = mono_realloc_native_code (cfg);
7323 cfg->stat_code_reallocs++;
7326 code = cfg->native_code + cfg->code_len;
7328 /* add code to raise exceptions */
7330 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7331 switch (patch_info->type) {
7332 case MONO_PATCH_INFO_EXC: {
7333 MonoClass *exc_class;
7337 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7339 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7340 g_assert (exc_class);
7341 throw_ip = patch_info->ip.i;
7343 //x86_breakpoint (code);
7344 /* Find a throw sequence for the same exception class */
7345 for (i = 0; i < nthrows; ++i)
7346 if (exc_classes [i] == exc_class)
7349 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7350 x86_jump_code (code, exc_throw_start [i]);
7351 patch_info->type = MONO_PATCH_INFO_NONE;
7355 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7359 exc_classes [nthrows] = exc_class;
7360 exc_throw_start [nthrows] = code;
7362 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7364 patch_info->type = MONO_PATCH_INFO_NONE;
7366 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7368 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7373 exc_throw_end [nthrows] = code;
7383 g_assert(code < cfg->native_code + cfg->code_size);
7386 /* Handle relocations with RIP relative addressing */
7387 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7388 gboolean remove = FALSE;
7389 guint8 *orig_code = code;
7391 switch (patch_info->type) {
7392 case MONO_PATCH_INFO_R8:
7393 case MONO_PATCH_INFO_R4: {
7394 guint8 *pos, *patch_pos;
7397 /* The SSE opcodes require a 16 byte alignment */
7398 #if defined(__default_codegen__)
7399 code = (guint8*)ALIGN_TO (code, 16);
7400 #elif defined(__native_client_codegen__)
7402 /* Pad this out with HLT instructions */
7403 /* or we can get garbage bytes emitted */
7404 /* which will fail validation */
7405 guint8 *aligned_code;
7406 /* extra align to make room for */
7407 /* mov/push below */
7408 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7409 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7410 /* The technique of hiding data in an */
7411 /* instruction has a problem here: we */
7412 /* need the data aligned to a 16-byte */
7413 /* boundary but the instruction cannot */
7414 /* cross the bundle boundary. so only */
7415 /* odd multiples of 16 can be used */
7416 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7419 while (code < aligned_code) {
7420 *(code++) = 0xf4; /* hlt */
7425 pos = cfg->native_code + patch_info->ip.i;
7426 if (IS_REX (pos [1])) {
7427 patch_pos = pos + 5;
7428 target_pos = code - pos - 9;
7431 patch_pos = pos + 4;
7432 target_pos = code - pos - 8;
7435 if (patch_info->type == MONO_PATCH_INFO_R8) {
7436 #ifdef __native_client_codegen__
7437 /* Hide 64-bit data in a */
7438 /* "mov imm64, r11" instruction. */
7439 /* write it before the start of */
7441 *(code-2) = 0x49; /* prefix */
7442 *(code-1) = 0xbb; /* mov X, %r11 */
7444 *(double*)code = *(double*)patch_info->data.target;
7445 code += sizeof (double);
7447 #ifdef __native_client_codegen__
7448 /* Hide 32-bit data in a */
7449 /* "push imm32" instruction. */
7450 *(code-1) = 0x68; /* push */
7452 *(float*)code = *(float*)patch_info->data.target;
7453 code += sizeof (float);
7456 *(guint32*)(patch_pos) = target_pos;
7461 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7464 if (cfg->compile_aot)
7467 /*loading is faster against aligned addresses.*/
7468 code = (guint8*)ALIGN_TO (code, 8);
7469 memset (orig_code, 0, code - orig_code);
7471 pos = cfg->native_code + patch_info->ip.i;
7473 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7474 if (IS_REX (pos [1]))
7475 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7477 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7479 *(gpointer*)code = (gpointer)patch_info->data.target;
7480 code += sizeof (gpointer);
7490 if (patch_info == cfg->patch_info)
7491 cfg->patch_info = patch_info->next;
7495 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7497 tmp->next = patch_info->next;
7500 g_assert (code < cfg->native_code + cfg->code_size);
7503 cfg->code_len = code - cfg->native_code;
7505 g_assert (cfg->code_len < cfg->code_size);
7509 #endif /* DISABLE_JIT */
7512 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7515 MonoMethodSignature *sig;
7517 int i, n, stack_area = 0;
7519 /* Keep this in sync with mono_arch_get_argument_info */
7521 if (enable_arguments) {
7522 /* Allocate a new area on the stack and save arguments there */
7523 sig = mono_method_signature (cfg->method);
7525 n = sig->param_count + sig->hasthis;
7527 stack_area = ALIGN_TO (n * 8, 16);
7529 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7531 for (i = 0; i < n; ++i) {
7532 inst = cfg->args [i];
7534 if (inst->opcode == OP_REGVAR)
7535 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7537 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7538 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7543 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7544 amd64_set_reg_template (code, AMD64_ARG_REG1);
7545 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7546 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7548 if (enable_arguments)
7549 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7563 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7566 int save_mode = SAVE_NONE;
7567 MonoMethod *method = cfg->method;
7568 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
7571 switch (ret_type->type) {
7572 case MONO_TYPE_VOID:
7573 /* special case string .ctor icall */
7574 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7575 save_mode = SAVE_EAX;
7577 save_mode = SAVE_NONE;
7581 save_mode = SAVE_EAX;
7585 save_mode = SAVE_XMM;
7587 case MONO_TYPE_GENERICINST:
7588 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7589 save_mode = SAVE_EAX;
7593 case MONO_TYPE_VALUETYPE:
7594 save_mode = SAVE_STRUCT;
7597 save_mode = SAVE_EAX;
7601 /* Save the result and copy it into the proper argument register */
7602 switch (save_mode) {
7604 amd64_push_reg (code, AMD64_RAX);
7606 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7607 if (enable_arguments)
7608 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7612 if (enable_arguments)
7613 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7616 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7617 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7619 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7621 * The result is already in the proper argument register so no copying
7628 g_assert_not_reached ();
7631 /* Set %al since this is a varargs call */
7632 if (save_mode == SAVE_XMM)
7633 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7635 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7637 if (preserve_argument_registers) {
7638 for (i = 0; i < PARAM_REGS; ++i)
7639 amd64_push_reg (code, param_regs [i]);
7642 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7643 amd64_set_reg_template (code, AMD64_ARG_REG1);
7644 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7646 if (preserve_argument_registers) {
7647 for (i = PARAM_REGS - 1; i >= 0; --i)
7648 amd64_pop_reg (code, param_regs [i]);
7651 /* Restore result */
7652 switch (save_mode) {
7654 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7655 amd64_pop_reg (code, AMD64_RAX);
7661 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7662 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7663 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7668 g_assert_not_reached ();
7675 mono_arch_flush_icache (guint8 *code, gint size)
7681 mono_arch_flush_register_windows (void)
7686 mono_arch_is_inst_imm (gint64 imm)
7688 return amd64_use_imm32 (imm);
7692 * Determine whenever the trap whose info is in SIGINFO is caused by
7696 mono_arch_is_int_overflow (void *sigctx, void *info)
7703 mono_sigctx_to_monoctx (sigctx, &ctx);
7705 rip = (guint8*)ctx.gregs [AMD64_RIP];
7707 if (IS_REX (rip [0])) {
7708 reg = amd64_rex_b (rip [0]);
7714 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7716 reg += x86_modrm_rm (rip [1]);
7718 value = ctx.gregs [reg];
7728 mono_arch_get_patch_offset (guint8 *code)
7734 * mono_breakpoint_clean_code:
7736 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
7737 * breakpoints in the original code, they are removed in the copy.
7739 * Returns TRUE if no sw breakpoint was present.
7742 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
7745 * If method_start is non-NULL we need to perform bound checks, since we access memory
7746 * at code - offset we could go before the start of the method and end up in a different
7747 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
7750 if (!method_start || code - offset >= method_start) {
7751 memcpy (buf, code - offset, size);
7753 int diff = code - method_start;
7754 memset (buf, 0, size);
7755 memcpy (buf + offset - diff, method_start, diff + size - offset);
7760 #if defined(__native_client_codegen__)
7761 /* For membase calls, we want the base register. for Native Client, */
7762 /* all indirect calls have the following sequence with the given sizes: */
7763 /* mov %eXX,%eXX [2-3] */
7764 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
7765 /* and $0xffffffffffffffe0,%r11d [4] */
7766 /* add %r15,%r11 [3] */
7767 /* callq *%r11 [3] */
7770 /* Determine if code points to a NaCl call-through-register sequence, */
7771 /* (i.e., the last 3 instructions listed above) */
7773 is_nacl_call_reg_sequence(guint8* code)
7775 const char *sequence = "\x41\x83\xe3\xe0" /* and */
7776 "\x4d\x03\xdf" /* add */
7777 "\x41\xff\xd3"; /* call */
7778 return memcmp(code, sequence, 10) == 0;
7781 /* Determine if code points to the first opcode of the mov membase component */
7782 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
7783 /* (there could be a REX prefix before the opcode but it is ignored) */
7785 is_nacl_indirect_call_membase_sequence(guint8* code)
7787 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
7788 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
7789 /* and that src reg = dest reg */
7790 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
7791 /* Check that next inst is mov, uses SIB byte (rm = 4), */
7793 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
7794 /* and has dst of r11 and base of r15 */
7795 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
7796 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
7798 #endif /* __native_client_codegen__ */
7801 mono_arch_get_this_arg_reg (guint8 *code)
7803 return AMD64_ARG_REG1;
7807 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
7809 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
7812 #define MAX_ARCH_DELEGATE_PARAMS 10
7815 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, guint32 param_count)
7817 guint8 *code, *start;
7818 GSList *unwind_ops = NULL;
7821 unwind_ops = mono_arch_get_cie_program ();
7824 start = code = mono_global_codeman_reserve (64);
7826 /* Replace the this argument with the target */
7827 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7828 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
7829 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7831 g_assert ((code - start) < 64);
7833 start = code = mono_global_codeman_reserve (64);
7835 if (param_count == 0) {
7836 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7838 /* We have to shift the arguments left */
7839 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7840 for (i = 0; i < param_count; ++i) {
7843 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7845 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
7847 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7851 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7853 g_assert ((code - start) < 64);
7856 nacl_global_codeman_validate (&start, 64, &code);
7857 mono_arch_flush_icache (start, code - start);
7860 *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
7862 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
7863 *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
7867 if (mono_jit_map_is_enabled ()) {
7870 buff = (char*)"delegate_invoke_has_target";
7872 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
7873 mono_emit_jit_tramp (start, code - start, buff);
7877 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
7882 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
7885 get_delegate_virtual_invoke_impl (MonoTrampInfo **info, gboolean load_imt_reg, int offset)
7887 guint8 *code, *start;
7892 if (offset / (int)sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
7895 start = code = mono_global_codeman_reserve (size);
7897 unwind_ops = mono_arch_get_cie_program ();
7899 /* Replace the this argument with the target */
7900 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7901 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
7904 /* Load the IMT reg */
7905 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
7908 /* Load the vtable */
7909 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
7910 amd64_jump_membase (code, AMD64_RAX, offset);
7911 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
7914 tramp_name = g_strdup_printf ("delegate_virtual_invoke_imt_%d", - offset / sizeof (gpointer));
7916 tramp_name = g_strdup_printf ("delegate_virtual_invoke_%d", offset / sizeof (gpointer));
7917 *info = mono_tramp_info_create (tramp_name, start, code - start, NULL, unwind_ops);
7918 g_free (tramp_name);
7924 * mono_arch_get_delegate_invoke_impls:
7926 * Return a list of MonoTrampInfo structures for the delegate invoke impl
7930 mono_arch_get_delegate_invoke_impls (void)
7933 MonoTrampInfo *info;
7936 get_delegate_invoke_impl (&info, TRUE, 0);
7937 res = g_slist_prepend (res, info);
7939 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
7940 get_delegate_invoke_impl (&info, FALSE, i);
7941 res = g_slist_prepend (res, info);
7944 for (i = 0; i <= MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
7945 get_delegate_virtual_invoke_impl (&info, TRUE, - i * SIZEOF_VOID_P);
7946 res = g_slist_prepend (res, info);
7948 get_delegate_virtual_invoke_impl (&info, FALSE, i * SIZEOF_VOID_P);
7949 res = g_slist_prepend (res, info);
7956 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
7958 guint8 *code, *start;
7961 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
7964 /* FIXME: Support more cases */
7965 if (MONO_TYPE_ISSTRUCT (mini_get_underlying_type (sig->ret)))
7969 static guint8* cached = NULL;
7974 if (mono_aot_only) {
7975 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
7977 MonoTrampInfo *info;
7978 start = get_delegate_invoke_impl (&info, TRUE, 0);
7979 mono_tramp_info_register (info, NULL);
7982 mono_memory_barrier ();
7986 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
7987 for (i = 0; i < sig->param_count; ++i)
7988 if (!mono_is_regsize_var (sig->params [i]))
7990 if (sig->param_count > 4)
7993 code = cache [sig->param_count];
7997 if (mono_aot_only) {
7998 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
7999 start = mono_aot_get_trampoline (name);
8002 MonoTrampInfo *info;
8003 start = get_delegate_invoke_impl (&info, FALSE, sig->param_count);
8004 mono_tramp_info_register (info, NULL);
8007 mono_memory_barrier ();
8009 cache [sig->param_count] = start;
8016 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
8018 MonoTrampInfo *info;
8021 code = get_delegate_virtual_invoke_impl (&info, load_imt_reg, offset);
8023 mono_tramp_info_register (info, NULL);
8028 mono_arch_finish_init (void)
8030 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
8031 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
8036 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8040 #if defined(__default_codegen__)
8041 #define CMP_SIZE (6 + 1)
8042 #define CMP_REG_REG_SIZE (4 + 1)
8043 #define BR_SMALL_SIZE 2
8044 #define BR_LARGE_SIZE 6
8045 #define MOV_REG_IMM_SIZE 10
8046 #define MOV_REG_IMM_32BIT_SIZE 6
8047 #define JUMP_REG_SIZE (2 + 1)
8048 #elif defined(__native_client_codegen__)
8049 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8050 #define CMP_SIZE ((6 + 1) * 2 - 1)
8051 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8052 #define BR_SMALL_SIZE (2 * 2 - 1)
8053 #define BR_LARGE_SIZE (6 * 2 - 1)
8054 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8055 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8056 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8057 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8058 /* Jump membase's size is large and unpredictable */
8059 /* in native client, just pad it out a whole bundle. */
8060 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8064 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8066 int i, distance = 0;
8067 for (i = start; i < target; ++i)
8068 distance += imt_entries [i]->chunk_size;
8073 * LOCKING: called with the domain lock held
8076 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8077 gpointer fail_tramp)
8081 guint8 *code, *start;
8082 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8085 for (i = 0; i < count; ++i) {
8086 MonoIMTCheckItem *item = imt_entries [i];
8087 if (item->is_equals) {
8088 if (item->check_target_idx) {
8089 if (!item->compare_done) {
8090 if (amd64_use_imm32 ((gint64)item->key))
8091 item->chunk_size += CMP_SIZE;
8093 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8095 if (item->has_target_code) {
8096 item->chunk_size += MOV_REG_IMM_SIZE;
8098 if (vtable_is_32bit)
8099 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8101 item->chunk_size += MOV_REG_IMM_SIZE;
8102 #ifdef __native_client_codegen__
8103 item->chunk_size += JUMP_MEMBASE_SIZE;
8106 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8109 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8110 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8112 if (vtable_is_32bit)
8113 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8115 item->chunk_size += MOV_REG_IMM_SIZE;
8116 item->chunk_size += JUMP_REG_SIZE;
8117 /* with assert below:
8118 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8120 #ifdef __native_client_codegen__
8121 item->chunk_size += JUMP_MEMBASE_SIZE;
8126 if (amd64_use_imm32 ((gint64)item->key))
8127 item->chunk_size += CMP_SIZE;
8129 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8130 item->chunk_size += BR_LARGE_SIZE;
8131 imt_entries [item->check_target_idx]->compare_done = TRUE;
8133 size += item->chunk_size;
8135 #if defined(__native_client__) && defined(__native_client_codegen__)
8136 /* In Native Client, we don't re-use thunks, allocate from the */
8137 /* normal code manager paths. */
8138 code = mono_domain_code_reserve (domain, size);
8141 code = mono_method_alloc_generic_virtual_thunk (domain, size);
8143 code = mono_domain_code_reserve (domain, size);
8147 unwind_ops = mono_arch_get_cie_program ();
8149 for (i = 0; i < count; ++i) {
8150 MonoIMTCheckItem *item = imt_entries [i];
8151 item->code_target = code;
8152 if (item->is_equals) {
8153 gboolean fail_case = !item->check_target_idx && fail_tramp;
8155 if (item->check_target_idx || fail_case) {
8156 if (!item->compare_done || fail_case) {
8157 if (amd64_use_imm32 ((gint64)item->key))
8158 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8160 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof(gpointer));
8161 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8164 item->jmp_code = code;
8165 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8166 if (item->has_target_code) {
8167 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8168 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8170 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8171 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8175 amd64_patch (item->jmp_code, code);
8176 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8177 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8178 item->jmp_code = NULL;
8181 /* enable the commented code to assert on wrong method */
8183 if (amd64_is_imm32 (item->key))
8184 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8186 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8187 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8189 item->jmp_code = code;
8190 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8191 /* See the comment below about R10 */
8192 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8193 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8194 amd64_patch (item->jmp_code, code);
8195 amd64_breakpoint (code);
8196 item->jmp_code = NULL;
8198 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8199 needs to be preserved. R10 needs
8200 to be preserved for calls which
8201 require a runtime generic context,
8202 but interface calls don't. */
8203 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8204 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8208 if (amd64_use_imm32 ((gint64)item->key))
8209 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8211 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof (gpointer));
8212 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8214 item->jmp_code = code;
8215 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8216 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8218 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8220 g_assert (code - item->code_target <= item->chunk_size);
8222 /* patch the branches to get to the target items */
8223 for (i = 0; i < count; ++i) {
8224 MonoIMTCheckItem *item = imt_entries [i];
8225 if (item->jmp_code) {
8226 if (item->check_target_idx) {
8227 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8233 mono_stats.imt_thunks_size += code - start;
8234 g_assert (code - start <= size);
8236 nacl_domain_code_validate(domain, &start, size, &code);
8237 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8239 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
8245 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8247 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8251 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8253 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8257 mono_arch_get_cie_program (void)
8261 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8262 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8270 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8272 MonoInst *ins = NULL;
8275 if (cmethod->klass == mono_defaults.math_class) {
8276 if (strcmp (cmethod->name, "Sin") == 0) {
8278 } else if (strcmp (cmethod->name, "Cos") == 0) {
8280 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8282 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8286 if (opcode && fsig->param_count == 1) {
8287 MONO_INST_NEW (cfg, ins, opcode);
8288 ins->type = STACK_R8;
8289 ins->dreg = mono_alloc_freg (cfg);
8290 ins->sreg1 = args [0]->dreg;
8291 MONO_ADD_INS (cfg->cbb, ins);
8295 if (cfg->opt & MONO_OPT_CMOV) {
8296 if (strcmp (cmethod->name, "Min") == 0) {
8297 if (fsig->params [0]->type == MONO_TYPE_I4)
8299 if (fsig->params [0]->type == MONO_TYPE_U4)
8300 opcode = OP_IMIN_UN;
8301 else if (fsig->params [0]->type == MONO_TYPE_I8)
8303 else if (fsig->params [0]->type == MONO_TYPE_U8)
8304 opcode = OP_LMIN_UN;
8305 } else if (strcmp (cmethod->name, "Max") == 0) {
8306 if (fsig->params [0]->type == MONO_TYPE_I4)
8308 if (fsig->params [0]->type == MONO_TYPE_U4)
8309 opcode = OP_IMAX_UN;
8310 else if (fsig->params [0]->type == MONO_TYPE_I8)
8312 else if (fsig->params [0]->type == MONO_TYPE_U8)
8313 opcode = OP_LMAX_UN;
8317 if (opcode && fsig->param_count == 2) {
8318 MONO_INST_NEW (cfg, ins, opcode);
8319 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8320 ins->dreg = mono_alloc_ireg (cfg);
8321 ins->sreg1 = args [0]->dreg;
8322 ins->sreg2 = args [1]->dreg;
8323 MONO_ADD_INS (cfg->cbb, ins);
8327 /* OP_FREM is not IEEE compatible */
8328 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8329 MONO_INST_NEW (cfg, ins, OP_FREM);
8330 ins->inst_i0 = args [0];
8331 ins->inst_i1 = args [1];
8341 mono_arch_print_tree (MonoInst *tree, int arity)
8347 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8349 return ctx->gregs [reg];
8353 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8355 ctx->gregs [reg] = val;
8359 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8361 gpointer *sp, old_value;
8365 bp = MONO_CONTEXT_GET_BP (ctx);
8366 sp = *(gpointer*)(bp + clause->exvar_offset);
8369 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8378 * mono_arch_emit_load_aotconst:
8380 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8381 * TARGET from the mscorlib GOT in full-aot code.
8382 * On AMD64, the result is placed into R11.
8385 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
8387 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8388 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8394 * mono_arch_get_trampolines:
8396 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8400 mono_arch_get_trampolines (gboolean aot)
8402 return mono_amd64_get_exception_trampolines (aot);
8405 /* Soft Debug support */
8406 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8409 * mono_arch_set_breakpoint:
8411 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8412 * The location should contain code emitted by OP_SEQ_POINT.
8415 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8418 guint8 *orig_code = code;
8421 guint32 native_offset = ip - (guint8*)ji->code_start;
8422 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8424 g_assert (info->bp_addrs [native_offset] == 0);
8425 info->bp_addrs [native_offset] = mini_get_breakpoint_trampoline ();
8428 * In production, we will use int3 (has to fix the size in the md
8429 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
8432 g_assert (code [0] == 0x90);
8433 if (breakpoint_size == 8) {
8434 amd64_mov_reg_mem (code, AMD64_R11, (guint64)bp_trigger_page, 4);
8436 amd64_mov_reg_imm_size (code, AMD64_R11, (guint64)bp_trigger_page, 8);
8437 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 4);
8440 g_assert (code - orig_code == breakpoint_size);
8445 * mono_arch_clear_breakpoint:
8447 * Clear the breakpoint at IP.
8450 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8456 guint32 native_offset = ip - (guint8*)ji->code_start;
8457 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8459 info->bp_addrs [native_offset] = NULL;
8461 for (i = 0; i < breakpoint_size; ++i)
8467 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8470 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8471 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == bp_trigger_page)
8476 siginfo_t* sinfo = (siginfo_t*) info;
8477 /* Sometimes the address is off by 4 */
8478 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
8486 * mono_arch_skip_breakpoint:
8488 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8489 * we resume, the instruction is not executed again.
8492 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8495 /* The breakpoint instruction is a call */
8497 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + breakpoint_fault_size);
8502 * mono_arch_start_single_stepping:
8504 * Start single stepping.
8507 mono_arch_start_single_stepping (void)
8509 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
8510 ss_trampoline = mini_get_single_step_trampoline ();
8514 * mono_arch_stop_single_stepping:
8516 * Stop single stepping.
8519 mono_arch_stop_single_stepping (void)
8521 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
8522 ss_trampoline = NULL;
8526 * mono_arch_is_single_step_event:
8528 * Return whenever the machine state in SIGCTX corresponds to a single
8532 mono_arch_is_single_step_event (void *info, void *sigctx)
8535 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8536 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == ss_trigger_page)
8541 siginfo_t* sinfo = (siginfo_t*) info;
8542 /* Sometimes the address is off by 4 */
8543 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
8551 * mono_arch_skip_single_step:
8553 * Modify CTX so the ip is placed after the single step trigger instruction,
8554 * we resume, the instruction is not executed again.
8557 mono_arch_skip_single_step (MonoContext *ctx)
8559 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + single_step_fault_size);
8563 * mono_arch_create_seq_point_info:
8565 * Return a pointer to a data structure which is used by the sequence
8566 * point implementation in AOTed code.
8569 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8574 // FIXME: Add a free function
8576 mono_domain_lock (domain);
8577 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8579 mono_domain_unlock (domain);
8582 ji = mono_jit_info_table_find (domain, (char*)code);
8585 // FIXME: Optimize the size
8586 info = g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8588 info->ss_tramp_addr = &ss_trampoline;
8590 mono_domain_lock (domain);
8591 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8593 mono_domain_unlock (domain);
8600 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8602 ext->lmf.previous_lmf = prev_lmf;
8603 /* Mark that this is a MonoLMFExt */
8604 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8605 ext->lmf.rsp = (gssize)ext;
8611 mono_arch_opcode_supported (int opcode)
8614 case OP_ATOMIC_ADD_I4:
8615 case OP_ATOMIC_ADD_I8:
8616 case OP_ATOMIC_EXCHANGE_I4:
8617 case OP_ATOMIC_EXCHANGE_I8:
8618 case OP_ATOMIC_CAS_I4:
8619 case OP_ATOMIC_CAS_I8:
8620 case OP_ATOMIC_LOAD_I1:
8621 case OP_ATOMIC_LOAD_I2:
8622 case OP_ATOMIC_LOAD_I4:
8623 case OP_ATOMIC_LOAD_I8:
8624 case OP_ATOMIC_LOAD_U1:
8625 case OP_ATOMIC_LOAD_U2:
8626 case OP_ATOMIC_LOAD_U4:
8627 case OP_ATOMIC_LOAD_U8:
8628 case OP_ATOMIC_LOAD_R4:
8629 case OP_ATOMIC_LOAD_R8:
8630 case OP_ATOMIC_STORE_I1:
8631 case OP_ATOMIC_STORE_I2:
8632 case OP_ATOMIC_STORE_I4:
8633 case OP_ATOMIC_STORE_I8:
8634 case OP_ATOMIC_STORE_U1:
8635 case OP_ATOMIC_STORE_U2:
8636 case OP_ATOMIC_STORE_U4:
8637 case OP_ATOMIC_STORE_U8:
8638 case OP_ATOMIC_STORE_R4:
8639 case OP_ATOMIC_STORE_R8: