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>
38 #include "mini-amd64.h"
39 #include "cpu-amd64.h"
40 #include "debugger-agent.h"
44 static gboolean optimize_for_xen = TRUE;
46 #define optimize_for_xen 0
49 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
51 #define IS_IMM32(val) ((((guint64)val) >> 32) == 0)
53 #define IS_REX(inst) (((inst) >= 0x40) && ((inst) <= 0x4f))
56 /* Under windows, the calling convention is never stdcall */
57 #define CALLCONV_IS_STDCALL(call_conv) (FALSE)
59 #define CALLCONV_IS_STDCALL(call_conv) ((call_conv) == MONO_CALL_STDCALL)
62 /* This mutex protects architecture specific caches */
63 #define mono_mini_arch_lock() mono_mutex_lock (&mini_arch_mutex)
64 #define mono_mini_arch_unlock() mono_mutex_unlock (&mini_arch_mutex)
65 static mono_mutex_t mini_arch_mutex;
68 mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
71 * The code generated for sequence points reads from this location, which is
72 * made read-only when single stepping is enabled.
74 static gpointer ss_trigger_page;
76 /* Enabled breakpoints read from this trigger page */
77 static gpointer bp_trigger_page;
79 /* The size of the breakpoint sequence */
80 static int breakpoint_size;
82 /* The size of the breakpoint instruction causing the actual fault */
83 static int breakpoint_fault_size;
85 /* The size of the single step instruction causing the actual fault */
86 static int single_step_fault_size;
88 /* Offset between fp and the first argument in the callee */
89 #define ARGS_OFFSET 16
90 #define GP_SCRATCH_REG AMD64_R11
93 * AMD64 register usage:
94 * - callee saved registers are used for global register allocation
95 * - %r11 is used for materializing 64 bit constants in opcodes
96 * - the rest is used for local allocation
100 * Floating point comparison results:
110 mono_arch_regname (int reg)
113 case AMD64_RAX: return "%rax";
114 case AMD64_RBX: return "%rbx";
115 case AMD64_RCX: return "%rcx";
116 case AMD64_RDX: return "%rdx";
117 case AMD64_RSP: return "%rsp";
118 case AMD64_RBP: return "%rbp";
119 case AMD64_RDI: return "%rdi";
120 case AMD64_RSI: return "%rsi";
121 case AMD64_R8: return "%r8";
122 case AMD64_R9: return "%r9";
123 case AMD64_R10: return "%r10";
124 case AMD64_R11: return "%r11";
125 case AMD64_R12: return "%r12";
126 case AMD64_R13: return "%r13";
127 case AMD64_R14: return "%r14";
128 case AMD64_R15: return "%r15";
133 static const char * packed_xmmregs [] = {
134 "p:xmm0", "p:xmm1", "p:xmm2", "p:xmm3", "p:xmm4", "p:xmm5", "p:xmm6", "p:xmm7", "p:xmm8",
135 "p:xmm9", "p:xmm10", "p:xmm11", "p:xmm12", "p:xmm13", "p:xmm14", "p:xmm15"
138 static const char * single_xmmregs [] = {
139 "s:xmm0", "s:xmm1", "s:xmm2", "s:xmm3", "s:xmm4", "s:xmm5", "s:xmm6", "s:xmm7", "s:xmm8",
140 "s:xmm9", "s:xmm10", "s:xmm11", "s:xmm12", "s:xmm13", "s:xmm14", "s:xmm15"
144 mono_arch_fregname (int reg)
146 if (reg < AMD64_XMM_NREG)
147 return single_xmmregs [reg];
153 mono_arch_xregname (int reg)
155 if (reg < AMD64_XMM_NREG)
156 return packed_xmmregs [reg];
165 return mono_debug_count ();
171 static inline gboolean
172 amd64_is_near_call (guint8 *code)
175 if ((code [0] >= 0x40) && (code [0] <= 0x4f))
178 return code [0] == 0xe8;
181 #ifdef __native_client_codegen__
183 /* Keep track of instruction "depth", that is, the level of sub-instruction */
184 /* for any given instruction. For instance, amd64_call_reg resolves to */
185 /* amd64_call_reg_internal, which uses amd64_alu_* macros, etc. */
186 /* We only want to force bundle alignment for the top level instruction, */
187 /* so NaCl pseudo-instructions can be implemented with sub instructions. */
188 static MonoNativeTlsKey nacl_instruction_depth;
190 static MonoNativeTlsKey nacl_rex_tag;
191 static MonoNativeTlsKey nacl_legacy_prefix_tag;
194 amd64_nacl_clear_legacy_prefix_tag ()
196 mono_native_tls_set_value (nacl_legacy_prefix_tag, NULL);
200 amd64_nacl_tag_legacy_prefix (guint8* code)
202 if (mono_native_tls_get_value (nacl_legacy_prefix_tag) == NULL)
203 mono_native_tls_set_value (nacl_legacy_prefix_tag, code);
207 amd64_nacl_tag_rex (guint8* code)
209 mono_native_tls_set_value (nacl_rex_tag, code);
213 amd64_nacl_get_legacy_prefix_tag ()
215 return (guint8*)mono_native_tls_get_value (nacl_legacy_prefix_tag);
219 amd64_nacl_get_rex_tag ()
221 return (guint8*)mono_native_tls_get_value (nacl_rex_tag);
224 /* Increment the instruction "depth" described above */
226 amd64_nacl_instruction_pre ()
228 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
230 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)depth);
233 /* amd64_nacl_instruction_post: Decrement instruction "depth", force bundle */
234 /* alignment if depth == 0 (top level instruction) */
235 /* IN: start, end pointers to instruction beginning and end */
236 /* OUT: start, end pointers to beginning and end after possible alignment */
237 /* GLOBALS: nacl_instruction_depth defined above */
239 amd64_nacl_instruction_post (guint8 **start, guint8 **end)
241 intptr_t depth = (intptr_t) mono_native_tls_get_value (nacl_instruction_depth);
243 mono_native_tls_set_value (nacl_instruction_depth, (void*)depth);
245 g_assert ( depth >= 0 );
247 uintptr_t space_in_block;
249 guint8 *prefix = amd64_nacl_get_legacy_prefix_tag ();
250 /* if legacy prefix is present, and if it was emitted before */
251 /* the start of the instruction sequence, adjust the start */
252 if (prefix != NULL && prefix < *start) {
253 g_assert (*start - prefix <= 3);/* only 3 are allowed */
256 space_in_block = kNaClAlignment - ((uintptr_t)(*start) & kNaClAlignmentMask);
257 instlen = (uintptr_t)(*end - *start);
258 /* Only check for instructions which are less than */
259 /* kNaClAlignment. The only instructions that should ever */
260 /* be that long are call sequences, which are already */
261 /* padded out to align the return to the next bundle. */
262 if (instlen > space_in_block && instlen < kNaClAlignment) {
263 const size_t MAX_NACL_INST_LENGTH = kNaClAlignment;
264 guint8 copy_of_instruction[MAX_NACL_INST_LENGTH];
265 const size_t length = (size_t)((*end)-(*start));
266 g_assert (length < MAX_NACL_INST_LENGTH);
268 memcpy (copy_of_instruction, *start, length);
269 *start = mono_arch_nacl_pad (*start, space_in_block);
270 memcpy (*start, copy_of_instruction, length);
271 *end = *start + length;
273 amd64_nacl_clear_legacy_prefix_tag ();
274 amd64_nacl_tag_rex (NULL);
278 /* amd64_nacl_membase_handler: ensure all access to memory of the form */
279 /* OFFSET(%rXX) is sandboxed. For allowable base registers %rip, %rbp, */
280 /* %rsp, and %r15, emit the membase as usual. For all other registers, */
281 /* make sure the upper 32-bits are cleared, and use that register in the */
282 /* index field of a new address of this form: OFFSET(%r15,%eXX,1) */
284 /* pointer to current instruction stream (in the */
285 /* middle of an instruction, after opcode is emitted) */
286 /* basereg/offset/dreg */
287 /* operands of normal membase address */
289 /* pointer to the end of the membase/memindex emit */
290 /* GLOBALS: nacl_rex_tag */
291 /* position in instruction stream that rex prefix was emitted */
292 /* nacl_legacy_prefix_tag */
293 /* (possibly NULL) position in instruction of legacy x86 prefix */
295 amd64_nacl_membase_handler (guint8** code, gint8 basereg, gint32 offset, gint8 dreg)
297 gint8 true_basereg = basereg;
299 /* Cache these values, they might change */
300 /* as new instructions are emitted below. */
301 guint8* rex_tag = amd64_nacl_get_rex_tag ();
302 guint8* legacy_prefix_tag = amd64_nacl_get_legacy_prefix_tag ();
304 /* 'basereg' is given masked to 0x7 at this point, so check */
305 /* the rex prefix to see if this is an extended register. */
306 if ((rex_tag != NULL) && IS_REX(*rex_tag) && (*rex_tag & AMD64_REX_B)) {
310 #define X86_LEA_OPCODE (0x8D)
312 if (!amd64_is_valid_nacl_base (true_basereg) && (*(*code-1) != X86_LEA_OPCODE)) {
313 guint8* old_instruction_start;
315 /* This will hold the 'mov %eXX, %eXX' that clears the upper */
316 /* 32-bits of the old base register (new index register) */
318 guint8* buf_ptr = buf;
321 g_assert (rex_tag != NULL);
323 if (IS_REX(*rex_tag)) {
324 /* The old rex.B should be the new rex.X */
325 if (*rex_tag & AMD64_REX_B) {
326 *rex_tag |= AMD64_REX_X;
328 /* Since our new base is %r15 set rex.B */
329 *rex_tag |= AMD64_REX_B;
331 /* Shift the instruction by one byte */
332 /* so we can insert a rex prefix */
333 memmove (rex_tag + 1, rex_tag, (size_t)(*code - rex_tag));
335 /* New rex prefix only needs rex.B for %r15 base */
336 *rex_tag = AMD64_REX(AMD64_REX_B);
339 if (legacy_prefix_tag) {
340 old_instruction_start = legacy_prefix_tag;
342 old_instruction_start = rex_tag;
345 /* Clears the upper 32-bits of the previous base register */
346 amd64_mov_reg_reg_size (buf_ptr, true_basereg, true_basereg, 4);
347 insert_len = buf_ptr - buf;
349 /* Move the old instruction forward to make */
350 /* room for 'mov' stored in 'buf_ptr' */
351 memmove (old_instruction_start + insert_len, old_instruction_start, (size_t)(*code - old_instruction_start));
353 memcpy (old_instruction_start, buf, insert_len);
355 /* Sandboxed replacement for the normal membase_emit */
356 x86_memindex_emit (*code, dreg, AMD64_R15, offset, basereg, 0);
359 /* Normal default behavior, emit membase memory location */
360 x86_membase_emit_body (*code, dreg, basereg, offset);
365 static inline unsigned char*
366 amd64_skip_nops (unsigned char* code)
371 if ( code[0] == 0x90) {
375 if ( code[0] == 0x66 && code[1] == 0x90) {
379 if (code[0] == 0x0f && code[1] == 0x1f
380 && code[2] == 0x00) {
384 if (code[0] == 0x0f && code[1] == 0x1f
385 && code[2] == 0x40 && code[3] == 0x00) {
389 if (code[0] == 0x0f && code[1] == 0x1f
390 && code[2] == 0x44 && code[3] == 0x00
391 && code[4] == 0x00) {
395 if (code[0] == 0x66 && code[1] == 0x0f
396 && code[2] == 0x1f && code[3] == 0x44
397 && code[4] == 0x00 && code[5] == 0x00) {
401 if (code[0] == 0x0f && code[1] == 0x1f
402 && code[2] == 0x80 && code[3] == 0x00
403 && code[4] == 0x00 && code[5] == 0x00
404 && code[6] == 0x00) {
408 if (code[0] == 0x0f && code[1] == 0x1f
409 && code[2] == 0x84 && code[3] == 0x00
410 && code[4] == 0x00 && code[5] == 0x00
411 && code[6] == 0x00 && code[7] == 0x00) {
420 mono_arch_nacl_skip_nops (guint8* code)
422 return amd64_skip_nops(code);
425 #endif /*__native_client_codegen__*/
428 amd64_patch (unsigned char* code, gpointer target)
432 #ifdef __native_client_codegen__
433 code = amd64_skip_nops (code);
435 #if defined(__native_client_codegen__) && defined(__native_client__)
436 if (nacl_is_code_address (code)) {
437 /* For tail calls, code is patched after being installed */
438 /* but not through the normal "patch callsite" method. */
439 unsigned char buf[kNaClAlignment];
440 unsigned char *aligned_code = (uintptr_t)code & ~kNaClAlignmentMask;
442 memcpy (buf, aligned_code, kNaClAlignment);
443 /* Patch a temp buffer of bundle size, */
444 /* then install to actual location. */
445 amd64_patch (buf + ((uintptr_t)code - (uintptr_t)aligned_code), target);
446 ret = nacl_dyncode_modify (aligned_code, buf, kNaClAlignment);
450 target = nacl_modify_patch_target (target);
454 if ((code [0] >= 0x40) && (code [0] <= 0x4f)) {
459 if ((code [0] & 0xf8) == 0xb8) {
460 /* amd64_set_reg_template */
461 *(guint64*)(code + 1) = (guint64)target;
463 else if ((code [0] == 0x8b) && rex && x86_modrm_mod (code [1]) == 0 && x86_modrm_rm (code [1]) == 5) {
464 /* mov 0(%rip), %dreg */
465 *(guint32*)(code + 2) = (guint32)(guint64)target - 7;
467 else if ((code [0] == 0xff) && (code [1] == 0x15)) {
468 /* call *<OFFSET>(%rip) */
469 *(guint32*)(code + 2) = ((guint32)(guint64)target) - 7;
471 else if (code [0] == 0xe8) {
473 gint64 disp = (guint8*)target - (guint8*)code;
474 g_assert (amd64_is_imm32 (disp));
475 x86_patch (code, (unsigned char*)target);
478 x86_patch (code, (unsigned char*)target);
482 mono_amd64_patch (unsigned char* code, gpointer target)
484 amd64_patch (code, target);
493 ArgValuetypeAddrInIReg,
494 ArgNone /* only in pair_storage */
502 /* Only if storage == ArgValuetypeInReg */
503 ArgStorage pair_storage [2];
505 /* The size of each pair */
515 gboolean need_stack_align;
516 gboolean vtype_retaddr;
517 /* The index of the vret arg in the argument list */
524 #define DEBUG(a) if (cfg->verbose_level > 1) a
527 static AMD64_Reg_No param_regs [] = { AMD64_RCX, AMD64_RDX, AMD64_R8, AMD64_R9 };
529 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
531 static AMD64_Reg_No param_regs [] = { AMD64_RDI, AMD64_RSI, AMD64_RDX, AMD64_RCX, AMD64_R8, AMD64_R9 };
533 static AMD64_Reg_No return_regs [] = { AMD64_RAX, AMD64_RDX };
537 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
539 ainfo->offset = *stack_size;
541 if (*gr >= PARAM_REGS) {
542 ainfo->storage = ArgOnStack;
543 /* Since the same stack slot size is used for all arg */
544 /* types, it needs to be big enough to hold them all */
545 (*stack_size) += sizeof(mgreg_t);
548 ainfo->storage = ArgInIReg;
549 ainfo->reg = param_regs [*gr];
555 #define FLOAT_PARAM_REGS 4
557 #define FLOAT_PARAM_REGS 8
561 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
563 ainfo->offset = *stack_size;
565 if (*gr >= FLOAT_PARAM_REGS) {
566 ainfo->storage = ArgOnStack;
567 /* Since the same stack slot size is used for both float */
568 /* types, it needs to be big enough to hold them both */
569 (*stack_size) += sizeof(mgreg_t);
572 /* A double register */
574 ainfo->storage = ArgInDoubleSSEReg;
576 ainfo->storage = ArgInFloatSSEReg;
582 typedef enum ArgumentClass {
590 merge_argument_class_from_type (MonoGenericSharingContext *gsctx, MonoType *type, ArgumentClass class1)
592 ArgumentClass class2 = ARG_CLASS_NO_CLASS;
595 ptype = mini_type_get_underlying_type (gsctx, type);
596 switch (ptype->type) {
597 case MONO_TYPE_BOOLEAN:
607 case MONO_TYPE_STRING:
608 case MONO_TYPE_OBJECT:
609 case MONO_TYPE_CLASS:
610 case MONO_TYPE_SZARRAY:
612 case MONO_TYPE_FNPTR:
613 case MONO_TYPE_ARRAY:
616 class2 = ARG_CLASS_INTEGER;
621 class2 = ARG_CLASS_INTEGER;
623 class2 = ARG_CLASS_SSE;
627 case MONO_TYPE_TYPEDBYREF:
628 g_assert_not_reached ();
630 case MONO_TYPE_GENERICINST:
631 if (!mono_type_generic_inst_is_valuetype (ptype)) {
632 class2 = ARG_CLASS_INTEGER;
636 case MONO_TYPE_VALUETYPE: {
637 MonoMarshalType *info = mono_marshal_load_type_info (ptype->data.klass);
640 for (i = 0; i < info->num_fields; ++i) {
642 class2 = merge_argument_class_from_type (gsctx, info->fields [i].field->type, class2);
647 g_assert_not_reached ();
651 if (class1 == class2)
653 else if (class1 == ARG_CLASS_NO_CLASS)
655 else if ((class1 == ARG_CLASS_MEMORY) || (class2 == ARG_CLASS_MEMORY))
656 class1 = ARG_CLASS_MEMORY;
657 else if ((class1 == ARG_CLASS_INTEGER) || (class2 == ARG_CLASS_INTEGER))
658 class1 = ARG_CLASS_INTEGER;
660 class1 = ARG_CLASS_SSE;
664 #ifdef __native_client_codegen__
666 /* Default alignment for Native Client is 32-byte. */
667 gint8 nacl_align_byte = -32; /* signed version of 0xe0 */
669 /* mono_arch_nacl_pad: Add pad bytes of alignment instructions at code, */
670 /* Check that alignment doesn't cross an alignment boundary. */
672 mono_arch_nacl_pad(guint8 *code, int pad)
674 const int kMaxPadding = 8; /* see amd64-codegen.h:amd64_padding_size() */
676 if (pad == 0) return code;
677 /* assertion: alignment cannot cross a block boundary */
678 g_assert (((uintptr_t)code & (~kNaClAlignmentMask)) ==
679 (((uintptr_t)code + pad - 1) & (~kNaClAlignmentMask)));
680 while (pad >= kMaxPadding) {
681 amd64_padding (code, kMaxPadding);
684 if (pad != 0) amd64_padding (code, pad);
690 count_fields_nested (MonoClass *klass)
692 MonoMarshalType *info;
695 info = mono_marshal_load_type_info (klass);
698 for (i = 0; i < info->num_fields; ++i) {
699 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type))
700 count += count_fields_nested (mono_class_from_mono_type (info->fields [i].field->type));
708 collect_field_info_nested (MonoClass *klass, MonoMarshalField *fields, int index, int offset)
710 MonoMarshalType *info;
713 info = mono_marshal_load_type_info (klass);
715 for (i = 0; i < info->num_fields; ++i) {
716 if (MONO_TYPE_ISSTRUCT (info->fields [i].field->type)) {
717 index = collect_field_info_nested (mono_class_from_mono_type (info->fields [i].field->type), fields, index, info->fields [i].offset);
719 memcpy (&fields [index], &info->fields [i], sizeof (MonoMarshalField));
720 fields [index].offset += offset;
728 add_valuetype (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
730 guint32 *gr, guint32 *fr, guint32 *stack_size)
732 guint32 size, quad, nquads, i, nfields;
733 /* Keep track of the size used in each quad so we can */
734 /* use the right size when copying args/return vars. */
735 guint32 quadsize [2] = {8, 8};
736 ArgumentClass args [2];
737 MonoMarshalType *info = NULL;
738 MonoMarshalField *fields = NULL;
740 MonoGenericSharingContext tmp_gsctx;
741 gboolean pass_on_stack = FALSE;
744 * The gsctx currently contains no data, it is only used for checking whenever
745 * open types are allowed, some callers like mono_arch_get_argument_info ()
746 * don't pass it to us, so work around that.
751 klass = mono_class_from_mono_type (type);
752 size = mini_type_stack_size_full (gsctx, &klass->byval_arg, NULL, sig->pinvoke);
754 if (!sig->pinvoke && ((is_return && (size == 8)) || (!is_return && (size <= 16)))) {
755 /* We pass and return vtypes of size 8 in a register */
756 } else if (!sig->pinvoke || (size == 0) || (size > 16)) {
757 pass_on_stack = TRUE;
761 pass_on_stack = TRUE;
765 /* If this struct can't be split up naturally into 8-byte */
766 /* chunks (registers), pass it on the stack. */
767 if (sig->pinvoke && !pass_on_stack) {
771 info = mono_marshal_load_type_info (klass);
775 * Collect field information recursively to be able to
776 * handle nested structures.
778 nfields = count_fields_nested (klass);
779 fields = g_new0 (MonoMarshalField, nfields);
780 collect_field_info_nested (klass, fields, 0, 0);
782 for (i = 0; i < nfields; ++i) {
783 field_size = mono_marshal_type_size (fields [i].field->type,
785 &align, TRUE, klass->unicode);
786 if ((fields [i].offset < 8) && (fields [i].offset + field_size) > 8) {
787 pass_on_stack = TRUE;
794 /* Allways pass in memory */
795 ainfo->offset = *stack_size;
796 *stack_size += ALIGN_TO (size, 8);
797 ainfo->storage = ArgOnStack;
803 /* FIXME: Handle structs smaller than 8 bytes */
804 //if ((size % 8) != 0)
813 int n = mono_class_value_size (klass, NULL);
815 quadsize [0] = n >= 8 ? 8 : n;
816 quadsize [1] = n >= 8 ? MAX (n - 8, 8) : 0;
818 /* Always pass in 1 or 2 integer registers */
819 args [0] = ARG_CLASS_INTEGER;
820 args [1] = ARG_CLASS_INTEGER;
821 /* Only the simplest cases are supported */
822 if (is_return && nquads != 1) {
823 args [0] = ARG_CLASS_MEMORY;
824 args [1] = ARG_CLASS_MEMORY;
828 * Implement the algorithm from section 3.2.3 of the X86_64 ABI.
829 * The X87 and SSEUP stuff is left out since there are no such types in
836 if (info->native_size > 16) {
837 ainfo->offset = *stack_size;
838 *stack_size += ALIGN_TO (info->native_size, 8);
839 ainfo->storage = ArgOnStack;
845 switch (info->native_size) {
846 case 1: case 2: case 4: case 8:
850 ainfo->storage = ArgOnStack;
851 ainfo->offset = *stack_size;
852 *stack_size += ALIGN_TO (info->native_size, 8);
855 ainfo->storage = ArgValuetypeAddrInIReg;
857 if (*gr < PARAM_REGS) {
858 ainfo->pair_storage [0] = ArgInIReg;
859 ainfo->pair_regs [0] = param_regs [*gr];
863 ainfo->pair_storage [0] = ArgOnStack;
864 ainfo->offset = *stack_size;
874 args [0] = ARG_CLASS_NO_CLASS;
875 args [1] = ARG_CLASS_NO_CLASS;
876 for (quad = 0; quad < nquads; ++quad) {
879 ArgumentClass class1;
882 class1 = ARG_CLASS_MEMORY;
884 class1 = ARG_CLASS_NO_CLASS;
885 for (i = 0; i < nfields; ++i) {
886 size = mono_marshal_type_size (fields [i].field->type,
888 &align, TRUE, klass->unicode);
889 if ((fields [i].offset < 8) && (fields [i].offset + size) > 8) {
890 /* Unaligned field */
894 /* Skip fields in other quad */
895 if ((quad == 0) && (fields [i].offset >= 8))
897 if ((quad == 1) && (fields [i].offset < 8))
900 /* How far into this quad this data extends.*/
901 /* (8 is size of quad) */
902 quadsize [quad] = fields [i].offset + size - (quad * 8);
904 class1 = merge_argument_class_from_type (gsctx, fields [i].field->type, class1);
906 g_assert (class1 != ARG_CLASS_NO_CLASS);
907 args [quad] = class1;
913 /* Post merger cleanup */
914 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY))
915 args [0] = args [1] = ARG_CLASS_MEMORY;
917 /* Allocate registers */
922 while (quadsize [0] != 1 && quadsize [0] != 2 && quadsize [0] != 4 && quadsize [0] != 8)
924 while (quadsize [1] != 1 && quadsize [1] != 2 && quadsize [1] != 4 && quadsize [1] != 8)
927 ainfo->storage = ArgValuetypeInReg;
928 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
929 g_assert (quadsize [0] <= 8);
930 g_assert (quadsize [1] <= 8);
931 ainfo->pair_size [0] = quadsize [0];
932 ainfo->pair_size [1] = quadsize [1];
933 ainfo->nregs = nquads;
934 for (quad = 0; quad < nquads; ++quad) {
935 switch (args [quad]) {
936 case ARG_CLASS_INTEGER:
937 if (*gr >= PARAM_REGS)
938 args [quad] = ARG_CLASS_MEMORY;
940 ainfo->pair_storage [quad] = ArgInIReg;
942 ainfo->pair_regs [quad] = return_regs [*gr];
944 ainfo->pair_regs [quad] = param_regs [*gr];
949 if (*fr >= FLOAT_PARAM_REGS)
950 args [quad] = ARG_CLASS_MEMORY;
952 if (quadsize[quad] <= 4)
953 ainfo->pair_storage [quad] = ArgInFloatSSEReg;
954 else ainfo->pair_storage [quad] = ArgInDoubleSSEReg;
955 ainfo->pair_regs [quad] = *fr;
959 case ARG_CLASS_MEMORY:
962 g_assert_not_reached ();
966 if ((args [0] == ARG_CLASS_MEMORY) || (args [1] == ARG_CLASS_MEMORY)) {
967 /* Revert possible register assignments */
971 ainfo->offset = *stack_size;
973 *stack_size += ALIGN_TO (info->native_size, 8);
975 *stack_size += nquads * sizeof(mgreg_t);
976 ainfo->storage = ArgOnStack;
984 * Obtain information about a call according to the calling convention.
985 * For AMD64, see the "System V ABI, x86-64 Architecture Processor Supplement
986 * Draft Version 0.23" document for more information.
989 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
991 guint32 i, gr, fr, pstart;
993 int n = sig->hasthis + sig->param_count;
994 guint32 stack_size = 0;
996 gboolean is_pinvoke = sig->pinvoke;
999 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1001 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1009 /* Reserve space where the callee can save the argument registers */
1010 stack_size = 4 * sizeof (mgreg_t);
1015 ret_type = mini_type_get_underlying_type (gsctx, sig->ret);
1016 switch (ret_type->type) {
1017 case MONO_TYPE_BOOLEAN:
1022 case MONO_TYPE_CHAR:
1028 case MONO_TYPE_FNPTR:
1029 case MONO_TYPE_CLASS:
1030 case MONO_TYPE_OBJECT:
1031 case MONO_TYPE_SZARRAY:
1032 case MONO_TYPE_ARRAY:
1033 case MONO_TYPE_STRING:
1034 cinfo->ret.storage = ArgInIReg;
1035 cinfo->ret.reg = AMD64_RAX;
1039 cinfo->ret.storage = ArgInIReg;
1040 cinfo->ret.reg = AMD64_RAX;
1043 cinfo->ret.storage = ArgInFloatSSEReg;
1044 cinfo->ret.reg = AMD64_XMM0;
1047 cinfo->ret.storage = ArgInDoubleSSEReg;
1048 cinfo->ret.reg = AMD64_XMM0;
1050 case MONO_TYPE_GENERICINST:
1051 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
1052 cinfo->ret.storage = ArgInIReg;
1053 cinfo->ret.reg = AMD64_RAX;
1057 #if defined( __native_client_codegen__ )
1058 case MONO_TYPE_TYPEDBYREF:
1060 case MONO_TYPE_VALUETYPE: {
1061 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
1063 add_valuetype (gsctx, sig, &cinfo->ret, ret_type, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
1064 if (cinfo->ret.storage == ArgOnStack) {
1065 cinfo->vtype_retaddr = TRUE;
1066 /* The caller passes the address where the value is stored */
1070 #if !defined( __native_client_codegen__ )
1071 case MONO_TYPE_TYPEDBYREF:
1072 /* Same as a valuetype with size 24 */
1073 cinfo->vtype_retaddr = TRUE;
1076 case MONO_TYPE_VOID:
1079 g_error ("Can't handle as return value 0x%x", ret_type->type);
1085 * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1086 * the first argument, allowing 'this' to be always passed in the first arg reg.
1087 * Also do this if the first argument is a reference type, since virtual calls
1088 * are sometimes made using calli without sig->hasthis set, like in the delegate
1091 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]))))) {
1093 add_general (&gr, &stack_size, cinfo->args + 0);
1095 add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0]);
1098 add_general (&gr, &stack_size, &cinfo->ret);
1099 cinfo->vret_arg_index = 1;
1103 add_general (&gr, &stack_size, cinfo->args + 0);
1105 if (cinfo->vtype_retaddr)
1106 add_general (&gr, &stack_size, &cinfo->ret);
1109 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
1111 fr = FLOAT_PARAM_REGS;
1113 /* Emit the signature cookie just before the implicit arguments */
1114 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1117 for (i = pstart; i < sig->param_count; ++i) {
1118 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
1122 /* The float param registers and other param registers must be the same index on Windows x64.*/
1129 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1130 /* We allways pass the sig cookie on the stack for simplicity */
1132 * Prevent implicit arguments + the sig cookie from being passed
1136 fr = FLOAT_PARAM_REGS;
1138 /* Emit the signature cookie just before the implicit arguments */
1139 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1142 ptype = mini_type_get_underlying_type (gsctx, sig->params [i]);
1143 switch (ptype->type) {
1144 case MONO_TYPE_BOOLEAN:
1147 add_general (&gr, &stack_size, ainfo);
1151 case MONO_TYPE_CHAR:
1152 add_general (&gr, &stack_size, ainfo);
1156 add_general (&gr, &stack_size, ainfo);
1161 case MONO_TYPE_FNPTR:
1162 case MONO_TYPE_CLASS:
1163 case MONO_TYPE_OBJECT:
1164 case MONO_TYPE_STRING:
1165 case MONO_TYPE_SZARRAY:
1166 case MONO_TYPE_ARRAY:
1167 add_general (&gr, &stack_size, ainfo);
1169 case MONO_TYPE_GENERICINST:
1170 if (!mono_type_generic_inst_is_valuetype (ptype)) {
1171 add_general (&gr, &stack_size, ainfo);
1175 case MONO_TYPE_VALUETYPE:
1176 case MONO_TYPE_TYPEDBYREF:
1177 add_valuetype (gsctx, sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
1182 add_general (&gr, &stack_size, ainfo);
1185 add_float (&fr, &stack_size, ainfo, FALSE);
1188 add_float (&fr, &stack_size, ainfo, TRUE);
1191 g_assert_not_reached ();
1195 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
1197 fr = FLOAT_PARAM_REGS;
1199 /* Emit the signature cookie just before the implicit arguments */
1200 add_general (&gr, &stack_size, &cinfo->sig_cookie);
1203 cinfo->stack_usage = stack_size;
1204 cinfo->reg_usage = gr;
1205 cinfo->freg_usage = fr;
1210 * mono_arch_get_argument_info:
1211 * @csig: a method signature
1212 * @param_count: the number of parameters to consider
1213 * @arg_info: an array to store the result infos
1215 * Gathers information on parameters such as size, alignment and
1216 * padding. arg_info should be large enought to hold param_count + 1 entries.
1218 * Returns the size of the argument area on the stack.
1221 mono_arch_get_argument_info (MonoGenericSharingContext *gsctx, MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
1224 CallInfo *cinfo = get_call_info (NULL, NULL, csig);
1225 guint32 args_size = cinfo->stack_usage;
1227 /* The arguments are saved to a stack area in mono_arch_instrument_prolog */
1228 if (csig->hasthis) {
1229 arg_info [0].offset = 0;
1232 for (k = 0; k < param_count; k++) {
1233 arg_info [k + 1].offset = ((k + csig->hasthis) * 8);
1235 arg_info [k + 1].size = 0;
1244 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
1248 MonoType *callee_ret;
1250 c1 = get_call_info (NULL, NULL, caller_sig);
1251 c2 = get_call_info (NULL, NULL, callee_sig);
1252 res = c1->stack_usage >= c2->stack_usage;
1253 callee_ret = mini_get_underlying_type (cfg, callee_sig->ret);
1254 if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != ArgValuetypeInReg)
1255 /* An address on the callee's stack is passed as the first argument */
1265 * Initialize the cpu to execute managed code.
1268 mono_arch_cpu_init (void)
1273 /* spec compliance requires running with double precision */
1274 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1275 fpcw &= ~X86_FPCW_PRECC_MASK;
1276 fpcw |= X86_FPCW_PREC_DOUBLE;
1277 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
1278 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
1280 /* TODO: This is crashing on Win64 right now.
1281 * _control87 (_PC_53, MCW_PC);
1287 * Initialize architecture specific code.
1290 mono_arch_init (void)
1294 mono_mutex_init_recursive (&mini_arch_mutex);
1295 #if defined(__native_client_codegen__)
1296 mono_native_tls_alloc (&nacl_instruction_depth, NULL);
1297 mono_native_tls_set_value (nacl_instruction_depth, (gpointer)0);
1298 mono_native_tls_alloc (&nacl_rex_tag, NULL);
1299 mono_native_tls_alloc (&nacl_legacy_prefix_tag, NULL);
1302 #ifdef MONO_ARCH_NOMAP32BIT
1303 flags = MONO_MMAP_READ;
1304 /* amd64_mov_reg_imm () + amd64_mov_reg_membase () */
1305 breakpoint_size = 13;
1306 breakpoint_fault_size = 3;
1308 flags = MONO_MMAP_READ|MONO_MMAP_32BIT;
1309 /* amd64_mov_reg_mem () */
1310 breakpoint_size = 8;
1311 breakpoint_fault_size = 8;
1314 /* amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4); */
1315 single_step_fault_size = 4;
1317 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), flags);
1318 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), flags);
1319 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
1321 mono_aot_register_jit_icall ("mono_amd64_throw_exception", mono_amd64_throw_exception);
1322 mono_aot_register_jit_icall ("mono_amd64_throw_corlib_exception", mono_amd64_throw_corlib_exception);
1323 mono_aot_register_jit_icall ("mono_amd64_get_original_ip", mono_amd64_get_original_ip);
1327 * Cleanup architecture specific code.
1330 mono_arch_cleanup (void)
1332 mono_mutex_destroy (&mini_arch_mutex);
1333 #if defined(__native_client_codegen__)
1334 mono_native_tls_free (nacl_instruction_depth);
1335 mono_native_tls_free (nacl_rex_tag);
1336 mono_native_tls_free (nacl_legacy_prefix_tag);
1341 * This function returns the optimizations supported on this cpu.
1344 mono_arch_cpu_optimizations (guint32 *exclude_mask)
1350 if (mono_hwcap_x86_has_cmov) {
1351 opts |= MONO_OPT_CMOV;
1353 if (mono_hwcap_x86_has_fcmov)
1354 opts |= MONO_OPT_FCMOV;
1356 *exclude_mask |= MONO_OPT_FCMOV;
1358 *exclude_mask |= MONO_OPT_CMOV;
1365 * This function test for all SSE functions supported.
1367 * Returns a bitmask corresponding to all supported versions.
1371 mono_arch_cpu_enumerate_simd_versions (void)
1373 guint32 sse_opts = 0;
1375 if (mono_hwcap_x86_has_sse1)
1376 sse_opts |= SIMD_VERSION_SSE1;
1378 if (mono_hwcap_x86_has_sse2)
1379 sse_opts |= SIMD_VERSION_SSE2;
1381 if (mono_hwcap_x86_has_sse3)
1382 sse_opts |= SIMD_VERSION_SSE3;
1384 if (mono_hwcap_x86_has_ssse3)
1385 sse_opts |= SIMD_VERSION_SSSE3;
1387 if (mono_hwcap_x86_has_sse41)
1388 sse_opts |= SIMD_VERSION_SSE41;
1390 if (mono_hwcap_x86_has_sse42)
1391 sse_opts |= SIMD_VERSION_SSE42;
1393 if (mono_hwcap_x86_has_sse4a)
1394 sse_opts |= SIMD_VERSION_SSE4a;
1402 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
1407 for (i = 0; i < cfg->num_varinfo; i++) {
1408 MonoInst *ins = cfg->varinfo [i];
1409 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
1412 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
1415 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
1416 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
1419 if (mono_is_regsize_var (ins->inst_vtype)) {
1420 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
1421 g_assert (i == vmv->idx);
1422 vars = g_list_prepend (vars, vmv);
1426 vars = mono_varlist_sort (cfg, vars, 0);
1432 * mono_arch_compute_omit_fp:
1434 * Determine whenever the frame pointer can be eliminated.
1437 mono_arch_compute_omit_fp (MonoCompile *cfg)
1439 MonoMethodSignature *sig;
1440 MonoMethodHeader *header;
1444 if (cfg->arch.omit_fp_computed)
1447 header = cfg->header;
1449 sig = mono_method_signature (cfg->method);
1451 if (!cfg->arch.cinfo)
1452 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1453 cinfo = cfg->arch.cinfo;
1456 * FIXME: Remove some of the restrictions.
1458 cfg->arch.omit_fp = TRUE;
1459 cfg->arch.omit_fp_computed = TRUE;
1461 #ifdef __native_client_codegen__
1462 /* NaCl modules may not change the value of RBP, so it cannot be */
1463 /* used as a normal register, but it can be used as a frame pointer*/
1464 cfg->disable_omit_fp = TRUE;
1465 cfg->arch.omit_fp = FALSE;
1468 if (cfg->disable_omit_fp)
1469 cfg->arch.omit_fp = FALSE;
1471 if (!debug_omit_fp ())
1472 cfg->arch.omit_fp = FALSE;
1474 if (cfg->method->save_lmf)
1475 cfg->arch.omit_fp = FALSE;
1477 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1478 cfg->arch.omit_fp = FALSE;
1479 if (header->num_clauses)
1480 cfg->arch.omit_fp = FALSE;
1481 if (cfg->param_area)
1482 cfg->arch.omit_fp = FALSE;
1483 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1484 cfg->arch.omit_fp = FALSE;
1485 if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1486 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1487 cfg->arch.omit_fp = FALSE;
1488 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1489 ArgInfo *ainfo = &cinfo->args [i];
1491 if (ainfo->storage == ArgOnStack) {
1493 * The stack offset can only be determined when the frame
1496 cfg->arch.omit_fp = FALSE;
1501 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1502 MonoInst *ins = cfg->varinfo [i];
1505 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1510 mono_arch_get_global_int_regs (MonoCompile *cfg)
1514 mono_arch_compute_omit_fp (cfg);
1516 if (cfg->globalra) {
1517 if (cfg->arch.omit_fp)
1518 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1520 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1521 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1522 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1523 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1524 #ifndef __native_client_codegen__
1525 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1528 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1529 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1530 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1531 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1532 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1533 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1534 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1535 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1537 if (cfg->arch.omit_fp)
1538 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1540 /* We use the callee saved registers for global allocation */
1541 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1542 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1543 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1544 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1545 #ifndef __native_client_codegen__
1546 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1549 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1550 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1558 mono_arch_get_global_fp_regs (MonoCompile *cfg)
1563 /* All XMM registers */
1564 for (i = 0; i < 16; ++i)
1565 regs = g_list_prepend (regs, GINT_TO_POINTER (i));
1571 mono_arch_get_iregs_clobbered_by_call (MonoCallInst *call)
1573 static GList *r = NULL;
1578 regs = g_list_prepend (regs, (gpointer)AMD64_RBP);
1579 regs = g_list_prepend (regs, (gpointer)AMD64_RBX);
1580 regs = g_list_prepend (regs, (gpointer)AMD64_R12);
1581 regs = g_list_prepend (regs, (gpointer)AMD64_R13);
1582 regs = g_list_prepend (regs, (gpointer)AMD64_R14);
1583 #ifndef __native_client_codegen__
1584 regs = g_list_prepend (regs, (gpointer)AMD64_R15);
1587 regs = g_list_prepend (regs, (gpointer)AMD64_R10);
1588 regs = g_list_prepend (regs, (gpointer)AMD64_R9);
1589 regs = g_list_prepend (regs, (gpointer)AMD64_R8);
1590 regs = g_list_prepend (regs, (gpointer)AMD64_RDI);
1591 regs = g_list_prepend (regs, (gpointer)AMD64_RSI);
1592 regs = g_list_prepend (regs, (gpointer)AMD64_RDX);
1593 regs = g_list_prepend (regs, (gpointer)AMD64_RCX);
1594 regs = g_list_prepend (regs, (gpointer)AMD64_RAX);
1596 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1603 mono_arch_get_fregs_clobbered_by_call (MonoCallInst *call)
1606 static GList *r = NULL;
1611 for (i = 0; i < AMD64_XMM_NREG; ++i)
1612 regs = g_list_prepend (regs, GINT_TO_POINTER (MONO_MAX_IREGS + i));
1614 InterlockedCompareExchangePointer ((gpointer*)&r, regs, NULL);
1621 * mono_arch_regalloc_cost:
1623 * Return the cost, in number of memory references, of the action of
1624 * allocating the variable VMV into a register during global register
1628 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1630 MonoInst *ins = cfg->varinfo [vmv->idx];
1632 if (cfg->method->save_lmf)
1633 /* The register is already saved */
1634 /* substract 1 for the invisible store in the prolog */
1635 return (ins->opcode == OP_ARG) ? 0 : 1;
1638 return (ins->opcode == OP_ARG) ? 1 : 2;
1642 * mono_arch_fill_argument_info:
1644 * Populate cfg->args, cfg->ret and cfg->vret_addr with information about the arguments
1648 mono_arch_fill_argument_info (MonoCompile *cfg)
1651 MonoMethodSignature *sig;
1656 sig = mono_method_signature (cfg->method);
1658 cinfo = cfg->arch.cinfo;
1659 sig_ret = mini_get_underlying_type (cfg, sig->ret);
1662 * Contrary to mono_arch_allocate_vars (), the information should describe
1663 * where the arguments are at the beginning of the method, not where they can be
1664 * accessed during the execution of the method. The later makes no sense for the
1665 * global register allocator, since a variable can be in more than one location.
1667 if (sig_ret->type != MONO_TYPE_VOID) {
1668 switch (cinfo->ret.storage) {
1670 case ArgInFloatSSEReg:
1671 case ArgInDoubleSSEReg:
1672 if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->vtype_retaddr)) {
1673 cfg->vret_addr->opcode = OP_REGVAR;
1674 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1677 cfg->ret->opcode = OP_REGVAR;
1678 cfg->ret->inst_c0 = cinfo->ret.reg;
1681 case ArgValuetypeInReg:
1682 cfg->ret->opcode = OP_REGOFFSET;
1683 cfg->ret->inst_basereg = -1;
1684 cfg->ret->inst_offset = -1;
1687 g_assert_not_reached ();
1691 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1692 ArgInfo *ainfo = &cinfo->args [i];
1694 ins = cfg->args [i];
1696 switch (ainfo->storage) {
1698 case ArgInFloatSSEReg:
1699 case ArgInDoubleSSEReg:
1700 ins->opcode = OP_REGVAR;
1701 ins->inst_c0 = ainfo->reg;
1704 ins->opcode = OP_REGOFFSET;
1705 ins->inst_basereg = -1;
1706 ins->inst_offset = -1;
1708 case ArgValuetypeInReg:
1710 ins->opcode = OP_NOP;
1713 g_assert_not_reached ();
1719 mono_arch_allocate_vars (MonoCompile *cfg)
1722 MonoMethodSignature *sig;
1725 guint32 locals_stack_size, locals_stack_align;
1729 sig = mono_method_signature (cfg->method);
1731 cinfo = cfg->arch.cinfo;
1732 sig_ret = mini_get_underlying_type (cfg, sig->ret);
1734 mono_arch_compute_omit_fp (cfg);
1737 * We use the ABI calling conventions for managed code as well.
1738 * Exception: valuetypes are only sometimes passed or returned in registers.
1742 * The stack looks like this:
1743 * <incoming arguments passed on the stack>
1745 * <lmf/caller saved registers>
1748 * <localloc area> -> grows dynamically
1752 if (cfg->arch.omit_fp) {
1753 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1754 cfg->frame_reg = AMD64_RSP;
1757 /* Locals are allocated backwards from %fp */
1758 cfg->frame_reg = AMD64_RBP;
1762 cfg->arch.saved_iregs = cfg->used_int_regs;
1763 if (cfg->method->save_lmf)
1764 /* Save all callee-saved registers normally, and restore them when unwinding through an LMF */
1765 cfg->arch.saved_iregs |= (1 << AMD64_RBX) | (1 << AMD64_R12) | (1 << AMD64_R13) | (1 << AMD64_R14) | (1 << AMD64_R15);
1767 if (cfg->arch.omit_fp)
1768 cfg->arch.reg_save_area_offset = offset;
1769 /* Reserve space for callee saved registers */
1770 for (i = 0; i < AMD64_NREG; ++i)
1771 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
1772 offset += sizeof(mgreg_t);
1774 if (!cfg->arch.omit_fp)
1775 cfg->arch.reg_save_area_offset = -offset;
1777 if (sig_ret->type != MONO_TYPE_VOID) {
1778 switch (cinfo->ret.storage) {
1780 case ArgInFloatSSEReg:
1781 case ArgInDoubleSSEReg:
1782 if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->vtype_retaddr)) {
1783 if (cfg->globalra) {
1784 cfg->vret_addr->opcode = OP_REGVAR;
1785 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1787 /* The register is volatile */
1788 cfg->vret_addr->opcode = OP_REGOFFSET;
1789 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1790 if (cfg->arch.omit_fp) {
1791 cfg->vret_addr->inst_offset = offset;
1795 cfg->vret_addr->inst_offset = -offset;
1797 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1798 printf ("vret_addr =");
1799 mono_print_ins (cfg->vret_addr);
1804 cfg->ret->opcode = OP_REGVAR;
1805 cfg->ret->inst_c0 = cinfo->ret.reg;
1808 case ArgValuetypeInReg:
1809 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1810 cfg->ret->opcode = OP_REGOFFSET;
1811 cfg->ret->inst_basereg = cfg->frame_reg;
1812 if (cfg->arch.omit_fp) {
1813 cfg->ret->inst_offset = offset;
1814 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1816 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1817 cfg->ret->inst_offset = - offset;
1821 g_assert_not_reached ();
1824 cfg->ret->dreg = cfg->ret->inst_c0;
1827 /* Allocate locals */
1828 if (!cfg->globalra) {
1829 offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1830 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1831 char *mname = mono_method_full_name (cfg->method, TRUE);
1832 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1833 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1838 if (locals_stack_align) {
1839 offset += (locals_stack_align - 1);
1840 offset &= ~(locals_stack_align - 1);
1842 if (cfg->arch.omit_fp) {
1843 cfg->locals_min_stack_offset = offset;
1844 cfg->locals_max_stack_offset = offset + locals_stack_size;
1846 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1847 cfg->locals_max_stack_offset = - offset;
1850 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1851 if (offsets [i] != -1) {
1852 MonoInst *ins = cfg->varinfo [i];
1853 ins->opcode = OP_REGOFFSET;
1854 ins->inst_basereg = cfg->frame_reg;
1855 if (cfg->arch.omit_fp)
1856 ins->inst_offset = (offset + offsets [i]);
1858 ins->inst_offset = - (offset + offsets [i]);
1859 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1862 offset += locals_stack_size;
1865 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1866 g_assert (!cfg->arch.omit_fp);
1867 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1868 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1871 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1872 ins = cfg->args [i];
1873 if (ins->opcode != OP_REGVAR) {
1874 ArgInfo *ainfo = &cinfo->args [i];
1875 gboolean inreg = TRUE;
1877 if (cfg->globalra) {
1878 /* The new allocator needs info about the original locations of the arguments */
1879 switch (ainfo->storage) {
1881 case ArgInFloatSSEReg:
1882 case ArgInDoubleSSEReg:
1883 ins->opcode = OP_REGVAR;
1884 ins->inst_c0 = ainfo->reg;
1887 g_assert (!cfg->arch.omit_fp);
1888 ins->opcode = OP_REGOFFSET;
1889 ins->inst_basereg = cfg->frame_reg;
1890 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1892 case ArgValuetypeInReg:
1893 ins->opcode = OP_REGOFFSET;
1894 ins->inst_basereg = cfg->frame_reg;
1895 /* These arguments are saved to the stack in the prolog */
1896 offset = ALIGN_TO (offset, sizeof(mgreg_t));
1897 if (cfg->arch.omit_fp) {
1898 ins->inst_offset = offset;
1899 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1901 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1902 ins->inst_offset = - offset;
1906 g_assert_not_reached ();
1912 /* FIXME: Allocate volatile arguments to registers */
1913 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1917 * Under AMD64, all registers used to pass arguments to functions
1918 * are volatile across calls.
1919 * FIXME: Optimize this.
1921 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg))
1924 ins->opcode = OP_REGOFFSET;
1926 switch (ainfo->storage) {
1928 case ArgInFloatSSEReg:
1929 case ArgInDoubleSSEReg:
1931 ins->opcode = OP_REGVAR;
1932 ins->dreg = ainfo->reg;
1936 g_assert (!cfg->arch.omit_fp);
1937 ins->opcode = OP_REGOFFSET;
1938 ins->inst_basereg = cfg->frame_reg;
1939 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1941 case ArgValuetypeInReg:
1943 case ArgValuetypeAddrInIReg: {
1945 g_assert (!cfg->arch.omit_fp);
1947 MONO_INST_NEW (cfg, indir, 0);
1948 indir->opcode = OP_REGOFFSET;
1949 if (ainfo->pair_storage [0] == ArgInIReg) {
1950 indir->inst_basereg = cfg->frame_reg;
1951 offset = ALIGN_TO (offset, sizeof (gpointer));
1952 offset += (sizeof (gpointer));
1953 indir->inst_offset = - offset;
1956 indir->inst_basereg = cfg->frame_reg;
1957 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
1960 ins->opcode = OP_VTARG_ADDR;
1961 ins->inst_left = indir;
1969 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg)) {
1970 ins->opcode = OP_REGOFFSET;
1971 ins->inst_basereg = cfg->frame_reg;
1972 /* These arguments are saved to the stack in the prolog */
1973 offset = ALIGN_TO (offset, sizeof(mgreg_t));
1974 if (cfg->arch.omit_fp) {
1975 ins->inst_offset = offset;
1976 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1977 // Arguments are yet supported by the stack map creation code
1978 //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
1980 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1981 ins->inst_offset = - offset;
1982 //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
1988 cfg->stack_offset = offset;
1992 mono_arch_create_vars (MonoCompile *cfg)
1994 MonoMethodSignature *sig;
1998 sig = mono_method_signature (cfg->method);
2000 if (!cfg->arch.cinfo)
2001 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
2002 cinfo = cfg->arch.cinfo;
2004 if (cinfo->ret.storage == ArgValuetypeInReg)
2005 cfg->ret_var_is_local = TRUE;
2007 sig_ret = mini_get_underlying_type (cfg, sig->ret);
2008 if ((cinfo->ret.storage != ArgValuetypeInReg) && MONO_TYPE_ISSTRUCT (sig_ret)) {
2009 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2010 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2011 printf ("vret_addr = ");
2012 mono_print_ins (cfg->vret_addr);
2016 if (cfg->gen_seq_points_debug_data) {
2019 if (cfg->compile_aot) {
2020 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2021 ins->flags |= MONO_INST_VOLATILE;
2022 cfg->arch.seq_point_info_var = ins;
2025 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2026 ins->flags |= MONO_INST_VOLATILE;
2027 cfg->arch.ss_trigger_page_var = ins;
2030 if (cfg->method->save_lmf)
2031 cfg->create_lmf_var = TRUE;
2033 if (cfg->method->save_lmf) {
2035 #if !defined(HOST_WIN32)
2036 if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
2037 cfg->lmf_ir_mono_lmf = TRUE;
2043 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2049 MONO_INST_NEW (cfg, ins, OP_MOVE);
2050 ins->dreg = mono_alloc_ireg_copy (cfg, tree->dreg);
2051 ins->sreg1 = tree->dreg;
2052 MONO_ADD_INS (cfg->cbb, ins);
2053 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2055 case ArgInFloatSSEReg:
2056 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
2057 ins->dreg = mono_alloc_freg (cfg);
2058 ins->sreg1 = tree->dreg;
2059 MONO_ADD_INS (cfg->cbb, ins);
2061 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2063 case ArgInDoubleSSEReg:
2064 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2065 ins->dreg = mono_alloc_freg (cfg);
2066 ins->sreg1 = tree->dreg;
2067 MONO_ADD_INS (cfg->cbb, ins);
2069 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2073 g_assert_not_reached ();
2078 arg_storage_to_load_membase (ArgStorage storage)
2082 #if defined(__mono_ilp32__)
2083 return OP_LOADI8_MEMBASE;
2085 return OP_LOAD_MEMBASE;
2087 case ArgInDoubleSSEReg:
2088 return OP_LOADR8_MEMBASE;
2089 case ArgInFloatSSEReg:
2090 return OP_LOADR4_MEMBASE;
2092 g_assert_not_reached ();
2099 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2101 MonoMethodSignature *tmp_sig;
2104 if (call->tail_call)
2107 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
2110 * mono_ArgIterator_Setup assumes the signature cookie is
2111 * passed first and all the arguments which were before it are
2112 * passed on the stack after the signature. So compensate by
2113 * passing a different signature.
2115 tmp_sig = mono_metadata_signature_dup_full (cfg->method->klass->image, call->signature);
2116 tmp_sig->param_count -= call->signature->sentinelpos;
2117 tmp_sig->sentinelpos = 0;
2118 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2120 sig_reg = mono_alloc_ireg (cfg);
2121 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2123 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, cinfo->sig_cookie.offset, sig_reg);
2127 static inline LLVMArgStorage
2128 arg_storage_to_llvm_arg_storage (MonoCompile *cfg, ArgStorage storage)
2132 return LLVMArgInIReg;
2136 g_assert_not_reached ();
2142 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2148 LLVMCallInfo *linfo;
2149 MonoType *t, *sig_ret;
2151 n = sig->param_count + sig->hasthis;
2152 sig_ret = mini_get_underlying_type (cfg, sig->ret);
2154 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
2156 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2159 * LLVM always uses the native ABI while we use our own ABI, the
2160 * only difference is the handling of vtypes:
2161 * - we only pass/receive them in registers in some cases, and only
2162 * in 1 or 2 integer registers.
2164 if (cinfo->ret.storage == ArgValuetypeInReg) {
2166 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2167 cfg->disable_llvm = TRUE;
2171 linfo->ret.storage = LLVMArgVtypeInReg;
2172 for (j = 0; j < 2; ++j)
2173 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
2176 if (MONO_TYPE_ISSTRUCT (sig_ret) && cinfo->ret.storage == ArgInIReg) {
2177 /* Vtype returned using a hidden argument */
2178 linfo->ret.storage = LLVMArgVtypeRetAddr;
2179 linfo->vret_arg_index = cinfo->vret_arg_index;
2182 for (i = 0; i < n; ++i) {
2183 ainfo = cinfo->args + i;
2185 if (i >= sig->hasthis)
2186 t = sig->params [i - sig->hasthis];
2188 t = &mono_defaults.int_class->byval_arg;
2190 linfo->args [i].storage = LLVMArgNone;
2192 switch (ainfo->storage) {
2194 linfo->args [i].storage = LLVMArgInIReg;
2196 case ArgInDoubleSSEReg:
2197 case ArgInFloatSSEReg:
2198 linfo->args [i].storage = LLVMArgInFPReg;
2201 if (MONO_TYPE_ISSTRUCT (t)) {
2202 linfo->args [i].storage = LLVMArgVtypeByVal;
2204 linfo->args [i].storage = LLVMArgInIReg;
2206 if (t->type == MONO_TYPE_R4)
2207 linfo->args [i].storage = LLVMArgInFPReg;
2208 else if (t->type == MONO_TYPE_R8)
2209 linfo->args [i].storage = LLVMArgInFPReg;
2213 case ArgValuetypeInReg:
2215 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2216 cfg->disable_llvm = TRUE;
2220 linfo->args [i].storage = LLVMArgVtypeInReg;
2221 for (j = 0; j < 2; ++j)
2222 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2225 cfg->exception_message = g_strdup ("ainfo->storage");
2226 cfg->disable_llvm = TRUE;
2236 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2239 MonoMethodSignature *sig;
2245 sig = call->signature;
2246 n = sig->param_count + sig->hasthis;
2248 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
2252 if (COMPILE_LLVM (cfg)) {
2253 /* We shouldn't be called in the llvm case */
2254 cfg->disable_llvm = TRUE;
2259 * Emit all arguments which are passed on the stack to prevent register
2260 * allocation problems.
2262 for (i = 0; i < n; ++i) {
2264 ainfo = cinfo->args + i;
2266 in = call->args [i];
2268 if (sig->hasthis && i == 0)
2269 t = &mono_defaults.object_class->byval_arg;
2271 t = sig->params [i - sig->hasthis];
2273 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call) {
2275 if (t->type == MONO_TYPE_R4)
2276 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2277 else if (t->type == MONO_TYPE_R8)
2278 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2280 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2282 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2284 if (cfg->compute_gc_maps) {
2287 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2293 * Emit all parameters passed in registers in non-reverse order for better readability
2294 * and to help the optimization in emit_prolog ().
2296 for (i = 0; i < n; ++i) {
2297 ainfo = cinfo->args + i;
2299 in = call->args [i];
2301 if (ainfo->storage == ArgInIReg)
2302 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2305 for (i = n - 1; i >= 0; --i) {
2306 ainfo = cinfo->args + i;
2308 in = call->args [i];
2310 switch (ainfo->storage) {
2314 case ArgInFloatSSEReg:
2315 case ArgInDoubleSSEReg:
2316 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2319 case ArgValuetypeInReg:
2320 case ArgValuetypeAddrInIReg:
2321 if (ainfo->storage == ArgOnStack && call->tail_call) {
2322 MonoInst *call_inst = (MonoInst*)call;
2323 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2324 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2325 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
2329 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
2330 size = sizeof (MonoTypedRef);
2331 align = sizeof (gpointer);
2335 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
2338 * Other backends use mono_type_stack_size (), but that
2339 * aligns the size to 8, which is larger than the size of
2340 * the source, leading to reads of invalid memory if the
2341 * source is at the end of address space.
2343 size = mono_class_value_size (in->klass, &align);
2346 g_assert (in->klass);
2348 if (ainfo->storage == ArgOnStack && size >= 10000) {
2349 /* Avoid asserts in emit_memcpy () */
2350 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
2351 cfg->exception_message = g_strdup_printf ("Passing an argument of size '%d'.", size);
2352 /* Continue normally */
2356 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2357 arg->sreg1 = in->dreg;
2358 arg->klass = in->klass;
2359 arg->backend.size = size;
2360 arg->inst_p0 = call;
2361 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2362 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2364 MONO_ADD_INS (cfg->cbb, arg);
2369 g_assert_not_reached ();
2372 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
2373 /* Emit the signature cookie just before the implicit arguments */
2374 emit_sig_cookie (cfg, call, cinfo);
2377 /* Handle the case where there are no implicit arguments */
2378 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2379 emit_sig_cookie (cfg, call, cinfo);
2381 sig_ret = mini_get_underlying_type (cfg, sig->ret);
2382 if (sig_ret && MONO_TYPE_ISSTRUCT (sig_ret)) {
2385 if (cinfo->ret.storage == ArgValuetypeInReg) {
2386 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2388 * Tell the JIT to use a more efficient calling convention: call using
2389 * OP_CALL, compute the result location after the call, and save the
2392 call->vret_in_reg = TRUE;
2394 * Nullify the instruction computing the vret addr to enable
2395 * future optimizations.
2398 NULLIFY_INS (call->vret_var);
2400 if (call->tail_call)
2403 * The valuetype is in RAX:RDX after the call, need to be copied to
2404 * the stack. Push the address here, so the call instruction can
2407 if (!cfg->arch.vret_addr_loc) {
2408 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2409 /* Prevent it from being register allocated or optimized away */
2410 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2413 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2417 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2418 vtarg->sreg1 = call->vret_var->dreg;
2419 vtarg->dreg = mono_alloc_preg (cfg);
2420 MONO_ADD_INS (cfg->cbb, vtarg);
2422 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2426 if (cfg->method->save_lmf) {
2427 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2428 MONO_ADD_INS (cfg->cbb, arg);
2431 call->stack_usage = cinfo->stack_usage;
2435 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2438 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2439 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2440 int size = ins->backend.size;
2442 if (ainfo->storage == ArgValuetypeInReg) {
2446 for (part = 0; part < 2; ++part) {
2447 if (ainfo->pair_storage [part] == ArgNone)
2450 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2451 load->inst_basereg = src->dreg;
2452 load->inst_offset = part * sizeof(mgreg_t);
2454 switch (ainfo->pair_storage [part]) {
2456 load->dreg = mono_alloc_ireg (cfg);
2458 case ArgInDoubleSSEReg:
2459 case ArgInFloatSSEReg:
2460 load->dreg = mono_alloc_freg (cfg);
2463 g_assert_not_reached ();
2465 MONO_ADD_INS (cfg->cbb, load);
2467 add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2469 } else if (ainfo->storage == ArgValuetypeAddrInIReg) {
2470 MonoInst *vtaddr, *load;
2471 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2473 MONO_INST_NEW (cfg, load, OP_LDADDR);
2474 cfg->has_indirection = TRUE;
2475 load->inst_p0 = vtaddr;
2476 vtaddr->flags |= MONO_INST_INDIRECT;
2477 load->type = STACK_MP;
2478 load->klass = vtaddr->klass;
2479 load->dreg = mono_alloc_ireg (cfg);
2480 MONO_ADD_INS (cfg->cbb, load);
2481 mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2483 if (ainfo->pair_storage [0] == ArgInIReg) {
2484 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2485 arg->dreg = mono_alloc_ireg (cfg);
2486 arg->sreg1 = load->dreg;
2488 MONO_ADD_INS (cfg->cbb, arg);
2489 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2491 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, load->dreg);
2495 int dreg = mono_alloc_ireg (cfg);
2497 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
2498 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, dreg);
2499 } else if (size <= 40) {
2500 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2502 // FIXME: Code growth
2503 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2506 if (cfg->compute_gc_maps) {
2508 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2514 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2516 MonoType *ret = mini_get_underlying_type (cfg, mono_method_signature (method)->ret);
2518 if (ret->type == MONO_TYPE_R4) {
2519 if (COMPILE_LLVM (cfg))
2520 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2522 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2524 } else if (ret->type == MONO_TYPE_R8) {
2525 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2529 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2532 #endif /* DISABLE_JIT */
2534 #define EMIT_COND_BRANCH(ins,cond,sign) \
2535 if (ins->inst_true_bb->native_offset) { \
2536 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2538 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2539 if ((cfg->opt & MONO_OPT_BRANCH) && \
2540 x86_is_imm8 (ins->inst_true_bb->max_offset - offset)) \
2541 x86_branch8 (code, cond, 0, sign); \
2543 x86_branch32 (code, cond, 0, sign); \
2547 MonoMethodSignature *sig;
2552 dyn_call_supported (MonoMethodSignature *sig, CallInfo *cinfo)
2560 switch (cinfo->ret.storage) {
2564 case ArgValuetypeInReg: {
2565 ArgInfo *ainfo = &cinfo->ret;
2567 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2569 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2577 for (i = 0; i < cinfo->nargs; ++i) {
2578 ArgInfo *ainfo = &cinfo->args [i];
2579 switch (ainfo->storage) {
2582 case ArgValuetypeInReg:
2583 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2585 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2597 * mono_arch_dyn_call_prepare:
2599 * Return a pointer to an arch-specific structure which contains information
2600 * needed by mono_arch_get_dyn_call_args (). Return NULL if OP_DYN_CALL is not
2601 * supported for SIG.
2602 * This function is equivalent to ffi_prep_cif in libffi.
2605 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2607 ArchDynCallInfo *info;
2610 cinfo = get_call_info (NULL, NULL, sig);
2612 if (!dyn_call_supported (sig, cinfo)) {
2617 info = g_new0 (ArchDynCallInfo, 1);
2618 // FIXME: Preprocess the info to speed up get_dyn_call_args ().
2620 info->cinfo = cinfo;
2622 return (MonoDynCallInfo*)info;
2626 * mono_arch_dyn_call_free:
2628 * Free a MonoDynCallInfo structure.
2631 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2633 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2635 g_free (ainfo->cinfo);
2639 #if !defined(__native_client__)
2640 #define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
2641 #define GREG_TO_PTR(greg) (gpointer)(greg)
2643 /* Correctly handle casts to/from 32-bit pointers without compiler warnings */
2644 #define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
2645 #define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
2649 * mono_arch_get_start_dyn_call:
2651 * Convert the arguments ARGS to a format which can be passed to OP_DYN_CALL, and
2652 * store the result into BUF.
2653 * ARGS should be an array of pointers pointing to the arguments.
2654 * RET should point to a memory buffer large enought to hold the result of the
2656 * This function should be as fast as possible, any work which does not depend
2657 * on the actual values of the arguments should be done in
2658 * mono_arch_dyn_call_prepare ().
2659 * start_dyn_call + OP_DYN_CALL + finish_dyn_call is equivalent to ffi_call in
2663 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2665 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2666 DynCallArgs *p = (DynCallArgs*)buf;
2667 int arg_index, greg, i, pindex;
2668 MonoMethodSignature *sig = dinfo->sig;
2670 g_assert (buf_len >= sizeof (DynCallArgs));
2679 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2680 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2685 if (dinfo->cinfo->vtype_retaddr)
2686 p->regs [greg ++] = PTR_TO_GREG(ret);
2688 for (i = pindex; i < sig->param_count; i++) {
2689 MonoType *t = mono_type_get_underlying_type (sig->params [i]);
2690 gpointer *arg = args [arg_index ++];
2693 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2698 case MONO_TYPE_STRING:
2699 case MONO_TYPE_CLASS:
2700 case MONO_TYPE_ARRAY:
2701 case MONO_TYPE_SZARRAY:
2702 case MONO_TYPE_OBJECT:
2706 #if !defined(__mono_ilp32__)
2710 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2711 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2713 #if defined(__mono_ilp32__)
2716 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2717 p->regs [greg ++] = *(guint64*)(arg);
2720 case MONO_TYPE_BOOLEAN:
2722 p->regs [greg ++] = *(guint8*)(arg);
2725 p->regs [greg ++] = *(gint8*)(arg);
2728 p->regs [greg ++] = *(gint16*)(arg);
2731 case MONO_TYPE_CHAR:
2732 p->regs [greg ++] = *(guint16*)(arg);
2735 p->regs [greg ++] = *(gint32*)(arg);
2738 p->regs [greg ++] = *(guint32*)(arg);
2740 case MONO_TYPE_GENERICINST:
2741 if (MONO_TYPE_IS_REFERENCE (t)) {
2742 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2747 case MONO_TYPE_VALUETYPE: {
2748 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2750 g_assert (ainfo->storage == ArgValuetypeInReg);
2751 if (ainfo->pair_storage [0] != ArgNone) {
2752 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2753 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2755 if (ainfo->pair_storage [1] != ArgNone) {
2756 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2757 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2762 g_assert_not_reached ();
2766 g_assert (greg <= PARAM_REGS);
2770 * mono_arch_finish_dyn_call:
2772 * Store the result of a dyn call into the return value buffer passed to
2773 * start_dyn_call ().
2774 * This function should be as fast as possible, any work which does not depend
2775 * on the actual values of the arguments should be done in
2776 * mono_arch_dyn_call_prepare ().
2779 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2781 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2782 MonoMethodSignature *sig = dinfo->sig;
2783 guint8 *ret = ((DynCallArgs*)buf)->ret;
2784 mgreg_t res = ((DynCallArgs*)buf)->res;
2785 MonoType *sig_ret = mono_type_get_underlying_type (sig->ret);
2787 switch (sig_ret->type) {
2788 case MONO_TYPE_VOID:
2789 *(gpointer*)ret = NULL;
2791 case MONO_TYPE_STRING:
2792 case MONO_TYPE_CLASS:
2793 case MONO_TYPE_ARRAY:
2794 case MONO_TYPE_SZARRAY:
2795 case MONO_TYPE_OBJECT:
2799 *(gpointer*)ret = GREG_TO_PTR(res);
2805 case MONO_TYPE_BOOLEAN:
2806 *(guint8*)ret = res;
2809 *(gint16*)ret = res;
2812 case MONO_TYPE_CHAR:
2813 *(guint16*)ret = res;
2816 *(gint32*)ret = res;
2819 *(guint32*)ret = res;
2822 *(gint64*)ret = res;
2825 *(guint64*)ret = res;
2827 case MONO_TYPE_GENERICINST:
2828 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
2829 *(gpointer*)ret = GREG_TO_PTR(res);
2834 case MONO_TYPE_VALUETYPE:
2835 if (dinfo->cinfo->vtype_retaddr) {
2838 ArgInfo *ainfo = &dinfo->cinfo->ret;
2840 g_assert (ainfo->storage == ArgValuetypeInReg);
2842 if (ainfo->pair_storage [0] != ArgNone) {
2843 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2844 ((mgreg_t*)ret)[0] = res;
2847 g_assert (ainfo->pair_storage [1] == ArgNone);
2851 g_assert_not_reached ();
2855 /* emit an exception if condition is fail */
2856 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2858 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2859 if (tins == NULL) { \
2860 mono_add_patch_info (cfg, code - cfg->native_code, \
2861 MONO_PATCH_INFO_EXC, exc_name); \
2862 x86_branch32 (code, cond, 0, signed); \
2864 EMIT_COND_BRANCH (tins, cond, signed); \
2868 #define EMIT_FPCOMPARE(code) do { \
2869 amd64_fcompp (code); \
2870 amd64_fnstsw (code); \
2873 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
2874 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
2875 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
2876 amd64_ ##op (code); \
2877 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
2878 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
2882 emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
2884 gboolean no_patch = FALSE;
2887 * FIXME: Add support for thunks
2890 gboolean near_call = FALSE;
2893 * Indirect calls are expensive so try to make a near call if possible.
2894 * The caller memory is allocated by the code manager so it is
2895 * guaranteed to be at a 32 bit offset.
2898 if (patch_type != MONO_PATCH_INFO_ABS) {
2899 /* The target is in memory allocated using the code manager */
2902 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
2903 if (((MonoMethod*)data)->klass->image->aot_module)
2904 /* The callee might be an AOT method */
2906 if (((MonoMethod*)data)->dynamic)
2907 /* The target is in malloc-ed memory */
2911 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
2913 * The call might go directly to a native function without
2916 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (data);
2918 gconstpointer target = mono_icall_get_wrapper (mi);
2919 if ((((guint64)target) >> 32) != 0)
2925 MonoJumpInfo *jinfo = NULL;
2927 if (cfg->abs_patches)
2928 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
2930 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2931 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
2932 if (mi && (((guint64)mi->func) >> 32) == 0)
2937 * This is not really an optimization, but required because the
2938 * generic class init trampolines use R11 to pass the vtable.
2943 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
2945 if (info->func == info->wrapper) {
2947 if ((((guint64)info->func) >> 32) == 0)
2951 /* See the comment in mono_codegen () */
2952 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
2956 else if ((((guint64)data) >> 32) == 0) {
2963 if (cfg->method->dynamic)
2964 /* These methods are allocated using malloc */
2967 #ifdef MONO_ARCH_NOMAP32BIT
2970 #if defined(__native_client__)
2971 /* Always use near_call == TRUE for Native Client */
2974 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
2975 if (optimize_for_xen)
2978 if (cfg->compile_aot) {
2985 * Align the call displacement to an address divisible by 4 so it does
2986 * not span cache lines. This is required for code patching to work on SMP
2989 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
2990 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
2991 amd64_padding (code, pad_size);
2993 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2994 amd64_call_code (code, 0);
2997 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
2998 amd64_set_reg_template (code, GP_SCRATCH_REG);
2999 amd64_call_reg (code, GP_SCRATCH_REG);
3006 static inline guint8*
3007 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
3010 if (win64_adjust_stack)
3011 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
3013 code = emit_call_body (cfg, code, patch_type, data);
3015 if (win64_adjust_stack)
3016 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
3023 store_membase_imm_to_store_membase_reg (int opcode)
3026 case OP_STORE_MEMBASE_IMM:
3027 return OP_STORE_MEMBASE_REG;
3028 case OP_STOREI4_MEMBASE_IMM:
3029 return OP_STOREI4_MEMBASE_REG;
3030 case OP_STOREI8_MEMBASE_IMM:
3031 return OP_STOREI8_MEMBASE_REG;
3039 #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)))
3042 * mono_arch_peephole_pass_1:
3044 * Perform peephole opts which should/can be performed before local regalloc
3047 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3051 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3052 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3054 switch (ins->opcode) {
3058 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
3060 * X86_LEA is like ADD, but doesn't have the
3061 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
3062 * its operand to 64 bit.
3064 ins->opcode = OP_X86_LEA_MEMBASE;
3065 ins->inst_basereg = ins->sreg1;
3070 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3074 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3075 * the latter has length 2-3 instead of 6 (reverse constant
3076 * propagation). These instruction sequences are very common
3077 * in the initlocals bblock.
3079 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3080 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)) {
3081 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3082 ins2->sreg1 = ins->dreg;
3083 } 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)) {
3085 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3088 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3096 case OP_COMPARE_IMM:
3097 case OP_LCOMPARE_IMM:
3098 /* OP_COMPARE_IMM (reg, 0)
3100 * OP_AMD64_TEST_NULL (reg)
3103 ins->opcode = OP_AMD64_TEST_NULL;
3105 case OP_ICOMPARE_IMM:
3107 ins->opcode = OP_X86_TEST_NULL;
3109 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3111 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3112 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3114 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3115 * OP_COMPARE_IMM reg, imm
3117 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3119 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3120 ins->inst_basereg == last_ins->inst_destbasereg &&
3121 ins->inst_offset == last_ins->inst_offset) {
3122 ins->opcode = OP_ICOMPARE_IMM;
3123 ins->sreg1 = last_ins->sreg1;
3125 /* check if we can remove cmp reg,0 with test null */
3127 ins->opcode = OP_X86_TEST_NULL;
3133 mono_peephole_ins (bb, ins);
3138 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3142 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3143 switch (ins->opcode) {
3146 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3147 /* reg = 0 -> XOR (reg, reg) */
3148 /* XOR sets cflags on x86, so we cant do it always */
3149 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3150 ins->opcode = OP_LXOR;
3151 ins->sreg1 = ins->dreg;
3152 ins->sreg2 = ins->dreg;
3160 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3161 * 0 result into 64 bits.
3163 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3164 ins->opcode = OP_IXOR;
3168 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3172 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3173 * the latter has length 2-3 instead of 6 (reverse constant
3174 * propagation). These instruction sequences are very common
3175 * in the initlocals bblock.
3177 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3178 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)) {
3179 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3180 ins2->sreg1 = ins->dreg;
3181 } 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)) {
3183 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3186 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3195 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3196 ins->opcode = OP_X86_INC_REG;
3199 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3200 ins->opcode = OP_X86_DEC_REG;
3204 mono_peephole_ins (bb, ins);
3208 #define NEW_INS(cfg,ins,dest,op) do { \
3209 MONO_INST_NEW ((cfg), (dest), (op)); \
3210 (dest)->cil_code = (ins)->cil_code; \
3211 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3215 * mono_arch_lowering_pass:
3217 * Converts complex opcodes into simpler ones so that each IR instruction
3218 * corresponds to one machine instruction.
3221 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3223 MonoInst *ins, *n, *temp;
3226 * FIXME: Need to add more instructions, but the current machine
3227 * description can't model some parts of the composite instructions like
3230 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3231 switch (ins->opcode) {
3235 case OP_IDIV_UN_IMM:
3236 case OP_IREM_UN_IMM:
3239 mono_decompose_op_imm (cfg, bb, ins);
3241 case OP_COMPARE_IMM:
3242 case OP_LCOMPARE_IMM:
3243 if (!amd64_is_imm32 (ins->inst_imm)) {
3244 NEW_INS (cfg, ins, temp, OP_I8CONST);
3245 temp->inst_c0 = ins->inst_imm;
3246 temp->dreg = mono_alloc_ireg (cfg);
3247 ins->opcode = OP_COMPARE;
3248 ins->sreg2 = temp->dreg;
3251 #ifndef __mono_ilp32__
3252 case OP_LOAD_MEMBASE:
3254 case OP_LOADI8_MEMBASE:
3255 #ifndef __native_client_codegen__
3256 /* Don't generate memindex opcodes (to simplify */
3257 /* read sandboxing) */
3258 if (!amd64_is_imm32 (ins->inst_offset)) {
3259 NEW_INS (cfg, ins, temp, OP_I8CONST);
3260 temp->inst_c0 = ins->inst_offset;
3261 temp->dreg = mono_alloc_ireg (cfg);
3262 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3263 ins->inst_indexreg = temp->dreg;
3267 #ifndef __mono_ilp32__
3268 case OP_STORE_MEMBASE_IMM:
3270 case OP_STOREI8_MEMBASE_IMM:
3271 if (!amd64_is_imm32 (ins->inst_imm)) {
3272 NEW_INS (cfg, ins, temp, OP_I8CONST);
3273 temp->inst_c0 = ins->inst_imm;
3274 temp->dreg = mono_alloc_ireg (cfg);
3275 ins->opcode = OP_STOREI8_MEMBASE_REG;
3276 ins->sreg1 = temp->dreg;
3279 #ifdef MONO_ARCH_SIMD_INTRINSICS
3280 case OP_EXPAND_I1: {
3281 int temp_reg1 = mono_alloc_ireg (cfg);
3282 int temp_reg2 = mono_alloc_ireg (cfg);
3283 int original_reg = ins->sreg1;
3285 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3286 temp->sreg1 = original_reg;
3287 temp->dreg = temp_reg1;
3289 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3290 temp->sreg1 = temp_reg1;
3291 temp->dreg = temp_reg2;
3294 NEW_INS (cfg, ins, temp, OP_LOR);
3295 temp->sreg1 = temp->dreg = temp_reg2;
3296 temp->sreg2 = temp_reg1;
3298 ins->opcode = OP_EXPAND_I2;
3299 ins->sreg1 = temp_reg2;
3308 bb->max_vreg = cfg->next_vreg;
3312 branch_cc_table [] = {
3313 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3314 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3315 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3318 /* Maps CMP_... constants to X86_CC_... constants */
3321 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3322 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3326 cc_signed_table [] = {
3327 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3328 FALSE, FALSE, FALSE, FALSE
3331 /*#include "cprop.c"*/
3333 static unsigned char*
3334 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3337 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3339 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3342 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3344 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3348 static unsigned char*
3349 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3351 int sreg = tree->sreg1;
3352 int need_touch = FALSE;
3354 #if defined(HOST_WIN32)
3356 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3357 if (!tree->flags & MONO_INST_INIT)
3366 * If requested stack size is larger than one page,
3367 * perform stack-touch operation
3370 * Generate stack probe code.
3371 * Under Windows, it is necessary to allocate one page at a time,
3372 * "touching" stack after each successful sub-allocation. This is
3373 * because of the way stack growth is implemented - there is a
3374 * guard page before the lowest stack page that is currently commited.
3375 * Stack normally grows sequentially so OS traps access to the
3376 * guard page and commits more pages when needed.
3378 amd64_test_reg_imm (code, sreg, ~0xFFF);
3379 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3381 br[2] = code; /* loop */
3382 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3383 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3384 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3385 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3386 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3387 amd64_patch (br[3], br[2]);
3388 amd64_test_reg_reg (code, sreg, sreg);
3389 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3390 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3392 br[1] = code; x86_jump8 (code, 0);
3394 amd64_patch (br[0], code);
3395 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3396 amd64_patch (br[1], code);
3397 amd64_patch (br[4], code);
3400 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3402 if (tree->flags & MONO_INST_INIT) {
3404 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3405 amd64_push_reg (code, AMD64_RAX);
3408 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3409 amd64_push_reg (code, AMD64_RCX);
3412 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3413 amd64_push_reg (code, AMD64_RDI);
3417 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3418 if (sreg != AMD64_RCX)
3419 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3420 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3422 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3423 if (cfg->param_area)
3424 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3426 #if defined(__default_codegen__)
3427 amd64_prefix (code, X86_REP_PREFIX);
3429 #elif defined(__native_client_codegen__)
3430 /* NaCl stos pseudo-instruction */
3431 amd64_codegen_pre(code);
3432 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3433 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3434 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3435 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3436 amd64_prefix (code, X86_REP_PREFIX);
3438 amd64_codegen_post(code);
3439 #endif /* __native_client_codegen__ */
3441 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3442 amd64_pop_reg (code, AMD64_RDI);
3443 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3444 amd64_pop_reg (code, AMD64_RCX);
3445 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3446 amd64_pop_reg (code, AMD64_RAX);
3452 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3457 /* Move return value to the target register */
3458 /* FIXME: do this in the local reg allocator */
3459 switch (ins->opcode) {
3462 case OP_CALL_MEMBASE:
3465 case OP_LCALL_MEMBASE:
3466 g_assert (ins->dreg == AMD64_RAX);
3470 case OP_FCALL_MEMBASE:
3471 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
3472 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3475 if (ins->dreg != AMD64_XMM0)
3476 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3481 case OP_RCALL_MEMBASE:
3482 if (ins->dreg != AMD64_XMM0)
3483 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3487 case OP_VCALL_MEMBASE:
3490 case OP_VCALL2_MEMBASE:
3491 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, ((MonoCallInst*)ins)->signature);
3492 if (cinfo->ret.storage == ArgValuetypeInReg) {
3493 MonoInst *loc = cfg->arch.vret_addr_loc;
3495 /* Load the destination address */
3496 g_assert (loc->opcode == OP_REGOFFSET);
3497 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3499 for (quad = 0; quad < 2; quad ++) {
3500 switch (cinfo->ret.pair_storage [quad]) {
3502 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3504 case ArgInFloatSSEReg:
3505 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3507 case ArgInDoubleSSEReg:
3508 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3523 #endif /* DISABLE_JIT */
3526 static int tls_gs_offset;
3530 mono_amd64_have_tls_get (void)
3533 static gboolean have_tls_get = FALSE;
3534 static gboolean inited = FALSE;
3538 return have_tls_get;
3540 ins = (guint8*)pthread_getspecific;
3543 * We're looking for these two instructions:
3545 * mov %gs:[offset](,%rdi,8),%rax
3548 have_tls_get = ins [0] == 0x65 &&
3560 tls_gs_offset = ins[5];
3562 return have_tls_get;
3563 #elif defined(TARGET_ANDROID)
3571 mono_amd64_get_tls_gs_offset (void)
3574 return tls_gs_offset;
3576 g_assert_not_reached ();
3582 * mono_amd64_emit_tls_get:
3583 * @code: buffer to store code to
3584 * @dreg: hard register where to place the result
3585 * @tls_offset: offset info
3587 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3588 * the dreg register the item in the thread local storage identified
3591 * Returns: a pointer to the end of the stored code
3594 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3597 if (tls_offset < 64) {
3598 x86_prefix (code, X86_GS_PREFIX);
3599 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3603 g_assert (tls_offset < 0x440);
3604 /* Load TEB->TlsExpansionSlots */
3605 x86_prefix (code, X86_GS_PREFIX);
3606 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3607 amd64_test_reg_reg (code, dreg, dreg);
3609 amd64_branch (code, X86_CC_EQ, code, TRUE);
3610 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3611 amd64_patch (buf [0], code);
3613 #elif defined(__APPLE__)
3614 x86_prefix (code, X86_GS_PREFIX);
3615 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3617 if (optimize_for_xen) {
3618 x86_prefix (code, X86_FS_PREFIX);
3619 amd64_mov_reg_mem (code, dreg, 0, 8);
3620 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3622 x86_prefix (code, X86_FS_PREFIX);
3623 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3630 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3632 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3634 if (dreg != offset_reg)
3635 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3636 amd64_prefix (code, X86_GS_PREFIX);
3637 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3638 #elif defined(__linux__)
3641 if (dreg == offset_reg) {
3642 /* Use a temporary reg by saving it to the redzone */
3643 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3644 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3645 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3646 offset_reg = tmpreg;
3648 x86_prefix (code, X86_FS_PREFIX);
3649 amd64_mov_reg_mem (code, dreg, 0, 8);
3650 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3652 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3654 g_assert_not_reached ();
3660 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3663 g_assert_not_reached ();
3664 #elif defined(__APPLE__)
3665 x86_prefix (code, X86_GS_PREFIX);
3666 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3668 g_assert (!optimize_for_xen);
3669 x86_prefix (code, X86_FS_PREFIX);
3670 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3676 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3678 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3680 g_assert_not_reached ();
3681 #elif defined(__APPLE__)
3682 x86_prefix (code, X86_GS_PREFIX);
3683 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3685 x86_prefix (code, X86_FS_PREFIX);
3686 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3692 * mono_arch_translate_tls_offset:
3694 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3697 mono_arch_translate_tls_offset (int offset)
3700 return tls_gs_offset + (offset * 8);
3709 * Emit code to initialize an LMF structure at LMF_OFFSET.
3712 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3715 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3718 * sp is saved right before calls but we need to save it here too so
3719 * async stack walks would work.
3721 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3723 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3724 if (cfg->arch.omit_fp && cfa_offset != -1)
3725 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3727 /* These can't contain refs */
3728 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3729 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3730 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3731 /* These are handled automatically by the stack marking code */
3732 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3737 #define REAL_PRINT_REG(text,reg) \
3738 mono_assert (reg >= 0); \
3739 amd64_push_reg (code, AMD64_RAX); \
3740 amd64_push_reg (code, AMD64_RDX); \
3741 amd64_push_reg (code, AMD64_RCX); \
3742 amd64_push_reg (code, reg); \
3743 amd64_push_imm (code, reg); \
3744 amd64_push_imm (code, text " %d %p\n"); \
3745 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
3746 amd64_call_reg (code, AMD64_RAX); \
3747 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
3748 amd64_pop_reg (code, AMD64_RCX); \
3749 amd64_pop_reg (code, AMD64_RDX); \
3750 amd64_pop_reg (code, AMD64_RAX);
3752 /* benchmark and set based on cpu */
3753 #define LOOP_ALIGNMENT 8
3754 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3758 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3763 guint8 *code = cfg->native_code + cfg->code_len;
3766 /* Fix max_offset estimate for each successor bb */
3767 if (cfg->opt & MONO_OPT_BRANCH) {
3768 int current_offset = cfg->code_len;
3769 MonoBasicBlock *current_bb;
3770 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3771 current_bb->max_offset = current_offset;
3772 current_offset += current_bb->max_length;
3776 if (cfg->opt & MONO_OPT_LOOP) {
3777 int pad, align = LOOP_ALIGNMENT;
3778 /* set alignment depending on cpu */
3779 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3781 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3782 amd64_padding (code, pad);
3783 cfg->code_len += pad;
3784 bb->native_offset = cfg->code_len;
3788 #if defined(__native_client_codegen__)
3789 /* For Native Client, all indirect call/jump targets must be */
3790 /* 32-byte aligned. Exception handler blocks are jumped to */
3791 /* indirectly as well. */
3792 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
3793 (bb->flags & BB_EXCEPTION_HANDLER);
3795 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
3796 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
3797 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
3798 cfg->code_len += pad;
3799 bb->native_offset = cfg->code_len;
3801 #endif /*__native_client_codegen__*/
3803 if (cfg->verbose_level > 2)
3804 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3806 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
3807 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3808 g_assert (!cfg->compile_aot);
3810 cov->data [bb->dfn].cil_code = bb->cil_code;
3811 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3812 /* this is not thread save, but good enough */
3813 amd64_inc_membase (code, AMD64_R11, 0);
3816 offset = code - cfg->native_code;
3818 mono_debug_open_block (cfg, bb, offset);
3820 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)
3821 x86_breakpoint (code);
3823 MONO_BB_FOR_EACH_INS (bb, ins) {
3824 offset = code - cfg->native_code;
3826 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3828 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
3830 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
3831 cfg->code_size *= 2;
3832 cfg->native_code = mono_realloc_native_code(cfg);
3833 code = cfg->native_code + offset;
3834 cfg->stat_code_reallocs++;
3837 if (cfg->debug_info)
3838 mono_debug_record_line_number (cfg, ins, offset);
3840 switch (ins->opcode) {
3842 amd64_mul_reg (code, ins->sreg2, TRUE);
3845 amd64_mul_reg (code, ins->sreg2, FALSE);
3847 case OP_X86_SETEQ_MEMBASE:
3848 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3850 case OP_STOREI1_MEMBASE_IMM:
3851 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3853 case OP_STOREI2_MEMBASE_IMM:
3854 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
3856 case OP_STOREI4_MEMBASE_IMM:
3857 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
3859 case OP_STOREI1_MEMBASE_REG:
3860 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
3862 case OP_STOREI2_MEMBASE_REG:
3863 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
3865 /* In AMD64 NaCl, pointers are 4 bytes, */
3866 /* so STORE_* != STOREI8_*. Likewise below. */
3867 case OP_STORE_MEMBASE_REG:
3868 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
3870 case OP_STOREI8_MEMBASE_REG:
3871 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
3873 case OP_STOREI4_MEMBASE_REG:
3874 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
3876 case OP_STORE_MEMBASE_IMM:
3877 #ifndef __native_client_codegen__
3878 /* In NaCl, this could be a PCONST type, which could */
3879 /* mean a pointer type was copied directly into the */
3880 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
3881 /* the value would be 0x00000000FFFFFFFF which is */
3882 /* not proper for an imm32 unless you cast it. */
3883 g_assert (amd64_is_imm32 (ins->inst_imm));
3885 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
3887 case OP_STOREI8_MEMBASE_IMM:
3888 g_assert (amd64_is_imm32 (ins->inst_imm));
3889 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
3892 #ifdef __mono_ilp32__
3893 /* In ILP32, pointers are 4 bytes, so separate these */
3894 /* cases, use literal 8 below where we really want 8 */
3895 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3896 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
3900 // FIXME: Decompose this earlier
3901 if (amd64_is_imm32 (ins->inst_imm))
3902 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
3904 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3905 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
3909 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3910 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
3913 // FIXME: Decompose this earlier
3914 if (amd64_is_imm32 (ins->inst_imm))
3915 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
3917 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3918 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3922 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3923 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
3926 /* For NaCl, pointers are 4 bytes, so separate these */
3927 /* cases, use literal 8 below where we really want 8 */
3928 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3929 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
3931 case OP_LOAD_MEMBASE:
3932 g_assert (amd64_is_imm32 (ins->inst_offset));
3933 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
3935 case OP_LOADI8_MEMBASE:
3936 /* Use literal 8 instead of sizeof pointer or */
3937 /* register, we really want 8 for this opcode */
3938 g_assert (amd64_is_imm32 (ins->inst_offset));
3939 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
3941 case OP_LOADI4_MEMBASE:
3942 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3944 case OP_LOADU4_MEMBASE:
3945 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
3947 case OP_LOADU1_MEMBASE:
3948 /* The cpu zero extends the result into 64 bits */
3949 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
3951 case OP_LOADI1_MEMBASE:
3952 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
3954 case OP_LOADU2_MEMBASE:
3955 /* The cpu zero extends the result into 64 bits */
3956 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
3958 case OP_LOADI2_MEMBASE:
3959 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
3961 case OP_AMD64_LOADI8_MEMINDEX:
3962 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
3964 case OP_LCONV_TO_I1:
3965 case OP_ICONV_TO_I1:
3967 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3969 case OP_LCONV_TO_I2:
3970 case OP_ICONV_TO_I2:
3972 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
3974 case OP_LCONV_TO_U1:
3975 case OP_ICONV_TO_U1:
3976 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
3978 case OP_LCONV_TO_U2:
3979 case OP_ICONV_TO_U2:
3980 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
3983 /* Clean out the upper word */
3984 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
3987 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
3991 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
3993 case OP_COMPARE_IMM:
3994 #if defined(__mono_ilp32__)
3995 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
3996 g_assert (amd64_is_imm32 (ins->inst_imm));
3997 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4000 case OP_LCOMPARE_IMM:
4001 g_assert (amd64_is_imm32 (ins->inst_imm));
4002 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
4004 case OP_X86_COMPARE_REG_MEMBASE:
4005 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
4007 case OP_X86_TEST_NULL:
4008 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
4010 case OP_AMD64_TEST_NULL:
4011 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4014 case OP_X86_ADD_REG_MEMBASE:
4015 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4017 case OP_X86_SUB_REG_MEMBASE:
4018 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4020 case OP_X86_AND_REG_MEMBASE:
4021 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4023 case OP_X86_OR_REG_MEMBASE:
4024 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4026 case OP_X86_XOR_REG_MEMBASE:
4027 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4030 case OP_X86_ADD_MEMBASE_IMM:
4031 /* FIXME: Make a 64 version too */
4032 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4034 case OP_X86_SUB_MEMBASE_IMM:
4035 g_assert (amd64_is_imm32 (ins->inst_imm));
4036 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4038 case OP_X86_AND_MEMBASE_IMM:
4039 g_assert (amd64_is_imm32 (ins->inst_imm));
4040 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4042 case OP_X86_OR_MEMBASE_IMM:
4043 g_assert (amd64_is_imm32 (ins->inst_imm));
4044 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4046 case OP_X86_XOR_MEMBASE_IMM:
4047 g_assert (amd64_is_imm32 (ins->inst_imm));
4048 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4050 case OP_X86_ADD_MEMBASE_REG:
4051 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4053 case OP_X86_SUB_MEMBASE_REG:
4054 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4056 case OP_X86_AND_MEMBASE_REG:
4057 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4059 case OP_X86_OR_MEMBASE_REG:
4060 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4062 case OP_X86_XOR_MEMBASE_REG:
4063 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4065 case OP_X86_INC_MEMBASE:
4066 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4068 case OP_X86_INC_REG:
4069 amd64_inc_reg_size (code, ins->dreg, 4);
4071 case OP_X86_DEC_MEMBASE:
4072 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4074 case OP_X86_DEC_REG:
4075 amd64_dec_reg_size (code, ins->dreg, 4);
4077 case OP_X86_MUL_REG_MEMBASE:
4078 case OP_X86_MUL_MEMBASE_REG:
4079 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4081 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4082 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4084 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4085 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4087 case OP_AMD64_COMPARE_MEMBASE_REG:
4088 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4090 case OP_AMD64_COMPARE_MEMBASE_IMM:
4091 g_assert (amd64_is_imm32 (ins->inst_imm));
4092 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4094 case OP_X86_COMPARE_MEMBASE8_IMM:
4095 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4097 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4098 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4100 case OP_AMD64_COMPARE_REG_MEMBASE:
4101 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4104 case OP_AMD64_ADD_REG_MEMBASE:
4105 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4107 case OP_AMD64_SUB_REG_MEMBASE:
4108 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4110 case OP_AMD64_AND_REG_MEMBASE:
4111 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4113 case OP_AMD64_OR_REG_MEMBASE:
4114 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4116 case OP_AMD64_XOR_REG_MEMBASE:
4117 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4120 case OP_AMD64_ADD_MEMBASE_REG:
4121 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4123 case OP_AMD64_SUB_MEMBASE_REG:
4124 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4126 case OP_AMD64_AND_MEMBASE_REG:
4127 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4129 case OP_AMD64_OR_MEMBASE_REG:
4130 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4132 case OP_AMD64_XOR_MEMBASE_REG:
4133 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4136 case OP_AMD64_ADD_MEMBASE_IMM:
4137 g_assert (amd64_is_imm32 (ins->inst_imm));
4138 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4140 case OP_AMD64_SUB_MEMBASE_IMM:
4141 g_assert (amd64_is_imm32 (ins->inst_imm));
4142 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4144 case OP_AMD64_AND_MEMBASE_IMM:
4145 g_assert (amd64_is_imm32 (ins->inst_imm));
4146 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4148 case OP_AMD64_OR_MEMBASE_IMM:
4149 g_assert (amd64_is_imm32 (ins->inst_imm));
4150 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4152 case OP_AMD64_XOR_MEMBASE_IMM:
4153 g_assert (amd64_is_imm32 (ins->inst_imm));
4154 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4158 amd64_breakpoint (code);
4160 case OP_RELAXED_NOP:
4161 x86_prefix (code, X86_REP_PREFIX);
4169 case OP_DUMMY_STORE:
4170 case OP_DUMMY_ICONST:
4171 case OP_DUMMY_R8CONST:
4172 case OP_NOT_REACHED:
4175 case OP_IL_SEQ_POINT:
4176 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4178 case OP_SEQ_POINT: {
4182 * Read from the single stepping trigger page. This will cause a
4183 * SIGSEGV when single stepping is enabled.
4184 * We do this _before_ the breakpoint, so single stepping after
4185 * a breakpoint is hit will step to the next IL offset.
4187 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4188 MonoInst *var = cfg->arch.ss_trigger_page_var;
4190 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4191 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4);
4195 * This is the address which is saved in seq points,
4197 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4199 if (cfg->compile_aot) {
4200 guint32 offset = code - cfg->native_code;
4202 MonoInst *info_var = cfg->arch.seq_point_info_var;
4205 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4206 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4207 /* Load the info->bp_addrs [offset], which is either a valid address or the address of a trigger page */
4208 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4209 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4212 * A placeholder for a possible breakpoint inserted by
4213 * mono_arch_set_breakpoint ().
4215 for (i = 0; i < breakpoint_size; ++i)
4219 * Add an additional nop so skipping the bp doesn't cause the ip to point
4220 * to another IL offset.
4228 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4231 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4235 g_assert (amd64_is_imm32 (ins->inst_imm));
4236 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4239 g_assert (amd64_is_imm32 (ins->inst_imm));
4240 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4245 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4248 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4252 g_assert (amd64_is_imm32 (ins->inst_imm));
4253 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4256 g_assert (amd64_is_imm32 (ins->inst_imm));
4257 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4260 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4264 g_assert (amd64_is_imm32 (ins->inst_imm));
4265 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4268 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4273 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4275 switch (ins->inst_imm) {
4279 if (ins->dreg != ins->sreg1)
4280 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4281 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4284 /* LEA r1, [r2 + r2*2] */
4285 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4288 /* LEA r1, [r2 + r2*4] */
4289 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4292 /* LEA r1, [r2 + r2*2] */
4294 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4295 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4298 /* LEA r1, [r2 + r2*8] */
4299 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4302 /* LEA r1, [r2 + r2*4] */
4304 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4305 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4308 /* LEA r1, [r2 + r2*2] */
4310 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4311 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4314 /* LEA r1, [r2 + r2*4] */
4315 /* LEA r1, [r1 + r1*4] */
4316 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4317 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4320 /* LEA r1, [r2 + r2*4] */
4322 /* LEA r1, [r1 + r1*4] */
4323 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4324 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4325 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4328 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
4335 #if defined( __native_client_codegen__ )
4336 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4337 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4339 /* Regalloc magic makes the div/rem cases the same */
4340 if (ins->sreg2 == AMD64_RDX) {
4341 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4343 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4346 amd64_div_reg (code, ins->sreg2, TRUE);
4351 #if defined( __native_client_codegen__ )
4352 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4353 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4355 if (ins->sreg2 == AMD64_RDX) {
4356 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4357 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4358 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4360 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4361 amd64_div_reg (code, ins->sreg2, FALSE);
4366 #if defined( __native_client_codegen__ )
4367 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4368 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4370 if (ins->sreg2 == AMD64_RDX) {
4371 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4372 amd64_cdq_size (code, 4);
4373 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4375 amd64_cdq_size (code, 4);
4376 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4381 #if defined( __native_client_codegen__ )
4382 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4383 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4385 if (ins->sreg2 == AMD64_RDX) {
4386 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4387 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4388 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4390 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4391 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4395 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4396 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4399 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4403 g_assert (amd64_is_imm32 (ins->inst_imm));
4404 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4407 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4411 g_assert (amd64_is_imm32 (ins->inst_imm));
4412 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4415 g_assert (ins->sreg2 == AMD64_RCX);
4416 amd64_shift_reg (code, X86_SHL, ins->dreg);
4419 g_assert (ins->sreg2 == AMD64_RCX);
4420 amd64_shift_reg (code, X86_SAR, ins->dreg);
4423 g_assert (amd64_is_imm32 (ins->inst_imm));
4424 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4427 g_assert (amd64_is_imm32 (ins->inst_imm));
4428 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4431 g_assert (amd64_is_imm32 (ins->inst_imm));
4432 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4434 case OP_LSHR_UN_IMM:
4435 g_assert (amd64_is_imm32 (ins->inst_imm));
4436 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4439 g_assert (ins->sreg2 == AMD64_RCX);
4440 amd64_shift_reg (code, X86_SHR, ins->dreg);
4443 g_assert (amd64_is_imm32 (ins->inst_imm));
4444 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4447 g_assert (amd64_is_imm32 (ins->inst_imm));
4448 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4453 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4456 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4459 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4462 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4466 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4469 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4472 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4475 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4478 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4481 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4484 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4487 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4490 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4493 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4496 amd64_neg_reg_size (code, ins->sreg1, 4);
4499 amd64_not_reg_size (code, ins->sreg1, 4);
4502 g_assert (ins->sreg2 == AMD64_RCX);
4503 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4506 g_assert (ins->sreg2 == AMD64_RCX);
4507 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4510 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4512 case OP_ISHR_UN_IMM:
4513 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4516 g_assert (ins->sreg2 == AMD64_RCX);
4517 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4520 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4523 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4526 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4527 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4529 case OP_IMUL_OVF_UN:
4530 case OP_LMUL_OVF_UN: {
4531 /* the mul operation and the exception check should most likely be split */
4532 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4533 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4534 /*g_assert (ins->sreg2 == X86_EAX);
4535 g_assert (ins->dreg == X86_EAX);*/
4536 if (ins->sreg2 == X86_EAX) {
4537 non_eax_reg = ins->sreg1;
4538 } else if (ins->sreg1 == X86_EAX) {
4539 non_eax_reg = ins->sreg2;
4541 /* no need to save since we're going to store to it anyway */
4542 if (ins->dreg != X86_EAX) {
4544 amd64_push_reg (code, X86_EAX);
4546 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4547 non_eax_reg = ins->sreg2;
4549 if (ins->dreg == X86_EDX) {
4552 amd64_push_reg (code, X86_EAX);
4556 amd64_push_reg (code, X86_EDX);
4558 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4559 /* save before the check since pop and mov don't change the flags */
4560 if (ins->dreg != X86_EAX)
4561 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4563 amd64_pop_reg (code, X86_EDX);
4565 amd64_pop_reg (code, X86_EAX);
4566 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4570 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4572 case OP_ICOMPARE_IMM:
4573 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4595 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4603 case OP_CMOV_INE_UN:
4604 case OP_CMOV_IGE_UN:
4605 case OP_CMOV_IGT_UN:
4606 case OP_CMOV_ILE_UN:
4607 case OP_CMOV_ILT_UN:
4613 case OP_CMOV_LNE_UN:
4614 case OP_CMOV_LGE_UN:
4615 case OP_CMOV_LGT_UN:
4616 case OP_CMOV_LLE_UN:
4617 case OP_CMOV_LLT_UN:
4618 g_assert (ins->dreg == ins->sreg1);
4619 /* This needs to operate on 64 bit values */
4620 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);
4624 amd64_not_reg (code, ins->sreg1);
4627 amd64_neg_reg (code, ins->sreg1);
4632 if ((((guint64)ins->inst_c0) >> 32) == 0)
4633 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4635 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4638 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4639 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4642 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4643 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4646 if (ins->dreg != ins->sreg1)
4647 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4649 case OP_AMD64_SET_XMMREG_R4: {
4651 if (ins->dreg != ins->sreg1)
4652 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4654 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4658 case OP_AMD64_SET_XMMREG_R8: {
4659 if (ins->dreg != ins->sreg1)
4660 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4664 MonoCallInst *call = (MonoCallInst*)ins;
4665 int i, save_area_offset;
4667 g_assert (!cfg->method->save_lmf);
4669 /* Restore callee saved registers */
4670 save_area_offset = cfg->arch.reg_save_area_offset;
4671 for (i = 0; i < AMD64_NREG; ++i)
4672 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4673 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4674 save_area_offset += 8;
4677 if (cfg->arch.omit_fp) {
4678 if (cfg->arch.stack_alloc_size)
4679 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4681 if (call->stack_usage)
4684 /* Copy arguments on the stack to our argument area */
4685 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4686 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4687 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4693 offset = code - cfg->native_code;
4694 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4695 if (cfg->compile_aot)
4696 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4698 amd64_set_reg_template (code, AMD64_R11);
4699 amd64_jump_reg (code, AMD64_R11);
4700 ins->flags |= MONO_INST_GC_CALLSITE;
4701 ins->backend.pc_offset = code - cfg->native_code;
4705 /* ensure ins->sreg1 is not NULL */
4706 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4709 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4710 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4720 call = (MonoCallInst*)ins;
4722 * The AMD64 ABI forces callers to know about varargs.
4724 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4725 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4726 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4728 * Since the unmanaged calling convention doesn't contain a
4729 * 'vararg' entry, we have to treat every pinvoke call as a
4730 * potential vararg call.
4734 for (i = 0; i < AMD64_XMM_NREG; ++i)
4735 if (call->used_fregs & (1 << i))
4738 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4740 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4743 if (ins->flags & MONO_INST_HAS_METHOD)
4744 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4746 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
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);
4756 case OP_VOIDCALL_REG:
4758 call = (MonoCallInst*)ins;
4760 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4761 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4762 ins->sreg1 = AMD64_R11;
4766 * The AMD64 ABI forces callers to know about varargs.
4768 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4769 if (ins->sreg1 == AMD64_RAX) {
4770 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4771 ins->sreg1 = AMD64_R11;
4773 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4774 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4776 * Since the unmanaged calling convention doesn't contain a
4777 * 'vararg' entry, we have to treat every pinvoke call as a
4778 * potential vararg call.
4782 for (i = 0; i < AMD64_XMM_NREG; ++i)
4783 if (call->used_fregs & (1 << i))
4785 if (ins->sreg1 == AMD64_RAX) {
4786 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4787 ins->sreg1 = AMD64_R11;
4790 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4792 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4795 amd64_call_reg (code, ins->sreg1);
4796 ins->flags |= MONO_INST_GC_CALLSITE;
4797 ins->backend.pc_offset = code - cfg->native_code;
4798 code = emit_move_return_value (cfg, ins, code);
4800 case OP_FCALL_MEMBASE:
4801 case OP_RCALL_MEMBASE:
4802 case OP_LCALL_MEMBASE:
4803 case OP_VCALL_MEMBASE:
4804 case OP_VCALL2_MEMBASE:
4805 case OP_VOIDCALL_MEMBASE:
4806 case OP_CALL_MEMBASE:
4807 call = (MonoCallInst*)ins;
4809 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
4810 ins->flags |= MONO_INST_GC_CALLSITE;
4811 ins->backend.pc_offset = code - cfg->native_code;
4812 code = emit_move_return_value (cfg, ins, code);
4816 MonoInst *var = cfg->dyn_call_var;
4818 g_assert (var->opcode == OP_REGOFFSET);
4820 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
4821 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4823 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
4825 /* Save args buffer */
4826 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
4828 /* Set argument registers */
4829 for (i = 0; i < PARAM_REGS; ++i)
4830 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
4833 amd64_call_reg (code, AMD64_R10);
4835 ins->flags |= MONO_INST_GC_CALLSITE;
4836 ins->backend.pc_offset = code - cfg->native_code;
4839 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4840 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
4843 case OP_AMD64_SAVE_SP_TO_LMF: {
4844 MonoInst *lmf_var = cfg->lmf_var;
4845 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
4849 g_assert_not_reached ();
4850 amd64_push_reg (code, ins->sreg1);
4852 case OP_X86_PUSH_IMM:
4853 g_assert_not_reached ();
4854 g_assert (amd64_is_imm32 (ins->inst_imm));
4855 amd64_push_imm (code, ins->inst_imm);
4857 case OP_X86_PUSH_MEMBASE:
4858 g_assert_not_reached ();
4859 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
4861 case OP_X86_PUSH_OBJ: {
4862 int size = ALIGN_TO (ins->inst_imm, 8);
4864 g_assert_not_reached ();
4866 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4867 amd64_push_reg (code, AMD64_RDI);
4868 amd64_push_reg (code, AMD64_RSI);
4869 amd64_push_reg (code, AMD64_RCX);
4870 if (ins->inst_offset)
4871 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
4873 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
4874 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
4875 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
4877 amd64_prefix (code, X86_REP_PREFIX);
4879 amd64_pop_reg (code, AMD64_RCX);
4880 amd64_pop_reg (code, AMD64_RSI);
4881 amd64_pop_reg (code, AMD64_RDI);
4885 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
4887 case OP_X86_LEA_MEMBASE:
4888 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
4891 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
4894 /* keep alignment */
4895 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
4896 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
4897 code = mono_emit_stack_alloc (cfg, code, ins);
4898 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4899 if (cfg->param_area)
4900 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4902 case OP_LOCALLOC_IMM: {
4903 guint32 size = ins->inst_imm;
4904 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
4906 if (ins->flags & MONO_INST_INIT) {
4910 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4911 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4913 for (i = 0; i < size; i += 8)
4914 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
4915 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4917 amd64_mov_reg_imm (code, ins->dreg, size);
4918 ins->sreg1 = ins->dreg;
4920 code = mono_emit_stack_alloc (cfg, code, ins);
4921 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4924 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4925 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4927 if (cfg->param_area)
4928 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4932 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4933 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4934 (gpointer)"mono_arch_throw_exception", FALSE);
4935 ins->flags |= MONO_INST_GC_CALLSITE;
4936 ins->backend.pc_offset = code - cfg->native_code;
4940 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4941 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4942 (gpointer)"mono_arch_rethrow_exception", FALSE);
4943 ins->flags |= MONO_INST_GC_CALLSITE;
4944 ins->backend.pc_offset = code - cfg->native_code;
4947 case OP_CALL_HANDLER:
4949 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
4950 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4951 amd64_call_imm (code, 0);
4952 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4953 /* Restore stack alignment */
4954 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
4956 case OP_START_HANDLER: {
4957 /* Even though we're saving RSP, use sizeof */
4958 /* gpointer because spvar is of type IntPtr */
4959 /* see: mono_create_spvar_for_region */
4960 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4961 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
4963 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
4964 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
4966 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
4970 case OP_ENDFINALLY: {
4971 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4972 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
4976 case OP_ENDFILTER: {
4977 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4978 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
4979 /* The local allocator will put the result into RAX */
4985 ins->inst_c0 = code - cfg->native_code;
4988 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
4989 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
4991 if (ins->inst_target_bb->native_offset) {
4992 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
4994 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4995 if ((cfg->opt & MONO_OPT_BRANCH) &&
4996 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
4997 x86_jump8 (code, 0);
4999 x86_jump32 (code, 0);
5003 amd64_jump_reg (code, ins->sreg1);
5026 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5027 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5029 case OP_COND_EXC_EQ:
5030 case OP_COND_EXC_NE_UN:
5031 case OP_COND_EXC_LT:
5032 case OP_COND_EXC_LT_UN:
5033 case OP_COND_EXC_GT:
5034 case OP_COND_EXC_GT_UN:
5035 case OP_COND_EXC_GE:
5036 case OP_COND_EXC_GE_UN:
5037 case OP_COND_EXC_LE:
5038 case OP_COND_EXC_LE_UN:
5039 case OP_COND_EXC_IEQ:
5040 case OP_COND_EXC_INE_UN:
5041 case OP_COND_EXC_ILT:
5042 case OP_COND_EXC_ILT_UN:
5043 case OP_COND_EXC_IGT:
5044 case OP_COND_EXC_IGT_UN:
5045 case OP_COND_EXC_IGE:
5046 case OP_COND_EXC_IGE_UN:
5047 case OP_COND_EXC_ILE:
5048 case OP_COND_EXC_ILE_UN:
5049 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
5051 case OP_COND_EXC_OV:
5052 case OP_COND_EXC_NO:
5054 case OP_COND_EXC_NC:
5055 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5056 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
5058 case OP_COND_EXC_IOV:
5059 case OP_COND_EXC_INO:
5060 case OP_COND_EXC_IC:
5061 case OP_COND_EXC_INC:
5062 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5063 (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
5066 /* floating point opcodes */
5068 double d = *(double *)ins->inst_p0;
5070 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5071 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5074 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5075 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5080 float f = *(float *)ins->inst_p0;
5082 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5084 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5086 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5089 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5090 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5092 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5096 case OP_STORER8_MEMBASE_REG:
5097 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5099 case OP_LOADR8_MEMBASE:
5100 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5102 case OP_STORER4_MEMBASE_REG:
5104 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5106 /* This requires a double->single conversion */
5107 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5108 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5111 case OP_LOADR4_MEMBASE:
5113 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5115 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5116 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5119 case OP_ICONV_TO_R4:
5121 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5123 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5124 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5127 case OP_ICONV_TO_R8:
5128 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5130 case OP_LCONV_TO_R4:
5132 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5134 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5135 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5138 case OP_LCONV_TO_R8:
5139 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5141 case OP_FCONV_TO_R4:
5143 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5145 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5146 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5149 case OP_FCONV_TO_I1:
5150 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5152 case OP_FCONV_TO_U1:
5153 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5155 case OP_FCONV_TO_I2:
5156 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5158 case OP_FCONV_TO_U2:
5159 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5161 case OP_FCONV_TO_U4:
5162 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5164 case OP_FCONV_TO_I4:
5166 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5168 case OP_FCONV_TO_I8:
5169 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5172 case OP_RCONV_TO_I1:
5173 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5174 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5176 case OP_RCONV_TO_U1:
5177 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5178 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5180 case OP_RCONV_TO_I2:
5181 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5182 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5184 case OP_RCONV_TO_U2:
5185 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5186 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5188 case OP_RCONV_TO_I4:
5189 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5191 case OP_RCONV_TO_U4:
5192 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5194 case OP_RCONV_TO_I8:
5195 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5197 case OP_RCONV_TO_R8:
5198 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5200 case OP_RCONV_TO_R4:
5201 if (ins->dreg != ins->sreg1)
5202 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5205 case OP_LCONV_TO_R_UN: {
5208 /* Based on gcc code */
5209 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5210 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5213 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5214 br [1] = code; x86_jump8 (code, 0);
5215 amd64_patch (br [0], code);
5218 /* Save to the red zone */
5219 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5220 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5221 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5222 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5223 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5224 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5225 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5226 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5227 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5229 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5230 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5231 amd64_patch (br [1], code);
5234 case OP_LCONV_TO_OVF_U4:
5235 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5236 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5237 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5239 case OP_LCONV_TO_OVF_I4_UN:
5240 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5241 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5242 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5245 if (ins->dreg != ins->sreg1)
5246 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5249 if (ins->dreg != ins->sreg1)
5250 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5252 case OP_MOVE_F_TO_I4:
5254 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5256 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5257 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5260 case OP_MOVE_I4_TO_F:
5261 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5263 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5265 case OP_MOVE_F_TO_I8:
5266 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5268 case OP_MOVE_I8_TO_F:
5269 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5272 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5275 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5278 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5281 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5284 static double r8_0 = -0.0;
5286 g_assert (ins->sreg1 == ins->dreg);
5288 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5289 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5293 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5296 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5299 static guint64 d = 0x7fffffffffffffffUL;
5301 g_assert (ins->sreg1 == ins->dreg);
5303 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5304 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5308 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5312 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5315 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5318 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5321 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5324 static float r4_0 = -0.0;
5326 g_assert (ins->sreg1 == ins->dreg);
5328 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5329 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5330 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
5335 g_assert (cfg->opt & MONO_OPT_CMOV);
5336 g_assert (ins->dreg == ins->sreg1);
5337 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5338 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
5341 g_assert (cfg->opt & MONO_OPT_CMOV);
5342 g_assert (ins->dreg == ins->sreg1);
5343 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5344 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
5347 g_assert (cfg->opt & MONO_OPT_CMOV);
5348 g_assert (ins->dreg == ins->sreg1);
5349 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5350 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
5353 g_assert (cfg->opt & MONO_OPT_CMOV);
5354 g_assert (ins->dreg == ins->sreg1);
5355 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5356 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
5359 g_assert (cfg->opt & MONO_OPT_CMOV);
5360 g_assert (ins->dreg == ins->sreg1);
5361 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5362 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
5365 g_assert (cfg->opt & MONO_OPT_CMOV);
5366 g_assert (ins->dreg == ins->sreg1);
5367 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5368 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
5371 g_assert (cfg->opt & MONO_OPT_CMOV);
5372 g_assert (ins->dreg == ins->sreg1);
5373 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5374 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
5377 g_assert (cfg->opt & MONO_OPT_CMOV);
5378 g_assert (ins->dreg == ins->sreg1);
5379 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5380 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5386 * The two arguments are swapped because the fbranch instructions
5387 * depend on this for the non-sse case to work.
5389 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5393 * FIXME: Get rid of this.
5394 * The two arguments are swapped because the fbranch instructions
5395 * depend on this for the non-sse case to work.
5397 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5401 /* zeroing the register at the start results in
5402 * shorter and faster code (we can also remove the widening op)
5404 guchar *unordered_check;
5406 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5407 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5408 unordered_check = code;
5409 x86_branch8 (code, X86_CC_P, 0, FALSE);
5411 if (ins->opcode == OP_FCEQ) {
5412 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5413 amd64_patch (unordered_check, code);
5415 guchar *jump_to_end;
5416 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5418 x86_jump8 (code, 0);
5419 amd64_patch (unordered_check, code);
5420 amd64_inc_reg (code, ins->dreg);
5421 amd64_patch (jump_to_end, code);
5427 /* zeroing the register at the start results in
5428 * shorter and faster code (we can also remove the widening op)
5430 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5431 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5432 if (ins->opcode == OP_FCLT_UN) {
5433 guchar *unordered_check = code;
5434 guchar *jump_to_end;
5435 x86_branch8 (code, X86_CC_P, 0, FALSE);
5436 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5438 x86_jump8 (code, 0);
5439 amd64_patch (unordered_check, code);
5440 amd64_inc_reg (code, ins->dreg);
5441 amd64_patch (jump_to_end, code);
5443 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5448 guchar *unordered_check;
5449 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5450 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5451 unordered_check = code;
5452 x86_branch8 (code, X86_CC_P, 0, FALSE);
5453 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5454 amd64_patch (unordered_check, code);
5459 /* zeroing the register at the start results in
5460 * shorter and faster code (we can also remove the widening op)
5462 guchar *unordered_check;
5464 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5465 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5466 if (ins->opcode == OP_FCGT) {
5467 unordered_check = code;
5468 x86_branch8 (code, X86_CC_P, 0, FALSE);
5469 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5470 amd64_patch (unordered_check, code);
5472 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5477 guchar *unordered_check;
5478 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5479 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5480 unordered_check = code;
5481 x86_branch8 (code, X86_CC_P, 0, FALSE);
5482 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5483 amd64_patch (unordered_check, code);
5493 gboolean unordered = FALSE;
5495 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5496 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5498 switch (ins->opcode) {
5500 x86_cond = X86_CC_EQ;
5503 x86_cond = X86_CC_LT;
5506 x86_cond = X86_CC_GT;
5509 x86_cond = X86_CC_GT;
5513 x86_cond = X86_CC_LT;
5517 g_assert_not_reached ();
5522 guchar *unordered_check;
5523 guchar *jump_to_end;
5525 unordered_check = code;
5526 x86_branch8 (code, X86_CC_P, 0, FALSE);
5527 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5529 x86_jump8 (code, 0);
5530 amd64_patch (unordered_check, code);
5531 amd64_inc_reg (code, ins->dreg);
5532 amd64_patch (jump_to_end, code);
5534 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5538 case OP_FCLT_MEMBASE:
5539 case OP_FCGT_MEMBASE:
5540 case OP_FCLT_UN_MEMBASE:
5541 case OP_FCGT_UN_MEMBASE:
5542 case OP_FCEQ_MEMBASE: {
5543 guchar *unordered_check, *jump_to_end;
5546 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5547 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5549 switch (ins->opcode) {
5550 case OP_FCEQ_MEMBASE:
5551 x86_cond = X86_CC_EQ;
5553 case OP_FCLT_MEMBASE:
5554 case OP_FCLT_UN_MEMBASE:
5555 x86_cond = X86_CC_LT;
5557 case OP_FCGT_MEMBASE:
5558 case OP_FCGT_UN_MEMBASE:
5559 x86_cond = X86_CC_GT;
5562 g_assert_not_reached ();
5565 unordered_check = code;
5566 x86_branch8 (code, X86_CC_P, 0, FALSE);
5567 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5569 switch (ins->opcode) {
5570 case OP_FCEQ_MEMBASE:
5571 case OP_FCLT_MEMBASE:
5572 case OP_FCGT_MEMBASE:
5573 amd64_patch (unordered_check, code);
5575 case OP_FCLT_UN_MEMBASE:
5576 case OP_FCGT_UN_MEMBASE:
5578 x86_jump8 (code, 0);
5579 amd64_patch (unordered_check, code);
5580 amd64_inc_reg (code, ins->dreg);
5581 amd64_patch (jump_to_end, code);
5589 guchar *jump = code;
5590 x86_branch8 (code, X86_CC_P, 0, TRUE);
5591 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5592 amd64_patch (jump, code);
5596 /* Branch if C013 != 100 */
5597 /* branch if !ZF or (PF|CF) */
5598 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5599 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5600 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5603 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5606 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5607 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5611 if (ins->opcode == OP_FBGT) {
5614 /* skip branch if C1=1 */
5616 x86_branch8 (code, X86_CC_P, 0, FALSE);
5617 /* branch if (C0 | C3) = 1 */
5618 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5619 amd64_patch (br1, code);
5622 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5626 /* Branch if C013 == 100 or 001 */
5629 /* skip branch if C1=1 */
5631 x86_branch8 (code, X86_CC_P, 0, FALSE);
5632 /* branch if (C0 | C3) = 1 */
5633 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5634 amd64_patch (br1, code);
5638 /* Branch if C013 == 000 */
5639 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5642 /* Branch if C013=000 or 100 */
5645 /* skip branch if C1=1 */
5647 x86_branch8 (code, X86_CC_P, 0, FALSE);
5648 /* branch if C0=0 */
5649 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5650 amd64_patch (br1, code);
5654 /* Branch if C013 != 001 */
5655 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5656 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5659 /* Transfer value to the fp stack */
5660 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5661 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5662 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5664 amd64_push_reg (code, AMD64_RAX);
5666 amd64_fnstsw (code);
5667 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5668 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5669 amd64_pop_reg (code, AMD64_RAX);
5670 amd64_fstp (code, 0);
5671 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
5672 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5675 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5678 case OP_TLS_GET_REG:
5679 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5682 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5685 case OP_TLS_SET_REG: {
5686 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5689 case OP_MEMORY_BARRIER: {
5690 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5694 case OP_ATOMIC_ADD_I4:
5695 case OP_ATOMIC_ADD_I8: {
5696 int dreg = ins->dreg;
5697 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5699 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5702 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5703 amd64_prefix (code, X86_LOCK_PREFIX);
5704 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5705 /* dreg contains the old value, add with sreg2 value */
5706 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5708 if (ins->dreg != dreg)
5709 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5713 case OP_ATOMIC_EXCHANGE_I4:
5714 case OP_ATOMIC_EXCHANGE_I8: {
5715 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5717 /* LOCK prefix is implied. */
5718 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5719 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5720 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5723 case OP_ATOMIC_CAS_I4:
5724 case OP_ATOMIC_CAS_I8: {
5727 if (ins->opcode == OP_ATOMIC_CAS_I8)
5733 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5734 * an explanation of how this works.
5736 g_assert (ins->sreg3 == AMD64_RAX);
5737 g_assert (ins->sreg1 != AMD64_RAX);
5738 g_assert (ins->sreg1 != ins->sreg2);
5740 amd64_prefix (code, X86_LOCK_PREFIX);
5741 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
5743 if (ins->dreg != AMD64_RAX)
5744 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
5747 case OP_ATOMIC_LOAD_I1: {
5748 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
5751 case OP_ATOMIC_LOAD_U1: {
5752 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
5755 case OP_ATOMIC_LOAD_I2: {
5756 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
5759 case OP_ATOMIC_LOAD_U2: {
5760 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
5763 case OP_ATOMIC_LOAD_I4: {
5764 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5767 case OP_ATOMIC_LOAD_U4:
5768 case OP_ATOMIC_LOAD_I8:
5769 case OP_ATOMIC_LOAD_U8: {
5770 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
5773 case OP_ATOMIC_LOAD_R4: {
5774 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5775 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5778 case OP_ATOMIC_LOAD_R8: {
5779 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5782 case OP_ATOMIC_STORE_I1:
5783 case OP_ATOMIC_STORE_U1:
5784 case OP_ATOMIC_STORE_I2:
5785 case OP_ATOMIC_STORE_U2:
5786 case OP_ATOMIC_STORE_I4:
5787 case OP_ATOMIC_STORE_U4:
5788 case OP_ATOMIC_STORE_I8:
5789 case OP_ATOMIC_STORE_U8: {
5792 switch (ins->opcode) {
5793 case OP_ATOMIC_STORE_I1:
5794 case OP_ATOMIC_STORE_U1:
5797 case OP_ATOMIC_STORE_I2:
5798 case OP_ATOMIC_STORE_U2:
5801 case OP_ATOMIC_STORE_I4:
5802 case OP_ATOMIC_STORE_U4:
5805 case OP_ATOMIC_STORE_I8:
5806 case OP_ATOMIC_STORE_U8:
5811 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
5813 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5817 case OP_ATOMIC_STORE_R4: {
5818 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5819 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5821 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5825 case OP_ATOMIC_STORE_R8: {
5828 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5832 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5836 case OP_CARD_TABLE_WBARRIER: {
5837 int ptr = ins->sreg1;
5838 int value = ins->sreg2;
5840 int nursery_shift, card_table_shift;
5841 gpointer card_table_mask;
5842 size_t nursery_size;
5844 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
5845 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
5846 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
5848 /*If either point to the stack we can simply avoid the WB. This happens due to
5849 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
5851 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
5855 * We need one register we can clobber, we choose EDX and make sreg1
5856 * fixed EAX to work around limitations in the local register allocator.
5857 * sreg2 might get allocated to EDX, but that is not a problem since
5858 * we use it before clobbering EDX.
5860 g_assert (ins->sreg1 == AMD64_RAX);
5863 * This is the code we produce:
5866 * edx >>= nursery_shift
5867 * cmp edx, (nursery_start >> nursery_shift)
5870 * edx >>= card_table_shift
5876 if (mono_gc_card_table_nursery_check ()) {
5877 if (value != AMD64_RDX)
5878 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
5879 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
5880 if (shifted_nursery_start >> 31) {
5882 * The value we need to compare against is 64 bits, so we need
5883 * another spare register. We use RBX, which we save and
5886 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
5887 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
5888 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
5889 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
5891 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
5893 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
5895 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
5896 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
5897 if (card_table_mask)
5898 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
5900 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
5901 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
5903 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
5905 if (mono_gc_card_table_nursery_check ())
5906 x86_patch (br, code);
5909 #ifdef MONO_ARCH_SIMD_INTRINSICS
5910 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
5912 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
5915 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
5918 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
5921 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
5924 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
5927 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
5930 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
5931 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5934 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
5937 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
5940 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
5943 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
5946 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
5949 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
5952 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
5955 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
5958 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
5961 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
5964 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
5967 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
5970 case OP_PSHUFLEW_HIGH:
5971 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5972 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5974 case OP_PSHUFLEW_LOW:
5975 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5976 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5979 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5980 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5983 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5984 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5987 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
5988 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5992 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
5995 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
5998 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
6001 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
6004 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
6007 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
6010 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6011 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6014 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
6017 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
6020 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
6023 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6026 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6029 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6032 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6035 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6038 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6041 case OP_EXTRACT_MASK:
6042 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6046 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6049 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6052 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6056 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6059 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6062 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6065 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6069 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6072 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6075 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6078 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6082 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6085 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6088 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6092 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6095 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6098 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6102 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6105 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6109 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6112 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6115 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6119 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6122 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6125 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6129 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6132 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6135 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6138 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6142 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6145 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6148 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6151 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6154 case OP_PSUM_ABS_DIFF:
6155 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6158 case OP_UNPACK_LOWB:
6159 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6161 case OP_UNPACK_LOWW:
6162 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6164 case OP_UNPACK_LOWD:
6165 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6167 case OP_UNPACK_LOWQ:
6168 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6170 case OP_UNPACK_LOWPS:
6171 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6173 case OP_UNPACK_LOWPD:
6174 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6177 case OP_UNPACK_HIGHB:
6178 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6180 case OP_UNPACK_HIGHW:
6181 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6183 case OP_UNPACK_HIGHD:
6184 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6186 case OP_UNPACK_HIGHQ:
6187 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6189 case OP_UNPACK_HIGHPS:
6190 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6192 case OP_UNPACK_HIGHPD:
6193 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6197 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6200 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6203 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6206 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6209 case OP_PADDB_SAT_UN:
6210 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6212 case OP_PSUBB_SAT_UN:
6213 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6215 case OP_PADDW_SAT_UN:
6216 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6218 case OP_PSUBW_SAT_UN:
6219 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6223 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6226 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6229 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6232 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6236 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6239 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6242 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6244 case OP_PMULW_HIGH_UN:
6245 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6248 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6252 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6255 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6259 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6262 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6266 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6269 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6273 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6276 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6280 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6283 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6287 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6290 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6294 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6297 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6300 /*TODO: This is appart of the sse spec but not added
6302 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6305 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6310 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6313 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6316 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6319 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6322 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6325 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6328 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6331 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6334 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6337 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6341 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6344 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6348 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6349 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6351 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6356 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6358 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6359 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6363 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6365 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6366 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6367 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6371 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6373 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6376 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6378 case OP_EXTRACTX_U2:
6379 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6381 case OP_INSERTX_U1_SLOW:
6382 /*sreg1 is the extracted ireg (scratch)
6383 /sreg2 is the to be inserted ireg (scratch)
6384 /dreg is the xreg to receive the value*/
6386 /*clear the bits from the extracted word*/
6387 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6388 /*shift the value to insert if needed*/
6389 if (ins->inst_c0 & 1)
6390 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6391 /*join them together*/
6392 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6393 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6395 case OP_INSERTX_I4_SLOW:
6396 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6397 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6398 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6400 case OP_INSERTX_I8_SLOW:
6401 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6403 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6405 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6408 case OP_INSERTX_R4_SLOW:
6409 switch (ins->inst_c0) {
6412 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6414 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6417 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6419 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6421 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6422 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6425 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6427 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6429 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6430 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6433 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6435 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6437 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6438 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6442 case OP_INSERTX_R8_SLOW:
6444 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6446 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6448 case OP_STOREX_MEMBASE_REG:
6449 case OP_STOREX_MEMBASE:
6450 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6452 case OP_LOADX_MEMBASE:
6453 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6455 case OP_LOADX_ALIGNED_MEMBASE:
6456 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6458 case OP_STOREX_ALIGNED_MEMBASE_REG:
6459 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6461 case OP_STOREX_NTA_MEMBASE_REG:
6462 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6464 case OP_PREFETCH_MEMBASE:
6465 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6469 /*FIXME the peephole pass should have killed this*/
6470 if (ins->dreg != ins->sreg1)
6471 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6474 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6476 case OP_ICONV_TO_R4_RAW:
6477 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6480 case OP_FCONV_TO_R8_X:
6481 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6484 case OP_XCONV_R8_TO_I4:
6485 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6486 switch (ins->backend.source_opcode) {
6487 case OP_FCONV_TO_I1:
6488 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6490 case OP_FCONV_TO_U1:
6491 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6493 case OP_FCONV_TO_I2:
6494 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6496 case OP_FCONV_TO_U2:
6497 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6503 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6504 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6505 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6508 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6509 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6512 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6513 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6517 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6519 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6520 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6522 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6525 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6526 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6529 case OP_LIVERANGE_START: {
6530 if (cfg->verbose_level > 1)
6531 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6532 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6535 case OP_LIVERANGE_END: {
6536 if (cfg->verbose_level > 1)
6537 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6538 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6541 case OP_NACL_GC_SAFE_POINT: {
6542 #if defined(__native_client_codegen__) && defined(__native_client_gc__)
6543 if (cfg->compile_aot)
6544 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc, TRUE);
6548 amd64_mov_reg_imm_size (code, AMD64_R11, (gpointer)&__nacl_thread_suspension_needed, 4);
6549 amd64_test_membase_imm_size (code, AMD64_R11, 0, 0xFFFFFFFF, 4);
6550 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6551 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc, TRUE);
6552 amd64_patch (br[0], code);
6557 case OP_GC_LIVENESS_DEF:
6558 case OP_GC_LIVENESS_USE:
6559 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6560 ins->backend.pc_offset = code - cfg->native_code;
6562 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6563 ins->backend.pc_offset = code - cfg->native_code;
6564 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6567 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6568 g_assert_not_reached ();
6571 if ((code - cfg->native_code - offset) > max_len) {
6572 #if !defined(__native_client_codegen__)
6573 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6574 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6575 g_assert_not_reached ();
6580 cfg->code_len = code - cfg->native_code;
6583 #endif /* DISABLE_JIT */
6586 mono_arch_register_lowlevel_calls (void)
6588 /* The signature doesn't matter */
6589 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6593 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
6595 MonoJumpInfo *patch_info;
6596 gboolean compile_aot = !run_cctors;
6598 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
6599 unsigned char *ip = patch_info->ip.i + code;
6600 unsigned char *target;
6603 switch (patch_info->type) {
6604 case MONO_PATCH_INFO_BB:
6605 case MONO_PATCH_INFO_LABEL:
6608 /* No need to patch these */
6613 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
6615 switch (patch_info->type) {
6616 case MONO_PATCH_INFO_NONE:
6618 case MONO_PATCH_INFO_METHOD_REL:
6619 case MONO_PATCH_INFO_R8:
6620 case MONO_PATCH_INFO_R4:
6621 g_assert_not_reached ();
6623 case MONO_PATCH_INFO_BB:
6630 * Debug code to help track down problems where the target of a near call is
6633 if (amd64_is_near_call (ip)) {
6634 gint64 disp = (guint8*)target - (guint8*)ip;
6636 if (!amd64_is_imm32 (disp)) {
6637 printf ("TYPE: %d\n", patch_info->type);
6638 switch (patch_info->type) {
6639 case MONO_PATCH_INFO_INTERNAL_METHOD:
6640 printf ("V: %s\n", patch_info->data.name);
6642 case MONO_PATCH_INFO_METHOD_JUMP:
6643 case MONO_PATCH_INFO_METHOD:
6644 printf ("V: %s\n", patch_info->data.method->name);
6652 amd64_patch (ip, (gpointer)target);
6659 get_max_epilog_size (MonoCompile *cfg)
6661 int max_epilog_size = 16;
6663 if (cfg->method->save_lmf)
6664 max_epilog_size += 256;
6666 if (mono_jit_trace_calls != NULL)
6667 max_epilog_size += 50;
6669 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6670 max_epilog_size += 50;
6672 max_epilog_size += (AMD64_NREG * 2);
6674 return max_epilog_size;
6678 * This macro is used for testing whenever the unwinder works correctly at every point
6679 * where an async exception can happen.
6681 /* This will generate a SIGSEGV at the given point in the code */
6682 #define async_exc_point(code) do { \
6683 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6684 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6685 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6686 cfg->arch.async_point_count ++; \
6691 mono_arch_emit_prolog (MonoCompile *cfg)
6693 MonoMethod *method = cfg->method;
6695 MonoMethodSignature *sig;
6697 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6700 MonoInst *lmf_var = cfg->lmf_var;
6701 gboolean args_clobbered = FALSE;
6702 gboolean trace = FALSE;
6703 #ifdef __native_client_codegen__
6704 guint alignment_check;
6707 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6709 #if defined(__default_codegen__)
6710 code = cfg->native_code = g_malloc (cfg->code_size);
6711 #elif defined(__native_client_codegen__)
6712 /* native_code_alloc is not 32-byte aligned, native_code is. */
6713 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6715 /* Align native_code to next nearest kNaclAlignment byte. */
6716 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6717 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6719 code = cfg->native_code;
6721 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6722 g_assert (alignment_check == 0);
6725 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6728 /* Amount of stack space allocated by register saving code */
6731 /* Offset between RSP and the CFA */
6735 * The prolog consists of the following parts:
6737 * - push rbp, mov rbp, rsp
6738 * - save callee saved regs using pushes
6740 * - save rgctx if needed
6741 * - save lmf if needed
6744 * - save rgctx if needed
6745 * - save lmf if needed
6746 * - save callee saved regs using moves
6751 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6752 // IP saved at CFA - 8
6753 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6754 async_exc_point (code);
6755 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6757 if (!cfg->arch.omit_fp) {
6758 amd64_push_reg (code, AMD64_RBP);
6760 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6761 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6762 async_exc_point (code);
6764 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6766 /* These are handled automatically by the stack marking code */
6767 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6769 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
6770 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
6771 async_exc_point (code);
6773 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6777 /* The param area is always at offset 0 from sp */
6778 /* This needs to be allocated here, since it has to come after the spill area */
6779 if (cfg->param_area) {
6780 if (cfg->arch.omit_fp)
6782 g_assert_not_reached ();
6783 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
6786 if (cfg->arch.omit_fp) {
6788 * On enter, the stack is misaligned by the pushing of the return
6789 * address. It is either made aligned by the pushing of %rbp, or by
6792 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
6793 if ((alloc_size % 16) == 0) {
6795 /* Mark the padding slot as NOREF */
6796 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
6799 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
6800 if (cfg->stack_offset != alloc_size) {
6801 /* Mark the padding slot as NOREF */
6802 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
6804 cfg->arch.sp_fp_offset = alloc_size;
6808 cfg->arch.stack_alloc_size = alloc_size;
6810 /* Allocate stack frame */
6812 /* See mono_emit_stack_alloc */
6813 #if defined(HOST_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
6814 guint32 remaining_size = alloc_size;
6815 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
6816 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
6817 guint32 offset = code - cfg->native_code;
6818 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
6819 while (required_code_size >= (cfg->code_size - offset))
6820 cfg->code_size *= 2;
6821 cfg->native_code = mono_realloc_native_code (cfg);
6822 code = cfg->native_code + offset;
6823 cfg->stat_code_reallocs++;
6826 while (remaining_size >= 0x1000) {
6827 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
6828 if (cfg->arch.omit_fp) {
6829 cfa_offset += 0x1000;
6830 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6832 async_exc_point (code);
6834 if (cfg->arch.omit_fp)
6835 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
6838 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
6839 remaining_size -= 0x1000;
6841 if (remaining_size) {
6842 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
6843 if (cfg->arch.omit_fp) {
6844 cfa_offset += remaining_size;
6845 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6846 async_exc_point (code);
6849 if (cfg->arch.omit_fp)
6850 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
6854 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
6855 if (cfg->arch.omit_fp) {
6856 cfa_offset += alloc_size;
6857 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6858 async_exc_point (code);
6863 /* Stack alignment check */
6866 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
6867 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
6868 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
6869 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
6870 amd64_breakpoint (code);
6874 if (mini_get_debug_options ()->init_stacks) {
6875 /* Fill the stack frame with a dummy value to force deterministic behavior */
6877 /* Save registers to the red zone */
6878 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
6879 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
6881 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
6882 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
6883 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
6886 #if defined(__default_codegen__)
6887 amd64_prefix (code, X86_REP_PREFIX);
6889 #elif defined(__native_client_codegen__)
6890 /* NaCl stos pseudo-instruction */
6891 amd64_codegen_pre (code);
6892 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
6893 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
6894 /* Add %r15 to %rdi using lea, condition flags unaffected. */
6895 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
6896 amd64_prefix (code, X86_REP_PREFIX);
6898 amd64_codegen_post (code);
6899 #endif /* __native_client_codegen__ */
6901 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
6902 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
6906 if (method->save_lmf)
6907 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
6909 /* Save callee saved registers */
6910 if (cfg->arch.omit_fp) {
6911 save_area_offset = cfg->arch.reg_save_area_offset;
6912 /* Save caller saved registers after sp is adjusted */
6913 /* The registers are saved at the bottom of the frame */
6914 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
6916 /* The registers are saved just below the saved rbp */
6917 save_area_offset = cfg->arch.reg_save_area_offset;
6920 for (i = 0; i < AMD64_NREG; ++i) {
6921 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
6922 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
6924 if (cfg->arch.omit_fp) {
6925 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
6926 /* These are handled automatically by the stack marking code */
6927 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
6929 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
6933 save_area_offset += 8;
6934 async_exc_point (code);
6938 /* store runtime generic context */
6939 if (cfg->rgctx_var) {
6940 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
6941 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
6943 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
6945 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
6946 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
6949 /* compute max_length in order to use short forward jumps */
6950 max_epilog_size = get_max_epilog_size (cfg);
6951 if (cfg->opt & MONO_OPT_BRANCH) {
6952 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6956 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6958 /* max alignment for loops */
6959 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
6960 max_length += LOOP_ALIGNMENT;
6961 #ifdef __native_client_codegen__
6962 /* max alignment for native client */
6963 max_length += kNaClAlignment;
6966 MONO_BB_FOR_EACH_INS (bb, ins) {
6967 #ifdef __native_client_codegen__
6969 int space_in_block = kNaClAlignment -
6970 ((max_length + cfg->code_len) & kNaClAlignmentMask);
6971 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
6972 if (space_in_block < max_len && max_len < kNaClAlignment) {
6973 max_length += space_in_block;
6976 #endif /*__native_client_codegen__*/
6977 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
6980 /* Take prolog and epilog instrumentation into account */
6981 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
6982 max_length += max_epilog_size;
6984 bb->max_length = max_length;
6988 sig = mono_method_signature (method);
6991 cinfo = cfg->arch.cinfo;
6993 if (sig->ret->type != MONO_TYPE_VOID) {
6994 /* Save volatile arguments to the stack */
6995 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
6996 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
6999 /* Keep this in sync with emit_load_volatile_arguments */
7000 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
7001 ArgInfo *ainfo = cinfo->args + i;
7003 ins = cfg->args [i];
7005 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
7006 /* Unused arguments */
7009 if (cfg->globalra) {
7010 /* All the other moves are done by the register allocator */
7011 switch (ainfo->storage) {
7012 case ArgInFloatSSEReg:
7013 amd64_sse_cvtss2sd_reg_reg (code, ainfo->reg, ainfo->reg);
7015 case ArgValuetypeInReg:
7016 for (quad = 0; quad < 2; quad ++) {
7017 switch (ainfo->pair_storage [quad]) {
7019 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7021 case ArgInFloatSSEReg:
7022 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7024 case ArgInDoubleSSEReg:
7025 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7030 g_assert_not_reached ();
7041 /* Save volatile arguments to the stack */
7042 if (ins->opcode != OP_REGVAR) {
7043 switch (ainfo->storage) {
7049 if (stack_offset & 0x1)
7051 else if (stack_offset & 0x2)
7053 else if (stack_offset & 0x4)
7058 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
7061 * Save the original location of 'this',
7062 * get_generic_info_from_stack_frame () needs this to properly look up
7063 * the argument value during the handling of async exceptions.
7065 if (ins == cfg->args [0]) {
7066 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7067 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
7071 case ArgInFloatSSEReg:
7072 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7074 case ArgInDoubleSSEReg:
7075 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7077 case ArgValuetypeInReg:
7078 for (quad = 0; quad < 2; quad ++) {
7079 switch (ainfo->pair_storage [quad]) {
7081 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7083 case ArgInFloatSSEReg:
7084 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7086 case ArgInDoubleSSEReg:
7087 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7092 g_assert_not_reached ();
7096 case ArgValuetypeAddrInIReg:
7097 if (ainfo->pair_storage [0] == ArgInIReg)
7098 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7104 /* Argument allocated to (non-volatile) register */
7105 switch (ainfo->storage) {
7107 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7110 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7113 g_assert_not_reached ();
7116 if (ins == cfg->args [0]) {
7117 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7118 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7123 if (cfg->method->save_lmf)
7124 args_clobbered = TRUE;
7127 args_clobbered = TRUE;
7128 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7131 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7132 args_clobbered = TRUE;
7135 * Optimize the common case of the first bblock making a call with the same
7136 * arguments as the method. This works because the arguments are still in their
7137 * original argument registers.
7138 * FIXME: Generalize this
7140 if (!args_clobbered) {
7141 MonoBasicBlock *first_bb = cfg->bb_entry;
7143 int filter = FILTER_IL_SEQ_POINT;
7145 next = mono_bb_first_inst (first_bb, filter);
7146 if (!next && first_bb->next_bb) {
7147 first_bb = first_bb->next_bb;
7148 next = mono_bb_first_inst (first_bb, filter);
7151 if (first_bb->in_count > 1)
7154 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7155 ArgInfo *ainfo = cinfo->args + i;
7156 gboolean match = FALSE;
7158 ins = cfg->args [i];
7159 if (ins->opcode != OP_REGVAR) {
7160 switch (ainfo->storage) {
7162 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7163 if (next->dreg == ainfo->reg) {
7167 next->opcode = OP_MOVE;
7168 next->sreg1 = ainfo->reg;
7169 /* Only continue if the instruction doesn't change argument regs */
7170 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7180 /* Argument allocated to (non-volatile) register */
7181 switch (ainfo->storage) {
7183 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7194 next = mono_inst_next (next, filter);
7195 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7202 if (cfg->gen_seq_points_debug_data) {
7203 MonoInst *info_var = cfg->arch.seq_point_info_var;
7205 /* Initialize seq_point_info_var */
7206 if (cfg->compile_aot) {
7207 /* Initialize the variable from a GOT slot */
7208 /* Same as OP_AOTCONST */
7209 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7210 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7211 g_assert (info_var->opcode == OP_REGOFFSET);
7212 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7215 /* Initialize ss_trigger_page_var */
7216 ins = cfg->arch.ss_trigger_page_var;
7218 g_assert (ins->opcode == OP_REGOFFSET);
7220 if (cfg->compile_aot) {
7221 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7222 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page), 8);
7224 amd64_mov_reg_imm (code, AMD64_R11, (guint64)ss_trigger_page);
7226 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7229 cfg->code_len = code - cfg->native_code;
7231 g_assert (cfg->code_len < cfg->code_size);
7237 mono_arch_emit_epilog (MonoCompile *cfg)
7239 MonoMethod *method = cfg->method;
7242 int max_epilog_size;
7244 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7245 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7247 max_epilog_size = get_max_epilog_size (cfg);
7249 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7250 cfg->code_size *= 2;
7251 cfg->native_code = mono_realloc_native_code (cfg);
7252 cfg->stat_code_reallocs++;
7254 code = cfg->native_code + cfg->code_len;
7256 cfg->has_unwind_info_for_epilog = TRUE;
7258 /* Mark the start of the epilog */
7259 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7261 /* Save the uwind state which is needed by the out-of-line code */
7262 mono_emit_unwind_op_remember_state (cfg, code);
7264 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7265 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7267 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7269 if (method->save_lmf) {
7270 /* check if we need to restore protection of the stack after a stack overflow */
7271 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7273 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7274 /* we load the value in a separate instruction: this mechanism may be
7275 * used later as a safer way to do thread interruption
7277 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7278 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7280 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7281 /* note that the call trampoline will preserve eax/edx */
7282 x86_call_reg (code, X86_ECX);
7283 x86_patch (patch, code);
7285 /* FIXME: maybe save the jit tls in the prolog */
7287 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7288 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7292 /* Restore callee saved regs */
7293 for (i = 0; i < AMD64_NREG; ++i) {
7294 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7295 /* Restore only used_int_regs, not arch.saved_iregs */
7296 if (cfg->used_int_regs & (1 << i)) {
7297 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7298 mono_emit_unwind_op_same_value (cfg, code, i);
7299 async_exc_point (code);
7301 save_area_offset += 8;
7305 /* Load returned vtypes into registers if needed */
7306 cinfo = cfg->arch.cinfo;
7307 if (cinfo->ret.storage == ArgValuetypeInReg) {
7308 ArgInfo *ainfo = &cinfo->ret;
7309 MonoInst *inst = cfg->ret;
7311 for (quad = 0; quad < 2; quad ++) {
7312 switch (ainfo->pair_storage [quad]) {
7314 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7316 case ArgInFloatSSEReg:
7317 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7319 case ArgInDoubleSSEReg:
7320 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7325 g_assert_not_reached ();
7330 if (cfg->arch.omit_fp) {
7331 if (cfg->arch.stack_alloc_size) {
7332 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7336 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7338 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7339 async_exc_point (code);
7342 /* Restore the unwind state to be the same as before the epilog */
7343 mono_emit_unwind_op_restore_state (cfg, code);
7345 cfg->code_len = code - cfg->native_code;
7347 g_assert (cfg->code_len < cfg->code_size);
7351 mono_arch_emit_exceptions (MonoCompile *cfg)
7353 MonoJumpInfo *patch_info;
7356 MonoClass *exc_classes [16];
7357 guint8 *exc_throw_start [16], *exc_throw_end [16];
7358 guint32 code_size = 0;
7360 /* Compute needed space */
7361 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7362 if (patch_info->type == MONO_PATCH_INFO_EXC)
7364 if (patch_info->type == MONO_PATCH_INFO_R8)
7365 code_size += 8 + 15; /* sizeof (double) + alignment */
7366 if (patch_info->type == MONO_PATCH_INFO_R4)
7367 code_size += 4 + 15; /* sizeof (float) + alignment */
7368 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7369 code_size += 8 + 7; /*sizeof (void*) + alignment */
7372 #ifdef __native_client_codegen__
7373 /* Give us extra room on Native Client. This could be */
7374 /* more carefully calculated, but bundle alignment makes */
7375 /* it much trickier, so *2 like other places is good. */
7379 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7380 cfg->code_size *= 2;
7381 cfg->native_code = mono_realloc_native_code (cfg);
7382 cfg->stat_code_reallocs++;
7385 code = cfg->native_code + cfg->code_len;
7387 /* add code to raise exceptions */
7389 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7390 switch (patch_info->type) {
7391 case MONO_PATCH_INFO_EXC: {
7392 MonoClass *exc_class;
7396 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7398 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7399 g_assert (exc_class);
7400 throw_ip = patch_info->ip.i;
7402 //x86_breakpoint (code);
7403 /* Find a throw sequence for the same exception class */
7404 for (i = 0; i < nthrows; ++i)
7405 if (exc_classes [i] == exc_class)
7408 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7409 x86_jump_code (code, exc_throw_start [i]);
7410 patch_info->type = MONO_PATCH_INFO_NONE;
7414 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7418 exc_classes [nthrows] = exc_class;
7419 exc_throw_start [nthrows] = code;
7421 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7423 patch_info->type = MONO_PATCH_INFO_NONE;
7425 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7427 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7432 exc_throw_end [nthrows] = code;
7442 g_assert(code < cfg->native_code + cfg->code_size);
7445 /* Handle relocations with RIP relative addressing */
7446 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7447 gboolean remove = FALSE;
7448 guint8 *orig_code = code;
7450 switch (patch_info->type) {
7451 case MONO_PATCH_INFO_R8:
7452 case MONO_PATCH_INFO_R4: {
7453 guint8 *pos, *patch_pos;
7456 /* The SSE opcodes require a 16 byte alignment */
7457 #if defined(__default_codegen__)
7458 code = (guint8*)ALIGN_TO (code, 16);
7459 #elif defined(__native_client_codegen__)
7461 /* Pad this out with HLT instructions */
7462 /* or we can get garbage bytes emitted */
7463 /* which will fail validation */
7464 guint8 *aligned_code;
7465 /* extra align to make room for */
7466 /* mov/push below */
7467 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7468 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7469 /* The technique of hiding data in an */
7470 /* instruction has a problem here: we */
7471 /* need the data aligned to a 16-byte */
7472 /* boundary but the instruction cannot */
7473 /* cross the bundle boundary. so only */
7474 /* odd multiples of 16 can be used */
7475 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7478 while (code < aligned_code) {
7479 *(code++) = 0xf4; /* hlt */
7484 pos = cfg->native_code + patch_info->ip.i;
7485 if (IS_REX (pos [1])) {
7486 patch_pos = pos + 5;
7487 target_pos = code - pos - 9;
7490 patch_pos = pos + 4;
7491 target_pos = code - pos - 8;
7494 if (patch_info->type == MONO_PATCH_INFO_R8) {
7495 #ifdef __native_client_codegen__
7496 /* Hide 64-bit data in a */
7497 /* "mov imm64, r11" instruction. */
7498 /* write it before the start of */
7500 *(code-2) = 0x49; /* prefix */
7501 *(code-1) = 0xbb; /* mov X, %r11 */
7503 *(double*)code = *(double*)patch_info->data.target;
7504 code += sizeof (double);
7506 #ifdef __native_client_codegen__
7507 /* Hide 32-bit data in a */
7508 /* "push imm32" instruction. */
7509 *(code-1) = 0x68; /* push */
7511 *(float*)code = *(float*)patch_info->data.target;
7512 code += sizeof (float);
7515 *(guint32*)(patch_pos) = target_pos;
7520 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7523 if (cfg->compile_aot)
7526 /*loading is faster against aligned addresses.*/
7527 code = (guint8*)ALIGN_TO (code, 8);
7528 memset (orig_code, 0, code - orig_code);
7530 pos = cfg->native_code + patch_info->ip.i;
7532 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7533 if (IS_REX (pos [1]))
7534 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7536 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7538 *(gpointer*)code = (gpointer)patch_info->data.target;
7539 code += sizeof (gpointer);
7549 if (patch_info == cfg->patch_info)
7550 cfg->patch_info = patch_info->next;
7554 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7556 tmp->next = patch_info->next;
7559 g_assert (code < cfg->native_code + cfg->code_size);
7562 cfg->code_len = code - cfg->native_code;
7564 g_assert (cfg->code_len < cfg->code_size);
7568 #endif /* DISABLE_JIT */
7571 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7574 MonoMethodSignature *sig;
7576 int i, n, stack_area = 0;
7578 /* Keep this in sync with mono_arch_get_argument_info */
7580 if (enable_arguments) {
7581 /* Allocate a new area on the stack and save arguments there */
7582 sig = mono_method_signature (cfg->method);
7584 n = sig->param_count + sig->hasthis;
7586 stack_area = ALIGN_TO (n * 8, 16);
7588 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7590 for (i = 0; i < n; ++i) {
7591 inst = cfg->args [i];
7593 if (inst->opcode == OP_REGVAR)
7594 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7596 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7597 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7602 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7603 amd64_set_reg_template (code, AMD64_ARG_REG1);
7604 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7605 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7607 if (enable_arguments)
7608 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7622 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7625 int save_mode = SAVE_NONE;
7626 MonoMethod *method = cfg->method;
7627 MonoType *ret_type = mini_get_underlying_type (cfg, mono_method_signature (method)->ret);
7630 switch (ret_type->type) {
7631 case MONO_TYPE_VOID:
7632 /* special case string .ctor icall */
7633 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7634 save_mode = SAVE_EAX;
7636 save_mode = SAVE_NONE;
7640 save_mode = SAVE_EAX;
7644 save_mode = SAVE_XMM;
7646 case MONO_TYPE_GENERICINST:
7647 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7648 save_mode = SAVE_EAX;
7652 case MONO_TYPE_VALUETYPE:
7653 save_mode = SAVE_STRUCT;
7656 save_mode = SAVE_EAX;
7660 /* Save the result and copy it into the proper argument register */
7661 switch (save_mode) {
7663 amd64_push_reg (code, AMD64_RAX);
7665 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7666 if (enable_arguments)
7667 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7671 if (enable_arguments)
7672 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7675 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7676 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7678 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7680 * The result is already in the proper argument register so no copying
7687 g_assert_not_reached ();
7690 /* Set %al since this is a varargs call */
7691 if (save_mode == SAVE_XMM)
7692 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7694 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7696 if (preserve_argument_registers) {
7697 for (i = 0; i < PARAM_REGS; ++i)
7698 amd64_push_reg (code, param_regs [i]);
7701 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7702 amd64_set_reg_template (code, AMD64_ARG_REG1);
7703 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7705 if (preserve_argument_registers) {
7706 for (i = PARAM_REGS - 1; i >= 0; --i)
7707 amd64_pop_reg (code, param_regs [i]);
7710 /* Restore result */
7711 switch (save_mode) {
7713 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7714 amd64_pop_reg (code, AMD64_RAX);
7720 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7721 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7722 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7727 g_assert_not_reached ();
7734 mono_arch_flush_icache (guint8 *code, gint size)
7740 mono_arch_flush_register_windows (void)
7745 mono_arch_is_inst_imm (gint64 imm)
7747 return amd64_is_imm32 (imm);
7751 * Determine whenever the trap whose info is in SIGINFO is caused by
7755 mono_arch_is_int_overflow (void *sigctx, void *info)
7762 mono_sigctx_to_monoctx (sigctx, &ctx);
7764 rip = (guint8*)ctx.rip;
7766 if (IS_REX (rip [0])) {
7767 reg = amd64_rex_b (rip [0]);
7773 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7775 reg += x86_modrm_rm (rip [1]);
7815 g_assert_not_reached ();
7827 mono_arch_get_patch_offset (guint8 *code)
7833 * mono_breakpoint_clean_code:
7835 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
7836 * breakpoints in the original code, they are removed in the copy.
7838 * Returns TRUE if no sw breakpoint was present.
7841 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
7844 * If method_start is non-NULL we need to perform bound checks, since we access memory
7845 * at code - offset we could go before the start of the method and end up in a different
7846 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
7849 if (!method_start || code - offset >= method_start) {
7850 memcpy (buf, code - offset, size);
7852 int diff = code - method_start;
7853 memset (buf, 0, size);
7854 memcpy (buf + offset - diff, method_start, diff + size - offset);
7859 #if defined(__native_client_codegen__)
7860 /* For membase calls, we want the base register. for Native Client, */
7861 /* all indirect calls have the following sequence with the given sizes: */
7862 /* mov %eXX,%eXX [2-3] */
7863 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
7864 /* and $0xffffffffffffffe0,%r11d [4] */
7865 /* add %r15,%r11 [3] */
7866 /* callq *%r11 [3] */
7869 /* Determine if code points to a NaCl call-through-register sequence, */
7870 /* (i.e., the last 3 instructions listed above) */
7872 is_nacl_call_reg_sequence(guint8* code)
7874 const char *sequence = "\x41\x83\xe3\xe0" /* and */
7875 "\x4d\x03\xdf" /* add */
7876 "\x41\xff\xd3"; /* call */
7877 return memcmp(code, sequence, 10) == 0;
7880 /* Determine if code points to the first opcode of the mov membase component */
7881 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
7882 /* (there could be a REX prefix before the opcode but it is ignored) */
7884 is_nacl_indirect_call_membase_sequence(guint8* code)
7886 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
7887 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
7888 /* and that src reg = dest reg */
7889 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
7890 /* Check that next inst is mov, uses SIB byte (rm = 4), */
7892 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
7893 /* and has dst of r11 and base of r15 */
7894 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
7895 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
7897 #endif /* __native_client_codegen__ */
7900 mono_arch_get_this_arg_reg (guint8 *code)
7902 return AMD64_ARG_REG1;
7906 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
7908 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
7911 #define MAX_ARCH_DELEGATE_PARAMS 10
7914 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len)
7916 guint8 *code, *start;
7920 start = code = mono_global_codeman_reserve (64);
7922 /* Replace the this argument with the target */
7923 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7924 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
7925 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7927 g_assert ((code - start) < 64);
7929 start = code = mono_global_codeman_reserve (64);
7931 if (param_count == 0) {
7932 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7934 /* We have to shift the arguments left */
7935 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7936 for (i = 0; i < param_count; ++i) {
7939 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7941 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
7943 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7947 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7949 g_assert ((code - start) < 64);
7952 nacl_global_codeman_validate (&start, 64, &code);
7955 *code_len = code - start;
7957 if (mono_jit_map_is_enabled ()) {
7960 buff = (char*)"delegate_invoke_has_target";
7962 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
7963 mono_emit_jit_tramp (start, code - start, buff);
7967 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
7973 * mono_arch_get_delegate_invoke_impls:
7975 * Return a list of MonoTrampInfo structures for the delegate invoke impl
7979 mono_arch_get_delegate_invoke_impls (void)
7987 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
7988 res = g_slist_prepend (res, mono_tramp_info_create ("delegate_invoke_impl_has_target", code, code_len, NULL, NULL));
7990 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
7991 code = get_delegate_invoke_impl (FALSE, i, &code_len);
7992 tramp_name = g_strdup_printf ("delegate_invoke_impl_target_%d", i);
7993 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
7994 g_free (tramp_name);
8001 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
8003 guint8 *code, *start;
8006 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
8009 /* FIXME: Support more cases */
8010 if (MONO_TYPE_ISSTRUCT (mini_replace_type (sig->ret)))
8014 static guint8* cached = NULL;
8020 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
8022 start = get_delegate_invoke_impl (TRUE, 0, NULL);
8024 mono_memory_barrier ();
8028 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
8029 for (i = 0; i < sig->param_count; ++i)
8030 if (!mono_is_regsize_var (sig->params [i]))
8032 if (sig->param_count > 4)
8035 code = cache [sig->param_count];
8039 if (mono_aot_only) {
8040 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
8041 start = mono_aot_get_trampoline (name);
8044 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
8047 mono_memory_barrier ();
8049 cache [sig->param_count] = start;
8056 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
8058 guint8 *code, *start;
8061 start = code = mono_global_codeman_reserve (size);
8063 /* Replace the this argument with the target */
8064 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8065 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8068 /* Load the IMT reg */
8069 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
8072 /* Load the vtable */
8073 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
8074 amd64_jump_membase (code, AMD64_RAX, offset);
8075 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8081 mono_arch_finish_init (void)
8083 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
8084 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
8089 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8093 #if defined(__default_codegen__)
8094 #define CMP_SIZE (6 + 1)
8095 #define CMP_REG_REG_SIZE (4 + 1)
8096 #define BR_SMALL_SIZE 2
8097 #define BR_LARGE_SIZE 6
8098 #define MOV_REG_IMM_SIZE 10
8099 #define MOV_REG_IMM_32BIT_SIZE 6
8100 #define JUMP_REG_SIZE (2 + 1)
8101 #elif defined(__native_client_codegen__)
8102 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8103 #define CMP_SIZE ((6 + 1) * 2 - 1)
8104 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8105 #define BR_SMALL_SIZE (2 * 2 - 1)
8106 #define BR_LARGE_SIZE (6 * 2 - 1)
8107 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8108 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8109 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8110 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8111 /* Jump membase's size is large and unpredictable */
8112 /* in native client, just pad it out a whole bundle. */
8113 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8117 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8119 int i, distance = 0;
8120 for (i = start; i < target; ++i)
8121 distance += imt_entries [i]->chunk_size;
8126 * LOCKING: called with the domain lock held
8129 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8130 gpointer fail_tramp)
8134 guint8 *code, *start;
8135 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8137 for (i = 0; i < count; ++i) {
8138 MonoIMTCheckItem *item = imt_entries [i];
8139 if (item->is_equals) {
8140 if (item->check_target_idx) {
8141 if (!item->compare_done) {
8142 if (amd64_is_imm32 (item->key))
8143 item->chunk_size += CMP_SIZE;
8145 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8147 if (item->has_target_code) {
8148 item->chunk_size += MOV_REG_IMM_SIZE;
8150 if (vtable_is_32bit)
8151 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8153 item->chunk_size += MOV_REG_IMM_SIZE;
8154 #ifdef __native_client_codegen__
8155 item->chunk_size += JUMP_MEMBASE_SIZE;
8158 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8161 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8162 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8164 if (vtable_is_32bit)
8165 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8167 item->chunk_size += MOV_REG_IMM_SIZE;
8168 item->chunk_size += JUMP_REG_SIZE;
8169 /* with assert below:
8170 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8172 #ifdef __native_client_codegen__
8173 item->chunk_size += JUMP_MEMBASE_SIZE;
8178 if (amd64_is_imm32 (item->key))
8179 item->chunk_size += CMP_SIZE;
8181 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8182 item->chunk_size += BR_LARGE_SIZE;
8183 imt_entries [item->check_target_idx]->compare_done = TRUE;
8185 size += item->chunk_size;
8187 #if defined(__native_client__) && defined(__native_client_codegen__)
8188 /* In Native Client, we don't re-use thunks, allocate from the */
8189 /* normal code manager paths. */
8190 code = mono_domain_code_reserve (domain, size);
8193 code = mono_method_alloc_generic_virtual_thunk (domain, size);
8195 code = mono_domain_code_reserve (domain, size);
8198 for (i = 0; i < count; ++i) {
8199 MonoIMTCheckItem *item = imt_entries [i];
8200 item->code_target = code;
8201 if (item->is_equals) {
8202 gboolean fail_case = !item->check_target_idx && fail_tramp;
8204 if (item->check_target_idx || fail_case) {
8205 if (!item->compare_done || fail_case) {
8206 if (amd64_is_imm32 (item->key))
8207 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8209 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8210 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8213 item->jmp_code = code;
8214 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8215 if (item->has_target_code) {
8216 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8217 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8219 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8220 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8224 amd64_patch (item->jmp_code, code);
8225 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8226 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8227 item->jmp_code = NULL;
8230 /* enable the commented code to assert on wrong method */
8232 if (amd64_is_imm32 (item->key))
8233 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8235 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8236 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8238 item->jmp_code = code;
8239 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8240 /* See the comment below about R10 */
8241 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8242 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8243 amd64_patch (item->jmp_code, code);
8244 amd64_breakpoint (code);
8245 item->jmp_code = NULL;
8247 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8248 needs to be preserved. R10 needs
8249 to be preserved for calls which
8250 require a runtime generic context,
8251 but interface calls don't. */
8252 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8253 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8257 if (amd64_is_imm32 (item->key))
8258 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8260 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8261 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8263 item->jmp_code = code;
8264 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8265 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8267 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8269 g_assert (code - item->code_target <= item->chunk_size);
8271 /* patch the branches to get to the target items */
8272 for (i = 0; i < count; ++i) {
8273 MonoIMTCheckItem *item = imt_entries [i];
8274 if (item->jmp_code) {
8275 if (item->check_target_idx) {
8276 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8282 mono_stats.imt_thunks_size += code - start;
8283 g_assert (code - start <= size);
8285 nacl_domain_code_validate(domain, &start, size, &code);
8286 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8292 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8294 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8298 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8300 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8304 mono_arch_get_cie_program (void)
8308 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8309 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8317 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8319 MonoInst *ins = NULL;
8322 if (cmethod->klass == mono_defaults.math_class) {
8323 if (strcmp (cmethod->name, "Sin") == 0) {
8325 } else if (strcmp (cmethod->name, "Cos") == 0) {
8327 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8329 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8333 if (opcode && fsig->param_count == 1) {
8334 MONO_INST_NEW (cfg, ins, opcode);
8335 ins->type = STACK_R8;
8336 ins->dreg = mono_alloc_freg (cfg);
8337 ins->sreg1 = args [0]->dreg;
8338 MONO_ADD_INS (cfg->cbb, ins);
8342 if (cfg->opt & MONO_OPT_CMOV) {
8343 if (strcmp (cmethod->name, "Min") == 0) {
8344 if (fsig->params [0]->type == MONO_TYPE_I4)
8346 if (fsig->params [0]->type == MONO_TYPE_U4)
8347 opcode = OP_IMIN_UN;
8348 else if (fsig->params [0]->type == MONO_TYPE_I8)
8350 else if (fsig->params [0]->type == MONO_TYPE_U8)
8351 opcode = OP_LMIN_UN;
8352 } else if (strcmp (cmethod->name, "Max") == 0) {
8353 if (fsig->params [0]->type == MONO_TYPE_I4)
8355 if (fsig->params [0]->type == MONO_TYPE_U4)
8356 opcode = OP_IMAX_UN;
8357 else if (fsig->params [0]->type == MONO_TYPE_I8)
8359 else if (fsig->params [0]->type == MONO_TYPE_U8)
8360 opcode = OP_LMAX_UN;
8364 if (opcode && fsig->param_count == 2) {
8365 MONO_INST_NEW (cfg, ins, opcode);
8366 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8367 ins->dreg = mono_alloc_ireg (cfg);
8368 ins->sreg1 = args [0]->dreg;
8369 ins->sreg2 = args [1]->dreg;
8370 MONO_ADD_INS (cfg->cbb, ins);
8374 /* OP_FREM is not IEEE compatible */
8375 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8376 MONO_INST_NEW (cfg, ins, OP_FREM);
8377 ins->inst_i0 = args [0];
8378 ins->inst_i1 = args [1];
8388 mono_arch_print_tree (MonoInst *tree, int arity)
8393 #define _CTX_REG(ctx,fld,i) ((&ctx->fld)[i])
8396 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8399 case AMD64_RCX: return ctx->rcx;
8400 case AMD64_RDX: return ctx->rdx;
8401 case AMD64_RBX: return ctx->rbx;
8402 case AMD64_RBP: return ctx->rbp;
8403 case AMD64_RSP: return ctx->rsp;
8405 return _CTX_REG (ctx, rax, reg);
8410 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8429 _CTX_REG (ctx, rax, reg) = val;
8434 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8436 gpointer *sp, old_value;
8440 bp = MONO_CONTEXT_GET_BP (ctx);
8441 sp = *(gpointer*)(bp + clause->exvar_offset);
8444 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8453 * mono_arch_emit_load_aotconst:
8455 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8456 * TARGET from the mscorlib GOT in full-aot code.
8457 * On AMD64, the result is placed into R11.
8460 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
8462 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8463 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8469 * mono_arch_get_trampolines:
8471 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8475 mono_arch_get_trampolines (gboolean aot)
8477 return mono_amd64_get_exception_trampolines (aot);
8480 /* Soft Debug support */
8481 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8484 * mono_arch_set_breakpoint:
8486 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8487 * The location should contain code emitted by OP_SEQ_POINT.
8490 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8493 guint8 *orig_code = code;
8496 guint32 native_offset = ip - (guint8*)ji->code_start;
8497 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8499 g_assert (info->bp_addrs [native_offset] == 0);
8500 info->bp_addrs [native_offset] = bp_trigger_page;
8503 * In production, we will use int3 (has to fix the size in the md
8504 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
8507 g_assert (code [0] == 0x90);
8508 if (breakpoint_size == 8) {
8509 amd64_mov_reg_mem (code, AMD64_R11, (guint64)bp_trigger_page, 4);
8511 amd64_mov_reg_imm_size (code, AMD64_R11, (guint64)bp_trigger_page, 8);
8512 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 4);
8515 g_assert (code - orig_code == breakpoint_size);
8520 * mono_arch_clear_breakpoint:
8522 * Clear the breakpoint at IP.
8525 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8531 guint32 native_offset = ip - (guint8*)ji->code_start;
8532 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8534 g_assert (info->bp_addrs [native_offset] == 0);
8535 info->bp_addrs [native_offset] = info;
8537 for (i = 0; i < breakpoint_size; ++i)
8543 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8546 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8547 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == bp_trigger_page)
8552 siginfo_t* sinfo = (siginfo_t*) info;
8553 /* Sometimes the address is off by 4 */
8554 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
8562 * mono_arch_skip_breakpoint:
8564 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8565 * we resume, the instruction is not executed again.
8568 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8571 /* amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8) */
8572 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 3);
8574 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + breakpoint_fault_size);
8579 * mono_arch_start_single_stepping:
8581 * Start single stepping.
8584 mono_arch_start_single_stepping (void)
8586 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
8590 * mono_arch_stop_single_stepping:
8592 * Stop single stepping.
8595 mono_arch_stop_single_stepping (void)
8597 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
8601 * mono_arch_is_single_step_event:
8603 * Return whenever the machine state in SIGCTX corresponds to a single
8607 mono_arch_is_single_step_event (void *info, void *sigctx)
8610 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8611 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == ss_trigger_page)
8616 siginfo_t* sinfo = (siginfo_t*) info;
8617 /* Sometimes the address is off by 4 */
8618 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
8626 * mono_arch_skip_single_step:
8628 * Modify CTX so the ip is placed after the single step trigger instruction,
8629 * we resume, the instruction is not executed again.
8632 mono_arch_skip_single_step (MonoContext *ctx)
8634 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + single_step_fault_size);
8638 * mono_arch_create_seq_point_info:
8640 * Return a pointer to a data structure which is used by the sequence
8641 * point implementation in AOTed code.
8644 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8650 // FIXME: Add a free function
8652 mono_domain_lock (domain);
8653 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8655 mono_domain_unlock (domain);
8658 ji = mono_jit_info_table_find (domain, (char*)code);
8661 // FIXME: Optimize the size
8662 info = g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8664 info->ss_trigger_page = ss_trigger_page;
8665 info->bp_trigger_page = bp_trigger_page;
8666 /* Initialize to a valid address */
8667 for (i = 0; i < ji->code_size; ++i)
8668 info->bp_addrs [i] = info;
8670 mono_domain_lock (domain);
8671 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8673 mono_domain_unlock (domain);
8680 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8682 ext->lmf.previous_lmf = prev_lmf;
8683 /* Mark that this is a MonoLMFExt */
8684 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8685 ext->lmf.rsp = (gssize)ext;
8691 mono_arch_opcode_supported (int opcode)
8694 case OP_ATOMIC_ADD_I4:
8695 case OP_ATOMIC_ADD_I8:
8696 case OP_ATOMIC_EXCHANGE_I4:
8697 case OP_ATOMIC_EXCHANGE_I8:
8698 case OP_ATOMIC_CAS_I4:
8699 case OP_ATOMIC_CAS_I8:
8700 case OP_ATOMIC_LOAD_I1:
8701 case OP_ATOMIC_LOAD_I2:
8702 case OP_ATOMIC_LOAD_I4:
8703 case OP_ATOMIC_LOAD_I8:
8704 case OP_ATOMIC_LOAD_U1:
8705 case OP_ATOMIC_LOAD_U2:
8706 case OP_ATOMIC_LOAD_U4:
8707 case OP_ATOMIC_LOAD_U8:
8708 case OP_ATOMIC_LOAD_R4:
8709 case OP_ATOMIC_LOAD_R8:
8710 case OP_ATOMIC_STORE_I1:
8711 case OP_ATOMIC_STORE_I2:
8712 case OP_ATOMIC_STORE_I4:
8713 case OP_ATOMIC_STORE_I8:
8714 case OP_ATOMIC_STORE_U1:
8715 case OP_ATOMIC_STORE_U2:
8716 case OP_ATOMIC_STORE_U4:
8717 case OP_ATOMIC_STORE_U8:
8718 case OP_ATOMIC_STORE_R4:
8719 case OP_ATOMIC_STORE_R8: