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 * This function returns the optimizations supported on this cpu.
420 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
422 int eax, ebx, ecx, edx;
426 /* Feature Flags function, flags returned in EDX. */
427 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
428 if (edx & (1 << 15)) {
429 opts |= MONO_OPT_CMOV;
431 opts |= MONO_OPT_FCMOV;
433 *exclude_mask |= MONO_OPT_FCMOV;
435 *exclude_mask |= MONO_OPT_CMOV;
441 is_regsize_var (MonoType *t) {
450 case MONO_TYPE_OBJECT:
451 case MONO_TYPE_STRING:
452 case MONO_TYPE_CLASS:
453 case MONO_TYPE_SZARRAY:
454 case MONO_TYPE_ARRAY:
456 case MONO_TYPE_VALUETYPE:
457 if (t->data.klass->enumtype)
458 return is_regsize_var (t->data.klass->enum_basetype);
465 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
470 for (i = 0; i < cfg->num_varinfo; i++) {
471 MonoInst *ins = cfg->varinfo [i];
472 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
475 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
478 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
479 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
482 /* we dont allocate I1 to registers because there is no simply way to sign extend
483 * 8bit quantities in caller saved registers on x86 */
484 if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) ||
485 (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
486 (ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
487 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
488 g_assert (i == vmv->idx);
489 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
497 mono_arch_get_global_int_regs (MonoCompile *cfg)
501 /* we can use 3 registers for global allocation */
502 regs = g_list_prepend (regs, (gpointer)X86_EBX);
503 regs = g_list_prepend (regs, (gpointer)X86_ESI);
504 regs = g_list_prepend (regs, (gpointer)X86_EDI);
510 * Set var information according to the calling convention. X86 version.
511 * The locals var stuff should most likely be split in another method.
514 mono_arch_allocate_vars (MonoCompile *m)
516 MonoMethodSignature *sig;
517 MonoMethodHeader *header;
519 int i, offset, size, align, curinst;
521 header = ((MonoMethodNormal *)m->method)->header;
523 sig = m->method->signature;
527 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
528 m->ret->opcode = OP_REGOFFSET;
529 m->ret->inst_basereg = X86_EBP;
530 m->ret->inst_offset = offset;
531 offset += sizeof (gpointer);
533 /* FIXME: handle long and FP values */
534 switch (sig->ret->type) {
538 m->ret->opcode = OP_REGVAR;
539 m->ret->inst_c0 = X86_EAX;
544 inst = m->varinfo [curinst];
545 if (inst->opcode != OP_REGVAR) {
546 inst->opcode = OP_REGOFFSET;
547 inst->inst_basereg = X86_EBP;
549 inst->inst_offset = offset;
550 offset += sizeof (gpointer);
554 if (sig->call_convention == MONO_CALL_VARARG) {
555 m->sig_cookie = offset;
556 offset += sizeof (gpointer);
559 for (i = 0; i < sig->param_count; ++i) {
560 inst = m->varinfo [curinst];
561 if (inst->opcode != OP_REGVAR) {
562 inst->opcode = OP_REGOFFSET;
563 inst->inst_basereg = X86_EBP;
565 inst->inst_offset = offset;
566 size = mono_type_size (sig->params [i], &align);
575 /* reserve space to save LMF and caller saved registers */
577 if (m->method->save_lmf) {
578 offset += sizeof (MonoLMF);
580 if (m->used_int_regs & (1 << X86_EBX)) {
584 if (m->used_int_regs & (1 << X86_EDI)) {
588 if (m->used_int_regs & (1 << X86_ESI)) {
593 for (i = curinst; i < m->num_varinfo; ++i) {
594 inst = m->varinfo [i];
596 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
599 /* inst->unused indicates native sized value types, this is used by the
600 * pinvoke wrappers when they call functions returning structure */
601 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
602 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
604 size = mono_type_size (inst->inst_vtype, &align);
608 offset &= ~(align - 1);
609 inst->opcode = OP_REGOFFSET;
610 inst->inst_basereg = X86_EBP;
611 inst->inst_offset = -offset;
612 //g_print ("allocating local %d to %d\n", i, -offset);
614 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
615 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
618 m->stack_offset = -offset;
621 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
622 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
626 * take the arguments and generate the arch-specific
627 * instructions to properly call the function in call.
628 * This includes pushing, moving arguments to the right register
630 * Issue: who does the spilling if needed, and when?
633 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
634 MonoInst *arg, *in, **rev_args;
635 MonoMethodSignature *sig;
636 int i, n, stack_size, type;
639 sig = call->signature;
640 n = sig->param_count + sig->hasthis;
641 rev_args = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * n);
643 if (sig->ret && (sig->ret->type == MONO_TYPE_I8 || sig->ret->type == MONO_TYPE_U8)) {
644 //g_warning ("long value returned");
646 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
650 for (i = 0; i < n; ++i) {
651 if (is_virtual && i == 0) {
652 /* the argument will be attached to the call instrucion */
653 rev_args [n - 1] = arg = NULL;
657 MONO_INST_NEW (cfg, arg, OP_OUTARG);
659 arg->cil_code = in->cil_code;
661 arg->type = in->type;
662 rev_args [n - i - 1] = arg;
663 if (i >= sig->hasthis) {
664 ptype = sig->params [i - sig->hasthis];
670 /* FIXME: validate arguments... */
674 case MONO_TYPE_BOOLEAN:
682 case MONO_TYPE_STRING:
683 case MONO_TYPE_CLASS:
684 case MONO_TYPE_OBJECT:
686 case MONO_TYPE_FNPTR:
687 case MONO_TYPE_ARRAY:
688 case MONO_TYPE_SZARRAY:
697 arg->opcode = OP_OUTARG_R4;
701 arg->opcode = OP_OUTARG_R8;
703 case MONO_TYPE_VALUETYPE:
704 if (MONO_TYPE_ISSTRUCT (ptype)) {
707 size = mono_type_native_stack_size (&in->klass->byval_arg, NULL);
709 size = mono_type_stack_size (&in->klass->byval_arg, NULL);
712 arg->opcode = OP_OUTARG_VT;
713 arg->klass = in->klass;
714 arg->unused = sig->pinvoke;
715 arg->inst_imm = size;
717 type = ptype->data.klass->enum_basetype->type;
721 case MONO_TYPE_TYPEDBYREF:
722 stack_size += sizeof (MonoTypedRef);
723 arg->opcode = OP_OUTARG_VT;
724 arg->klass = in->klass;
725 arg->unused = sig->pinvoke;
726 arg->inst_imm = sizeof (MonoTypedRef);
729 g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
732 /* the this argument */
737 /* they need to be pushed in reverse order */
738 /* if the function returns a struct, the called method already does a ret $0x4 */
739 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
741 call->args = rev_args;
742 call->stack_usage = stack_size;
744 * should set more info in call, such as the stack space
745 * used by the args that needs to be added back to esp
752 * Allow tracing to work with this interface (with an optional argument)
756 * This may be needed on some archs or for debugging support.
759 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
761 /* no stack room needed now (may be needed for FASTCALL-trace support) */
763 /* split prolog-epilog requirements? */
764 *code = 50; /* max bytes needed: check this number */
768 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
772 /* if some args are passed in registers, we need to save them here */
773 x86_push_reg (code, X86_EBP);
774 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
775 x86_push_imm (code, cfg->method);
776 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
777 x86_call_code (code, 0);
778 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
792 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
795 int arg_size = 0, save_mode = SAVE_NONE;
796 MonoMethod *method = cfg->method;
797 int rtype = method->signature->ret->type;
802 /* special case string .ctor icall */
803 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
804 save_mode = SAVE_EAX;
806 save_mode = SAVE_NONE;
810 save_mode = SAVE_EAX_EDX;
816 case MONO_TYPE_VALUETYPE:
817 if (method->signature->ret->data.klass->enumtype) {
818 rtype = method->signature->ret->data.klass->enum_basetype->type;
821 save_mode = SAVE_STRUCT;
824 save_mode = SAVE_EAX;
830 x86_push_reg (code, X86_EDX);
831 x86_push_reg (code, X86_EAX);
832 if (enable_arguments) {
833 x86_push_reg (code, X86_EDX);
834 x86_push_reg (code, X86_EAX);
839 x86_push_reg (code, X86_EAX);
840 if (enable_arguments) {
841 x86_push_reg (code, X86_EAX);
846 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
847 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
848 if (enable_arguments) {
849 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
850 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
855 if (enable_arguments) {
856 x86_push_membase (code, X86_EBP, 8);
866 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
867 x86_push_imm (code, method);
868 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
869 x86_call_code (code, 0);
870 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
874 x86_pop_reg (code, X86_EAX);
875 x86_pop_reg (code, X86_EDX);
878 x86_pop_reg (code, X86_EAX);
881 x86_fld_membase (code, X86_ESP, 0, TRUE);
882 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
892 #define EMIT_COND_BRANCH(ins,cond,sign) \
893 if (ins->flags & MONO_INST_BRLABEL) { \
894 if (ins->inst_i0->inst_c0) { \
895 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
897 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
898 x86_branch32 (code, cond, 0, sign); \
901 if (ins->inst_true_bb->native_offset) { \
902 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
904 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
905 if ((cfg->opt & MONO_OPT_BRANCH) && \
906 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
907 x86_branch8 (code, cond, 0, sign); \
909 x86_branch32 (code, cond, 0, sign); \
913 /* emit an exception if condition is fail */
914 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
916 mono_add_patch_info (cfg, code - cfg->native_code, \
917 MONO_PATCH_INFO_EXC, exc_name); \
918 x86_branch32 (code, cond, 0, signed); \
921 #define EMIT_FPCOMPARE(code) do { \
927 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
929 MonoInst *ins, *last_ins = NULL;
934 switch (ins->opcode) {
936 /* reg = 0 -> XOR (reg, reg) */
937 /* XOR sets cflags on x86, so we cant do it always */
938 if (ins->inst_c0 == 0 && ins->next &&
939 (ins->next->opcode == CEE_BR)) {
940 ins->opcode = CEE_XOR;
941 ins->sreg1 = ins->dreg;
942 ins->sreg2 = ins->dreg;
946 /* remove unnecessary multiplication with 1 */
947 if (ins->inst_imm == 1) {
948 if (ins->dreg != ins->sreg1) {
949 ins->opcode = OP_MOVE;
951 last_ins->next = ins->next;
958 /* OP_COMPARE_IMM (reg, 0) --> OP_X86_TEST_NULL (reg) */
959 if (ins->inst_imm == 0 && ins->next &&
960 (ins->next->opcode == CEE_BEQ || ins->next->opcode == CEE_BNE_UN ||
961 ins->next->opcode == OP_CEQ)) {
962 ins->opcode = OP_X86_TEST_NULL;
965 case OP_LOAD_MEMBASE:
966 case OP_LOADI4_MEMBASE:
968 * OP_STORE_MEMBASE_REG reg, offset(basereg)
969 * OP_LOAD_MEMBASE offset(basereg), reg
971 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
972 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
973 ins->inst_basereg == last_ins->inst_destbasereg &&
974 ins->inst_offset == last_ins->inst_offset) {
975 if (ins->dreg == last_ins->sreg1) {
976 last_ins->next = ins->next;
980 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
981 ins->opcode = OP_MOVE;
982 ins->sreg1 = last_ins->sreg1;
986 * Note: reg1 must be different from the basereg in the second load
987 * OP_LOAD_MEMBASE offset(basereg), reg1
988 * OP_LOAD_MEMBASE offset(basereg), reg2
990 * OP_LOAD_MEMBASE offset(basereg), reg1
993 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
994 || last_ins->opcode == OP_LOAD_MEMBASE) &&
995 ins->inst_basereg != last_ins->dreg &&
996 ins->inst_basereg == last_ins->inst_basereg &&
997 ins->inst_offset == last_ins->inst_offset) {
999 if (ins->dreg == last_ins->dreg) {
1000 last_ins->next = ins->next;
1004 ins->opcode = OP_MOVE;
1005 ins->sreg1 = last_ins->dreg;
1008 //g_assert_not_reached ();
1012 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1013 * OP_LOAD_MEMBASE offset(basereg), reg
1015 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1016 * OP_ICONST reg, imm
1018 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1019 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1020 ins->inst_basereg == last_ins->inst_destbasereg &&
1021 ins->inst_offset == last_ins->inst_offset) {
1022 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1023 ins->opcode = OP_ICONST;
1024 ins->inst_c0 = last_ins->inst_imm;
1025 g_assert_not_reached (); // check this rule
1029 case OP_LOADU1_MEMBASE:
1030 case OP_LOADI1_MEMBASE:
1032 * FIXME: Missing explanation
1034 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1035 ins->inst_basereg == last_ins->inst_destbasereg &&
1036 ins->inst_offset == last_ins->inst_offset) {
1037 if (ins->dreg == last_ins->sreg1) {
1038 last_ins->next = ins->next;
1042 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1043 ins->opcode = OP_MOVE;
1044 ins->sreg1 = last_ins->sreg1;
1048 case OP_LOADU2_MEMBASE:
1049 case OP_LOADI2_MEMBASE:
1051 * FIXME: Missing explanation
1053 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1054 ins->inst_basereg == last_ins->inst_destbasereg &&
1055 ins->inst_offset == last_ins->inst_offset) {
1056 if (ins->dreg == last_ins->sreg1) {
1057 last_ins->next = ins->next;
1061 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1062 ins->opcode = OP_MOVE;
1063 ins->sreg1 = last_ins->sreg1;
1073 if (ins->dreg == ins->sreg1) {
1075 last_ins->next = ins->next;
1080 * OP_MOVE sreg, dreg
1081 * OP_MOVE dreg, sreg
1083 if (last_ins && last_ins->opcode == OP_MOVE &&
1084 ins->sreg1 == last_ins->dreg &&
1085 ins->dreg == last_ins->sreg1) {
1086 last_ins->next = ins->next;
1095 bb->last_ins = last_ins;
1099 branch_cc_table [] = {
1100 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1101 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1102 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1105 #define DEBUG(a) if (cfg->verbose_level > 1) a
1107 #define reg_is_freeable(r) ((r) >= 0 && (r) <= 7 && X86_IS_CALLEE ((r)))
1116 static const char*const * ins_spec = pentium_desc;
1119 print_ins (int i, MonoInst *ins)
1121 const char *spec = ins_spec [ins->opcode];
1122 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1123 if (spec [MONO_INST_DEST]) {
1124 if (ins->dreg >= MONO_MAX_IREGS)
1125 g_print (" R%d <-", ins->dreg);
1127 g_print (" %s <-", mono_arch_regname (ins->dreg));
1129 if (spec [MONO_INST_SRC1]) {
1130 if (ins->sreg1 >= MONO_MAX_IREGS)
1131 g_print (" R%d", ins->sreg1);
1133 g_print (" %s", mono_arch_regname (ins->sreg1));
1135 if (spec [MONO_INST_SRC2]) {
1136 if (ins->sreg2 >= MONO_MAX_IREGS)
1137 g_print (" R%d", ins->sreg2);
1139 g_print (" %s", mono_arch_regname (ins->sreg2));
1141 if (spec [MONO_INST_CLOB])
1142 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1147 print_regtrack (RegTrack *t, int num)
1153 for (i = 0; i < num; ++i) {
1156 if (i >= MONO_MAX_IREGS) {
1157 g_snprintf (buf, sizeof(buf), "R%d", i);
1160 r = mono_arch_regname (i);
1161 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1165 typedef struct InstList InstList;
1173 static inline InstList*
1174 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1176 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1186 * Force the spilling of the variable in the symbolic register 'reg'.
1189 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1194 sel = cfg->rs->iassign [reg];
1195 /*i = cfg->rs->isymbolic [sel];
1196 g_assert (i == reg);*/
1198 spill = ++cfg->spill_count;
1199 cfg->rs->iassign [i] = -spill - 1;
1200 mono_regstate_free_int (cfg->rs, sel);
1201 /* we need to create a spill var and insert a load to sel after the current instruction */
1202 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1204 load->inst_basereg = X86_EBP;
1205 load->inst_offset = mono_spillvar_offset (cfg, spill);
1207 while (ins->next != item->prev->data)
1210 load->next = ins->next;
1212 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1213 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1214 g_assert (i == sel);
1220 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1225 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));
1226 /* exclude the registers in the current instruction */
1227 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1228 if (ins->sreg1 >= MONO_MAX_IREGS)
1229 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1231 regmask &= ~ (1 << ins->sreg1);
1232 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1234 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1235 if (ins->sreg2 >= MONO_MAX_IREGS)
1236 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1238 regmask &= ~ (1 << ins->sreg2);
1239 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1241 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1242 regmask &= ~ (1 << ins->dreg);
1243 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1246 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1247 g_assert (regmask); /* need at least a register we can free */
1249 /* we should track prev_use and spill the register that's farther */
1250 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1251 if (regmask & (1 << i)) {
1253 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1257 i = cfg->rs->isymbolic [sel];
1258 spill = ++cfg->spill_count;
1259 cfg->rs->iassign [i] = -spill - 1;
1260 mono_regstate_free_int (cfg->rs, sel);
1261 /* we need to create a spill var and insert a load to sel after the current instruction */
1262 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1264 load->inst_basereg = X86_EBP;
1265 load->inst_offset = mono_spillvar_offset (cfg, spill);
1267 while (ins->next != item->prev->data)
1270 load->next = ins->next;
1272 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1273 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1274 g_assert (i == sel);
1280 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1283 MONO_INST_NEW (cfg, copy, OP_MOVE);
1287 copy->next = ins->next;
1290 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1295 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1298 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1300 store->inst_destbasereg = X86_EBP;
1301 store->inst_offset = mono_spillvar_offset (cfg, spill);
1303 store->next = ins->next;
1306 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)));
1311 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1315 prev = item->next->data;
1317 while (prev->next != ins)
1319 to_insert->next = ins;
1320 prev->next = to_insert;
1322 to_insert->next = ins;
1325 * needed otherwise in the next instruction we can add an ins to the
1326 * end and that would get past this instruction.
1328 item->data = to_insert;
1333 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1335 int val = cfg->rs->iassign [sym_reg];
1339 /* the register gets spilled after this inst */
1342 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1344 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1345 cfg->rs->iassign [sym_reg] = val;
1346 /* add option to store before the instruction for src registers */
1348 create_spilled_store (cfg, spill, val, sym_reg, ins);
1350 cfg->rs->isymbolic [val] = sym_reg;
1358 * Local register allocation.
1359 * We first scan the list of instructions and we save the liveness info of
1360 * each register (when the register is first used, when it's value is set etc.).
1361 * We also reverse the list of instructions (in the InstList list) because assigning
1362 * registers backwards allows for more tricks to be used.
1365 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1368 MonoRegState *rs = cfg->rs;
1369 int i, val, fpcount;
1370 RegTrack *reginfo, *reginfof;
1371 RegTrack *reginfo1, *reginfo2, *reginfod;
1372 InstList *tmp, *reversed = NULL;
1374 guint32 src1_mask, src2_mask, dest_mask;
1378 rs->next_vireg = bb->max_ireg;
1379 rs->next_vfreg = bb->max_freg;
1380 mono_regstate_assign (rs);
1381 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1382 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1383 rs->ifree_mask = X86_CALLEE_REGS;
1387 if (cfg->opt & MONO_OPT_COPYPROP)
1388 local_copy_prop (cfg, ins);
1391 fpcount = 0; /* FIXME: track fp stack utilization */
1392 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1393 /* forward pass on the instructions to collect register liveness info */
1395 spec = ins_spec [ins->opcode];
1396 DEBUG (print_ins (i, ins));
1397 if (spec [MONO_INST_SRC1]) {
1398 if (spec [MONO_INST_SRC1] == 'f')
1399 reginfo1 = reginfof;
1402 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1403 reginfo1 [ins->sreg1].last_use = i;
1407 if (spec [MONO_INST_SRC2]) {
1408 if (spec [MONO_INST_SRC2] == 'f')
1409 reginfo2 = reginfof;
1412 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1413 reginfo2 [ins->sreg2].last_use = i;
1417 if (spec [MONO_INST_DEST]) {
1418 if (spec [MONO_INST_DEST] == 'f')
1419 reginfod = reginfof;
1422 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1423 reginfod [ins->dreg].killed_in = i;
1424 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1425 reginfod [ins->dreg].last_use = i;
1426 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1427 reginfod [ins->dreg].born_in = i;
1428 if (spec [MONO_INST_DEST] == 'l') {
1429 /* result in eax:edx, the virtual register is allocated sequentially */
1430 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1431 reginfod [ins->dreg + 1].last_use = i;
1432 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1433 reginfod [ins->dreg + 1].born_in = i;
1438 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1443 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1444 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1447 int prev_dreg, prev_sreg1, prev_sreg2;
1448 dest_mask = src1_mask = src2_mask = X86_CALLEE_REGS;
1451 spec = ins_spec [ins->opcode];
1452 DEBUG (g_print ("processing:"));
1453 DEBUG (print_ins (i, ins));
1454 if (spec [MONO_INST_CLOB] == 's') {
1455 if (rs->ifree_mask & (1 << X86_ECX)) {
1456 DEBUG (g_print ("\tshortcut assignment of R%d to ECX\n", ins->sreg2));
1457 rs->iassign [ins->sreg2] = X86_ECX;
1458 rs->isymbolic [X86_ECX] = ins->sreg2;
1459 ins->sreg2 = X86_ECX;
1460 rs->ifree_mask &= ~ (1 << X86_ECX);
1462 int need_ecx_spill = TRUE;
1464 * we first check if src1/dreg is already assigned a register
1465 * and then we force a spill of the var assigned to ECX.
1467 /* the destination register can't be ECX */
1468 dest_mask &= ~ (1 << X86_ECX);
1469 src1_mask &= ~ (1 << X86_ECX);
1470 val = rs->iassign [ins->dreg];
1472 * the destination register is already assigned to ECX:
1473 * we need to allocate another register for it and then
1474 * copy from this to ECX.
1476 if (val == X86_ECX && ins->dreg != ins->sreg2) {
1477 int new_dest = mono_regstate_alloc_int (rs, dest_mask);
1479 new_dest = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1480 g_assert (new_dest >= 0);
1481 ins->dreg = new_dest;
1482 create_copy_ins (cfg, X86_ECX, new_dest, ins);
1483 need_ecx_spill = FALSE;
1484 /*DEBUG (g_print ("\tforced spill of R%d\n", ins->dreg));
1485 val = get_register_force_spilling (cfg, tmp, ins, ins->dreg);
1486 rs->iassign [ins->dreg] = val;
1487 rs->isymbolic [val] = prev_dreg;
1490 val = rs->iassign [ins->sreg1];
1491 if (val == X86_ECX) {
1492 g_assert_not_reached ();
1493 } else if (val >= 0) {
1495 * the first src reg was already assigned to a register,
1496 * we need to copy it to the dest register because the
1497 * shift instruction clobbers the first operand.
1499 MonoInst *copy = create_copy_ins (cfg, ins->dreg, val, NULL);
1500 insert_before_ins (ins, tmp, copy);
1502 val = rs->iassign [ins->sreg2];
1503 if (val >= 0 && val != X86_ECX) {
1504 MonoInst *move = create_copy_ins (cfg, X86_ECX, val, NULL);
1505 DEBUG (g_print ("\tmoved arg from R%d (%d) to ECX\n", val, ins->sreg2));
1507 g_assert_not_reached ();
1508 /* FIXME: where is move connected to the instruction list? */
1509 //tmp->prev->data->next = move;
1511 if (need_ecx_spill && !(rs->ifree_mask & (1 << X86_ECX))) {
1512 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_ECX]));
1513 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_ECX]);
1514 mono_regstate_free_int (rs, X86_ECX);
1516 /* force-set sreg2 */
1517 rs->iassign [ins->sreg2] = X86_ECX;
1518 rs->isymbolic [X86_ECX] = ins->sreg2;
1519 ins->sreg2 = X86_ECX;
1520 rs->ifree_mask &= ~ (1 << X86_ECX);
1522 } else if (spec [MONO_INST_CLOB] == 'd') { /* division */
1523 int dest_reg = X86_EAX;
1524 int clob_reg = X86_EDX;
1525 if (spec [MONO_INST_DEST] == 'd') {
1526 dest_reg = X86_EDX; /* reminder */
1529 val = rs->iassign [ins->dreg];
1530 if (0 && val >= 0 && val != dest_reg && !(rs->ifree_mask & (1 << dest_reg))) {
1531 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1532 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1533 mono_regstate_free_int (rs, dest_reg);
1537 /* the register gets spilled after this inst */
1538 int spill = -val -1;
1539 dest_mask = 1 << clob_reg;
1540 prev_dreg = ins->dreg;
1541 val = mono_regstate_alloc_int (rs, dest_mask);
1543 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1544 rs->iassign [ins->dreg] = val;
1546 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1547 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1548 rs->isymbolic [val] = prev_dreg;
1550 if (val != dest_reg) { /* force a copy */
1551 create_copy_ins (cfg, val, dest_reg, ins);
1554 DEBUG (g_print ("\tshortcut assignment of R%d to %s\n", ins->dreg, mono_arch_regname (dest_reg)));
1555 rs->iassign [ins->dreg] = dest_reg;
1556 rs->isymbolic [dest_reg] = ins->dreg;
1557 ins->dreg = dest_reg;
1558 rs->ifree_mask &= ~ (1 << dest_reg);
1561 //DEBUG (g_print ("dest reg in div assigned: %s\n", mono_arch_regname (val)));
1562 if (val != dest_reg) { /* force a copy */
1563 create_copy_ins (cfg, val, dest_reg, ins);
1564 if (!(rs->ifree_mask & (1 << dest_reg)) && rs->isymbolic [dest_reg] >= MONO_MAX_IREGS) {
1565 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1566 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1567 mono_regstate_free_int (rs, dest_reg);
1571 src1_mask = 1 << X86_EAX;
1572 src2_mask = 1 << X86_ECX;
1574 if (spec [MONO_INST_DEST] == 'l') {
1575 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1576 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1577 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1578 mono_regstate_free_int (rs, X86_EAX);
1580 if (!(rs->ifree_mask & (1 << X86_EDX))) {
1581 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EDX]));
1582 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EDX]);
1583 mono_regstate_free_int (rs, X86_EDX);
1586 /* update for use with FP regs... */
1587 if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
1588 val = rs->iassign [ins->dreg];
1589 prev_dreg = ins->dreg;
1593 /* the register gets spilled after this inst */
1596 val = mono_regstate_alloc_int (rs, dest_mask);
1598 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1599 rs->iassign [ins->dreg] = val;
1601 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1603 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1604 rs->isymbolic [val] = prev_dreg;
1606 if (spec [MONO_INST_DEST] == 'l') {
1607 int hreg = prev_dreg + 1;
1608 val = rs->iassign [hreg];
1612 /* the register gets spilled after this inst */
1615 val = mono_regstate_alloc_int (rs, dest_mask);
1617 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1618 rs->iassign [hreg] = val;
1620 create_spilled_store (cfg, spill, val, hreg, ins);
1622 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1623 rs->isymbolic [val] = hreg;
1624 /* FIXME:? ins->dreg = val; */
1625 if (ins->dreg == X86_EAX) {
1627 create_copy_ins (cfg, val, X86_EDX, ins);
1628 } else if (ins->dreg == X86_EDX) {
1629 if (val == X86_EAX) {
1631 g_assert_not_reached ();
1633 /* two forced copies */
1634 create_copy_ins (cfg, val, X86_EDX, ins);
1635 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1638 if (val == X86_EDX) {
1639 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1641 /* two forced copies */
1642 create_copy_ins (cfg, val, X86_EDX, ins);
1643 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1646 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1647 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1648 mono_regstate_free_int (rs, val);
1650 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != X86_EAX && spec [MONO_INST_CLOB] != 'd') {
1651 /* this instruction only outputs to EAX, need to copy */
1652 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1653 } else if (spec [MONO_INST_DEST] == 'd' && ins->dreg != X86_EDX && spec [MONO_INST_CLOB] != 'd') {
1654 create_copy_ins (cfg, ins->dreg, X86_EDX, ins);
1659 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1660 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1661 mono_regstate_free_int (rs, ins->dreg);
1663 /* put src1 in EAX if it needs to be */
1664 if (spec [MONO_INST_SRC1] == 'a') {
1665 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1666 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1667 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1668 mono_regstate_free_int (rs, X86_EAX);
1670 /* force-set sreg1 */
1671 rs->iassign [ins->sreg1] = X86_EAX;
1672 rs->isymbolic [X86_EAX] = ins->sreg1;
1673 ins->sreg1 = X86_EAX;
1674 rs->ifree_mask &= ~ (1 << X86_EAX);
1676 if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
1677 val = rs->iassign [ins->sreg1];
1678 prev_sreg1 = ins->sreg1;
1682 /* the register gets spilled after this inst */
1685 if (0 && ins->opcode == OP_MOVE) {
1687 * small optimization: the dest register is already allocated
1688 * but the src one is not: we can simply assign the same register
1689 * here and peephole will get rid of the instruction later.
1690 * This optimization may interfere with the clobbering handling:
1691 * it removes a mov operation that will be added again to handle clobbering.
1692 * There are also some other issues that should with make testjit.
1694 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1695 val = rs->iassign [ins->sreg1] = ins->dreg;
1696 //g_assert (val >= 0);
1697 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1699 //g_assert (val == -1); /* source cannot be spilled */
1700 val = mono_regstate_alloc_int (rs, src1_mask);
1702 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1703 rs->iassign [ins->sreg1] = val;
1704 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1707 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1708 insert_before_ins (ins, tmp, store);
1711 rs->isymbolic [val] = prev_sreg1;
1716 /* handle clobbering of sreg1 */
1717 if ((spec [MONO_INST_CLOB] == '1' || spec [MONO_INST_CLOB] == 's') && ins->dreg != ins->sreg1) {
1718 MonoInst *copy = create_copy_ins (cfg, ins->dreg, ins->sreg1, NULL);
1719 DEBUG (g_print ("\tneed to copy sreg1 %s to dreg %s\n", mono_arch_regname (ins->sreg1), mono_arch_regname (ins->dreg)));
1720 if (ins->sreg2 == -1 || spec [MONO_INST_CLOB] == 's') {
1721 /* note: the copy is inserted before the current instruction! */
1722 insert_before_ins (ins, tmp, copy);
1723 /* we set sreg1 to dest as well */
1724 prev_sreg1 = ins->sreg1 = ins->dreg;
1726 /* inserted after the operation */
1727 copy->next = ins->next;
1731 if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
1732 val = rs->iassign [ins->sreg2];
1733 prev_sreg2 = ins->sreg2;
1737 /* the register gets spilled after this inst */
1740 val = mono_regstate_alloc_int (rs, src2_mask);
1742 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1743 rs->iassign [ins->sreg2] = val;
1744 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1746 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1748 rs->isymbolic [val] = prev_sreg2;
1750 if (spec [MONO_INST_CLOB] == 's' && ins->sreg2 != X86_ECX) {
1751 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]));
1757 if (spec [MONO_INST_CLOB] == 'c') {
1759 guint32 clob_mask = X86_CALLEE_REGS;
1760 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1762 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1763 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1767 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1768 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1769 mono_regstate_free_int (rs, ins->sreg1);
1771 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1772 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1773 mono_regstate_free_int (rs, ins->sreg2);
1776 //DEBUG (print_ins (i, ins));
1777 /* this may result from a insert_before call */
1779 bb->code = tmp->data;
1784 static unsigned char*
1785 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1787 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1788 x86_fnstcw_membase(code, X86_ESP, 0);
1789 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1790 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1791 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1792 x86_fldcw_membase (code, X86_ESP, 2);
1794 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1795 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1796 x86_pop_reg (code, dreg);
1797 /* FIXME: need the high register
1798 * x86_pop_reg (code, dreg_high);
1801 x86_push_reg (code, X86_EAX); // SP = SP - 4
1802 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1803 x86_pop_reg (code, dreg);
1805 x86_fldcw_membase (code, X86_ESP, 0);
1806 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1809 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1811 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1815 static unsigned char*
1816 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1818 int sreg = tree->sreg1;
1819 #ifdef PLATFORM_WIN32
1824 * If requested stack size is larger than one page,
1825 * perform stack-touch operation
1828 * Generate stack probe code.
1829 * Under Windows, it is necessary to allocate one page at a time,
1830 * "touching" stack after each successful sub-allocation. This is
1831 * because of the way stack growth is implemented - there is a
1832 * guard page before the lowest stack page that is currently commited.
1833 * Stack normally grows sequentially so OS traps access to the
1834 * guard page and commits more pages when needed.
1836 x86_test_reg_imm (code, sreg, ~0xFFF);
1837 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1839 br[2] = code; /* loop */
1840 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
1841 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
1842 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
1843 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
1844 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
1845 x86_patch (br[3], br[2]);
1846 x86_test_reg_reg (code, sreg, sreg);
1847 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1848 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1850 br[1] = code; x86_jump8 (code, 0);
1852 x86_patch (br[0], code);
1853 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1854 x86_patch (br[1], code);
1855 x86_patch (br[4], code);
1856 #else /* PLATFORM_WIN32 */
1857 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1859 if (tree->flags & MONO_INST_INIT) {
1861 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1862 x86_push_reg (code, X86_EAX);
1865 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1866 x86_push_reg (code, X86_ECX);
1869 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1870 x86_push_reg (code, X86_EDI);
1874 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1875 if (sreg != X86_ECX)
1876 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1877 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1879 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1881 x86_prefix (code, X86_REP_PREFIX);
1884 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1885 x86_pop_reg (code, X86_EDI);
1886 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1887 x86_pop_reg (code, X86_ECX);
1888 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1889 x86_pop_reg (code, X86_EAX);
1894 #define REAL_PRINT_REG(text,reg) \
1895 mono_assert (reg >= 0); \
1896 x86_push_reg (code, X86_EAX); \
1897 x86_push_reg (code, X86_EDX); \
1898 x86_push_reg (code, X86_ECX); \
1899 x86_push_reg (code, reg); \
1900 x86_push_imm (code, reg); \
1901 x86_push_imm (code, text " %d %p\n"); \
1902 x86_mov_reg_imm (code, X86_EAX, printf); \
1903 x86_call_reg (code, X86_EAX); \
1904 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
1905 x86_pop_reg (code, X86_ECX); \
1906 x86_pop_reg (code, X86_EDX); \
1907 x86_pop_reg (code, X86_EAX);
1910 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1915 guint8 *code = cfg->native_code + cfg->code_len;
1916 MonoInst *last_ins = NULL;
1917 guint last_offset = 0;
1920 if (cfg->opt & MONO_OPT_PEEPHOLE)
1921 peephole_pass (cfg, bb);
1925 * various stratgies to align BBs. Using real loop detection or simply
1926 * aligning every block leads to more consistent benchmark results,
1927 * but usually slows down the code
1928 * we should do the alignment outside this function or we should adjust
1929 * bb->native offset as well or the code is effectively slowed down!
1931 /* align all blocks */
1932 // if ((pad = (cfg->code_len & (align - 1)))) {
1933 /* poor man loop start detection */
1934 // if (bb->code && bb->in_count && bb->in_bb [0]->cil_code > bb->cil_code && (pad = (cfg->code_len & (align - 1)))) {
1935 /* consider real loop detection and nesting level */
1936 // if (bb->loop_blocks && bb->nesting < 3 && (pad = (cfg->code_len & (align - 1)))) {
1937 /* consider real loop detection */
1938 if (bb->loop_blocks && (pad = (cfg->code_len & (align - 1)))) {
1940 x86_padding (code, pad);
1941 cfg->code_len += pad;
1942 bb->native_offset = cfg->code_len;
1946 if (cfg->verbose_level > 2)
1947 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1949 cpos = bb->max_offset;
1951 if (mono_trace_coverage) {
1952 MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
1953 g_assert (!mono_compile_aot);
1956 // fixme: make this work with inlining
1957 g_assert_not_reached ();
1959 //cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
1960 /* this is not thread save, but good enough */
1961 /* fixme: howto handle overflows? */
1962 x86_inc_mem (code, &cov->data [bb->dfn].count);
1965 offset = code - cfg->native_code;
1969 offset = code - cfg->native_code;
1971 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1973 if (offset > (cfg->code_size - max_len - 16)) {
1974 cfg->code_size *= 2;
1975 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1976 code = cfg->native_code + offset;
1977 mono_jit_stats.code_reallocs++;
1980 mono_debug_record_line_number (cfg, ins, offset);
1982 switch (ins->opcode) {
1984 x86_mul_reg (code, ins->sreg2, FALSE);
1986 case OP_X86_SETEQ_MEMBASE:
1987 x86_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
1989 case OP_STOREI1_MEMBASE_IMM:
1990 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
1992 case OP_STOREI2_MEMBASE_IMM:
1993 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
1995 case OP_STORE_MEMBASE_IMM:
1996 case OP_STOREI4_MEMBASE_IMM:
1997 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
1999 case OP_STOREI1_MEMBASE_REG:
2000 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2002 case OP_STOREI2_MEMBASE_REG:
2003 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2005 case OP_STORE_MEMBASE_REG:
2006 case OP_STOREI4_MEMBASE_REG:
2007 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2012 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2015 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2016 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2018 case OP_LOAD_MEMBASE:
2019 case OP_LOADI4_MEMBASE:
2020 case OP_LOADU4_MEMBASE:
2021 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2023 case OP_LOADU1_MEMBASE:
2024 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2026 case OP_LOADI1_MEMBASE:
2027 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2029 case OP_LOADU2_MEMBASE:
2030 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2032 case OP_LOADI2_MEMBASE:
2033 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2036 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2039 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2042 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2045 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2048 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2050 case OP_COMPARE_IMM:
2051 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2053 case OP_X86_COMPARE_MEMBASE_REG:
2054 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2056 case OP_X86_COMPARE_MEMBASE_IMM:
2057 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2059 case OP_X86_COMPARE_REG_MEMBASE:
2060 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2062 case OP_X86_TEST_NULL:
2063 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2065 case OP_X86_ADD_MEMBASE_IMM:
2066 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2068 case OP_X86_SUB_MEMBASE_IMM:
2069 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2071 case OP_X86_INC_MEMBASE:
2072 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2074 case OP_X86_INC_REG:
2075 x86_inc_reg (code, ins->dreg);
2077 case OP_X86_DEC_MEMBASE:
2078 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2080 case OP_X86_DEC_REG:
2081 x86_dec_reg (code, ins->dreg);
2084 x86_breakpoint (code);
2088 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2091 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2094 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2097 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2101 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2104 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2107 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2110 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2113 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2116 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2120 x86_div_reg (code, ins->sreg2, TRUE);
2123 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2124 x86_div_reg (code, ins->sreg2, FALSE);
2127 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2129 x86_div_reg (code, ins->sreg2, TRUE);
2133 x86_div_reg (code, ins->sreg2, TRUE);
2136 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2137 x86_div_reg (code, ins->sreg2, FALSE);
2140 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2142 x86_div_reg (code, ins->sreg2, TRUE);
2145 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2148 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2151 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2154 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2157 g_assert (ins->sreg2 == X86_ECX);
2158 x86_shift_reg (code, X86_SHL, ins->dreg);
2161 g_assert (ins->sreg2 == X86_ECX);
2162 x86_shift_reg (code, X86_SAR, ins->dreg);
2165 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2168 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2171 g_assert (ins->sreg2 == X86_ECX);
2172 x86_shift_reg (code, X86_SHR, ins->dreg);
2175 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2178 x86_not_reg (code, ins->sreg1);
2181 x86_neg_reg (code, ins->sreg1);
2184 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2187 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2190 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2193 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2196 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2197 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2199 case CEE_MUL_OVF_UN: {
2200 /* the mul operation and the exception check should most likely be split */
2201 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2202 /*g_assert (ins->sreg2 == X86_EAX);
2203 g_assert (ins->dreg == X86_EAX);*/
2204 if (ins->sreg2 == X86_EAX) {
2205 non_eax_reg = ins->sreg1;
2206 } else if (ins->sreg1 == X86_EAX) {
2207 non_eax_reg = ins->sreg2;
2209 /* no need to save since we're going to store to it anyway */
2210 if (ins->dreg != X86_EAX) {
2212 x86_push_reg (code, X86_EAX);
2214 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2215 non_eax_reg = ins->sreg2;
2217 if (ins->dreg == X86_EDX) {
2220 x86_push_reg (code, X86_EAX);
2222 } else if (ins->dreg != X86_EAX) {
2224 x86_push_reg (code, X86_EDX);
2226 x86_mul_reg (code, non_eax_reg, FALSE);
2227 /* save before the check since pop and mov don't change the flags */
2229 x86_pop_reg (code, X86_EDX);
2231 x86_pop_reg (code, X86_EAX);
2232 if (ins->dreg != X86_EAX)
2233 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2234 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2238 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2241 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2242 x86_mov_reg_imm (code, ins->dreg, 0);
2247 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2251 * Note: this 'frame destruction' logic is useful for tail calls, too.
2252 * Keep in sync with the code in emit_epilog.
2256 /* FIXME: no tracing support... */
2257 if (mono_jit_profile)
2258 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2259 /* reset offset to make max_len work */
2260 offset = code - cfg->native_code;
2262 g_assert (!cfg->method->save_lmf);
2264 if (cfg->used_int_regs & (1 << X86_EBX))
2266 if (cfg->used_int_regs & (1 << X86_EDI))
2268 if (cfg->used_int_regs & (1 << X86_ESI))
2271 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2273 if (cfg->used_int_regs & (1 << X86_ESI))
2274 x86_pop_reg (code, X86_ESI);
2275 if (cfg->used_int_regs & (1 << X86_EDI))
2276 x86_pop_reg (code, X86_EDI);
2277 if (cfg->used_int_regs & (1 << X86_EBX))
2278 x86_pop_reg (code, X86_EBX);
2280 /* restore ESP/EBP */
2282 offset = code - cfg->native_code;
2283 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2284 x86_jump32 (code, 0);
2288 /* ensure ins->sreg1 is not NULL */
2289 x86_alu_membase_imm (code, X86_CMP, ins->sreg1, 0, 0);
2292 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2293 x86_push_reg (code, hreg);
2294 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2295 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2296 x86_pop_reg (code, hreg);
2304 call = (MonoCallInst*)ins;
2305 if (call->signature->call_convention == MONO_CALL_VARARG) {
2307 x86_push_imm (code, call->signature);
2308 offset = code - cfg->native_code;
2310 if (ins->flags & MONO_INST_HAS_METHOD)
2311 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2313 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2315 x86_call_code (code, 0);
2316 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2317 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2322 case OP_VOIDCALL_REG:
2324 call = (MonoCallInst*)ins;
2325 x86_call_reg (code, ins->sreg1);
2326 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2327 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2329 case OP_FCALL_MEMBASE:
2330 case OP_LCALL_MEMBASE:
2331 case OP_VCALL_MEMBASE:
2332 case OP_VOIDCALL_MEMBASE:
2333 case OP_CALL_MEMBASE:
2334 call = (MonoCallInst*)ins;
2335 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2336 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2337 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2341 x86_push_reg (code, ins->sreg1);
2343 case OP_X86_PUSH_IMM:
2344 x86_push_imm (code, ins->inst_imm);
2346 case OP_X86_PUSH_MEMBASE:
2347 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2349 case OP_X86_PUSH_OBJ:
2350 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2351 x86_push_reg (code, X86_EDI);
2352 x86_push_reg (code, X86_ESI);
2353 x86_push_reg (code, X86_ECX);
2354 if (ins->inst_offset)
2355 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2357 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2358 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2359 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2361 x86_prefix (code, X86_REP_PREFIX);
2363 x86_pop_reg (code, X86_ECX);
2364 x86_pop_reg (code, X86_ESI);
2365 x86_pop_reg (code, X86_EDI);
2368 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
2371 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2374 /* keep alignment */
2375 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
2376 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
2377 code = mono_emit_stack_alloc (code, ins);
2378 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2384 x86_push_reg (code, ins->sreg1);
2385 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2386 (gpointer)"mono_arch_throw_exception");
2387 x86_call_code (code, 0);
2390 case OP_CALL_HANDLER:
2391 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2392 x86_call_imm (code, 0);
2395 ins->inst_c0 = code - cfg->native_code;
2398 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2399 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2401 if (ins->flags & MONO_INST_BRLABEL) {
2402 if (ins->inst_i0->inst_c0) {
2403 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2405 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2406 x86_jump32 (code, 0);
2409 if (ins->inst_target_bb->native_offset) {
2410 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2412 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2413 if ((cfg->opt & MONO_OPT_BRANCH) &&
2414 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
2415 x86_jump8 (code, 0);
2417 x86_jump32 (code, 0);
2422 x86_jump_reg (code, ins->sreg1);
2425 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2426 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2429 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
2430 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2433 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2434 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2437 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
2438 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2441 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2442 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2444 case OP_COND_EXC_EQ:
2445 case OP_COND_EXC_NE_UN:
2446 case OP_COND_EXC_LT:
2447 case OP_COND_EXC_LT_UN:
2448 case OP_COND_EXC_GT:
2449 case OP_COND_EXC_GT_UN:
2450 case OP_COND_EXC_GE:
2451 case OP_COND_EXC_GE_UN:
2452 case OP_COND_EXC_LE:
2453 case OP_COND_EXC_LE_UN:
2454 case OP_COND_EXC_OV:
2455 case OP_COND_EXC_NO:
2457 case OP_COND_EXC_NC:
2458 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2459 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2471 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
2474 /* floating point opcodes */
2476 double d = *(double *)ins->inst_p0;
2478 if ((d == 0.0) && (signbit (d) == 0)) {
2480 } else if (d == 1.0) {
2483 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
2484 x86_fld (code, NULL, TRUE);
2489 float f = *(float *)ins->inst_p0;
2491 if ((f == 0.0) && (signbit (f) == 0)) {
2493 } else if (f == 1.0) {
2496 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
2497 x86_fld (code, NULL, FALSE);
2501 case OP_STORER8_MEMBASE_REG:
2502 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
2504 case OP_LOADR8_MEMBASE:
2505 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2507 case OP_STORER4_MEMBASE_REG:
2508 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
2510 case OP_LOADR4_MEMBASE:
2511 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2513 case CEE_CONV_R4: /* FIXME: change precision */
2515 x86_push_reg (code, ins->sreg1);
2516 x86_fild_membase (code, X86_ESP, 0, FALSE);
2517 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2519 case OP_X86_FP_LOAD_I8:
2520 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2522 case OP_X86_FP_LOAD_I4:
2523 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2525 case OP_FCONV_TO_I1:
2526 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2528 case OP_FCONV_TO_U1:
2529 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2531 case OP_FCONV_TO_I2:
2532 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2534 case OP_FCONV_TO_U2:
2535 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2537 case OP_FCONV_TO_I4:
2539 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2541 case OP_FCONV_TO_I8:
2542 /* we defined this instruction to output only to eax:edx */
2543 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2544 x86_fnstcw_membase(code, X86_ESP, 0);
2545 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 2);
2546 x86_alu_reg_imm (code, X86_OR, X86_EAX, 0xc00);
2547 x86_mov_membase_reg (code, X86_ESP, 2, X86_EAX, 2);
2548 x86_fldcw_membase (code, X86_ESP, 2);
2549 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2550 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2551 x86_pop_reg (code, X86_EAX);
2552 x86_pop_reg (code, X86_EDX);
2553 x86_fldcw_membase (code, X86_ESP, 0);
2554 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2556 case OP_LCONV_TO_R_UN: {
2557 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2560 /* load 64bit integer to FP stack */
2561 x86_push_imm (code, 0);
2562 x86_push_reg (code, ins->sreg2);
2563 x86_push_reg (code, ins->sreg1);
2564 x86_fild_membase (code, X86_ESP, 0, TRUE);
2565 /* store as 80bit FP value */
2566 x86_fst80_membase (code, X86_ESP, 0);
2568 /* test if lreg is negative */
2569 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2570 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2572 /* add correction constant mn */
2573 x86_fld80_mem (code, mn);
2574 x86_fld80_membase (code, X86_ESP, 0);
2575 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2576 x86_fst80_membase (code, X86_ESP, 0);
2578 x86_patch (br, code);
2580 x86_fld80_membase (code, X86_ESP, 0);
2581 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2585 case OP_LCONV_TO_OVF_I: {
2586 guint8 *br [3], *label [1];
2589 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2591 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2593 /* If the low word top bit is set, see if we are negative */
2594 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2595 /* We are not negative (no top bit set, check for our top word to be zero */
2596 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2597 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2600 /* throw exception */
2601 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2602 x86_jump32 (code, 0);
2604 x86_patch (br [0], code);
2605 /* our top bit is set, check that top word is 0xfffffff */
2606 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2608 x86_patch (br [1], code);
2609 /* nope, emit exception */
2610 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2611 x86_patch (br [2], label [0]);
2613 if (ins->dreg != ins->sreg1)
2614 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2618 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2621 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
2624 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
2627 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
2643 * it really doesn't make sense to inline all this code,
2644 * it's here just to show that things may not be as simple
2647 guchar *check_pos, *end_tan, *pop_jump;
2648 x86_push_reg (code, X86_EAX);
2651 x86_test_reg_imm (code, X86_EAX, 0x400);
2653 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2654 x86_fstp (code, 0); /* pop the 1.0 */
2656 x86_jump8 (code, 0);
2658 x86_fp_op (code, X86_FADD, 0);
2662 x86_test_reg_imm (code, X86_EAX, 0x400);
2664 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2667 x86_patch (pop_jump, code);
2668 x86_fstp (code, 0); /* pop the 1.0 */
2669 x86_patch (check_pos, code);
2670 x86_patch (end_tan, code);
2671 x86_pop_reg (code, X86_EAX);
2687 x86_push_reg (code, X86_EAX);
2688 /* we need to exchange ST(0) with ST(1) */
2691 /* this requires a loop, because fprem somtimes
2692 * returns a partial remainder */
2694 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
2695 /* x86_fprem1 (code); */
2698 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x0400);
2700 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
2705 x86_pop_reg (code, X86_EAX);
2709 if (cfg->opt & MONO_OPT_FCMOV) {
2710 x86_fcomip (code, 1);
2714 /* this overwrites EAX */
2715 EMIT_FPCOMPARE(code);
2716 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2719 if (cfg->opt & MONO_OPT_FCMOV) {
2720 /* zeroing the register at the start results in
2721 * shorter and faster code (we can also remove the widening op)
2723 guchar *unordered_check;
2724 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2725 x86_fcomip (code, 1);
2727 unordered_check = code;
2728 x86_branch8 (code, X86_CC_P, 0, FALSE);
2729 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
2730 x86_patch (unordered_check, code);
2733 if (ins->dreg != X86_EAX)
2734 x86_push_reg (code, X86_EAX);
2736 EMIT_FPCOMPARE(code);
2737 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2738 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2739 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2740 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2742 if (ins->dreg != X86_EAX)
2743 x86_pop_reg (code, X86_EAX);
2747 if (cfg->opt & MONO_OPT_FCMOV) {
2748 /* zeroing the register at the start results in
2749 * shorter and faster code (we can also remove the widening op)
2751 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2752 x86_fcomip (code, 1);
2754 if (ins->opcode == OP_FCLT_UN) {
2755 guchar *unordered_check = code;
2756 guchar *jump_to_end;
2757 x86_branch8 (code, X86_CC_P, 0, FALSE);
2758 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2760 x86_jump8 (code, 0);
2761 x86_patch (unordered_check, code);
2762 x86_inc_reg (code, ins->dreg);
2763 x86_patch (jump_to_end, code);
2765 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2769 if (ins->dreg != X86_EAX)
2770 x86_push_reg (code, X86_EAX);
2772 EMIT_FPCOMPARE(code);
2773 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2774 if (ins->opcode == OP_FCLT_UN) {
2775 guchar *is_not_zero_check, *end_jump;
2776 is_not_zero_check = code;
2777 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2779 x86_jump8 (code, 0);
2780 x86_patch (is_not_zero_check, code);
2781 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2783 x86_patch (end_jump, code);
2785 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2786 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2788 if (ins->dreg != X86_EAX)
2789 x86_pop_reg (code, X86_EAX);
2793 if (cfg->opt & MONO_OPT_FCMOV) {
2794 /* zeroing the register at the start results in
2795 * shorter and faster code (we can also remove the widening op)
2797 guchar *unordered_check;
2798 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2799 x86_fcomip (code, 1);
2801 if (ins->opcode == OP_FCGT) {
2802 unordered_check = code;
2803 x86_branch8 (code, X86_CC_P, 0, FALSE);
2804 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2805 x86_patch (unordered_check, code);
2807 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2811 if (ins->dreg != X86_EAX)
2812 x86_push_reg (code, X86_EAX);
2814 EMIT_FPCOMPARE(code);
2815 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2816 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2817 if (ins->opcode == OP_FCGT_UN) {
2818 guchar *is_not_zero_check, *end_jump;
2819 is_not_zero_check = code;
2820 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2822 x86_jump8 (code, 0);
2823 x86_patch (is_not_zero_check, code);
2824 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2826 x86_patch (end_jump, code);
2828 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2829 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2831 if (ins->dreg != X86_EAX)
2832 x86_pop_reg (code, X86_EAX);
2835 if (cfg->opt & MONO_OPT_FCMOV) {
2836 guchar *jump = code;
2837 x86_branch8 (code, X86_CC_P, 0, TRUE);
2838 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2839 x86_patch (jump, code);
2842 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2843 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
2846 if (cfg->opt & MONO_OPT_FCMOV) {
2847 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2848 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2851 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2852 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2855 if (cfg->opt & MONO_OPT_FCMOV) {
2856 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2859 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2862 if (cfg->opt & MONO_OPT_FCMOV) {
2863 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2864 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2867 if (ins->opcode == OP_FBLT_UN) {
2868 guchar *is_not_zero_check, *end_jump;
2869 is_not_zero_check = code;
2870 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2872 x86_jump8 (code, 0);
2873 x86_patch (is_not_zero_check, code);
2874 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2876 x86_patch (end_jump, code);
2878 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2882 if (cfg->opt & MONO_OPT_FCMOV) {
2883 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
2886 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2887 if (ins->opcode == OP_FBGT_UN) {
2888 guchar *is_not_zero_check, *end_jump;
2889 is_not_zero_check = code;
2890 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2892 x86_jump8 (code, 0);
2893 x86_patch (is_not_zero_check, code);
2894 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2896 x86_patch (end_jump, code);
2898 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2902 if (cfg->opt & MONO_OPT_FCMOV) {
2903 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
2906 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2910 if (cfg->opt & MONO_OPT_FCMOV) {
2911 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2912 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
2915 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2916 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2918 case CEE_CKFINITE: {
2919 x86_push_reg (code, X86_EAX);
2922 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2923 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2924 x86_pop_reg (code, X86_EAX);
2925 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
2929 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2930 g_assert_not_reached ();
2933 if ((code - cfg->native_code - offset) > max_len) {
2934 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2935 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2936 g_assert_not_reached ();
2942 last_offset = offset;
2947 cfg->code_len = code - cfg->native_code;
2951 mono_arch_register_lowlevel_calls (void)
2953 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2954 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2958 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji)
2960 MonoJumpInfo *patch_info;
2962 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2963 unsigned char *ip = patch_info->ip.i + code;
2964 const unsigned char *target = NULL;
2966 switch (patch_info->type) {
2967 case MONO_PATCH_INFO_BB:
2968 target = patch_info->data.bb->native_offset + code;
2970 case MONO_PATCH_INFO_ABS:
2971 target = patch_info->data.target;
2973 case MONO_PATCH_INFO_LABEL:
2974 target = patch_info->data.inst->inst_c0 + code;
2976 case MONO_PATCH_INFO_IP:
2977 *((gpointer *)(ip)) = ip;
2979 case MONO_PATCH_INFO_INTERNAL_METHOD: {
2980 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
2982 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
2983 g_assert_not_reached ();
2985 target = mono_icall_get_wrapper (mi);
2988 case MONO_PATCH_INFO_METHOD_JUMP:
2989 /* get the trampoline to the method from the domain */
2990 if (!(target = g_hash_table_lookup (domain->jit_code_hash, patch_info->data.method))) {
2992 target = mono_arch_create_jump_trampoline (patch_info->data.method);
2993 if (!domain->jump_target_hash)
2994 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
2995 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
2996 list = g_slist_prepend (list, ip);
2997 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
3000 case MONO_PATCH_INFO_METHOD:
3001 if (patch_info->data.method == method) {
3004 /* get the trampoline to the method from the domain */
3005 if (!(target = g_hash_table_lookup (domain->jit_code_hash, patch_info->data.method)))
3006 target = mono_arch_create_jit_trampoline (patch_info->data.method);
3009 case MONO_PATCH_INFO_SWITCH: {
3010 gpointer *table = (gpointer *)patch_info->data.target;
3013 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3015 for (i = 0; i < patch_info->table_size; i++) {
3016 table [i] = (int)patch_info->data.table [i] + code;
3018 /* we put into the table the absolute address, no need fo x86_patch in this case */
3021 case MONO_PATCH_INFO_METHODCONST:
3022 case MONO_PATCH_INFO_CLASS:
3023 case MONO_PATCH_INFO_IMAGE:
3024 case MONO_PATCH_INFO_FIELD:
3025 *((gconstpointer *)(ip + 1)) = patch_info->data.target;
3027 case MONO_PATCH_INFO_R4:
3028 case MONO_PATCH_INFO_R8:
3029 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3032 g_assert_not_reached ();
3034 x86_patch (ip, target);
3039 mono_arch_max_epilog_size (MonoCompile *cfg)
3041 int exc_count = 0, max_epilog_size = 16;
3042 MonoJumpInfo *patch_info;
3044 if (cfg->method->save_lmf)
3045 max_epilog_size += 128;
3047 if (mono_jit_trace_calls)
3048 max_epilog_size += 50;
3050 if (mono_jit_profile)
3051 max_epilog_size += 50;
3053 /* count the number of exception infos */
3055 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3056 if (patch_info->type == MONO_PATCH_INFO_EXC)
3061 * make sure we have enough space for exceptions
3062 * 16 is the size of two push_imm instructions and a call
3064 max_epilog_size += exc_count*16;
3066 return max_epilog_size;
3070 mono_arch_emit_prolog (MonoCompile *cfg)
3072 MonoMethod *method = cfg->method;
3074 MonoMethodSignature *sig;
3076 int alloc_size, pos, max_offset, i;
3079 cfg->code_size = MAX (((MonoMethodNormal *)method)->header->code_size * 4, 256);
3080 code = cfg->native_code = g_malloc (cfg->code_size);
3082 x86_push_reg (code, X86_EBP);
3083 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
3085 alloc_size = - cfg->stack_offset;
3088 if (method->save_lmf) {
3089 pos += sizeof (MonoLMF);
3091 /* save the current IP */
3092 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3093 x86_push_imm (code, 0);
3095 /* save all caller saved regs */
3096 x86_push_reg (code, X86_EBX);
3097 x86_push_reg (code, X86_EDI);
3098 x86_push_reg (code, X86_ESI);
3099 x86_push_reg (code, X86_EBP);
3101 /* save method info */
3102 x86_push_imm (code, method);
3104 /* get the address of lmf for the current thread */
3105 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3106 (gpointer)"mono_get_lmf_addr");
3107 x86_call_code (code, 0);
3110 x86_push_reg (code, X86_EAX);
3111 /* push *lfm (previous_lmf) */
3112 x86_push_membase (code, X86_EAX, 0);
3114 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3117 if (cfg->used_int_regs & (1 << X86_EBX)) {
3118 x86_push_reg (code, X86_EBX);
3122 if (cfg->used_int_regs & (1 << X86_EDI)) {
3123 x86_push_reg (code, X86_EDI);
3127 if (cfg->used_int_regs & (1 << X86_ESI)) {
3128 x86_push_reg (code, X86_ESI);
3136 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
3138 /* compute max_offset in order to use short forward jumps */
3140 if (cfg->opt & MONO_OPT_BRANCH) {
3141 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3142 MonoInst *ins = bb->code;
3143 bb->max_offset = max_offset;
3145 if (mono_trace_coverage)
3149 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3155 if (mono_jit_trace_calls)
3156 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
3158 /* load arguments allocated to register from the stack */
3159 sig = method->signature;
3162 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3163 inst = cfg->varinfo [pos];
3164 if (inst->opcode == OP_REGVAR) {
3165 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
3166 if (cfg->verbose_level > 2)
3167 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3172 cfg->code_len = code - cfg->native_code;
3178 mono_arch_emit_epilog (MonoCompile *cfg)
3180 MonoJumpInfo *patch_info;
3181 MonoMethod *method = cfg->method;
3185 code = cfg->native_code + cfg->code_len;
3187 if (mono_jit_trace_calls)
3188 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
3190 /* the code restoring the registers must be kept in sync with CEE_JMP */
3193 if (method->save_lmf) {
3194 pos = -sizeof (MonoLMF);
3196 if (cfg->used_int_regs & (1 << X86_EBX)) {
3199 if (cfg->used_int_regs & (1 << X86_EDI)) {
3202 if (cfg->used_int_regs & (1 << X86_ESI)) {
3208 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3210 if (method->save_lmf) {
3211 /* ebx = previous_lmf */
3212 x86_pop_reg (code, X86_EBX);
3214 x86_pop_reg (code, X86_EDI);
3215 /* *(lmf) = previous_lmf */
3216 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
3218 /* discard method info */
3219 x86_pop_reg (code, X86_ESI);
3221 /* restore caller saved regs */
3222 x86_pop_reg (code, X86_EBP);
3223 x86_pop_reg (code, X86_ESI);
3224 x86_pop_reg (code, X86_EDI);
3225 x86_pop_reg (code, X86_EBX);
3229 if (cfg->used_int_regs & (1 << X86_ESI)) {
3230 x86_pop_reg (code, X86_ESI);
3232 if (cfg->used_int_regs & (1 << X86_EDI)) {
3233 x86_pop_reg (code, X86_EDI);
3235 if (cfg->used_int_regs & (1 << X86_EBX)) {
3236 x86_pop_reg (code, X86_EBX);
3241 /* FIXME: add another check to support stdcall convention here */
3242 if (MONO_TYPE_ISSTRUCT (cfg->method->signature->ret))
3243 x86_ret_imm (code, 4);
3247 /* add code to raise exceptions */
3248 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3249 switch (patch_info->type) {
3250 case MONO_PATCH_INFO_EXC:
3251 x86_patch (patch_info->ip.i + cfg->native_code, code);
3252 x86_push_imm (code, patch_info->data.target);
3253 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
3254 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3255 patch_info->data.name = "mono_arch_throw_exception_by_name";
3256 patch_info->ip.i = code - cfg->native_code;
3257 x86_jump_code (code, 0);
3265 cfg->code_len = code - cfg->native_code;
3267 g_assert (cfg->code_len < cfg->code_size);
3272 mono_arch_flush_icache (guint8 *code, gint size)