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 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
72 * The code generated for sequence points reads from this location, which is
73 * made read-only when single stepping is enabled.
75 static gpointer ss_trigger_page;
77 /* Enabled breakpoints read from this trigger page */
78 static gpointer bp_trigger_page;
80 /* The size of the breakpoint sequence */
81 static int breakpoint_size;
83 /* The size of the breakpoint instruction causing the actual fault */
84 static int breakpoint_fault_size;
86 /* The size of the single step instruction causing the actual fault */
87 static int single_step_fault_size;
89 /* The single step trampoline */
90 static gpointer ss_trampoline;
92 /* Offset between fp and the first argument in the callee */
93 #define ARGS_OFFSET 16
94 #define GP_SCRATCH_REG AMD64_R11
97 * AMD64 register usage:
98 * - callee saved registers are used for global register allocation
99 * - %r11 is used for materializing 64 bit constants in opcodes
100 * - the rest is used for local allocation
104 * Floating point comparison results:
114 mono_arch_regname (int reg)
117 case AMD64_RAX: return "%rax";
118 case AMD64_RBX: return "%rbx";
119 case AMD64_RCX: return "%rcx";
120 case AMD64_RDX: return "%rdx";
121 case AMD64_RSP: return "%rsp";
122 case AMD64_RBP: return "%rbp";
123 case AMD64_RDI: return "%rdi";
124 case AMD64_RSI: return "%rsi";
125 case AMD64_R8: return "%r8";
126 case AMD64_R9: return "%r9";
127 case AMD64_R10: return "%r10";
128 case AMD64_R11: return "%r11";
129 case AMD64_R12: return "%r12";
130 case AMD64_R13: return "%r13";
131 case AMD64_R14: return "%r14";
132 case AMD64_R15: return "%r15";
137 static const char * packed_xmmregs [] = {
138 "p:xmm0", "p:xmm1", "p:xmm2", "p:xmm3", "p:xmm4", "p:xmm5", "p:xmm6", "p:xmm7", "p:xmm8",
139 "p:xmm9", "p:xmm10", "p:xmm11", "p:xmm12", "p:xmm13", "p:xmm14", "p:xmm15"
142 static const char * single_xmmregs [] = {
143 "s:xmm0", "s:xmm1", "s:xmm2", "s:xmm3", "s:xmm4", "s:xmm5", "s:xmm6", "s:xmm7", "s:xmm8",
144 "s:xmm9", "s:xmm10", "s:xmm11", "s:xmm12", "s:xmm13", "s:xmm14", "s:xmm15"
148 mono_arch_fregname (int reg)
150 if (reg < AMD64_XMM_NREG)
151 return single_xmmregs [reg];
157 mono_arch_xregname (int reg)
159 if (reg < AMD64_XMM_NREG)
160 return packed_xmmregs [reg];
169 return mono_debug_count ();
175 static inline gboolean
176 amd64_is_near_call (guint8 *code)
179 if ((code [0] >= 0x40) && (code [0] <= 0x4f))
182 return code [0] == 0xe8;
185 #ifdef __native_client_codegen__
187 /* Keep track of instruction "depth", that is, the level of sub-instruction */
188 /* for any given instruction. For instance, amd64_call_reg resolves to */
189 /* amd64_call_reg_internal, which uses amd64_alu_* macros, etc. */
190 /* We only want to force bundle alignment for the top level instruction, */
191 /* so NaCl pseudo-instructions can be implemented with sub instructions. */
192 static MonoNativeTlsKey nacl_instruction_depth;
194 static MonoNativeTlsKey nacl_rex_tag;
195 static MonoNativeTlsKey nacl_legacy_prefix_tag;
198 amd64_nacl_clear_legacy_prefix_tag ()
200 mono_native_tls_set_value (nacl_legacy_prefix_tag, NULL);
204 amd64_nacl_tag_legacy_prefix (guint8* code)
206 if (mono_native_tls_get_value (nacl_legacy_prefix_tag) == NULL)
207 mono_native_tls_set_value (nacl_legacy_prefix_tag, code);
211 amd64_nacl_tag_rex (guint8* code)
213 mono_native_tls_set_value (nacl_rex_tag, code);
217 amd64_nacl_get_legacy_prefix_tag ()
219 return (guint8*)mono_native_tls_get_value (nacl_legacy_prefix_tag);
223 amd64_nacl_get_rex_tag ()
225 return (guint8*)mono_native_tls_get_value (nacl_rex_tag);
228 /* Increment the instruction "depth" described above */
230 amd64_nacl_instruction_pre ()
232 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
234 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)depth);
237 /* amd64_nacl_instruction_post: Decrement instruction "depth", force bundle */
238 /* alignment if depth == 0 (top level instruction) */
239 /* IN: start, end pointers to instruction beginning and end */
240 /* OUT: start, end pointers to beginning and end after possible alignment */
241 /* GLOBALS: nacl_instruction_depth defined above */
243 amd64_nacl_instruction_post (guint8 **start, guint8 **end)
245 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
247 mono_native_tls_set_value (nacl_instruction_depth, (void*)depth);
249 g_assert ( depth >= 0 );
251 uintptr_t space_in_block;
253 guint8 *prefix = amd64_nacl_get_legacy_prefix_tag ();
254 /* if legacy prefix is present, and if it was emitted before */
255 /* the start of the instruction sequence, adjust the start */
256 if (prefix != NULL && prefix < *start) {
257 g_assert (*start - prefix <= 3);/* only 3 are allowed */
260 space_in_block = kNaClAlignment - ((uintptr_t)(*start) & kNaClAlignmentMask);
261 instlen = (uintptr_t)(*end - *start);
262 /* Only check for instructions which are less than */
263 /* kNaClAlignment. The only instructions that should ever */
264 /* be that long are call sequences, which are already */
265 /* padded out to align the return to the next bundle. */
266 if (instlen > space_in_block && instlen < kNaClAlignment) {
267 const size_t MAX_NACL_INST_LENGTH = kNaClAlignment;
268 guint8 copy_of_instruction[MAX_NACL_INST_LENGTH];
269 const size_t length = (size_t)((*end)-(*start));
270 g_assert (length < MAX_NACL_INST_LENGTH);
272 memcpy (copy_of_instruction, *start, length);
273 *start = mono_arch_nacl_pad (*start, space_in_block);
274 memcpy (*start, copy_of_instruction, length);
275 *end = *start + length;
277 amd64_nacl_clear_legacy_prefix_tag ();
278 amd64_nacl_tag_rex (NULL);
282 /* amd64_nacl_membase_handler: ensure all access to memory of the form */
283 /* OFFSET(%rXX) is sandboxed. For allowable base registers %rip, %rbp, */
284 /* %rsp, and %r15, emit the membase as usual. For all other registers, */
285 /* make sure the upper 32-bits are cleared, and use that register in the */
286 /* index field of a new address of this form: OFFSET(%r15,%eXX,1) */
288 /* pointer to current instruction stream (in the */
289 /* middle of an instruction, after opcode is emitted) */
290 /* basereg/offset/dreg */
291 /* operands of normal membase address */
293 /* pointer to the end of the membase/memindex emit */
294 /* GLOBALS: nacl_rex_tag */
295 /* position in instruction stream that rex prefix was emitted */
296 /* nacl_legacy_prefix_tag */
297 /* (possibly NULL) position in instruction of legacy x86 prefix */
299 amd64_nacl_membase_handler (guint8** code, gint8 basereg, gint32 offset, gint8 dreg)
301 gint8 true_basereg = basereg;
303 /* Cache these values, they might change */
304 /* as new instructions are emitted below. */
305 guint8* rex_tag = amd64_nacl_get_rex_tag ();
306 guint8* legacy_prefix_tag = amd64_nacl_get_legacy_prefix_tag ();
308 /* 'basereg' is given masked to 0x7 at this point, so check */
309 /* the rex prefix to see if this is an extended register. */
310 if ((rex_tag != NULL) && IS_REX(*rex_tag) && (*rex_tag & AMD64_REX_B)) {
314 #define X86_LEA_OPCODE (0x8D)
316 if (!amd64_is_valid_nacl_base (true_basereg) && (*(*code-1) != X86_LEA_OPCODE)) {
317 guint8* old_instruction_start;
319 /* This will hold the 'mov %eXX, %eXX' that clears the upper */
320 /* 32-bits of the old base register (new index register) */
322 guint8* buf_ptr = buf;
325 g_assert (rex_tag != NULL);
327 if (IS_REX(*rex_tag)) {
328 /* The old rex.B should be the new rex.X */
329 if (*rex_tag & AMD64_REX_B) {
330 *rex_tag |= AMD64_REX_X;
332 /* Since our new base is %r15 set rex.B */
333 *rex_tag |= AMD64_REX_B;
335 /* Shift the instruction by one byte */
336 /* so we can insert a rex prefix */
337 memmove (rex_tag + 1, rex_tag, (size_t)(*code - rex_tag));
339 /* New rex prefix only needs rex.B for %r15 base */
340 *rex_tag = AMD64_REX(AMD64_REX_B);
343 if (legacy_prefix_tag) {
344 old_instruction_start = legacy_prefix_tag;
346 old_instruction_start = rex_tag;
349 /* Clears the upper 32-bits of the previous base register */
350 amd64_mov_reg_reg_size (buf_ptr, true_basereg, true_basereg, 4);
351 insert_len = buf_ptr - buf;
353 /* Move the old instruction forward to make */
354 /* room for 'mov' stored in 'buf_ptr' */
355 memmove (old_instruction_start + insert_len, old_instruction_start, (size_t)(*code - old_instruction_start));
357 memcpy (old_instruction_start, buf, insert_len);
359 /* Sandboxed replacement for the normal membase_emit */
360 x86_memindex_emit (*code, dreg, AMD64_R15, offset, basereg, 0);
363 /* Normal default behavior, emit membase memory location */
364 x86_membase_emit_body (*code, dreg, basereg, offset);
369 static inline unsigned char*
370 amd64_skip_nops (unsigned char* code)
375 if ( code[0] == 0x90) {
379 if ( code[0] == 0x66 && code[1] == 0x90) {
383 if (code[0] == 0x0f && code[1] == 0x1f
384 && code[2] == 0x00) {
388 if (code[0] == 0x0f && code[1] == 0x1f
389 && code[2] == 0x40 && code[3] == 0x00) {
393 if (code[0] == 0x0f && code[1] == 0x1f
394 && code[2] == 0x44 && code[3] == 0x00
395 && code[4] == 0x00) {
399 if (code[0] == 0x66 && code[1] == 0x0f
400 && code[2] == 0x1f && code[3] == 0x44
401 && code[4] == 0x00 && code[5] == 0x00) {
405 if (code[0] == 0x0f && code[1] == 0x1f
406 && code[2] == 0x80 && code[3] == 0x00
407 && code[4] == 0x00 && code[5] == 0x00
408 && code[6] == 0x00) {
412 if (code[0] == 0x0f && code[1] == 0x1f
413 && code[2] == 0x84 && code[3] == 0x00
414 && code[4] == 0x00 && code[5] == 0x00
415 && code[6] == 0x00 && code[7] == 0x00) {
424 mono_arch_nacl_skip_nops (guint8* code)
426 return amd64_skip_nops(code);
429 #endif /*__native_client_codegen__*/
432 amd64_patch (unsigned char* code, gpointer target)
436 #ifdef __native_client_codegen__
437 code = amd64_skip_nops (code);
439 #if defined(__native_client_codegen__) && defined(__native_client__)
440 if (nacl_is_code_address (code)) {
441 /* For tail calls, code is patched after being installed */
442 /* but not through the normal "patch callsite" method. */
443 unsigned char buf[kNaClAlignment];
444 unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
446 memcpy (buf, aligned_code, kNaClAlignment);
447 /* Patch a temp buffer of bundle size, */
448 /* then install to actual location. */
449 amd64_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), target);
450 ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
454 target = nacl_modify_patch_target (target);
458 if ((code [0] >= 0x40) && (code [0] <= 0x4f)) {
463 if ((code [0] & 0xf8) == 0xb8) {
464 /* amd64_set_reg_template */
465 *(guint64*)(code + 1) = (guint64)target;
467 else if ((code [0] == 0x8b) && rex && x86_modrm_mod (code [1]) == 0 && x86_modrm_rm (code [1]) == 5) {
468 /* mov 0(%rip), %dreg */
469 *(guint32*)(code + 2) = (guint32)(guint64)target - 7;
471 else if ((code [0] == 0xff) && (code [1] == 0x15)) {
472 /* call *<OFFSET>(%rip) */
473 *(guint32*)(code + 2) = ((guint32)(guint64)target) - 7;
475 else if (code [0] == 0xe8) {
477 gint64 disp = (guint8*)target - (guint8*)code;
478 g_assert (amd64_is_imm32 (disp));
479 x86_patch (code, (unsigned char*)target);
482 x86_patch (code, (unsigned char*)target);
486 mono_amd64_patch (unsigned char* code, gpointer target)
488 amd64_patch (code, target);
497 ArgValuetypeAddrInIReg,
498 ArgNone /* only in pair_storage */
506 /* Only if storage == ArgValuetypeInReg */
507 ArgStorage pair_storage [2];
509 /* The size of each pair */
519 gboolean need_stack_align;
520 gboolean vtype_retaddr;
521 /* The index of the vret arg in the argument list */
528 #define DEBUG(a) if (cfg->verbose_level > 1) a
531 static AMD64_Reg_No param_regs [] = { AMD64_RCX, AMD64_RDX, AMD64_R8, AMD64_R9 };
533 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
535 static AMD64_Reg_No param_regs [] = { AMD64_RDI, AMD64_RSI, AMD64_RDX, AMD64_RCX, AMD64_R8, AMD64_R9 };
537 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
541 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
543 ainfo->offset = *stack_size;
545 if (*gr >= PARAM_REGS) {
546 ainfo->storage = ArgOnStack;
547 /* Since the same stack slot size is used for all arg */
548 /* types, it needs to be big enough to hold them all */
549 (*stack_size) += sizeof(mgreg_t);
552 ainfo->storage = ArgInIReg;
553 ainfo->reg = param_regs [*gr];
559 #define FLOAT_PARAM_REGS 4
561 #define FLOAT_PARAM_REGS 8
565 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
567 ainfo->offset = *stack_size;
569 if (*gr >= FLOAT_PARAM_REGS) {
570 ainfo->storage = ArgOnStack;
571 /* Since the same stack slot size is used for both float */
572 /* types, it needs to be big enough to hold them both */
573 (*stack_size) += sizeof(mgreg_t);
576 /* A double register */
578 ainfo->storage = ArgInDoubleSSEReg;
580 ainfo->storage = ArgInFloatSSEReg;
586 typedef enum ArgumentClass {
594 merge_argument_class_from_type (MonoGenericSharingContext *gsctx, MonoType *type, ArgumentClass class1)
596 ArgumentClass class2 = ARG_CLASS_NO_CLASS;
599 ptype = mini_type_get_underlying_type (gsctx, type);
600 switch (ptype->type) {
609 case MONO_TYPE_STRING:
610 case MONO_TYPE_OBJECT:
611 case MONO_TYPE_CLASS:
612 case MONO_TYPE_SZARRAY:
614 case MONO_TYPE_FNPTR:
615 case MONO_TYPE_ARRAY:
618 class2 = ARG_CLASS_INTEGER;
623 class2 = ARG_CLASS_INTEGER;
625 class2 = ARG_CLASS_SSE;
629 case MONO_TYPE_TYPEDBYREF:
630 g_assert_not_reached ();
632 case MONO_TYPE_GENERICINST:
633 if (!mono_type_generic_inst_is_valuetype (ptype)) {
634 class2 = ARG_CLASS_INTEGER;
638 case MONO_TYPE_VALUETYPE: {
639 MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
642 for (i = 0; i < info->num_fields; ++i) {
644 class2 = merge_argument_class_from_type (gsctx, info->fields [i].field->type, class2);
649 g_assert_not_reached ();
653 if (class1 == class2)
655 else if (class1 == ARG_CLASS_NO_CLASS)
657 else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
658 class1 = ARG_CLASS_MEMORY;
659 else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
660 class1 = ARG_CLASS_INTEGER;
662 class1 = ARG_CLASS_SSE;
666 #ifdef __native_client_codegen__
668 /* Default alignment for Native Client is 32-byte. */
669 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
671 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
672 /* Check that alignment doesn't cross an alignment boundary. */
674 mono_arch_nacl_pad(guint8 *code, int pad)
676 const int kMaxPadding = 8; /* see amd64-codegen.h:amd64_padding_size() */
678 if (pad == 0) return code;
679 /* assertion: alignment cannot cross a block boundary */
680 g_assert (((uintptr_t)code & (~kNaClAlignmentMask)) ==
681 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
682 while (pad >= kMaxPadding) {
683 amd64_padding (code, kMaxPadding);
686 if (pad != 0) amd64_padding (code, pad);
692 count_fields_nested (MonoClass *klass)
694 MonoMarshalType *info;
697 info = mono_marshal_load_type_info (klass);
700 for (i = 0; i < info->num_fields; ++i) {
701 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type))
702 count += count_fields_nested (mono_class_from_mono_type (info->fields [i].field->type));
710 collect_field_info_nested (MonoClass *klass, MonoMarshalField *fields, int index, int offset)
712 MonoMarshalType *info;
715 info = mono_marshal_load_type_info (klass);
717 for (i = 0; i < info->num_fields; ++i) {
718 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type)) {
719 index = collect_field_info_nested (mono_class_from_mono_type (info->fields [i].field->type), fields, index, info->fields [i].offset);
721 memcpy (&fields [index], &info->fields [i], sizeof (MonoMarshalField));
722 fields [index].offset += offset;
730 add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
732 guint32 *gr, guint32 *fr, guint32 *stack_size)
734 guint32 size, quad, nquads, i, nfields;
735 /* Keep track of the size used in each quad so we can */
736 /* use the right size when copying args/return vars. */
737 guint32 quadsize [2] = {8, 8};
738 ArgumentClass args [2];
739 MonoMarshalType *info = NULL;
740 MonoMarshalField *fields = NULL;
742 MonoGenericSharingContext tmp_gsctx;
743 gboolean pass_on_stack = FALSE;
746 * The gsctx currently contains no data, it is only used for checking whenever
747 * open types are allowed, some callers like mono_arch_get_argument_info ()
748 * don't pass it to us, so work around that.
753 klass = mono_class_from_mono_type (type);
754 size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
756 if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
757 /* We pass and return vtypes of size 8 in a register */
758 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
759 pass_on_stack = TRUE;
763 pass_on_stack = TRUE;
767 /* If this struct can't be split up naturally into 8-byte */
768 /* chunks (registers), pass it on the stack. */
769 if (sig->pinvoke && !pass_on_stack) {
773 info = mono_marshal_load_type_info (klass);
777 * Collect field information recursively to be able to
778 * handle nested structures.
780 nfields = count_fields_nested (klass);
781 fields = g_new0 (MonoMarshalField, nfields);
782 collect_field_info_nested (klass, fields, 0, 0);
784 for (i = 0; i < nfields; ++i) {
785 field_size = mono_marshal_type_size (fields [i].field->type,
787 &align, TRUE, klass->unicode);
788 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
789 pass_on_stack = TRUE;
796 /* Allways pass in memory */
797 ainfo->offset = *stack_size;
798 *stack_size += ALIGN_TO (size, 8);
799 ainfo->storage = ArgOnStack;
805 /* FIXME: Handle structs smaller than 8 bytes */
806 //if ((size % 8) != 0)
815 int n = mono_class_value_size (klass, NULL);
817 quadsize [0] = n >= 8 ? 8 : n;
818 quadsize [1] = n >= 8 ? MAX (n - 8, 8) : 0;
820 /* Always pass in 1 or 2 integer registers */
821 args [0] = ARG_CLASS_INTEGER;
822 args [1] = ARG_CLASS_INTEGER;
823 /* Only the simplest cases are supported */
824 if (is_return && nquads != 1) {
825 args [0] = ARG_CLASS_MEMORY;
826 args [1] = ARG_CLASS_MEMORY;
830 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
831 * The X87 and SSEUP stuff is left out since there are no such types in
838 if (info->native_size > 16) {
839 ainfo->offset = *stack_size;
840 *stack_size += ALIGN_TO (info->native_size, 8);
841 ainfo->storage = ArgOnStack;
847 switch (info->native_size) {
848 case 1: case 2: case 4: case 8:
852 ainfo->storage = ArgOnStack;
853 ainfo->offset = *stack_size;
854 *stack_size += ALIGN_TO (info->native_size, 8);
857 ainfo->storage = ArgValuetypeAddrInIReg;
859 if (*gr < PARAM_REGS) {
860 ainfo->pair_storage [0] = ArgInIReg;
861 ainfo->pair_regs [0] = param_regs [*gr];
865 ainfo->pair_storage [0] = ArgOnStack;
866 ainfo->offset = *stack_size;
876 args [0] = ARG_CLASS_NO_CLASS;
877 args [1] = ARG_CLASS_NO_CLASS;
878 for (quad = 0; quad < nquads; ++quad) {
881 ArgumentClass class1;
884 class1 = ARG_CLASS_MEMORY;
886 class1 = ARG_CLASS_NO_CLASS;
887 for (i = 0; i < nfields; ++i) {
888 size = mono_marshal_type_size (fields [i].field->type,
890 &align, TRUE, klass->unicode);
891 if ((fields [i].offset < 8) && (fields [i].offset + size) > 8) {
892 /* Unaligned field */
896 /* Skip fields in other quad */
897 if ((quad == 0) && (fields [i].offset >= 8))
899 if ((quad == 1) && (fields [i].offset < 8))
902 /* How far into this quad this data extends.*/
903 /* (8 is size of quad) */
904 quadsize [quad] = fields [i].offset + size - (quad * 8);
906 class1 = merge_argument_class_from_type (gsctx, fields [i].field->type, class1);
908 g_assert (class1 != ARG_CLASS_NO_CLASS);
909 args [quad] = class1;
915 /* Post merger cleanup */
916 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
917 args [0] = args [1] = ARG_CLASS_MEMORY;
919 /* Allocate registers */
924 while (quadsize [0] != 1 && quadsize [0] != 2 && quadsize [0] != 4 && quadsize [0] != 8)
926 while (quadsize [1] != 1 && quadsize [1] != 2 && quadsize [1] != 4 && quadsize [1] != 8)
929 ainfo->storage = ArgValuetypeInReg;
930 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
931 g_assert (quadsize [0] <= 8);
932 g_assert (quadsize [1] <= 8);
933 ainfo->pair_size [0] = quadsize [0];
934 ainfo->pair_size [1] = quadsize [1];
935 ainfo->nregs = nquads;
936 for (quad = 0; quad < nquads; ++quad) {
937 switch (args [quad]) {
938 case ARG_CLASS_INTEGER:
939 if (*gr >= PARAM_REGS)
940 args [quad] = ARG_CLASS_MEMORY;
942 ainfo->pair_storage [quad] = ArgInIReg;
944 ainfo->pair_regs [quad] = return_regs [*gr];
946 ainfo->pair_regs [quad] = param_regs [*gr];
951 if (*fr >= FLOAT_PARAM_REGS)
952 args [quad] = ARG_CLASS_MEMORY;
954 if (quadsize[quad] <= 4)
955 ainfo->pair_storage [quad] = ArgInFloatSSEReg;
956 else ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
957 ainfo->pair_regs [quad] = *fr;
961 case ARG_CLASS_MEMORY:
964 g_assert_not_reached ();
968 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
969 /* Revert possible register assignments */
973 ainfo->offset = *stack_size;
975 *stack_size += ALIGN_TO (info->native_size, 8);
977 *stack_size += nquads * sizeof(mgreg_t);
978 ainfo->storage = ArgOnStack;
986 * Obtain information about a call according to the calling convention.
987 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
988 * Draft Version 0.23" document for more information.
991 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
993 guint32 i, gr, fr, pstart;
995 int n = sig->hasthis + sig->param_count;
996 guint32 stack_size = 0;
998 gboolean is_pinvoke = sig->pinvoke;
1001 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1003 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1011 /* Reserve space where the callee can save the argument registers */
1012 stack_size = 4 * sizeof (mgreg_t);
1016 ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
1017 switch (ret_type->type) {
1027 case MONO_TYPE_FNPTR:
1028 case MONO_TYPE_CLASS:
1029 case MONO_TYPE_OBJECT:
1030 case MONO_TYPE_SZARRAY:
1031 case MONO_TYPE_ARRAY:
1032 case MONO_TYPE_STRING:
1033 cinfo->ret.storage = ArgInIReg;
1034 cinfo->ret.reg = AMD64_RAX;
1038 cinfo->ret.storage = ArgInIReg;
1039 cinfo->ret.reg = AMD64_RAX;
1042 cinfo->ret.storage = ArgInFloatSSEReg;
1043 cinfo->ret.reg = AMD64_XMM0;
1046 cinfo->ret.storage = ArgInDoubleSSEReg;
1047 cinfo->ret.reg = AMD64_XMM0;
1049 case MONO_TYPE_GENERICINST:
1050 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1051 cinfo->ret.storage = ArgInIReg;
1052 cinfo->ret.reg = AMD64_RAX;
1056 #if defined( __native_client_codegen__ )
1057 case MONO_TYPE_TYPEDBYREF:
1059 case MONO_TYPE_VALUETYPE: {
1060 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
1062 add_valuetype (gsctx, sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
1063 if (cinfo->ret.storage == ArgOnStack) {
1064 cinfo->vtype_retaddr = TRUE;
1065 /* The caller passes the address where the value is stored */
1069 #if !defined( __native_client_codegen__ )
1070 case MONO_TYPE_TYPEDBYREF:
1071 /* Same as a valuetype with size 24 */
1072 cinfo->vtype_retaddr = TRUE;
1075 case MONO_TYPE_VOID:
1078 g_error ("Can't handle as return value 0x%x", ret_type->type);
1083 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1084 * the first argument, allowing 'this' to be always passed in the first arg reg.
1085 * Also do this if the first argument is a reference type, since virtual calls
1086 * are sometimes made using calli without sig->hasthis set, like in the delegate
1089 if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
1091 add_general (&gr, &stack_size, cinfo->args + 0);
1093 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
1096 add_general (&gr, &stack_size, &cinfo->ret);
1097 cinfo->vret_arg_index = 1;
1101 add_general (&gr, &stack_size, cinfo->args + 0);
1103 if (cinfo->vtype_retaddr)
1104 add_general (&gr, &stack_size, &cinfo->ret);
1107 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
1109 fr = FLOAT_PARAM_REGS;
1111 /* Emit the signature cookie just before the implicit arguments */
1112 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1115 for (i = pstart; i < sig->param_count; ++i) {
1116 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
1120 /* The float param registers and other param registers must be the same index on Windows x64.*/
1127 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1128 /* We allways pass the sig cookie on the stack for simplicity */
1130 * Prevent implicit arguments + the sig cookie from being passed
1134 fr = FLOAT_PARAM_REGS;
1136 /* Emit the signature cookie just before the implicit arguments */
1137 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1140 ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
1141 switch (ptype->type) {
1144 add_general (&gr, &stack_size, ainfo);
1148 add_general (&gr, &stack_size, ainfo);
1152 add_general (&gr, &stack_size, ainfo);
1157 case MONO_TYPE_FNPTR:
1158 case MONO_TYPE_CLASS:
1159 case MONO_TYPE_OBJECT:
1160 case MONO_TYPE_STRING:
1161 case MONO_TYPE_SZARRAY:
1162 case MONO_TYPE_ARRAY:
1163 add_general (&gr, &stack_size, ainfo);
1165 case MONO_TYPE_GENERICINST:
1166 if (!mono_type_generic_inst_is_valuetype (ptype)) {
1167 add_general (&gr, &stack_size, ainfo);
1171 case MONO_TYPE_VALUETYPE:
1172 case MONO_TYPE_TYPEDBYREF:
1173 add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
1178 add_general (&gr, &stack_size, ainfo);
1181 add_float (&fr, &stack_size, ainfo, FALSE);
1184 add_float (&fr, &stack_size, ainfo, TRUE);
1187 g_assert_not_reached ();
1191 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
1193 fr = FLOAT_PARAM_REGS;
1195 /* Emit the signature cookie just before the implicit arguments */
1196 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1199 cinfo->stack_usage = stack_size;
1200 cinfo->reg_usage = gr;
1201 cinfo->freg_usage = fr;
1206 * mono_arch_get_argument_info:
1207 * @csig: a method signature
1208 * @param_count: the number of parameters to consider
1209 * @arg_info: an array to store the result infos
1211 * Gathers information on parameters such as size, alignment and
1212 * padding. arg_info should be large enought to hold param_count + 1 entries.
1214 * Returns the size of the argument area on the stack.
1217 mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
1220 CallInfo *cinfo = get_call_info (NULL, NULL, csig);
1221 guint32 args_size = cinfo->stack_usage;
1223 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
1224 if (csig->hasthis) {
1225 arg_info [0].offset = 0;
1228 for (k = 0; k < param_count; k++) {
1229 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
1231 arg_info [k + 1].size = 0;
1240 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
1244 MonoType *callee_ret;
1246 c1 = get_call_info (NULL, NULL, caller_sig);
1247 c2 = get_call_info (NULL, NULL, callee_sig);
1248 res = c1->stack_usage >= c2->stack_usage;
1249 callee_ret = mini_get_underlying_type (cfg, callee_sig->ret);
1250 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
1251 /* An address on the callee's stack is passed as the first argument */
1261 * Initialize the cpu to execute managed code.
1264 mono_arch_cpu_init (void)
1269 /* spec compliance requires running with double precision */
1270 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1271 fpcw &= ~X86_FPCW_PRECC_MASK;
1272 fpcw |= X86_FPCW_PREC_DOUBLE;
1273 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
1274 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1276 /* TODO: This is crashing on Win64 right now.
1277 * _control87 (_PC_53, MCW_PC);
1283 * Initialize architecture specific code.
1286 mono_arch_init (void)
1290 mono_mutex_init_recursive (&mini_arch_mutex);
1291 #if defined(__native_client_codegen__)
1292 mono_native_tls_alloc (&nacl_instruction_depth, NULL);
1293 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)0);
1294 mono_native_tls_alloc (&nacl_rex_tag, NULL);
1295 mono_native_tls_alloc (&nacl_legacy_prefix_tag, NULL);
1298 #ifdef MONO_ARCH_NOMAP32BIT
1299 flags = MONO_MMAP_READ;
1300 /* amd64_mov_reg_imm () + amd64_mov_reg_membase () */
1301 breakpoint_size = 13;
1302 breakpoint_fault_size = 3;
1304 flags = MONO_MMAP_READ|MONO_MMAP_32BIT;
1305 /* amd64_mov_reg_mem () */
1306 breakpoint_size = 8;
1307 breakpoint_fault_size = 8;
1310 /* amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4); */
1311 single_step_fault_size = 4;
1313 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), flags);
1314 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), flags);
1315 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
1317 mono_aot_register_jit_icall ("mono_amd64_throw_exception", mono_amd64_throw_exception);
1318 mono_aot_register_jit_icall ("mono_amd64_throw_corlib_exception", mono_amd64_throw_corlib_exception);
1319 mono_aot_register_jit_icall ("mono_amd64_resume_unwind", mono_amd64_resume_unwind);
1320 mono_aot_register_jit_icall ("mono_amd64_get_original_ip", mono_amd64_get_original_ip);
1324 * Cleanup architecture specific code.
1327 mono_arch_cleanup (void)
1329 mono_mutex_destroy (&mini_arch_mutex);
1330 #if defined(__native_client_codegen__)
1331 mono_native_tls_free (nacl_instruction_depth);
1332 mono_native_tls_free (nacl_rex_tag);
1333 mono_native_tls_free (nacl_legacy_prefix_tag);
1338 * This function returns the optimizations supported on this cpu.
1341 mono_arch_cpu_optimizations (guint32 *exclude_mask)
1347 if (mono_hwcap_x86_has_cmov) {
1348 opts |= MONO_OPT_CMOV;
1350 if (mono_hwcap_x86_has_fcmov)
1351 opts |= MONO_OPT_FCMOV;
1353 *exclude_mask |= MONO_OPT_FCMOV;
1355 *exclude_mask |= MONO_OPT_CMOV;
1362 * This function test for all SSE functions supported.
1364 * Returns a bitmask corresponding to all supported versions.
1368 mono_arch_cpu_enumerate_simd_versions (void)
1370 guint32 sse_opts = 0;
1372 if (mono_hwcap_x86_has_sse1)
1373 sse_opts |= SIMD_VERSION_SSE1;
1375 if (mono_hwcap_x86_has_sse2)
1376 sse_opts |= SIMD_VERSION_SSE2;
1378 if (mono_hwcap_x86_has_sse3)
1379 sse_opts |= SIMD_VERSION_SSE3;
1381 if (mono_hwcap_x86_has_ssse3)
1382 sse_opts |= SIMD_VERSION_SSSE3;
1384 if (mono_hwcap_x86_has_sse41)
1385 sse_opts |= SIMD_VERSION_SSE41;
1387 if (mono_hwcap_x86_has_sse42)
1388 sse_opts |= SIMD_VERSION_SSE42;
1390 if (mono_hwcap_x86_has_sse4a)
1391 sse_opts |= SIMD_VERSION_SSE4a;
1399 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1404 for (i = 0; i < cfg->num_varinfo; i++) {
1405 MonoInst *ins = cfg->varinfo [i];
1406 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1409 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1412 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
1413 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1416 if (mono_is_regsize_var (ins->inst_vtype)) {
1417 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1418 g_assert (i == vmv->idx);
1419 vars = g_list_prepend (vars, vmv);
1423 vars = mono_varlist_sort (cfg, vars, 0);
1429 * mono_arch_compute_omit_fp:
1431 * Determine whenever the frame pointer can be eliminated.
1434 mono_arch_compute_omit_fp (MonoCompile *cfg)
1436 MonoMethodSignature *sig;
1437 MonoMethodHeader *header;
1441 if (cfg->arch.omit_fp_computed)
1444 header = cfg->header;
1446 sig = mono_method_signature (cfg->method);
1448 if (!cfg->arch.cinfo)
1449 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1450 cinfo = cfg->arch.cinfo;
1453 * FIXME: Remove some of the restrictions.
1455 cfg->arch.omit_fp = TRUE;
1456 cfg->arch.omit_fp_computed = TRUE;
1458 #ifdef __native_client_codegen__
1459 /* NaCl modules may not change the value of RBP, so it cannot be */
1460 /* used as a normal register, but it can be used as a frame pointer*/
1461 cfg->disable_omit_fp = TRUE;
1462 cfg->arch.omit_fp = FALSE;
1465 if (cfg->disable_omit_fp)
1466 cfg->arch.omit_fp = FALSE;
1468 if (!debug_omit_fp ())
1469 cfg->arch.omit_fp = FALSE;
1471 if (cfg->method->save_lmf)
1472 cfg->arch.omit_fp = FALSE;
1474 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1475 cfg->arch.omit_fp = FALSE;
1476 if (header->num_clauses)
1477 cfg->arch.omit_fp = FALSE;
1478 if (cfg->param_area)
1479 cfg->arch.omit_fp = FALSE;
1480 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1481 cfg->arch.omit_fp = FALSE;
1482 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1483 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1484 cfg->arch.omit_fp = FALSE;
1485 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1486 ArgInfo *ainfo = &cinfo->args [i];
1488 if (ainfo->storage == ArgOnStack) {
1490 * The stack offset can only be determined when the frame
1493 cfg->arch.omit_fp = FALSE;
1498 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1499 MonoInst *ins = cfg->varinfo [i];
1502 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1507 mono_arch_get_global_int_regs (MonoCompile *cfg)
1511 mono_arch_compute_omit_fp (cfg);
1513 if (cfg->arch.omit_fp)
1514 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1516 /* We use the callee saved registers for global allocation */
1517 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1518 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1519 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1520 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1521 #ifndef __native_client_codegen__
1522 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1525 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1526 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1533 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1538 /* All XMM registers */
1539 for (i = 0; i < 16; ++i)
1540 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1546 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1548 static GList *r = NULL;
1553 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1554 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1555 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1556 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1557 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1558 #ifndef __native_client_codegen__
1559 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1562 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1563 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1564 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1565 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1566 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1567 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1568 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1569 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1571 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1578 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1581 static GList *r = NULL;
1586 for (i = 0; i < AMD64_XMM_NREG; ++i)
1587 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1589 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1596 * mono_arch_regalloc_cost:
1598 * Return the cost, in number of memory references, of the action of
1599 * allocating the variable VMV into a register during global register
1603 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1605 MonoInst *ins = cfg->varinfo [vmv->idx];
1607 if (cfg->method->save_lmf)
1608 /* The register is already saved */
1609 /* substract 1 for the invisible store in the prolog */
1610 return (ins->opcode == OP_ARG) ? 0 : 1;
1613 return (ins->opcode == OP_ARG) ? 1 : 2;
1617 * mono_arch_fill_argument_info:
1619 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1623 mono_arch_fill_argument_info (MonoCompile *cfg)
1626 MonoMethodSignature *sig;
1631 sig = mono_method_signature (cfg->method);
1633 cinfo = cfg->arch.cinfo;
1634 sig_ret = mini_get_underlying_type (cfg, sig->ret);
1637 * Contrary to mono_arch_allocate_vars (), the information should describe
1638 * where the arguments are at the beginning of the method, not where they can be
1639 * accessed during the execution of the method. The later makes no sense for the
1640 * global register allocator, since a variable can be in more than one location.
1642 if (sig_ret->type != MONO_TYPE_VOID) {
1643 switch (cinfo->ret.storage) {
1645 case ArgInFloatSSEReg:
1646 case ArgInDoubleSSEReg:
1647 if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->vtype_retaddr)) {
1648 cfg->vret_addr->opcode = OP_REGVAR;
1649 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1652 cfg->ret->opcode = OP_REGVAR;
1653 cfg->ret->inst_c0 = cinfo->ret.reg;
1656 case ArgValuetypeInReg:
1657 cfg->ret->opcode = OP_REGOFFSET;
1658 cfg->ret->inst_basereg = -1;
1659 cfg->ret->inst_offset = -1;
1662 g_assert_not_reached ();
1666 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1667 ArgInfo *ainfo = &cinfo->args [i];
1669 ins = cfg->args [i];
1671 switch (ainfo->storage) {
1673 case ArgInFloatSSEReg:
1674 case ArgInDoubleSSEReg:
1675 ins->opcode = OP_REGVAR;
1676 ins->inst_c0 = ainfo->reg;
1679 ins->opcode = OP_REGOFFSET;
1680 ins->inst_basereg = -1;
1681 ins->inst_offset = -1;
1683 case ArgValuetypeInReg:
1685 ins->opcode = OP_NOP;
1688 g_assert_not_reached ();
1694 mono_arch_allocate_vars (MonoCompile *cfg)
1697 MonoMethodSignature *sig;
1700 guint32 locals_stack_size, locals_stack_align;
1704 sig = mono_method_signature (cfg->method);
1706 cinfo = cfg->arch.cinfo;
1707 sig_ret = mini_get_underlying_type (cfg, sig->ret);
1709 mono_arch_compute_omit_fp (cfg);
1712 * We use the ABI calling conventions for managed code as well.
1713 * Exception: valuetypes are only sometimes passed or returned in registers.
1717 * The stack looks like this:
1718 * <incoming arguments passed on the stack>
1720 * <lmf/caller saved registers>
1723 * <localloc area> -> grows dynamically
1727 if (cfg->arch.omit_fp) {
1728 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1729 cfg->frame_reg = AMD64_RSP;
1732 /* Locals are allocated backwards from %fp */
1733 cfg->frame_reg = AMD64_RBP;
1737 cfg->arch.saved_iregs = cfg->used_int_regs;
1738 if (cfg->method->save_lmf)
1739 /* Save all callee-saved registers normally, and restore them when unwinding through an LMF */
1740 cfg->arch.saved_iregs |= (1 << AMD64_RBX) | (1 << AMD64_R12) | (1 << AMD64_R13) | (1 << AMD64_R14) | (1 << AMD64_R15);
1742 if (cfg->arch.omit_fp)
1743 cfg->arch.reg_save_area_offset = offset;
1744 /* Reserve space for callee saved registers */
1745 for (i = 0; i < AMD64_NREG; ++i)
1746 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
1747 offset += sizeof(mgreg_t);
1749 if (!cfg->arch.omit_fp)
1750 cfg->arch.reg_save_area_offset = -offset;
1752 if (sig_ret->type != MONO_TYPE_VOID) {
1753 switch (cinfo->ret.storage) {
1755 case ArgInFloatSSEReg:
1756 case ArgInDoubleSSEReg:
1757 if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->vtype_retaddr)) {
1758 /* The register is volatile */
1759 cfg->vret_addr->opcode = OP_REGOFFSET;
1760 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1761 if (cfg->arch.omit_fp) {
1762 cfg->vret_addr->inst_offset = offset;
1766 cfg->vret_addr->inst_offset = -offset;
1768 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1769 printf ("vret_addr =");
1770 mono_print_ins (cfg->vret_addr);
1774 cfg->ret->opcode = OP_REGVAR;
1775 cfg->ret->inst_c0 = cinfo->ret.reg;
1778 case ArgValuetypeInReg:
1779 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1780 cfg->ret->opcode = OP_REGOFFSET;
1781 cfg->ret->inst_basereg = cfg->frame_reg;
1782 if (cfg->arch.omit_fp) {
1783 cfg->ret->inst_offset = offset;
1784 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1786 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1787 cfg->ret->inst_offset = - offset;
1791 g_assert_not_reached ();
1793 cfg->ret->dreg = cfg->ret->inst_c0;
1796 /* Allocate locals */
1797 offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1798 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1799 char *mname = mono_method_full_name (cfg->method, TRUE);
1800 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1801 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1806 if (locals_stack_align) {
1807 offset += (locals_stack_align - 1);
1808 offset &= ~(locals_stack_align - 1);
1810 if (cfg->arch.omit_fp) {
1811 cfg->locals_min_stack_offset = offset;
1812 cfg->locals_max_stack_offset = offset + locals_stack_size;
1814 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1815 cfg->locals_max_stack_offset = - offset;
1818 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1819 if (offsets [i] != -1) {
1820 MonoInst *ins = cfg->varinfo [i];
1821 ins->opcode = OP_REGOFFSET;
1822 ins->inst_basereg = cfg->frame_reg;
1823 if (cfg->arch.omit_fp)
1824 ins->inst_offset = (offset + offsets [i]);
1826 ins->inst_offset = - (offset + offsets [i]);
1827 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1830 offset += locals_stack_size;
1832 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1833 g_assert (!cfg->arch.omit_fp);
1834 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1835 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1838 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1839 ins = cfg->args [i];
1840 if (ins->opcode != OP_REGVAR) {
1841 ArgInfo *ainfo = &cinfo->args [i];
1842 gboolean inreg = TRUE;
1844 /* FIXME: Allocate volatile arguments to registers */
1845 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1849 * Under AMD64, all registers used to pass arguments to functions
1850 * are volatile across calls.
1851 * FIXME: Optimize this.
1853 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg))
1856 ins->opcode = OP_REGOFFSET;
1858 switch (ainfo->storage) {
1860 case ArgInFloatSSEReg:
1861 case ArgInDoubleSSEReg:
1863 ins->opcode = OP_REGVAR;
1864 ins->dreg = ainfo->reg;
1868 g_assert (!cfg->arch.omit_fp);
1869 ins->opcode = OP_REGOFFSET;
1870 ins->inst_basereg = cfg->frame_reg;
1871 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1873 case ArgValuetypeInReg:
1875 case ArgValuetypeAddrInIReg: {
1877 g_assert (!cfg->arch.omit_fp);
1879 MONO_INST_NEW (cfg, indir, 0);
1880 indir->opcode = OP_REGOFFSET;
1881 if (ainfo->pair_storage [0] == ArgInIReg) {
1882 indir->inst_basereg = cfg->frame_reg;
1883 offset = ALIGN_TO (offset, sizeof (gpointer));
1884 offset += (sizeof (gpointer));
1885 indir->inst_offset = - offset;
1888 indir->inst_basereg = cfg->frame_reg;
1889 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
1892 ins->opcode = OP_VTARG_ADDR;
1893 ins->inst_left = indir;
1901 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg)) {
1902 ins->opcode = OP_REGOFFSET;
1903 ins->inst_basereg = cfg->frame_reg;
1904 /* These arguments are saved to the stack in the prolog */
1905 offset = ALIGN_TO (offset, sizeof(mgreg_t));
1906 if (cfg->arch.omit_fp) {
1907 ins->inst_offset = offset;
1908 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1909 // Arguments are yet supported by the stack map creation code
1910 //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
1912 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1913 ins->inst_offset = - offset;
1914 //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
1920 cfg->stack_offset = offset;
1924 mono_arch_create_vars (MonoCompile *cfg)
1926 MonoMethodSignature *sig;
1930 sig = mono_method_signature (cfg->method);
1932 if (!cfg->arch.cinfo)
1933 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1934 cinfo = cfg->arch.cinfo;
1936 if (cinfo->ret.storage == ArgValuetypeInReg)
1937 cfg->ret_var_is_local = TRUE;
1939 sig_ret = mini_get_underlying_type (cfg, sig->ret);
1940 if ((cinfo->ret.storage != ArgValuetypeInReg) && MONO_TYPE_ISSTRUCT (sig_ret)) {
1941 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1942 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1943 printf ("vret_addr = ");
1944 mono_print_ins (cfg->vret_addr);
1948 if (cfg->gen_sdb_seq_points) {
1951 if (cfg->compile_aot) {
1952 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1953 ins->flags |= MONO_INST_VOLATILE;
1954 cfg->arch.seq_point_info_var = ins;
1956 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1957 ins->flags |= MONO_INST_VOLATILE;
1958 cfg->arch.ss_tramp_var = ins;
1961 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1962 ins->flags |= MONO_INST_VOLATILE;
1963 cfg->arch.ss_trigger_page_var = ins;
1966 if (cfg->method->save_lmf)
1967 cfg->create_lmf_var = TRUE;
1969 if (cfg->method->save_lmf) {
1971 #if !defined(TARGET_WIN32)
1972 if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
1973 cfg->lmf_ir_mono_lmf = TRUE;
1979 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
1985 MONO_INST_NEW (cfg, ins, OP_MOVE);
1986 ins->dreg = mono_alloc_ireg_copy (cfg, tree->dreg);
1987 ins->sreg1 = tree->dreg;
1988 MONO_ADD_INS (cfg->cbb, ins);
1989 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
1991 case ArgInFloatSSEReg:
1992 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
1993 ins->dreg = mono_alloc_freg (cfg);
1994 ins->sreg1 = tree->dreg;
1995 MONO_ADD_INS (cfg->cbb, ins);
1997 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
1999 case ArgInDoubleSSEReg:
2000 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2001 ins->dreg = mono_alloc_freg (cfg);
2002 ins->sreg1 = tree->dreg;
2003 MONO_ADD_INS (cfg->cbb, ins);
2005 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2009 g_assert_not_reached ();
2014 arg_storage_to_load_membase (ArgStorage storage)
2018 #if defined(__mono_ilp32__)
2019 return OP_LOADI8_MEMBASE;
2021 return OP_LOAD_MEMBASE;
2023 case ArgInDoubleSSEReg:
2024 return OP_LOADR8_MEMBASE;
2025 case ArgInFloatSSEReg:
2026 return OP_LOADR4_MEMBASE;
2028 g_assert_not_reached ();
2035 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2037 MonoMethodSignature *tmp_sig;
2040 if (call->tail_call)
2043 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
2046 * mono_ArgIterator_Setup assumes the signature cookie is
2047 * passed first and all the arguments which were before it are
2048 * passed on the stack after the signature. So compensate by
2049 * passing a different signature.
2051 tmp_sig = mono_metadata_signature_dup_full (cfg->method->klass->image, call->signature);
2052 tmp_sig->param_count -= call->signature->sentinelpos;
2053 tmp_sig->sentinelpos = 0;
2054 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2056 sig_reg = mono_alloc_ireg (cfg);
2057 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2059 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, cinfo->sig_cookie.offset, sig_reg);
2063 static inline LLVMArgStorage
2064 arg_storage_to_llvm_arg_storage (MonoCompile *cfg, ArgStorage storage)
2068 return LLVMArgInIReg;
2072 g_assert_not_reached ();
2078 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2084 LLVMCallInfo *linfo;
2085 MonoType *t, *sig_ret;
2087 n = sig->param_count + sig->hasthis;
2088 sig_ret = mini_get_underlying_type (cfg, sig->ret);
2090 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
2092 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2095 * LLVM always uses the native ABI while we use our own ABI, the
2096 * only difference is the handling of vtypes:
2097 * - we only pass/receive them in registers in some cases, and only
2098 * in 1 or 2 integer registers.
2100 if (cinfo->ret.storage == ArgValuetypeInReg) {
2102 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2103 cfg->disable_llvm = TRUE;
2107 linfo->ret.storage = LLVMArgVtypeInReg;
2108 for (j = 0; j < 2; ++j)
2109 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
2112 if (MONO_TYPE_ISSTRUCT (sig_ret) && cinfo->ret.storage == ArgInIReg) {
2113 /* Vtype returned using a hidden argument */
2114 linfo->ret.storage = LLVMArgVtypeRetAddr;
2115 linfo->vret_arg_index = cinfo->vret_arg_index;
2118 for (i = 0; i < n; ++i) {
2119 ainfo = cinfo->args + i;
2121 if (i >= sig->hasthis)
2122 t = sig->params [i - sig->hasthis];
2124 t = &mono_defaults.int_class->byval_arg;
2126 linfo->args [i].storage = LLVMArgNone;
2128 switch (ainfo->storage) {
2130 linfo->args [i].storage = LLVMArgInIReg;
2132 case ArgInDoubleSSEReg:
2133 case ArgInFloatSSEReg:
2134 linfo->args [i].storage = LLVMArgInFPReg;
2137 if (MONO_TYPE_ISSTRUCT (t)) {
2138 linfo->args [i].storage = LLVMArgVtypeByVal;
2140 linfo->args [i].storage = LLVMArgInIReg;
2142 if (t->type == MONO_TYPE_R4)
2143 linfo->args [i].storage = LLVMArgInFPReg;
2144 else if (t->type == MONO_TYPE_R8)
2145 linfo->args [i].storage = LLVMArgInFPReg;
2149 case ArgValuetypeInReg:
2151 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2152 cfg->disable_llvm = TRUE;
2156 linfo->args [i].storage = LLVMArgVtypeInReg;
2157 for (j = 0; j < 2; ++j)
2158 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2161 cfg->exception_message = g_strdup ("ainfo->storage");
2162 cfg->disable_llvm = TRUE;
2172 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2175 MonoMethodSignature *sig;
2181 sig = call->signature;
2182 n = sig->param_count + sig->hasthis;
2184 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
2188 if (COMPILE_LLVM (cfg)) {
2189 /* We shouldn't be called in the llvm case */
2190 cfg->disable_llvm = TRUE;
2195 * Emit all arguments which are passed on the stack to prevent register
2196 * allocation problems.
2198 for (i = 0; i < n; ++i) {
2200 ainfo = cinfo->args + i;
2202 in = call->args [i];
2204 if (sig->hasthis && i == 0)
2205 t = &mono_defaults.object_class->byval_arg;
2207 t = sig->params [i - sig->hasthis];
2209 t = mini_get_underlying_type (cfg, t);
2210 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call) {
2212 if (t->type == MONO_TYPE_R4)
2213 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2214 else if (t->type == MONO_TYPE_R8)
2215 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2217 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2219 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2221 if (cfg->compute_gc_maps) {
2224 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2230 * Emit all parameters passed in registers in non-reverse order for better readability
2231 * and to help the optimization in emit_prolog ().
2233 for (i = 0; i < n; ++i) {
2234 ainfo = cinfo->args + i;
2236 in = call->args [i];
2238 if (ainfo->storage == ArgInIReg)
2239 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2242 for (i = n - 1; i >= 0; --i) {
2243 ainfo = cinfo->args + i;
2245 in = call->args [i];
2247 switch (ainfo->storage) {
2251 case ArgInFloatSSEReg:
2252 case ArgInDoubleSSEReg:
2253 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2256 case ArgValuetypeInReg:
2257 case ArgValuetypeAddrInIReg:
2258 if (ainfo->storage == ArgOnStack && call->tail_call) {
2259 MonoInst *call_inst = (MonoInst*)call;
2260 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2261 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2262 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
2266 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
2267 size = sizeof (MonoTypedRef);
2268 align = sizeof (gpointer);
2272 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
2275 * Other backends use mono_type_stack_size (), but that
2276 * aligns the size to 8, which is larger than the size of
2277 * the source, leading to reads of invalid memory if the
2278 * source is at the end of address space.
2280 size = mono_class_value_size (in->klass, &align);
2283 g_assert (in->klass);
2285 if (ainfo->storage == ArgOnStack && size >= 10000) {
2286 /* Avoid asserts in emit_memcpy () */
2287 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
2288 cfg->exception_message = g_strdup_printf ("Passing an argument of size '%d'.", size);
2289 /* Continue normally */
2293 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2294 arg->sreg1 = in->dreg;
2295 arg->klass = in->klass;
2296 arg->backend.size = size;
2297 arg->inst_p0 = call;
2298 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2299 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2301 MONO_ADD_INS (cfg->cbb, arg);
2306 g_assert_not_reached ();
2309 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
2310 /* Emit the signature cookie just before the implicit arguments */
2311 emit_sig_cookie (cfg, call, cinfo);
2314 /* Handle the case where there are no implicit arguments */
2315 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2316 emit_sig_cookie (cfg, call, cinfo);
2318 sig_ret = mini_get_underlying_type (cfg, sig->ret);
2319 if (sig_ret && MONO_TYPE_ISSTRUCT (sig_ret)) {
2322 if (cinfo->ret.storage == ArgValuetypeInReg) {
2323 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2325 * Tell the JIT to use a more efficient calling convention: call using
2326 * OP_CALL, compute the result location after the call, and save the
2329 call->vret_in_reg = TRUE;
2331 * Nullify the instruction computing the vret addr to enable
2332 * future optimizations.
2335 NULLIFY_INS (call->vret_var);
2337 if (call->tail_call)
2340 * The valuetype is in RAX:RDX after the call, need to be copied to
2341 * the stack. Push the address here, so the call instruction can
2344 if (!cfg->arch.vret_addr_loc) {
2345 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2346 /* Prevent it from being register allocated or optimized away */
2347 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2350 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2354 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2355 vtarg->sreg1 = call->vret_var->dreg;
2356 vtarg->dreg = mono_alloc_preg (cfg);
2357 MONO_ADD_INS (cfg->cbb, vtarg);
2359 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2363 if (cfg->method->save_lmf) {
2364 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2365 MONO_ADD_INS (cfg->cbb, arg);
2368 call->stack_usage = cinfo->stack_usage;
2372 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2375 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2376 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2377 int size = ins->backend.size;
2379 if (ainfo->storage == ArgValuetypeInReg) {
2383 for (part = 0; part < 2; ++part) {
2384 if (ainfo->pair_storage [part] == ArgNone)
2387 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2388 load->inst_basereg = src->dreg;
2389 load->inst_offset = part * sizeof(mgreg_t);
2391 switch (ainfo->pair_storage [part]) {
2393 load->dreg = mono_alloc_ireg (cfg);
2395 case ArgInDoubleSSEReg:
2396 case ArgInFloatSSEReg:
2397 load->dreg = mono_alloc_freg (cfg);
2400 g_assert_not_reached ();
2402 MONO_ADD_INS (cfg->cbb, load);
2404 add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2406 } else if (ainfo->storage == ArgValuetypeAddrInIReg) {
2407 MonoInst *vtaddr, *load;
2408 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2410 MONO_INST_NEW (cfg, load, OP_LDADDR);
2411 cfg->has_indirection = TRUE;
2412 load->inst_p0 = vtaddr;
2413 vtaddr->flags |= MONO_INST_INDIRECT;
2414 load->type = STACK_MP;
2415 load->klass = vtaddr->klass;
2416 load->dreg = mono_alloc_ireg (cfg);
2417 MONO_ADD_INS (cfg->cbb, load);
2418 mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2420 if (ainfo->pair_storage [0] == ArgInIReg) {
2421 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2422 arg->dreg = mono_alloc_ireg (cfg);
2423 arg->sreg1 = load->dreg;
2425 MONO_ADD_INS (cfg->cbb, arg);
2426 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2428 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, load->dreg);
2432 int dreg = mono_alloc_ireg (cfg);
2434 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
2435 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, dreg);
2436 } else if (size <= 40) {
2437 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2439 // FIXME: Code growth
2440 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2443 if (cfg->compute_gc_maps) {
2445 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2451 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2453 MonoType *ret = mini_get_underlying_type (cfg, mono_method_signature (method)->ret);
2455 if (ret->type == MONO_TYPE_R4) {
2456 if (COMPILE_LLVM (cfg))
2457 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2459 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2461 } else if (ret->type == MONO_TYPE_R8) {
2462 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2466 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2469 #endif /* DISABLE_JIT */
2471 #define EMIT_COND_BRANCH(ins,cond,sign) \
2472 if (ins->inst_true_bb->native_offset) { \
2473 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2475 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2476 if ((cfg->opt & MONO_OPT_BRANCH) && \
2477 x86_is_imm8 (ins->inst_true_bb->max_offset - offset)) \
2478 x86_branch8 (code, cond, 0, sign); \
2480 x86_branch32 (code, cond, 0, sign); \
2484 MonoMethodSignature *sig;
2489 dyn_call_supported (MonoMethodSignature *sig, CallInfo *cinfo)
2497 switch (cinfo->ret.storage) {
2501 case ArgValuetypeInReg: {
2502 ArgInfo *ainfo = &cinfo->ret;
2504 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2506 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2514 for (i = 0; i < cinfo->nargs; ++i) {
2515 ArgInfo *ainfo = &cinfo->args [i];
2516 switch (ainfo->storage) {
2519 case ArgValuetypeInReg:
2520 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2522 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2534 * mono_arch_dyn_call_prepare:
2536 * Return a pointer to an arch-specific structure which contains information
2537 * needed by mono_arch_get_dyn_call_args (). Return NULL if OP_DYN_CALL is not
2538 * supported for SIG.
2539 * This function is equivalent to ffi_prep_cif in libffi.
2542 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2544 ArchDynCallInfo *info;
2547 cinfo = get_call_info (NULL, NULL, sig);
2549 if (!dyn_call_supported (sig, cinfo)) {
2554 info = g_new0 (ArchDynCallInfo, 1);
2555 // FIXME: Preprocess the info to speed up get_dyn_call_args ().
2557 info->cinfo = cinfo;
2559 return (MonoDynCallInfo*)info;
2563 * mono_arch_dyn_call_free:
2565 * Free a MonoDynCallInfo structure.
2568 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2570 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2572 g_free (ainfo->cinfo);
2576 #if !defined(__native_client__)
2577 #define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
2578 #define GREG_TO_PTR(greg) (gpointer)(greg)
2580 /* Correctly handle casts to/from 32-bit pointers without compiler warnings */
2581 #define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
2582 #define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
2586 * mono_arch_get_start_dyn_call:
2588 * Convert the arguments ARGS to a format which can be passed to OP_DYN_CALL, and
2589 * store the result into BUF.
2590 * ARGS should be an array of pointers pointing to the arguments.
2591 * RET should point to a memory buffer large enought to hold the result of the
2593 * This function should be as fast as possible, any work which does not depend
2594 * on the actual values of the arguments should be done in
2595 * mono_arch_dyn_call_prepare ().
2596 * start_dyn_call + OP_DYN_CALL + finish_dyn_call is equivalent to ffi_call in
2600 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2602 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2603 DynCallArgs *p = (DynCallArgs*)buf;
2604 int arg_index, greg, i, pindex;
2605 MonoMethodSignature *sig = dinfo->sig;
2607 g_assert (buf_len >= sizeof (DynCallArgs));
2616 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2617 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2622 if (dinfo->cinfo->vtype_retaddr)
2623 p->regs [greg ++] = PTR_TO_GREG(ret);
2625 for (i = pindex; i < sig->param_count; i++) {
2626 MonoType *t = mini_type_get_underlying_type (NULL, sig->params [i]);
2627 gpointer *arg = args [arg_index ++];
2630 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2635 case MONO_TYPE_STRING:
2636 case MONO_TYPE_CLASS:
2637 case MONO_TYPE_ARRAY:
2638 case MONO_TYPE_SZARRAY:
2639 case MONO_TYPE_OBJECT:
2643 #if !defined(__mono_ilp32__)
2647 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2648 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2650 #if defined(__mono_ilp32__)
2653 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2654 p->regs [greg ++] = *(guint64*)(arg);
2658 p->regs [greg ++] = *(guint8*)(arg);
2661 p->regs [greg ++] = *(gint8*)(arg);
2664 p->regs [greg ++] = *(gint16*)(arg);
2667 p->regs [greg ++] = *(guint16*)(arg);
2670 p->regs [greg ++] = *(gint32*)(arg);
2673 p->regs [greg ++] = *(guint32*)(arg);
2675 case MONO_TYPE_GENERICINST:
2676 if (MONO_TYPE_IS_REFERENCE (t)) {
2677 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2682 case MONO_TYPE_VALUETYPE: {
2683 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2685 g_assert (ainfo->storage == ArgValuetypeInReg);
2686 if (ainfo->pair_storage [0] != ArgNone) {
2687 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2688 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2690 if (ainfo->pair_storage [1] != ArgNone) {
2691 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2692 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2697 g_assert_not_reached ();
2701 g_assert (greg <= PARAM_REGS);
2705 * mono_arch_finish_dyn_call:
2707 * Store the result of a dyn call into the return value buffer passed to
2708 * start_dyn_call ().
2709 * This function should be as fast as possible, any work which does not depend
2710 * on the actual values of the arguments should be done in
2711 * mono_arch_dyn_call_prepare ().
2714 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2716 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2717 MonoMethodSignature *sig = dinfo->sig;
2718 guint8 *ret = ((DynCallArgs*)buf)->ret;
2719 mgreg_t res = ((DynCallArgs*)buf)->res;
2720 MonoType *sig_ret = mini_type_get_underlying_type (NULL, sig->ret);
2722 switch (sig_ret->type) {
2723 case MONO_TYPE_VOID:
2724 *(gpointer*)ret = NULL;
2726 case MONO_TYPE_STRING:
2727 case MONO_TYPE_CLASS:
2728 case MONO_TYPE_ARRAY:
2729 case MONO_TYPE_SZARRAY:
2730 case MONO_TYPE_OBJECT:
2734 *(gpointer*)ret = GREG_TO_PTR(res);
2740 *(guint8*)ret = res;
2743 *(gint16*)ret = res;
2746 *(guint16*)ret = res;
2749 *(gint32*)ret = res;
2752 *(guint32*)ret = res;
2755 *(gint64*)ret = res;
2758 *(guint64*)ret = res;
2760 case MONO_TYPE_GENERICINST:
2761 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
2762 *(gpointer*)ret = GREG_TO_PTR(res);
2767 case MONO_TYPE_VALUETYPE:
2768 if (dinfo->cinfo->vtype_retaddr) {
2771 ArgInfo *ainfo = &dinfo->cinfo->ret;
2773 g_assert (ainfo->storage == ArgValuetypeInReg);
2775 if (ainfo->pair_storage [0] != ArgNone) {
2776 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2777 ((mgreg_t*)ret)[0] = res;
2780 g_assert (ainfo->pair_storage [1] == ArgNone);
2784 g_assert_not_reached ();
2788 /* emit an exception if condition is fail */
2789 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2791 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2792 if (tins == NULL) { \
2793 mono_add_patch_info (cfg, code - cfg->native_code, \
2794 MONO_PATCH_INFO_EXC, exc_name); \
2795 x86_branch32 (code, cond, 0, signed); \
2797 EMIT_COND_BRANCH (tins, cond, signed); \
2801 #define EMIT_FPCOMPARE(code) do { \
2802 amd64_fcompp (code); \
2803 amd64_fnstsw (code); \
2806 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
2807 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
2808 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
2809 amd64_ ##op (code); \
2810 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
2811 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
2815 emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
2817 gboolean no_patch = FALSE;
2820 * FIXME: Add support for thunks
2823 gboolean near_call = FALSE;
2826 * Indirect calls are expensive so try to make a near call if possible.
2827 * The caller memory is allocated by the code manager so it is
2828 * guaranteed to be at a 32 bit offset.
2831 if (patch_type != MONO_PATCH_INFO_ABS) {
2832 /* The target is in memory allocated using the code manager */
2835 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
2836 if (((MonoMethod*)data)->klass->image->aot_module)
2837 /* The callee might be an AOT method */
2839 if (((MonoMethod*)data)->dynamic)
2840 /* The target is in malloc-ed memory */
2844 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
2846 * The call might go directly to a native function without
2849 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (data);
2851 gconstpointer target = mono_icall_get_wrapper (mi);
2852 if ((((guint64)target) >> 32) != 0)
2858 MonoJumpInfo *jinfo = NULL;
2860 if (cfg->abs_patches)
2861 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
2863 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2864 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
2865 if (mi && (((guint64)mi->func) >> 32) == 0)
2870 * This is not really an optimization, but required because the
2871 * generic class init trampolines use R11 to pass the vtable.
2876 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
2878 if (info->func == info->wrapper) {
2880 if ((((guint64)info->func) >> 32) == 0)
2884 /* See the comment in mono_codegen () */
2885 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
2889 else if ((((guint64)data) >> 32) == 0) {
2896 if (cfg->method->dynamic)
2897 /* These methods are allocated using malloc */
2900 #ifdef MONO_ARCH_NOMAP32BIT
2903 #if defined(__native_client__)
2904 /* Always use near_call == TRUE for Native Client */
2907 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
2908 if (optimize_for_xen)
2911 if (cfg->compile_aot) {
2918 * Align the call displacement to an address divisible by 4 so it does
2919 * not span cache lines. This is required for code patching to work on SMP
2922 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
2923 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
2924 amd64_padding (code, pad_size);
2926 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2927 amd64_call_code (code, 0);
2930 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2931 amd64_set_reg_template (code, GP_SCRATCH_REG);
2932 amd64_call_reg (code, GP_SCRATCH_REG);
2939 static inline guint8*
2940 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
2943 if (win64_adjust_stack)
2944 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
2946 code = emit_call_body (cfg, code, patch_type, data);
2948 if (win64_adjust_stack)
2949 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
2956 store_membase_imm_to_store_membase_reg (int opcode)
2959 case OP_STORE_MEMBASE_IMM:
2960 return OP_STORE_MEMBASE_REG;
2961 case OP_STOREI4_MEMBASE_IMM:
2962 return OP_STOREI4_MEMBASE_REG;
2963 case OP_STOREI8_MEMBASE_IMM:
2964 return OP_STOREI8_MEMBASE_REG;
2972 #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)))
2975 * mono_arch_peephole_pass_1:
2977 * Perform peephole opts which should/can be performed before local regalloc
2980 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2984 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2985 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
2987 switch (ins->opcode) {
2991 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
2993 * X86_LEA is like ADD, but doesn't have the
2994 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
2995 * its operand to 64 bit.
2997 ins->opcode = OP_X86_LEA_MEMBASE;
2998 ins->inst_basereg = ins->sreg1;
3003 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3007 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3008 * the latter has length 2-3 instead of 6 (reverse constant
3009 * propagation). These instruction sequences are very common
3010 * in the initlocals bblock.
3012 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3013 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)) {
3014 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3015 ins2->sreg1 = ins->dreg;
3016 } 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)) {
3018 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3021 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3029 case OP_COMPARE_IMM:
3030 case OP_LCOMPARE_IMM:
3031 /* OP_COMPARE_IMM (reg, 0)
3033 * OP_AMD64_TEST_NULL (reg)
3036 ins->opcode = OP_AMD64_TEST_NULL;
3038 case OP_ICOMPARE_IMM:
3040 ins->opcode = OP_X86_TEST_NULL;
3042 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3044 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3045 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3047 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3048 * OP_COMPARE_IMM reg, imm
3050 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3052 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3053 ins->inst_basereg == last_ins->inst_destbasereg &&
3054 ins->inst_offset == last_ins->inst_offset) {
3055 ins->opcode = OP_ICOMPARE_IMM;
3056 ins->sreg1 = last_ins->sreg1;
3058 /* check if we can remove cmp reg,0 with test null */
3060 ins->opcode = OP_X86_TEST_NULL;
3066 mono_peephole_ins (bb, ins);
3071 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3075 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3076 switch (ins->opcode) {
3079 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3080 /* reg = 0 -> XOR (reg, reg) */
3081 /* XOR sets cflags on x86, so we cant do it always */
3082 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3083 ins->opcode = OP_LXOR;
3084 ins->sreg1 = ins->dreg;
3085 ins->sreg2 = ins->dreg;
3093 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3094 * 0 result into 64 bits.
3096 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3097 ins->opcode = OP_IXOR;
3101 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3105 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3106 * the latter has length 2-3 instead of 6 (reverse constant
3107 * propagation). These instruction sequences are very common
3108 * in the initlocals bblock.
3110 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3111 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)) {
3112 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3113 ins2->sreg1 = ins->dreg;
3114 } 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)) {
3116 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3119 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3128 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3129 ins->opcode = OP_X86_INC_REG;
3132 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3133 ins->opcode = OP_X86_DEC_REG;
3137 mono_peephole_ins (bb, ins);
3141 #define NEW_INS(cfg,ins,dest,op) do { \
3142 MONO_INST_NEW ((cfg), (dest), (op)); \
3143 (dest)->cil_code = (ins)->cil_code; \
3144 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3148 * mono_arch_lowering_pass:
3150 * Converts complex opcodes into simpler ones so that each IR instruction
3151 * corresponds to one machine instruction.
3154 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3156 MonoInst *ins, *n, *temp;
3159 * FIXME: Need to add more instructions, but the current machine
3160 * description can't model some parts of the composite instructions like
3163 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3164 switch (ins->opcode) {
3168 case OP_IDIV_UN_IMM:
3169 case OP_IREM_UN_IMM:
3172 mono_decompose_op_imm (cfg, bb, ins);
3174 case OP_COMPARE_IMM:
3175 case OP_LCOMPARE_IMM:
3176 if (!amd64_is_imm32 (ins->inst_imm)) {
3177 NEW_INS (cfg, ins, temp, OP_I8CONST);
3178 temp->inst_c0 = ins->inst_imm;
3179 temp->dreg = mono_alloc_ireg (cfg);
3180 ins->opcode = OP_COMPARE;
3181 ins->sreg2 = temp->dreg;
3184 #ifndef __mono_ilp32__
3185 case OP_LOAD_MEMBASE:
3187 case OP_LOADI8_MEMBASE:
3188 #ifndef __native_client_codegen__
3189 /* Don't generate memindex opcodes (to simplify */
3190 /* read sandboxing) */
3191 if (!amd64_is_imm32 (ins->inst_offset)) {
3192 NEW_INS (cfg, ins, temp, OP_I8CONST);
3193 temp->inst_c0 = ins->inst_offset;
3194 temp->dreg = mono_alloc_ireg (cfg);
3195 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3196 ins->inst_indexreg = temp->dreg;
3200 #ifndef __mono_ilp32__
3201 case OP_STORE_MEMBASE_IMM:
3203 case OP_STOREI8_MEMBASE_IMM:
3204 if (!amd64_is_imm32 (ins->inst_imm)) {
3205 NEW_INS (cfg, ins, temp, OP_I8CONST);
3206 temp->inst_c0 = ins->inst_imm;
3207 temp->dreg = mono_alloc_ireg (cfg);
3208 ins->opcode = OP_STOREI8_MEMBASE_REG;
3209 ins->sreg1 = temp->dreg;
3212 #ifdef MONO_ARCH_SIMD_INTRINSICS
3213 case OP_EXPAND_I1: {
3214 int temp_reg1 = mono_alloc_ireg (cfg);
3215 int temp_reg2 = mono_alloc_ireg (cfg);
3216 int original_reg = ins->sreg1;
3218 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3219 temp->sreg1 = original_reg;
3220 temp->dreg = temp_reg1;
3222 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3223 temp->sreg1 = temp_reg1;
3224 temp->dreg = temp_reg2;
3227 NEW_INS (cfg, ins, temp, OP_LOR);
3228 temp->sreg1 = temp->dreg = temp_reg2;
3229 temp->sreg2 = temp_reg1;
3231 ins->opcode = OP_EXPAND_I2;
3232 ins->sreg1 = temp_reg2;
3241 bb->max_vreg = cfg->next_vreg;
3245 branch_cc_table [] = {
3246 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3247 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3248 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3251 /* Maps CMP_... constants to X86_CC_... constants */
3254 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3255 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3259 cc_signed_table [] = {
3260 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3261 FALSE, FALSE, FALSE, FALSE
3264 /*#include "cprop.c"*/
3266 static unsigned char*
3267 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3270 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3272 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3275 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3277 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3281 static unsigned char*
3282 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3284 int sreg = tree->sreg1;
3285 int need_touch = FALSE;
3287 #if defined(TARGET_WIN32)
3289 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3290 if (!tree->flags & MONO_INST_INIT)
3299 * If requested stack size is larger than one page,
3300 * perform stack-touch operation
3303 * Generate stack probe code.
3304 * Under Windows, it is necessary to allocate one page at a time,
3305 * "touching" stack after each successful sub-allocation. This is
3306 * because of the way stack growth is implemented - there is a
3307 * guard page before the lowest stack page that is currently commited.
3308 * Stack normally grows sequentially so OS traps access to the
3309 * guard page and commits more pages when needed.
3311 amd64_test_reg_imm (code, sreg, ~0xFFF);
3312 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3314 br[2] = code; /* loop */
3315 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3316 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3317 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3318 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3319 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3320 amd64_patch (br[3], br[2]);
3321 amd64_test_reg_reg (code, sreg, sreg);
3322 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3323 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3325 br[1] = code; x86_jump8 (code, 0);
3327 amd64_patch (br[0], code);
3328 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3329 amd64_patch (br[1], code);
3330 amd64_patch (br[4], code);
3333 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3335 if (tree->flags & MONO_INST_INIT) {
3337 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3338 amd64_push_reg (code, AMD64_RAX);
3341 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3342 amd64_push_reg (code, AMD64_RCX);
3345 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3346 amd64_push_reg (code, AMD64_RDI);
3350 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3351 if (sreg != AMD64_RCX)
3352 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3353 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3355 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3356 if (cfg->param_area)
3357 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3359 #if defined(__default_codegen__)
3360 amd64_prefix (code, X86_REP_PREFIX);
3362 #elif defined(__native_client_codegen__)
3363 /* NaCl stos pseudo-instruction */
3364 amd64_codegen_pre(code);
3365 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3366 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3367 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3368 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3369 amd64_prefix (code, X86_REP_PREFIX);
3371 amd64_codegen_post(code);
3372 #endif /* __native_client_codegen__ */
3374 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3375 amd64_pop_reg (code, AMD64_RDI);
3376 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3377 amd64_pop_reg (code, AMD64_RCX);
3378 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3379 amd64_pop_reg (code, AMD64_RAX);
3385 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3390 /* Move return value to the target register */
3391 /* FIXME: do this in the local reg allocator */
3392 switch (ins->opcode) {
3395 case OP_CALL_MEMBASE:
3398 case OP_LCALL_MEMBASE:
3399 g_assert (ins->dreg == AMD64_RAX);
3403 case OP_FCALL_MEMBASE: {
3404 MonoType *rtype = mini_get_underlying_type (cfg, ((MonoCallInst*)ins)->signature->ret);
3405 if (rtype->type == MONO_TYPE_R4) {
3406 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3409 if (ins->dreg != AMD64_XMM0)
3410 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3416 case OP_RCALL_MEMBASE:
3417 if (ins->dreg != AMD64_XMM0)
3418 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3422 case OP_VCALL_MEMBASE:
3425 case OP_VCALL2_MEMBASE:
3426 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, ((MonoCallInst*)ins)->signature);
3427 if (cinfo->ret.storage == ArgValuetypeInReg) {
3428 MonoInst *loc = cfg->arch.vret_addr_loc;
3430 /* Load the destination address */
3431 g_assert (loc->opcode == OP_REGOFFSET);
3432 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3434 for (quad = 0; quad < 2; quad ++) {
3435 switch (cinfo->ret.pair_storage [quad]) {
3437 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3439 case ArgInFloatSSEReg:
3440 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3442 case ArgInDoubleSSEReg:
3443 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3458 #endif /* DISABLE_JIT */
3461 static int tls_gs_offset;
3465 mono_amd64_have_tls_get (void)
3468 static gboolean have_tls_get = FALSE;
3469 static gboolean inited = FALSE;
3473 return have_tls_get;
3475 ins = (guint8*)pthread_getspecific;
3478 * We're looking for these two instructions:
3480 * mov %gs:[offset](,%rdi,8),%rax
3483 have_tls_get = ins [0] == 0x65 &&
3495 tls_gs_offset = ins[5];
3497 return have_tls_get;
3498 #elif defined(TARGET_ANDROID)
3506 mono_amd64_get_tls_gs_offset (void)
3509 return tls_gs_offset;
3511 g_assert_not_reached ();
3517 * mono_amd64_emit_tls_get:
3518 * @code: buffer to store code to
3519 * @dreg: hard register where to place the result
3520 * @tls_offset: offset info
3522 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3523 * the dreg register the item in the thread local storage identified
3526 * Returns: a pointer to the end of the stored code
3529 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3532 if (tls_offset < 64) {
3533 x86_prefix (code, X86_GS_PREFIX);
3534 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3538 g_assert (tls_offset < 0x440);
3539 /* Load TEB->TlsExpansionSlots */
3540 x86_prefix (code, X86_GS_PREFIX);
3541 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3542 amd64_test_reg_reg (code, dreg, dreg);
3544 amd64_branch (code, X86_CC_EQ, code, TRUE);
3545 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3546 amd64_patch (buf [0], code);
3548 #elif defined(__APPLE__)
3549 x86_prefix (code, X86_GS_PREFIX);
3550 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3552 if (optimize_for_xen) {
3553 x86_prefix (code, X86_FS_PREFIX);
3554 amd64_mov_reg_mem (code, dreg, 0, 8);
3555 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3557 x86_prefix (code, X86_FS_PREFIX);
3558 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3565 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3567 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3569 if (dreg != offset_reg)
3570 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3571 amd64_prefix (code, X86_GS_PREFIX);
3572 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3573 #elif defined(__linux__)
3576 if (dreg == offset_reg) {
3577 /* Use a temporary reg by saving it to the redzone */
3578 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3579 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3580 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3581 offset_reg = tmpreg;
3583 x86_prefix (code, X86_FS_PREFIX);
3584 amd64_mov_reg_mem (code, dreg, 0, 8);
3585 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3587 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3589 g_assert_not_reached ();
3595 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3598 g_assert_not_reached ();
3599 #elif defined(__APPLE__)
3600 x86_prefix (code, X86_GS_PREFIX);
3601 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3603 g_assert (!optimize_for_xen);
3604 x86_prefix (code, X86_FS_PREFIX);
3605 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3611 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3613 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3615 g_assert_not_reached ();
3616 #elif defined(__APPLE__)
3617 x86_prefix (code, X86_GS_PREFIX);
3618 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3620 x86_prefix (code, X86_FS_PREFIX);
3621 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3627 * mono_arch_translate_tls_offset:
3629 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3632 mono_arch_translate_tls_offset (int offset)
3635 return tls_gs_offset + (offset * 8);
3644 * Emit code to initialize an LMF structure at LMF_OFFSET.
3647 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3650 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3653 * sp is saved right before calls but we need to save it here too so
3654 * async stack walks would work.
3656 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3658 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3659 if (cfg->arch.omit_fp && cfa_offset != -1)
3660 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3662 /* These can't contain refs */
3663 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3664 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3665 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3666 /* These are handled automatically by the stack marking code */
3667 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3672 #define REAL_PRINT_REG(text,reg) \
3673 mono_assert (reg >= 0); \
3674 amd64_push_reg (code, AMD64_RAX); \
3675 amd64_push_reg (code, AMD64_RDX); \
3676 amd64_push_reg (code, AMD64_RCX); \
3677 amd64_push_reg (code, reg); \
3678 amd64_push_imm (code, reg); \
3679 amd64_push_imm (code, text " %d %p\n"); \
3680 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
3681 amd64_call_reg (code, AMD64_RAX); \
3682 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
3683 amd64_pop_reg (code, AMD64_RCX); \
3684 amd64_pop_reg (code, AMD64_RDX); \
3685 amd64_pop_reg (code, AMD64_RAX);
3687 /* benchmark and set based on cpu */
3688 #define LOOP_ALIGNMENT 8
3689 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3693 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3698 guint8 *code = cfg->native_code + cfg->code_len;
3701 /* Fix max_offset estimate for each successor bb */
3702 if (cfg->opt & MONO_OPT_BRANCH) {
3703 int current_offset = cfg->code_len;
3704 MonoBasicBlock *current_bb;
3705 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3706 current_bb->max_offset = current_offset;
3707 current_offset += current_bb->max_length;
3711 if (cfg->opt & MONO_OPT_LOOP) {
3712 int pad, align = LOOP_ALIGNMENT;
3713 /* set alignment depending on cpu */
3714 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3716 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3717 amd64_padding (code, pad);
3718 cfg->code_len += pad;
3719 bb->native_offset = cfg->code_len;
3723 #if defined(__native_client_codegen__)
3724 /* For Native Client, all indirect call/jump targets must be */
3725 /* 32-byte aligned. Exception handler blocks are jumped to */
3726 /* indirectly as well. */
3727 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
3728 (bb->flags & BB_EXCEPTION_HANDLER);
3730 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
3731 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
3732 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
3733 cfg->code_len += pad;
3734 bb->native_offset = cfg->code_len;
3736 #endif /*__native_client_codegen__*/
3738 if (cfg->verbose_level > 2)
3739 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3741 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
3742 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3743 g_assert (!cfg->compile_aot);
3745 cov->data [bb->dfn].cil_code = bb->cil_code;
3746 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3747 /* this is not thread save, but good enough */
3748 amd64_inc_membase (code, AMD64_R11, 0);
3751 offset = code - cfg->native_code;
3753 mono_debug_open_block (cfg, bb, offset);
3755 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)
3756 x86_breakpoint (code);
3758 MONO_BB_FOR_EACH_INS (bb, ins) {
3759 offset = code - cfg->native_code;
3761 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3763 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
3765 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
3766 cfg->code_size *= 2;
3767 cfg->native_code = mono_realloc_native_code(cfg);
3768 code = cfg->native_code + offset;
3769 cfg->stat_code_reallocs++;
3772 if (cfg->debug_info)
3773 mono_debug_record_line_number (cfg, ins, offset);
3775 switch (ins->opcode) {
3777 amd64_mul_reg (code, ins->sreg2, TRUE);
3780 amd64_mul_reg (code, ins->sreg2, FALSE);
3782 case OP_X86_SETEQ_MEMBASE:
3783 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3785 case OP_STOREI1_MEMBASE_IMM:
3786 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3788 case OP_STOREI2_MEMBASE_IMM:
3789 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
3791 case OP_STOREI4_MEMBASE_IMM:
3792 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
3794 case OP_STOREI1_MEMBASE_REG:
3795 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
3797 case OP_STOREI2_MEMBASE_REG:
3798 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
3800 /* In AMD64 NaCl, pointers are 4 bytes, */
3801 /* so STORE_* != STOREI8_*. Likewise below. */
3802 case OP_STORE_MEMBASE_REG:
3803 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
3805 case OP_STOREI8_MEMBASE_REG:
3806 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
3808 case OP_STOREI4_MEMBASE_REG:
3809 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
3811 case OP_STORE_MEMBASE_IMM:
3812 #ifndef __native_client_codegen__
3813 /* In NaCl, this could be a PCONST type, which could */
3814 /* mean a pointer type was copied directly into the */
3815 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
3816 /* the value would be 0x00000000FFFFFFFF which is */
3817 /* not proper for an imm32 unless you cast it. */
3818 g_assert (amd64_is_imm32 (ins->inst_imm));
3820 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
3822 case OP_STOREI8_MEMBASE_IMM:
3823 g_assert (amd64_is_imm32 (ins->inst_imm));
3824 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
3827 #ifdef __mono_ilp32__
3828 /* In ILP32, pointers are 4 bytes, so separate these */
3829 /* cases, use literal 8 below where we really want 8 */
3830 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3831 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
3835 // FIXME: Decompose this earlier
3836 if (amd64_is_imm32 (ins->inst_imm))
3837 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
3839 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3840 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
3844 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3845 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
3848 // FIXME: Decompose this earlier
3849 if (amd64_is_imm32 (ins->inst_imm))
3850 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
3852 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3853 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3857 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3858 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
3861 /* For NaCl, pointers are 4 bytes, so separate these */
3862 /* cases, use literal 8 below where we really want 8 */
3863 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3864 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
3866 case OP_LOAD_MEMBASE:
3867 g_assert (amd64_is_imm32 (ins->inst_offset));
3868 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
3870 case OP_LOADI8_MEMBASE:
3871 /* Use literal 8 instead of sizeof pointer or */
3872 /* register, we really want 8 for this opcode */
3873 g_assert (amd64_is_imm32 (ins->inst_offset));
3874 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
3876 case OP_LOADI4_MEMBASE:
3877 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3879 case OP_LOADU4_MEMBASE:
3880 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
3882 case OP_LOADU1_MEMBASE:
3883 /* The cpu zero extends the result into 64 bits */
3884 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
3886 case OP_LOADI1_MEMBASE:
3887 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
3889 case OP_LOADU2_MEMBASE:
3890 /* The cpu zero extends the result into 64 bits */
3891 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
3893 case OP_LOADI2_MEMBASE:
3894 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
3896 case OP_AMD64_LOADI8_MEMINDEX:
3897 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
3899 case OP_LCONV_TO_I1:
3900 case OP_ICONV_TO_I1:
3902 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3904 case OP_LCONV_TO_I2:
3905 case OP_ICONV_TO_I2:
3907 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
3909 case OP_LCONV_TO_U1:
3910 case OP_ICONV_TO_U1:
3911 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
3913 case OP_LCONV_TO_U2:
3914 case OP_ICONV_TO_U2:
3915 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
3918 /* Clean out the upper word */
3919 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
3922 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
3926 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3928 case OP_COMPARE_IMM:
3929 #if defined(__mono_ilp32__)
3930 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
3931 g_assert (amd64_is_imm32 (ins->inst_imm));
3932 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
3935 case OP_LCOMPARE_IMM:
3936 g_assert (amd64_is_imm32 (ins->inst_imm));
3937 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
3939 case OP_X86_COMPARE_REG_MEMBASE:
3940 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
3942 case OP_X86_TEST_NULL:
3943 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
3945 case OP_AMD64_TEST_NULL:
3946 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
3949 case OP_X86_ADD_REG_MEMBASE:
3950 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3952 case OP_X86_SUB_REG_MEMBASE:
3953 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3955 case OP_X86_AND_REG_MEMBASE:
3956 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3958 case OP_X86_OR_REG_MEMBASE:
3959 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3961 case OP_X86_XOR_REG_MEMBASE:
3962 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
3965 case OP_X86_ADD_MEMBASE_IMM:
3966 /* FIXME: Make a 64 version too */
3967 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3969 case OP_X86_SUB_MEMBASE_IMM:
3970 g_assert (amd64_is_imm32 (ins->inst_imm));
3971 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3973 case OP_X86_AND_MEMBASE_IMM:
3974 g_assert (amd64_is_imm32 (ins->inst_imm));
3975 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3977 case OP_X86_OR_MEMBASE_IMM:
3978 g_assert (amd64_is_imm32 (ins->inst_imm));
3979 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3981 case OP_X86_XOR_MEMBASE_IMM:
3982 g_assert (amd64_is_imm32 (ins->inst_imm));
3983 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
3985 case OP_X86_ADD_MEMBASE_REG:
3986 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3988 case OP_X86_SUB_MEMBASE_REG:
3989 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3991 case OP_X86_AND_MEMBASE_REG:
3992 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3994 case OP_X86_OR_MEMBASE_REG:
3995 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
3997 case OP_X86_XOR_MEMBASE_REG:
3998 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4000 case OP_X86_INC_MEMBASE:
4001 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4003 case OP_X86_INC_REG:
4004 amd64_inc_reg_size (code, ins->dreg, 4);
4006 case OP_X86_DEC_MEMBASE:
4007 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4009 case OP_X86_DEC_REG:
4010 amd64_dec_reg_size (code, ins->dreg, 4);
4012 case OP_X86_MUL_REG_MEMBASE:
4013 case OP_X86_MUL_MEMBASE_REG:
4014 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4016 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4017 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4019 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4020 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4022 case OP_AMD64_COMPARE_MEMBASE_REG:
4023 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4025 case OP_AMD64_COMPARE_MEMBASE_IMM:
4026 g_assert (amd64_is_imm32 (ins->inst_imm));
4027 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4029 case OP_X86_COMPARE_MEMBASE8_IMM:
4030 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4032 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4033 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4035 case OP_AMD64_COMPARE_REG_MEMBASE:
4036 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4039 case OP_AMD64_ADD_REG_MEMBASE:
4040 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4042 case OP_AMD64_SUB_REG_MEMBASE:
4043 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4045 case OP_AMD64_AND_REG_MEMBASE:
4046 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4048 case OP_AMD64_OR_REG_MEMBASE:
4049 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4051 case OP_AMD64_XOR_REG_MEMBASE:
4052 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4055 case OP_AMD64_ADD_MEMBASE_REG:
4056 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4058 case OP_AMD64_SUB_MEMBASE_REG:
4059 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4061 case OP_AMD64_AND_MEMBASE_REG:
4062 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4064 case OP_AMD64_OR_MEMBASE_REG:
4065 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4067 case OP_AMD64_XOR_MEMBASE_REG:
4068 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4071 case OP_AMD64_ADD_MEMBASE_IMM:
4072 g_assert (amd64_is_imm32 (ins->inst_imm));
4073 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4075 case OP_AMD64_SUB_MEMBASE_IMM:
4076 g_assert (amd64_is_imm32 (ins->inst_imm));
4077 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4079 case OP_AMD64_AND_MEMBASE_IMM:
4080 g_assert (amd64_is_imm32 (ins->inst_imm));
4081 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4083 case OP_AMD64_OR_MEMBASE_IMM:
4084 g_assert (amd64_is_imm32 (ins->inst_imm));
4085 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4087 case OP_AMD64_XOR_MEMBASE_IMM:
4088 g_assert (amd64_is_imm32 (ins->inst_imm));
4089 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4093 amd64_breakpoint (code);
4095 case OP_RELAXED_NOP:
4096 x86_prefix (code, X86_REP_PREFIX);
4104 case OP_DUMMY_STORE:
4105 case OP_DUMMY_ICONST:
4106 case OP_DUMMY_R8CONST:
4107 case OP_NOT_REACHED:
4110 case OP_IL_SEQ_POINT:
4111 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4113 case OP_SEQ_POINT: {
4116 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4117 if (cfg->compile_aot) {
4118 MonoInst *var = cfg->arch.ss_tramp_var;
4121 /* Load ss_tramp_var */
4122 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4123 /* Load the trampoline address */
4124 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4125 /* Call it if it is non-null */
4126 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4128 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4129 amd64_call_reg (code, AMD64_R11);
4130 amd64_patch (label, code);
4133 * Read from the single stepping trigger page. This will cause a
4134 * SIGSEGV when single stepping is enabled.
4135 * We do this _before_ the breakpoint, so single stepping after
4136 * a breakpoint is hit will step to the next IL offset.
4138 MonoInst *var = cfg->arch.ss_trigger_page_var;
4140 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4141 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4);
4146 * This is the address which is saved in seq points,
4148 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4150 if (cfg->compile_aot) {
4151 guint32 offset = code - cfg->native_code;
4153 MonoInst *info_var = cfg->arch.seq_point_info_var;
4157 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4158 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4159 /* Load the info->bp_addrs [offset], which is either NULL or the address of the breakpoint trampoline */
4160 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4161 amd64_test_reg_reg (code, AMD64_R11, AMD64_R11);
4163 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
4164 /* Call the trampoline */
4165 amd64_call_reg (code, AMD64_R11);
4166 amd64_patch (label, code);
4169 * A placeholder for a possible breakpoint inserted by
4170 * mono_arch_set_breakpoint ().
4172 for (i = 0; i < breakpoint_size; ++i)
4176 * Add an additional nop so skipping the bp doesn't cause the ip to point
4177 * to another IL offset.
4185 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4188 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4192 g_assert (amd64_is_imm32 (ins->inst_imm));
4193 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4196 g_assert (amd64_is_imm32 (ins->inst_imm));
4197 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4202 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4205 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4209 g_assert (amd64_is_imm32 (ins->inst_imm));
4210 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4213 g_assert (amd64_is_imm32 (ins->inst_imm));
4214 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4217 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4221 g_assert (amd64_is_imm32 (ins->inst_imm));
4222 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4225 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4230 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4232 switch (ins->inst_imm) {
4236 if (ins->dreg != ins->sreg1)
4237 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4238 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4241 /* LEA r1, [r2 + r2*2] */
4242 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4245 /* LEA r1, [r2 + r2*4] */
4246 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4249 /* LEA r1, [r2 + r2*2] */
4251 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4252 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4255 /* LEA r1, [r2 + r2*8] */
4256 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4259 /* LEA r1, [r2 + r2*4] */
4261 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4262 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4265 /* LEA r1, [r2 + r2*2] */
4267 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4268 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4271 /* LEA r1, [r2 + r2*4] */
4272 /* LEA r1, [r1 + r1*4] */
4273 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4274 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4277 /* LEA r1, [r2 + r2*4] */
4279 /* LEA r1, [r1 + r1*4] */
4280 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4281 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4282 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4285 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
4292 #if defined( __native_client_codegen__ )
4293 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4294 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4296 /* Regalloc magic makes the div/rem cases the same */
4297 if (ins->sreg2 == AMD64_RDX) {
4298 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4300 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4303 amd64_div_reg (code, ins->sreg2, TRUE);
4308 #if defined( __native_client_codegen__ )
4309 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4310 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4312 if (ins->sreg2 == AMD64_RDX) {
4313 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4314 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4315 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4317 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4318 amd64_div_reg (code, ins->sreg2, FALSE);
4323 #if defined( __native_client_codegen__ )
4324 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4325 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4327 if (ins->sreg2 == AMD64_RDX) {
4328 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4329 amd64_cdq_size (code, 4);
4330 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4332 amd64_cdq_size (code, 4);
4333 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4338 #if defined( __native_client_codegen__ )
4339 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4340 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4342 if (ins->sreg2 == AMD64_RDX) {
4343 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4344 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4345 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4347 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4348 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4352 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4353 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4356 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4360 g_assert (amd64_is_imm32 (ins->inst_imm));
4361 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4364 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4368 g_assert (amd64_is_imm32 (ins->inst_imm));
4369 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4372 g_assert (ins->sreg2 == AMD64_RCX);
4373 amd64_shift_reg (code, X86_SHL, ins->dreg);
4376 g_assert (ins->sreg2 == AMD64_RCX);
4377 amd64_shift_reg (code, X86_SAR, ins->dreg);
4381 g_assert (amd64_is_imm32 (ins->inst_imm));
4382 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4385 g_assert (amd64_is_imm32 (ins->inst_imm));
4386 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4388 case OP_LSHR_UN_IMM:
4389 g_assert (amd64_is_imm32 (ins->inst_imm));
4390 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4393 g_assert (ins->sreg2 == AMD64_RCX);
4394 amd64_shift_reg (code, X86_SHR, ins->dreg);
4398 g_assert (amd64_is_imm32 (ins->inst_imm));
4399 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4404 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4407 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4410 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4413 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4417 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4420 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4423 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4426 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4429 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4432 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4435 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4438 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4441 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4444 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4447 amd64_neg_reg_size (code, ins->sreg1, 4);
4450 amd64_not_reg_size (code, ins->sreg1, 4);
4453 g_assert (ins->sreg2 == AMD64_RCX);
4454 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4457 g_assert (ins->sreg2 == AMD64_RCX);
4458 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4461 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4463 case OP_ISHR_UN_IMM:
4464 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4467 g_assert (ins->sreg2 == AMD64_RCX);
4468 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4471 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4474 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4477 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4478 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4480 case OP_IMUL_OVF_UN:
4481 case OP_LMUL_OVF_UN: {
4482 /* the mul operation and the exception check should most likely be split */
4483 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4484 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4485 /*g_assert (ins->sreg2 == X86_EAX);
4486 g_assert (ins->dreg == X86_EAX);*/
4487 if (ins->sreg2 == X86_EAX) {
4488 non_eax_reg = ins->sreg1;
4489 } else if (ins->sreg1 == X86_EAX) {
4490 non_eax_reg = ins->sreg2;
4492 /* no need to save since we're going to store to it anyway */
4493 if (ins->dreg != X86_EAX) {
4495 amd64_push_reg (code, X86_EAX);
4497 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4498 non_eax_reg = ins->sreg2;
4500 if (ins->dreg == X86_EDX) {
4503 amd64_push_reg (code, X86_EAX);
4507 amd64_push_reg (code, X86_EDX);
4509 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4510 /* save before the check since pop and mov don't change the flags */
4511 if (ins->dreg != X86_EAX)
4512 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4514 amd64_pop_reg (code, X86_EDX);
4516 amd64_pop_reg (code, X86_EAX);
4517 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4521 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4523 case OP_ICOMPARE_IMM:
4524 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4546 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4554 case OP_CMOV_INE_UN:
4555 case OP_CMOV_IGE_UN:
4556 case OP_CMOV_IGT_UN:
4557 case OP_CMOV_ILE_UN:
4558 case OP_CMOV_ILT_UN:
4564 case OP_CMOV_LNE_UN:
4565 case OP_CMOV_LGE_UN:
4566 case OP_CMOV_LGT_UN:
4567 case OP_CMOV_LLE_UN:
4568 case OP_CMOV_LLT_UN:
4569 g_assert (ins->dreg == ins->sreg1);
4570 /* This needs to operate on 64 bit values */
4571 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);
4575 amd64_not_reg (code, ins->sreg1);
4578 amd64_neg_reg (code, ins->sreg1);
4583 if ((((guint64)ins->inst_c0) >> 32) == 0)
4584 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4586 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4589 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4590 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4593 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4594 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4597 if (ins->dreg != ins->sreg1)
4598 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4600 case OP_AMD64_SET_XMMREG_R4: {
4602 if (ins->dreg != ins->sreg1)
4603 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4605 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4609 case OP_AMD64_SET_XMMREG_R8: {
4610 if (ins->dreg != ins->sreg1)
4611 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4615 MonoCallInst *call = (MonoCallInst*)ins;
4616 int i, save_area_offset;
4618 g_assert (!cfg->method->save_lmf);
4620 /* Restore callee saved registers */
4621 save_area_offset = cfg->arch.reg_save_area_offset;
4622 for (i = 0; i < AMD64_NREG; ++i)
4623 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4624 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4625 save_area_offset += 8;
4628 if (cfg->arch.omit_fp) {
4629 if (cfg->arch.stack_alloc_size)
4630 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4632 if (call->stack_usage)
4635 /* Copy arguments on the stack to our argument area */
4636 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4637 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4638 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4644 offset = code - cfg->native_code;
4645 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4646 if (cfg->compile_aot)
4647 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4649 amd64_set_reg_template (code, AMD64_R11);
4650 amd64_jump_reg (code, AMD64_R11);
4651 ins->flags |= MONO_INST_GC_CALLSITE;
4652 ins->backend.pc_offset = code - cfg->native_code;
4656 /* ensure ins->sreg1 is not NULL */
4657 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4660 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4661 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4671 call = (MonoCallInst*)ins;
4673 * The AMD64 ABI forces callers to know about varargs.
4675 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4676 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4677 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4679 * Since the unmanaged calling convention doesn't contain a
4680 * 'vararg' entry, we have to treat every pinvoke call as a
4681 * potential vararg call.
4685 for (i = 0; i < AMD64_XMM_NREG; ++i)
4686 if (call->used_fregs & (1 << i))
4689 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4691 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4694 if (ins->flags & MONO_INST_HAS_METHOD)
4695 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4697 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
4698 ins->flags |= MONO_INST_GC_CALLSITE;
4699 ins->backend.pc_offset = code - cfg->native_code;
4700 code = emit_move_return_value (cfg, ins, code);
4707 case OP_VOIDCALL_REG:
4709 call = (MonoCallInst*)ins;
4711 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4712 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4713 ins->sreg1 = AMD64_R11;
4717 * The AMD64 ABI forces callers to know about varargs.
4719 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4720 if (ins->sreg1 == AMD64_RAX) {
4721 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4722 ins->sreg1 = AMD64_R11;
4724 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4725 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4727 * Since the unmanaged calling convention doesn't contain a
4728 * 'vararg' entry, we have to treat every pinvoke call as a
4729 * potential vararg call.
4733 for (i = 0; i < AMD64_XMM_NREG; ++i)
4734 if (call->used_fregs & (1 << i))
4736 if (ins->sreg1 == AMD64_RAX) {
4737 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4738 ins->sreg1 = AMD64_R11;
4741 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4743 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4746 amd64_call_reg (code, ins->sreg1);
4747 ins->flags |= MONO_INST_GC_CALLSITE;
4748 ins->backend.pc_offset = code - cfg->native_code;
4749 code = emit_move_return_value (cfg, ins, code);
4751 case OP_FCALL_MEMBASE:
4752 case OP_RCALL_MEMBASE:
4753 case OP_LCALL_MEMBASE:
4754 case OP_VCALL_MEMBASE:
4755 case OP_VCALL2_MEMBASE:
4756 case OP_VOIDCALL_MEMBASE:
4757 case OP_CALL_MEMBASE:
4758 call = (MonoCallInst*)ins;
4760 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
4761 ins->flags |= MONO_INST_GC_CALLSITE;
4762 ins->backend.pc_offset = code - cfg->native_code;
4763 code = emit_move_return_value (cfg, ins, code);
4767 MonoInst *var = cfg->dyn_call_var;
4769 g_assert (var->opcode == OP_REGOFFSET);
4771 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
4772 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4774 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
4776 /* Save args buffer */
4777 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
4779 /* Set argument registers */
4780 for (i = 0; i < PARAM_REGS; ++i)
4781 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
4784 amd64_call_reg (code, AMD64_R10);
4786 ins->flags |= MONO_INST_GC_CALLSITE;
4787 ins->backend.pc_offset = code - cfg->native_code;
4790 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4791 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
4794 case OP_AMD64_SAVE_SP_TO_LMF: {
4795 MonoInst *lmf_var = cfg->lmf_var;
4796 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
4800 g_assert_not_reached ();
4801 amd64_push_reg (code, ins->sreg1);
4803 case OP_X86_PUSH_IMM:
4804 g_assert_not_reached ();
4805 g_assert (amd64_is_imm32 (ins->inst_imm));
4806 amd64_push_imm (code, ins->inst_imm);
4808 case OP_X86_PUSH_MEMBASE:
4809 g_assert_not_reached ();
4810 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
4812 case OP_X86_PUSH_OBJ: {
4813 int size = ALIGN_TO (ins->inst_imm, 8);
4815 g_assert_not_reached ();
4817 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4818 amd64_push_reg (code, AMD64_RDI);
4819 amd64_push_reg (code, AMD64_RSI);
4820 amd64_push_reg (code, AMD64_RCX);
4821 if (ins->inst_offset)
4822 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
4824 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
4825 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
4826 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
4828 amd64_prefix (code, X86_REP_PREFIX);
4830 amd64_pop_reg (code, AMD64_RCX);
4831 amd64_pop_reg (code, AMD64_RSI);
4832 amd64_pop_reg (code, AMD64_RDI);
4835 case OP_GENERIC_CLASS_INIT: {
4836 static int byte_offset = -1;
4837 static guint8 bitmask;
4840 g_assert (ins->sreg1 == MONO_AMD64_ARG_REG1);
4842 if (byte_offset < 0)
4843 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
4845 amd64_test_membase_imm_size (code, ins->sreg1, byte_offset, bitmask, 1);
4847 amd64_branch8 (code, X86_CC_NZ, -1, 1);
4849 code = emit_call (cfg, code, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init", FALSE);
4850 ins->flags |= MONO_INST_GC_CALLSITE;
4851 ins->backend.pc_offset = code - cfg->native_code;
4853 x86_patch (jump, code);
4858 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
4860 case OP_X86_LEA_MEMBASE:
4861 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
4864 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
4867 /* keep alignment */
4868 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
4869 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
4870 code = mono_emit_stack_alloc (cfg, code, ins);
4871 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4872 if (cfg->param_area)
4873 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4875 case OP_LOCALLOC_IMM: {
4876 guint32 size = ins->inst_imm;
4877 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
4879 if (ins->flags & MONO_INST_INIT) {
4883 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4884 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4886 for (i = 0; i < size; i += 8)
4887 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
4888 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4890 amd64_mov_reg_imm (code, ins->dreg, size);
4891 ins->sreg1 = ins->dreg;
4893 code = mono_emit_stack_alloc (cfg, code, ins);
4894 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4897 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4898 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4900 if (cfg->param_area)
4901 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4905 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4906 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4907 (gpointer)"mono_arch_throw_exception", FALSE);
4908 ins->flags |= MONO_INST_GC_CALLSITE;
4909 ins->backend.pc_offset = code - cfg->native_code;
4913 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4914 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4915 (gpointer)"mono_arch_rethrow_exception", FALSE);
4916 ins->flags |= MONO_INST_GC_CALLSITE;
4917 ins->backend.pc_offset = code - cfg->native_code;
4920 case OP_CALL_HANDLER:
4922 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
4923 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4924 amd64_call_imm (code, 0);
4925 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4926 /* Restore stack alignment */
4927 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
4929 case OP_START_HANDLER: {
4930 /* Even though we're saving RSP, use sizeof */
4931 /* gpointer because spvar is of type IntPtr */
4932 /* see: mono_create_spvar_for_region */
4933 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4934 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
4936 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
4937 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
4939 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
4943 case OP_ENDFINALLY: {
4944 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4945 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
4949 case OP_ENDFILTER: {
4950 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4951 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
4952 /* The local allocator will put the result into RAX */
4957 if (ins->dreg != AMD64_RAX)
4958 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, sizeof (gpointer));
4961 ins->inst_c0 = code - cfg->native_code;
4964 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
4965 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
4967 if (ins->inst_target_bb->native_offset) {
4968 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
4970 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4971 if ((cfg->opt & MONO_OPT_BRANCH) &&
4972 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
4973 x86_jump8 (code, 0);
4975 x86_jump32 (code, 0);
4979 amd64_jump_reg (code, ins->sreg1);
5002 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5003 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5005 case OP_COND_EXC_EQ:
5006 case OP_COND_EXC_NE_UN:
5007 case OP_COND_EXC_LT:
5008 case OP_COND_EXC_LT_UN:
5009 case OP_COND_EXC_GT:
5010 case OP_COND_EXC_GT_UN:
5011 case OP_COND_EXC_GE:
5012 case OP_COND_EXC_GE_UN:
5013 case OP_COND_EXC_LE:
5014 case OP_COND_EXC_LE_UN:
5015 case OP_COND_EXC_IEQ:
5016 case OP_COND_EXC_INE_UN:
5017 case OP_COND_EXC_ILT:
5018 case OP_COND_EXC_ILT_UN:
5019 case OP_COND_EXC_IGT:
5020 case OP_COND_EXC_IGT_UN:
5021 case OP_COND_EXC_IGE:
5022 case OP_COND_EXC_IGE_UN:
5023 case OP_COND_EXC_ILE:
5024 case OP_COND_EXC_ILE_UN:
5025 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
5027 case OP_COND_EXC_OV:
5028 case OP_COND_EXC_NO:
5030 case OP_COND_EXC_NC:
5031 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5032 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
5034 case OP_COND_EXC_IOV:
5035 case OP_COND_EXC_INO:
5036 case OP_COND_EXC_IC:
5037 case OP_COND_EXC_INC:
5038 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5039 (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
5042 /* floating point opcodes */
5044 double d = *(double *)ins->inst_p0;
5046 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5047 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5050 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5051 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5056 float f = *(float *)ins->inst_p0;
5058 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5060 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5062 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5065 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5066 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5068 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5072 case OP_STORER8_MEMBASE_REG:
5073 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5075 case OP_LOADR8_MEMBASE:
5076 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5078 case OP_STORER4_MEMBASE_REG:
5080 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5082 /* This requires a double->single conversion */
5083 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5084 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5087 case OP_LOADR4_MEMBASE:
5089 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5091 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5092 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5095 case OP_ICONV_TO_R4:
5097 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5099 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5100 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5103 case OP_ICONV_TO_R8:
5104 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5106 case OP_LCONV_TO_R4:
5108 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5110 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5111 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5114 case OP_LCONV_TO_R8:
5115 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5117 case OP_FCONV_TO_R4:
5119 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5121 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5122 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5125 case OP_FCONV_TO_I1:
5126 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5128 case OP_FCONV_TO_U1:
5129 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5131 case OP_FCONV_TO_I2:
5132 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5134 case OP_FCONV_TO_U2:
5135 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5137 case OP_FCONV_TO_U4:
5138 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5140 case OP_FCONV_TO_I4:
5142 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5144 case OP_FCONV_TO_I8:
5145 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5148 case OP_RCONV_TO_I1:
5149 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5150 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5152 case OP_RCONV_TO_U1:
5153 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5154 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5156 case OP_RCONV_TO_I2:
5157 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5158 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5160 case OP_RCONV_TO_U2:
5161 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5162 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5164 case OP_RCONV_TO_I4:
5165 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5167 case OP_RCONV_TO_U4:
5168 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5170 case OP_RCONV_TO_I8:
5171 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5173 case OP_RCONV_TO_R8:
5174 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5176 case OP_RCONV_TO_R4:
5177 if (ins->dreg != ins->sreg1)
5178 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5181 case OP_LCONV_TO_R_UN: {
5184 /* Based on gcc code */
5185 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5186 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5189 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5190 br [1] = code; x86_jump8 (code, 0);
5191 amd64_patch (br [0], code);
5194 /* Save to the red zone */
5195 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5196 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5197 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5198 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5199 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5200 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5201 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5202 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5203 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5205 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5206 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5207 amd64_patch (br [1], code);
5210 case OP_LCONV_TO_OVF_U4:
5211 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5212 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5213 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5215 case OP_LCONV_TO_OVF_I4_UN:
5216 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5217 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5218 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5221 if (ins->dreg != ins->sreg1)
5222 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5225 if (ins->dreg != ins->sreg1)
5226 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5228 case OP_MOVE_F_TO_I4:
5230 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5232 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5233 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5236 case OP_MOVE_I4_TO_F:
5237 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5239 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5241 case OP_MOVE_F_TO_I8:
5242 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5244 case OP_MOVE_I8_TO_F:
5245 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5248 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5251 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5254 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5257 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5260 static double r8_0 = -0.0;
5262 g_assert (ins->sreg1 == ins->dreg);
5264 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5265 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5269 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5272 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5275 static guint64 d = 0x7fffffffffffffffUL;
5277 g_assert (ins->sreg1 == ins->dreg);
5279 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5280 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5284 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5288 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5291 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5294 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5297 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5300 static float r4_0 = -0.0;
5302 g_assert (ins->sreg1 == ins->dreg);
5304 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5305 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5306 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
5311 g_assert (cfg->opt & MONO_OPT_CMOV);
5312 g_assert (ins->dreg == ins->sreg1);
5313 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5314 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
5317 g_assert (cfg->opt & MONO_OPT_CMOV);
5318 g_assert (ins->dreg == ins->sreg1);
5319 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5320 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
5323 g_assert (cfg->opt & MONO_OPT_CMOV);
5324 g_assert (ins->dreg == ins->sreg1);
5325 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5326 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
5329 g_assert (cfg->opt & MONO_OPT_CMOV);
5330 g_assert (ins->dreg == ins->sreg1);
5331 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5332 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
5335 g_assert (cfg->opt & MONO_OPT_CMOV);
5336 g_assert (ins->dreg == ins->sreg1);
5337 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5338 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
5341 g_assert (cfg->opt & MONO_OPT_CMOV);
5342 g_assert (ins->dreg == ins->sreg1);
5343 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5344 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
5347 g_assert (cfg->opt & MONO_OPT_CMOV);
5348 g_assert (ins->dreg == ins->sreg1);
5349 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5350 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
5353 g_assert (cfg->opt & MONO_OPT_CMOV);
5354 g_assert (ins->dreg == ins->sreg1);
5355 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5356 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5362 * The two arguments are swapped because the fbranch instructions
5363 * depend on this for the non-sse case to work.
5365 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5369 * FIXME: Get rid of this.
5370 * The two arguments are swapped because the fbranch instructions
5371 * depend on this for the non-sse case to work.
5373 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5377 /* zeroing the register at the start results in
5378 * shorter and faster code (we can also remove the widening op)
5380 guchar *unordered_check;
5382 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5383 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5384 unordered_check = code;
5385 x86_branch8 (code, X86_CC_P, 0, FALSE);
5387 if (ins->opcode == OP_FCEQ) {
5388 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5389 amd64_patch (unordered_check, code);
5391 guchar *jump_to_end;
5392 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5394 x86_jump8 (code, 0);
5395 amd64_patch (unordered_check, code);
5396 amd64_inc_reg (code, ins->dreg);
5397 amd64_patch (jump_to_end, code);
5403 /* zeroing the register at the start results in
5404 * shorter and faster code (we can also remove the widening op)
5406 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5407 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5408 if (ins->opcode == OP_FCLT_UN) {
5409 guchar *unordered_check = code;
5410 guchar *jump_to_end;
5411 x86_branch8 (code, X86_CC_P, 0, FALSE);
5412 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5414 x86_jump8 (code, 0);
5415 amd64_patch (unordered_check, code);
5416 amd64_inc_reg (code, ins->dreg);
5417 amd64_patch (jump_to_end, code);
5419 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5424 guchar *unordered_check;
5425 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5426 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5427 unordered_check = code;
5428 x86_branch8 (code, X86_CC_P, 0, FALSE);
5429 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5430 amd64_patch (unordered_check, code);
5435 /* zeroing the register at the start results in
5436 * shorter and faster code (we can also remove the widening op)
5438 guchar *unordered_check;
5440 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5441 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5442 if (ins->opcode == OP_FCGT) {
5443 unordered_check = code;
5444 x86_branch8 (code, X86_CC_P, 0, FALSE);
5445 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5446 amd64_patch (unordered_check, code);
5448 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5453 guchar *unordered_check;
5454 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5455 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5456 unordered_check = code;
5457 x86_branch8 (code, X86_CC_P, 0, FALSE);
5458 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5459 amd64_patch (unordered_check, code);
5469 gboolean unordered = FALSE;
5471 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5472 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5474 switch (ins->opcode) {
5476 x86_cond = X86_CC_EQ;
5479 x86_cond = X86_CC_LT;
5482 x86_cond = X86_CC_GT;
5485 x86_cond = X86_CC_GT;
5489 x86_cond = X86_CC_LT;
5493 g_assert_not_reached ();
5498 guchar *unordered_check;
5499 guchar *jump_to_end;
5501 unordered_check = code;
5502 x86_branch8 (code, X86_CC_P, 0, FALSE);
5503 amd64_set_reg (code, x86_cond, 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_cond, ins->dreg, FALSE);
5514 case OP_FCLT_MEMBASE:
5515 case OP_FCGT_MEMBASE:
5516 case OP_FCLT_UN_MEMBASE:
5517 case OP_FCGT_UN_MEMBASE:
5518 case OP_FCEQ_MEMBASE: {
5519 guchar *unordered_check, *jump_to_end;
5522 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5523 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5525 switch (ins->opcode) {
5526 case OP_FCEQ_MEMBASE:
5527 x86_cond = X86_CC_EQ;
5529 case OP_FCLT_MEMBASE:
5530 case OP_FCLT_UN_MEMBASE:
5531 x86_cond = X86_CC_LT;
5533 case OP_FCGT_MEMBASE:
5534 case OP_FCGT_UN_MEMBASE:
5535 x86_cond = X86_CC_GT;
5538 g_assert_not_reached ();
5541 unordered_check = code;
5542 x86_branch8 (code, X86_CC_P, 0, FALSE);
5543 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5545 switch (ins->opcode) {
5546 case OP_FCEQ_MEMBASE:
5547 case OP_FCLT_MEMBASE:
5548 case OP_FCGT_MEMBASE:
5549 amd64_patch (unordered_check, code);
5551 case OP_FCLT_UN_MEMBASE:
5552 case OP_FCGT_UN_MEMBASE:
5554 x86_jump8 (code, 0);
5555 amd64_patch (unordered_check, code);
5556 amd64_inc_reg (code, ins->dreg);
5557 amd64_patch (jump_to_end, code);
5565 guchar *jump = code;
5566 x86_branch8 (code, X86_CC_P, 0, TRUE);
5567 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5568 amd64_patch (jump, code);
5572 /* Branch if C013 != 100 */
5573 /* branch if !ZF or (PF|CF) */
5574 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5575 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5576 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5579 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5582 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5583 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5587 if (ins->opcode == OP_FBGT) {
5590 /* skip branch if C1=1 */
5592 x86_branch8 (code, X86_CC_P, 0, FALSE);
5593 /* branch if (C0 | C3) = 1 */
5594 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5595 amd64_patch (br1, code);
5598 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5602 /* Branch if C013 == 100 or 001 */
5605 /* skip branch if C1=1 */
5607 x86_branch8 (code, X86_CC_P, 0, FALSE);
5608 /* branch if (C0 | C3) = 1 */
5609 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5610 amd64_patch (br1, code);
5614 /* Branch if C013 == 000 */
5615 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5618 /* Branch if C013=000 or 100 */
5621 /* skip branch if C1=1 */
5623 x86_branch8 (code, X86_CC_P, 0, FALSE);
5624 /* branch if C0=0 */
5625 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5626 amd64_patch (br1, code);
5630 /* Branch if C013 != 001 */
5631 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5632 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5635 /* Transfer value to the fp stack */
5636 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5637 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5638 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5640 amd64_push_reg (code, AMD64_RAX);
5642 amd64_fnstsw (code);
5643 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5644 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5645 amd64_pop_reg (code, AMD64_RAX);
5646 amd64_fstp (code, 0);
5647 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
5648 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5651 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5654 case OP_TLS_GET_REG:
5655 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5658 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5661 case OP_TLS_SET_REG: {
5662 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5665 case OP_MEMORY_BARRIER: {
5666 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5670 case OP_ATOMIC_ADD_I4:
5671 case OP_ATOMIC_ADD_I8: {
5672 int dreg = ins->dreg;
5673 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5675 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5678 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5679 amd64_prefix (code, X86_LOCK_PREFIX);
5680 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5681 /* dreg contains the old value, add with sreg2 value */
5682 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5684 if (ins->dreg != dreg)
5685 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5689 case OP_ATOMIC_EXCHANGE_I4:
5690 case OP_ATOMIC_EXCHANGE_I8: {
5691 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5693 /* LOCK prefix is implied. */
5694 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5695 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5696 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5699 case OP_ATOMIC_CAS_I4:
5700 case OP_ATOMIC_CAS_I8: {
5703 if (ins->opcode == OP_ATOMIC_CAS_I8)
5709 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5710 * an explanation of how this works.
5712 g_assert (ins->sreg3 == AMD64_RAX);
5713 g_assert (ins->sreg1 != AMD64_RAX);
5714 g_assert (ins->sreg1 != ins->sreg2);
5716 amd64_prefix (code, X86_LOCK_PREFIX);
5717 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
5719 if (ins->dreg != AMD64_RAX)
5720 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
5723 case OP_ATOMIC_LOAD_I1: {
5724 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
5727 case OP_ATOMIC_LOAD_U1: {
5728 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
5731 case OP_ATOMIC_LOAD_I2: {
5732 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
5735 case OP_ATOMIC_LOAD_U2: {
5736 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
5739 case OP_ATOMIC_LOAD_I4: {
5740 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5743 case OP_ATOMIC_LOAD_U4:
5744 case OP_ATOMIC_LOAD_I8:
5745 case OP_ATOMIC_LOAD_U8: {
5746 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
5749 case OP_ATOMIC_LOAD_R4: {
5750 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5751 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5754 case OP_ATOMIC_LOAD_R8: {
5755 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5758 case OP_ATOMIC_STORE_I1:
5759 case OP_ATOMIC_STORE_U1:
5760 case OP_ATOMIC_STORE_I2:
5761 case OP_ATOMIC_STORE_U2:
5762 case OP_ATOMIC_STORE_I4:
5763 case OP_ATOMIC_STORE_U4:
5764 case OP_ATOMIC_STORE_I8:
5765 case OP_ATOMIC_STORE_U8: {
5768 switch (ins->opcode) {
5769 case OP_ATOMIC_STORE_I1:
5770 case OP_ATOMIC_STORE_U1:
5773 case OP_ATOMIC_STORE_I2:
5774 case OP_ATOMIC_STORE_U2:
5777 case OP_ATOMIC_STORE_I4:
5778 case OP_ATOMIC_STORE_U4:
5781 case OP_ATOMIC_STORE_I8:
5782 case OP_ATOMIC_STORE_U8:
5787 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
5789 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5793 case OP_ATOMIC_STORE_R4: {
5794 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5795 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5797 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5801 case OP_ATOMIC_STORE_R8: {
5804 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5808 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5812 case OP_CARD_TABLE_WBARRIER: {
5813 int ptr = ins->sreg1;
5814 int value = ins->sreg2;
5816 int nursery_shift, card_table_shift;
5817 gpointer card_table_mask;
5818 size_t nursery_size;
5820 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
5821 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
5822 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
5824 /*If either point to the stack we can simply avoid the WB. This happens due to
5825 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
5827 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
5831 * We need one register we can clobber, we choose EDX and make sreg1
5832 * fixed EAX to work around limitations in the local register allocator.
5833 * sreg2 might get allocated to EDX, but that is not a problem since
5834 * we use it before clobbering EDX.
5836 g_assert (ins->sreg1 == AMD64_RAX);
5839 * This is the code we produce:
5842 * edx >>= nursery_shift
5843 * cmp edx, (nursery_start >> nursery_shift)
5846 * edx >>= card_table_shift
5852 if (mono_gc_card_table_nursery_check ()) {
5853 if (value != AMD64_RDX)
5854 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
5855 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
5856 if (shifted_nursery_start >> 31) {
5858 * The value we need to compare against is 64 bits, so we need
5859 * another spare register. We use RBX, which we save and
5862 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
5863 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
5864 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
5865 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
5867 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
5869 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
5871 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
5872 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
5873 if (card_table_mask)
5874 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
5876 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
5877 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
5879 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
5881 if (mono_gc_card_table_nursery_check ())
5882 x86_patch (br, code);
5885 #ifdef MONO_ARCH_SIMD_INTRINSICS
5886 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
5888 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
5891 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
5894 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
5897 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
5900 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
5903 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
5906 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
5907 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5910 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
5913 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
5916 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
5919 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
5922 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
5925 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
5928 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
5931 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
5934 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
5937 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
5940 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
5943 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
5946 case OP_PSHUFLEW_HIGH:
5947 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5948 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5950 case OP_PSHUFLEW_LOW:
5951 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5952 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5955 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5956 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5959 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5960 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5963 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
5964 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5968 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
5971 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
5974 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
5977 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
5980 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
5983 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
5986 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
5987 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5990 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
5993 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
5996 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
5999 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6002 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6005 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6008 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6011 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6014 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6017 case OP_EXTRACT_MASK:
6018 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6022 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6025 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6028 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6032 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6035 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6038 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6041 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6045 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6048 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6051 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6054 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6058 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6061 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6064 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6068 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6071 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6074 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6078 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6081 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6085 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6088 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6091 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6095 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6098 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6101 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6105 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6108 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6111 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6114 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6118 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6121 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6124 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6127 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6130 case OP_PSUM_ABS_DIFF:
6131 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6134 case OP_UNPACK_LOWB:
6135 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6137 case OP_UNPACK_LOWW:
6138 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6140 case OP_UNPACK_LOWD:
6141 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6143 case OP_UNPACK_LOWQ:
6144 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6146 case OP_UNPACK_LOWPS:
6147 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6149 case OP_UNPACK_LOWPD:
6150 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6153 case OP_UNPACK_HIGHB:
6154 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6156 case OP_UNPACK_HIGHW:
6157 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6159 case OP_UNPACK_HIGHD:
6160 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6162 case OP_UNPACK_HIGHQ:
6163 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6165 case OP_UNPACK_HIGHPS:
6166 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6168 case OP_UNPACK_HIGHPD:
6169 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6173 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6176 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6179 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6182 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6185 case OP_PADDB_SAT_UN:
6186 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6188 case OP_PSUBB_SAT_UN:
6189 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6191 case OP_PADDW_SAT_UN:
6192 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6194 case OP_PSUBW_SAT_UN:
6195 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6199 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6202 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6205 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6208 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6212 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6215 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6218 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6220 case OP_PMULW_HIGH_UN:
6221 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6224 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6228 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6231 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6235 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6238 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6242 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6245 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6249 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6252 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6256 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6259 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6263 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6266 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6270 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6273 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6276 /*TODO: This is appart of the sse spec but not added
6278 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6281 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6286 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6289 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6292 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6295 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6298 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6301 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6304 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6307 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6310 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6313 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6317 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6320 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6324 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6325 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6327 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6332 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6334 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6335 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6339 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6341 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6342 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6343 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6347 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6349 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6352 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6354 case OP_EXTRACTX_U2:
6355 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6357 case OP_INSERTX_U1_SLOW:
6358 /*sreg1 is the extracted ireg (scratch)
6359 /sreg2 is the to be inserted ireg (scratch)
6360 /dreg is the xreg to receive the value*/
6362 /*clear the bits from the extracted word*/
6363 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6364 /*shift the value to insert if needed*/
6365 if (ins->inst_c0 & 1)
6366 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6367 /*join them together*/
6368 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6369 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6371 case OP_INSERTX_I4_SLOW:
6372 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6373 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6374 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6376 case OP_INSERTX_I8_SLOW:
6377 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6379 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6381 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6384 case OP_INSERTX_R4_SLOW:
6385 switch (ins->inst_c0) {
6388 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6390 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6393 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6395 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6397 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6398 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6401 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6403 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6405 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6406 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6409 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6411 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6413 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6414 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6418 case OP_INSERTX_R8_SLOW:
6420 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6422 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6424 case OP_STOREX_MEMBASE_REG:
6425 case OP_STOREX_MEMBASE:
6426 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6428 case OP_LOADX_MEMBASE:
6429 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6431 case OP_LOADX_ALIGNED_MEMBASE:
6432 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6434 case OP_STOREX_ALIGNED_MEMBASE_REG:
6435 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6437 case OP_STOREX_NTA_MEMBASE_REG:
6438 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6440 case OP_PREFETCH_MEMBASE:
6441 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6445 /*FIXME the peephole pass should have killed this*/
6446 if (ins->dreg != ins->sreg1)
6447 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6450 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6452 case OP_ICONV_TO_R4_RAW:
6453 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6456 case OP_FCONV_TO_R8_X:
6457 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6460 case OP_XCONV_R8_TO_I4:
6461 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6462 switch (ins->backend.source_opcode) {
6463 case OP_FCONV_TO_I1:
6464 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6466 case OP_FCONV_TO_U1:
6467 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6469 case OP_FCONV_TO_I2:
6470 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6472 case OP_FCONV_TO_U2:
6473 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6479 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6480 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6481 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6484 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6485 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6488 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6489 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6493 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6495 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6496 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6498 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6501 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6502 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6505 case OP_LIVERANGE_START: {
6506 if (cfg->verbose_level > 1)
6507 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6508 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6511 case OP_LIVERANGE_END: {
6512 if (cfg->verbose_level > 1)
6513 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6514 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6517 case OP_GC_SAFE_POINT: {
6518 const char *polling_func = NULL;
6519 int compare_val = 0;
6522 #if defined (USE_COOP_GC)
6523 polling_func = "mono_threads_state_poll";
6525 #elif defined(__native_client_codegen__) && defined(__native_client_gc__)
6526 polling_func = "mono_nacl_gc";
6527 compare_val = 0xFFFFFFFF;
6532 amd64_test_membase_imm_size (code, ins->sreg1, 0, compare_val, 4);
6533 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6534 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, polling_func, FALSE);
6535 amd64_patch (br[0], code);
6539 case OP_GC_LIVENESS_DEF:
6540 case OP_GC_LIVENESS_USE:
6541 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6542 ins->backend.pc_offset = code - cfg->native_code;
6544 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6545 ins->backend.pc_offset = code - cfg->native_code;
6546 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6549 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6550 g_assert_not_reached ();
6553 if ((code - cfg->native_code - offset) > max_len) {
6554 #if !defined(__native_client_codegen__)
6555 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6556 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6557 g_assert_not_reached ();
6562 cfg->code_len = code - cfg->native_code;
6565 #endif /* DISABLE_JIT */
6568 mono_arch_register_lowlevel_calls (void)
6570 /* The signature doesn't matter */
6571 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6575 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
6577 unsigned char *ip = ji->ip.i + code;
6580 * Debug code to help track down problems where the target of a near call is
6583 if (amd64_is_near_call (ip)) {
6584 gint64 disp = (guint8*)target - (guint8*)ip;
6586 if (!amd64_is_imm32 (disp)) {
6587 printf ("TYPE: %d\n", ji->type);
6589 case MONO_PATCH_INFO_INTERNAL_METHOD:
6590 printf ("V: %s\n", ji->data.name);
6592 case MONO_PATCH_INFO_METHOD_JUMP:
6593 case MONO_PATCH_INFO_METHOD:
6594 printf ("V: %s\n", ji->data.method->name);
6602 amd64_patch (ip, (gpointer)target);
6608 get_max_epilog_size (MonoCompile *cfg)
6610 int max_epilog_size = 16;
6612 if (cfg->method->save_lmf)
6613 max_epilog_size += 256;
6615 if (mono_jit_trace_calls != NULL)
6616 max_epilog_size += 50;
6618 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6619 max_epilog_size += 50;
6621 max_epilog_size += (AMD64_NREG * 2);
6623 return max_epilog_size;
6627 * This macro is used for testing whenever the unwinder works correctly at every point
6628 * where an async exception can happen.
6630 /* This will generate a SIGSEGV at the given point in the code */
6631 #define async_exc_point(code) do { \
6632 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6633 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6634 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6635 cfg->arch.async_point_count ++; \
6640 mono_arch_emit_prolog (MonoCompile *cfg)
6642 MonoMethod *method = cfg->method;
6644 MonoMethodSignature *sig;
6646 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6649 MonoInst *lmf_var = cfg->lmf_var;
6650 gboolean args_clobbered = FALSE;
6651 gboolean trace = FALSE;
6652 #ifdef __native_client_codegen__
6653 guint alignment_check;
6656 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6658 #if defined(__default_codegen__)
6659 code = cfg->native_code = g_malloc (cfg->code_size);
6660 #elif defined(__native_client_codegen__)
6661 /* native_code_alloc is not 32-byte aligned, native_code is. */
6662 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6664 /* Align native_code to next nearest kNaclAlignment byte. */
6665 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6666 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6668 code = cfg->native_code;
6670 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6671 g_assert (alignment_check == 0);
6674 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6677 /* Amount of stack space allocated by register saving code */
6680 /* Offset between RSP and the CFA */
6684 * The prolog consists of the following parts:
6686 * - push rbp, mov rbp, rsp
6687 * - save callee saved regs using pushes
6689 * - save rgctx if needed
6690 * - save lmf if needed
6693 * - save rgctx if needed
6694 * - save lmf if needed
6695 * - save callee saved regs using moves
6700 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6701 // IP saved at CFA - 8
6702 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6703 async_exc_point (code);
6704 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6706 if (!cfg->arch.omit_fp) {
6707 amd64_push_reg (code, AMD64_RBP);
6709 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6710 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6711 async_exc_point (code);
6713 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6715 /* These are handled automatically by the stack marking code */
6716 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6718 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
6719 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
6720 async_exc_point (code);
6722 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6726 /* The param area is always at offset 0 from sp */
6727 /* This needs to be allocated here, since it has to come after the spill area */
6728 if (cfg->param_area) {
6729 if (cfg->arch.omit_fp)
6731 g_assert_not_reached ();
6732 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
6735 if (cfg->arch.omit_fp) {
6737 * On enter, the stack is misaligned by the pushing of the return
6738 * address. It is either made aligned by the pushing of %rbp, or by
6741 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
6742 if ((alloc_size % 16) == 0) {
6744 /* Mark the padding slot as NOREF */
6745 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
6748 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
6749 if (cfg->stack_offset != alloc_size) {
6750 /* Mark the padding slot as NOREF */
6751 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
6753 cfg->arch.sp_fp_offset = alloc_size;
6757 cfg->arch.stack_alloc_size = alloc_size;
6759 /* Allocate stack frame */
6761 /* See mono_emit_stack_alloc */
6762 #if defined(TARGET_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
6763 guint32 remaining_size = alloc_size;
6764 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
6765 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
6766 guint32 offset = code - cfg->native_code;
6767 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
6768 while (required_code_size >= (cfg->code_size - offset))
6769 cfg->code_size *= 2;
6770 cfg->native_code = mono_realloc_native_code (cfg);
6771 code = cfg->native_code + offset;
6772 cfg->stat_code_reallocs++;
6775 while (remaining_size >= 0x1000) {
6776 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
6777 if (cfg->arch.omit_fp) {
6778 cfa_offset += 0x1000;
6779 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6781 async_exc_point (code);
6783 if (cfg->arch.omit_fp)
6784 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
6787 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
6788 remaining_size -= 0x1000;
6790 if (remaining_size) {
6791 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
6792 if (cfg->arch.omit_fp) {
6793 cfa_offset += remaining_size;
6794 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6795 async_exc_point (code);
6798 if (cfg->arch.omit_fp)
6799 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
6803 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
6804 if (cfg->arch.omit_fp) {
6805 cfa_offset += alloc_size;
6806 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6807 async_exc_point (code);
6812 /* Stack alignment check */
6815 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
6816 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
6817 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
6818 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
6819 amd64_breakpoint (code);
6823 if (mini_get_debug_options ()->init_stacks) {
6824 /* Fill the stack frame with a dummy value to force deterministic behavior */
6826 /* Save registers to the red zone */
6827 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
6828 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
6830 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
6831 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
6832 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
6835 #if defined(__default_codegen__)
6836 amd64_prefix (code, X86_REP_PREFIX);
6838 #elif defined(__native_client_codegen__)
6839 /* NaCl stos pseudo-instruction */
6840 amd64_codegen_pre (code);
6841 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
6842 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
6843 /* Add %r15 to %rdi using lea, condition flags unaffected. */
6844 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
6845 amd64_prefix (code, X86_REP_PREFIX);
6847 amd64_codegen_post (code);
6848 #endif /* __native_client_codegen__ */
6850 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
6851 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
6855 if (method->save_lmf)
6856 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
6858 /* Save callee saved registers */
6859 if (cfg->arch.omit_fp) {
6860 save_area_offset = cfg->arch.reg_save_area_offset;
6861 /* Save caller saved registers after sp is adjusted */
6862 /* The registers are saved at the bottom of the frame */
6863 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
6865 /* The registers are saved just below the saved rbp */
6866 save_area_offset = cfg->arch.reg_save_area_offset;
6869 for (i = 0; i < AMD64_NREG; ++i) {
6870 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
6871 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
6873 if (cfg->arch.omit_fp) {
6874 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
6875 /* These are handled automatically by the stack marking code */
6876 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
6878 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
6882 save_area_offset += 8;
6883 async_exc_point (code);
6887 /* store runtime generic context */
6888 if (cfg->rgctx_var) {
6889 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
6890 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
6892 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
6894 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
6895 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
6898 /* compute max_length in order to use short forward jumps */
6899 max_epilog_size = get_max_epilog_size (cfg);
6900 if (cfg->opt & MONO_OPT_BRANCH) {
6901 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6905 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6907 /* max alignment for loops */
6908 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
6909 max_length += LOOP_ALIGNMENT;
6910 #ifdef __native_client_codegen__
6911 /* max alignment for native client */
6912 max_length += kNaClAlignment;
6915 MONO_BB_FOR_EACH_INS (bb, ins) {
6916 #ifdef __native_client_codegen__
6918 int space_in_block = kNaClAlignment -
6919 ((max_length + cfg->code_len) & kNaClAlignmentMask);
6920 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
6921 if (space_in_block < max_len && max_len < kNaClAlignment) {
6922 max_length += space_in_block;
6925 #endif /*__native_client_codegen__*/
6926 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
6929 /* Take prolog and epilog instrumentation into account */
6930 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
6931 max_length += max_epilog_size;
6933 bb->max_length = max_length;
6937 sig = mono_method_signature (method);
6940 cinfo = cfg->arch.cinfo;
6942 if (sig->ret->type != MONO_TYPE_VOID) {
6943 /* Save volatile arguments to the stack */
6944 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
6945 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
6948 /* Keep this in sync with emit_load_volatile_arguments */
6949 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6950 ArgInfo *ainfo = cinfo->args + i;
6952 ins = cfg->args [i];
6954 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
6955 /* Unused arguments */
6958 /* Save volatile arguments to the stack */
6959 if (ins->opcode != OP_REGVAR) {
6960 switch (ainfo->storage) {
6966 if (stack_offset & 0x1)
6968 else if (stack_offset & 0x2)
6970 else if (stack_offset & 0x4)
6975 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
6978 * Save the original location of 'this',
6979 * get_generic_info_from_stack_frame () needs this to properly look up
6980 * the argument value during the handling of async exceptions.
6982 if (ins == cfg->args [0]) {
6983 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
6984 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
6988 case ArgInFloatSSEReg:
6989 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
6991 case ArgInDoubleSSEReg:
6992 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
6994 case ArgValuetypeInReg:
6995 for (quad = 0; quad < 2; quad ++) {
6996 switch (ainfo->pair_storage [quad]) {
6998 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7000 case ArgInFloatSSEReg:
7001 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7003 case ArgInDoubleSSEReg:
7004 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7009 g_assert_not_reached ();
7013 case ArgValuetypeAddrInIReg:
7014 if (ainfo->pair_storage [0] == ArgInIReg)
7015 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7021 /* Argument allocated to (non-volatile) register */
7022 switch (ainfo->storage) {
7024 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7027 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7030 g_assert_not_reached ();
7033 if (ins == cfg->args [0]) {
7034 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7035 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7040 if (cfg->method->save_lmf)
7041 args_clobbered = TRUE;
7044 args_clobbered = TRUE;
7045 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7048 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7049 args_clobbered = TRUE;
7052 * Optimize the common case of the first bblock making a call with the same
7053 * arguments as the method. This works because the arguments are still in their
7054 * original argument registers.
7055 * FIXME: Generalize this
7057 if (!args_clobbered) {
7058 MonoBasicBlock *first_bb = cfg->bb_entry;
7060 int filter = FILTER_IL_SEQ_POINT;
7062 next = mono_bb_first_inst (first_bb, filter);
7063 if (!next && first_bb->next_bb) {
7064 first_bb = first_bb->next_bb;
7065 next = mono_bb_first_inst (first_bb, filter);
7068 if (first_bb->in_count > 1)
7071 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7072 ArgInfo *ainfo = cinfo->args + i;
7073 gboolean match = FALSE;
7075 ins = cfg->args [i];
7076 if (ins->opcode != OP_REGVAR) {
7077 switch (ainfo->storage) {
7079 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7080 if (next->dreg == ainfo->reg) {
7084 next->opcode = OP_MOVE;
7085 next->sreg1 = ainfo->reg;
7086 /* Only continue if the instruction doesn't change argument regs */
7087 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7097 /* Argument allocated to (non-volatile) register */
7098 switch (ainfo->storage) {
7100 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7111 next = mono_inst_next (next, filter);
7112 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7119 if (cfg->gen_sdb_seq_points) {
7120 MonoInst *info_var = cfg->arch.seq_point_info_var;
7122 /* Initialize seq_point_info_var */
7123 if (cfg->compile_aot) {
7124 /* Initialize the variable from a GOT slot */
7125 /* Same as OP_AOTCONST */
7126 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7127 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7128 g_assert (info_var->opcode == OP_REGOFFSET);
7129 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7132 if (cfg->compile_aot) {
7133 /* Initialize ss_tramp_var */
7134 ins = cfg->arch.ss_tramp_var;
7135 g_assert (ins->opcode == OP_REGOFFSET);
7137 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7138 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr), 8);
7139 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7141 /* Initialize ss_trigger_page_var */
7142 ins = cfg->arch.ss_trigger_page_var;
7144 g_assert (ins->opcode == OP_REGOFFSET);
7146 amd64_mov_reg_imm (code, AMD64_R11, (guint64)ss_trigger_page);
7147 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7151 cfg->code_len = code - cfg->native_code;
7153 g_assert (cfg->code_len < cfg->code_size);
7159 mono_arch_emit_epilog (MonoCompile *cfg)
7161 MonoMethod *method = cfg->method;
7164 int max_epilog_size;
7166 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7167 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7169 max_epilog_size = get_max_epilog_size (cfg);
7171 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7172 cfg->code_size *= 2;
7173 cfg->native_code = mono_realloc_native_code (cfg);
7174 cfg->stat_code_reallocs++;
7176 code = cfg->native_code + cfg->code_len;
7178 cfg->has_unwind_info_for_epilog = TRUE;
7180 /* Mark the start of the epilog */
7181 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7183 /* Save the uwind state which is needed by the out-of-line code */
7184 mono_emit_unwind_op_remember_state (cfg, code);
7186 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7187 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7189 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7191 if (method->save_lmf) {
7192 /* check if we need to restore protection of the stack after a stack overflow */
7193 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7195 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7196 /* we load the value in a separate instruction: this mechanism may be
7197 * used later as a safer way to do thread interruption
7199 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7200 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7202 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7203 /* note that the call trampoline will preserve eax/edx */
7204 x86_call_reg (code, X86_ECX);
7205 x86_patch (patch, code);
7207 /* FIXME: maybe save the jit tls in the prolog */
7209 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7210 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7214 /* Restore callee saved regs */
7215 for (i = 0; i < AMD64_NREG; ++i) {
7216 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7217 /* Restore only used_int_regs, not arch.saved_iregs */
7218 if (cfg->used_int_regs & (1 << i)) {
7219 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7220 mono_emit_unwind_op_same_value (cfg, code, i);
7221 async_exc_point (code);
7223 save_area_offset += 8;
7227 /* Load returned vtypes into registers if needed */
7228 cinfo = cfg->arch.cinfo;
7229 if (cinfo->ret.storage == ArgValuetypeInReg) {
7230 ArgInfo *ainfo = &cinfo->ret;
7231 MonoInst *inst = cfg->ret;
7233 for (quad = 0; quad < 2; quad ++) {
7234 switch (ainfo->pair_storage [quad]) {
7236 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7238 case ArgInFloatSSEReg:
7239 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7241 case ArgInDoubleSSEReg:
7242 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7247 g_assert_not_reached ();
7252 if (cfg->arch.omit_fp) {
7253 if (cfg->arch.stack_alloc_size) {
7254 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7258 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7260 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7261 async_exc_point (code);
7264 /* Restore the unwind state to be the same as before the epilog */
7265 mono_emit_unwind_op_restore_state (cfg, code);
7267 cfg->code_len = code - cfg->native_code;
7269 g_assert (cfg->code_len < cfg->code_size);
7273 mono_arch_emit_exceptions (MonoCompile *cfg)
7275 MonoJumpInfo *patch_info;
7278 MonoClass *exc_classes [16];
7279 guint8 *exc_throw_start [16], *exc_throw_end [16];
7280 guint32 code_size = 0;
7282 /* Compute needed space */
7283 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7284 if (patch_info->type == MONO_PATCH_INFO_EXC)
7286 if (patch_info->type == MONO_PATCH_INFO_R8)
7287 code_size += 8 + 15; /* sizeof (double) + alignment */
7288 if (patch_info->type == MONO_PATCH_INFO_R4)
7289 code_size += 4 + 15; /* sizeof (float) + alignment */
7290 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7291 code_size += 8 + 7; /*sizeof (void*) + alignment */
7294 #ifdef __native_client_codegen__
7295 /* Give us extra room on Native Client. This could be */
7296 /* more carefully calculated, but bundle alignment makes */
7297 /* it much trickier, so *2 like other places is good. */
7301 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7302 cfg->code_size *= 2;
7303 cfg->native_code = mono_realloc_native_code (cfg);
7304 cfg->stat_code_reallocs++;
7307 code = cfg->native_code + cfg->code_len;
7309 /* add code to raise exceptions */
7311 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7312 switch (patch_info->type) {
7313 case MONO_PATCH_INFO_EXC: {
7314 MonoClass *exc_class;
7318 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7320 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7321 g_assert (exc_class);
7322 throw_ip = patch_info->ip.i;
7324 //x86_breakpoint (code);
7325 /* Find a throw sequence for the same exception class */
7326 for (i = 0; i < nthrows; ++i)
7327 if (exc_classes [i] == exc_class)
7330 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7331 x86_jump_code (code, exc_throw_start [i]);
7332 patch_info->type = MONO_PATCH_INFO_NONE;
7336 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7340 exc_classes [nthrows] = exc_class;
7341 exc_throw_start [nthrows] = code;
7343 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7345 patch_info->type = MONO_PATCH_INFO_NONE;
7347 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7349 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7354 exc_throw_end [nthrows] = code;
7364 g_assert(code < cfg->native_code + cfg->code_size);
7367 /* Handle relocations with RIP relative addressing */
7368 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7369 gboolean remove = FALSE;
7370 guint8 *orig_code = code;
7372 switch (patch_info->type) {
7373 case MONO_PATCH_INFO_R8:
7374 case MONO_PATCH_INFO_R4: {
7375 guint8 *pos, *patch_pos;
7378 /* The SSE opcodes require a 16 byte alignment */
7379 #if defined(__default_codegen__)
7380 code = (guint8*)ALIGN_TO (code, 16);
7381 #elif defined(__native_client_codegen__)
7383 /* Pad this out with HLT instructions */
7384 /* or we can get garbage bytes emitted */
7385 /* which will fail validation */
7386 guint8 *aligned_code;
7387 /* extra align to make room for */
7388 /* mov/push below */
7389 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7390 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7391 /* The technique of hiding data in an */
7392 /* instruction has a problem here: we */
7393 /* need the data aligned to a 16-byte */
7394 /* boundary but the instruction cannot */
7395 /* cross the bundle boundary. so only */
7396 /* odd multiples of 16 can be used */
7397 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7400 while (code < aligned_code) {
7401 *(code++) = 0xf4; /* hlt */
7406 pos = cfg->native_code + patch_info->ip.i;
7407 if (IS_REX (pos [1])) {
7408 patch_pos = pos + 5;
7409 target_pos = code - pos - 9;
7412 patch_pos = pos + 4;
7413 target_pos = code - pos - 8;
7416 if (patch_info->type == MONO_PATCH_INFO_R8) {
7417 #ifdef __native_client_codegen__
7418 /* Hide 64-bit data in a */
7419 /* "mov imm64, r11" instruction. */
7420 /* write it before the start of */
7422 *(code-2) = 0x49; /* prefix */
7423 *(code-1) = 0xbb; /* mov X, %r11 */
7425 *(double*)code = *(double*)patch_info->data.target;
7426 code += sizeof (double);
7428 #ifdef __native_client_codegen__
7429 /* Hide 32-bit data in a */
7430 /* "push imm32" instruction. */
7431 *(code-1) = 0x68; /* push */
7433 *(float*)code = *(float*)patch_info->data.target;
7434 code += sizeof (float);
7437 *(guint32*)(patch_pos) = target_pos;
7442 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7445 if (cfg->compile_aot)
7448 /*loading is faster against aligned addresses.*/
7449 code = (guint8*)ALIGN_TO (code, 8);
7450 memset (orig_code, 0, code - orig_code);
7452 pos = cfg->native_code + patch_info->ip.i;
7454 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7455 if (IS_REX (pos [1]))
7456 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7458 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7460 *(gpointer*)code = (gpointer)patch_info->data.target;
7461 code += sizeof (gpointer);
7471 if (patch_info == cfg->patch_info)
7472 cfg->patch_info = patch_info->next;
7476 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7478 tmp->next = patch_info->next;
7481 g_assert (code < cfg->native_code + cfg->code_size);
7484 cfg->code_len = code - cfg->native_code;
7486 g_assert (cfg->code_len < cfg->code_size);
7490 #endif /* DISABLE_JIT */
7493 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7496 MonoMethodSignature *sig;
7498 int i, n, stack_area = 0;
7500 /* Keep this in sync with mono_arch_get_argument_info */
7502 if (enable_arguments) {
7503 /* Allocate a new area on the stack and save arguments there */
7504 sig = mono_method_signature (cfg->method);
7506 n = sig->param_count + sig->hasthis;
7508 stack_area = ALIGN_TO (n * 8, 16);
7510 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7512 for (i = 0; i < n; ++i) {
7513 inst = cfg->args [i];
7515 if (inst->opcode == OP_REGVAR)
7516 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7518 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7519 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7524 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7525 amd64_set_reg_template (code, AMD64_ARG_REG1);
7526 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7527 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7529 if (enable_arguments)
7530 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7544 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7547 int save_mode = SAVE_NONE;
7548 MonoMethod *method = cfg->method;
7549 MonoType *ret_type = mini_get_underlying_type (cfg, mono_method_signature (method)->ret);
7552 switch (ret_type->type) {
7553 case MONO_TYPE_VOID:
7554 /* special case string .ctor icall */
7555 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7556 save_mode = SAVE_EAX;
7558 save_mode = SAVE_NONE;
7562 save_mode = SAVE_EAX;
7566 save_mode = SAVE_XMM;
7568 case MONO_TYPE_GENERICINST:
7569 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7570 save_mode = SAVE_EAX;
7574 case MONO_TYPE_VALUETYPE:
7575 save_mode = SAVE_STRUCT;
7578 save_mode = SAVE_EAX;
7582 /* Save the result and copy it into the proper argument register */
7583 switch (save_mode) {
7585 amd64_push_reg (code, AMD64_RAX);
7587 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7588 if (enable_arguments)
7589 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7593 if (enable_arguments)
7594 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7597 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7598 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7600 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7602 * The result is already in the proper argument register so no copying
7609 g_assert_not_reached ();
7612 /* Set %al since this is a varargs call */
7613 if (save_mode == SAVE_XMM)
7614 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7616 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7618 if (preserve_argument_registers) {
7619 for (i = 0; i < PARAM_REGS; ++i)
7620 amd64_push_reg (code, param_regs [i]);
7623 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7624 amd64_set_reg_template (code, AMD64_ARG_REG1);
7625 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7627 if (preserve_argument_registers) {
7628 for (i = PARAM_REGS - 1; i >= 0; --i)
7629 amd64_pop_reg (code, param_regs [i]);
7632 /* Restore result */
7633 switch (save_mode) {
7635 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7636 amd64_pop_reg (code, AMD64_RAX);
7642 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7643 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7644 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7649 g_assert_not_reached ();
7656 mono_arch_flush_icache (guint8 *code, gint size)
7662 mono_arch_flush_register_windows (void)
7667 mono_arch_is_inst_imm (gint64 imm)
7669 return amd64_is_imm32 (imm);
7673 * Determine whenever the trap whose info is in SIGINFO is caused by
7677 mono_arch_is_int_overflow (void *sigctx, void *info)
7684 mono_sigctx_to_monoctx (sigctx, &ctx);
7686 rip = (guint8*)ctx.gregs [AMD64_RIP];
7688 if (IS_REX (rip [0])) {
7689 reg = amd64_rex_b (rip [0]);
7695 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7697 reg += x86_modrm_rm (rip [1]);
7699 value = ctx.gregs [reg];
7709 mono_arch_get_patch_offset (guint8 *code)
7715 * mono_breakpoint_clean_code:
7717 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
7718 * breakpoints in the original code, they are removed in the copy.
7720 * Returns TRUE if no sw breakpoint was present.
7723 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
7726 * If method_start is non-NULL we need to perform bound checks, since we access memory
7727 * at code - offset we could go before the start of the method and end up in a different
7728 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
7731 if (!method_start || code - offset >= method_start) {
7732 memcpy (buf, code - offset, size);
7734 int diff = code - method_start;
7735 memset (buf, 0, size);
7736 memcpy (buf + offset - diff, method_start, diff + size - offset);
7741 #if defined(__native_client_codegen__)
7742 /* For membase calls, we want the base register. for Native Client, */
7743 /* all indirect calls have the following sequence with the given sizes: */
7744 /* mov %eXX,%eXX [2-3] */
7745 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
7746 /* and $0xffffffffffffffe0,%r11d [4] */
7747 /* add %r15,%r11 [3] */
7748 /* callq *%r11 [3] */
7751 /* Determine if code points to a NaCl call-through-register sequence, */
7752 /* (i.e., the last 3 instructions listed above) */
7754 is_nacl_call_reg_sequence(guint8* code)
7756 const char *sequence = "\x41\x83\xe3\xe0" /* and */
7757 "\x4d\x03\xdf" /* add */
7758 "\x41\xff\xd3"; /* call */
7759 return memcmp(code, sequence, 10) == 0;
7762 /* Determine if code points to the first opcode of the mov membase component */
7763 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
7764 /* (there could be a REX prefix before the opcode but it is ignored) */
7766 is_nacl_indirect_call_membase_sequence(guint8* code)
7768 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
7769 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
7770 /* and that src reg = dest reg */
7771 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
7772 /* Check that next inst is mov, uses SIB byte (rm = 4), */
7774 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
7775 /* and has dst of r11 and base of r15 */
7776 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
7777 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
7779 #endif /* __native_client_codegen__ */
7782 mono_arch_get_this_arg_reg (guint8 *code)
7784 return AMD64_ARG_REG1;
7788 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
7790 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
7793 #define MAX_ARCH_DELEGATE_PARAMS 10
7796 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len)
7798 guint8 *code, *start;
7802 start = code = mono_global_codeman_reserve (64);
7804 /* Replace the this argument with the target */
7805 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7806 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
7807 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7809 g_assert ((code - start) < 64);
7811 start = code = mono_global_codeman_reserve (64);
7813 if (param_count == 0) {
7814 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7816 /* We have to shift the arguments left */
7817 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7818 for (i = 0; i < param_count; ++i) {
7821 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7823 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
7825 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7829 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7831 g_assert ((code - start) < 64);
7834 nacl_global_codeman_validate (&start, 64, &code);
7835 mono_arch_flush_icache (start, code - start);
7838 *code_len = code - start;
7840 if (mono_jit_map_is_enabled ()) {
7843 buff = (char*)"delegate_invoke_has_target";
7845 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
7846 mono_emit_jit_tramp (start, code - start, buff);
7850 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
7855 #define MAX_VIRTUAL_DELEGATE_OFFSET 32
7858 get_delegate_virtual_invoke_impl (gboolean load_imt_reg, int offset, guint32 *code_len)
7860 guint8 *code, *start;
7863 if (offset / sizeof (gpointer) > MAX_VIRTUAL_DELEGATE_OFFSET)
7866 start = code = mono_global_codeman_reserve (size);
7868 /* Replace the this argument with the target */
7869 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7870 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
7873 /* Load the IMT reg */
7874 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
7877 /* Load the vtable */
7878 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
7879 amd64_jump_membase (code, AMD64_RAX, offset);
7880 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
7883 *code_len = code - start;
7889 * mono_arch_get_delegate_invoke_impls:
7891 * Return a list of MonoTrampInfo structures for the delegate invoke impl
7895 mono_arch_get_delegate_invoke_impls (void)
7903 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
7904 res = g_slist_prepend (res, mono_tramp_info_create ("delegate_invoke_impl_has_target", code, code_len, NULL, NULL));
7906 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
7907 code = get_delegate_invoke_impl (FALSE, i, &code_len);
7908 tramp_name = g_strdup_printf ("delegate_invoke_impl_target_%d", i);
7909 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
7910 g_free (tramp_name);
7913 for (i = 0; i < MAX_VIRTUAL_DELEGATE_OFFSET; ++i) {
7914 code = get_delegate_virtual_invoke_impl (TRUE, i * SIZEOF_VOID_P, &code_len);
7915 tramp_name = g_strdup_printf ("delegate_virtual_invoke_imt_%d", i);
7916 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
7917 g_free (tramp_name);
7919 code = get_delegate_virtual_invoke_impl (FALSE, i * SIZEOF_VOID_P, &code_len);
7920 tramp_name = g_strdup_printf ("delegate_virtual_invoke_%d", i);
7921 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
7922 g_free (tramp_name);
7929 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
7931 guint8 *code, *start;
7934 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
7937 /* FIXME: Support more cases */
7938 if (MONO_TYPE_ISSTRUCT (mini_replace_type (sig->ret)))
7942 static guint8* cached = NULL;
7948 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
7950 start = get_delegate_invoke_impl (TRUE, 0, NULL);
7952 mono_memory_barrier ();
7956 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
7957 for (i = 0; i < sig->param_count; ++i)
7958 if (!mono_is_regsize_var (sig->params [i]))
7960 if (sig->param_count > 4)
7963 code = cache [sig->param_count];
7967 if (mono_aot_only) {
7968 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
7969 start = mono_aot_get_trampoline (name);
7972 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
7975 mono_memory_barrier ();
7977 cache [sig->param_count] = start;
7984 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
7986 return get_delegate_virtual_invoke_impl (load_imt_reg, offset, NULL);
7990 mono_arch_finish_init (void)
7992 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
7993 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
7998 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8002 #if defined(__default_codegen__)
8003 #define CMP_SIZE (6 + 1)
8004 #define CMP_REG_REG_SIZE (4 + 1)
8005 #define BR_SMALL_SIZE 2
8006 #define BR_LARGE_SIZE 6
8007 #define MOV_REG_IMM_SIZE 10
8008 #define MOV_REG_IMM_32BIT_SIZE 6
8009 #define JUMP_REG_SIZE (2 + 1)
8010 #elif defined(__native_client_codegen__)
8011 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8012 #define CMP_SIZE ((6 + 1) * 2 - 1)
8013 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8014 #define BR_SMALL_SIZE (2 * 2 - 1)
8015 #define BR_LARGE_SIZE (6 * 2 - 1)
8016 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8017 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8018 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8019 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8020 /* Jump membase's size is large and unpredictable */
8021 /* in native client, just pad it out a whole bundle. */
8022 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8026 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8028 int i, distance = 0;
8029 for (i = start; i < target; ++i)
8030 distance += imt_entries [i]->chunk_size;
8035 * LOCKING: called with the domain lock held
8038 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8039 gpointer fail_tramp)
8043 guint8 *code, *start;
8044 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8046 for (i = 0; i < count; ++i) {
8047 MonoIMTCheckItem *item = imt_entries [i];
8048 if (item->is_equals) {
8049 if (item->check_target_idx) {
8050 if (!item->compare_done) {
8051 if (amd64_is_imm32 (item->key))
8052 item->chunk_size += CMP_SIZE;
8054 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8056 if (item->has_target_code) {
8057 item->chunk_size += MOV_REG_IMM_SIZE;
8059 if (vtable_is_32bit)
8060 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8062 item->chunk_size += MOV_REG_IMM_SIZE;
8063 #ifdef __native_client_codegen__
8064 item->chunk_size += JUMP_MEMBASE_SIZE;
8067 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8070 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8071 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8073 if (vtable_is_32bit)
8074 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8076 item->chunk_size += MOV_REG_IMM_SIZE;
8077 item->chunk_size += JUMP_REG_SIZE;
8078 /* with assert below:
8079 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8081 #ifdef __native_client_codegen__
8082 item->chunk_size += JUMP_MEMBASE_SIZE;
8087 if (amd64_is_imm32 (item->key))
8088 item->chunk_size += CMP_SIZE;
8090 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8091 item->chunk_size += BR_LARGE_SIZE;
8092 imt_entries [item->check_target_idx]->compare_done = TRUE;
8094 size += item->chunk_size;
8096 #if defined(__native_client__) && defined(__native_client_codegen__)
8097 /* In Native Client, we don't re-use thunks, allocate from the */
8098 /* normal code manager paths. */
8099 code = mono_domain_code_reserve (domain, size);
8102 code = mono_method_alloc_generic_virtual_thunk (domain, size);
8104 code = mono_domain_code_reserve (domain, size);
8107 for (i = 0; i < count; ++i) {
8108 MonoIMTCheckItem *item = imt_entries [i];
8109 item->code_target = code;
8110 if (item->is_equals) {
8111 gboolean fail_case = !item->check_target_idx && fail_tramp;
8113 if (item->check_target_idx || fail_case) {
8114 if (!item->compare_done || fail_case) {
8115 if (amd64_is_imm32 (item->key))
8116 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8118 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8119 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8122 item->jmp_code = code;
8123 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8124 if (item->has_target_code) {
8125 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8126 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8128 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8129 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8133 amd64_patch (item->jmp_code, code);
8134 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8135 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8136 item->jmp_code = NULL;
8139 /* enable the commented code to assert on wrong method */
8141 if (amd64_is_imm32 (item->key))
8142 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8144 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8145 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8147 item->jmp_code = code;
8148 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8149 /* See the comment below about R10 */
8150 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8151 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8152 amd64_patch (item->jmp_code, code);
8153 amd64_breakpoint (code);
8154 item->jmp_code = NULL;
8156 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8157 needs to be preserved. R10 needs
8158 to be preserved for calls which
8159 require a runtime generic context,
8160 but interface calls don't. */
8161 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8162 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8166 if (amd64_is_imm32 (item->key))
8167 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8169 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8170 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8172 item->jmp_code = code;
8173 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8174 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8176 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8178 g_assert (code - item->code_target <= item->chunk_size);
8180 /* patch the branches to get to the target items */
8181 for (i = 0; i < count; ++i) {
8182 MonoIMTCheckItem *item = imt_entries [i];
8183 if (item->jmp_code) {
8184 if (item->check_target_idx) {
8185 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8191 mono_stats.imt_thunks_size += code - start;
8192 g_assert (code - start <= size);
8194 nacl_domain_code_validate(domain, &start, size, &code);
8195 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8201 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8203 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8207 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8209 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8213 mono_arch_get_cie_program (void)
8217 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8218 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8226 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8228 MonoInst *ins = NULL;
8231 if (cmethod->klass == mono_defaults.math_class) {
8232 if (strcmp (cmethod->name, "Sin") == 0) {
8234 } else if (strcmp (cmethod->name, "Cos") == 0) {
8236 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8238 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8242 if (opcode && fsig->param_count == 1) {
8243 MONO_INST_NEW (cfg, ins, opcode);
8244 ins->type = STACK_R8;
8245 ins->dreg = mono_alloc_freg (cfg);
8246 ins->sreg1 = args [0]->dreg;
8247 MONO_ADD_INS (cfg->cbb, ins);
8251 if (cfg->opt & MONO_OPT_CMOV) {
8252 if (strcmp (cmethod->name, "Min") == 0) {
8253 if (fsig->params [0]->type == MONO_TYPE_I4)
8255 if (fsig->params [0]->type == MONO_TYPE_U4)
8256 opcode = OP_IMIN_UN;
8257 else if (fsig->params [0]->type == MONO_TYPE_I8)
8259 else if (fsig->params [0]->type == MONO_TYPE_U8)
8260 opcode = OP_LMIN_UN;
8261 } else if (strcmp (cmethod->name, "Max") == 0) {
8262 if (fsig->params [0]->type == MONO_TYPE_I4)
8264 if (fsig->params [0]->type == MONO_TYPE_U4)
8265 opcode = OP_IMAX_UN;
8266 else if (fsig->params [0]->type == MONO_TYPE_I8)
8268 else if (fsig->params [0]->type == MONO_TYPE_U8)
8269 opcode = OP_LMAX_UN;
8273 if (opcode && fsig->param_count == 2) {
8274 MONO_INST_NEW (cfg, ins, opcode);
8275 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8276 ins->dreg = mono_alloc_ireg (cfg);
8277 ins->sreg1 = args [0]->dreg;
8278 ins->sreg2 = args [1]->dreg;
8279 MONO_ADD_INS (cfg->cbb, ins);
8283 /* OP_FREM is not IEEE compatible */
8284 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8285 MONO_INST_NEW (cfg, ins, OP_FREM);
8286 ins->inst_i0 = args [0];
8287 ins->inst_i1 = args [1];
8297 mono_arch_print_tree (MonoInst *tree, int arity)
8303 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8305 return ctx->gregs [reg];
8309 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8311 ctx->gregs [reg] = val;
8315 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8317 gpointer *sp, old_value;
8321 bp = MONO_CONTEXT_GET_BP (ctx);
8322 sp = *(gpointer*)(bp + clause->exvar_offset);
8325 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8334 * mono_arch_emit_load_aotconst:
8336 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8337 * TARGET from the mscorlib GOT in full-aot code.
8338 * On AMD64, the result is placed into R11.
8341 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
8343 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8344 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8350 * mono_arch_get_trampolines:
8352 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8356 mono_arch_get_trampolines (gboolean aot)
8358 return mono_amd64_get_exception_trampolines (aot);
8361 /* Soft Debug support */
8362 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8365 * mono_arch_set_breakpoint:
8367 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8368 * The location should contain code emitted by OP_SEQ_POINT.
8371 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8374 guint8 *orig_code = code;
8377 guint32 native_offset = ip - (guint8*)ji->code_start;
8378 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8380 g_assert (info->bp_addrs [native_offset] == 0);
8381 info->bp_addrs [native_offset] = mini_get_breakpoint_trampoline ();
8384 * In production, we will use int3 (has to fix the size in the md
8385 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
8388 g_assert (code [0] == 0x90);
8389 if (breakpoint_size == 8) {
8390 amd64_mov_reg_mem (code, AMD64_R11, (guint64)bp_trigger_page, 4);
8392 amd64_mov_reg_imm_size (code, AMD64_R11, (guint64)bp_trigger_page, 8);
8393 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 4);
8396 g_assert (code - orig_code == breakpoint_size);
8401 * mono_arch_clear_breakpoint:
8403 * Clear the breakpoint at IP.
8406 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8412 guint32 native_offset = ip - (guint8*)ji->code_start;
8413 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8415 info->bp_addrs [native_offset] = NULL;
8417 for (i = 0; i < breakpoint_size; ++i)
8423 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8426 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8427 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == bp_trigger_page)
8432 siginfo_t* sinfo = (siginfo_t*) info;
8433 /* Sometimes the address is off by 4 */
8434 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
8442 * mono_arch_skip_breakpoint:
8444 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8445 * we resume, the instruction is not executed again.
8448 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8451 /* The breakpoint instruction is a call */
8453 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + breakpoint_fault_size);
8458 * mono_arch_start_single_stepping:
8460 * Start single stepping.
8463 mono_arch_start_single_stepping (void)
8465 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
8466 ss_trampoline = mini_get_single_step_trampoline ();
8470 * mono_arch_stop_single_stepping:
8472 * Stop single stepping.
8475 mono_arch_stop_single_stepping (void)
8477 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
8478 ss_trampoline = NULL;
8482 * mono_arch_is_single_step_event:
8484 * Return whenever the machine state in SIGCTX corresponds to a single
8488 mono_arch_is_single_step_event (void *info, void *sigctx)
8491 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8492 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == ss_trigger_page)
8497 siginfo_t* sinfo = (siginfo_t*) info;
8498 /* Sometimes the address is off by 4 */
8499 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
8507 * mono_arch_skip_single_step:
8509 * Modify CTX so the ip is placed after the single step trigger instruction,
8510 * we resume, the instruction is not executed again.
8513 mono_arch_skip_single_step (MonoContext *ctx)
8515 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + single_step_fault_size);
8519 * mono_arch_create_seq_point_info:
8521 * Return a pointer to a data structure which is used by the sequence
8522 * point implementation in AOTed code.
8525 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8530 // FIXME: Add a free function
8532 mono_domain_lock (domain);
8533 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8535 mono_domain_unlock (domain);
8538 ji = mono_jit_info_table_find (domain, (char*)code);
8541 // FIXME: Optimize the size
8542 info = g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8544 info->ss_tramp_addr = &ss_trampoline;
8546 mono_domain_lock (domain);
8547 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8549 mono_domain_unlock (domain);
8556 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8558 ext->lmf.previous_lmf = prev_lmf;
8559 /* Mark that this is a MonoLMFExt */
8560 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8561 ext->lmf.rsp = (gssize)ext;
8567 mono_arch_opcode_supported (int opcode)
8570 case OP_ATOMIC_ADD_I4:
8571 case OP_ATOMIC_ADD_I8:
8572 case OP_ATOMIC_EXCHANGE_I4:
8573 case OP_ATOMIC_EXCHANGE_I8:
8574 case OP_ATOMIC_CAS_I4:
8575 case OP_ATOMIC_CAS_I8:
8576 case OP_ATOMIC_LOAD_I1:
8577 case OP_ATOMIC_LOAD_I2:
8578 case OP_ATOMIC_LOAD_I4:
8579 case OP_ATOMIC_LOAD_I8:
8580 case OP_ATOMIC_LOAD_U1:
8581 case OP_ATOMIC_LOAD_U2:
8582 case OP_ATOMIC_LOAD_U4:
8583 case OP_ATOMIC_LOAD_U8:
8584 case OP_ATOMIC_LOAD_R4:
8585 case OP_ATOMIC_LOAD_R8:
8586 case OP_ATOMIC_STORE_I1:
8587 case OP_ATOMIC_STORE_I2:
8588 case OP_ATOMIC_STORE_I4:
8589 case OP_ATOMIC_STORE_I8:
8590 case OP_ATOMIC_STORE_U1:
8591 case OP_ATOMIC_STORE_U2:
8592 case OP_ATOMIC_STORE_U4:
8593 case OP_ATOMIC_STORE_U8:
8594 case OP_ATOMIC_STORE_R4:
8595 case OP_ATOMIC_STORE_R8: