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 /* gsharedvt argument passed by addr */
507 ArgNone /* only in pair_storage */
515 /* Only if storage == ArgValuetypeInReg */
516 ArgStorage pair_storage [2];
518 /* The size of each pair */
528 gboolean need_stack_align;
529 /* The index of the vret arg in the argument list */
536 #define DEBUG(a) if (cfg->verbose_level > 1) a
539 static AMD64_Reg_No param_regs [] = { AMD64_RCX, AMD64_RDX, AMD64_R8, AMD64_R9 };
541 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
543 static AMD64_Reg_No param_regs [] = { AMD64_RDI, AMD64_RSI, AMD64_RDX, AMD64_RCX, AMD64_R8, AMD64_R9 };
545 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
549 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
551 ainfo->offset = *stack_size;
553 if (*gr >= PARAM_REGS) {
554 ainfo->storage = ArgOnStack;
555 /* Since the same stack slot size is used for all arg */
556 /* types, it needs to be big enough to hold them all */
557 (*stack_size) += sizeof(mgreg_t);
560 ainfo->storage = ArgInIReg;
561 ainfo->reg = param_regs [*gr];
567 #define FLOAT_PARAM_REGS 4
569 #define FLOAT_PARAM_REGS 8
573 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
575 ainfo->offset = *stack_size;
577 if (*gr >= FLOAT_PARAM_REGS) {
578 ainfo->storage = ArgOnStack;
579 /* Since the same stack slot size is used for both float */
580 /* types, it needs to be big enough to hold them both */
581 (*stack_size) += sizeof(mgreg_t);
584 /* A double register */
586 ainfo->storage = ArgInDoubleSSEReg;
588 ainfo->storage = ArgInFloatSSEReg;
594 typedef enum ArgumentClass {
602 merge_argument_class_from_type (MonoType *type, ArgumentClass class1)
604 ArgumentClass class2 = ARG_CLASS_NO_CLASS;
607 ptype = mini_get_underlying_type (type);
608 switch (ptype->type) {
617 case MONO_TYPE_STRING:
618 case MONO_TYPE_OBJECT:
619 case MONO_TYPE_CLASS:
620 case MONO_TYPE_SZARRAY:
622 case MONO_TYPE_FNPTR:
623 case MONO_TYPE_ARRAY:
626 class2 = ARG_CLASS_INTEGER;
631 class2 = ARG_CLASS_INTEGER;
633 class2 = ARG_CLASS_SSE;
637 case MONO_TYPE_TYPEDBYREF:
638 g_assert_not_reached ();
640 case MONO_TYPE_GENERICINST:
641 if (!mono_type_generic_inst_is_valuetype (ptype)) {
642 class2 = ARG_CLASS_INTEGER;
646 case MONO_TYPE_VALUETYPE: {
647 MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
650 for (i = 0; i < info->num_fields; ++i) {
652 class2 = merge_argument_class_from_type (info->fields [i].field->type, class2);
657 g_assert_not_reached ();
661 if (class1 == class2)
663 else if (class1 == ARG_CLASS_NO_CLASS)
665 else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
666 class1 = ARG_CLASS_MEMORY;
667 else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
668 class1 = ARG_CLASS_INTEGER;
670 class1 = ARG_CLASS_SSE;
674 #ifdef __native_client_codegen__
676 /* Default alignment for Native Client is 32-byte. */
677 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
679 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
680 /* Check that alignment doesn't cross an alignment boundary. */
682 mono_arch_nacl_pad(guint8 *code, int pad)
684 const int kMaxPadding = 8; /* see amd64-codegen.h:amd64_padding_size() */
686 if (pad == 0) return code;
687 /* assertion: alignment cannot cross a block boundary */
688 g_assert (((uintptr_t)code & (~kNaClAlignmentMask)) ==
689 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
690 while (pad >= kMaxPadding) {
691 amd64_padding (code, kMaxPadding);
694 if (pad != 0) amd64_padding (code, pad);
700 count_fields_nested (MonoClass *klass)
702 MonoMarshalType *info;
705 info = mono_marshal_load_type_info (klass);
708 for (i = 0; i < info->num_fields; ++i) {
709 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type))
710 count += count_fields_nested (mono_class_from_mono_type (info->fields [i].field->type));
718 collect_field_info_nested (MonoClass *klass, MonoMarshalField *fields, int index, int offset)
720 MonoMarshalType *info;
723 info = mono_marshal_load_type_info (klass);
725 for (i = 0; i < info->num_fields; ++i) {
726 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type)) {
727 index = collect_field_info_nested (mono_class_from_mono_type (info->fields [i].field->type), fields, index, info->fields [i].offset);
729 memcpy (&fields [index], &info->fields [i], sizeof (MonoMarshalField));
730 fields [index].offset += offset;
738 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
740 guint32 *gr, guint32 *fr, guint32 *stack_size)
742 guint32 size, quad, nquads, i, nfields;
743 /* Keep track of the size used in each quad so we can */
744 /* use the right size when copying args/return vars. */
745 guint32 quadsize [2] = {8, 8};
746 ArgumentClass args [2];
747 MonoMarshalType *info = NULL;
748 MonoMarshalField *fields = NULL;
750 gboolean pass_on_stack = FALSE;
752 klass = mono_class_from_mono_type (type);
753 size = mini_type_stack_size_full (&klass->byval_arg, NULL, sig->pinvoke);
755 if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
756 /* We pass and return vtypes of size 8 in a register */
757 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
758 pass_on_stack = TRUE;
762 pass_on_stack = TRUE;
766 /* If this struct can't be split up naturally into 8-byte */
767 /* chunks (registers), pass it on the stack. */
768 if (sig->pinvoke && !pass_on_stack) {
772 info = mono_marshal_load_type_info (klass);
776 * Collect field information recursively to be able to
777 * handle nested structures.
779 nfields = count_fields_nested (klass);
780 fields = g_new0 (MonoMarshalField, nfields);
781 collect_field_info_nested (klass, fields, 0, 0);
783 for (i = 0; i < nfields; ++i) {
784 field_size = mono_marshal_type_size (fields [i].field->type,
786 &align, TRUE, klass->unicode);
787 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
788 pass_on_stack = TRUE;
796 ainfo->storage = ArgValuetypeInReg;
797 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
803 /* Allways pass in memory */
804 ainfo->offset = *stack_size;
805 *stack_size += ALIGN_TO (size, 8);
806 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
812 /* FIXME: Handle structs smaller than 8 bytes */
813 //if ((size % 8) != 0)
822 int n = mono_class_value_size (klass, NULL);
824 quadsize [0] = n >= 8 ? 8 : n;
825 quadsize [1] = n >= 8 ? MAX (n - 8, 8) : 0;
827 /* Always pass in 1 or 2 integer registers */
828 args [0] = ARG_CLASS_INTEGER;
829 args [1] = ARG_CLASS_INTEGER;
830 /* Only the simplest cases are supported */
831 if (is_return && nquads != 1) {
832 args [0] = ARG_CLASS_MEMORY;
833 args [1] = ARG_CLASS_MEMORY;
837 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
838 * The X87 and SSEUP stuff is left out since there are no such types in
844 ainfo->storage = ArgValuetypeInReg;
845 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
850 if (info->native_size > 16) {
851 ainfo->offset = *stack_size;
852 *stack_size += ALIGN_TO (info->native_size, 8);
853 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
859 switch (info->native_size) {
860 case 1: case 2: case 4: case 8:
864 ainfo->storage = ArgValuetypeAddrInIReg;
865 ainfo->offset = *stack_size;
866 *stack_size += ALIGN_TO (info->native_size, 8);
869 ainfo->storage = ArgValuetypeAddrInIReg;
871 if (*gr < PARAM_REGS) {
872 ainfo->pair_storage [0] = ArgInIReg;
873 ainfo->pair_regs [0] = param_regs [*gr];
877 ainfo->pair_storage [0] = ArgOnStack;
878 ainfo->offset = *stack_size;
888 args [0] = ARG_CLASS_NO_CLASS;
889 args [1] = ARG_CLASS_NO_CLASS;
890 for (quad = 0; quad < nquads; ++quad) {
893 ArgumentClass class1;
896 class1 = ARG_CLASS_MEMORY;
898 class1 = ARG_CLASS_NO_CLASS;
899 for (i = 0; i < nfields; ++i) {
900 size = mono_marshal_type_size (fields [i].field->type,
902 &align, TRUE, klass->unicode);
903 if ((fields [i].offset < 8) && (fields [i].offset + size) > 8) {
904 /* Unaligned field */
908 /* Skip fields in other quad */
909 if ((quad == 0) && (fields [i].offset >= 8))
911 if ((quad == 1) && (fields [i].offset < 8))
914 /* How far into this quad this data extends.*/
915 /* (8 is size of quad) */
916 quadsize [quad] = fields [i].offset + size - (quad * 8);
918 class1 = merge_argument_class_from_type (fields [i].field->type, class1);
920 g_assert (class1 != ARG_CLASS_NO_CLASS);
921 args [quad] = class1;
927 /* Post merger cleanup */
928 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
929 args [0] = args [1] = ARG_CLASS_MEMORY;
931 /* Allocate registers */
936 while (quadsize [0] != 1 && quadsize [0] != 2 && quadsize [0] != 4 && quadsize [0] != 8)
938 while (quadsize [1] != 1 && quadsize [1] != 2 && quadsize [1] != 4 && quadsize [1] != 8)
941 ainfo->storage = ArgValuetypeInReg;
942 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
943 g_assert (quadsize [0] <= 8);
944 g_assert (quadsize [1] <= 8);
945 ainfo->pair_size [0] = quadsize [0];
946 ainfo->pair_size [1] = quadsize [1];
947 ainfo->nregs = nquads;
948 for (quad = 0; quad < nquads; ++quad) {
949 switch (args [quad]) {
950 case ARG_CLASS_INTEGER:
951 if (*gr >= PARAM_REGS)
952 args [quad] = ARG_CLASS_MEMORY;
954 ainfo->pair_storage [quad] = ArgInIReg;
956 ainfo->pair_regs [quad] = return_regs [*gr];
958 ainfo->pair_regs [quad] = param_regs [*gr];
963 if (*fr >= FLOAT_PARAM_REGS)
964 args [quad] = ARG_CLASS_MEMORY;
966 if (quadsize[quad] <= 4)
967 ainfo->pair_storage [quad] = ArgInFloatSSEReg;
968 else ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
969 ainfo->pair_regs [quad] = *fr;
973 case ARG_CLASS_MEMORY:
976 g_assert_not_reached ();
980 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
981 /* Revert possible register assignments */
985 ainfo->offset = *stack_size;
987 *stack_size += ALIGN_TO (info->native_size, 8);
989 *stack_size += nquads * sizeof(mgreg_t);
990 ainfo->storage = is_return ? ArgValuetypeAddrInIReg : ArgOnStack;
998 * Obtain information about a call according to the calling convention.
999 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
1000 * Draft Version 0.23" document for more information.
1003 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
1005 guint32 i, gr, fr, pstart;
1007 int n = sig->hasthis + sig->param_count;
1008 guint32 stack_size = 0;
1010 gboolean is_pinvoke = sig->pinvoke;
1013 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1015 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1023 /* Reserve space where the callee can save the argument registers */
1024 stack_size = 4 * sizeof (mgreg_t);
1028 ret_type = mini_get_underlying_type (sig->ret);
1029 switch (ret_type->type) {
1039 case MONO_TYPE_FNPTR:
1040 case MONO_TYPE_CLASS:
1041 case MONO_TYPE_OBJECT:
1042 case MONO_TYPE_SZARRAY:
1043 case MONO_TYPE_ARRAY:
1044 case MONO_TYPE_STRING:
1045 cinfo->ret.storage = ArgInIReg;
1046 cinfo->ret.reg = AMD64_RAX;
1050 cinfo->ret.storage = ArgInIReg;
1051 cinfo->ret.reg = AMD64_RAX;
1054 cinfo->ret.storage = ArgInFloatSSEReg;
1055 cinfo->ret.reg = AMD64_XMM0;
1058 cinfo->ret.storage = ArgInDoubleSSEReg;
1059 cinfo->ret.reg = AMD64_XMM0;
1061 case MONO_TYPE_GENERICINST:
1062 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1063 cinfo->ret.storage = ArgInIReg;
1064 cinfo->ret.reg = AMD64_RAX;
1067 if (mini_is_gsharedvt_type (ret_type)) {
1068 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1072 #if defined( __native_client_codegen__ )
1073 case MONO_TYPE_TYPEDBYREF:
1075 case MONO_TYPE_VALUETYPE: {
1076 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
1078 add_valuetype (sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
1079 g_assert (cinfo->ret.storage != ArgInIReg);
1082 #if !defined( __native_client_codegen__ )
1083 case MONO_TYPE_TYPEDBYREF:
1084 /* Same as a valuetype with size 24 */
1085 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1089 case MONO_TYPE_MVAR:
1090 g_assert (mini_is_gsharedvt_type (ret_type));
1091 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1093 case MONO_TYPE_VOID:
1096 g_error ("Can't handle as return value 0x%x", ret_type->type);
1101 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1102 * the first argument, allowing 'this' to be always passed in the first arg reg.
1103 * Also do this if the first argument is a reference type, since virtual calls
1104 * are sometimes made using calli without sig->hasthis set, like in the delegate
1107 if (cinfo->ret.storage == ArgValuetypeAddrInIReg && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
1109 add_general (&gr, &stack_size, cinfo->args + 0);
1111 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
1114 add_general (&gr, &stack_size, &cinfo->ret);
1115 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1116 cinfo->vret_arg_index = 1;
1120 add_general (&gr, &stack_size, cinfo->args + 0);
1122 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
1123 add_general (&gr, &stack_size, &cinfo->ret);
1124 cinfo->ret.storage = ArgValuetypeAddrInIReg;
1128 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
1130 fr = FLOAT_PARAM_REGS;
1132 /* Emit the signature cookie just before the implicit arguments */
1133 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1136 for (i = pstart; i < sig->param_count; ++i) {
1137 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
1141 /* The float param registers and other param registers must be the same index on Windows x64.*/
1148 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1149 /* We allways pass the sig cookie on the stack for simplicity */
1151 * Prevent implicit arguments + the sig cookie from being passed
1155 fr = FLOAT_PARAM_REGS;
1157 /* Emit the signature cookie just before the implicit arguments */
1158 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1161 ptype = mini_get_underlying_type (sig->params [i]);
1162 switch (ptype->type) {
1165 add_general (&gr, &stack_size, ainfo);
1169 add_general (&gr, &stack_size, ainfo);
1173 add_general (&gr, &stack_size, ainfo);
1178 case MONO_TYPE_FNPTR:
1179 case MONO_TYPE_CLASS:
1180 case MONO_TYPE_OBJECT:
1181 case MONO_TYPE_STRING:
1182 case MONO_TYPE_SZARRAY:
1183 case MONO_TYPE_ARRAY:
1184 add_general (&gr, &stack_size, ainfo);
1186 case MONO_TYPE_GENERICINST:
1187 if (!mono_type_generic_inst_is_valuetype (ptype)) {
1188 add_general (&gr, &stack_size, ainfo);
1191 if (mini_is_gsharedvt_type (ptype)) {
1192 /* gsharedvt arguments are passed by ref */
1193 add_general (&gr, &stack_size, ainfo);
1194 if (ainfo->storage == ArgInIReg)
1195 ainfo->storage = ArgGSharedVtInReg;
1197 ainfo->storage = ArgGSharedVtOnStack;
1201 case MONO_TYPE_VALUETYPE:
1202 case MONO_TYPE_TYPEDBYREF:
1203 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
1208 add_general (&gr, &stack_size, ainfo);
1211 add_float (&fr, &stack_size, ainfo, FALSE);
1214 add_float (&fr, &stack_size, ainfo, TRUE);
1217 case MONO_TYPE_MVAR:
1218 /* gsharedvt arguments are passed by ref */
1219 g_assert (mini_is_gsharedvt_type (ptype));
1220 add_general (&gr, &stack_size, ainfo);
1221 if (ainfo->storage == ArgInIReg)
1222 ainfo->storage = ArgGSharedVtInReg;
1224 ainfo->storage = ArgGSharedVtOnStack;
1227 g_assert_not_reached ();
1231 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
1233 fr = FLOAT_PARAM_REGS;
1235 /* Emit the signature cookie just before the implicit arguments */
1236 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1239 cinfo->stack_usage = stack_size;
1240 cinfo->reg_usage = gr;
1241 cinfo->freg_usage = fr;
1246 * mono_arch_get_argument_info:
1247 * @csig: a method signature
1248 * @param_count: the number of parameters to consider
1249 * @arg_info: an array to store the result infos
1251 * Gathers information on parameters such as size, alignment and
1252 * padding. arg_info should be large enought to hold param_count + 1 entries.
1254 * Returns the size of the argument area on the stack.
1257 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
1260 CallInfo *cinfo = get_call_info (NULL, csig);
1261 guint32 args_size = cinfo->stack_usage;
1263 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
1264 if (csig->hasthis) {
1265 arg_info [0].offset = 0;
1268 for (k = 0; k < param_count; k++) {
1269 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
1271 arg_info [k + 1].size = 0;
1280 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
1284 MonoType *callee_ret;
1286 c1 = get_call_info (NULL, caller_sig);
1287 c2 = get_call_info (NULL, callee_sig);
1288 res = c1->stack_usage >= c2->stack_usage;
1289 callee_ret = mini_get_underlying_type (callee_sig->ret);
1290 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
1291 /* An address on the callee's stack is passed as the first argument */
1301 * Initialize the cpu to execute managed code.
1304 mono_arch_cpu_init (void)
1309 /* spec compliance requires running with double precision */
1310 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1311 fpcw &= ~X86_FPCW_PRECC_MASK;
1312 fpcw |= X86_FPCW_PREC_DOUBLE;
1313 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
1314 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1316 /* TODO: This is crashing on Win64 right now.
1317 * _control87 (_PC_53, MCW_PC);
1323 * Initialize architecture specific code.
1326 mono_arch_init (void)
1330 mono_mutex_init_recursive (&mini_arch_mutex);
1331 #if defined(__native_client_codegen__)
1332 mono_native_tls_alloc (&nacl_instruction_depth, NULL);
1333 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)0);
1334 mono_native_tls_alloc (&nacl_rex_tag, NULL);
1335 mono_native_tls_alloc (&nacl_legacy_prefix_tag, NULL);
1338 #ifdef MONO_ARCH_NOMAP32BIT
1339 flags = MONO_MMAP_READ;
1340 /* amd64_mov_reg_imm () + amd64_mov_reg_membase () */
1341 breakpoint_size = 13;
1342 breakpoint_fault_size = 3;
1344 flags = MONO_MMAP_READ|MONO_MMAP_32BIT;
1345 /* amd64_mov_reg_mem () */
1346 breakpoint_size = 8;
1347 breakpoint_fault_size = 8;
1350 /* amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4); */
1351 single_step_fault_size = 4;
1353 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), flags);
1354 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), flags);
1355 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
1357 mono_aot_register_jit_icall ("mono_amd64_throw_exception", mono_amd64_throw_exception);
1358 mono_aot_register_jit_icall ("mono_amd64_throw_corlib_exception", mono_amd64_throw_corlib_exception);
1359 mono_aot_register_jit_icall ("mono_amd64_resume_unwind", mono_amd64_resume_unwind);
1360 mono_aot_register_jit_icall ("mono_amd64_get_original_ip", mono_amd64_get_original_ip);
1364 * Cleanup architecture specific code.
1367 mono_arch_cleanup (void)
1369 mono_mutex_destroy (&mini_arch_mutex);
1370 #if defined(__native_client_codegen__)
1371 mono_native_tls_free (nacl_instruction_depth);
1372 mono_native_tls_free (nacl_rex_tag);
1373 mono_native_tls_free (nacl_legacy_prefix_tag);
1378 * This function returns the optimizations supported on this cpu.
1381 mono_arch_cpu_optimizations (guint32 *exclude_mask)
1387 if (mono_hwcap_x86_has_cmov) {
1388 opts |= MONO_OPT_CMOV;
1390 if (mono_hwcap_x86_has_fcmov)
1391 opts |= MONO_OPT_FCMOV;
1393 *exclude_mask |= MONO_OPT_FCMOV;
1395 *exclude_mask |= MONO_OPT_CMOV;
1402 * This function test for all SSE functions supported.
1404 * Returns a bitmask corresponding to all supported versions.
1408 mono_arch_cpu_enumerate_simd_versions (void)
1410 guint32 sse_opts = 0;
1412 if (mono_hwcap_x86_has_sse1)
1413 sse_opts |= SIMD_VERSION_SSE1;
1415 if (mono_hwcap_x86_has_sse2)
1416 sse_opts |= SIMD_VERSION_SSE2;
1418 if (mono_hwcap_x86_has_sse3)
1419 sse_opts |= SIMD_VERSION_SSE3;
1421 if (mono_hwcap_x86_has_ssse3)
1422 sse_opts |= SIMD_VERSION_SSSE3;
1424 if (mono_hwcap_x86_has_sse41)
1425 sse_opts |= SIMD_VERSION_SSE41;
1427 if (mono_hwcap_x86_has_sse42)
1428 sse_opts |= SIMD_VERSION_SSE42;
1430 if (mono_hwcap_x86_has_sse4a)
1431 sse_opts |= SIMD_VERSION_SSE4a;
1439 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1444 for (i = 0; i < cfg->num_varinfo; i++) {
1445 MonoInst *ins = cfg->varinfo [i];
1446 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1449 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1452 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
1453 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1456 if (mono_is_regsize_var (ins->inst_vtype)) {
1457 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1458 g_assert (i == vmv->idx);
1459 vars = g_list_prepend (vars, vmv);
1463 vars = mono_varlist_sort (cfg, vars, 0);
1469 * mono_arch_compute_omit_fp:
1471 * Determine whenever the frame pointer can be eliminated.
1474 mono_arch_compute_omit_fp (MonoCompile *cfg)
1476 MonoMethodSignature *sig;
1477 MonoMethodHeader *header;
1481 if (cfg->arch.omit_fp_computed)
1484 header = cfg->header;
1486 sig = mono_method_signature (cfg->method);
1488 if (!cfg->arch.cinfo)
1489 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1490 cinfo = cfg->arch.cinfo;
1493 * FIXME: Remove some of the restrictions.
1495 cfg->arch.omit_fp = TRUE;
1496 cfg->arch.omit_fp_computed = TRUE;
1498 #ifdef __native_client_codegen__
1499 /* NaCl modules may not change the value of RBP, so it cannot be */
1500 /* used as a normal register, but it can be used as a frame pointer*/
1501 cfg->disable_omit_fp = TRUE;
1502 cfg->arch.omit_fp = FALSE;
1505 if (cfg->disable_omit_fp)
1506 cfg->arch.omit_fp = FALSE;
1508 if (!debug_omit_fp ())
1509 cfg->arch.omit_fp = FALSE;
1511 if (cfg->method->save_lmf)
1512 cfg->arch.omit_fp = FALSE;
1514 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1515 cfg->arch.omit_fp = FALSE;
1516 if (header->num_clauses)
1517 cfg->arch.omit_fp = FALSE;
1518 if (cfg->param_area)
1519 cfg->arch.omit_fp = FALSE;
1520 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1521 cfg->arch.omit_fp = FALSE;
1522 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1523 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1524 cfg->arch.omit_fp = FALSE;
1525 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1526 ArgInfo *ainfo = &cinfo->args [i];
1528 if (ainfo->storage == ArgOnStack) {
1530 * The stack offset can only be determined when the frame
1533 cfg->arch.omit_fp = FALSE;
1538 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1539 MonoInst *ins = cfg->varinfo [i];
1542 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1547 mono_arch_get_global_int_regs (MonoCompile *cfg)
1551 mono_arch_compute_omit_fp (cfg);
1553 if (cfg->arch.omit_fp)
1554 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1556 /* We use the callee saved registers for global allocation */
1557 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1558 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1559 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1560 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1561 #ifndef __native_client_codegen__
1562 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1565 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1566 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1573 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1578 /* All XMM registers */
1579 for (i = 0; i < 16; ++i)
1580 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1586 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1588 static GList *r = NULL;
1593 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1594 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1595 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1596 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1597 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1598 #ifndef __native_client_codegen__
1599 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1602 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1603 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1604 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1605 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1606 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1607 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1608 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1609 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1611 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1618 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1621 static GList *r = NULL;
1626 for (i = 0; i < AMD64_XMM_NREG; ++i)
1627 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1629 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1636 * mono_arch_regalloc_cost:
1638 * Return the cost, in number of memory references, of the action of
1639 * allocating the variable VMV into a register during global register
1643 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1645 MonoInst *ins = cfg->varinfo [vmv->idx];
1647 if (cfg->method->save_lmf)
1648 /* The register is already saved */
1649 /* substract 1 for the invisible store in the prolog */
1650 return (ins->opcode == OP_ARG) ? 0 : 1;
1653 return (ins->opcode == OP_ARG) ? 1 : 2;
1657 * mono_arch_fill_argument_info:
1659 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1663 mono_arch_fill_argument_info (MonoCompile *cfg)
1666 MonoMethodSignature *sig;
1671 sig = mono_method_signature (cfg->method);
1673 cinfo = cfg->arch.cinfo;
1674 sig_ret = mini_get_underlying_type (sig->ret);
1677 * Contrary to mono_arch_allocate_vars (), the information should describe
1678 * where the arguments are at the beginning of the method, not where they can be
1679 * accessed during the execution of the method. The later makes no sense for the
1680 * global register allocator, since a variable can be in more than one location.
1682 if (sig_ret->type != MONO_TYPE_VOID) {
1683 switch (cinfo->ret.storage) {
1685 case ArgInFloatSSEReg:
1686 case ArgInDoubleSSEReg:
1687 if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->ret.storage == ArgValuetypeAddrInIReg)) {
1688 cfg->vret_addr->opcode = OP_REGVAR;
1689 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1692 cfg->ret->opcode = OP_REGVAR;
1693 cfg->ret->inst_c0 = cinfo->ret.reg;
1696 case ArgValuetypeInReg:
1697 cfg->ret->opcode = OP_REGOFFSET;
1698 cfg->ret->inst_basereg = -1;
1699 cfg->ret->inst_offset = -1;
1702 g_assert_not_reached ();
1706 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1707 ArgInfo *ainfo = &cinfo->args [i];
1709 ins = cfg->args [i];
1711 switch (ainfo->storage) {
1713 case ArgInFloatSSEReg:
1714 case ArgInDoubleSSEReg:
1715 ins->opcode = OP_REGVAR;
1716 ins->inst_c0 = ainfo->reg;
1719 ins->opcode = OP_REGOFFSET;
1720 ins->inst_basereg = -1;
1721 ins->inst_offset = -1;
1723 case ArgValuetypeInReg:
1725 ins->opcode = OP_NOP;
1728 g_assert_not_reached ();
1734 * mono_arch_init_compile:
1736 * Set architecture specific flags in CFG.
1739 mono_arch_init_compile (MonoCompile *cfg)
1741 cfg->have_card_table_wb = 1;
1742 cfg->have_op_generic_class_init = 1;
1743 cfg->have_generalized_imt_thunk = 1;
1744 cfg->gshared_supported = 1;
1745 cfg->have_tls_get = mono_amd64_have_tls_get ();
1746 cfg->have_liverange_ops = 1;
1750 mono_arch_allocate_vars (MonoCompile *cfg)
1753 MonoMethodSignature *sig;
1756 guint32 locals_stack_size, locals_stack_align;
1760 sig = mono_method_signature (cfg->method);
1762 cinfo = cfg->arch.cinfo;
1763 sig_ret = mini_get_underlying_type (sig->ret);
1765 mono_arch_compute_omit_fp (cfg);
1768 * We use the ABI calling conventions for managed code as well.
1769 * Exception: valuetypes are only sometimes passed or returned in registers.
1773 * The stack looks like this:
1774 * <incoming arguments passed on the stack>
1776 * <lmf/caller saved registers>
1779 * <localloc area> -> grows dynamically
1783 if (cfg->arch.omit_fp) {
1784 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1785 cfg->frame_reg = AMD64_RSP;
1788 /* Locals are allocated backwards from %fp */
1789 cfg->frame_reg = AMD64_RBP;
1793 cfg->arch.saved_iregs = cfg->used_int_regs;
1794 if (cfg->method->save_lmf)
1795 /* Save all callee-saved registers normally, and restore them when unwinding through an LMF */
1796 cfg->arch.saved_iregs |= (1 << AMD64_RBX) | (1 << AMD64_R12) | (1 << AMD64_R13) | (1 << AMD64_R14) | (1 << AMD64_R15);
1798 if (cfg->arch.omit_fp)
1799 cfg->arch.reg_save_area_offset = offset;
1800 /* Reserve space for callee saved registers */
1801 for (i = 0; i < AMD64_NREG; ++i)
1802 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
1803 offset += sizeof(mgreg_t);
1805 if (!cfg->arch.omit_fp)
1806 cfg->arch.reg_save_area_offset = -offset;
1808 if (sig_ret->type != MONO_TYPE_VOID) {
1809 switch (cinfo->ret.storage) {
1811 case ArgInFloatSSEReg:
1812 case ArgInDoubleSSEReg:
1813 cfg->ret->opcode = OP_REGVAR;
1814 cfg->ret->inst_c0 = cinfo->ret.reg;
1816 case ArgValuetypeAddrInIReg:
1817 /* The register is volatile */
1818 cfg->vret_addr->opcode = OP_REGOFFSET;
1819 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1820 if (cfg->arch.omit_fp) {
1821 cfg->vret_addr->inst_offset = offset;
1825 cfg->vret_addr->inst_offset = -offset;
1827 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1828 printf ("vret_addr =");
1829 mono_print_ins (cfg->vret_addr);
1832 case ArgValuetypeInReg:
1833 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1834 cfg->ret->opcode = OP_REGOFFSET;
1835 cfg->ret->inst_basereg = cfg->frame_reg;
1836 if (cfg->arch.omit_fp) {
1837 cfg->ret->inst_offset = offset;
1838 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1840 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1841 cfg->ret->inst_offset = - offset;
1845 g_assert_not_reached ();
1847 cfg->ret->dreg = cfg->ret->inst_c0;
1850 /* Allocate locals */
1851 offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1852 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1853 char *mname = mono_method_full_name (cfg->method, TRUE);
1854 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1855 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1860 if (locals_stack_align) {
1861 offset += (locals_stack_align - 1);
1862 offset &= ~(locals_stack_align - 1);
1864 if (cfg->arch.omit_fp) {
1865 cfg->locals_min_stack_offset = offset;
1866 cfg->locals_max_stack_offset = offset + locals_stack_size;
1868 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1869 cfg->locals_max_stack_offset = - offset;
1872 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1873 if (offsets [i] != -1) {
1874 MonoInst *ins = cfg->varinfo [i];
1875 ins->opcode = OP_REGOFFSET;
1876 ins->inst_basereg = cfg->frame_reg;
1877 if (cfg->arch.omit_fp)
1878 ins->inst_offset = (offset + offsets [i]);
1880 ins->inst_offset = - (offset + offsets [i]);
1881 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1884 offset += locals_stack_size;
1886 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1887 g_assert (!cfg->arch.omit_fp);
1888 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1889 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1892 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1893 ins = cfg->args [i];
1894 if (ins->opcode != OP_REGVAR) {
1895 ArgInfo *ainfo = &cinfo->args [i];
1896 gboolean inreg = TRUE;
1898 /* FIXME: Allocate volatile arguments to registers */
1899 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1903 * Under AMD64, all registers used to pass arguments to functions
1904 * are volatile across calls.
1905 * FIXME: Optimize this.
1907 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg) || (ainfo->storage == ArgGSharedVtInReg))
1910 ins->opcode = OP_REGOFFSET;
1912 switch (ainfo->storage) {
1914 case ArgInFloatSSEReg:
1915 case ArgInDoubleSSEReg:
1916 case ArgGSharedVtInReg:
1918 ins->opcode = OP_REGVAR;
1919 ins->dreg = ainfo->reg;
1923 case ArgGSharedVtOnStack:
1924 g_assert (!cfg->arch.omit_fp);
1925 ins->opcode = OP_REGOFFSET;
1926 ins->inst_basereg = cfg->frame_reg;
1927 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1929 case ArgValuetypeInReg:
1931 case ArgValuetypeAddrInIReg: {
1933 g_assert (!cfg->arch.omit_fp);
1935 MONO_INST_NEW (cfg, indir, 0);
1936 indir->opcode = OP_REGOFFSET;
1937 if (ainfo->pair_storage [0] == ArgInIReg) {
1938 indir->inst_basereg = cfg->frame_reg;
1939 offset = ALIGN_TO (offset, sizeof (gpointer));
1940 offset += (sizeof (gpointer));
1941 indir->inst_offset = - offset;
1944 indir->inst_basereg = cfg->frame_reg;
1945 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
1948 ins->opcode = OP_VTARG_ADDR;
1949 ins->inst_left = indir;
1957 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg)) {
1958 ins->opcode = OP_REGOFFSET;
1959 ins->inst_basereg = cfg->frame_reg;
1960 /* These arguments are saved to the stack in the prolog */
1961 offset = ALIGN_TO (offset, sizeof(mgreg_t));
1962 if (cfg->arch.omit_fp) {
1963 ins->inst_offset = offset;
1964 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1965 // Arguments are yet supported by the stack map creation code
1966 //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
1968 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1969 ins->inst_offset = - offset;
1970 //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
1976 cfg->stack_offset = offset;
1980 mono_arch_create_vars (MonoCompile *cfg)
1982 MonoMethodSignature *sig;
1986 sig = mono_method_signature (cfg->method);
1988 if (!cfg->arch.cinfo)
1989 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1990 cinfo = cfg->arch.cinfo;
1992 if (cinfo->ret.storage == ArgValuetypeInReg)
1993 cfg->ret_var_is_local = TRUE;
1995 sig_ret = mini_get_underlying_type (sig->ret);
1996 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
1997 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1998 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1999 printf ("vret_addr = ");
2000 mono_print_ins (cfg->vret_addr);
2004 if (cfg->gen_sdb_seq_points) {
2007 if (cfg->compile_aot) {
2008 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2009 ins->flags |= MONO_INST_VOLATILE;
2010 cfg->arch.seq_point_info_var = ins;
2012 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2013 ins->flags |= MONO_INST_VOLATILE;
2014 cfg->arch.ss_tramp_var = ins;
2017 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2018 ins->flags |= MONO_INST_VOLATILE;
2019 cfg->arch.ss_trigger_page_var = ins;
2022 if (cfg->method->save_lmf)
2023 cfg->create_lmf_var = TRUE;
2025 if (cfg->method->save_lmf) {
2027 #if !defined(TARGET_WIN32)
2028 if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
2029 cfg->lmf_ir_mono_lmf = TRUE;
2035 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2041 MONO_INST_NEW (cfg, ins, OP_MOVE);
2042 ins->dreg = mono_alloc_ireg_copy (cfg, tree->dreg);
2043 ins->sreg1 = tree->dreg;
2044 MONO_ADD_INS (cfg->cbb, ins);
2045 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2047 case ArgInFloatSSEReg:
2048 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
2049 ins->dreg = mono_alloc_freg (cfg);
2050 ins->sreg1 = tree->dreg;
2051 MONO_ADD_INS (cfg->cbb, ins);
2053 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2055 case ArgInDoubleSSEReg:
2056 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2057 ins->dreg = mono_alloc_freg (cfg);
2058 ins->sreg1 = tree->dreg;
2059 MONO_ADD_INS (cfg->cbb, ins);
2061 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2065 g_assert_not_reached ();
2070 arg_storage_to_load_membase (ArgStorage storage)
2074 #if defined(__mono_ilp32__)
2075 return OP_LOADI8_MEMBASE;
2077 return OP_LOAD_MEMBASE;
2079 case ArgInDoubleSSEReg:
2080 return OP_LOADR8_MEMBASE;
2081 case ArgInFloatSSEReg:
2082 return OP_LOADR4_MEMBASE;
2084 g_assert_not_reached ();
2091 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2093 MonoMethodSignature *tmp_sig;
2096 if (call->tail_call)
2099 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
2102 * mono_ArgIterator_Setup assumes the signature cookie is
2103 * passed first and all the arguments which were before it are
2104 * passed on the stack after the signature. So compensate by
2105 * passing a different signature.
2107 tmp_sig = mono_metadata_signature_dup_full (cfg->method->klass->image, call->signature);
2108 tmp_sig->param_count -= call->signature->sentinelpos;
2109 tmp_sig->sentinelpos = 0;
2110 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2112 sig_reg = mono_alloc_ireg (cfg);
2113 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2115 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, cinfo->sig_cookie.offset, sig_reg);
2119 static inline LLVMArgStorage
2120 arg_storage_to_llvm_arg_storage (MonoCompile *cfg, ArgStorage storage)
2124 return LLVMArgInIReg;
2127 case ArgGSharedVtInReg:
2128 case ArgGSharedVtOnStack:
2129 return LLVMArgGSharedVt;
2131 g_assert_not_reached ();
2137 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2143 LLVMCallInfo *linfo;
2144 MonoType *t, *sig_ret;
2146 n = sig->param_count + sig->hasthis;
2147 sig_ret = mini_get_underlying_type (sig->ret);
2149 cinfo = get_call_info (cfg->mempool, sig);
2151 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2154 * LLVM always uses the native ABI while we use our own ABI, the
2155 * only difference is the handling of vtypes:
2156 * - we only pass/receive them in registers in some cases, and only
2157 * in 1 or 2 integer registers.
2159 if (cinfo->ret.storage == ArgValuetypeInReg) {
2161 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2162 cfg->disable_llvm = TRUE;
2166 linfo->ret.storage = LLVMArgVtypeInReg;
2167 for (j = 0; j < 2; ++j)
2168 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
2171 if (cinfo->ret.storage == ArgValuetypeAddrInIReg) {
2172 /* Vtype returned using a hidden argument */
2173 linfo->ret.storage = LLVMArgVtypeRetAddr;
2174 linfo->vret_arg_index = cinfo->vret_arg_index;
2177 for (i = 0; i < n; ++i) {
2178 ainfo = cinfo->args + i;
2180 if (i >= sig->hasthis)
2181 t = sig->params [i - sig->hasthis];
2183 t = &mono_defaults.int_class->byval_arg;
2185 linfo->args [i].storage = LLVMArgNone;
2187 switch (ainfo->storage) {
2189 linfo->args [i].storage = LLVMArgInIReg;
2191 case ArgInDoubleSSEReg:
2192 case ArgInFloatSSEReg:
2193 linfo->args [i].storage = LLVMArgInFPReg;
2196 if (MONO_TYPE_ISSTRUCT (t)) {
2197 linfo->args [i].storage = LLVMArgVtypeByVal;
2199 linfo->args [i].storage = LLVMArgInIReg;
2201 if (t->type == MONO_TYPE_R4)
2202 linfo->args [i].storage = LLVMArgInFPReg;
2203 else if (t->type == MONO_TYPE_R8)
2204 linfo->args [i].storage = LLVMArgInFPReg;
2208 case ArgValuetypeInReg:
2210 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2211 cfg->disable_llvm = TRUE;
2215 linfo->args [i].storage = LLVMArgVtypeInReg;
2216 for (j = 0; j < 2; ++j)
2217 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2219 case ArgGSharedVtInReg:
2220 case ArgGSharedVtOnStack:
2221 linfo->args [i].storage = LLVMArgGSharedVt;
2224 cfg->exception_message = g_strdup ("ainfo->storage");
2225 cfg->disable_llvm = TRUE;
2235 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2238 MonoMethodSignature *sig;
2244 sig = call->signature;
2245 n = sig->param_count + sig->hasthis;
2247 cinfo = get_call_info (cfg->mempool, sig);
2251 if (COMPILE_LLVM (cfg)) {
2252 /* We shouldn't be called in the llvm case */
2253 cfg->disable_llvm = TRUE;
2258 * Emit all arguments which are passed on the stack to prevent register
2259 * allocation problems.
2261 for (i = 0; i < n; ++i) {
2263 ainfo = cinfo->args + i;
2265 in = call->args [i];
2267 if (sig->hasthis && i == 0)
2268 t = &mono_defaults.object_class->byval_arg;
2270 t = sig->params [i - sig->hasthis];
2272 t = mini_get_underlying_type (t);
2273 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call) {
2275 if (t->type == MONO_TYPE_R4)
2276 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2277 else if (t->type == MONO_TYPE_R8)
2278 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2280 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2282 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2284 if (cfg->compute_gc_maps) {
2287 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2293 * Emit all parameters passed in registers in non-reverse order for better readability
2294 * and to help the optimization in emit_prolog ().
2296 for (i = 0; i < n; ++i) {
2297 ainfo = cinfo->args + i;
2299 in = call->args [i];
2301 if (ainfo->storage == ArgInIReg)
2302 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2305 for (i = n - 1; i >= 0; --i) {
2308 ainfo = cinfo->args + i;
2310 in = call->args [i];
2312 if (sig->hasthis && i == 0)
2313 t = &mono_defaults.object_class->byval_arg;
2315 t = sig->params [i - sig->hasthis];
2316 t = mini_get_underlying_type (t);
2318 switch (ainfo->storage) {
2322 case ArgInFloatSSEReg:
2323 case ArgInDoubleSSEReg:
2324 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2327 case ArgValuetypeInReg:
2328 case ArgValuetypeAddrInIReg:
2329 case ArgGSharedVtInReg:
2330 case ArgGSharedVtOnStack: {
2331 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call)
2332 /* Already emitted above */
2334 if (ainfo->storage == ArgOnStack && call->tail_call) {
2335 MonoInst *call_inst = (MonoInst*)call;
2336 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2337 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2344 if (t->type == MONO_TYPE_TYPEDBYREF) {
2345 size = sizeof (MonoTypedRef);
2346 align = sizeof (gpointer);
2350 size = mono_type_native_stack_size (t, &align);
2353 * Other backends use mono_type_stack_size (), but that
2354 * aligns the size to 8, which is larger than the size of
2355 * the source, leading to reads of invalid memory if the
2356 * source is at the end of address space.
2358 size = mono_class_value_size (mono_class_from_mono_type (t), &align);
2362 if (size >= 10000) {
2363 /* Avoid asserts in emit_memcpy () */
2364 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
2365 cfg->exception_message = g_strdup_printf ("Passing an argument of size '%d'.", size);
2366 /* Continue normally */
2370 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2371 arg->sreg1 = in->dreg;
2372 arg->klass = mono_class_from_mono_type (t);
2373 arg->backend.size = size;
2374 arg->inst_p0 = call;
2375 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2376 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2378 MONO_ADD_INS (cfg->cbb, arg);
2383 g_assert_not_reached ();
2386 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
2387 /* Emit the signature cookie just before the implicit arguments */
2388 emit_sig_cookie (cfg, call, cinfo);
2391 /* Handle the case where there are no implicit arguments */
2392 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2393 emit_sig_cookie (cfg, call, cinfo);
2395 switch (cinfo->ret.storage) {
2396 case ArgValuetypeInReg:
2397 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2399 * Tell the JIT to use a more efficient calling convention: call using
2400 * OP_CALL, compute the result location after the call, and save the
2403 call->vret_in_reg = TRUE;
2405 * Nullify the instruction computing the vret addr to enable
2406 * future optimizations.
2409 NULLIFY_INS (call->vret_var);
2411 if (call->tail_call)
2414 * The valuetype is in RAX:RDX after the call, need to be copied to
2415 * the stack. Push the address here, so the call instruction can
2418 if (!cfg->arch.vret_addr_loc) {
2419 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2420 /* Prevent it from being register allocated or optimized away */
2421 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2424 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2427 case ArgValuetypeAddrInIReg: {
2429 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2430 vtarg->sreg1 = call->vret_var->dreg;
2431 vtarg->dreg = mono_alloc_preg (cfg);
2432 MONO_ADD_INS (cfg->cbb, vtarg);
2434 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2441 if (cfg->method->save_lmf) {
2442 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2443 MONO_ADD_INS (cfg->cbb, arg);
2446 call->stack_usage = cinfo->stack_usage;
2450 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2453 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2454 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2455 int size = ins->backend.size;
2457 switch (ainfo->storage) {
2458 case ArgValuetypeInReg: {
2462 for (part = 0; part < 2; ++part) {
2463 if (ainfo->pair_storage [part] == ArgNone)
2466 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2467 load->inst_basereg = src->dreg;
2468 load->inst_offset = part * sizeof(mgreg_t);
2470 switch (ainfo->pair_storage [part]) {
2472 load->dreg = mono_alloc_ireg (cfg);
2474 case ArgInDoubleSSEReg:
2475 case ArgInFloatSSEReg:
2476 load->dreg = mono_alloc_freg (cfg);
2479 g_assert_not_reached ();
2481 MONO_ADD_INS (cfg->cbb, load);
2483 add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2487 case ArgValuetypeAddrInIReg: {
2488 MonoInst *vtaddr, *load;
2489 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2491 MONO_INST_NEW (cfg, load, OP_LDADDR);
2492 cfg->has_indirection = TRUE;
2493 load->inst_p0 = vtaddr;
2494 vtaddr->flags |= MONO_INST_INDIRECT;
2495 load->type = STACK_MP;
2496 load->klass = vtaddr->klass;
2497 load->dreg = mono_alloc_ireg (cfg);
2498 MONO_ADD_INS (cfg->cbb, load);
2499 mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2501 if (ainfo->pair_storage [0] == ArgInIReg) {
2502 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2503 arg->dreg = mono_alloc_ireg (cfg);
2504 arg->sreg1 = load->dreg;
2506 MONO_ADD_INS (cfg->cbb, arg);
2507 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2509 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, load->dreg);
2513 case ArgGSharedVtInReg:
2515 mono_call_inst_add_outarg_reg (cfg, call, src->dreg, ainfo->reg, FALSE);
2517 case ArgGSharedVtOnStack:
2518 g_assert_not_reached ();
2522 int dreg = mono_alloc_ireg (cfg);
2524 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
2525 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, dreg);
2526 } else if (size <= 40) {
2527 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2529 // FIXME: Code growth
2530 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2533 if (cfg->compute_gc_maps) {
2535 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2541 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2543 MonoType *ret = mini_get_underlying_type (mono_method_signature (method)->ret);
2545 if (ret->type == MONO_TYPE_R4) {
2546 if (COMPILE_LLVM (cfg))
2547 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2549 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2551 } else if (ret->type == MONO_TYPE_R8) {
2552 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2556 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2559 #endif /* DISABLE_JIT */
2561 #define EMIT_COND_BRANCH(ins,cond,sign) \
2562 if (ins->inst_true_bb->native_offset) { \
2563 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2565 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2566 if ((cfg->opt & MONO_OPT_BRANCH) && \
2567 x86_is_imm8 (ins->inst_true_bb->max_offset - offset)) \
2568 x86_branch8 (code, cond, 0, sign); \
2570 x86_branch32 (code, cond, 0, sign); \
2574 MonoMethodSignature *sig;
2579 dyn_call_supported (MonoMethodSignature *sig, CallInfo *cinfo)
2587 switch (cinfo->ret.storage) {
2591 case ArgValuetypeInReg: {
2592 ArgInfo *ainfo = &cinfo->ret;
2594 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2596 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2604 for (i = 0; i < cinfo->nargs; ++i) {
2605 ArgInfo *ainfo = &cinfo->args [i];
2606 switch (ainfo->storage) {
2609 case ArgValuetypeInReg:
2610 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2612 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2624 * mono_arch_dyn_call_prepare:
2626 * Return a pointer to an arch-specific structure which contains information
2627 * needed by mono_arch_get_dyn_call_args (). Return NULL if OP_DYN_CALL is not
2628 * supported for SIG.
2629 * This function is equivalent to ffi_prep_cif in libffi.
2632 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2634 ArchDynCallInfo *info;
2637 cinfo = get_call_info (NULL, sig);
2639 if (!dyn_call_supported (sig, cinfo)) {
2644 info = g_new0 (ArchDynCallInfo, 1);
2645 // FIXME: Preprocess the info to speed up get_dyn_call_args ().
2647 info->cinfo = cinfo;
2649 return (MonoDynCallInfo*)info;
2653 * mono_arch_dyn_call_free:
2655 * Free a MonoDynCallInfo structure.
2658 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2660 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2662 g_free (ainfo->cinfo);
2666 #if !defined(__native_client__)
2667 #define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
2668 #define GREG_TO_PTR(greg) (gpointer)(greg)
2670 /* Correctly handle casts to/from 32-bit pointers without compiler warnings */
2671 #define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
2672 #define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
2676 * mono_arch_get_start_dyn_call:
2678 * Convert the arguments ARGS to a format which can be passed to OP_DYN_CALL, and
2679 * store the result into BUF.
2680 * ARGS should be an array of pointers pointing to the arguments.
2681 * RET should point to a memory buffer large enought to hold the result of the
2683 * This function should be as fast as possible, any work which does not depend
2684 * on the actual values of the arguments should be done in
2685 * mono_arch_dyn_call_prepare ().
2686 * start_dyn_call + OP_DYN_CALL + finish_dyn_call is equivalent to ffi_call in
2690 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2692 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2693 DynCallArgs *p = (DynCallArgs*)buf;
2694 int arg_index, greg, i, pindex;
2695 MonoMethodSignature *sig = dinfo->sig;
2697 g_assert (buf_len >= sizeof (DynCallArgs));
2706 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2707 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2712 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg)
2713 p->regs [greg ++] = PTR_TO_GREG(ret);
2715 for (i = pindex; i < sig->param_count; i++) {
2716 MonoType *t = mini_get_underlying_type (sig->params [i]);
2717 gpointer *arg = args [arg_index ++];
2720 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2725 case MONO_TYPE_STRING:
2726 case MONO_TYPE_CLASS:
2727 case MONO_TYPE_ARRAY:
2728 case MONO_TYPE_SZARRAY:
2729 case MONO_TYPE_OBJECT:
2733 #if !defined(__mono_ilp32__)
2737 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2738 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2740 #if defined(__mono_ilp32__)
2743 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2744 p->regs [greg ++] = *(guint64*)(arg);
2748 p->regs [greg ++] = *(guint8*)(arg);
2751 p->regs [greg ++] = *(gint8*)(arg);
2754 p->regs [greg ++] = *(gint16*)(arg);
2757 p->regs [greg ++] = *(guint16*)(arg);
2760 p->regs [greg ++] = *(gint32*)(arg);
2763 p->regs [greg ++] = *(guint32*)(arg);
2765 case MONO_TYPE_GENERICINST:
2766 if (MONO_TYPE_IS_REFERENCE (t)) {
2767 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2772 case MONO_TYPE_VALUETYPE: {
2773 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2775 g_assert (ainfo->storage == ArgValuetypeInReg);
2776 if (ainfo->pair_storage [0] != ArgNone) {
2777 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2778 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2780 if (ainfo->pair_storage [1] != ArgNone) {
2781 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2782 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2787 g_assert_not_reached ();
2791 g_assert (greg <= PARAM_REGS);
2795 * mono_arch_finish_dyn_call:
2797 * Store the result of a dyn call into the return value buffer passed to
2798 * start_dyn_call ().
2799 * This function should be as fast as possible, any work which does not depend
2800 * on the actual values of the arguments should be done in
2801 * mono_arch_dyn_call_prepare ().
2804 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2806 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2807 MonoMethodSignature *sig = dinfo->sig;
2808 guint8 *ret = ((DynCallArgs*)buf)->ret;
2809 mgreg_t res = ((DynCallArgs*)buf)->res;
2810 MonoType *sig_ret = mini_get_underlying_type (sig->ret);
2812 switch (sig_ret->type) {
2813 case MONO_TYPE_VOID:
2814 *(gpointer*)ret = NULL;
2816 case MONO_TYPE_STRING:
2817 case MONO_TYPE_CLASS:
2818 case MONO_TYPE_ARRAY:
2819 case MONO_TYPE_SZARRAY:
2820 case MONO_TYPE_OBJECT:
2824 *(gpointer*)ret = GREG_TO_PTR(res);
2830 *(guint8*)ret = res;
2833 *(gint16*)ret = res;
2836 *(guint16*)ret = res;
2839 *(gint32*)ret = res;
2842 *(guint32*)ret = res;
2845 *(gint64*)ret = res;
2848 *(guint64*)ret = res;
2850 case MONO_TYPE_GENERICINST:
2851 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
2852 *(gpointer*)ret = GREG_TO_PTR(res);
2857 case MONO_TYPE_VALUETYPE:
2858 if (dinfo->cinfo->ret.storage == ArgValuetypeAddrInIReg) {
2861 ArgInfo *ainfo = &dinfo->cinfo->ret;
2863 g_assert (ainfo->storage == ArgValuetypeInReg);
2865 if (ainfo->pair_storage [0] != ArgNone) {
2866 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2867 ((mgreg_t*)ret)[0] = res;
2870 g_assert (ainfo->pair_storage [1] == ArgNone);
2874 g_assert_not_reached ();
2878 /* emit an exception if condition is fail */
2879 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2881 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2882 if (tins == NULL) { \
2883 mono_add_patch_info (cfg, code - cfg->native_code, \
2884 MONO_PATCH_INFO_EXC, exc_name); \
2885 x86_branch32 (code, cond, 0, signed); \
2887 EMIT_COND_BRANCH (tins, cond, signed); \
2891 #define EMIT_FPCOMPARE(code) do { \
2892 amd64_fcompp (code); \
2893 amd64_fnstsw (code); \
2896 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
2897 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
2898 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
2899 amd64_ ##op (code); \
2900 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
2901 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
2905 emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
2907 gboolean no_patch = FALSE;
2910 * FIXME: Add support for thunks
2913 gboolean near_call = FALSE;
2916 * Indirect calls are expensive so try to make a near call if possible.
2917 * The caller memory is allocated by the code manager so it is
2918 * guaranteed to be at a 32 bit offset.
2921 if (patch_type != MONO_PATCH_INFO_ABS) {
2922 /* The target is in memory allocated using the code manager */
2925 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
2926 if (((MonoMethod*)data)->klass->image->aot_module)
2927 /* The callee might be an AOT method */
2929 if (((MonoMethod*)data)->dynamic)
2930 /* The target is in malloc-ed memory */
2934 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
2936 * The call might go directly to a native function without
2939 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (data);
2941 gconstpointer target = mono_icall_get_wrapper (mi);
2942 if ((((guint64)target) >> 32) != 0)
2948 MonoJumpInfo *jinfo = NULL;
2950 if (cfg->abs_patches)
2951 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
2953 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2954 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
2955 if (mi && (((guint64)mi->func) >> 32) == 0)
2960 * This is not really an optimization, but required because the
2961 * generic class init trampolines use R11 to pass the vtable.
2966 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
2968 if (info->func == info->wrapper) {
2970 if ((((guint64)info->func) >> 32) == 0)
2974 /* See the comment in mono_codegen () */
2975 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
2979 else if ((((guint64)data) >> 32) == 0) {
2986 if (cfg->method->dynamic)
2987 /* These methods are allocated using malloc */
2990 #ifdef MONO_ARCH_NOMAP32BIT
2993 #if defined(__native_client__)
2994 /* Always use near_call == TRUE for Native Client */
2997 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
2998 if (optimize_for_xen)
3001 if (cfg->compile_aot) {
3008 * Align the call displacement to an address divisible by 4 so it does
3009 * not span cache lines. This is required for code patching to work on SMP
3012 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
3013 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
3014 amd64_padding (code, pad_size);
3016 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3017 amd64_call_code (code, 0);
3020 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3021 amd64_set_reg_template (code, GP_SCRATCH_REG);
3022 amd64_call_reg (code, GP_SCRATCH_REG);
3029 static inline guint8*
3030 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
3033 if (win64_adjust_stack)
3034 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
3036 code = emit_call_body (cfg, code, patch_type, data);
3038 if (win64_adjust_stack)
3039 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
3046 store_membase_imm_to_store_membase_reg (int opcode)
3049 case OP_STORE_MEMBASE_IMM:
3050 return OP_STORE_MEMBASE_REG;
3051 case OP_STOREI4_MEMBASE_IMM:
3052 return OP_STOREI4_MEMBASE_REG;
3053 case OP_STOREI8_MEMBASE_IMM:
3054 return OP_STOREI8_MEMBASE_REG;
3062 #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)))
3065 * mono_arch_peephole_pass_1:
3067 * Perform peephole opts which should/can be performed before local regalloc
3070 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3074 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3075 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3077 switch (ins->opcode) {
3081 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
3083 * X86_LEA is like ADD, but doesn't have the
3084 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
3085 * its operand to 64 bit.
3087 ins->opcode = OP_X86_LEA_MEMBASE;
3088 ins->inst_basereg = ins->sreg1;
3093 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3097 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3098 * the latter has length 2-3 instead of 6 (reverse constant
3099 * propagation). These instruction sequences are very common
3100 * in the initlocals bblock.
3102 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3103 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)) {
3104 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3105 ins2->sreg1 = ins->dreg;
3106 } 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)) {
3108 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3111 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3119 case OP_COMPARE_IMM:
3120 case OP_LCOMPARE_IMM:
3121 /* OP_COMPARE_IMM (reg, 0)
3123 * OP_AMD64_TEST_NULL (reg)
3126 ins->opcode = OP_AMD64_TEST_NULL;
3128 case OP_ICOMPARE_IMM:
3130 ins->opcode = OP_X86_TEST_NULL;
3132 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3134 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3135 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3137 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3138 * OP_COMPARE_IMM reg, imm
3140 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3142 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3143 ins->inst_basereg == last_ins->inst_destbasereg &&
3144 ins->inst_offset == last_ins->inst_offset) {
3145 ins->opcode = OP_ICOMPARE_IMM;
3146 ins->sreg1 = last_ins->sreg1;
3148 /* check if we can remove cmp reg,0 with test null */
3150 ins->opcode = OP_X86_TEST_NULL;
3156 mono_peephole_ins (bb, ins);
3161 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3165 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3166 switch (ins->opcode) {
3169 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3170 /* reg = 0 -> XOR (reg, reg) */
3171 /* XOR sets cflags on x86, so we cant do it always */
3172 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3173 ins->opcode = OP_LXOR;
3174 ins->sreg1 = ins->dreg;
3175 ins->sreg2 = ins->dreg;
3183 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3184 * 0 result into 64 bits.
3186 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3187 ins->opcode = OP_IXOR;
3191 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3195 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3196 * the latter has length 2-3 instead of 6 (reverse constant
3197 * propagation). These instruction sequences are very common
3198 * in the initlocals bblock.
3200 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3201 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)) {
3202 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3203 ins2->sreg1 = ins->dreg;
3204 } 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)) {
3206 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3209 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3218 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3219 ins->opcode = OP_X86_INC_REG;
3222 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3223 ins->opcode = OP_X86_DEC_REG;
3227 mono_peephole_ins (bb, ins);
3231 #define NEW_INS(cfg,ins,dest,op) do { \
3232 MONO_INST_NEW ((cfg), (dest), (op)); \
3233 (dest)->cil_code = (ins)->cil_code; \
3234 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3238 * mono_arch_lowering_pass:
3240 * Converts complex opcodes into simpler ones so that each IR instruction
3241 * corresponds to one machine instruction.
3244 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3246 MonoInst *ins, *n, *temp;
3249 * FIXME: Need to add more instructions, but the current machine
3250 * description can't model some parts of the composite instructions like
3253 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3254 switch (ins->opcode) {
3258 case OP_IDIV_UN_IMM:
3259 case OP_IREM_UN_IMM:
3262 mono_decompose_op_imm (cfg, bb, ins);
3264 case OP_COMPARE_IMM:
3265 case OP_LCOMPARE_IMM:
3266 if (!amd64_use_imm32 (ins->inst_imm)) {
3267 NEW_INS (cfg, ins, temp, OP_I8CONST);
3268 temp->inst_c0 = ins->inst_imm;
3269 temp->dreg = mono_alloc_ireg (cfg);
3270 ins->opcode = OP_COMPARE;
3271 ins->sreg2 = temp->dreg;
3274 #ifndef __mono_ilp32__
3275 case OP_LOAD_MEMBASE:
3277 case OP_LOADI8_MEMBASE:
3278 #ifndef __native_client_codegen__
3279 /* Don't generate memindex opcodes (to simplify */
3280 /* read sandboxing) */
3281 if (!amd64_use_imm32 (ins->inst_offset)) {
3282 NEW_INS (cfg, ins, temp, OP_I8CONST);
3283 temp->inst_c0 = ins->inst_offset;
3284 temp->dreg = mono_alloc_ireg (cfg);
3285 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3286 ins->inst_indexreg = temp->dreg;
3290 #ifndef __mono_ilp32__
3291 case OP_STORE_MEMBASE_IMM:
3293 case OP_STOREI8_MEMBASE_IMM:
3294 if (!amd64_use_imm32 (ins->inst_imm)) {
3295 NEW_INS (cfg, ins, temp, OP_I8CONST);
3296 temp->inst_c0 = ins->inst_imm;
3297 temp->dreg = mono_alloc_ireg (cfg);
3298 ins->opcode = OP_STOREI8_MEMBASE_REG;
3299 ins->sreg1 = temp->dreg;
3302 #ifdef MONO_ARCH_SIMD_INTRINSICS
3303 case OP_EXPAND_I1: {
3304 int temp_reg1 = mono_alloc_ireg (cfg);
3305 int temp_reg2 = mono_alloc_ireg (cfg);
3306 int original_reg = ins->sreg1;
3308 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3309 temp->sreg1 = original_reg;
3310 temp->dreg = temp_reg1;
3312 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3313 temp->sreg1 = temp_reg1;
3314 temp->dreg = temp_reg2;
3317 NEW_INS (cfg, ins, temp, OP_LOR);
3318 temp->sreg1 = temp->dreg = temp_reg2;
3319 temp->sreg2 = temp_reg1;
3321 ins->opcode = OP_EXPAND_I2;
3322 ins->sreg1 = temp_reg2;
3331 bb->max_vreg = cfg->next_vreg;
3335 branch_cc_table [] = {
3336 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3337 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3338 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3341 /* Maps CMP_... constants to X86_CC_... constants */
3344 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3345 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3349 cc_signed_table [] = {
3350 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3351 FALSE, FALSE, FALSE, FALSE
3354 /*#include "cprop.c"*/
3356 static unsigned char*
3357 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3360 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3362 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3365 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3367 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3371 static unsigned char*
3372 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3374 int sreg = tree->sreg1;
3375 int need_touch = FALSE;
3377 #if defined(TARGET_WIN32)
3379 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3380 if (!tree->flags & MONO_INST_INIT)
3389 * If requested stack size is larger than one page,
3390 * perform stack-touch operation
3393 * Generate stack probe code.
3394 * Under Windows, it is necessary to allocate one page at a time,
3395 * "touching" stack after each successful sub-allocation. This is
3396 * because of the way stack growth is implemented - there is a
3397 * guard page before the lowest stack page that is currently commited.
3398 * Stack normally grows sequentially so OS traps access to the
3399 * guard page and commits more pages when needed.
3401 amd64_test_reg_imm (code, sreg, ~0xFFF);
3402 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3404 br[2] = code; /* loop */
3405 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3406 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3407 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3408 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3409 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3410 amd64_patch (br[3], br[2]);
3411 amd64_test_reg_reg (code, sreg, sreg);
3412 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3413 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3415 br[1] = code; x86_jump8 (code, 0);
3417 amd64_patch (br[0], code);
3418 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3419 amd64_patch (br[1], code);
3420 amd64_patch (br[4], code);
3423 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3425 if (tree->flags & MONO_INST_INIT) {
3427 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3428 amd64_push_reg (code, AMD64_RAX);
3431 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3432 amd64_push_reg (code, AMD64_RCX);
3435 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3436 amd64_push_reg (code, AMD64_RDI);
3440 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3441 if (sreg != AMD64_RCX)
3442 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3443 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3445 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3446 if (cfg->param_area)
3447 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3449 #if defined(__default_codegen__)
3450 amd64_prefix (code, X86_REP_PREFIX);
3452 #elif defined(__native_client_codegen__)
3453 /* NaCl stos pseudo-instruction */
3454 amd64_codegen_pre(code);
3455 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3456 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3457 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3458 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3459 amd64_prefix (code, X86_REP_PREFIX);
3461 amd64_codegen_post(code);
3462 #endif /* __native_client_codegen__ */
3464 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3465 amd64_pop_reg (code, AMD64_RDI);
3466 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3467 amd64_pop_reg (code, AMD64_RCX);
3468 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3469 amd64_pop_reg (code, AMD64_RAX);
3475 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3480 /* Move return value to the target register */
3481 /* FIXME: do this in the local reg allocator */
3482 switch (ins->opcode) {
3485 case OP_CALL_MEMBASE:
3488 case OP_LCALL_MEMBASE:
3489 g_assert (ins->dreg == AMD64_RAX);
3493 case OP_FCALL_MEMBASE: {
3494 MonoType *rtype = mini_get_underlying_type (((MonoCallInst*)ins)->signature->ret);
3495 if (rtype->type == MONO_TYPE_R4) {
3496 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3499 if (ins->dreg != AMD64_XMM0)
3500 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3506 case OP_RCALL_MEMBASE:
3507 if (ins->dreg != AMD64_XMM0)
3508 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3512 case OP_VCALL_MEMBASE:
3515 case OP_VCALL2_MEMBASE:
3516 cinfo = get_call_info (cfg->mempool, ((MonoCallInst*)ins)->signature);
3517 if (cinfo->ret.storage == ArgValuetypeInReg) {
3518 MonoInst *loc = cfg->arch.vret_addr_loc;
3520 /* Load the destination address */
3521 g_assert (loc->opcode == OP_REGOFFSET);
3522 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3524 for (quad = 0; quad < 2; quad ++) {
3525 switch (cinfo->ret.pair_storage [quad]) {
3527 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3529 case ArgInFloatSSEReg:
3530 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3532 case ArgInDoubleSSEReg:
3533 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3548 #endif /* DISABLE_JIT */
3551 static int tls_gs_offset;
3555 mono_amd64_have_tls_get (void)
3558 static gboolean have_tls_get = FALSE;
3559 static gboolean inited = FALSE;
3562 return have_tls_get;
3564 #if MONO_HAVE_FAST_TLS
3565 guint8 *ins = (guint8*)pthread_getspecific;
3568 * We're looking for these two instructions:
3570 * mov %gs:[offset](,%rdi,8),%rax
3573 have_tls_get = ins [0] == 0x65 &&
3583 tls_gs_offset = ins[5];
3588 return have_tls_get;
3589 #elif defined(TARGET_ANDROID)
3597 mono_amd64_get_tls_gs_offset (void)
3600 return tls_gs_offset;
3602 g_assert_not_reached ();
3608 * mono_amd64_emit_tls_get:
3609 * @code: buffer to store code to
3610 * @dreg: hard register where to place the result
3611 * @tls_offset: offset info
3613 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3614 * the dreg register the item in the thread local storage identified
3617 * Returns: a pointer to the end of the stored code
3620 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3623 if (tls_offset < 64) {
3624 x86_prefix (code, X86_GS_PREFIX);
3625 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3629 g_assert (tls_offset < 0x440);
3630 /* Load TEB->TlsExpansionSlots */
3631 x86_prefix (code, X86_GS_PREFIX);
3632 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3633 amd64_test_reg_reg (code, dreg, dreg);
3635 amd64_branch (code, X86_CC_EQ, code, TRUE);
3636 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3637 amd64_patch (buf [0], code);
3639 #elif defined(__APPLE__)
3640 x86_prefix (code, X86_GS_PREFIX);
3641 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3643 if (optimize_for_xen) {
3644 x86_prefix (code, X86_FS_PREFIX);
3645 amd64_mov_reg_mem (code, dreg, 0, 8);
3646 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3648 x86_prefix (code, X86_FS_PREFIX);
3649 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3656 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3658 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3660 if (dreg != offset_reg)
3661 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3662 amd64_prefix (code, X86_GS_PREFIX);
3663 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3664 #elif defined(__linux__)
3667 if (dreg == offset_reg) {
3668 /* Use a temporary reg by saving it to the redzone */
3669 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3670 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3671 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3672 offset_reg = tmpreg;
3674 x86_prefix (code, X86_FS_PREFIX);
3675 amd64_mov_reg_mem (code, dreg, 0, 8);
3676 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3678 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3680 g_assert_not_reached ();
3686 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3689 g_assert_not_reached ();
3690 #elif defined(__APPLE__)
3691 x86_prefix (code, X86_GS_PREFIX);
3692 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3694 g_assert (!optimize_for_xen);
3695 x86_prefix (code, X86_FS_PREFIX);
3696 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3702 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3704 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3706 g_assert_not_reached ();
3707 #elif defined(__APPLE__)
3708 x86_prefix (code, X86_GS_PREFIX);
3709 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3711 x86_prefix (code, X86_FS_PREFIX);
3712 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3718 * mono_arch_translate_tls_offset:
3720 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3723 mono_arch_translate_tls_offset (int offset)
3726 return tls_gs_offset + (offset * 8);
3735 * Emit code to initialize an LMF structure at LMF_OFFSET.
3738 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3741 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3744 * sp is saved right before calls but we need to save it here too so
3745 * async stack walks would work.
3747 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3749 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3750 if (cfg->arch.omit_fp && cfa_offset != -1)
3751 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3753 /* These can't contain refs */
3754 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3755 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3756 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3757 /* These are handled automatically by the stack marking code */
3758 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3763 #define REAL_PRINT_REG(text,reg) \
3764 mono_assert (reg >= 0); \
3765 amd64_push_reg (code, AMD64_RAX); \
3766 amd64_push_reg (code, AMD64_RDX); \
3767 amd64_push_reg (code, AMD64_RCX); \
3768 amd64_push_reg (code, reg); \
3769 amd64_push_imm (code, reg); \
3770 amd64_push_imm (code, text " %d %p\n"); \
3771 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
3772 amd64_call_reg (code, AMD64_RAX); \
3773 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
3774 amd64_pop_reg (code, AMD64_RCX); \
3775 amd64_pop_reg (code, AMD64_RDX); \
3776 amd64_pop_reg (code, AMD64_RAX);
3778 /* benchmark and set based on cpu */
3779 #define LOOP_ALIGNMENT 8
3780 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3784 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3789 guint8 *code = cfg->native_code + cfg->code_len;
3792 /* Fix max_offset estimate for each successor bb */
3793 if (cfg->opt & MONO_OPT_BRANCH) {
3794 int current_offset = cfg->code_len;
3795 MonoBasicBlock *current_bb;
3796 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3797 current_bb->max_offset = current_offset;
3798 current_offset += current_bb->max_length;
3802 if (cfg->opt & MONO_OPT_LOOP) {
3803 int pad, align = LOOP_ALIGNMENT;
3804 /* set alignment depending on cpu */
3805 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3807 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3808 amd64_padding (code, pad);
3809 cfg->code_len += pad;
3810 bb->native_offset = cfg->code_len;
3814 #if defined(__native_client_codegen__)
3815 /* For Native Client, all indirect call/jump targets must be */
3816 /* 32-byte aligned. Exception handler blocks are jumped to */
3817 /* indirectly as well. */
3818 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
3819 (bb->flags & BB_EXCEPTION_HANDLER);
3821 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
3822 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
3823 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
3824 cfg->code_len += pad;
3825 bb->native_offset = cfg->code_len;
3827 #endif /*__native_client_codegen__*/
3829 if (cfg->verbose_level > 2)
3830 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3832 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
3833 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3834 g_assert (!cfg->compile_aot);
3836 cov->data [bb->dfn].cil_code = bb->cil_code;
3837 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3838 /* this is not thread save, but good enough */
3839 amd64_inc_membase (code, AMD64_R11, 0);
3842 offset = code - cfg->native_code;
3844 mono_debug_open_block (cfg, bb, offset);
3846 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)
3847 x86_breakpoint (code);
3849 MONO_BB_FOR_EACH_INS (bb, ins) {
3850 offset = code - cfg->native_code;
3852 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3854 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
3856 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
3857 cfg->code_size *= 2;
3858 cfg->native_code = mono_realloc_native_code(cfg);
3859 code = cfg->native_code + offset;
3860 cfg->stat_code_reallocs++;
3863 if (cfg->debug_info)
3864 mono_debug_record_line_number (cfg, ins, offset);
3866 switch (ins->opcode) {
3868 amd64_mul_reg (code, ins->sreg2, TRUE);
3871 amd64_mul_reg (code, ins->sreg2, FALSE);
3873 case OP_X86_SETEQ_MEMBASE:
3874 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3876 case OP_STOREI1_MEMBASE_IMM:
3877 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3879 case OP_STOREI2_MEMBASE_IMM:
3880 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
3882 case OP_STOREI4_MEMBASE_IMM:
3883 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
3885 case OP_STOREI1_MEMBASE_REG:
3886 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
3888 case OP_STOREI2_MEMBASE_REG:
3889 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
3891 /* In AMD64 NaCl, pointers are 4 bytes, */
3892 /* so STORE_* != STOREI8_*. Likewise below. */
3893 case OP_STORE_MEMBASE_REG:
3894 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
3896 case OP_STOREI8_MEMBASE_REG:
3897 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
3899 case OP_STOREI4_MEMBASE_REG:
3900 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
3902 case OP_STORE_MEMBASE_IMM:
3903 #ifndef __native_client_codegen__
3904 /* In NaCl, this could be a PCONST type, which could */
3905 /* mean a pointer type was copied directly into the */
3906 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
3907 /* the value would be 0x00000000FFFFFFFF which is */
3908 /* not proper for an imm32 unless you cast it. */
3909 g_assert (amd64_is_imm32 (ins->inst_imm));
3911 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
3913 case OP_STOREI8_MEMBASE_IMM:
3914 g_assert (amd64_is_imm32 (ins->inst_imm));
3915 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
3918 #ifdef __mono_ilp32__
3919 /* In ILP32, pointers are 4 bytes, so separate these */
3920 /* cases, use literal 8 below where we really want 8 */
3921 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3922 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
3926 // FIXME: Decompose this earlier
3927 if (amd64_use_imm32 (ins->inst_imm))
3928 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
3930 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
3931 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
3935 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3936 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
3939 // FIXME: Decompose this earlier
3940 if (amd64_use_imm32 (ins->inst_imm))
3941 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
3943 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_imm, sizeof(gpointer));
3944 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3948 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3949 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
3952 /* For NaCl, pointers are 4 bytes, so separate these */
3953 /* cases, use literal 8 below where we really want 8 */
3954 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3955 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
3957 case OP_LOAD_MEMBASE:
3958 g_assert (amd64_is_imm32 (ins->inst_offset));
3959 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
3961 case OP_LOADI8_MEMBASE:
3962 /* Use literal 8 instead of sizeof pointer or */
3963 /* register, we really want 8 for this opcode */
3964 g_assert (amd64_is_imm32 (ins->inst_offset));
3965 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
3967 case OP_LOADI4_MEMBASE:
3968 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3970 case OP_LOADU4_MEMBASE:
3971 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
3973 case OP_LOADU1_MEMBASE:
3974 /* The cpu zero extends the result into 64 bits */
3975 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
3977 case OP_LOADI1_MEMBASE:
3978 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
3980 case OP_LOADU2_MEMBASE:
3981 /* The cpu zero extends the result into 64 bits */
3982 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
3984 case OP_LOADI2_MEMBASE:
3985 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
3987 case OP_AMD64_LOADI8_MEMINDEX:
3988 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
3990 case OP_LCONV_TO_I1:
3991 case OP_ICONV_TO_I1:
3993 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3995 case OP_LCONV_TO_I2:
3996 case OP_ICONV_TO_I2:
3998 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
4000 case OP_LCONV_TO_U1:
4001 case OP_ICONV_TO_U1:
4002 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
4004 case OP_LCONV_TO_U2:
4005 case OP_ICONV_TO_U2:
4006 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
4009 /* Clean out the upper word */
4010 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4013 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
4017 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4019 case OP_COMPARE_IMM:
4020 #if defined(__mono_ilp32__)
4021 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
4022 g_assert (amd64_is_imm32 (ins->inst_imm));
4023 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4026 case OP_LCOMPARE_IMM:
4027 g_assert (amd64_is_imm32 (ins->inst_imm));
4028 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
4030 case OP_X86_COMPARE_REG_MEMBASE:
4031 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
4033 case OP_X86_TEST_NULL:
4034 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
4036 case OP_AMD64_TEST_NULL:
4037 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4040 case OP_X86_ADD_REG_MEMBASE:
4041 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4043 case OP_X86_SUB_REG_MEMBASE:
4044 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4046 case OP_X86_AND_REG_MEMBASE:
4047 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4049 case OP_X86_OR_REG_MEMBASE:
4050 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4052 case OP_X86_XOR_REG_MEMBASE:
4053 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4056 case OP_X86_ADD_MEMBASE_IMM:
4057 /* FIXME: Make a 64 version too */
4058 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4060 case OP_X86_SUB_MEMBASE_IMM:
4061 g_assert (amd64_is_imm32 (ins->inst_imm));
4062 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4064 case OP_X86_AND_MEMBASE_IMM:
4065 g_assert (amd64_is_imm32 (ins->inst_imm));
4066 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4068 case OP_X86_OR_MEMBASE_IMM:
4069 g_assert (amd64_is_imm32 (ins->inst_imm));
4070 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4072 case OP_X86_XOR_MEMBASE_IMM:
4073 g_assert (amd64_is_imm32 (ins->inst_imm));
4074 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4076 case OP_X86_ADD_MEMBASE_REG:
4077 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4079 case OP_X86_SUB_MEMBASE_REG:
4080 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4082 case OP_X86_AND_MEMBASE_REG:
4083 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4085 case OP_X86_OR_MEMBASE_REG:
4086 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4088 case OP_X86_XOR_MEMBASE_REG:
4089 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4091 case OP_X86_INC_MEMBASE:
4092 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4094 case OP_X86_INC_REG:
4095 amd64_inc_reg_size (code, ins->dreg, 4);
4097 case OP_X86_DEC_MEMBASE:
4098 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4100 case OP_X86_DEC_REG:
4101 amd64_dec_reg_size (code, ins->dreg, 4);
4103 case OP_X86_MUL_REG_MEMBASE:
4104 case OP_X86_MUL_MEMBASE_REG:
4105 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4107 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4108 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4110 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4111 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4113 case OP_AMD64_COMPARE_MEMBASE_REG:
4114 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4116 case OP_AMD64_COMPARE_MEMBASE_IMM:
4117 g_assert (amd64_is_imm32 (ins->inst_imm));
4118 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4120 case OP_X86_COMPARE_MEMBASE8_IMM:
4121 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4123 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4124 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4126 case OP_AMD64_COMPARE_REG_MEMBASE:
4127 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4130 case OP_AMD64_ADD_REG_MEMBASE:
4131 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4133 case OP_AMD64_SUB_REG_MEMBASE:
4134 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4136 case OP_AMD64_AND_REG_MEMBASE:
4137 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4139 case OP_AMD64_OR_REG_MEMBASE:
4140 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4142 case OP_AMD64_XOR_REG_MEMBASE:
4143 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4146 case OP_AMD64_ADD_MEMBASE_REG:
4147 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4149 case OP_AMD64_SUB_MEMBASE_REG:
4150 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4152 case OP_AMD64_AND_MEMBASE_REG:
4153 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4155 case OP_AMD64_OR_MEMBASE_REG:
4156 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4158 case OP_AMD64_XOR_MEMBASE_REG:
4159 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4162 case OP_AMD64_ADD_MEMBASE_IMM:
4163 g_assert (amd64_is_imm32 (ins->inst_imm));
4164 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4166 case OP_AMD64_SUB_MEMBASE_IMM:
4167 g_assert (amd64_is_imm32 (ins->inst_imm));
4168 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4170 case OP_AMD64_AND_MEMBASE_IMM:
4171 g_assert (amd64_is_imm32 (ins->inst_imm));
4172 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4174 case OP_AMD64_OR_MEMBASE_IMM:
4175 g_assert (amd64_is_imm32 (ins->inst_imm));
4176 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4178 case OP_AMD64_XOR_MEMBASE_IMM:
4179 g_assert (amd64_is_imm32 (ins->inst_imm));
4180 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4184 amd64_breakpoint (code);
4186 case OP_RELAXED_NOP:
4187 x86_prefix (code, X86_REP_PREFIX);
4195 case OP_DUMMY_STORE:
4196 case OP_DUMMY_ICONST:
4197 case OP_DUMMY_R8CONST:
4198 case OP_NOT_REACHED:
4201 case OP_IL_SEQ_POINT:
4202 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4204 case OP_SEQ_POINT: {
4207 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4208 if (cfg->compile_aot) {
4209 MonoInst *var = cfg->arch.ss_tramp_var;
4212 /* Load ss_tramp_var */
4213 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4214 /* Load the trampoline address */
4215 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4216 /* Call it if it is non-null */
4217 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4219 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4220 amd64_call_reg (code, AMD64_R11);
4221 amd64_patch (label, code);
4224 * Read from the single stepping trigger page. This will cause a
4225 * SIGSEGV when single stepping is enabled.
4226 * We do this _before_ the breakpoint, so single stepping after
4227 * a breakpoint is hit will step to the next IL offset.
4229 MonoInst *var = cfg->arch.ss_trigger_page_var;
4231 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4232 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4);
4237 * This is the address which is saved in seq points,
4239 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4241 if (cfg->compile_aot) {
4242 guint32 offset = code - cfg->native_code;
4244 MonoInst *info_var = cfg->arch.seq_point_info_var;
4248 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4249 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4250 /* Load the info->bp_addrs [offset], which is either NULL or the address of the breakpoint trampoline */
4251 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4252 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4254 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4255 /* Call the trampoline */
4256 amd64_call_reg (code, AMD64_R11);
4257 amd64_patch (label, code);
4260 * A placeholder for a possible breakpoint inserted by
4261 * mono_arch_set_breakpoint ().
4263 for (i = 0; i < breakpoint_size; ++i)
4267 * Add an additional nop so skipping the bp doesn't cause the ip to point
4268 * to another IL offset.
4276 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4279 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4283 g_assert (amd64_is_imm32 (ins->inst_imm));
4284 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4287 g_assert (amd64_is_imm32 (ins->inst_imm));
4288 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4293 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4296 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4300 g_assert (amd64_is_imm32 (ins->inst_imm));
4301 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4304 g_assert (amd64_is_imm32 (ins->inst_imm));
4305 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4308 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4312 g_assert (amd64_is_imm32 (ins->inst_imm));
4313 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4316 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4321 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4323 switch (ins->inst_imm) {
4327 if (ins->dreg != ins->sreg1)
4328 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4329 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4332 /* LEA r1, [r2 + r2*2] */
4333 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4336 /* LEA r1, [r2 + r2*4] */
4337 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4340 /* LEA r1, [r2 + r2*2] */
4342 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4343 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4346 /* LEA r1, [r2 + r2*8] */
4347 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4350 /* LEA r1, [r2 + r2*4] */
4352 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4353 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4356 /* LEA r1, [r2 + r2*2] */
4358 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4359 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4362 /* LEA r1, [r2 + r2*4] */
4363 /* LEA r1, [r1 + r1*4] */
4364 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4365 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4368 /* LEA r1, [r2 + r2*4] */
4370 /* LEA r1, [r1 + r1*4] */
4371 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4372 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4373 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4376 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
4383 #if defined( __native_client_codegen__ )
4384 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4385 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4387 /* Regalloc magic makes the div/rem cases the same */
4388 if (ins->sreg2 == AMD64_RDX) {
4389 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4391 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4394 amd64_div_reg (code, ins->sreg2, TRUE);
4399 #if defined( __native_client_codegen__ )
4400 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4401 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4403 if (ins->sreg2 == AMD64_RDX) {
4404 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4405 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4406 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4408 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4409 amd64_div_reg (code, ins->sreg2, FALSE);
4414 #if defined( __native_client_codegen__ )
4415 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4416 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4418 if (ins->sreg2 == AMD64_RDX) {
4419 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4420 amd64_cdq_size (code, 4);
4421 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4423 amd64_cdq_size (code, 4);
4424 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4429 #if defined( __native_client_codegen__ )
4430 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4431 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4433 if (ins->sreg2 == AMD64_RDX) {
4434 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4435 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4436 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4438 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4439 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4443 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4444 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4447 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4451 g_assert (amd64_is_imm32 (ins->inst_imm));
4452 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4455 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4459 g_assert (amd64_is_imm32 (ins->inst_imm));
4460 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4463 g_assert (ins->sreg2 == AMD64_RCX);
4464 amd64_shift_reg (code, X86_SHL, ins->dreg);
4467 g_assert (ins->sreg2 == AMD64_RCX);
4468 amd64_shift_reg (code, X86_SAR, ins->dreg);
4472 g_assert (amd64_is_imm32 (ins->inst_imm));
4473 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4476 g_assert (amd64_is_imm32 (ins->inst_imm));
4477 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4479 case OP_LSHR_UN_IMM:
4480 g_assert (amd64_is_imm32 (ins->inst_imm));
4481 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4484 g_assert (ins->sreg2 == AMD64_RCX);
4485 amd64_shift_reg (code, X86_SHR, ins->dreg);
4489 g_assert (amd64_is_imm32 (ins->inst_imm));
4490 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4495 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4498 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4501 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4504 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4508 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4511 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4514 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4517 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4520 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4523 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4526 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4529 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4532 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4535 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4538 amd64_neg_reg_size (code, ins->sreg1, 4);
4541 amd64_not_reg_size (code, ins->sreg1, 4);
4544 g_assert (ins->sreg2 == AMD64_RCX);
4545 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4548 g_assert (ins->sreg2 == AMD64_RCX);
4549 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4552 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4554 case OP_ISHR_UN_IMM:
4555 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4558 g_assert (ins->sreg2 == AMD64_RCX);
4559 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4562 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4565 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4568 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4569 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4571 case OP_IMUL_OVF_UN:
4572 case OP_LMUL_OVF_UN: {
4573 /* the mul operation and the exception check should most likely be split */
4574 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4575 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4576 /*g_assert (ins->sreg2 == X86_EAX);
4577 g_assert (ins->dreg == X86_EAX);*/
4578 if (ins->sreg2 == X86_EAX) {
4579 non_eax_reg = ins->sreg1;
4580 } else if (ins->sreg1 == X86_EAX) {
4581 non_eax_reg = ins->sreg2;
4583 /* no need to save since we're going to store to it anyway */
4584 if (ins->dreg != X86_EAX) {
4586 amd64_push_reg (code, X86_EAX);
4588 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4589 non_eax_reg = ins->sreg2;
4591 if (ins->dreg == X86_EDX) {
4594 amd64_push_reg (code, X86_EAX);
4598 amd64_push_reg (code, X86_EDX);
4600 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4601 /* save before the check since pop and mov don't change the flags */
4602 if (ins->dreg != X86_EAX)
4603 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4605 amd64_pop_reg (code, X86_EDX);
4607 amd64_pop_reg (code, X86_EAX);
4608 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4612 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4614 case OP_ICOMPARE_IMM:
4615 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4637 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4645 case OP_CMOV_INE_UN:
4646 case OP_CMOV_IGE_UN:
4647 case OP_CMOV_IGT_UN:
4648 case OP_CMOV_ILE_UN:
4649 case OP_CMOV_ILT_UN:
4655 case OP_CMOV_LNE_UN:
4656 case OP_CMOV_LGE_UN:
4657 case OP_CMOV_LGT_UN:
4658 case OP_CMOV_LLE_UN:
4659 case OP_CMOV_LLT_UN:
4660 g_assert (ins->dreg == ins->sreg1);
4661 /* This needs to operate on 64 bit values */
4662 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);
4666 amd64_not_reg (code, ins->sreg1);
4669 amd64_neg_reg (code, ins->sreg1);
4674 if ((((guint64)ins->inst_c0) >> 32) == 0 && !mini_get_debug_options()->single_imm_size)
4675 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4677 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4680 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4681 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4684 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4685 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4688 if (ins->dreg != ins->sreg1)
4689 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4691 case OP_AMD64_SET_XMMREG_R4: {
4693 if (ins->dreg != ins->sreg1)
4694 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4696 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4700 case OP_AMD64_SET_XMMREG_R8: {
4701 if (ins->dreg != ins->sreg1)
4702 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4706 MonoCallInst *call = (MonoCallInst*)ins;
4707 int i, save_area_offset;
4709 g_assert (!cfg->method->save_lmf);
4711 /* Restore callee saved registers */
4712 save_area_offset = cfg->arch.reg_save_area_offset;
4713 for (i = 0; i < AMD64_NREG; ++i)
4714 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4715 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4716 save_area_offset += 8;
4719 if (cfg->arch.omit_fp) {
4720 if (cfg->arch.stack_alloc_size)
4721 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4723 if (call->stack_usage)
4726 /* Copy arguments on the stack to our argument area */
4727 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4728 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4729 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4735 offset = code - cfg->native_code;
4736 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4737 if (cfg->compile_aot)
4738 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4740 amd64_set_reg_template (code, AMD64_R11);
4741 amd64_jump_reg (code, AMD64_R11);
4742 ins->flags |= MONO_INST_GC_CALLSITE;
4743 ins->backend.pc_offset = code - cfg->native_code;
4747 /* ensure ins->sreg1 is not NULL */
4748 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4751 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4752 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4762 call = (MonoCallInst*)ins;
4764 * The AMD64 ABI forces callers to know about varargs.
4766 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4767 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4768 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4770 * Since the unmanaged calling convention doesn't contain a
4771 * 'vararg' entry, we have to treat every pinvoke call as a
4772 * potential vararg call.
4776 for (i = 0; i < AMD64_XMM_NREG; ++i)
4777 if (call->used_fregs & (1 << i))
4780 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4782 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4785 if (ins->flags & MONO_INST_HAS_METHOD)
4786 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4788 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
4789 ins->flags |= MONO_INST_GC_CALLSITE;
4790 ins->backend.pc_offset = code - cfg->native_code;
4791 code = emit_move_return_value (cfg, ins, code);
4798 case OP_VOIDCALL_REG:
4800 call = (MonoCallInst*)ins;
4802 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4803 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4804 ins->sreg1 = AMD64_R11;
4808 * The AMD64 ABI forces callers to know about varargs.
4810 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4811 if (ins->sreg1 == AMD64_RAX) {
4812 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4813 ins->sreg1 = AMD64_R11;
4815 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4816 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4818 * Since the unmanaged calling convention doesn't contain a
4819 * 'vararg' entry, we have to treat every pinvoke call as a
4820 * potential vararg call.
4824 for (i = 0; i < AMD64_XMM_NREG; ++i)
4825 if (call->used_fregs & (1 << i))
4827 if (ins->sreg1 == AMD64_RAX) {
4828 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4829 ins->sreg1 = AMD64_R11;
4832 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4834 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4837 amd64_call_reg (code, ins->sreg1);
4838 ins->flags |= MONO_INST_GC_CALLSITE;
4839 ins->backend.pc_offset = code - cfg->native_code;
4840 code = emit_move_return_value (cfg, ins, code);
4842 case OP_FCALL_MEMBASE:
4843 case OP_RCALL_MEMBASE:
4844 case OP_LCALL_MEMBASE:
4845 case OP_VCALL_MEMBASE:
4846 case OP_VCALL2_MEMBASE:
4847 case OP_VOIDCALL_MEMBASE:
4848 case OP_CALL_MEMBASE:
4849 call = (MonoCallInst*)ins;
4851 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
4852 ins->flags |= MONO_INST_GC_CALLSITE;
4853 ins->backend.pc_offset = code - cfg->native_code;
4854 code = emit_move_return_value (cfg, ins, code);
4858 MonoInst *var = cfg->dyn_call_var;
4860 g_assert (var->opcode == OP_REGOFFSET);
4862 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
4863 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4865 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
4867 /* Save args buffer */
4868 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
4870 /* Set argument registers */
4871 for (i = 0; i < PARAM_REGS; ++i)
4872 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
4875 amd64_call_reg (code, AMD64_R10);
4877 ins->flags |= MONO_INST_GC_CALLSITE;
4878 ins->backend.pc_offset = code - cfg->native_code;
4881 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4882 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
4885 case OP_AMD64_SAVE_SP_TO_LMF: {
4886 MonoInst *lmf_var = cfg->lmf_var;
4887 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
4891 g_assert_not_reached ();
4892 amd64_push_reg (code, ins->sreg1);
4894 case OP_X86_PUSH_IMM:
4895 g_assert_not_reached ();
4896 g_assert (amd64_is_imm32 (ins->inst_imm));
4897 amd64_push_imm (code, ins->inst_imm);
4899 case OP_X86_PUSH_MEMBASE:
4900 g_assert_not_reached ();
4901 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
4903 case OP_X86_PUSH_OBJ: {
4904 int size = ALIGN_TO (ins->inst_imm, 8);
4906 g_assert_not_reached ();
4908 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4909 amd64_push_reg (code, AMD64_RDI);
4910 amd64_push_reg (code, AMD64_RSI);
4911 amd64_push_reg (code, AMD64_RCX);
4912 if (ins->inst_offset)
4913 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
4915 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
4916 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
4917 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
4919 amd64_prefix (code, X86_REP_PREFIX);
4921 amd64_pop_reg (code, AMD64_RCX);
4922 amd64_pop_reg (code, AMD64_RSI);
4923 amd64_pop_reg (code, AMD64_RDI);
4926 case OP_GENERIC_CLASS_INIT: {
4927 static int byte_offset = -1;
4928 static guint8 bitmask;
4931 g_assert (ins->sreg1 == MONO_AMD64_ARG_REG1);
4933 if (byte_offset < 0)
4934 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
4936 amd64_test_membase_imm_size (code, ins->sreg1, byte_offset, bitmask, 1);
4938 amd64_branch8 (code, X86_CC_NZ, -1, 1);
4940 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_generic_class_init", FALSE);
4941 ins->flags |= MONO_INST_GC_CALLSITE;
4942 ins->backend.pc_offset = code - cfg->native_code;
4944 x86_patch (jump, code);
4949 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
4951 case OP_X86_LEA_MEMBASE:
4952 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
4955 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
4958 /* keep alignment */
4959 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
4960 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
4961 code = mono_emit_stack_alloc (cfg, code, ins);
4962 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4963 if (cfg->param_area)
4964 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4966 case OP_LOCALLOC_IMM: {
4967 guint32 size = ins->inst_imm;
4968 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
4970 if (ins->flags & MONO_INST_INIT) {
4974 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4975 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4977 for (i = 0; i < size; i += 8)
4978 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
4979 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4981 amd64_mov_reg_imm (code, ins->dreg, size);
4982 ins->sreg1 = ins->dreg;
4984 code = mono_emit_stack_alloc (cfg, code, ins);
4985 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4988 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4989 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4991 if (cfg->param_area)
4992 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4996 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4997 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4998 (gpointer)"mono_arch_throw_exception", FALSE);
4999 ins->flags |= MONO_INST_GC_CALLSITE;
5000 ins->backend.pc_offset = code - cfg->native_code;
5004 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
5005 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
5006 (gpointer)"mono_arch_rethrow_exception", FALSE);
5007 ins->flags |= MONO_INST_GC_CALLSITE;
5008 ins->backend.pc_offset = code - cfg->native_code;
5011 case OP_CALL_HANDLER:
5013 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
5014 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5015 amd64_call_imm (code, 0);
5016 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
5017 /* Restore stack alignment */
5018 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
5020 case OP_START_HANDLER: {
5021 /* Even though we're saving RSP, use sizeof */
5022 /* gpointer because spvar is of type IntPtr */
5023 /* see: mono_create_spvar_for_region */
5024 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5025 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
5027 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
5028 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
5030 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
5034 case OP_ENDFINALLY: {
5035 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5036 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5040 case OP_ENDFILTER: {
5041 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5042 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5043 /* The local allocator will put the result into RAX */
5048 if (ins->dreg != AMD64_RAX)
5049 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, sizeof (gpointer));
5052 ins->inst_c0 = code - cfg->native_code;
5055 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
5056 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
5058 if (ins->inst_target_bb->native_offset) {
5059 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
5061 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5062 if ((cfg->opt & MONO_OPT_BRANCH) &&
5063 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
5064 x86_jump8 (code, 0);
5066 x86_jump32 (code, 0);
5070 amd64_jump_reg (code, ins->sreg1);
5093 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5094 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5096 case OP_COND_EXC_EQ:
5097 case OP_COND_EXC_NE_UN:
5098 case OP_COND_EXC_LT:
5099 case OP_COND_EXC_LT_UN:
5100 case OP_COND_EXC_GT:
5101 case OP_COND_EXC_GT_UN:
5102 case OP_COND_EXC_GE:
5103 case OP_COND_EXC_GE_UN:
5104 case OP_COND_EXC_LE:
5105 case OP_COND_EXC_LE_UN:
5106 case OP_COND_EXC_IEQ:
5107 case OP_COND_EXC_INE_UN:
5108 case OP_COND_EXC_ILT:
5109 case OP_COND_EXC_ILT_UN:
5110 case OP_COND_EXC_IGT:
5111 case OP_COND_EXC_IGT_UN:
5112 case OP_COND_EXC_IGE:
5113 case OP_COND_EXC_IGE_UN:
5114 case OP_COND_EXC_ILE:
5115 case OP_COND_EXC_ILE_UN:
5116 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
5118 case OP_COND_EXC_OV:
5119 case OP_COND_EXC_NO:
5121 case OP_COND_EXC_NC:
5122 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5123 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
5125 case OP_COND_EXC_IOV:
5126 case OP_COND_EXC_INO:
5127 case OP_COND_EXC_IC:
5128 case OP_COND_EXC_INC:
5129 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5130 (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
5133 /* floating point opcodes */
5135 double d = *(double *)ins->inst_p0;
5137 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5138 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5141 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5142 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5147 float f = *(float *)ins->inst_p0;
5149 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5151 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5153 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5156 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5157 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5159 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5163 case OP_STORER8_MEMBASE_REG:
5164 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5166 case OP_LOADR8_MEMBASE:
5167 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5169 case OP_STORER4_MEMBASE_REG:
5171 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5173 /* This requires a double->single conversion */
5174 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5175 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5178 case OP_LOADR4_MEMBASE:
5180 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5182 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5183 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5186 case OP_ICONV_TO_R4:
5188 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5190 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5191 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5194 case OP_ICONV_TO_R8:
5195 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5197 case OP_LCONV_TO_R4:
5199 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5201 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5202 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5205 case OP_LCONV_TO_R8:
5206 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5208 case OP_FCONV_TO_R4:
5210 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5212 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5213 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5216 case OP_FCONV_TO_I1:
5217 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5219 case OP_FCONV_TO_U1:
5220 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5222 case OP_FCONV_TO_I2:
5223 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5225 case OP_FCONV_TO_U2:
5226 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5228 case OP_FCONV_TO_U4:
5229 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5231 case OP_FCONV_TO_I4:
5233 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5235 case OP_FCONV_TO_I8:
5236 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5239 case OP_RCONV_TO_I1:
5240 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5241 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5243 case OP_RCONV_TO_U1:
5244 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5245 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5247 case OP_RCONV_TO_I2:
5248 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5249 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5251 case OP_RCONV_TO_U2:
5252 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5253 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5255 case OP_RCONV_TO_I4:
5256 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5258 case OP_RCONV_TO_U4:
5259 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5261 case OP_RCONV_TO_I8:
5262 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5264 case OP_RCONV_TO_R8:
5265 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5267 case OP_RCONV_TO_R4:
5268 if (ins->dreg != ins->sreg1)
5269 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5272 case OP_LCONV_TO_R_UN: {
5275 /* Based on gcc code */
5276 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5277 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5280 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5281 br [1] = code; x86_jump8 (code, 0);
5282 amd64_patch (br [0], code);
5285 /* Save to the red zone */
5286 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5287 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5288 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5289 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5290 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5291 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5292 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5293 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5294 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5296 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5297 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5298 amd64_patch (br [1], code);
5301 case OP_LCONV_TO_OVF_U4:
5302 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5303 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5304 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5306 case OP_LCONV_TO_OVF_I4_UN:
5307 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5308 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5309 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5312 if (ins->dreg != ins->sreg1)
5313 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5316 if (ins->dreg != ins->sreg1)
5317 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5319 case OP_MOVE_F_TO_I4:
5321 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5323 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5324 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5327 case OP_MOVE_I4_TO_F:
5328 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5330 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5332 case OP_MOVE_F_TO_I8:
5333 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5335 case OP_MOVE_I8_TO_F:
5336 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5339 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5342 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5345 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5348 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5351 static double r8_0 = -0.0;
5353 g_assert (ins->sreg1 == ins->dreg);
5355 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5356 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5360 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5363 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5366 static guint64 d = 0x7fffffffffffffffUL;
5368 g_assert (ins->sreg1 == ins->dreg);
5370 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5371 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5375 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5379 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5382 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5385 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5388 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5391 static float r4_0 = -0.0;
5393 g_assert (ins->sreg1 == ins->dreg);
5395 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5396 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5397 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
5402 g_assert (cfg->opt & MONO_OPT_CMOV);
5403 g_assert (ins->dreg == ins->sreg1);
5404 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5405 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
5408 g_assert (cfg->opt & MONO_OPT_CMOV);
5409 g_assert (ins->dreg == ins->sreg1);
5410 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5411 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
5414 g_assert (cfg->opt & MONO_OPT_CMOV);
5415 g_assert (ins->dreg == ins->sreg1);
5416 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5417 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
5420 g_assert (cfg->opt & MONO_OPT_CMOV);
5421 g_assert (ins->dreg == ins->sreg1);
5422 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5423 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
5426 g_assert (cfg->opt & MONO_OPT_CMOV);
5427 g_assert (ins->dreg == ins->sreg1);
5428 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5429 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
5432 g_assert (cfg->opt & MONO_OPT_CMOV);
5433 g_assert (ins->dreg == ins->sreg1);
5434 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5435 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
5438 g_assert (cfg->opt & MONO_OPT_CMOV);
5439 g_assert (ins->dreg == ins->sreg1);
5440 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5441 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
5444 g_assert (cfg->opt & MONO_OPT_CMOV);
5445 g_assert (ins->dreg == ins->sreg1);
5446 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5447 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5453 * The two arguments are swapped because the fbranch instructions
5454 * depend on this for the non-sse case to work.
5456 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5460 * FIXME: Get rid of this.
5461 * The two arguments are swapped because the fbranch instructions
5462 * depend on this for the non-sse case to work.
5464 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5468 /* zeroing the register at the start results in
5469 * shorter and faster code (we can also remove the widening op)
5471 guchar *unordered_check;
5473 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5474 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5475 unordered_check = code;
5476 x86_branch8 (code, X86_CC_P, 0, FALSE);
5478 if (ins->opcode == OP_FCEQ) {
5479 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5480 amd64_patch (unordered_check, code);
5482 guchar *jump_to_end;
5483 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5485 x86_jump8 (code, 0);
5486 amd64_patch (unordered_check, code);
5487 amd64_inc_reg (code, ins->dreg);
5488 amd64_patch (jump_to_end, code);
5494 /* zeroing the register at the start results in
5495 * shorter and faster code (we can also remove the widening op)
5497 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5498 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5499 if (ins->opcode == OP_FCLT_UN) {
5500 guchar *unordered_check = code;
5501 guchar *jump_to_end;
5502 x86_branch8 (code, X86_CC_P, 0, FALSE);
5503 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5505 x86_jump8 (code, 0);
5506 amd64_patch (unordered_check, code);
5507 amd64_inc_reg (code, ins->dreg);
5508 amd64_patch (jump_to_end, code);
5510 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5515 guchar *unordered_check;
5516 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5517 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5518 unordered_check = code;
5519 x86_branch8 (code, X86_CC_P, 0, FALSE);
5520 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5521 amd64_patch (unordered_check, code);
5526 /* zeroing the register at the start results in
5527 * shorter and faster code (we can also remove the widening op)
5529 guchar *unordered_check;
5531 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5532 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5533 if (ins->opcode == OP_FCGT) {
5534 unordered_check = code;
5535 x86_branch8 (code, X86_CC_P, 0, FALSE);
5536 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5537 amd64_patch (unordered_check, code);
5539 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5544 guchar *unordered_check;
5545 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5546 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5547 unordered_check = code;
5548 x86_branch8 (code, X86_CC_P, 0, FALSE);
5549 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5550 amd64_patch (unordered_check, code);
5560 gboolean unordered = FALSE;
5562 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5563 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5565 switch (ins->opcode) {
5567 x86_cond = X86_CC_EQ;
5570 x86_cond = X86_CC_LT;
5573 x86_cond = X86_CC_GT;
5576 x86_cond = X86_CC_GT;
5580 x86_cond = X86_CC_LT;
5584 g_assert_not_reached ();
5589 guchar *unordered_check;
5590 guchar *jump_to_end;
5592 unordered_check = code;
5593 x86_branch8 (code, X86_CC_P, 0, FALSE);
5594 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5596 x86_jump8 (code, 0);
5597 amd64_patch (unordered_check, code);
5598 amd64_inc_reg (code, ins->dreg);
5599 amd64_patch (jump_to_end, code);
5601 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5605 case OP_FCLT_MEMBASE:
5606 case OP_FCGT_MEMBASE:
5607 case OP_FCLT_UN_MEMBASE:
5608 case OP_FCGT_UN_MEMBASE:
5609 case OP_FCEQ_MEMBASE: {
5610 guchar *unordered_check, *jump_to_end;
5613 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5614 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5616 switch (ins->opcode) {
5617 case OP_FCEQ_MEMBASE:
5618 x86_cond = X86_CC_EQ;
5620 case OP_FCLT_MEMBASE:
5621 case OP_FCLT_UN_MEMBASE:
5622 x86_cond = X86_CC_LT;
5624 case OP_FCGT_MEMBASE:
5625 case OP_FCGT_UN_MEMBASE:
5626 x86_cond = X86_CC_GT;
5629 g_assert_not_reached ();
5632 unordered_check = code;
5633 x86_branch8 (code, X86_CC_P, 0, FALSE);
5634 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5636 switch (ins->opcode) {
5637 case OP_FCEQ_MEMBASE:
5638 case OP_FCLT_MEMBASE:
5639 case OP_FCGT_MEMBASE:
5640 amd64_patch (unordered_check, code);
5642 case OP_FCLT_UN_MEMBASE:
5643 case OP_FCGT_UN_MEMBASE:
5645 x86_jump8 (code, 0);
5646 amd64_patch (unordered_check, code);
5647 amd64_inc_reg (code, ins->dreg);
5648 amd64_patch (jump_to_end, code);
5656 guchar *jump = code;
5657 x86_branch8 (code, X86_CC_P, 0, TRUE);
5658 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5659 amd64_patch (jump, code);
5663 /* Branch if C013 != 100 */
5664 /* branch if !ZF or (PF|CF) */
5665 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5666 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5667 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5670 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5673 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5674 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5678 if (ins->opcode == OP_FBGT) {
5681 /* skip branch if C1=1 */
5683 x86_branch8 (code, X86_CC_P, 0, FALSE);
5684 /* branch if (C0 | C3) = 1 */
5685 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5686 amd64_patch (br1, code);
5689 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5693 /* Branch if C013 == 100 or 001 */
5696 /* skip branch if C1=1 */
5698 x86_branch8 (code, X86_CC_P, 0, FALSE);
5699 /* branch if (C0 | C3) = 1 */
5700 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5701 amd64_patch (br1, code);
5705 /* Branch if C013 == 000 */
5706 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5709 /* Branch if C013=000 or 100 */
5712 /* skip branch if C1=1 */
5714 x86_branch8 (code, X86_CC_P, 0, FALSE);
5715 /* branch if C0=0 */
5716 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5717 amd64_patch (br1, code);
5721 /* Branch if C013 != 001 */
5722 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5723 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5726 /* Transfer value to the fp stack */
5727 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5728 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5729 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5731 amd64_push_reg (code, AMD64_RAX);
5733 amd64_fnstsw (code);
5734 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5735 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5736 amd64_pop_reg (code, AMD64_RAX);
5737 amd64_fstp (code, 0);
5738 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
5739 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5742 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5745 case OP_TLS_GET_REG:
5746 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5749 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5752 case OP_TLS_SET_REG: {
5753 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5756 case OP_MEMORY_BARRIER: {
5757 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5761 case OP_ATOMIC_ADD_I4:
5762 case OP_ATOMIC_ADD_I8: {
5763 int dreg = ins->dreg;
5764 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5766 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5769 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5770 amd64_prefix (code, X86_LOCK_PREFIX);
5771 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5772 /* dreg contains the old value, add with sreg2 value */
5773 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5775 if (ins->dreg != dreg)
5776 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5780 case OP_ATOMIC_EXCHANGE_I4:
5781 case OP_ATOMIC_EXCHANGE_I8: {
5782 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5784 /* LOCK prefix is implied. */
5785 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5786 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5787 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5790 case OP_ATOMIC_CAS_I4:
5791 case OP_ATOMIC_CAS_I8: {
5794 if (ins->opcode == OP_ATOMIC_CAS_I8)
5800 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5801 * an explanation of how this works.
5803 g_assert (ins->sreg3 == AMD64_RAX);
5804 g_assert (ins->sreg1 != AMD64_RAX);
5805 g_assert (ins->sreg1 != ins->sreg2);
5807 amd64_prefix (code, X86_LOCK_PREFIX);
5808 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
5810 if (ins->dreg != AMD64_RAX)
5811 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
5814 case OP_ATOMIC_LOAD_I1: {
5815 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
5818 case OP_ATOMIC_LOAD_U1: {
5819 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
5822 case OP_ATOMIC_LOAD_I2: {
5823 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
5826 case OP_ATOMIC_LOAD_U2: {
5827 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
5830 case OP_ATOMIC_LOAD_I4: {
5831 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5834 case OP_ATOMIC_LOAD_U4:
5835 case OP_ATOMIC_LOAD_I8:
5836 case OP_ATOMIC_LOAD_U8: {
5837 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
5840 case OP_ATOMIC_LOAD_R4: {
5841 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5842 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5845 case OP_ATOMIC_LOAD_R8: {
5846 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5849 case OP_ATOMIC_STORE_I1:
5850 case OP_ATOMIC_STORE_U1:
5851 case OP_ATOMIC_STORE_I2:
5852 case OP_ATOMIC_STORE_U2:
5853 case OP_ATOMIC_STORE_I4:
5854 case OP_ATOMIC_STORE_U4:
5855 case OP_ATOMIC_STORE_I8:
5856 case OP_ATOMIC_STORE_U8: {
5859 switch (ins->opcode) {
5860 case OP_ATOMIC_STORE_I1:
5861 case OP_ATOMIC_STORE_U1:
5864 case OP_ATOMIC_STORE_I2:
5865 case OP_ATOMIC_STORE_U2:
5868 case OP_ATOMIC_STORE_I4:
5869 case OP_ATOMIC_STORE_U4:
5872 case OP_ATOMIC_STORE_I8:
5873 case OP_ATOMIC_STORE_U8:
5878 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
5880 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5884 case OP_ATOMIC_STORE_R4: {
5885 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5886 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5888 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5892 case OP_ATOMIC_STORE_R8: {
5895 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5899 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5903 case OP_CARD_TABLE_WBARRIER: {
5904 int ptr = ins->sreg1;
5905 int value = ins->sreg2;
5907 int nursery_shift, card_table_shift;
5908 gpointer card_table_mask;
5909 size_t nursery_size;
5911 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
5912 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
5913 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
5915 /*If either point to the stack we can simply avoid the WB. This happens due to
5916 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
5918 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
5922 * We need one register we can clobber, we choose EDX and make sreg1
5923 * fixed EAX to work around limitations in the local register allocator.
5924 * sreg2 might get allocated to EDX, but that is not a problem since
5925 * we use it before clobbering EDX.
5927 g_assert (ins->sreg1 == AMD64_RAX);
5930 * This is the code we produce:
5933 * edx >>= nursery_shift
5934 * cmp edx, (nursery_start >> nursery_shift)
5937 * edx >>= card_table_shift
5943 if (mono_gc_card_table_nursery_check ()) {
5944 if (value != AMD64_RDX)
5945 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
5946 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
5947 if (shifted_nursery_start >> 31) {
5949 * The value we need to compare against is 64 bits, so we need
5950 * another spare register. We use RBX, which we save and
5953 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
5954 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
5955 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
5956 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
5958 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
5960 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
5962 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
5963 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
5964 if (card_table_mask)
5965 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
5967 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
5968 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
5970 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
5972 if (mono_gc_card_table_nursery_check ())
5973 x86_patch (br, code);
5976 #ifdef MONO_ARCH_SIMD_INTRINSICS
5977 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
5979 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
5982 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
5985 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
5988 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
5991 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
5994 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
5997 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
5998 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6001 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
6004 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
6007 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
6010 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
6013 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6016 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
6019 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
6022 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6025 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
6028 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
6031 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
6034 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
6037 case OP_PSHUFLEW_HIGH:
6038 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6039 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6041 case OP_PSHUFLEW_LOW:
6042 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6043 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6046 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6047 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6050 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6051 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6054 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
6055 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6059 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
6062 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
6065 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
6068 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
6071 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
6074 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
6077 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6078 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6081 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
6084 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
6087 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
6090 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6093 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6096 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6099 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6102 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6105 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6108 case OP_EXTRACT_MASK:
6109 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6113 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6116 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6119 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6123 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6126 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6129 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6132 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6136 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6139 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6142 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6145 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6149 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6152 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6155 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6159 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6162 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6165 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6169 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6172 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6176 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6179 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6182 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6186 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6189 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6192 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6196 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6199 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6202 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6205 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6209 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6212 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6215 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6218 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6221 case OP_PSUM_ABS_DIFF:
6222 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6225 case OP_UNPACK_LOWB:
6226 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6228 case OP_UNPACK_LOWW:
6229 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6231 case OP_UNPACK_LOWD:
6232 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6234 case OP_UNPACK_LOWQ:
6235 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6237 case OP_UNPACK_LOWPS:
6238 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6240 case OP_UNPACK_LOWPD:
6241 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6244 case OP_UNPACK_HIGHB:
6245 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6247 case OP_UNPACK_HIGHW:
6248 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6250 case OP_UNPACK_HIGHD:
6251 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6253 case OP_UNPACK_HIGHQ:
6254 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6256 case OP_UNPACK_HIGHPS:
6257 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6259 case OP_UNPACK_HIGHPD:
6260 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6264 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6267 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6270 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6273 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6276 case OP_PADDB_SAT_UN:
6277 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6279 case OP_PSUBB_SAT_UN:
6280 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6282 case OP_PADDW_SAT_UN:
6283 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6285 case OP_PSUBW_SAT_UN:
6286 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6290 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6293 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6296 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6299 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6303 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6306 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6309 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6311 case OP_PMULW_HIGH_UN:
6312 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6315 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6319 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6322 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6326 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6329 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6333 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6336 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6340 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6343 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6347 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6350 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6354 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6357 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6361 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6364 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6367 /*TODO: This is appart of the sse spec but not added
6369 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6372 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6377 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6380 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6383 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6386 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6389 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6392 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6395 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6398 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6401 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6404 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6408 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6411 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6415 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6416 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6418 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6423 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6425 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6426 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6430 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6432 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6433 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6434 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6438 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6440 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6443 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6445 case OP_EXTRACTX_U2:
6446 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6448 case OP_INSERTX_U1_SLOW:
6449 /*sreg1 is the extracted ireg (scratch)
6450 /sreg2 is the to be inserted ireg (scratch)
6451 /dreg is the xreg to receive the value*/
6453 /*clear the bits from the extracted word*/
6454 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6455 /*shift the value to insert if needed*/
6456 if (ins->inst_c0 & 1)
6457 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6458 /*join them together*/
6459 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6460 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6462 case OP_INSERTX_I4_SLOW:
6463 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6464 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6465 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6467 case OP_INSERTX_I8_SLOW:
6468 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6470 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6472 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6475 case OP_INSERTX_R4_SLOW:
6476 switch (ins->inst_c0) {
6479 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6481 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6484 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6486 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6488 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6489 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6492 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6494 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6496 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6497 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6500 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6502 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6504 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6505 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6509 case OP_INSERTX_R8_SLOW:
6511 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6513 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6515 case OP_STOREX_MEMBASE_REG:
6516 case OP_STOREX_MEMBASE:
6517 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6519 case OP_LOADX_MEMBASE:
6520 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6522 case OP_LOADX_ALIGNED_MEMBASE:
6523 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6525 case OP_STOREX_ALIGNED_MEMBASE_REG:
6526 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6528 case OP_STOREX_NTA_MEMBASE_REG:
6529 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6531 case OP_PREFETCH_MEMBASE:
6532 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6536 /*FIXME the peephole pass should have killed this*/
6537 if (ins->dreg != ins->sreg1)
6538 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6541 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6543 case OP_ICONV_TO_R4_RAW:
6544 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6547 case OP_FCONV_TO_R8_X:
6548 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6551 case OP_XCONV_R8_TO_I4:
6552 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6553 switch (ins->backend.source_opcode) {
6554 case OP_FCONV_TO_I1:
6555 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6557 case OP_FCONV_TO_U1:
6558 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6560 case OP_FCONV_TO_I2:
6561 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6563 case OP_FCONV_TO_U2:
6564 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6570 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6571 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6572 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6575 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6576 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6579 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6580 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6584 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6586 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6587 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6589 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6592 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6593 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6596 case OP_LIVERANGE_START: {
6597 if (cfg->verbose_level > 1)
6598 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6599 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6602 case OP_LIVERANGE_END: {
6603 if (cfg->verbose_level > 1)
6604 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6605 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6608 case OP_GC_SAFE_POINT: {
6609 const char *polling_func = NULL;
6610 int compare_val = 0;
6613 #if defined (USE_COOP_GC)
6614 polling_func = "mono_threads_state_poll";
6616 #elif defined(__native_client_codegen__) && defined(__native_client_gc__)
6617 polling_func = "mono_nacl_gc";
6618 compare_val = 0xFFFFFFFF;
6623 amd64_test_membase_imm_size (code, ins->sreg1, 0, compare_val, 4);
6624 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6625 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func, FALSE);
6626 amd64_patch (br[0], code);
6630 case OP_GC_LIVENESS_DEF:
6631 case OP_GC_LIVENESS_USE:
6632 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6633 ins->backend.pc_offset = code - cfg->native_code;
6635 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6636 ins->backend.pc_offset = code - cfg->native_code;
6637 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6640 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6641 g_assert_not_reached ();
6644 if ((code - cfg->native_code - offset) > max_len) {
6645 #if !defined(__native_client_codegen__)
6646 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6647 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6648 g_assert_not_reached ();
6653 cfg->code_len = code - cfg->native_code;
6656 #endif /* DISABLE_JIT */
6659 mono_arch_register_lowlevel_calls (void)
6661 /* The signature doesn't matter */
6662 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6666 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
6668 unsigned char *ip = ji->ip.i + code;
6671 * Debug code to help track down problems where the target of a near call is
6674 if (amd64_is_near_call (ip)) {
6675 gint64 disp = (guint8*)target - (guint8*)ip;
6677 if (!amd64_is_imm32 (disp)) {
6678 printf ("TYPE: %d\n", ji->type);
6680 case MONO_PATCH_INFO_INTERNAL_METHOD:
6681 printf ("V: %s\n", ji->data.name);
6683 case MONO_PATCH_INFO_METHOD_JUMP:
6684 case MONO_PATCH_INFO_METHOD:
6685 printf ("V: %s\n", ji->data.method->name);
6693 amd64_patch (ip, (gpointer)target);
6699 get_max_epilog_size (MonoCompile *cfg)
6701 int max_epilog_size = 16;
6703 if (cfg->method->save_lmf)
6704 max_epilog_size += 256;
6706 if (mono_jit_trace_calls != NULL)
6707 max_epilog_size += 50;
6709 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6710 max_epilog_size += 50;
6712 max_epilog_size += (AMD64_NREG * 2);
6714 return max_epilog_size;
6718 * This macro is used for testing whenever the unwinder works correctly at every point
6719 * where an async exception can happen.
6721 /* This will generate a SIGSEGV at the given point in the code */
6722 #define async_exc_point(code) do { \
6723 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6724 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6725 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6726 cfg->arch.async_point_count ++; \
6731 mono_arch_emit_prolog (MonoCompile *cfg)
6733 MonoMethod *method = cfg->method;
6735 MonoMethodSignature *sig;
6737 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6740 MonoInst *lmf_var = cfg->lmf_var;
6741 gboolean args_clobbered = FALSE;
6742 gboolean trace = FALSE;
6743 #ifdef __native_client_codegen__
6744 guint alignment_check;
6747 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6749 #if defined(__default_codegen__)
6750 code = cfg->native_code = g_malloc (cfg->code_size);
6751 #elif defined(__native_client_codegen__)
6752 /* native_code_alloc is not 32-byte aligned, native_code is. */
6753 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6755 /* Align native_code to next nearest kNaclAlignment byte. */
6756 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6757 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6759 code = cfg->native_code;
6761 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6762 g_assert (alignment_check == 0);
6765 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6768 /* Amount of stack space allocated by register saving code */
6771 /* Offset between RSP and the CFA */
6775 * The prolog consists of the following parts:
6777 * - push rbp, mov rbp, rsp
6778 * - save callee saved regs using pushes
6780 * - save rgctx if needed
6781 * - save lmf if needed
6784 * - save rgctx if needed
6785 * - save lmf if needed
6786 * - save callee saved regs using moves
6791 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6792 // IP saved at CFA - 8
6793 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6794 async_exc_point (code);
6795 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6797 if (!cfg->arch.omit_fp) {
6798 amd64_push_reg (code, AMD64_RBP);
6800 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6801 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6802 async_exc_point (code);
6804 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6806 /* These are handled automatically by the stack marking code */
6807 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6809 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
6810 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
6811 async_exc_point (code);
6813 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6817 /* The param area is always at offset 0 from sp */
6818 /* This needs to be allocated here, since it has to come after the spill area */
6819 if (cfg->param_area) {
6820 if (cfg->arch.omit_fp)
6822 g_assert_not_reached ();
6823 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
6826 if (cfg->arch.omit_fp) {
6828 * On enter, the stack is misaligned by the pushing of the return
6829 * address. It is either made aligned by the pushing of %rbp, or by
6832 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
6833 if ((alloc_size % 16) == 0) {
6835 /* Mark the padding slot as NOREF */
6836 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
6839 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
6840 if (cfg->stack_offset != alloc_size) {
6841 /* Mark the padding slot as NOREF */
6842 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
6844 cfg->arch.sp_fp_offset = alloc_size;
6848 cfg->arch.stack_alloc_size = alloc_size;
6850 /* Allocate stack frame */
6852 /* See mono_emit_stack_alloc */
6853 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
6854 guint32 remaining_size = alloc_size;
6855 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
6856 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
6857 guint32 offset = code - cfg->native_code;
6858 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
6859 while (required_code_size >= (cfg->code_size - offset))
6860 cfg->code_size *= 2;
6861 cfg->native_code = mono_realloc_native_code (cfg);
6862 code = cfg->native_code + offset;
6863 cfg->stat_code_reallocs++;
6866 while (remaining_size >= 0x1000) {
6867 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
6868 if (cfg->arch.omit_fp) {
6869 cfa_offset += 0x1000;
6870 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6872 async_exc_point (code);
6874 if (cfg->arch.omit_fp)
6875 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
6878 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
6879 remaining_size -= 0x1000;
6881 if (remaining_size) {
6882 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
6883 if (cfg->arch.omit_fp) {
6884 cfa_offset += remaining_size;
6885 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6886 async_exc_point (code);
6889 if (cfg->arch.omit_fp)
6890 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
6894 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
6895 if (cfg->arch.omit_fp) {
6896 cfa_offset += alloc_size;
6897 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6898 async_exc_point (code);
6903 /* Stack alignment check */
6906 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
6907 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
6908 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
6909 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
6910 amd64_breakpoint (code);
6914 if (mini_get_debug_options ()->init_stacks) {
6915 /* Fill the stack frame with a dummy value to force deterministic behavior */
6917 /* Save registers to the red zone */
6918 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
6919 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
6921 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
6922 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
6923 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
6926 #if defined(__default_codegen__)
6927 amd64_prefix (code, X86_REP_PREFIX);
6929 #elif defined(__native_client_codegen__)
6930 /* NaCl stos pseudo-instruction */
6931 amd64_codegen_pre (code);
6932 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
6933 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
6934 /* Add %r15 to %rdi using lea, condition flags unaffected. */
6935 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
6936 amd64_prefix (code, X86_REP_PREFIX);
6938 amd64_codegen_post (code);
6939 #endif /* __native_client_codegen__ */
6941 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
6942 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
6946 if (method->save_lmf)
6947 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
6949 /* Save callee saved registers */
6950 if (cfg->arch.omit_fp) {
6951 save_area_offset = cfg->arch.reg_save_area_offset;
6952 /* Save caller saved registers after sp is adjusted */
6953 /* The registers are saved at the bottom of the frame */
6954 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
6956 /* The registers are saved just below the saved rbp */
6957 save_area_offset = cfg->arch.reg_save_area_offset;
6960 for (i = 0; i < AMD64_NREG; ++i) {
6961 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
6962 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
6964 if (cfg->arch.omit_fp) {
6965 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
6966 /* These are handled automatically by the stack marking code */
6967 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
6969 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
6973 save_area_offset += 8;
6974 async_exc_point (code);
6978 /* store runtime generic context */
6979 if (cfg->rgctx_var) {
6980 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
6981 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
6983 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
6985 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
6986 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
6989 /* compute max_length in order to use short forward jumps */
6990 max_epilog_size = get_max_epilog_size (cfg);
6991 if (cfg->opt & MONO_OPT_BRANCH) {
6992 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6996 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6998 /* max alignment for loops */
6999 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
7000 max_length += LOOP_ALIGNMENT;
7001 #ifdef __native_client_codegen__
7002 /* max alignment for native client */
7003 max_length += kNaClAlignment;
7006 MONO_BB_FOR_EACH_INS (bb, ins) {
7007 #ifdef __native_client_codegen__
7009 int space_in_block = kNaClAlignment -
7010 ((max_length + cfg->code_len) & kNaClAlignmentMask);
7011 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7012 if (space_in_block < max_len && max_len < kNaClAlignment) {
7013 max_length += space_in_block;
7016 #endif /*__native_client_codegen__*/
7017 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7020 /* Take prolog and epilog instrumentation into account */
7021 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
7022 max_length += max_epilog_size;
7024 bb->max_length = max_length;
7028 sig = mono_method_signature (method);
7031 cinfo = cfg->arch.cinfo;
7033 if (sig->ret->type != MONO_TYPE_VOID) {
7034 /* Save volatile arguments to the stack */
7035 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
7036 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
7039 /* Keep this in sync with emit_load_volatile_arguments */
7040 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
7041 ArgInfo *ainfo = cinfo->args + i;
7043 ins = cfg->args [i];
7045 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
7046 /* Unused arguments */
7049 /* Save volatile arguments to the stack */
7050 if (ins->opcode != OP_REGVAR) {
7051 switch (ainfo->storage) {
7057 if (stack_offset & 0x1)
7059 else if (stack_offset & 0x2)
7061 else if (stack_offset & 0x4)
7066 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
7069 * Save the original location of 'this',
7070 * get_generic_info_from_stack_frame () needs this to properly look up
7071 * the argument value during the handling of async exceptions.
7073 if (ins == cfg->args [0]) {
7074 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7075 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
7079 case ArgInFloatSSEReg:
7080 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7082 case ArgInDoubleSSEReg:
7083 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7085 case ArgValuetypeInReg:
7086 for (quad = 0; quad < 2; quad ++) {
7087 switch (ainfo->pair_storage [quad]) {
7089 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7091 case ArgInFloatSSEReg:
7092 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7094 case ArgInDoubleSSEReg:
7095 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7100 g_assert_not_reached ();
7104 case ArgValuetypeAddrInIReg:
7105 if (ainfo->pair_storage [0] == ArgInIReg)
7106 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7112 /* Argument allocated to (non-volatile) register */
7113 switch (ainfo->storage) {
7115 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7118 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7121 g_assert_not_reached ();
7124 if (ins == cfg->args [0]) {
7125 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7126 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7131 if (cfg->method->save_lmf)
7132 args_clobbered = TRUE;
7135 args_clobbered = TRUE;
7136 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7139 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7140 args_clobbered = TRUE;
7143 * Optimize the common case of the first bblock making a call with the same
7144 * arguments as the method. This works because the arguments are still in their
7145 * original argument registers.
7146 * FIXME: Generalize this
7148 if (!args_clobbered) {
7149 MonoBasicBlock *first_bb = cfg->bb_entry;
7151 int filter = FILTER_IL_SEQ_POINT;
7153 next = mono_bb_first_inst (first_bb, filter);
7154 if (!next && first_bb->next_bb) {
7155 first_bb = first_bb->next_bb;
7156 next = mono_bb_first_inst (first_bb, filter);
7159 if (first_bb->in_count > 1)
7162 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7163 ArgInfo *ainfo = cinfo->args + i;
7164 gboolean match = FALSE;
7166 ins = cfg->args [i];
7167 if (ins->opcode != OP_REGVAR) {
7168 switch (ainfo->storage) {
7170 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7171 if (next->dreg == ainfo->reg) {
7175 next->opcode = OP_MOVE;
7176 next->sreg1 = ainfo->reg;
7177 /* Only continue if the instruction doesn't change argument regs */
7178 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7188 /* Argument allocated to (non-volatile) register */
7189 switch (ainfo->storage) {
7191 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7202 next = mono_inst_next (next, filter);
7203 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7210 if (cfg->gen_sdb_seq_points) {
7211 MonoInst *info_var = cfg->arch.seq_point_info_var;
7213 /* Initialize seq_point_info_var */
7214 if (cfg->compile_aot) {
7215 /* Initialize the variable from a GOT slot */
7216 /* Same as OP_AOTCONST */
7217 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7218 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7219 g_assert (info_var->opcode == OP_REGOFFSET);
7220 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7223 if (cfg->compile_aot) {
7224 /* Initialize ss_tramp_var */
7225 ins = cfg->arch.ss_tramp_var;
7226 g_assert (ins->opcode == OP_REGOFFSET);
7228 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7229 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr), 8);
7230 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7232 /* Initialize ss_trigger_page_var */
7233 ins = cfg->arch.ss_trigger_page_var;
7235 g_assert (ins->opcode == OP_REGOFFSET);
7237 amd64_mov_reg_imm (code, AMD64_R11, (guint64)ss_trigger_page);
7238 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7242 cfg->code_len = code - cfg->native_code;
7244 g_assert (cfg->code_len < cfg->code_size);
7250 mono_arch_emit_epilog (MonoCompile *cfg)
7252 MonoMethod *method = cfg->method;
7255 int max_epilog_size;
7257 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7258 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7260 max_epilog_size = get_max_epilog_size (cfg);
7262 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7263 cfg->code_size *= 2;
7264 cfg->native_code = mono_realloc_native_code (cfg);
7265 cfg->stat_code_reallocs++;
7267 code = cfg->native_code + cfg->code_len;
7269 cfg->has_unwind_info_for_epilog = TRUE;
7271 /* Mark the start of the epilog */
7272 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7274 /* Save the uwind state which is needed by the out-of-line code */
7275 mono_emit_unwind_op_remember_state (cfg, code);
7277 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7278 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7280 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7282 if (method->save_lmf) {
7283 /* check if we need to restore protection of the stack after a stack overflow */
7284 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7286 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7287 /* we load the value in a separate instruction: this mechanism may be
7288 * used later as a safer way to do thread interruption
7290 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7291 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7293 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7294 /* note that the call trampoline will preserve eax/edx */
7295 x86_call_reg (code, X86_ECX);
7296 x86_patch (patch, code);
7298 /* FIXME: maybe save the jit tls in the prolog */
7300 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7301 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7305 /* Restore callee saved regs */
7306 for (i = 0; i < AMD64_NREG; ++i) {
7307 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7308 /* Restore only used_int_regs, not arch.saved_iregs */
7309 if (cfg->used_int_regs & (1 << i)) {
7310 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7311 mono_emit_unwind_op_same_value (cfg, code, i);
7312 async_exc_point (code);
7314 save_area_offset += 8;
7318 /* Load returned vtypes into registers if needed */
7319 cinfo = cfg->arch.cinfo;
7320 if (cinfo->ret.storage == ArgValuetypeInReg) {
7321 ArgInfo *ainfo = &cinfo->ret;
7322 MonoInst *inst = cfg->ret;
7324 for (quad = 0; quad < 2; quad ++) {
7325 switch (ainfo->pair_storage [quad]) {
7327 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7329 case ArgInFloatSSEReg:
7330 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7332 case ArgInDoubleSSEReg:
7333 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7338 g_assert_not_reached ();
7343 if (cfg->arch.omit_fp) {
7344 if (cfg->arch.stack_alloc_size) {
7345 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7349 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7351 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7352 async_exc_point (code);
7355 /* Restore the unwind state to be the same as before the epilog */
7356 mono_emit_unwind_op_restore_state (cfg, code);
7358 cfg->code_len = code - cfg->native_code;
7360 g_assert (cfg->code_len < cfg->code_size);
7364 mono_arch_emit_exceptions (MonoCompile *cfg)
7366 MonoJumpInfo *patch_info;
7369 MonoClass *exc_classes [16];
7370 guint8 *exc_throw_start [16], *exc_throw_end [16];
7371 guint32 code_size = 0;
7373 /* Compute needed space */
7374 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7375 if (patch_info->type == MONO_PATCH_INFO_EXC)
7377 if (patch_info->type == MONO_PATCH_INFO_R8)
7378 code_size += 8 + 15; /* sizeof (double) + alignment */
7379 if (patch_info->type == MONO_PATCH_INFO_R4)
7380 code_size += 4 + 15; /* sizeof (float) + alignment */
7381 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7382 code_size += 8 + 7; /*sizeof (void*) + alignment */
7385 #ifdef __native_client_codegen__
7386 /* Give us extra room on Native Client. This could be */
7387 /* more carefully calculated, but bundle alignment makes */
7388 /* it much trickier, so *2 like other places is good. */
7392 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7393 cfg->code_size *= 2;
7394 cfg->native_code = mono_realloc_native_code (cfg);
7395 cfg->stat_code_reallocs++;
7398 code = cfg->native_code + cfg->code_len;
7400 /* add code to raise exceptions */
7402 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7403 switch (patch_info->type) {
7404 case MONO_PATCH_INFO_EXC: {
7405 MonoClass *exc_class;
7409 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7411 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7412 g_assert (exc_class);
7413 throw_ip = patch_info->ip.i;
7415 //x86_breakpoint (code);
7416 /* Find a throw sequence for the same exception class */
7417 for (i = 0; i < nthrows; ++i)
7418 if (exc_classes [i] == exc_class)
7421 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7422 x86_jump_code (code, exc_throw_start [i]);
7423 patch_info->type = MONO_PATCH_INFO_NONE;
7427 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7431 exc_classes [nthrows] = exc_class;
7432 exc_throw_start [nthrows] = code;
7434 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7436 patch_info->type = MONO_PATCH_INFO_NONE;
7438 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7440 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7445 exc_throw_end [nthrows] = code;
7455 g_assert(code < cfg->native_code + cfg->code_size);
7458 /* Handle relocations with RIP relative addressing */
7459 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7460 gboolean remove = FALSE;
7461 guint8 *orig_code = code;
7463 switch (patch_info->type) {
7464 case MONO_PATCH_INFO_R8:
7465 case MONO_PATCH_INFO_R4: {
7466 guint8 *pos, *patch_pos;
7469 /* The SSE opcodes require a 16 byte alignment */
7470 #if defined(__default_codegen__)
7471 code = (guint8*)ALIGN_TO (code, 16);
7472 #elif defined(__native_client_codegen__)
7474 /* Pad this out with HLT instructions */
7475 /* or we can get garbage bytes emitted */
7476 /* which will fail validation */
7477 guint8 *aligned_code;
7478 /* extra align to make room for */
7479 /* mov/push below */
7480 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7481 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7482 /* The technique of hiding data in an */
7483 /* instruction has a problem here: we */
7484 /* need the data aligned to a 16-byte */
7485 /* boundary but the instruction cannot */
7486 /* cross the bundle boundary. so only */
7487 /* odd multiples of 16 can be used */
7488 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7491 while (code < aligned_code) {
7492 *(code++) = 0xf4; /* hlt */
7497 pos = cfg->native_code + patch_info->ip.i;
7498 if (IS_REX (pos [1])) {
7499 patch_pos = pos + 5;
7500 target_pos = code - pos - 9;
7503 patch_pos = pos + 4;
7504 target_pos = code - pos - 8;
7507 if (patch_info->type == MONO_PATCH_INFO_R8) {
7508 #ifdef __native_client_codegen__
7509 /* Hide 64-bit data in a */
7510 /* "mov imm64, r11" instruction. */
7511 /* write it before the start of */
7513 *(code-2) = 0x49; /* prefix */
7514 *(code-1) = 0xbb; /* mov X, %r11 */
7516 *(double*)code = *(double*)patch_info->data.target;
7517 code += sizeof (double);
7519 #ifdef __native_client_codegen__
7520 /* Hide 32-bit data in a */
7521 /* "push imm32" instruction. */
7522 *(code-1) = 0x68; /* push */
7524 *(float*)code = *(float*)patch_info->data.target;
7525 code += sizeof (float);
7528 *(guint32*)(patch_pos) = target_pos;
7533 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7536 if (cfg->compile_aot)
7539 /*loading is faster against aligned addresses.*/
7540 code = (guint8*)ALIGN_TO (code, 8);
7541 memset (orig_code, 0, code - orig_code);
7543 pos = cfg->native_code + patch_info->ip.i;
7545 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7546 if (IS_REX (pos [1]))
7547 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7549 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7551 *(gpointer*)code = (gpointer)patch_info->data.target;
7552 code += sizeof (gpointer);
7562 if (patch_info == cfg->patch_info)
7563 cfg->patch_info = patch_info->next;
7567 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7569 tmp->next = patch_info->next;
7572 g_assert (code < cfg->native_code + cfg->code_size);
7575 cfg->code_len = code - cfg->native_code;
7577 g_assert (cfg->code_len < cfg->code_size);
7581 #endif /* DISABLE_JIT */
7584 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7587 MonoMethodSignature *sig;
7589 int i, n, stack_area = 0;
7591 /* Keep this in sync with mono_arch_get_argument_info */
7593 if (enable_arguments) {
7594 /* Allocate a new area on the stack and save arguments there */
7595 sig = mono_method_signature (cfg->method);
7597 n = sig->param_count + sig->hasthis;
7599 stack_area = ALIGN_TO (n * 8, 16);
7601 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7603 for (i = 0; i < n; ++i) {
7604 inst = cfg->args [i];
7606 if (inst->opcode == OP_REGVAR)
7607 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7609 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7610 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7615 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7616 amd64_set_reg_template (code, AMD64_ARG_REG1);
7617 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7618 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7620 if (enable_arguments)
7621 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7635 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7638 int save_mode = SAVE_NONE;
7639 MonoMethod *method = cfg->method;
7640 MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
7643 switch (ret_type->type) {
7644 case MONO_TYPE_VOID:
7645 /* special case string .ctor icall */
7646 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7647 save_mode = SAVE_EAX;
7649 save_mode = SAVE_NONE;
7653 save_mode = SAVE_EAX;
7657 save_mode = SAVE_XMM;
7659 case MONO_TYPE_GENERICINST:
7660 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7661 save_mode = SAVE_EAX;
7665 case MONO_TYPE_VALUETYPE:
7666 save_mode = SAVE_STRUCT;
7669 save_mode = SAVE_EAX;
7673 /* Save the result and copy it into the proper argument register */
7674 switch (save_mode) {
7676 amd64_push_reg (code, AMD64_RAX);
7678 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7679 if (enable_arguments)
7680 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7684 if (enable_arguments)
7685 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7688 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7689 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7691 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7693 * The result is already in the proper argument register so no copying
7700 g_assert_not_reached ();
7703 /* Set %al since this is a varargs call */
7704 if (save_mode == SAVE_XMM)
7705 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7707 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7709 if (preserve_argument_registers) {
7710 for (i = 0; i < PARAM_REGS; ++i)
7711 amd64_push_reg (code, param_regs [i]);
7714 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7715 amd64_set_reg_template (code, AMD64_ARG_REG1);
7716 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7718 if (preserve_argument_registers) {
7719 for (i = PARAM_REGS - 1; i >= 0; --i)
7720 amd64_pop_reg (code, param_regs [i]);
7723 /* Restore result */
7724 switch (save_mode) {
7726 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7727 amd64_pop_reg (code, AMD64_RAX);
7733 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7734 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7735 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7740 g_assert_not_reached ();
7747 mono_arch_flush_icache (guint8 *code, gint size)
7753 mono_arch_flush_register_windows (void)
7758 mono_arch_is_inst_imm (gint64 imm)
7760 return amd64_use_imm32 (imm);
7764 * Determine whenever the trap whose info is in SIGINFO is caused by
7768 mono_arch_is_int_overflow (void *sigctx, void *info)
7775 mono_sigctx_to_monoctx (sigctx, &ctx);
7777 rip = (guint8*)ctx.gregs [AMD64_RIP];
7779 if (IS_REX (rip [0])) {
7780 reg = amd64_rex_b (rip [0]);
7786 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7788 reg += x86_modrm_rm (rip [1]);
7790 value = ctx.gregs [reg];
7800 mono_arch_get_patch_offset (guint8 *code)
7806 * mono_breakpoint_clean_code:
7808 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
7809 * breakpoints in the original code, they are removed in the copy.
7811 * Returns TRUE if no sw breakpoint was present.
7814 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
7817 * If method_start is non-NULL we need to perform bound checks, since we access memory
7818 * at code - offset we could go before the start of the method and end up in a different
7819 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
7822 if (!method_start || code - offset >= method_start) {
7823 memcpy (buf, code - offset, size);
7825 int diff = code - method_start;
7826 memset (buf, 0, size);
7827 memcpy (buf + offset - diff, method_start, diff + size - offset);
7832 #if defined(__native_client_codegen__)
7833 /* For membase calls, we want the base register. for Native Client, */
7834 /* all indirect calls have the following sequence with the given sizes: */
7835 /* mov %eXX,%eXX [2-3] */
7836 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
7837 /* and $0xffffffffffffffe0,%r11d [4] */
7838 /* add %r15,%r11 [3] */
7839 /* callq *%r11 [3] */
7842 /* Determine if code points to a NaCl call-through-register sequence, */
7843 /* (i.e., the last 3 instructions listed above) */
7845 is_nacl_call_reg_sequence(guint8* code)
7847 const char *sequence = "\x41\x83\xe3\xe0" /* and */
7848 "\x4d\x03\xdf" /* add */
7849 "\x41\xff\xd3"; /* call */
7850 return memcmp(code, sequence, 10) == 0;
7853 /* Determine if code points to the first opcode of the mov membase component */
7854 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
7855 /* (there could be a REX prefix before the opcode but it is ignored) */
7857 is_nacl_indirect_call_membase_sequence(guint8* code)
7859 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
7860 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
7861 /* and that src reg = dest reg */
7862 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
7863 /* Check that next inst is mov, uses SIB byte (rm = 4), */
7865 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
7866 /* and has dst of r11 and base of r15 */
7867 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
7868 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
7870 #endif /* __native_client_codegen__ */
7873 mono_arch_get_this_arg_reg (guint8 *code)
7875 return AMD64_ARG_REG1;
7879 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
7881 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
7884 #define MAX_ARCH_DELEGATE_PARAMS 10
7887 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, guint32 param_count)
7889 guint8 *code, *start;
7890 GSList *unwind_ops = NULL;
7893 unwind_ops = mono_arch_get_cie_program ();
7896 start = code = mono_global_codeman_reserve (64);
7898 /* Replace the this argument with the target */
7899 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7900 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
7901 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7903 g_assert ((code - start) < 64);
7905 start = code = mono_global_codeman_reserve (64);
7907 if (param_count == 0) {
7908 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7910 /* We have to shift the arguments left */
7911 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7912 for (i = 0; i < param_count; ++i) {
7915 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7917 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
7919 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7923 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7925 g_assert ((code - start) < 64);
7928 nacl_global_codeman_validate (&start, 64, &code);
7929 mono_arch_flush_icache (start, code - start);
7932 *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
7934 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
7935 *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
7939 if (mono_jit_map_is_enabled ()) {
7942 buff = (char*)"delegate_invoke_has_target";
7944 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
7945 mono_emit_jit_tramp (start, code - start, buff);
7949 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
7954 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
7957 get_delegate_virtual_invoke_impl (MonoTrampInfo **info, gboolean load_imt_reg, int offset)
7959 guint8 *code, *start;
7964 if (offset / (int)sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
7967 start = code = mono_global_codeman_reserve (size);
7969 unwind_ops = mono_arch_get_cie_program ();
7971 /* Replace the this argument with the target */
7972 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7973 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
7976 /* Load the IMT reg */
7977 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
7980 /* Load the vtable */
7981 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
7982 amd64_jump_membase (code, AMD64_RAX, offset);
7983 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
7986 tramp_name = g_strdup_printf ("delegate_virtual_invoke_imt_%d", - offset / sizeof (gpointer));
7988 tramp_name = g_strdup_printf ("delegate_virtual_invoke_%d", offset / sizeof (gpointer));
7989 *info = mono_tramp_info_create (tramp_name, start, code - start, NULL, unwind_ops);
7990 g_free (tramp_name);
7996 * mono_arch_get_delegate_invoke_impls:
7998 * Return a list of MonoTrampInfo structures for the delegate invoke impl
8002 mono_arch_get_delegate_invoke_impls (void)
8005 MonoTrampInfo *info;
8008 get_delegate_invoke_impl (&info, TRUE, 0);
8009 res = g_slist_prepend (res, info);
8011 for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
8012 get_delegate_invoke_impl (&info, FALSE, i);
8013 res = g_slist_prepend (res, info);
8016 for (i = 0; i <= MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
8017 get_delegate_virtual_invoke_impl (&info, TRUE, - i * SIZEOF_VOID_P);
8018 res = g_slist_prepend (res, info);
8020 get_delegate_virtual_invoke_impl (&info, FALSE, i * SIZEOF_VOID_P);
8021 res = g_slist_prepend (res, info);
8028 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
8030 guint8 *code, *start;
8033 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
8036 /* FIXME: Support more cases */
8037 if (MONO_TYPE_ISSTRUCT (mini_get_underlying_type (sig->ret)))
8041 static guint8* cached = NULL;
8046 if (mono_aot_only) {
8047 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
8049 MonoTrampInfo *info;
8050 start = get_delegate_invoke_impl (&info, TRUE, 0);
8051 mono_tramp_info_register (info, NULL);
8054 mono_memory_barrier ();
8058 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
8059 for (i = 0; i < sig->param_count; ++i)
8060 if (!mono_is_regsize_var (sig->params [i]))
8062 if (sig->param_count > 4)
8065 code = cache [sig->param_count];
8069 if (mono_aot_only) {
8070 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
8071 start = mono_aot_get_trampoline (name);
8074 MonoTrampInfo *info;
8075 start = get_delegate_invoke_impl (&info, FALSE, sig->param_count);
8076 mono_tramp_info_register (info, NULL);
8079 mono_memory_barrier ();
8081 cache [sig->param_count] = start;
8088 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
8090 MonoTrampInfo *info;
8093 code = get_delegate_virtual_invoke_impl (&info, load_imt_reg, offset);
8095 mono_tramp_info_register (info, NULL);
8100 mono_arch_finish_init (void)
8102 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
8103 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
8108 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8112 #if defined(__default_codegen__)
8113 #define CMP_SIZE (6 + 1)
8114 #define CMP_REG_REG_SIZE (4 + 1)
8115 #define BR_SMALL_SIZE 2
8116 #define BR_LARGE_SIZE 6
8117 #define MOV_REG_IMM_SIZE 10
8118 #define MOV_REG_IMM_32BIT_SIZE 6
8119 #define JUMP_REG_SIZE (2 + 1)
8120 #elif defined(__native_client_codegen__)
8121 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8122 #define CMP_SIZE ((6 + 1) * 2 - 1)
8123 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8124 #define BR_SMALL_SIZE (2 * 2 - 1)
8125 #define BR_LARGE_SIZE (6 * 2 - 1)
8126 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8127 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8128 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8129 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8130 /* Jump membase's size is large and unpredictable */
8131 /* in native client, just pad it out a whole bundle. */
8132 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8136 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8138 int i, distance = 0;
8139 for (i = start; i < target; ++i)
8140 distance += imt_entries [i]->chunk_size;
8145 * LOCKING: called with the domain lock held
8148 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8149 gpointer fail_tramp)
8153 guint8 *code, *start;
8154 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8157 for (i = 0; i < count; ++i) {
8158 MonoIMTCheckItem *item = imt_entries [i];
8159 if (item->is_equals) {
8160 if (item->check_target_idx) {
8161 if (!item->compare_done) {
8162 if (amd64_use_imm32 ((gint64)item->key))
8163 item->chunk_size += CMP_SIZE;
8165 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8167 if (item->has_target_code) {
8168 item->chunk_size += MOV_REG_IMM_SIZE;
8170 if (vtable_is_32bit)
8171 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8173 item->chunk_size += MOV_REG_IMM_SIZE;
8174 #ifdef __native_client_codegen__
8175 item->chunk_size += JUMP_MEMBASE_SIZE;
8178 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8181 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8182 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8184 if (vtable_is_32bit)
8185 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8187 item->chunk_size += MOV_REG_IMM_SIZE;
8188 item->chunk_size += JUMP_REG_SIZE;
8189 /* with assert below:
8190 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8192 #ifdef __native_client_codegen__
8193 item->chunk_size += JUMP_MEMBASE_SIZE;
8198 if (amd64_use_imm32 ((gint64)item->key))
8199 item->chunk_size += CMP_SIZE;
8201 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8202 item->chunk_size += BR_LARGE_SIZE;
8203 imt_entries [item->check_target_idx]->compare_done = TRUE;
8205 size += item->chunk_size;
8207 #if defined(__native_client__) && defined(__native_client_codegen__)
8208 /* In Native Client, we don't re-use thunks, allocate from the */
8209 /* normal code manager paths. */
8210 code = mono_domain_code_reserve (domain, size);
8213 code = mono_method_alloc_generic_virtual_thunk (domain, size);
8215 code = mono_domain_code_reserve (domain, size);
8219 unwind_ops = mono_arch_get_cie_program ();
8221 for (i = 0; i < count; ++i) {
8222 MonoIMTCheckItem *item = imt_entries [i];
8223 item->code_target = code;
8224 if (item->is_equals) {
8225 gboolean fail_case = !item->check_target_idx && fail_tramp;
8227 if (item->check_target_idx || fail_case) {
8228 if (!item->compare_done || fail_case) {
8229 if (amd64_use_imm32 ((gint64)item->key))
8230 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8232 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof(gpointer));
8233 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8236 item->jmp_code = code;
8237 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8238 if (item->has_target_code) {
8239 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8240 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8242 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8243 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8247 amd64_patch (item->jmp_code, code);
8248 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8249 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8250 item->jmp_code = NULL;
8253 /* enable the commented code to assert on wrong method */
8255 if (amd64_is_imm32 (item->key))
8256 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8258 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8259 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8261 item->jmp_code = code;
8262 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8263 /* See the comment below about R10 */
8264 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8265 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8266 amd64_patch (item->jmp_code, code);
8267 amd64_breakpoint (code);
8268 item->jmp_code = NULL;
8270 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8271 needs to be preserved. R10 needs
8272 to be preserved for calls which
8273 require a runtime generic context,
8274 but interface calls don't. */
8275 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8276 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8280 if (amd64_use_imm32 ((gint64)item->key))
8281 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8283 amd64_mov_reg_imm_size (code, MONO_ARCH_IMT_SCRATCH_REG, item->key, sizeof (gpointer));
8284 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8286 item->jmp_code = code;
8287 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8288 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8290 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8292 g_assert (code - item->code_target <= item->chunk_size);
8294 /* patch the branches to get to the target items */
8295 for (i = 0; i < count; ++i) {
8296 MonoIMTCheckItem *item = imt_entries [i];
8297 if (item->jmp_code) {
8298 if (item->check_target_idx) {
8299 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8305 mono_stats.imt_thunks_size += code - start;
8306 g_assert (code - start <= size);
8308 nacl_domain_code_validate(domain, &start, size, &code);
8309 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8311 mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, unwind_ops), domain);
8317 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8319 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8323 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8325 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8329 mono_arch_get_cie_program (void)
8333 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8334 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8342 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8344 MonoInst *ins = NULL;
8347 if (cmethod->klass == mono_defaults.math_class) {
8348 if (strcmp (cmethod->name, "Sin") == 0) {
8350 } else if (strcmp (cmethod->name, "Cos") == 0) {
8352 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8354 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8358 if (opcode && fsig->param_count == 1) {
8359 MONO_INST_NEW (cfg, ins, opcode);
8360 ins->type = STACK_R8;
8361 ins->dreg = mono_alloc_freg (cfg);
8362 ins->sreg1 = args [0]->dreg;
8363 MONO_ADD_INS (cfg->cbb, ins);
8367 if (cfg->opt & MONO_OPT_CMOV) {
8368 if (strcmp (cmethod->name, "Min") == 0) {
8369 if (fsig->params [0]->type == MONO_TYPE_I4)
8371 if (fsig->params [0]->type == MONO_TYPE_U4)
8372 opcode = OP_IMIN_UN;
8373 else if (fsig->params [0]->type == MONO_TYPE_I8)
8375 else if (fsig->params [0]->type == MONO_TYPE_U8)
8376 opcode = OP_LMIN_UN;
8377 } else if (strcmp (cmethod->name, "Max") == 0) {
8378 if (fsig->params [0]->type == MONO_TYPE_I4)
8380 if (fsig->params [0]->type == MONO_TYPE_U4)
8381 opcode = OP_IMAX_UN;
8382 else if (fsig->params [0]->type == MONO_TYPE_I8)
8384 else if (fsig->params [0]->type == MONO_TYPE_U8)
8385 opcode = OP_LMAX_UN;
8389 if (opcode && fsig->param_count == 2) {
8390 MONO_INST_NEW (cfg, ins, opcode);
8391 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8392 ins->dreg = mono_alloc_ireg (cfg);
8393 ins->sreg1 = args [0]->dreg;
8394 ins->sreg2 = args [1]->dreg;
8395 MONO_ADD_INS (cfg->cbb, ins);
8399 /* OP_FREM is not IEEE compatible */
8400 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8401 MONO_INST_NEW (cfg, ins, OP_FREM);
8402 ins->inst_i0 = args [0];
8403 ins->inst_i1 = args [1];
8413 mono_arch_print_tree (MonoInst *tree, int arity)
8419 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8421 return ctx->gregs [reg];
8425 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8427 ctx->gregs [reg] = val;
8431 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8433 gpointer *sp, old_value;
8437 bp = MONO_CONTEXT_GET_BP (ctx);
8438 sp = *(gpointer*)(bp + clause->exvar_offset);
8441 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8450 * mono_arch_emit_load_aotconst:
8452 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8453 * TARGET from the mscorlib GOT in full-aot code.
8454 * On AMD64, the result is placed into R11.
8457 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
8459 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8460 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8466 * mono_arch_get_trampolines:
8468 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8472 mono_arch_get_trampolines (gboolean aot)
8474 return mono_amd64_get_exception_trampolines (aot);
8477 /* Soft Debug support */
8478 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8481 * mono_arch_set_breakpoint:
8483 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8484 * The location should contain code emitted by OP_SEQ_POINT.
8487 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8490 guint8 *orig_code = code;
8493 guint32 native_offset = ip - (guint8*)ji->code_start;
8494 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8496 g_assert (info->bp_addrs [native_offset] == 0);
8497 info->bp_addrs [native_offset] = mini_get_breakpoint_trampoline ();
8500 * In production, we will use int3 (has to fix the size in the md
8501 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
8504 g_assert (code [0] == 0x90);
8505 if (breakpoint_size == 8) {
8506 amd64_mov_reg_mem (code, AMD64_R11, (guint64)bp_trigger_page, 4);
8508 amd64_mov_reg_imm_size (code, AMD64_R11, (guint64)bp_trigger_page, 8);
8509 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 4);
8512 g_assert (code - orig_code == breakpoint_size);
8517 * mono_arch_clear_breakpoint:
8519 * Clear the breakpoint at IP.
8522 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8528 guint32 native_offset = ip - (guint8*)ji->code_start;
8529 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8531 info->bp_addrs [native_offset] = NULL;
8533 for (i = 0; i < breakpoint_size; ++i)
8539 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8542 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8543 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == bp_trigger_page)
8548 siginfo_t* sinfo = (siginfo_t*) info;
8549 /* Sometimes the address is off by 4 */
8550 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
8558 * mono_arch_skip_breakpoint:
8560 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8561 * we resume, the instruction is not executed again.
8564 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8567 /* The breakpoint instruction is a call */
8569 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + breakpoint_fault_size);
8574 * mono_arch_start_single_stepping:
8576 * Start single stepping.
8579 mono_arch_start_single_stepping (void)
8581 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
8582 ss_trampoline = mini_get_single_step_trampoline ();
8586 * mono_arch_stop_single_stepping:
8588 * Stop single stepping.
8591 mono_arch_stop_single_stepping (void)
8593 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
8594 ss_trampoline = NULL;
8598 * mono_arch_is_single_step_event:
8600 * Return whenever the machine state in SIGCTX corresponds to a single
8604 mono_arch_is_single_step_event (void *info, void *sigctx)
8607 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8608 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == ss_trigger_page)
8613 siginfo_t* sinfo = (siginfo_t*) info;
8614 /* Sometimes the address is off by 4 */
8615 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
8623 * mono_arch_skip_single_step:
8625 * Modify CTX so the ip is placed after the single step trigger instruction,
8626 * we resume, the instruction is not executed again.
8629 mono_arch_skip_single_step (MonoContext *ctx)
8631 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + single_step_fault_size);
8635 * mono_arch_create_seq_point_info:
8637 * Return a pointer to a data structure which is used by the sequence
8638 * point implementation in AOTed code.
8641 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8646 // FIXME: Add a free function
8648 mono_domain_lock (domain);
8649 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8651 mono_domain_unlock (domain);
8654 ji = mono_jit_info_table_find (domain, (char*)code);
8657 // FIXME: Optimize the size
8658 info = g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8660 info->ss_tramp_addr = &ss_trampoline;
8662 mono_domain_lock (domain);
8663 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8665 mono_domain_unlock (domain);
8672 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8674 ext->lmf.previous_lmf = prev_lmf;
8675 /* Mark that this is a MonoLMFExt */
8676 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8677 ext->lmf.rsp = (gssize)ext;
8683 mono_arch_opcode_supported (int opcode)
8686 case OP_ATOMIC_ADD_I4:
8687 case OP_ATOMIC_ADD_I8:
8688 case OP_ATOMIC_EXCHANGE_I4:
8689 case OP_ATOMIC_EXCHANGE_I8:
8690 case OP_ATOMIC_CAS_I4:
8691 case OP_ATOMIC_CAS_I8:
8692 case OP_ATOMIC_LOAD_I1:
8693 case OP_ATOMIC_LOAD_I2:
8694 case OP_ATOMIC_LOAD_I4:
8695 case OP_ATOMIC_LOAD_I8:
8696 case OP_ATOMIC_LOAD_U1:
8697 case OP_ATOMIC_LOAD_U2:
8698 case OP_ATOMIC_LOAD_U4:
8699 case OP_ATOMIC_LOAD_U8:
8700 case OP_ATOMIC_LOAD_R4:
8701 case OP_ATOMIC_LOAD_R8:
8702 case OP_ATOMIC_STORE_I1:
8703 case OP_ATOMIC_STORE_I2:
8704 case OP_ATOMIC_STORE_I4:
8705 case OP_ATOMIC_STORE_I8:
8706 case OP_ATOMIC_STORE_U1:
8707 case OP_ATOMIC_STORE_U2:
8708 case OP_ATOMIC_STORE_U4:
8709 case OP_ATOMIC_STORE_U8:
8710 case OP_ATOMIC_STORE_R4:
8711 case OP_ATOMIC_STORE_R8:
8718 #if defined(ENABLE_GSHAREDVT)
8720 #include "../../../mono-extensions/mono/mini/mini-amd64-gsharedvt.c"
8722 #endif /* !MONOTOUCH */