2 * mini-x86.c: x86 backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2003 Ximian, Inc.
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/profiler-private.h>
20 #include <mono/utils/mono-math.h>
27 /* On windows, these hold the key returned by TlsAlloc () */
28 static gint lmf_tls_offset
= -1;
29 static gint appdomain_tls_offset
= -1;
30 static gint thread_tls_offset
= -1;
33 /* TRUE by default until we add runtime detection of Xen */
34 static gboolean optimize_for_xen
= TRUE
;
36 #define optimize_for_xen 0
39 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
44 /* Under windows, the default pinvoke calling convention is stdcall */
45 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
47 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
50 #define NOT_IMPLEMENTED g_assert_not_reached ()
53 mono_arch_regname (int reg
) {
55 case X86_EAX
: return "%eax";
56 case X86_EBX
: return "%ebx";
57 case X86_ECX
: return "%ecx";
58 case X86_EDX
: return "%edx";
59 case X86_ESP
: return "%esp"; case X86_EBP
: return "%ebp";
60 case X86_EDI
: return "%edi";
61 case X86_ESI
: return "%esi";
67 mono_arch_fregname (int reg
) {
87 /* Only if storage == ArgValuetypeInReg */
88 ArgStorage pair_storage
[2];
97 gboolean need_stack_align
;
98 guint32 stack_align_amount
;
106 #define FLOAT_PARAM_REGS 0
108 static X86_Reg_No param_regs
[] = { 0 };
110 #ifdef PLATFORM_WIN32
111 static X86_Reg_No return_regs
[] = { X86_EAX
, X86_EDX
};
115 add_general (guint32
*gr
, guint32
*stack_size
, ArgInfo
*ainfo
)
117 ainfo
->offset
= *stack_size
;
119 if (*gr
>= PARAM_REGS
) {
120 ainfo
->storage
= ArgOnStack
;
121 (*stack_size
) += sizeof (gpointer
);
124 ainfo
->storage
= ArgInIReg
;
125 ainfo
->reg
= param_regs
[*gr
];
131 add_general_pair (guint32
*gr
, guint32
*stack_size
, ArgInfo
*ainfo
)
133 ainfo
->offset
= *stack_size
;
135 g_assert (PARAM_REGS
== 0);
137 ainfo
->storage
= ArgOnStack
;
138 (*stack_size
) += sizeof (gpointer
) * 2;
142 add_float (guint32
*gr
, guint32
*stack_size
, ArgInfo
*ainfo
, gboolean is_double
)
144 ainfo
->offset
= *stack_size
;
146 if (*gr
>= FLOAT_PARAM_REGS
) {
147 ainfo
->storage
= ArgOnStack
;
148 (*stack_size
) += is_double
? 8 : 4;
151 /* A double register */
153 ainfo
->storage
= ArgInDoubleSSEReg
;
155 ainfo
->storage
= ArgInFloatSSEReg
;
163 add_valuetype (MonoMethodSignature
*sig
, ArgInfo
*ainfo
, MonoType
*type
,
165 guint32
*gr
, guint32
*fr
, guint32
*stack_size
)
170 klass
= mono_class_from_mono_type (type
);
172 size
= mono_type_native_stack_size (&klass
->byval_arg
, NULL
);
174 size
= mono_type_stack_size (&klass
->byval_arg
, NULL
);
176 #ifdef PLATFORM_WIN32
177 if (sig
->pinvoke
&& is_return
) {
178 MonoMarshalType
*info
;
181 * the exact rules are not very well documented, the code below seems to work with the
182 * code generated by gcc 3.3.3 -mno-cygwin.
184 info
= mono_marshal_load_type_info (klass
);
187 ainfo
->pair_storage
[0] = ainfo
->pair_storage
[1] = ArgNone
;
189 /* Special case structs with only a float member */
190 if ((info
->native_size
== 8) && (info
->num_fields
== 1) && (info
->fields
[0].field
->type
->type
== MONO_TYPE_R8
)) {
191 ainfo
->storage
= ArgValuetypeInReg
;
192 ainfo
->pair_storage
[0] = ArgOnDoubleFpStack
;
195 if ((info
->native_size
== 4) && (info
->num_fields
== 1) && (info
->fields
[0].field
->type
->type
== MONO_TYPE_R4
)) {
196 ainfo
->storage
= ArgValuetypeInReg
;
197 ainfo
->pair_storage
[0] = ArgOnFloatFpStack
;
200 if ((info
->native_size
== 1) || (info
->native_size
== 2) || (info
->native_size
== 4) || (info
->native_size
== 8)) {
201 ainfo
->storage
= ArgValuetypeInReg
;
202 ainfo
->pair_storage
[0] = ArgInIReg
;
203 ainfo
->pair_regs
[0] = return_regs
[0];
204 if (info
->native_size
> 4) {
205 ainfo
->pair_storage
[1] = ArgInIReg
;
206 ainfo
->pair_regs
[1] = return_regs
[1];
213 ainfo
->offset
= *stack_size
;
214 ainfo
->storage
= ArgOnStack
;
215 *stack_size
+= ALIGN_TO (size
, sizeof (gpointer
));
221 * Obtain information about a call according to the calling convention.
222 * For x86 ELF, see the "System V Application Binary Interface Intel386
223 * Architecture Processor Supplment, Fourth Edition" document for more
225 * For x86 win32, see ???.
228 get_call_info (MonoMethodSignature
*sig
, gboolean is_pinvoke
)
232 int n
= sig
->hasthis
+ sig
->param_count
;
233 guint32 stack_size
= 0;
236 cinfo
= g_malloc0 (sizeof (CallInfo
) + (sizeof (ArgInfo
) * n
));
243 ret_type
= mono_type_get_underlying_type (sig
->ret
);
244 switch (ret_type
->type
) {
245 case MONO_TYPE_BOOLEAN
:
256 case MONO_TYPE_FNPTR
:
257 case MONO_TYPE_CLASS
:
258 case MONO_TYPE_OBJECT
:
259 case MONO_TYPE_SZARRAY
:
260 case MONO_TYPE_ARRAY
:
261 case MONO_TYPE_STRING
:
262 cinfo
->ret
.storage
= ArgInIReg
;
263 cinfo
->ret
.reg
= X86_EAX
;
267 cinfo
->ret
.storage
= ArgInIReg
;
268 cinfo
->ret
.reg
= X86_EAX
;
271 cinfo
->ret
.storage
= ArgOnFloatFpStack
;
274 cinfo
->ret
.storage
= ArgOnDoubleFpStack
;
276 case MONO_TYPE_GENERICINST
:
277 if (!mono_type_generic_inst_is_valuetype (sig
->ret
)) {
278 cinfo
->ret
.storage
= ArgInIReg
;
279 cinfo
->ret
.reg
= X86_EAX
;
283 case MONO_TYPE_VALUETYPE
: {
284 guint32 tmp_gr
= 0, tmp_fr
= 0, tmp_stacksize
= 0;
286 add_valuetype (sig
, &cinfo
->ret
, sig
->ret
, TRUE
, &tmp_gr
, &tmp_fr
, &tmp_stacksize
);
287 if (cinfo
->ret
.storage
== ArgOnStack
)
288 /* The caller passes the address where the value is stored */
289 add_general (&gr
, &stack_size
, &cinfo
->ret
);
292 case MONO_TYPE_TYPEDBYREF
:
293 /* Same as a valuetype with size 24 */
294 add_general (&gr
, &stack_size
, &cinfo
->ret
);
298 cinfo
->ret
.storage
= ArgNone
;
301 g_error ("Can't handle as return value 0x%x", sig
->ret
->type
);
307 add_general (&gr
, &stack_size
, cinfo
->args
+ 0);
309 if (!sig
->pinvoke
&& (sig
->call_convention
== MONO_CALL_VARARG
) && (n
== 0)) {
311 fr
= FLOAT_PARAM_REGS
;
313 /* Emit the signature cookie just before the implicit arguments */
314 add_general (&gr
, &stack_size
, &cinfo
->sig_cookie
);
317 for (i
= 0; i
< sig
->param_count
; ++i
) {
318 ArgInfo
*ainfo
= &cinfo
->args
[sig
->hasthis
+ i
];
321 if (!sig
->pinvoke
&& (sig
->call_convention
== MONO_CALL_VARARG
) && (i
== sig
->sentinelpos
)) {
322 /* We allways pass the sig cookie on the stack for simplicity */
324 * Prevent implicit arguments + the sig cookie from being passed
328 fr
= FLOAT_PARAM_REGS
;
330 /* Emit the signature cookie just before the implicit arguments */
331 add_general (&gr
, &stack_size
, &cinfo
->sig_cookie
);
334 if (sig
->params
[i
]->byref
) {
335 add_general (&gr
, &stack_size
, ainfo
);
338 ptype
= mono_type_get_underlying_type (sig
->params
[i
]);
339 switch (ptype
->type
) {
340 case MONO_TYPE_BOOLEAN
:
343 add_general (&gr
, &stack_size
, ainfo
);
348 add_general (&gr
, &stack_size
, ainfo
);
352 add_general (&gr
, &stack_size
, ainfo
);
357 case MONO_TYPE_FNPTR
:
358 case MONO_TYPE_CLASS
:
359 case MONO_TYPE_OBJECT
:
360 case MONO_TYPE_STRING
:
361 case MONO_TYPE_SZARRAY
:
362 case MONO_TYPE_ARRAY
:
363 add_general (&gr
, &stack_size
, ainfo
);
365 case MONO_TYPE_GENERICINST
:
366 if (!mono_type_generic_inst_is_valuetype (sig
->params
[i
])) {
367 add_general (&gr
, &stack_size
, ainfo
);
371 case MONO_TYPE_VALUETYPE
:
372 add_valuetype (sig
, ainfo
, sig
->params
[i
], FALSE
, &gr
, &fr
, &stack_size
);
374 case MONO_TYPE_TYPEDBYREF
:
375 stack_size
+= sizeof (MonoTypedRef
);
376 ainfo
->storage
= ArgOnStack
;
380 add_general_pair (&gr
, &stack_size
, ainfo
);
383 add_float (&fr
, &stack_size
, ainfo
, FALSE
);
386 add_float (&fr
, &stack_size
, ainfo
, TRUE
);
389 g_error ("unexpected type 0x%x", ptype
->type
);
390 g_assert_not_reached ();
394 if (!sig
->pinvoke
&& (sig
->call_convention
== MONO_CALL_VARARG
) && (n
> 0) && (sig
->sentinelpos
== sig
->param_count
)) {
396 fr
= FLOAT_PARAM_REGS
;
398 /* Emit the signature cookie just before the implicit arguments */
399 add_general (&gr
, &stack_size
, &cinfo
->sig_cookie
);
402 #if defined(__APPLE__)
403 if ((stack_size
% 16) != 0) {
404 cinfo
->need_stack_align
= TRUE
;
405 stack_size
+= cinfo
->stack_align_amount
= 16-(stack_size
% 16);
409 cinfo
->stack_usage
= stack_size
;
410 cinfo
->reg_usage
= gr
;
411 cinfo
->freg_usage
= fr
;
416 * mono_arch_get_argument_info:
417 * @csig: a method signature
418 * @param_count: the number of parameters to consider
419 * @arg_info: an array to store the result infos
421 * Gathers information on parameters such as size, alignment and
422 * padding. arg_info should be large enought to hold param_count + 1 entries.
424 * Returns the size of the activation frame.
427 mono_arch_get_argument_info (MonoMethodSignature
*csig
, int param_count
, MonoJitArgumentInfo
*arg_info
)
429 int k
, frame_size
= 0;
435 cinfo
= get_call_info (csig
, FALSE
);
437 if (MONO_TYPE_ISSTRUCT (csig
->ret
) && (cinfo
->ret
.storage
== ArgOnStack
)) {
438 frame_size
+= sizeof (gpointer
);
442 arg_info
[0].offset
= offset
;
445 frame_size
+= sizeof (gpointer
);
449 arg_info
[0].size
= frame_size
;
451 for (k
= 0; k
< param_count
; k
++) {
454 size
= mono_type_native_stack_size (csig
->params
[k
], &align
);
457 size
= mono_type_stack_size (csig
->params
[k
], &ialign
);
461 /* ignore alignment for now */
464 frame_size
+= pad
= (align
- (frame_size
& (align
- 1))) & (align
- 1);
465 arg_info
[k
].pad
= pad
;
467 arg_info
[k
+ 1].pad
= 0;
468 arg_info
[k
+ 1].size
= size
;
470 arg_info
[k
+ 1].offset
= offset
;
474 align
= MONO_ARCH_FRAME_ALIGNMENT
;
475 frame_size
+= pad
= (align
- (frame_size
& (align
- 1))) & (align
- 1);
476 arg_info
[k
].pad
= pad
;
483 static const guchar cpuid_impl
[] = {
484 0x55, /* push %ebp */
485 0x89, 0xe5, /* mov %esp,%ebp */
486 0x53, /* push %ebx */
487 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
488 0x0f, 0xa2, /* cpuid */
489 0x50, /* push %eax */
490 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
491 0x89, 0x18, /* mov %ebx,(%eax) */
492 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
493 0x89, 0x08, /* mov %ecx,(%eax) */
494 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
495 0x89, 0x10, /* mov %edx,(%eax) */
497 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
498 0x89, 0x02, /* mov %eax,(%edx) */
504 typedef void (*CpuidFunc
) (int id
, int* p_eax
, int* p_ebx
, int* p_ecx
, int* p_edx
);
507 cpuid (int id
, int* p_eax
, int* p_ebx
, int* p_ecx
, int* p_edx
)
511 __asm__
__volatile__ (
514 "movl %%eax, %%edx\n"
515 "xorl $0x200000, %%eax\n"
520 "xorl %%edx, %%eax\n"
521 "andl $0x200000, %%eax\n"
543 /* Have to use the code manager to get around WinXP DEP */
544 MonoCodeManager
*codeman
= mono_code_manager_new_dynamic ();
546 void *ptr
= mono_code_manager_reserve (codeman
, sizeof (cpuid_impl
));
547 memcpy (ptr
, cpuid_impl
, sizeof (cpuid_impl
));
549 func
= (CpuidFunc
)ptr
;
550 func (id
, p_eax
, p_ebx
, p_ecx
, p_edx
);
552 mono_code_manager_destroy (codeman
);
555 * We use this approach because of issues with gcc and pic code, see:
556 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
557 __asm__ __volatile__ ("cpuid"
558 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
567 * Initialize the cpu to execute managed code.
570 mono_arch_cpu_init (void)
572 /* spec compliance requires running with double precision */
576 __asm__
__volatile__ ("fnstcw %0\n": "=m" (fpcw
));
577 fpcw
&= ~X86_FPCW_PRECC_MASK
;
578 fpcw
|= X86_FPCW_PREC_DOUBLE
;
579 __asm__
__volatile__ ("fldcw %0\n": : "m" (fpcw
));
580 __asm__
__volatile__ ("fnstcw %0\n": "=m" (fpcw
));
582 _control87 (_PC_53
, MCW_PC
);
587 * This function returns the optimizations supported on this cpu.
590 mono_arch_cpu_optimizazions (guint32
*exclude_mask
)
592 int eax
, ebx
, ecx
, edx
;
596 /* Feature Flags function, flags returned in EDX. */
597 if (cpuid (1, &eax
, &ebx
, &ecx
, &edx
)) {
598 if (edx
& (1 << 15)) {
599 opts
|= MONO_OPT_CMOV
;
601 opts
|= MONO_OPT_FCMOV
;
603 *exclude_mask
|= MONO_OPT_FCMOV
;
605 *exclude_mask
|= MONO_OPT_CMOV
;
611 * Determine whenever the trap whose info is in SIGINFO is caused by
615 mono_arch_is_int_overflow (void *sigctx
, void *info
)
620 mono_arch_sigctx_to_monoctx (sigctx
, &ctx
);
622 ip
= (guint8
*)ctx
.eip
;
624 if ((ip
[0] == 0xf7) && (x86_modrm_mod (ip
[1]) == 0x3) && (x86_modrm_reg (ip
[1]) == 0x7)) {
628 switch (x86_modrm_rm (ip
[1])) {
648 g_assert_not_reached ();
660 is_regsize_var (MonoType
*t
) {
663 switch (mono_type_get_underlying_type (t
)->type
) {
669 case MONO_TYPE_FNPTR
:
671 case MONO_TYPE_OBJECT
:
672 case MONO_TYPE_STRING
:
673 case MONO_TYPE_CLASS
:
674 case MONO_TYPE_SZARRAY
:
675 case MONO_TYPE_ARRAY
:
677 case MONO_TYPE_GENERICINST
:
678 if (!mono_type_generic_inst_is_valuetype (t
))
681 case MONO_TYPE_VALUETYPE
:
688 mono_arch_get_allocatable_int_vars (MonoCompile
*cfg
)
693 for (i
= 0; i
< cfg
->num_varinfo
; i
++) {
694 MonoInst
*ins
= cfg
->varinfo
[i
];
695 MonoMethodVar
*vmv
= MONO_VARINFO (cfg
, i
);
698 if (vmv
->range
.first_use
.abs_pos
>= vmv
->range
.last_use
.abs_pos
)
701 if ((ins
->flags
& (MONO_INST_IS_DEAD
|MONO_INST_VOLATILE
|MONO_INST_INDIRECT
)) ||
702 (ins
->opcode
!= OP_LOCAL
&& ins
->opcode
!= OP_ARG
))
705 /* we dont allocate I1 to registers because there is no simply way to sign extend
706 * 8bit quantities in caller saved registers on x86 */
707 if (is_regsize_var (ins
->inst_vtype
) || (ins
->inst_vtype
->type
== MONO_TYPE_BOOLEAN
) ||
708 (ins
->inst_vtype
->type
== MONO_TYPE_U1
) || (ins
->inst_vtype
->type
== MONO_TYPE_U2
)||
709 (ins
->inst_vtype
->type
== MONO_TYPE_I2
) || (ins
->inst_vtype
->type
== MONO_TYPE_CHAR
)) {
710 g_assert (MONO_VARINFO (cfg
, i
)->reg
== -1);
711 g_assert (i
== vmv
->idx
);
712 vars
= g_list_prepend (vars
, vmv
);
716 vars
= mono_varlist_sort (cfg
, vars
, 0);
722 mono_arch_get_global_int_regs (MonoCompile
*cfg
)
726 /* we can use 3 registers for global allocation */
727 regs
= g_list_prepend (regs
, (gpointer
)X86_EBX
);
728 regs
= g_list_prepend (regs
, (gpointer
)X86_ESI
);
729 regs
= g_list_prepend (regs
, (gpointer
)X86_EDI
);
735 * mono_arch_regalloc_cost:
737 * Return the cost, in number of memory references, of the action of
738 * allocating the variable VMV into a register during global register
742 mono_arch_regalloc_cost (MonoCompile
*cfg
, MonoMethodVar
*vmv
)
744 MonoInst
*ins
= cfg
->varinfo
[vmv
->idx
];
746 if (cfg
->method
->save_lmf
)
747 /* The register is already saved */
748 return (ins
->opcode
== OP_ARG
) ? 1 : 0;
750 /* push+pop+possible load if it is an argument */
751 return (ins
->opcode
== OP_ARG
) ? 3 : 2;
755 * Set var information according to the calling convention. X86 version.
756 * The locals var stuff should most likely be split in another method.
759 mono_arch_allocate_vars (MonoCompile
*cfg
)
761 MonoMethodSignature
*sig
;
762 MonoMethodHeader
*header
;
764 guint32 locals_stack_size
, locals_stack_align
;
769 header
= mono_method_get_header (cfg
->method
);
770 sig
= mono_method_signature (cfg
->method
);
772 cinfo
= get_call_info (sig
, FALSE
);
774 cfg
->frame_reg
= MONO_ARCH_BASEREG
;
777 /* Reserve space to save LMF and caller saved registers */
779 if (cfg
->method
->save_lmf
) {
780 offset
+= sizeof (MonoLMF
);
782 if (cfg
->used_int_regs
& (1 << X86_EBX
)) {
786 if (cfg
->used_int_regs
& (1 << X86_EDI
)) {
790 if (cfg
->used_int_regs
& (1 << X86_ESI
)) {
795 switch (cinfo
->ret
.storage
) {
796 case ArgValuetypeInReg
:
797 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
799 cfg
->ret
->opcode
= OP_REGOFFSET
;
800 cfg
->ret
->inst_basereg
= X86_EBP
;
801 cfg
->ret
->inst_offset
= - offset
;
807 /* Allocate locals */
808 offsets
= mono_allocate_stack_slots (cfg
, &locals_stack_size
, &locals_stack_align
);
809 if (locals_stack_align
) {
810 offset
+= (locals_stack_align
- 1);
811 offset
&= ~(locals_stack_align
- 1);
813 for (i
= cfg
->locals_start
; i
< cfg
->num_varinfo
; i
++) {
814 if (offsets
[i
] != -1) {
815 MonoInst
*inst
= cfg
->varinfo
[i
];
816 inst
->opcode
= OP_REGOFFSET
;
817 inst
->inst_basereg
= X86_EBP
;
818 inst
->inst_offset
= - (offset
+ offsets
[i
]);
819 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
823 offset
+= locals_stack_size
;
827 * Allocate arguments+return value
830 switch (cinfo
->ret
.storage
) {
832 cfg
->ret
->opcode
= OP_REGOFFSET
;
833 cfg
->ret
->inst_basereg
= X86_EBP
;
834 cfg
->ret
->inst_offset
= cinfo
->ret
.offset
+ ARGS_OFFSET
;
836 case ArgValuetypeInReg
:
839 cfg
->ret
->opcode
= OP_REGVAR
;
840 cfg
->ret
->inst_c0
= cinfo
->ret
.reg
;
843 case ArgOnFloatFpStack
:
844 case ArgOnDoubleFpStack
:
847 g_assert_not_reached ();
850 if (sig
->call_convention
== MONO_CALL_VARARG
) {
851 g_assert (cinfo
->sig_cookie
.storage
== ArgOnStack
);
852 cfg
->sig_cookie
= cinfo
->sig_cookie
.offset
+ ARGS_OFFSET
;
855 for (i
= 0; i
< sig
->param_count
+ sig
->hasthis
; ++i
) {
856 ArgInfo
*ainfo
= &cinfo
->args
[i
];
857 inst
= cfg
->varinfo
[i
];
858 if (inst
->opcode
!= OP_REGVAR
) {
859 inst
->opcode
= OP_REGOFFSET
;
860 inst
->inst_basereg
= X86_EBP
;
862 inst
->inst_offset
= ainfo
->offset
+ ARGS_OFFSET
;
865 offset
+= (MONO_ARCH_FRAME_ALIGNMENT
- 1);
866 offset
&= ~(MONO_ARCH_FRAME_ALIGNMENT
- 1);
868 cfg
->stack_offset
= offset
;
874 mono_arch_create_vars (MonoCompile
*cfg
)
876 MonoMethodSignature
*sig
;
879 sig
= mono_method_signature (cfg
->method
);
881 cinfo
= get_call_info (sig
, FALSE
);
883 if (cinfo
->ret
.storage
== ArgValuetypeInReg
)
884 cfg
->ret_var_is_local
= TRUE
;
889 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
890 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
894 emit_sig_cookie (MonoCompile
*cfg
, MonoCallInst
*call
)
897 MonoMethodSignature
*tmp_sig
;
900 /* FIXME: Add support for signature tokens to AOT */
901 cfg
->disable_aot
= TRUE
;
902 MONO_INST_NEW (cfg
, arg
, OP_OUTARG
);
905 * mono_ArgIterator_Setup assumes the signature cookie is
906 * passed first and all the arguments which were before it are
907 * passed on the stack after the signature. So compensate by
908 * passing a different signature.
910 tmp_sig
= mono_metadata_signature_dup (call
->signature
);
911 tmp_sig
->param_count
-= call
->signature
->sentinelpos
;
912 tmp_sig
->sentinelpos
= 0;
913 memcpy (tmp_sig
->params
, call
->signature
->params
+ call
->signature
->sentinelpos
, tmp_sig
->param_count
* sizeof (MonoType
*));
915 MONO_INST_NEW (cfg
, sig_arg
, OP_ICONST
);
916 sig_arg
->inst_p0
= tmp_sig
;
918 arg
->inst_left
= sig_arg
;
919 arg
->type
= STACK_PTR
;
920 /* prepend, so they get reversed */
921 arg
->next
= call
->out_args
;
922 call
->out_args
= arg
;
926 * take the arguments and generate the arch-specific
927 * instructions to properly call the function in call.
928 * This includes pushing, moving arguments to the right register
932 mono_arch_call_opcode (MonoCompile
*cfg
, MonoBasicBlock
* bb
, MonoCallInst
*call
, int is_virtual
) {
934 MonoMethodSignature
*sig
;
939 sig
= call
->signature
;
940 n
= sig
->param_count
+ sig
->hasthis
;
942 cinfo
= get_call_info (sig
, FALSE
);
944 if (!sig
->pinvoke
&& (sig
->call_convention
== MONO_CALL_VARARG
))
945 sentinelpos
= sig
->sentinelpos
+ (is_virtual
? 1 : 0);
947 for (i
= 0; i
< n
; ++i
) {
948 ArgInfo
*ainfo
= cinfo
->args
+ i
;
950 /* Emit the signature cookie just before the implicit arguments */
951 if (!sig
->pinvoke
&& (sig
->call_convention
== MONO_CALL_VARARG
) && (i
== sentinelpos
)) {
952 emit_sig_cookie (cfg
, call
);
955 if (is_virtual
&& i
== 0) {
956 /* the argument will be attached to the call instrucion */
961 if (i
>= sig
->hasthis
)
962 t
= sig
->params
[i
- sig
->hasthis
];
964 t
= &mono_defaults
.int_class
->byval_arg
;
965 t
= mono_type_get_underlying_type (t
);
967 MONO_INST_NEW (cfg
, arg
, OP_OUTARG
);
969 arg
->cil_code
= in
->cil_code
;
971 arg
->type
= in
->type
;
972 /* prepend, so they get reversed */
973 arg
->next
= call
->out_args
;
974 call
->out_args
= arg
;
976 if ((i
>= sig
->hasthis
) && (MONO_TYPE_ISSTRUCT(t
))) {
979 if (t
->type
== MONO_TYPE_TYPEDBYREF
) {
980 size
= sizeof (MonoTypedRef
);
981 align
= sizeof (gpointer
);
985 size
= mono_type_native_stack_size (&in
->klass
->byval_arg
, &align
);
988 size
= mono_type_stack_size (&in
->klass
->byval_arg
, &ialign
);
991 arg
->opcode
= OP_OUTARG_VT
;
992 arg
->klass
= in
->klass
;
993 arg
->unused
= sig
->pinvoke
;
994 arg
->inst_imm
= size
;
997 switch (ainfo
->storage
) {
999 arg
->opcode
= OP_OUTARG
;
1001 if (t
->type
== MONO_TYPE_R4
)
1002 arg
->opcode
= OP_OUTARG_R4
;
1004 if (t
->type
== MONO_TYPE_R8
)
1005 arg
->opcode
= OP_OUTARG_R8
;
1009 g_assert_not_reached ();
1015 /* Handle the case where there are no implicit arguments */
1016 if (!sig
->pinvoke
&& (sig
->call_convention
== MONO_CALL_VARARG
) && (n
== sentinelpos
)) {
1017 emit_sig_cookie (cfg
, call
);
1020 if (sig
->ret
&& MONO_TYPE_ISSTRUCT (sig
->ret
)) {
1021 if (cinfo
->ret
.storage
== ArgValuetypeInReg
) {
1022 MonoInst
*zero_inst
;
1024 * After the call, the struct is in registers, but needs to be saved to the memory pointed
1025 * to by vt_arg in this_vret_args. This means that vt_arg needs to be saved somewhere
1026 * before calling the function. So we add a dummy instruction to represent pushing the
1027 * struct return address to the stack. The return address will be saved to this stack slot
1028 * by the code emitted in this_vret_args.
1030 MONO_INST_NEW (cfg
, arg
, OP_OUTARG
);
1031 MONO_INST_NEW (cfg
, zero_inst
, OP_ICONST
);
1032 zero_inst
->inst_p0
= 0;
1033 arg
->inst_left
= zero_inst
;
1034 arg
->type
= STACK_PTR
;
1035 /* prepend, so they get reversed */
1036 arg
->next
= call
->out_args
;
1037 call
->out_args
= arg
;
1040 /* if the function returns a struct, the called method already does a ret $0x4 */
1041 if (sig
->ret
&& MONO_TYPE_ISSTRUCT (sig
->ret
))
1042 cinfo
->stack_usage
-= 4;
1045 call
->stack_usage
= cinfo
->stack_usage
;
1047 #if defined(__APPLE__)
1048 if (cinfo
->need_stack_align
) {
1049 MONO_INST_NEW (cfg
, arg
, OP_X86_OUTARG_ALIGN_STACK
);
1050 arg
->inst_c0
= cinfo
->stack_align_amount
;
1051 arg
->next
= call
->out_args
;
1052 call
->out_args
= arg
;
1062 * Allow tracing to work with this interface (with an optional argument)
1065 mono_arch_instrument_prolog (MonoCompile
*cfg
, void *func
, void *p
, gboolean enable_arguments
)
1069 /* if some args are passed in registers, we need to save them here */
1070 x86_push_reg (code
, X86_EBP
);
1072 if (cfg
->compile_aot
) {
1073 x86_push_imm (code
, cfg
->method
);
1074 x86_mov_reg_imm (code
, X86_EAX
, func
);
1075 x86_call_reg (code
, X86_EAX
);
1077 mono_add_patch_info (cfg
, code
-cfg
->native_code
, MONO_PATCH_INFO_METHODCONST
, cfg
->method
);
1078 x86_push_imm (code
, cfg
->method
);
1079 mono_add_patch_info (cfg
, code
-cfg
->native_code
, MONO_PATCH_INFO_ABS
, func
);
1080 x86_call_code (code
, 0);
1082 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 8);
1096 mono_arch_instrument_epilog (MonoCompile
*cfg
, void *func
, void *p
, gboolean enable_arguments
)
1099 int arg_size
= 0, save_mode
= SAVE_NONE
;
1100 MonoMethod
*method
= cfg
->method
;
1102 switch (mono_type_get_underlying_type (mono_method_signature (method
)->ret
)->type
) {
1103 case MONO_TYPE_VOID
:
1104 /* special case string .ctor icall */
1105 if (strcmp (".ctor", method
->name
) && method
->klass
== mono_defaults
.string_class
)
1106 save_mode
= SAVE_EAX
;
1108 save_mode
= SAVE_NONE
;
1112 save_mode
= SAVE_EAX_EDX
;
1116 save_mode
= SAVE_FP
;
1118 case MONO_TYPE_GENERICINST
:
1119 if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method
)->ret
)) {
1120 save_mode
= SAVE_EAX
;
1124 case MONO_TYPE_VALUETYPE
:
1125 save_mode
= SAVE_STRUCT
;
1128 save_mode
= SAVE_EAX
;
1132 switch (save_mode
) {
1134 x86_push_reg (code
, X86_EDX
);
1135 x86_push_reg (code
, X86_EAX
);
1136 if (enable_arguments
) {
1137 x86_push_reg (code
, X86_EDX
);
1138 x86_push_reg (code
, X86_EAX
);
1143 x86_push_reg (code
, X86_EAX
);
1144 if (enable_arguments
) {
1145 x86_push_reg (code
, X86_EAX
);
1150 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 8);
1151 x86_fst_membase (code
, X86_ESP
, 0, TRUE
, TRUE
);
1152 if (enable_arguments
) {
1153 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 8);
1154 x86_fst_membase (code
, X86_ESP
, 0, TRUE
, TRUE
);
1159 if (enable_arguments
) {
1160 x86_push_membase (code
, X86_EBP
, 8);
1169 if (cfg
->compile_aot
) {
1170 x86_push_imm (code
, method
);
1171 x86_mov_reg_imm (code
, X86_EAX
, func
);
1172 x86_call_reg (code
, X86_EAX
);
1174 mono_add_patch_info (cfg
, code
-cfg
->native_code
, MONO_PATCH_INFO_METHODCONST
, method
);
1175 x86_push_imm (code
, method
);
1176 mono_add_patch_info (cfg
, code
-cfg
->native_code
, MONO_PATCH_INFO_ABS
, func
);
1177 x86_call_code (code
, 0);
1179 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, arg_size
+ 4);
1181 switch (save_mode
) {
1183 x86_pop_reg (code
, X86_EAX
);
1184 x86_pop_reg (code
, X86_EDX
);
1187 x86_pop_reg (code
, X86_EAX
);
1190 x86_fld_membase (code
, X86_ESP
, 0, TRUE
);
1191 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 8);
1201 #define EMIT_COND_BRANCH(ins,cond,sign) \
1202 if (ins->flags & MONO_INST_BRLABEL) { \
1203 if (ins->inst_i0->inst_c0) { \
1204 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
1206 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1207 if ((cfg->opt & MONO_OPT_BRANCH) && \
1208 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
1209 x86_branch8 (code, cond, 0, sign); \
1211 x86_branch32 (code, cond, 0, sign); \
1214 if (ins->inst_true_bb->native_offset) { \
1215 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1217 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1218 if ((cfg->opt & MONO_OPT_BRANCH) && \
1219 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1220 x86_branch8 (code, cond, 0, sign); \
1222 x86_branch32 (code, cond, 0, sign); \
1227 * Emit an exception if condition is fail and
1228 * if possible do a directly branch to target
1230 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1232 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1233 if (tins == NULL) { \
1234 mono_add_patch_info (cfg, code - cfg->native_code, \
1235 MONO_PATCH_INFO_EXC, exc_name); \
1236 x86_branch32 (code, cond, 0, signed); \
1238 EMIT_COND_BRANCH (tins, cond, signed); \
1242 #define EMIT_FPCOMPARE(code) do { \
1243 x86_fcompp (code); \
1244 x86_fnstsw (code); \
1249 emit_call (MonoCompile
*cfg
, guint8
*code
, guint32 patch_type
, gconstpointer data
)
1251 mono_add_patch_info (cfg
, code
- cfg
->native_code
, patch_type
, data
);
1252 x86_call_code (code
, 0);
1257 /* FIXME: Add more instructions */
1258 #define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_STORE_MEMBASE_IMM) || ((ins)->opcode == OP_STOREI4_MEMBASE_REG))
1261 peephole_pass (MonoCompile
*cfg
, MonoBasicBlock
*bb
)
1263 MonoInst
*ins
, *last_ins
= NULL
;
1268 switch (ins
->opcode
) {
1270 /* reg = 0 -> XOR (reg, reg) */
1271 /* XOR sets cflags on x86, so we cant do it always */
1272 if (ins
->inst_c0
== 0 && ins
->next
&& INST_IGNORES_CFLAGS (ins
->next
)) {
1273 ins
->opcode
= CEE_XOR
;
1274 ins
->sreg1
= ins
->dreg
;
1275 ins
->sreg2
= ins
->dreg
;
1279 /* remove unnecessary multiplication with 1 */
1280 if (ins
->inst_imm
== 1) {
1281 if (ins
->dreg
!= ins
->sreg1
) {
1282 ins
->opcode
= OP_MOVE
;
1284 last_ins
->next
= ins
->next
;
1290 case OP_COMPARE_IMM
:
1291 /* OP_COMPARE_IMM (reg, 0)
1293 * OP_X86_TEST_NULL (reg)
1296 ins
->opcode
= OP_X86_TEST_NULL
;
1298 case OP_X86_COMPARE_MEMBASE_IMM
:
1300 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1301 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1303 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1304 * OP_COMPARE_IMM reg, imm
1306 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1308 if (last_ins
&& (last_ins
->opcode
== OP_STOREI4_MEMBASE_REG
) &&
1309 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
1310 ins
->inst_offset
== last_ins
->inst_offset
) {
1311 ins
->opcode
= OP_COMPARE_IMM
;
1312 ins
->sreg1
= last_ins
->sreg1
;
1314 /* check if we can remove cmp reg,0 with test null */
1316 ins
->opcode
= OP_X86_TEST_NULL
;
1320 case OP_LOAD_MEMBASE
:
1321 case OP_LOADI4_MEMBASE
:
1323 * Note: if reg1 = reg2 the load op is removed
1325 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1326 * OP_LOAD_MEMBASE offset(basereg), reg2
1328 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1329 * OP_MOVE reg1, reg2
1331 if (last_ins
&& (last_ins
->opcode
== OP_STOREI4_MEMBASE_REG
1332 || last_ins
->opcode
== OP_STORE_MEMBASE_REG
) &&
1333 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
1334 ins
->inst_offset
== last_ins
->inst_offset
) {
1335 if (ins
->dreg
== last_ins
->sreg1
) {
1336 last_ins
->next
= ins
->next
;
1340 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1341 ins
->opcode
= OP_MOVE
;
1342 ins
->sreg1
= last_ins
->sreg1
;
1346 * Note: reg1 must be different from the basereg in the second load
1347 * Note: if reg1 = reg2 is equal then second load is removed
1349 * OP_LOAD_MEMBASE offset(basereg), reg1
1350 * OP_LOAD_MEMBASE offset(basereg), reg2
1352 * OP_LOAD_MEMBASE offset(basereg), reg1
1353 * OP_MOVE reg1, reg2
1355 } if (last_ins
&& (last_ins
->opcode
== OP_LOADI4_MEMBASE
1356 || last_ins
->opcode
== OP_LOAD_MEMBASE
) &&
1357 ins
->inst_basereg
!= last_ins
->dreg
&&
1358 ins
->inst_basereg
== last_ins
->inst_basereg
&&
1359 ins
->inst_offset
== last_ins
->inst_offset
) {
1361 if (ins
->dreg
== last_ins
->dreg
) {
1362 last_ins
->next
= ins
->next
;
1366 ins
->opcode
= OP_MOVE
;
1367 ins
->sreg1
= last_ins
->dreg
;
1370 //g_assert_not_reached ();
1374 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1375 * OP_LOAD_MEMBASE offset(basereg), reg
1377 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1378 * OP_ICONST reg, imm
1380 } else if (last_ins
&& (last_ins
->opcode
== OP_STOREI4_MEMBASE_IMM
1381 || last_ins
->opcode
== OP_STORE_MEMBASE_IMM
) &&
1382 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
1383 ins
->inst_offset
== last_ins
->inst_offset
) {
1384 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1385 ins
->opcode
= OP_ICONST
;
1386 ins
->inst_c0
= last_ins
->inst_imm
;
1387 g_assert_not_reached (); // check this rule
1391 case OP_LOADU1_MEMBASE
:
1392 case OP_LOADI1_MEMBASE
:
1394 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1395 * OP_LOAD_MEMBASE offset(basereg), reg2
1397 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1398 * CONV_I2/U2 reg1, reg2
1400 if (last_ins
&& X86_IS_BYTE_REG (last_ins
->sreg1
) &&
1401 (last_ins
->opcode
== OP_STOREI1_MEMBASE_REG
) &&
1402 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
1403 ins
->inst_offset
== last_ins
->inst_offset
) {
1404 ins
->opcode
= (ins
->opcode
== OP_LOADI1_MEMBASE
) ? CEE_CONV_I1
: CEE_CONV_U1
;
1405 ins
->sreg1
= last_ins
->sreg1
;
1408 case OP_LOADU2_MEMBASE
:
1409 case OP_LOADI2_MEMBASE
:
1411 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1412 * OP_LOAD_MEMBASE offset(basereg), reg2
1414 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1415 * CONV_I2/U2 reg1, reg2
1417 if (last_ins
&& (last_ins
->opcode
== OP_STOREI2_MEMBASE_REG
) &&
1418 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
1419 ins
->inst_offset
== last_ins
->inst_offset
) {
1420 ins
->opcode
= (ins
->opcode
== OP_LOADI2_MEMBASE
) ? CEE_CONV_I2
: CEE_CONV_U2
;
1421 ins
->sreg1
= last_ins
->sreg1
;
1432 if (ins
->dreg
== ins
->sreg1
) {
1434 last_ins
->next
= ins
->next
;
1441 * OP_MOVE sreg, dreg
1442 * OP_MOVE dreg, sreg
1444 if (last_ins
&& last_ins
->opcode
== OP_MOVE
&&
1445 ins
->sreg1
== last_ins
->dreg
&&
1446 ins
->dreg
== last_ins
->sreg1
) {
1447 last_ins
->next
= ins
->next
;
1453 case OP_X86_PUSH_MEMBASE
:
1454 if (last_ins
&& (last_ins
->opcode
== OP_STOREI4_MEMBASE_REG
||
1455 last_ins
->opcode
== OP_STORE_MEMBASE_REG
) &&
1456 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
1457 ins
->inst_offset
== last_ins
->inst_offset
) {
1458 ins
->opcode
= OP_X86_PUSH
;
1459 ins
->sreg1
= last_ins
->sreg1
;
1466 bb
->last_ins
= last_ins
;
1470 branch_cc_table
[] = {
1471 X86_CC_EQ
, X86_CC_GE
, X86_CC_GT
, X86_CC_LE
, X86_CC_LT
,
1472 X86_CC_NE
, X86_CC_GE
, X86_CC_GT
, X86_CC_LE
, X86_CC_LT
,
1473 X86_CC_O
, X86_CC_NO
, X86_CC_C
, X86_CC_NC
1476 static const char*const * ins_spec
= x86_desc
;
1478 /*#include "cprop.c"*/
1480 mono_arch_local_regalloc (MonoCompile
*cfg
, MonoBasicBlock
*bb
)
1482 mono_local_regalloc (cfg
, bb
);
1485 static unsigned char*
1486 emit_float_to_int (MonoCompile
*cfg
, guchar
*code
, int dreg
, int size
, gboolean is_signed
)
1488 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 4);
1489 x86_fnstcw_membase(code
, X86_ESP
, 0);
1490 x86_mov_reg_membase (code
, dreg
, X86_ESP
, 0, 2);
1491 x86_alu_reg_imm (code
, X86_OR
, dreg
, 0xc00);
1492 x86_mov_membase_reg (code
, X86_ESP
, 2, dreg
, 2);
1493 x86_fldcw_membase (code
, X86_ESP
, 2);
1495 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 8);
1496 x86_fist_pop_membase (code
, X86_ESP
, 0, TRUE
);
1497 x86_pop_reg (code
, dreg
);
1498 /* FIXME: need the high register
1499 * x86_pop_reg (code, dreg_high);
1502 x86_push_reg (code
, X86_EAX
); // SP = SP - 4
1503 x86_fist_pop_membase (code
, X86_ESP
, 0, FALSE
);
1504 x86_pop_reg (code
, dreg
);
1506 x86_fldcw_membase (code
, X86_ESP
, 0);
1507 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 4);
1510 x86_widen_reg (code
, dreg
, dreg
, is_signed
, FALSE
);
1512 x86_widen_reg (code
, dreg
, dreg
, is_signed
, TRUE
);
1516 static unsigned char*
1517 mono_emit_stack_alloc (guchar
*code
, MonoInst
* tree
)
1519 int sreg
= tree
->sreg1
;
1520 int need_touch
= FALSE
;
1522 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
1531 * If requested stack size is larger than one page,
1532 * perform stack-touch operation
1535 * Generate stack probe code.
1536 * Under Windows, it is necessary to allocate one page at a time,
1537 * "touching" stack after each successful sub-allocation. This is
1538 * because of the way stack growth is implemented - there is a
1539 * guard page before the lowest stack page that is currently commited.
1540 * Stack normally grows sequentially so OS traps access to the
1541 * guard page and commits more pages when needed.
1543 x86_test_reg_imm (code
, sreg
, ~0xFFF);
1544 br
[0] = code
; x86_branch8 (code
, X86_CC_Z
, 0, FALSE
);
1546 br
[2] = code
; /* loop */
1547 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 0x1000);
1548 x86_test_membase_reg (code
, X86_ESP
, 0, X86_ESP
);
1551 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
1552 * that follows only initializes the last part of the area.
1554 /* Same as the init code below with size==0x1000 */
1555 if (tree
->flags
& MONO_INST_INIT
) {
1556 x86_push_reg (code
, X86_EAX
);
1557 x86_push_reg (code
, X86_ECX
);
1558 x86_push_reg (code
, X86_EDI
);
1559 x86_mov_reg_imm (code
, X86_ECX
, (0x1000 >> 2));
1560 x86_alu_reg_reg (code
, X86_XOR
, X86_EAX
, X86_EAX
);
1561 x86_lea_membase (code
, X86_EDI
, X86_ESP
, 12);
1563 x86_prefix (code
, X86_REP_PREFIX
);
1565 x86_pop_reg (code
, X86_EDI
);
1566 x86_pop_reg (code
, X86_ECX
);
1567 x86_pop_reg (code
, X86_EAX
);
1570 x86_alu_reg_imm (code
, X86_SUB
, sreg
, 0x1000);
1571 x86_alu_reg_imm (code
, X86_CMP
, sreg
, 0x1000);
1572 br
[3] = code
; x86_branch8 (code
, X86_CC_AE
, 0, FALSE
);
1573 x86_patch (br
[3], br
[2]);
1574 x86_test_reg_reg (code
, sreg
, sreg
);
1575 br
[4] = code
; x86_branch8 (code
, X86_CC_Z
, 0, FALSE
);
1576 x86_alu_reg_reg (code
, X86_SUB
, X86_ESP
, sreg
);
1578 br
[1] = code
; x86_jump8 (code
, 0);
1580 x86_patch (br
[0], code
);
1581 x86_alu_reg_reg (code
, X86_SUB
, X86_ESP
, sreg
);
1582 x86_patch (br
[1], code
);
1583 x86_patch (br
[4], code
);
1586 x86_alu_reg_reg (code
, X86_SUB
, X86_ESP
, tree
->sreg1
);
1588 if (tree
->flags
& MONO_INST_INIT
) {
1590 if (tree
->dreg
!= X86_EAX
&& sreg
!= X86_EAX
) {
1591 x86_push_reg (code
, X86_EAX
);
1594 if (tree
->dreg
!= X86_ECX
&& sreg
!= X86_ECX
) {
1595 x86_push_reg (code
, X86_ECX
);
1598 if (tree
->dreg
!= X86_EDI
&& sreg
!= X86_EDI
) {
1599 x86_push_reg (code
, X86_EDI
);
1603 x86_shift_reg_imm (code
, X86_SHR
, sreg
, 2);
1604 if (sreg
!= X86_ECX
)
1605 x86_mov_reg_reg (code
, X86_ECX
, sreg
, 4);
1606 x86_alu_reg_reg (code
, X86_XOR
, X86_EAX
, X86_EAX
);
1608 x86_lea_membase (code
, X86_EDI
, X86_ESP
, offset
);
1610 x86_prefix (code
, X86_REP_PREFIX
);
1613 if (tree
->dreg
!= X86_EDI
&& sreg
!= X86_EDI
)
1614 x86_pop_reg (code
, X86_EDI
);
1615 if (tree
->dreg
!= X86_ECX
&& sreg
!= X86_ECX
)
1616 x86_pop_reg (code
, X86_ECX
);
1617 if (tree
->dreg
!= X86_EAX
&& sreg
!= X86_EAX
)
1618 x86_pop_reg (code
, X86_EAX
);
1625 emit_move_return_value (MonoCompile
*cfg
, MonoInst
*ins
, guint8
*code
)
1630 /* Move return value to the target register */
1631 switch (ins
->opcode
) {
1634 case OP_CALL_MEMBASE
:
1635 if (ins
->dreg
!= X86_EAX
)
1636 x86_mov_reg_reg (code
, ins
->dreg
, X86_EAX
, 4);
1640 case OP_VCALL_MEMBASE
:
1641 cinfo
= get_call_info (((MonoCallInst
*)ins
)->signature
, FALSE
);
1642 if (cinfo
->ret
.storage
== ArgValuetypeInReg
) {
1643 /* Pop the destination address from the stack */
1644 x86_pop_reg (code
, X86_ECX
);
1646 for (quad
= 0; quad
< 2; quad
++) {
1647 switch (cinfo
->ret
.pair_storage
[quad
]) {
1649 g_assert (cinfo
->ret
.pair_regs
[quad
] != X86_ECX
);
1650 x86_mov_membase_reg (code
, X86_ECX
, (quad
* sizeof (gpointer
)), cinfo
->ret
.pair_regs
[quad
], sizeof (gpointer
));
1655 g_assert_not_reached ();
1669 * @code: buffer to store code to
1670 * @dreg: hard register where to place the result
1671 * @tls_offset: offset info
1673 * emit_tls_get emits in @code the native code that puts in the dreg register
1674 * the item in the thread local storage identified by tls_offset.
1676 * Returns: a pointer to the end of the stored code
1679 emit_tls_get (guint8
* code
, int dreg
, int tls_offset
)
1681 #ifdef PLATFORM_WIN32
1683 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
1684 * Journal and/or a disassembly of the TlsGet () function.
1686 g_assert (tls_offset
< 64);
1687 x86_prefix (code
, X86_FS_PREFIX
);
1688 x86_mov_reg_mem (code
, dreg
, 0x18, 4);
1689 /* Dunno what this does but TlsGetValue () contains it */
1690 x86_alu_membase_imm (code
, X86_AND
, dreg
, 0x34, 0);
1691 x86_mov_reg_membase (code
, dreg
, dreg
, 3600 + (tls_offset
* 4), 4);
1693 if (optimize_for_xen
) {
1694 x86_prefix (code
, X86_GS_PREFIX
);
1695 x86_mov_reg_mem (code
, dreg
, 0, 4);
1696 x86_mov_reg_membase (code
, dreg
, dreg
, tls_offset
, 4);
1698 x86_prefix (code
, X86_GS_PREFIX
);
1699 x86_mov_reg_mem (code
, dreg
, tls_offset
, 4);
1705 #define REAL_PRINT_REG(text,reg) \
1706 mono_assert (reg >= 0); \
1707 x86_push_reg (code, X86_EAX); \
1708 x86_push_reg (code, X86_EDX); \
1709 x86_push_reg (code, X86_ECX); \
1710 x86_push_reg (code, reg); \
1711 x86_push_imm (code, reg); \
1712 x86_push_imm (code, text " %d %p\n"); \
1713 x86_mov_reg_imm (code, X86_EAX, printf); \
1714 x86_call_reg (code, X86_EAX); \
1715 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
1716 x86_pop_reg (code, X86_ECX); \
1717 x86_pop_reg (code, X86_EDX); \
1718 x86_pop_reg (code, X86_EAX);
1720 /* benchmark and set based on cpu */
1721 #define LOOP_ALIGNMENT 8
1722 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
1725 mono_arch_output_basic_block (MonoCompile
*cfg
, MonoBasicBlock
*bb
)
1730 guint8
*code
= cfg
->native_code
+ cfg
->code_len
;
1731 MonoInst
*last_ins
= NULL
;
1732 guint last_offset
= 0;
1735 if (cfg
->opt
& MONO_OPT_PEEPHOLE
)
1736 peephole_pass (cfg
, bb
);
1738 if (cfg
->opt
& MONO_OPT_LOOP
) {
1739 int pad
, align
= LOOP_ALIGNMENT
;
1740 /* set alignment depending on cpu */
1741 if (bb_is_loop_start (bb
) && (pad
= (cfg
->code_len
& (align
- 1)))) {
1743 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
1744 x86_padding (code
, pad
);
1745 cfg
->code_len
+= pad
;
1746 bb
->native_offset
= cfg
->code_len
;
1750 if (cfg
->verbose_level
> 2)
1751 g_print ("Basic block %d starting at offset 0x%x\n", bb
->block_num
, bb
->native_offset
);
1753 cpos
= bb
->max_offset
;
1755 if (cfg
->prof_options
& MONO_PROFILE_COVERAGE
) {
1756 MonoProfileCoverageInfo
*cov
= cfg
->coverage_info
;
1757 g_assert (!cfg
->compile_aot
);
1760 cov
->data
[bb
->dfn
].cil_code
= bb
->cil_code
;
1761 /* this is not thread save, but good enough */
1762 x86_inc_mem (code
, &cov
->data
[bb
->dfn
].count
);
1765 offset
= code
- cfg
->native_code
;
1767 mono_debug_open_block (cfg
, bb
, offset
);
1771 offset
= code
- cfg
->native_code
;
1773 max_len
= ((guint8
*)ins_spec
[ins
->opcode
])[MONO_INST_LEN
];
1775 if (offset
> (cfg
->code_size
- max_len
- 16)) {
1776 cfg
->code_size
*= 2;
1777 cfg
->native_code
= g_realloc (cfg
->native_code
, cfg
->code_size
);
1778 code
= cfg
->native_code
+ offset
;
1779 mono_jit_stats
.code_reallocs
++;
1782 mono_debug_record_line_number (cfg
, ins
, offset
);
1784 switch (ins
->opcode
) {
1786 x86_mul_reg (code
, ins
->sreg2
, TRUE
);
1789 x86_mul_reg (code
, ins
->sreg2
, FALSE
);
1791 case OP_X86_SETEQ_MEMBASE
:
1792 case OP_X86_SETNE_MEMBASE
:
1793 x86_set_membase (code
, ins
->opcode
== OP_X86_SETEQ_MEMBASE
? X86_CC_EQ
: X86_CC_NE
,
1794 ins
->inst_basereg
, ins
->inst_offset
, TRUE
);
1796 case OP_STOREI1_MEMBASE_IMM
:
1797 x86_mov_membase_imm (code
, ins
->inst_destbasereg
, ins
->inst_offset
, ins
->inst_imm
, 1);
1799 case OP_STOREI2_MEMBASE_IMM
:
1800 x86_mov_membase_imm (code
, ins
->inst_destbasereg
, ins
->inst_offset
, ins
->inst_imm
, 2);
1802 case OP_STORE_MEMBASE_IMM
:
1803 case OP_STOREI4_MEMBASE_IMM
:
1804 x86_mov_membase_imm (code
, ins
->inst_destbasereg
, ins
->inst_offset
, ins
->inst_imm
, 4);
1806 case OP_STOREI1_MEMBASE_REG
:
1807 x86_mov_membase_reg (code
, ins
->inst_destbasereg
, ins
->inst_offset
, ins
->sreg1
, 1);
1809 case OP_STOREI2_MEMBASE_REG
:
1810 x86_mov_membase_reg (code
, ins
->inst_destbasereg
, ins
->inst_offset
, ins
->sreg1
, 2);
1812 case OP_STORE_MEMBASE_REG
:
1813 case OP_STOREI4_MEMBASE_REG
:
1814 x86_mov_membase_reg (code
, ins
->inst_destbasereg
, ins
->inst_offset
, ins
->sreg1
, 4);
1819 x86_mov_reg_mem (code
, ins
->dreg
, ins
->inst_p0
, 4);
1822 x86_mov_reg_imm (code
, ins
->dreg
, ins
->inst_p0
);
1823 x86_mov_reg_membase (code
, ins
->dreg
, ins
->dreg
, 0, 4);
1825 case OP_LOAD_MEMBASE
:
1826 case OP_LOADI4_MEMBASE
:
1827 case OP_LOADU4_MEMBASE
:
1828 x86_mov_reg_membase (code
, ins
->dreg
, ins
->inst_basereg
, ins
->inst_offset
, 4);
1830 case OP_LOADU1_MEMBASE
:
1831 x86_widen_membase (code
, ins
->dreg
, ins
->inst_basereg
, ins
->inst_offset
, FALSE
, FALSE
);
1833 case OP_LOADI1_MEMBASE
:
1834 x86_widen_membase (code
, ins
->dreg
, ins
->inst_basereg
, ins
->inst_offset
, TRUE
, FALSE
);
1836 case OP_LOADU2_MEMBASE
:
1837 x86_widen_membase (code
, ins
->dreg
, ins
->inst_basereg
, ins
->inst_offset
, FALSE
, TRUE
);
1839 case OP_LOADI2_MEMBASE
:
1840 x86_widen_membase (code
, ins
->dreg
, ins
->inst_basereg
, ins
->inst_offset
, TRUE
, TRUE
);
1843 x86_widen_reg (code
, ins
->dreg
, ins
->sreg1
, TRUE
, FALSE
);
1846 x86_widen_reg (code
, ins
->dreg
, ins
->sreg1
, TRUE
, TRUE
);
1849 x86_widen_reg (code
, ins
->dreg
, ins
->sreg1
, FALSE
, FALSE
);
1852 x86_widen_reg (code
, ins
->dreg
, ins
->sreg1
, FALSE
, TRUE
);
1855 x86_alu_reg_reg (code
, X86_CMP
, ins
->sreg1
, ins
->sreg2
);
1857 case OP_COMPARE_IMM
:
1858 x86_alu_reg_imm (code
, X86_CMP
, ins
->sreg1
, ins
->inst_imm
);
1860 case OP_X86_COMPARE_MEMBASE_REG
:
1861 x86_alu_membase_reg (code
, X86_CMP
, ins
->inst_basereg
, ins
->inst_offset
, ins
->sreg2
);
1863 case OP_X86_COMPARE_MEMBASE_IMM
:
1864 x86_alu_membase_imm (code
, X86_CMP
, ins
->inst_basereg
, ins
->inst_offset
, ins
->inst_imm
);
1866 case OP_X86_COMPARE_MEMBASE8_IMM
:
1867 x86_alu_membase8_imm (code
, X86_CMP
, ins
->inst_basereg
, ins
->inst_offset
, ins
->inst_imm
);
1869 case OP_X86_COMPARE_REG_MEMBASE
:
1870 x86_alu_reg_membase (code
, X86_CMP
, ins
->sreg1
, ins
->sreg2
, ins
->inst_offset
);
1872 case OP_X86_COMPARE_MEM_IMM
:
1873 x86_alu_mem_imm (code
, X86_CMP
, ins
->inst_offset
, ins
->inst_imm
);
1875 case OP_X86_TEST_NULL
:
1876 x86_test_reg_reg (code
, ins
->sreg1
, ins
->sreg1
);
1878 case OP_X86_ADD_MEMBASE_IMM
:
1879 x86_alu_membase_imm (code
, X86_ADD
, ins
->inst_basereg
, ins
->inst_offset
, ins
->inst_imm
);
1881 case OP_X86_ADD_MEMBASE
:
1882 x86_alu_reg_membase (code
, X86_ADD
, ins
->sreg1
, ins
->sreg2
, ins
->inst_offset
);
1884 case OP_X86_SUB_MEMBASE_IMM
:
1885 x86_alu_membase_imm (code
, X86_SUB
, ins
->inst_basereg
, ins
->inst_offset
, ins
->inst_imm
);
1887 case OP_X86_SUB_MEMBASE
:
1888 x86_alu_reg_membase (code
, X86_SUB
, ins
->sreg1
, ins
->sreg2
, ins
->inst_offset
);
1890 case OP_X86_AND_MEMBASE_IMM
:
1891 x86_alu_membase_imm (code
, X86_AND
, ins
->inst_basereg
, ins
->inst_offset
, ins
->inst_imm
);
1893 case OP_X86_OR_MEMBASE_IMM
:
1894 x86_alu_membase_imm (code
, X86_OR
, ins
->inst_basereg
, ins
->inst_offset
, ins
->inst_imm
);
1896 case OP_X86_XOR_MEMBASE_IMM
:
1897 x86_alu_membase_imm (code
, X86_XOR
, ins
->inst_basereg
, ins
->inst_offset
, ins
->inst_imm
);
1899 case OP_X86_INC_MEMBASE
:
1900 x86_inc_membase (code
, ins
->inst_basereg
, ins
->inst_offset
);
1902 case OP_X86_INC_REG
:
1903 x86_inc_reg (code
, ins
->dreg
);
1905 case OP_X86_DEC_MEMBASE
:
1906 x86_dec_membase (code
, ins
->inst_basereg
, ins
->inst_offset
);
1908 case OP_X86_DEC_REG
:
1909 x86_dec_reg (code
, ins
->dreg
);
1911 case OP_X86_MUL_MEMBASE
:
1912 x86_imul_reg_membase (code
, ins
->sreg1
, ins
->sreg2
, ins
->inst_offset
);
1915 x86_breakpoint (code
);
1919 x86_alu_reg_reg (code
, X86_ADD
, ins
->sreg1
, ins
->sreg2
);
1922 x86_alu_reg_reg (code
, X86_ADC
, ins
->sreg1
, ins
->sreg2
);
1926 x86_alu_reg_imm (code
, X86_ADD
, ins
->dreg
, ins
->inst_imm
);
1929 x86_alu_reg_imm (code
, X86_ADC
, ins
->dreg
, ins
->inst_imm
);
1933 x86_alu_reg_reg (code
, X86_SUB
, ins
->sreg1
, ins
->sreg2
);
1936 x86_alu_reg_reg (code
, X86_SBB
, ins
->sreg1
, ins
->sreg2
);
1940 x86_alu_reg_imm (code
, X86_SUB
, ins
->dreg
, ins
->inst_imm
);
1943 x86_alu_reg_imm (code
, X86_SBB
, ins
->dreg
, ins
->inst_imm
);
1946 x86_alu_reg_reg (code
, X86_AND
, ins
->sreg1
, ins
->sreg2
);
1949 x86_alu_reg_imm (code
, X86_AND
, ins
->sreg1
, ins
->inst_imm
);
1953 x86_div_reg (code
, ins
->sreg2
, TRUE
);
1956 x86_alu_reg_reg (code
, X86_XOR
, X86_EDX
, X86_EDX
);
1957 x86_div_reg (code
, ins
->sreg2
, FALSE
);
1960 x86_mov_reg_imm (code
, ins
->sreg2
, ins
->inst_imm
);
1962 x86_div_reg (code
, ins
->sreg2
, TRUE
);
1966 x86_div_reg (code
, ins
->sreg2
, TRUE
);
1969 x86_alu_reg_reg (code
, X86_XOR
, X86_EDX
, X86_EDX
);
1970 x86_div_reg (code
, ins
->sreg2
, FALSE
);
1973 x86_mov_reg_imm (code
, ins
->sreg2
, ins
->inst_imm
);
1975 x86_div_reg (code
, ins
->sreg2
, TRUE
);
1978 x86_alu_reg_reg (code
, X86_OR
, ins
->sreg1
, ins
->sreg2
);
1981 x86_alu_reg_imm (code
, X86_OR
, ins
->sreg1
, ins
->inst_imm
);
1984 x86_alu_reg_reg (code
, X86_XOR
, ins
->sreg1
, ins
->sreg2
);
1987 x86_alu_reg_imm (code
, X86_XOR
, ins
->sreg1
, ins
->inst_imm
);
1990 g_assert (ins
->sreg2
== X86_ECX
);
1991 x86_shift_reg (code
, X86_SHL
, ins
->dreg
);
1994 g_assert (ins
->sreg2
== X86_ECX
);
1995 x86_shift_reg (code
, X86_SAR
, ins
->dreg
);
1998 x86_shift_reg_imm (code
, X86_SAR
, ins
->dreg
, ins
->inst_imm
);
2001 x86_shift_reg_imm (code
, X86_SHR
, ins
->dreg
, ins
->inst_imm
);
2004 g_assert (ins
->sreg2
== X86_ECX
);
2005 x86_shift_reg (code
, X86_SHR
, ins
->dreg
);
2008 x86_shift_reg_imm (code
, X86_SHL
, ins
->dreg
, ins
->inst_imm
);
2011 guint8
*jump_to_end
;
2013 /* handle shifts below 32 bits */
2014 x86_shld_reg (code
, ins
->unused
, ins
->sreg1
);
2015 x86_shift_reg (code
, X86_SHL
, ins
->sreg1
);
2017 x86_test_reg_imm (code
, X86_ECX
, 32);
2018 jump_to_end
= code
; x86_branch8 (code
, X86_CC_EQ
, 0, TRUE
);
2020 /* handle shift over 32 bit */
2021 x86_mov_reg_reg (code
, ins
->unused
, ins
->sreg1
, 4);
2022 x86_clear_reg (code
, ins
->sreg1
);
2024 x86_patch (jump_to_end
, code
);
2028 guint8
*jump_to_end
;
2030 /* handle shifts below 32 bits */
2031 x86_shrd_reg (code
, ins
->sreg1
, ins
->unused
);
2032 x86_shift_reg (code
, X86_SAR
, ins
->unused
);
2034 x86_test_reg_imm (code
, X86_ECX
, 32);
2035 jump_to_end
= code
; x86_branch8 (code
, X86_CC_EQ
, 0, FALSE
);
2037 /* handle shifts over 31 bits */
2038 x86_mov_reg_reg (code
, ins
->sreg1
, ins
->unused
, 4);
2039 x86_shift_reg_imm (code
, X86_SAR
, ins
->unused
, 31);
2041 x86_patch (jump_to_end
, code
);
2045 guint8
*jump_to_end
;
2047 /* handle shifts below 32 bits */
2048 x86_shrd_reg (code
, ins
->sreg1
, ins
->unused
);
2049 x86_shift_reg (code
, X86_SHR
, ins
->unused
);
2051 x86_test_reg_imm (code
, X86_ECX
, 32);
2052 jump_to_end
= code
; x86_branch8 (code
, X86_CC_EQ
, 0, FALSE
);
2054 /* handle shifts over 31 bits */
2055 x86_mov_reg_reg (code
, ins
->sreg1
, ins
->unused
, 4);
2056 x86_clear_reg (code
, ins
->unused
);
2058 x86_patch (jump_to_end
, code
);
2062 if (ins
->inst_imm
>= 32) {
2063 x86_mov_reg_reg (code
, ins
->unused
, ins
->sreg1
, 4);
2064 x86_clear_reg (code
, ins
->sreg1
);
2065 x86_shift_reg_imm (code
, X86_SHL
, ins
->unused
, ins
->inst_imm
- 32);
2067 x86_shld_reg_imm (code
, ins
->unused
, ins
->sreg1
, ins
->inst_imm
);
2068 x86_shift_reg_imm (code
, X86_SHL
, ins
->sreg1
, ins
->inst_imm
);
2072 if (ins
->inst_imm
>= 32) {
2073 x86_mov_reg_reg (code
, ins
->sreg1
, ins
->unused
, 4);
2074 x86_shift_reg_imm (code
, X86_SAR
, ins
->unused
, 0x1f);
2075 x86_shift_reg_imm (code
, X86_SAR
, ins
->sreg1
, ins
->inst_imm
- 32);
2077 x86_shrd_reg_imm (code
, ins
->sreg1
, ins
->unused
, ins
->inst_imm
);
2078 x86_shift_reg_imm (code
, X86_SAR
, ins
->unused
, ins
->inst_imm
);
2081 case OP_LSHR_UN_IMM
:
2082 if (ins
->inst_imm
>= 32) {
2083 x86_mov_reg_reg (code
, ins
->sreg1
, ins
->unused
, 4);
2084 x86_clear_reg (code
, ins
->unused
);
2085 x86_shift_reg_imm (code
, X86_SHR
, ins
->sreg1
, ins
->inst_imm
- 32);
2087 x86_shrd_reg_imm (code
, ins
->sreg1
, ins
->unused
, ins
->inst_imm
);
2088 x86_shift_reg_imm (code
, X86_SHR
, ins
->unused
, ins
->inst_imm
);
2092 x86_not_reg (code
, ins
->sreg1
);
2095 x86_neg_reg (code
, ins
->sreg1
);
2098 x86_widen_reg (code
, ins
->dreg
, ins
->sreg1
, TRUE
, FALSE
);
2101 x86_widen_reg (code
, ins
->dreg
, ins
->sreg1
, TRUE
, TRUE
);
2104 x86_imul_reg_reg (code
, ins
->sreg1
, ins
->sreg2
);
2107 switch (ins
->inst_imm
) {
2111 if (ins
->dreg
!= ins
->sreg1
)
2112 x86_mov_reg_reg (code
, ins
->dreg
, ins
->sreg1
, 4);
2113 x86_alu_reg_reg (code
, X86_ADD
, ins
->dreg
, ins
->dreg
);
2116 /* LEA r1, [r2 + r2*2] */
2117 x86_lea_memindex (code
, ins
->dreg
, ins
->sreg1
, 0, ins
->sreg1
, 1);
2120 /* LEA r1, [r2 + r2*4] */
2121 x86_lea_memindex (code
, ins
->dreg
, ins
->sreg1
, 0, ins
->sreg1
, 2);
2124 /* LEA r1, [r2 + r2*2] */
2126 x86_lea_memindex (code
, ins
->dreg
, ins
->sreg1
, 0, ins
->sreg1
, 1);
2127 x86_alu_reg_reg (code
, X86_ADD
, ins
->dreg
, ins
->dreg
);
2130 /* LEA r1, [r2 + r2*8] */
2131 x86_lea_memindex (code
, ins
->dreg
, ins
->sreg1
, 0, ins
->sreg1
, 3);
2134 /* LEA r1, [r2 + r2*4] */
2136 x86_lea_memindex (code
, ins
->dreg
, ins
->sreg1
, 0, ins
->sreg1
, 2);
2137 x86_alu_reg_reg (code
, X86_ADD
, ins
->dreg
, ins
->dreg
);
2140 /* LEA r1, [r2 + r2*2] */
2142 x86_lea_memindex (code
, ins
->dreg
, ins
->sreg1
, 0, ins
->sreg1
, 1);
2143 x86_shift_reg_imm (code
, X86_SHL
, ins
->dreg
, 2);
2146 /* LEA r1, [r2 + r2*4] */
2147 /* LEA r1, [r1 + r1*4] */
2148 x86_lea_memindex (code
, ins
->dreg
, ins
->sreg1
, 0, ins
->sreg1
, 2);
2149 x86_lea_memindex (code
, ins
->dreg
, ins
->dreg
, 0, ins
->dreg
, 2);
2152 /* LEA r1, [r2 + r2*4] */
2154 /* LEA r1, [r1 + r1*4] */
2155 x86_lea_memindex (code
, ins
->dreg
, ins
->sreg1
, 0, ins
->sreg1
, 2);
2156 x86_shift_reg_imm (code
, X86_SHL
, ins
->dreg
, 2);
2157 x86_lea_memindex (code
, ins
->dreg
, ins
->dreg
, 0, ins
->dreg
, 2);
2160 x86_imul_reg_reg_imm (code
, ins
->dreg
, ins
->sreg1
, ins
->inst_imm
);
2165 x86_imul_reg_reg (code
, ins
->sreg1
, ins
->sreg2
);
2166 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O
, FALSE
, "OverflowException");
2168 case CEE_MUL_OVF_UN
: {
2169 /* the mul operation and the exception check should most likely be split */
2170 int non_eax_reg
, saved_eax
= FALSE
, saved_edx
= FALSE
;
2171 /*g_assert (ins->sreg2 == X86_EAX);
2172 g_assert (ins->dreg == X86_EAX);*/
2173 if (ins
->sreg2
== X86_EAX
) {
2174 non_eax_reg
= ins
->sreg1
;
2175 } else if (ins
->sreg1
== X86_EAX
) {
2176 non_eax_reg
= ins
->sreg2
;
2178 /* no need to save since we're going to store to it anyway */
2179 if (ins
->dreg
!= X86_EAX
) {
2181 x86_push_reg (code
, X86_EAX
);
2183 x86_mov_reg_reg (code
, X86_EAX
, ins
->sreg1
, 4);
2184 non_eax_reg
= ins
->sreg2
;
2186 if (ins
->dreg
== X86_EDX
) {
2189 x86_push_reg (code
, X86_EAX
);
2191 } else if (ins
->dreg
!= X86_EAX
) {
2193 x86_push_reg (code
, X86_EDX
);
2195 x86_mul_reg (code
, non_eax_reg
, FALSE
);
2196 /* save before the check since pop and mov don't change the flags */
2197 if (ins
->dreg
!= X86_EAX
)
2198 x86_mov_reg_reg (code
, ins
->dreg
, X86_EAX
, 4);
2200 x86_pop_reg (code
, X86_EDX
);
2202 x86_pop_reg (code
, X86_EAX
);
2203 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O
, FALSE
, "OverflowException");
2207 x86_mov_reg_imm (code
, ins
->dreg
, ins
->inst_c0
);
2210 g_assert_not_reached ();
2211 mono_add_patch_info (cfg
, offset
, (MonoJumpInfoType
)ins
->inst_i1
, ins
->inst_p0
);
2212 x86_mov_reg_imm (code
, ins
->dreg
, 0);
2214 case OP_LOAD_GOTADDR
:
2215 x86_call_imm (code
, 0);
2217 * The patch needs to point to the pop, since the GOT offset needs
2218 * to be added to that address.
2220 mono_add_patch_info (cfg
, code
- cfg
->native_code
, MONO_PATCH_INFO_GOT_OFFSET
, NULL
);
2221 x86_pop_reg (code
, ins
->dreg
);
2222 x86_alu_reg_imm (code
, X86_ADD
, ins
->dreg
, 0xf0f0f0f0);
2225 mono_add_patch_info (cfg
, offset
, (MonoJumpInfoType
)ins
->inst_right
->inst_i1
, ins
->inst_right
->inst_p0
);
2226 x86_mov_reg_membase (code
, ins
->dreg
, ins
->inst_basereg
, 0xf0f0f0f0, 4);
2228 case OP_X86_PUSH_GOT_ENTRY
:
2229 mono_add_patch_info (cfg
, offset
, (MonoJumpInfoType
)ins
->inst_right
->inst_i1
, ins
->inst_right
->inst_p0
);
2230 x86_push_membase (code
, ins
->inst_basereg
, 0xf0f0f0f0);
2234 x86_mov_reg_reg (code
, ins
->dreg
, ins
->sreg1
, 4);
2237 g_assert_not_reached ();
2240 * Note: this 'frame destruction' logic is useful for tail calls, too.
2241 * Keep in sync with the code in emit_epilog.
2245 /* FIXME: no tracing support... */
2246 if (cfg
->prof_options
& MONO_PROFILE_ENTER_LEAVE
)
2247 code
= mono_arch_instrument_epilog (cfg
, mono_profiler_method_leave
, code
, FALSE
);
2248 /* reset offset to make max_len work */
2249 offset
= code
- cfg
->native_code
;
2251 g_assert (!cfg
->method
->save_lmf
);
2253 if (cfg
->used_int_regs
& (1 << X86_EBX
))
2255 if (cfg
->used_int_regs
& (1 << X86_EDI
))
2257 if (cfg
->used_int_regs
& (1 << X86_ESI
))
2260 x86_lea_membase (code
, X86_ESP
, X86_EBP
, pos
);
2262 if (cfg
->used_int_regs
& (1 << X86_ESI
))
2263 x86_pop_reg (code
, X86_ESI
);
2264 if (cfg
->used_int_regs
& (1 << X86_EDI
))
2265 x86_pop_reg (code
, X86_EDI
);
2266 if (cfg
->used_int_regs
& (1 << X86_EBX
))
2267 x86_pop_reg (code
, X86_EBX
);
2269 /* restore ESP/EBP */
2271 offset
= code
- cfg
->native_code
;
2272 mono_add_patch_info (cfg
, offset
, MONO_PATCH_INFO_METHOD_JUMP
, ins
->inst_p0
);
2273 x86_jump32 (code
, 0);
2277 /* ensure ins->sreg1 is not NULL
2278 * note that cmp DWORD PTR [eax], eax is one byte shorter than
2279 * cmp DWORD PTR [eax], 0
2281 x86_alu_membase_reg (code
, X86_CMP
, ins
->sreg1
, 0, ins
->sreg1
);
2284 int hreg
= ins
->sreg1
== X86_EAX
? X86_ECX
: X86_EAX
;
2285 x86_push_reg (code
, hreg
);
2286 x86_lea_membase (code
, hreg
, X86_EBP
, cfg
->sig_cookie
);
2287 x86_mov_membase_reg (code
, ins
->sreg1
, 0, hreg
, 4);
2288 x86_pop_reg (code
, hreg
);
2296 call
= (MonoCallInst
*)ins
;
2297 if (ins
->flags
& MONO_INST_HAS_METHOD
)
2298 code
= emit_call (cfg
, code
, MONO_PATCH_INFO_METHOD
, call
->method
);
2300 code
= emit_call (cfg
, code
, MONO_PATCH_INFO_ABS
, call
->fptr
);
2301 if (call
->stack_usage
&& !CALLCONV_IS_STDCALL (call
->signature
)) {
2302 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
2303 * bytes to pop, we want to use pops. GCC does this (note it won't happen
2304 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
2305 * smart enough to do that optimization yet
2307 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
2308 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
2309 * (most likely from locality benefits). People with other processors should
2310 * check on theirs to see what happens.
2312 if (call
->stack_usage
== 4) {
2313 /* we want to use registers that won't get used soon, so use
2314 * ecx, as eax will get allocated first. edx is used by long calls,
2315 * so we can't use that.
2318 x86_pop_reg (code
, X86_ECX
);
2320 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, call
->stack_usage
);
2323 code
= emit_move_return_value (cfg
, ins
, code
);
2328 case OP_VOIDCALL_REG
:
2330 call
= (MonoCallInst
*)ins
;
2331 x86_call_reg (code
, ins
->sreg1
);
2332 if (call
->stack_usage
&& !CALLCONV_IS_STDCALL (call
->signature
)) {
2333 if (call
->stack_usage
== 4)
2334 x86_pop_reg (code
, X86_ECX
);
2336 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, call
->stack_usage
);
2338 code
= emit_move_return_value (cfg
, ins
, code
);
2340 case OP_FCALL_MEMBASE
:
2341 case OP_LCALL_MEMBASE
:
2342 case OP_VCALL_MEMBASE
:
2343 case OP_VOIDCALL_MEMBASE
:
2344 case OP_CALL_MEMBASE
:
2345 call
= (MonoCallInst
*)ins
;
2346 x86_call_membase (code
, ins
->sreg1
, ins
->inst_offset
);
2347 if (call
->stack_usage
&& !CALLCONV_IS_STDCALL (call
->signature
)) {
2348 if (call
->stack_usage
== 4)
2349 x86_pop_reg (code
, X86_ECX
);
2351 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, call
->stack_usage
);
2353 code
= emit_move_return_value (cfg
, ins
, code
);
2357 x86_push_reg (code
, ins
->sreg1
);
2359 case OP_X86_PUSH_IMM
:
2360 x86_push_imm (code
, ins
->inst_imm
);
2362 case OP_X86_PUSH_MEMBASE
:
2363 x86_push_membase (code
, ins
->inst_basereg
, ins
->inst_offset
);
2365 case OP_X86_PUSH_OBJ
:
2366 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, ins
->inst_imm
);
2367 x86_push_reg (code
, X86_EDI
);
2368 x86_push_reg (code
, X86_ESI
);
2369 x86_push_reg (code
, X86_ECX
);
2370 if (ins
->inst_offset
)
2371 x86_lea_membase (code
, X86_ESI
, ins
->inst_basereg
, ins
->inst_offset
);
2373 x86_mov_reg_reg (code
, X86_ESI
, ins
->inst_basereg
, 4);
2374 x86_lea_membase (code
, X86_EDI
, X86_ESP
, 12);
2375 x86_mov_reg_imm (code
, X86_ECX
, (ins
->inst_imm
>> 2));
2377 x86_prefix (code
, X86_REP_PREFIX
);
2379 x86_pop_reg (code
, X86_ECX
);
2380 x86_pop_reg (code
, X86_ESI
);
2381 x86_pop_reg (code
, X86_EDI
);
2384 x86_lea_memindex (code
, ins
->dreg
, ins
->sreg1
, ins
->inst_imm
, ins
->sreg2
, ins
->unused
);
2386 case OP_X86_LEA_MEMBASE
:
2387 x86_lea_membase (code
, ins
->dreg
, ins
->sreg1
, ins
->inst_imm
);
2390 x86_xchg_reg_reg (code
, ins
->sreg1
, ins
->sreg2
, 4);
2393 /* keep alignment */
2394 x86_alu_reg_imm (code
, X86_ADD
, ins
->sreg1
, MONO_ARCH_FRAME_ALIGNMENT
- 1);
2395 x86_alu_reg_imm (code
, X86_AND
, ins
->sreg1
, ~(MONO_ARCH_FRAME_ALIGNMENT
- 1));
2396 code
= mono_emit_stack_alloc (code
, ins
);
2397 x86_mov_reg_reg (code
, ins
->dreg
, X86_ESP
, 4);
2403 x86_push_reg (code
, ins
->sreg1
);
2404 code
= emit_call (cfg
, code
, MONO_PATCH_INFO_INTERNAL_METHOD
,
2405 (gpointer
)"mono_arch_throw_exception");
2409 x86_push_reg (code
, ins
->sreg1
);
2410 code
= emit_call (cfg
, code
, MONO_PATCH_INFO_INTERNAL_METHOD
,
2411 (gpointer
)"mono_arch_rethrow_exception");
2414 case OP_CALL_HANDLER
:
2417 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 12);
2419 mono_add_patch_info (cfg
, code
- cfg
->native_code
, MONO_PATCH_INFO_BB
, ins
->inst_target_bb
);
2420 x86_call_imm (code
, 0);
2422 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 12);
2426 ins
->inst_c0
= code
- cfg
->native_code
;
2429 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2430 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2432 if (ins
->flags
& MONO_INST_BRLABEL
) {
2433 if (ins
->inst_i0
->inst_c0
) {
2434 x86_jump_code (code
, cfg
->native_code
+ ins
->inst_i0
->inst_c0
);
2436 mono_add_patch_info (cfg
, offset
, MONO_PATCH_INFO_LABEL
, ins
->inst_i0
);
2437 if ((cfg
->opt
& MONO_OPT_BRANCH
) &&
2438 x86_is_imm8 (ins
->inst_i0
->inst_c1
- cpos
))
2439 x86_jump8 (code
, 0);
2441 x86_jump32 (code
, 0);
2444 if (ins
->inst_target_bb
->native_offset
) {
2445 x86_jump_code (code
, cfg
->native_code
+ ins
->inst_target_bb
->native_offset
);
2447 mono_add_patch_info (cfg
, offset
, MONO_PATCH_INFO_BB
, ins
->inst_target_bb
);
2448 if ((cfg
->opt
& MONO_OPT_BRANCH
) &&
2449 x86_is_imm8 (ins
->inst_target_bb
->max_offset
- cpos
))
2450 x86_jump8 (code
, 0);
2452 x86_jump32 (code
, 0);
2457 x86_jump_reg (code
, ins
->sreg1
);
2460 x86_set_reg (code
, X86_CC_EQ
, ins
->dreg
, TRUE
);
2461 x86_widen_reg (code
, ins
->dreg
, ins
->dreg
, FALSE
, FALSE
);
2464 x86_set_reg (code
, X86_CC_LT
, ins
->dreg
, TRUE
);
2465 x86_widen_reg (code
, ins
->dreg
, ins
->dreg
, FALSE
, FALSE
);
2468 x86_set_reg (code
, X86_CC_LT
, ins
->dreg
, FALSE
);
2469 x86_widen_reg (code
, ins
->dreg
, ins
->dreg
, FALSE
, FALSE
);
2472 x86_set_reg (code
, X86_CC_GT
, ins
->dreg
, TRUE
);
2473 x86_widen_reg (code
, ins
->dreg
, ins
->dreg
, FALSE
, FALSE
);
2476 x86_set_reg (code
, X86_CC_GT
, ins
->dreg
, FALSE
);
2477 x86_widen_reg (code
, ins
->dreg
, ins
->dreg
, FALSE
, FALSE
);
2480 x86_set_reg (code
, X86_CC_NE
, ins
->dreg
, TRUE
);
2481 x86_widen_reg (code
, ins
->dreg
, ins
->dreg
, FALSE
, FALSE
);
2483 case OP_COND_EXC_EQ
:
2484 case OP_COND_EXC_NE_UN
:
2485 case OP_COND_EXC_LT
:
2486 case OP_COND_EXC_LT_UN
:
2487 case OP_COND_EXC_GT
:
2488 case OP_COND_EXC_GT_UN
:
2489 case OP_COND_EXC_GE
:
2490 case OP_COND_EXC_GE_UN
:
2491 case OP_COND_EXC_LE
:
2492 case OP_COND_EXC_LE_UN
:
2493 case OP_COND_EXC_OV
:
2494 case OP_COND_EXC_NO
:
2496 case OP_COND_EXC_NC
:
2497 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table
[ins
->opcode
- OP_COND_EXC_EQ
], (ins
->opcode
< OP_COND_EXC_NE_UN
), ins
->inst_p1
);
2509 EMIT_COND_BRANCH (ins
, branch_cc_table
[ins
->opcode
- CEE_BEQ
], (ins
->opcode
< CEE_BNE_UN
));
2512 /* floating point opcodes */
2514 double d
= *(double *)ins
->inst_p0
;
2516 if ((d
== 0.0) && (mono_signbit (d
) == 0)) {
2518 } else if (d
== 1.0) {
2521 if (cfg
->compile_aot
) {
2522 guint32
*val
= (guint32
*)&d
;
2523 x86_push_imm (code
, val
[1]);
2524 x86_push_imm (code
, val
[0]);
2525 x86_fld_membase (code
, X86_ESP
, 0, TRUE
);
2526 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 8);
2529 mono_add_patch_info (cfg
, code
- cfg
->native_code
, MONO_PATCH_INFO_R8
, ins
->inst_p0
);
2530 x86_fld (code
, NULL
, TRUE
);
2536 float f
= *(float *)ins
->inst_p0
;
2538 if ((f
== 0.0) && (mono_signbit (f
) == 0)) {
2540 } else if (f
== 1.0) {
2543 if (cfg
->compile_aot
) {
2544 guint32 val
= *(guint32
*)&f
;
2545 x86_push_imm (code
, val
);
2546 x86_fld_membase (code
, X86_ESP
, 0, FALSE
);
2547 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 4);
2550 mono_add_patch_info (cfg
, code
- cfg
->native_code
, MONO_PATCH_INFO_R4
, ins
->inst_p0
);
2551 x86_fld (code
, NULL
, FALSE
);
2556 case OP_STORER8_MEMBASE_REG
:
2557 x86_fst_membase (code
, ins
->inst_destbasereg
, ins
->inst_offset
, TRUE
, TRUE
);
2559 case OP_LOADR8_SPILL_MEMBASE
:
2560 x86_fld_membase (code
, ins
->inst_basereg
, ins
->inst_offset
, TRUE
);
2563 case OP_LOADR8_MEMBASE
:
2564 x86_fld_membase (code
, ins
->inst_basereg
, ins
->inst_offset
, TRUE
);
2566 case OP_STORER4_MEMBASE_REG
:
2567 x86_fst_membase (code
, ins
->inst_destbasereg
, ins
->inst_offset
, FALSE
, TRUE
);
2569 case OP_LOADR4_MEMBASE
:
2570 x86_fld_membase (code
, ins
->inst_basereg
, ins
->inst_offset
, FALSE
);
2572 case CEE_CONV_R4
: /* FIXME: change precision */
2574 x86_push_reg (code
, ins
->sreg1
);
2575 x86_fild_membase (code
, X86_ESP
, 0, FALSE
);
2576 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 4);
2578 case OP_X86_FP_LOAD_I8
:
2579 x86_fild_membase (code
, ins
->inst_basereg
, ins
->inst_offset
, TRUE
);
2581 case OP_X86_FP_LOAD_I4
:
2582 x86_fild_membase (code
, ins
->inst_basereg
, ins
->inst_offset
, FALSE
);
2584 case OP_FCONV_TO_I1
:
2585 code
= emit_float_to_int (cfg
, code
, ins
->dreg
, 1, TRUE
);
2587 case OP_FCONV_TO_U1
:
2588 code
= emit_float_to_int (cfg
, code
, ins
->dreg
, 1, FALSE
);
2590 case OP_FCONV_TO_I2
:
2591 code
= emit_float_to_int (cfg
, code
, ins
->dreg
, 2, TRUE
);
2593 case OP_FCONV_TO_U2
:
2594 code
= emit_float_to_int (cfg
, code
, ins
->dreg
, 2, FALSE
);
2596 case OP_FCONV_TO_I4
:
2598 code
= emit_float_to_int (cfg
, code
, ins
->dreg
, 4, TRUE
);
2600 case OP_FCONV_TO_I8
:
2601 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 4);
2602 x86_fnstcw_membase(code
, X86_ESP
, 0);
2603 x86_mov_reg_membase (code
, ins
->dreg
, X86_ESP
, 0, 2);
2604 x86_alu_reg_imm (code
, X86_OR
, ins
->dreg
, 0xc00);
2605 x86_mov_membase_reg (code
, X86_ESP
, 2, ins
->dreg
, 2);
2606 x86_fldcw_membase (code
, X86_ESP
, 2);
2607 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 8);
2608 x86_fist_pop_membase (code
, X86_ESP
, 0, TRUE
);
2609 x86_pop_reg (code
, ins
->dreg
);
2610 x86_pop_reg (code
, ins
->unused
);
2611 x86_fldcw_membase (code
, X86_ESP
, 0);
2612 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 4);
2614 case OP_LCONV_TO_R_UN
: {
2615 static guint8 mn
[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2618 /* load 64bit integer to FP stack */
2619 x86_push_imm (code
, 0);
2620 x86_push_reg (code
, ins
->sreg2
);
2621 x86_push_reg (code
, ins
->sreg1
);
2622 x86_fild_membase (code
, X86_ESP
, 0, TRUE
);
2623 /* store as 80bit FP value */
2624 x86_fst80_membase (code
, X86_ESP
, 0);
2626 /* test if lreg is negative */
2627 x86_test_reg_reg (code
, ins
->sreg2
, ins
->sreg2
);
2628 br
= code
; x86_branch8 (code
, X86_CC_GEZ
, 0, TRUE
);
2630 /* add correction constant mn */
2631 x86_fld80_mem (code
, mn
);
2632 x86_fld80_membase (code
, X86_ESP
, 0);
2633 x86_fp_op_reg (code
, X86_FADD
, 1, TRUE
);
2634 x86_fst80_membase (code
, X86_ESP
, 0);
2636 x86_patch (br
, code
);
2638 x86_fld80_membase (code
, X86_ESP
, 0);
2639 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 12);
2643 case OP_LCONV_TO_OVF_I
: {
2644 guint8
*br
[3], *label
[1];
2648 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2650 x86_test_reg_reg (code
, ins
->sreg1
, ins
->sreg1
);
2652 /* If the low word top bit is set, see if we are negative */
2653 br
[0] = code
; x86_branch8 (code
, X86_CC_LT
, 0, TRUE
);
2654 /* We are not negative (no top bit set, check for our top word to be zero */
2655 x86_test_reg_reg (code
, ins
->sreg2
, ins
->sreg2
);
2656 br
[1] = code
; x86_branch8 (code
, X86_CC_EQ
, 0, TRUE
);
2659 /* throw exception */
2660 tins
= mono_branch_optimize_exception_target (cfg
, bb
, "OverflowException");
2662 mono_add_patch_info (cfg
, code
- cfg
->native_code
, MONO_PATCH_INFO_BB
, tins
->inst_true_bb
);
2663 if ((cfg
->opt
& MONO_OPT_BRANCH
) && x86_is_imm8 (tins
->inst_true_bb
->max_offset
- cpos
))
2664 x86_jump8 (code
, 0);
2666 x86_jump32 (code
, 0);
2668 mono_add_patch_info (cfg
, code
- cfg
->native_code
, MONO_PATCH_INFO_EXC
, "OverflowException");
2669 x86_jump32 (code
, 0);
2673 x86_patch (br
[0], code
);
2674 /* our top bit is set, check that top word is 0xfffffff */
2675 x86_alu_reg_imm (code
, X86_CMP
, ins
->sreg2
, 0xffffffff);
2677 x86_patch (br
[1], code
);
2678 /* nope, emit exception */
2679 br
[2] = code
; x86_branch8 (code
, X86_CC_NE
, 0, TRUE
);
2680 x86_patch (br
[2], label
[0]);
2682 if (ins
->dreg
!= ins
->sreg1
)
2683 x86_mov_reg_reg (code
, ins
->dreg
, ins
->sreg1
, 4);
2687 x86_fp_op_reg (code
, X86_FADD
, 1, TRUE
);
2690 x86_fp_op_reg (code
, X86_FSUB
, 1, TRUE
);
2693 x86_fp_op_reg (code
, X86_FMUL
, 1, TRUE
);
2696 x86_fp_op_reg (code
, X86_FDIV
, 1, TRUE
);
2704 x86_fp_op_reg (code
, X86_FADD
, 1, TRUE
);
2709 x86_fp_op_reg (code
, X86_FADD
, 1, TRUE
);
2716 * it really doesn't make sense to inline all this code,
2717 * it's here just to show that things may not be as simple
2720 guchar
*check_pos
, *end_tan
, *pop_jump
;
2721 x86_push_reg (code
, X86_EAX
);
2724 x86_test_reg_imm (code
, X86_EAX
, X86_FP_C2
);
2726 x86_branch8 (code
, X86_CC_NE
, 0, FALSE
);
2727 x86_fstp (code
, 0); /* pop the 1.0 */
2729 x86_jump8 (code
, 0);
2731 x86_fp_op (code
, X86_FADD
, 0);
2735 x86_test_reg_imm (code
, X86_EAX
, X86_FP_C2
);
2737 x86_branch8 (code
, X86_CC_NE
, 0, FALSE
);
2740 x86_patch (pop_jump
, code
);
2741 x86_fstp (code
, 0); /* pop the 1.0 */
2742 x86_patch (check_pos
, code
);
2743 x86_patch (end_tan
, code
);
2745 x86_fp_op_reg (code
, X86_FADD
, 1, TRUE
);
2746 x86_pop_reg (code
, X86_EAX
);
2753 x86_fp_op_reg (code
, X86_FADD
, 1, TRUE
);
2764 x86_push_reg (code
, X86_EAX
);
2765 /* we need to exchange ST(0) with ST(1) */
2768 /* this requires a loop, because fprem somtimes
2769 * returns a partial remainder */
2771 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
2772 /* x86_fprem1 (code); */
2775 x86_alu_reg_imm (code
, X86_AND
, X86_EAX
, X86_FP_C2
);
2777 x86_branch8 (code
, X86_CC_NE
, l1
- l2
, FALSE
);
2782 x86_pop_reg (code
, X86_EAX
);
2786 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2787 x86_fcomip (code
, 1);
2791 /* this overwrites EAX */
2792 EMIT_FPCOMPARE(code
);
2793 x86_alu_reg_imm (code
, X86_AND
, X86_EAX
, X86_FP_CC_MASK
);
2796 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2797 /* zeroing the register at the start results in
2798 * shorter and faster code (we can also remove the widening op)
2800 guchar
*unordered_check
;
2801 x86_alu_reg_reg (code
, X86_XOR
, ins
->dreg
, ins
->dreg
);
2802 x86_fcomip (code
, 1);
2804 unordered_check
= code
;
2805 x86_branch8 (code
, X86_CC_P
, 0, FALSE
);
2806 x86_set_reg (code
, X86_CC_EQ
, ins
->dreg
, FALSE
);
2807 x86_patch (unordered_check
, code
);
2810 if (ins
->dreg
!= X86_EAX
)
2811 x86_push_reg (code
, X86_EAX
);
2813 EMIT_FPCOMPARE(code
);
2814 x86_alu_reg_imm (code
, X86_AND
, X86_EAX
, X86_FP_CC_MASK
);
2815 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, 0x4000);
2816 x86_set_reg (code
, X86_CC_EQ
, ins
->dreg
, TRUE
);
2817 x86_widen_reg (code
, ins
->dreg
, ins
->dreg
, FALSE
, FALSE
);
2819 if (ins
->dreg
!= X86_EAX
)
2820 x86_pop_reg (code
, X86_EAX
);
2824 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2825 /* zeroing the register at the start results in
2826 * shorter and faster code (we can also remove the widening op)
2828 x86_alu_reg_reg (code
, X86_XOR
, ins
->dreg
, ins
->dreg
);
2829 x86_fcomip (code
, 1);
2831 if (ins
->opcode
== OP_FCLT_UN
) {
2832 guchar
*unordered_check
= code
;
2833 guchar
*jump_to_end
;
2834 x86_branch8 (code
, X86_CC_P
, 0, FALSE
);
2835 x86_set_reg (code
, X86_CC_GT
, ins
->dreg
, FALSE
);
2837 x86_jump8 (code
, 0);
2838 x86_patch (unordered_check
, code
);
2839 x86_inc_reg (code
, ins
->dreg
);
2840 x86_patch (jump_to_end
, code
);
2842 x86_set_reg (code
, X86_CC_GT
, ins
->dreg
, FALSE
);
2846 if (ins
->dreg
!= X86_EAX
)
2847 x86_push_reg (code
, X86_EAX
);
2849 EMIT_FPCOMPARE(code
);
2850 x86_alu_reg_imm (code
, X86_AND
, X86_EAX
, X86_FP_CC_MASK
);
2851 if (ins
->opcode
== OP_FCLT_UN
) {
2852 guchar
*is_not_zero_check
, *end_jump
;
2853 is_not_zero_check
= code
;
2854 x86_branch8 (code
, X86_CC_NZ
, 0, TRUE
);
2856 x86_jump8 (code
, 0);
2857 x86_patch (is_not_zero_check
, code
);
2858 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_CC_MASK
);
2860 x86_patch (end_jump
, code
);
2862 x86_set_reg (code
, X86_CC_EQ
, ins
->dreg
, TRUE
);
2863 x86_widen_reg (code
, ins
->dreg
, ins
->dreg
, FALSE
, FALSE
);
2865 if (ins
->dreg
!= X86_EAX
)
2866 x86_pop_reg (code
, X86_EAX
);
2870 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2871 /* zeroing the register at the start results in
2872 * shorter and faster code (we can also remove the widening op)
2874 guchar
*unordered_check
;
2875 x86_alu_reg_reg (code
, X86_XOR
, ins
->dreg
, ins
->dreg
);
2876 x86_fcomip (code
, 1);
2878 if (ins
->opcode
== OP_FCGT
) {
2879 unordered_check
= code
;
2880 x86_branch8 (code
, X86_CC_P
, 0, FALSE
);
2881 x86_set_reg (code
, X86_CC_LT
, ins
->dreg
, FALSE
);
2882 x86_patch (unordered_check
, code
);
2884 x86_set_reg (code
, X86_CC_LT
, ins
->dreg
, FALSE
);
2888 if (ins
->dreg
!= X86_EAX
)
2889 x86_push_reg (code
, X86_EAX
);
2891 EMIT_FPCOMPARE(code
);
2892 x86_alu_reg_imm (code
, X86_AND
, X86_EAX
, X86_FP_CC_MASK
);
2893 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_C0
);
2894 if (ins
->opcode
== OP_FCGT_UN
) {
2895 guchar
*is_not_zero_check
, *end_jump
;
2896 is_not_zero_check
= code
;
2897 x86_branch8 (code
, X86_CC_NZ
, 0, TRUE
);
2899 x86_jump8 (code
, 0);
2900 x86_patch (is_not_zero_check
, code
);
2901 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_CC_MASK
);
2903 x86_patch (end_jump
, code
);
2905 x86_set_reg (code
, X86_CC_EQ
, ins
->dreg
, TRUE
);
2906 x86_widen_reg (code
, ins
->dreg
, ins
->dreg
, FALSE
, FALSE
);
2908 if (ins
->dreg
!= X86_EAX
)
2909 x86_pop_reg (code
, X86_EAX
);
2912 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2913 guchar
*jump
= code
;
2914 x86_branch8 (code
, X86_CC_P
, 0, TRUE
);
2915 EMIT_COND_BRANCH (ins
, X86_CC_EQ
, FALSE
);
2916 x86_patch (jump
, code
);
2919 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, 0x4000);
2920 EMIT_COND_BRANCH (ins
, X86_CC_EQ
, TRUE
);
2923 /* Branch if C013 != 100 */
2924 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2925 /* branch if !ZF or (PF|CF) */
2926 EMIT_COND_BRANCH (ins
, X86_CC_NE
, FALSE
);
2927 EMIT_COND_BRANCH (ins
, X86_CC_P
, FALSE
);
2928 EMIT_COND_BRANCH (ins
, X86_CC_B
, FALSE
);
2931 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_C3
);
2932 EMIT_COND_BRANCH (ins
, X86_CC_NE
, FALSE
);
2935 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2936 EMIT_COND_BRANCH (ins
, X86_CC_GT
, FALSE
);
2939 EMIT_COND_BRANCH (ins
, X86_CC_EQ
, FALSE
);
2942 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2943 EMIT_COND_BRANCH (ins
, X86_CC_P
, FALSE
);
2944 EMIT_COND_BRANCH (ins
, X86_CC_GT
, FALSE
);
2947 if (ins
->opcode
== OP_FBLT_UN
) {
2948 guchar
*is_not_zero_check
, *end_jump
;
2949 is_not_zero_check
= code
;
2950 x86_branch8 (code
, X86_CC_NZ
, 0, TRUE
);
2952 x86_jump8 (code
, 0);
2953 x86_patch (is_not_zero_check
, code
);
2954 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_CC_MASK
);
2956 x86_patch (end_jump
, code
);
2958 EMIT_COND_BRANCH (ins
, X86_CC_EQ
, FALSE
);
2962 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2963 EMIT_COND_BRANCH (ins
, X86_CC_LT
, FALSE
);
2966 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_C0
);
2967 if (ins
->opcode
== OP_FBGT_UN
) {
2968 guchar
*is_not_zero_check
, *end_jump
;
2969 is_not_zero_check
= code
;
2970 x86_branch8 (code
, X86_CC_NZ
, 0, TRUE
);
2972 x86_jump8 (code
, 0);
2973 x86_patch (is_not_zero_check
, code
);
2974 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_CC_MASK
);
2976 x86_patch (end_jump
, code
);
2978 EMIT_COND_BRANCH (ins
, X86_CC_EQ
, FALSE
);
2981 /* Branch if C013 == 100 or 001 */
2982 if (cfg
->opt
& MONO_OPT_FCMOV
) {
2985 /* skip branch if C1=1 */
2987 x86_branch8 (code
, X86_CC_P
, 0, FALSE
);
2988 /* branch if (C0 | C3) = 1 */
2989 EMIT_COND_BRANCH (ins
, X86_CC_BE
, FALSE
);
2990 x86_patch (br1
, code
);
2993 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_C0
);
2994 EMIT_COND_BRANCH (ins
, X86_CC_EQ
, FALSE
);
2995 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_C3
);
2996 EMIT_COND_BRANCH (ins
, X86_CC_EQ
, FALSE
);
2999 /* Branch if C013 == 000 */
3000 if (cfg
->opt
& MONO_OPT_FCMOV
) {
3001 EMIT_COND_BRANCH (ins
, X86_CC_LE
, FALSE
);
3004 EMIT_COND_BRANCH (ins
, X86_CC_NE
, FALSE
);
3007 /* Branch if C013=000 or 100 */
3008 if (cfg
->opt
& MONO_OPT_FCMOV
) {
3011 /* skip branch if C1=1 */
3013 x86_branch8 (code
, X86_CC_P
, 0, FALSE
);
3014 /* branch if C0=0 */
3015 EMIT_COND_BRANCH (ins
, X86_CC_NB
, FALSE
);
3016 x86_patch (br1
, code
);
3019 x86_alu_reg_imm (code
, X86_AND
, X86_EAX
, (X86_FP_C0
|X86_FP_C1
));
3020 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, 0);
3021 EMIT_COND_BRANCH (ins
, X86_CC_EQ
, FALSE
);
3024 /* Branch if C013 != 001 */
3025 if (cfg
->opt
& MONO_OPT_FCMOV
) {
3026 EMIT_COND_BRANCH (ins
, X86_CC_P
, FALSE
);
3027 EMIT_COND_BRANCH (ins
, X86_CC_GE
, FALSE
);
3030 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_C0
);
3031 EMIT_COND_BRANCH (ins
, X86_CC_NE
, FALSE
);
3033 case CEE_CKFINITE
: {
3034 x86_push_reg (code
, X86_EAX
);
3037 x86_alu_reg_imm (code
, X86_AND
, X86_EAX
, 0x4100);
3038 x86_alu_reg_imm (code
, X86_CMP
, X86_EAX
, X86_FP_C0
);
3039 x86_pop_reg (code
, X86_EAX
);
3040 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ
, FALSE
, "ArithmeticException");
3044 code
= emit_tls_get (code
, ins
->dreg
, ins
->inst_offset
);
3047 case OP_MEMORY_BARRIER
: {
3048 /* Not needed on x86 */
3051 case OP_ATOMIC_ADD_I4
: {
3052 int dreg
= ins
->dreg
;
3054 if (dreg
== ins
->inst_basereg
) {
3055 x86_push_reg (code
, ins
->sreg2
);
3059 if (dreg
!= ins
->sreg2
)
3060 x86_mov_reg_reg (code
, ins
->dreg
, ins
->sreg2
, 4);
3062 x86_prefix (code
, X86_LOCK_PREFIX
);
3063 x86_xadd_membase_reg (code
, ins
->inst_basereg
, ins
->inst_offset
, dreg
, 4);
3065 if (dreg
!= ins
->dreg
) {
3066 x86_mov_reg_reg (code
, ins
->dreg
, dreg
, 4);
3067 x86_pop_reg (code
, dreg
);
3072 case OP_ATOMIC_ADD_NEW_I4
: {
3073 int dreg
= ins
->dreg
;
3075 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
3076 if (ins
->sreg2
== dreg
) {
3077 if (dreg
== X86_EBX
) {
3079 if (ins
->inst_basereg
== X86_EDI
)
3083 if (ins
->inst_basereg
== X86_EBX
)
3086 } else if (ins
->inst_basereg
== dreg
) {
3087 if (dreg
== X86_EBX
) {
3089 if (ins
->sreg2
== X86_EDI
)
3093 if (ins
->sreg2
== X86_EBX
)
3098 if (dreg
!= ins
->dreg
) {
3099 x86_push_reg (code
, dreg
);
3102 x86_mov_reg_reg (code
, dreg
, ins
->sreg2
, 4);
3103 x86_prefix (code
, X86_LOCK_PREFIX
);
3104 x86_xadd_membase_reg (code
, ins
->inst_basereg
, ins
->inst_offset
, dreg
, 4);
3105 /* dreg contains the old value, add with sreg2 value */
3106 x86_alu_reg_reg (code
, X86_ADD
, dreg
, ins
->sreg2
);
3108 if (ins
->dreg
!= dreg
) {
3109 x86_mov_reg_reg (code
, ins
->dreg
, dreg
, 4);
3110 x86_pop_reg (code
, dreg
);
3115 case OP_ATOMIC_EXCHANGE_I4
: {
3117 int sreg2
= ins
->sreg2
;
3118 int breg
= ins
->inst_basereg
;
3120 /* cmpxchg uses eax as comperand, need to make sure we can use it
3121 * hack to overcome limits in x86 reg allocator
3122 * (req: dreg == eax and sreg2 != eax and breg != eax)
3124 if (ins
->dreg
!= X86_EAX
)
3125 x86_push_reg (code
, X86_EAX
);
3127 /* We need the EAX reg for the cmpxchg */
3128 if (ins
->sreg2
== X86_EAX
) {
3129 x86_push_reg (code
, X86_EDX
);
3130 x86_mov_reg_reg (code
, X86_EDX
, X86_EAX
, 4);
3134 if (breg
== X86_EAX
) {
3135 x86_push_reg (code
, X86_ESI
);
3136 x86_mov_reg_reg (code
, X86_ESI
, X86_EAX
, 4);
3140 x86_mov_reg_membase (code
, X86_EAX
, breg
, ins
->inst_offset
, 4);
3142 br
[0] = code
; x86_prefix (code
, X86_LOCK_PREFIX
);
3143 x86_cmpxchg_membase_reg (code
, breg
, ins
->inst_offset
, sreg2
);
3144 br
[1] = code
; x86_branch8 (code
, X86_CC_NE
, -1, FALSE
);
3145 x86_patch (br
[1], br
[0]);
3147 if (breg
!= ins
->inst_basereg
)
3148 x86_pop_reg (code
, X86_ESI
);
3150 if (ins
->dreg
!= X86_EAX
) {
3151 x86_mov_reg_reg (code
, ins
->dreg
, X86_EAX
, 4);
3152 x86_pop_reg (code
, X86_EAX
);
3155 if (ins
->sreg2
!= sreg2
)
3156 x86_pop_reg (code
, X86_EDX
);
3161 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins
->opcode
), __FUNCTION__
);
3162 g_assert_not_reached ();
3165 if ((code
- cfg
->native_code
- offset
) > max_len
) {
3166 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3167 mono_inst_name (ins
->opcode
), max_len
, code
- cfg
->native_code
- offset
);
3168 g_assert_not_reached ();
3174 last_offset
= offset
;
3179 cfg
->code_len
= code
- cfg
->native_code
;
3183 mono_arch_register_lowlevel_calls (void)
3188 mono_arch_patch_code (MonoMethod
*method
, MonoDomain
*domain
, guint8
*code
, MonoJumpInfo
*ji
, gboolean run_cctors
)
3190 MonoJumpInfo
*patch_info
;
3191 gboolean compile_aot
= !run_cctors
;
3193 for (patch_info
= ji
; patch_info
; patch_info
= patch_info
->next
) {
3194 unsigned char *ip
= patch_info
->ip
.i
+ code
;
3195 const unsigned char *target
;
3197 target
= mono_resolve_patch_target (method
, domain
, code
, patch_info
, run_cctors
);
3200 switch (patch_info
->type
) {
3201 case MONO_PATCH_INFO_BB
:
3202 case MONO_PATCH_INFO_LABEL
:
3205 /* No need to patch these */
3210 switch (patch_info
->type
) {
3211 case MONO_PATCH_INFO_IP
:
3212 *((gconstpointer
*)(ip
)) = target
;
3214 case MONO_PATCH_INFO_CLASS_INIT
: {
3216 /* Might already been changed to a nop */
3217 x86_call_code (code
, 0);
3218 x86_patch (ip
, target
);
3221 case MONO_PATCH_INFO_ABS
:
3222 case MONO_PATCH_INFO_METHOD
:
3223 case MONO_PATCH_INFO_METHOD_JUMP
:
3224 case MONO_PATCH_INFO_INTERNAL_METHOD
:
3225 case MONO_PATCH_INFO_BB
:
3226 case MONO_PATCH_INFO_LABEL
:
3227 x86_patch (ip
, target
);
3229 case MONO_PATCH_INFO_NONE
:
3232 guint32 offset
= mono_arch_get_patch_offset (ip
);
3233 *((gconstpointer
*)(ip
+ offset
)) = target
;
3241 mono_arch_emit_prolog (MonoCompile
*cfg
)
3243 MonoMethod
*method
= cfg
->method
;
3245 MonoMethodSignature
*sig
;
3247 int alloc_size
, pos
, max_offset
, i
;
3250 cfg
->code_size
= MAX (mono_method_get_header (method
)->code_size
* 4, 256);
3251 code
= cfg
->native_code
= g_malloc (cfg
->code_size
);
3253 x86_push_reg (code
, X86_EBP
);
3254 x86_mov_reg_reg (code
, X86_EBP
, X86_ESP
, 4);
3256 alloc_size
= cfg
->stack_offset
;
3259 if (method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
) {
3260 /* Might need to attach the thread to the JIT */
3261 if (lmf_tls_offset
!= -1) {
3264 code
= emit_tls_get ( code
, X86_EAX
, lmf_tls_offset
);
3265 x86_test_reg_reg (code
, X86_EAX
, X86_EAX
);
3267 x86_branch8 (code
, X86_CC_NE
, 0, 0);
3268 x86_push_imm (code
, cfg
->domain
);
3269 code
= emit_call (cfg
, code
, MONO_PATCH_INFO_INTERNAL_METHOD
, (gpointer
)"mono_jit_thread_attach");
3270 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 4);
3271 x86_patch (buf
, code
);
3272 #ifdef PLATFORM_WIN32
3273 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3274 /* FIXME: Add a separate key for LMF to avoid this */
3275 x86_alu_reg_imm (code
, X86_ADD
, X86_EAX
, G_STRUCT_OFFSET (MonoJitTlsData
, lmf
));
3278 g_assert (!cfg
->compile_aot
);
3279 x86_push_imm (code
, cfg
->domain
);
3280 code
= emit_call (cfg
, code
, MONO_PATCH_INFO_INTERNAL_METHOD
, (gpointer
)"mono_jit_thread_attach");
3281 x86_alu_reg_imm (code
, X86_ADD
, X86_ESP
, 4);
3285 if (method
->save_lmf
) {
3286 pos
+= sizeof (MonoLMF
);
3288 /* save the current IP */
3289 mono_add_patch_info (cfg
, code
+ 1 - cfg
->native_code
, MONO_PATCH_INFO_IP
, NULL
);
3290 x86_push_imm_template (code
);
3292 /* save all caller saved regs */
3293 x86_push_reg (code
, X86_EBP
);
3294 x86_push_reg (code
, X86_ESI
);
3295 x86_push_reg (code
, X86_EDI
);
3296 x86_push_reg (code
, X86_EBX
);
3298 /* save method info */
3299 x86_push_imm (code
, method
);
3301 /* get the address of lmf for the current thread */
3303 * This is performance critical so we try to use some tricks to make
3306 if (lmf_tls_offset
!= -1) {
3307 /* Load lmf quicky using the GS register */
3308 code
= emit_tls_get (code
, X86_EAX
, lmf_tls_offset
);
3309 #ifdef PLATFORM_WIN32
3310 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3311 /* FIXME: Add a separate key for LMF to avoid this */
3312 x86_alu_reg_imm (code
, X86_ADD
, X86_EAX
, G_STRUCT_OFFSET (MonoJitTlsData
, lmf
));
3315 code
= emit_call (cfg
, code
, MONO_PATCH_INFO_INTERNAL_METHOD
, (gpointer
)"mono_get_lmf_addr");
3319 x86_push_reg (code
, X86_EAX
);
3320 /* push *lfm (previous_lmf) */
3321 x86_push_membase (code
, X86_EAX
, 0);
3323 x86_mov_membase_reg (code
, X86_EAX
, 0, X86_ESP
, 4);
3326 if (cfg
->used_int_regs
& (1 << X86_EBX
)) {
3327 x86_push_reg (code
, X86_EBX
);
3331 if (cfg
->used_int_regs
& (1 << X86_EDI
)) {
3332 x86_push_reg (code
, X86_EDI
);
3336 if (cfg
->used_int_regs
& (1 << X86_ESI
)) {
3337 x86_push_reg (code
, X86_ESI
);
3345 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
3347 int tot
= alloc_size
+ pos
+ 4 + 4; /* ret ip + ebp */
3359 /* See mono_emit_stack_alloc */
3360 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3361 guint32 remaining_size
= alloc_size
;
3362 while (remaining_size
>= 0x1000) {
3363 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, 0x1000);
3364 x86_test_membase_reg (code
, X86_ESP
, 0, X86_ESP
);
3365 remaining_size
-= 0x1000;
3368 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, remaining_size
);
3370 x86_alu_reg_imm (code
, X86_SUB
, X86_ESP
, alloc_size
);
3375 /* check the stack is aligned */
3376 x86_mov_reg_reg (code
, X86_EDX
, X86_ESP
, 4);
3377 x86_alu_reg_imm (code
, X86_AND
, X86_EDX
, 15);
3378 x86_alu_reg_imm (code
, X86_CMP
, X86_EDX
, 0);
3379 x86_branch_disp (code
, X86_CC_EQ
, 3, FALSE
);
3380 x86_breakpoint (code
);
3383 /* compute max_offset in order to use short forward jumps */
3385 if (cfg
->opt
& MONO_OPT_BRANCH
) {
3386 for (bb
= cfg
->bb_entry
; bb
; bb
= bb
->next_bb
) {
3387 MonoInst
*ins
= bb
->code
;
3388 bb
->max_offset
= max_offset
;
3390 if (cfg
->prof_options
& MONO_PROFILE_COVERAGE
)
3392 /* max alignment for loops */
3393 if ((cfg
->opt
& MONO_OPT_LOOP
) && bb_is_loop_start (bb
))
3394 max_offset
+= LOOP_ALIGNMENT
;
3397 if (ins
->opcode
== OP_LABEL
)
3398 ins
->inst_c1
= max_offset
;
3400 max_offset
+= ((guint8
*)ins_spec
[ins
->opcode
])[MONO_INST_LEN
];
3406 if (mono_jit_trace_calls
!= NULL
&& mono_trace_eval (method
))
3407 code
= mono_arch_instrument_prolog (cfg
, mono_trace_enter_method
, code
, TRUE
);
3409 /* load arguments allocated to register from the stack */
3410 sig
= mono_method_signature (method
);
3413 for (i
= 0; i
< sig
->param_count
+ sig
->hasthis
; ++i
) {
3414 inst
= cfg
->varinfo
[pos
];
3415 if (inst
->opcode
== OP_REGVAR
) {
3416 x86_mov_reg_membase (code
, inst
->dreg
, X86_EBP
, inst
->inst_offset
, 4);
3417 if (cfg
->verbose_level
> 2)
3418 g_print ("Argument %d assigned to register %s\n", pos
, mono_arch_regname (inst
->dreg
));
3423 cfg
->code_len
= code
- cfg
->native_code
;
3429 mono_arch_emit_epilog (MonoCompile
*cfg
)
3431 MonoMethod
*method
= cfg
->method
;
3432 MonoMethodSignature
*sig
= mono_method_signature (method
);
3434 guint32 stack_to_pop
;
3436 int max_epilog_size
= 16;
3439 if (cfg
->method
->save_lmf
)
3440 max_epilog_size
+= 128;
3442 if (mono_jit_trace_calls
!= NULL
)
3443 max_epilog_size
+= 50;
3445 while (cfg
->code_len
+ max_epilog_size
> (cfg
->code_size
- 16)) {
3446 cfg
->code_size
*= 2;
3447 cfg
->native_code
= g_realloc (cfg
->native_code
, cfg
->code_size
);
3448 mono_jit_stats
.code_reallocs
++;
3451 code
= cfg
->native_code
+ cfg
->code_len
;
3453 if (mono_jit_trace_calls
!= NULL
&& mono_trace_eval (method
))
3454 code
= mono_arch_instrument_epilog (cfg
, mono_trace_leave_method
, code
, TRUE
);
3456 /* the code restoring the registers must be kept in sync with CEE_JMP */
3459 if (method
->save_lmf
) {
3460 gint32 prev_lmf_reg
;
3461 gint32 lmf_offset
= -sizeof (MonoLMF
);
3463 /* Find a spare register */
3464 switch (sig
->ret
->type
) {
3467 prev_lmf_reg
= X86_EDI
;
3468 cfg
->used_int_regs
|= (1 << X86_EDI
);
3471 prev_lmf_reg
= X86_EDX
;
3475 /* reg = previous_lmf */
3476 x86_mov_reg_membase (code
, prev_lmf_reg
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, previous_lmf
), 4);
3479 x86_mov_reg_membase (code
, X86_ECX
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, lmf_addr
), 4);
3481 /* *(lmf) = previous_lmf */
3482 x86_mov_membase_reg (code
, X86_ECX
, 0, prev_lmf_reg
, 4);
3484 /* restore caller saved regs */
3485 if (cfg
->used_int_regs
& (1 << X86_EBX
)) {
3486 x86_mov_reg_membase (code
, X86_EBX
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, ebx
), 4);
3489 if (cfg
->used_int_regs
& (1 << X86_EDI
)) {
3490 x86_mov_reg_membase (code
, X86_EDI
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, edi
), 4);
3492 if (cfg
->used_int_regs
& (1 << X86_ESI
)) {
3493 x86_mov_reg_membase (code
, X86_ESI
, X86_EBP
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, esi
), 4);
3496 /* EBP is restored by LEAVE */
3498 if (cfg
->used_int_regs
& (1 << X86_EBX
)) {
3501 if (cfg
->used_int_regs
& (1 << X86_EDI
)) {
3504 if (cfg
->used_int_regs
& (1 << X86_ESI
)) {
3509 x86_lea_membase (code
, X86_ESP
, X86_EBP
, pos
);
3511 if (cfg
->used_int_regs
& (1 << X86_ESI
)) {
3512 x86_pop_reg (code
, X86_ESI
);
3514 if (cfg
->used_int_regs
& (1 << X86_EDI
)) {
3515 x86_pop_reg (code
, X86_EDI
);
3517 if (cfg
->used_int_regs
& (1 << X86_EBX
)) {
3518 x86_pop_reg (code
, X86_EBX
);
3522 /* Load returned vtypes into registers if needed */
3523 cinfo
= get_call_info (sig
, FALSE
);
3524 if (cinfo
->ret
.storage
== ArgValuetypeInReg
) {
3525 for (quad
= 0; quad
< 2; quad
++) {
3526 switch (cinfo
->ret
.pair_storage
[quad
]) {
3528 x86_mov_reg_membase (code
, cinfo
->ret
.pair_regs
[quad
], cfg
->ret
->inst_basereg
, cfg
->ret
->inst_offset
+ (quad
* sizeof (gpointer
)), 4);
3530 case ArgOnFloatFpStack
:
3531 x86_fld_membase (code
, cfg
->ret
->inst_basereg
, cfg
->ret
->inst_offset
+ (quad
* sizeof (gpointer
)), FALSE
);
3533 case ArgOnDoubleFpStack
:
3534 x86_fld_membase (code
, cfg
->ret
->inst_basereg
, cfg
->ret
->inst_offset
+ (quad
* sizeof (gpointer
)), TRUE
);
3539 g_assert_not_reached ();
3546 if (CALLCONV_IS_STDCALL (sig
)) {
3547 MonoJitArgumentInfo
*arg_info
= alloca (sizeof (MonoJitArgumentInfo
) * (sig
->param_count
+ 1));
3549 stack_to_pop
= mono_arch_get_argument_info (sig
, sig
->param_count
, arg_info
);
3550 } else if (MONO_TYPE_ISSTRUCT (mono_method_signature (cfg
->method
)->ret
) && (cinfo
->ret
.storage
== ArgOnStack
))
3556 x86_ret_imm (code
, stack_to_pop
);
3562 cfg
->code_len
= code
- cfg
->native_code
;
3564 g_assert (cfg
->code_len
< cfg
->code_size
);
3568 mono_arch_emit_exceptions (MonoCompile
*cfg
)
3570 MonoJumpInfo
*patch_info
;
3573 MonoClass
*exc_classes
[16];
3574 guint8
*exc_throw_start
[16], *exc_throw_end
[16];
3578 /* Compute needed space */
3579 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
3580 if (patch_info
->type
== MONO_PATCH_INFO_EXC
)
3585 * make sure we have enough space for exceptions
3586 * 16 is the size of two push_imm instructions and a call
3588 if (cfg
->compile_aot
)
3589 code_size
= exc_count
* 32;
3591 code_size
= exc_count
* 16;
3593 while (cfg
->code_len
+ code_size
> (cfg
->code_size
- 16)) {
3594 cfg
->code_size
*= 2;
3595 cfg
->native_code
= g_realloc (cfg
->native_code
, cfg
->code_size
);
3596 mono_jit_stats
.code_reallocs
++;
3599 code
= cfg
->native_code
+ cfg
->code_len
;
3602 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
3603 switch (patch_info
->type
) {
3604 case MONO_PATCH_INFO_EXC
: {
3605 MonoClass
*exc_class
;
3609 x86_patch (patch_info
->ip
.i
+ cfg
->native_code
, code
);
3611 exc_class
= mono_class_from_name (mono_defaults
.corlib
, "System", patch_info
->data
.name
);
3612 g_assert (exc_class
);
3613 throw_ip
= patch_info
->ip
.i
;
3615 /* Find a throw sequence for the same exception class */
3616 for (i
= 0; i
< nthrows
; ++i
)
3617 if (exc_classes
[i
] == exc_class
)
3620 x86_push_imm (code
, (exc_throw_end
[i
] - cfg
->native_code
) - throw_ip
);
3621 x86_jump_code (code
, exc_throw_start
[i
]);
3622 patch_info
->type
= MONO_PATCH_INFO_NONE
;
3627 /* Compute size of code following the push <OFFSET> */
3630 if ((code
- cfg
->native_code
) - throw_ip
< 126 - size
) {
3631 /* Use the shorter form */
3633 x86_push_imm (code
, 0);
3637 x86_push_imm (code
, 0xf0f0f0f0);
3642 exc_classes
[nthrows
] = exc_class
;
3643 exc_throw_start
[nthrows
] = code
;
3646 x86_push_imm (code
, exc_class
->type_token
);
3647 patch_info
->data
.name
= "mono_arch_throw_corlib_exception";
3648 patch_info
->type
= MONO_PATCH_INFO_INTERNAL_METHOD
;
3649 patch_info
->ip
.i
= code
- cfg
->native_code
;
3650 x86_call_code (code
, 0);
3651 x86_push_imm (buf
, (code
- cfg
->native_code
) - throw_ip
);
3656 exc_throw_end
[nthrows
] = code
;
3668 cfg
->code_len
= code
- cfg
->native_code
;
3670 g_assert (cfg
->code_len
< cfg
->code_size
);
3674 mono_arch_flush_icache (guint8
*code
, gint size
)
3680 mono_arch_flush_register_windows (void)
3685 * Support for fast access to the thread-local lmf structure using the GS
3686 * segment register on NPTL + kernel 2.6.x.
3689 static gboolean tls_offset_inited
= FALSE
;
3692 mono_arch_setup_jit_tls_data (MonoJitTlsData
*tls
)
3694 if (!tls_offset_inited
) {
3695 if (!getenv ("MONO_NO_TLS")) {
3696 #ifdef PLATFORM_WIN32
3698 * We need to init this multiple times, since when we are first called, the key might not
3699 * be initialized yet.
3701 appdomain_tls_offset
= mono_domain_get_tls_key ();
3702 lmf_tls_offset
= mono_get_jit_tls_key ();
3703 thread_tls_offset
= mono_thread_get_tls_key ();
3705 /* Only 64 tls entries can be accessed using inline code */
3706 if (appdomain_tls_offset
>= 64)
3707 appdomain_tls_offset
= -1;
3708 if (lmf_tls_offset
>= 64)
3709 lmf_tls_offset
= -1;
3710 if (thread_tls_offset
>= 64)
3711 thread_tls_offset
= -1;
3714 optimize_for_xen
= access ("/proc/xen", F_OK
) == 0;
3716 tls_offset_inited
= TRUE
;
3717 appdomain_tls_offset
= mono_domain_get_tls_offset ();
3718 lmf_tls_offset
= mono_get_lmf_tls_offset ();
3719 thread_tls_offset
= mono_thread_get_tls_offset ();
3726 mono_arch_free_jit_tls_data (MonoJitTlsData
*tls
)
3731 mono_arch_emit_this_vret_args (MonoCompile
*cfg
, MonoCallInst
*inst
, int this_reg
, int this_type
, int vt_reg
)
3733 MonoCallInst
*call
= (MonoCallInst
*)inst
;
3734 CallInfo
*cinfo
= get_call_info (inst
->signature
, FALSE
);
3736 /* add the this argument */
3737 if (this_reg
!= -1) {
3738 if (cinfo
->args
[0].storage
== ArgInIReg
) {
3740 MONO_INST_NEW (cfg
, this, OP_MOVE
);
3741 this->type
= this_type
;
3742 this->sreg1
= this_reg
;
3743 this->dreg
= mono_regstate_next_int (cfg
->rs
);
3744 mono_bblock_add_inst (cfg
->cbb
, this);
3746 mono_call_inst_add_outarg_reg (cfg
, call
, this->dreg
, cinfo
->args
[0].reg
, FALSE
);
3750 MONO_INST_NEW (cfg
, this, OP_OUTARG
);
3751 this->type
= this_type
;
3752 this->sreg1
= this_reg
;
3753 mono_bblock_add_inst (cfg
->cbb
, this);
3760 if (cinfo
->ret
.storage
== ArgValuetypeInReg
) {
3762 * The valuetype is in EAX:EDX after the call, needs to be copied to
3763 * the stack. Save the address here, so the call instruction can
3766 MONO_INST_NEW (cfg
, vtarg
, OP_STORE_MEMBASE_REG
);
3767 vtarg
->inst_destbasereg
= X86_ESP
;
3768 vtarg
->inst_offset
= inst
->stack_usage
;
3769 vtarg
->sreg1
= vt_reg
;
3770 mono_bblock_add_inst (cfg
->cbb
, vtarg
);
3772 else if (cinfo
->ret
.storage
== ArgInIReg
) {
3773 /* The return address is passed in a register */
3774 MONO_INST_NEW (cfg
, vtarg
, OP_MOVE
);
3775 vtarg
->sreg1
= vt_reg
;
3776 vtarg
->dreg
= mono_regstate_next_int (cfg
->rs
);
3777 mono_bblock_add_inst (cfg
->cbb
, vtarg
);
3779 mono_call_inst_add_outarg_reg (cfg
, call
, vtarg
->dreg
, cinfo
->ret
.reg
, FALSE
);
3782 MONO_INST_NEW (cfg
, vtarg
, OP_OUTARG
);
3783 vtarg
->type
= STACK_MP
;
3784 vtarg
->sreg1
= vt_reg
;
3785 mono_bblock_add_inst (cfg
->cbb
, vtarg
);
3793 mono_arch_get_inst_for_method (MonoCompile
*cfg
, MonoMethod
*cmethod
, MonoMethodSignature
*fsig
, MonoInst
**args
)
3795 MonoInst
*ins
= NULL
;
3797 if (cmethod
->klass
== mono_defaults
.math_class
) {
3798 if (strcmp (cmethod
->name
, "Sin") == 0) {
3799 MONO_INST_NEW (cfg
, ins
, OP_SIN
);
3800 ins
->inst_i0
= args
[0];
3801 } else if (strcmp (cmethod
->name
, "Cos") == 0) {
3802 MONO_INST_NEW (cfg
, ins
, OP_COS
);
3803 ins
->inst_i0
= args
[0];
3804 } else if (strcmp (cmethod
->name
, "Tan") == 0) {
3805 MONO_INST_NEW (cfg
, ins
, OP_TAN
);
3806 ins
->inst_i0
= args
[0];
3807 } else if (strcmp (cmethod
->name
, "Atan") == 0) {
3808 MONO_INST_NEW (cfg
, ins
, OP_ATAN
);
3809 ins
->inst_i0
= args
[0];
3810 } else if (strcmp (cmethod
->name
, "Sqrt") == 0) {
3811 MONO_INST_NEW (cfg
, ins
, OP_SQRT
);
3812 ins
->inst_i0
= args
[0];
3813 } else if (strcmp (cmethod
->name
, "Abs") == 0 && fsig
->params
[0]->type
== MONO_TYPE_R8
) {
3814 MONO_INST_NEW (cfg
, ins
, OP_ABS
);
3815 ins
->inst_i0
= args
[0];
3818 /* OP_FREM is not IEEE compatible */
3819 else if (strcmp (cmethod
->name
, "IEEERemainder") == 0) {
3820 MONO_INST_NEW (cfg
, ins
, OP_FREM
);
3821 ins
->inst_i0
= args
[0];
3822 ins
->inst_i1
= args
[1];
3825 } else if (cmethod
->klass
== mono_defaults
.thread_class
&&
3826 strcmp (cmethod
->name
, "MemoryBarrier") == 0) {
3827 MONO_INST_NEW (cfg
, ins
, OP_MEMORY_BARRIER
);
3828 } else if(cmethod
->klass
->image
== mono_defaults
.corlib
&&
3829 (strcmp (cmethod
->klass
->name_space
, "System.Threading") == 0) &&
3830 (strcmp (cmethod
->klass
->name
, "Interlocked") == 0)) {
3832 if (strcmp (cmethod
->name
, "Increment") == 0 && fsig
->params
[0]->type
== MONO_TYPE_I4
) {
3833 MonoInst
*ins_iconst
;
3835 MONO_INST_NEW (cfg
, ins
, OP_ATOMIC_ADD_NEW_I4
);
3836 MONO_INST_NEW (cfg
, ins_iconst
, OP_ICONST
);
3837 ins_iconst
->inst_c0
= 1;
3839 ins
->inst_i0
= args
[0];
3840 ins
->inst_i1
= ins_iconst
;
3841 } else if (strcmp (cmethod
->name
, "Decrement") == 0 && fsig
->params
[0]->type
== MONO_TYPE_I4
) {
3842 MonoInst
*ins_iconst
;
3844 MONO_INST_NEW (cfg
, ins
, OP_ATOMIC_ADD_NEW_I4
);
3845 MONO_INST_NEW (cfg
, ins_iconst
, OP_ICONST
);
3846 ins_iconst
->inst_c0
= -1;
3848 ins
->inst_i0
= args
[0];
3849 ins
->inst_i1
= ins_iconst
;
3850 } else if (strcmp (cmethod
->name
, "Exchange") == 0 && fsig
->params
[0]->type
== MONO_TYPE_I4
) {
3851 MONO_INST_NEW (cfg
, ins
, OP_ATOMIC_EXCHANGE_I4
);
3853 ins
->inst_i0
= args
[0];
3854 ins
->inst_i1
= args
[1];
3855 } else if (strcmp (cmethod
->name
, "Add") == 0 && fsig
->params
[0]->type
== MONO_TYPE_I4
) {
3856 MONO_INST_NEW (cfg
, ins
, OP_ATOMIC_ADD_NEW_I4
);
3858 ins
->inst_i0
= args
[0];
3859 ins
->inst_i1
= args
[1];
3868 mono_arch_print_tree (MonoInst
*tree
, int arity
)
3873 MonoInst
* mono_arch_get_domain_intrinsic (MonoCompile
* cfg
)
3877 if (appdomain_tls_offset
== -1)
3880 MONO_INST_NEW (cfg
, ins
, OP_TLS_GET
);
3881 ins
->inst_offset
= appdomain_tls_offset
;
3885 MonoInst
* mono_arch_get_thread_intrinsic (MonoCompile
* cfg
)
3889 if (thread_tls_offset
== -1)
3892 MONO_INST_NEW (cfg
, ins
, OP_TLS_GET
);
3893 ins
->inst_offset
= thread_tls_offset
;
3898 mono_arch_get_patch_offset (guint8
*code
)
3900 if ((code
[0] == 0x8b) && (x86_modrm_mod (code
[1]) == 0x2))
3902 else if ((code
[0] == 0xba))
3904 else if ((code
[0] == 0x68))
3907 else if ((code
[0] == 0xff) && (x86_modrm_reg (code
[1]) == 0x6))
3908 /* push <OFFSET>(<REG>) */
3910 else if ((code
[0] == 0xff) && (x86_modrm_reg (code
[1]) == 0x2))
3911 /* call *<OFFSET>(<REG>) */
3913 else if ((code
[0] == 0xdd) || (code
[0] == 0xd9))
3916 else if ((code
[0] == 0x58) && (code
[1] == 0x05))
3917 /* pop %eax; add <OFFSET>, %eax */
3919 else if ((code
[0] >= 0x58) && (code
[0] <= 0x58 + X86_NREG
) && (code
[1] == 0x81))
3920 /* pop <REG>; add <OFFSET>, <REG> */
3923 g_assert_not_reached ();
3929 mono_arch_get_vcall_slot_addr (guint8
*code
, gpointer
*regs
)
3934 /* go to the start of the call instruction
3936 * address_byte = (m << 6) | (o << 3) | reg
3937 * call opcode: 0xff address_byte displacement
3939 * 0xff m=2,o=2 imm32
3944 * A given byte sequence can match more than case here, so we have to be
3945 * really careful about the ordering of the cases. Longer sequences
3948 if ((code
[-2] == 0x8b) && (x86_modrm_mod (code
[-1]) == 0x2) && (code
[4] == 0xff) && (x86_modrm_reg (code
[5]) == 0x2) && (x86_modrm_mod (code
[5]) == 0x0)) {
3950 * This is an interface call
3951 * 8b 80 0c e8 ff ff mov 0xffffe80c(%eax),%eax
3952 * ff 10 call *(%eax)
3954 reg
= x86_modrm_rm (code
[5]);
3956 } else if ((code
[1] != 0xe8) && (code
[3] == 0xff) && ((code
[4] & 0x18) == 0x10) && ((code
[4] >> 6) == 1)) {
3957 reg
= code
[4] & 0x07;
3958 disp
= (signed char)code
[5];
3960 if ((code
[0] == 0xff) && ((code
[1] & 0x18) == 0x10) && ((code
[1] >> 6) == 2)) {
3961 reg
= code
[1] & 0x07;
3962 disp
= *((gint32
*)(code
+ 2));
3963 } else if ((code
[1] == 0xe8)) {
3965 } else if ((code
[4] == 0xff) && (((code
[5] >> 6) & 0x3) == 0) && (((code
[5] >> 3) & 0x7) == 2)) {
3967 * This is a interface call
3968 * 8b 40 30 mov 0x30(%eax),%eax
3969 * ff 10 call *(%eax)
3972 reg
= code
[5] & 0x07;
3978 return (gpointer
*)(((gint32
)(regs
[reg
])) + disp
);
3982 mono_arch_get_delegate_method_ptr_addr (guint8
* code
, gpointer
*regs
)
3988 if ((code
[0] == 0x8b) && (x86_modrm_mod (code
[1]) == 3) && (x86_modrm_reg (code
[1]) == X86_EAX
) && (code
[2] == 0x8b) && (code
[3] == 0x40) && (code
[5] == 0xff) && (code
[6] == 0xd0)) {
3989 reg
= x86_modrm_rm (code
[1]);
3995 return (gpointer
*)(((gint32
)(regs
[reg
])) + disp
);