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.
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/debug-helpers.h>
17 #include <mono/metadata/threads.h>
18 #include <mono/metadata/profiler-private.h>
19 #include <mono/utils/mono-math.h>
24 #include "cpu-pentium.h"
26 static gint lmf_tls_offset = -1;
27 static gint appdomain_tls_offset = -1;
28 static gint thread_tls_offset = -1;
30 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
33 /* Under windows, the default pinvoke calling convention is stdcall */
34 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
36 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
39 #define SIGNAL_STACK_SIZE (64 * 1024)
41 #define NOT_IMPLEMENTED g_assert_not_reached ()
44 mono_arch_regname (int reg) {
46 case X86_EAX: return "%eax";
47 case X86_EBX: return "%ebx";
48 case X86_ECX: return "%ecx";
49 case X86_EDX: return "%edx";
50 case X86_ESP: return "%esp"; case X86_EBP: return "%ebp";
51 case X86_EDI: return "%edi";
52 case X86_ESI: return "%esi";
73 /* Only if storage == ArgValuetypeInReg */
74 ArgStorage pair_storage [2];
83 gboolean need_stack_align;
91 #define FLOAT_PARAM_REGS 0
93 static X86_Reg_No param_regs [] = { };
96 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
100 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
102 ainfo->offset = *stack_size;
104 if (*gr >= PARAM_REGS) {
105 ainfo->storage = ArgOnStack;
106 (*stack_size) += sizeof (gpointer);
109 ainfo->storage = ArgInIReg;
110 ainfo->reg = param_regs [*gr];
116 add_general_pair (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
118 ainfo->offset = *stack_size;
120 g_assert (PARAM_REGS == 0);
122 ainfo->storage = ArgOnStack;
123 (*stack_size) += sizeof (gpointer) * 2;
127 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
129 ainfo->offset = *stack_size;
131 if (*gr >= FLOAT_PARAM_REGS) {
132 ainfo->storage = ArgOnStack;
133 (*stack_size) += sizeof (gpointer);
136 /* A double register */
138 ainfo->storage = ArgInDoubleSSEReg;
140 ainfo->storage = ArgInFloatSSEReg;
148 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
150 guint32 *gr, guint32 *fr, guint32 *stack_size)
155 klass = mono_class_from_mono_type (type);
157 size = mono_type_native_stack_size (&klass->byval_arg, NULL);
159 size = mono_type_stack_size (&klass->byval_arg, NULL);
161 #ifdef PLATFORM_WIN32
162 if (sig->pinvoke && is_return) {
163 MonoMarshalType *info;
166 * the exact rules are not very well documented, the code below seems to work with the
167 * code generated by gcc 3.3.3 -mno-cygwin.
169 info = mono_marshal_load_type_info (klass);
172 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
174 /* Special case structs with only a float member */
175 if ((info->native_size == 8) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R8)) {
176 ainfo->storage = ArgValuetypeInReg;
177 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
180 if ((info->native_size == 4) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R4)) {
181 ainfo->storage = ArgValuetypeInReg;
182 ainfo->pair_storage [0] = ArgOnFloatFpStack;
185 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
186 ainfo->storage = ArgValuetypeInReg;
187 ainfo->pair_storage [0] = ArgInIReg;
188 ainfo->pair_regs [0] = return_regs [0];
189 if (info->native_size > 4) {
190 ainfo->pair_storage [1] = ArgInIReg;
191 ainfo->pair_regs [1] = return_regs [1];
198 ainfo->offset = *stack_size;
199 ainfo->storage = ArgOnStack;
200 *stack_size += ALIGN_TO (size, sizeof (gpointer));
206 * Obtain information about a call according to the calling convention.
207 * For x86 ELF, see the "System V Application Binary Interface Intel386
208 * Architecture Processor Supplment, Fourth Edition" document for more
210 * For x86 win32, see ???.
213 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
217 int n = sig->hasthis + sig->param_count;
218 guint32 stack_size = 0;
221 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
228 ret_type = mono_type_get_underlying_type (sig->ret);
229 switch (ret_type->type) {
230 case MONO_TYPE_BOOLEAN:
241 case MONO_TYPE_CLASS:
242 case MONO_TYPE_OBJECT:
243 case MONO_TYPE_SZARRAY:
244 case MONO_TYPE_ARRAY:
245 case MONO_TYPE_STRING:
246 cinfo->ret.storage = ArgInIReg;
247 cinfo->ret.reg = X86_EAX;
251 cinfo->ret.storage = ArgInIReg;
252 cinfo->ret.reg = X86_EAX;
255 cinfo->ret.storage = ArgOnFloatFpStack;
258 cinfo->ret.storage = ArgOnDoubleFpStack;
260 case MONO_TYPE_VALUETYPE: {
261 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
263 add_valuetype (sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
264 if (cinfo->ret.storage == ArgOnStack)
265 /* The caller passes the address where the value is stored */
266 add_general (&gr, &stack_size, &cinfo->ret);
269 case MONO_TYPE_TYPEDBYREF:
270 /* Same as a valuetype with size 24 */
271 add_general (&gr, &stack_size, &cinfo->ret);
275 cinfo->ret.storage = ArgNone;
278 g_error ("Can't handle as return value 0x%x", sig->ret->type);
284 add_general (&gr, &stack_size, cinfo->args + 0);
286 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
288 fr = FLOAT_PARAM_REGS;
290 /* Emit the signature cookie just before the implicit arguments */
291 add_general (&gr, &stack_size, &cinfo->sig_cookie);
294 for (i = 0; i < sig->param_count; ++i) {
295 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
298 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
299 /* We allways pass the sig cookie on the stack for simplicity */
301 * Prevent implicit arguments + the sig cookie from being passed
305 fr = FLOAT_PARAM_REGS;
307 /* Emit the signature cookie just before the implicit arguments */
308 add_general (&gr, &stack_size, &cinfo->sig_cookie);
311 if (sig->params [i]->byref) {
312 add_general (&gr, &stack_size, ainfo);
315 ptype = mono_type_get_underlying_type (sig->params [i]);
316 switch (ptype->type) {
317 case MONO_TYPE_BOOLEAN:
320 add_general (&gr, &stack_size, ainfo);
325 add_general (&gr, &stack_size, ainfo);
329 add_general (&gr, &stack_size, ainfo);
334 case MONO_TYPE_CLASS:
335 case MONO_TYPE_OBJECT:
336 case MONO_TYPE_STRING:
337 case MONO_TYPE_SZARRAY:
338 case MONO_TYPE_ARRAY:
339 add_general (&gr, &stack_size, ainfo);
341 case MONO_TYPE_VALUETYPE:
342 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
344 case MONO_TYPE_TYPEDBYREF:
345 stack_size += sizeof (MonoTypedRef);
346 ainfo->storage = ArgOnStack;
350 add_general_pair (&gr, &stack_size, ainfo);
353 add_float (&fr, &stack_size, ainfo, FALSE);
356 add_float (&fr, &stack_size, ainfo, TRUE);
359 g_assert_not_reached ();
363 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
365 fr = FLOAT_PARAM_REGS;
367 /* Emit the signature cookie just before the implicit arguments */
368 add_general (&gr, &stack_size, &cinfo->sig_cookie);
371 cinfo->stack_usage = stack_size;
372 cinfo->reg_usage = gr;
373 cinfo->freg_usage = fr;
378 * mono_arch_get_argument_info:
379 * @csig: a method signature
380 * @param_count: the number of parameters to consider
381 * @arg_info: an array to store the result infos
383 * Gathers information on parameters such as size, alignment and
384 * padding. arg_info should be large enought to hold param_count + 1 entries.
386 * Returns the size of the activation frame.
389 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
391 int k, frame_size = 0;
392 int size, align, pad;
396 cinfo = get_call_info (csig, FALSE);
398 if (MONO_TYPE_ISSTRUCT (csig->ret) && (cinfo->ret.storage == ArgOnStack)) {
399 frame_size += sizeof (gpointer);
403 arg_info [0].offset = offset;
406 frame_size += sizeof (gpointer);
410 arg_info [0].size = frame_size;
412 for (k = 0; k < param_count; k++) {
415 size = mono_type_native_stack_size (csig->params [k], &align);
417 size = mono_type_stack_size (csig->params [k], &align);
419 /* ignore alignment for now */
422 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
423 arg_info [k].pad = pad;
425 arg_info [k + 1].pad = 0;
426 arg_info [k + 1].size = size;
428 arg_info [k + 1].offset = offset;
432 align = MONO_ARCH_FRAME_ALIGNMENT;
433 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
434 arg_info [k].pad = pad;
441 static const guchar cpuid_impl [] = {
442 0x55, /* push %ebp */
443 0x89, 0xe5, /* mov %esp,%ebp */
444 0x53, /* push %ebx */
445 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
446 0x0f, 0xa2, /* cpuid */
447 0x50, /* push %eax */
448 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
449 0x89, 0x18, /* mov %ebx,(%eax) */
450 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
451 0x89, 0x08, /* mov %ecx,(%eax) */
452 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
453 0x89, 0x10, /* mov %edx,(%eax) */
455 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
456 0x89, 0x02, /* mov %eax,(%edx) */
462 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
465 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
468 __asm__ __volatile__ (
471 "movl %%eax, %%edx\n"
472 "xorl $0x200000, %%eax\n"
477 "xorl %%edx, %%eax\n"
478 "andl $0x200000, %%eax\n"
486 /* Have to use the code manager to get around WinXP DEP */
487 MonoCodeManager *codeman = mono_code_manager_new_dynamic ();
489 void *ptr = mono_code_manager_reserve (codeman, sizeof (cpuid_impl));
490 memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
492 func = (CpuidFunc)ptr;
493 func (id, p_eax, p_ebx, p_ecx, p_edx);
495 mono_code_manager_destroy (codeman);
498 * We use this approach because of issues with gcc and pic code, see:
499 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
500 __asm__ __volatile__ ("cpuid"
501 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
510 * Initialize the cpu to execute managed code.
513 mono_arch_cpu_init (void)
517 /* spec compliance requires running with double precision */
518 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
519 fpcw &= ~X86_FPCW_PRECC_MASK;
520 fpcw |= X86_FPCW_PREC_DOUBLE;
521 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
522 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
524 mono_x86_tramp_init ();
528 * This function returns the optimizations supported on this cpu.
531 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
533 int eax, ebx, ecx, edx;
537 /* Feature Flags function, flags returned in EDX. */
538 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
539 if (edx & (1 << 15)) {
540 opts |= MONO_OPT_CMOV;
542 opts |= MONO_OPT_FCMOV;
544 *exclude_mask |= MONO_OPT_FCMOV;
546 *exclude_mask |= MONO_OPT_CMOV;
552 * Determine whenever the trap whose info is in SIGINFO is caused by
556 mono_arch_is_int_overflow (void *sigctx, void *info)
558 struct sigcontext *ctx = (struct sigcontext*)sigctx;
561 ip = (guint8*)ctx->SC_EIP;
563 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
567 switch (x86_modrm_rm (ip [1])) {
575 g_assert_not_reached ();
587 is_regsize_var (MonoType *t) {
590 switch (mono_type_get_underlying_type (t)->type) {
597 case MONO_TYPE_OBJECT:
598 case MONO_TYPE_STRING:
599 case MONO_TYPE_CLASS:
600 case MONO_TYPE_SZARRAY:
601 case MONO_TYPE_ARRAY:
603 case MONO_TYPE_VALUETYPE:
610 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
615 for (i = 0; i < cfg->num_varinfo; i++) {
616 MonoInst *ins = cfg->varinfo [i];
617 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
620 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
623 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
624 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
627 /* we dont allocate I1 to registers because there is no simply way to sign extend
628 * 8bit quantities in caller saved registers on x86 */
629 if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) ||
630 (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
631 (ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
632 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
633 g_assert (i == vmv->idx);
634 vars = g_list_prepend (vars, vmv);
638 vars = mono_varlist_sort (cfg, vars, 0);
644 mono_arch_get_global_int_regs (MonoCompile *cfg)
648 /* we can use 3 registers for global allocation */
649 regs = g_list_prepend (regs, (gpointer)X86_EBX);
650 regs = g_list_prepend (regs, (gpointer)X86_ESI);
651 regs = g_list_prepend (regs, (gpointer)X86_EDI);
657 * mono_arch_regalloc_cost:
659 * Return the cost, in number of memory references, of the action of
660 * allocating the variable VMV into a register during global register
664 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
666 MonoInst *ins = cfg->varinfo [vmv->idx];
668 if (cfg->method->save_lmf)
669 /* The register is already saved */
670 return (ins->opcode == OP_ARG) ? 1 : 0;
672 /* push+pop+possible load if it is an argument */
673 return (ins->opcode == OP_ARG) ? 3 : 2;
677 * Set var information according to the calling convention. X86 version.
678 * The locals var stuff should most likely be split in another method.
681 mono_arch_allocate_vars (MonoCompile *m)
683 MonoMethodSignature *sig;
684 MonoMethodHeader *header;
686 guint32 locals_stack_size, locals_stack_align;
687 int i, offset, curinst, size, align;
691 header = mono_method_get_header (m->method);
692 sig = mono_method_signature (m->method);
697 cinfo = get_call_info (sig, FALSE);
699 switch (cinfo->ret.storage) {
701 m->ret->opcode = OP_REGOFFSET;
702 m->ret->inst_basereg = X86_EBP;
703 m->ret->inst_offset = offset;
704 offset += sizeof (gpointer);
706 case ArgValuetypeInReg:
709 m->ret->opcode = OP_REGVAR;
710 m->ret->inst_c0 = cinfo->ret.reg;
713 case ArgOnFloatFpStack:
714 case ArgOnDoubleFpStack:
717 g_assert_not_reached ();
721 inst = m->varinfo [curinst];
722 if (inst->opcode != OP_REGVAR) {
723 inst->opcode = OP_REGOFFSET;
724 inst->inst_basereg = X86_EBP;
726 inst->inst_offset = offset;
727 offset += sizeof (gpointer);
731 if (sig->call_convention == MONO_CALL_VARARG) {
732 m->sig_cookie = offset;
733 offset += sizeof (gpointer);
736 for (i = 0; i < sig->param_count; ++i) {
737 inst = m->varinfo [curinst];
738 if (inst->opcode != OP_REGVAR) {
739 inst->opcode = OP_REGOFFSET;
740 inst->inst_basereg = X86_EBP;
742 inst->inst_offset = offset;
743 size = mono_type_size (sig->params [i], &align);
752 /* reserve space to save LMF and caller saved registers */
754 if (m->method->save_lmf) {
755 offset += sizeof (MonoLMF);
757 if (m->used_int_regs & (1 << X86_EBX)) {
761 if (m->used_int_regs & (1 << X86_EDI)) {
765 if (m->used_int_regs & (1 << X86_ESI)) {
770 switch (cinfo->ret.storage) {
771 case ArgValuetypeInReg:
772 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
774 m->ret->opcode = OP_REGOFFSET;
775 m->ret->inst_basereg = X86_EBP;
776 m->ret->inst_offset = - offset;
782 /* Allocate locals */
783 offsets = mono_allocate_stack_slots (m, &locals_stack_size, &locals_stack_align);
784 if (locals_stack_align) {
785 offset += (locals_stack_align - 1);
786 offset &= ~(locals_stack_align - 1);
788 for (i = m->locals_start; i < m->num_varinfo; i++) {
789 if (offsets [i] != -1) {
790 MonoInst *inst = m->varinfo [i];
791 inst->opcode = OP_REGOFFSET;
792 inst->inst_basereg = X86_EBP;
793 inst->inst_offset = - (offset + offsets [i]);
794 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
798 offset += locals_stack_size;
800 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
801 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
806 m->stack_offset = -offset;
810 mono_arch_create_vars (MonoCompile *cfg)
812 MonoMethodSignature *sig;
815 sig = mono_method_signature (cfg->method);
817 cinfo = get_call_info (sig, FALSE);
819 if (cinfo->ret.storage == ArgValuetypeInReg)
820 cfg->ret_var_is_local = TRUE;
825 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
826 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
830 * take the arguments and generate the arch-specific
831 * instructions to properly call the function in call.
832 * This includes pushing, moving arguments to the right register
834 * Issue: who does the spilling if needed, and when?
837 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
839 MonoMethodSignature *sig;
840 int i, n, stack_size, type;
845 /* add the vararg cookie before the non-implicit args */
846 if (call->signature->call_convention == MONO_CALL_VARARG) {
848 /* FIXME: Add support for signature tokens to AOT */
849 cfg->disable_aot = TRUE;
850 MONO_INST_NEW (cfg, arg, OP_OUTARG);
851 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
852 sig_arg->inst_p0 = call->signature;
853 arg->inst_left = sig_arg;
854 arg->type = STACK_PTR;
855 /* prepend, so they get reversed */
856 arg->next = call->out_args;
857 call->out_args = arg;
858 stack_size += sizeof (gpointer);
860 sig = call->signature;
861 n = sig->param_count + sig->hasthis;
863 cinfo = get_call_info (sig, FALSE);
865 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
866 if (cinfo->ret.storage == ArgOnStack)
867 stack_size += sizeof (gpointer);
870 for (i = 0; i < n; ++i) {
871 if (is_virtual && i == 0) {
872 /* the argument will be attached to the call instrucion */
876 MONO_INST_NEW (cfg, arg, OP_OUTARG);
878 arg->cil_code = in->cil_code;
880 arg->type = in->type;
881 /* prepend, so they get reversed */
882 arg->next = call->out_args;
883 call->out_args = arg;
884 if (i >= sig->hasthis) {
885 MonoType *t = sig->params [i - sig->hasthis];
886 ptype = mono_type_get_underlying_type (t);
891 /* FIXME: validate arguments... */
895 case MONO_TYPE_BOOLEAN:
903 case MONO_TYPE_STRING:
904 case MONO_TYPE_CLASS:
905 case MONO_TYPE_OBJECT:
907 case MONO_TYPE_FNPTR:
908 case MONO_TYPE_ARRAY:
909 case MONO_TYPE_SZARRAY:
918 arg->opcode = OP_OUTARG_R4;
922 arg->opcode = OP_OUTARG_R8;
924 case MONO_TYPE_VALUETYPE: {
927 size = mono_type_native_stack_size (&in->klass->byval_arg, NULL);
929 size = mono_type_stack_size (&in->klass->byval_arg, NULL);
932 arg->opcode = OP_OUTARG_VT;
933 arg->klass = in->klass;
934 arg->unused = sig->pinvoke;
935 arg->inst_imm = size;
938 case MONO_TYPE_TYPEDBYREF:
939 stack_size += sizeof (MonoTypedRef);
940 arg->opcode = OP_OUTARG_VT;
941 arg->klass = in->klass;
942 arg->unused = sig->pinvoke;
943 arg->inst_imm = sizeof (MonoTypedRef);
946 g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
949 /* the this argument */
955 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
956 if (cinfo->ret.storage == ArgValuetypeInReg) {
959 * After the call, the struct is in registers, but needs to be saved to the memory pointed
960 * to by vt_arg in this_vret_args. This means that vt_arg needs to be saved somewhere
961 * before calling the function. So we add a dummy instruction to represent pushing the
962 * struct return address to the stack. The return address will be saved to this stack slot
963 * by the code emitted in this_vret_args.
965 MONO_INST_NEW (cfg, arg, OP_OUTARG);
966 MONO_INST_NEW (cfg, zero_inst, OP_ICONST);
967 zero_inst->inst_p0 = 0;
968 arg->inst_left = zero_inst;
969 arg->type = STACK_PTR;
970 /* prepend, so they get reversed */
971 arg->next = call->out_args;
972 call->out_args = arg;
975 /* if the function returns a struct, the called method already does a ret $0x4 */
976 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
980 call->stack_usage = stack_size;
984 * should set more info in call, such as the stack space
985 * used by the args that needs to be added back to esp
992 * Allow tracing to work with this interface (with an optional argument)
995 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
999 /* if some args are passed in registers, we need to save them here */
1000 x86_push_reg (code, X86_EBP);
1002 if (cfg->compile_aot) {
1003 x86_push_imm (code, cfg->method);
1004 x86_mov_reg_imm (code, X86_EAX, func);
1005 x86_call_reg (code, X86_EAX);
1007 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1008 x86_push_imm (code, cfg->method);
1009 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1010 x86_call_code (code, 0);
1012 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1026 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1029 int arg_size = 0, save_mode = SAVE_NONE;
1030 MonoMethod *method = cfg->method;
1032 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
1033 case MONO_TYPE_VOID:
1034 /* special case string .ctor icall */
1035 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1036 save_mode = SAVE_EAX;
1038 save_mode = SAVE_NONE;
1042 save_mode = SAVE_EAX_EDX;
1046 save_mode = SAVE_FP;
1048 case MONO_TYPE_VALUETYPE:
1049 save_mode = SAVE_STRUCT;
1052 save_mode = SAVE_EAX;
1056 switch (save_mode) {
1058 x86_push_reg (code, X86_EDX);
1059 x86_push_reg (code, X86_EAX);
1060 if (enable_arguments) {
1061 x86_push_reg (code, X86_EDX);
1062 x86_push_reg (code, X86_EAX);
1067 x86_push_reg (code, X86_EAX);
1068 if (enable_arguments) {
1069 x86_push_reg (code, X86_EAX);
1074 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1075 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1076 if (enable_arguments) {
1077 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1078 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1083 if (enable_arguments) {
1084 x86_push_membase (code, X86_EBP, 8);
1093 if (cfg->compile_aot) {
1094 x86_push_imm (code, method);
1095 x86_mov_reg_imm (code, X86_EAX, func);
1096 x86_call_reg (code, X86_EAX);
1098 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1099 x86_push_imm (code, method);
1100 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1101 x86_call_code (code, 0);
1103 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1105 switch (save_mode) {
1107 x86_pop_reg (code, X86_EAX);
1108 x86_pop_reg (code, X86_EDX);
1111 x86_pop_reg (code, X86_EAX);
1114 x86_fld_membase (code, X86_ESP, 0, TRUE);
1115 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1125 #define EMIT_COND_BRANCH(ins,cond,sign) \
1126 if (ins->flags & MONO_INST_BRLABEL) { \
1127 if (ins->inst_i0->inst_c0) { \
1128 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
1130 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1131 if ((cfg->opt & MONO_OPT_BRANCH) && \
1132 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
1133 x86_branch8 (code, cond, 0, sign); \
1135 x86_branch32 (code, cond, 0, sign); \
1138 if (ins->inst_true_bb->native_offset) { \
1139 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1141 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1142 if ((cfg->opt & MONO_OPT_BRANCH) && \
1143 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1144 x86_branch8 (code, cond, 0, sign); \
1146 x86_branch32 (code, cond, 0, sign); \
1150 /* emit an exception if condition is fail */
1151 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1153 mono_add_patch_info (cfg, code - cfg->native_code, \
1154 MONO_PATCH_INFO_EXC, exc_name); \
1155 x86_branch32 (code, cond, 0, signed); \
1158 #define EMIT_FPCOMPARE(code) do { \
1159 x86_fcompp (code); \
1160 x86_fnstsw (code); \
1165 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1167 if (cfg->compile_aot) {
1168 guint32 got_reg = X86_EAX;
1170 if (cfg->compile_aot) {
1172 * Since the patches are generated by the back end, there is
1173 * no way to generate a got_var at this point.
1175 g_assert (cfg->got_var);
1177 if (cfg->got_var->opcode == OP_REGOFFSET)
1178 x86_mov_reg_membase (code, X86_EAX, cfg->got_var->inst_basereg, cfg->got_var->inst_offset, 4);
1180 got_reg = cfg->got_var->dreg;
1183 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1184 x86_call_membase (code, got_reg, 0xf0f0f0f0);
1187 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1188 x86_call_code (code, 0);
1194 /* FIXME: Add more instructions */
1195 #define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_STORE_MEMBASE_IMM) || ((ins)->opcode == OP_STOREI4_MEMBASE_REG))
1198 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1200 MonoInst *ins, *last_ins = NULL;
1205 switch (ins->opcode) {
1207 /* reg = 0 -> XOR (reg, reg) */
1208 /* XOR sets cflags on x86, so we cant do it always */
1209 if (ins->inst_c0 == 0 && ins->next && INST_IGNORES_CFLAGS (ins->next)) {
1210 ins->opcode = CEE_XOR;
1211 ins->sreg1 = ins->dreg;
1212 ins->sreg2 = ins->dreg;
1216 /* remove unnecessary multiplication with 1 */
1217 if (ins->inst_imm == 1) {
1218 if (ins->dreg != ins->sreg1) {
1219 ins->opcode = OP_MOVE;
1221 last_ins->next = ins->next;
1227 case OP_COMPARE_IMM:
1228 /* OP_COMPARE_IMM (reg, 0)
1230 * OP_X86_TEST_NULL (reg)
1233 ins->opcode = OP_X86_TEST_NULL;
1235 case OP_X86_COMPARE_MEMBASE_IMM:
1237 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1238 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1240 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1241 * OP_COMPARE_IMM reg, imm
1243 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1245 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1246 ins->inst_basereg == last_ins->inst_destbasereg &&
1247 ins->inst_offset == last_ins->inst_offset) {
1248 ins->opcode = OP_COMPARE_IMM;
1249 ins->sreg1 = last_ins->sreg1;
1251 /* check if we can remove cmp reg,0 with test null */
1253 ins->opcode = OP_X86_TEST_NULL;
1257 case OP_LOAD_MEMBASE:
1258 case OP_LOADI4_MEMBASE:
1260 * Note: if reg1 = reg2 the load op is removed
1262 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1263 * OP_LOAD_MEMBASE offset(basereg), reg2
1265 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1266 * OP_MOVE reg1, reg2
1268 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1269 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1270 ins->inst_basereg == last_ins->inst_destbasereg &&
1271 ins->inst_offset == last_ins->inst_offset) {
1272 if (ins->dreg == last_ins->sreg1) {
1273 last_ins->next = ins->next;
1277 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1278 ins->opcode = OP_MOVE;
1279 ins->sreg1 = last_ins->sreg1;
1283 * Note: reg1 must be different from the basereg in the second load
1284 * Note: if reg1 = reg2 is equal then second load is removed
1286 * OP_LOAD_MEMBASE offset(basereg), reg1
1287 * OP_LOAD_MEMBASE offset(basereg), reg2
1289 * OP_LOAD_MEMBASE offset(basereg), reg1
1290 * OP_MOVE reg1, reg2
1292 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1293 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1294 ins->inst_basereg != last_ins->dreg &&
1295 ins->inst_basereg == last_ins->inst_basereg &&
1296 ins->inst_offset == last_ins->inst_offset) {
1298 if (ins->dreg == last_ins->dreg) {
1299 last_ins->next = ins->next;
1303 ins->opcode = OP_MOVE;
1304 ins->sreg1 = last_ins->dreg;
1307 //g_assert_not_reached ();
1311 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1312 * OP_LOAD_MEMBASE offset(basereg), reg
1314 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1315 * OP_ICONST reg, imm
1317 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1318 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1319 ins->inst_basereg == last_ins->inst_destbasereg &&
1320 ins->inst_offset == last_ins->inst_offset) {
1321 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1322 ins->opcode = OP_ICONST;
1323 ins->inst_c0 = last_ins->inst_imm;
1324 g_assert_not_reached (); // check this rule
1328 case OP_LOADU1_MEMBASE:
1329 case OP_LOADI1_MEMBASE:
1331 * Note: if reg1 = reg2 the load op is removed
1333 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1334 * OP_LOAD_MEMBASE offset(basereg), reg2
1336 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1337 * OP_MOVE reg1, reg2
1339 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1340 ins->inst_basereg == last_ins->inst_destbasereg &&
1341 ins->inst_offset == last_ins->inst_offset) {
1342 if (ins->dreg == last_ins->sreg1) {
1343 last_ins->next = ins->next;
1347 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1348 ins->opcode = OP_MOVE;
1349 ins->sreg1 = last_ins->sreg1;
1353 case OP_LOADU2_MEMBASE:
1354 case OP_LOADI2_MEMBASE:
1356 * Note: if reg1 = reg2 the load op is removed
1358 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1359 * OP_LOAD_MEMBASE offset(basereg), reg2
1361 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1362 * OP_MOVE reg1, reg2
1364 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1365 ins->inst_basereg == last_ins->inst_destbasereg &&
1366 ins->inst_offset == last_ins->inst_offset) {
1367 if (ins->dreg == last_ins->sreg1) {
1368 last_ins->next = ins->next;
1372 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1373 ins->opcode = OP_MOVE;
1374 ins->sreg1 = last_ins->sreg1;
1386 if (ins->dreg == ins->sreg1) {
1388 last_ins->next = ins->next;
1395 * OP_MOVE sreg, dreg
1396 * OP_MOVE dreg, sreg
1398 if (last_ins && last_ins->opcode == OP_MOVE &&
1399 ins->sreg1 == last_ins->dreg &&
1400 ins->dreg == last_ins->sreg1) {
1401 last_ins->next = ins->next;
1407 case OP_X86_PUSH_MEMBASE:
1408 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1409 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1410 ins->inst_basereg == last_ins->inst_destbasereg &&
1411 ins->inst_offset == last_ins->inst_offset) {
1412 ins->opcode = OP_X86_PUSH;
1413 ins->sreg1 = last_ins->sreg1;
1420 bb->last_ins = last_ins;
1424 branch_cc_table [] = {
1425 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1426 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1427 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1430 #define DEBUG(a) if (cfg->verbose_level > 1) a
1434 * returns the offset used by spillvar. It allocates a new
1435 * spill variable if necessary.
1438 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1440 MonoSpillInfo **si, *info;
1443 si = &cfg->spill_info;
1445 while (i <= spillvar) {
1448 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1450 cfg->stack_offset -= sizeof (gpointer);
1451 info->offset = cfg->stack_offset;
1455 return (*si)->offset;
1461 g_assert_not_reached ();
1466 * returns the offset used by spillvar. It allocates a new
1467 * spill float variable if necessary.
1468 * (same as mono_spillvar_offset but for float)
1471 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1473 MonoSpillInfo **si, *info;
1476 si = &cfg->spill_info_float;
1478 while (i <= spillvar) {
1481 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1483 cfg->stack_offset -= sizeof (double);
1484 info->offset = cfg->stack_offset;
1488 return (*si)->offset;
1494 g_assert_not_reached ();
1499 * Creates a store for spilled floating point items
1502 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, MonoInst *ins)
1505 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1507 store->inst_destbasereg = X86_EBP;
1508 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1510 DEBUG (g_print ("SPILLED FLOAT STORE (%d at 0x%08x(%%sp)) (from %d)\n", spill, store->inst_offset, reg));
1515 * Creates a load for spilled floating point items
1518 create_spilled_load_float (MonoCompile *cfg, int spill, int reg, MonoInst *ins)
1521 MONO_INST_NEW (cfg, load, OP_LOADR8_SPILL_MEMBASE);
1523 load->inst_basereg = X86_EBP;
1524 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1526 DEBUG (g_print ("SPILLED FLOAT LOAD (%d at 0x%08x(%%sp)) (from %d)\n", spill, load->inst_offset, reg));
1530 #define is_global_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS && !X86_IS_CALLEE ((r)))
1531 #define reg_is_freeable(r) ((r) >= 0 && (r) < MONO_MAX_IREGS && X86_IS_CALLEE ((r)))
1538 int flags; /* used to track fp spill/load */
1541 static const char*const * ins_spec = pentium_desc;
1544 print_ins (int i, MonoInst *ins)
1546 const char *spec = ins_spec [ins->opcode];
1547 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1548 if (spec [MONO_INST_DEST]) {
1549 if (ins->dreg >= MONO_MAX_IREGS)
1550 g_print (" R%d <-", ins->dreg);
1552 g_print (" %s <-", mono_arch_regname (ins->dreg));
1554 if (spec [MONO_INST_SRC1]) {
1555 if (ins->sreg1 >= MONO_MAX_IREGS)
1556 g_print (" R%d", ins->sreg1);
1558 g_print (" %s", mono_arch_regname (ins->sreg1));
1560 if (spec [MONO_INST_SRC2]) {
1561 if (ins->sreg2 >= MONO_MAX_IREGS)
1562 g_print (" R%d", ins->sreg2);
1564 g_print (" %s", mono_arch_regname (ins->sreg2));
1566 if (spec [MONO_INST_CLOB])
1567 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1572 print_regtrack (RegTrack *t, int num)
1578 for (i = 0; i < num; ++i) {
1581 if (i >= MONO_MAX_IREGS) {
1582 g_snprintf (buf, sizeof(buf), "R%d", i);
1585 r = mono_arch_regname (i);
1586 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1590 typedef struct InstList InstList;
1598 static inline InstList*
1599 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1601 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1611 * Force the spilling of the variable in the symbolic register 'reg'.
1614 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1619 sel = cfg->rs->iassign [reg];
1620 /*i = cfg->rs->isymbolic [sel];
1621 g_assert (i == reg);*/
1623 spill = ++cfg->spill_count;
1624 cfg->rs->iassign [i] = -spill - 1;
1625 mono_regstate_free_int (cfg->rs, sel);
1626 /* we need to create a spill var and insert a load to sel after the current instruction */
1627 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1629 load->inst_basereg = X86_EBP;
1630 load->inst_offset = mono_spillvar_offset (cfg, spill);
1632 while (ins->next != item->prev->data)
1635 load->next = ins->next;
1637 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1638 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1639 g_assert (i == sel);
1645 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1650 DEBUG (g_print ("\tstart regmask to assign R%d: 0x%08x (R%d <- R%d R%d)\n", reg, regmask, ins->dreg, ins->sreg1, ins->sreg2));
1651 /* exclude the registers in the current instruction */
1652 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1653 if (ins->sreg1 >= MONO_MAX_IREGS)
1654 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1656 regmask &= ~ (1 << ins->sreg1);
1657 DEBUG (g_print ("\t\texcluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1659 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1660 if (ins->sreg2 >= MONO_MAX_IREGS)
1661 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1663 regmask &= ~ (1 << ins->sreg2);
1664 DEBUG (g_print ("\t\texcluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1666 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1667 regmask &= ~ (1 << ins->dreg);
1668 DEBUG (g_print ("\t\texcluding dreg %s\n", mono_arch_regname (ins->dreg)));
1671 DEBUG (g_print ("\t\tavailable regmask: 0x%08x\n", regmask));
1672 g_assert (regmask); /* need at least a register we can free */
1674 /* we should track prev_use and spill the register that's farther */
1675 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1676 if (regmask & (1 << i)) {
1678 DEBUG (g_print ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1682 i = cfg->rs->isymbolic [sel];
1683 spill = ++cfg->spill_count;
1684 cfg->rs->iassign [i] = -spill - 1;
1685 mono_regstate_free_int (cfg->rs, sel);
1686 /* we need to create a spill var and insert a load to sel after the current instruction */
1687 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1689 load->inst_basereg = X86_EBP;
1690 load->inst_offset = mono_spillvar_offset (cfg, spill);
1692 while (ins->next != item->prev->data)
1695 load->next = ins->next;
1697 DEBUG (g_print ("\tSPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1698 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1699 g_assert (i == sel);
1705 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1708 MONO_INST_NEW (cfg, copy, OP_MOVE);
1712 copy->next = ins->next;
1715 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1720 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1723 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1725 store->inst_destbasereg = X86_EBP;
1726 store->inst_offset = mono_spillvar_offset (cfg, spill);
1728 store->next = ins->next;
1731 DEBUG (g_print ("\tSPILLED STORE (%d at 0x%08x(%%ebp)) R%d (from %s)\n", spill, store->inst_offset, prev_reg, mono_arch_regname (reg)));
1736 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1740 prev = item->next->data;
1742 while (prev->next != ins)
1744 to_insert->next = ins;
1745 prev->next = to_insert;
1747 to_insert->next = ins;
1750 * needed otherwise in the next instruction we can add an ins to the
1751 * end and that would get past this instruction.
1753 item->data = to_insert;
1759 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1761 int val = cfg->rs->iassign [sym_reg];
1765 /* the register gets spilled after this inst */
1768 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1770 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1771 cfg->rs->iassign [sym_reg] = val;
1772 /* add option to store before the instruction for src registers */
1774 create_spilled_store (cfg, spill, val, sym_reg, ins);
1776 cfg->rs->isymbolic [val] = sym_reg;
1781 /* flags used in reginfo->flags */
1783 MONO_X86_FP_NEEDS_LOAD_SPILL = 1 << 0,
1784 MONO_X86_FP_NEEDS_SPILL = 1 << 1,
1785 MONO_X86_FP_NEEDS_LOAD = 1 << 2,
1786 MONO_X86_REG_NOT_ECX = 1 << 3,
1787 MONO_X86_REG_EAX = 1 << 4,
1788 MONO_X86_REG_EDX = 1 << 5,
1789 MONO_X86_REG_ECX = 1 << 6
1793 mono_x86_alloc_int_reg (MonoCompile *cfg, InstList *tmp, MonoInst *ins, guint32 dest_mask, int sym_reg, int flags)
1796 int test_mask = dest_mask;
1798 if (flags & MONO_X86_REG_EAX)
1799 test_mask &= (1 << X86_EAX);
1800 else if (flags & MONO_X86_REG_EDX)
1801 test_mask &= (1 << X86_EDX);
1802 else if (flags & MONO_X86_REG_ECX)
1803 test_mask &= (1 << X86_ECX);
1804 else if (flags & MONO_X86_REG_NOT_ECX)
1805 test_mask &= ~ (1 << X86_ECX);
1807 val = mono_regstate_alloc_int (cfg->rs, test_mask);
1808 if (val >= 0 && test_mask != dest_mask)
1809 DEBUG(g_print ("\tUsed flag to allocate reg %s for R%u\n", mono_arch_regname (val), sym_reg));
1811 if (val < 0 && (flags & MONO_X86_REG_NOT_ECX)) {
1812 DEBUG(g_print ("\tFailed to allocate flag suggested mask (%u) but exluding ECX\n", test_mask));
1813 val = mono_regstate_alloc_int (cfg->rs, (dest_mask & (~1 << X86_ECX)));
1817 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
1819 val = get_register_spilling (cfg, tmp, ins, dest_mask, sym_reg);
1826 assign_ireg (MonoRegState *rs, int reg, int hreg)
1828 g_assert (reg >= MONO_MAX_IREGS);
1829 g_assert (hreg < MONO_MAX_IREGS);
1830 g_assert (! is_global_ireg (hreg));
1832 rs->iassign [reg] = hreg;
1833 rs->isymbolic [hreg] = reg;
1834 rs->ifree_mask &= ~ (1 << hreg);
1837 /*#include "cprop.c"*/
1840 * Local register allocation.
1841 * We first scan the list of instructions and we save the liveness info of
1842 * each register (when the register is first used, when it's value is set etc.).
1843 * We also reverse the list of instructions (in the InstList list) because assigning
1844 * registers backwards allows for more tricks to be used.
1847 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1850 MonoRegState *rs = cfg->rs;
1851 int i, val, fpcount;
1852 RegTrack *reginfo, *reginfof;
1853 RegTrack *reginfo1, *reginfo2, *reginfod;
1854 InstList *tmp, *reversed = NULL;
1856 guint32 src1_mask, src2_mask, dest_mask;
1857 GList *fspill_list = NULL;
1862 rs->next_vireg = bb->max_ireg;
1863 rs->next_vfreg = bb->max_freg;
1864 mono_regstate_assign (rs);
1865 reginfo = g_malloc0 (sizeof (RegTrack) * rs->next_vireg);
1866 reginfof = g_malloc0 (sizeof (RegTrack) * rs->next_vfreg);
1867 rs->ifree_mask = X86_CALLEE_REGS;
1871 /*if (cfg->opt & MONO_OPT_COPYPROP)
1872 local_copy_prop (cfg, ins);*/
1876 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1877 /* forward pass on the instructions to collect register liveness info */
1879 spec = ins_spec [ins->opcode];
1881 DEBUG (print_ins (i, ins));
1883 if (spec [MONO_INST_SRC1]) {
1884 if (spec [MONO_INST_SRC1] == 'f') {
1886 reginfo1 = reginfof;
1888 spill = g_list_first (fspill_list);
1889 if (spill && fpcount < MONO_MAX_FREGS) {
1890 reginfo1 [ins->sreg1].flags |= MONO_X86_FP_NEEDS_LOAD;
1891 fspill_list = g_list_remove (fspill_list, spill->data);
1897 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1898 reginfo1 [ins->sreg1].last_use = i;
1899 if (spec [MONO_INST_SRC1] == 'L') {
1900 /* The virtual register is allocated sequentially */
1901 reginfo1 [ins->sreg1 + 1].prev_use = reginfo1 [ins->sreg1 + 1].last_use;
1902 reginfo1 [ins->sreg1 + 1].last_use = i;
1903 if (reginfo1 [ins->sreg1 + 1].born_in == 0 || reginfo1 [ins->sreg1 + 1].born_in > i)
1904 reginfo1 [ins->sreg1 + 1].born_in = i;
1906 reginfo1 [ins->sreg1].flags |= MONO_X86_REG_EAX;
1907 reginfo1 [ins->sreg1 + 1].flags |= MONO_X86_REG_EDX;
1912 if (spec [MONO_INST_SRC2]) {
1913 if (spec [MONO_INST_SRC2] == 'f') {
1915 reginfo2 = reginfof;
1916 spill = g_list_first (fspill_list);
1918 reginfo2 [ins->sreg2].flags |= MONO_X86_FP_NEEDS_LOAD;
1919 fspill_list = g_list_remove (fspill_list, spill->data);
1920 if (fpcount >= MONO_MAX_FREGS) {
1922 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
1923 reginfo2 [ins->sreg2].flags |= MONO_X86_FP_NEEDS_LOAD_SPILL;
1930 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1931 reginfo2 [ins->sreg2].last_use = i;
1932 if (spec [MONO_INST_SRC2] == 'L') {
1933 /* The virtual register is allocated sequentially */
1934 reginfo2 [ins->sreg2 + 1].prev_use = reginfo2 [ins->sreg2 + 1].last_use;
1935 reginfo2 [ins->sreg2 + 1].last_use = i;
1936 if (reginfo2 [ins->sreg2 + 1].born_in == 0 || reginfo2 [ins->sreg2 + 1].born_in > i)
1937 reginfo2 [ins->sreg2 + 1].born_in = i;
1939 if (spec [MONO_INST_CLOB] == 's') {
1940 reginfo2 [ins->sreg1].flags |= MONO_X86_REG_NOT_ECX;
1941 reginfo2 [ins->sreg2].flags |= MONO_X86_REG_ECX;
1946 if (spec [MONO_INST_DEST]) {
1947 if (spec [MONO_INST_DEST] == 'f') {
1948 reginfod = reginfof;
1949 if (fpcount >= MONO_MAX_FREGS) {
1950 reginfod [ins->dreg].flags |= MONO_X86_FP_NEEDS_SPILL;
1952 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
1959 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1960 reginfod [ins->dreg].killed_in = i;
1961 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1962 reginfod [ins->dreg].last_use = i;
1963 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1964 reginfod [ins->dreg].born_in = i;
1965 if (spec [MONO_INST_DEST] == 'l' || spec [MONO_INST_DEST] == 'L') {
1966 /* The virtual register is allocated sequentially */
1967 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1968 reginfod [ins->dreg + 1].last_use = i;
1969 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1970 reginfod [ins->dreg + 1].born_in = i;
1972 reginfod [ins->dreg].flags |= MONO_X86_REG_EAX;
1973 reginfod [ins->dreg + 1].flags |= MONO_X86_REG_EDX;
1979 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1984 // todo: check if we have anything left on fp stack, in verify mode?
1987 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1988 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1991 int prev_dreg, prev_sreg1, prev_sreg2, clob_dreg;
1992 dest_mask = src1_mask = src2_mask = X86_CALLEE_REGS;
1995 spec = ins_spec [ins->opcode];
1998 DEBUG (g_print ("processing:"));
1999 DEBUG (print_ins (i, ins));
2000 if (spec [MONO_INST_CLOB] == 's') {
2002 * Shift opcodes, SREG2 must be RCX
2004 if (rs->ifree_mask & (1 << X86_ECX)) {
2005 if (ins->sreg2 < MONO_MAX_IREGS) {
2006 /* Argument already in hard reg, need to copy */
2007 MonoInst *copy = create_copy_ins (cfg, X86_ECX, ins->sreg2, NULL);
2008 insert_before_ins (ins, tmp, copy);
2011 DEBUG (g_print ("\tshortcut assignment of R%d to ECX\n", ins->sreg2));
2012 assign_ireg (rs, ins->sreg2, X86_ECX);
2015 int need_ecx_spill = TRUE;
2017 * we first check if src1/dreg is already assigned a register
2018 * and then we force a spill of the var assigned to ECX.
2020 /* the destination register can't be ECX */
2021 dest_mask &= ~ (1 << X86_ECX);
2022 src1_mask &= ~ (1 << X86_ECX);
2023 val = rs->iassign [ins->dreg];
2025 * the destination register is already assigned to ECX:
2026 * we need to allocate another register for it and then
2027 * copy from this to ECX.
2029 if (val == X86_ECX && ins->dreg != ins->sreg2) {
2031 new_dest = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
2032 g_assert (new_dest >= 0);
2033 DEBUG (g_print ("\tclob:s changing dreg R%d to %s from ECX\n", ins->dreg, mono_arch_regname (new_dest)));
2035 rs->isymbolic [new_dest] = ins->dreg;
2036 rs->iassign [ins->dreg] = new_dest;
2037 clob_dreg = ins->dreg;
2038 ins->dreg = new_dest;
2039 create_copy_ins (cfg, X86_ECX, new_dest, ins);
2040 need_ecx_spill = FALSE;
2041 /*DEBUG (g_print ("\tforced spill of R%d\n", ins->dreg));
2042 val = get_register_force_spilling (cfg, tmp, ins, ins->dreg);
2043 rs->iassign [ins->dreg] = val;
2044 rs->isymbolic [val] = prev_dreg;
2047 if (is_global_ireg (ins->sreg2)) {
2048 MonoInst *copy = create_copy_ins (cfg, X86_ECX, ins->sreg2, NULL);
2049 insert_before_ins (ins, tmp, copy);
2052 val = rs->iassign [ins->sreg2];
2053 if (val >= 0 && val != X86_ECX) {
2054 MonoInst *move = create_copy_ins (cfg, X86_ECX, val, NULL);
2055 DEBUG (g_print ("\tmoved arg from R%d (%d) to ECX\n", val, ins->sreg2));
2057 g_assert_not_reached ();
2058 /* FIXME: where is move connected to the instruction list? */
2059 //tmp->prev->data->next = move;
2063 need_ecx_spill = FALSE;
2066 if (need_ecx_spill && !(rs->ifree_mask & (1 << X86_ECX))) {
2067 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_ECX]));
2068 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_ECX]);
2069 mono_regstate_free_int (rs, X86_ECX);
2071 if (!is_global_ireg (ins->sreg2))
2072 /* force-set sreg2 */
2073 assign_ireg (rs, ins->sreg2, X86_ECX);
2075 ins->sreg2 = X86_ECX;
2076 } else if (spec [MONO_INST_CLOB] == 'd') {
2080 int dest_reg = X86_EAX;
2081 int clob_reg = X86_EDX;
2082 if (spec [MONO_INST_DEST] == 'd') {
2083 dest_reg = X86_EDX; /* reminder */
2086 if (is_global_ireg (ins->dreg))
2089 val = rs->iassign [ins->dreg];
2090 if (0 && val >= 0 && val != dest_reg && !(rs->ifree_mask & (1 << dest_reg))) {
2091 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
2092 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
2093 mono_regstate_free_int (rs, dest_reg);
2097 /* the register gets spilled after this inst */
2098 int spill = -val -1;
2099 dest_mask = 1 << dest_reg;
2100 prev_dreg = ins->dreg;
2101 val = mono_regstate_alloc_int (rs, dest_mask);
2103 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
2104 rs->iassign [ins->dreg] = val;
2106 create_spilled_store (cfg, spill, val, prev_dreg, ins);
2107 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
2108 rs->isymbolic [val] = prev_dreg;
2111 DEBUG (g_print ("\tshortcut assignment of R%d to %s\n", ins->dreg, mono_arch_regname (dest_reg)));
2112 prev_dreg = ins->dreg;
2113 assign_ireg (rs, ins->dreg, dest_reg);
2114 ins->dreg = dest_reg;
2119 //DEBUG (g_print ("dest reg in div assigned: %s\n", mono_arch_regname (val)));
2120 if (val != dest_reg) { /* force a copy */
2121 create_copy_ins (cfg, val, dest_reg, ins);
2122 if (!(rs->ifree_mask & (1 << dest_reg)) && rs->isymbolic [dest_reg] >= MONO_MAX_IREGS) {
2123 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
2124 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
2125 mono_regstate_free_int (rs, dest_reg);
2128 if (!(rs->ifree_mask & (1 << clob_reg)) && (clob_reg != val) && (rs->isymbolic [clob_reg] >= 8)) {
2129 DEBUG (g_print ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
2130 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [clob_reg]);
2131 mono_regstate_free_int (rs, clob_reg);
2133 src1_mask = 1 << X86_EAX;
2134 src2_mask = 1 << X86_ECX;
2135 } else if (spec [MONO_INST_DEST] == 'l') {
2137 val = rs->iassign [ins->dreg];
2138 /* check special case when dreg have been moved from ecx (clob shift) */
2139 if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
2140 hreg = clob_dreg + 1;
2142 hreg = ins->dreg + 1;
2144 /* base prev_dreg on fixed hreg, handle clob case */
2147 if (val != rs->isymbolic [X86_EAX] && !(rs->ifree_mask & (1 << X86_EAX))) {
2148 DEBUG (g_print ("\t(long-low) forced spill of R%d\n", rs->isymbolic [X86_EAX]));
2149 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
2150 mono_regstate_free_int (rs, X86_EAX);
2152 if (hreg != rs->isymbolic [X86_EDX] && !(rs->ifree_mask & (1 << X86_EDX))) {
2153 DEBUG (g_print ("\t(long-high) forced spill of R%d\n", rs->isymbolic [X86_EDX]));
2154 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EDX]);
2155 mono_regstate_free_int (rs, X86_EDX);
2157 } else if (spec [MONO_INST_CLOB] == 'b') {
2159 * x86_set_reg instructions, dreg needs to be EAX..EDX
2161 dest_mask = (1 << X86_EAX) | (1 << X86_EBX) | (1 << X86_ECX) | (1 << X86_EDX);
2162 if ((ins->dreg < MONO_MAX_IREGS) && (! (dest_mask & (1 << ins->dreg)))) {
2164 * ins->dreg is already a hard reg, need to allocate another
2165 * suitable hard reg and make a copy.
2167 int new_dest = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
2168 g_assert (new_dest >= 0);
2170 create_copy_ins (cfg, ins->dreg, new_dest, ins);
2171 DEBUG (g_print ("\tclob:b changing dreg R%d to %s\n", ins->dreg, mono_arch_regname (new_dest)));
2172 ins->dreg = new_dest;
2174 /* The hard reg is no longer needed */
2175 mono_regstate_free_int (rs, new_dest);
2182 if (spec [MONO_INST_DEST] == 'f') {
2183 if (reginfof [ins->dreg].flags & MONO_X86_FP_NEEDS_SPILL) {
2186 spill_node = g_list_first (fspill_list);
2187 g_assert (spill_node);
2189 store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->dreg, ins);
2190 insert_before_ins (ins, tmp, store);
2191 fspill_list = g_list_remove (fspill_list, spill_node->data);
2194 } else if (spec [MONO_INST_DEST] == 'L') {
2196 val = rs->iassign [ins->dreg];
2197 /* check special case when dreg have been moved from ecx (clob shift) */
2198 if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
2199 hreg = clob_dreg + 1;
2201 hreg = ins->dreg + 1;
2203 /* base prev_dreg on fixed hreg, handle clob case */
2204 prev_dreg = hreg - 1;
2209 /* the register gets spilled after this inst */
2212 val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
2213 rs->iassign [ins->dreg] = val;
2215 create_spilled_store (cfg, spill, val, prev_dreg, ins);
2218 DEBUG (g_print ("\tassigned dreg (long) %s to dest R%d\n", mono_arch_regname (val), hreg - 1));
2220 rs->isymbolic [val] = hreg - 1;
2223 val = rs->iassign [hreg];
2227 /* the register gets spilled after this inst */
2230 val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, hreg, reginfo [hreg].flags);
2231 rs->iassign [hreg] = val;
2233 create_spilled_store (cfg, spill, val, hreg, ins);
2236 DEBUG (g_print ("\tassigned hreg (long-high) %s to dest R%d\n", mono_arch_regname (val), hreg));
2237 rs->isymbolic [val] = hreg;
2238 /* save reg allocating into unused */
2241 /* check if we can free our long reg */
2242 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
2243 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (val), hreg, reginfo [hreg].born_in));
2244 mono_regstate_free_int (rs, val);
2247 else if (ins->dreg >= MONO_MAX_IREGS) {
2249 val = rs->iassign [ins->dreg];
2250 if (spec [MONO_INST_DEST] == 'l') {
2251 /* check special case when dreg have been moved from ecx (clob shift) */
2252 if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
2253 hreg = clob_dreg + 1;
2255 hreg = ins->dreg + 1;
2257 /* base prev_dreg on fixed hreg, handle clob case */
2258 prev_dreg = hreg - 1;
2260 prev_dreg = ins->dreg;
2265 /* the register gets spilled after this inst */
2268 val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
2269 rs->iassign [ins->dreg] = val;
2271 create_spilled_store (cfg, spill, val, prev_dreg, ins);
2273 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
2274 rs->isymbolic [val] = prev_dreg;
2276 /* handle cases where lreg needs to be eax:edx */
2277 if (spec [MONO_INST_DEST] == 'l') {
2278 /* check special case when dreg have been moved from ecx (clob shift) */
2279 int hreg = prev_dreg + 1;
2280 val = rs->iassign [hreg];
2284 /* the register gets spilled after this inst */
2287 val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, hreg, reginfo [hreg].flags);
2288 rs->iassign [hreg] = val;
2290 create_spilled_store (cfg, spill, val, hreg, ins);
2292 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
2293 rs->isymbolic [val] = hreg;
2294 if (ins->dreg == X86_EAX) {
2296 create_copy_ins (cfg, val, X86_EDX, ins);
2297 } else if (ins->dreg == X86_EDX) {
2298 if (val == X86_EAX) {
2300 g_assert_not_reached ();
2302 /* two forced copies */
2303 create_copy_ins (cfg, val, X86_EDX, ins);
2304 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
2307 if (val == X86_EDX) {
2308 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
2310 /* two forced copies */
2311 create_copy_ins (cfg, val, X86_EDX, ins);
2312 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
2315 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
2316 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
2317 mono_regstate_free_int (rs, val);
2319 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != X86_EAX && spec [MONO_INST_CLOB] != 'd') {
2320 /* this instruction only outputs to EAX, need to copy */
2321 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
2322 } else if (spec [MONO_INST_DEST] == 'd' && ins->dreg != X86_EDX && spec [MONO_INST_CLOB] != 'd') {
2323 create_copy_ins (cfg, ins->dreg, X86_EDX, ins);
2326 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
2327 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
2328 mono_regstate_free_int (rs, ins->dreg);
2330 /* put src1 in EAX if it needs to be */
2331 if (spec [MONO_INST_SRC1] == 'a') {
2332 if (!(rs->ifree_mask & (1 << X86_EAX))) {
2333 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
2334 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
2335 mono_regstate_free_int (rs, X86_EAX);
2337 if (ins->sreg1 < MONO_MAX_IREGS) {
2338 /* The argument is already in a hard reg, need to copy */
2339 MonoInst *copy = create_copy_ins (cfg, X86_EAX, ins->sreg1, NULL);
2340 insert_before_ins (ins, tmp, copy);
2343 /* force-set sreg1 */
2344 assign_ireg (rs, ins->sreg1, X86_EAX);
2345 ins->sreg1 = X86_EAX;
2351 if (spec [MONO_INST_SRC1] == 'f') {
2352 if (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD) {
2354 MonoInst *store = NULL;
2356 if (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD_SPILL) {
2358 spill_node = g_list_first (fspill_list);
2359 g_assert (spill_node);
2361 store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->sreg1, ins);
2362 fspill_list = g_list_remove (fspill_list, spill_node->data);
2366 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
2367 load = create_spilled_load_float (cfg, fspill, ins->sreg1, ins);
2368 insert_before_ins (ins, tmp, load);
2370 insert_before_ins (load, tmp, store);
2372 } else if ((spec [MONO_INST_DEST] == 'L') && (spec [MONO_INST_SRC1] == 'L')) {
2373 /* force source to be same as dest */
2374 assign_ireg (rs, ins->sreg1, ins->dreg);
2375 assign_ireg (rs, ins->sreg1 + 1, ins->unused);
2377 DEBUG (g_print ("\tassigned sreg1 (long) %s to sreg1 R%d\n", mono_arch_regname (ins->dreg), ins->sreg1));
2378 DEBUG (g_print ("\tassigned sreg1 (long-high) %s to sreg1 R%d\n", mono_arch_regname (ins->unused), ins->sreg1 + 1));
2380 ins->sreg1 = ins->dreg;
2382 * No need for saving the reg, we know that src1=dest in this cases
2383 * ins->inst_c0 = ins->unused;
2386 else if (ins->sreg1 >= MONO_MAX_IREGS) {
2387 val = rs->iassign [ins->sreg1];
2388 prev_sreg1 = ins->sreg1;
2392 /* the register gets spilled after this inst */
2395 if (0 && ins->opcode == OP_MOVE) {
2397 * small optimization: the dest register is already allocated
2398 * but the src one is not: we can simply assign the same register
2399 * here and peephole will get rid of the instruction later.
2400 * This optimization may interfere with the clobbering handling:
2401 * it removes a mov operation that will be added again to handle clobbering.
2402 * There are also some other issues that should with make testjit.
2404 mono_regstate_alloc_int (rs, 1 << ins->dreg);
2405 val = rs->iassign [ins->sreg1] = ins->dreg;
2406 //g_assert (val >= 0);
2407 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2409 //g_assert (val == -1); /* source cannot be spilled */
2410 val = mono_x86_alloc_int_reg (cfg, tmp, ins, src1_mask, ins->sreg1, reginfo [ins->sreg1].flags);
2411 rs->iassign [ins->sreg1] = val;
2412 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2415 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
2416 insert_before_ins (ins, tmp, store);
2419 rs->isymbolic [val] = prev_sreg1;
2424 /* handle clobbering of sreg1 */
2425 if ((spec [MONO_INST_CLOB] == '1' || spec [MONO_INST_CLOB] == 's') && ins->dreg != ins->sreg1) {
2426 MonoInst *sreg2_copy = NULL;
2427 MonoInst *copy = NULL;
2429 if (ins->dreg == ins->sreg2) {
2431 * copying sreg1 to dreg could clobber sreg2, so allocate a new
2436 reg2 = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->sreg2, 0);
2438 DEBUG (g_print ("\tneed to copy sreg2 %s to reg %s\n", mono_arch_regname (ins->sreg2), mono_arch_regname (reg2)));
2439 sreg2_copy = create_copy_ins (cfg, reg2, ins->sreg2, NULL);
2440 prev_sreg2 = ins->sreg2 = reg2;
2442 mono_regstate_free_int (rs, reg2);
2445 copy = create_copy_ins (cfg, ins->dreg, ins->sreg1, NULL);
2446 DEBUG (g_print ("\tneed to copy sreg1 %s to dreg %s\n", mono_arch_regname (ins->sreg1), mono_arch_regname (ins->dreg)));
2447 insert_before_ins (ins, tmp, copy);
2450 insert_before_ins (copy, tmp, sreg2_copy);
2453 * Need to prevent sreg2 to be allocated to sreg1, since that
2454 * would screw up the previous copy.
2456 src2_mask &= ~ (1 << ins->sreg1);
2457 /* we set sreg1 to dest as well */
2458 prev_sreg1 = ins->sreg1 = ins->dreg;
2459 src2_mask &= ~ (1 << ins->dreg);
2465 if (spec [MONO_INST_SRC2] == 'f') {
2466 if (reginfof [ins->sreg2].flags & MONO_X86_FP_NEEDS_LOAD) {
2468 MonoInst *store = NULL;
2470 if (reginfof [ins->sreg2].flags & MONO_X86_FP_NEEDS_LOAD_SPILL) {
2473 spill_node = g_list_first (fspill_list);
2474 g_assert (spill_node);
2475 if (spec [MONO_INST_SRC1] == 'f' && (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD_SPILL))
2476 spill_node = g_list_next (spill_node);
2478 store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->sreg2, ins);
2479 fspill_list = g_list_remove (fspill_list, spill_node->data);
2483 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
2484 load = create_spilled_load_float (cfg, fspill, ins->sreg2, ins);
2485 insert_before_ins (ins, tmp, load);
2487 insert_before_ins (load, tmp, store);
2490 else if (ins->sreg2 >= MONO_MAX_IREGS) {
2491 val = rs->iassign [ins->sreg2];
2492 prev_sreg2 = ins->sreg2;
2496 /* the register gets spilled after this inst */
2499 val = mono_x86_alloc_int_reg (cfg, tmp, ins, src2_mask, ins->sreg2, reginfo [ins->sreg2].flags);
2500 rs->iassign [ins->sreg2] = val;
2501 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
2503 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
2505 rs->isymbolic [val] = prev_sreg2;
2507 if (spec [MONO_INST_CLOB] == 's' && ins->sreg2 != X86_ECX) {
2508 DEBUG (g_print ("\tassigned sreg2 %s to R%d, but ECX is needed (R%d)\n", mono_arch_regname (val), ins->sreg2, rs->iassign [X86_ECX]));
2514 if (spec [MONO_INST_CLOB] == 'c') {
2516 guint32 clob_mask = X86_CALLEE_REGS;
2517 for (j = 0; j < MONO_MAX_IREGS; ++j) {
2519 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
2520 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
2524 if (spec [MONO_INST_CLOB] == 'a') {
2525 guint32 clob_reg = X86_EAX;
2526 if (!(rs->ifree_mask & (1 << clob_reg)) && (rs->isymbolic [clob_reg] >= 8)) {
2527 DEBUG (g_print ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
2528 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [clob_reg]);
2529 mono_regstate_free_int (rs, clob_reg);
2532 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2533 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2534 mono_regstate_free_int (rs, ins->sreg1);
2536 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2537 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2538 mono_regstate_free_int (rs, ins->sreg2);
2541 //DEBUG (print_ins (i, ins));
2542 /* this may result from a insert_before call */
2544 bb->code = tmp->data;
2550 g_list_free (fspill_list);
2553 static unsigned char*
2554 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
2556 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2557 x86_fnstcw_membase(code, X86_ESP, 0);
2558 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
2559 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
2560 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
2561 x86_fldcw_membase (code, X86_ESP, 2);
2563 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2564 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2565 x86_pop_reg (code, dreg);
2566 /* FIXME: need the high register
2567 * x86_pop_reg (code, dreg_high);
2570 x86_push_reg (code, X86_EAX); // SP = SP - 4
2571 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
2572 x86_pop_reg (code, dreg);
2574 x86_fldcw_membase (code, X86_ESP, 0);
2575 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2578 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2580 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2584 static unsigned char*
2585 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2587 int sreg = tree->sreg1;
2588 #ifdef PLATFORM_WIN32
2593 * If requested stack size is larger than one page,
2594 * perform stack-touch operation
2597 * Generate stack probe code.
2598 * Under Windows, it is necessary to allocate one page at a time,
2599 * "touching" stack after each successful sub-allocation. This is
2600 * because of the way stack growth is implemented - there is a
2601 * guard page before the lowest stack page that is currently commited.
2602 * Stack normally grows sequentially so OS traps access to the
2603 * guard page and commits more pages when needed.
2605 x86_test_reg_imm (code, sreg, ~0xFFF);
2606 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2608 br[2] = code; /* loop */
2609 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
2610 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
2611 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2612 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2613 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2614 x86_patch (br[3], br[2]);
2615 x86_test_reg_reg (code, sreg, sreg);
2616 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2617 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2619 br[1] = code; x86_jump8 (code, 0);
2621 x86_patch (br[0], code);
2622 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2623 x86_patch (br[1], code);
2624 x86_patch (br[4], code);
2625 #else /* PLATFORM_WIN32 */
2626 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2628 if (tree->flags & MONO_INST_INIT) {
2630 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2631 x86_push_reg (code, X86_EAX);
2634 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2635 x86_push_reg (code, X86_ECX);
2638 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2639 x86_push_reg (code, X86_EDI);
2643 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2644 if (sreg != X86_ECX)
2645 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2646 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2648 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2650 x86_prefix (code, X86_REP_PREFIX);
2653 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2654 x86_pop_reg (code, X86_EDI);
2655 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2656 x86_pop_reg (code, X86_ECX);
2657 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2658 x86_pop_reg (code, X86_EAX);
2665 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2670 /* Move return value to the target register */
2671 switch (ins->opcode) {
2674 case OP_CALL_MEMBASE:
2675 if (ins->dreg != X86_EAX)
2676 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2680 case OP_VCALL_MEMBASE:
2681 cinfo = get_call_info (((MonoCallInst*)ins)->signature, FALSE);
2682 if (cinfo->ret.storage == ArgValuetypeInReg) {
2683 /* Pop the destination address from the stack */
2684 x86_pop_reg (code, X86_ECX);
2686 for (quad = 0; quad < 2; quad ++) {
2687 switch (cinfo->ret.pair_storage [quad]) {
2689 g_assert (cinfo->ret.pair_regs [quad] != X86_ECX);
2690 x86_mov_membase_reg (code, X86_ECX, (quad * sizeof (gpointer)), cinfo->ret.pair_regs [quad], sizeof (gpointer));
2695 g_assert_not_reached ();
2707 #define REAL_PRINT_REG(text,reg) \
2708 mono_assert (reg >= 0); \
2709 x86_push_reg (code, X86_EAX); \
2710 x86_push_reg (code, X86_EDX); \
2711 x86_push_reg (code, X86_ECX); \
2712 x86_push_reg (code, reg); \
2713 x86_push_imm (code, reg); \
2714 x86_push_imm (code, text " %d %p\n"); \
2715 x86_mov_reg_imm (code, X86_EAX, printf); \
2716 x86_call_reg (code, X86_EAX); \
2717 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
2718 x86_pop_reg (code, X86_ECX); \
2719 x86_pop_reg (code, X86_EDX); \
2720 x86_pop_reg (code, X86_EAX);
2722 /* benchmark and set based on cpu */
2723 #define LOOP_ALIGNMENT 8
2724 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2727 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2732 guint8 *code = cfg->native_code + cfg->code_len;
2733 MonoInst *last_ins = NULL;
2734 guint last_offset = 0;
2737 if (cfg->opt & MONO_OPT_PEEPHOLE)
2738 peephole_pass (cfg, bb);
2740 if (cfg->opt & MONO_OPT_LOOP) {
2741 int pad, align = LOOP_ALIGNMENT;
2742 /* set alignment depending on cpu */
2743 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2745 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2746 x86_padding (code, pad);
2747 cfg->code_len += pad;
2748 bb->native_offset = cfg->code_len;
2752 if (cfg->verbose_level > 2)
2753 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2755 cpos = bb->max_offset;
2757 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2758 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2759 g_assert (!cfg->compile_aot);
2762 cov->data [bb->dfn].cil_code = bb->cil_code;
2763 /* this is not thread save, but good enough */
2764 x86_inc_mem (code, &cov->data [bb->dfn].count);
2767 offset = code - cfg->native_code;
2771 offset = code - cfg->native_code;
2773 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2775 if (offset > (cfg->code_size - max_len - 16)) {
2776 cfg->code_size *= 2;
2777 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2778 code = cfg->native_code + offset;
2779 mono_jit_stats.code_reallocs++;
2782 mono_debug_record_line_number (cfg, ins, offset);
2784 switch (ins->opcode) {
2786 x86_mul_reg (code, ins->sreg2, TRUE);
2789 x86_mul_reg (code, ins->sreg2, FALSE);
2791 case OP_X86_SETEQ_MEMBASE:
2792 case OP_X86_SETNE_MEMBASE:
2793 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2794 ins->inst_basereg, ins->inst_offset, TRUE);
2796 case OP_STOREI1_MEMBASE_IMM:
2797 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2799 case OP_STOREI2_MEMBASE_IMM:
2800 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2802 case OP_STORE_MEMBASE_IMM:
2803 case OP_STOREI4_MEMBASE_IMM:
2804 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2806 case OP_STOREI1_MEMBASE_REG:
2807 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2809 case OP_STOREI2_MEMBASE_REG:
2810 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2812 case OP_STORE_MEMBASE_REG:
2813 case OP_STOREI4_MEMBASE_REG:
2814 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2819 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2822 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2823 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2825 case OP_LOAD_MEMBASE:
2826 case OP_LOADI4_MEMBASE:
2827 case OP_LOADU4_MEMBASE:
2828 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2830 case OP_LOADU1_MEMBASE:
2831 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2833 case OP_LOADI1_MEMBASE:
2834 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2836 case OP_LOADU2_MEMBASE:
2837 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2839 case OP_LOADI2_MEMBASE:
2840 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2843 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2846 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2849 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2852 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2855 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2857 case OP_COMPARE_IMM:
2858 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2860 case OP_X86_COMPARE_MEMBASE_REG:
2861 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2863 case OP_X86_COMPARE_MEMBASE_IMM:
2864 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2866 case OP_X86_COMPARE_MEMBASE8_IMM:
2867 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2869 case OP_X86_COMPARE_REG_MEMBASE:
2870 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2872 case OP_X86_COMPARE_MEM_IMM:
2873 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2875 case OP_X86_TEST_NULL:
2876 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2878 case OP_X86_ADD_MEMBASE_IMM:
2879 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2881 case OP_X86_ADD_MEMBASE:
2882 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2884 case OP_X86_SUB_MEMBASE_IMM:
2885 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2887 case OP_X86_SUB_MEMBASE:
2888 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2890 case OP_X86_INC_MEMBASE:
2891 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2893 case OP_X86_INC_REG:
2894 x86_inc_reg (code, ins->dreg);
2896 case OP_X86_DEC_MEMBASE:
2897 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2899 case OP_X86_DEC_REG:
2900 x86_dec_reg (code, ins->dreg);
2902 case OP_X86_MUL_MEMBASE:
2903 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2906 x86_breakpoint (code);
2910 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2913 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2917 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2920 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2924 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2927 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2931 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2934 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2937 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2940 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2944 x86_div_reg (code, ins->sreg2, TRUE);
2947 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2948 x86_div_reg (code, ins->sreg2, FALSE);
2951 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2953 x86_div_reg (code, ins->sreg2, TRUE);
2957 x86_div_reg (code, ins->sreg2, TRUE);
2960 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2961 x86_div_reg (code, ins->sreg2, FALSE);
2964 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2966 x86_div_reg (code, ins->sreg2, TRUE);
2969 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2972 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2975 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2978 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2981 g_assert (ins->sreg2 == X86_ECX);
2982 x86_shift_reg (code, X86_SHL, ins->dreg);
2985 g_assert (ins->sreg2 == X86_ECX);
2986 x86_shift_reg (code, X86_SAR, ins->dreg);
2989 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2992 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2995 g_assert (ins->sreg2 == X86_ECX);
2996 x86_shift_reg (code, X86_SHR, ins->dreg);
2999 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
3002 guint8 *jump_to_end;
3004 /* handle shifts below 32 bits */
3005 x86_shld_reg (code, ins->unused, ins->sreg1);
3006 x86_shift_reg (code, X86_SHL, ins->sreg1);
3008 x86_test_reg_imm (code, X86_ECX, 32);
3009 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3011 /* handle shift over 32 bit */
3012 x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
3013 x86_clear_reg (code, ins->sreg1);
3015 x86_patch (jump_to_end, code);
3019 guint8 *jump_to_end;
3021 /* handle shifts below 32 bits */
3022 x86_shrd_reg (code, ins->sreg1, ins->unused);
3023 x86_shift_reg (code, X86_SAR, ins->unused);
3025 x86_test_reg_imm (code, X86_ECX, 32);
3026 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3028 /* handle shifts over 31 bits */
3029 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
3030 x86_shift_reg_imm (code, X86_SAR, ins->unused, 31);
3032 x86_patch (jump_to_end, code);
3036 guint8 *jump_to_end;
3038 /* handle shifts below 32 bits */
3039 x86_shrd_reg (code, ins->sreg1, ins->unused);
3040 x86_shift_reg (code, X86_SHR, ins->unused);
3042 x86_test_reg_imm (code, X86_ECX, 32);
3043 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3045 /* handle shifts over 31 bits */
3046 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
3047 x86_shift_reg_imm (code, X86_SHR, ins->unused, 31);
3049 x86_patch (jump_to_end, code);
3053 if (ins->inst_imm >= 32) {
3054 x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
3055 x86_clear_reg (code, ins->sreg1);
3056 x86_shift_reg_imm (code, X86_SHL, ins->unused, ins->inst_imm - 32);
3058 x86_shld_reg_imm (code, ins->unused, ins->sreg1, ins->inst_imm);
3059 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
3063 if (ins->inst_imm >= 32) {
3064 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
3065 x86_shift_reg_imm (code, X86_SAR, ins->unused, 0x1f);
3066 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
3068 x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
3069 x86_shift_reg_imm (code, X86_SAR, ins->unused, ins->inst_imm);
3072 case OP_LSHR_UN_IMM:
3073 if (ins->inst_imm >= 32) {
3074 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
3075 x86_clear_reg (code, ins->unused);
3076 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
3078 x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
3079 x86_shift_reg_imm (code, X86_SHR, ins->unused, ins->inst_imm);
3083 x86_not_reg (code, ins->sreg1);
3086 x86_neg_reg (code, ins->sreg1);
3089 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3092 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
3095 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3098 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
3101 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3102 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3104 case CEE_MUL_OVF_UN: {
3105 /* the mul operation and the exception check should most likely be split */
3106 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3107 /*g_assert (ins->sreg2 == X86_EAX);
3108 g_assert (ins->dreg == X86_EAX);*/
3109 if (ins->sreg2 == X86_EAX) {
3110 non_eax_reg = ins->sreg1;
3111 } else if (ins->sreg1 == X86_EAX) {
3112 non_eax_reg = ins->sreg2;
3114 /* no need to save since we're going to store to it anyway */
3115 if (ins->dreg != X86_EAX) {
3117 x86_push_reg (code, X86_EAX);
3119 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
3120 non_eax_reg = ins->sreg2;
3122 if (ins->dreg == X86_EDX) {
3125 x86_push_reg (code, X86_EAX);
3127 } else if (ins->dreg != X86_EAX) {
3129 x86_push_reg (code, X86_EDX);
3131 x86_mul_reg (code, non_eax_reg, FALSE);
3132 /* save before the check since pop and mov don't change the flags */
3133 if (ins->dreg != X86_EAX)
3134 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3136 x86_pop_reg (code, X86_EDX);
3138 x86_pop_reg (code, X86_EAX);
3139 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3143 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
3146 g_assert_not_reached ();
3147 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3148 x86_mov_reg_imm (code, ins->dreg, 0);
3150 case OP_LOAD_GOTADDR:
3151 x86_call_imm (code, 0);
3153 * The patch needs to point to the pop, since the GOT offset needs
3154 * to be added to that address.
3156 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
3157 x86_pop_reg (code, ins->dreg);
3158 x86_alu_reg_imm (code, X86_ADD, ins->dreg, 0xf0f0f0f0);
3161 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3162 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
3164 case OP_X86_PUSH_GOT_ENTRY:
3165 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3166 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
3170 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3173 g_assert_not_reached ();
3176 * Note: this 'frame destruction' logic is useful for tail calls, too.
3177 * Keep in sync with the code in emit_epilog.
3181 /* FIXME: no tracing support... */
3182 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3183 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
3184 /* reset offset to make max_len work */
3185 offset = code - cfg->native_code;
3187 g_assert (!cfg->method->save_lmf);
3189 if (cfg->used_int_regs & (1 << X86_EBX))
3191 if (cfg->used_int_regs & (1 << X86_EDI))
3193 if (cfg->used_int_regs & (1 << X86_ESI))
3196 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3198 if (cfg->used_int_regs & (1 << X86_ESI))
3199 x86_pop_reg (code, X86_ESI);
3200 if (cfg->used_int_regs & (1 << X86_EDI))
3201 x86_pop_reg (code, X86_EDI);
3202 if (cfg->used_int_regs & (1 << X86_EBX))
3203 x86_pop_reg (code, X86_EBX);
3205 /* restore ESP/EBP */
3207 offset = code - cfg->native_code;
3208 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
3209 x86_jump32 (code, 0);
3213 /* ensure ins->sreg1 is not NULL
3214 * note that cmp DWORD PTR [eax], eax is one byte shorter than
3215 * cmp DWORD PTR [eax], 0
3217 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
3220 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
3221 x86_push_reg (code, hreg);
3222 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
3223 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
3224 x86_pop_reg (code, hreg);
3232 call = (MonoCallInst*)ins;
3233 if (ins->flags & MONO_INST_HAS_METHOD)
3234 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
3236 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
3237 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3238 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
3239 * bytes to pop, we want to use pops. GCC does this (note it won't happen
3240 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
3241 * smart enough to do that optimization yet
3243 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
3244 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
3245 * (most likely from locality benefits). People with other processors should
3246 * check on theirs to see what happens.
3248 if (call->stack_usage == 4) {
3249 /* we want to use registers that won't get used soon, so use
3250 * ecx, as eax will get allocated first. edx is used by long calls,
3251 * so we can't use that.
3254 x86_pop_reg (code, X86_ECX);
3256 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3259 code = emit_move_return_value (cfg, ins, code);
3264 case OP_VOIDCALL_REG:
3266 call = (MonoCallInst*)ins;
3267 x86_call_reg (code, ins->sreg1);
3268 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3269 if (call->stack_usage == 4)
3270 x86_pop_reg (code, X86_ECX);
3272 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3274 code = emit_move_return_value (cfg, ins, code);
3276 case OP_FCALL_MEMBASE:
3277 case OP_LCALL_MEMBASE:
3278 case OP_VCALL_MEMBASE:
3279 case OP_VOIDCALL_MEMBASE:
3280 case OP_CALL_MEMBASE:
3281 call = (MonoCallInst*)ins;
3282 x86_call_membase (code, ins->sreg1, ins->inst_offset);
3283 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3284 if (call->stack_usage == 4)
3285 x86_pop_reg (code, X86_ECX);
3287 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3289 code = emit_move_return_value (cfg, ins, code);
3293 x86_push_reg (code, ins->sreg1);
3295 case OP_X86_PUSH_IMM:
3296 x86_push_imm (code, ins->inst_imm);
3298 case OP_X86_PUSH_MEMBASE:
3299 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
3301 case OP_X86_PUSH_OBJ:
3302 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
3303 x86_push_reg (code, X86_EDI);
3304 x86_push_reg (code, X86_ESI);
3305 x86_push_reg (code, X86_ECX);
3306 if (ins->inst_offset)
3307 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
3309 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
3310 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
3311 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
3313 x86_prefix (code, X86_REP_PREFIX);
3315 x86_pop_reg (code, X86_ECX);
3316 x86_pop_reg (code, X86_ESI);
3317 x86_pop_reg (code, X86_EDI);
3320 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
3322 case OP_X86_LEA_MEMBASE:
3323 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
3326 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
3329 /* keep alignment */
3330 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
3331 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
3332 code = mono_emit_stack_alloc (code, ins);
3333 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3339 x86_push_reg (code, ins->sreg1);
3340 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3341 (gpointer)"mono_arch_throw_exception");
3345 x86_push_reg (code, ins->sreg1);
3346 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3347 (gpointer)"mono_arch_rethrow_exception");
3350 case OP_CALL_HANDLER:
3351 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3352 x86_call_imm (code, 0);
3355 ins->inst_c0 = code - cfg->native_code;
3358 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
3359 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
3361 if (ins->flags & MONO_INST_BRLABEL) {
3362 if (ins->inst_i0->inst_c0) {
3363 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
3365 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
3366 if ((cfg->opt & MONO_OPT_BRANCH) &&
3367 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
3368 x86_jump8 (code, 0);
3370 x86_jump32 (code, 0);
3373 if (ins->inst_target_bb->native_offset) {
3374 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3376 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3377 if ((cfg->opt & MONO_OPT_BRANCH) &&
3378 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
3379 x86_jump8 (code, 0);
3381 x86_jump32 (code, 0);
3386 x86_jump_reg (code, ins->sreg1);
3389 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3390 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3393 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
3394 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3397 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3398 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3401 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
3402 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3405 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3406 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3409 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
3410 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3412 case OP_COND_EXC_EQ:
3413 case OP_COND_EXC_NE_UN:
3414 case OP_COND_EXC_LT:
3415 case OP_COND_EXC_LT_UN:
3416 case OP_COND_EXC_GT:
3417 case OP_COND_EXC_GT_UN:
3418 case OP_COND_EXC_GE:
3419 case OP_COND_EXC_GE_UN:
3420 case OP_COND_EXC_LE:
3421 case OP_COND_EXC_LE_UN:
3422 case OP_COND_EXC_OV:
3423 case OP_COND_EXC_NO:
3425 case OP_COND_EXC_NC:
3426 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
3427 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
3439 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
3442 /* floating point opcodes */
3444 double d = *(double *)ins->inst_p0;
3446 if ((d == 0.0) && (mono_signbit (d) == 0)) {
3448 } else if (d == 1.0) {
3451 if (cfg->compile_aot) {
3452 guint32 *val = (guint32*)&d;
3453 x86_push_imm (code, val [1]);
3454 x86_push_imm (code, val [0]);
3455 x86_fld_membase (code, X86_ESP, 0, TRUE);
3456 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3459 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
3460 x86_fld (code, NULL, TRUE);
3466 float f = *(float *)ins->inst_p0;
3468 if ((f == 0.0) && (mono_signbit (f) == 0)) {
3470 } else if (f == 1.0) {
3473 if (cfg->compile_aot) {
3474 guint32 val = *(guint32*)&f;
3475 x86_push_imm (code, val);
3476 x86_fld_membase (code, X86_ESP, 0, FALSE);
3477 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3480 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
3481 x86_fld (code, NULL, FALSE);
3486 case OP_STORER8_MEMBASE_REG:
3487 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
3489 case OP_LOADR8_SPILL_MEMBASE:
3490 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3493 case OP_LOADR8_MEMBASE:
3494 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3496 case OP_STORER4_MEMBASE_REG:
3497 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
3499 case OP_LOADR4_MEMBASE:
3500 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3502 case CEE_CONV_R4: /* FIXME: change precision */
3504 x86_push_reg (code, ins->sreg1);
3505 x86_fild_membase (code, X86_ESP, 0, FALSE);
3506 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3508 case OP_X86_FP_LOAD_I8:
3509 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3511 case OP_X86_FP_LOAD_I4:
3512 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3514 case OP_FCONV_TO_I1:
3515 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
3517 case OP_FCONV_TO_U1:
3518 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
3520 case OP_FCONV_TO_I2:
3521 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
3523 case OP_FCONV_TO_U2:
3524 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
3526 case OP_FCONV_TO_I4:
3528 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
3530 case OP_FCONV_TO_I8:
3531 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3532 x86_fnstcw_membase(code, X86_ESP, 0);
3533 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
3534 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
3535 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
3536 x86_fldcw_membase (code, X86_ESP, 2);
3537 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3538 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
3539 x86_pop_reg (code, ins->dreg);
3540 x86_pop_reg (code, ins->unused);
3541 x86_fldcw_membase (code, X86_ESP, 0);
3542 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3544 case OP_LCONV_TO_R_UN: {
3545 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
3548 /* load 64bit integer to FP stack */
3549 x86_push_imm (code, 0);
3550 x86_push_reg (code, ins->sreg2);
3551 x86_push_reg (code, ins->sreg1);
3552 x86_fild_membase (code, X86_ESP, 0, TRUE);
3553 /* store as 80bit FP value */
3554 x86_fst80_membase (code, X86_ESP, 0);
3556 /* test if lreg is negative */
3557 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3558 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
3560 /* add correction constant mn */
3561 x86_fld80_mem (code, mn);
3562 x86_fld80_membase (code, X86_ESP, 0);
3563 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3564 x86_fst80_membase (code, X86_ESP, 0);
3566 x86_patch (br, code);
3568 x86_fld80_membase (code, X86_ESP, 0);
3569 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
3573 case OP_LCONV_TO_OVF_I: {
3574 guint8 *br [3], *label [1];
3577 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3579 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
3581 /* If the low word top bit is set, see if we are negative */
3582 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
3583 /* We are not negative (no top bit set, check for our top word to be zero */
3584 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3585 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3588 /* throw exception */
3589 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
3590 x86_jump32 (code, 0);
3592 x86_patch (br [0], code);
3593 /* our top bit is set, check that top word is 0xfffffff */
3594 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
3596 x86_patch (br [1], code);
3597 /* nope, emit exception */
3598 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
3599 x86_patch (br [2], label [0]);
3601 if (ins->dreg != ins->sreg1)
3602 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3606 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3609 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
3612 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
3615 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
3623 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3628 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3635 * it really doesn't make sense to inline all this code,
3636 * it's here just to show that things may not be as simple
3639 guchar *check_pos, *end_tan, *pop_jump;
3640 x86_push_reg (code, X86_EAX);
3643 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3645 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3646 x86_fstp (code, 0); /* pop the 1.0 */
3648 x86_jump8 (code, 0);
3650 x86_fp_op (code, X86_FADD, 0);
3654 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3656 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3659 x86_patch (pop_jump, code);
3660 x86_fstp (code, 0); /* pop the 1.0 */
3661 x86_patch (check_pos, code);
3662 x86_patch (end_tan, code);
3664 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3665 x86_pop_reg (code, X86_EAX);
3672 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3683 x86_push_reg (code, X86_EAX);
3684 /* we need to exchange ST(0) with ST(1) */
3687 /* this requires a loop, because fprem somtimes
3688 * returns a partial remainder */
3690 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3691 /* x86_fprem1 (code); */
3694 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3696 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
3701 x86_pop_reg (code, X86_EAX);
3705 if (cfg->opt & MONO_OPT_FCMOV) {
3706 x86_fcomip (code, 1);
3710 /* this overwrites EAX */
3711 EMIT_FPCOMPARE(code);
3712 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3715 if (cfg->opt & MONO_OPT_FCMOV) {
3716 /* zeroing the register at the start results in
3717 * shorter and faster code (we can also remove the widening op)
3719 guchar *unordered_check;
3720 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3721 x86_fcomip (code, 1);
3723 unordered_check = code;
3724 x86_branch8 (code, X86_CC_P, 0, FALSE);
3725 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3726 x86_patch (unordered_check, code);
3729 if (ins->dreg != X86_EAX)
3730 x86_push_reg (code, X86_EAX);
3732 EMIT_FPCOMPARE(code);
3733 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3734 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3735 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3736 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3738 if (ins->dreg != X86_EAX)
3739 x86_pop_reg (code, X86_EAX);
3743 if (cfg->opt & MONO_OPT_FCMOV) {
3744 /* zeroing the register at the start results in
3745 * shorter and faster code (we can also remove the widening op)
3747 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3748 x86_fcomip (code, 1);
3750 if (ins->opcode == OP_FCLT_UN) {
3751 guchar *unordered_check = code;
3752 guchar *jump_to_end;
3753 x86_branch8 (code, X86_CC_P, 0, FALSE);
3754 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3756 x86_jump8 (code, 0);
3757 x86_patch (unordered_check, code);
3758 x86_inc_reg (code, ins->dreg);
3759 x86_patch (jump_to_end, code);
3761 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3765 if (ins->dreg != X86_EAX)
3766 x86_push_reg (code, X86_EAX);
3768 EMIT_FPCOMPARE(code);
3769 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3770 if (ins->opcode == OP_FCLT_UN) {
3771 guchar *is_not_zero_check, *end_jump;
3772 is_not_zero_check = code;
3773 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3775 x86_jump8 (code, 0);
3776 x86_patch (is_not_zero_check, code);
3777 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3779 x86_patch (end_jump, code);
3781 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3782 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3784 if (ins->dreg != X86_EAX)
3785 x86_pop_reg (code, X86_EAX);
3789 if (cfg->opt & MONO_OPT_FCMOV) {
3790 /* zeroing the register at the start results in
3791 * shorter and faster code (we can also remove the widening op)
3793 guchar *unordered_check;
3794 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3795 x86_fcomip (code, 1);
3797 if (ins->opcode == OP_FCGT) {
3798 unordered_check = code;
3799 x86_branch8 (code, X86_CC_P, 0, FALSE);
3800 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3801 x86_patch (unordered_check, code);
3803 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3807 if (ins->dreg != X86_EAX)
3808 x86_push_reg (code, X86_EAX);
3810 EMIT_FPCOMPARE(code);
3811 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3812 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3813 if (ins->opcode == OP_FCGT_UN) {
3814 guchar *is_not_zero_check, *end_jump;
3815 is_not_zero_check = code;
3816 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3818 x86_jump8 (code, 0);
3819 x86_patch (is_not_zero_check, code);
3820 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3822 x86_patch (end_jump, code);
3824 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3825 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3827 if (ins->dreg != X86_EAX)
3828 x86_pop_reg (code, X86_EAX);
3831 if (cfg->opt & MONO_OPT_FCMOV) {
3832 guchar *jump = code;
3833 x86_branch8 (code, X86_CC_P, 0, TRUE);
3834 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3835 x86_patch (jump, code);
3838 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3839 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
3842 /* Branch if C013 != 100 */
3843 if (cfg->opt & MONO_OPT_FCMOV) {
3844 /* branch if !ZF or (PF|CF) */
3845 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3846 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3847 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
3850 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3851 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3854 if (cfg->opt & MONO_OPT_FCMOV) {
3855 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3858 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3861 if (cfg->opt & MONO_OPT_FCMOV) {
3862 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3863 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3866 if (ins->opcode == OP_FBLT_UN) {
3867 guchar *is_not_zero_check, *end_jump;
3868 is_not_zero_check = code;
3869 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3871 x86_jump8 (code, 0);
3872 x86_patch (is_not_zero_check, code);
3873 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3875 x86_patch (end_jump, code);
3877 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3881 if (cfg->opt & MONO_OPT_FCMOV) {
3882 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3885 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3886 if (ins->opcode == OP_FBGT_UN) {
3887 guchar *is_not_zero_check, *end_jump;
3888 is_not_zero_check = code;
3889 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3891 x86_jump8 (code, 0);
3892 x86_patch (is_not_zero_check, code);
3893 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3895 x86_patch (end_jump, code);
3897 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3900 /* Branch if C013 == 100 or 001 */
3901 if (cfg->opt & MONO_OPT_FCMOV) {
3904 /* skip branch if C1=1 */
3906 x86_branch8 (code, X86_CC_P, 0, FALSE);
3907 /* branch if (C0 | C3) = 1 */
3908 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
3909 x86_patch (br1, code);
3912 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3913 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3914 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3915 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3918 /* Branch if C013 == 000 */
3919 if (cfg->opt & MONO_OPT_FCMOV) {
3920 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
3923 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3926 /* Branch if C013=000 or 100 */
3927 if (cfg->opt & MONO_OPT_FCMOV) {
3930 /* skip branch if C1=1 */
3932 x86_branch8 (code, X86_CC_P, 0, FALSE);
3933 /* branch if C0=0 */
3934 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
3935 x86_patch (br1, code);
3938 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
3939 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
3940 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3943 /* Branch if C013 != 001 */
3944 if (cfg->opt & MONO_OPT_FCMOV) {
3945 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3946 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
3949 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3950 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3952 case CEE_CKFINITE: {
3953 x86_push_reg (code, X86_EAX);
3956 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
3957 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3958 x86_pop_reg (code, X86_EAX);
3959 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
3963 x86_prefix (code, X86_GS_PREFIX);
3964 x86_mov_reg_mem (code, ins->dreg, ins->inst_offset, 4);
3967 case OP_ATOMIC_ADD_I4: {
3968 int dreg = ins->dreg;
3970 if (dreg == ins->inst_basereg) {
3971 x86_push_reg (code, ins->sreg2);
3975 if (dreg != ins->sreg2)
3976 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
3978 x86_prefix (code, X86_LOCK_PREFIX);
3979 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3981 if (dreg != ins->dreg) {
3982 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3983 x86_pop_reg (code, dreg);
3988 case OP_ATOMIC_ADD_NEW_I4: {
3989 int dreg = ins->dreg;
3991 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
3992 if (ins->sreg2 == dreg) {
3993 if (dreg == X86_EBX) {
3995 if (ins->inst_basereg == X86_EDI)
3999 if (ins->inst_basereg == X86_EBX)
4002 } else if (ins->inst_basereg == dreg) {
4003 if (dreg == X86_EBX) {
4005 if (ins->sreg2 == X86_EDI)
4009 if (ins->sreg2 == X86_EBX)
4014 if (dreg != ins->dreg) {
4015 x86_push_reg (code, dreg);
4018 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
4019 x86_prefix (code, X86_LOCK_PREFIX);
4020 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4021 /* dreg contains the old value, add with sreg2 value */
4022 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
4024 if (ins->dreg != dreg) {
4025 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4026 x86_pop_reg (code, dreg);
4031 case OP_ATOMIC_EXCHANGE_I4: {
4033 int sreg2 = ins->sreg2;
4034 int breg = ins->inst_basereg;
4036 /* cmpxchg uses eax as comperand, need to make sure we can use it
4037 * hack to overcome limits in x86 reg allocator
4038 * (req: dreg == eax and sreg2 != eax and breg != eax)
4040 if (ins->dreg != X86_EAX)
4041 x86_push_reg (code, X86_EAX);
4043 /* We need the EAX reg for the cmpxchg */
4044 if (ins->sreg2 == X86_EAX) {
4045 x86_push_reg (code, X86_EDX);
4046 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
4050 if (breg == X86_EAX) {
4051 x86_push_reg (code, X86_ESI);
4052 x86_mov_reg_reg (code, X86_ESI, X86_EAX, 4);
4056 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
4058 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
4059 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
4060 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4061 x86_patch (br [1], br [0]);
4063 if (breg != ins->inst_basereg)
4064 x86_pop_reg (code, X86_ESI);
4066 if (ins->dreg != X86_EAX) {
4067 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
4068 x86_pop_reg (code, X86_EAX);
4071 if (ins->sreg2 != sreg2)
4072 x86_pop_reg (code, X86_EDX);
4077 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4078 g_assert_not_reached ();
4081 if ((code - cfg->native_code - offset) > max_len) {
4082 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4083 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4084 g_assert_not_reached ();
4090 last_offset = offset;
4095 cfg->code_len = code - cfg->native_code;
4099 mono_arch_register_lowlevel_calls (void)
4104 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
4106 MonoJumpInfo *patch_info;
4107 gboolean compile_aot = !run_cctors;
4109 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4110 unsigned char *ip = patch_info->ip.i + code;
4111 const unsigned char *target;
4113 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4116 switch (patch_info->type) {
4117 case MONO_PATCH_INFO_BB:
4118 case MONO_PATCH_INFO_LABEL:
4121 /* No need to patch these */
4126 switch (patch_info->type) {
4127 case MONO_PATCH_INFO_IP:
4128 *((gconstpointer *)(ip)) = target;
4130 case MONO_PATCH_INFO_CLASS_INIT: {
4132 /* Might already been changed to a nop */
4133 x86_call_code (code, 0);
4134 x86_patch (ip, target);
4137 case MONO_PATCH_INFO_ABS:
4138 case MONO_PATCH_INFO_METHOD:
4139 case MONO_PATCH_INFO_METHOD_JUMP:
4140 case MONO_PATCH_INFO_INTERNAL_METHOD:
4141 case MONO_PATCH_INFO_BB:
4142 case MONO_PATCH_INFO_LABEL:
4143 x86_patch (ip, target);
4145 case MONO_PATCH_INFO_NONE:
4148 guint32 offset = mono_arch_get_patch_offset (ip);
4149 *((gconstpointer *)(ip + offset)) = target;
4157 mono_arch_emit_prolog (MonoCompile *cfg)
4159 MonoMethod *method = cfg->method;
4161 MonoMethodSignature *sig;
4163 int alloc_size, pos, max_offset, i;
4166 cfg->code_size = MAX (mono_method_get_header (method)->code_size * 4, 256);
4167 code = cfg->native_code = g_malloc (cfg->code_size);
4169 x86_push_reg (code, X86_EBP);
4170 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
4172 alloc_size = - cfg->stack_offset;
4175 if (method->save_lmf) {
4176 pos += sizeof (MonoLMF);
4178 /* save the current IP */
4179 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
4180 x86_push_imm_template (code);
4182 /* save all caller saved regs */
4183 x86_push_reg (code, X86_EBP);
4184 x86_push_reg (code, X86_ESI);
4185 x86_push_reg (code, X86_EDI);
4186 x86_push_reg (code, X86_EBX);
4188 /* save method info */
4189 x86_push_imm (code, method);
4191 /* get the address of lmf for the current thread */
4193 * This is performance critical so we try to use some tricks to make
4196 if (lmf_tls_offset != -1) {
4197 /* Load lmf quicky using the GS register */
4198 x86_prefix (code, X86_GS_PREFIX);
4199 x86_mov_reg_mem (code, X86_EAX, lmf_tls_offset, 4);
4202 if (cfg->compile_aot) {
4203 /* The GOT var does not exist yet */
4204 x86_call_imm (code, 0);
4205 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
4206 x86_pop_reg (code, X86_EAX);
4207 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 0);
4208 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
4209 x86_call_membase (code, X86_EAX, 0xf0f0f0f0);
4212 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
4216 x86_push_reg (code, X86_EAX);
4217 /* push *lfm (previous_lmf) */
4218 x86_push_membase (code, X86_EAX, 0);
4220 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
4223 if (cfg->used_int_regs & (1 << X86_EBX)) {
4224 x86_push_reg (code, X86_EBX);
4228 if (cfg->used_int_regs & (1 << X86_EDI)) {
4229 x86_push_reg (code, X86_EDI);
4233 if (cfg->used_int_regs & (1 << X86_ESI)) {
4234 x86_push_reg (code, X86_ESI);
4242 /* See mono_emit_stack_alloc */
4243 #ifdef PLATFORM_WIN32
4244 guint32 remaining_size = alloc_size;
4245 while (remaining_size >= 0x1000) {
4246 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
4247 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
4248 remaining_size -= 0x1000;
4251 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
4253 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
4257 /* compute max_offset in order to use short forward jumps */
4259 if (cfg->opt & MONO_OPT_BRANCH) {
4260 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4261 MonoInst *ins = bb->code;
4262 bb->max_offset = max_offset;
4264 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4266 /* max alignment for loops */
4267 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
4268 max_offset += LOOP_ALIGNMENT;
4271 if (ins->opcode == OP_LABEL)
4272 ins->inst_c1 = max_offset;
4274 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
4280 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4281 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
4283 /* load arguments allocated to register from the stack */
4284 sig = mono_method_signature (method);
4287 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4288 inst = cfg->varinfo [pos];
4289 if (inst->opcode == OP_REGVAR) {
4290 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
4291 if (cfg->verbose_level > 2)
4292 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
4297 cfg->code_len = code - cfg->native_code;
4303 mono_arch_emit_epilog (MonoCompile *cfg)
4305 MonoMethod *method = cfg->method;
4306 MonoMethodSignature *sig = mono_method_signature (method);
4308 guint32 stack_to_pop;
4310 int max_epilog_size = 16;
4313 if (cfg->method->save_lmf)
4314 max_epilog_size += 128;
4316 if (mono_jit_trace_calls != NULL)
4317 max_epilog_size += 50;
4319 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4320 cfg->code_size *= 2;
4321 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4322 mono_jit_stats.code_reallocs++;
4325 code = cfg->native_code + cfg->code_len;
4327 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4328 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
4330 /* the code restoring the registers must be kept in sync with CEE_JMP */
4333 if (method->save_lmf) {
4334 gint32 prev_lmf_reg;
4336 /* Find a spare register */
4337 switch (sig->ret->type) {
4340 prev_lmf_reg = X86_EDI;
4341 cfg->used_int_regs |= (1 << X86_EDI);
4344 prev_lmf_reg = X86_EDX;
4348 /* reg = previous_lmf */
4349 x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, -32, 4);
4352 x86_mov_reg_membase (code, X86_ECX, X86_EBP, -28, 4);
4354 /* *(lmf) = previous_lmf */
4355 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
4357 /* restore caller saved regs */
4358 if (cfg->used_int_regs & (1 << X86_EBX)) {
4359 x86_mov_reg_membase (code, X86_EBX, X86_EBP, -20, 4);
4362 if (cfg->used_int_regs & (1 << X86_EDI)) {
4363 x86_mov_reg_membase (code, X86_EDI, X86_EBP, -16, 4);
4365 if (cfg->used_int_regs & (1 << X86_ESI)) {
4366 x86_mov_reg_membase (code, X86_ESI, X86_EBP, -12, 4);
4369 /* EBP is restored by LEAVE */
4371 if (cfg->used_int_regs & (1 << X86_EBX)) {
4374 if (cfg->used_int_regs & (1 << X86_EDI)) {
4377 if (cfg->used_int_regs & (1 << X86_ESI)) {
4382 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
4384 if (cfg->used_int_regs & (1 << X86_ESI)) {
4385 x86_pop_reg (code, X86_ESI);
4387 if (cfg->used_int_regs & (1 << X86_EDI)) {
4388 x86_pop_reg (code, X86_EDI);
4390 if (cfg->used_int_regs & (1 << X86_EBX)) {
4391 x86_pop_reg (code, X86_EBX);
4395 /* Load returned vtypes into registers if needed */
4396 cinfo = get_call_info (sig, FALSE);
4397 if (cinfo->ret.storage == ArgValuetypeInReg) {
4398 for (quad = 0; quad < 2; quad ++) {
4399 switch (cinfo->ret.pair_storage [quad]) {
4401 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
4403 case ArgOnFloatFpStack:
4404 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
4406 case ArgOnDoubleFpStack:
4407 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
4412 g_assert_not_reached ();
4419 if (CALLCONV_IS_STDCALL (sig)) {
4420 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
4422 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
4423 } else if (MONO_TYPE_ISSTRUCT (mono_method_signature (cfg->method)->ret) && (cinfo->ret.storage == ArgOnStack))
4429 x86_ret_imm (code, stack_to_pop);
4435 cfg->code_len = code - cfg->native_code;
4437 g_assert (cfg->code_len < cfg->code_size);
4441 mono_arch_emit_exceptions (MonoCompile *cfg)
4443 MonoJumpInfo *patch_info;
4446 MonoClass *exc_classes [16];
4447 guint8 *exc_throw_start [16], *exc_throw_end [16];
4451 /* Compute needed space */
4452 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4453 if (patch_info->type == MONO_PATCH_INFO_EXC)
4458 * make sure we have enough space for exceptions
4459 * 16 is the size of two push_imm instructions and a call
4461 if (cfg->compile_aot)
4462 code_size = exc_count * 32;
4464 code_size = exc_count * 16;
4466 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
4467 cfg->code_size *= 2;
4468 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4469 mono_jit_stats.code_reallocs++;
4472 code = cfg->native_code + cfg->code_len;
4475 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4476 switch (patch_info->type) {
4477 case MONO_PATCH_INFO_EXC: {
4478 MonoClass *exc_class;
4482 x86_patch (patch_info->ip.i + cfg->native_code, code);
4484 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4485 g_assert (exc_class);
4486 throw_ip = patch_info->ip.i;
4488 /* Find a throw sequence for the same exception class */
4489 for (i = 0; i < nthrows; ++i)
4490 if (exc_classes [i] == exc_class)
4493 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
4494 x86_jump_code (code, exc_throw_start [i]);
4495 patch_info->type = MONO_PATCH_INFO_NONE;
4498 guint32 got_reg = X86_EAX;
4501 /* Compute size of code following the push <OFFSET> */
4502 if (cfg->compile_aot) {
4506 else if (cfg->got_var->opcode == OP_REGOFFSET)
4512 if ((code - cfg->native_code) - throw_ip < 126 - size) {
4513 /* Use the shorter form */
4515 x86_push_imm (code, 0);
4519 x86_push_imm (code, 0xf0f0f0f0);
4524 exc_classes [nthrows] = exc_class;
4525 exc_throw_start [nthrows] = code;
4528 if (cfg->compile_aot) {
4530 * Since the patches are generated by the back end, there is * no way to generate a got_var at this point.
4532 if (!cfg->got_var) {
4533 x86_call_imm (code, 0);
4534 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
4535 x86_pop_reg (code, X86_EAX);
4536 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 0);
4539 if (cfg->got_var->opcode == OP_REGOFFSET)
4540 x86_mov_reg_membase (code, X86_EAX, cfg->got_var->inst_basereg, cfg->got_var->inst_offset, 4);
4542 got_reg = cfg->got_var->dreg;
4546 x86_push_imm (code, exc_class->type_token);
4547 patch_info->data.name = "mono_arch_throw_corlib_exception";
4548 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4549 patch_info->ip.i = code - cfg->native_code;
4550 if (cfg->compile_aot)
4551 x86_call_membase (code, got_reg, 0xf0f0f0f0);
4553 x86_call_code (code, 0);
4554 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
4559 exc_throw_end [nthrows] = code;
4571 cfg->code_len = code - cfg->native_code;
4573 g_assert (cfg->code_len < cfg->code_size);
4577 mono_arch_flush_icache (guint8 *code, gint size)
4583 mono_arch_flush_register_windows (void)
4588 * Support for fast access to the thread-local lmf structure using the GS
4589 * segment register on NPTL + kernel 2.6.x.
4592 static gboolean tls_offset_inited = FALSE;
4595 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4597 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4598 pthread_t self = pthread_self();
4599 pthread_attr_t attr;
4600 void *staddr = NULL;
4602 struct sigaltstack sa;
4605 if (!tls_offset_inited) {
4606 tls_offset_inited = TRUE;
4607 if (!getenv ("MONO_NO_TLS")) {
4608 appdomain_tls_offset = mono_domain_get_tls_offset ();
4609 lmf_tls_offset = mono_get_lmf_tls_offset ();
4610 thread_tls_offset = mono_thread_get_tls_offset ();
4614 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4616 /* Determine stack boundaries */
4617 if (!mono_running_on_valgrind ()) {
4618 #ifdef HAVE_PTHREAD_GETATTR_NP
4619 pthread_getattr_np( self, &attr );
4621 #ifdef HAVE_PTHREAD_ATTR_GET_NP
4622 pthread_attr_get_np( self, &attr );
4624 pthread_attr_init( &attr );
4625 pthread_attr_getstacksize( &attr, &stsize );
4627 #error "Not implemented"
4631 pthread_attr_getstack( &attr, &staddr, &stsize );
4636 * staddr seems to be wrong for the main thread, so we keep the value in
4639 tls->stack_size = stsize;
4641 /* Setup an alternate signal stack */
4642 tls->signal_stack = g_malloc (SIGNAL_STACK_SIZE);
4643 tls->signal_stack_size = SIGNAL_STACK_SIZE;
4645 sa.ss_sp = tls->signal_stack;
4646 sa.ss_size = SIGNAL_STACK_SIZE;
4647 sa.ss_flags = SS_ONSTACK;
4648 sigaltstack (&sa, NULL);
4653 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4655 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4656 struct sigaltstack sa;
4658 sa.ss_sp = tls->signal_stack;
4659 sa.ss_size = SIGNAL_STACK_SIZE;
4660 sa.ss_flags = SS_DISABLE;
4661 sigaltstack (&sa, NULL);
4663 if (tls->signal_stack)
4664 g_free (tls->signal_stack);
4669 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
4672 /* add the this argument */
4673 if (this_reg != -1) {
4675 MONO_INST_NEW (cfg, this, OP_OUTARG);
4676 this->type = this_type;
4677 this->sreg1 = this_reg;
4678 mono_bblock_add_inst (cfg->cbb, this);
4682 CallInfo * cinfo = get_call_info (inst->signature, FALSE);
4685 if (cinfo->ret.storage == ArgValuetypeInReg) {
4687 * The valuetype is in EAX:EDX after the call, needs to be copied to
4688 * the stack. Save the address here, so the call instruction can
4691 MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
4692 vtarg->inst_destbasereg = X86_ESP;
4693 vtarg->inst_offset = inst->stack_usage;
4694 vtarg->sreg1 = vt_reg;
4695 mono_bblock_add_inst (cfg->cbb, vtarg);
4699 MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
4700 vtarg->type = STACK_MP;
4701 vtarg->sreg1 = vt_reg;
4702 mono_bblock_add_inst (cfg->cbb, vtarg);
4711 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4713 MonoInst *ins = NULL;
4715 if (cmethod->klass == mono_defaults.math_class) {
4716 if (strcmp (cmethod->name, "Sin") == 0) {
4717 MONO_INST_NEW (cfg, ins, OP_SIN);
4718 ins->inst_i0 = args [0];
4719 } else if (strcmp (cmethod->name, "Cos") == 0) {
4720 MONO_INST_NEW (cfg, ins, OP_COS);
4721 ins->inst_i0 = args [0];
4722 } else if (strcmp (cmethod->name, "Tan") == 0) {
4723 MONO_INST_NEW (cfg, ins, OP_TAN);
4724 ins->inst_i0 = args [0];
4725 } else if (strcmp (cmethod->name, "Atan") == 0) {
4726 MONO_INST_NEW (cfg, ins, OP_ATAN);
4727 ins->inst_i0 = args [0];
4728 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
4729 MONO_INST_NEW (cfg, ins, OP_SQRT);
4730 ins->inst_i0 = args [0];
4731 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
4732 MONO_INST_NEW (cfg, ins, OP_ABS);
4733 ins->inst_i0 = args [0];
4736 /* OP_FREM is not IEEE compatible */
4737 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
4738 MONO_INST_NEW (cfg, ins, OP_FREM);
4739 ins->inst_i0 = args [0];
4740 ins->inst_i1 = args [1];
4743 } else if(cmethod->klass->image == mono_defaults.corlib &&
4744 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
4745 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
4747 if (strcmp (cmethod->name, "Increment") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4748 MonoInst *ins_iconst;
4750 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
4751 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4752 ins_iconst->inst_c0 = 1;
4754 ins->inst_i0 = args [0];
4755 ins->inst_i1 = ins_iconst;
4756 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4757 MonoInst *ins_iconst;
4759 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
4760 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4761 ins_iconst->inst_c0 = -1;
4763 ins->inst_i0 = args [0];
4764 ins->inst_i1 = ins_iconst;
4765 } else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4766 MONO_INST_NEW (cfg, ins, OP_ATOMIC_EXCHANGE_I4);
4768 ins->inst_i0 = args [0];
4769 ins->inst_i1 = args [1];
4770 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4771 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_I4);
4773 ins->inst_i0 = args [0];
4774 ins->inst_i1 = args [1];
4783 mono_arch_print_tree (MonoInst *tree, int arity)
4788 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4792 if (appdomain_tls_offset == -1)
4795 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
4796 ins->inst_offset = appdomain_tls_offset;
4800 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
4804 if (thread_tls_offset == -1)
4807 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
4808 ins->inst_offset = thread_tls_offset;
4813 mono_arch_get_patch_offset (guint8 *code)
4815 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
4817 else if ((code [0] == 0xba))
4819 else if ((code [0] == 0x68))
4822 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
4823 /* push <OFFSET>(<REG>) */
4825 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
4826 /* call *<OFFSET>(<REG>) */
4828 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
4831 else if ((code [0] == 0x58) && (code [1] == 0x05))
4832 /* pop %eax; add <OFFSET>, %eax */
4834 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
4835 /* pop <REG>; add <OFFSET>, <REG> */
4838 g_assert_not_reached ();