2 * mini-x86.c: x86 backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/debug-helpers.h>
16 #include <mono/metadata/profiler-private.h>
20 #include "cpu-pentium.h"
23 mono_arch_regname (int reg) {
25 case X86_EAX: return "%eax";
26 case X86_EBX: return "%ebx";
27 case X86_ECX: return "%ecx";
28 case X86_EDX: return "%edx";
29 case X86_ESP: return "%esp";
30 case X86_EBP: return "%ebp";
31 case X86_EDI: return "%edi";
32 case X86_ESI: return "%esi";
41 } MonoJitArgumentInfo;
44 * arch_get_argument_info:
45 * @csig: a method signature
46 * @param_count: the number of parameters to consider
47 * @arg_info: an array to store the result infos
49 * Gathers information on parameters such as size, alignment and
50 * padding. arg_info should be large enought to hold param_count + 1 entries.
52 * Returns the size of the activation frame.
55 arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
57 int k, frame_size = 0;
61 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
62 frame_size += sizeof (gpointer);
66 arg_info [0].offset = offset;
69 frame_size += sizeof (gpointer);
73 arg_info [0].size = frame_size;
75 for (k = 0; k < param_count; k++) {
78 size = mono_type_native_stack_size (csig->params [k], &align);
80 size = mono_type_stack_size (csig->params [k], &align);
82 /* ignore alignment for now */
85 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
86 arg_info [k].pad = pad;
88 arg_info [k + 1].pad = 0;
89 arg_info [k + 1].size = size;
91 arg_info [k + 1].offset = offset;
95 align = MONO_ARCH_FRAME_ALIGNMENT;
96 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
97 arg_info [k].pad = pad;
102 static int indent_level = 0;
104 static void indent (int diff) {
105 int v = indent_level;
109 indent_level += diff;
113 enter_method (MonoMethod *method, char *ebp)
118 MonoJitArgumentInfo *arg_info;
119 MonoMethodSignature *sig;
122 fname = mono_method_full_name (method, TRUE);
124 printf ("ENTER: %s(", fname);
127 if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
128 g_error ("unaligned stack detected (%p)", ebp);
131 sig = method->signature;
133 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
135 arch_get_argument_info (sig, sig->param_count, arg_info);
137 if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
138 g_assert (!method->signature->ret->byref);
140 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
143 if (method->signature->hasthis) {
144 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
145 if (method->klass->valuetype) {
146 printf ("value:%p, ", *this);
148 o = *((MonoObject **)this);
151 class = o->vtable->klass;
153 if (class == mono_defaults.string_class) {
154 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
156 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
159 printf ("this:NULL, ");
163 for (i = 0; i < method->signature->param_count; ++i) {
164 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
165 int size = arg_info [i + 1].size;
167 MonoType *type = method->signature->params [i];
170 printf ("[BYREF:%p], ", *cpos);
171 } else switch (type->type) {
175 printf ("%p, ", (gpointer)*((int *)(cpos)));
177 case MONO_TYPE_BOOLEAN:
185 printf ("%d, ", *((int *)(cpos)));
187 case MONO_TYPE_STRING: {
188 MonoString *s = *((MonoString **)cpos);
190 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
191 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
193 printf ("[STRING:null], ");
196 case MONO_TYPE_CLASS:
197 case MONO_TYPE_OBJECT: {
198 o = *((MonoObject **)cpos);
200 class = o->vtable->klass;
202 if (class == mono_defaults.string_class) {
203 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
204 } else if (class == mono_defaults.int32_class) {
205 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
207 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
209 printf ("%p, ", *((gpointer *)(cpos)));
214 case MONO_TYPE_FNPTR:
215 case MONO_TYPE_ARRAY:
216 case MONO_TYPE_SZARRAY:
217 printf ("%p, ", *((gpointer *)(cpos)));
221 printf ("0x%016llx, ", *((gint64 *)(cpos)));
224 printf ("%f, ", *((float *)(cpos)));
227 printf ("%f, ", *((double *)(cpos)));
229 case MONO_TYPE_VALUETYPE:
231 for (j = 0; j < size; j++)
232 printf ("%02x,", *((guint8*)cpos +j));
244 leave_method (MonoMethod *method, ...)
250 va_start(ap, method);
252 fname = mono_method_full_name (method, TRUE);
254 printf ("LEAVE: %s", fname);
257 type = method->signature->ret;
260 switch (type->type) {
263 case MONO_TYPE_BOOLEAN: {
264 int eax = va_arg (ap, int);
266 printf ("TRUE:%d", eax);
281 int eax = va_arg (ap, int);
282 printf ("EAX=%d", eax);
285 case MONO_TYPE_STRING: {
286 MonoString *s = va_arg (ap, MonoString *);
289 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
290 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
292 printf ("[STRING:null], ");
295 case MONO_TYPE_CLASS:
296 case MONO_TYPE_OBJECT: {
297 MonoObject *o = va_arg (ap, MonoObject *);
300 if (o->vtable->klass == mono_defaults.boolean_class) {
301 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
302 } else if (o->vtable->klass == mono_defaults.int32_class) {
303 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
304 } else if (o->vtable->klass == mono_defaults.int64_class) {
305 printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
307 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
309 printf ("[OBJECT:%p]", o);
314 case MONO_TYPE_FNPTR:
315 case MONO_TYPE_ARRAY:
316 case MONO_TYPE_SZARRAY: {
317 gpointer p = va_arg (ap, gpointer);
318 printf ("EAX=%p", p);
322 gint64 l = va_arg (ap, gint64);
323 printf ("EAX/EDX=0x%16llx", l);
327 gint64 l = va_arg (ap, gint64);
328 printf ("EAX/EDX=0x%16llx", l);
332 double f = va_arg (ap, double);
333 printf ("FP=%f\n", f);
336 case MONO_TYPE_VALUETYPE:
337 if (type->data.klass->enumtype) {
338 type = type->data.klass->enum_basetype;
341 guint8 *p = va_arg (ap, gpointer);
343 size = mono_type_size (type, &align);
345 for (j = 0; p && j < size; j++)
346 printf ("%02x,", p [j]);
351 printf ("(unknown return type %x)", method->signature->ret->type);
357 static const guchar cpuid_impl [] = {
358 0x55, /* push %ebp */
359 0x89, 0xe5, /* mov %esp,%ebp */
360 0x53, /* push %ebx */
361 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
362 0x0f, 0xa2, /* cpuid */
363 0x50, /* push %eax */
364 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
365 0x89, 0x18, /* mov %ebx,(%eax) */
366 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
367 0x89, 0x08, /* mov %ecx,(%eax) */
368 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
369 0x89, 0x10, /* mov %edx,(%eax) */
371 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
372 0x89, 0x02, /* mov %eax,(%edx) */
378 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
381 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
384 __asm__ __volatile__ (
387 "movl %%eax, %%edx\n"
388 "xorl $0x200000, %%eax\n"
393 "xorl %%edx, %%eax\n"
394 "andl $0x200000, %%eax\n"
402 CpuidFunc func = (CpuidFunc)cpuid_impl;
403 func (id, p_eax, p_ebx, p_ecx, p_edx);
405 * We use this approach because of issues with gcc and pic code, see:
406 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
407 __asm__ __volatile__ ("cpuid"
408 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
417 * Initialize the cpu to execute managed code.
420 mono_arch_cpu_init (void)
424 /* spec compliance requires running with double precision */
425 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
426 fpcw &= ~X86_FPCW_PRECC_MASK;
427 fpcw |= X86_FPCW_PREC_DOUBLE;
428 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
429 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
434 * This function returns the optimizations supported on this cpu.
437 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
439 int eax, ebx, ecx, edx;
443 /* Feature Flags function, flags returned in EDX. */
444 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
445 if (edx & (1 << 15)) {
446 opts |= MONO_OPT_CMOV;
448 opts |= MONO_OPT_FCMOV;
450 *exclude_mask |= MONO_OPT_FCMOV;
452 *exclude_mask |= MONO_OPT_CMOV;
458 is_regsize_var (MonoType *t) {
467 case MONO_TYPE_OBJECT:
468 case MONO_TYPE_STRING:
469 case MONO_TYPE_CLASS:
470 case MONO_TYPE_SZARRAY:
471 case MONO_TYPE_ARRAY:
473 case MONO_TYPE_VALUETYPE:
474 if (t->data.klass->enumtype)
475 return is_regsize_var (t->data.klass->enum_basetype);
482 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
487 for (i = 0; i < cfg->num_varinfo; i++) {
488 MonoInst *ins = cfg->varinfo [i];
489 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
492 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
495 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
496 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
499 /* we dont allocate I1 to registers because there is no simply way to sign extend
500 * 8bit quantities in caller saved registers on x86 */
501 if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) ||
502 (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
503 (ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
504 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
505 g_assert (i == vmv->idx);
506 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
514 mono_arch_get_global_int_regs (MonoCompile *cfg)
518 /* we can use 3 registers for global allocation */
519 regs = g_list_prepend (regs, (gpointer)X86_EBX);
520 regs = g_list_prepend (regs, (gpointer)X86_ESI);
521 regs = g_list_prepend (regs, (gpointer)X86_EDI);
527 * Set var information according to the calling convention. X86 version.
528 * The locals var stuff should most likely be split in another method.
531 mono_arch_allocate_vars (MonoCompile *m)
533 MonoMethodSignature *sig;
534 MonoMethodHeader *header;
536 int i, offset, size, align, curinst;
538 header = ((MonoMethodNormal *)m->method)->header;
540 sig = m->method->signature;
544 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
545 m->ret->opcode = OP_REGOFFSET;
546 m->ret->inst_basereg = X86_EBP;
547 m->ret->inst_offset = offset;
548 offset += sizeof (gpointer);
550 /* FIXME: handle long and FP values */
551 switch (sig->ret->type) {
555 m->ret->opcode = OP_REGVAR;
556 m->ret->inst_c0 = X86_EAX;
561 inst = m->varinfo [curinst];
562 if (inst->opcode != OP_REGVAR) {
563 inst->opcode = OP_REGOFFSET;
564 inst->inst_basereg = X86_EBP;
566 inst->inst_offset = offset;
567 offset += sizeof (gpointer);
571 if (sig->call_convention == MONO_CALL_VARARG) {
572 m->sig_cookie = offset;
573 offset += sizeof (gpointer);
576 for (i = 0; i < sig->param_count; ++i) {
577 inst = m->varinfo [curinst];
578 if (inst->opcode != OP_REGVAR) {
579 inst->opcode = OP_REGOFFSET;
580 inst->inst_basereg = X86_EBP;
582 inst->inst_offset = offset;
583 size = mono_type_size (sig->params [i], &align);
592 /* reserve space to save LMF and caller saved registers */
594 if (m->method->save_lmf) {
595 offset += sizeof (MonoLMF);
597 if (m->used_int_regs & (1 << X86_EBX)) {
601 if (m->used_int_regs & (1 << X86_EDI)) {
605 if (m->used_int_regs & (1 << X86_ESI)) {
610 for (i = curinst; i < m->num_varinfo; ++i) {
611 inst = m->varinfo [i];
613 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
616 /* inst->unused indicates native sized value types, this is used by the
617 * pinvoke wrappers when they call functions returning structure */
618 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
619 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
621 size = mono_type_size (inst->inst_vtype, &align);
625 offset &= ~(align - 1);
626 inst->opcode = OP_REGOFFSET;
627 inst->inst_basereg = X86_EBP;
628 inst->inst_offset = -offset;
629 //g_print ("allocating local %d to %d\n", i, -offset);
631 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
632 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
635 m->stack_offset = -offset;
638 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
639 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
643 * take the arguments and generate the arch-specific
644 * instructions to properly call the function in call.
645 * This includes pushing, moving arguments to the right register
647 * Issue: who does the spilling if needed, and when?
650 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
652 MonoMethodSignature *sig;
653 int i, n, stack_size, type;
657 /* add the vararg cookie before the non-implicit args */
658 if (call->signature->call_convention == MONO_CALL_VARARG) {
660 MONO_INST_NEW (cfg, arg, OP_OUTARG);
661 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
662 sig_arg->inst_p0 = call->signature;
663 arg->inst_left = sig_arg;
664 arg->type = STACK_PTR;
665 /* prepend, so they get reversed */
666 arg->next = call->out_args;
667 call->out_args = arg;
668 stack_size += sizeof (gpointer);
670 sig = call->signature;
671 n = sig->param_count + sig->hasthis;
673 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
674 stack_size += sizeof (gpointer);
675 for (i = 0; i < n; ++i) {
676 if (is_virtual && i == 0) {
677 /* the argument will be attached to the call instrucion */
681 MONO_INST_NEW (cfg, arg, OP_OUTARG);
683 arg->cil_code = in->cil_code;
685 arg->type = in->type;
686 /* prepend, so they get reversed */
687 arg->next = call->out_args;
688 call->out_args = arg;
689 if (i >= sig->hasthis) {
690 ptype = sig->params [i - sig->hasthis];
696 /* FIXME: validate arguments... */
700 case MONO_TYPE_BOOLEAN:
708 case MONO_TYPE_STRING:
709 case MONO_TYPE_CLASS:
710 case MONO_TYPE_OBJECT:
712 case MONO_TYPE_FNPTR:
713 case MONO_TYPE_ARRAY:
714 case MONO_TYPE_SZARRAY:
723 arg->opcode = OP_OUTARG_R4;
727 arg->opcode = OP_OUTARG_R8;
729 case MONO_TYPE_VALUETYPE:
730 if (MONO_TYPE_ISSTRUCT (ptype)) {
733 size = mono_type_native_stack_size (&in->klass->byval_arg, NULL);
735 size = mono_type_stack_size (&in->klass->byval_arg, NULL);
738 arg->opcode = OP_OUTARG_VT;
739 arg->klass = in->klass;
740 arg->unused = sig->pinvoke;
741 arg->inst_imm = size;
743 type = ptype->data.klass->enum_basetype->type;
747 case MONO_TYPE_TYPEDBYREF:
748 stack_size += sizeof (MonoTypedRef);
749 arg->opcode = OP_OUTARG_VT;
750 arg->klass = in->klass;
751 arg->unused = sig->pinvoke;
752 arg->inst_imm = sizeof (MonoTypedRef);
755 g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
758 /* the this argument */
763 /* if the function returns a struct, the called method already does a ret $0x4 */
764 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
766 call->stack_usage = stack_size;
768 * should set more info in call, such as the stack space
769 * used by the args that needs to be added back to esp
776 * Allow tracing to work with this interface (with an optional argument)
780 * This may be needed on some archs or for debugging support.
783 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
785 /* no stack room needed now (may be needed for FASTCALL-trace support) */
787 /* split prolog-epilog requirements? */
788 *code = 50; /* max bytes needed: check this number */
792 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
796 /* if some args are passed in registers, we need to save them here */
797 x86_push_reg (code, X86_EBP);
798 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
799 x86_push_imm (code, cfg->method);
800 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
801 x86_call_code (code, 0);
802 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
816 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
819 int arg_size = 0, save_mode = SAVE_NONE;
820 MonoMethod *method = cfg->method;
821 int rtype = method->signature->ret->type;
826 /* special case string .ctor icall */
827 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
828 save_mode = SAVE_EAX;
830 save_mode = SAVE_NONE;
834 save_mode = SAVE_EAX_EDX;
840 case MONO_TYPE_VALUETYPE:
841 if (method->signature->ret->data.klass->enumtype) {
842 rtype = method->signature->ret->data.klass->enum_basetype->type;
845 save_mode = SAVE_STRUCT;
848 save_mode = SAVE_EAX;
854 x86_push_reg (code, X86_EDX);
855 x86_push_reg (code, X86_EAX);
856 if (enable_arguments) {
857 x86_push_reg (code, X86_EDX);
858 x86_push_reg (code, X86_EAX);
863 x86_push_reg (code, X86_EAX);
864 if (enable_arguments) {
865 x86_push_reg (code, X86_EAX);
870 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
871 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
872 if (enable_arguments) {
873 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
874 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
879 if (enable_arguments) {
880 x86_push_membase (code, X86_EBP, 8);
890 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
891 x86_push_imm (code, method);
892 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
893 x86_call_code (code, 0);
894 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
898 x86_pop_reg (code, X86_EAX);
899 x86_pop_reg (code, X86_EDX);
902 x86_pop_reg (code, X86_EAX);
905 x86_fld_membase (code, X86_ESP, 0, TRUE);
906 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
916 #define EMIT_COND_BRANCH(ins,cond,sign) \
917 if (ins->flags & MONO_INST_BRLABEL) { \
918 if (ins->inst_i0->inst_c0) { \
919 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
921 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
922 x86_branch32 (code, cond, 0, sign); \
925 if (ins->inst_true_bb->native_offset) { \
926 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
928 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
929 if ((cfg->opt & MONO_OPT_BRANCH) && \
930 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
931 x86_branch8 (code, cond, 0, sign); \
933 x86_branch32 (code, cond, 0, sign); \
937 /* emit an exception if condition is fail */
938 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
940 mono_add_patch_info (cfg, code - cfg->native_code, \
941 MONO_PATCH_INFO_EXC, exc_name); \
942 x86_branch32 (code, cond, 0, signed); \
945 #define EMIT_FPCOMPARE(code) do { \
951 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
953 MonoInst *ins, *last_ins = NULL;
958 switch (ins->opcode) {
960 /* reg = 0 -> XOR (reg, reg) */
961 /* XOR sets cflags on x86, so we cant do it always */
962 if (ins->inst_c0 == 0 && ins->next &&
963 (ins->next->opcode == CEE_BR)) {
964 ins->opcode = CEE_XOR;
965 ins->sreg1 = ins->dreg;
966 ins->sreg2 = ins->dreg;
970 /* remove unnecessary multiplication with 1 */
971 if (ins->inst_imm == 1) {
972 if (ins->dreg != ins->sreg1) {
973 ins->opcode = OP_MOVE;
975 last_ins->next = ins->next;
982 /* OP_COMPARE_IMM (reg, 0) --> OP_X86_TEST_NULL (reg) */
983 if (ins->inst_imm == 0 && ins->next &&
984 (ins->next->opcode == CEE_BEQ || ins->next->opcode == CEE_BNE_UN ||
985 ins->next->opcode == OP_CEQ)) {
986 ins->opcode = OP_X86_TEST_NULL;
989 case OP_LOAD_MEMBASE:
990 case OP_LOADI4_MEMBASE:
992 * OP_STORE_MEMBASE_REG reg, offset(basereg)
993 * OP_LOAD_MEMBASE offset(basereg), reg
995 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
996 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
997 ins->inst_basereg == last_ins->inst_destbasereg &&
998 ins->inst_offset == last_ins->inst_offset) {
999 if (ins->dreg == last_ins->sreg1) {
1000 last_ins->next = ins->next;
1004 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1005 ins->opcode = OP_MOVE;
1006 ins->sreg1 = last_ins->sreg1;
1010 * Note: reg1 must be different from the basereg in the second load
1011 * OP_LOAD_MEMBASE offset(basereg), reg1
1012 * OP_LOAD_MEMBASE offset(basereg), reg2
1014 * OP_LOAD_MEMBASE offset(basereg), reg1
1015 * OP_MOVE reg1, reg2
1017 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1018 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1019 ins->inst_basereg != last_ins->dreg &&
1020 ins->inst_basereg == last_ins->inst_basereg &&
1021 ins->inst_offset == last_ins->inst_offset) {
1023 if (ins->dreg == last_ins->dreg) {
1024 last_ins->next = ins->next;
1028 ins->opcode = OP_MOVE;
1029 ins->sreg1 = last_ins->dreg;
1032 //g_assert_not_reached ();
1036 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1037 * OP_LOAD_MEMBASE offset(basereg), reg
1039 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1040 * OP_ICONST reg, imm
1042 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1043 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1044 ins->inst_basereg == last_ins->inst_destbasereg &&
1045 ins->inst_offset == last_ins->inst_offset) {
1046 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1047 ins->opcode = OP_ICONST;
1048 ins->inst_c0 = last_ins->inst_imm;
1049 g_assert_not_reached (); // check this rule
1053 case OP_LOADU1_MEMBASE:
1054 case OP_LOADI1_MEMBASE:
1056 * FIXME: Missing explanation
1058 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1059 ins->inst_basereg == last_ins->inst_destbasereg &&
1060 ins->inst_offset == last_ins->inst_offset) {
1061 if (ins->dreg == last_ins->sreg1) {
1062 last_ins->next = ins->next;
1066 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1067 ins->opcode = OP_MOVE;
1068 ins->sreg1 = last_ins->sreg1;
1072 case OP_LOADU2_MEMBASE:
1073 case OP_LOADI2_MEMBASE:
1075 * FIXME: Missing explanation
1077 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1078 ins->inst_basereg == last_ins->inst_destbasereg &&
1079 ins->inst_offset == last_ins->inst_offset) {
1080 if (ins->dreg == last_ins->sreg1) {
1081 last_ins->next = ins->next;
1085 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1086 ins->opcode = OP_MOVE;
1087 ins->sreg1 = last_ins->sreg1;
1097 if (ins->dreg == ins->sreg1) {
1099 last_ins->next = ins->next;
1104 * OP_MOVE sreg, dreg
1105 * OP_MOVE dreg, sreg
1107 if (last_ins && last_ins->opcode == OP_MOVE &&
1108 ins->sreg1 == last_ins->dreg &&
1109 ins->dreg == last_ins->sreg1) {
1110 last_ins->next = ins->next;
1119 bb->last_ins = last_ins;
1123 branch_cc_table [] = {
1124 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1125 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1126 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1129 #define DEBUG(a) if (cfg->verbose_level > 1) a
1131 #define reg_is_freeable(r) ((r) >= 0 && (r) <= 7 && X86_IS_CALLEE ((r)))
1140 static const char*const * ins_spec = pentium_desc;
1143 print_ins (int i, MonoInst *ins)
1145 const char *spec = ins_spec [ins->opcode];
1146 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1147 if (spec [MONO_INST_DEST]) {
1148 if (ins->dreg >= MONO_MAX_IREGS)
1149 g_print (" R%d <-", ins->dreg);
1151 g_print (" %s <-", mono_arch_regname (ins->dreg));
1153 if (spec [MONO_INST_SRC1]) {
1154 if (ins->sreg1 >= MONO_MAX_IREGS)
1155 g_print (" R%d", ins->sreg1);
1157 g_print (" %s", mono_arch_regname (ins->sreg1));
1159 if (spec [MONO_INST_SRC2]) {
1160 if (ins->sreg2 >= MONO_MAX_IREGS)
1161 g_print (" R%d", ins->sreg2);
1163 g_print (" %s", mono_arch_regname (ins->sreg2));
1165 if (spec [MONO_INST_CLOB])
1166 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1171 print_regtrack (RegTrack *t, int num)
1177 for (i = 0; i < num; ++i) {
1180 if (i >= MONO_MAX_IREGS) {
1181 g_snprintf (buf, sizeof(buf), "R%d", i);
1184 r = mono_arch_regname (i);
1185 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1189 typedef struct InstList InstList;
1197 static inline InstList*
1198 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1200 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1210 * Force the spilling of the variable in the symbolic register 'reg'.
1213 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1218 sel = cfg->rs->iassign [reg];
1219 /*i = cfg->rs->isymbolic [sel];
1220 g_assert (i == reg);*/
1222 spill = ++cfg->spill_count;
1223 cfg->rs->iassign [i] = -spill - 1;
1224 mono_regstate_free_int (cfg->rs, sel);
1225 /* we need to create a spill var and insert a load to sel after the current instruction */
1226 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1228 load->inst_basereg = X86_EBP;
1229 load->inst_offset = mono_spillvar_offset (cfg, spill);
1231 while (ins->next != item->prev->data)
1234 load->next = ins->next;
1236 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1237 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1238 g_assert (i == sel);
1244 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1249 DEBUG (g_print ("start regmask to assign R%d: 0x%08x (R%d <- R%d R%d)\n", reg, regmask, ins->dreg, ins->sreg1, ins->sreg2));
1250 /* exclude the registers in the current instruction */
1251 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1252 if (ins->sreg1 >= MONO_MAX_IREGS)
1253 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1255 regmask &= ~ (1 << ins->sreg1);
1256 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1258 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1259 if (ins->sreg2 >= MONO_MAX_IREGS)
1260 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1262 regmask &= ~ (1 << ins->sreg2);
1263 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1265 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1266 regmask &= ~ (1 << ins->dreg);
1267 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1270 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1271 g_assert (regmask); /* need at least a register we can free */
1273 /* we should track prev_use and spill the register that's farther */
1274 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1275 if (regmask & (1 << i)) {
1277 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1281 i = cfg->rs->isymbolic [sel];
1282 spill = ++cfg->spill_count;
1283 cfg->rs->iassign [i] = -spill - 1;
1284 mono_regstate_free_int (cfg->rs, sel);
1285 /* we need to create a spill var and insert a load to sel after the current instruction */
1286 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1288 load->inst_basereg = X86_EBP;
1289 load->inst_offset = mono_spillvar_offset (cfg, spill);
1291 while (ins->next != item->prev->data)
1294 load->next = ins->next;
1296 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1297 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1298 g_assert (i == sel);
1304 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1307 MONO_INST_NEW (cfg, copy, OP_MOVE);
1311 copy->next = ins->next;
1314 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1319 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1322 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1324 store->inst_destbasereg = X86_EBP;
1325 store->inst_offset = mono_spillvar_offset (cfg, spill);
1327 store->next = ins->next;
1330 DEBUG (g_print ("SPILLED STORE (%d at 0x%08x(%%ebp)) R%d (from %s)\n", spill, store->inst_offset, prev_reg, mono_arch_regname (reg)));
1335 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1339 prev = item->next->data;
1341 while (prev->next != ins)
1343 to_insert->next = ins;
1344 prev->next = to_insert;
1346 to_insert->next = ins;
1349 * needed otherwise in the next instruction we can add an ins to the
1350 * end and that would get past this instruction.
1352 item->data = to_insert;
1357 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1359 int val = cfg->rs->iassign [sym_reg];
1363 /* the register gets spilled after this inst */
1366 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1368 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1369 cfg->rs->iassign [sym_reg] = val;
1370 /* add option to store before the instruction for src registers */
1372 create_spilled_store (cfg, spill, val, sym_reg, ins);
1374 cfg->rs->isymbolic [val] = sym_reg;
1379 /*#include "cprop.c"*/
1382 * Local register allocation.
1383 * We first scan the list of instructions and we save the liveness info of
1384 * each register (when the register is first used, when it's value is set etc.).
1385 * We also reverse the list of instructions (in the InstList list) because assigning
1386 * registers backwards allows for more tricks to be used.
1389 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1392 MonoRegState *rs = cfg->rs;
1393 int i, val, fpcount;
1394 RegTrack *reginfo, *reginfof;
1395 RegTrack *reginfo1, *reginfo2, *reginfod;
1396 InstList *tmp, *reversed = NULL;
1398 guint32 src1_mask, src2_mask, dest_mask;
1402 rs->next_vireg = bb->max_ireg;
1403 rs->next_vfreg = bb->max_freg;
1404 mono_regstate_assign (rs);
1405 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1406 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1407 rs->ifree_mask = X86_CALLEE_REGS;
1411 /*if (cfg->opt & MONO_OPT_COPYPROP)
1412 local_copy_prop (cfg, ins);*/
1415 fpcount = 0; /* FIXME: track fp stack utilization */
1416 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1417 /* forward pass on the instructions to collect register liveness info */
1419 spec = ins_spec [ins->opcode];
1420 DEBUG (print_ins (i, ins));
1421 if (spec [MONO_INST_SRC1]) {
1422 if (spec [MONO_INST_SRC1] == 'f')
1423 reginfo1 = reginfof;
1426 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1427 reginfo1 [ins->sreg1].last_use = i;
1431 if (spec [MONO_INST_SRC2]) {
1432 if (spec [MONO_INST_SRC2] == 'f')
1433 reginfo2 = reginfof;
1436 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1437 reginfo2 [ins->sreg2].last_use = i;
1441 if (spec [MONO_INST_DEST]) {
1442 if (spec [MONO_INST_DEST] == 'f')
1443 reginfod = reginfof;
1446 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1447 reginfod [ins->dreg].killed_in = i;
1448 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1449 reginfod [ins->dreg].last_use = i;
1450 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1451 reginfod [ins->dreg].born_in = i;
1452 if (spec [MONO_INST_DEST] == 'l') {
1453 /* result in eax:edx, the virtual register is allocated sequentially */
1454 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1455 reginfod [ins->dreg + 1].last_use = i;
1456 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1457 reginfod [ins->dreg + 1].born_in = i;
1462 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1467 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1468 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1471 int prev_dreg, prev_sreg1, prev_sreg2;
1472 dest_mask = src1_mask = src2_mask = X86_CALLEE_REGS;
1475 spec = ins_spec [ins->opcode];
1476 DEBUG (g_print ("processing:"));
1477 DEBUG (print_ins (i, ins));
1478 if (spec [MONO_INST_CLOB] == 's') {
1479 if (rs->ifree_mask & (1 << X86_ECX)) {
1480 DEBUG (g_print ("\tshortcut assignment of R%d to ECX\n", ins->sreg2));
1481 rs->iassign [ins->sreg2] = X86_ECX;
1482 rs->isymbolic [X86_ECX] = ins->sreg2;
1483 ins->sreg2 = X86_ECX;
1484 rs->ifree_mask &= ~ (1 << X86_ECX);
1486 int need_ecx_spill = TRUE;
1488 * we first check if src1/dreg is already assigned a register
1489 * and then we force a spill of the var assigned to ECX.
1491 /* the destination register can't be ECX */
1492 dest_mask &= ~ (1 << X86_ECX);
1493 src1_mask &= ~ (1 << X86_ECX);
1494 val = rs->iassign [ins->dreg];
1496 * the destination register is already assigned to ECX:
1497 * we need to allocate another register for it and then
1498 * copy from this to ECX.
1500 if (val == X86_ECX && ins->dreg != ins->sreg2) {
1501 int new_dest = mono_regstate_alloc_int (rs, dest_mask);
1503 new_dest = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1504 g_assert (new_dest >= 0);
1505 ins->dreg = new_dest;
1506 create_copy_ins (cfg, X86_ECX, new_dest, ins);
1507 need_ecx_spill = FALSE;
1508 /*DEBUG (g_print ("\tforced spill of R%d\n", ins->dreg));
1509 val = get_register_force_spilling (cfg, tmp, ins, ins->dreg);
1510 rs->iassign [ins->dreg] = val;
1511 rs->isymbolic [val] = prev_dreg;
1514 val = rs->iassign [ins->sreg1];
1515 if (val == X86_ECX) {
1516 g_assert_not_reached ();
1517 } else if (val >= 0) {
1519 * the first src reg was already assigned to a register,
1520 * we need to copy it to the dest register because the
1521 * shift instruction clobbers the first operand.
1523 MonoInst *copy = create_copy_ins (cfg, ins->dreg, val, NULL);
1524 insert_before_ins (ins, tmp, copy);
1526 val = rs->iassign [ins->sreg2];
1527 if (val >= 0 && val != X86_ECX) {
1528 MonoInst *move = create_copy_ins (cfg, X86_ECX, val, NULL);
1529 DEBUG (g_print ("\tmoved arg from R%d (%d) to ECX\n", val, ins->sreg2));
1531 g_assert_not_reached ();
1532 /* FIXME: where is move connected to the instruction list? */
1533 //tmp->prev->data->next = move;
1535 if (need_ecx_spill && !(rs->ifree_mask & (1 << X86_ECX))) {
1536 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_ECX]));
1537 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_ECX]);
1538 mono_regstate_free_int (rs, X86_ECX);
1540 /* force-set sreg2 */
1541 rs->iassign [ins->sreg2] = X86_ECX;
1542 rs->isymbolic [X86_ECX] = ins->sreg2;
1543 ins->sreg2 = X86_ECX;
1544 rs->ifree_mask &= ~ (1 << X86_ECX);
1546 } else if (spec [MONO_INST_CLOB] == 'd') { /* division */
1547 int dest_reg = X86_EAX;
1548 int clob_reg = X86_EDX;
1549 if (spec [MONO_INST_DEST] == 'd') {
1550 dest_reg = X86_EDX; /* reminder */
1553 val = rs->iassign [ins->dreg];
1554 if (0 && val >= 0 && val != dest_reg && !(rs->ifree_mask & (1 << dest_reg))) {
1555 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1556 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1557 mono_regstate_free_int (rs, dest_reg);
1561 /* the register gets spilled after this inst */
1562 int spill = -val -1;
1563 dest_mask = 1 << clob_reg;
1564 prev_dreg = ins->dreg;
1565 val = mono_regstate_alloc_int (rs, dest_mask);
1567 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1568 rs->iassign [ins->dreg] = val;
1570 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1571 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1572 rs->isymbolic [val] = prev_dreg;
1574 if (val != dest_reg) { /* force a copy */
1575 create_copy_ins (cfg, val, dest_reg, ins);
1578 DEBUG (g_print ("\tshortcut assignment of R%d to %s\n", ins->dreg, mono_arch_regname (dest_reg)));
1579 rs->iassign [ins->dreg] = dest_reg;
1580 rs->isymbolic [dest_reg] = ins->dreg;
1581 ins->dreg = dest_reg;
1582 rs->ifree_mask &= ~ (1 << dest_reg);
1585 //DEBUG (g_print ("dest reg in div assigned: %s\n", mono_arch_regname (val)));
1586 if (val != dest_reg) { /* force a copy */
1587 create_copy_ins (cfg, val, dest_reg, ins);
1588 if (!(rs->ifree_mask & (1 << dest_reg)) && rs->isymbolic [dest_reg] >= MONO_MAX_IREGS) {
1589 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1590 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1591 mono_regstate_free_int (rs, dest_reg);
1595 src1_mask = 1 << X86_EAX;
1596 src2_mask = 1 << X86_ECX;
1598 if (spec [MONO_INST_DEST] == 'l') {
1599 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1600 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1601 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1602 mono_regstate_free_int (rs, X86_EAX);
1604 if (!(rs->ifree_mask & (1 << X86_EDX))) {
1605 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EDX]));
1606 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EDX]);
1607 mono_regstate_free_int (rs, X86_EDX);
1610 /* update for use with FP regs... */
1611 if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
1612 val = rs->iassign [ins->dreg];
1613 prev_dreg = ins->dreg;
1617 /* the register gets spilled after this inst */
1620 val = mono_regstate_alloc_int (rs, dest_mask);
1622 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1623 rs->iassign [ins->dreg] = val;
1625 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1627 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1628 rs->isymbolic [val] = prev_dreg;
1630 if (spec [MONO_INST_DEST] == 'l') {
1631 int hreg = prev_dreg + 1;
1632 val = rs->iassign [hreg];
1636 /* the register gets spilled after this inst */
1639 val = mono_regstate_alloc_int (rs, dest_mask);
1641 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1642 rs->iassign [hreg] = val;
1644 create_spilled_store (cfg, spill, val, hreg, ins);
1646 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1647 rs->isymbolic [val] = hreg;
1648 /* FIXME:? ins->dreg = val; */
1649 if (ins->dreg == X86_EAX) {
1651 create_copy_ins (cfg, val, X86_EDX, ins);
1652 } else if (ins->dreg == X86_EDX) {
1653 if (val == X86_EAX) {
1655 g_assert_not_reached ();
1657 /* two forced copies */
1658 create_copy_ins (cfg, val, X86_EDX, ins);
1659 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1662 if (val == X86_EDX) {
1663 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1665 /* two forced copies */
1666 create_copy_ins (cfg, val, X86_EDX, ins);
1667 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1670 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1671 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1672 mono_regstate_free_int (rs, val);
1674 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != X86_EAX && spec [MONO_INST_CLOB] != 'd') {
1675 /* this instruction only outputs to EAX, need to copy */
1676 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1677 } else if (spec [MONO_INST_DEST] == 'd' && ins->dreg != X86_EDX && spec [MONO_INST_CLOB] != 'd') {
1678 create_copy_ins (cfg, ins->dreg, X86_EDX, ins);
1683 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1684 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1685 mono_regstate_free_int (rs, ins->dreg);
1687 /* put src1 in EAX if it needs to be */
1688 if (spec [MONO_INST_SRC1] == 'a') {
1689 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1690 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1691 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1692 mono_regstate_free_int (rs, X86_EAX);
1694 /* force-set sreg1 */
1695 rs->iassign [ins->sreg1] = X86_EAX;
1696 rs->isymbolic [X86_EAX] = ins->sreg1;
1697 ins->sreg1 = X86_EAX;
1698 rs->ifree_mask &= ~ (1 << X86_EAX);
1700 if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
1701 val = rs->iassign [ins->sreg1];
1702 prev_sreg1 = ins->sreg1;
1706 /* the register gets spilled after this inst */
1709 if (0 && ins->opcode == OP_MOVE) {
1711 * small optimization: the dest register is already allocated
1712 * but the src one is not: we can simply assign the same register
1713 * here and peephole will get rid of the instruction later.
1714 * This optimization may interfere with the clobbering handling:
1715 * it removes a mov operation that will be added again to handle clobbering.
1716 * There are also some other issues that should with make testjit.
1718 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1719 val = rs->iassign [ins->sreg1] = ins->dreg;
1720 //g_assert (val >= 0);
1721 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1723 //g_assert (val == -1); /* source cannot be spilled */
1724 val = mono_regstate_alloc_int (rs, src1_mask);
1726 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1727 rs->iassign [ins->sreg1] = val;
1728 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1731 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1732 insert_before_ins (ins, tmp, store);
1735 rs->isymbolic [val] = prev_sreg1;
1740 /* handle clobbering of sreg1 */
1741 if ((spec [MONO_INST_CLOB] == '1' || spec [MONO_INST_CLOB] == 's') && ins->dreg != ins->sreg1) {
1742 MonoInst *copy = create_copy_ins (cfg, ins->dreg, ins->sreg1, NULL);
1743 DEBUG (g_print ("\tneed to copy sreg1 %s to dreg %s\n", mono_arch_regname (ins->sreg1), mono_arch_regname (ins->dreg)));
1744 if (ins->sreg2 == -1 || spec [MONO_INST_CLOB] == 's') {
1745 /* note: the copy is inserted before the current instruction! */
1746 insert_before_ins (ins, tmp, copy);
1747 /* we set sreg1 to dest as well */
1748 prev_sreg1 = ins->sreg1 = ins->dreg;
1750 /* inserted after the operation */
1751 copy->next = ins->next;
1755 if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
1756 val = rs->iassign [ins->sreg2];
1757 prev_sreg2 = ins->sreg2;
1761 /* the register gets spilled after this inst */
1764 val = mono_regstate_alloc_int (rs, src2_mask);
1766 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1767 rs->iassign [ins->sreg2] = val;
1768 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1770 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1772 rs->isymbolic [val] = prev_sreg2;
1774 if (spec [MONO_INST_CLOB] == 's' && ins->sreg2 != X86_ECX) {
1775 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]));
1781 if (spec [MONO_INST_CLOB] == 'c') {
1783 guint32 clob_mask = X86_CALLEE_REGS;
1784 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1786 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1787 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1791 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1792 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1793 mono_regstate_free_int (rs, ins->sreg1);
1795 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1796 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1797 mono_regstate_free_int (rs, ins->sreg2);
1800 //DEBUG (print_ins (i, ins));
1801 /* this may result from a insert_before call */
1803 bb->code = tmp->data;
1808 static unsigned char*
1809 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1811 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1812 x86_fnstcw_membase(code, X86_ESP, 0);
1813 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1814 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1815 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1816 x86_fldcw_membase (code, X86_ESP, 2);
1818 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1819 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1820 x86_pop_reg (code, dreg);
1821 /* FIXME: need the high register
1822 * x86_pop_reg (code, dreg_high);
1825 x86_push_reg (code, X86_EAX); // SP = SP - 4
1826 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1827 x86_pop_reg (code, dreg);
1829 x86_fldcw_membase (code, X86_ESP, 0);
1830 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1833 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1835 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1839 static unsigned char*
1840 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1842 int sreg = tree->sreg1;
1843 #ifdef PLATFORM_WIN32
1848 * If requested stack size is larger than one page,
1849 * perform stack-touch operation
1852 * Generate stack probe code.
1853 * Under Windows, it is necessary to allocate one page at a time,
1854 * "touching" stack after each successful sub-allocation. This is
1855 * because of the way stack growth is implemented - there is a
1856 * guard page before the lowest stack page that is currently commited.
1857 * Stack normally grows sequentially so OS traps access to the
1858 * guard page and commits more pages when needed.
1860 x86_test_reg_imm (code, sreg, ~0xFFF);
1861 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1863 br[2] = code; /* loop */
1864 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
1865 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
1866 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
1867 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
1868 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
1869 x86_patch (br[3], br[2]);
1870 x86_test_reg_reg (code, sreg, sreg);
1871 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1872 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1874 br[1] = code; x86_jump8 (code, 0);
1876 x86_patch (br[0], code);
1877 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1878 x86_patch (br[1], code);
1879 x86_patch (br[4], code);
1880 #else /* PLATFORM_WIN32 */
1881 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1883 if (tree->flags & MONO_INST_INIT) {
1885 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1886 x86_push_reg (code, X86_EAX);
1889 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1890 x86_push_reg (code, X86_ECX);
1893 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1894 x86_push_reg (code, X86_EDI);
1898 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1899 if (sreg != X86_ECX)
1900 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1901 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1903 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1905 x86_prefix (code, X86_REP_PREFIX);
1908 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1909 x86_pop_reg (code, X86_EDI);
1910 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1911 x86_pop_reg (code, X86_ECX);
1912 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1913 x86_pop_reg (code, X86_EAX);
1918 #define REAL_PRINT_REG(text,reg) \
1919 mono_assert (reg >= 0); \
1920 x86_push_reg (code, X86_EAX); \
1921 x86_push_reg (code, X86_EDX); \
1922 x86_push_reg (code, X86_ECX); \
1923 x86_push_reg (code, reg); \
1924 x86_push_imm (code, reg); \
1925 x86_push_imm (code, text " %d %p\n"); \
1926 x86_mov_reg_imm (code, X86_EAX, printf); \
1927 x86_call_reg (code, X86_EAX); \
1928 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
1929 x86_pop_reg (code, X86_ECX); \
1930 x86_pop_reg (code, X86_EDX); \
1931 x86_pop_reg (code, X86_EAX);
1934 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1939 guint8 *code = cfg->native_code + cfg->code_len;
1940 MonoInst *last_ins = NULL;
1941 guint last_offset = 0;
1944 if (cfg->opt & MONO_OPT_PEEPHOLE)
1945 peephole_pass (cfg, bb);
1949 * various stratgies to align BBs. Using real loop detection or simply
1950 * aligning every block leads to more consistent benchmark results,
1951 * but usually slows down the code
1952 * we should do the alignment outside this function or we should adjust
1953 * bb->native offset as well or the code is effectively slowed down!
1955 /* align all blocks */
1956 // if ((pad = (cfg->code_len & (align - 1)))) {
1957 /* poor man loop start detection */
1958 // if (bb->code && bb->in_count && bb->in_bb [0]->cil_code > bb->cil_code && (pad = (cfg->code_len & (align - 1)))) {
1959 /* consider real loop detection and nesting level */
1960 // if (bb->loop_blocks && bb->nesting < 3 && (pad = (cfg->code_len & (align - 1)))) {
1961 /* consider real loop detection */
1962 if (bb->loop_blocks && (pad = (cfg->code_len & (align - 1)))) {
1964 x86_padding (code, pad);
1965 cfg->code_len += pad;
1966 bb->native_offset = cfg->code_len;
1970 if (cfg->verbose_level > 2)
1971 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1973 cpos = bb->max_offset;
1975 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1976 MonoProfileCoverageInfo *cov = cfg->coverage_info;
1977 g_assert (!mono_compile_aot);
1980 cov->data [bb->dfn].cil_code = bb->cil_code;
1981 /* this is not thread save, but good enough */
1982 x86_inc_mem (code, &cov->data [bb->dfn].count);
1985 offset = code - cfg->native_code;
1989 offset = code - cfg->native_code;
1991 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1993 if (offset > (cfg->code_size - max_len - 16)) {
1994 cfg->code_size *= 2;
1995 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1996 code = cfg->native_code + offset;
1997 mono_jit_stats.code_reallocs++;
2000 mono_debug_record_line_number (cfg, ins, offset);
2002 switch (ins->opcode) {
2004 x86_mul_reg (code, ins->sreg2, TRUE);
2007 x86_mul_reg (code, ins->sreg2, FALSE);
2009 case OP_X86_SETEQ_MEMBASE:
2010 x86_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
2012 case OP_STOREI1_MEMBASE_IMM:
2013 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2015 case OP_STOREI2_MEMBASE_IMM:
2016 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2018 case OP_STORE_MEMBASE_IMM:
2019 case OP_STOREI4_MEMBASE_IMM:
2020 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2022 case OP_STOREI1_MEMBASE_REG:
2023 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2025 case OP_STOREI2_MEMBASE_REG:
2026 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2028 case OP_STORE_MEMBASE_REG:
2029 case OP_STOREI4_MEMBASE_REG:
2030 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2035 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2038 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2039 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2041 case OP_LOAD_MEMBASE:
2042 case OP_LOADI4_MEMBASE:
2043 case OP_LOADU4_MEMBASE:
2044 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2046 case OP_LOADU1_MEMBASE:
2047 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2049 case OP_LOADI1_MEMBASE:
2050 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2052 case OP_LOADU2_MEMBASE:
2053 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2055 case OP_LOADI2_MEMBASE:
2056 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2059 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2062 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2065 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2068 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2071 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2073 case OP_COMPARE_IMM:
2074 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2076 case OP_X86_COMPARE_MEMBASE_REG:
2077 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2079 case OP_X86_COMPARE_MEMBASE_IMM:
2080 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2082 case OP_X86_COMPARE_REG_MEMBASE:
2083 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2085 case OP_X86_TEST_NULL:
2086 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2088 case OP_X86_ADD_MEMBASE_IMM:
2089 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2091 case OP_X86_SUB_MEMBASE_IMM:
2092 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2094 case OP_X86_INC_MEMBASE:
2095 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2097 case OP_X86_INC_REG:
2098 x86_inc_reg (code, ins->dreg);
2100 case OP_X86_DEC_MEMBASE:
2101 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2103 case OP_X86_DEC_REG:
2104 x86_dec_reg (code, ins->dreg);
2107 x86_breakpoint (code);
2111 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2114 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2117 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2120 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2124 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2127 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2130 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2133 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2136 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2139 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2143 x86_div_reg (code, ins->sreg2, TRUE);
2146 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2147 x86_div_reg (code, ins->sreg2, FALSE);
2150 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2152 x86_div_reg (code, ins->sreg2, TRUE);
2156 x86_div_reg (code, ins->sreg2, TRUE);
2159 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2160 x86_div_reg (code, ins->sreg2, FALSE);
2163 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2165 x86_div_reg (code, ins->sreg2, TRUE);
2168 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2171 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2174 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2177 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2180 g_assert (ins->sreg2 == X86_ECX);
2181 x86_shift_reg (code, X86_SHL, ins->dreg);
2184 g_assert (ins->sreg2 == X86_ECX);
2185 x86_shift_reg (code, X86_SAR, ins->dreg);
2188 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2191 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2194 g_assert (ins->sreg2 == X86_ECX);
2195 x86_shift_reg (code, X86_SHR, ins->dreg);
2198 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2201 x86_not_reg (code, ins->sreg1);
2204 x86_neg_reg (code, ins->sreg1);
2207 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2210 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2213 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2216 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2219 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2220 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2222 case CEE_MUL_OVF_UN: {
2223 /* the mul operation and the exception check should most likely be split */
2224 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2225 /*g_assert (ins->sreg2 == X86_EAX);
2226 g_assert (ins->dreg == X86_EAX);*/
2227 if (ins->sreg2 == X86_EAX) {
2228 non_eax_reg = ins->sreg1;
2229 } else if (ins->sreg1 == X86_EAX) {
2230 non_eax_reg = ins->sreg2;
2232 /* no need to save since we're going to store to it anyway */
2233 if (ins->dreg != X86_EAX) {
2235 x86_push_reg (code, X86_EAX);
2237 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2238 non_eax_reg = ins->sreg2;
2240 if (ins->dreg == X86_EDX) {
2243 x86_push_reg (code, X86_EAX);
2245 } else if (ins->dreg != X86_EAX) {
2247 x86_push_reg (code, X86_EDX);
2249 x86_mul_reg (code, non_eax_reg, FALSE);
2250 /* save before the check since pop and mov don't change the flags */
2252 x86_pop_reg (code, X86_EDX);
2254 x86_pop_reg (code, X86_EAX);
2255 if (ins->dreg != X86_EAX)
2256 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2257 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2261 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2264 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2265 x86_mov_reg_imm (code, ins->dreg, 0);
2270 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2274 * Note: this 'frame destruction' logic is useful for tail calls, too.
2275 * Keep in sync with the code in emit_epilog.
2279 /* FIXME: no tracing support... */
2280 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2281 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2282 /* reset offset to make max_len work */
2283 offset = code - cfg->native_code;
2285 g_assert (!cfg->method->save_lmf);
2287 if (cfg->used_int_regs & (1 << X86_EBX))
2289 if (cfg->used_int_regs & (1 << X86_EDI))
2291 if (cfg->used_int_regs & (1 << X86_ESI))
2294 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2296 if (cfg->used_int_regs & (1 << X86_ESI))
2297 x86_pop_reg (code, X86_ESI);
2298 if (cfg->used_int_regs & (1 << X86_EDI))
2299 x86_pop_reg (code, X86_EDI);
2300 if (cfg->used_int_regs & (1 << X86_EBX))
2301 x86_pop_reg (code, X86_EBX);
2303 /* restore ESP/EBP */
2305 offset = code - cfg->native_code;
2306 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2307 x86_jump32 (code, 0);
2311 /* ensure ins->sreg1 is not NULL */
2312 x86_alu_membase_imm (code, X86_CMP, ins->sreg1, 0, 0);
2315 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2316 x86_push_reg (code, hreg);
2317 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2318 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2319 x86_pop_reg (code, hreg);
2327 call = (MonoCallInst*)ins;
2328 if (ins->flags & MONO_INST_HAS_METHOD)
2329 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2331 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2333 x86_call_code (code, 0);
2334 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2335 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2340 case OP_VOIDCALL_REG:
2342 call = (MonoCallInst*)ins;
2343 x86_call_reg (code, ins->sreg1);
2344 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2345 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2347 case OP_FCALL_MEMBASE:
2348 case OP_LCALL_MEMBASE:
2349 case OP_VCALL_MEMBASE:
2350 case OP_VOIDCALL_MEMBASE:
2351 case OP_CALL_MEMBASE:
2352 call = (MonoCallInst*)ins;
2353 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2354 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2355 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2359 x86_push_reg (code, ins->sreg1);
2361 case OP_X86_PUSH_IMM:
2362 x86_push_imm (code, ins->inst_imm);
2364 case OP_X86_PUSH_MEMBASE:
2365 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2367 case OP_X86_PUSH_OBJ:
2368 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2369 x86_push_reg (code, X86_EDI);
2370 x86_push_reg (code, X86_ESI);
2371 x86_push_reg (code, X86_ECX);
2372 if (ins->inst_offset)
2373 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2375 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2376 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2377 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2379 x86_prefix (code, X86_REP_PREFIX);
2381 x86_pop_reg (code, X86_ECX);
2382 x86_pop_reg (code, X86_ESI);
2383 x86_pop_reg (code, X86_EDI);
2386 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
2388 case OP_X86_LEA_MEMBASE:
2389 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
2392 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2395 /* keep alignment */
2396 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
2397 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
2398 code = mono_emit_stack_alloc (code, ins);
2399 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2405 x86_push_reg (code, ins->sreg1);
2406 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2407 (gpointer)"mono_arch_throw_exception");
2408 x86_call_code (code, 0);
2411 case OP_CALL_HANDLER:
2412 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2413 x86_call_imm (code, 0);
2416 ins->inst_c0 = code - cfg->native_code;
2419 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2420 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2422 if (ins->flags & MONO_INST_BRLABEL) {
2423 if (ins->inst_i0->inst_c0) {
2424 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2426 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2427 x86_jump32 (code, 0);
2430 if (ins->inst_target_bb->native_offset) {
2431 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2433 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2434 if ((cfg->opt & MONO_OPT_BRANCH) &&
2435 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
2436 x86_jump8 (code, 0);
2438 x86_jump32 (code, 0);
2443 x86_jump_reg (code, ins->sreg1);
2446 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2447 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2450 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
2451 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2454 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2455 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2458 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
2459 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2462 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2463 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2465 case OP_COND_EXC_EQ:
2466 case OP_COND_EXC_NE_UN:
2467 case OP_COND_EXC_LT:
2468 case OP_COND_EXC_LT_UN:
2469 case OP_COND_EXC_GT:
2470 case OP_COND_EXC_GT_UN:
2471 case OP_COND_EXC_GE:
2472 case OP_COND_EXC_GE_UN:
2473 case OP_COND_EXC_LE:
2474 case OP_COND_EXC_LE_UN:
2475 case OP_COND_EXC_OV:
2476 case OP_COND_EXC_NO:
2478 case OP_COND_EXC_NC:
2479 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2480 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2492 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
2495 /* floating point opcodes */
2497 double d = *(double *)ins->inst_p0;
2499 if ((d == 0.0) && (signbit (d) == 0)) {
2501 } else if (d == 1.0) {
2504 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
2505 x86_fld (code, NULL, TRUE);
2510 float f = *(float *)ins->inst_p0;
2512 if ((f == 0.0) && (signbit (f) == 0)) {
2514 } else if (f == 1.0) {
2517 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
2518 x86_fld (code, NULL, FALSE);
2522 case OP_STORER8_MEMBASE_REG:
2523 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
2525 case OP_LOADR8_MEMBASE:
2526 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2528 case OP_STORER4_MEMBASE_REG:
2529 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
2531 case OP_LOADR4_MEMBASE:
2532 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2534 case CEE_CONV_R4: /* FIXME: change precision */
2536 x86_push_reg (code, ins->sreg1);
2537 x86_fild_membase (code, X86_ESP, 0, FALSE);
2538 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2540 case OP_X86_FP_LOAD_I8:
2541 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2543 case OP_X86_FP_LOAD_I4:
2544 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2546 case OP_FCONV_TO_I1:
2547 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2549 case OP_FCONV_TO_U1:
2550 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2552 case OP_FCONV_TO_I2:
2553 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2555 case OP_FCONV_TO_U2:
2556 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2558 case OP_FCONV_TO_I4:
2560 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2562 case OP_FCONV_TO_I8:
2563 /* we defined this instruction to output only to eax:edx */
2564 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2565 x86_fnstcw_membase(code, X86_ESP, 0);
2566 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 2);
2567 x86_alu_reg_imm (code, X86_OR, X86_EAX, 0xc00);
2568 x86_mov_membase_reg (code, X86_ESP, 2, X86_EAX, 2);
2569 x86_fldcw_membase (code, X86_ESP, 2);
2570 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2571 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2572 x86_pop_reg (code, X86_EAX);
2573 x86_pop_reg (code, X86_EDX);
2574 x86_fldcw_membase (code, X86_ESP, 0);
2575 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2577 case OP_LCONV_TO_R_UN: {
2578 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2581 /* load 64bit integer to FP stack */
2582 x86_push_imm (code, 0);
2583 x86_push_reg (code, ins->sreg2);
2584 x86_push_reg (code, ins->sreg1);
2585 x86_fild_membase (code, X86_ESP, 0, TRUE);
2586 /* store as 80bit FP value */
2587 x86_fst80_membase (code, X86_ESP, 0);
2589 /* test if lreg is negative */
2590 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2591 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2593 /* add correction constant mn */
2594 x86_fld80_mem (code, mn);
2595 x86_fld80_membase (code, X86_ESP, 0);
2596 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2597 x86_fst80_membase (code, X86_ESP, 0);
2599 x86_patch (br, code);
2601 x86_fld80_membase (code, X86_ESP, 0);
2602 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2606 case OP_LCONV_TO_OVF_I: {
2607 guint8 *br [3], *label [1];
2610 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2612 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2614 /* If the low word top bit is set, see if we are negative */
2615 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2616 /* We are not negative (no top bit set, check for our top word to be zero */
2617 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2618 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2621 /* throw exception */
2622 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2623 x86_jump32 (code, 0);
2625 x86_patch (br [0], code);
2626 /* our top bit is set, check that top word is 0xfffffff */
2627 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2629 x86_patch (br [1], code);
2630 /* nope, emit exception */
2631 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2632 x86_patch (br [2], label [0]);
2634 if (ins->dreg != ins->sreg1)
2635 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2639 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2642 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
2645 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
2648 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
2664 * it really doesn't make sense to inline all this code,
2665 * it's here just to show that things may not be as simple
2668 guchar *check_pos, *end_tan, *pop_jump;
2669 x86_push_reg (code, X86_EAX);
2672 x86_test_reg_imm (code, X86_EAX, 0x400);
2674 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2675 x86_fstp (code, 0); /* pop the 1.0 */
2677 x86_jump8 (code, 0);
2679 x86_fp_op (code, X86_FADD, 0);
2683 x86_test_reg_imm (code, X86_EAX, 0x400);
2685 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2688 x86_patch (pop_jump, code);
2689 x86_fstp (code, 0); /* pop the 1.0 */
2690 x86_patch (check_pos, code);
2691 x86_patch (end_tan, code);
2692 x86_pop_reg (code, X86_EAX);
2708 x86_push_reg (code, X86_EAX);
2709 /* we need to exchange ST(0) with ST(1) */
2712 /* this requires a loop, because fprem somtimes
2713 * returns a partial remainder */
2715 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
2716 /* x86_fprem1 (code); */
2719 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x0400);
2721 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
2726 x86_pop_reg (code, X86_EAX);
2730 if (cfg->opt & MONO_OPT_FCMOV) {
2731 x86_fcomip (code, 1);
2735 /* this overwrites EAX */
2736 EMIT_FPCOMPARE(code);
2737 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2740 if (cfg->opt & MONO_OPT_FCMOV) {
2741 /* zeroing the register at the start results in
2742 * shorter and faster code (we can also remove the widening op)
2744 guchar *unordered_check;
2745 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2746 x86_fcomip (code, 1);
2748 unordered_check = code;
2749 x86_branch8 (code, X86_CC_P, 0, FALSE);
2750 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
2751 x86_patch (unordered_check, code);
2754 if (ins->dreg != X86_EAX)
2755 x86_push_reg (code, X86_EAX);
2757 EMIT_FPCOMPARE(code);
2758 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2759 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2760 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2761 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2763 if (ins->dreg != X86_EAX)
2764 x86_pop_reg (code, X86_EAX);
2768 if (cfg->opt & MONO_OPT_FCMOV) {
2769 /* zeroing the register at the start results in
2770 * shorter and faster code (we can also remove the widening op)
2772 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2773 x86_fcomip (code, 1);
2775 if (ins->opcode == OP_FCLT_UN) {
2776 guchar *unordered_check = code;
2777 guchar *jump_to_end;
2778 x86_branch8 (code, X86_CC_P, 0, FALSE);
2779 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2781 x86_jump8 (code, 0);
2782 x86_patch (unordered_check, code);
2783 x86_inc_reg (code, ins->dreg);
2784 x86_patch (jump_to_end, code);
2786 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2790 if (ins->dreg != X86_EAX)
2791 x86_push_reg (code, X86_EAX);
2793 EMIT_FPCOMPARE(code);
2794 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2795 if (ins->opcode == OP_FCLT_UN) {
2796 guchar *is_not_zero_check, *end_jump;
2797 is_not_zero_check = code;
2798 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2800 x86_jump8 (code, 0);
2801 x86_patch (is_not_zero_check, code);
2802 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2804 x86_patch (end_jump, code);
2806 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2807 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2809 if (ins->dreg != X86_EAX)
2810 x86_pop_reg (code, X86_EAX);
2814 if (cfg->opt & MONO_OPT_FCMOV) {
2815 /* zeroing the register at the start results in
2816 * shorter and faster code (we can also remove the widening op)
2818 guchar *unordered_check;
2819 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2820 x86_fcomip (code, 1);
2822 if (ins->opcode == OP_FCGT) {
2823 unordered_check = code;
2824 x86_branch8 (code, X86_CC_P, 0, FALSE);
2825 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2826 x86_patch (unordered_check, code);
2828 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2832 if (ins->dreg != X86_EAX)
2833 x86_push_reg (code, X86_EAX);
2835 EMIT_FPCOMPARE(code);
2836 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2837 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2838 if (ins->opcode == OP_FCGT_UN) {
2839 guchar *is_not_zero_check, *end_jump;
2840 is_not_zero_check = code;
2841 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2843 x86_jump8 (code, 0);
2844 x86_patch (is_not_zero_check, code);
2845 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2847 x86_patch (end_jump, code);
2849 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2850 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2852 if (ins->dreg != X86_EAX)
2853 x86_pop_reg (code, X86_EAX);
2856 if (cfg->opt & MONO_OPT_FCMOV) {
2857 guchar *jump = code;
2858 x86_branch8 (code, X86_CC_P, 0, TRUE);
2859 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2860 x86_patch (jump, code);
2863 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2864 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
2867 if (cfg->opt & MONO_OPT_FCMOV) {
2868 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2869 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2872 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2873 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2876 if (cfg->opt & MONO_OPT_FCMOV) {
2877 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2880 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2883 if (cfg->opt & MONO_OPT_FCMOV) {
2884 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2885 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2888 if (ins->opcode == OP_FBLT_UN) {
2889 guchar *is_not_zero_check, *end_jump;
2890 is_not_zero_check = code;
2891 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2893 x86_jump8 (code, 0);
2894 x86_patch (is_not_zero_check, code);
2895 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2897 x86_patch (end_jump, code);
2899 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2903 if (cfg->opt & MONO_OPT_FCMOV) {
2904 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
2907 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2908 if (ins->opcode == OP_FBGT_UN) {
2909 guchar *is_not_zero_check, *end_jump;
2910 is_not_zero_check = code;
2911 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2913 x86_jump8 (code, 0);
2914 x86_patch (is_not_zero_check, code);
2915 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2917 x86_patch (end_jump, code);
2919 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2923 if (cfg->opt & MONO_OPT_FCMOV) {
2924 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
2927 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2931 if (cfg->opt & MONO_OPT_FCMOV) {
2932 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2933 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
2936 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2937 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2939 case CEE_CKFINITE: {
2940 x86_push_reg (code, X86_EAX);
2943 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2944 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2945 x86_pop_reg (code, X86_EAX);
2946 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
2950 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2951 g_assert_not_reached ();
2954 if ((code - cfg->native_code - offset) > max_len) {
2955 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2956 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2957 g_assert_not_reached ();
2963 last_offset = offset;
2968 cfg->code_len = code - cfg->native_code;
2972 mono_arch_register_lowlevel_calls (void)
2974 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2975 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2979 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji)
2981 MonoJumpInfo *patch_info;
2983 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2984 unsigned char *ip = patch_info->ip.i + code;
2985 const unsigned char *target = NULL;
2987 switch (patch_info->type) {
2988 case MONO_PATCH_INFO_BB:
2989 target = patch_info->data.bb->native_offset + code;
2991 case MONO_PATCH_INFO_ABS:
2992 target = patch_info->data.target;
2994 case MONO_PATCH_INFO_LABEL:
2995 target = patch_info->data.inst->inst_c0 + code;
2997 case MONO_PATCH_INFO_IP:
2998 *((gpointer *)(ip)) = ip;
3000 case MONO_PATCH_INFO_INTERNAL_METHOD: {
3001 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
3003 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
3004 g_assert_not_reached ();
3006 target = mono_icall_get_wrapper (mi);
3009 case MONO_PATCH_INFO_METHOD_JUMP:
3010 /* get the trampoline to the method from the domain */
3011 if (!(target = g_hash_table_lookup (domain->jit_code_hash, patch_info->data.method))) {
3013 target = mono_arch_create_jump_trampoline (patch_info->data.method);
3014 if (!domain->jump_target_hash)
3015 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
3016 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
3017 list = g_slist_prepend (list, ip);
3018 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
3021 case MONO_PATCH_INFO_METHOD:
3022 if (patch_info->data.method == method) {
3025 /* get the trampoline to the method from the domain */
3026 if (!(target = g_hash_table_lookup (domain->jit_code_hash, patch_info->data.method)))
3027 target = mono_arch_create_jit_trampoline (patch_info->data.method);
3030 case MONO_PATCH_INFO_SWITCH: {
3031 gpointer *table = (gpointer *)patch_info->data.target;
3034 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3036 for (i = 0; i < patch_info->table_size; i++) {
3037 table [i] = (int)patch_info->data.table [i] + code;
3039 /* we put into the table the absolute address, no need fo x86_patch in this case */
3042 case MONO_PATCH_INFO_METHODCONST:
3043 case MONO_PATCH_INFO_CLASS:
3044 case MONO_PATCH_INFO_IMAGE:
3045 case MONO_PATCH_INFO_FIELD:
3046 *((gconstpointer *)(ip + 1)) = patch_info->data.target;
3048 case MONO_PATCH_INFO_R4:
3049 case MONO_PATCH_INFO_R8:
3050 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3053 g_assert_not_reached ();
3055 x86_patch (ip, target);
3060 mono_arch_max_epilog_size (MonoCompile *cfg)
3062 int exc_count = 0, max_epilog_size = 16;
3063 MonoJumpInfo *patch_info;
3065 if (cfg->method->save_lmf)
3066 max_epilog_size += 128;
3068 if (mono_jit_trace_calls)
3069 max_epilog_size += 50;
3071 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3072 max_epilog_size += 50;
3074 /* count the number of exception infos */
3076 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3077 if (patch_info->type == MONO_PATCH_INFO_EXC)
3082 * make sure we have enough space for exceptions
3083 * 16 is the size of two push_imm instructions and a call
3085 max_epilog_size += exc_count*16;
3087 return max_epilog_size;
3091 mono_arch_emit_prolog (MonoCompile *cfg)
3093 MonoMethod *method = cfg->method;
3095 MonoMethodSignature *sig;
3097 int alloc_size, pos, max_offset, i;
3100 cfg->code_size = MAX (((MonoMethodNormal *)method)->header->code_size * 4, 256);
3101 code = cfg->native_code = g_malloc (cfg->code_size);
3103 x86_push_reg (code, X86_EBP);
3104 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
3106 alloc_size = - cfg->stack_offset;
3109 if (method->save_lmf) {
3110 pos += sizeof (MonoLMF);
3112 /* save the current IP */
3113 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3114 x86_push_imm (code, 0);
3116 /* save all caller saved regs */
3117 x86_push_reg (code, X86_EBX);
3118 x86_push_reg (code, X86_EDI);
3119 x86_push_reg (code, X86_ESI);
3120 x86_push_reg (code, X86_EBP);
3122 /* save method info */
3123 x86_push_imm (code, method);
3125 /* get the address of lmf for the current thread */
3126 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3127 (gpointer)"mono_get_lmf_addr");
3128 x86_call_code (code, 0);
3131 x86_push_reg (code, X86_EAX);
3132 /* push *lfm (previous_lmf) */
3133 x86_push_membase (code, X86_EAX, 0);
3135 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3138 if (cfg->used_int_regs & (1 << X86_EBX)) {
3139 x86_push_reg (code, X86_EBX);
3143 if (cfg->used_int_regs & (1 << X86_EDI)) {
3144 x86_push_reg (code, X86_EDI);
3148 if (cfg->used_int_regs & (1 << X86_ESI)) {
3149 x86_push_reg (code, X86_ESI);
3157 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
3159 /* compute max_offset in order to use short forward jumps */
3161 if (cfg->opt & MONO_OPT_BRANCH) {
3162 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3163 MonoInst *ins = bb->code;
3164 bb->max_offset = max_offset;
3166 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3170 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3176 if (mono_jit_trace_calls)
3177 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
3179 /* load arguments allocated to register from the stack */
3180 sig = method->signature;
3183 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3184 inst = cfg->varinfo [pos];
3185 if (inst->opcode == OP_REGVAR) {
3186 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
3187 if (cfg->verbose_level > 2)
3188 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3193 cfg->code_len = code - cfg->native_code;
3199 mono_arch_emit_epilog (MonoCompile *cfg)
3201 MonoJumpInfo *patch_info;
3202 MonoMethod *method = cfg->method;
3206 code = cfg->native_code + cfg->code_len;
3208 if (mono_jit_trace_calls)
3209 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
3211 /* the code restoring the registers must be kept in sync with CEE_JMP */
3214 if (method->save_lmf) {
3215 pos = -sizeof (MonoLMF);
3217 if (cfg->used_int_regs & (1 << X86_EBX)) {
3220 if (cfg->used_int_regs & (1 << X86_EDI)) {
3223 if (cfg->used_int_regs & (1 << X86_ESI)) {
3229 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3231 if (method->save_lmf) {
3232 /* ebx = previous_lmf */
3233 x86_pop_reg (code, X86_EBX);
3235 x86_pop_reg (code, X86_EDI);
3236 /* *(lmf) = previous_lmf */
3237 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
3239 /* discard method info */
3240 x86_pop_reg (code, X86_ESI);
3242 /* restore caller saved regs */
3243 x86_pop_reg (code, X86_EBP);
3244 x86_pop_reg (code, X86_ESI);
3245 x86_pop_reg (code, X86_EDI);
3246 x86_pop_reg (code, X86_EBX);
3250 if (cfg->used_int_regs & (1 << X86_ESI)) {
3251 x86_pop_reg (code, X86_ESI);
3253 if (cfg->used_int_regs & (1 << X86_EDI)) {
3254 x86_pop_reg (code, X86_EDI);
3256 if (cfg->used_int_regs & (1 << X86_EBX)) {
3257 x86_pop_reg (code, X86_EBX);
3262 /* FIXME: add another check to support stdcall convention here */
3263 if (MONO_TYPE_ISSTRUCT (cfg->method->signature->ret))
3264 x86_ret_imm (code, 4);
3268 /* add code to raise exceptions */
3269 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3270 switch (patch_info->type) {
3271 case MONO_PATCH_INFO_EXC:
3272 x86_patch (patch_info->ip.i + cfg->native_code, code);
3273 x86_push_imm (code, patch_info->data.target);
3274 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
3275 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3276 patch_info->data.name = "mono_arch_throw_exception_by_name";
3277 patch_info->ip.i = code - cfg->native_code;
3278 x86_jump_code (code, 0);
3286 cfg->code_len = code - cfg->native_code;
3288 g_assert (cfg->code_len < cfg->code_size);
3293 mono_arch_flush_icache (guint8 *code, gint size)