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 /* On windows, these hold the key returned by TlsAlloc () */
27 static gint lmf_tls_offset = -1;
28 static gint appdomain_tls_offset = -1;
29 static gint thread_tls_offset = -1;
31 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
34 /* Under windows, the default pinvoke calling convention is stdcall */
35 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
37 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
40 #define SIGNAL_STACK_SIZE (64 * 1024)
42 #define NOT_IMPLEMENTED g_assert_not_reached ()
45 mono_arch_regname (int reg) {
47 case X86_EAX: return "%eax";
48 case X86_EBX: return "%ebx";
49 case X86_ECX: return "%ecx";
50 case X86_EDX: return "%edx";
51 case X86_ESP: return "%esp"; case X86_EBP: return "%ebp";
52 case X86_EDI: return "%edi";
53 case X86_ESI: return "%esi";
74 /* Only if storage == ArgValuetypeInReg */
75 ArgStorage pair_storage [2];
84 gboolean need_stack_align;
92 #define FLOAT_PARAM_REGS 0
94 static X86_Reg_No param_regs [] = { };
97 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
101 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
103 ainfo->offset = *stack_size;
105 if (*gr >= PARAM_REGS) {
106 ainfo->storage = ArgOnStack;
107 (*stack_size) += sizeof (gpointer);
110 ainfo->storage = ArgInIReg;
111 ainfo->reg = param_regs [*gr];
117 add_general_pair (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
119 ainfo->offset = *stack_size;
121 g_assert (PARAM_REGS == 0);
123 ainfo->storage = ArgOnStack;
124 (*stack_size) += sizeof (gpointer) * 2;
128 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
130 ainfo->offset = *stack_size;
132 if (*gr >= FLOAT_PARAM_REGS) {
133 ainfo->storage = ArgOnStack;
134 (*stack_size) += sizeof (gpointer);
137 /* A double register */
139 ainfo->storage = ArgInDoubleSSEReg;
141 ainfo->storage = ArgInFloatSSEReg;
149 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
151 guint32 *gr, guint32 *fr, guint32 *stack_size)
156 klass = mono_class_from_mono_type (type);
158 size = mono_type_native_stack_size (&klass->byval_arg, NULL);
160 size = mono_type_stack_size (&klass->byval_arg, NULL);
162 #ifdef PLATFORM_WIN32
163 if (sig->pinvoke && is_return) {
164 MonoMarshalType *info;
167 * the exact rules are not very well documented, the code below seems to work with the
168 * code generated by gcc 3.3.3 -mno-cygwin.
170 info = mono_marshal_load_type_info (klass);
173 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
175 /* Special case structs with only a float member */
176 if ((info->native_size == 8) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R8)) {
177 ainfo->storage = ArgValuetypeInReg;
178 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
181 if ((info->native_size == 4) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R4)) {
182 ainfo->storage = ArgValuetypeInReg;
183 ainfo->pair_storage [0] = ArgOnFloatFpStack;
186 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
187 ainfo->storage = ArgValuetypeInReg;
188 ainfo->pair_storage [0] = ArgInIReg;
189 ainfo->pair_regs [0] = return_regs [0];
190 if (info->native_size > 4) {
191 ainfo->pair_storage [1] = ArgInIReg;
192 ainfo->pair_regs [1] = return_regs [1];
199 ainfo->offset = *stack_size;
200 ainfo->storage = ArgOnStack;
201 *stack_size += ALIGN_TO (size, sizeof (gpointer));
207 * Obtain information about a call according to the calling convention.
208 * For x86 ELF, see the "System V Application Binary Interface Intel386
209 * Architecture Processor Supplment, Fourth Edition" document for more
211 * For x86 win32, see ???.
214 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
218 int n = sig->hasthis + sig->param_count;
219 guint32 stack_size = 0;
222 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
229 ret_type = mono_type_get_underlying_type (sig->ret);
230 switch (ret_type->type) {
231 case MONO_TYPE_BOOLEAN:
242 case MONO_TYPE_FNPTR:
243 case MONO_TYPE_CLASS:
244 case MONO_TYPE_OBJECT:
245 case MONO_TYPE_SZARRAY:
246 case MONO_TYPE_ARRAY:
247 case MONO_TYPE_STRING:
248 cinfo->ret.storage = ArgInIReg;
249 cinfo->ret.reg = X86_EAX;
253 cinfo->ret.storage = ArgInIReg;
254 cinfo->ret.reg = X86_EAX;
257 cinfo->ret.storage = ArgOnFloatFpStack;
260 cinfo->ret.storage = ArgOnDoubleFpStack;
262 case MONO_TYPE_VALUETYPE: {
263 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
265 add_valuetype (sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
266 if (cinfo->ret.storage == ArgOnStack)
267 /* The caller passes the address where the value is stored */
268 add_general (&gr, &stack_size, &cinfo->ret);
271 case MONO_TYPE_TYPEDBYREF:
272 /* Same as a valuetype with size 24 */
273 add_general (&gr, &stack_size, &cinfo->ret);
277 cinfo->ret.storage = ArgNone;
280 g_error ("Can't handle as return value 0x%x", sig->ret->type);
286 add_general (&gr, &stack_size, cinfo->args + 0);
288 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
290 fr = FLOAT_PARAM_REGS;
292 /* Emit the signature cookie just before the implicit arguments */
293 add_general (&gr, &stack_size, &cinfo->sig_cookie);
296 for (i = 0; i < sig->param_count; ++i) {
297 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
300 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
301 /* We allways pass the sig cookie on the stack for simplicity */
303 * Prevent implicit arguments + the sig cookie from being passed
307 fr = FLOAT_PARAM_REGS;
309 /* Emit the signature cookie just before the implicit arguments */
310 add_general (&gr, &stack_size, &cinfo->sig_cookie);
313 if (sig->params [i]->byref) {
314 add_general (&gr, &stack_size, ainfo);
317 ptype = mono_type_get_underlying_type (sig->params [i]);
318 switch (ptype->type) {
319 case MONO_TYPE_BOOLEAN:
322 add_general (&gr, &stack_size, ainfo);
327 add_general (&gr, &stack_size, ainfo);
331 add_general (&gr, &stack_size, ainfo);
336 case MONO_TYPE_FNPTR:
337 case MONO_TYPE_CLASS:
338 case MONO_TYPE_OBJECT:
339 case MONO_TYPE_STRING:
340 case MONO_TYPE_SZARRAY:
341 case MONO_TYPE_ARRAY:
342 add_general (&gr, &stack_size, ainfo);
344 case MONO_TYPE_VALUETYPE:
345 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
347 case MONO_TYPE_TYPEDBYREF:
348 stack_size += sizeof (MonoTypedRef);
349 ainfo->storage = ArgOnStack;
353 add_general_pair (&gr, &stack_size, ainfo);
356 add_float (&fr, &stack_size, ainfo, FALSE);
359 add_float (&fr, &stack_size, ainfo, TRUE);
362 g_error ("unexpected type 0x%x", ptype->type);
363 g_assert_not_reached ();
367 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
369 fr = FLOAT_PARAM_REGS;
371 /* Emit the signature cookie just before the implicit arguments */
372 add_general (&gr, &stack_size, &cinfo->sig_cookie);
375 cinfo->stack_usage = stack_size;
376 cinfo->reg_usage = gr;
377 cinfo->freg_usage = fr;
382 * mono_arch_get_argument_info:
383 * @csig: a method signature
384 * @param_count: the number of parameters to consider
385 * @arg_info: an array to store the result infos
387 * Gathers information on parameters such as size, alignment and
388 * padding. arg_info should be large enought to hold param_count + 1 entries.
390 * Returns the size of the activation frame.
393 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
395 int k, frame_size = 0;
396 int size, align, pad;
400 cinfo = get_call_info (csig, FALSE);
402 if (MONO_TYPE_ISSTRUCT (csig->ret) && (cinfo->ret.storage == ArgOnStack)) {
403 frame_size += sizeof (gpointer);
407 arg_info [0].offset = offset;
410 frame_size += sizeof (gpointer);
414 arg_info [0].size = frame_size;
416 for (k = 0; k < param_count; k++) {
419 size = mono_type_native_stack_size (csig->params [k], &align);
421 size = mono_type_stack_size (csig->params [k], &align);
423 /* ignore alignment for now */
426 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
427 arg_info [k].pad = pad;
429 arg_info [k + 1].pad = 0;
430 arg_info [k + 1].size = size;
432 arg_info [k + 1].offset = offset;
436 align = MONO_ARCH_FRAME_ALIGNMENT;
437 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
438 arg_info [k].pad = pad;
445 static const guchar cpuid_impl [] = {
446 0x55, /* push %ebp */
447 0x89, 0xe5, /* mov %esp,%ebp */
448 0x53, /* push %ebx */
449 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
450 0x0f, 0xa2, /* cpuid */
451 0x50, /* push %eax */
452 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
453 0x89, 0x18, /* mov %ebx,(%eax) */
454 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
455 0x89, 0x08, /* mov %ecx,(%eax) */
456 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
457 0x89, 0x10, /* mov %edx,(%eax) */
459 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
460 0x89, 0x02, /* mov %eax,(%edx) */
466 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
469 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
472 __asm__ __volatile__ (
475 "movl %%eax, %%edx\n"
476 "xorl $0x200000, %%eax\n"
481 "xorl %%edx, %%eax\n"
482 "andl $0x200000, %%eax\n"
490 /* Have to use the code manager to get around WinXP DEP */
491 MonoCodeManager *codeman = mono_code_manager_new_dynamic ();
493 void *ptr = mono_code_manager_reserve (codeman, sizeof (cpuid_impl));
494 memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
496 func = (CpuidFunc)ptr;
497 func (id, p_eax, p_ebx, p_ecx, p_edx);
499 mono_code_manager_destroy (codeman);
502 * We use this approach because of issues with gcc and pic code, see:
503 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
504 __asm__ __volatile__ ("cpuid"
505 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
514 * Initialize the cpu to execute managed code.
517 mono_arch_cpu_init (void)
521 /* spec compliance requires running with double precision */
522 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
523 fpcw &= ~X86_FPCW_PRECC_MASK;
524 fpcw |= X86_FPCW_PREC_DOUBLE;
525 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
526 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
530 * This function returns the optimizations supported on this cpu.
533 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
535 int eax, ebx, ecx, edx;
539 /* Feature Flags function, flags returned in EDX. */
540 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
541 if (edx & (1 << 15)) {
542 opts |= MONO_OPT_CMOV;
544 opts |= MONO_OPT_FCMOV;
546 *exclude_mask |= MONO_OPT_FCMOV;
548 *exclude_mask |= MONO_OPT_CMOV;
554 * Determine whenever the trap whose info is in SIGINFO is caused by
558 mono_arch_is_int_overflow (void *sigctx, void *info)
560 struct sigcontext *ctx = (struct sigcontext*)sigctx;
563 ip = (guint8*)ctx->SC_EIP;
565 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
569 switch (x86_modrm_rm (ip [1])) {
577 g_assert_not_reached ();
589 is_regsize_var (MonoType *t) {
592 switch (mono_type_get_underlying_type (t)->type) {
598 case MONO_TYPE_FNPTR:
600 case MONO_TYPE_OBJECT:
601 case MONO_TYPE_STRING:
602 case MONO_TYPE_CLASS:
603 case MONO_TYPE_SZARRAY:
604 case MONO_TYPE_ARRAY:
606 case MONO_TYPE_VALUETYPE:
613 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
618 for (i = 0; i < cfg->num_varinfo; i++) {
619 MonoInst *ins = cfg->varinfo [i];
620 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
623 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
626 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
627 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
630 /* we dont allocate I1 to registers because there is no simply way to sign extend
631 * 8bit quantities in caller saved registers on x86 */
632 if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) ||
633 (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
634 (ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
635 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
636 g_assert (i == vmv->idx);
637 vars = g_list_prepend (vars, vmv);
641 vars = mono_varlist_sort (cfg, vars, 0);
647 mono_arch_get_global_int_regs (MonoCompile *cfg)
651 /* we can use 3 registers for global allocation */
652 regs = g_list_prepend (regs, (gpointer)X86_EBX);
653 regs = g_list_prepend (regs, (gpointer)X86_ESI);
654 regs = g_list_prepend (regs, (gpointer)X86_EDI);
660 * mono_arch_regalloc_cost:
662 * Return the cost, in number of memory references, of the action of
663 * allocating the variable VMV into a register during global register
667 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
669 MonoInst *ins = cfg->varinfo [vmv->idx];
671 if (cfg->method->save_lmf)
672 /* The register is already saved */
673 return (ins->opcode == OP_ARG) ? 1 : 0;
675 /* push+pop+possible load if it is an argument */
676 return (ins->opcode == OP_ARG) ? 3 : 2;
680 * Set var information according to the calling convention. X86 version.
681 * The locals var stuff should most likely be split in another method.
684 mono_arch_allocate_vars (MonoCompile *m)
686 MonoMethodSignature *sig;
687 MonoMethodHeader *header;
689 guint32 locals_stack_size, locals_stack_align;
690 int i, offset, curinst, size, align;
694 header = mono_method_get_header (m->method);
695 sig = mono_method_signature (m->method);
700 cinfo = get_call_info (sig, FALSE);
702 switch (cinfo->ret.storage) {
704 m->ret->opcode = OP_REGOFFSET;
705 m->ret->inst_basereg = X86_EBP;
706 m->ret->inst_offset = offset;
707 offset += sizeof (gpointer);
709 case ArgValuetypeInReg:
712 m->ret->opcode = OP_REGVAR;
713 m->ret->inst_c0 = cinfo->ret.reg;
716 case ArgOnFloatFpStack:
717 case ArgOnDoubleFpStack:
720 g_assert_not_reached ();
724 inst = m->varinfo [curinst];
725 if (inst->opcode != OP_REGVAR) {
726 inst->opcode = OP_REGOFFSET;
727 inst->inst_basereg = X86_EBP;
729 inst->inst_offset = offset;
730 offset += sizeof (gpointer);
734 if (sig->call_convention == MONO_CALL_VARARG) {
735 m->sig_cookie = offset;
736 offset += sizeof (gpointer);
739 for (i = 0; i < sig->param_count; ++i) {
740 inst = m->varinfo [curinst];
741 if (inst->opcode != OP_REGVAR) {
742 inst->opcode = OP_REGOFFSET;
743 inst->inst_basereg = X86_EBP;
745 inst->inst_offset = offset;
746 size = mono_type_size (sig->params [i], &align);
755 /* reserve space to save LMF and caller saved registers */
757 if (m->method->save_lmf) {
758 offset += sizeof (MonoLMF);
760 if (m->used_int_regs & (1 << X86_EBX)) {
764 if (m->used_int_regs & (1 << X86_EDI)) {
768 if (m->used_int_regs & (1 << X86_ESI)) {
773 switch (cinfo->ret.storage) {
774 case ArgValuetypeInReg:
775 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
777 m->ret->opcode = OP_REGOFFSET;
778 m->ret->inst_basereg = X86_EBP;
779 m->ret->inst_offset = - offset;
785 /* Allocate locals */
786 offsets = mono_allocate_stack_slots (m, &locals_stack_size, &locals_stack_align);
787 if (locals_stack_align) {
788 offset += (locals_stack_align - 1);
789 offset &= ~(locals_stack_align - 1);
791 for (i = m->locals_start; i < m->num_varinfo; i++) {
792 if (offsets [i] != -1) {
793 MonoInst *inst = m->varinfo [i];
794 inst->opcode = OP_REGOFFSET;
795 inst->inst_basereg = X86_EBP;
796 inst->inst_offset = - (offset + offsets [i]);
797 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
801 offset += locals_stack_size;
803 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
804 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
809 m->stack_offset = -offset;
813 mono_arch_create_vars (MonoCompile *cfg)
815 MonoMethodSignature *sig;
818 sig = mono_method_signature (cfg->method);
820 cinfo = get_call_info (sig, FALSE);
822 if (cinfo->ret.storage == ArgValuetypeInReg)
823 cfg->ret_var_is_local = TRUE;
828 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
829 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
833 * take the arguments and generate the arch-specific
834 * instructions to properly call the function in call.
835 * This includes pushing, moving arguments to the right register
837 * Issue: who does the spilling if needed, and when?
840 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
842 MonoMethodSignature *sig;
843 int i, n, stack_size, type;
848 /* add the vararg cookie before the non-implicit args */
849 if (call->signature->call_convention == MONO_CALL_VARARG) {
851 /* FIXME: Add support for signature tokens to AOT */
852 cfg->disable_aot = TRUE;
853 MONO_INST_NEW (cfg, arg, OP_OUTARG);
854 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
855 sig_arg->inst_p0 = call->signature;
856 arg->inst_left = sig_arg;
857 arg->type = STACK_PTR;
858 /* prepend, so they get reversed */
859 arg->next = call->out_args;
860 call->out_args = arg;
861 stack_size += sizeof (gpointer);
863 sig = call->signature;
864 n = sig->param_count + sig->hasthis;
866 cinfo = get_call_info (sig, FALSE);
868 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
869 if (cinfo->ret.storage == ArgOnStack)
870 stack_size += sizeof (gpointer);
873 for (i = 0; i < n; ++i) {
874 if (is_virtual && i == 0) {
875 /* the argument will be attached to the call instrucion */
879 MONO_INST_NEW (cfg, arg, OP_OUTARG);
881 arg->cil_code = in->cil_code;
883 arg->type = in->type;
884 /* prepend, so they get reversed */
885 arg->next = call->out_args;
886 call->out_args = arg;
887 if (i >= sig->hasthis) {
888 MonoType *t = sig->params [i - sig->hasthis];
889 ptype = mono_type_get_underlying_type (t);
894 /* FIXME: validate arguments... */
898 case MONO_TYPE_BOOLEAN:
906 case MONO_TYPE_STRING:
907 case MONO_TYPE_CLASS:
908 case MONO_TYPE_OBJECT:
910 case MONO_TYPE_FNPTR:
911 case MONO_TYPE_ARRAY:
912 case MONO_TYPE_SZARRAY:
921 arg->opcode = OP_OUTARG_R4;
925 arg->opcode = OP_OUTARG_R8;
927 case MONO_TYPE_VALUETYPE: {
930 size = mono_type_native_stack_size (&in->klass->byval_arg, NULL);
932 size = mono_type_stack_size (&in->klass->byval_arg, NULL);
935 arg->opcode = OP_OUTARG_VT;
936 arg->klass = in->klass;
937 arg->unused = sig->pinvoke;
938 arg->inst_imm = size;
941 case MONO_TYPE_TYPEDBYREF:
942 stack_size += sizeof (MonoTypedRef);
943 arg->opcode = OP_OUTARG_VT;
944 arg->klass = in->klass;
945 arg->unused = sig->pinvoke;
946 arg->inst_imm = sizeof (MonoTypedRef);
949 g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
952 /* the this argument */
958 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
959 if (cinfo->ret.storage == ArgValuetypeInReg) {
962 * After the call, the struct is in registers, but needs to be saved to the memory pointed
963 * to by vt_arg in this_vret_args. This means that vt_arg needs to be saved somewhere
964 * before calling the function. So we add a dummy instruction to represent pushing the
965 * struct return address to the stack. The return address will be saved to this stack slot
966 * by the code emitted in this_vret_args.
968 MONO_INST_NEW (cfg, arg, OP_OUTARG);
969 MONO_INST_NEW (cfg, zero_inst, OP_ICONST);
970 zero_inst->inst_p0 = 0;
971 arg->inst_left = zero_inst;
972 arg->type = STACK_PTR;
973 /* prepend, so they get reversed */
974 arg->next = call->out_args;
975 call->out_args = arg;
978 /* if the function returns a struct, the called method already does a ret $0x4 */
979 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
983 call->stack_usage = stack_size;
987 * should set more info in call, such as the stack space
988 * used by the args that needs to be added back to esp
995 * Allow tracing to work with this interface (with an optional argument)
998 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1002 /* if some args are passed in registers, we need to save them here */
1003 x86_push_reg (code, X86_EBP);
1005 if (cfg->compile_aot) {
1006 x86_push_imm (code, cfg->method);
1007 x86_mov_reg_imm (code, X86_EAX, func);
1008 x86_call_reg (code, X86_EAX);
1010 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1011 x86_push_imm (code, cfg->method);
1012 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1013 x86_call_code (code, 0);
1015 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1029 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1032 int arg_size = 0, save_mode = SAVE_NONE;
1033 MonoMethod *method = cfg->method;
1035 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
1036 case MONO_TYPE_VOID:
1037 /* special case string .ctor icall */
1038 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1039 save_mode = SAVE_EAX;
1041 save_mode = SAVE_NONE;
1045 save_mode = SAVE_EAX_EDX;
1049 save_mode = SAVE_FP;
1051 case MONO_TYPE_VALUETYPE:
1052 save_mode = SAVE_STRUCT;
1055 save_mode = SAVE_EAX;
1059 switch (save_mode) {
1061 x86_push_reg (code, X86_EDX);
1062 x86_push_reg (code, X86_EAX);
1063 if (enable_arguments) {
1064 x86_push_reg (code, X86_EDX);
1065 x86_push_reg (code, X86_EAX);
1070 x86_push_reg (code, X86_EAX);
1071 if (enable_arguments) {
1072 x86_push_reg (code, X86_EAX);
1077 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1078 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1079 if (enable_arguments) {
1080 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1081 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1086 if (enable_arguments) {
1087 x86_push_membase (code, X86_EBP, 8);
1096 if (cfg->compile_aot) {
1097 x86_push_imm (code, method);
1098 x86_mov_reg_imm (code, X86_EAX, func);
1099 x86_call_reg (code, X86_EAX);
1101 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1102 x86_push_imm (code, method);
1103 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1104 x86_call_code (code, 0);
1106 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1108 switch (save_mode) {
1110 x86_pop_reg (code, X86_EAX);
1111 x86_pop_reg (code, X86_EDX);
1114 x86_pop_reg (code, X86_EAX);
1117 x86_fld_membase (code, X86_ESP, 0, TRUE);
1118 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1128 #define EMIT_COND_BRANCH(ins,cond,sign) \
1129 if (ins->flags & MONO_INST_BRLABEL) { \
1130 if (ins->inst_i0->inst_c0) { \
1131 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
1133 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1134 if ((cfg->opt & MONO_OPT_BRANCH) && \
1135 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
1136 x86_branch8 (code, cond, 0, sign); \
1138 x86_branch32 (code, cond, 0, sign); \
1141 if (ins->inst_true_bb->native_offset) { \
1142 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1144 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1145 if ((cfg->opt & MONO_OPT_BRANCH) && \
1146 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1147 x86_branch8 (code, cond, 0, sign); \
1149 x86_branch32 (code, cond, 0, sign); \
1153 /* emit an exception if condition is fail */
1154 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1156 mono_add_patch_info (cfg, code - cfg->native_code, \
1157 MONO_PATCH_INFO_EXC, exc_name); \
1158 x86_branch32 (code, cond, 0, signed); \
1161 #define EMIT_FPCOMPARE(code) do { \
1162 x86_fcompp (code); \
1163 x86_fnstsw (code); \
1168 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1170 if (cfg->compile_aot) {
1171 guint32 got_reg = X86_EAX;
1173 if (cfg->compile_aot) {
1175 * Since the patches are generated by the back end, there is
1176 * no way to generate a got_var at this point.
1178 g_assert (cfg->got_var);
1180 if (cfg->got_var->opcode == OP_REGOFFSET)
1181 x86_mov_reg_membase (code, X86_EAX, cfg->got_var->inst_basereg, cfg->got_var->inst_offset, 4);
1183 got_reg = cfg->got_var->dreg;
1186 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1187 x86_call_membase (code, got_reg, 0xf0f0f0f0);
1190 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1191 x86_call_code (code, 0);
1197 /* FIXME: Add more instructions */
1198 #define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_STORE_MEMBASE_IMM) || ((ins)->opcode == OP_STOREI4_MEMBASE_REG))
1201 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1203 MonoInst *ins, *last_ins = NULL;
1208 switch (ins->opcode) {
1210 /* reg = 0 -> XOR (reg, reg) */
1211 /* XOR sets cflags on x86, so we cant do it always */
1212 if (ins->inst_c0 == 0 && ins->next && INST_IGNORES_CFLAGS (ins->next)) {
1213 ins->opcode = CEE_XOR;
1214 ins->sreg1 = ins->dreg;
1215 ins->sreg2 = ins->dreg;
1219 /* remove unnecessary multiplication with 1 */
1220 if (ins->inst_imm == 1) {
1221 if (ins->dreg != ins->sreg1) {
1222 ins->opcode = OP_MOVE;
1224 last_ins->next = ins->next;
1230 case OP_COMPARE_IMM:
1231 /* OP_COMPARE_IMM (reg, 0)
1233 * OP_X86_TEST_NULL (reg)
1236 ins->opcode = OP_X86_TEST_NULL;
1238 case OP_X86_COMPARE_MEMBASE_IMM:
1240 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1241 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1243 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1244 * OP_COMPARE_IMM reg, imm
1246 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1248 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1249 ins->inst_basereg == last_ins->inst_destbasereg &&
1250 ins->inst_offset == last_ins->inst_offset) {
1251 ins->opcode = OP_COMPARE_IMM;
1252 ins->sreg1 = last_ins->sreg1;
1254 /* check if we can remove cmp reg,0 with test null */
1256 ins->opcode = OP_X86_TEST_NULL;
1260 case OP_LOAD_MEMBASE:
1261 case OP_LOADI4_MEMBASE:
1263 * Note: if reg1 = reg2 the load op is removed
1265 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1266 * OP_LOAD_MEMBASE offset(basereg), reg2
1268 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1269 * OP_MOVE reg1, reg2
1271 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1272 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1273 ins->inst_basereg == last_ins->inst_destbasereg &&
1274 ins->inst_offset == last_ins->inst_offset) {
1275 if (ins->dreg == last_ins->sreg1) {
1276 last_ins->next = ins->next;
1280 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1281 ins->opcode = OP_MOVE;
1282 ins->sreg1 = last_ins->sreg1;
1286 * Note: reg1 must be different from the basereg in the second load
1287 * Note: if reg1 = reg2 is equal then second load is removed
1289 * OP_LOAD_MEMBASE offset(basereg), reg1
1290 * OP_LOAD_MEMBASE offset(basereg), reg2
1292 * OP_LOAD_MEMBASE offset(basereg), reg1
1293 * OP_MOVE reg1, reg2
1295 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1296 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1297 ins->inst_basereg != last_ins->dreg &&
1298 ins->inst_basereg == last_ins->inst_basereg &&
1299 ins->inst_offset == last_ins->inst_offset) {
1301 if (ins->dreg == last_ins->dreg) {
1302 last_ins->next = ins->next;
1306 ins->opcode = OP_MOVE;
1307 ins->sreg1 = last_ins->dreg;
1310 //g_assert_not_reached ();
1314 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1315 * OP_LOAD_MEMBASE offset(basereg), reg
1317 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1318 * OP_ICONST reg, imm
1320 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1321 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1322 ins->inst_basereg == last_ins->inst_destbasereg &&
1323 ins->inst_offset == last_ins->inst_offset) {
1324 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1325 ins->opcode = OP_ICONST;
1326 ins->inst_c0 = last_ins->inst_imm;
1327 g_assert_not_reached (); // check this rule
1331 case OP_LOADU1_MEMBASE:
1332 case OP_LOADI1_MEMBASE:
1334 * Note: if reg1 = reg2 the load op is removed
1336 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1337 * OP_LOAD_MEMBASE offset(basereg), reg2
1339 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1340 * OP_MOVE reg1, reg2
1342 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1343 ins->inst_basereg == last_ins->inst_destbasereg &&
1344 ins->inst_offset == last_ins->inst_offset) {
1345 if (ins->dreg == last_ins->sreg1) {
1346 last_ins->next = ins->next;
1350 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1351 ins->opcode = OP_MOVE;
1352 ins->sreg1 = last_ins->sreg1;
1356 case OP_LOADU2_MEMBASE:
1357 case OP_LOADI2_MEMBASE:
1359 * Note: if reg1 = reg2 the load op is removed
1361 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1362 * OP_LOAD_MEMBASE offset(basereg), reg2
1364 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1365 * OP_MOVE reg1, reg2
1367 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1368 ins->inst_basereg == last_ins->inst_destbasereg &&
1369 ins->inst_offset == last_ins->inst_offset) {
1370 if (ins->dreg == last_ins->sreg1) {
1371 last_ins->next = ins->next;
1375 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1376 ins->opcode = OP_MOVE;
1377 ins->sreg1 = last_ins->sreg1;
1389 if (ins->dreg == ins->sreg1) {
1391 last_ins->next = ins->next;
1398 * OP_MOVE sreg, dreg
1399 * OP_MOVE dreg, sreg
1401 if (last_ins && last_ins->opcode == OP_MOVE &&
1402 ins->sreg1 == last_ins->dreg &&
1403 ins->dreg == last_ins->sreg1) {
1404 last_ins->next = ins->next;
1410 case OP_X86_PUSH_MEMBASE:
1411 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1412 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1413 ins->inst_basereg == last_ins->inst_destbasereg &&
1414 ins->inst_offset == last_ins->inst_offset) {
1415 ins->opcode = OP_X86_PUSH;
1416 ins->sreg1 = last_ins->sreg1;
1423 bb->last_ins = last_ins;
1427 branch_cc_table [] = {
1428 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1429 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1430 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1433 #define DEBUG(a) if (cfg->verbose_level > 1) a
1437 * returns the offset used by spillvar. It allocates a new
1438 * spill variable if necessary.
1441 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1443 MonoSpillInfo **si, *info;
1446 si = &cfg->spill_info;
1448 while (i <= spillvar) {
1451 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1453 cfg->stack_offset -= sizeof (gpointer);
1454 info->offset = cfg->stack_offset;
1458 return (*si)->offset;
1464 g_assert_not_reached ();
1469 * returns the offset used by spillvar. It allocates a new
1470 * spill float variable if necessary.
1471 * (same as mono_spillvar_offset but for float)
1474 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1476 MonoSpillInfo **si, *info;
1479 si = &cfg->spill_info_float;
1481 while (i <= spillvar) {
1484 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1486 cfg->stack_offset -= sizeof (double);
1487 info->offset = cfg->stack_offset;
1491 return (*si)->offset;
1497 g_assert_not_reached ();
1502 * Creates a store for spilled floating point items
1505 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, MonoInst *ins)
1508 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1510 store->inst_destbasereg = X86_EBP;
1511 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1513 DEBUG (g_print ("SPILLED FLOAT STORE (%d at 0x%08x(%%sp)) (from %d)\n", spill, store->inst_offset, reg));
1518 * Creates a load for spilled floating point items
1521 create_spilled_load_float (MonoCompile *cfg, int spill, int reg, MonoInst *ins)
1524 MONO_INST_NEW (cfg, load, OP_LOADR8_SPILL_MEMBASE);
1526 load->inst_basereg = X86_EBP;
1527 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1529 DEBUG (g_print ("SPILLED FLOAT LOAD (%d at 0x%08x(%%sp)) (from %d)\n", spill, load->inst_offset, reg));
1533 #define is_global_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS && !X86_IS_CALLEE ((r)))
1534 #define reg_is_freeable(r) ((r) >= 0 && (r) < MONO_MAX_IREGS && X86_IS_CALLEE ((r)))
1541 int flags; /* used to track fp spill/load */
1544 static const char*const * ins_spec = pentium_desc;
1547 print_ins (int i, MonoInst *ins)
1549 const char *spec = ins_spec [ins->opcode];
1550 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1551 if (spec [MONO_INST_DEST]) {
1552 if (ins->dreg >= MONO_MAX_IREGS)
1553 g_print (" R%d <-", ins->dreg);
1555 g_print (" %s <-", mono_arch_regname (ins->dreg));
1557 if (spec [MONO_INST_SRC1]) {
1558 if (ins->sreg1 >= MONO_MAX_IREGS)
1559 g_print (" R%d", ins->sreg1);
1561 g_print (" %s", mono_arch_regname (ins->sreg1));
1563 if (spec [MONO_INST_SRC2]) {
1564 if (ins->sreg2 >= MONO_MAX_IREGS)
1565 g_print (" R%d", ins->sreg2);
1567 g_print (" %s", mono_arch_regname (ins->sreg2));
1569 if (spec [MONO_INST_CLOB])
1570 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1575 print_regtrack (RegTrack *t, int num)
1581 for (i = 0; i < num; ++i) {
1584 if (i >= MONO_MAX_IREGS) {
1585 g_snprintf (buf, sizeof(buf), "R%d", i);
1588 r = mono_arch_regname (i);
1589 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1593 typedef struct InstList InstList;
1601 static inline InstList*
1602 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1604 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1614 * Force the spilling of the variable in the symbolic register 'reg'.
1617 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1622 sel = cfg->rs->iassign [reg];
1623 /*i = cfg->rs->isymbolic [sel];
1624 g_assert (i == reg);*/
1626 spill = ++cfg->spill_count;
1627 cfg->rs->iassign [i] = -spill - 1;
1628 mono_regstate_free_int (cfg->rs, sel);
1629 /* we need to create a spill var and insert a load to sel after the current instruction */
1630 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1632 load->inst_basereg = X86_EBP;
1633 load->inst_offset = mono_spillvar_offset (cfg, spill);
1635 while (ins->next != item->prev->data)
1638 load->next = ins->next;
1640 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1641 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1642 g_assert (i == sel);
1648 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1653 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));
1654 /* exclude the registers in the current instruction */
1655 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1656 if (ins->sreg1 >= MONO_MAX_IREGS)
1657 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1659 regmask &= ~ (1 << ins->sreg1);
1660 DEBUG (g_print ("\t\texcluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1662 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1663 if (ins->sreg2 >= MONO_MAX_IREGS)
1664 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1666 regmask &= ~ (1 << ins->sreg2);
1667 DEBUG (g_print ("\t\texcluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1669 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1670 regmask &= ~ (1 << ins->dreg);
1671 DEBUG (g_print ("\t\texcluding dreg %s\n", mono_arch_regname (ins->dreg)));
1674 DEBUG (g_print ("\t\tavailable regmask: 0x%08x\n", regmask));
1675 g_assert (regmask); /* need at least a register we can free */
1677 /* we should track prev_use and spill the register that's farther */
1678 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1679 if (regmask & (1 << i)) {
1681 DEBUG (g_print ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1685 i = cfg->rs->isymbolic [sel];
1686 spill = ++cfg->spill_count;
1687 cfg->rs->iassign [i] = -spill - 1;
1688 mono_regstate_free_int (cfg->rs, sel);
1689 /* we need to create a spill var and insert a load to sel after the current instruction */
1690 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1692 load->inst_basereg = X86_EBP;
1693 load->inst_offset = mono_spillvar_offset (cfg, spill);
1695 while (ins->next != item->prev->data)
1698 load->next = ins->next;
1700 DEBUG (g_print ("\tSPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1701 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1702 g_assert (i == sel);
1708 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1711 MONO_INST_NEW (cfg, copy, OP_MOVE);
1715 copy->next = ins->next;
1718 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1723 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1726 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1728 store->inst_destbasereg = X86_EBP;
1729 store->inst_offset = mono_spillvar_offset (cfg, spill);
1731 store->next = ins->next;
1734 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)));
1739 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1743 prev = item->next->data;
1745 while (prev->next != ins)
1747 to_insert->next = ins;
1748 prev->next = to_insert;
1750 to_insert->next = ins;
1753 * needed otherwise in the next instruction we can add an ins to the
1754 * end and that would get past this instruction.
1756 item->data = to_insert;
1762 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1764 int val = cfg->rs->iassign [sym_reg];
1768 /* the register gets spilled after this inst */
1771 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1773 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1774 cfg->rs->iassign [sym_reg] = val;
1775 /* add option to store before the instruction for src registers */
1777 create_spilled_store (cfg, spill, val, sym_reg, ins);
1779 cfg->rs->isymbolic [val] = sym_reg;
1784 /* flags used in reginfo->flags */
1786 MONO_X86_FP_NEEDS_LOAD_SPILL = 1 << 0,
1787 MONO_X86_FP_NEEDS_SPILL = 1 << 1,
1788 MONO_X86_FP_NEEDS_LOAD = 1 << 2,
1789 MONO_X86_REG_NOT_ECX = 1 << 3,
1790 MONO_X86_REG_EAX = 1 << 4,
1791 MONO_X86_REG_EDX = 1 << 5,
1792 MONO_X86_REG_ECX = 1 << 6
1796 mono_x86_alloc_int_reg (MonoCompile *cfg, InstList *tmp, MonoInst *ins, guint32 dest_mask, int sym_reg, int flags)
1799 int test_mask = dest_mask;
1801 if (flags & MONO_X86_REG_EAX)
1802 test_mask &= (1 << X86_EAX);
1803 else if (flags & MONO_X86_REG_EDX)
1804 test_mask &= (1 << X86_EDX);
1805 else if (flags & MONO_X86_REG_ECX)
1806 test_mask &= (1 << X86_ECX);
1807 else if (flags & MONO_X86_REG_NOT_ECX)
1808 test_mask &= ~ (1 << X86_ECX);
1810 val = mono_regstate_alloc_int (cfg->rs, test_mask);
1811 if (val >= 0 && test_mask != dest_mask)
1812 DEBUG(g_print ("\tUsed flag to allocate reg %s for R%u\n", mono_arch_regname (val), sym_reg));
1814 if (val < 0 && (flags & MONO_X86_REG_NOT_ECX)) {
1815 DEBUG(g_print ("\tFailed to allocate flag suggested mask (%u) but exluding ECX\n", test_mask));
1816 val = mono_regstate_alloc_int (cfg->rs, (dest_mask & (~1 << X86_ECX)));
1820 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
1822 val = get_register_spilling (cfg, tmp, ins, dest_mask, sym_reg);
1829 assign_ireg (MonoRegState *rs, int reg, int hreg)
1831 g_assert (reg >= MONO_MAX_IREGS);
1832 g_assert (hreg < MONO_MAX_IREGS);
1833 g_assert (! is_global_ireg (hreg));
1835 rs->iassign [reg] = hreg;
1836 rs->isymbolic [hreg] = reg;
1837 rs->ifree_mask &= ~ (1 << hreg);
1840 /*#include "cprop.c"*/
1843 * Local register allocation.
1844 * We first scan the list of instructions and we save the liveness info of
1845 * each register (when the register is first used, when it's value is set etc.).
1846 * We also reverse the list of instructions (in the InstList list) because assigning
1847 * registers backwards allows for more tricks to be used.
1850 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1853 MonoRegState *rs = cfg->rs;
1854 int i, val, fpcount;
1855 RegTrack *reginfo, *reginfof;
1856 RegTrack *reginfo1, *reginfo2, *reginfod;
1857 InstList *tmp, *reversed = NULL;
1859 guint32 src1_mask, src2_mask, dest_mask;
1860 GList *fspill_list = NULL;
1865 rs->next_vireg = bb->max_ireg;
1866 rs->next_vfreg = bb->max_freg;
1867 mono_regstate_assign (rs);
1868 reginfo = g_malloc0 (sizeof (RegTrack) * rs->next_vireg);
1869 reginfof = g_malloc0 (sizeof (RegTrack) * rs->next_vfreg);
1870 rs->ifree_mask = X86_CALLEE_REGS;
1874 /*if (cfg->opt & MONO_OPT_COPYPROP)
1875 local_copy_prop (cfg, ins);*/
1879 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1880 /* forward pass on the instructions to collect register liveness info */
1882 spec = ins_spec [ins->opcode];
1884 DEBUG (print_ins (i, ins));
1886 if (spec [MONO_INST_SRC1]) {
1887 if (spec [MONO_INST_SRC1] == 'f') {
1889 reginfo1 = reginfof;
1891 spill = g_list_first (fspill_list);
1892 if (spill && fpcount < MONO_MAX_FREGS) {
1893 reginfo1 [ins->sreg1].flags |= MONO_X86_FP_NEEDS_LOAD;
1894 fspill_list = g_list_remove (fspill_list, spill->data);
1900 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1901 reginfo1 [ins->sreg1].last_use = i;
1902 if (spec [MONO_INST_SRC1] == 'L') {
1903 /* The virtual register is allocated sequentially */
1904 reginfo1 [ins->sreg1 + 1].prev_use = reginfo1 [ins->sreg1 + 1].last_use;
1905 reginfo1 [ins->sreg1 + 1].last_use = i;
1906 if (reginfo1 [ins->sreg1 + 1].born_in == 0 || reginfo1 [ins->sreg1 + 1].born_in > i)
1907 reginfo1 [ins->sreg1 + 1].born_in = i;
1909 reginfo1 [ins->sreg1].flags |= MONO_X86_REG_EAX;
1910 reginfo1 [ins->sreg1 + 1].flags |= MONO_X86_REG_EDX;
1915 if (spec [MONO_INST_SRC2]) {
1916 if (spec [MONO_INST_SRC2] == 'f') {
1918 reginfo2 = reginfof;
1919 spill = g_list_first (fspill_list);
1921 reginfo2 [ins->sreg2].flags |= MONO_X86_FP_NEEDS_LOAD;
1922 fspill_list = g_list_remove (fspill_list, spill->data);
1923 if (fpcount >= MONO_MAX_FREGS) {
1925 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
1926 reginfo2 [ins->sreg2].flags |= MONO_X86_FP_NEEDS_LOAD_SPILL;
1933 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1934 reginfo2 [ins->sreg2].last_use = i;
1935 if (spec [MONO_INST_SRC2] == 'L') {
1936 /* The virtual register is allocated sequentially */
1937 reginfo2 [ins->sreg2 + 1].prev_use = reginfo2 [ins->sreg2 + 1].last_use;
1938 reginfo2 [ins->sreg2 + 1].last_use = i;
1939 if (reginfo2 [ins->sreg2 + 1].born_in == 0 || reginfo2 [ins->sreg2 + 1].born_in > i)
1940 reginfo2 [ins->sreg2 + 1].born_in = i;
1942 if (spec [MONO_INST_CLOB] == 's') {
1943 reginfo2 [ins->sreg1].flags |= MONO_X86_REG_NOT_ECX;
1944 reginfo2 [ins->sreg2].flags |= MONO_X86_REG_ECX;
1949 if (spec [MONO_INST_DEST]) {
1950 if (spec [MONO_INST_DEST] == 'f') {
1951 reginfod = reginfof;
1952 if (fpcount >= MONO_MAX_FREGS) {
1953 reginfod [ins->dreg].flags |= MONO_X86_FP_NEEDS_SPILL;
1955 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
1962 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1963 reginfod [ins->dreg].killed_in = i;
1964 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1965 reginfod [ins->dreg].last_use = i;
1966 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1967 reginfod [ins->dreg].born_in = i;
1968 if (spec [MONO_INST_DEST] == 'l' || spec [MONO_INST_DEST] == 'L') {
1969 /* The virtual register is allocated sequentially */
1970 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1971 reginfod [ins->dreg + 1].last_use = i;
1972 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1973 reginfod [ins->dreg + 1].born_in = i;
1975 reginfod [ins->dreg].flags |= MONO_X86_REG_EAX;
1976 reginfod [ins->dreg + 1].flags |= MONO_X86_REG_EDX;
1982 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1987 // todo: check if we have anything left on fp stack, in verify mode?
1990 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1991 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1994 int prev_dreg, prev_sreg1, prev_sreg2, clob_dreg;
1995 dest_mask = src1_mask = src2_mask = X86_CALLEE_REGS;
1998 spec = ins_spec [ins->opcode];
2001 DEBUG (g_print ("processing:"));
2002 DEBUG (print_ins (i, ins));
2003 if (spec [MONO_INST_CLOB] == 's') {
2005 * Shift opcodes, SREG2 must be RCX
2007 if (rs->ifree_mask & (1 << X86_ECX)) {
2008 if (ins->sreg2 < MONO_MAX_IREGS) {
2009 /* Argument already in hard reg, need to copy */
2010 MonoInst *copy = create_copy_ins (cfg, X86_ECX, ins->sreg2, NULL);
2011 insert_before_ins (ins, tmp, copy);
2014 DEBUG (g_print ("\tshortcut assignment of R%d to ECX\n", ins->sreg2));
2015 assign_ireg (rs, ins->sreg2, X86_ECX);
2018 int need_ecx_spill = TRUE;
2020 * we first check if src1/dreg is already assigned a register
2021 * and then we force a spill of the var assigned to ECX.
2023 /* the destination register can't be ECX */
2024 dest_mask &= ~ (1 << X86_ECX);
2025 src1_mask &= ~ (1 << X86_ECX);
2026 val = rs->iassign [ins->dreg];
2028 * the destination register is already assigned to ECX:
2029 * we need to allocate another register for it and then
2030 * copy from this to ECX.
2032 if (val == X86_ECX && ins->dreg != ins->sreg2) {
2034 new_dest = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
2035 g_assert (new_dest >= 0);
2036 DEBUG (g_print ("\tclob:s changing dreg R%d to %s from ECX\n", ins->dreg, mono_arch_regname (new_dest)));
2038 rs->isymbolic [new_dest] = ins->dreg;
2039 rs->iassign [ins->dreg] = new_dest;
2040 clob_dreg = ins->dreg;
2041 ins->dreg = new_dest;
2042 create_copy_ins (cfg, X86_ECX, new_dest, ins);
2043 need_ecx_spill = FALSE;
2044 /*DEBUG (g_print ("\tforced spill of R%d\n", ins->dreg));
2045 val = get_register_force_spilling (cfg, tmp, ins, ins->dreg);
2046 rs->iassign [ins->dreg] = val;
2047 rs->isymbolic [val] = prev_dreg;
2050 if (is_global_ireg (ins->sreg2)) {
2051 MonoInst *copy = create_copy_ins (cfg, X86_ECX, ins->sreg2, NULL);
2052 insert_before_ins (ins, tmp, copy);
2055 val = rs->iassign [ins->sreg2];
2056 if (val >= 0 && val != X86_ECX) {
2057 MonoInst *move = create_copy_ins (cfg, X86_ECX, val, NULL);
2058 DEBUG (g_print ("\tmoved arg from R%d (%d) to ECX\n", val, ins->sreg2));
2060 g_assert_not_reached ();
2061 /* FIXME: where is move connected to the instruction list? */
2062 //tmp->prev->data->next = move;
2066 need_ecx_spill = FALSE;
2069 if (need_ecx_spill && !(rs->ifree_mask & (1 << X86_ECX))) {
2070 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_ECX]));
2071 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_ECX]);
2072 mono_regstate_free_int (rs, X86_ECX);
2074 if (!is_global_ireg (ins->sreg2))
2075 /* force-set sreg2 */
2076 assign_ireg (rs, ins->sreg2, X86_ECX);
2078 ins->sreg2 = X86_ECX;
2079 } else if (spec [MONO_INST_CLOB] == 'd') {
2083 int dest_reg = X86_EAX;
2084 int clob_reg = X86_EDX;
2085 if (spec [MONO_INST_DEST] == 'd') {
2086 dest_reg = X86_EDX; /* reminder */
2089 if (is_global_ireg (ins->dreg))
2092 val = rs->iassign [ins->dreg];
2093 if (0 && val >= 0 && val != dest_reg && !(rs->ifree_mask & (1 << dest_reg))) {
2094 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
2095 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
2096 mono_regstate_free_int (rs, dest_reg);
2100 /* the register gets spilled after this inst */
2101 int spill = -val -1;
2102 dest_mask = 1 << dest_reg;
2103 prev_dreg = ins->dreg;
2104 val = mono_regstate_alloc_int (rs, dest_mask);
2106 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
2107 rs->iassign [ins->dreg] = val;
2109 create_spilled_store (cfg, spill, val, prev_dreg, ins);
2110 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
2111 rs->isymbolic [val] = prev_dreg;
2114 DEBUG (g_print ("\tshortcut assignment of R%d to %s\n", ins->dreg, mono_arch_regname (dest_reg)));
2115 prev_dreg = ins->dreg;
2116 assign_ireg (rs, ins->dreg, dest_reg);
2117 ins->dreg = dest_reg;
2122 //DEBUG (g_print ("dest reg in div assigned: %s\n", mono_arch_regname (val)));
2123 if (val != dest_reg) { /* force a copy */
2124 create_copy_ins (cfg, val, dest_reg, ins);
2125 if (!(rs->ifree_mask & (1 << dest_reg)) && rs->isymbolic [dest_reg] >= MONO_MAX_IREGS) {
2126 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
2127 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
2128 mono_regstate_free_int (rs, dest_reg);
2131 if (!(rs->ifree_mask & (1 << clob_reg)) && (clob_reg != val) && (rs->isymbolic [clob_reg] >= 8)) {
2132 DEBUG (g_print ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
2133 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [clob_reg]);
2134 mono_regstate_free_int (rs, clob_reg);
2136 src1_mask = 1 << X86_EAX;
2137 src2_mask = 1 << X86_ECX;
2138 } else if (spec [MONO_INST_DEST] == 'l') {
2140 val = rs->iassign [ins->dreg];
2141 /* check special case when dreg have been moved from ecx (clob shift) */
2142 if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
2143 hreg = clob_dreg + 1;
2145 hreg = ins->dreg + 1;
2147 /* base prev_dreg on fixed hreg, handle clob case */
2150 if (val != rs->isymbolic [X86_EAX] && !(rs->ifree_mask & (1 << X86_EAX))) {
2151 DEBUG (g_print ("\t(long-low) forced spill of R%d\n", rs->isymbolic [X86_EAX]));
2152 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
2153 mono_regstate_free_int (rs, X86_EAX);
2155 if (hreg != rs->isymbolic [X86_EDX] && !(rs->ifree_mask & (1 << X86_EDX))) {
2156 DEBUG (g_print ("\t(long-high) forced spill of R%d\n", rs->isymbolic [X86_EDX]));
2157 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EDX]);
2158 mono_regstate_free_int (rs, X86_EDX);
2160 } else if (spec [MONO_INST_CLOB] == 'b') {
2162 * x86_set_reg instructions, dreg needs to be EAX..EDX
2164 dest_mask = (1 << X86_EAX) | (1 << X86_EBX) | (1 << X86_ECX) | (1 << X86_EDX);
2165 if ((ins->dreg < MONO_MAX_IREGS) && (! (dest_mask & (1 << ins->dreg)))) {
2167 * ins->dreg is already a hard reg, need to allocate another
2168 * suitable hard reg and make a copy.
2170 int new_dest = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
2171 g_assert (new_dest >= 0);
2173 create_copy_ins (cfg, ins->dreg, new_dest, ins);
2174 DEBUG (g_print ("\tclob:b changing dreg R%d to %s\n", ins->dreg, mono_arch_regname (new_dest)));
2175 ins->dreg = new_dest;
2177 /* The hard reg is no longer needed */
2178 mono_regstate_free_int (rs, new_dest);
2185 if (spec [MONO_INST_DEST] == 'f') {
2186 if (reginfof [ins->dreg].flags & MONO_X86_FP_NEEDS_SPILL) {
2189 spill_node = g_list_first (fspill_list);
2190 g_assert (spill_node);
2192 store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->dreg, ins);
2193 insert_before_ins (ins, tmp, store);
2194 fspill_list = g_list_remove (fspill_list, spill_node->data);
2197 } else if (spec [MONO_INST_DEST] == 'L') {
2199 val = rs->iassign [ins->dreg];
2200 /* check special case when dreg have been moved from ecx (clob shift) */
2201 if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
2202 hreg = clob_dreg + 1;
2204 hreg = ins->dreg + 1;
2206 /* base prev_dreg on fixed hreg, handle clob case */
2207 prev_dreg = hreg - 1;
2212 /* the register gets spilled after this inst */
2215 val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
2216 rs->iassign [ins->dreg] = val;
2218 create_spilled_store (cfg, spill, val, prev_dreg, ins);
2221 DEBUG (g_print ("\tassigned dreg (long) %s to dest R%d\n", mono_arch_regname (val), hreg - 1));
2223 rs->isymbolic [val] = hreg - 1;
2226 val = rs->iassign [hreg];
2230 /* the register gets spilled after this inst */
2233 val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, hreg, reginfo [hreg].flags);
2234 rs->iassign [hreg] = val;
2236 create_spilled_store (cfg, spill, val, hreg, ins);
2239 DEBUG (g_print ("\tassigned hreg (long-high) %s to dest R%d\n", mono_arch_regname (val), hreg));
2240 rs->isymbolic [val] = hreg;
2241 /* save reg allocating into unused */
2244 /* check if we can free our long reg */
2245 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
2246 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (val), hreg, reginfo [hreg].born_in));
2247 mono_regstate_free_int (rs, val);
2250 else if (ins->dreg >= MONO_MAX_IREGS) {
2252 val = rs->iassign [ins->dreg];
2253 if (spec [MONO_INST_DEST] == 'l') {
2254 /* check special case when dreg have been moved from ecx (clob shift) */
2255 if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
2256 hreg = clob_dreg + 1;
2258 hreg = ins->dreg + 1;
2260 /* base prev_dreg on fixed hreg, handle clob case */
2261 prev_dreg = hreg - 1;
2263 prev_dreg = ins->dreg;
2268 /* the register gets spilled after this inst */
2271 val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->dreg, reginfo [ins->dreg].flags);
2272 rs->iassign [ins->dreg] = val;
2274 create_spilled_store (cfg, spill, val, prev_dreg, ins);
2276 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
2277 rs->isymbolic [val] = prev_dreg;
2279 /* handle cases where lreg needs to be eax:edx */
2280 if (spec [MONO_INST_DEST] == 'l') {
2281 /* check special case when dreg have been moved from ecx (clob shift) */
2282 int hreg = prev_dreg + 1;
2283 val = rs->iassign [hreg];
2287 /* the register gets spilled after this inst */
2290 val = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, hreg, reginfo [hreg].flags);
2291 rs->iassign [hreg] = val;
2293 create_spilled_store (cfg, spill, val, hreg, ins);
2295 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
2296 rs->isymbolic [val] = hreg;
2297 if (ins->dreg == X86_EAX) {
2299 create_copy_ins (cfg, val, X86_EDX, ins);
2300 } else if (ins->dreg == X86_EDX) {
2301 if (val == X86_EAX) {
2303 g_assert_not_reached ();
2305 /* two forced copies */
2306 create_copy_ins (cfg, val, X86_EDX, ins);
2307 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
2310 if (val == X86_EDX) {
2311 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
2313 /* two forced copies */
2314 create_copy_ins (cfg, val, X86_EDX, ins);
2315 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
2318 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
2319 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
2320 mono_regstate_free_int (rs, val);
2322 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != X86_EAX && spec [MONO_INST_CLOB] != 'd') {
2323 /* this instruction only outputs to EAX, need to copy */
2324 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
2325 } else if (spec [MONO_INST_DEST] == 'd' && ins->dreg != X86_EDX && spec [MONO_INST_CLOB] != 'd') {
2326 create_copy_ins (cfg, ins->dreg, X86_EDX, ins);
2329 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
2330 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
2331 mono_regstate_free_int (rs, ins->dreg);
2333 /* put src1 in EAX if it needs to be */
2334 if (spec [MONO_INST_SRC1] == 'a') {
2335 if (!(rs->ifree_mask & (1 << X86_EAX))) {
2336 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
2337 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
2338 mono_regstate_free_int (rs, X86_EAX);
2340 if (ins->sreg1 < MONO_MAX_IREGS) {
2341 /* The argument is already in a hard reg, need to copy */
2342 MonoInst *copy = create_copy_ins (cfg, X86_EAX, ins->sreg1, NULL);
2343 insert_before_ins (ins, tmp, copy);
2346 /* force-set sreg1 */
2347 assign_ireg (rs, ins->sreg1, X86_EAX);
2348 ins->sreg1 = X86_EAX;
2354 if (spec [MONO_INST_SRC1] == 'f') {
2355 if (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD) {
2357 MonoInst *store = NULL;
2359 if (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD_SPILL) {
2361 spill_node = g_list_first (fspill_list);
2362 g_assert (spill_node);
2364 store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->sreg1, ins);
2365 fspill_list = g_list_remove (fspill_list, spill_node->data);
2369 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
2370 load = create_spilled_load_float (cfg, fspill, ins->sreg1, ins);
2371 insert_before_ins (ins, tmp, load);
2373 insert_before_ins (load, tmp, store);
2375 } else if ((spec [MONO_INST_DEST] == 'L') && (spec [MONO_INST_SRC1] == 'L')) {
2376 /* force source to be same as dest */
2377 assign_ireg (rs, ins->sreg1, ins->dreg);
2378 assign_ireg (rs, ins->sreg1 + 1, ins->unused);
2380 DEBUG (g_print ("\tassigned sreg1 (long) %s to sreg1 R%d\n", mono_arch_regname (ins->dreg), ins->sreg1));
2381 DEBUG (g_print ("\tassigned sreg1 (long-high) %s to sreg1 R%d\n", mono_arch_regname (ins->unused), ins->sreg1 + 1));
2383 ins->sreg1 = ins->dreg;
2385 * No need for saving the reg, we know that src1=dest in this cases
2386 * ins->inst_c0 = ins->unused;
2389 else if (ins->sreg1 >= MONO_MAX_IREGS) {
2390 val = rs->iassign [ins->sreg1];
2391 prev_sreg1 = ins->sreg1;
2395 /* the register gets spilled after this inst */
2398 if (0 && ins->opcode == OP_MOVE) {
2400 * small optimization: the dest register is already allocated
2401 * but the src one is not: we can simply assign the same register
2402 * here and peephole will get rid of the instruction later.
2403 * This optimization may interfere with the clobbering handling:
2404 * it removes a mov operation that will be added again to handle clobbering.
2405 * There are also some other issues that should with make testjit.
2407 mono_regstate_alloc_int (rs, 1 << ins->dreg);
2408 val = rs->iassign [ins->sreg1] = ins->dreg;
2409 //g_assert (val >= 0);
2410 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2412 //g_assert (val == -1); /* source cannot be spilled */
2413 val = mono_x86_alloc_int_reg (cfg, tmp, ins, src1_mask, ins->sreg1, reginfo [ins->sreg1].flags);
2414 rs->iassign [ins->sreg1] = val;
2415 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2418 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
2419 insert_before_ins (ins, tmp, store);
2422 rs->isymbolic [val] = prev_sreg1;
2427 /* handle clobbering of sreg1 */
2428 if ((spec [MONO_INST_CLOB] == '1' || spec [MONO_INST_CLOB] == 's') && ins->dreg != ins->sreg1) {
2429 MonoInst *sreg2_copy = NULL;
2430 MonoInst *copy = NULL;
2432 if (ins->dreg == ins->sreg2) {
2434 * copying sreg1 to dreg could clobber sreg2, so allocate a new
2439 reg2 = mono_x86_alloc_int_reg (cfg, tmp, ins, dest_mask, ins->sreg2, 0);
2441 DEBUG (g_print ("\tneed to copy sreg2 %s to reg %s\n", mono_arch_regname (ins->sreg2), mono_arch_regname (reg2)));
2442 sreg2_copy = create_copy_ins (cfg, reg2, ins->sreg2, NULL);
2443 prev_sreg2 = ins->sreg2 = reg2;
2445 mono_regstate_free_int (rs, reg2);
2448 copy = create_copy_ins (cfg, ins->dreg, ins->sreg1, NULL);
2449 DEBUG (g_print ("\tneed to copy sreg1 %s to dreg %s\n", mono_arch_regname (ins->sreg1), mono_arch_regname (ins->dreg)));
2450 insert_before_ins (ins, tmp, copy);
2453 insert_before_ins (copy, tmp, sreg2_copy);
2456 * Need to prevent sreg2 to be allocated to sreg1, since that
2457 * would screw up the previous copy.
2459 src2_mask &= ~ (1 << ins->sreg1);
2460 /* we set sreg1 to dest as well */
2461 prev_sreg1 = ins->sreg1 = ins->dreg;
2462 src2_mask &= ~ (1 << ins->dreg);
2468 if (spec [MONO_INST_SRC2] == 'f') {
2469 if (reginfof [ins->sreg2].flags & MONO_X86_FP_NEEDS_LOAD) {
2471 MonoInst *store = NULL;
2473 if (reginfof [ins->sreg2].flags & MONO_X86_FP_NEEDS_LOAD_SPILL) {
2476 spill_node = g_list_first (fspill_list);
2477 g_assert (spill_node);
2478 if (spec [MONO_INST_SRC1] == 'f' && (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD_SPILL))
2479 spill_node = g_list_next (spill_node);
2481 store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->sreg2, ins);
2482 fspill_list = g_list_remove (fspill_list, spill_node->data);
2486 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
2487 load = create_spilled_load_float (cfg, fspill, ins->sreg2, ins);
2488 insert_before_ins (ins, tmp, load);
2490 insert_before_ins (load, tmp, store);
2493 else if (ins->sreg2 >= MONO_MAX_IREGS) {
2494 val = rs->iassign [ins->sreg2];
2495 prev_sreg2 = ins->sreg2;
2499 /* the register gets spilled after this inst */
2502 val = mono_x86_alloc_int_reg (cfg, tmp, ins, src2_mask, ins->sreg2, reginfo [ins->sreg2].flags);
2503 rs->iassign [ins->sreg2] = val;
2504 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
2506 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
2508 rs->isymbolic [val] = prev_sreg2;
2510 if (spec [MONO_INST_CLOB] == 's' && ins->sreg2 != X86_ECX) {
2511 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]));
2517 if (spec [MONO_INST_CLOB] == 'c') {
2519 guint32 clob_mask = X86_CALLEE_REGS;
2520 for (j = 0; j < MONO_MAX_IREGS; ++j) {
2522 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
2523 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
2527 if (spec [MONO_INST_CLOB] == 'a') {
2528 guint32 clob_reg = X86_EAX;
2529 if (!(rs->ifree_mask & (1 << clob_reg)) && (rs->isymbolic [clob_reg] >= 8)) {
2530 DEBUG (g_print ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
2531 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [clob_reg]);
2532 mono_regstate_free_int (rs, clob_reg);
2535 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2536 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2537 mono_regstate_free_int (rs, ins->sreg1);
2539 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2540 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2541 mono_regstate_free_int (rs, ins->sreg2);
2544 //DEBUG (print_ins (i, ins));
2545 /* this may result from a insert_before call */
2547 bb->code = tmp->data;
2553 g_list_free (fspill_list);
2556 static unsigned char*
2557 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
2559 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2560 x86_fnstcw_membase(code, X86_ESP, 0);
2561 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
2562 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
2563 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
2564 x86_fldcw_membase (code, X86_ESP, 2);
2566 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2567 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2568 x86_pop_reg (code, dreg);
2569 /* FIXME: need the high register
2570 * x86_pop_reg (code, dreg_high);
2573 x86_push_reg (code, X86_EAX); // SP = SP - 4
2574 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
2575 x86_pop_reg (code, dreg);
2577 x86_fldcw_membase (code, X86_ESP, 0);
2578 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2581 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
2583 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
2587 static unsigned char*
2588 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2590 int sreg = tree->sreg1;
2591 #ifdef PLATFORM_WIN32
2596 * If requested stack size is larger than one page,
2597 * perform stack-touch operation
2600 * Generate stack probe code.
2601 * Under Windows, it is necessary to allocate one page at a time,
2602 * "touching" stack after each successful sub-allocation. This is
2603 * because of the way stack growth is implemented - there is a
2604 * guard page before the lowest stack page that is currently commited.
2605 * Stack normally grows sequentially so OS traps access to the
2606 * guard page and commits more pages when needed.
2608 x86_test_reg_imm (code, sreg, ~0xFFF);
2609 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2611 br[2] = code; /* loop */
2612 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
2613 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
2614 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2615 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2616 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2617 x86_patch (br[3], br[2]);
2618 x86_test_reg_reg (code, sreg, sreg);
2619 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2620 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2622 br[1] = code; x86_jump8 (code, 0);
2624 x86_patch (br[0], code);
2625 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2626 x86_patch (br[1], code);
2627 x86_patch (br[4], code);
2628 #else /* PLATFORM_WIN32 */
2629 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2631 if (tree->flags & MONO_INST_INIT) {
2633 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2634 x86_push_reg (code, X86_EAX);
2637 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2638 x86_push_reg (code, X86_ECX);
2641 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2642 x86_push_reg (code, X86_EDI);
2646 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2647 if (sreg != X86_ECX)
2648 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2649 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2651 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2653 x86_prefix (code, X86_REP_PREFIX);
2656 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2657 x86_pop_reg (code, X86_EDI);
2658 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2659 x86_pop_reg (code, X86_ECX);
2660 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2661 x86_pop_reg (code, X86_EAX);
2668 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
2673 /* Move return value to the target register */
2674 switch (ins->opcode) {
2677 case OP_CALL_MEMBASE:
2678 if (ins->dreg != X86_EAX)
2679 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2683 case OP_VCALL_MEMBASE:
2684 cinfo = get_call_info (((MonoCallInst*)ins)->signature, FALSE);
2685 if (cinfo->ret.storage == ArgValuetypeInReg) {
2686 /* Pop the destination address from the stack */
2687 x86_pop_reg (code, X86_ECX);
2689 for (quad = 0; quad < 2; quad ++) {
2690 switch (cinfo->ret.pair_storage [quad]) {
2692 g_assert (cinfo->ret.pair_regs [quad] != X86_ECX);
2693 x86_mov_membase_reg (code, X86_ECX, (quad * sizeof (gpointer)), cinfo->ret.pair_regs [quad], sizeof (gpointer));
2698 g_assert_not_reached ();
2711 emit_tls_get (guint8* code, int dreg, int tls_offset)
2713 #ifdef PLATFORM_WIN32
2715 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
2716 * Journal and/or a disassembly of the TlsGet () function.
2718 g_assert (tls_offset < 64);
2719 x86_prefix (code, X86_FS_PREFIX);
2720 x86_mov_reg_mem (code, dreg, 0x18, 4);
2721 /* Dunno what this does but TlsGetValue () contains it */
2722 x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
2723 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
2725 x86_prefix (code, X86_GS_PREFIX);
2726 x86_mov_reg_mem (code, dreg, tls_offset, 4);
2731 #define REAL_PRINT_REG(text,reg) \
2732 mono_assert (reg >= 0); \
2733 x86_push_reg (code, X86_EAX); \
2734 x86_push_reg (code, X86_EDX); \
2735 x86_push_reg (code, X86_ECX); \
2736 x86_push_reg (code, reg); \
2737 x86_push_imm (code, reg); \
2738 x86_push_imm (code, text " %d %p\n"); \
2739 x86_mov_reg_imm (code, X86_EAX, printf); \
2740 x86_call_reg (code, X86_EAX); \
2741 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
2742 x86_pop_reg (code, X86_ECX); \
2743 x86_pop_reg (code, X86_EDX); \
2744 x86_pop_reg (code, X86_EAX);
2746 /* benchmark and set based on cpu */
2747 #define LOOP_ALIGNMENT 8
2748 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2751 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2756 guint8 *code = cfg->native_code + cfg->code_len;
2757 MonoInst *last_ins = NULL;
2758 guint last_offset = 0;
2761 if (cfg->opt & MONO_OPT_PEEPHOLE)
2762 peephole_pass (cfg, bb);
2764 if (cfg->opt & MONO_OPT_LOOP) {
2765 int pad, align = LOOP_ALIGNMENT;
2766 /* set alignment depending on cpu */
2767 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2769 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2770 x86_padding (code, pad);
2771 cfg->code_len += pad;
2772 bb->native_offset = cfg->code_len;
2776 if (cfg->verbose_level > 2)
2777 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2779 cpos = bb->max_offset;
2781 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2782 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2783 g_assert (!cfg->compile_aot);
2786 cov->data [bb->dfn].cil_code = bb->cil_code;
2787 /* this is not thread save, but good enough */
2788 x86_inc_mem (code, &cov->data [bb->dfn].count);
2791 offset = code - cfg->native_code;
2795 offset = code - cfg->native_code;
2797 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2799 if (offset > (cfg->code_size - max_len - 16)) {
2800 cfg->code_size *= 2;
2801 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2802 code = cfg->native_code + offset;
2803 mono_jit_stats.code_reallocs++;
2806 mono_debug_record_line_number (cfg, ins, offset);
2808 switch (ins->opcode) {
2810 x86_mul_reg (code, ins->sreg2, TRUE);
2813 x86_mul_reg (code, ins->sreg2, FALSE);
2815 case OP_X86_SETEQ_MEMBASE:
2816 case OP_X86_SETNE_MEMBASE:
2817 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2818 ins->inst_basereg, ins->inst_offset, TRUE);
2820 case OP_STOREI1_MEMBASE_IMM:
2821 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2823 case OP_STOREI2_MEMBASE_IMM:
2824 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2826 case OP_STORE_MEMBASE_IMM:
2827 case OP_STOREI4_MEMBASE_IMM:
2828 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2830 case OP_STOREI1_MEMBASE_REG:
2831 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2833 case OP_STOREI2_MEMBASE_REG:
2834 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2836 case OP_STORE_MEMBASE_REG:
2837 case OP_STOREI4_MEMBASE_REG:
2838 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2843 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2846 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2847 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2849 case OP_LOAD_MEMBASE:
2850 case OP_LOADI4_MEMBASE:
2851 case OP_LOADU4_MEMBASE:
2852 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2854 case OP_LOADU1_MEMBASE:
2855 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2857 case OP_LOADI1_MEMBASE:
2858 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2860 case OP_LOADU2_MEMBASE:
2861 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2863 case OP_LOADI2_MEMBASE:
2864 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2867 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2870 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2873 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2876 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2879 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2881 case OP_COMPARE_IMM:
2882 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2884 case OP_X86_COMPARE_MEMBASE_REG:
2885 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2887 case OP_X86_COMPARE_MEMBASE_IMM:
2888 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2890 case OP_X86_COMPARE_MEMBASE8_IMM:
2891 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2893 case OP_X86_COMPARE_REG_MEMBASE:
2894 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2896 case OP_X86_COMPARE_MEM_IMM:
2897 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2899 case OP_X86_TEST_NULL:
2900 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2902 case OP_X86_ADD_MEMBASE_IMM:
2903 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2905 case OP_X86_ADD_MEMBASE:
2906 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2908 case OP_X86_SUB_MEMBASE_IMM:
2909 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2911 case OP_X86_SUB_MEMBASE:
2912 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2914 case OP_X86_INC_MEMBASE:
2915 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2917 case OP_X86_INC_REG:
2918 x86_inc_reg (code, ins->dreg);
2920 case OP_X86_DEC_MEMBASE:
2921 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2923 case OP_X86_DEC_REG:
2924 x86_dec_reg (code, ins->dreg);
2926 case OP_X86_MUL_MEMBASE:
2927 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2930 x86_breakpoint (code);
2934 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2937 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2941 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2944 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2948 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2951 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2955 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2958 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2961 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2964 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2968 x86_div_reg (code, ins->sreg2, TRUE);
2971 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2972 x86_div_reg (code, ins->sreg2, FALSE);
2975 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2977 x86_div_reg (code, ins->sreg2, TRUE);
2981 x86_div_reg (code, ins->sreg2, TRUE);
2984 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2985 x86_div_reg (code, ins->sreg2, FALSE);
2988 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2990 x86_div_reg (code, ins->sreg2, TRUE);
2993 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2996 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2999 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
3002 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
3005 g_assert (ins->sreg2 == X86_ECX);
3006 x86_shift_reg (code, X86_SHL, ins->dreg);
3009 g_assert (ins->sreg2 == X86_ECX);
3010 x86_shift_reg (code, X86_SAR, ins->dreg);
3013 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
3016 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
3019 g_assert (ins->sreg2 == X86_ECX);
3020 x86_shift_reg (code, X86_SHR, ins->dreg);
3023 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
3026 guint8 *jump_to_end;
3028 /* handle shifts below 32 bits */
3029 x86_shld_reg (code, ins->unused, ins->sreg1);
3030 x86_shift_reg (code, X86_SHL, ins->sreg1);
3032 x86_test_reg_imm (code, X86_ECX, 32);
3033 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3035 /* handle shift over 32 bit */
3036 x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
3037 x86_clear_reg (code, ins->sreg1);
3039 x86_patch (jump_to_end, code);
3043 guint8 *jump_to_end;
3045 /* handle shifts below 32 bits */
3046 x86_shrd_reg (code, ins->sreg1, ins->unused);
3047 x86_shift_reg (code, X86_SAR, ins->unused);
3049 x86_test_reg_imm (code, X86_ECX, 32);
3050 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3052 /* handle shifts over 31 bits */
3053 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
3054 x86_shift_reg_imm (code, X86_SAR, ins->unused, 31);
3056 x86_patch (jump_to_end, code);
3060 guint8 *jump_to_end;
3062 /* handle shifts below 32 bits */
3063 x86_shrd_reg (code, ins->sreg1, ins->unused);
3064 x86_shift_reg (code, X86_SHR, ins->unused);
3066 x86_test_reg_imm (code, X86_ECX, 32);
3067 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
3069 /* handle shifts over 31 bits */
3070 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
3071 x86_shift_reg_imm (code, X86_SHR, ins->unused, 31);
3073 x86_patch (jump_to_end, code);
3077 if (ins->inst_imm >= 32) {
3078 x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
3079 x86_clear_reg (code, ins->sreg1);
3080 x86_shift_reg_imm (code, X86_SHL, ins->unused, ins->inst_imm - 32);
3082 x86_shld_reg_imm (code, ins->unused, ins->sreg1, ins->inst_imm);
3083 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
3087 if (ins->inst_imm >= 32) {
3088 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
3089 x86_shift_reg_imm (code, X86_SAR, ins->unused, 0x1f);
3090 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
3092 x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
3093 x86_shift_reg_imm (code, X86_SAR, ins->unused, ins->inst_imm);
3096 case OP_LSHR_UN_IMM:
3097 if (ins->inst_imm >= 32) {
3098 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
3099 x86_clear_reg (code, ins->unused);
3100 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
3102 x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
3103 x86_shift_reg_imm (code, X86_SHR, ins->unused, ins->inst_imm);
3107 x86_not_reg (code, ins->sreg1);
3110 x86_neg_reg (code, ins->sreg1);
3113 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
3116 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
3119 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3122 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
3125 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
3126 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3128 case CEE_MUL_OVF_UN: {
3129 /* the mul operation and the exception check should most likely be split */
3130 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
3131 /*g_assert (ins->sreg2 == X86_EAX);
3132 g_assert (ins->dreg == X86_EAX);*/
3133 if (ins->sreg2 == X86_EAX) {
3134 non_eax_reg = ins->sreg1;
3135 } else if (ins->sreg1 == X86_EAX) {
3136 non_eax_reg = ins->sreg2;
3138 /* no need to save since we're going to store to it anyway */
3139 if (ins->dreg != X86_EAX) {
3141 x86_push_reg (code, X86_EAX);
3143 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
3144 non_eax_reg = ins->sreg2;
3146 if (ins->dreg == X86_EDX) {
3149 x86_push_reg (code, X86_EAX);
3151 } else if (ins->dreg != X86_EAX) {
3153 x86_push_reg (code, X86_EDX);
3155 x86_mul_reg (code, non_eax_reg, FALSE);
3156 /* save before the check since pop and mov don't change the flags */
3157 if (ins->dreg != X86_EAX)
3158 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3160 x86_pop_reg (code, X86_EDX);
3162 x86_pop_reg (code, X86_EAX);
3163 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
3167 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
3170 g_assert_not_reached ();
3171 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3172 x86_mov_reg_imm (code, ins->dreg, 0);
3174 case OP_LOAD_GOTADDR:
3175 x86_call_imm (code, 0);
3177 * The patch needs to point to the pop, since the GOT offset needs
3178 * to be added to that address.
3180 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
3181 x86_pop_reg (code, ins->dreg);
3182 x86_alu_reg_imm (code, X86_ADD, ins->dreg, 0xf0f0f0f0);
3185 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3186 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
3188 case OP_X86_PUSH_GOT_ENTRY:
3189 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
3190 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
3194 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3197 g_assert_not_reached ();
3200 * Note: this 'frame destruction' logic is useful for tail calls, too.
3201 * Keep in sync with the code in emit_epilog.
3205 /* FIXME: no tracing support... */
3206 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3207 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
3208 /* reset offset to make max_len work */
3209 offset = code - cfg->native_code;
3211 g_assert (!cfg->method->save_lmf);
3213 if (cfg->used_int_regs & (1 << X86_EBX))
3215 if (cfg->used_int_regs & (1 << X86_EDI))
3217 if (cfg->used_int_regs & (1 << X86_ESI))
3220 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3222 if (cfg->used_int_regs & (1 << X86_ESI))
3223 x86_pop_reg (code, X86_ESI);
3224 if (cfg->used_int_regs & (1 << X86_EDI))
3225 x86_pop_reg (code, X86_EDI);
3226 if (cfg->used_int_regs & (1 << X86_EBX))
3227 x86_pop_reg (code, X86_EBX);
3229 /* restore ESP/EBP */
3231 offset = code - cfg->native_code;
3232 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
3233 x86_jump32 (code, 0);
3237 /* ensure ins->sreg1 is not NULL
3238 * note that cmp DWORD PTR [eax], eax is one byte shorter than
3239 * cmp DWORD PTR [eax], 0
3241 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
3244 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
3245 x86_push_reg (code, hreg);
3246 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
3247 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
3248 x86_pop_reg (code, hreg);
3256 call = (MonoCallInst*)ins;
3257 if (ins->flags & MONO_INST_HAS_METHOD)
3258 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
3260 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
3261 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3262 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
3263 * bytes to pop, we want to use pops. GCC does this (note it won't happen
3264 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
3265 * smart enough to do that optimization yet
3267 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
3268 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
3269 * (most likely from locality benefits). People with other processors should
3270 * check on theirs to see what happens.
3272 if (call->stack_usage == 4) {
3273 /* we want to use registers that won't get used soon, so use
3274 * ecx, as eax will get allocated first. edx is used by long calls,
3275 * so we can't use that.
3278 x86_pop_reg (code, X86_ECX);
3280 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3283 code = emit_move_return_value (cfg, ins, code);
3288 case OP_VOIDCALL_REG:
3290 call = (MonoCallInst*)ins;
3291 x86_call_reg (code, ins->sreg1);
3292 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3293 if (call->stack_usage == 4)
3294 x86_pop_reg (code, X86_ECX);
3296 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3298 code = emit_move_return_value (cfg, ins, code);
3300 case OP_FCALL_MEMBASE:
3301 case OP_LCALL_MEMBASE:
3302 case OP_VCALL_MEMBASE:
3303 case OP_VOIDCALL_MEMBASE:
3304 case OP_CALL_MEMBASE:
3305 call = (MonoCallInst*)ins;
3306 x86_call_membase (code, ins->sreg1, ins->inst_offset);
3307 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
3308 if (call->stack_usage == 4)
3309 x86_pop_reg (code, X86_ECX);
3311 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
3313 code = emit_move_return_value (cfg, ins, code);
3317 x86_push_reg (code, ins->sreg1);
3319 case OP_X86_PUSH_IMM:
3320 x86_push_imm (code, ins->inst_imm);
3322 case OP_X86_PUSH_MEMBASE:
3323 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
3325 case OP_X86_PUSH_OBJ:
3326 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
3327 x86_push_reg (code, X86_EDI);
3328 x86_push_reg (code, X86_ESI);
3329 x86_push_reg (code, X86_ECX);
3330 if (ins->inst_offset)
3331 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
3333 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
3334 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
3335 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
3337 x86_prefix (code, X86_REP_PREFIX);
3339 x86_pop_reg (code, X86_ECX);
3340 x86_pop_reg (code, X86_ESI);
3341 x86_pop_reg (code, X86_EDI);
3344 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
3346 case OP_X86_LEA_MEMBASE:
3347 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
3350 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
3353 /* keep alignment */
3354 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
3355 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
3356 code = mono_emit_stack_alloc (code, ins);
3357 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
3363 x86_push_reg (code, ins->sreg1);
3364 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3365 (gpointer)"mono_arch_throw_exception");
3369 x86_push_reg (code, ins->sreg1);
3370 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
3371 (gpointer)"mono_arch_rethrow_exception");
3374 case OP_CALL_HANDLER:
3375 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3376 x86_call_imm (code, 0);
3379 ins->inst_c0 = code - cfg->native_code;
3382 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
3383 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
3385 if (ins->flags & MONO_INST_BRLABEL) {
3386 if (ins->inst_i0->inst_c0) {
3387 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
3389 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
3390 if ((cfg->opt & MONO_OPT_BRANCH) &&
3391 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
3392 x86_jump8 (code, 0);
3394 x86_jump32 (code, 0);
3397 if (ins->inst_target_bb->native_offset) {
3398 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3400 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3401 if ((cfg->opt & MONO_OPT_BRANCH) &&
3402 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
3403 x86_jump8 (code, 0);
3405 x86_jump32 (code, 0);
3410 x86_jump_reg (code, ins->sreg1);
3413 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3414 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3417 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
3418 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3421 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3422 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3425 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
3426 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3429 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3430 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3433 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
3434 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3436 case OP_COND_EXC_EQ:
3437 case OP_COND_EXC_NE_UN:
3438 case OP_COND_EXC_LT:
3439 case OP_COND_EXC_LT_UN:
3440 case OP_COND_EXC_GT:
3441 case OP_COND_EXC_GT_UN:
3442 case OP_COND_EXC_GE:
3443 case OP_COND_EXC_GE_UN:
3444 case OP_COND_EXC_LE:
3445 case OP_COND_EXC_LE_UN:
3446 case OP_COND_EXC_OV:
3447 case OP_COND_EXC_NO:
3449 case OP_COND_EXC_NC:
3450 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
3451 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
3463 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
3466 /* floating point opcodes */
3468 double d = *(double *)ins->inst_p0;
3470 if ((d == 0.0) && (mono_signbit (d) == 0)) {
3472 } else if (d == 1.0) {
3475 if (cfg->compile_aot) {
3476 guint32 *val = (guint32*)&d;
3477 x86_push_imm (code, val [1]);
3478 x86_push_imm (code, val [0]);
3479 x86_fld_membase (code, X86_ESP, 0, TRUE);
3480 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
3483 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
3484 x86_fld (code, NULL, TRUE);
3490 float f = *(float *)ins->inst_p0;
3492 if ((f == 0.0) && (mono_signbit (f) == 0)) {
3494 } else if (f == 1.0) {
3497 if (cfg->compile_aot) {
3498 guint32 val = *(guint32*)&f;
3499 x86_push_imm (code, val);
3500 x86_fld_membase (code, X86_ESP, 0, FALSE);
3501 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3504 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
3505 x86_fld (code, NULL, FALSE);
3510 case OP_STORER8_MEMBASE_REG:
3511 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
3513 case OP_LOADR8_SPILL_MEMBASE:
3514 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3517 case OP_LOADR8_MEMBASE:
3518 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3520 case OP_STORER4_MEMBASE_REG:
3521 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
3523 case OP_LOADR4_MEMBASE:
3524 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3526 case CEE_CONV_R4: /* FIXME: change precision */
3528 x86_push_reg (code, ins->sreg1);
3529 x86_fild_membase (code, X86_ESP, 0, FALSE);
3530 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3532 case OP_X86_FP_LOAD_I8:
3533 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
3535 case OP_X86_FP_LOAD_I4:
3536 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
3538 case OP_FCONV_TO_I1:
3539 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
3541 case OP_FCONV_TO_U1:
3542 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
3544 case OP_FCONV_TO_I2:
3545 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
3547 case OP_FCONV_TO_U2:
3548 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
3550 case OP_FCONV_TO_I4:
3552 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
3554 case OP_FCONV_TO_I8:
3555 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3556 x86_fnstcw_membase(code, X86_ESP, 0);
3557 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
3558 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
3559 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
3560 x86_fldcw_membase (code, X86_ESP, 2);
3561 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3562 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
3563 x86_pop_reg (code, ins->dreg);
3564 x86_pop_reg (code, ins->unused);
3565 x86_fldcw_membase (code, X86_ESP, 0);
3566 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3568 case OP_LCONV_TO_R_UN: {
3569 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
3572 /* load 64bit integer to FP stack */
3573 x86_push_imm (code, 0);
3574 x86_push_reg (code, ins->sreg2);
3575 x86_push_reg (code, ins->sreg1);
3576 x86_fild_membase (code, X86_ESP, 0, TRUE);
3577 /* store as 80bit FP value */
3578 x86_fst80_membase (code, X86_ESP, 0);
3580 /* test if lreg is negative */
3581 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3582 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
3584 /* add correction constant mn */
3585 x86_fld80_mem (code, mn);
3586 x86_fld80_membase (code, X86_ESP, 0);
3587 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3588 x86_fst80_membase (code, X86_ESP, 0);
3590 x86_patch (br, code);
3592 x86_fld80_membase (code, X86_ESP, 0);
3593 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
3597 case OP_LCONV_TO_OVF_I: {
3598 guint8 *br [3], *label [1];
3601 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3603 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
3605 /* If the low word top bit is set, see if we are negative */
3606 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
3607 /* We are not negative (no top bit set, check for our top word to be zero */
3608 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
3609 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
3612 /* throw exception */
3613 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
3614 x86_jump32 (code, 0);
3616 x86_patch (br [0], code);
3617 /* our top bit is set, check that top word is 0xfffffff */
3618 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
3620 x86_patch (br [1], code);
3621 /* nope, emit exception */
3622 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
3623 x86_patch (br [2], label [0]);
3625 if (ins->dreg != ins->sreg1)
3626 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
3630 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3633 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
3636 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
3639 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
3647 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3652 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3659 * it really doesn't make sense to inline all this code,
3660 * it's here just to show that things may not be as simple
3663 guchar *check_pos, *end_tan, *pop_jump;
3664 x86_push_reg (code, X86_EAX);
3667 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3669 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3670 x86_fstp (code, 0); /* pop the 1.0 */
3672 x86_jump8 (code, 0);
3674 x86_fp_op (code, X86_FADD, 0);
3678 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3680 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3683 x86_patch (pop_jump, code);
3684 x86_fstp (code, 0); /* pop the 1.0 */
3685 x86_patch (check_pos, code);
3686 x86_patch (end_tan, code);
3688 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3689 x86_pop_reg (code, X86_EAX);
3696 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3707 x86_push_reg (code, X86_EAX);
3708 /* we need to exchange ST(0) with ST(1) */
3711 /* this requires a loop, because fprem somtimes
3712 * returns a partial remainder */
3714 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3715 /* x86_fprem1 (code); */
3718 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3720 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
3725 x86_pop_reg (code, X86_EAX);
3729 if (cfg->opt & MONO_OPT_FCMOV) {
3730 x86_fcomip (code, 1);
3734 /* this overwrites EAX */
3735 EMIT_FPCOMPARE(code);
3736 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3739 if (cfg->opt & MONO_OPT_FCMOV) {
3740 /* zeroing the register at the start results in
3741 * shorter and faster code (we can also remove the widening op)
3743 guchar *unordered_check;
3744 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3745 x86_fcomip (code, 1);
3747 unordered_check = code;
3748 x86_branch8 (code, X86_CC_P, 0, FALSE);
3749 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3750 x86_patch (unordered_check, code);
3753 if (ins->dreg != X86_EAX)
3754 x86_push_reg (code, X86_EAX);
3756 EMIT_FPCOMPARE(code);
3757 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3758 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3759 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3760 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3762 if (ins->dreg != X86_EAX)
3763 x86_pop_reg (code, X86_EAX);
3767 if (cfg->opt & MONO_OPT_FCMOV) {
3768 /* zeroing the register at the start results in
3769 * shorter and faster code (we can also remove the widening op)
3771 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3772 x86_fcomip (code, 1);
3774 if (ins->opcode == OP_FCLT_UN) {
3775 guchar *unordered_check = code;
3776 guchar *jump_to_end;
3777 x86_branch8 (code, X86_CC_P, 0, FALSE);
3778 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3780 x86_jump8 (code, 0);
3781 x86_patch (unordered_check, code);
3782 x86_inc_reg (code, ins->dreg);
3783 x86_patch (jump_to_end, code);
3785 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3789 if (ins->dreg != X86_EAX)
3790 x86_push_reg (code, X86_EAX);
3792 EMIT_FPCOMPARE(code);
3793 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3794 if (ins->opcode == OP_FCLT_UN) {
3795 guchar *is_not_zero_check, *end_jump;
3796 is_not_zero_check = code;
3797 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3799 x86_jump8 (code, 0);
3800 x86_patch (is_not_zero_check, code);
3801 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3803 x86_patch (end_jump, code);
3805 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3806 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3808 if (ins->dreg != X86_EAX)
3809 x86_pop_reg (code, X86_EAX);
3813 if (cfg->opt & MONO_OPT_FCMOV) {
3814 /* zeroing the register at the start results in
3815 * shorter and faster code (we can also remove the widening op)
3817 guchar *unordered_check;
3818 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3819 x86_fcomip (code, 1);
3821 if (ins->opcode == OP_FCGT) {
3822 unordered_check = code;
3823 x86_branch8 (code, X86_CC_P, 0, FALSE);
3824 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3825 x86_patch (unordered_check, code);
3827 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3831 if (ins->dreg != X86_EAX)
3832 x86_push_reg (code, X86_EAX);
3834 EMIT_FPCOMPARE(code);
3835 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3836 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3837 if (ins->opcode == OP_FCGT_UN) {
3838 guchar *is_not_zero_check, *end_jump;
3839 is_not_zero_check = code;
3840 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3842 x86_jump8 (code, 0);
3843 x86_patch (is_not_zero_check, code);
3844 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3846 x86_patch (end_jump, code);
3848 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3849 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3851 if (ins->dreg != X86_EAX)
3852 x86_pop_reg (code, X86_EAX);
3855 if (cfg->opt & MONO_OPT_FCMOV) {
3856 guchar *jump = code;
3857 x86_branch8 (code, X86_CC_P, 0, TRUE);
3858 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3859 x86_patch (jump, code);
3862 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3863 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
3866 /* Branch if C013 != 100 */
3867 if (cfg->opt & MONO_OPT_FCMOV) {
3868 /* branch if !ZF or (PF|CF) */
3869 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3870 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3871 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
3874 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3875 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3878 if (cfg->opt & MONO_OPT_FCMOV) {
3879 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3882 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3885 if (cfg->opt & MONO_OPT_FCMOV) {
3886 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3887 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3890 if (ins->opcode == OP_FBLT_UN) {
3891 guchar *is_not_zero_check, *end_jump;
3892 is_not_zero_check = code;
3893 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3895 x86_jump8 (code, 0);
3896 x86_patch (is_not_zero_check, code);
3897 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3899 x86_patch (end_jump, code);
3901 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3905 if (cfg->opt & MONO_OPT_FCMOV) {
3906 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3909 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3910 if (ins->opcode == OP_FBGT_UN) {
3911 guchar *is_not_zero_check, *end_jump;
3912 is_not_zero_check = code;
3913 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3915 x86_jump8 (code, 0);
3916 x86_patch (is_not_zero_check, code);
3917 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3919 x86_patch (end_jump, code);
3921 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3924 /* Branch if C013 == 100 or 001 */
3925 if (cfg->opt & MONO_OPT_FCMOV) {
3928 /* skip branch if C1=1 */
3930 x86_branch8 (code, X86_CC_P, 0, FALSE);
3931 /* branch if (C0 | C3) = 1 */
3932 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
3933 x86_patch (br1, code);
3936 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3937 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3938 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3939 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3942 /* Branch if C013 == 000 */
3943 if (cfg->opt & MONO_OPT_FCMOV) {
3944 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
3947 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3950 /* Branch if C013=000 or 100 */
3951 if (cfg->opt & MONO_OPT_FCMOV) {
3954 /* skip branch if C1=1 */
3956 x86_branch8 (code, X86_CC_P, 0, FALSE);
3957 /* branch if C0=0 */
3958 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
3959 x86_patch (br1, code);
3962 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
3963 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
3964 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3967 /* Branch if C013 != 001 */
3968 if (cfg->opt & MONO_OPT_FCMOV) {
3969 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3970 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
3973 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3974 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3976 case CEE_CKFINITE: {
3977 x86_push_reg (code, X86_EAX);
3980 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
3981 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3982 x86_pop_reg (code, X86_EAX);
3983 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
3987 code = emit_tls_get (code, ins->dreg, ins->inst_offset);
3990 case OP_ATOMIC_ADD_I4: {
3991 int dreg = ins->dreg;
3993 if (dreg == ins->inst_basereg) {
3994 x86_push_reg (code, ins->sreg2);
3998 if (dreg != ins->sreg2)
3999 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
4001 x86_prefix (code, X86_LOCK_PREFIX);
4002 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4004 if (dreg != ins->dreg) {
4005 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4006 x86_pop_reg (code, dreg);
4011 case OP_ATOMIC_ADD_NEW_I4: {
4012 int dreg = ins->dreg;
4014 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
4015 if (ins->sreg2 == dreg) {
4016 if (dreg == X86_EBX) {
4018 if (ins->inst_basereg == X86_EDI)
4022 if (ins->inst_basereg == X86_EBX)
4025 } else if (ins->inst_basereg == dreg) {
4026 if (dreg == X86_EBX) {
4028 if (ins->sreg2 == X86_EDI)
4032 if (ins->sreg2 == X86_EBX)
4037 if (dreg != ins->dreg) {
4038 x86_push_reg (code, dreg);
4041 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
4042 x86_prefix (code, X86_LOCK_PREFIX);
4043 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
4044 /* dreg contains the old value, add with sreg2 value */
4045 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
4047 if (ins->dreg != dreg) {
4048 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
4049 x86_pop_reg (code, dreg);
4054 case OP_ATOMIC_EXCHANGE_I4: {
4056 int sreg2 = ins->sreg2;
4057 int breg = ins->inst_basereg;
4059 /* cmpxchg uses eax as comperand, need to make sure we can use it
4060 * hack to overcome limits in x86 reg allocator
4061 * (req: dreg == eax and sreg2 != eax and breg != eax)
4063 if (ins->dreg != X86_EAX)
4064 x86_push_reg (code, X86_EAX);
4066 /* We need the EAX reg for the cmpxchg */
4067 if (ins->sreg2 == X86_EAX) {
4068 x86_push_reg (code, X86_EDX);
4069 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
4073 if (breg == X86_EAX) {
4074 x86_push_reg (code, X86_ESI);
4075 x86_mov_reg_reg (code, X86_ESI, X86_EAX, 4);
4079 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
4081 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
4082 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
4083 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
4084 x86_patch (br [1], br [0]);
4086 if (breg != ins->inst_basereg)
4087 x86_pop_reg (code, X86_ESI);
4089 if (ins->dreg != X86_EAX) {
4090 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
4091 x86_pop_reg (code, X86_EAX);
4094 if (ins->sreg2 != sreg2)
4095 x86_pop_reg (code, X86_EDX);
4100 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4101 g_assert_not_reached ();
4104 if ((code - cfg->native_code - offset) > max_len) {
4105 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4106 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4107 g_assert_not_reached ();
4113 last_offset = offset;
4118 cfg->code_len = code - cfg->native_code;
4122 mono_arch_register_lowlevel_calls (void)
4127 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
4129 MonoJumpInfo *patch_info;
4130 gboolean compile_aot = !run_cctors;
4132 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4133 unsigned char *ip = patch_info->ip.i + code;
4134 const unsigned char *target;
4136 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4139 switch (patch_info->type) {
4140 case MONO_PATCH_INFO_BB:
4141 case MONO_PATCH_INFO_LABEL:
4144 /* No need to patch these */
4149 switch (patch_info->type) {
4150 case MONO_PATCH_INFO_IP:
4151 *((gconstpointer *)(ip)) = target;
4153 case MONO_PATCH_INFO_CLASS_INIT: {
4155 /* Might already been changed to a nop */
4156 x86_call_code (code, 0);
4157 x86_patch (ip, target);
4160 case MONO_PATCH_INFO_ABS:
4161 case MONO_PATCH_INFO_METHOD:
4162 case MONO_PATCH_INFO_METHOD_JUMP:
4163 case MONO_PATCH_INFO_INTERNAL_METHOD:
4164 case MONO_PATCH_INFO_BB:
4165 case MONO_PATCH_INFO_LABEL:
4166 x86_patch (ip, target);
4168 case MONO_PATCH_INFO_NONE:
4171 guint32 offset = mono_arch_get_patch_offset (ip);
4172 *((gconstpointer *)(ip + offset)) = target;
4180 mono_arch_emit_prolog (MonoCompile *cfg)
4182 MonoMethod *method = cfg->method;
4184 MonoMethodSignature *sig;
4186 int alloc_size, pos, max_offset, i;
4189 cfg->code_size = MAX (mono_method_get_header (method)->code_size * 4, 256);
4190 code = cfg->native_code = g_malloc (cfg->code_size);
4192 x86_push_reg (code, X86_EBP);
4193 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
4195 alloc_size = - cfg->stack_offset;
4198 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
4199 /* Might need to attach the thread to the JIT */
4200 if (lmf_tls_offset != -1) {
4203 code = emit_tls_get ( code, X86_EAX, lmf_tls_offset);
4204 #ifdef PLATFORM_WIN32
4205 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
4206 /* FIXME: Add a separate key for LMF to avoid this */
4207 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
4209 x86_test_reg_reg (code, X86_EAX, X86_EAX);
4211 x86_branch8 (code, X86_CC_NE, 0, 0);
4212 x86_push_imm (code, cfg->domain);
4213 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
4214 x86_patch (buf, code);
4217 g_assert (!cfg->compile_aot);
4218 x86_push_imm (code, cfg->domain);
4219 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
4223 if (method->save_lmf) {
4224 pos += sizeof (MonoLMF);
4226 /* save the current IP */
4227 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
4228 x86_push_imm_template (code);
4230 /* save all caller saved regs */
4231 x86_push_reg (code, X86_EBP);
4232 x86_push_reg (code, X86_ESI);
4233 x86_push_reg (code, X86_EDI);
4234 x86_push_reg (code, X86_EBX);
4236 /* save method info */
4237 x86_push_imm (code, method);
4239 /* get the address of lmf for the current thread */
4241 * This is performance critical so we try to use some tricks to make
4244 if (lmf_tls_offset != -1) {
4245 /* Load lmf quicky using the GS register */
4246 code = emit_tls_get (code, X86_EAX, lmf_tls_offset);
4247 #ifdef PLATFORM_WIN32
4248 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
4249 /* FIXME: Add a separate key for LMF to avoid this */
4250 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
4254 if (cfg->compile_aot) {
4255 /* The GOT var does not exist yet */
4256 x86_call_imm (code, 0);
4257 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
4258 x86_pop_reg (code, X86_EAX);
4259 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 0);
4260 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
4261 x86_call_membase (code, X86_EAX, 0xf0f0f0f0);
4264 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
4268 x86_push_reg (code, X86_EAX);
4269 /* push *lfm (previous_lmf) */
4270 x86_push_membase (code, X86_EAX, 0);
4272 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
4275 if (cfg->used_int_regs & (1 << X86_EBX)) {
4276 x86_push_reg (code, X86_EBX);
4280 if (cfg->used_int_regs & (1 << X86_EDI)) {
4281 x86_push_reg (code, X86_EDI);
4285 if (cfg->used_int_regs & (1 << X86_ESI)) {
4286 x86_push_reg (code, X86_ESI);
4294 /* See mono_emit_stack_alloc */
4295 #ifdef PLATFORM_WIN32
4296 guint32 remaining_size = alloc_size;
4297 while (remaining_size >= 0x1000) {
4298 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
4299 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
4300 remaining_size -= 0x1000;
4303 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
4305 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
4309 /* compute max_offset in order to use short forward jumps */
4311 if (cfg->opt & MONO_OPT_BRANCH) {
4312 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4313 MonoInst *ins = bb->code;
4314 bb->max_offset = max_offset;
4316 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
4318 /* max alignment for loops */
4319 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
4320 max_offset += LOOP_ALIGNMENT;
4323 if (ins->opcode == OP_LABEL)
4324 ins->inst_c1 = max_offset;
4326 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
4332 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4333 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
4335 /* load arguments allocated to register from the stack */
4336 sig = mono_method_signature (method);
4339 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4340 inst = cfg->varinfo [pos];
4341 if (inst->opcode == OP_REGVAR) {
4342 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
4343 if (cfg->verbose_level > 2)
4344 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
4349 cfg->code_len = code - cfg->native_code;
4355 mono_arch_emit_epilog (MonoCompile *cfg)
4357 MonoMethod *method = cfg->method;
4358 MonoMethodSignature *sig = mono_method_signature (method);
4360 guint32 stack_to_pop;
4362 int max_epilog_size = 16;
4365 if (cfg->method->save_lmf)
4366 max_epilog_size += 128;
4368 if (mono_jit_trace_calls != NULL)
4369 max_epilog_size += 50;
4371 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4372 cfg->code_size *= 2;
4373 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4374 mono_jit_stats.code_reallocs++;
4377 code = cfg->native_code + cfg->code_len;
4379 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4380 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
4382 /* the code restoring the registers must be kept in sync with CEE_JMP */
4385 if (method->save_lmf) {
4386 gint32 prev_lmf_reg;
4388 /* Find a spare register */
4389 switch (sig->ret->type) {
4392 prev_lmf_reg = X86_EDI;
4393 cfg->used_int_regs |= (1 << X86_EDI);
4396 prev_lmf_reg = X86_EDX;
4400 /* reg = previous_lmf */
4401 x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, -32, 4);
4404 x86_mov_reg_membase (code, X86_ECX, X86_EBP, -28, 4);
4406 /* *(lmf) = previous_lmf */
4407 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
4409 /* restore caller saved regs */
4410 if (cfg->used_int_regs & (1 << X86_EBX)) {
4411 x86_mov_reg_membase (code, X86_EBX, X86_EBP, -20, 4);
4414 if (cfg->used_int_regs & (1 << X86_EDI)) {
4415 x86_mov_reg_membase (code, X86_EDI, X86_EBP, -16, 4);
4417 if (cfg->used_int_regs & (1 << X86_ESI)) {
4418 x86_mov_reg_membase (code, X86_ESI, X86_EBP, -12, 4);
4421 /* EBP is restored by LEAVE */
4423 if (cfg->used_int_regs & (1 << X86_EBX)) {
4426 if (cfg->used_int_regs & (1 << X86_EDI)) {
4429 if (cfg->used_int_regs & (1 << X86_ESI)) {
4434 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
4436 if (cfg->used_int_regs & (1 << X86_ESI)) {
4437 x86_pop_reg (code, X86_ESI);
4439 if (cfg->used_int_regs & (1 << X86_EDI)) {
4440 x86_pop_reg (code, X86_EDI);
4442 if (cfg->used_int_regs & (1 << X86_EBX)) {
4443 x86_pop_reg (code, X86_EBX);
4447 /* Load returned vtypes into registers if needed */
4448 cinfo = get_call_info (sig, FALSE);
4449 if (cinfo->ret.storage == ArgValuetypeInReg) {
4450 for (quad = 0; quad < 2; quad ++) {
4451 switch (cinfo->ret.pair_storage [quad]) {
4453 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
4455 case ArgOnFloatFpStack:
4456 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
4458 case ArgOnDoubleFpStack:
4459 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
4464 g_assert_not_reached ();
4471 if (CALLCONV_IS_STDCALL (sig)) {
4472 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
4474 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
4475 } else if (MONO_TYPE_ISSTRUCT (mono_method_signature (cfg->method)->ret) && (cinfo->ret.storage == ArgOnStack))
4481 x86_ret_imm (code, stack_to_pop);
4487 cfg->code_len = code - cfg->native_code;
4489 g_assert (cfg->code_len < cfg->code_size);
4493 mono_arch_emit_exceptions (MonoCompile *cfg)
4495 MonoJumpInfo *patch_info;
4498 MonoClass *exc_classes [16];
4499 guint8 *exc_throw_start [16], *exc_throw_end [16];
4503 /* Compute needed space */
4504 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4505 if (patch_info->type == MONO_PATCH_INFO_EXC)
4510 * make sure we have enough space for exceptions
4511 * 16 is the size of two push_imm instructions and a call
4513 if (cfg->compile_aot)
4514 code_size = exc_count * 32;
4516 code_size = exc_count * 16;
4518 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
4519 cfg->code_size *= 2;
4520 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4521 mono_jit_stats.code_reallocs++;
4524 code = cfg->native_code + cfg->code_len;
4527 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4528 switch (patch_info->type) {
4529 case MONO_PATCH_INFO_EXC: {
4530 MonoClass *exc_class;
4534 x86_patch (patch_info->ip.i + cfg->native_code, code);
4536 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4537 g_assert (exc_class);
4538 throw_ip = patch_info->ip.i;
4540 /* Find a throw sequence for the same exception class */
4541 for (i = 0; i < nthrows; ++i)
4542 if (exc_classes [i] == exc_class)
4545 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
4546 x86_jump_code (code, exc_throw_start [i]);
4547 patch_info->type = MONO_PATCH_INFO_NONE;
4550 guint32 got_reg = X86_EAX;
4553 /* Compute size of code following the push <OFFSET> */
4554 if (cfg->compile_aot) {
4558 else if (cfg->got_var->opcode == OP_REGOFFSET)
4564 if ((code - cfg->native_code) - throw_ip < 126 - size) {
4565 /* Use the shorter form */
4567 x86_push_imm (code, 0);
4571 x86_push_imm (code, 0xf0f0f0f0);
4576 exc_classes [nthrows] = exc_class;
4577 exc_throw_start [nthrows] = code;
4580 if (cfg->compile_aot) {
4582 * Since the patches are generated by the back end, there is * no way to generate a got_var at this point.
4584 if (!cfg->got_var) {
4585 x86_call_imm (code, 0);
4586 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
4587 x86_pop_reg (code, X86_EAX);
4588 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 0);
4591 if (cfg->got_var->opcode == OP_REGOFFSET)
4592 x86_mov_reg_membase (code, X86_EAX, cfg->got_var->inst_basereg, cfg->got_var->inst_offset, 4);
4594 got_reg = cfg->got_var->dreg;
4598 x86_push_imm (code, exc_class->type_token);
4599 patch_info->data.name = "mono_arch_throw_corlib_exception";
4600 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4601 patch_info->ip.i = code - cfg->native_code;
4602 if (cfg->compile_aot)
4603 x86_call_membase (code, got_reg, 0xf0f0f0f0);
4605 x86_call_code (code, 0);
4606 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
4611 exc_throw_end [nthrows] = code;
4623 cfg->code_len = code - cfg->native_code;
4625 g_assert (cfg->code_len < cfg->code_size);
4629 mono_arch_flush_icache (guint8 *code, gint size)
4635 mono_arch_flush_register_windows (void)
4640 * Support for fast access to the thread-local lmf structure using the GS
4641 * segment register on NPTL + kernel 2.6.x.
4644 static gboolean tls_offset_inited = FALSE;
4647 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4649 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4650 pthread_t self = pthread_self();
4651 pthread_attr_t attr;
4652 void *staddr = NULL;
4654 struct sigaltstack sa;
4657 if (!tls_offset_inited) {
4658 if (!getenv ("MONO_NO_TLS")) {
4659 #ifdef PLATFORM_WIN32
4661 * We need to init this multiple times, since when we are first called, the key might not
4662 * be initialized yet.
4664 appdomain_tls_offset = mono_domain_get_tls_key ();
4665 lmf_tls_offset = mono_get_jit_tls_key ();
4666 thread_tls_offset = mono_thread_get_tls_key ();
4668 /* Only 64 tls entries can be accessed using inline code */
4669 if (appdomain_tls_offset >= 64)
4670 appdomain_tls_offset = -1;
4671 if (lmf_tls_offset >= 64)
4672 lmf_tls_offset = -1;
4673 if (thread_tls_offset >= 64)
4674 thread_tls_offset = -1;
4676 tls_offset_inited = TRUE;
4677 appdomain_tls_offset = mono_domain_get_tls_offset ();
4678 lmf_tls_offset = mono_get_lmf_tls_offset ();
4679 thread_tls_offset = mono_thread_get_tls_offset ();
4684 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4686 /* Determine stack boundaries */
4687 if (!mono_running_on_valgrind ()) {
4688 #ifdef HAVE_PTHREAD_GETATTR_NP
4689 pthread_getattr_np( self, &attr );
4691 #ifdef HAVE_PTHREAD_ATTR_GET_NP
4692 pthread_attr_get_np( self, &attr );
4694 pthread_attr_init( &attr );
4695 pthread_attr_getstacksize( &attr, &stsize );
4697 #error "Not implemented"
4701 pthread_attr_getstack( &attr, &staddr, &stsize );
4706 * staddr seems to be wrong for the main thread, so we keep the value in
4709 tls->stack_size = stsize;
4711 /* Setup an alternate signal stack */
4712 tls->signal_stack = g_malloc (SIGNAL_STACK_SIZE);
4713 tls->signal_stack_size = SIGNAL_STACK_SIZE;
4715 sa.ss_sp = tls->signal_stack;
4716 sa.ss_size = SIGNAL_STACK_SIZE;
4717 sa.ss_flags = SS_ONSTACK;
4718 sigaltstack (&sa, NULL);
4723 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4725 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4726 struct sigaltstack sa;
4728 sa.ss_sp = tls->signal_stack;
4729 sa.ss_size = SIGNAL_STACK_SIZE;
4730 sa.ss_flags = SS_DISABLE;
4731 sigaltstack (&sa, NULL);
4733 if (tls->signal_stack)
4734 g_free (tls->signal_stack);
4739 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
4742 /* add the this argument */
4743 if (this_reg != -1) {
4745 MONO_INST_NEW (cfg, this, OP_OUTARG);
4746 this->type = this_type;
4747 this->sreg1 = this_reg;
4748 mono_bblock_add_inst (cfg->cbb, this);
4752 CallInfo * cinfo = get_call_info (inst->signature, FALSE);
4755 if (cinfo->ret.storage == ArgValuetypeInReg) {
4757 * The valuetype is in EAX:EDX after the call, needs to be copied to
4758 * the stack. Save the address here, so the call instruction can
4761 MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
4762 vtarg->inst_destbasereg = X86_ESP;
4763 vtarg->inst_offset = inst->stack_usage;
4764 vtarg->sreg1 = vt_reg;
4765 mono_bblock_add_inst (cfg->cbb, vtarg);
4769 MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
4770 vtarg->type = STACK_MP;
4771 vtarg->sreg1 = vt_reg;
4772 mono_bblock_add_inst (cfg->cbb, vtarg);
4781 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4783 MonoInst *ins = NULL;
4785 if (cmethod->klass == mono_defaults.math_class) {
4786 if (strcmp (cmethod->name, "Sin") == 0) {
4787 MONO_INST_NEW (cfg, ins, OP_SIN);
4788 ins->inst_i0 = args [0];
4789 } else if (strcmp (cmethod->name, "Cos") == 0) {
4790 MONO_INST_NEW (cfg, ins, OP_COS);
4791 ins->inst_i0 = args [0];
4792 } else if (strcmp (cmethod->name, "Tan") == 0) {
4793 MONO_INST_NEW (cfg, ins, OP_TAN);
4794 ins->inst_i0 = args [0];
4795 } else if (strcmp (cmethod->name, "Atan") == 0) {
4796 MONO_INST_NEW (cfg, ins, OP_ATAN);
4797 ins->inst_i0 = args [0];
4798 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
4799 MONO_INST_NEW (cfg, ins, OP_SQRT);
4800 ins->inst_i0 = args [0];
4801 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
4802 MONO_INST_NEW (cfg, ins, OP_ABS);
4803 ins->inst_i0 = args [0];
4806 /* OP_FREM is not IEEE compatible */
4807 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
4808 MONO_INST_NEW (cfg, ins, OP_FREM);
4809 ins->inst_i0 = args [0];
4810 ins->inst_i1 = args [1];
4813 } else if(cmethod->klass->image == mono_defaults.corlib &&
4814 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
4815 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
4817 if (strcmp (cmethod->name, "Increment") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4818 MonoInst *ins_iconst;
4820 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
4821 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4822 ins_iconst->inst_c0 = 1;
4824 ins->inst_i0 = args [0];
4825 ins->inst_i1 = ins_iconst;
4826 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4827 MonoInst *ins_iconst;
4829 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
4830 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4831 ins_iconst->inst_c0 = -1;
4833 ins->inst_i0 = args [0];
4834 ins->inst_i1 = ins_iconst;
4835 } else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4836 MONO_INST_NEW (cfg, ins, OP_ATOMIC_EXCHANGE_I4);
4838 ins->inst_i0 = args [0];
4839 ins->inst_i1 = args [1];
4840 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4841 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_I4);
4843 ins->inst_i0 = args [0];
4844 ins->inst_i1 = args [1];
4853 mono_arch_print_tree (MonoInst *tree, int arity)
4858 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4862 if (appdomain_tls_offset == -1)
4865 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
4866 ins->inst_offset = appdomain_tls_offset;
4870 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
4874 if (thread_tls_offset == -1)
4877 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
4878 ins->inst_offset = thread_tls_offset;
4883 mono_arch_get_patch_offset (guint8 *code)
4885 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
4887 else if ((code [0] == 0xba))
4889 else if ((code [0] == 0x68))
4892 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
4893 /* push <OFFSET>(<REG>) */
4895 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
4896 /* call *<OFFSET>(<REG>) */
4898 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
4901 else if ((code [0] == 0x58) && (code [1] == 0x05))
4902 /* pop %eax; add <OFFSET>, %eax */
4904 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
4905 /* pop <REG>; add <OFFSET>, <REG> */
4908 g_assert_not_reached ();
4914 mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
4919 /* go to the start of the call instruction
4921 * address_byte = (m << 6) | (o << 3) | reg
4922 * call opcode: 0xff address_byte displacement
4924 * 0xff m=2,o=2 imm32
4927 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
4928 reg = code [4] & 0x07;
4929 disp = (signed char)code [5];
4931 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
4932 reg = code [1] & 0x07;
4933 disp = *((gint32*)(code + 2));
4934 } else if ((code [1] == 0xe8)) {
4936 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
4938 * This is a interface call: should check the above code can't catch it earlier
4939 * 8b 40 30 mov 0x30(%eax),%eax
4940 * ff 10 call *(%eax)
4943 reg = code [5] & 0x07;
4949 return (gpointer*)(((gint32)(regs [reg])) + disp);
4953 mono_arch_get_delegate_method_ptr_addr (guint8* code, gpointer *regs)
4959 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 3) && (x86_modrm_reg (code [1]) == X86_EAX) && (code [2] == 0x8b) && (code [3] == 0x40) && (code [5] == 0xff) && (code [6] == 0xd0)) {
4960 reg = x86_modrm_rm (code [1]);
4966 return (gpointer*)(((gint32)(regs [reg])) + disp);