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>
17 #include <mono/utils/mono-math.h>
21 #include "cpu-pentium.h"
23 static gint lmf_tls_offset = -1;
26 mono_arch_regname (int reg) {
28 case X86_EAX: return "%eax";
29 case X86_EBX: return "%ebx";
30 case X86_ECX: return "%ecx";
31 case X86_EDX: return "%edx";
32 case X86_ESP: return "%esp";
33 case X86_EBP: return "%ebp";
34 case X86_EDI: return "%edi";
35 case X86_ESI: return "%esi";
44 } MonoJitArgumentInfo;
47 * arch_get_argument_info:
48 * @csig: a method signature
49 * @param_count: the number of parameters to consider
50 * @arg_info: an array to store the result infos
52 * Gathers information on parameters such as size, alignment and
53 * padding. arg_info should be large enought to hold param_count + 1 entries.
55 * Returns the size of the activation frame.
58 arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
60 int k, frame_size = 0;
64 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
65 frame_size += sizeof (gpointer);
69 arg_info [0].offset = offset;
72 frame_size += sizeof (gpointer);
76 arg_info [0].size = frame_size;
78 for (k = 0; k < param_count; k++) {
81 size = mono_type_native_stack_size (csig->params [k], &align);
83 size = mono_type_stack_size (csig->params [k], &align);
85 /* ignore alignment for now */
88 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
89 arg_info [k].pad = pad;
91 arg_info [k + 1].pad = 0;
92 arg_info [k + 1].size = size;
94 arg_info [k + 1].offset = offset;
98 align = MONO_ARCH_FRAME_ALIGNMENT;
99 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
100 arg_info [k].pad = pad;
105 static int indent_level = 0;
107 static void indent (int diff) {
108 int v = indent_level;
112 indent_level += diff;
115 static gboolean enable_trace = TRUE;
118 enter_method (MonoMethod *method, char *ebp)
123 MonoJitArgumentInfo *arg_info;
124 MonoMethodSignature *sig;
130 fname = mono_method_full_name (method, TRUE);
132 printf ("ENTER: %s(", fname);
135 if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
136 g_error ("unaligned stack detected (%p)", ebp);
139 sig = method->signature;
141 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
143 arch_get_argument_info (sig, sig->param_count, arg_info);
145 if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
146 g_assert (!method->signature->ret->byref);
148 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
151 if (method->signature->hasthis) {
152 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
153 if (method->klass->valuetype) {
154 printf ("value:%p, ", *this);
156 o = *((MonoObject **)this);
159 class = o->vtable->klass;
161 if (class == mono_defaults.string_class) {
162 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
164 printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
167 printf ("this:NULL, ");
171 for (i = 0; i < method->signature->param_count; ++i) {
172 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
173 int size = arg_info [i + 1].size;
175 MonoType *type = method->signature->params [i];
178 printf ("[BYREF:%p], ", *cpos);
179 } else switch (type->type) {
183 printf ("%p, ", (gpointer)*((int *)(cpos)));
185 case MONO_TYPE_BOOLEAN:
193 printf ("%d, ", *((int *)(cpos)));
195 case MONO_TYPE_STRING: {
196 MonoString *s = *((MonoString **)cpos);
198 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
199 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
201 printf ("[STRING:null], ");
204 case MONO_TYPE_CLASS:
205 case MONO_TYPE_OBJECT: {
206 o = *((MonoObject **)cpos);
208 class = o->vtable->klass;
210 if (class == mono_defaults.string_class) {
211 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
212 } else if (class == mono_defaults.int32_class) {
213 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
215 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
217 printf ("%p, ", *((gpointer *)(cpos)));
222 case MONO_TYPE_FNPTR:
223 case MONO_TYPE_ARRAY:
224 case MONO_TYPE_SZARRAY:
225 printf ("%p, ", *((gpointer *)(cpos)));
229 printf ("0x%016llx, ", *((gint64 *)(cpos)));
232 printf ("%f, ", *((float *)(cpos)));
235 printf ("%f, ", *((double *)(cpos)));
237 case MONO_TYPE_VALUETYPE:
239 for (j = 0; j < size; j++)
240 printf ("%02x,", *((guint8*)cpos +j));
252 leave_method (MonoMethod *method, ...)
261 va_start(ap, method);
263 fname = mono_method_full_name (method, TRUE);
265 printf ("LEAVE: %s", fname);
268 type = method->signature->ret;
271 switch (type->type) {
274 case MONO_TYPE_BOOLEAN: {
275 int eax = va_arg (ap, int);
277 printf ("TRUE:%d", eax);
292 int eax = va_arg (ap, int);
293 printf ("EAX=%d", eax);
296 case MONO_TYPE_STRING: {
297 MonoString *s = va_arg (ap, MonoString *);
300 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
301 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
303 printf ("[STRING:null], ");
306 case MONO_TYPE_CLASS:
307 case MONO_TYPE_OBJECT: {
308 MonoObject *o = va_arg (ap, MonoObject *);
311 if (o->vtable->klass == mono_defaults.boolean_class) {
312 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
313 } else if (o->vtable->klass == mono_defaults.int32_class) {
314 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
315 } else if (o->vtable->klass == mono_defaults.int64_class) {
316 printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
318 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
320 printf ("[OBJECT:%p]", o);
325 case MONO_TYPE_FNPTR:
326 case MONO_TYPE_ARRAY:
327 case MONO_TYPE_SZARRAY: {
328 gpointer p = va_arg (ap, gpointer);
329 printf ("EAX=%p", p);
333 gint64 l = va_arg (ap, gint64);
334 printf ("EAX/EDX=0x%16llx", l);
338 gint64 l = va_arg (ap, gint64);
339 printf ("EAX/EDX=0x%16llx", l);
343 double f = va_arg (ap, double);
344 printf ("FP=%f\n", f);
347 case MONO_TYPE_VALUETYPE:
348 if (type->data.klass->enumtype) {
349 type = type->data.klass->enum_basetype;
352 guint8 *p = va_arg (ap, gpointer);
354 size = mono_type_size (type, &align);
356 for (j = 0; p && j < size; j++)
357 printf ("%02x,", p [j]);
362 printf ("(unknown return type %x)", method->signature->ret->type);
368 static const guchar cpuid_impl [] = {
369 0x55, /* push %ebp */
370 0x89, 0xe5, /* mov %esp,%ebp */
371 0x53, /* push %ebx */
372 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
373 0x0f, 0xa2, /* cpuid */
374 0x50, /* push %eax */
375 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
376 0x89, 0x18, /* mov %ebx,(%eax) */
377 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
378 0x89, 0x08, /* mov %ecx,(%eax) */
379 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
380 0x89, 0x10, /* mov %edx,(%eax) */
382 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
383 0x89, 0x02, /* mov %eax,(%edx) */
389 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
392 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
395 __asm__ __volatile__ (
398 "movl %%eax, %%edx\n"
399 "xorl $0x200000, %%eax\n"
404 "xorl %%edx, %%eax\n"
405 "andl $0x200000, %%eax\n"
413 CpuidFunc func = (CpuidFunc)cpuid_impl;
414 func (id, p_eax, p_ebx, p_ecx, p_edx);
416 * We use this approach because of issues with gcc and pic code, see:
417 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
418 __asm__ __volatile__ ("cpuid"
419 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
428 * Initialize the cpu to execute managed code.
431 mono_arch_cpu_init (void)
435 /* spec compliance requires running with double precision */
436 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
437 fpcw &= ~X86_FPCW_PRECC_MASK;
438 fpcw |= X86_FPCW_PREC_DOUBLE;
439 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
440 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
445 * This function returns the optimizations supported on this cpu.
448 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
450 int eax, ebx, ecx, edx;
454 /* Feature Flags function, flags returned in EDX. */
455 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
456 if (edx & (1 << 15)) {
457 opts |= MONO_OPT_CMOV;
459 opts |= MONO_OPT_FCMOV;
461 *exclude_mask |= MONO_OPT_FCMOV;
463 *exclude_mask |= MONO_OPT_CMOV;
469 is_regsize_var (MonoType *t) {
478 case MONO_TYPE_OBJECT:
479 case MONO_TYPE_STRING:
480 case MONO_TYPE_CLASS:
481 case MONO_TYPE_SZARRAY:
482 case MONO_TYPE_ARRAY:
484 case MONO_TYPE_VALUETYPE:
485 if (t->data.klass->enumtype)
486 return is_regsize_var (t->data.klass->enum_basetype);
493 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
498 for (i = 0; i < cfg->num_varinfo; i++) {
499 MonoInst *ins = cfg->varinfo [i];
500 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
503 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
506 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
507 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
510 /* we dont allocate I1 to registers because there is no simply way to sign extend
511 * 8bit quantities in caller saved registers on x86 */
512 if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) ||
513 (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
514 (ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
515 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
516 g_assert (i == vmv->idx);
517 vars = g_list_prepend (vars, vmv);
521 vars = mono_varlist_sort (cfg, vars, 0);
527 mono_arch_get_global_int_regs (MonoCompile *cfg)
531 /* we can use 3 registers for global allocation */
532 regs = g_list_prepend (regs, (gpointer)X86_EBX);
533 regs = g_list_prepend (regs, (gpointer)X86_ESI);
534 regs = g_list_prepend (regs, (gpointer)X86_EDI);
540 * Set var information according to the calling convention. X86 version.
541 * The locals var stuff should most likely be split in another method.
544 mono_arch_allocate_vars (MonoCompile *m)
546 MonoMethodSignature *sig;
547 MonoMethodHeader *header;
549 int i, offset, size, align, curinst;
551 header = ((MonoMethodNormal *)m->method)->header;
553 sig = m->method->signature;
557 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
558 m->ret->opcode = OP_REGOFFSET;
559 m->ret->inst_basereg = X86_EBP;
560 m->ret->inst_offset = offset;
561 offset += sizeof (gpointer);
563 /* FIXME: handle long and FP values */
564 switch (sig->ret->type) {
568 m->ret->opcode = OP_REGVAR;
569 m->ret->inst_c0 = X86_EAX;
574 inst = m->varinfo [curinst];
575 if (inst->opcode != OP_REGVAR) {
576 inst->opcode = OP_REGOFFSET;
577 inst->inst_basereg = X86_EBP;
579 inst->inst_offset = offset;
580 offset += sizeof (gpointer);
584 if (sig->call_convention == MONO_CALL_VARARG) {
585 m->sig_cookie = offset;
586 offset += sizeof (gpointer);
589 for (i = 0; i < sig->param_count; ++i) {
590 inst = m->varinfo [curinst];
591 if (inst->opcode != OP_REGVAR) {
592 inst->opcode = OP_REGOFFSET;
593 inst->inst_basereg = X86_EBP;
595 inst->inst_offset = offset;
596 size = mono_type_size (sig->params [i], &align);
605 /* reserve space to save LMF and caller saved registers */
607 if (m->method->save_lmf) {
608 offset += sizeof (MonoLMF);
610 if (m->used_int_regs & (1 << X86_EBX)) {
614 if (m->used_int_regs & (1 << X86_EDI)) {
618 if (m->used_int_regs & (1 << X86_ESI)) {
623 for (i = curinst; i < m->num_varinfo; ++i) {
624 inst = m->varinfo [i];
626 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
629 /* inst->unused indicates native sized value types, this is used by the
630 * pinvoke wrappers when they call functions returning structure */
631 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
632 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
634 size = mono_type_size (inst->inst_vtype, &align);
638 offset &= ~(align - 1);
639 inst->opcode = OP_REGOFFSET;
640 inst->inst_basereg = X86_EBP;
641 inst->inst_offset = -offset;
642 //g_print ("allocating local %d to %d\n", i, -offset);
644 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
645 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
648 m->stack_offset = -offset;
651 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
652 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
656 * take the arguments and generate the arch-specific
657 * instructions to properly call the function in call.
658 * This includes pushing, moving arguments to the right register
660 * Issue: who does the spilling if needed, and when?
663 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
665 MonoMethodSignature *sig;
666 int i, n, stack_size, type;
670 /* add the vararg cookie before the non-implicit args */
671 if (call->signature->call_convention == MONO_CALL_VARARG) {
673 /* FIXME: Add support for signature tokens to AOT */
674 cfg->disable_aot = TRUE;
675 MONO_INST_NEW (cfg, arg, OP_OUTARG);
676 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
677 sig_arg->inst_p0 = call->signature;
678 arg->inst_left = sig_arg;
679 arg->type = STACK_PTR;
680 /* prepend, so they get reversed */
681 arg->next = call->out_args;
682 call->out_args = arg;
683 stack_size += sizeof (gpointer);
685 sig = call->signature;
686 n = sig->param_count + sig->hasthis;
688 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
689 stack_size += sizeof (gpointer);
690 for (i = 0; i < n; ++i) {
691 if (is_virtual && i == 0) {
692 /* the argument will be attached to the call instrucion */
696 MONO_INST_NEW (cfg, arg, OP_OUTARG);
698 arg->cil_code = in->cil_code;
700 arg->type = in->type;
701 /* prepend, so they get reversed */
702 arg->next = call->out_args;
703 call->out_args = arg;
704 if (i >= sig->hasthis) {
705 ptype = sig->params [i - sig->hasthis];
711 /* FIXME: validate arguments... */
715 case MONO_TYPE_BOOLEAN:
723 case MONO_TYPE_STRING:
724 case MONO_TYPE_CLASS:
725 case MONO_TYPE_OBJECT:
727 case MONO_TYPE_FNPTR:
728 case MONO_TYPE_ARRAY:
729 case MONO_TYPE_SZARRAY:
738 arg->opcode = OP_OUTARG_R4;
742 arg->opcode = OP_OUTARG_R8;
744 case MONO_TYPE_VALUETYPE:
745 if (MONO_TYPE_ISSTRUCT (ptype)) {
748 size = mono_type_native_stack_size (&in->klass->byval_arg, NULL);
750 size = mono_type_stack_size (&in->klass->byval_arg, NULL);
753 arg->opcode = OP_OUTARG_VT;
754 arg->klass = in->klass;
755 arg->unused = sig->pinvoke;
756 arg->inst_imm = size;
758 type = ptype->data.klass->enum_basetype->type;
762 case MONO_TYPE_TYPEDBYREF:
763 stack_size += sizeof (MonoTypedRef);
764 arg->opcode = OP_OUTARG_VT;
765 arg->klass = in->klass;
766 arg->unused = sig->pinvoke;
767 arg->inst_imm = sizeof (MonoTypedRef);
769 case MONO_TYPE_GENERICINST:
770 type = ptype->data.generic_inst->generic_type->type;
774 g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
777 /* the this argument */
782 /* if the function returns a struct, the called method already does a ret $0x4 */
783 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
785 call->stack_usage = stack_size;
787 * should set more info in call, such as the stack space
788 * used by the args that needs to be added back to esp
795 * Allow tracing to work with this interface (with an optional argument)
799 * This may be needed on some archs or for debugging support.
802 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
804 /* no stack room needed now (may be needed for FASTCALL-trace support) */
806 /* split prolog-epilog requirements? */
807 *code = 50; /* max bytes needed: check this number */
811 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
815 /* if some args are passed in registers, we need to save them here */
816 x86_push_reg (code, X86_EBP);
817 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
818 x86_push_imm (code, cfg->method);
819 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
820 x86_call_code (code, 0);
821 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
835 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
838 int arg_size = 0, save_mode = SAVE_NONE;
839 MonoMethod *method = cfg->method;
840 int rtype = method->signature->ret->type;
845 /* special case string .ctor icall */
846 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
847 save_mode = SAVE_EAX;
849 save_mode = SAVE_NONE;
853 save_mode = SAVE_EAX_EDX;
859 case MONO_TYPE_VALUETYPE:
860 if (method->signature->ret->data.klass->enumtype) {
861 rtype = method->signature->ret->data.klass->enum_basetype->type;
864 save_mode = SAVE_STRUCT;
867 save_mode = SAVE_EAX;
873 x86_push_reg (code, X86_EDX);
874 x86_push_reg (code, X86_EAX);
875 if (enable_arguments) {
876 x86_push_reg (code, X86_EDX);
877 x86_push_reg (code, X86_EAX);
882 x86_push_reg (code, X86_EAX);
883 if (enable_arguments) {
884 x86_push_reg (code, X86_EAX);
889 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
890 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
891 if (enable_arguments) {
892 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
893 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
898 if (enable_arguments) {
899 x86_push_membase (code, X86_EBP, 8);
909 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
910 x86_push_imm (code, method);
911 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
912 x86_call_code (code, 0);
913 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
917 x86_pop_reg (code, X86_EAX);
918 x86_pop_reg (code, X86_EDX);
921 x86_pop_reg (code, X86_EAX);
924 x86_fld_membase (code, X86_ESP, 0, TRUE);
925 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
935 #define EMIT_COND_BRANCH(ins,cond,sign) \
936 if (ins->flags & MONO_INST_BRLABEL) { \
937 if (ins->inst_i0->inst_c0) { \
938 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
940 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
941 x86_branch32 (code, cond, 0, sign); \
944 if (ins->inst_true_bb->native_offset) { \
945 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
947 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
948 if ((cfg->opt & MONO_OPT_BRANCH) && \
949 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
950 x86_branch8 (code, cond, 0, sign); \
952 x86_branch32 (code, cond, 0, sign); \
956 /* emit an exception if condition is fail */
957 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
959 mono_add_patch_info (cfg, code - cfg->native_code, \
960 MONO_PATCH_INFO_EXC, exc_name); \
961 x86_branch32 (code, cond, 0, signed); \
964 #define EMIT_FPCOMPARE(code) do { \
970 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
972 MonoInst *ins, *last_ins = NULL;
977 switch (ins->opcode) {
979 /* reg = 0 -> XOR (reg, reg) */
980 /* XOR sets cflags on x86, so we cant do it always */
981 if (ins->inst_c0 == 0 && ins->next &&
982 (ins->next->opcode == CEE_BR)) {
983 ins->opcode = CEE_XOR;
984 ins->sreg1 = ins->dreg;
985 ins->sreg2 = ins->dreg;
989 /* remove unnecessary multiplication with 1 */
990 if (ins->inst_imm == 1) {
991 if (ins->dreg != ins->sreg1) {
992 ins->opcode = OP_MOVE;
994 last_ins->next = ins->next;
1000 case OP_COMPARE_IMM:
1001 /* OP_COMPARE_IMM (reg, 0) --> OP_X86_TEST_NULL (reg) */
1002 if (ins->inst_imm == 0 && ins->next &&
1003 (ins->next->opcode == CEE_BEQ || ins->next->opcode == CEE_BNE_UN ||
1004 ins->next->opcode == OP_CEQ)) {
1005 ins->opcode = OP_X86_TEST_NULL;
1008 case OP_LOAD_MEMBASE:
1009 case OP_LOADI4_MEMBASE:
1011 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1012 * OP_LOAD_MEMBASE offset(basereg), reg
1014 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1015 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1016 ins->inst_basereg == last_ins->inst_destbasereg &&
1017 ins->inst_offset == last_ins->inst_offset) {
1018 if (ins->dreg == last_ins->sreg1) {
1019 last_ins->next = ins->next;
1023 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1024 ins->opcode = OP_MOVE;
1025 ins->sreg1 = last_ins->sreg1;
1029 * Note: reg1 must be different from the basereg in the second load
1030 * OP_LOAD_MEMBASE offset(basereg), reg1
1031 * OP_LOAD_MEMBASE offset(basereg), reg2
1033 * OP_LOAD_MEMBASE offset(basereg), reg1
1034 * OP_MOVE reg1, reg2
1036 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1037 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1038 ins->inst_basereg != last_ins->dreg &&
1039 ins->inst_basereg == last_ins->inst_basereg &&
1040 ins->inst_offset == last_ins->inst_offset) {
1042 if (ins->dreg == last_ins->dreg) {
1043 last_ins->next = ins->next;
1047 ins->opcode = OP_MOVE;
1048 ins->sreg1 = last_ins->dreg;
1051 //g_assert_not_reached ();
1055 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1056 * OP_LOAD_MEMBASE offset(basereg), reg
1058 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1059 * OP_ICONST reg, imm
1061 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1062 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1063 ins->inst_basereg == last_ins->inst_destbasereg &&
1064 ins->inst_offset == last_ins->inst_offset) {
1065 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1066 ins->opcode = OP_ICONST;
1067 ins->inst_c0 = last_ins->inst_imm;
1068 g_assert_not_reached (); // check this rule
1072 case OP_LOADU1_MEMBASE:
1073 case OP_LOADI1_MEMBASE:
1075 * FIXME: Missing explanation
1077 if (last_ins && (last_ins->opcode == OP_STOREI1_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;
1091 case OP_LOADU2_MEMBASE:
1092 case OP_LOADI2_MEMBASE:
1094 * FIXME: Missing explanation
1096 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1097 ins->inst_basereg == last_ins->inst_destbasereg &&
1098 ins->inst_offset == last_ins->inst_offset) {
1099 if (ins->dreg == last_ins->sreg1) {
1100 last_ins->next = ins->next;
1104 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1105 ins->opcode = OP_MOVE;
1106 ins->sreg1 = last_ins->sreg1;
1116 if (ins->dreg == ins->sreg1) {
1118 last_ins->next = ins->next;
1123 * OP_MOVE sreg, dreg
1124 * OP_MOVE dreg, sreg
1126 if (last_ins && last_ins->opcode == OP_MOVE &&
1127 ins->sreg1 == last_ins->dreg &&
1128 ins->dreg == last_ins->sreg1) {
1129 last_ins->next = ins->next;
1138 bb->last_ins = last_ins;
1142 branch_cc_table [] = {
1143 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1144 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1145 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1149 * returns the offset used by spillvar. It allocates a new
1150 * spill variable if necessary.
1153 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1155 MonoSpillInfo **si, *info;
1158 si = &cfg->spill_info;
1160 while (i <= spillvar) {
1163 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1165 cfg->stack_offset -= sizeof (gpointer);
1166 info->offset = cfg->stack_offset;
1170 return (*si)->offset;
1176 g_assert_not_reached ();
1180 #define DEBUG(a) if (cfg->verbose_level > 1) a
1182 #define reg_is_freeable(r) ((r) >= 0 && (r) <= 7 && X86_IS_CALLEE ((r)))
1191 static const char*const * ins_spec = pentium_desc;
1194 print_ins (int i, MonoInst *ins)
1196 const char *spec = ins_spec [ins->opcode];
1197 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1198 if (spec [MONO_INST_DEST]) {
1199 if (ins->dreg >= MONO_MAX_IREGS)
1200 g_print (" R%d <-", ins->dreg);
1202 g_print (" %s <-", mono_arch_regname (ins->dreg));
1204 if (spec [MONO_INST_SRC1]) {
1205 if (ins->sreg1 >= MONO_MAX_IREGS)
1206 g_print (" R%d", ins->sreg1);
1208 g_print (" %s", mono_arch_regname (ins->sreg1));
1210 if (spec [MONO_INST_SRC2]) {
1211 if (ins->sreg2 >= MONO_MAX_IREGS)
1212 g_print (" R%d", ins->sreg2);
1214 g_print (" %s", mono_arch_regname (ins->sreg2));
1216 if (spec [MONO_INST_CLOB])
1217 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1222 print_regtrack (RegTrack *t, int num)
1228 for (i = 0; i < num; ++i) {
1231 if (i >= MONO_MAX_IREGS) {
1232 g_snprintf (buf, sizeof(buf), "R%d", i);
1235 r = mono_arch_regname (i);
1236 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1240 typedef struct InstList InstList;
1248 static inline InstList*
1249 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1251 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1261 * Force the spilling of the variable in the symbolic register 'reg'.
1264 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1269 sel = cfg->rs->iassign [reg];
1270 /*i = cfg->rs->isymbolic [sel];
1271 g_assert (i == reg);*/
1273 spill = ++cfg->spill_count;
1274 cfg->rs->iassign [i] = -spill - 1;
1275 mono_regstate_free_int (cfg->rs, sel);
1276 /* we need to create a spill var and insert a load to sel after the current instruction */
1277 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1279 load->inst_basereg = X86_EBP;
1280 load->inst_offset = mono_spillvar_offset (cfg, spill);
1282 while (ins->next != item->prev->data)
1285 load->next = ins->next;
1287 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1288 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1289 g_assert (i == sel);
1295 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1300 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));
1301 /* exclude the registers in the current instruction */
1302 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1303 if (ins->sreg1 >= MONO_MAX_IREGS)
1304 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1306 regmask &= ~ (1 << ins->sreg1);
1307 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1309 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1310 if (ins->sreg2 >= MONO_MAX_IREGS)
1311 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1313 regmask &= ~ (1 << ins->sreg2);
1314 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1316 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1317 regmask &= ~ (1 << ins->dreg);
1318 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1321 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1322 g_assert (regmask); /* need at least a register we can free */
1324 /* we should track prev_use and spill the register that's farther */
1325 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1326 if (regmask & (1 << i)) {
1328 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1332 i = cfg->rs->isymbolic [sel];
1333 spill = ++cfg->spill_count;
1334 cfg->rs->iassign [i] = -spill - 1;
1335 mono_regstate_free_int (cfg->rs, sel);
1336 /* we need to create a spill var and insert a load to sel after the current instruction */
1337 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1339 load->inst_basereg = X86_EBP;
1340 load->inst_offset = mono_spillvar_offset (cfg, spill);
1342 while (ins->next != item->prev->data)
1345 load->next = ins->next;
1347 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1348 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1349 g_assert (i == sel);
1355 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1358 MONO_INST_NEW (cfg, copy, OP_MOVE);
1362 copy->next = ins->next;
1365 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1370 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1373 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1375 store->inst_destbasereg = X86_EBP;
1376 store->inst_offset = mono_spillvar_offset (cfg, spill);
1378 store->next = ins->next;
1381 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)));
1386 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1390 prev = item->next->data;
1392 while (prev->next != ins)
1394 to_insert->next = ins;
1395 prev->next = to_insert;
1397 to_insert->next = ins;
1400 * needed otherwise in the next instruction we can add an ins to the
1401 * end and that would get past this instruction.
1403 item->data = to_insert;
1408 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1410 int val = cfg->rs->iassign [sym_reg];
1414 /* the register gets spilled after this inst */
1417 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1419 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1420 cfg->rs->iassign [sym_reg] = val;
1421 /* add option to store before the instruction for src registers */
1423 create_spilled_store (cfg, spill, val, sym_reg, ins);
1425 cfg->rs->isymbolic [val] = sym_reg;
1430 /*#include "cprop.c"*/
1433 * Local register allocation.
1434 * We first scan the list of instructions and we save the liveness info of
1435 * each register (when the register is first used, when it's value is set etc.).
1436 * We also reverse the list of instructions (in the InstList list) because assigning
1437 * registers backwards allows for more tricks to be used.
1440 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1443 MonoRegState *rs = cfg->rs;
1444 int i, val, fpcount;
1445 RegTrack *reginfo, *reginfof;
1446 RegTrack *reginfo1, *reginfo2, *reginfod;
1447 InstList *tmp, *reversed = NULL;
1449 guint32 src1_mask, src2_mask, dest_mask;
1453 rs->next_vireg = bb->max_ireg;
1454 rs->next_vfreg = bb->max_freg;
1455 mono_regstate_assign (rs);
1456 reginfo = g_malloc0 (sizeof (RegTrack) * rs->next_vireg);
1457 reginfof = g_malloc0 (sizeof (RegTrack) * rs->next_vfreg);
1458 rs->ifree_mask = X86_CALLEE_REGS;
1462 /*if (cfg->opt & MONO_OPT_COPYPROP)
1463 local_copy_prop (cfg, ins);*/
1466 fpcount = 0; /* FIXME: track fp stack utilization */
1467 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1468 /* forward pass on the instructions to collect register liveness info */
1470 spec = ins_spec [ins->opcode];
1471 DEBUG (print_ins (i, ins));
1472 if (spec [MONO_INST_SRC1]) {
1473 if (spec [MONO_INST_SRC1] == 'f')
1474 reginfo1 = reginfof;
1477 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1478 reginfo1 [ins->sreg1].last_use = i;
1482 if (spec [MONO_INST_SRC2]) {
1483 if (spec [MONO_INST_SRC2] == 'f')
1484 reginfo2 = reginfof;
1487 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1488 reginfo2 [ins->sreg2].last_use = i;
1492 if (spec [MONO_INST_DEST]) {
1493 if (spec [MONO_INST_DEST] == 'f')
1494 reginfod = reginfof;
1497 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1498 reginfod [ins->dreg].killed_in = i;
1499 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1500 reginfod [ins->dreg].last_use = i;
1501 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1502 reginfod [ins->dreg].born_in = i;
1503 if (spec [MONO_INST_DEST] == 'l') {
1504 /* result in eax:edx, the virtual register is allocated sequentially */
1505 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1506 reginfod [ins->dreg + 1].last_use = i;
1507 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1508 reginfod [ins->dreg + 1].born_in = i;
1513 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1518 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1519 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1522 int prev_dreg, prev_sreg1, prev_sreg2;
1523 dest_mask = src1_mask = src2_mask = X86_CALLEE_REGS;
1526 spec = ins_spec [ins->opcode];
1528 DEBUG (g_print ("processing:"));
1529 DEBUG (print_ins (i, ins));
1530 if (spec [MONO_INST_CLOB] == 's') {
1531 if (rs->ifree_mask & (1 << X86_ECX)) {
1532 DEBUG (g_print ("\tshortcut assignment of R%d to ECX\n", ins->sreg2));
1533 rs->iassign [ins->sreg2] = X86_ECX;
1534 rs->isymbolic [X86_ECX] = ins->sreg2;
1535 ins->sreg2 = X86_ECX;
1536 rs->ifree_mask &= ~ (1 << X86_ECX);
1538 int need_ecx_spill = TRUE;
1540 * we first check if src1/dreg is already assigned a register
1541 * and then we force a spill of the var assigned to ECX.
1543 /* the destination register can't be ECX */
1544 dest_mask &= ~ (1 << X86_ECX);
1545 src1_mask &= ~ (1 << X86_ECX);
1546 val = rs->iassign [ins->dreg];
1548 * the destination register is already assigned to ECX:
1549 * we need to allocate another register for it and then
1550 * copy from this to ECX.
1552 if (val == X86_ECX && ins->dreg != ins->sreg2) {
1553 int new_dest = mono_regstate_alloc_int (rs, dest_mask);
1555 new_dest = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1556 g_assert (new_dest >= 0);
1557 ins->dreg = new_dest;
1558 create_copy_ins (cfg, X86_ECX, new_dest, ins);
1559 need_ecx_spill = FALSE;
1560 /*DEBUG (g_print ("\tforced spill of R%d\n", ins->dreg));
1561 val = get_register_force_spilling (cfg, tmp, ins, ins->dreg);
1562 rs->iassign [ins->dreg] = val;
1563 rs->isymbolic [val] = prev_dreg;
1566 val = rs->iassign [ins->sreg1];
1567 if (val == X86_ECX) {
1568 g_assert_not_reached ();
1569 } else if (val >= 0) {
1571 * the first src reg was already assigned to a register,
1572 * we need to copy it to the dest register because the
1573 * shift instruction clobbers the first operand.
1575 MonoInst *copy = create_copy_ins (cfg, ins->dreg, val, NULL);
1576 insert_before_ins (ins, tmp, copy);
1578 val = rs->iassign [ins->sreg2];
1579 if (val >= 0 && val != X86_ECX) {
1580 MonoInst *move = create_copy_ins (cfg, X86_ECX, val, NULL);
1581 DEBUG (g_print ("\tmoved arg from R%d (%d) to ECX\n", val, ins->sreg2));
1583 g_assert_not_reached ();
1584 /* FIXME: where is move connected to the instruction list? */
1585 //tmp->prev->data->next = move;
1587 if (need_ecx_spill && !(rs->ifree_mask & (1 << X86_ECX))) {
1588 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_ECX]));
1589 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_ECX]);
1590 mono_regstate_free_int (rs, X86_ECX);
1592 /* force-set sreg2 */
1593 rs->iassign [ins->sreg2] = X86_ECX;
1594 rs->isymbolic [X86_ECX] = ins->sreg2;
1595 ins->sreg2 = X86_ECX;
1596 rs->ifree_mask &= ~ (1 << X86_ECX);
1598 } else if (spec [MONO_INST_CLOB] == 'd') { /* division */
1599 int dest_reg = X86_EAX;
1600 int clob_reg = X86_EDX;
1601 if (spec [MONO_INST_DEST] == 'd') {
1602 dest_reg = X86_EDX; /* reminder */
1605 val = rs->iassign [ins->dreg];
1606 if (0 && val >= 0 && val != dest_reg && !(rs->ifree_mask & (1 << dest_reg))) {
1607 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1608 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1609 mono_regstate_free_int (rs, dest_reg);
1613 /* the register gets spilled after this inst */
1614 int spill = -val -1;
1615 dest_mask = 1 << clob_reg;
1616 prev_dreg = ins->dreg;
1617 val = mono_regstate_alloc_int (rs, dest_mask);
1619 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1620 rs->iassign [ins->dreg] = val;
1622 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1623 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1624 rs->isymbolic [val] = prev_dreg;
1626 if (val != dest_reg) { /* force a copy */
1627 create_copy_ins (cfg, val, dest_reg, ins);
1630 DEBUG (g_print ("\tshortcut assignment of R%d to %s\n", ins->dreg, mono_arch_regname (dest_reg)));
1631 prev_dreg = ins->dreg;
1632 rs->iassign [ins->dreg] = dest_reg;
1633 rs->isymbolic [dest_reg] = ins->dreg;
1634 ins->dreg = dest_reg;
1635 rs->ifree_mask &= ~ (1 << dest_reg);
1638 //DEBUG (g_print ("dest reg in div assigned: %s\n", mono_arch_regname (val)));
1639 if (val != dest_reg) { /* force a copy */
1640 create_copy_ins (cfg, val, dest_reg, ins);
1641 if (!(rs->ifree_mask & (1 << dest_reg)) && rs->isymbolic [dest_reg] >= MONO_MAX_IREGS) {
1642 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1643 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1644 mono_regstate_free_int (rs, dest_reg);
1648 src1_mask = 1 << X86_EAX;
1649 src2_mask = 1 << X86_ECX;
1651 if (spec [MONO_INST_DEST] == 'l') {
1652 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1653 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1654 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1655 mono_regstate_free_int (rs, X86_EAX);
1657 if (!(rs->ifree_mask & (1 << X86_EDX))) {
1658 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EDX]));
1659 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EDX]);
1660 mono_regstate_free_int (rs, X86_EDX);
1664 /* update for use with FP regs... */
1665 if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
1666 val = rs->iassign [ins->dreg];
1667 prev_dreg = ins->dreg;
1671 /* the register gets spilled after this inst */
1674 val = mono_regstate_alloc_int (rs, dest_mask);
1676 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1677 rs->iassign [ins->dreg] = val;
1679 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1681 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1682 rs->isymbolic [val] = prev_dreg;
1684 if (spec [MONO_INST_DEST] == 'l') {
1685 int hreg = prev_dreg + 1;
1686 val = rs->iassign [hreg];
1690 /* the register gets spilled after this inst */
1693 val = mono_regstate_alloc_int (rs, dest_mask);
1695 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1696 rs->iassign [hreg] = val;
1698 create_spilled_store (cfg, spill, val, hreg, ins);
1700 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1701 rs->isymbolic [val] = hreg;
1702 /* FIXME:? ins->dreg = val; */
1703 if (ins->dreg == X86_EAX) {
1705 create_copy_ins (cfg, val, X86_EDX, ins);
1706 } else if (ins->dreg == X86_EDX) {
1707 if (val == X86_EAX) {
1709 g_assert_not_reached ();
1711 /* two forced copies */
1712 create_copy_ins (cfg, val, X86_EDX, ins);
1713 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1716 if (val == X86_EDX) {
1717 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1719 /* two forced copies */
1720 create_copy_ins (cfg, val, X86_EDX, ins);
1721 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1724 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1725 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1726 mono_regstate_free_int (rs, val);
1728 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != X86_EAX && spec [MONO_INST_CLOB] != 'd') {
1729 /* this instruction only outputs to EAX, need to copy */
1730 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1731 } else if (spec [MONO_INST_DEST] == 'd' && ins->dreg != X86_EDX && spec [MONO_INST_CLOB] != 'd') {
1732 create_copy_ins (cfg, ins->dreg, X86_EDX, ins);
1735 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1736 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1737 mono_regstate_free_int (rs, ins->dreg);
1739 /* put src1 in EAX if it needs to be */
1740 if (spec [MONO_INST_SRC1] == 'a') {
1741 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1742 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1743 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1744 mono_regstate_free_int (rs, X86_EAX);
1746 /* force-set sreg1 */
1747 rs->iassign [ins->sreg1] = X86_EAX;
1748 rs->isymbolic [X86_EAX] = ins->sreg1;
1749 ins->sreg1 = X86_EAX;
1750 rs->ifree_mask &= ~ (1 << X86_EAX);
1752 if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
1753 val = rs->iassign [ins->sreg1];
1754 prev_sreg1 = ins->sreg1;
1758 /* the register gets spilled after this inst */
1761 if (0 && ins->opcode == OP_MOVE) {
1763 * small optimization: the dest register is already allocated
1764 * but the src one is not: we can simply assign the same register
1765 * here and peephole will get rid of the instruction later.
1766 * This optimization may interfere with the clobbering handling:
1767 * it removes a mov operation that will be added again to handle clobbering.
1768 * There are also some other issues that should with make testjit.
1770 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1771 val = rs->iassign [ins->sreg1] = ins->dreg;
1772 //g_assert (val >= 0);
1773 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1775 //g_assert (val == -1); /* source cannot be spilled */
1776 val = mono_regstate_alloc_int (rs, src1_mask);
1778 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1779 rs->iassign [ins->sreg1] = val;
1780 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1783 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1784 insert_before_ins (ins, tmp, store);
1787 rs->isymbolic [val] = prev_sreg1;
1792 /* handle clobbering of sreg1 */
1793 if ((spec [MONO_INST_CLOB] == '1' || spec [MONO_INST_CLOB] == 's') && ins->dreg != ins->sreg1) {
1794 MonoInst *copy = create_copy_ins (cfg, ins->dreg, ins->sreg1, NULL);
1795 DEBUG (g_print ("\tneed to copy sreg1 %s to dreg %s\n", mono_arch_regname (ins->sreg1), mono_arch_regname (ins->dreg)));
1796 if (ins->sreg2 == -1 || spec [MONO_INST_CLOB] == 's') {
1797 /* note: the copy is inserted before the current instruction! */
1798 insert_before_ins (ins, tmp, copy);
1799 /* we set sreg1 to dest as well */
1800 prev_sreg1 = ins->sreg1 = ins->dreg;
1802 /* inserted after the operation */
1803 copy->next = ins->next;
1807 if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
1808 val = rs->iassign [ins->sreg2];
1809 prev_sreg2 = ins->sreg2;
1813 /* the register gets spilled after this inst */
1816 val = mono_regstate_alloc_int (rs, src2_mask);
1818 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1819 rs->iassign [ins->sreg2] = val;
1820 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1822 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1824 rs->isymbolic [val] = prev_sreg2;
1826 if (spec [MONO_INST_CLOB] == 's' && ins->sreg2 != X86_ECX) {
1827 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]));
1833 if (spec [MONO_INST_CLOB] == 'c') {
1835 guint32 clob_mask = X86_CALLEE_REGS;
1836 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1838 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1839 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1843 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1844 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1845 mono_regstate_free_int (rs, ins->sreg1);
1847 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1848 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1849 mono_regstate_free_int (rs, ins->sreg2);
1852 //DEBUG (print_ins (i, ins));
1853 /* this may result from a insert_before call */
1855 bb->code = tmp->data;
1863 static unsigned char*
1864 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1866 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1867 x86_fnstcw_membase(code, X86_ESP, 0);
1868 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1869 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1870 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1871 x86_fldcw_membase (code, X86_ESP, 2);
1873 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1874 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1875 x86_pop_reg (code, dreg);
1876 /* FIXME: need the high register
1877 * x86_pop_reg (code, dreg_high);
1880 x86_push_reg (code, X86_EAX); // SP = SP - 4
1881 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1882 x86_pop_reg (code, dreg);
1884 x86_fldcw_membase (code, X86_ESP, 0);
1885 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1888 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1890 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1894 static unsigned char*
1895 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1897 int sreg = tree->sreg1;
1898 #ifdef PLATFORM_WIN32
1903 * If requested stack size is larger than one page,
1904 * perform stack-touch operation
1907 * Generate stack probe code.
1908 * Under Windows, it is necessary to allocate one page at a time,
1909 * "touching" stack after each successful sub-allocation. This is
1910 * because of the way stack growth is implemented - there is a
1911 * guard page before the lowest stack page that is currently commited.
1912 * Stack normally grows sequentially so OS traps access to the
1913 * guard page and commits more pages when needed.
1915 x86_test_reg_imm (code, sreg, ~0xFFF);
1916 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1918 br[2] = code; /* loop */
1919 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
1920 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
1921 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
1922 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
1923 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
1924 x86_patch (br[3], br[2]);
1925 x86_test_reg_reg (code, sreg, sreg);
1926 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1927 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1929 br[1] = code; x86_jump8 (code, 0);
1931 x86_patch (br[0], code);
1932 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1933 x86_patch (br[1], code);
1934 x86_patch (br[4], code);
1935 #else /* PLATFORM_WIN32 */
1936 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1938 if (tree->flags & MONO_INST_INIT) {
1940 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1941 x86_push_reg (code, X86_EAX);
1944 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1945 x86_push_reg (code, X86_ECX);
1948 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1949 x86_push_reg (code, X86_EDI);
1953 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1954 if (sreg != X86_ECX)
1955 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1956 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1958 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1960 x86_prefix (code, X86_REP_PREFIX);
1963 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1964 x86_pop_reg (code, X86_EDI);
1965 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1966 x86_pop_reg (code, X86_ECX);
1967 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1968 x86_pop_reg (code, X86_EAX);
1973 #define REAL_PRINT_REG(text,reg) \
1974 mono_assert (reg >= 0); \
1975 x86_push_reg (code, X86_EAX); \
1976 x86_push_reg (code, X86_EDX); \
1977 x86_push_reg (code, X86_ECX); \
1978 x86_push_reg (code, reg); \
1979 x86_push_imm (code, reg); \
1980 x86_push_imm (code, text " %d %p\n"); \
1981 x86_mov_reg_imm (code, X86_EAX, printf); \
1982 x86_call_reg (code, X86_EAX); \
1983 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
1984 x86_pop_reg (code, X86_ECX); \
1985 x86_pop_reg (code, X86_EDX); \
1986 x86_pop_reg (code, X86_EAX);
1989 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1994 guint8 *code = cfg->native_code + cfg->code_len;
1995 MonoInst *last_ins = NULL;
1996 guint last_offset = 0;
1999 if (cfg->opt & MONO_OPT_PEEPHOLE)
2000 peephole_pass (cfg, bb);
2004 * various stratgies to align BBs. Using real loop detection or simply
2005 * aligning every block leads to more consistent benchmark results,
2006 * but usually slows down the code
2007 * we should do the alignment outside this function or we should adjust
2008 * bb->native offset as well or the code is effectively slowed down!
2010 /* align all blocks */
2011 // if ((pad = (cfg->code_len & (align - 1)))) {
2012 /* poor man loop start detection */
2013 // if (bb->code && bb->in_count && bb->in_bb [0]->cil_code > bb->cil_code && (pad = (cfg->code_len & (align - 1)))) {
2014 /* consider real loop detection and nesting level */
2015 // if (bb->loop_blocks && bb->nesting < 3 && (pad = (cfg->code_len & (align - 1)))) {
2016 /* consider real loop detection */
2017 if (bb->loop_blocks && (pad = (cfg->code_len & (align - 1)))) {
2019 x86_padding (code, pad);
2020 cfg->code_len += pad;
2021 bb->native_offset = cfg->code_len;
2025 if (cfg->verbose_level > 2)
2026 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2028 cpos = bb->max_offset;
2030 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2031 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2032 g_assert (!mono_compile_aot);
2035 cov->data [bb->dfn].cil_code = bb->cil_code;
2036 /* this is not thread save, but good enough */
2037 x86_inc_mem (code, &cov->data [bb->dfn].count);
2040 offset = code - cfg->native_code;
2044 offset = code - cfg->native_code;
2046 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2048 if (offset > (cfg->code_size - max_len - 16)) {
2049 cfg->code_size *= 2;
2050 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2051 code = cfg->native_code + offset;
2052 mono_jit_stats.code_reallocs++;
2055 mono_debug_record_line_number (cfg, ins, offset);
2057 switch (ins->opcode) {
2059 x86_mul_reg (code, ins->sreg2, TRUE);
2062 x86_mul_reg (code, ins->sreg2, FALSE);
2064 case OP_X86_SETEQ_MEMBASE:
2065 x86_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
2067 case OP_STOREI1_MEMBASE_IMM:
2068 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2070 case OP_STOREI2_MEMBASE_IMM:
2071 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2073 case OP_STORE_MEMBASE_IMM:
2074 case OP_STOREI4_MEMBASE_IMM:
2075 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2077 case OP_STOREI1_MEMBASE_REG:
2078 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2080 case OP_STOREI2_MEMBASE_REG:
2081 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2083 case OP_STORE_MEMBASE_REG:
2084 case OP_STOREI4_MEMBASE_REG:
2085 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2090 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2093 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2094 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2096 case OP_LOAD_MEMBASE:
2097 case OP_LOADI4_MEMBASE:
2098 case OP_LOADU4_MEMBASE:
2099 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2101 case OP_LOADU1_MEMBASE:
2102 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2104 case OP_LOADI1_MEMBASE:
2105 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2107 case OP_LOADU2_MEMBASE:
2108 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2110 case OP_LOADI2_MEMBASE:
2111 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2114 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2117 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2120 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2123 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2126 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2128 case OP_COMPARE_IMM:
2129 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2131 case OP_X86_COMPARE_MEMBASE_REG:
2132 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2134 case OP_X86_COMPARE_MEMBASE_IMM:
2135 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2137 case OP_X86_COMPARE_REG_MEMBASE:
2138 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2140 case OP_X86_TEST_NULL:
2141 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2143 case OP_X86_ADD_MEMBASE_IMM:
2144 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2146 case OP_X86_SUB_MEMBASE_IMM:
2147 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2149 case OP_X86_INC_MEMBASE:
2150 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2152 case OP_X86_INC_REG:
2153 x86_inc_reg (code, ins->dreg);
2155 case OP_X86_DEC_MEMBASE:
2156 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2158 case OP_X86_DEC_REG:
2159 x86_dec_reg (code, ins->dreg);
2162 x86_breakpoint (code);
2166 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2169 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2172 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2175 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2179 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2182 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2185 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2188 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2191 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2194 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2198 x86_div_reg (code, ins->sreg2, TRUE);
2201 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2202 x86_div_reg (code, ins->sreg2, FALSE);
2205 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2207 x86_div_reg (code, ins->sreg2, TRUE);
2211 x86_div_reg (code, ins->sreg2, TRUE);
2214 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2215 x86_div_reg (code, ins->sreg2, FALSE);
2218 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2220 x86_div_reg (code, ins->sreg2, TRUE);
2223 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2226 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2229 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2232 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2235 g_assert (ins->sreg2 == X86_ECX);
2236 x86_shift_reg (code, X86_SHL, ins->dreg);
2239 g_assert (ins->sreg2 == X86_ECX);
2240 x86_shift_reg (code, X86_SAR, ins->dreg);
2243 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2246 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2249 g_assert (ins->sreg2 == X86_ECX);
2250 x86_shift_reg (code, X86_SHR, ins->dreg);
2253 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2256 x86_not_reg (code, ins->sreg1);
2259 x86_neg_reg (code, ins->sreg1);
2262 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2265 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2268 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2271 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2274 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2275 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2277 case CEE_MUL_OVF_UN: {
2278 /* the mul operation and the exception check should most likely be split */
2279 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2280 /*g_assert (ins->sreg2 == X86_EAX);
2281 g_assert (ins->dreg == X86_EAX);*/
2282 if (ins->sreg2 == X86_EAX) {
2283 non_eax_reg = ins->sreg1;
2284 } else if (ins->sreg1 == X86_EAX) {
2285 non_eax_reg = ins->sreg2;
2287 /* no need to save since we're going to store to it anyway */
2288 if (ins->dreg != X86_EAX) {
2290 x86_push_reg (code, X86_EAX);
2292 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2293 non_eax_reg = ins->sreg2;
2295 if (ins->dreg == X86_EDX) {
2298 x86_push_reg (code, X86_EAX);
2300 } else if (ins->dreg != X86_EAX) {
2302 x86_push_reg (code, X86_EDX);
2304 x86_mul_reg (code, non_eax_reg, FALSE);
2305 /* save before the check since pop and mov don't change the flags */
2307 x86_pop_reg (code, X86_EDX);
2309 x86_pop_reg (code, X86_EAX);
2310 if (ins->dreg != X86_EAX)
2311 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2312 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2316 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2319 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2320 x86_mov_reg_imm (code, ins->dreg, 0);
2325 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2329 * Note: this 'frame destruction' logic is useful for tail calls, too.
2330 * Keep in sync with the code in emit_epilog.
2334 /* FIXME: no tracing support... */
2335 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2336 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2337 /* reset offset to make max_len work */
2338 offset = code - cfg->native_code;
2340 g_assert (!cfg->method->save_lmf);
2342 if (cfg->used_int_regs & (1 << X86_EBX))
2344 if (cfg->used_int_regs & (1 << X86_EDI))
2346 if (cfg->used_int_regs & (1 << X86_ESI))
2349 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2351 if (cfg->used_int_regs & (1 << X86_ESI))
2352 x86_pop_reg (code, X86_ESI);
2353 if (cfg->used_int_regs & (1 << X86_EDI))
2354 x86_pop_reg (code, X86_EDI);
2355 if (cfg->used_int_regs & (1 << X86_EBX))
2356 x86_pop_reg (code, X86_EBX);
2358 /* restore ESP/EBP */
2360 offset = code - cfg->native_code;
2361 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2362 x86_jump32 (code, 0);
2366 /* ensure ins->sreg1 is not NULL */
2367 x86_alu_membase_imm (code, X86_CMP, ins->sreg1, 0, 0);
2370 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2371 x86_push_reg (code, hreg);
2372 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2373 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2374 x86_pop_reg (code, hreg);
2382 call = (MonoCallInst*)ins;
2383 if (ins->flags & MONO_INST_HAS_METHOD)
2384 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2386 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2388 x86_call_code (code, 0);
2389 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2390 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2395 case OP_VOIDCALL_REG:
2397 call = (MonoCallInst*)ins;
2398 x86_call_reg (code, ins->sreg1);
2399 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2400 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2402 case OP_FCALL_MEMBASE:
2403 case OP_LCALL_MEMBASE:
2404 case OP_VCALL_MEMBASE:
2405 case OP_VOIDCALL_MEMBASE:
2406 case OP_CALL_MEMBASE:
2407 call = (MonoCallInst*)ins;
2408 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2409 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2410 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2414 x86_push_reg (code, ins->sreg1);
2416 case OP_X86_PUSH_IMM:
2417 x86_push_imm (code, ins->inst_imm);
2419 case OP_X86_PUSH_MEMBASE:
2420 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2422 case OP_X86_PUSH_OBJ:
2423 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2424 x86_push_reg (code, X86_EDI);
2425 x86_push_reg (code, X86_ESI);
2426 x86_push_reg (code, X86_ECX);
2427 if (ins->inst_offset)
2428 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2430 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2431 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2432 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2434 x86_prefix (code, X86_REP_PREFIX);
2436 x86_pop_reg (code, X86_ECX);
2437 x86_pop_reg (code, X86_ESI);
2438 x86_pop_reg (code, X86_EDI);
2441 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
2443 case OP_X86_LEA_MEMBASE:
2444 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
2447 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2450 /* keep alignment */
2451 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
2452 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
2453 code = mono_emit_stack_alloc (code, ins);
2454 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2460 x86_push_reg (code, ins->sreg1);
2461 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2462 (gpointer)"mono_arch_throw_exception");
2463 x86_call_code (code, 0);
2466 case OP_CALL_HANDLER:
2467 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2468 x86_call_imm (code, 0);
2471 ins->inst_c0 = code - cfg->native_code;
2474 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2475 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2477 if (ins->flags & MONO_INST_BRLABEL) {
2478 if (ins->inst_i0->inst_c0) {
2479 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2481 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2482 x86_jump32 (code, 0);
2485 if (ins->inst_target_bb->native_offset) {
2486 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2488 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2489 if ((cfg->opt & MONO_OPT_BRANCH) &&
2490 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
2491 x86_jump8 (code, 0);
2493 x86_jump32 (code, 0);
2498 x86_jump_reg (code, ins->sreg1);
2501 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2502 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2505 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
2506 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2509 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2510 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2513 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
2514 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2517 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2518 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2520 case OP_COND_EXC_EQ:
2521 case OP_COND_EXC_NE_UN:
2522 case OP_COND_EXC_LT:
2523 case OP_COND_EXC_LT_UN:
2524 case OP_COND_EXC_GT:
2525 case OP_COND_EXC_GT_UN:
2526 case OP_COND_EXC_GE:
2527 case OP_COND_EXC_GE_UN:
2528 case OP_COND_EXC_LE:
2529 case OP_COND_EXC_LE_UN:
2530 case OP_COND_EXC_OV:
2531 case OP_COND_EXC_NO:
2533 case OP_COND_EXC_NC:
2534 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2535 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2547 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
2550 /* floating point opcodes */
2552 double d = *(double *)ins->inst_p0;
2554 if ((d == 0.0) && (mono_signbit (d) == 0)) {
2556 } else if (d == 1.0) {
2559 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
2560 x86_fld (code, NULL, TRUE);
2565 float f = *(float *)ins->inst_p0;
2567 if ((f == 0.0) && (mono_signbit (f) == 0)) {
2569 } else if (f == 1.0) {
2572 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
2573 x86_fld (code, NULL, FALSE);
2577 case OP_STORER8_MEMBASE_REG:
2578 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
2580 case OP_LOADR8_MEMBASE:
2581 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2583 case OP_STORER4_MEMBASE_REG:
2584 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
2586 case OP_LOADR4_MEMBASE:
2587 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2589 case CEE_CONV_R4: /* FIXME: change precision */
2591 x86_push_reg (code, ins->sreg1);
2592 x86_fild_membase (code, X86_ESP, 0, FALSE);
2593 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2595 case OP_X86_FP_LOAD_I8:
2596 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2598 case OP_X86_FP_LOAD_I4:
2599 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2601 case OP_FCONV_TO_I1:
2602 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2604 case OP_FCONV_TO_U1:
2605 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2607 case OP_FCONV_TO_I2:
2608 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2610 case OP_FCONV_TO_U2:
2611 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2613 case OP_FCONV_TO_I4:
2615 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2617 case OP_FCONV_TO_I8:
2618 /* we defined this instruction to output only to eax:edx */
2619 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2620 x86_fnstcw_membase(code, X86_ESP, 0);
2621 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 2);
2622 x86_alu_reg_imm (code, X86_OR, X86_EAX, 0xc00);
2623 x86_mov_membase_reg (code, X86_ESP, 2, X86_EAX, 2);
2624 x86_fldcw_membase (code, X86_ESP, 2);
2625 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2626 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2627 x86_pop_reg (code, X86_EAX);
2628 x86_pop_reg (code, X86_EDX);
2629 x86_fldcw_membase (code, X86_ESP, 0);
2630 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2632 case OP_LCONV_TO_R_UN: {
2633 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2636 /* load 64bit integer to FP stack */
2637 x86_push_imm (code, 0);
2638 x86_push_reg (code, ins->sreg2);
2639 x86_push_reg (code, ins->sreg1);
2640 x86_fild_membase (code, X86_ESP, 0, TRUE);
2641 /* store as 80bit FP value */
2642 x86_fst80_membase (code, X86_ESP, 0);
2644 /* test if lreg is negative */
2645 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2646 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2648 /* add correction constant mn */
2649 x86_fld80_mem (code, mn);
2650 x86_fld80_membase (code, X86_ESP, 0);
2651 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2652 x86_fst80_membase (code, X86_ESP, 0);
2654 x86_patch (br, code);
2656 x86_fld80_membase (code, X86_ESP, 0);
2657 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2661 case OP_LCONV_TO_OVF_I: {
2662 guint8 *br [3], *label [1];
2665 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2667 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2669 /* If the low word top bit is set, see if we are negative */
2670 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2671 /* We are not negative (no top bit set, check for our top word to be zero */
2672 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2673 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2676 /* throw exception */
2677 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2678 x86_jump32 (code, 0);
2680 x86_patch (br [0], code);
2681 /* our top bit is set, check that top word is 0xfffffff */
2682 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2684 x86_patch (br [1], code);
2685 /* nope, emit exception */
2686 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2687 x86_patch (br [2], label [0]);
2689 if (ins->dreg != ins->sreg1)
2690 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2694 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2697 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
2700 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
2703 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
2719 * it really doesn't make sense to inline all this code,
2720 * it's here just to show that things may not be as simple
2723 guchar *check_pos, *end_tan, *pop_jump;
2724 x86_push_reg (code, X86_EAX);
2727 x86_test_reg_imm (code, X86_EAX, 0x400);
2729 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2730 x86_fstp (code, 0); /* pop the 1.0 */
2732 x86_jump8 (code, 0);
2734 x86_fp_op (code, X86_FADD, 0);
2738 x86_test_reg_imm (code, X86_EAX, 0x400);
2740 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2743 x86_patch (pop_jump, code);
2744 x86_fstp (code, 0); /* pop the 1.0 */
2745 x86_patch (check_pos, code);
2746 x86_patch (end_tan, code);
2747 x86_pop_reg (code, X86_EAX);
2763 x86_push_reg (code, X86_EAX);
2764 /* we need to exchange ST(0) with ST(1) */
2767 /* this requires a loop, because fprem somtimes
2768 * returns a partial remainder */
2770 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
2771 /* x86_fprem1 (code); */
2774 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x0400);
2776 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
2781 x86_pop_reg (code, X86_EAX);
2785 if (cfg->opt & MONO_OPT_FCMOV) {
2786 x86_fcomip (code, 1);
2790 /* this overwrites EAX */
2791 EMIT_FPCOMPARE(code);
2792 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2795 if (cfg->opt & MONO_OPT_FCMOV) {
2796 /* zeroing the register at the start results in
2797 * shorter and faster code (we can also remove the widening op)
2799 guchar *unordered_check;
2800 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2801 x86_fcomip (code, 1);
2803 unordered_check = code;
2804 x86_branch8 (code, X86_CC_P, 0, FALSE);
2805 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
2806 x86_patch (unordered_check, code);
2809 if (ins->dreg != X86_EAX)
2810 x86_push_reg (code, X86_EAX);
2812 EMIT_FPCOMPARE(code);
2813 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2814 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2815 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2816 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2818 if (ins->dreg != X86_EAX)
2819 x86_pop_reg (code, X86_EAX);
2823 if (cfg->opt & MONO_OPT_FCMOV) {
2824 /* zeroing the register at the start results in
2825 * shorter and faster code (we can also remove the widening op)
2827 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2828 x86_fcomip (code, 1);
2830 if (ins->opcode == OP_FCLT_UN) {
2831 guchar *unordered_check = code;
2832 guchar *jump_to_end;
2833 x86_branch8 (code, X86_CC_P, 0, FALSE);
2834 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2836 x86_jump8 (code, 0);
2837 x86_patch (unordered_check, code);
2838 x86_inc_reg (code, ins->dreg);
2839 x86_patch (jump_to_end, code);
2841 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2845 if (ins->dreg != X86_EAX)
2846 x86_push_reg (code, X86_EAX);
2848 EMIT_FPCOMPARE(code);
2849 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2850 if (ins->opcode == OP_FCLT_UN) {
2851 guchar *is_not_zero_check, *end_jump;
2852 is_not_zero_check = code;
2853 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2855 x86_jump8 (code, 0);
2856 x86_patch (is_not_zero_check, code);
2857 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2859 x86_patch (end_jump, code);
2861 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2862 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2864 if (ins->dreg != X86_EAX)
2865 x86_pop_reg (code, X86_EAX);
2869 if (cfg->opt & MONO_OPT_FCMOV) {
2870 /* zeroing the register at the start results in
2871 * shorter and faster code (we can also remove the widening op)
2873 guchar *unordered_check;
2874 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2875 x86_fcomip (code, 1);
2877 if (ins->opcode == OP_FCGT) {
2878 unordered_check = code;
2879 x86_branch8 (code, X86_CC_P, 0, FALSE);
2880 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2881 x86_patch (unordered_check, code);
2883 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2887 if (ins->dreg != X86_EAX)
2888 x86_push_reg (code, X86_EAX);
2890 EMIT_FPCOMPARE(code);
2891 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2892 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2893 if (ins->opcode == OP_FCGT_UN) {
2894 guchar *is_not_zero_check, *end_jump;
2895 is_not_zero_check = code;
2896 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2898 x86_jump8 (code, 0);
2899 x86_patch (is_not_zero_check, code);
2900 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2902 x86_patch (end_jump, code);
2904 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2905 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2907 if (ins->dreg != X86_EAX)
2908 x86_pop_reg (code, X86_EAX);
2911 if (cfg->opt & MONO_OPT_FCMOV) {
2912 guchar *jump = code;
2913 x86_branch8 (code, X86_CC_P, 0, TRUE);
2914 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2915 x86_patch (jump, code);
2918 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2919 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
2922 if (cfg->opt & MONO_OPT_FCMOV) {
2923 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2924 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2927 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2928 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2931 if (cfg->opt & MONO_OPT_FCMOV) {
2932 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2935 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2938 if (cfg->opt & MONO_OPT_FCMOV) {
2939 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2940 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2943 if (ins->opcode == OP_FBLT_UN) {
2944 guchar *is_not_zero_check, *end_jump;
2945 is_not_zero_check = code;
2946 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2948 x86_jump8 (code, 0);
2949 x86_patch (is_not_zero_check, code);
2950 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2952 x86_patch (end_jump, code);
2954 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2958 if (cfg->opt & MONO_OPT_FCMOV) {
2959 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
2962 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2963 if (ins->opcode == OP_FBGT_UN) {
2964 guchar *is_not_zero_check, *end_jump;
2965 is_not_zero_check = code;
2966 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2968 x86_jump8 (code, 0);
2969 x86_patch (is_not_zero_check, code);
2970 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2972 x86_patch (end_jump, code);
2974 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2978 if (cfg->opt & MONO_OPT_FCMOV) {
2979 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
2982 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2986 if (cfg->opt & MONO_OPT_FCMOV) {
2987 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2988 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
2991 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2992 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2994 case CEE_CKFINITE: {
2995 x86_push_reg (code, X86_EAX);
2998 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2999 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
3000 x86_pop_reg (code, X86_EAX);
3001 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
3005 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3006 g_assert_not_reached ();
3009 if ((code - cfg->native_code - offset) > max_len) {
3010 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3011 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3012 g_assert_not_reached ();
3018 last_offset = offset;
3023 cfg->code_len = code - cfg->native_code;
3027 mono_arch_register_lowlevel_calls (void)
3029 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
3030 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
3034 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3036 MonoJumpInfo *patch_info;
3038 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3039 unsigned char *ip = patch_info->ip.i + code;
3040 const unsigned char *target = NULL;
3042 switch (patch_info->type) {
3043 case MONO_PATCH_INFO_BB:
3044 target = patch_info->data.bb->native_offset + code;
3046 case MONO_PATCH_INFO_ABS:
3047 target = patch_info->data.target;
3049 case MONO_PATCH_INFO_LABEL:
3050 target = patch_info->data.inst->inst_c0 + code;
3052 case MONO_PATCH_INFO_IP:
3053 *((gpointer *)(ip)) = ip;
3055 case MONO_PATCH_INFO_METHOD_REL:
3056 *((gpointer *)(ip)) = code + patch_info->data.offset;
3058 case MONO_PATCH_INFO_INTERNAL_METHOD: {
3059 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
3061 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
3062 g_assert_not_reached ();
3064 target = mono_icall_get_wrapper (mi);
3067 case MONO_PATCH_INFO_METHOD_JUMP: {
3070 /* get the trampoline to the method from the domain */
3071 target = mono_arch_create_jump_trampoline (patch_info->data.method);
3072 if (!domain->jump_target_hash)
3073 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
3074 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
3075 list = g_slist_prepend (list, ip);
3076 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
3079 case MONO_PATCH_INFO_METHOD:
3080 if (patch_info->data.method == method) {
3083 /* get the trampoline to the method from the domain */
3084 target = mono_arch_create_jit_trampoline (patch_info->data.method);
3086 case MONO_PATCH_INFO_SWITCH: {
3087 gpointer *jump_table = mono_mempool_alloc (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
3090 *((gconstpointer *)(ip + 2)) = jump_table;
3092 for (i = 0; i < patch_info->table_size; i++) {
3093 jump_table [i] = code + (int)patch_info->data.table [i];
3095 /* we put into the table the absolute address, no need for x86_patch in this case */
3098 case MONO_PATCH_INFO_METHODCONST:
3099 case MONO_PATCH_INFO_CLASS:
3100 case MONO_PATCH_INFO_IMAGE:
3101 case MONO_PATCH_INFO_FIELD:
3102 *((gconstpointer *)(ip + 1)) = patch_info->data.target;
3104 case MONO_PATCH_INFO_IID:
3105 mono_class_init (patch_info->data.klass);
3106 *((guint32 *)(ip + 1)) = patch_info->data.klass->interface_id;
3108 case MONO_PATCH_INFO_VTABLE:
3109 *((gconstpointer *)(ip + 1)) = mono_class_vtable (domain, patch_info->data.klass);
3111 case MONO_PATCH_INFO_CLASS_INIT: {
3113 /* Might already been changed to a nop */
3114 x86_call_imm (code, 0);
3115 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
3118 case MONO_PATCH_INFO_SFLDA: {
3119 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
3120 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
3121 /* Done by the generated code */
3125 mono_runtime_class_init (vtable);
3127 *((gconstpointer *)(ip + 1)) =
3128 (char*)vtable->data + patch_info->data.field->offset;
3131 case MONO_PATCH_INFO_R4:
3132 case MONO_PATCH_INFO_R8:
3133 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3135 case MONO_PATCH_INFO_EXC_NAME:
3136 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3138 case MONO_PATCH_INFO_LDSTR:
3139 *((gconstpointer *)(ip + 1)) =
3140 mono_ldstr (domain, patch_info->data.token->image,
3141 mono_metadata_token_index (patch_info->data.token->token));
3143 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
3145 MonoClass *handle_class;
3147 handle = mono_ldtoken (patch_info->data.token->image,
3148 patch_info->data.token->token, &handle_class);
3149 mono_class_init (handle_class);
3150 mono_class_init (mono_class_from_mono_type (handle));
3152 *((gconstpointer *)(ip + 1)) =
3153 mono_type_get_object (domain, handle);
3156 case MONO_PATCH_INFO_LDTOKEN: {
3158 MonoClass *handle_class;
3160 handle = mono_ldtoken (patch_info->data.token->image,
3161 patch_info->data.token->token, &handle_class);
3162 mono_class_init (handle_class);
3164 *((gconstpointer *)(ip + 1)) = handle;
3168 g_assert_not_reached ();
3170 x86_patch (ip, target);
3175 mono_arch_max_epilog_size (MonoCompile *cfg)
3177 int exc_count = 0, max_epilog_size = 16;
3178 MonoJumpInfo *patch_info;
3180 if (cfg->method->save_lmf)
3181 max_epilog_size += 128;
3183 if (mono_jit_trace_calls != NULL)
3184 max_epilog_size += 50;
3186 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3187 max_epilog_size += 50;
3189 /* count the number of exception infos */
3191 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3192 if (patch_info->type == MONO_PATCH_INFO_EXC)
3197 * make sure we have enough space for exceptions
3198 * 16 is the size of two push_imm instructions and a call
3200 max_epilog_size += exc_count*16;
3202 return max_epilog_size;
3206 mono_arch_emit_prolog (MonoCompile *cfg)
3208 MonoMethod *method = cfg->method;
3210 MonoMethodSignature *sig;
3212 int alloc_size, pos, max_offset, i;
3215 cfg->code_size = MAX (((MonoMethodNormal *)method)->header->code_size * 4, 256);
3216 code = cfg->native_code = g_malloc (cfg->code_size);
3218 x86_push_reg (code, X86_EBP);
3219 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
3221 alloc_size = - cfg->stack_offset;
3224 if (method->save_lmf) {
3225 pos += sizeof (MonoLMF);
3227 /* save the current IP */
3228 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3229 x86_push_imm (code, 0);
3231 /* save all caller saved regs */
3232 x86_push_reg (code, X86_EBX);
3233 x86_push_reg (code, X86_EDI);
3234 x86_push_reg (code, X86_ESI);
3235 x86_push_reg (code, X86_EBP);
3237 /* save method info */
3238 x86_push_imm (code, method);
3240 /* get the address of lmf for the current thread */
3242 * This is performance critical so we try to use some tricks to make
3245 if (lmf_tls_offset != -1) {
3246 /* Load lmf quicky using the GS register */
3247 x86_prefix (code, X86_GS_PREFIX);
3248 x86_mov_reg_mem (code, X86_EAX, 0, 4);
3249 x86_mov_reg_membase (code, X86_EAX, X86_EAX, lmf_tls_offset, 4);
3252 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3253 (gpointer)"mono_get_lmf_addr");
3254 x86_call_code (code, 0);
3258 x86_push_reg (code, X86_EAX);
3259 /* push *lfm (previous_lmf) */
3260 x86_push_membase (code, X86_EAX, 0);
3262 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3265 if (cfg->used_int_regs & (1 << X86_EBX)) {
3266 x86_push_reg (code, X86_EBX);
3270 if (cfg->used_int_regs & (1 << X86_EDI)) {
3271 x86_push_reg (code, X86_EDI);
3275 if (cfg->used_int_regs & (1 << X86_ESI)) {
3276 x86_push_reg (code, X86_ESI);
3284 /* See mono_emit_stack_alloc */
3285 #ifdef PLATFORM_WIN32
3286 guint32 remaining_size = alloc_size;
3287 while (remaining_size >= 0x1000) {
3288 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
3289 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
3290 remaining_size -= 0x1000;
3293 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
3295 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
3299 /* compute max_offset in order to use short forward jumps */
3301 if (cfg->opt & MONO_OPT_BRANCH) {
3302 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3303 MonoInst *ins = bb->code;
3304 bb->max_offset = max_offset;
3306 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3310 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3316 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3317 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
3319 /* load arguments allocated to register from the stack */
3320 sig = method->signature;
3323 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3324 inst = cfg->varinfo [pos];
3325 if (inst->opcode == OP_REGVAR) {
3326 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
3327 if (cfg->verbose_level > 2)
3328 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3333 cfg->code_len = code - cfg->native_code;
3339 mono_arch_emit_epilog (MonoCompile *cfg)
3341 MonoJumpInfo *patch_info;
3342 MonoMethod *method = cfg->method;
3343 MonoMethodSignature *sig = method->signature;
3345 guint32 stack_to_pop;
3348 code = cfg->native_code + cfg->code_len;
3350 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3351 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
3353 /* the code restoring the registers must be kept in sync with CEE_JMP */
3356 if (method->save_lmf) {
3357 pos = -sizeof (MonoLMF);
3359 if (cfg->used_int_regs & (1 << X86_EBX)) {
3362 if (cfg->used_int_regs & (1 << X86_EDI)) {
3365 if (cfg->used_int_regs & (1 << X86_ESI)) {
3371 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3373 if (method->save_lmf) {
3374 /* ebx = previous_lmf */
3375 x86_pop_reg (code, X86_EBX);
3377 x86_pop_reg (code, X86_EDI);
3378 /* *(lmf) = previous_lmf */
3379 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
3381 /* discard method info */
3382 x86_pop_reg (code, X86_ESI);
3384 /* restore caller saved regs */
3385 x86_pop_reg (code, X86_EBP);
3386 x86_pop_reg (code, X86_ESI);
3387 x86_pop_reg (code, X86_EDI);
3388 x86_pop_reg (code, X86_EBX);
3392 if (cfg->used_int_regs & (1 << X86_ESI)) {
3393 x86_pop_reg (code, X86_ESI);
3395 if (cfg->used_int_regs & (1 << X86_EDI)) {
3396 x86_pop_reg (code, X86_EDI);
3398 if (cfg->used_int_regs & (1 << X86_EBX)) {
3399 x86_pop_reg (code, X86_EBX);
3405 if (sig->call_convention == MONO_CALL_STDCALL) {
3406 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
3408 stack_to_pop = arch_get_argument_info (sig, sig->param_count, arg_info);
3411 if (MONO_TYPE_ISSTRUCT (cfg->method->signature->ret))
3417 x86_ret_imm (code, stack_to_pop);
3421 /* add code to raise exceptions */
3422 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3423 switch (patch_info->type) {
3424 case MONO_PATCH_INFO_EXC:
3425 x86_patch (patch_info->ip.i + cfg->native_code, code);
3426 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);
3427 x86_push_imm (code, patch_info->data.target);
3428 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_METHOD_REL, (gpointer)patch_info->ip.i);
3429 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
3430 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3431 patch_info->data.name = "mono_arch_throw_exception_by_name";
3432 patch_info->ip.i = code - cfg->native_code;
3433 x86_jump_code (code, 0);
3441 cfg->code_len = code - cfg->native_code;
3443 g_assert (cfg->code_len < cfg->code_size);
3448 mono_arch_flush_icache (guint8 *code, gint size)
3454 * Support for fast access to the thread-local lmf structure using the GS
3455 * segment register on NPTL + kernel 2.6.x.
3458 static gboolean tls_offset_inited = FALSE;
3460 #ifdef HAVE_KW_THREAD
3461 static __thread gpointer mono_lmf_addr;
3465 mono_arch_get_lmf_addr (void)
3467 #ifdef HAVE_KW_THREAD
3468 return mono_lmf_addr;
3470 g_assert_not_reached ();
3476 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3478 if (!tls_offset_inited) {
3481 tls_offset_inited = TRUE;
3483 if (getenv ("MONO_NPTL")) {
3485 * Determine the offset of mono_lfm_addr inside the TLS structures
3486 * by disassembling the function above.
3488 code = (guint8*)&mono_arch_get_lmf_addr;
3490 /* This is generated by gcc 3.3.2 */
3491 if ((code [0] == 0x55) && (code [1] == 0x89) && (code [2] == 0xe5) &&
3492 (code [3] == 0x65) && (code [4] == 0xa1) && (code [5] == 0x00) &&
3493 (code [6] == 0x00) && (code [7] == 0x00) && (code [8] == 0x00) &&
3494 (code [9] == 0x8b) && (code [10] == 0x80)) {
3495 lmf_tls_offset = *(int*)&(code [11]);
3500 #ifdef HAVE_KW_THREAD
3501 mono_lmf_addr = &tls->lmf;
3506 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3509 /* add the this argument */
3510 if (this_reg != -1) {
3512 MONO_INST_NEW (cfg, this, OP_OUTARG);
3513 this->type = this_type;
3514 this->sreg1 = this_reg;
3515 mono_bblock_add_inst (cfg->cbb, this);
3520 MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
3521 vtarg->type = STACK_MP;
3522 vtarg->sreg1 = vt_reg;
3523 mono_bblock_add_inst (cfg->cbb, vtarg);
3529 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3531 if (cmethod->klass == mono_defaults.math_class) {
3532 if (strcmp (cmethod->name, "Sin") == 0)
3534 else if (strcmp (cmethod->name, "Cos") == 0)
3536 else if (strcmp (cmethod->name, "Tan") == 0)
3538 else if (strcmp (cmethod->name, "Atan") == 0)
3540 else if (strcmp (cmethod->name, "Sqrt") == 0)
3542 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
3545 /* OP_FREM is not IEEE compatible */
3546 else if (strcmp (cmethod->name, "IEEERemainder") == 0)