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;
1652 MonoMethodHeader *header;
1657 header = cfg->header;
1659 sig = mono_method_signature (cfg->method);
1661 cinfo = cfg->arch.cinfo;
1662 sig_ret = mini_get_underlying_type (cfg, sig->ret);
1665 * Contrary to mono_arch_allocate_vars (), the information should describe
1666 * where the arguments are at the beginning of the method, not where they can be
1667 * accessed during the execution of the method. The later makes no sense for the
1668 * global register allocator, since a variable can be in more than one location.
1670 if (sig_ret->type != MONO_TYPE_VOID) {
1671 switch (cinfo->ret.storage) {
1673 case ArgInFloatSSEReg:
1674 case ArgInDoubleSSEReg:
1675 if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->vtype_retaddr)) {
1676 cfg->vret_addr->opcode = OP_REGVAR;
1677 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1680 cfg->ret->opcode = OP_REGVAR;
1681 cfg->ret->inst_c0 = cinfo->ret.reg;
1684 case ArgValuetypeInReg:
1685 cfg->ret->opcode = OP_REGOFFSET;
1686 cfg->ret->inst_basereg = -1;
1687 cfg->ret->inst_offset = -1;
1690 g_assert_not_reached ();
1694 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1695 ArgInfo *ainfo = &cinfo->args [i];
1698 ins = cfg->args [i];
1700 if (sig->hasthis && (i == 0))
1701 arg_type = &mono_defaults.object_class->byval_arg;
1703 arg_type = sig->params [i - sig->hasthis];
1705 switch (ainfo->storage) {
1707 case ArgInFloatSSEReg:
1708 case ArgInDoubleSSEReg:
1709 ins->opcode = OP_REGVAR;
1710 ins->inst_c0 = ainfo->reg;
1713 ins->opcode = OP_REGOFFSET;
1714 ins->inst_basereg = -1;
1715 ins->inst_offset = -1;
1717 case ArgValuetypeInReg:
1719 ins->opcode = OP_NOP;
1722 g_assert_not_reached ();
1728 mono_arch_allocate_vars (MonoCompile *cfg)
1731 MonoMethodSignature *sig;
1732 MonoMethodHeader *header;
1735 guint32 locals_stack_size, locals_stack_align;
1739 header = cfg->header;
1741 sig = mono_method_signature (cfg->method);
1743 cinfo = cfg->arch.cinfo;
1744 sig_ret = mini_get_underlying_type (cfg, sig->ret);
1746 mono_arch_compute_omit_fp (cfg);
1749 * We use the ABI calling conventions for managed code as well.
1750 * Exception: valuetypes are only sometimes passed or returned in registers.
1754 * The stack looks like this:
1755 * <incoming arguments passed on the stack>
1757 * <lmf/caller saved registers>
1760 * <localloc area> -> grows dynamically
1764 if (cfg->arch.omit_fp) {
1765 cfg->flags |= MONO_CFG_HAS_SPILLUP;
1766 cfg->frame_reg = AMD64_RSP;
1769 /* Locals are allocated backwards from %fp */
1770 cfg->frame_reg = AMD64_RBP;
1774 cfg->arch.saved_iregs = cfg->used_int_regs;
1775 if (cfg->method->save_lmf)
1776 /* Save all callee-saved registers normally, and restore them when unwinding through an LMF */
1777 cfg->arch.saved_iregs |= (1 << AMD64_RBX) | (1 << AMD64_R12) | (1 << AMD64_R13) | (1 << AMD64_R14) | (1 << AMD64_R15);
1779 if (cfg->arch.omit_fp)
1780 cfg->arch.reg_save_area_offset = offset;
1781 /* Reserve space for callee saved registers */
1782 for (i = 0; i < AMD64_NREG; ++i)
1783 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
1784 offset += sizeof(mgreg_t);
1786 if (!cfg->arch.omit_fp)
1787 cfg->arch.reg_save_area_offset = -offset;
1789 if (sig_ret->type != MONO_TYPE_VOID) {
1790 switch (cinfo->ret.storage) {
1792 case ArgInFloatSSEReg:
1793 case ArgInDoubleSSEReg:
1794 if ((MONO_TYPE_ISSTRUCT (sig_ret) && !mono_class_from_mono_type (sig_ret)->enumtype) || ((sig_ret->type == MONO_TYPE_TYPEDBYREF) && cinfo->vtype_retaddr)) {
1795 if (cfg->globalra) {
1796 cfg->vret_addr->opcode = OP_REGVAR;
1797 cfg->vret_addr->inst_c0 = cinfo->ret.reg;
1799 /* The register is volatile */
1800 cfg->vret_addr->opcode = OP_REGOFFSET;
1801 cfg->vret_addr->inst_basereg = cfg->frame_reg;
1802 if (cfg->arch.omit_fp) {
1803 cfg->vret_addr->inst_offset = offset;
1807 cfg->vret_addr->inst_offset = -offset;
1809 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1810 printf ("vret_addr =");
1811 mono_print_ins (cfg->vret_addr);
1816 cfg->ret->opcode = OP_REGVAR;
1817 cfg->ret->inst_c0 = cinfo->ret.reg;
1820 case ArgValuetypeInReg:
1821 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1822 cfg->ret->opcode = OP_REGOFFSET;
1823 cfg->ret->inst_basereg = cfg->frame_reg;
1824 if (cfg->arch.omit_fp) {
1825 cfg->ret->inst_offset = offset;
1826 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1828 offset += cinfo->ret.pair_storage [1] == ArgNone ? 8 : 16;
1829 cfg->ret->inst_offset = - offset;
1833 g_assert_not_reached ();
1836 cfg->ret->dreg = cfg->ret->inst_c0;
1839 /* Allocate locals */
1840 if (!cfg->globalra) {
1841 offsets = mono_allocate_stack_slots (cfg, cfg->arch.omit_fp ? FALSE: TRUE, &locals_stack_size, &locals_stack_align);
1842 if (locals_stack_size > MONO_ARCH_MAX_FRAME_SIZE) {
1843 char *mname = mono_method_full_name (cfg->method, TRUE);
1844 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
1845 cfg->exception_message = g_strdup_printf ("Method %s stack is too big.", mname);
1850 if (locals_stack_align) {
1851 offset += (locals_stack_align - 1);
1852 offset &= ~(locals_stack_align - 1);
1854 if (cfg->arch.omit_fp) {
1855 cfg->locals_min_stack_offset = offset;
1856 cfg->locals_max_stack_offset = offset + locals_stack_size;
1858 cfg->locals_min_stack_offset = - (offset + locals_stack_size);
1859 cfg->locals_max_stack_offset = - offset;
1862 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1863 if (offsets [i] != -1) {
1864 MonoInst *ins = cfg->varinfo [i];
1865 ins->opcode = OP_REGOFFSET;
1866 ins->inst_basereg = cfg->frame_reg;
1867 if (cfg->arch.omit_fp)
1868 ins->inst_offset = (offset + offsets [i]);
1870 ins->inst_offset = - (offset + offsets [i]);
1871 //printf ("allocated local %d to ", i); mono_print_tree_nl (ins);
1874 offset += locals_stack_size;
1877 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG)) {
1878 g_assert (!cfg->arch.omit_fp);
1879 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
1880 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
1883 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1884 ins = cfg->args [i];
1885 if (ins->opcode != OP_REGVAR) {
1886 ArgInfo *ainfo = &cinfo->args [i];
1887 gboolean inreg = TRUE;
1890 if (sig->hasthis && (i == 0))
1891 arg_type = &mono_defaults.object_class->byval_arg;
1893 arg_type = sig->params [i - sig->hasthis];
1895 if (cfg->globalra) {
1896 /* The new allocator needs info about the original locations of the arguments */
1897 switch (ainfo->storage) {
1899 case ArgInFloatSSEReg:
1900 case ArgInDoubleSSEReg:
1901 ins->opcode = OP_REGVAR;
1902 ins->inst_c0 = ainfo->reg;
1905 g_assert (!cfg->arch.omit_fp);
1906 ins->opcode = OP_REGOFFSET;
1907 ins->inst_basereg = cfg->frame_reg;
1908 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1910 case ArgValuetypeInReg:
1911 ins->opcode = OP_REGOFFSET;
1912 ins->inst_basereg = cfg->frame_reg;
1913 /* These arguments are saved to the stack in the prolog */
1914 offset = ALIGN_TO (offset, sizeof(mgreg_t));
1915 if (cfg->arch.omit_fp) {
1916 ins->inst_offset = offset;
1917 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1919 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1920 ins->inst_offset = - offset;
1924 g_assert_not_reached ();
1930 /* FIXME: Allocate volatile arguments to registers */
1931 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
1935 * Under AMD64, all registers used to pass arguments to functions
1936 * are volatile across calls.
1937 * FIXME: Optimize this.
1939 if ((ainfo->storage == ArgInIReg) || (ainfo->storage == ArgInFloatSSEReg) || (ainfo->storage == ArgInDoubleSSEReg) || (ainfo->storage == ArgValuetypeInReg))
1942 ins->opcode = OP_REGOFFSET;
1944 switch (ainfo->storage) {
1946 case ArgInFloatSSEReg:
1947 case ArgInDoubleSSEReg:
1949 ins->opcode = OP_REGVAR;
1950 ins->dreg = ainfo->reg;
1954 g_assert (!cfg->arch.omit_fp);
1955 ins->opcode = OP_REGOFFSET;
1956 ins->inst_basereg = cfg->frame_reg;
1957 ins->inst_offset = ainfo->offset + ARGS_OFFSET;
1959 case ArgValuetypeInReg:
1961 case ArgValuetypeAddrInIReg: {
1963 g_assert (!cfg->arch.omit_fp);
1965 MONO_INST_NEW (cfg, indir, 0);
1966 indir->opcode = OP_REGOFFSET;
1967 if (ainfo->pair_storage [0] == ArgInIReg) {
1968 indir->inst_basereg = cfg->frame_reg;
1969 offset = ALIGN_TO (offset, sizeof (gpointer));
1970 offset += (sizeof (gpointer));
1971 indir->inst_offset = - offset;
1974 indir->inst_basereg = cfg->frame_reg;
1975 indir->inst_offset = ainfo->offset + ARGS_OFFSET;
1978 ins->opcode = OP_VTARG_ADDR;
1979 ins->inst_left = indir;
1987 if (!inreg && (ainfo->storage != ArgOnStack) && (ainfo->storage != ArgValuetypeAddrInIReg)) {
1988 ins->opcode = OP_REGOFFSET;
1989 ins->inst_basereg = cfg->frame_reg;
1990 /* These arguments are saved to the stack in the prolog */
1991 offset = ALIGN_TO (offset, sizeof(mgreg_t));
1992 if (cfg->arch.omit_fp) {
1993 ins->inst_offset = offset;
1994 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1995 // Arguments are yet supported by the stack map creation code
1996 //cfg->locals_max_stack_offset = MAX (cfg->locals_max_stack_offset, offset);
1998 offset += (ainfo->storage == ArgValuetypeInReg) ? ainfo->nregs * sizeof (mgreg_t) : sizeof (mgreg_t);
1999 ins->inst_offset = - offset;
2000 //cfg->locals_min_stack_offset = MIN (cfg->locals_min_stack_offset, offset);
2006 cfg->stack_offset = offset;
2010 mono_arch_create_vars (MonoCompile *cfg)
2012 MonoMethodSignature *sig;
2016 sig = mono_method_signature (cfg->method);
2018 if (!cfg->arch.cinfo)
2019 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
2020 cinfo = cfg->arch.cinfo;
2022 if (cinfo->ret.storage == ArgValuetypeInReg)
2023 cfg->ret_var_is_local = TRUE;
2025 sig_ret = mini_get_underlying_type (cfg, sig->ret);
2026 if ((cinfo->ret.storage != ArgValuetypeInReg) && MONO_TYPE_ISSTRUCT (sig_ret)) {
2027 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2028 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2029 printf ("vret_addr = ");
2030 mono_print_ins (cfg->vret_addr);
2034 if (cfg->gen_seq_points_debug_data) {
2037 if (cfg->compile_aot) {
2038 MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2039 ins->flags |= MONO_INST_VOLATILE;
2040 cfg->arch.seq_point_info_var = ins;
2043 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2044 ins->flags |= MONO_INST_VOLATILE;
2045 cfg->arch.ss_trigger_page_var = ins;
2048 if (cfg->method->save_lmf)
2049 cfg->create_lmf_var = TRUE;
2051 if (cfg->method->save_lmf) {
2053 #if !defined(HOST_WIN32)
2054 if (mono_get_lmf_tls_offset () != -1 && !optimize_for_xen)
2055 cfg->lmf_ir_mono_lmf = TRUE;
2061 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *tree)
2067 MONO_INST_NEW (cfg, ins, OP_MOVE);
2068 ins->dreg = mono_alloc_ireg_copy (cfg, tree->dreg);
2069 ins->sreg1 = tree->dreg;
2070 MONO_ADD_INS (cfg->cbb, ins);
2071 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, FALSE);
2073 case ArgInFloatSSEReg:
2074 MONO_INST_NEW (cfg, ins, OP_AMD64_SET_XMMREG_R4);
2075 ins->dreg = mono_alloc_freg (cfg);
2076 ins->sreg1 = tree->dreg;
2077 MONO_ADD_INS (cfg->cbb, ins);
2079 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2081 case ArgInDoubleSSEReg:
2082 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2083 ins->dreg = mono_alloc_freg (cfg);
2084 ins->sreg1 = tree->dreg;
2085 MONO_ADD_INS (cfg->cbb, ins);
2087 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2091 g_assert_not_reached ();
2096 arg_storage_to_load_membase (ArgStorage storage)
2100 #if defined(__mono_ilp32__)
2101 return OP_LOADI8_MEMBASE;
2103 return OP_LOAD_MEMBASE;
2105 case ArgInDoubleSSEReg:
2106 return OP_LOADR8_MEMBASE;
2107 case ArgInFloatSSEReg:
2108 return OP_LOADR4_MEMBASE;
2110 g_assert_not_reached ();
2117 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2119 MonoMethodSignature *tmp_sig;
2122 if (call->tail_call)
2125 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
2128 * mono_ArgIterator_Setup assumes the signature cookie is
2129 * passed first and all the arguments which were before it are
2130 * passed on the stack after the signature. So compensate by
2131 * passing a different signature.
2133 tmp_sig = mono_metadata_signature_dup_full (cfg->method->klass->image, call->signature);
2134 tmp_sig->param_count -= call->signature->sentinelpos;
2135 tmp_sig->sentinelpos = 0;
2136 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2138 sig_reg = mono_alloc_ireg (cfg);
2139 MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2141 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, cinfo->sig_cookie.offset, sig_reg);
2144 static inline LLVMArgStorage
2145 arg_storage_to_llvm_arg_storage (MonoCompile *cfg, ArgStorage storage)
2149 return LLVMArgInIReg;
2153 g_assert_not_reached ();
2160 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2166 LLVMCallInfo *linfo;
2167 MonoType *t, *sig_ret;
2169 n = sig->param_count + sig->hasthis;
2170 sig_ret = mini_get_underlying_type (cfg, sig->ret);
2172 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
2174 linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2177 * LLVM always uses the native ABI while we use our own ABI, the
2178 * only difference is the handling of vtypes:
2179 * - we only pass/receive them in registers in some cases, and only
2180 * in 1 or 2 integer registers.
2182 if (cinfo->ret.storage == ArgValuetypeInReg) {
2184 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2185 cfg->disable_llvm = TRUE;
2189 linfo->ret.storage = LLVMArgVtypeInReg;
2190 for (j = 0; j < 2; ++j)
2191 linfo->ret.pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, cinfo->ret.pair_storage [j]);
2194 if (MONO_TYPE_ISSTRUCT (sig_ret) && cinfo->ret.storage == ArgInIReg) {
2195 /* Vtype returned using a hidden argument */
2196 linfo->ret.storage = LLVMArgVtypeRetAddr;
2197 linfo->vret_arg_index = cinfo->vret_arg_index;
2200 for (i = 0; i < n; ++i) {
2201 ainfo = cinfo->args + i;
2203 if (i >= sig->hasthis)
2204 t = sig->params [i - sig->hasthis];
2206 t = &mono_defaults.int_class->byval_arg;
2208 linfo->args [i].storage = LLVMArgNone;
2210 switch (ainfo->storage) {
2212 linfo->args [i].storage = LLVMArgInIReg;
2214 case ArgInDoubleSSEReg:
2215 case ArgInFloatSSEReg:
2216 linfo->args [i].storage = LLVMArgInFPReg;
2219 if (MONO_TYPE_ISSTRUCT (t)) {
2220 linfo->args [i].storage = LLVMArgVtypeByVal;
2222 linfo->args [i].storage = LLVMArgInIReg;
2224 if (t->type == MONO_TYPE_R4)
2225 linfo->args [i].storage = LLVMArgInFPReg;
2226 else if (t->type == MONO_TYPE_R8)
2227 linfo->args [i].storage = LLVMArgInFPReg;
2231 case ArgValuetypeInReg:
2233 cfg->exception_message = g_strdup ("pinvoke + vtypes");
2234 cfg->disable_llvm = TRUE;
2238 linfo->args [i].storage = LLVMArgVtypeInReg;
2239 for (j = 0; j < 2; ++j)
2240 linfo->args [i].pair_storage [j] = arg_storage_to_llvm_arg_storage (cfg, ainfo->pair_storage [j]);
2243 cfg->exception_message = g_strdup ("ainfo->storage");
2244 cfg->disable_llvm = TRUE;
2254 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2257 MonoMethodSignature *sig;
2259 int i, n, stack_size;
2265 sig = call->signature;
2266 n = sig->param_count + sig->hasthis;
2268 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
2272 if (COMPILE_LLVM (cfg)) {
2273 /* We shouldn't be called in the llvm case */
2274 cfg->disable_llvm = TRUE;
2279 * Emit all arguments which are passed on the stack to prevent register
2280 * allocation problems.
2282 for (i = 0; i < n; ++i) {
2284 ainfo = cinfo->args + i;
2286 in = call->args [i];
2288 if (sig->hasthis && i == 0)
2289 t = &mono_defaults.object_class->byval_arg;
2291 t = sig->params [i - sig->hasthis];
2293 if (ainfo->storage == ArgOnStack && !MONO_TYPE_ISSTRUCT (t) && !call->tail_call) {
2295 if (t->type == MONO_TYPE_R4)
2296 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2297 else if (t->type == MONO_TYPE_R8)
2298 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2300 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2302 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, in->dreg);
2304 if (cfg->compute_gc_maps) {
2307 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, t);
2313 * Emit all parameters passed in registers in non-reverse order for better readability
2314 * and to help the optimization in emit_prolog ().
2316 for (i = 0; i < n; ++i) {
2317 ainfo = cinfo->args + i;
2319 in = call->args [i];
2321 if (ainfo->storage == ArgInIReg)
2322 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2325 for (i = n - 1; i >= 0; --i) {
2326 ainfo = cinfo->args + i;
2328 in = call->args [i];
2330 switch (ainfo->storage) {
2334 case ArgInFloatSSEReg:
2335 case ArgInDoubleSSEReg:
2336 add_outarg_reg (cfg, call, ainfo->storage, ainfo->reg, in);
2339 case ArgValuetypeInReg:
2340 case ArgValuetypeAddrInIReg:
2341 if (ainfo->storage == ArgOnStack && call->tail_call) {
2342 MonoInst *call_inst = (MonoInst*)call;
2343 cfg->args [i]->flags |= MONO_INST_VOLATILE;
2344 EMIT_NEW_ARGSTORE (cfg, call_inst, i, in);
2345 } else if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
2349 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
2350 size = sizeof (MonoTypedRef);
2351 align = sizeof (gpointer);
2355 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
2358 * Other backends use mono_type_stack_size (), but that
2359 * aligns the size to 8, which is larger than the size of
2360 * the source, leading to reads of invalid memory if the
2361 * source is at the end of address space.
2363 size = mono_class_value_size (in->klass, &align);
2366 g_assert (in->klass);
2368 if (ainfo->storage == ArgOnStack && size >= 10000) {
2369 /* Avoid asserts in emit_memcpy () */
2370 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
2371 cfg->exception_message = g_strdup_printf ("Passing an argument of size '%d'.", size);
2372 /* Continue normally */
2376 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
2377 arg->sreg1 = in->dreg;
2378 arg->klass = in->klass;
2379 arg->backend.size = size;
2380 arg->inst_p0 = call;
2381 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2382 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
2384 MONO_ADD_INS (cfg->cbb, arg);
2389 g_assert_not_reached ();
2392 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos))
2393 /* Emit the signature cookie just before the implicit arguments */
2394 emit_sig_cookie (cfg, call, cinfo);
2397 /* Handle the case where there are no implicit arguments */
2398 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2399 emit_sig_cookie (cfg, call, cinfo);
2401 sig_ret = mini_get_underlying_type (cfg, sig->ret);
2402 if (sig_ret && MONO_TYPE_ISSTRUCT (sig_ret)) {
2405 if (cinfo->ret.storage == ArgValuetypeInReg) {
2406 if (cinfo->ret.pair_storage [0] == ArgInIReg && cinfo->ret.pair_storage [1] == ArgNone) {
2408 * Tell the JIT to use a more efficient calling convention: call using
2409 * OP_CALL, compute the result location after the call, and save the
2412 call->vret_in_reg = TRUE;
2414 * Nullify the instruction computing the vret addr to enable
2415 * future optimizations.
2418 NULLIFY_INS (call->vret_var);
2420 if (call->tail_call)
2423 * The valuetype is in RAX:RDX after the call, need to be copied to
2424 * the stack. Push the address here, so the call instruction can
2427 if (!cfg->arch.vret_addr_loc) {
2428 cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2429 /* Prevent it from being register allocated or optimized away */
2430 ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2433 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2437 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2438 vtarg->sreg1 = call->vret_var->dreg;
2439 vtarg->dreg = mono_alloc_preg (cfg);
2440 MONO_ADD_INS (cfg->cbb, vtarg);
2442 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2446 if (cfg->method->save_lmf) {
2447 MONO_INST_NEW (cfg, arg, OP_AMD64_SAVE_SP_TO_LMF);
2448 MONO_ADD_INS (cfg->cbb, arg);
2451 call->stack_usage = cinfo->stack_usage;
2455 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2458 MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2459 ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
2460 int size = ins->backend.size;
2462 if (ainfo->storage == ArgValuetypeInReg) {
2466 for (part = 0; part < 2; ++part) {
2467 if (ainfo->pair_storage [part] == ArgNone)
2470 MONO_INST_NEW (cfg, load, arg_storage_to_load_membase (ainfo->pair_storage [part]));
2471 load->inst_basereg = src->dreg;
2472 load->inst_offset = part * sizeof(mgreg_t);
2474 switch (ainfo->pair_storage [part]) {
2476 load->dreg = mono_alloc_ireg (cfg);
2478 case ArgInDoubleSSEReg:
2479 case ArgInFloatSSEReg:
2480 load->dreg = mono_alloc_freg (cfg);
2483 g_assert_not_reached ();
2485 MONO_ADD_INS (cfg->cbb, load);
2487 add_outarg_reg (cfg, call, ainfo->pair_storage [part], ainfo->pair_regs [part], load);
2489 } else if (ainfo->storage == ArgValuetypeAddrInIReg) {
2490 MonoInst *vtaddr, *load;
2491 vtaddr = mono_compile_create_var (cfg, &ins->klass->byval_arg, OP_LOCAL);
2493 MONO_INST_NEW (cfg, load, OP_LDADDR);
2494 cfg->has_indirection = TRUE;
2495 load->inst_p0 = vtaddr;
2496 vtaddr->flags |= MONO_INST_INDIRECT;
2497 load->type = STACK_MP;
2498 load->klass = vtaddr->klass;
2499 load->dreg = mono_alloc_ireg (cfg);
2500 MONO_ADD_INS (cfg->cbb, load);
2501 mini_emit_memcpy (cfg, load->dreg, 0, src->dreg, 0, size, 4);
2503 if (ainfo->pair_storage [0] == ArgInIReg) {
2504 MONO_INST_NEW (cfg, arg, OP_X86_LEA_MEMBASE);
2505 arg->dreg = mono_alloc_ireg (cfg);
2506 arg->sreg1 = load->dreg;
2508 MONO_ADD_INS (cfg->cbb, arg);
2509 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, ainfo->pair_regs [0], FALSE);
2511 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, load->dreg);
2515 int dreg = mono_alloc_ireg (cfg);
2517 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, 0);
2518 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, AMD64_RSP, ainfo->offset, dreg);
2519 } else if (size <= 40) {
2520 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2522 // FIXME: Code growth
2523 mini_emit_memcpy (cfg, AMD64_RSP, ainfo->offset, src->dreg, 0, size, 4);
2526 if (cfg->compute_gc_maps) {
2528 EMIT_NEW_GC_PARAM_SLOT_LIVENESS_DEF (cfg, def, ainfo->offset, &ins->klass->byval_arg);
2534 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2536 MonoType *ret = mini_get_underlying_type (cfg, mono_method_signature (method)->ret);
2538 if (ret->type == MONO_TYPE_R4) {
2539 if (COMPILE_LLVM (cfg))
2540 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2542 MONO_EMIT_NEW_UNALU (cfg, OP_AMD64_SET_XMMREG_R4, cfg->ret->dreg, val->dreg);
2544 } else if (ret->type == MONO_TYPE_R8) {
2545 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2549 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2552 #endif /* DISABLE_JIT */
2554 #define EMIT_COND_BRANCH(ins,cond,sign) \
2555 if (ins->inst_true_bb->native_offset) { \
2556 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
2558 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2559 if ((cfg->opt & MONO_OPT_BRANCH) && \
2560 x86_is_imm8 (ins->inst_true_bb->max_offset - offset)) \
2561 x86_branch8 (code, cond, 0, sign); \
2563 x86_branch32 (code, cond, 0, sign); \
2567 MonoMethodSignature *sig;
2572 dyn_call_supported (MonoMethodSignature *sig, CallInfo *cinfo)
2580 switch (cinfo->ret.storage) {
2584 case ArgValuetypeInReg: {
2585 ArgInfo *ainfo = &cinfo->ret;
2587 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2589 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2597 for (i = 0; i < cinfo->nargs; ++i) {
2598 ArgInfo *ainfo = &cinfo->args [i];
2599 switch (ainfo->storage) {
2602 case ArgValuetypeInReg:
2603 if (ainfo->pair_storage [0] != ArgNone && ainfo->pair_storage [0] != ArgInIReg)
2605 if (ainfo->pair_storage [1] != ArgNone && ainfo->pair_storage [1] != ArgInIReg)
2617 * mono_arch_dyn_call_prepare:
2619 * Return a pointer to an arch-specific structure which contains information
2620 * needed by mono_arch_get_dyn_call_args (). Return NULL if OP_DYN_CALL is not
2621 * supported for SIG.
2622 * This function is equivalent to ffi_prep_cif in libffi.
2625 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2627 ArchDynCallInfo *info;
2630 cinfo = get_call_info (NULL, NULL, sig);
2632 if (!dyn_call_supported (sig, cinfo)) {
2637 info = g_new0 (ArchDynCallInfo, 1);
2638 // FIXME: Preprocess the info to speed up get_dyn_call_args ().
2640 info->cinfo = cinfo;
2642 return (MonoDynCallInfo*)info;
2646 * mono_arch_dyn_call_free:
2648 * Free a MonoDynCallInfo structure.
2651 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2653 ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2655 g_free (ainfo->cinfo);
2659 #if !defined(__native_client__)
2660 #define PTR_TO_GREG(ptr) (mgreg_t)(ptr)
2661 #define GREG_TO_PTR(greg) (gpointer)(greg)
2663 /* Correctly handle casts to/from 32-bit pointers without compiler warnings */
2664 #define PTR_TO_GREG(ptr) (mgreg_t)(uintptr_t)(ptr)
2665 #define GREG_TO_PTR(greg) (gpointer)(guint32)(greg)
2669 * mono_arch_get_start_dyn_call:
2671 * Convert the arguments ARGS to a format which can be passed to OP_DYN_CALL, and
2672 * store the result into BUF.
2673 * ARGS should be an array of pointers pointing to the arguments.
2674 * RET should point to a memory buffer large enought to hold the result of the
2676 * This function should be as fast as possible, any work which does not depend
2677 * on the actual values of the arguments should be done in
2678 * mono_arch_dyn_call_prepare ().
2679 * start_dyn_call + OP_DYN_CALL + finish_dyn_call is equivalent to ffi_call in
2683 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2685 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2686 DynCallArgs *p = (DynCallArgs*)buf;
2687 int arg_index, greg, i, pindex;
2688 MonoMethodSignature *sig = dinfo->sig;
2690 g_assert (buf_len >= sizeof (DynCallArgs));
2699 if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2700 p->regs [greg ++] = PTR_TO_GREG(*(args [arg_index ++]));
2705 if (dinfo->cinfo->vtype_retaddr)
2706 p->regs [greg ++] = PTR_TO_GREG(ret);
2708 for (i = pindex; i < sig->param_count; i++) {
2709 MonoType *t = mono_type_get_underlying_type (sig->params [i]);
2710 gpointer *arg = args [arg_index ++];
2713 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2718 case MONO_TYPE_STRING:
2719 case MONO_TYPE_CLASS:
2720 case MONO_TYPE_ARRAY:
2721 case MONO_TYPE_SZARRAY:
2722 case MONO_TYPE_OBJECT:
2726 #if !defined(__mono_ilp32__)
2730 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2731 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2733 #if defined(__mono_ilp32__)
2736 g_assert (dinfo->cinfo->args [i + sig->hasthis].reg == param_regs [greg]);
2737 p->regs [greg ++] = *(guint64*)(arg);
2740 case MONO_TYPE_BOOLEAN:
2742 p->regs [greg ++] = *(guint8*)(arg);
2745 p->regs [greg ++] = *(gint8*)(arg);
2748 p->regs [greg ++] = *(gint16*)(arg);
2751 case MONO_TYPE_CHAR:
2752 p->regs [greg ++] = *(guint16*)(arg);
2755 p->regs [greg ++] = *(gint32*)(arg);
2758 p->regs [greg ++] = *(guint32*)(arg);
2760 case MONO_TYPE_GENERICINST:
2761 if (MONO_TYPE_IS_REFERENCE (t)) {
2762 p->regs [greg ++] = PTR_TO_GREG(*(arg));
2767 case MONO_TYPE_VALUETYPE: {
2768 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2770 g_assert (ainfo->storage == ArgValuetypeInReg);
2771 if (ainfo->pair_storage [0] != ArgNone) {
2772 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2773 p->regs [greg ++] = ((mgreg_t*)(arg))[0];
2775 if (ainfo->pair_storage [1] != ArgNone) {
2776 g_assert (ainfo->pair_storage [1] == ArgInIReg);
2777 p->regs [greg ++] = ((mgreg_t*)(arg))[1];
2782 g_assert_not_reached ();
2786 g_assert (greg <= PARAM_REGS);
2790 * mono_arch_finish_dyn_call:
2792 * Store the result of a dyn call into the return value buffer passed to
2793 * start_dyn_call ().
2794 * This function should be as fast as possible, any work which does not depend
2795 * on the actual values of the arguments should be done in
2796 * mono_arch_dyn_call_prepare ().
2799 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2801 ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2802 MonoMethodSignature *sig = dinfo->sig;
2803 guint8 *ret = ((DynCallArgs*)buf)->ret;
2804 mgreg_t res = ((DynCallArgs*)buf)->res;
2805 MonoType *sig_ret = mono_type_get_underlying_type (sig->ret);
2807 switch (sig_ret->type) {
2808 case MONO_TYPE_VOID:
2809 *(gpointer*)ret = NULL;
2811 case MONO_TYPE_STRING:
2812 case MONO_TYPE_CLASS:
2813 case MONO_TYPE_ARRAY:
2814 case MONO_TYPE_SZARRAY:
2815 case MONO_TYPE_OBJECT:
2819 *(gpointer*)ret = GREG_TO_PTR(res);
2825 case MONO_TYPE_BOOLEAN:
2826 *(guint8*)ret = res;
2829 *(gint16*)ret = res;
2832 case MONO_TYPE_CHAR:
2833 *(guint16*)ret = res;
2836 *(gint32*)ret = res;
2839 *(guint32*)ret = res;
2842 *(gint64*)ret = res;
2845 *(guint64*)ret = res;
2847 case MONO_TYPE_GENERICINST:
2848 if (MONO_TYPE_IS_REFERENCE (sig_ret)) {
2849 *(gpointer*)ret = GREG_TO_PTR(res);
2854 case MONO_TYPE_VALUETYPE:
2855 if (dinfo->cinfo->vtype_retaddr) {
2858 ArgInfo *ainfo = &dinfo->cinfo->ret;
2860 g_assert (ainfo->storage == ArgValuetypeInReg);
2862 if (ainfo->pair_storage [0] != ArgNone) {
2863 g_assert (ainfo->pair_storage [0] == ArgInIReg);
2864 ((mgreg_t*)ret)[0] = res;
2867 g_assert (ainfo->pair_storage [1] == ArgNone);
2871 g_assert_not_reached ();
2875 /* emit an exception if condition is fail */
2876 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
2878 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
2879 if (tins == NULL) { \
2880 mono_add_patch_info (cfg, code - cfg->native_code, \
2881 MONO_PATCH_INFO_EXC, exc_name); \
2882 x86_branch32 (code, cond, 0, signed); \
2884 EMIT_COND_BRANCH (tins, cond, signed); \
2888 #define EMIT_FPCOMPARE(code) do { \
2889 amd64_fcompp (code); \
2890 amd64_fnstsw (code); \
2893 #define EMIT_SSE2_FPFUNC(code, op, dreg, sreg1) do { \
2894 amd64_movsd_membase_reg (code, AMD64_RSP, -8, (sreg1)); \
2895 amd64_fld_membase (code, AMD64_RSP, -8, TRUE); \
2896 amd64_ ##op (code); \
2897 amd64_fst_membase (code, AMD64_RSP, -8, TRUE, TRUE); \
2898 amd64_movsd_reg_membase (code, (dreg), AMD64_RSP, -8); \
2902 emit_call_body (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
2904 gboolean no_patch = FALSE;
2907 * FIXME: Add support for thunks
2910 gboolean near_call = FALSE;
2913 * Indirect calls are expensive so try to make a near call if possible.
2914 * The caller memory is allocated by the code manager so it is
2915 * guaranteed to be at a 32 bit offset.
2918 if (patch_type != MONO_PATCH_INFO_ABS) {
2919 /* The target is in memory allocated using the code manager */
2922 if ((patch_type == MONO_PATCH_INFO_METHOD) || (patch_type == MONO_PATCH_INFO_METHOD_JUMP)) {
2923 if (((MonoMethod*)data)->klass->image->aot_module)
2924 /* The callee might be an AOT method */
2926 if (((MonoMethod*)data)->dynamic)
2927 /* The target is in malloc-ed memory */
2931 if (patch_type == MONO_PATCH_INFO_INTERNAL_METHOD) {
2933 * The call might go directly to a native function without
2936 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (data);
2938 gconstpointer target = mono_icall_get_wrapper (mi);
2939 if ((((guint64)target) >> 32) != 0)
2945 MonoJumpInfo *jinfo = NULL;
2947 if (cfg->abs_patches)
2948 jinfo = g_hash_table_lookup (cfg->abs_patches, data);
2950 if (jinfo->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2951 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (jinfo->data.name);
2952 if (mi && (((guint64)mi->func) >> 32) == 0)
2957 * This is not really an optimization, but required because the
2958 * generic class init trampolines use R11 to pass the vtable.
2963 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (data);
2965 if (info->func == info->wrapper) {
2967 if ((((guint64)info->func) >> 32) == 0)
2971 /* See the comment in mono_codegen () */
2972 if ((info->name [0] != 'v') || (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL))
2976 else if ((((guint64)data) >> 32) == 0) {
2983 if (cfg->method->dynamic)
2984 /* These methods are allocated using malloc */
2987 #ifdef MONO_ARCH_NOMAP32BIT
2990 #if defined(__native_client__)
2991 /* Always use near_call == TRUE for Native Client */
2994 /* The 64bit XEN kernel does not honour the MAP_32BIT flag. (#522894) */
2995 if (optimize_for_xen)
2998 if (cfg->compile_aot) {
3005 * Align the call displacement to an address divisible by 4 so it does
3006 * not span cache lines. This is required for code patching to work on SMP
3009 if (!no_patch && ((guint32)(code + 1 - cfg->native_code) % 4) != 0) {
3010 guint32 pad_size = 4 - ((guint32)(code + 1 - cfg->native_code) % 4);
3011 amd64_padding (code, pad_size);
3013 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3014 amd64_call_code (code, 0);
3017 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
3018 amd64_set_reg_template (code, GP_SCRATCH_REG);
3019 amd64_call_reg (code, GP_SCRATCH_REG);
3026 static inline guint8*
3027 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data, gboolean win64_adjust_stack)
3030 if (win64_adjust_stack)
3031 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 32);
3033 code = emit_call_body (cfg, code, patch_type, data);
3035 if (win64_adjust_stack)
3036 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 32);
3043 store_membase_imm_to_store_membase_reg (int opcode)
3046 case OP_STORE_MEMBASE_IMM:
3047 return OP_STORE_MEMBASE_REG;
3048 case OP_STOREI4_MEMBASE_IMM:
3049 return OP_STOREI4_MEMBASE_REG;
3050 case OP_STOREI8_MEMBASE_IMM:
3051 return OP_STOREI8_MEMBASE_REG;
3059 #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)))
3062 * mono_arch_peephole_pass_1:
3064 * Perform peephole opts which should/can be performed before local regalloc
3067 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3071 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3072 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3074 switch (ins->opcode) {
3078 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS) && (ins->inst_imm > 0)) {
3080 * X86_LEA is like ADD, but doesn't have the
3081 * sreg1==dreg restriction. inst_imm > 0 is needed since LEA sign-extends
3082 * its operand to 64 bit.
3084 ins->opcode = OP_X86_LEA_MEMBASE;
3085 ins->inst_basereg = ins->sreg1;
3090 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3094 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3095 * the latter has length 2-3 instead of 6 (reverse constant
3096 * propagation). These instruction sequences are very common
3097 * in the initlocals bblock.
3099 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3100 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)) {
3101 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3102 ins2->sreg1 = ins->dreg;
3103 } 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)) {
3105 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3108 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3116 case OP_COMPARE_IMM:
3117 case OP_LCOMPARE_IMM:
3118 /* OP_COMPARE_IMM (reg, 0)
3120 * OP_AMD64_TEST_NULL (reg)
3123 ins->opcode = OP_AMD64_TEST_NULL;
3125 case OP_ICOMPARE_IMM:
3127 ins->opcode = OP_X86_TEST_NULL;
3129 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
3131 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3132 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
3134 * OP_STORE_MEMBASE_REG reg, offset(basereg)
3135 * OP_COMPARE_IMM reg, imm
3137 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
3139 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
3140 ins->inst_basereg == last_ins->inst_destbasereg &&
3141 ins->inst_offset == last_ins->inst_offset) {
3142 ins->opcode = OP_ICOMPARE_IMM;
3143 ins->sreg1 = last_ins->sreg1;
3145 /* check if we can remove cmp reg,0 with test null */
3147 ins->opcode = OP_X86_TEST_NULL;
3153 mono_peephole_ins (bb, ins);
3158 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3162 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3163 switch (ins->opcode) {
3166 MonoInst *next = mono_inst_next (ins, FILTER_IL_SEQ_POINT);
3167 /* reg = 0 -> XOR (reg, reg) */
3168 /* XOR sets cflags on x86, so we cant do it always */
3169 if (ins->inst_c0 == 0 && (!next || (next && INST_IGNORES_CFLAGS (next->opcode)))) {
3170 ins->opcode = OP_LXOR;
3171 ins->sreg1 = ins->dreg;
3172 ins->sreg2 = ins->dreg;
3180 * Use IXOR to avoid a rex prefix if possible. The cpu will sign extend the
3181 * 0 result into 64 bits.
3183 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3184 ins->opcode = OP_IXOR;
3188 if ((ins->sreg1 == ins->sreg2) && (ins->sreg1 == ins->dreg)) {
3192 * Replace STORE_MEMBASE_IMM 0 with STORE_MEMBASE_REG since
3193 * the latter has length 2-3 instead of 6 (reverse constant
3194 * propagation). These instruction sequences are very common
3195 * in the initlocals bblock.
3197 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
3198 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)) {
3199 ins2->opcode = store_membase_imm_to_store_membase_reg (ins2->opcode);
3200 ins2->sreg1 = ins->dreg;
3201 } 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)) {
3203 } else if (((ins2->opcode == OP_ICONST) || (ins2->opcode == OP_I8CONST)) && (ins2->dreg == ins->dreg) && (ins2->inst_c0 == 0)) {
3206 } else if (ins2->opcode == OP_IL_SEQ_POINT) {
3215 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3216 ins->opcode = OP_X86_INC_REG;
3219 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
3220 ins->opcode = OP_X86_DEC_REG;
3224 mono_peephole_ins (bb, ins);
3228 #define NEW_INS(cfg,ins,dest,op) do { \
3229 MONO_INST_NEW ((cfg), (dest), (op)); \
3230 (dest)->cil_code = (ins)->cil_code; \
3231 mono_bblock_insert_before_ins (bb, ins, (dest)); \
3235 * mono_arch_lowering_pass:
3237 * Converts complex opcodes into simpler ones so that each IR instruction
3238 * corresponds to one machine instruction.
3241 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3243 MonoInst *ins, *n, *temp;
3246 * FIXME: Need to add more instructions, but the current machine
3247 * description can't model some parts of the composite instructions like
3250 MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3251 switch (ins->opcode) {
3255 case OP_IDIV_UN_IMM:
3256 case OP_IREM_UN_IMM:
3259 mono_decompose_op_imm (cfg, bb, ins);
3261 case OP_COMPARE_IMM:
3262 case OP_LCOMPARE_IMM:
3263 if (!amd64_is_imm32 (ins->inst_imm)) {
3264 NEW_INS (cfg, ins, temp, OP_I8CONST);
3265 temp->inst_c0 = ins->inst_imm;
3266 temp->dreg = mono_alloc_ireg (cfg);
3267 ins->opcode = OP_COMPARE;
3268 ins->sreg2 = temp->dreg;
3271 #ifndef __mono_ilp32__
3272 case OP_LOAD_MEMBASE:
3274 case OP_LOADI8_MEMBASE:
3275 #ifndef __native_client_codegen__
3276 /* Don't generate memindex opcodes (to simplify */
3277 /* read sandboxing) */
3278 if (!amd64_is_imm32 (ins->inst_offset)) {
3279 NEW_INS (cfg, ins, temp, OP_I8CONST);
3280 temp->inst_c0 = ins->inst_offset;
3281 temp->dreg = mono_alloc_ireg (cfg);
3282 ins->opcode = OP_AMD64_LOADI8_MEMINDEX;
3283 ins->inst_indexreg = temp->dreg;
3287 #ifndef __mono_ilp32__
3288 case OP_STORE_MEMBASE_IMM:
3290 case OP_STOREI8_MEMBASE_IMM:
3291 if (!amd64_is_imm32 (ins->inst_imm)) {
3292 NEW_INS (cfg, ins, temp, OP_I8CONST);
3293 temp->inst_c0 = ins->inst_imm;
3294 temp->dreg = mono_alloc_ireg (cfg);
3295 ins->opcode = OP_STOREI8_MEMBASE_REG;
3296 ins->sreg1 = temp->dreg;
3299 #ifdef MONO_ARCH_SIMD_INTRINSICS
3300 case OP_EXPAND_I1: {
3301 int temp_reg1 = mono_alloc_ireg (cfg);
3302 int temp_reg2 = mono_alloc_ireg (cfg);
3303 int original_reg = ins->sreg1;
3305 NEW_INS (cfg, ins, temp, OP_ICONV_TO_U1);
3306 temp->sreg1 = original_reg;
3307 temp->dreg = temp_reg1;
3309 NEW_INS (cfg, ins, temp, OP_SHL_IMM);
3310 temp->sreg1 = temp_reg1;
3311 temp->dreg = temp_reg2;
3314 NEW_INS (cfg, ins, temp, OP_LOR);
3315 temp->sreg1 = temp->dreg = temp_reg2;
3316 temp->sreg2 = temp_reg1;
3318 ins->opcode = OP_EXPAND_I2;
3319 ins->sreg1 = temp_reg2;
3328 bb->max_vreg = cfg->next_vreg;
3332 branch_cc_table [] = {
3333 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3334 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
3335 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
3338 /* Maps CMP_... constants to X86_CC_... constants */
3341 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
3342 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
3346 cc_signed_table [] = {
3347 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
3348 FALSE, FALSE, FALSE, FALSE
3351 /*#include "cprop.c"*/
3353 static unsigned char*
3354 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3357 amd64_sse_cvttsd2si_reg_reg (code, dreg, sreg);
3359 amd64_sse_cvttsd2si_reg_reg_size (code, dreg, sreg, 4);
3362 amd64_widen_reg (code, dreg, dreg, is_signed, FALSE);
3364 amd64_widen_reg (code, dreg, dreg, is_signed, TRUE);
3368 static unsigned char*
3369 mono_emit_stack_alloc (MonoCompile *cfg, guchar *code, MonoInst* tree)
3371 int sreg = tree->sreg1;
3372 int need_touch = FALSE;
3374 #if defined(HOST_WIN32)
3376 #elif defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3377 if (!tree->flags & MONO_INST_INIT)
3386 * If requested stack size is larger than one page,
3387 * perform stack-touch operation
3390 * Generate stack probe code.
3391 * Under Windows, it is necessary to allocate one page at a time,
3392 * "touching" stack after each successful sub-allocation. This is
3393 * because of the way stack growth is implemented - there is a
3394 * guard page before the lowest stack page that is currently commited.
3395 * Stack normally grows sequentially so OS traps access to the
3396 * guard page and commits more pages when needed.
3398 amd64_test_reg_imm (code, sreg, ~0xFFF);
3399 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3401 br[2] = code; /* loop */
3402 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
3403 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
3404 amd64_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
3405 amd64_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
3406 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
3407 amd64_patch (br[3], br[2]);
3408 amd64_test_reg_reg (code, sreg, sreg);
3409 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
3410 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3412 br[1] = code; x86_jump8 (code, 0);
3414 amd64_patch (br[0], code);
3415 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, sreg);
3416 amd64_patch (br[1], code);
3417 amd64_patch (br[4], code);
3420 amd64_alu_reg_reg (code, X86_SUB, AMD64_RSP, tree->sreg1);
3422 if (tree->flags & MONO_INST_INIT) {
3424 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX) {
3425 amd64_push_reg (code, AMD64_RAX);
3428 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX) {
3429 amd64_push_reg (code, AMD64_RCX);
3432 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI) {
3433 amd64_push_reg (code, AMD64_RDI);
3437 amd64_shift_reg_imm (code, X86_SHR, sreg, 3);
3438 if (sreg != AMD64_RCX)
3439 amd64_mov_reg_reg (code, AMD64_RCX, sreg, 8);
3440 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
3442 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, offset);
3443 if (cfg->param_area)
3444 amd64_alu_reg_imm (code, X86_ADD, AMD64_RDI, cfg->param_area);
3446 #if defined(__default_codegen__)
3447 amd64_prefix (code, X86_REP_PREFIX);
3449 #elif defined(__native_client_codegen__)
3450 /* NaCl stos pseudo-instruction */
3451 amd64_codegen_pre(code);
3452 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
3453 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
3454 /* Add %r15 to %rdi using lea, condition flags unaffected. */
3455 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
3456 amd64_prefix (code, X86_REP_PREFIX);
3458 amd64_codegen_post(code);
3459 #endif /* __native_client_codegen__ */
3461 if (tree->dreg != AMD64_RDI && sreg != AMD64_RDI)
3462 amd64_pop_reg (code, AMD64_RDI);
3463 if (tree->dreg != AMD64_RCX && sreg != AMD64_RCX)
3464 amd64_pop_reg (code, AMD64_RCX);
3465 if (tree->dreg != AMD64_RAX && sreg != AMD64_RAX)
3466 amd64_pop_reg (code, AMD64_RAX);
3472 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
3477 /* Move return value to the target register */
3478 /* FIXME: do this in the local reg allocator */
3479 switch (ins->opcode) {
3482 case OP_CALL_MEMBASE:
3485 case OP_LCALL_MEMBASE:
3486 g_assert (ins->dreg == AMD64_RAX);
3490 case OP_FCALL_MEMBASE:
3491 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
3492 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, AMD64_XMM0);
3495 if (ins->dreg != AMD64_XMM0)
3496 amd64_sse_movsd_reg_reg (code, ins->dreg, AMD64_XMM0);
3501 case OP_RCALL_MEMBASE:
3502 if (ins->dreg != AMD64_XMM0)
3503 amd64_sse_movss_reg_reg (code, ins->dreg, AMD64_XMM0);
3507 case OP_VCALL_MEMBASE:
3510 case OP_VCALL2_MEMBASE:
3511 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, ((MonoCallInst*)ins)->signature);
3512 if (cinfo->ret.storage == ArgValuetypeInReg) {
3513 MonoInst *loc = cfg->arch.vret_addr_loc;
3515 /* Load the destination address */
3516 g_assert (loc->opcode == OP_REGOFFSET);
3517 amd64_mov_reg_membase (code, AMD64_RCX, loc->inst_basereg, loc->inst_offset, sizeof(gpointer));
3519 for (quad = 0; quad < 2; quad ++) {
3520 switch (cinfo->ret.pair_storage [quad]) {
3522 amd64_mov_membase_reg (code, AMD64_RCX, (quad * sizeof(mgreg_t)), cinfo->ret.pair_regs [quad], sizeof(mgreg_t));
3524 case ArgInFloatSSEReg:
3525 amd64_movss_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3527 case ArgInDoubleSSEReg:
3528 amd64_movsd_membase_reg (code, AMD64_RCX, (quad * 8), cinfo->ret.pair_regs [quad]);
3543 #endif /* DISABLE_JIT */
3546 static int tls_gs_offset;
3550 mono_amd64_have_tls_get (void)
3553 static gboolean have_tls_get = FALSE;
3554 static gboolean inited = FALSE;
3558 return have_tls_get;
3560 ins = (guint8*)pthread_getspecific;
3563 * We're looking for these two instructions:
3565 * mov %gs:[offset](,%rdi,8),%rax
3568 have_tls_get = ins [0] == 0x65 &&
3580 tls_gs_offset = ins[5];
3582 return have_tls_get;
3583 #elif defined(TARGET_ANDROID)
3591 mono_amd64_get_tls_gs_offset (void)
3594 return tls_gs_offset;
3596 g_assert_not_reached ();
3602 * mono_amd64_emit_tls_get:
3603 * @code: buffer to store code to
3604 * @dreg: hard register where to place the result
3605 * @tls_offset: offset info
3607 * mono_amd64_emit_tls_get emits in @code the native code that puts in
3608 * the dreg register the item in the thread local storage identified
3611 * Returns: a pointer to the end of the stored code
3614 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset)
3617 if (tls_offset < 64) {
3618 x86_prefix (code, X86_GS_PREFIX);
3619 amd64_mov_reg_mem (code, dreg, (tls_offset * 8) + 0x1480, 8);
3623 g_assert (tls_offset < 0x440);
3624 /* Load TEB->TlsExpansionSlots */
3625 x86_prefix (code, X86_GS_PREFIX);
3626 amd64_mov_reg_mem (code, dreg, 0x1780, 8);
3627 amd64_test_reg_reg (code, dreg, dreg);
3629 amd64_branch (code, X86_CC_EQ, code, TRUE);
3630 amd64_mov_reg_membase (code, dreg, dreg, (tls_offset * 8) - 0x200, 8);
3631 amd64_patch (buf [0], code);
3633 #elif defined(__APPLE__)
3634 x86_prefix (code, X86_GS_PREFIX);
3635 amd64_mov_reg_mem (code, dreg, tls_gs_offset + (tls_offset * 8), 8);
3637 if (optimize_for_xen) {
3638 x86_prefix (code, X86_FS_PREFIX);
3639 amd64_mov_reg_mem (code, dreg, 0, 8);
3640 amd64_mov_reg_membase (code, dreg, dreg, tls_offset, 8);
3642 x86_prefix (code, X86_FS_PREFIX);
3643 amd64_mov_reg_mem (code, dreg, tls_offset, 8);
3650 emit_tls_get_reg (guint8* code, int dreg, int offset_reg)
3652 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3654 if (dreg != offset_reg)
3655 amd64_mov_reg_reg (code, dreg, offset_reg, sizeof (mgreg_t));
3656 amd64_prefix (code, X86_GS_PREFIX);
3657 amd64_mov_reg_membase (code, dreg, dreg, 0, sizeof (mgreg_t));
3658 #elif defined(__linux__)
3661 if (dreg == offset_reg) {
3662 /* Use a temporary reg by saving it to the redzone */
3663 tmpreg = dreg == AMD64_RAX ? AMD64_RCX : AMD64_RAX;
3664 amd64_mov_membase_reg (code, AMD64_RSP, -8, tmpreg, 8);
3665 amd64_mov_reg_reg (code, tmpreg, offset_reg, sizeof (gpointer));
3666 offset_reg = tmpreg;
3668 x86_prefix (code, X86_FS_PREFIX);
3669 amd64_mov_reg_mem (code, dreg, 0, 8);
3670 amd64_mov_reg_memindex (code, dreg, dreg, 0, offset_reg, 0, 8);
3672 amd64_mov_reg_membase (code, tmpreg, AMD64_RSP, -8, 8);
3674 g_assert_not_reached ();
3680 amd64_emit_tls_set (guint8 *code, int sreg, int tls_offset)
3683 g_assert_not_reached ();
3684 #elif defined(__APPLE__)
3685 x86_prefix (code, X86_GS_PREFIX);
3686 amd64_mov_mem_reg (code, tls_gs_offset + (tls_offset * 8), sreg, 8);
3688 g_assert (!optimize_for_xen);
3689 x86_prefix (code, X86_FS_PREFIX);
3690 amd64_mov_mem_reg (code, tls_offset, sreg, 8);
3696 amd64_emit_tls_set_reg (guint8 *code, int sreg, int offset_reg)
3698 /* offset_reg contains a value translated by mono_arch_translate_tls_offset () */
3700 g_assert_not_reached ();
3701 #elif defined(__APPLE__)
3702 x86_prefix (code, X86_GS_PREFIX);
3703 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3705 x86_prefix (code, X86_FS_PREFIX);
3706 amd64_mov_membase_reg (code, offset_reg, 0, sreg, 8);
3712 * mono_arch_translate_tls_offset:
3714 * Translate the TLS offset OFFSET computed by MONO_THREAD_VAR_OFFSET () into a format usable by OP_TLS_GET_REG/OP_TLS_SET_REG.
3717 mono_arch_translate_tls_offset (int offset)
3720 return tls_gs_offset + (offset * 8);
3729 * Emit code to initialize an LMF structure at LMF_OFFSET.
3732 emit_setup_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset, int cfa_offset)
3735 * The ip field is not set, the exception handling code will obtain it from the stack location pointed to by the sp field.
3738 * sp is saved right before calls but we need to save it here too so
3739 * async stack walks would work.
3741 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
3743 amd64_mov_membase_reg (code, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), AMD64_RBP, 8);
3744 if (cfg->arch.omit_fp && cfa_offset != -1)
3745 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - (cfa_offset - (lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp))));
3747 /* These can't contain refs */
3748 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf), SLOT_NOREF);
3749 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rip), SLOT_NOREF);
3750 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), SLOT_NOREF);
3751 /* These are handled automatically by the stack marking code */
3752 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), SLOT_NOREF);
3757 #define REAL_PRINT_REG(text,reg) \
3758 mono_assert (reg >= 0); \
3759 amd64_push_reg (code, AMD64_RAX); \
3760 amd64_push_reg (code, AMD64_RDX); \
3761 amd64_push_reg (code, AMD64_RCX); \
3762 amd64_push_reg (code, reg); \
3763 amd64_push_imm (code, reg); \
3764 amd64_push_imm (code, text " %d %p\n"); \
3765 amd64_mov_reg_imm (code, AMD64_RAX, printf); \
3766 amd64_call_reg (code, AMD64_RAX); \
3767 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 3*4); \
3768 amd64_pop_reg (code, AMD64_RCX); \
3769 amd64_pop_reg (code, AMD64_RDX); \
3770 amd64_pop_reg (code, AMD64_RAX);
3772 /* benchmark and set based on cpu */
3773 #define LOOP_ALIGNMENT 8
3774 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
3778 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3783 guint8 *code = cfg->native_code + cfg->code_len;
3784 MonoInst *last_ins = NULL;
3785 guint last_offset = 0;
3788 /* Fix max_offset estimate for each successor bb */
3789 if (cfg->opt & MONO_OPT_BRANCH) {
3790 int current_offset = cfg->code_len;
3791 MonoBasicBlock *current_bb;
3792 for (current_bb = bb; current_bb != NULL; current_bb = current_bb->next_bb) {
3793 current_bb->max_offset = current_offset;
3794 current_offset += current_bb->max_length;
3798 if (cfg->opt & MONO_OPT_LOOP) {
3799 int pad, align = LOOP_ALIGNMENT;
3800 /* set alignment depending on cpu */
3801 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
3803 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
3804 amd64_padding (code, pad);
3805 cfg->code_len += pad;
3806 bb->native_offset = cfg->code_len;
3810 #if defined(__native_client_codegen__)
3811 /* For Native Client, all indirect call/jump targets must be */
3812 /* 32-byte aligned. Exception handler blocks are jumped to */
3813 /* indirectly as well. */
3814 gboolean bb_needs_alignment = (bb->flags & BB_INDIRECT_JUMP_TARGET) ||
3815 (bb->flags & BB_EXCEPTION_HANDLER);
3817 if ( bb_needs_alignment && ((cfg->code_len & kNaClAlignmentMask) != 0)) {
3818 int pad = kNaClAlignment - (cfg->code_len & kNaClAlignmentMask);
3819 if (pad != kNaClAlignment) code = mono_arch_nacl_pad(code, pad);
3820 cfg->code_len += pad;
3821 bb->native_offset = cfg->code_len;
3823 #endif /*__native_client_codegen__*/
3825 if (cfg->verbose_level > 2)
3826 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3828 if ((cfg->prof_options & MONO_PROFILE_COVERAGE) && cfg->coverage_info) {
3829 MonoProfileCoverageInfo *cov = cfg->coverage_info;
3830 g_assert (!cfg->compile_aot);
3832 cov->data [bb->dfn].cil_code = bb->cil_code;
3833 amd64_mov_reg_imm (code, AMD64_R11, (guint64)&cov->data [bb->dfn].count);
3834 /* this is not thread save, but good enough */
3835 amd64_inc_membase (code, AMD64_R11, 0);
3838 offset = code - cfg->native_code;
3840 mono_debug_open_block (cfg, bb, offset);
3842 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)
3843 x86_breakpoint (code);
3845 MONO_BB_FOR_EACH_INS (bb, ins) {
3846 offset = code - cfg->native_code;
3848 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3850 #define EXTRA_CODE_SPACE (NACL_SIZE (16, 16 + kNaClAlignment))
3852 if (G_UNLIKELY (offset > (cfg->code_size - max_len - EXTRA_CODE_SPACE))) {
3853 cfg->code_size *= 2;
3854 cfg->native_code = mono_realloc_native_code(cfg);
3855 code = cfg->native_code + offset;
3856 cfg->stat_code_reallocs++;
3859 if (cfg->debug_info)
3860 mono_debug_record_line_number (cfg, ins, offset);
3862 switch (ins->opcode) {
3864 amd64_mul_reg (code, ins->sreg2, TRUE);
3867 amd64_mul_reg (code, ins->sreg2, FALSE);
3869 case OP_X86_SETEQ_MEMBASE:
3870 amd64_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
3872 case OP_STOREI1_MEMBASE_IMM:
3873 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
3875 case OP_STOREI2_MEMBASE_IMM:
3876 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
3878 case OP_STOREI4_MEMBASE_IMM:
3879 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
3881 case OP_STOREI1_MEMBASE_REG:
3882 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
3884 case OP_STOREI2_MEMBASE_REG:
3885 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
3887 /* In AMD64 NaCl, pointers are 4 bytes, */
3888 /* so STORE_* != STOREI8_*. Likewise below. */
3889 case OP_STORE_MEMBASE_REG:
3890 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, sizeof(gpointer));
3892 case OP_STOREI8_MEMBASE_REG:
3893 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 8);
3895 case OP_STOREI4_MEMBASE_REG:
3896 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
3898 case OP_STORE_MEMBASE_IMM:
3899 #ifndef __native_client_codegen__
3900 /* In NaCl, this could be a PCONST type, which could */
3901 /* mean a pointer type was copied directly into the */
3902 /* lower 32-bits of inst_imm, so for InvalidPtr==-1 */
3903 /* the value would be 0x00000000FFFFFFFF which is */
3904 /* not proper for an imm32 unless you cast it. */
3905 g_assert (amd64_is_imm32 (ins->inst_imm));
3907 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, (gint32)ins->inst_imm, sizeof(gpointer));
3909 case OP_STOREI8_MEMBASE_IMM:
3910 g_assert (amd64_is_imm32 (ins->inst_imm));
3911 amd64_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 8);
3914 #ifdef __mono_ilp32__
3915 /* In ILP32, pointers are 4 bytes, so separate these */
3916 /* cases, use literal 8 below where we really want 8 */
3917 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3918 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, sizeof(gpointer));
3922 // FIXME: Decompose this earlier
3923 if (amd64_is_imm32 (ins->inst_imm))
3924 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 8);
3926 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3927 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 8);
3931 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3932 amd64_movsxd_reg_membase (code, ins->dreg, ins->dreg, 0);
3935 // FIXME: Decompose this earlier
3936 if (amd64_is_imm32 (ins->inst_imm))
3937 amd64_mov_reg_mem (code, ins->dreg, ins->inst_imm, 4);
3939 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3940 amd64_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
3944 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3945 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, FALSE);
3948 /* For NaCl, pointers are 4 bytes, so separate these */
3949 /* cases, use literal 8 below where we really want 8 */
3950 amd64_mov_reg_imm (code, ins->dreg, ins->inst_imm);
3951 amd64_widen_membase (code, ins->dreg, ins->dreg, 0, FALSE, TRUE);
3953 case OP_LOAD_MEMBASE:
3954 g_assert (amd64_is_imm32 (ins->inst_offset));
3955 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, sizeof(gpointer));
3957 case OP_LOADI8_MEMBASE:
3958 /* Use literal 8 instead of sizeof pointer or */
3959 /* register, we really want 8 for this opcode */
3960 g_assert (amd64_is_imm32 (ins->inst_offset));
3961 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 8);
3963 case OP_LOADI4_MEMBASE:
3964 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3966 case OP_LOADU4_MEMBASE:
3967 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
3969 case OP_LOADU1_MEMBASE:
3970 /* The cpu zero extends the result into 64 bits */
3971 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE, 4);
3973 case OP_LOADI1_MEMBASE:
3974 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
3976 case OP_LOADU2_MEMBASE:
3977 /* The cpu zero extends the result into 64 bits */
3978 amd64_widen_membase_size (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE, 4);
3980 case OP_LOADI2_MEMBASE:
3981 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
3983 case OP_AMD64_LOADI8_MEMINDEX:
3984 amd64_mov_reg_memindex_size (code, ins->dreg, ins->inst_basereg, 0, ins->inst_indexreg, 0, 8);
3986 case OP_LCONV_TO_I1:
3987 case OP_ICONV_TO_I1:
3989 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3991 case OP_LCONV_TO_I2:
3992 case OP_ICONV_TO_I2:
3994 amd64_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
3996 case OP_LCONV_TO_U1:
3997 case OP_ICONV_TO_U1:
3998 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
4000 case OP_LCONV_TO_U2:
4001 case OP_ICONV_TO_U2:
4002 amd64_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
4005 /* Clean out the upper word */
4006 amd64_mov_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
4009 amd64_movsxd_reg_reg (code, ins->dreg, ins->sreg1);
4013 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
4015 case OP_COMPARE_IMM:
4016 #if defined(__mono_ilp32__)
4017 /* Comparison of pointer immediates should be 4 bytes to avoid sign-extend problems */
4018 g_assert (amd64_is_imm32 (ins->inst_imm));
4019 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4022 case OP_LCOMPARE_IMM:
4023 g_assert (amd64_is_imm32 (ins->inst_imm));
4024 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
4026 case OP_X86_COMPARE_REG_MEMBASE:
4027 amd64_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
4029 case OP_X86_TEST_NULL:
4030 amd64_test_reg_reg_size (code, ins->sreg1, ins->sreg1, 4);
4032 case OP_AMD64_TEST_NULL:
4033 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
4036 case OP_X86_ADD_REG_MEMBASE:
4037 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4039 case OP_X86_SUB_REG_MEMBASE:
4040 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4042 case OP_X86_AND_REG_MEMBASE:
4043 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4045 case OP_X86_OR_REG_MEMBASE:
4046 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4048 case OP_X86_XOR_REG_MEMBASE:
4049 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4052 case OP_X86_ADD_MEMBASE_IMM:
4053 /* FIXME: Make a 64 version too */
4054 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4056 case OP_X86_SUB_MEMBASE_IMM:
4057 g_assert (amd64_is_imm32 (ins->inst_imm));
4058 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4060 case OP_X86_AND_MEMBASE_IMM:
4061 g_assert (amd64_is_imm32 (ins->inst_imm));
4062 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4064 case OP_X86_OR_MEMBASE_IMM:
4065 g_assert (amd64_is_imm32 (ins->inst_imm));
4066 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4068 case OP_X86_XOR_MEMBASE_IMM:
4069 g_assert (amd64_is_imm32 (ins->inst_imm));
4070 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4072 case OP_X86_ADD_MEMBASE_REG:
4073 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4075 case OP_X86_SUB_MEMBASE_REG:
4076 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4078 case OP_X86_AND_MEMBASE_REG:
4079 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4081 case OP_X86_OR_MEMBASE_REG:
4082 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4084 case OP_X86_XOR_MEMBASE_REG:
4085 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4087 case OP_X86_INC_MEMBASE:
4088 amd64_inc_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4090 case OP_X86_INC_REG:
4091 amd64_inc_reg_size (code, ins->dreg, 4);
4093 case OP_X86_DEC_MEMBASE:
4094 amd64_dec_membase_size (code, ins->inst_basereg, ins->inst_offset, 4);
4096 case OP_X86_DEC_REG:
4097 amd64_dec_reg_size (code, ins->dreg, 4);
4099 case OP_X86_MUL_REG_MEMBASE:
4100 case OP_X86_MUL_MEMBASE_REG:
4101 amd64_imul_reg_membase_size (code, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4103 case OP_AMD64_ICOMPARE_MEMBASE_REG:
4104 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 4);
4106 case OP_AMD64_ICOMPARE_MEMBASE_IMM:
4107 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4109 case OP_AMD64_COMPARE_MEMBASE_REG:
4110 amd64_alu_membase_reg_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4112 case OP_AMD64_COMPARE_MEMBASE_IMM:
4113 g_assert (amd64_is_imm32 (ins->inst_imm));
4114 amd64_alu_membase_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4116 case OP_X86_COMPARE_MEMBASE8_IMM:
4117 amd64_alu_membase8_imm_size (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 4);
4119 case OP_AMD64_ICOMPARE_REG_MEMBASE:
4120 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 4);
4122 case OP_AMD64_COMPARE_REG_MEMBASE:
4123 amd64_alu_reg_membase_size (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4126 case OP_AMD64_ADD_REG_MEMBASE:
4127 amd64_alu_reg_membase_size (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4129 case OP_AMD64_SUB_REG_MEMBASE:
4130 amd64_alu_reg_membase_size (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4132 case OP_AMD64_AND_REG_MEMBASE:
4133 amd64_alu_reg_membase_size (code, X86_AND, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4135 case OP_AMD64_OR_REG_MEMBASE:
4136 amd64_alu_reg_membase_size (code, X86_OR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4138 case OP_AMD64_XOR_REG_MEMBASE:
4139 amd64_alu_reg_membase_size (code, X86_XOR, ins->sreg1, ins->sreg2, ins->inst_offset, 8);
4142 case OP_AMD64_ADD_MEMBASE_REG:
4143 amd64_alu_membase_reg_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4145 case OP_AMD64_SUB_MEMBASE_REG:
4146 amd64_alu_membase_reg_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4148 case OP_AMD64_AND_MEMBASE_REG:
4149 amd64_alu_membase_reg_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4151 case OP_AMD64_OR_MEMBASE_REG:
4152 amd64_alu_membase_reg_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4154 case OP_AMD64_XOR_MEMBASE_REG:
4155 amd64_alu_membase_reg_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->sreg2, 8);
4158 case OP_AMD64_ADD_MEMBASE_IMM:
4159 g_assert (amd64_is_imm32 (ins->inst_imm));
4160 amd64_alu_membase_imm_size (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4162 case OP_AMD64_SUB_MEMBASE_IMM:
4163 g_assert (amd64_is_imm32 (ins->inst_imm));
4164 amd64_alu_membase_imm_size (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4166 case OP_AMD64_AND_MEMBASE_IMM:
4167 g_assert (amd64_is_imm32 (ins->inst_imm));
4168 amd64_alu_membase_imm_size (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4170 case OP_AMD64_OR_MEMBASE_IMM:
4171 g_assert (amd64_is_imm32 (ins->inst_imm));
4172 amd64_alu_membase_imm_size (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4174 case OP_AMD64_XOR_MEMBASE_IMM:
4175 g_assert (amd64_is_imm32 (ins->inst_imm));
4176 amd64_alu_membase_imm_size (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm, 8);
4180 amd64_breakpoint (code);
4182 case OP_RELAXED_NOP:
4183 x86_prefix (code, X86_REP_PREFIX);
4191 case OP_DUMMY_STORE:
4192 case OP_DUMMY_ICONST:
4193 case OP_DUMMY_R8CONST:
4194 case OP_NOT_REACHED:
4197 case OP_IL_SEQ_POINT:
4198 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4200 case OP_SEQ_POINT: {
4204 * Read from the single stepping trigger page. This will cause a
4205 * SIGSEGV when single stepping is enabled.
4206 * We do this _before_ the breakpoint, so single stepping after
4207 * a breakpoint is hit will step to the next IL offset.
4209 if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4210 MonoInst *var = cfg->arch.ss_trigger_page_var;
4212 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4213 amd64_alu_membase_imm_size (code, X86_CMP, AMD64_R11, 0, 0, 4);
4217 * This is the address which is saved in seq points,
4219 mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4221 if (cfg->compile_aot) {
4222 guint32 offset = code - cfg->native_code;
4224 MonoInst *info_var = cfg->arch.seq_point_info_var;
4227 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
4228 val = ((offset) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4229 /* Load the info->bp_addrs [offset], which is either a valid address or the address of a trigger page */
4230 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, val, 8);
4231 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8);
4234 * A placeholder for a possible breakpoint inserted by
4235 * mono_arch_set_breakpoint ().
4237 for (i = 0; i < breakpoint_size; ++i)
4241 * Add an additional nop so skipping the bp doesn't cause the ip to point
4242 * to another IL offset.
4250 amd64_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
4253 amd64_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
4257 g_assert (amd64_is_imm32 (ins->inst_imm));
4258 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
4261 g_assert (amd64_is_imm32 (ins->inst_imm));
4262 amd64_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
4267 amd64_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
4270 amd64_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
4274 g_assert (amd64_is_imm32 (ins->inst_imm));
4275 amd64_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
4278 g_assert (amd64_is_imm32 (ins->inst_imm));
4279 amd64_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
4282 amd64_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
4286 g_assert (amd64_is_imm32 (ins->inst_imm));
4287 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
4290 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4295 guint32 size = (ins->opcode == OP_IMUL_IMM) ? 4 : 8;
4297 switch (ins->inst_imm) {
4301 if (ins->dreg != ins->sreg1)
4302 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, size);
4303 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4306 /* LEA r1, [r2 + r2*2] */
4307 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4310 /* LEA r1, [r2 + r2*4] */
4311 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4314 /* LEA r1, [r2 + r2*2] */
4316 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4317 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4320 /* LEA r1, [r2 + r2*8] */
4321 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
4324 /* LEA r1, [r2 + r2*4] */
4326 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4327 amd64_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
4330 /* LEA r1, [r2 + r2*2] */
4332 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
4333 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4336 /* LEA r1, [r2 + r2*4] */
4337 /* LEA r1, [r1 + r1*4] */
4338 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4339 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4342 /* LEA r1, [r2 + r2*4] */
4344 /* LEA r1, [r1 + r1*4] */
4345 amd64_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
4346 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
4347 amd64_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
4350 amd64_imul_reg_reg_imm_size (code, ins->dreg, ins->sreg1, ins->inst_imm, size);
4357 #if defined( __native_client_codegen__ )
4358 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4359 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4361 /* Regalloc magic makes the div/rem cases the same */
4362 if (ins->sreg2 == AMD64_RDX) {
4363 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4365 amd64_div_membase (code, AMD64_RSP, -8, TRUE);
4368 amd64_div_reg (code, ins->sreg2, TRUE);
4373 #if defined( __native_client_codegen__ )
4374 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4375 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4377 if (ins->sreg2 == AMD64_RDX) {
4378 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4379 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4380 amd64_div_membase (code, AMD64_RSP, -8, FALSE);
4382 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4383 amd64_div_reg (code, ins->sreg2, FALSE);
4388 #if defined( __native_client_codegen__ )
4389 amd64_alu_reg_imm (code, X86_CMP, ins->sreg2, 0);
4390 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4392 if (ins->sreg2 == AMD64_RDX) {
4393 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4394 amd64_cdq_size (code, 4);
4395 amd64_div_membase_size (code, AMD64_RSP, -8, TRUE, 4);
4397 amd64_cdq_size (code, 4);
4398 amd64_div_reg_size (code, ins->sreg2, TRUE, 4);
4403 #if defined( __native_client_codegen__ )
4404 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg2, 0, 4);
4405 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, TRUE, "DivideByZeroException");
4407 if (ins->sreg2 == AMD64_RDX) {
4408 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDX, 8);
4409 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4410 amd64_div_membase_size (code, AMD64_RSP, -8, FALSE, 4);
4412 amd64_alu_reg_reg (code, X86_XOR, AMD64_RDX, AMD64_RDX);
4413 amd64_div_reg_size (code, ins->sreg2, FALSE, 4);
4417 amd64_imul_reg_reg (code, ins->sreg1, ins->sreg2);
4418 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4421 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
4425 g_assert (amd64_is_imm32 (ins->inst_imm));
4426 amd64_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
4429 amd64_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
4433 g_assert (amd64_is_imm32 (ins->inst_imm));
4434 amd64_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
4437 g_assert (ins->sreg2 == AMD64_RCX);
4438 amd64_shift_reg (code, X86_SHL, ins->dreg);
4441 g_assert (ins->sreg2 == AMD64_RCX);
4442 amd64_shift_reg (code, X86_SAR, ins->dreg);
4445 g_assert (amd64_is_imm32 (ins->inst_imm));
4446 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4449 g_assert (amd64_is_imm32 (ins->inst_imm));
4450 amd64_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
4453 g_assert (amd64_is_imm32 (ins->inst_imm));
4454 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4456 case OP_LSHR_UN_IMM:
4457 g_assert (amd64_is_imm32 (ins->inst_imm));
4458 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
4461 g_assert (ins->sreg2 == AMD64_RCX);
4462 amd64_shift_reg (code, X86_SHR, ins->dreg);
4465 g_assert (amd64_is_imm32 (ins->inst_imm));
4466 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4469 g_assert (amd64_is_imm32 (ins->inst_imm));
4470 amd64_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
4475 amd64_alu_reg_reg_size (code, X86_ADD, ins->sreg1, ins->sreg2, 4);
4478 amd64_alu_reg_reg_size (code, X86_ADC, ins->sreg1, ins->sreg2, 4);
4481 amd64_alu_reg_imm_size (code, X86_ADD, ins->dreg, ins->inst_imm, 4);
4484 amd64_alu_reg_imm_size (code, X86_ADC, ins->dreg, ins->inst_imm, 4);
4488 amd64_alu_reg_reg_size (code, X86_SUB, ins->sreg1, ins->sreg2, 4);
4491 amd64_alu_reg_reg_size (code, X86_SBB, ins->sreg1, ins->sreg2, 4);
4494 amd64_alu_reg_imm_size (code, X86_SUB, ins->dreg, ins->inst_imm, 4);
4497 amd64_alu_reg_imm_size (code, X86_SBB, ins->dreg, ins->inst_imm, 4);
4500 amd64_alu_reg_reg_size (code, X86_AND, ins->sreg1, ins->sreg2, 4);
4503 amd64_alu_reg_imm_size (code, X86_AND, ins->sreg1, ins->inst_imm, 4);
4506 amd64_alu_reg_reg_size (code, X86_OR, ins->sreg1, ins->sreg2, 4);
4509 amd64_alu_reg_imm_size (code, X86_OR, ins->sreg1, ins->inst_imm, 4);
4512 amd64_alu_reg_reg_size (code, X86_XOR, ins->sreg1, ins->sreg2, 4);
4515 amd64_alu_reg_imm_size (code, X86_XOR, ins->sreg1, ins->inst_imm, 4);
4518 amd64_neg_reg_size (code, ins->sreg1, 4);
4521 amd64_not_reg_size (code, ins->sreg1, 4);
4524 g_assert (ins->sreg2 == AMD64_RCX);
4525 amd64_shift_reg_size (code, X86_SHL, ins->dreg, 4);
4528 g_assert (ins->sreg2 == AMD64_RCX);
4529 amd64_shift_reg_size (code, X86_SAR, ins->dreg, 4);
4532 amd64_shift_reg_imm_size (code, X86_SAR, ins->dreg, ins->inst_imm, 4);
4534 case OP_ISHR_UN_IMM:
4535 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, ins->inst_imm, 4);
4538 g_assert (ins->sreg2 == AMD64_RCX);
4539 amd64_shift_reg_size (code, X86_SHR, ins->dreg, 4);
4542 amd64_shift_reg_imm_size (code, X86_SHL, ins->dreg, ins->inst_imm, 4);
4545 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4548 amd64_imul_reg_reg_size (code, ins->sreg1, ins->sreg2, 4);
4549 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4551 case OP_IMUL_OVF_UN:
4552 case OP_LMUL_OVF_UN: {
4553 /* the mul operation and the exception check should most likely be split */
4554 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
4555 int size = (ins->opcode == OP_IMUL_OVF_UN) ? 4 : 8;
4556 /*g_assert (ins->sreg2 == X86_EAX);
4557 g_assert (ins->dreg == X86_EAX);*/
4558 if (ins->sreg2 == X86_EAX) {
4559 non_eax_reg = ins->sreg1;
4560 } else if (ins->sreg1 == X86_EAX) {
4561 non_eax_reg = ins->sreg2;
4563 /* no need to save since we're going to store to it anyway */
4564 if (ins->dreg != X86_EAX) {
4566 amd64_push_reg (code, X86_EAX);
4568 amd64_mov_reg_reg (code, X86_EAX, ins->sreg1, size);
4569 non_eax_reg = ins->sreg2;
4571 if (ins->dreg == X86_EDX) {
4574 amd64_push_reg (code, X86_EAX);
4578 amd64_push_reg (code, X86_EDX);
4580 amd64_mul_reg_size (code, non_eax_reg, FALSE, size);
4581 /* save before the check since pop and mov don't change the flags */
4582 if (ins->dreg != X86_EAX)
4583 amd64_mov_reg_reg (code, ins->dreg, X86_EAX, size);
4585 amd64_pop_reg (code, X86_EDX);
4587 amd64_pop_reg (code, X86_EAX);
4588 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
4592 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
4594 case OP_ICOMPARE_IMM:
4595 amd64_alu_reg_imm_size (code, X86_CMP, ins->sreg1, ins->inst_imm, 4);
4617 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
4625 case OP_CMOV_INE_UN:
4626 case OP_CMOV_IGE_UN:
4627 case OP_CMOV_IGT_UN:
4628 case OP_CMOV_ILE_UN:
4629 case OP_CMOV_ILT_UN:
4635 case OP_CMOV_LNE_UN:
4636 case OP_CMOV_LGE_UN:
4637 case OP_CMOV_LGT_UN:
4638 case OP_CMOV_LLE_UN:
4639 case OP_CMOV_LLT_UN:
4640 g_assert (ins->dreg == ins->sreg1);
4641 /* This needs to operate on 64 bit values */
4642 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);
4646 amd64_not_reg (code, ins->sreg1);
4649 amd64_neg_reg (code, ins->sreg1);
4654 if ((((guint64)ins->inst_c0) >> 32) == 0)
4655 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 4);
4657 amd64_mov_reg_imm_size (code, ins->dreg, ins->inst_c0, 8);
4660 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4661 amd64_mov_reg_membase (code, ins->dreg, AMD64_RIP, 0, sizeof(gpointer));
4664 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4665 amd64_mov_reg_imm_size (code, ins->dreg, 0, 8);
4668 if (ins->dreg != ins->sreg1)
4669 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, sizeof(mgreg_t));
4671 case OP_AMD64_SET_XMMREG_R4: {
4673 if (ins->dreg != ins->sreg1)
4674 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
4676 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
4680 case OP_AMD64_SET_XMMREG_R8: {
4681 if (ins->dreg != ins->sreg1)
4682 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
4686 MonoCallInst *call = (MonoCallInst*)ins;
4687 int i, save_area_offset;
4689 g_assert (!cfg->method->save_lmf);
4691 /* Restore callee saved registers */
4692 save_area_offset = cfg->arch.reg_save_area_offset;
4693 for (i = 0; i < AMD64_NREG; ++i)
4694 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->used_int_regs & (1 << i))) {
4695 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
4696 save_area_offset += 8;
4699 if (cfg->arch.omit_fp) {
4700 if (cfg->arch.stack_alloc_size)
4701 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
4703 if (call->stack_usage)
4706 /* Copy arguments on the stack to our argument area */
4707 for (i = 0; i < call->stack_usage; i += sizeof(mgreg_t)) {
4708 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, i, sizeof(mgreg_t));
4709 amd64_mov_membase_reg (code, AMD64_RBP, 16 + i, AMD64_RAX, sizeof(mgreg_t));
4715 offset = code - cfg->native_code;
4716 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4717 if (cfg->compile_aot)
4718 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
4720 amd64_set_reg_template (code, AMD64_R11);
4721 amd64_jump_reg (code, AMD64_R11);
4722 ins->flags |= MONO_INST_GC_CALLSITE;
4723 ins->backend.pc_offset = code - cfg->native_code;
4727 /* ensure ins->sreg1 is not NULL */
4728 amd64_alu_membase_imm_size (code, X86_CMP, ins->sreg1, 0, 0, 4);
4731 amd64_lea_membase (code, AMD64_R11, cfg->frame_reg, cfg->sig_cookie);
4732 amd64_mov_membase_reg (code, ins->sreg1, 0, AMD64_R11, sizeof(gpointer));
4742 call = (MonoCallInst*)ins;
4744 * The AMD64 ABI forces callers to know about varargs.
4746 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke))
4747 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4748 else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4750 * Since the unmanaged calling convention doesn't contain a
4751 * 'vararg' entry, we have to treat every pinvoke call as a
4752 * potential vararg call.
4756 for (i = 0; i < AMD64_XMM_NREG; ++i)
4757 if (call->used_fregs & (1 << i))
4760 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4762 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4765 if (ins->flags & MONO_INST_HAS_METHOD)
4766 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method, FALSE);
4768 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr, FALSE);
4769 ins->flags |= MONO_INST_GC_CALLSITE;
4770 ins->backend.pc_offset = code - cfg->native_code;
4771 code = emit_move_return_value (cfg, ins, code);
4778 case OP_VOIDCALL_REG:
4780 call = (MonoCallInst*)ins;
4782 if (AMD64_IS_ARGUMENT_REG (ins->sreg1)) {
4783 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4784 ins->sreg1 = AMD64_R11;
4788 * The AMD64 ABI forces callers to know about varargs.
4790 if ((call->signature->call_convention == MONO_CALL_VARARG) && (call->signature->pinvoke)) {
4791 if (ins->sreg1 == AMD64_RAX) {
4792 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4793 ins->sreg1 = AMD64_R11;
4795 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4796 } else if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (cfg->method->klass->image != mono_defaults.corlib)) {
4798 * Since the unmanaged calling convention doesn't contain a
4799 * 'vararg' entry, we have to treat every pinvoke call as a
4800 * potential vararg call.
4804 for (i = 0; i < AMD64_XMM_NREG; ++i)
4805 if (call->used_fregs & (1 << i))
4807 if (ins->sreg1 == AMD64_RAX) {
4808 amd64_mov_reg_reg (code, AMD64_R11, AMD64_RAX, 8);
4809 ins->sreg1 = AMD64_R11;
4812 amd64_alu_reg_reg (code, X86_XOR, AMD64_RAX, AMD64_RAX);
4814 amd64_mov_reg_imm (code, AMD64_RAX, nregs);
4817 amd64_call_reg (code, ins->sreg1);
4818 ins->flags |= MONO_INST_GC_CALLSITE;
4819 ins->backend.pc_offset = code - cfg->native_code;
4820 code = emit_move_return_value (cfg, ins, code);
4822 case OP_FCALL_MEMBASE:
4823 case OP_RCALL_MEMBASE:
4824 case OP_LCALL_MEMBASE:
4825 case OP_VCALL_MEMBASE:
4826 case OP_VCALL2_MEMBASE:
4827 case OP_VOIDCALL_MEMBASE:
4828 case OP_CALL_MEMBASE:
4829 call = (MonoCallInst*)ins;
4831 amd64_call_membase (code, ins->sreg1, ins->inst_offset);
4832 ins->flags |= MONO_INST_GC_CALLSITE;
4833 ins->backend.pc_offset = code - cfg->native_code;
4834 code = emit_move_return_value (cfg, ins, code);
4838 MonoInst *var = cfg->dyn_call_var;
4840 g_assert (var->opcode == OP_REGOFFSET);
4842 /* r11 = args buffer filled by mono_arch_get_dyn_call_args () */
4843 amd64_mov_reg_reg (code, AMD64_R11, ins->sreg1, 8);
4845 amd64_mov_reg_reg (code, AMD64_R10, ins->sreg2, 8);
4847 /* Save args buffer */
4848 amd64_mov_membase_reg (code, var->inst_basereg, var->inst_offset, AMD64_R11, 8);
4850 /* Set argument registers */
4851 for (i = 0; i < PARAM_REGS; ++i)
4852 amd64_mov_reg_membase (code, param_regs [i], AMD64_R11, i * sizeof(mgreg_t), sizeof(mgreg_t));
4855 amd64_call_reg (code, AMD64_R10);
4857 ins->flags |= MONO_INST_GC_CALLSITE;
4858 ins->backend.pc_offset = code - cfg->native_code;
4861 amd64_mov_reg_membase (code, AMD64_R11, var->inst_basereg, var->inst_offset, 8);
4862 amd64_mov_membase_reg (code, AMD64_R11, MONO_STRUCT_OFFSET (DynCallArgs, res), AMD64_RAX, 8);
4865 case OP_AMD64_SAVE_SP_TO_LMF: {
4866 MonoInst *lmf_var = cfg->lmf_var;
4867 amd64_mov_membase_reg (code, lmf_var->inst_basereg, lmf_var->inst_offset + MONO_STRUCT_OFFSET (MonoLMF, rsp), AMD64_RSP, 8);
4871 g_assert_not_reached ();
4872 amd64_push_reg (code, ins->sreg1);
4874 case OP_X86_PUSH_IMM:
4875 g_assert_not_reached ();
4876 g_assert (amd64_is_imm32 (ins->inst_imm));
4877 amd64_push_imm (code, ins->inst_imm);
4879 case OP_X86_PUSH_MEMBASE:
4880 g_assert_not_reached ();
4881 amd64_push_membase (code, ins->inst_basereg, ins->inst_offset);
4883 case OP_X86_PUSH_OBJ: {
4884 int size = ALIGN_TO (ins->inst_imm, 8);
4886 g_assert_not_reached ();
4888 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4889 amd64_push_reg (code, AMD64_RDI);
4890 amd64_push_reg (code, AMD64_RSI);
4891 amd64_push_reg (code, AMD64_RCX);
4892 if (ins->inst_offset)
4893 amd64_lea_membase (code, AMD64_RSI, ins->inst_basereg, ins->inst_offset);
4895 amd64_mov_reg_reg (code, AMD64_RSI, ins->inst_basereg, 8);
4896 amd64_lea_membase (code, AMD64_RDI, AMD64_RSP, (3 * 8));
4897 amd64_mov_reg_imm (code, AMD64_RCX, (size >> 3));
4899 amd64_prefix (code, X86_REP_PREFIX);
4901 amd64_pop_reg (code, AMD64_RCX);
4902 amd64_pop_reg (code, AMD64_RSI);
4903 amd64_pop_reg (code, AMD64_RDI);
4907 amd64_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
4909 case OP_X86_LEA_MEMBASE:
4910 amd64_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
4913 amd64_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
4916 /* keep alignment */
4917 amd64_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
4918 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
4919 code = mono_emit_stack_alloc (cfg, code, ins);
4920 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4921 if (cfg->param_area)
4922 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4924 case OP_LOCALLOC_IMM: {
4925 guint32 size = ins->inst_imm;
4926 size = (size + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
4928 if (ins->flags & MONO_INST_INIT) {
4932 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4933 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
4935 for (i = 0; i < size; i += 8)
4936 amd64_mov_membase_reg (code, AMD64_RSP, i, ins->dreg, 8);
4937 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4939 amd64_mov_reg_imm (code, ins->dreg, size);
4940 ins->sreg1 = ins->dreg;
4942 code = mono_emit_stack_alloc (cfg, code, ins);
4943 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4946 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, size);
4947 amd64_mov_reg_reg (code, ins->dreg, AMD64_RSP, 8);
4949 if (cfg->param_area)
4950 amd64_alu_reg_imm (code, X86_ADD, ins->dreg, cfg->param_area);
4954 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4955 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4956 (gpointer)"mono_arch_throw_exception", FALSE);
4957 ins->flags |= MONO_INST_GC_CALLSITE;
4958 ins->backend.pc_offset = code - cfg->native_code;
4962 amd64_mov_reg_reg (code, AMD64_ARG_REG1, ins->sreg1, 8);
4963 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
4964 (gpointer)"mono_arch_rethrow_exception", FALSE);
4965 ins->flags |= MONO_INST_GC_CALLSITE;
4966 ins->backend.pc_offset = code - cfg->native_code;
4969 case OP_CALL_HANDLER:
4971 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
4972 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4973 amd64_call_imm (code, 0);
4974 mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4975 /* Restore stack alignment */
4976 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
4978 case OP_START_HANDLER: {
4979 /* Even though we're saving RSP, use sizeof */
4980 /* gpointer because spvar is of type IntPtr */
4981 /* see: mono_create_spvar_for_region */
4982 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4983 amd64_mov_membase_reg (code, spvar->inst_basereg, spvar->inst_offset, AMD64_RSP, sizeof(gpointer));
4985 if ((MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY) ||
4986 MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_FINALLY)) &&
4988 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
4992 case OP_ENDFINALLY: {
4993 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4994 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
4998 case OP_ENDFILTER: {
4999 MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5000 amd64_mov_reg_membase (code, AMD64_RSP, spvar->inst_basereg, spvar->inst_offset, sizeof(gpointer));
5001 /* The local allocator will put the result into RAX */
5007 ins->inst_c0 = code - cfg->native_code;
5010 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
5011 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
5013 if (ins->inst_target_bb->native_offset) {
5014 amd64_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
5016 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5017 if ((cfg->opt & MONO_OPT_BRANCH) &&
5018 x86_is_imm8 (ins->inst_target_bb->max_offset - offset))
5019 x86_jump8 (code, 0);
5021 x86_jump32 (code, 0);
5025 amd64_jump_reg (code, ins->sreg1);
5048 amd64_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
5049 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5051 case OP_COND_EXC_EQ:
5052 case OP_COND_EXC_NE_UN:
5053 case OP_COND_EXC_LT:
5054 case OP_COND_EXC_LT_UN:
5055 case OP_COND_EXC_GT:
5056 case OP_COND_EXC_GT_UN:
5057 case OP_COND_EXC_GE:
5058 case OP_COND_EXC_GE_UN:
5059 case OP_COND_EXC_LE:
5060 case OP_COND_EXC_LE_UN:
5061 case OP_COND_EXC_IEQ:
5062 case OP_COND_EXC_INE_UN:
5063 case OP_COND_EXC_ILT:
5064 case OP_COND_EXC_ILT_UN:
5065 case OP_COND_EXC_IGT:
5066 case OP_COND_EXC_IGT_UN:
5067 case OP_COND_EXC_IGE:
5068 case OP_COND_EXC_IGE_UN:
5069 case OP_COND_EXC_ILE:
5070 case OP_COND_EXC_ILE_UN:
5071 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
5073 case OP_COND_EXC_OV:
5074 case OP_COND_EXC_NO:
5076 case OP_COND_EXC_NC:
5077 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
5078 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
5080 case OP_COND_EXC_IOV:
5081 case OP_COND_EXC_INO:
5082 case OP_COND_EXC_IC:
5083 case OP_COND_EXC_INC:
5084 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_IEQ],
5085 (ins->opcode < OP_COND_EXC_INE_UN), ins->inst_p1);
5088 /* floating point opcodes */
5090 double d = *(double *)ins->inst_p0;
5092 if ((d == 0.0) && (mono_signbit (d) == 0)) {
5093 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5096 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
5097 amd64_sse_movsd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5102 float f = *(float *)ins->inst_p0;
5104 if ((f == 0.0) && (mono_signbit (f) == 0)) {
5106 amd64_sse_xorps_reg_reg (code, ins->dreg, ins->dreg);
5108 amd64_sse_xorpd_reg_reg (code, ins->dreg, ins->dreg);
5111 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
5112 amd64_sse_movss_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5114 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5118 case OP_STORER8_MEMBASE_REG:
5119 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5121 case OP_LOADR8_MEMBASE:
5122 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5124 case OP_STORER4_MEMBASE_REG:
5126 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5128 /* This requires a double->single conversion */
5129 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5130 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5133 case OP_LOADR4_MEMBASE:
5135 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5137 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5138 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5141 case OP_ICONV_TO_R4:
5143 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5145 amd64_sse_cvtsi2ss_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5146 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5149 case OP_ICONV_TO_R8:
5150 amd64_sse_cvtsi2sd_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5152 case OP_LCONV_TO_R4:
5154 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5156 amd64_sse_cvtsi2ss_reg_reg (code, ins->dreg, ins->sreg1);
5157 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5160 case OP_LCONV_TO_R8:
5161 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5163 case OP_FCONV_TO_R4:
5165 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5167 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg1);
5168 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5171 case OP_FCONV_TO_I1:
5172 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5174 case OP_FCONV_TO_U1:
5175 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5177 case OP_FCONV_TO_I2:
5178 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5180 case OP_FCONV_TO_U2:
5181 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5183 case OP_FCONV_TO_U4:
5184 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5186 case OP_FCONV_TO_I4:
5188 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5190 case OP_FCONV_TO_I8:
5191 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 8, TRUE);
5194 case OP_RCONV_TO_I1:
5195 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5196 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
5198 case OP_RCONV_TO_U1:
5199 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5200 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
5202 case OP_RCONV_TO_I2:
5203 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5204 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
5206 case OP_RCONV_TO_U2:
5207 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5208 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
5210 case OP_RCONV_TO_I4:
5211 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5213 case OP_RCONV_TO_U4:
5214 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 4);
5216 case OP_RCONV_TO_I8:
5217 amd64_sse_cvtss2si_reg_reg_size (code, ins->dreg, ins->sreg1, 8);
5219 case OP_RCONV_TO_R8:
5220 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->sreg1);
5222 case OP_RCONV_TO_R4:
5223 if (ins->dreg != ins->sreg1)
5224 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5227 case OP_LCONV_TO_R_UN: {
5230 /* Based on gcc code */
5231 amd64_test_reg_reg (code, ins->sreg1, ins->sreg1);
5232 br [0] = code; x86_branch8 (code, X86_CC_S, 0, TRUE);
5235 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, ins->sreg1);
5236 br [1] = code; x86_jump8 (code, 0);
5237 amd64_patch (br [0], code);
5240 /* Save to the red zone */
5241 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RAX, 8);
5242 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
5243 amd64_mov_reg_reg (code, AMD64_RCX, ins->sreg1, 8);
5244 amd64_mov_reg_reg (code, AMD64_RAX, ins->sreg1, 8);
5245 amd64_alu_reg_imm (code, X86_AND, AMD64_RCX, 1);
5246 amd64_shift_reg_imm (code, X86_SHR, AMD64_RAX, 1);
5247 amd64_alu_reg_imm (code, X86_OR, AMD64_RAX, AMD64_RCX);
5248 amd64_sse_cvtsi2sd_reg_reg (code, ins->dreg, AMD64_RAX);
5249 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->dreg);
5251 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
5252 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_RSP, -8, 8);
5253 amd64_patch (br [1], code);
5256 case OP_LCONV_TO_OVF_U4:
5257 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0);
5258 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_LT, TRUE, "OverflowException");
5259 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5261 case OP_LCONV_TO_OVF_I4_UN:
5262 amd64_alu_reg_imm (code, X86_CMP, ins->sreg1, 0x7fffffff);
5263 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_GT, FALSE, "OverflowException");
5264 amd64_mov_reg_reg (code, ins->dreg, ins->sreg1, 8);
5267 if (ins->dreg != ins->sreg1)
5268 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
5271 if (ins->dreg != ins->sreg1)
5272 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg1);
5274 case OP_MOVE_F_TO_I4:
5276 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5278 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5279 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
5282 case OP_MOVE_I4_TO_F:
5283 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5285 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5287 case OP_MOVE_F_TO_I8:
5288 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
5290 case OP_MOVE_I8_TO_F:
5291 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
5294 amd64_sse_addsd_reg_reg (code, ins->dreg, ins->sreg2);
5297 amd64_sse_subsd_reg_reg (code, ins->dreg, ins->sreg2);
5300 amd64_sse_mulsd_reg_reg (code, ins->dreg, ins->sreg2);
5303 amd64_sse_divsd_reg_reg (code, ins->dreg, ins->sreg2);
5306 static double r8_0 = -0.0;
5308 g_assert (ins->sreg1 == ins->dreg);
5310 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &r8_0);
5311 amd64_sse_xorpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5315 EMIT_SSE2_FPFUNC (code, fsin, ins->dreg, ins->sreg1);
5318 EMIT_SSE2_FPFUNC (code, fcos, ins->dreg, ins->sreg1);
5321 static guint64 d = 0x7fffffffffffffffUL;
5323 g_assert (ins->sreg1 == ins->dreg);
5325 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, &d);
5326 amd64_sse_andpd_reg_membase (code, ins->dreg, AMD64_RIP, 0);
5330 EMIT_SSE2_FPFUNC (code, fsqrt, ins->dreg, ins->sreg1);
5334 amd64_sse_addss_reg_reg (code, ins->dreg, ins->sreg2);
5337 amd64_sse_subss_reg_reg (code, ins->dreg, ins->sreg2);
5340 amd64_sse_mulss_reg_reg (code, ins->dreg, ins->sreg2);
5343 amd64_sse_divss_reg_reg (code, ins->dreg, ins->sreg2);
5346 static float r4_0 = -0.0;
5348 g_assert (ins->sreg1 == ins->dreg);
5350 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, &r4_0);
5351 amd64_sse_movss_reg_membase (code, MONO_ARCH_FP_SCRATCH_REG, AMD64_RIP, 0);
5352 amd64_sse_xorps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
5357 g_assert (cfg->opt & MONO_OPT_CMOV);
5358 g_assert (ins->dreg == ins->sreg1);
5359 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5360 amd64_cmov_reg_size (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2, 4);
5363 g_assert (cfg->opt & MONO_OPT_CMOV);
5364 g_assert (ins->dreg == ins->sreg1);
5365 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5366 amd64_cmov_reg_size (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2, 4);
5369 g_assert (cfg->opt & MONO_OPT_CMOV);
5370 g_assert (ins->dreg == ins->sreg1);
5371 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5372 amd64_cmov_reg_size (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2, 4);
5375 g_assert (cfg->opt & MONO_OPT_CMOV);
5376 g_assert (ins->dreg == ins->sreg1);
5377 amd64_alu_reg_reg_size (code, X86_CMP, ins->sreg1, ins->sreg2, 4);
5378 amd64_cmov_reg_size (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2, 4);
5381 g_assert (cfg->opt & MONO_OPT_CMOV);
5382 g_assert (ins->dreg == ins->sreg1);
5383 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5384 amd64_cmov_reg (code, X86_CC_GT, TRUE, ins->dreg, ins->sreg2);
5387 g_assert (cfg->opt & MONO_OPT_CMOV);
5388 g_assert (ins->dreg == ins->sreg1);
5389 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5390 amd64_cmov_reg (code, X86_CC_GT, FALSE, ins->dreg, ins->sreg2);
5393 g_assert (cfg->opt & MONO_OPT_CMOV);
5394 g_assert (ins->dreg == ins->sreg1);
5395 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5396 amd64_cmov_reg (code, X86_CC_LT, TRUE, ins->dreg, ins->sreg2);
5399 g_assert (cfg->opt & MONO_OPT_CMOV);
5400 g_assert (ins->dreg == ins->sreg1);
5401 amd64_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
5402 amd64_cmov_reg (code, X86_CC_LT, FALSE, ins->dreg, ins->sreg2);
5408 * The two arguments are swapped because the fbranch instructions
5409 * depend on this for the non-sse case to work.
5411 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5415 * FIXME: Get rid of this.
5416 * The two arguments are swapped because the fbranch instructions
5417 * depend on this for the non-sse case to work.
5419 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5423 /* zeroing the register at the start results in
5424 * shorter and faster code (we can also remove the widening op)
5426 guchar *unordered_check;
5428 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5429 amd64_sse_comisd_reg_reg (code, ins->sreg1, ins->sreg2);
5430 unordered_check = code;
5431 x86_branch8 (code, X86_CC_P, 0, FALSE);
5433 if (ins->opcode == OP_FCEQ) {
5434 amd64_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
5435 amd64_patch (unordered_check, code);
5437 guchar *jump_to_end;
5438 amd64_set_reg (code, X86_CC_NE, ins->dreg, FALSE);
5440 x86_jump8 (code, 0);
5441 amd64_patch (unordered_check, code);
5442 amd64_inc_reg (code, ins->dreg);
5443 amd64_patch (jump_to_end, code);
5449 /* zeroing the register at the start results in
5450 * shorter and faster code (we can also remove the widening op)
5452 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5453 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5454 if (ins->opcode == OP_FCLT_UN) {
5455 guchar *unordered_check = code;
5456 guchar *jump_to_end;
5457 x86_branch8 (code, X86_CC_P, 0, FALSE);
5458 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5460 x86_jump8 (code, 0);
5461 amd64_patch (unordered_check, code);
5462 amd64_inc_reg (code, ins->dreg);
5463 amd64_patch (jump_to_end, code);
5465 amd64_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
5470 guchar *unordered_check;
5471 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5472 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5473 unordered_check = code;
5474 x86_branch8 (code, X86_CC_P, 0, FALSE);
5475 amd64_set_reg (code, X86_CC_NB, ins->dreg, FALSE);
5476 amd64_patch (unordered_check, code);
5481 /* zeroing the register at the start results in
5482 * shorter and faster code (we can also remove the widening op)
5484 guchar *unordered_check;
5486 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5487 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5488 if (ins->opcode == OP_FCGT) {
5489 unordered_check = code;
5490 x86_branch8 (code, X86_CC_P, 0, FALSE);
5491 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5492 amd64_patch (unordered_check, code);
5494 amd64_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
5499 guchar *unordered_check;
5500 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5501 amd64_sse_comisd_reg_reg (code, ins->sreg2, ins->sreg1);
5502 unordered_check = code;
5503 x86_branch8 (code, X86_CC_P, 0, FALSE);
5504 amd64_set_reg (code, X86_CC_NA, ins->dreg, FALSE);
5505 amd64_patch (unordered_check, code);
5515 gboolean unordered = FALSE;
5517 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5518 amd64_sse_comiss_reg_reg (code, ins->sreg2, ins->sreg1);
5520 switch (ins->opcode) {
5522 x86_cond = X86_CC_EQ;
5525 x86_cond = X86_CC_LT;
5528 x86_cond = X86_CC_GT;
5531 x86_cond = X86_CC_GT;
5535 x86_cond = X86_CC_LT;
5539 g_assert_not_reached ();
5544 guchar *unordered_check;
5545 guchar *jump_to_end;
5547 unordered_check = code;
5548 x86_branch8 (code, X86_CC_P, 0, FALSE);
5549 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5551 x86_jump8 (code, 0);
5552 amd64_patch (unordered_check, code);
5553 amd64_inc_reg (code, ins->dreg);
5554 amd64_patch (jump_to_end, code);
5556 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5560 case OP_FCLT_MEMBASE:
5561 case OP_FCGT_MEMBASE:
5562 case OP_FCLT_UN_MEMBASE:
5563 case OP_FCGT_UN_MEMBASE:
5564 case OP_FCEQ_MEMBASE: {
5565 guchar *unordered_check, *jump_to_end;
5568 amd64_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
5569 amd64_sse_comisd_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
5571 switch (ins->opcode) {
5572 case OP_FCEQ_MEMBASE:
5573 x86_cond = X86_CC_EQ;
5575 case OP_FCLT_MEMBASE:
5576 case OP_FCLT_UN_MEMBASE:
5577 x86_cond = X86_CC_LT;
5579 case OP_FCGT_MEMBASE:
5580 case OP_FCGT_UN_MEMBASE:
5581 x86_cond = X86_CC_GT;
5584 g_assert_not_reached ();
5587 unordered_check = code;
5588 x86_branch8 (code, X86_CC_P, 0, FALSE);
5589 amd64_set_reg (code, x86_cond, ins->dreg, FALSE);
5591 switch (ins->opcode) {
5592 case OP_FCEQ_MEMBASE:
5593 case OP_FCLT_MEMBASE:
5594 case OP_FCGT_MEMBASE:
5595 amd64_patch (unordered_check, code);
5597 case OP_FCLT_UN_MEMBASE:
5598 case OP_FCGT_UN_MEMBASE:
5600 x86_jump8 (code, 0);
5601 amd64_patch (unordered_check, code);
5602 amd64_inc_reg (code, ins->dreg);
5603 amd64_patch (jump_to_end, code);
5611 guchar *jump = code;
5612 x86_branch8 (code, X86_CC_P, 0, TRUE);
5613 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
5614 amd64_patch (jump, code);
5618 /* Branch if C013 != 100 */
5619 /* branch if !ZF or (PF|CF) */
5620 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
5621 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5622 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
5625 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5628 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5629 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
5633 if (ins->opcode == OP_FBGT) {
5636 /* skip branch if C1=1 */
5638 x86_branch8 (code, X86_CC_P, 0, FALSE);
5639 /* branch if (C0 | C3) = 1 */
5640 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5641 amd64_patch (br1, code);
5644 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
5648 /* Branch if C013 == 100 or 001 */
5651 /* skip branch if C1=1 */
5653 x86_branch8 (code, X86_CC_P, 0, FALSE);
5654 /* branch if (C0 | C3) = 1 */
5655 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
5656 amd64_patch (br1, code);
5660 /* Branch if C013 == 000 */
5661 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
5664 /* Branch if C013=000 or 100 */
5667 /* skip branch if C1=1 */
5669 x86_branch8 (code, X86_CC_P, 0, FALSE);
5670 /* branch if C0=0 */
5671 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
5672 amd64_patch (br1, code);
5676 /* Branch if C013 != 001 */
5677 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
5678 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
5681 /* Transfer value to the fp stack */
5682 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 16);
5683 amd64_movsd_membase_reg (code, AMD64_RSP, 0, ins->sreg1);
5684 amd64_fld_membase (code, AMD64_RSP, 0, TRUE);
5686 amd64_push_reg (code, AMD64_RAX);
5688 amd64_fnstsw (code);
5689 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0x4100);
5690 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, X86_FP_C0);
5691 amd64_pop_reg (code, AMD64_RAX);
5692 amd64_fstp (code, 0);
5693 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
5694 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 16);
5697 code = mono_amd64_emit_tls_get (code, ins->dreg, ins->inst_offset);
5700 case OP_TLS_GET_REG:
5701 code = emit_tls_get_reg (code, ins->dreg, ins->sreg1);
5704 code = amd64_emit_tls_set (code, ins->sreg1, ins->inst_offset);
5707 case OP_TLS_SET_REG: {
5708 code = amd64_emit_tls_set_reg (code, ins->sreg1, ins->sreg2);
5711 case OP_MEMORY_BARRIER: {
5712 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5716 case OP_ATOMIC_ADD_I4:
5717 case OP_ATOMIC_ADD_I8: {
5718 int dreg = ins->dreg;
5719 guint32 size = (ins->opcode == OP_ATOMIC_ADD_I4) ? 4 : 8;
5721 if ((dreg == ins->sreg2) || (dreg == ins->inst_basereg))
5724 amd64_mov_reg_reg (code, dreg, ins->sreg2, size);
5725 amd64_prefix (code, X86_LOCK_PREFIX);
5726 amd64_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, size);
5727 /* dreg contains the old value, add with sreg2 value */
5728 amd64_alu_reg_reg_size (code, X86_ADD, dreg, ins->sreg2, size);
5730 if (ins->dreg != dreg)
5731 amd64_mov_reg_reg (code, ins->dreg, dreg, size);
5735 case OP_ATOMIC_EXCHANGE_I4:
5736 case OP_ATOMIC_EXCHANGE_I8: {
5737 guint32 size = ins->opcode == OP_ATOMIC_EXCHANGE_I4 ? 4 : 8;
5739 /* LOCK prefix is implied. */
5740 amd64_mov_reg_reg (code, GP_SCRATCH_REG, ins->sreg2, size);
5741 amd64_xchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, GP_SCRATCH_REG, size);
5742 amd64_mov_reg_reg (code, ins->dreg, GP_SCRATCH_REG, size);
5745 case OP_ATOMIC_CAS_I4:
5746 case OP_ATOMIC_CAS_I8: {
5749 if (ins->opcode == OP_ATOMIC_CAS_I8)
5755 * See http://msdn.microsoft.com/en-us/magazine/cc302329.aspx for
5756 * an explanation of how this works.
5758 g_assert (ins->sreg3 == AMD64_RAX);
5759 g_assert (ins->sreg1 != AMD64_RAX);
5760 g_assert (ins->sreg1 != ins->sreg2);
5762 amd64_prefix (code, X86_LOCK_PREFIX);
5763 amd64_cmpxchg_membase_reg_size (code, ins->sreg1, ins->inst_offset, ins->sreg2, size);
5765 if (ins->dreg != AMD64_RAX)
5766 amd64_mov_reg_reg (code, ins->dreg, AMD64_RAX, size);
5769 case OP_ATOMIC_LOAD_I1: {
5770 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
5773 case OP_ATOMIC_LOAD_U1: {
5774 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
5777 case OP_ATOMIC_LOAD_I2: {
5778 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
5781 case OP_ATOMIC_LOAD_U2: {
5782 amd64_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
5785 case OP_ATOMIC_LOAD_I4: {
5786 amd64_movsxd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5789 case OP_ATOMIC_LOAD_U4:
5790 case OP_ATOMIC_LOAD_I8:
5791 case OP_ATOMIC_LOAD_U8: {
5792 amd64_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, ins->opcode == OP_ATOMIC_LOAD_U4 ? 4 : 8);
5795 case OP_ATOMIC_LOAD_R4: {
5796 amd64_sse_movss_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5797 amd64_sse_cvtss2sd_reg_reg (code, ins->dreg, ins->dreg);
5800 case OP_ATOMIC_LOAD_R8: {
5801 amd64_sse_movsd_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5804 case OP_ATOMIC_STORE_I1:
5805 case OP_ATOMIC_STORE_U1:
5806 case OP_ATOMIC_STORE_I2:
5807 case OP_ATOMIC_STORE_U2:
5808 case OP_ATOMIC_STORE_I4:
5809 case OP_ATOMIC_STORE_U4:
5810 case OP_ATOMIC_STORE_I8:
5811 case OP_ATOMIC_STORE_U8: {
5814 switch (ins->opcode) {
5815 case OP_ATOMIC_STORE_I1:
5816 case OP_ATOMIC_STORE_U1:
5819 case OP_ATOMIC_STORE_I2:
5820 case OP_ATOMIC_STORE_U2:
5823 case OP_ATOMIC_STORE_I4:
5824 case OP_ATOMIC_STORE_U4:
5827 case OP_ATOMIC_STORE_I8:
5828 case OP_ATOMIC_STORE_U8:
5833 amd64_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, size);
5835 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5839 case OP_ATOMIC_STORE_R4: {
5840 amd64_sse_cvtsd2ss_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
5841 amd64_sse_movss_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, MONO_ARCH_FP_SCRATCH_REG);
5843 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5847 case OP_ATOMIC_STORE_R8: {
5850 amd64_sse_movsd_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1);
5854 if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
5858 case OP_CARD_TABLE_WBARRIER: {
5859 int ptr = ins->sreg1;
5860 int value = ins->sreg2;
5862 int nursery_shift, card_table_shift;
5863 gpointer card_table_mask;
5864 size_t nursery_size;
5866 gpointer card_table = mono_gc_get_card_table (&card_table_shift, &card_table_mask);
5867 guint64 nursery_start = (guint64)mono_gc_get_nursery (&nursery_shift, &nursery_size);
5868 guint64 shifted_nursery_start = nursery_start >> nursery_shift;
5870 /*If either point to the stack we can simply avoid the WB. This happens due to
5871 * optimizations revealing a stack store that was not visible when op_cardtable was emited.
5873 if (ins->sreg1 == AMD64_RSP || ins->sreg2 == AMD64_RSP)
5877 * We need one register we can clobber, we choose EDX and make sreg1
5878 * fixed EAX to work around limitations in the local register allocator.
5879 * sreg2 might get allocated to EDX, but that is not a problem since
5880 * we use it before clobbering EDX.
5882 g_assert (ins->sreg1 == AMD64_RAX);
5885 * This is the code we produce:
5888 * edx >>= nursery_shift
5889 * cmp edx, (nursery_start >> nursery_shift)
5892 * edx >>= card_table_shift
5898 if (mono_gc_card_table_nursery_check ()) {
5899 if (value != AMD64_RDX)
5900 amd64_mov_reg_reg (code, AMD64_RDX, value, 8);
5901 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, nursery_shift);
5902 if (shifted_nursery_start >> 31) {
5904 * The value we need to compare against is 64 bits, so we need
5905 * another spare register. We use RBX, which we save and
5908 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RBX, 8);
5909 amd64_mov_reg_imm (code, AMD64_RBX, shifted_nursery_start);
5910 amd64_alu_reg_reg (code, X86_CMP, AMD64_RDX, AMD64_RBX);
5911 amd64_mov_reg_membase (code, AMD64_RBX, AMD64_RSP, -8, 8);
5913 amd64_alu_reg_imm (code, X86_CMP, AMD64_RDX, shifted_nursery_start);
5915 br = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
5917 amd64_mov_reg_reg (code, AMD64_RDX, ptr, 8);
5918 amd64_shift_reg_imm (code, X86_SHR, AMD64_RDX, card_table_shift);
5919 if (card_table_mask)
5920 amd64_alu_reg_imm (code, X86_AND, AMD64_RDX, (guint32)(guint64)card_table_mask);
5922 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GC_CARD_TABLE_ADDR, card_table);
5923 amd64_alu_reg_membase (code, X86_ADD, AMD64_RDX, AMD64_RIP, 0);
5925 amd64_mov_membase_imm (code, AMD64_RDX, 0, 1, 1);
5927 if (mono_gc_card_table_nursery_check ())
5928 x86_patch (br, code);
5931 #ifdef MONO_ARCH_SIMD_INTRINSICS
5932 /* TODO: Some of these IR opcodes are marked as no clobber when they indeed do. */
5934 amd64_sse_addps_reg_reg (code, ins->sreg1, ins->sreg2);
5937 amd64_sse_divps_reg_reg (code, ins->sreg1, ins->sreg2);
5940 amd64_sse_mulps_reg_reg (code, ins->sreg1, ins->sreg2);
5943 amd64_sse_subps_reg_reg (code, ins->sreg1, ins->sreg2);
5946 amd64_sse_maxps_reg_reg (code, ins->sreg1, ins->sreg2);
5949 amd64_sse_minps_reg_reg (code, ins->sreg1, ins->sreg2);
5952 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
5953 amd64_sse_cmpps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
5956 amd64_sse_andps_reg_reg (code, ins->sreg1, ins->sreg2);
5959 amd64_sse_andnps_reg_reg (code, ins->sreg1, ins->sreg2);
5962 amd64_sse_orps_reg_reg (code, ins->sreg1, ins->sreg2);
5965 amd64_sse_xorps_reg_reg (code, ins->sreg1, ins->sreg2);
5968 amd64_sse_sqrtps_reg_reg (code, ins->dreg, ins->sreg1);
5971 amd64_sse_rsqrtps_reg_reg (code, ins->dreg, ins->sreg1);
5974 amd64_sse_rcpps_reg_reg (code, ins->dreg, ins->sreg1);
5977 amd64_sse_addsubps_reg_reg (code, ins->sreg1, ins->sreg2);
5980 amd64_sse_haddps_reg_reg (code, ins->sreg1, ins->sreg2);
5983 amd64_sse_hsubps_reg_reg (code, ins->sreg1, ins->sreg2);
5986 amd64_sse_movshdup_reg_reg (code, ins->dreg, ins->sreg1);
5989 amd64_sse_movsldup_reg_reg (code, ins->dreg, ins->sreg1);
5992 case OP_PSHUFLEW_HIGH:
5993 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5994 amd64_sse_pshufhw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
5996 case OP_PSHUFLEW_LOW:
5997 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
5998 amd64_sse_pshuflw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6001 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6002 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6005 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0xFF);
6006 amd64_sse_shufps_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6009 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 0x3);
6010 amd64_sse_shufpd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6014 amd64_sse_addpd_reg_reg (code, ins->sreg1, ins->sreg2);
6017 amd64_sse_divpd_reg_reg (code, ins->sreg1, ins->sreg2);
6020 amd64_sse_mulpd_reg_reg (code, ins->sreg1, ins->sreg2);
6023 amd64_sse_subpd_reg_reg (code, ins->sreg1, ins->sreg2);
6026 amd64_sse_maxpd_reg_reg (code, ins->sreg1, ins->sreg2);
6029 amd64_sse_minpd_reg_reg (code, ins->sreg1, ins->sreg2);
6032 g_assert (ins->inst_c0 >= 0 && ins->inst_c0 <= 7);
6033 amd64_sse_cmppd_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6036 amd64_sse_andpd_reg_reg (code, ins->sreg1, ins->sreg2);
6039 amd64_sse_andnpd_reg_reg (code, ins->sreg1, ins->sreg2);
6042 amd64_sse_orpd_reg_reg (code, ins->sreg1, ins->sreg2);
6045 amd64_sse_xorpd_reg_reg (code, ins->sreg1, ins->sreg2);
6048 amd64_sse_sqrtpd_reg_reg (code, ins->dreg, ins->sreg1);
6051 amd64_sse_addsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6054 amd64_sse_haddpd_reg_reg (code, ins->sreg1, ins->sreg2);
6057 amd64_sse_hsubpd_reg_reg (code, ins->sreg1, ins->sreg2);
6060 amd64_sse_movddup_reg_reg (code, ins->dreg, ins->sreg1);
6063 case OP_EXTRACT_MASK:
6064 amd64_sse_pmovmskb_reg_reg (code, ins->dreg, ins->sreg1);
6068 amd64_sse_pand_reg_reg (code, ins->sreg1, ins->sreg2);
6071 amd64_sse_por_reg_reg (code, ins->sreg1, ins->sreg2);
6074 amd64_sse_pxor_reg_reg (code, ins->sreg1, ins->sreg2);
6078 amd64_sse_paddb_reg_reg (code, ins->sreg1, ins->sreg2);
6081 amd64_sse_paddw_reg_reg (code, ins->sreg1, ins->sreg2);
6084 amd64_sse_paddd_reg_reg (code, ins->sreg1, ins->sreg2);
6087 amd64_sse_paddq_reg_reg (code, ins->sreg1, ins->sreg2);
6091 amd64_sse_psubb_reg_reg (code, ins->sreg1, ins->sreg2);
6094 amd64_sse_psubw_reg_reg (code, ins->sreg1, ins->sreg2);
6097 amd64_sse_psubd_reg_reg (code, ins->sreg1, ins->sreg2);
6100 amd64_sse_psubq_reg_reg (code, ins->sreg1, ins->sreg2);
6104 amd64_sse_pmaxub_reg_reg (code, ins->sreg1, ins->sreg2);
6107 amd64_sse_pmaxuw_reg_reg (code, ins->sreg1, ins->sreg2);
6110 amd64_sse_pmaxud_reg_reg (code, ins->sreg1, ins->sreg2);
6114 amd64_sse_pmaxsb_reg_reg (code, ins->sreg1, ins->sreg2);
6117 amd64_sse_pmaxsw_reg_reg (code, ins->sreg1, ins->sreg2);
6120 amd64_sse_pmaxsd_reg_reg (code, ins->sreg1, ins->sreg2);
6124 amd64_sse_pavgb_reg_reg (code, ins->sreg1, ins->sreg2);
6127 amd64_sse_pavgw_reg_reg (code, ins->sreg1, ins->sreg2);
6131 amd64_sse_pminub_reg_reg (code, ins->sreg1, ins->sreg2);
6134 amd64_sse_pminuw_reg_reg (code, ins->sreg1, ins->sreg2);
6137 amd64_sse_pminud_reg_reg (code, ins->sreg1, ins->sreg2);
6141 amd64_sse_pminsb_reg_reg (code, ins->sreg1, ins->sreg2);
6144 amd64_sse_pminsw_reg_reg (code, ins->sreg1, ins->sreg2);
6147 amd64_sse_pminsd_reg_reg (code, ins->sreg1, ins->sreg2);
6151 amd64_sse_pcmpeqb_reg_reg (code, ins->sreg1, ins->sreg2);
6154 amd64_sse_pcmpeqw_reg_reg (code, ins->sreg1, ins->sreg2);
6157 amd64_sse_pcmpeqd_reg_reg (code, ins->sreg1, ins->sreg2);
6160 amd64_sse_pcmpeqq_reg_reg (code, ins->sreg1, ins->sreg2);
6164 amd64_sse_pcmpgtb_reg_reg (code, ins->sreg1, ins->sreg2);
6167 amd64_sse_pcmpgtw_reg_reg (code, ins->sreg1, ins->sreg2);
6170 amd64_sse_pcmpgtd_reg_reg (code, ins->sreg1, ins->sreg2);
6173 amd64_sse_pcmpgtq_reg_reg (code, ins->sreg1, ins->sreg2);
6176 case OP_PSUM_ABS_DIFF:
6177 amd64_sse_psadbw_reg_reg (code, ins->sreg1, ins->sreg2);
6180 case OP_UNPACK_LOWB:
6181 amd64_sse_punpcklbw_reg_reg (code, ins->sreg1, ins->sreg2);
6183 case OP_UNPACK_LOWW:
6184 amd64_sse_punpcklwd_reg_reg (code, ins->sreg1, ins->sreg2);
6186 case OP_UNPACK_LOWD:
6187 amd64_sse_punpckldq_reg_reg (code, ins->sreg1, ins->sreg2);
6189 case OP_UNPACK_LOWQ:
6190 amd64_sse_punpcklqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6192 case OP_UNPACK_LOWPS:
6193 amd64_sse_unpcklps_reg_reg (code, ins->sreg1, ins->sreg2);
6195 case OP_UNPACK_LOWPD:
6196 amd64_sse_unpcklpd_reg_reg (code, ins->sreg1, ins->sreg2);
6199 case OP_UNPACK_HIGHB:
6200 amd64_sse_punpckhbw_reg_reg (code, ins->sreg1, ins->sreg2);
6202 case OP_UNPACK_HIGHW:
6203 amd64_sse_punpckhwd_reg_reg (code, ins->sreg1, ins->sreg2);
6205 case OP_UNPACK_HIGHD:
6206 amd64_sse_punpckhdq_reg_reg (code, ins->sreg1, ins->sreg2);
6208 case OP_UNPACK_HIGHQ:
6209 amd64_sse_punpckhqdq_reg_reg (code, ins->sreg1, ins->sreg2);
6211 case OP_UNPACK_HIGHPS:
6212 amd64_sse_unpckhps_reg_reg (code, ins->sreg1, ins->sreg2);
6214 case OP_UNPACK_HIGHPD:
6215 amd64_sse_unpckhpd_reg_reg (code, ins->sreg1, ins->sreg2);
6219 amd64_sse_packsswb_reg_reg (code, ins->sreg1, ins->sreg2);
6222 amd64_sse_packssdw_reg_reg (code, ins->sreg1, ins->sreg2);
6225 amd64_sse_packuswb_reg_reg (code, ins->sreg1, ins->sreg2);
6228 amd64_sse_packusdw_reg_reg (code, ins->sreg1, ins->sreg2);
6231 case OP_PADDB_SAT_UN:
6232 amd64_sse_paddusb_reg_reg (code, ins->sreg1, ins->sreg2);
6234 case OP_PSUBB_SAT_UN:
6235 amd64_sse_psubusb_reg_reg (code, ins->sreg1, ins->sreg2);
6237 case OP_PADDW_SAT_UN:
6238 amd64_sse_paddusw_reg_reg (code, ins->sreg1, ins->sreg2);
6240 case OP_PSUBW_SAT_UN:
6241 amd64_sse_psubusw_reg_reg (code, ins->sreg1, ins->sreg2);
6245 amd64_sse_paddsb_reg_reg (code, ins->sreg1, ins->sreg2);
6248 amd64_sse_psubsb_reg_reg (code, ins->sreg1, ins->sreg2);
6251 amd64_sse_paddsw_reg_reg (code, ins->sreg1, ins->sreg2);
6254 amd64_sse_psubsw_reg_reg (code, ins->sreg1, ins->sreg2);
6258 amd64_sse_pmullw_reg_reg (code, ins->sreg1, ins->sreg2);
6261 amd64_sse_pmulld_reg_reg (code, ins->sreg1, ins->sreg2);
6264 amd64_sse_pmuludq_reg_reg (code, ins->sreg1, ins->sreg2);
6266 case OP_PMULW_HIGH_UN:
6267 amd64_sse_pmulhuw_reg_reg (code, ins->sreg1, ins->sreg2);
6270 amd64_sse_pmulhw_reg_reg (code, ins->sreg1, ins->sreg2);
6274 amd64_sse_psrlw_reg_imm (code, ins->dreg, ins->inst_imm);
6277 amd64_sse_psrlw_reg_reg (code, ins->dreg, ins->sreg2);
6281 amd64_sse_psraw_reg_imm (code, ins->dreg, ins->inst_imm);
6284 amd64_sse_psraw_reg_reg (code, ins->dreg, ins->sreg2);
6288 amd64_sse_psllw_reg_imm (code, ins->dreg, ins->inst_imm);
6291 amd64_sse_psllw_reg_reg (code, ins->dreg, ins->sreg2);
6295 amd64_sse_psrld_reg_imm (code, ins->dreg, ins->inst_imm);
6298 amd64_sse_psrld_reg_reg (code, ins->dreg, ins->sreg2);
6302 amd64_sse_psrad_reg_imm (code, ins->dreg, ins->inst_imm);
6305 amd64_sse_psrad_reg_reg (code, ins->dreg, ins->sreg2);
6309 amd64_sse_pslld_reg_imm (code, ins->dreg, ins->inst_imm);
6312 amd64_sse_pslld_reg_reg (code, ins->dreg, ins->sreg2);
6316 amd64_sse_psrlq_reg_imm (code, ins->dreg, ins->inst_imm);
6319 amd64_sse_psrlq_reg_reg (code, ins->dreg, ins->sreg2);
6322 /*TODO: This is appart of the sse spec but not added
6324 amd64_sse_psraq_reg_imm (code, ins->dreg, ins->inst_imm);
6327 amd64_sse_psraq_reg_reg (code, ins->dreg, ins->sreg2);
6332 amd64_sse_psllq_reg_imm (code, ins->dreg, ins->inst_imm);
6335 amd64_sse_psllq_reg_reg (code, ins->dreg, ins->sreg2);
6338 amd64_sse_cvtdq2pd_reg_reg (code, ins->dreg, ins->sreg1);
6341 amd64_sse_cvtdq2ps_reg_reg (code, ins->dreg, ins->sreg1);
6344 amd64_sse_cvtpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6347 amd64_sse_cvtpd2ps_reg_reg (code, ins->dreg, ins->sreg1);
6350 amd64_sse_cvtps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6353 amd64_sse_cvtps2pd_reg_reg (code, ins->dreg, ins->sreg1);
6356 amd64_sse_cvttpd2dq_reg_reg (code, ins->dreg, ins->sreg1);
6359 amd64_sse_cvttps2dq_reg_reg (code, ins->dreg, ins->sreg1);
6363 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6366 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6370 amd64_movhlps_reg_reg (code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg1);
6371 amd64_movd_reg_xreg_size (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG, 8);
6373 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 8);
6378 amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6380 amd64_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_c0 * 8);
6381 amd64_widen_reg (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I1, FALSE);
6385 /*amd64_movd_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6387 amd64_shift_reg_imm_size (code, X86_SHR, ins->dreg, 16, 4);*/
6388 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6389 amd64_widen_reg_size (code, ins->dreg, ins->dreg, ins->opcode == OP_EXTRACT_I2, TRUE, 4);
6393 amd64_movhlps_reg_reg (code, ins->dreg, ins->sreg1);
6395 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6398 amd64_sse_pinsrw_reg_reg_imm (code, ins->sreg1, ins->sreg2, ins->inst_c0);
6400 case OP_EXTRACTX_U2:
6401 amd64_sse_pextrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0);
6403 case OP_INSERTX_U1_SLOW:
6404 /*sreg1 is the extracted ireg (scratch)
6405 /sreg2 is the to be inserted ireg (scratch)
6406 /dreg is the xreg to receive the value*/
6408 /*clear the bits from the extracted word*/
6409 amd64_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_c0 & 1 ? 0x00FF : 0xFF00);
6410 /*shift the value to insert if needed*/
6411 if (ins->inst_c0 & 1)
6412 amd64_shift_reg_imm_size (code, X86_SHL, ins->sreg2, 8, 4);
6413 /*join them together*/
6414 amd64_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
6415 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_c0 / 2);
6417 case OP_INSERTX_I4_SLOW:
6418 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2);
6419 amd64_shift_reg_imm (code, X86_SHR, ins->sreg2, 16);
6420 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg2, ins->inst_c0 * 2 + 1);
6422 case OP_INSERTX_I8_SLOW:
6423 amd64_movd_xreg_reg_size(code, MONO_ARCH_FP_SCRATCH_REG, ins->sreg2, 8);
6425 amd64_movlhps_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6427 amd64_sse_movsd_reg_reg (code, ins->dreg, MONO_ARCH_FP_SCRATCH_REG);
6430 case OP_INSERTX_R4_SLOW:
6431 switch (ins->inst_c0) {
6434 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6436 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6439 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6441 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6443 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6444 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(1, 0, 2, 3));
6447 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6449 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6451 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6452 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(2, 1, 0, 3));
6455 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6457 amd64_sse_movss_reg_reg (code, ins->dreg, ins->sreg2);
6459 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->sreg2);
6460 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, mono_simd_shuffle_mask(3, 1, 2, 0));
6464 case OP_INSERTX_R8_SLOW:
6466 amd64_movlhps_reg_reg (code, ins->dreg, ins->sreg2);
6468 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg2);
6470 case OP_STOREX_MEMBASE_REG:
6471 case OP_STOREX_MEMBASE:
6472 amd64_sse_movups_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6474 case OP_LOADX_MEMBASE:
6475 amd64_sse_movups_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6477 case OP_LOADX_ALIGNED_MEMBASE:
6478 amd64_sse_movaps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6480 case OP_STOREX_ALIGNED_MEMBASE_REG:
6481 amd64_sse_movaps_membase_reg (code, ins->dreg, ins->inst_offset, ins->sreg1);
6483 case OP_STOREX_NTA_MEMBASE_REG:
6484 amd64_sse_movntps_reg_membase (code, ins->dreg, ins->sreg1, ins->inst_offset);
6486 case OP_PREFETCH_MEMBASE:
6487 amd64_sse_prefetch_reg_membase (code, ins->backend.arg_info, ins->sreg1, ins->inst_offset);
6491 /*FIXME the peephole pass should have killed this*/
6492 if (ins->dreg != ins->sreg1)
6493 amd64_sse_movaps_reg_reg (code, ins->dreg, ins->sreg1);
6496 amd64_sse_pxor_reg_reg (code, ins->dreg, ins->dreg);
6498 case OP_ICONV_TO_R4_RAW:
6499 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6502 case OP_FCONV_TO_R8_X:
6503 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6506 case OP_XCONV_R8_TO_I4:
6507 amd64_sse_cvttsd2si_reg_xreg_size (code, ins->dreg, ins->sreg1, 4);
6508 switch (ins->backend.source_opcode) {
6509 case OP_FCONV_TO_I1:
6510 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, FALSE);
6512 case OP_FCONV_TO_U1:
6513 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
6515 case OP_FCONV_TO_I2:
6516 amd64_widen_reg (code, ins->dreg, ins->dreg, TRUE, TRUE);
6518 case OP_FCONV_TO_U2:
6519 amd64_widen_reg (code, ins->dreg, ins->dreg, FALSE, TRUE);
6525 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 0);
6526 amd64_sse_pinsrw_reg_reg_imm (code, ins->dreg, ins->sreg1, 1);
6527 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6530 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 4);
6531 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6534 amd64_movd_xreg_reg_size (code, ins->dreg, ins->sreg1, 8);
6535 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6539 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6541 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6542 amd64_sse_cvtsd2ss_reg_reg (code, ins->dreg, ins->dreg);
6544 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0);
6547 amd64_sse_movsd_reg_reg (code, ins->dreg, ins->sreg1);
6548 amd64_sse_pshufd_reg_reg_imm (code, ins->dreg, ins->dreg, 0x44);
6551 case OP_LIVERANGE_START: {
6552 if (cfg->verbose_level > 1)
6553 printf ("R%d START=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6554 MONO_VARINFO (cfg, ins->inst_c0)->live_range_start = code - cfg->native_code;
6557 case OP_LIVERANGE_END: {
6558 if (cfg->verbose_level > 1)
6559 printf ("R%d END=0x%x\n", MONO_VARINFO (cfg, ins->inst_c0)->vreg, (int)(code - cfg->native_code));
6560 MONO_VARINFO (cfg, ins->inst_c0)->live_range_end = code - cfg->native_code;
6563 case OP_NACL_GC_SAFE_POINT: {
6564 #if defined(__native_client_codegen__) && defined(__native_client_gc__)
6565 if (cfg->compile_aot)
6566 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc, TRUE);
6570 amd64_mov_reg_imm_size (code, AMD64_R11, (gpointer)&__nacl_thread_suspension_needed, 4);
6571 amd64_test_membase_imm_size (code, AMD64_R11, 0, 0xFFFFFFFF, 4);
6572 br[0] = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
6573 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)mono_nacl_gc, TRUE);
6574 amd64_patch (br[0], code);
6579 case OP_GC_LIVENESS_DEF:
6580 case OP_GC_LIVENESS_USE:
6581 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
6582 ins->backend.pc_offset = code - cfg->native_code;
6584 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
6585 ins->backend.pc_offset = code - cfg->native_code;
6586 bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
6589 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
6590 g_assert_not_reached ();
6593 if ((code - cfg->native_code - offset) > max_len) {
6594 #if !defined(__native_client_codegen__)
6595 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %ld)",
6596 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
6597 g_assert_not_reached ();
6602 last_offset = offset;
6605 cfg->code_len = code - cfg->native_code;
6608 #endif /* DISABLE_JIT */
6611 mono_arch_register_lowlevel_calls (void)
6613 /* The signature doesn't matter */
6614 mono_register_jit_icall (mono_amd64_throw_exception, "mono_amd64_throw_exception", mono_create_icall_signature ("void"), TRUE);
6618 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
6620 MonoJumpInfo *patch_info;
6621 gboolean compile_aot = !run_cctors;
6623 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
6624 unsigned char *ip = patch_info->ip.i + code;
6625 unsigned char *target;
6628 switch (patch_info->type) {
6629 case MONO_PATCH_INFO_BB:
6630 case MONO_PATCH_INFO_LABEL:
6633 /* No need to patch these */
6638 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
6640 switch (patch_info->type) {
6641 case MONO_PATCH_INFO_NONE:
6643 case MONO_PATCH_INFO_METHOD_REL:
6644 case MONO_PATCH_INFO_R8:
6645 case MONO_PATCH_INFO_R4:
6646 g_assert_not_reached ();
6648 case MONO_PATCH_INFO_BB:
6655 * Debug code to help track down problems where the target of a near call is
6658 if (amd64_is_near_call (ip)) {
6659 gint64 disp = (guint8*)target - (guint8*)ip;
6661 if (!amd64_is_imm32 (disp)) {
6662 printf ("TYPE: %d\n", patch_info->type);
6663 switch (patch_info->type) {
6664 case MONO_PATCH_INFO_INTERNAL_METHOD:
6665 printf ("V: %s\n", patch_info->data.name);
6667 case MONO_PATCH_INFO_METHOD_JUMP:
6668 case MONO_PATCH_INFO_METHOD:
6669 printf ("V: %s\n", patch_info->data.method->name);
6677 amd64_patch (ip, (gpointer)target);
6684 get_max_epilog_size (MonoCompile *cfg)
6686 int max_epilog_size = 16;
6688 if (cfg->method->save_lmf)
6689 max_epilog_size += 256;
6691 if (mono_jit_trace_calls != NULL)
6692 max_epilog_size += 50;
6694 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6695 max_epilog_size += 50;
6697 max_epilog_size += (AMD64_NREG * 2);
6699 return max_epilog_size;
6703 * This macro is used for testing whenever the unwinder works correctly at every point
6704 * where an async exception can happen.
6706 /* This will generate a SIGSEGV at the given point in the code */
6707 #define async_exc_point(code) do { \
6708 if (mono_inject_async_exc_method && mono_method_desc_full_match (mono_inject_async_exc_method, cfg->method)) { \
6709 if (cfg->arch.async_point_count == mono_inject_async_exc_pos) \
6710 amd64_mov_reg_mem (code, AMD64_RAX, 0, 4); \
6711 cfg->arch.async_point_count ++; \
6716 mono_arch_emit_prolog (MonoCompile *cfg)
6718 MonoMethod *method = cfg->method;
6720 MonoMethodSignature *sig;
6722 int alloc_size, pos, i, cfa_offset, quad, max_epilog_size, save_area_offset;
6725 MonoInst *lmf_var = cfg->lmf_var;
6726 gboolean args_clobbered = FALSE;
6727 gboolean trace = FALSE;
6728 #ifdef __native_client_codegen__
6729 guint alignment_check;
6732 cfg->code_size = MAX (cfg->header->code_size * 4, 1024);
6734 #if defined(__default_codegen__)
6735 code = cfg->native_code = g_malloc (cfg->code_size);
6736 #elif defined(__native_client_codegen__)
6737 /* native_code_alloc is not 32-byte aligned, native_code is. */
6738 cfg->native_code_alloc = g_malloc (cfg->code_size + kNaClAlignment);
6740 /* Align native_code to next nearest kNaclAlignment byte. */
6741 cfg->native_code = (uintptr_t)cfg->native_code_alloc + kNaClAlignment;
6742 cfg->native_code = (uintptr_t)cfg->native_code & ~kNaClAlignmentMask;
6744 code = cfg->native_code;
6746 alignment_check = (guint)cfg->native_code & kNaClAlignmentMask;
6747 g_assert (alignment_check == 0);
6750 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6753 /* Amount of stack space allocated by register saving code */
6756 /* Offset between RSP and the CFA */
6760 * The prolog consists of the following parts:
6762 * - push rbp, mov rbp, rsp
6763 * - save callee saved regs using pushes
6765 * - save rgctx if needed
6766 * - save lmf if needed
6769 * - save rgctx if needed
6770 * - save lmf if needed
6771 * - save callee saved regs using moves
6776 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
6777 // IP saved at CFA - 8
6778 mono_emit_unwind_op_offset (cfg, code, AMD64_RIP, -cfa_offset);
6779 async_exc_point (code);
6780 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6782 if (!cfg->arch.omit_fp) {
6783 amd64_push_reg (code, AMD64_RBP);
6785 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6786 mono_emit_unwind_op_offset (cfg, code, AMD64_RBP, - cfa_offset);
6787 async_exc_point (code);
6789 mono_arch_unwindinfo_add_push_nonvol (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6791 /* These are handled automatically by the stack marking code */
6792 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset, SLOT_NOREF);
6794 amd64_mov_reg_reg (code, AMD64_RBP, AMD64_RSP, sizeof(mgreg_t));
6795 mono_emit_unwind_op_def_cfa_reg (cfg, code, AMD64_RBP);
6796 async_exc_point (code);
6798 mono_arch_unwindinfo_add_set_fpreg (&cfg->arch.unwindinfo, cfg->native_code, code, AMD64_RBP);
6802 /* The param area is always at offset 0 from sp */
6803 /* This needs to be allocated here, since it has to come after the spill area */
6804 if (cfg->param_area) {
6805 if (cfg->arch.omit_fp)
6807 g_assert_not_reached ();
6808 cfg->stack_offset += ALIGN_TO (cfg->param_area, sizeof(mgreg_t));
6811 if (cfg->arch.omit_fp) {
6813 * On enter, the stack is misaligned by the pushing of the return
6814 * address. It is either made aligned by the pushing of %rbp, or by
6817 alloc_size = ALIGN_TO (cfg->stack_offset, 8);
6818 if ((alloc_size % 16) == 0) {
6820 /* Mark the padding slot as NOREF */
6821 mini_gc_set_slot_type_from_cfa (cfg, -cfa_offset - sizeof (mgreg_t), SLOT_NOREF);
6824 alloc_size = ALIGN_TO (cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
6825 if (cfg->stack_offset != alloc_size) {
6826 /* Mark the padding slot as NOREF */
6827 mini_gc_set_slot_type_from_fp (cfg, -alloc_size + cfg->param_area, SLOT_NOREF);
6829 cfg->arch.sp_fp_offset = alloc_size;
6833 cfg->arch.stack_alloc_size = alloc_size;
6835 /* Allocate stack frame */
6837 /* See mono_emit_stack_alloc */
6838 #if defined(HOST_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
6839 guint32 remaining_size = alloc_size;
6840 /*FIXME handle unbounded code expansion, we should use a loop in case of more than X interactions*/
6841 guint32 required_code_size = ((remaining_size / 0x1000) + 1) * 10; /*10 is the max size of amd64_alu_reg_imm + amd64_test_membase_reg*/
6842 guint32 offset = code - cfg->native_code;
6843 if (G_UNLIKELY (required_code_size >= (cfg->code_size - offset))) {
6844 while (required_code_size >= (cfg->code_size - offset))
6845 cfg->code_size *= 2;
6846 cfg->native_code = mono_realloc_native_code (cfg);
6847 code = cfg->native_code + offset;
6848 cfg->stat_code_reallocs++;
6851 while (remaining_size >= 0x1000) {
6852 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 0x1000);
6853 if (cfg->arch.omit_fp) {
6854 cfa_offset += 0x1000;
6855 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6857 async_exc_point (code);
6859 if (cfg->arch.omit_fp)
6860 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, 0x1000);
6863 amd64_test_membase_reg (code, AMD64_RSP, 0, AMD64_RSP);
6864 remaining_size -= 0x1000;
6866 if (remaining_size) {
6867 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, remaining_size);
6868 if (cfg->arch.omit_fp) {
6869 cfa_offset += remaining_size;
6870 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6871 async_exc_point (code);
6874 if (cfg->arch.omit_fp)
6875 mono_arch_unwindinfo_add_alloc_stack (&cfg->arch.unwindinfo, cfg->native_code, code, remaining_size);
6879 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, alloc_size);
6880 if (cfg->arch.omit_fp) {
6881 cfa_offset += alloc_size;
6882 mono_emit_unwind_op_def_cfa_offset (cfg, code, cfa_offset);
6883 async_exc_point (code);
6888 /* Stack alignment check */
6891 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_RSP, 8);
6892 amd64_alu_reg_imm (code, X86_AND, AMD64_RAX, 0xf);
6893 amd64_alu_reg_imm (code, X86_CMP, AMD64_RAX, 0);
6894 x86_branch8 (code, X86_CC_EQ, 2, FALSE);
6895 amd64_breakpoint (code);
6899 if (mini_get_debug_options ()->init_stacks) {
6900 /* Fill the stack frame with a dummy value to force deterministic behavior */
6902 /* Save registers to the red zone */
6903 amd64_mov_membase_reg (code, AMD64_RSP, -8, AMD64_RDI, 8);
6904 amd64_mov_membase_reg (code, AMD64_RSP, -16, AMD64_RCX, 8);
6906 amd64_mov_reg_imm (code, AMD64_RAX, 0x2a2a2a2a2a2a2a2a);
6907 amd64_mov_reg_imm (code, AMD64_RCX, alloc_size / 8);
6908 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RSP, 8);
6911 #if defined(__default_codegen__)
6912 amd64_prefix (code, X86_REP_PREFIX);
6914 #elif defined(__native_client_codegen__)
6915 /* NaCl stos pseudo-instruction */
6916 amd64_codegen_pre (code);
6917 /* First, clear the upper 32 bits of RDI (mov %edi, %edi) */
6918 amd64_mov_reg_reg (code, AMD64_RDI, AMD64_RDI, 4);
6919 /* Add %r15 to %rdi using lea, condition flags unaffected. */
6920 amd64_lea_memindex_size (code, AMD64_RDI, AMD64_R15, 0, AMD64_RDI, 0, 8);
6921 amd64_prefix (code, X86_REP_PREFIX);
6923 amd64_codegen_post (code);
6924 #endif /* __native_client_codegen__ */
6926 amd64_mov_reg_membase (code, AMD64_RDI, AMD64_RSP, -8, 8);
6927 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RSP, -16, 8);
6931 if (method->save_lmf)
6932 code = emit_setup_lmf (cfg, code, lmf_var->inst_offset, cfa_offset);
6934 /* Save callee saved registers */
6935 if (cfg->arch.omit_fp) {
6936 save_area_offset = cfg->arch.reg_save_area_offset;
6937 /* Save caller saved registers after sp is adjusted */
6938 /* The registers are saved at the bottom of the frame */
6939 /* FIXME: Optimize this so the regs are saved at the end of the frame in increasing order */
6941 /* The registers are saved just below the saved rbp */
6942 save_area_offset = cfg->arch.reg_save_area_offset;
6945 for (i = 0; i < AMD64_NREG; ++i) {
6946 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
6947 amd64_mov_membase_reg (code, cfg->frame_reg, save_area_offset, i, 8);
6949 if (cfg->arch.omit_fp) {
6950 mono_emit_unwind_op_offset (cfg, code, i, - (cfa_offset - save_area_offset));
6951 /* These are handled automatically by the stack marking code */
6952 mini_gc_set_slot_type_from_cfa (cfg, - (cfa_offset - save_area_offset), SLOT_NOREF);
6954 mono_emit_unwind_op_offset (cfg, code, i, - (-save_area_offset + (2 * 8)));
6958 save_area_offset += 8;
6959 async_exc_point (code);
6963 /* store runtime generic context */
6964 if (cfg->rgctx_var) {
6965 g_assert (cfg->rgctx_var->opcode == OP_REGOFFSET &&
6966 (cfg->rgctx_var->inst_basereg == AMD64_RBP || cfg->rgctx_var->inst_basereg == AMD64_RSP));
6968 amd64_mov_membase_reg (code, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, MONO_ARCH_RGCTX_REG, sizeof(gpointer));
6970 mono_add_var_location (cfg, cfg->rgctx_var, TRUE, MONO_ARCH_RGCTX_REG, 0, 0, code - cfg->native_code);
6971 mono_add_var_location (cfg, cfg->rgctx_var, FALSE, cfg->rgctx_var->inst_basereg, cfg->rgctx_var->inst_offset, code - cfg->native_code, 0);
6974 /* compute max_length in order to use short forward jumps */
6975 max_epilog_size = get_max_epilog_size (cfg);
6976 if (cfg->opt & MONO_OPT_BRANCH) {
6977 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6981 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6983 /* max alignment for loops */
6984 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
6985 max_length += LOOP_ALIGNMENT;
6986 #ifdef __native_client_codegen__
6987 /* max alignment for native client */
6988 max_length += kNaClAlignment;
6991 MONO_BB_FOR_EACH_INS (bb, ins) {
6992 #ifdef __native_client_codegen__
6994 int space_in_block = kNaClAlignment -
6995 ((max_length + cfg->code_len) & kNaClAlignmentMask);
6996 int max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
6997 if (space_in_block < max_len && max_len < kNaClAlignment) {
6998 max_length += space_in_block;
7001 #endif /*__native_client_codegen__*/
7002 max_length += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
7005 /* Take prolog and epilog instrumentation into account */
7006 if (bb == cfg->bb_entry || bb == cfg->bb_exit)
7007 max_length += max_epilog_size;
7009 bb->max_length = max_length;
7013 sig = mono_method_signature (method);
7016 cinfo = cfg->arch.cinfo;
7018 if (sig->ret->type != MONO_TYPE_VOID) {
7019 /* Save volatile arguments to the stack */
7020 if (cfg->vret_addr && (cfg->vret_addr->opcode != OP_REGVAR))
7021 amd64_mov_membase_reg (code, cfg->vret_addr->inst_basereg, cfg->vret_addr->inst_offset, cinfo->ret.reg, 8);
7024 /* Keep this in sync with emit_load_volatile_arguments */
7025 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
7026 ArgInfo *ainfo = cinfo->args + i;
7027 gint32 stack_offset;
7030 ins = cfg->args [i];
7032 if ((ins->flags & MONO_INST_IS_DEAD) && !trace)
7033 /* Unused arguments */
7036 if (sig->hasthis && (i == 0))
7037 arg_type = &mono_defaults.object_class->byval_arg;
7039 arg_type = sig->params [i - sig->hasthis];
7041 stack_offset = ainfo->offset + ARGS_OFFSET;
7043 if (cfg->globalra) {
7044 /* All the other moves are done by the register allocator */
7045 switch (ainfo->storage) {
7046 case ArgInFloatSSEReg:
7047 amd64_sse_cvtss2sd_reg_reg (code, ainfo->reg, ainfo->reg);
7049 case ArgValuetypeInReg:
7050 for (quad = 0; quad < 2; quad ++) {
7051 switch (ainfo->pair_storage [quad]) {
7053 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7055 case ArgInFloatSSEReg:
7056 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7058 case ArgInDoubleSSEReg:
7059 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7064 g_assert_not_reached ();
7075 /* Save volatile arguments to the stack */
7076 if (ins->opcode != OP_REGVAR) {
7077 switch (ainfo->storage) {
7083 if (stack_offset & 0x1)
7085 else if (stack_offset & 0x2)
7087 else if (stack_offset & 0x4)
7092 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg, size);
7095 * Save the original location of 'this',
7096 * get_generic_info_from_stack_frame () needs this to properly look up
7097 * the argument value during the handling of async exceptions.
7099 if (ins == cfg->args [0]) {
7100 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7101 mono_add_var_location (cfg, ins, FALSE, ins->inst_basereg, ins->inst_offset, code - cfg->native_code, 0);
7105 case ArgInFloatSSEReg:
7106 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7108 case ArgInDoubleSSEReg:
7109 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset, ainfo->reg);
7111 case ArgValuetypeInReg:
7112 for (quad = 0; quad < 2; quad ++) {
7113 switch (ainfo->pair_storage [quad]) {
7115 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad], sizeof(mgreg_t));
7117 case ArgInFloatSSEReg:
7118 amd64_movss_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7120 case ArgInDoubleSSEReg:
7121 amd64_movsd_membase_reg (code, ins->inst_basereg, ins->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_regs [quad]);
7126 g_assert_not_reached ();
7130 case ArgValuetypeAddrInIReg:
7131 if (ainfo->pair_storage [0] == ArgInIReg)
7132 amd64_mov_membase_reg (code, ins->inst_left->inst_basereg, ins->inst_left->inst_offset, ainfo->pair_regs [0], sizeof (gpointer));
7138 /* Argument allocated to (non-volatile) register */
7139 switch (ainfo->storage) {
7141 amd64_mov_reg_reg (code, ins->dreg, ainfo->reg, 8);
7144 amd64_mov_reg_membase (code, ins->dreg, AMD64_RBP, ARGS_OFFSET + ainfo->offset, 8);
7147 g_assert_not_reached ();
7150 if (ins == cfg->args [0]) {
7151 mono_add_var_location (cfg, ins, TRUE, ainfo->reg, 0, 0, code - cfg->native_code);
7152 mono_add_var_location (cfg, ins, TRUE, ins->dreg, 0, code - cfg->native_code, 0);
7157 if (cfg->method->save_lmf)
7158 args_clobbered = TRUE;
7161 args_clobbered = TRUE;
7162 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
7165 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
7166 args_clobbered = TRUE;
7169 * Optimize the common case of the first bblock making a call with the same
7170 * arguments as the method. This works because the arguments are still in their
7171 * original argument registers.
7172 * FIXME: Generalize this
7174 if (!args_clobbered) {
7175 MonoBasicBlock *first_bb = cfg->bb_entry;
7177 int filter = FILTER_IL_SEQ_POINT;
7179 next = mono_bb_first_inst (first_bb, filter);
7180 if (!next && first_bb->next_bb) {
7181 first_bb = first_bb->next_bb;
7182 next = mono_bb_first_inst (first_bb, filter);
7185 if (first_bb->in_count > 1)
7188 for (i = 0; next && i < sig->param_count + sig->hasthis; ++i) {
7189 ArgInfo *ainfo = cinfo->args + i;
7190 gboolean match = FALSE;
7192 ins = cfg->args [i];
7193 if (ins->opcode != OP_REGVAR) {
7194 switch (ainfo->storage) {
7196 if (((next->opcode == OP_LOAD_MEMBASE) || (next->opcode == OP_LOADI4_MEMBASE)) && next->inst_basereg == ins->inst_basereg && next->inst_offset == ins->inst_offset) {
7197 if (next->dreg == ainfo->reg) {
7201 next->opcode = OP_MOVE;
7202 next->sreg1 = ainfo->reg;
7203 /* Only continue if the instruction doesn't change argument regs */
7204 if (next->dreg == ainfo->reg || next->dreg == AMD64_RAX)
7214 /* Argument allocated to (non-volatile) register */
7215 switch (ainfo->storage) {
7217 if (next->opcode == OP_MOVE && next->sreg1 == ins->dreg && next->dreg == ainfo->reg) {
7228 next = mono_inst_next (next, filter);
7229 //next = mono_inst_list_next (&next->node, &first_bb->ins_list);
7236 if (cfg->gen_seq_points_debug_data) {
7237 MonoInst *info_var = cfg->arch.seq_point_info_var;
7239 /* Initialize seq_point_info_var */
7240 if (cfg->compile_aot) {
7241 /* Initialize the variable from a GOT slot */
7242 /* Same as OP_AOTCONST */
7243 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
7244 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, sizeof(gpointer));
7245 g_assert (info_var->opcode == OP_REGOFFSET);
7246 amd64_mov_membase_reg (code, info_var->inst_basereg, info_var->inst_offset, AMD64_R11, 8);
7249 /* Initialize ss_trigger_page_var */
7250 ins = cfg->arch.ss_trigger_page_var;
7252 g_assert (ins->opcode == OP_REGOFFSET);
7254 if (cfg->compile_aot) {
7255 amd64_mov_reg_membase (code, AMD64_R11, info_var->inst_basereg, info_var->inst_offset, 8);
7256 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, MONO_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page), 8);
7258 amd64_mov_reg_imm (code, AMD64_R11, (guint64)ss_trigger_page);
7260 amd64_mov_membase_reg (code, ins->inst_basereg, ins->inst_offset, AMD64_R11, 8);
7263 cfg->code_len = code - cfg->native_code;
7265 g_assert (cfg->code_len < cfg->code_size);
7271 mono_arch_emit_epilog (MonoCompile *cfg)
7273 MonoMethod *method = cfg->method;
7276 int max_epilog_size;
7278 gint32 lmf_offset = cfg->lmf_var ? ((MonoInst*)cfg->lmf_var)->inst_offset : -1;
7279 gint32 save_area_offset = cfg->arch.reg_save_area_offset;
7281 max_epilog_size = get_max_epilog_size (cfg);
7283 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
7284 cfg->code_size *= 2;
7285 cfg->native_code = mono_realloc_native_code (cfg);
7286 cfg->stat_code_reallocs++;
7288 code = cfg->native_code + cfg->code_len;
7290 cfg->has_unwind_info_for_epilog = TRUE;
7292 /* Mark the start of the epilog */
7293 mono_emit_unwind_op_mark_loc (cfg, code, 0);
7295 /* Save the uwind state which is needed by the out-of-line code */
7296 mono_emit_unwind_op_remember_state (cfg, code);
7298 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
7299 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
7301 /* the code restoring the registers must be kept in sync with OP_TAILCALL */
7304 if (method->save_lmf) {
7305 /* check if we need to restore protection of the stack after a stack overflow */
7306 if (!cfg->compile_aot && mono_get_jit_tls_offset () != -1) {
7308 code = mono_amd64_emit_tls_get (code, AMD64_RCX, mono_get_jit_tls_offset ());
7309 /* we load the value in a separate instruction: this mechanism may be
7310 * used later as a safer way to do thread interruption
7312 amd64_mov_reg_membase (code, AMD64_RCX, AMD64_RCX, MONO_STRUCT_OFFSET (MonoJitTlsData, restore_stack_prot), 8);
7313 x86_alu_reg_imm (code, X86_CMP, X86_ECX, 0);
7315 x86_branch8 (code, X86_CC_Z, 0, FALSE);
7316 /* note that the call trampoline will preserve eax/edx */
7317 x86_call_reg (code, X86_ECX);
7318 x86_patch (patch, code);
7320 /* FIXME: maybe save the jit tls in the prolog */
7322 if (cfg->used_int_regs & (1 << AMD64_RBP)) {
7323 amd64_mov_reg_membase (code, AMD64_RBP, cfg->frame_reg, lmf_offset + MONO_STRUCT_OFFSET (MonoLMF, rbp), 8);
7327 /* Restore callee saved regs */
7328 for (i = 0; i < AMD64_NREG; ++i) {
7329 if (AMD64_IS_CALLEE_SAVED_REG (i) && (cfg->arch.saved_iregs & (1 << i))) {
7330 /* Restore only used_int_regs, not arch.saved_iregs */
7331 if (cfg->used_int_regs & (1 << i)) {
7332 amd64_mov_reg_membase (code, i, cfg->frame_reg, save_area_offset, 8);
7333 mono_emit_unwind_op_same_value (cfg, code, i);
7334 async_exc_point (code);
7336 save_area_offset += 8;
7340 /* Load returned vtypes into registers if needed */
7341 cinfo = cfg->arch.cinfo;
7342 if (cinfo->ret.storage == ArgValuetypeInReg) {
7343 ArgInfo *ainfo = &cinfo->ret;
7344 MonoInst *inst = cfg->ret;
7346 for (quad = 0; quad < 2; quad ++) {
7347 switch (ainfo->pair_storage [quad]) {
7349 amd64_mov_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)), ainfo->pair_size [quad]);
7351 case ArgInFloatSSEReg:
7352 amd64_movss_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7354 case ArgInDoubleSSEReg:
7355 amd64_movsd_reg_membase (code, ainfo->pair_regs [quad], inst->inst_basereg, inst->inst_offset + (quad * sizeof(mgreg_t)));
7360 g_assert_not_reached ();
7365 if (cfg->arch.omit_fp) {
7366 if (cfg->arch.stack_alloc_size) {
7367 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, cfg->arch.stack_alloc_size);
7371 mono_emit_unwind_op_same_value (cfg, code, AMD64_RBP);
7373 mono_emit_unwind_op_def_cfa (cfg, code, AMD64_RSP, 8);
7374 async_exc_point (code);
7377 /* Restore the unwind state to be the same as before the epilog */
7378 mono_emit_unwind_op_restore_state (cfg, code);
7380 cfg->code_len = code - cfg->native_code;
7382 g_assert (cfg->code_len < cfg->code_size);
7386 mono_arch_emit_exceptions (MonoCompile *cfg)
7388 MonoJumpInfo *patch_info;
7391 MonoClass *exc_classes [16];
7392 guint8 *exc_throw_start [16], *exc_throw_end [16];
7393 guint32 code_size = 0;
7395 /* Compute needed space */
7396 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7397 if (patch_info->type == MONO_PATCH_INFO_EXC)
7399 if (patch_info->type == MONO_PATCH_INFO_R8)
7400 code_size += 8 + 15; /* sizeof (double) + alignment */
7401 if (patch_info->type == MONO_PATCH_INFO_R4)
7402 code_size += 4 + 15; /* sizeof (float) + alignment */
7403 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR)
7404 code_size += 8 + 7; /*sizeof (void*) + alignment */
7407 #ifdef __native_client_codegen__
7408 /* Give us extra room on Native Client. This could be */
7409 /* more carefully calculated, but bundle alignment makes */
7410 /* it much trickier, so *2 like other places is good. */
7414 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
7415 cfg->code_size *= 2;
7416 cfg->native_code = mono_realloc_native_code (cfg);
7417 cfg->stat_code_reallocs++;
7420 code = cfg->native_code + cfg->code_len;
7422 /* add code to raise exceptions */
7424 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7425 switch (patch_info->type) {
7426 case MONO_PATCH_INFO_EXC: {
7427 MonoClass *exc_class;
7431 amd64_patch (patch_info->ip.i + cfg->native_code, code);
7433 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
7434 g_assert (exc_class);
7435 throw_ip = patch_info->ip.i;
7437 //x86_breakpoint (code);
7438 /* Find a throw sequence for the same exception class */
7439 for (i = 0; i < nthrows; ++i)
7440 if (exc_classes [i] == exc_class)
7443 amd64_mov_reg_imm (code, AMD64_ARG_REG2, (exc_throw_end [i] - cfg->native_code) - throw_ip);
7444 x86_jump_code (code, exc_throw_start [i]);
7445 patch_info->type = MONO_PATCH_INFO_NONE;
7449 amd64_mov_reg_imm_size (code, AMD64_ARG_REG2, 0xf0f0f0f0, 4);
7453 exc_classes [nthrows] = exc_class;
7454 exc_throw_start [nthrows] = code;
7456 amd64_mov_reg_imm (code, AMD64_ARG_REG1, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
7458 patch_info->type = MONO_PATCH_INFO_NONE;
7460 code = emit_call_body (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arch_throw_corlib_exception");
7462 amd64_mov_reg_imm (buf, AMD64_ARG_REG2, (code - cfg->native_code) - throw_ip);
7467 exc_throw_end [nthrows] = code;
7477 g_assert(code < cfg->native_code + cfg->code_size);
7480 /* Handle relocations with RIP relative addressing */
7481 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7482 gboolean remove = FALSE;
7483 guint8 *orig_code = code;
7485 switch (patch_info->type) {
7486 case MONO_PATCH_INFO_R8:
7487 case MONO_PATCH_INFO_R4: {
7488 guint8 *pos, *patch_pos;
7491 /* The SSE opcodes require a 16 byte alignment */
7492 #if defined(__default_codegen__)
7493 code = (guint8*)ALIGN_TO (code, 16);
7494 #elif defined(__native_client_codegen__)
7496 /* Pad this out with HLT instructions */
7497 /* or we can get garbage bytes emitted */
7498 /* which will fail validation */
7499 guint8 *aligned_code;
7500 /* extra align to make room for */
7501 /* mov/push below */
7502 int extra_align = patch_info->type == MONO_PATCH_INFO_R8 ? 2 : 1;
7503 aligned_code = (guint8*)ALIGN_TO (code + extra_align, 16);
7504 /* The technique of hiding data in an */
7505 /* instruction has a problem here: we */
7506 /* need the data aligned to a 16-byte */
7507 /* boundary but the instruction cannot */
7508 /* cross the bundle boundary. so only */
7509 /* odd multiples of 16 can be used */
7510 if ((intptr_t)aligned_code % kNaClAlignment == 0) {
7513 while (code < aligned_code) {
7514 *(code++) = 0xf4; /* hlt */
7519 pos = cfg->native_code + patch_info->ip.i;
7520 if (IS_REX (pos [1])) {
7521 patch_pos = pos + 5;
7522 target_pos = code - pos - 9;
7525 patch_pos = pos + 4;
7526 target_pos = code - pos - 8;
7529 if (patch_info->type == MONO_PATCH_INFO_R8) {
7530 #ifdef __native_client_codegen__
7531 /* Hide 64-bit data in a */
7532 /* "mov imm64, r11" instruction. */
7533 /* write it before the start of */
7535 *(code-2) = 0x49; /* prefix */
7536 *(code-1) = 0xbb; /* mov X, %r11 */
7538 *(double*)code = *(double*)patch_info->data.target;
7539 code += sizeof (double);
7541 #ifdef __native_client_codegen__
7542 /* Hide 32-bit data in a */
7543 /* "push imm32" instruction. */
7544 *(code-1) = 0x68; /* push */
7546 *(float*)code = *(float*)patch_info->data.target;
7547 code += sizeof (float);
7550 *(guint32*)(patch_pos) = target_pos;
7555 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR: {
7558 if (cfg->compile_aot)
7561 /*loading is faster against aligned addresses.*/
7562 code = (guint8*)ALIGN_TO (code, 8);
7563 memset (orig_code, 0, code - orig_code);
7565 pos = cfg->native_code + patch_info->ip.i;
7567 /*alu_op [rex] modr/m imm32 - 7 or 8 bytes */
7568 if (IS_REX (pos [1]))
7569 *(guint32*)(pos + 4) = (guint8*)code - pos - 8;
7571 *(guint32*)(pos + 3) = (guint8*)code - pos - 7;
7573 *(gpointer*)code = (gpointer)patch_info->data.target;
7574 code += sizeof (gpointer);
7584 if (patch_info == cfg->patch_info)
7585 cfg->patch_info = patch_info->next;
7589 for (tmp = cfg->patch_info; tmp->next != patch_info; tmp = tmp->next)
7591 tmp->next = patch_info->next;
7594 g_assert (code < cfg->native_code + cfg->code_size);
7597 cfg->code_len = code - cfg->native_code;
7599 g_assert (cfg->code_len < cfg->code_size);
7603 #endif /* DISABLE_JIT */
7606 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
7609 CallInfo *cinfo = NULL;
7610 MonoMethodSignature *sig;
7612 int i, n, stack_area = 0;
7614 /* Keep this in sync with mono_arch_get_argument_info */
7616 if (enable_arguments) {
7617 /* Allocate a new area on the stack and save arguments there */
7618 sig = mono_method_signature (cfg->method);
7620 cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
7622 n = sig->param_count + sig->hasthis;
7624 stack_area = ALIGN_TO (n * 8, 16);
7626 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, stack_area);
7628 for (i = 0; i < n; ++i) {
7629 inst = cfg->args [i];
7631 if (inst->opcode == OP_REGVAR)
7632 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), inst->dreg, 8);
7634 amd64_mov_reg_membase (code, AMD64_R11, inst->inst_basereg, inst->inst_offset, 8);
7635 amd64_mov_membase_reg (code, AMD64_RSP, (i * 8), AMD64_R11, 8);
7640 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
7641 amd64_set_reg_template (code, AMD64_ARG_REG1);
7642 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RSP, 8);
7643 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7645 if (enable_arguments)
7646 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, stack_area);
7660 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
7663 int save_mode = SAVE_NONE;
7664 MonoMethod *method = cfg->method;
7665 MonoType *ret_type = mini_get_underlying_type (cfg, mono_method_signature (method)->ret);
7668 switch (ret_type->type) {
7669 case MONO_TYPE_VOID:
7670 /* special case string .ctor icall */
7671 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
7672 save_mode = SAVE_EAX;
7674 save_mode = SAVE_NONE;
7678 save_mode = SAVE_EAX;
7682 save_mode = SAVE_XMM;
7684 case MONO_TYPE_GENERICINST:
7685 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
7686 save_mode = SAVE_EAX;
7690 case MONO_TYPE_VALUETYPE:
7691 save_mode = SAVE_STRUCT;
7694 save_mode = SAVE_EAX;
7698 /* Save the result and copy it into the proper argument register */
7699 switch (save_mode) {
7701 amd64_push_reg (code, AMD64_RAX);
7703 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7704 if (enable_arguments)
7705 amd64_mov_reg_reg (code, AMD64_ARG_REG2, AMD64_RAX, 8);
7709 if (enable_arguments)
7710 amd64_mov_reg_imm (code, AMD64_ARG_REG2, 0);
7713 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7714 amd64_movsd_membase_reg (code, AMD64_RSP, 0, AMD64_XMM0);
7716 amd64_alu_reg_imm (code, X86_SUB, AMD64_RSP, 8);
7718 * The result is already in the proper argument register so no copying
7725 g_assert_not_reached ();
7728 /* Set %al since this is a varargs call */
7729 if (save_mode == SAVE_XMM)
7730 amd64_mov_reg_imm (code, AMD64_RAX, 1);
7732 amd64_mov_reg_imm (code, AMD64_RAX, 0);
7734 if (preserve_argument_registers) {
7735 for (i = 0; i < PARAM_REGS; ++i)
7736 amd64_push_reg (code, param_regs [i]);
7739 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
7740 amd64_set_reg_template (code, AMD64_ARG_REG1);
7741 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, (gpointer)func, TRUE);
7743 if (preserve_argument_registers) {
7744 for (i = PARAM_REGS - 1; i >= 0; --i)
7745 amd64_pop_reg (code, param_regs [i]);
7748 /* Restore result */
7749 switch (save_mode) {
7751 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7752 amd64_pop_reg (code, AMD64_RAX);
7758 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7759 amd64_movsd_reg_membase (code, AMD64_XMM0, AMD64_RSP, 0);
7760 amd64_alu_reg_imm (code, X86_ADD, AMD64_RSP, 8);
7765 g_assert_not_reached ();
7772 mono_arch_flush_icache (guint8 *code, gint size)
7778 mono_arch_flush_register_windows (void)
7783 mono_arch_is_inst_imm (gint64 imm)
7785 return amd64_is_imm32 (imm);
7789 * Determine whenever the trap whose info is in SIGINFO is caused by
7793 mono_arch_is_int_overflow (void *sigctx, void *info)
7800 mono_sigctx_to_monoctx (sigctx, &ctx);
7802 rip = (guint8*)ctx.rip;
7804 if (IS_REX (rip [0])) {
7805 reg = amd64_rex_b (rip [0]);
7811 if ((rip [0] == 0xf7) && (x86_modrm_mod (rip [1]) == 0x3) && (x86_modrm_reg (rip [1]) == 0x7)) {
7813 reg += x86_modrm_rm (rip [1]);
7853 g_assert_not_reached ();
7865 mono_arch_get_patch_offset (guint8 *code)
7871 * mono_breakpoint_clean_code:
7873 * Copy @size bytes from @code - @offset to the buffer @buf. If the debugger inserted software
7874 * breakpoints in the original code, they are removed in the copy.
7876 * Returns TRUE if no sw breakpoint was present.
7879 mono_breakpoint_clean_code (guint8 *method_start, guint8 *code, int offset, guint8 *buf, int size)
7882 * If method_start is non-NULL we need to perform bound checks, since we access memory
7883 * at code - offset we could go before the start of the method and end up in a different
7884 * page of memory that is not mapped or read incorrect data anyway. We zero-fill the bytes
7887 if (!method_start || code - offset >= method_start) {
7888 memcpy (buf, code - offset, size);
7890 int diff = code - method_start;
7891 memset (buf, 0, size);
7892 memcpy (buf + offset - diff, method_start, diff + size - offset);
7897 #if defined(__native_client_codegen__)
7898 /* For membase calls, we want the base register. for Native Client, */
7899 /* all indirect calls have the following sequence with the given sizes: */
7900 /* mov %eXX,%eXX [2-3] */
7901 /* mov disp(%r15,%rXX,scale),%r11d [4-8] */
7902 /* and $0xffffffffffffffe0,%r11d [4] */
7903 /* add %r15,%r11 [3] */
7904 /* callq *%r11 [3] */
7907 /* Determine if code points to a NaCl call-through-register sequence, */
7908 /* (i.e., the last 3 instructions listed above) */
7910 is_nacl_call_reg_sequence(guint8* code)
7912 const char *sequence = "\x41\x83\xe3\xe0" /* and */
7913 "\x4d\x03\xdf" /* add */
7914 "\x41\xff\xd3"; /* call */
7915 return memcmp(code, sequence, 10) == 0;
7918 /* Determine if code points to the first opcode of the mov membase component */
7919 /* of an indirect call sequence (i.e. the first 2 instructions listed above) */
7920 /* (there could be a REX prefix before the opcode but it is ignored) */
7922 is_nacl_indirect_call_membase_sequence(guint8* code)
7924 /* Check for mov opcode, reg-reg addressing mode (mod = 3), */
7925 return code[0] == 0x8b && amd64_modrm_mod(code[1]) == 3 &&
7926 /* and that src reg = dest reg */
7927 amd64_modrm_reg(code[1]) == amd64_modrm_rm(code[1]) &&
7928 /* Check that next inst is mov, uses SIB byte (rm = 4), */
7930 code[3] == 0x8b && amd64_modrm_rm(code[4]) == 4 &&
7931 /* and has dst of r11 and base of r15 */
7932 (amd64_modrm_reg(code[4]) + amd64_rex_r(code[2])) == AMD64_R11 &&
7933 (amd64_sib_base(code[5]) + amd64_rex_b(code[2])) == AMD64_R15;
7935 #endif /* __native_client_codegen__ */
7938 mono_arch_get_this_arg_reg (guint8 *code)
7940 return AMD64_ARG_REG1;
7944 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
7946 return (gpointer)regs [mono_arch_get_this_arg_reg (code)];
7949 #define MAX_ARCH_DELEGATE_PARAMS 10
7952 get_delegate_invoke_impl (gboolean has_target, guint32 param_count, guint32 *code_len)
7954 guint8 *code, *start;
7958 start = code = mono_global_codeman_reserve (64);
7960 /* Replace the this argument with the target */
7961 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7962 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
7963 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7965 g_assert ((code - start) < 64);
7967 start = code = mono_global_codeman_reserve (64);
7969 if (param_count == 0) {
7970 amd64_jump_membase (code, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7972 /* We have to shift the arguments left */
7973 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
7974 for (i = 0; i < param_count; ++i) {
7977 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7979 amd64_mov_reg_membase (code, param_regs [i], AMD64_RSP, 0x28, 8);
7981 amd64_mov_reg_reg (code, param_regs [i], param_regs [i + 1], 8);
7985 amd64_jump_membase (code, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
7987 g_assert ((code - start) < 64);
7990 nacl_global_codeman_validate (&start, 64, &code);
7993 *code_len = code - start;
7995 if (mono_jit_map_is_enabled ()) {
7998 buff = (char*)"delegate_invoke_has_target";
8000 buff = g_strdup_printf ("delegate_invoke_no_target_%d", param_count);
8001 mono_emit_jit_tramp (start, code - start, buff);
8005 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8011 * mono_arch_get_delegate_invoke_impls:
8013 * Return a list of MonoTrampInfo structures for the delegate invoke impl
8017 mono_arch_get_delegate_invoke_impls (void)
8025 code = get_delegate_invoke_impl (TRUE, 0, &code_len);
8026 res = g_slist_prepend (res, mono_tramp_info_create ("delegate_invoke_impl_has_target", code, code_len, NULL, NULL));
8028 for (i = 0; i < MAX_ARCH_DELEGATE_PARAMS; ++i) {
8029 code = get_delegate_invoke_impl (FALSE, i, &code_len);
8030 tramp_name = g_strdup_printf ("delegate_invoke_impl_target_%d", i);
8031 res = g_slist_prepend (res, mono_tramp_info_create (tramp_name, code, code_len, NULL, NULL));
8032 g_free (tramp_name);
8039 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
8041 guint8 *code, *start;
8044 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
8047 /* FIXME: Support more cases */
8048 if (MONO_TYPE_ISSTRUCT (mini_replace_type (sig->ret)))
8052 static guint8* cached = NULL;
8058 start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
8060 start = get_delegate_invoke_impl (TRUE, 0, NULL);
8062 mono_memory_barrier ();
8066 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
8067 for (i = 0; i < sig->param_count; ++i)
8068 if (!mono_is_regsize_var (sig->params [i]))
8070 if (sig->param_count > 4)
8073 code = cache [sig->param_count];
8077 if (mono_aot_only) {
8078 char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
8079 start = mono_aot_get_trampoline (name);
8082 start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
8085 mono_memory_barrier ();
8087 cache [sig->param_count] = start;
8094 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
8096 guint8 *code, *start;
8099 start = code = mono_global_codeman_reserve (size);
8101 /* Replace the this argument with the target */
8102 amd64_mov_reg_reg (code, AMD64_RAX, AMD64_ARG_REG1, 8);
8103 amd64_mov_reg_membase (code, AMD64_ARG_REG1, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, target), 8);
8106 /* Load the IMT reg */
8107 amd64_mov_reg_membase (code, MONO_ARCH_IMT_REG, AMD64_RAX, MONO_STRUCT_OFFSET (MonoDelegate, method), 8);
8110 /* Load the vtable */
8111 amd64_mov_reg_membase (code, AMD64_RAX, AMD64_ARG_REG1, MONO_STRUCT_OFFSET (MonoObject, vtable), 8);
8112 amd64_jump_membase (code, AMD64_RAX, offset);
8113 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
8119 mono_arch_finish_init (void)
8121 #if !defined(HOST_WIN32) && defined(MONO_XEN_OPT)
8122 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
8127 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
8131 #if defined(__default_codegen__)
8132 #define CMP_SIZE (6 + 1)
8133 #define CMP_REG_REG_SIZE (4 + 1)
8134 #define BR_SMALL_SIZE 2
8135 #define BR_LARGE_SIZE 6
8136 #define MOV_REG_IMM_SIZE 10
8137 #define MOV_REG_IMM_32BIT_SIZE 6
8138 #define JUMP_REG_SIZE (2 + 1)
8139 #elif defined(__native_client_codegen__)
8140 /* NaCl N-byte instructions can be padded up to N-1 bytes */
8141 #define CMP_SIZE ((6 + 1) * 2 - 1)
8142 #define CMP_REG_REG_SIZE ((4 + 1) * 2 - 1)
8143 #define BR_SMALL_SIZE (2 * 2 - 1)
8144 #define BR_LARGE_SIZE (6 * 2 - 1)
8145 #define MOV_REG_IMM_SIZE (10 * 2 - 1)
8146 #define MOV_REG_IMM_32BIT_SIZE (6 * 2 - 1)
8147 /* Jump reg for NaCl adds a mask (+4) and add (+3) */
8148 #define JUMP_REG_SIZE ((2 + 1 + 4 + 3) * 2 - 1)
8149 /* Jump membase's size is large and unpredictable */
8150 /* in native client, just pad it out a whole bundle. */
8151 #define JUMP_MEMBASE_SIZE (kNaClAlignment)
8155 imt_branch_distance (MonoIMTCheckItem **imt_entries, int start, int target)
8157 int i, distance = 0;
8158 for (i = start; i < target; ++i)
8159 distance += imt_entries [i]->chunk_size;
8164 * LOCKING: called with the domain lock held
8167 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
8168 gpointer fail_tramp)
8172 guint8 *code, *start;
8173 gboolean vtable_is_32bit = ((gsize)(vtable) == (gsize)(int)(gsize)(vtable));
8175 for (i = 0; i < count; ++i) {
8176 MonoIMTCheckItem *item = imt_entries [i];
8177 if (item->is_equals) {
8178 if (item->check_target_idx) {
8179 if (!item->compare_done) {
8180 if (amd64_is_imm32 (item->key))
8181 item->chunk_size += CMP_SIZE;
8183 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8185 if (item->has_target_code) {
8186 item->chunk_size += MOV_REG_IMM_SIZE;
8188 if (vtable_is_32bit)
8189 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8191 item->chunk_size += MOV_REG_IMM_SIZE;
8192 #ifdef __native_client_codegen__
8193 item->chunk_size += JUMP_MEMBASE_SIZE;
8196 item->chunk_size += BR_SMALL_SIZE + JUMP_REG_SIZE;
8199 item->chunk_size += MOV_REG_IMM_SIZE * 3 + CMP_REG_REG_SIZE +
8200 BR_SMALL_SIZE + JUMP_REG_SIZE * 2;
8202 if (vtable_is_32bit)
8203 item->chunk_size += MOV_REG_IMM_32BIT_SIZE;
8205 item->chunk_size += MOV_REG_IMM_SIZE;
8206 item->chunk_size += JUMP_REG_SIZE;
8207 /* with assert below:
8208 * item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
8210 #ifdef __native_client_codegen__
8211 item->chunk_size += JUMP_MEMBASE_SIZE;
8216 if (amd64_is_imm32 (item->key))
8217 item->chunk_size += CMP_SIZE;
8219 item->chunk_size += MOV_REG_IMM_SIZE + CMP_REG_REG_SIZE;
8220 item->chunk_size += BR_LARGE_SIZE;
8221 imt_entries [item->check_target_idx]->compare_done = TRUE;
8223 size += item->chunk_size;
8225 #if defined(__native_client__) && defined(__native_client_codegen__)
8226 /* In Native Client, we don't re-use thunks, allocate from the */
8227 /* normal code manager paths. */
8228 code = mono_domain_code_reserve (domain, size);
8231 code = mono_method_alloc_generic_virtual_thunk (domain, size);
8233 code = mono_domain_code_reserve (domain, size);
8236 for (i = 0; i < count; ++i) {
8237 MonoIMTCheckItem *item = imt_entries [i];
8238 item->code_target = code;
8239 if (item->is_equals) {
8240 gboolean fail_case = !item->check_target_idx && fail_tramp;
8242 if (item->check_target_idx || fail_case) {
8243 if (!item->compare_done || fail_case) {
8244 if (amd64_is_imm32 (item->key))
8245 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8247 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8248 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8251 item->jmp_code = code;
8252 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8253 if (item->has_target_code) {
8254 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->value.target_code);
8255 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8257 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8258 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8262 amd64_patch (item->jmp_code, code);
8263 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, fail_tramp);
8264 amd64_jump_reg (code, MONO_ARCH_IMT_SCRATCH_REG);
8265 item->jmp_code = NULL;
8268 /* enable the commented code to assert on wrong method */
8270 if (amd64_is_imm32 (item->key))
8271 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof(gpointer));
8273 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8274 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8276 item->jmp_code = code;
8277 amd64_branch8 (code, X86_CC_NE, 0, FALSE);
8278 /* See the comment below about R10 */
8279 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8280 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8281 amd64_patch (item->jmp_code, code);
8282 amd64_breakpoint (code);
8283 item->jmp_code = NULL;
8285 /* We're using R10 (MONO_ARCH_IMT_SCRATCH_REG) here because R11 (MONO_ARCH_IMT_REG)
8286 needs to be preserved. R10 needs
8287 to be preserved for calls which
8288 require a runtime generic context,
8289 but interface calls don't. */
8290 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, & (vtable->vtable [item->value.vtable_slot]));
8291 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
8295 if (amd64_is_imm32 (item->key))
8296 amd64_alu_reg_imm_size (code, X86_CMP, MONO_ARCH_IMT_REG, (guint32)(gssize)item->key, sizeof (gpointer));
8298 amd64_mov_reg_imm (code, MONO_ARCH_IMT_SCRATCH_REG, item->key);
8299 amd64_alu_reg_reg (code, X86_CMP, MONO_ARCH_IMT_REG, MONO_ARCH_IMT_SCRATCH_REG);
8301 item->jmp_code = code;
8302 if (x86_is_imm8 (imt_branch_distance (imt_entries, i, item->check_target_idx)))
8303 x86_branch8 (code, X86_CC_GE, 0, FALSE);
8305 x86_branch32 (code, X86_CC_GE, 0, FALSE);
8307 g_assert (code - item->code_target <= item->chunk_size);
8309 /* patch the branches to get to the target items */
8310 for (i = 0; i < count; ++i) {
8311 MonoIMTCheckItem *item = imt_entries [i];
8312 if (item->jmp_code) {
8313 if (item->check_target_idx) {
8314 amd64_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
8320 mono_stats.imt_thunks_size += code - start;
8321 g_assert (code - start <= size);
8323 nacl_domain_code_validate(domain, &start, size, &code);
8324 mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
8330 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
8332 return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
8336 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
8338 return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
8342 mono_arch_get_cie_program (void)
8346 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
8347 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
8355 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
8357 MonoInst *ins = NULL;
8360 if (cmethod->klass == mono_defaults.math_class) {
8361 if (strcmp (cmethod->name, "Sin") == 0) {
8363 } else if (strcmp (cmethod->name, "Cos") == 0) {
8365 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
8367 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
8371 if (opcode && fsig->param_count == 1) {
8372 MONO_INST_NEW (cfg, ins, opcode);
8373 ins->type = STACK_R8;
8374 ins->dreg = mono_alloc_freg (cfg);
8375 ins->sreg1 = args [0]->dreg;
8376 MONO_ADD_INS (cfg->cbb, ins);
8380 if (cfg->opt & MONO_OPT_CMOV) {
8381 if (strcmp (cmethod->name, "Min") == 0) {
8382 if (fsig->params [0]->type == MONO_TYPE_I4)
8384 if (fsig->params [0]->type == MONO_TYPE_U4)
8385 opcode = OP_IMIN_UN;
8386 else if (fsig->params [0]->type == MONO_TYPE_I8)
8388 else if (fsig->params [0]->type == MONO_TYPE_U8)
8389 opcode = OP_LMIN_UN;
8390 } else if (strcmp (cmethod->name, "Max") == 0) {
8391 if (fsig->params [0]->type == MONO_TYPE_I4)
8393 if (fsig->params [0]->type == MONO_TYPE_U4)
8394 opcode = OP_IMAX_UN;
8395 else if (fsig->params [0]->type == MONO_TYPE_I8)
8397 else if (fsig->params [0]->type == MONO_TYPE_U8)
8398 opcode = OP_LMAX_UN;
8402 if (opcode && fsig->param_count == 2) {
8403 MONO_INST_NEW (cfg, ins, opcode);
8404 ins->type = fsig->params [0]->type == MONO_TYPE_I4 ? STACK_I4 : STACK_I8;
8405 ins->dreg = mono_alloc_ireg (cfg);
8406 ins->sreg1 = args [0]->dreg;
8407 ins->sreg2 = args [1]->dreg;
8408 MONO_ADD_INS (cfg->cbb, ins);
8412 /* OP_FREM is not IEEE compatible */
8413 else if (strcmp (cmethod->name, "IEEERemainder") == 0 && fsig->param_count == 2) {
8414 MONO_INST_NEW (cfg, ins, OP_FREM);
8415 ins->inst_i0 = args [0];
8416 ins->inst_i1 = args [1];
8426 mono_arch_print_tree (MonoInst *tree, int arity)
8431 #define _CTX_REG(ctx,fld,i) ((&ctx->fld)[i])
8434 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
8437 case AMD64_RCX: return ctx->rcx;
8438 case AMD64_RDX: return ctx->rdx;
8439 case AMD64_RBX: return ctx->rbx;
8440 case AMD64_RBP: return ctx->rbp;
8441 case AMD64_RSP: return ctx->rsp;
8443 return _CTX_REG (ctx, rax, reg);
8448 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
8467 _CTX_REG (ctx, rax, reg) = val;
8472 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
8474 gpointer *sp, old_value;
8478 bp = MONO_CONTEXT_GET_BP (ctx);
8479 sp = *(gpointer*)(bp + clause->exvar_offset);
8482 if (old_value < ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
8491 * mono_arch_emit_load_aotconst:
8493 * Emit code to load the contents of the GOT slot identified by TRAMP_TYPE and
8494 * TARGET from the mscorlib GOT in full-aot code.
8495 * On AMD64, the result is placed into R11.
8498 mono_arch_emit_load_aotconst (guint8 *start, guint8 *code, MonoJumpInfo **ji, int tramp_type, gconstpointer target)
8500 *ji = mono_patch_info_list_prepend (*ji, code - start, tramp_type, target);
8501 amd64_mov_reg_membase (code, AMD64_R11, AMD64_RIP, 0, 8);
8507 * mono_arch_get_trampolines:
8509 * Return a list of MonoTrampInfo structures describing arch specific trampolines
8513 mono_arch_get_trampolines (gboolean aot)
8515 return mono_amd64_get_exception_trampolines (aot);
8518 /* Soft Debug support */
8519 #ifdef MONO_ARCH_SOFT_DEBUG_SUPPORTED
8522 * mono_arch_set_breakpoint:
8524 * Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
8525 * The location should contain code emitted by OP_SEQ_POINT.
8528 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
8531 guint8 *orig_code = code;
8534 guint32 native_offset = ip - (guint8*)ji->code_start;
8535 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8537 g_assert (info->bp_addrs [native_offset] == 0);
8538 info->bp_addrs [native_offset] = bp_trigger_page;
8541 * In production, we will use int3 (has to fix the size in the md
8542 * file). But that could confuse gdb, so during development, we emit a SIGSEGV
8545 g_assert (code [0] == 0x90);
8546 if (breakpoint_size == 8) {
8547 amd64_mov_reg_mem (code, AMD64_R11, (guint64)bp_trigger_page, 4);
8549 amd64_mov_reg_imm_size (code, AMD64_R11, (guint64)bp_trigger_page, 8);
8550 amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 4);
8553 g_assert (code - orig_code == breakpoint_size);
8558 * mono_arch_clear_breakpoint:
8560 * Clear the breakpoint at IP.
8563 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
8569 guint32 native_offset = ip - (guint8*)ji->code_start;
8570 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
8572 g_assert (info->bp_addrs [native_offset] == 0);
8573 info->bp_addrs [native_offset] = info;
8575 for (i = 0; i < breakpoint_size; ++i)
8581 mono_arch_is_breakpoint_event (void *info, void *sigctx)
8584 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8585 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == bp_trigger_page)
8590 siginfo_t* sinfo = (siginfo_t*) info;
8591 /* Sometimes the address is off by 4 */
8592 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
8600 * mono_arch_skip_breakpoint:
8602 * Modify CTX so the ip is placed after the breakpoint instruction, so when
8603 * we resume, the instruction is not executed again.
8606 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
8609 /* amd64_mov_reg_membase (code, AMD64_R11, AMD64_R11, 0, 8) */
8610 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 3);
8612 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + breakpoint_fault_size);
8617 * mono_arch_start_single_stepping:
8619 * Start single stepping.
8622 mono_arch_start_single_stepping (void)
8624 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
8628 * mono_arch_stop_single_stepping:
8630 * Stop single stepping.
8633 mono_arch_stop_single_stepping (void)
8635 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
8639 * mono_arch_is_single_step_event:
8641 * Return whenever the machine state in SIGCTX corresponds to a single
8645 mono_arch_is_single_step_event (void *info, void *sigctx)
8648 EXCEPTION_RECORD* einfo = ((EXCEPTION_POINTERS*)info)->ExceptionRecord;
8649 if (einfo->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && (gpointer)einfo->ExceptionInformation [1] == ss_trigger_page)
8654 siginfo_t* sinfo = (siginfo_t*) info;
8655 /* Sometimes the address is off by 4 */
8656 if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
8664 * mono_arch_skip_single_step:
8666 * Modify CTX so the ip is placed after the single step trigger instruction,
8667 * we resume, the instruction is not executed again.
8670 mono_arch_skip_single_step (MonoContext *ctx)
8672 MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + single_step_fault_size);
8676 * mono_arch_create_seq_point_info:
8678 * Return a pointer to a data structure which is used by the sequence
8679 * point implementation in AOTed code.
8682 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
8688 // FIXME: Add a free function
8690 mono_domain_lock (domain);
8691 info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points,
8693 mono_domain_unlock (domain);
8696 ji = mono_jit_info_table_find (domain, (char*)code);
8699 // FIXME: Optimize the size
8700 info = g_malloc0 (sizeof (SeqPointInfo) + (ji->code_size * sizeof (gpointer)));
8702 info->ss_trigger_page = ss_trigger_page;
8703 info->bp_trigger_page = bp_trigger_page;
8704 /* Initialize to a valid address */
8705 for (i = 0; i < ji->code_size; ++i)
8706 info->bp_addrs [i] = info;
8708 mono_domain_lock (domain);
8709 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
8711 mono_domain_unlock (domain);
8718 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
8720 ext->lmf.previous_lmf = prev_lmf;
8721 /* Mark that this is a MonoLMFExt */
8722 ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
8723 ext->lmf.rsp = (gssize)ext;
8729 mono_arch_opcode_supported (int opcode)
8732 case OP_ATOMIC_ADD_I4:
8733 case OP_ATOMIC_ADD_I8:
8734 case OP_ATOMIC_EXCHANGE_I4:
8735 case OP_ATOMIC_EXCHANGE_I8:
8736 case OP_ATOMIC_CAS_I4:
8737 case OP_ATOMIC_CAS_I8:
8738 case OP_ATOMIC_LOAD_I1:
8739 case OP_ATOMIC_LOAD_I2:
8740 case OP_ATOMIC_LOAD_I4:
8741 case OP_ATOMIC_LOAD_I8:
8742 case OP_ATOMIC_LOAD_U1:
8743 case OP_ATOMIC_LOAD_U2:
8744 case OP_ATOMIC_LOAD_U4:
8745 case OP_ATOMIC_LOAD_U8:
8746 case OP_ATOMIC_LOAD_R4:
8747 case OP_ATOMIC_LOAD_R8:
8748 case OP_ATOMIC_STORE_I1:
8749 case OP_ATOMIC_STORE_I2:
8750 case OP_ATOMIC_STORE_I4:
8751 case OP_ATOMIC_STORE_I8:
8752 case OP_ATOMIC_STORE_U1:
8753 case OP_ATOMIC_STORE_U2:
8754 case OP_ATOMIC_STORE_U4:
8755 case OP_ATOMIC_STORE_U8:
8756 case OP_ATOMIC_STORE_R4:
8757 case OP_ATOMIC_STORE_R8: