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;
116 enter_method (MonoMethod *method, char *ebp)
121 MonoJitArgumentInfo *arg_info;
122 MonoMethodSignature *sig;
125 fname = mono_method_full_name (method, TRUE);
127 printf ("ENTER: %s(", fname);
130 if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
131 g_error ("unaligned stack detected (%p)", ebp);
134 sig = method->signature;
136 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
138 arch_get_argument_info (sig, sig->param_count, arg_info);
140 if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
141 g_assert (!method->signature->ret->byref);
143 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
146 if (method->signature->hasthis) {
147 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
148 if (method->klass->valuetype) {
149 printf ("value:%p, ", *this);
151 o = *((MonoObject **)this);
154 class = o->vtable->klass;
156 if (class == mono_defaults.string_class) {
157 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
159 printf ("this:%p[%s.%s %s], ", o, class->name_space, class->name, o->vtable->domain->friendly_name);
162 printf ("this:NULL, ");
166 for (i = 0; i < method->signature->param_count; ++i) {
167 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
168 int size = arg_info [i + 1].size;
170 MonoType *type = method->signature->params [i];
173 printf ("[BYREF:%p], ", *cpos);
174 } else switch (type->type) {
178 printf ("%p, ", (gpointer)*((int *)(cpos)));
180 case MONO_TYPE_BOOLEAN:
188 printf ("%d, ", *((int *)(cpos)));
190 case MONO_TYPE_STRING: {
191 MonoString *s = *((MonoString **)cpos);
193 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
194 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
196 printf ("[STRING:null], ");
199 case MONO_TYPE_CLASS:
200 case MONO_TYPE_OBJECT: {
201 o = *((MonoObject **)cpos);
203 class = o->vtable->klass;
205 if (class == mono_defaults.string_class) {
206 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
207 } else if (class == mono_defaults.int32_class) {
208 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
210 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
212 printf ("%p, ", *((gpointer *)(cpos)));
217 case MONO_TYPE_FNPTR:
218 case MONO_TYPE_ARRAY:
219 case MONO_TYPE_SZARRAY:
220 printf ("%p, ", *((gpointer *)(cpos)));
224 printf ("0x%016llx, ", *((gint64 *)(cpos)));
227 printf ("%f, ", *((float *)(cpos)));
230 printf ("%f, ", *((double *)(cpos)));
232 case MONO_TYPE_VALUETYPE:
234 for (j = 0; j < size; j++)
235 printf ("%02x,", *((guint8*)cpos +j));
247 leave_method (MonoMethod *method, ...)
253 va_start(ap, method);
255 fname = mono_method_full_name (method, TRUE);
257 printf ("LEAVE: %s", fname);
260 type = method->signature->ret;
263 switch (type->type) {
266 case MONO_TYPE_BOOLEAN: {
267 int eax = va_arg (ap, int);
269 printf ("TRUE:%d", eax);
284 int eax = va_arg (ap, int);
285 printf ("EAX=%d", eax);
288 case MONO_TYPE_STRING: {
289 MonoString *s = va_arg (ap, MonoString *);
292 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
293 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
295 printf ("[STRING:null], ");
298 case MONO_TYPE_CLASS:
299 case MONO_TYPE_OBJECT: {
300 MonoObject *o = va_arg (ap, MonoObject *);
303 if (o->vtable->klass == mono_defaults.boolean_class) {
304 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
305 } else if (o->vtable->klass == mono_defaults.int32_class) {
306 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
307 } else if (o->vtable->klass == mono_defaults.int64_class) {
308 printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
310 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
312 printf ("[OBJECT:%p]", o);
317 case MONO_TYPE_FNPTR:
318 case MONO_TYPE_ARRAY:
319 case MONO_TYPE_SZARRAY: {
320 gpointer p = va_arg (ap, gpointer);
321 printf ("EAX=%p", p);
325 gint64 l = va_arg (ap, gint64);
326 printf ("EAX/EDX=0x%16llx", l);
330 gint64 l = va_arg (ap, gint64);
331 printf ("EAX/EDX=0x%16llx", l);
335 double f = va_arg (ap, double);
336 printf ("FP=%f\n", f);
339 case MONO_TYPE_VALUETYPE:
340 if (type->data.klass->enumtype) {
341 type = type->data.klass->enum_basetype;
344 guint8 *p = va_arg (ap, gpointer);
346 size = mono_type_size (type, &align);
348 for (j = 0; p && j < size; j++)
349 printf ("%02x,", p [j]);
354 printf ("(unknown return type %x)", method->signature->ret->type);
360 static const guchar cpuid_impl [] = {
361 0x55, /* push %ebp */
362 0x89, 0xe5, /* mov %esp,%ebp */
363 0x53, /* push %ebx */
364 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
365 0x0f, 0xa2, /* cpuid */
366 0x50, /* push %eax */
367 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
368 0x89, 0x18, /* mov %ebx,(%eax) */
369 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
370 0x89, 0x08, /* mov %ecx,(%eax) */
371 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
372 0x89, 0x10, /* mov %edx,(%eax) */
374 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
375 0x89, 0x02, /* mov %eax,(%edx) */
381 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
384 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
387 __asm__ __volatile__ (
390 "movl %%eax, %%edx\n"
391 "xorl $0x200000, %%eax\n"
396 "xorl %%edx, %%eax\n"
397 "andl $0x200000, %%eax\n"
405 CpuidFunc func = (CpuidFunc)cpuid_impl;
406 func (id, p_eax, p_ebx, p_ecx, p_edx);
408 * We use this approach because of issues with gcc and pic code, see:
409 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
410 __asm__ __volatile__ ("cpuid"
411 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
420 * Initialize the cpu to execute managed code.
423 mono_arch_cpu_init (void)
427 /* spec compliance requires running with double precision */
428 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
429 fpcw &= ~X86_FPCW_PRECC_MASK;
430 fpcw |= X86_FPCW_PREC_DOUBLE;
431 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
432 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
437 * This function returns the optimizations supported on this cpu.
440 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
442 int eax, ebx, ecx, edx;
446 /* Feature Flags function, flags returned in EDX. */
447 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
448 if (edx & (1 << 15)) {
449 opts |= MONO_OPT_CMOV;
451 opts |= MONO_OPT_FCMOV;
453 *exclude_mask |= MONO_OPT_FCMOV;
455 *exclude_mask |= MONO_OPT_CMOV;
461 is_regsize_var (MonoType *t) {
470 case MONO_TYPE_OBJECT:
471 case MONO_TYPE_STRING:
472 case MONO_TYPE_CLASS:
473 case MONO_TYPE_SZARRAY:
474 case MONO_TYPE_ARRAY:
476 case MONO_TYPE_VALUETYPE:
477 if (t->data.klass->enumtype)
478 return is_regsize_var (t->data.klass->enum_basetype);
485 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
490 for (i = 0; i < cfg->num_varinfo; i++) {
491 MonoInst *ins = cfg->varinfo [i];
492 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
495 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
498 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
499 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
502 /* we dont allocate I1 to registers because there is no simply way to sign extend
503 * 8bit quantities in caller saved registers on x86 */
504 if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) ||
505 (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
506 (ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
507 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
508 g_assert (i == vmv->idx);
509 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
517 mono_arch_get_global_int_regs (MonoCompile *cfg)
521 /* we can use 3 registers for global allocation */
522 regs = g_list_prepend (regs, (gpointer)X86_EBX);
523 regs = g_list_prepend (regs, (gpointer)X86_ESI);
524 regs = g_list_prepend (regs, (gpointer)X86_EDI);
530 * Set var information according to the calling convention. X86 version.
531 * The locals var stuff should most likely be split in another method.
534 mono_arch_allocate_vars (MonoCompile *m)
536 MonoMethodSignature *sig;
537 MonoMethodHeader *header;
539 int i, offset, size, align, curinst;
541 header = ((MonoMethodNormal *)m->method)->header;
543 sig = m->method->signature;
547 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
548 m->ret->opcode = OP_REGOFFSET;
549 m->ret->inst_basereg = X86_EBP;
550 m->ret->inst_offset = offset;
551 offset += sizeof (gpointer);
553 /* FIXME: handle long and FP values */
554 switch (sig->ret->type) {
558 m->ret->opcode = OP_REGVAR;
559 m->ret->inst_c0 = X86_EAX;
564 inst = m->varinfo [curinst];
565 if (inst->opcode != OP_REGVAR) {
566 inst->opcode = OP_REGOFFSET;
567 inst->inst_basereg = X86_EBP;
569 inst->inst_offset = offset;
570 offset += sizeof (gpointer);
574 if (sig->call_convention == MONO_CALL_VARARG) {
575 m->sig_cookie = offset;
576 offset += sizeof (gpointer);
579 for (i = 0; i < sig->param_count; ++i) {
580 inst = m->varinfo [curinst];
581 if (inst->opcode != OP_REGVAR) {
582 inst->opcode = OP_REGOFFSET;
583 inst->inst_basereg = X86_EBP;
585 inst->inst_offset = offset;
586 size = mono_type_size (sig->params [i], &align);
595 /* reserve space to save LMF and caller saved registers */
597 if (m->method->save_lmf) {
598 offset += sizeof (MonoLMF);
600 if (m->used_int_regs & (1 << X86_EBX)) {
604 if (m->used_int_regs & (1 << X86_EDI)) {
608 if (m->used_int_regs & (1 << X86_ESI)) {
613 for (i = curinst; i < m->num_varinfo; ++i) {
614 inst = m->varinfo [i];
616 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
619 /* inst->unused indicates native sized value types, this is used by the
620 * pinvoke wrappers when they call functions returning structure */
621 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
622 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
624 size = mono_type_size (inst->inst_vtype, &align);
628 offset &= ~(align - 1);
629 inst->opcode = OP_REGOFFSET;
630 inst->inst_basereg = X86_EBP;
631 inst->inst_offset = -offset;
632 //g_print ("allocating local %d to %d\n", i, -offset);
634 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
635 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
638 m->stack_offset = -offset;
641 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
642 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
646 * take the arguments and generate the arch-specific
647 * instructions to properly call the function in call.
648 * This includes pushing, moving arguments to the right register
650 * Issue: who does the spilling if needed, and when?
653 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
655 MonoMethodSignature *sig;
656 int i, n, stack_size, type;
660 /* add the vararg cookie before the non-implicit args */
661 if (call->signature->call_convention == MONO_CALL_VARARG) {
663 MONO_INST_NEW (cfg, arg, OP_OUTARG);
664 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
665 sig_arg->inst_p0 = call->signature;
666 arg->inst_left = sig_arg;
667 arg->type = STACK_PTR;
668 /* prepend, so they get reversed */
669 arg->next = call->out_args;
670 call->out_args = arg;
671 stack_size += sizeof (gpointer);
673 sig = call->signature;
674 n = sig->param_count + sig->hasthis;
676 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
677 stack_size += sizeof (gpointer);
678 for (i = 0; i < n; ++i) {
679 if (is_virtual && i == 0) {
680 /* the argument will be attached to the call instrucion */
684 MONO_INST_NEW (cfg, arg, OP_OUTARG);
686 arg->cil_code = in->cil_code;
688 arg->type = in->type;
689 /* prepend, so they get reversed */
690 arg->next = call->out_args;
691 call->out_args = arg;
692 if (i >= sig->hasthis) {
693 ptype = sig->params [i - sig->hasthis];
699 /* FIXME: validate arguments... */
703 case MONO_TYPE_BOOLEAN:
711 case MONO_TYPE_STRING:
712 case MONO_TYPE_CLASS:
713 case MONO_TYPE_OBJECT:
715 case MONO_TYPE_FNPTR:
716 case MONO_TYPE_ARRAY:
717 case MONO_TYPE_SZARRAY:
726 arg->opcode = OP_OUTARG_R4;
730 arg->opcode = OP_OUTARG_R8;
732 case MONO_TYPE_VALUETYPE:
733 if (MONO_TYPE_ISSTRUCT (ptype)) {
736 size = mono_type_native_stack_size (&in->klass->byval_arg, NULL);
738 size = mono_type_stack_size (&in->klass->byval_arg, NULL);
741 arg->opcode = OP_OUTARG_VT;
742 arg->klass = in->klass;
743 arg->unused = sig->pinvoke;
744 arg->inst_imm = size;
746 type = ptype->data.klass->enum_basetype->type;
750 case MONO_TYPE_TYPEDBYREF:
751 stack_size += sizeof (MonoTypedRef);
752 arg->opcode = OP_OUTARG_VT;
753 arg->klass = in->klass;
754 arg->unused = sig->pinvoke;
755 arg->inst_imm = sizeof (MonoTypedRef);
757 case MONO_TYPE_GENERICINST:
758 type = ptype->data.generic_inst->generic_type->type;
762 g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
765 /* the this argument */
770 /* if the function returns a struct, the called method already does a ret $0x4 */
771 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
773 call->stack_usage = stack_size;
775 * should set more info in call, such as the stack space
776 * used by the args that needs to be added back to esp
783 * Allow tracing to work with this interface (with an optional argument)
787 * This may be needed on some archs or for debugging support.
790 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
792 /* no stack room needed now (may be needed for FASTCALL-trace support) */
794 /* split prolog-epilog requirements? */
795 *code = 50; /* max bytes needed: check this number */
799 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
803 /* if some args are passed in registers, we need to save them here */
804 x86_push_reg (code, X86_EBP);
805 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
806 x86_push_imm (code, cfg->method);
807 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
808 x86_call_code (code, 0);
809 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
823 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
826 int arg_size = 0, save_mode = SAVE_NONE;
827 MonoMethod *method = cfg->method;
828 int rtype = method->signature->ret->type;
833 /* special case string .ctor icall */
834 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
835 save_mode = SAVE_EAX;
837 save_mode = SAVE_NONE;
841 save_mode = SAVE_EAX_EDX;
847 case MONO_TYPE_VALUETYPE:
848 if (method->signature->ret->data.klass->enumtype) {
849 rtype = method->signature->ret->data.klass->enum_basetype->type;
852 save_mode = SAVE_STRUCT;
855 save_mode = SAVE_EAX;
861 x86_push_reg (code, X86_EDX);
862 x86_push_reg (code, X86_EAX);
863 if (enable_arguments) {
864 x86_push_reg (code, X86_EDX);
865 x86_push_reg (code, X86_EAX);
870 x86_push_reg (code, X86_EAX);
871 if (enable_arguments) {
872 x86_push_reg (code, X86_EAX);
877 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
878 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
879 if (enable_arguments) {
880 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
881 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
886 if (enable_arguments) {
887 x86_push_membase (code, X86_EBP, 8);
897 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
898 x86_push_imm (code, method);
899 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
900 x86_call_code (code, 0);
901 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
905 x86_pop_reg (code, X86_EAX);
906 x86_pop_reg (code, X86_EDX);
909 x86_pop_reg (code, X86_EAX);
912 x86_fld_membase (code, X86_ESP, 0, TRUE);
913 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
923 #define EMIT_COND_BRANCH(ins,cond,sign) \
924 if (ins->flags & MONO_INST_BRLABEL) { \
925 if (ins->inst_i0->inst_c0) { \
926 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
928 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
929 x86_branch32 (code, cond, 0, sign); \
932 if (ins->inst_true_bb->native_offset) { \
933 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
935 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
936 if ((cfg->opt & MONO_OPT_BRANCH) && \
937 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
938 x86_branch8 (code, cond, 0, sign); \
940 x86_branch32 (code, cond, 0, sign); \
944 /* emit an exception if condition is fail */
945 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
947 mono_add_patch_info (cfg, code - cfg->native_code, \
948 MONO_PATCH_INFO_EXC, exc_name); \
949 x86_branch32 (code, cond, 0, signed); \
952 #define EMIT_FPCOMPARE(code) do { \
958 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
960 MonoInst *ins, *last_ins = NULL;
965 switch (ins->opcode) {
967 /* reg = 0 -> XOR (reg, reg) */
968 /* XOR sets cflags on x86, so we cant do it always */
969 if (ins->inst_c0 == 0 && ins->next &&
970 (ins->next->opcode == CEE_BR)) {
971 ins->opcode = CEE_XOR;
972 ins->sreg1 = ins->dreg;
973 ins->sreg2 = ins->dreg;
977 /* remove unnecessary multiplication with 1 */
978 if (ins->inst_imm == 1) {
979 if (ins->dreg != ins->sreg1) {
980 ins->opcode = OP_MOVE;
982 last_ins->next = ins->next;
989 /* OP_COMPARE_IMM (reg, 0) --> OP_X86_TEST_NULL (reg) */
990 if (ins->inst_imm == 0 && ins->next &&
991 (ins->next->opcode == CEE_BEQ || ins->next->opcode == CEE_BNE_UN ||
992 ins->next->opcode == OP_CEQ)) {
993 ins->opcode = OP_X86_TEST_NULL;
996 case OP_LOAD_MEMBASE:
997 case OP_LOADI4_MEMBASE:
999 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1000 * OP_LOAD_MEMBASE offset(basereg), reg
1002 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1003 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1004 ins->inst_basereg == last_ins->inst_destbasereg &&
1005 ins->inst_offset == last_ins->inst_offset) {
1006 if (ins->dreg == last_ins->sreg1) {
1007 last_ins->next = ins->next;
1011 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1012 ins->opcode = OP_MOVE;
1013 ins->sreg1 = last_ins->sreg1;
1017 * Note: reg1 must be different from the basereg in the second load
1018 * OP_LOAD_MEMBASE offset(basereg), reg1
1019 * OP_LOAD_MEMBASE offset(basereg), reg2
1021 * OP_LOAD_MEMBASE offset(basereg), reg1
1022 * OP_MOVE reg1, reg2
1024 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1025 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1026 ins->inst_basereg != last_ins->dreg &&
1027 ins->inst_basereg == last_ins->inst_basereg &&
1028 ins->inst_offset == last_ins->inst_offset) {
1030 if (ins->dreg == last_ins->dreg) {
1031 last_ins->next = ins->next;
1035 ins->opcode = OP_MOVE;
1036 ins->sreg1 = last_ins->dreg;
1039 //g_assert_not_reached ();
1043 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1044 * OP_LOAD_MEMBASE offset(basereg), reg
1046 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1047 * OP_ICONST reg, imm
1049 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1050 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1051 ins->inst_basereg == last_ins->inst_destbasereg &&
1052 ins->inst_offset == last_ins->inst_offset) {
1053 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1054 ins->opcode = OP_ICONST;
1055 ins->inst_c0 = last_ins->inst_imm;
1056 g_assert_not_reached (); // check this rule
1060 case OP_LOADU1_MEMBASE:
1061 case OP_LOADI1_MEMBASE:
1063 * FIXME: Missing explanation
1065 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1066 ins->inst_basereg == last_ins->inst_destbasereg &&
1067 ins->inst_offset == last_ins->inst_offset) {
1068 if (ins->dreg == last_ins->sreg1) {
1069 last_ins->next = ins->next;
1073 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1074 ins->opcode = OP_MOVE;
1075 ins->sreg1 = last_ins->sreg1;
1079 case OP_LOADU2_MEMBASE:
1080 case OP_LOADI2_MEMBASE:
1082 * FIXME: Missing explanation
1084 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1085 ins->inst_basereg == last_ins->inst_destbasereg &&
1086 ins->inst_offset == last_ins->inst_offset) {
1087 if (ins->dreg == last_ins->sreg1) {
1088 last_ins->next = ins->next;
1092 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1093 ins->opcode = OP_MOVE;
1094 ins->sreg1 = last_ins->sreg1;
1104 if (ins->dreg == ins->sreg1) {
1106 last_ins->next = ins->next;
1111 * OP_MOVE sreg, dreg
1112 * OP_MOVE dreg, sreg
1114 if (last_ins && last_ins->opcode == OP_MOVE &&
1115 ins->sreg1 == last_ins->dreg &&
1116 ins->dreg == last_ins->sreg1) {
1117 last_ins->next = ins->next;
1126 bb->last_ins = last_ins;
1130 branch_cc_table [] = {
1131 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1132 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1133 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1136 #define DEBUG(a) if (cfg->verbose_level > 1) a
1138 #define reg_is_freeable(r) ((r) >= 0 && (r) <= 7 && X86_IS_CALLEE ((r)))
1147 static const char*const * ins_spec = pentium_desc;
1150 print_ins (int i, MonoInst *ins)
1152 const char *spec = ins_spec [ins->opcode];
1153 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1154 if (spec [MONO_INST_DEST]) {
1155 if (ins->dreg >= MONO_MAX_IREGS)
1156 g_print (" R%d <-", ins->dreg);
1158 g_print (" %s <-", mono_arch_regname (ins->dreg));
1160 if (spec [MONO_INST_SRC1]) {
1161 if (ins->sreg1 >= MONO_MAX_IREGS)
1162 g_print (" R%d", ins->sreg1);
1164 g_print (" %s", mono_arch_regname (ins->sreg1));
1166 if (spec [MONO_INST_SRC2]) {
1167 if (ins->sreg2 >= MONO_MAX_IREGS)
1168 g_print (" R%d", ins->sreg2);
1170 g_print (" %s", mono_arch_regname (ins->sreg2));
1172 if (spec [MONO_INST_CLOB])
1173 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1178 print_regtrack (RegTrack *t, int num)
1184 for (i = 0; i < num; ++i) {
1187 if (i >= MONO_MAX_IREGS) {
1188 g_snprintf (buf, sizeof(buf), "R%d", i);
1191 r = mono_arch_regname (i);
1192 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1196 typedef struct InstList InstList;
1204 static inline InstList*
1205 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1207 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1217 * Force the spilling of the variable in the symbolic register 'reg'.
1220 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1225 sel = cfg->rs->iassign [reg];
1226 /*i = cfg->rs->isymbolic [sel];
1227 g_assert (i == reg);*/
1229 spill = ++cfg->spill_count;
1230 cfg->rs->iassign [i] = -spill - 1;
1231 mono_regstate_free_int (cfg->rs, sel);
1232 /* we need to create a spill var and insert a load to sel after the current instruction */
1233 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1235 load->inst_basereg = X86_EBP;
1236 load->inst_offset = mono_spillvar_offset (cfg, spill);
1238 while (ins->next != item->prev->data)
1241 load->next = ins->next;
1243 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1244 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1245 g_assert (i == sel);
1251 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1256 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));
1257 /* exclude the registers in the current instruction */
1258 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1259 if (ins->sreg1 >= MONO_MAX_IREGS)
1260 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1262 regmask &= ~ (1 << ins->sreg1);
1263 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1265 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1266 if (ins->sreg2 >= MONO_MAX_IREGS)
1267 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1269 regmask &= ~ (1 << ins->sreg2);
1270 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1272 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1273 regmask &= ~ (1 << ins->dreg);
1274 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1277 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1278 g_assert (regmask); /* need at least a register we can free */
1280 /* we should track prev_use and spill the register that's farther */
1281 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1282 if (regmask & (1 << i)) {
1284 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1288 i = cfg->rs->isymbolic [sel];
1289 spill = ++cfg->spill_count;
1290 cfg->rs->iassign [i] = -spill - 1;
1291 mono_regstate_free_int (cfg->rs, sel);
1292 /* we need to create a spill var and insert a load to sel after the current instruction */
1293 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1295 load->inst_basereg = X86_EBP;
1296 load->inst_offset = mono_spillvar_offset (cfg, spill);
1298 while (ins->next != item->prev->data)
1301 load->next = ins->next;
1303 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1304 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1305 g_assert (i == sel);
1311 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1314 MONO_INST_NEW (cfg, copy, OP_MOVE);
1318 copy->next = ins->next;
1321 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1326 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1329 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1331 store->inst_destbasereg = X86_EBP;
1332 store->inst_offset = mono_spillvar_offset (cfg, spill);
1334 store->next = ins->next;
1337 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)));
1342 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1346 prev = item->next->data;
1348 while (prev->next != ins)
1350 to_insert->next = ins;
1351 prev->next = to_insert;
1353 to_insert->next = ins;
1356 * needed otherwise in the next instruction we can add an ins to the
1357 * end and that would get past this instruction.
1359 item->data = to_insert;
1364 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1366 int val = cfg->rs->iassign [sym_reg];
1370 /* the register gets spilled after this inst */
1373 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1375 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1376 cfg->rs->iassign [sym_reg] = val;
1377 /* add option to store before the instruction for src registers */
1379 create_spilled_store (cfg, spill, val, sym_reg, ins);
1381 cfg->rs->isymbolic [val] = sym_reg;
1386 /*#include "cprop.c"*/
1389 * Local register allocation.
1390 * We first scan the list of instructions and we save the liveness info of
1391 * each register (when the register is first used, when it's value is set etc.).
1392 * We also reverse the list of instructions (in the InstList list) because assigning
1393 * registers backwards allows for more tricks to be used.
1396 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1399 MonoRegState *rs = cfg->rs;
1400 int i, val, fpcount;
1401 RegTrack *reginfo, *reginfof;
1402 RegTrack *reginfo1, *reginfo2, *reginfod;
1403 InstList *tmp, *reversed = NULL;
1405 guint32 src1_mask, src2_mask, dest_mask;
1409 rs->next_vireg = bb->max_ireg;
1410 rs->next_vfreg = bb->max_freg;
1411 mono_regstate_assign (rs);
1412 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1413 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1414 rs->ifree_mask = X86_CALLEE_REGS;
1418 /*if (cfg->opt & MONO_OPT_COPYPROP)
1419 local_copy_prop (cfg, ins);*/
1422 fpcount = 0; /* FIXME: track fp stack utilization */
1423 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1424 /* forward pass on the instructions to collect register liveness info */
1426 spec = ins_spec [ins->opcode];
1427 DEBUG (print_ins (i, ins));
1428 if (spec [MONO_INST_SRC1]) {
1429 if (spec [MONO_INST_SRC1] == 'f')
1430 reginfo1 = reginfof;
1433 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1434 reginfo1 [ins->sreg1].last_use = i;
1438 if (spec [MONO_INST_SRC2]) {
1439 if (spec [MONO_INST_SRC2] == 'f')
1440 reginfo2 = reginfof;
1443 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1444 reginfo2 [ins->sreg2].last_use = i;
1448 if (spec [MONO_INST_DEST]) {
1449 if (spec [MONO_INST_DEST] == 'f')
1450 reginfod = reginfof;
1453 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1454 reginfod [ins->dreg].killed_in = i;
1455 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1456 reginfod [ins->dreg].last_use = i;
1457 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1458 reginfod [ins->dreg].born_in = i;
1459 if (spec [MONO_INST_DEST] == 'l') {
1460 /* result in eax:edx, the virtual register is allocated sequentially */
1461 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1462 reginfod [ins->dreg + 1].last_use = i;
1463 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1464 reginfod [ins->dreg + 1].born_in = i;
1469 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1474 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1475 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1478 int prev_dreg, prev_sreg1, prev_sreg2;
1479 dest_mask = src1_mask = src2_mask = X86_CALLEE_REGS;
1482 spec = ins_spec [ins->opcode];
1483 DEBUG (g_print ("processing:"));
1484 DEBUG (print_ins (i, ins));
1485 if (spec [MONO_INST_CLOB] == 's') {
1486 if (rs->ifree_mask & (1 << X86_ECX)) {
1487 DEBUG (g_print ("\tshortcut assignment of R%d to ECX\n", ins->sreg2));
1488 rs->iassign [ins->sreg2] = X86_ECX;
1489 rs->isymbolic [X86_ECX] = ins->sreg2;
1490 ins->sreg2 = X86_ECX;
1491 rs->ifree_mask &= ~ (1 << X86_ECX);
1493 int need_ecx_spill = TRUE;
1495 * we first check if src1/dreg is already assigned a register
1496 * and then we force a spill of the var assigned to ECX.
1498 /* the destination register can't be ECX */
1499 dest_mask &= ~ (1 << X86_ECX);
1500 src1_mask &= ~ (1 << X86_ECX);
1501 val = rs->iassign [ins->dreg];
1503 * the destination register is already assigned to ECX:
1504 * we need to allocate another register for it and then
1505 * copy from this to ECX.
1507 if (val == X86_ECX && ins->dreg != ins->sreg2) {
1508 int new_dest = mono_regstate_alloc_int (rs, dest_mask);
1510 new_dest = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1511 g_assert (new_dest >= 0);
1512 ins->dreg = new_dest;
1513 create_copy_ins (cfg, X86_ECX, new_dest, ins);
1514 need_ecx_spill = FALSE;
1515 /*DEBUG (g_print ("\tforced spill of R%d\n", ins->dreg));
1516 val = get_register_force_spilling (cfg, tmp, ins, ins->dreg);
1517 rs->iassign [ins->dreg] = val;
1518 rs->isymbolic [val] = prev_dreg;
1521 val = rs->iassign [ins->sreg1];
1522 if (val == X86_ECX) {
1523 g_assert_not_reached ();
1524 } else if (val >= 0) {
1526 * the first src reg was already assigned to a register,
1527 * we need to copy it to the dest register because the
1528 * shift instruction clobbers the first operand.
1530 MonoInst *copy = create_copy_ins (cfg, ins->dreg, val, NULL);
1531 insert_before_ins (ins, tmp, copy);
1533 val = rs->iassign [ins->sreg2];
1534 if (val >= 0 && val != X86_ECX) {
1535 MonoInst *move = create_copy_ins (cfg, X86_ECX, val, NULL);
1536 DEBUG (g_print ("\tmoved arg from R%d (%d) to ECX\n", val, ins->sreg2));
1538 g_assert_not_reached ();
1539 /* FIXME: where is move connected to the instruction list? */
1540 //tmp->prev->data->next = move;
1542 if (need_ecx_spill && !(rs->ifree_mask & (1 << X86_ECX))) {
1543 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_ECX]));
1544 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_ECX]);
1545 mono_regstate_free_int (rs, X86_ECX);
1547 /* force-set sreg2 */
1548 rs->iassign [ins->sreg2] = X86_ECX;
1549 rs->isymbolic [X86_ECX] = ins->sreg2;
1550 ins->sreg2 = X86_ECX;
1551 rs->ifree_mask &= ~ (1 << X86_ECX);
1553 } else if (spec [MONO_INST_CLOB] == 'd') { /* division */
1554 int dest_reg = X86_EAX;
1555 int clob_reg = X86_EDX;
1556 if (spec [MONO_INST_DEST] == 'd') {
1557 dest_reg = X86_EDX; /* reminder */
1560 val = rs->iassign [ins->dreg];
1561 if (0 && val >= 0 && val != dest_reg && !(rs->ifree_mask & (1 << dest_reg))) {
1562 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1563 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1564 mono_regstate_free_int (rs, dest_reg);
1568 /* the register gets spilled after this inst */
1569 int spill = -val -1;
1570 dest_mask = 1 << clob_reg;
1571 prev_dreg = ins->dreg;
1572 val = mono_regstate_alloc_int (rs, dest_mask);
1574 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1575 rs->iassign [ins->dreg] = val;
1577 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1578 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1579 rs->isymbolic [val] = prev_dreg;
1581 if (val != dest_reg) { /* force a copy */
1582 create_copy_ins (cfg, val, dest_reg, ins);
1585 DEBUG (g_print ("\tshortcut assignment of R%d to %s\n", ins->dreg, mono_arch_regname (dest_reg)));
1586 rs->iassign [ins->dreg] = dest_reg;
1587 rs->isymbolic [dest_reg] = ins->dreg;
1588 ins->dreg = dest_reg;
1589 rs->ifree_mask &= ~ (1 << dest_reg);
1592 //DEBUG (g_print ("dest reg in div assigned: %s\n", mono_arch_regname (val)));
1593 if (val != dest_reg) { /* force a copy */
1594 create_copy_ins (cfg, val, dest_reg, ins);
1595 if (!(rs->ifree_mask & (1 << dest_reg)) && rs->isymbolic [dest_reg] >= MONO_MAX_IREGS) {
1596 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1597 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1598 mono_regstate_free_int (rs, dest_reg);
1602 src1_mask = 1 << X86_EAX;
1603 src2_mask = 1 << X86_ECX;
1605 if (spec [MONO_INST_DEST] == 'l') {
1606 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1607 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1608 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1609 mono_regstate_free_int (rs, X86_EAX);
1611 if (!(rs->ifree_mask & (1 << X86_EDX))) {
1612 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EDX]));
1613 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EDX]);
1614 mono_regstate_free_int (rs, X86_EDX);
1618 /* update for use with FP regs... */
1619 if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
1620 val = rs->iassign [ins->dreg];
1621 prev_dreg = ins->dreg;
1625 /* the register gets spilled after this inst */
1628 val = mono_regstate_alloc_int (rs, dest_mask);
1630 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1631 rs->iassign [ins->dreg] = val;
1633 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1635 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1636 rs->isymbolic [val] = prev_dreg;
1638 if (spec [MONO_INST_DEST] == 'l') {
1639 int hreg = prev_dreg + 1;
1640 val = rs->iassign [hreg];
1644 /* the register gets spilled after this inst */
1647 val = mono_regstate_alloc_int (rs, dest_mask);
1649 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1650 rs->iassign [hreg] = val;
1652 create_spilled_store (cfg, spill, val, hreg, ins);
1654 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1655 rs->isymbolic [val] = hreg;
1656 /* FIXME:? ins->dreg = val; */
1657 if (ins->dreg == X86_EAX) {
1659 create_copy_ins (cfg, val, X86_EDX, ins);
1660 } else if (ins->dreg == X86_EDX) {
1661 if (val == X86_EAX) {
1663 g_assert_not_reached ();
1665 /* two forced copies */
1666 create_copy_ins (cfg, val, X86_EDX, ins);
1667 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1670 if (val == X86_EDX) {
1671 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1673 /* two forced copies */
1674 create_copy_ins (cfg, val, X86_EDX, ins);
1675 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1678 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1679 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1680 mono_regstate_free_int (rs, val);
1682 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != X86_EAX && spec [MONO_INST_CLOB] != 'd') {
1683 /* this instruction only outputs to EAX, need to copy */
1684 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1685 } else if (spec [MONO_INST_DEST] == 'd' && ins->dreg != X86_EDX && spec [MONO_INST_CLOB] != 'd') {
1686 create_copy_ins (cfg, ins->dreg, X86_EDX, ins);
1691 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1692 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1693 mono_regstate_free_int (rs, ins->dreg);
1695 /* put src1 in EAX if it needs to be */
1696 if (spec [MONO_INST_SRC1] == 'a') {
1697 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1698 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1699 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1700 mono_regstate_free_int (rs, X86_EAX);
1702 /* force-set sreg1 */
1703 rs->iassign [ins->sreg1] = X86_EAX;
1704 rs->isymbolic [X86_EAX] = ins->sreg1;
1705 ins->sreg1 = X86_EAX;
1706 rs->ifree_mask &= ~ (1 << X86_EAX);
1708 if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
1709 val = rs->iassign [ins->sreg1];
1710 prev_sreg1 = ins->sreg1;
1714 /* the register gets spilled after this inst */
1717 if (0 && ins->opcode == OP_MOVE) {
1719 * small optimization: the dest register is already allocated
1720 * but the src one is not: we can simply assign the same register
1721 * here and peephole will get rid of the instruction later.
1722 * This optimization may interfere with the clobbering handling:
1723 * it removes a mov operation that will be added again to handle clobbering.
1724 * There are also some other issues that should with make testjit.
1726 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1727 val = rs->iassign [ins->sreg1] = ins->dreg;
1728 //g_assert (val >= 0);
1729 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1731 //g_assert (val == -1); /* source cannot be spilled */
1732 val = mono_regstate_alloc_int (rs, src1_mask);
1734 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1735 rs->iassign [ins->sreg1] = val;
1736 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1739 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1740 insert_before_ins (ins, tmp, store);
1743 rs->isymbolic [val] = prev_sreg1;
1748 /* handle clobbering of sreg1 */
1749 if ((spec [MONO_INST_CLOB] == '1' || spec [MONO_INST_CLOB] == 's') && ins->dreg != ins->sreg1) {
1750 MonoInst *copy = create_copy_ins (cfg, ins->dreg, ins->sreg1, NULL);
1751 DEBUG (g_print ("\tneed to copy sreg1 %s to dreg %s\n", mono_arch_regname (ins->sreg1), mono_arch_regname (ins->dreg)));
1752 if (ins->sreg2 == -1 || spec [MONO_INST_CLOB] == 's') {
1753 /* note: the copy is inserted before the current instruction! */
1754 insert_before_ins (ins, tmp, copy);
1755 /* we set sreg1 to dest as well */
1756 prev_sreg1 = ins->sreg1 = ins->dreg;
1758 /* inserted after the operation */
1759 copy->next = ins->next;
1763 if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
1764 val = rs->iassign [ins->sreg2];
1765 prev_sreg2 = ins->sreg2;
1769 /* the register gets spilled after this inst */
1772 val = mono_regstate_alloc_int (rs, src2_mask);
1774 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1775 rs->iassign [ins->sreg2] = val;
1776 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1778 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1780 rs->isymbolic [val] = prev_sreg2;
1782 if (spec [MONO_INST_CLOB] == 's' && ins->sreg2 != X86_ECX) {
1783 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]));
1789 if (spec [MONO_INST_CLOB] == 'c') {
1791 guint32 clob_mask = X86_CALLEE_REGS;
1792 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1794 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1795 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1799 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1800 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1801 mono_regstate_free_int (rs, ins->sreg1);
1803 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1804 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1805 mono_regstate_free_int (rs, ins->sreg2);
1808 //DEBUG (print_ins (i, ins));
1809 /* this may result from a insert_before call */
1811 bb->code = tmp->data;
1816 static unsigned char*
1817 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1819 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1820 x86_fnstcw_membase(code, X86_ESP, 0);
1821 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1822 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1823 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1824 x86_fldcw_membase (code, X86_ESP, 2);
1826 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1827 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1828 x86_pop_reg (code, dreg);
1829 /* FIXME: need the high register
1830 * x86_pop_reg (code, dreg_high);
1833 x86_push_reg (code, X86_EAX); // SP = SP - 4
1834 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1835 x86_pop_reg (code, dreg);
1837 x86_fldcw_membase (code, X86_ESP, 0);
1838 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1841 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1843 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1847 static unsigned char*
1848 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1850 int sreg = tree->sreg1;
1851 #ifdef PLATFORM_WIN32
1856 * If requested stack size is larger than one page,
1857 * perform stack-touch operation
1860 * Generate stack probe code.
1861 * Under Windows, it is necessary to allocate one page at a time,
1862 * "touching" stack after each successful sub-allocation. This is
1863 * because of the way stack growth is implemented - there is a
1864 * guard page before the lowest stack page that is currently commited.
1865 * Stack normally grows sequentially so OS traps access to the
1866 * guard page and commits more pages when needed.
1868 x86_test_reg_imm (code, sreg, ~0xFFF);
1869 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1871 br[2] = code; /* loop */
1872 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
1873 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
1874 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
1875 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
1876 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
1877 x86_patch (br[3], br[2]);
1878 x86_test_reg_reg (code, sreg, sreg);
1879 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1880 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1882 br[1] = code; x86_jump8 (code, 0);
1884 x86_patch (br[0], code);
1885 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1886 x86_patch (br[1], code);
1887 x86_patch (br[4], code);
1888 #else /* PLATFORM_WIN32 */
1889 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1891 if (tree->flags & MONO_INST_INIT) {
1893 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1894 x86_push_reg (code, X86_EAX);
1897 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1898 x86_push_reg (code, X86_ECX);
1901 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1902 x86_push_reg (code, X86_EDI);
1906 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1907 if (sreg != X86_ECX)
1908 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1909 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1911 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1913 x86_prefix (code, X86_REP_PREFIX);
1916 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1917 x86_pop_reg (code, X86_EDI);
1918 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1919 x86_pop_reg (code, X86_ECX);
1920 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1921 x86_pop_reg (code, X86_EAX);
1926 #define REAL_PRINT_REG(text,reg) \
1927 mono_assert (reg >= 0); \
1928 x86_push_reg (code, X86_EAX); \
1929 x86_push_reg (code, X86_EDX); \
1930 x86_push_reg (code, X86_ECX); \
1931 x86_push_reg (code, reg); \
1932 x86_push_imm (code, reg); \
1933 x86_push_imm (code, text " %d %p\n"); \
1934 x86_mov_reg_imm (code, X86_EAX, printf); \
1935 x86_call_reg (code, X86_EAX); \
1936 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
1937 x86_pop_reg (code, X86_ECX); \
1938 x86_pop_reg (code, X86_EDX); \
1939 x86_pop_reg (code, X86_EAX);
1942 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1947 guint8 *code = cfg->native_code + cfg->code_len;
1948 MonoInst *last_ins = NULL;
1949 guint last_offset = 0;
1952 if (cfg->opt & MONO_OPT_PEEPHOLE)
1953 peephole_pass (cfg, bb);
1957 * various stratgies to align BBs. Using real loop detection or simply
1958 * aligning every block leads to more consistent benchmark results,
1959 * but usually slows down the code
1960 * we should do the alignment outside this function or we should adjust
1961 * bb->native offset as well or the code is effectively slowed down!
1963 /* align all blocks */
1964 // if ((pad = (cfg->code_len & (align - 1)))) {
1965 /* poor man loop start detection */
1966 // if (bb->code && bb->in_count && bb->in_bb [0]->cil_code > bb->cil_code && (pad = (cfg->code_len & (align - 1)))) {
1967 /* consider real loop detection and nesting level */
1968 // if (bb->loop_blocks && bb->nesting < 3 && (pad = (cfg->code_len & (align - 1)))) {
1969 /* consider real loop detection */
1970 if (bb->loop_blocks && (pad = (cfg->code_len & (align - 1)))) {
1972 x86_padding (code, pad);
1973 cfg->code_len += pad;
1974 bb->native_offset = cfg->code_len;
1978 if (cfg->verbose_level > 2)
1979 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1981 cpos = bb->max_offset;
1983 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1984 MonoProfileCoverageInfo *cov = cfg->coverage_info;
1985 g_assert (!mono_compile_aot);
1988 cov->data [bb->dfn].cil_code = bb->cil_code;
1989 /* this is not thread save, but good enough */
1990 x86_inc_mem (code, &cov->data [bb->dfn].count);
1993 offset = code - cfg->native_code;
1997 offset = code - cfg->native_code;
1999 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2001 if (offset > (cfg->code_size - max_len - 16)) {
2002 cfg->code_size *= 2;
2003 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2004 code = cfg->native_code + offset;
2005 mono_jit_stats.code_reallocs++;
2008 mono_debug_record_line_number (cfg, ins, offset);
2010 switch (ins->opcode) {
2012 x86_mul_reg (code, ins->sreg2, TRUE);
2015 x86_mul_reg (code, ins->sreg2, FALSE);
2017 case OP_X86_SETEQ_MEMBASE:
2018 x86_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
2020 case OP_STOREI1_MEMBASE_IMM:
2021 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2023 case OP_STOREI2_MEMBASE_IMM:
2024 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2026 case OP_STORE_MEMBASE_IMM:
2027 case OP_STOREI4_MEMBASE_IMM:
2028 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2030 case OP_STOREI1_MEMBASE_REG:
2031 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2033 case OP_STOREI2_MEMBASE_REG:
2034 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2036 case OP_STORE_MEMBASE_REG:
2037 case OP_STOREI4_MEMBASE_REG:
2038 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2043 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2046 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2047 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2049 case OP_LOAD_MEMBASE:
2050 case OP_LOADI4_MEMBASE:
2051 case OP_LOADU4_MEMBASE:
2052 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2054 case OP_LOADU1_MEMBASE:
2055 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2057 case OP_LOADI1_MEMBASE:
2058 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2060 case OP_LOADU2_MEMBASE:
2061 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2063 case OP_LOADI2_MEMBASE:
2064 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2067 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2070 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2073 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2076 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2079 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2081 case OP_COMPARE_IMM:
2082 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2084 case OP_X86_COMPARE_MEMBASE_REG:
2085 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2087 case OP_X86_COMPARE_MEMBASE_IMM:
2088 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2090 case OP_X86_COMPARE_REG_MEMBASE:
2091 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2093 case OP_X86_TEST_NULL:
2094 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2096 case OP_X86_ADD_MEMBASE_IMM:
2097 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2099 case OP_X86_SUB_MEMBASE_IMM:
2100 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2102 case OP_X86_INC_MEMBASE:
2103 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2105 case OP_X86_INC_REG:
2106 x86_inc_reg (code, ins->dreg);
2108 case OP_X86_DEC_MEMBASE:
2109 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2111 case OP_X86_DEC_REG:
2112 x86_dec_reg (code, ins->dreg);
2115 x86_breakpoint (code);
2119 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2122 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2125 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2128 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2132 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2135 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2138 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2141 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2144 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2147 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2151 x86_div_reg (code, ins->sreg2, TRUE);
2154 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2155 x86_div_reg (code, ins->sreg2, FALSE);
2158 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2160 x86_div_reg (code, ins->sreg2, TRUE);
2164 x86_div_reg (code, ins->sreg2, TRUE);
2167 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2168 x86_div_reg (code, ins->sreg2, FALSE);
2171 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2173 x86_div_reg (code, ins->sreg2, TRUE);
2176 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2179 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2182 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2185 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2188 g_assert (ins->sreg2 == X86_ECX);
2189 x86_shift_reg (code, X86_SHL, ins->dreg);
2192 g_assert (ins->sreg2 == X86_ECX);
2193 x86_shift_reg (code, X86_SAR, ins->dreg);
2196 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2199 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2202 g_assert (ins->sreg2 == X86_ECX);
2203 x86_shift_reg (code, X86_SHR, ins->dreg);
2206 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2209 x86_not_reg (code, ins->sreg1);
2212 x86_neg_reg (code, ins->sreg1);
2215 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2218 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2221 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2224 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2227 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2228 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2230 case CEE_MUL_OVF_UN: {
2231 /* the mul operation and the exception check should most likely be split */
2232 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2233 /*g_assert (ins->sreg2 == X86_EAX);
2234 g_assert (ins->dreg == X86_EAX);*/
2235 if (ins->sreg2 == X86_EAX) {
2236 non_eax_reg = ins->sreg1;
2237 } else if (ins->sreg1 == X86_EAX) {
2238 non_eax_reg = ins->sreg2;
2240 /* no need to save since we're going to store to it anyway */
2241 if (ins->dreg != X86_EAX) {
2243 x86_push_reg (code, X86_EAX);
2245 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2246 non_eax_reg = ins->sreg2;
2248 if (ins->dreg == X86_EDX) {
2251 x86_push_reg (code, X86_EAX);
2253 } else if (ins->dreg != X86_EAX) {
2255 x86_push_reg (code, X86_EDX);
2257 x86_mul_reg (code, non_eax_reg, FALSE);
2258 /* save before the check since pop and mov don't change the flags */
2260 x86_pop_reg (code, X86_EDX);
2262 x86_pop_reg (code, X86_EAX);
2263 if (ins->dreg != X86_EAX)
2264 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2265 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2269 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2272 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2273 x86_mov_reg_imm (code, ins->dreg, 0);
2278 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2282 * Note: this 'frame destruction' logic is useful for tail calls, too.
2283 * Keep in sync with the code in emit_epilog.
2287 /* FIXME: no tracing support... */
2288 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2289 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2290 /* reset offset to make max_len work */
2291 offset = code - cfg->native_code;
2293 g_assert (!cfg->method->save_lmf);
2295 if (cfg->used_int_regs & (1 << X86_EBX))
2297 if (cfg->used_int_regs & (1 << X86_EDI))
2299 if (cfg->used_int_regs & (1 << X86_ESI))
2302 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2304 if (cfg->used_int_regs & (1 << X86_ESI))
2305 x86_pop_reg (code, X86_ESI);
2306 if (cfg->used_int_regs & (1 << X86_EDI))
2307 x86_pop_reg (code, X86_EDI);
2308 if (cfg->used_int_regs & (1 << X86_EBX))
2309 x86_pop_reg (code, X86_EBX);
2311 /* restore ESP/EBP */
2313 offset = code - cfg->native_code;
2314 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2315 x86_jump32 (code, 0);
2319 /* ensure ins->sreg1 is not NULL */
2320 x86_alu_membase_imm (code, X86_CMP, ins->sreg1, 0, 0);
2323 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2324 x86_push_reg (code, hreg);
2325 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2326 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2327 x86_pop_reg (code, hreg);
2335 call = (MonoCallInst*)ins;
2336 if (ins->flags & MONO_INST_HAS_METHOD)
2337 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2339 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2341 x86_call_code (code, 0);
2342 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2343 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2348 case OP_VOIDCALL_REG:
2350 call = (MonoCallInst*)ins;
2351 x86_call_reg (code, ins->sreg1);
2352 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2353 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2355 case OP_FCALL_MEMBASE:
2356 case OP_LCALL_MEMBASE:
2357 case OP_VCALL_MEMBASE:
2358 case OP_VOIDCALL_MEMBASE:
2359 case OP_CALL_MEMBASE:
2360 call = (MonoCallInst*)ins;
2361 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2362 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2363 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2367 x86_push_reg (code, ins->sreg1);
2369 case OP_X86_PUSH_IMM:
2370 x86_push_imm (code, ins->inst_imm);
2372 case OP_X86_PUSH_MEMBASE:
2373 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2375 case OP_X86_PUSH_OBJ:
2376 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2377 x86_push_reg (code, X86_EDI);
2378 x86_push_reg (code, X86_ESI);
2379 x86_push_reg (code, X86_ECX);
2380 if (ins->inst_offset)
2381 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2383 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2384 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2385 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2387 x86_prefix (code, X86_REP_PREFIX);
2389 x86_pop_reg (code, X86_ECX);
2390 x86_pop_reg (code, X86_ESI);
2391 x86_pop_reg (code, X86_EDI);
2394 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
2396 case OP_X86_LEA_MEMBASE:
2397 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
2400 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2403 /* keep alignment */
2404 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
2405 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
2406 code = mono_emit_stack_alloc (code, ins);
2407 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2413 x86_push_reg (code, ins->sreg1);
2414 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2415 (gpointer)"mono_arch_throw_exception");
2416 x86_call_code (code, 0);
2419 case OP_CALL_HANDLER:
2420 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2421 x86_call_imm (code, 0);
2424 ins->inst_c0 = code - cfg->native_code;
2427 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2428 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2430 if (ins->flags & MONO_INST_BRLABEL) {
2431 if (ins->inst_i0->inst_c0) {
2432 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2434 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2435 x86_jump32 (code, 0);
2438 if (ins->inst_target_bb->native_offset) {
2439 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2441 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2442 if ((cfg->opt & MONO_OPT_BRANCH) &&
2443 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
2444 x86_jump8 (code, 0);
2446 x86_jump32 (code, 0);
2451 x86_jump_reg (code, ins->sreg1);
2454 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2455 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2458 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
2459 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2462 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2463 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2466 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
2467 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2470 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2471 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2473 case OP_COND_EXC_EQ:
2474 case OP_COND_EXC_NE_UN:
2475 case OP_COND_EXC_LT:
2476 case OP_COND_EXC_LT_UN:
2477 case OP_COND_EXC_GT:
2478 case OP_COND_EXC_GT_UN:
2479 case OP_COND_EXC_GE:
2480 case OP_COND_EXC_GE_UN:
2481 case OP_COND_EXC_LE:
2482 case OP_COND_EXC_LE_UN:
2483 case OP_COND_EXC_OV:
2484 case OP_COND_EXC_NO:
2486 case OP_COND_EXC_NC:
2487 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2488 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2500 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
2503 /* floating point opcodes */
2505 double d = *(double *)ins->inst_p0;
2507 if ((d == 0.0) && (mono_signbit (d) == 0)) {
2509 } else if (d == 1.0) {
2512 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
2513 x86_fld (code, NULL, TRUE);
2518 float f = *(float *)ins->inst_p0;
2520 if ((f == 0.0) && (mono_signbit (f) == 0)) {
2522 } else if (f == 1.0) {
2525 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
2526 x86_fld (code, NULL, FALSE);
2530 case OP_STORER8_MEMBASE_REG:
2531 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
2533 case OP_LOADR8_MEMBASE:
2534 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2536 case OP_STORER4_MEMBASE_REG:
2537 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
2539 case OP_LOADR4_MEMBASE:
2540 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2542 case CEE_CONV_R4: /* FIXME: change precision */
2544 x86_push_reg (code, ins->sreg1);
2545 x86_fild_membase (code, X86_ESP, 0, FALSE);
2546 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2548 case OP_X86_FP_LOAD_I8:
2549 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2551 case OP_X86_FP_LOAD_I4:
2552 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2554 case OP_FCONV_TO_I1:
2555 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2557 case OP_FCONV_TO_U1:
2558 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2560 case OP_FCONV_TO_I2:
2561 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2563 case OP_FCONV_TO_U2:
2564 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2566 case OP_FCONV_TO_I4:
2568 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2570 case OP_FCONV_TO_I8:
2571 /* we defined this instruction to output only to eax:edx */
2572 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2573 x86_fnstcw_membase(code, X86_ESP, 0);
2574 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 0, 2);
2575 x86_alu_reg_imm (code, X86_OR, X86_EAX, 0xc00);
2576 x86_mov_membase_reg (code, X86_ESP, 2, X86_EAX, 2);
2577 x86_fldcw_membase (code, X86_ESP, 2);
2578 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2579 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2580 x86_pop_reg (code, X86_EAX);
2581 x86_pop_reg (code, X86_EDX);
2582 x86_fldcw_membase (code, X86_ESP, 0);
2583 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2585 case OP_LCONV_TO_R_UN: {
2586 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2589 /* load 64bit integer to FP stack */
2590 x86_push_imm (code, 0);
2591 x86_push_reg (code, ins->sreg2);
2592 x86_push_reg (code, ins->sreg1);
2593 x86_fild_membase (code, X86_ESP, 0, TRUE);
2594 /* store as 80bit FP value */
2595 x86_fst80_membase (code, X86_ESP, 0);
2597 /* test if lreg is negative */
2598 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2599 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2601 /* add correction constant mn */
2602 x86_fld80_mem (code, mn);
2603 x86_fld80_membase (code, X86_ESP, 0);
2604 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2605 x86_fst80_membase (code, X86_ESP, 0);
2607 x86_patch (br, code);
2609 x86_fld80_membase (code, X86_ESP, 0);
2610 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2614 case OP_LCONV_TO_OVF_I: {
2615 guint8 *br [3], *label [1];
2618 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2620 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2622 /* If the low word top bit is set, see if we are negative */
2623 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2624 /* We are not negative (no top bit set, check for our top word to be zero */
2625 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2626 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2629 /* throw exception */
2630 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2631 x86_jump32 (code, 0);
2633 x86_patch (br [0], code);
2634 /* our top bit is set, check that top word is 0xfffffff */
2635 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2637 x86_patch (br [1], code);
2638 /* nope, emit exception */
2639 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2640 x86_patch (br [2], label [0]);
2642 if (ins->dreg != ins->sreg1)
2643 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2647 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2650 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
2653 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
2656 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
2672 * it really doesn't make sense to inline all this code,
2673 * it's here just to show that things may not be as simple
2676 guchar *check_pos, *end_tan, *pop_jump;
2677 x86_push_reg (code, X86_EAX);
2680 x86_test_reg_imm (code, X86_EAX, 0x400);
2682 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2683 x86_fstp (code, 0); /* pop the 1.0 */
2685 x86_jump8 (code, 0);
2687 x86_fp_op (code, X86_FADD, 0);
2691 x86_test_reg_imm (code, X86_EAX, 0x400);
2693 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2696 x86_patch (pop_jump, code);
2697 x86_fstp (code, 0); /* pop the 1.0 */
2698 x86_patch (check_pos, code);
2699 x86_patch (end_tan, code);
2700 x86_pop_reg (code, X86_EAX);
2716 x86_push_reg (code, X86_EAX);
2717 /* we need to exchange ST(0) with ST(1) */
2720 /* this requires a loop, because fprem somtimes
2721 * returns a partial remainder */
2723 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
2724 /* x86_fprem1 (code); */
2727 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x0400);
2729 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
2734 x86_pop_reg (code, X86_EAX);
2738 if (cfg->opt & MONO_OPT_FCMOV) {
2739 x86_fcomip (code, 1);
2743 /* this overwrites EAX */
2744 EMIT_FPCOMPARE(code);
2745 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2748 if (cfg->opt & MONO_OPT_FCMOV) {
2749 /* zeroing the register at the start results in
2750 * shorter and faster code (we can also remove the widening op)
2752 guchar *unordered_check;
2753 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2754 x86_fcomip (code, 1);
2756 unordered_check = code;
2757 x86_branch8 (code, X86_CC_P, 0, FALSE);
2758 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
2759 x86_patch (unordered_check, code);
2762 if (ins->dreg != X86_EAX)
2763 x86_push_reg (code, X86_EAX);
2765 EMIT_FPCOMPARE(code);
2766 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2767 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2768 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2769 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2771 if (ins->dreg != X86_EAX)
2772 x86_pop_reg (code, X86_EAX);
2776 if (cfg->opt & MONO_OPT_FCMOV) {
2777 /* zeroing the register at the start results in
2778 * shorter and faster code (we can also remove the widening op)
2780 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2781 x86_fcomip (code, 1);
2783 if (ins->opcode == OP_FCLT_UN) {
2784 guchar *unordered_check = code;
2785 guchar *jump_to_end;
2786 x86_branch8 (code, X86_CC_P, 0, FALSE);
2787 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2789 x86_jump8 (code, 0);
2790 x86_patch (unordered_check, code);
2791 x86_inc_reg (code, ins->dreg);
2792 x86_patch (jump_to_end, code);
2794 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2798 if (ins->dreg != X86_EAX)
2799 x86_push_reg (code, X86_EAX);
2801 EMIT_FPCOMPARE(code);
2802 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2803 if (ins->opcode == OP_FCLT_UN) {
2804 guchar *is_not_zero_check, *end_jump;
2805 is_not_zero_check = code;
2806 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2808 x86_jump8 (code, 0);
2809 x86_patch (is_not_zero_check, code);
2810 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2812 x86_patch (end_jump, code);
2814 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2815 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2817 if (ins->dreg != X86_EAX)
2818 x86_pop_reg (code, X86_EAX);
2822 if (cfg->opt & MONO_OPT_FCMOV) {
2823 /* zeroing the register at the start results in
2824 * shorter and faster code (we can also remove the widening op)
2826 guchar *unordered_check;
2827 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2828 x86_fcomip (code, 1);
2830 if (ins->opcode == OP_FCGT) {
2831 unordered_check = code;
2832 x86_branch8 (code, X86_CC_P, 0, FALSE);
2833 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2834 x86_patch (unordered_check, code);
2836 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2840 if (ins->dreg != X86_EAX)
2841 x86_push_reg (code, X86_EAX);
2843 EMIT_FPCOMPARE(code);
2844 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500);
2845 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2846 if (ins->opcode == OP_FCGT_UN) {
2847 guchar *is_not_zero_check, *end_jump;
2848 is_not_zero_check = code;
2849 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2851 x86_jump8 (code, 0);
2852 x86_patch (is_not_zero_check, code);
2853 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2855 x86_patch (end_jump, code);
2857 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2858 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2860 if (ins->dreg != X86_EAX)
2861 x86_pop_reg (code, X86_EAX);
2864 if (cfg->opt & MONO_OPT_FCMOV) {
2865 guchar *jump = code;
2866 x86_branch8 (code, X86_CC_P, 0, TRUE);
2867 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2868 x86_patch (jump, code);
2871 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2872 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
2875 if (cfg->opt & MONO_OPT_FCMOV) {
2876 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2877 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2880 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2881 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2884 if (cfg->opt & MONO_OPT_FCMOV) {
2885 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2888 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2891 if (cfg->opt & MONO_OPT_FCMOV) {
2892 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2893 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2896 if (ins->opcode == OP_FBLT_UN) {
2897 guchar *is_not_zero_check, *end_jump;
2898 is_not_zero_check = code;
2899 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2901 x86_jump8 (code, 0);
2902 x86_patch (is_not_zero_check, code);
2903 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2905 x86_patch (end_jump, code);
2907 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2911 if (cfg->opt & MONO_OPT_FCMOV) {
2912 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
2915 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2916 if (ins->opcode == OP_FBGT_UN) {
2917 guchar *is_not_zero_check, *end_jump;
2918 is_not_zero_check = code;
2919 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2921 x86_jump8 (code, 0);
2922 x86_patch (is_not_zero_check, code);
2923 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4500);
2925 x86_patch (end_jump, code);
2927 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2931 if (cfg->opt & MONO_OPT_FCMOV) {
2932 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
2935 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2939 if (cfg->opt & MONO_OPT_FCMOV) {
2940 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2941 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
2944 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2945 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2947 case CEE_CKFINITE: {
2948 x86_push_reg (code, X86_EAX);
2951 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2952 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2953 x86_pop_reg (code, X86_EAX);
2954 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
2958 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2959 g_assert_not_reached ();
2962 if ((code - cfg->native_code - offset) > max_len) {
2963 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2964 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2965 g_assert_not_reached ();
2971 last_offset = offset;
2976 cfg->code_len = code - cfg->native_code;
2980 mono_arch_register_lowlevel_calls (void)
2982 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2983 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2987 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji)
2989 MonoJumpInfo *patch_info;
2991 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2992 unsigned char *ip = patch_info->ip.i + code;
2993 const unsigned char *target = NULL;
2995 switch (patch_info->type) {
2996 case MONO_PATCH_INFO_BB:
2997 target = patch_info->data.bb->native_offset + code;
2999 case MONO_PATCH_INFO_ABS:
3000 target = patch_info->data.target;
3002 case MONO_PATCH_INFO_LABEL:
3003 target = patch_info->data.inst->inst_c0 + code;
3005 case MONO_PATCH_INFO_IP:
3006 *((gpointer *)(ip)) = ip;
3008 case MONO_PATCH_INFO_METHOD_REL:
3009 *((gpointer *)(ip)) = code + patch_info->data.offset;
3011 case MONO_PATCH_INFO_INTERNAL_METHOD: {
3012 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
3014 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
3015 g_assert_not_reached ();
3017 target = mono_icall_get_wrapper (mi);
3020 case MONO_PATCH_INFO_METHOD_JUMP: {
3023 /* get the trampoline to the method from the domain */
3024 target = mono_arch_create_jump_trampoline (patch_info->data.method);
3025 if (!domain->jump_target_hash)
3026 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
3027 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
3028 list = g_slist_prepend (list, ip);
3029 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
3032 case MONO_PATCH_INFO_METHOD:
3033 if (patch_info->data.method == method) {
3036 /* get the trampoline to the method from the domain */
3037 target = mono_arch_create_jit_trampoline (patch_info->data.method);
3039 case MONO_PATCH_INFO_SWITCH: {
3040 gpointer *jump_table = mono_mempool_alloc (domain->code_mp, sizeof (gpointer) * patch_info->table_size);
3043 *((gconstpointer *)(ip + 2)) = jump_table;
3045 for (i = 0; i < patch_info->table_size; i++) {
3046 jump_table [i] = code + (int)patch_info->data.table [i];
3048 /* we put into the table the absolute address, no need for x86_patch in this case */
3051 case MONO_PATCH_INFO_METHODCONST:
3052 case MONO_PATCH_INFO_CLASS:
3053 case MONO_PATCH_INFO_IMAGE:
3054 case MONO_PATCH_INFO_FIELD:
3055 *((gconstpointer *)(ip + 1)) = patch_info->data.target;
3057 case MONO_PATCH_INFO_IID:
3058 mono_class_init (patch_info->data.klass);
3059 *((guint32 *)(ip + 1)) = patch_info->data.klass->interface_id;
3061 case MONO_PATCH_INFO_VTABLE:
3062 *((gconstpointer *)(ip + 1)) = mono_class_vtable (domain, patch_info->data.klass);
3064 case MONO_PATCH_INFO_CLASS_INIT: {
3066 /* Might already been changed to a nop */
3067 x86_call_imm (code, 0);
3068 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
3071 case MONO_PATCH_INFO_SFLDA: {
3072 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
3073 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
3074 /* Done by the generated code */
3077 mono_runtime_class_init (vtable);
3079 *((gconstpointer *)(ip + 1)) =
3080 (char*)vtable->data + patch_info->data.field->offset;
3083 case MONO_PATCH_INFO_R4:
3084 case MONO_PATCH_INFO_R8:
3085 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3087 case MONO_PATCH_INFO_EXC_NAME:
3088 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3090 case MONO_PATCH_INFO_LDSTR:
3091 *((gconstpointer *)(ip + 1)) =
3092 mono_ldstr (domain, patch_info->data.token->image,
3093 mono_metadata_token_index (patch_info->data.token->token));
3095 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
3097 MonoClass *handle_class;
3099 handle = mono_ldtoken (patch_info->data.token->image,
3100 patch_info->data.token->token, &handle_class);
3101 mono_class_init (handle_class);
3102 mono_class_init (mono_class_from_mono_type (handle));
3104 *((gconstpointer *)(ip + 1)) =
3105 mono_type_get_object (domain, handle);
3108 case MONO_PATCH_INFO_LDTOKEN: {
3110 MonoClass *handle_class;
3112 handle = mono_ldtoken (patch_info->data.token->image,
3113 patch_info->data.token->token, &handle_class);
3114 mono_class_init (handle_class);
3116 *((gconstpointer *)(ip + 1)) = handle;
3120 g_assert_not_reached ();
3122 x86_patch (ip, target);
3127 mono_arch_max_epilog_size (MonoCompile *cfg)
3129 int exc_count = 0, max_epilog_size = 16;
3130 MonoJumpInfo *patch_info;
3132 if (cfg->method->save_lmf)
3133 max_epilog_size += 128;
3135 if (mono_jit_trace_calls != NULL)
3136 max_epilog_size += 50;
3138 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3139 max_epilog_size += 50;
3141 /* count the number of exception infos */
3143 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3144 if (patch_info->type == MONO_PATCH_INFO_EXC)
3149 * make sure we have enough space for exceptions
3150 * 16 is the size of two push_imm instructions and a call
3152 max_epilog_size += exc_count*16;
3154 return max_epilog_size;
3158 mono_arch_emit_prolog (MonoCompile *cfg)
3160 MonoMethod *method = cfg->method;
3162 MonoMethodSignature *sig;
3164 int alloc_size, pos, max_offset, i;
3167 cfg->code_size = MAX (((MonoMethodNormal *)method)->header->code_size * 4, 256);
3168 code = cfg->native_code = g_malloc (cfg->code_size);
3170 x86_push_reg (code, X86_EBP);
3171 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
3173 alloc_size = - cfg->stack_offset;
3176 if (method->save_lmf) {
3177 pos += sizeof (MonoLMF);
3179 /* save the current IP */
3180 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3181 x86_push_imm (code, 0);
3183 /* save all caller saved regs */
3184 x86_push_reg (code, X86_EBX);
3185 x86_push_reg (code, X86_EDI);
3186 x86_push_reg (code, X86_ESI);
3187 x86_push_reg (code, X86_EBP);
3189 /* save method info */
3190 x86_push_imm (code, method);
3192 /* get the address of lmf for the current thread */
3194 * This is performance critical so we try to use some tricks to make
3197 if (lmf_tls_offset != -1) {
3198 /* Load lmf quicky using the GS register */
3199 x86_prefix (code, X86_GS_PREFIX);
3200 x86_mov_reg_mem (code, X86_EAX, 0, 4);
3201 x86_mov_reg_membase (code, X86_EAX, X86_EAX, lmf_tls_offset, 4);
3204 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3205 (gpointer)"mono_get_lmf_addr");
3206 x86_call_code (code, 0);
3210 x86_push_reg (code, X86_EAX);
3211 /* push *lfm (previous_lmf) */
3212 x86_push_membase (code, X86_EAX, 0);
3214 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3217 if (cfg->used_int_regs & (1 << X86_EBX)) {
3218 x86_push_reg (code, X86_EBX);
3222 if (cfg->used_int_regs & (1 << X86_EDI)) {
3223 x86_push_reg (code, X86_EDI);
3227 if (cfg->used_int_regs & (1 << X86_ESI)) {
3228 x86_push_reg (code, X86_ESI);
3236 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
3238 /* compute max_offset in order to use short forward jumps */
3240 if (cfg->opt & MONO_OPT_BRANCH) {
3241 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3242 MonoInst *ins = bb->code;
3243 bb->max_offset = max_offset;
3245 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3249 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3255 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3256 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
3258 /* load arguments allocated to register from the stack */
3259 sig = method->signature;
3262 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3263 inst = cfg->varinfo [pos];
3264 if (inst->opcode == OP_REGVAR) {
3265 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
3266 if (cfg->verbose_level > 2)
3267 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3272 cfg->code_len = code - cfg->native_code;
3278 mono_arch_emit_epilog (MonoCompile *cfg)
3280 MonoJumpInfo *patch_info;
3281 MonoMethod *method = cfg->method;
3282 MonoMethodSignature *sig = method->signature;
3284 guint32 stack_to_pop;
3287 code = cfg->native_code + cfg->code_len;
3289 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3290 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
3292 /* the code restoring the registers must be kept in sync with CEE_JMP */
3295 if (method->save_lmf) {
3296 pos = -sizeof (MonoLMF);
3298 if (cfg->used_int_regs & (1 << X86_EBX)) {
3301 if (cfg->used_int_regs & (1 << X86_EDI)) {
3304 if (cfg->used_int_regs & (1 << X86_ESI)) {
3310 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3312 if (method->save_lmf) {
3313 /* ebx = previous_lmf */
3314 x86_pop_reg (code, X86_EBX);
3316 x86_pop_reg (code, X86_EDI);
3317 /* *(lmf) = previous_lmf */
3318 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
3320 /* discard method info */
3321 x86_pop_reg (code, X86_ESI);
3323 /* restore caller saved regs */
3324 x86_pop_reg (code, X86_EBP);
3325 x86_pop_reg (code, X86_ESI);
3326 x86_pop_reg (code, X86_EDI);
3327 x86_pop_reg (code, X86_EBX);
3331 if (cfg->used_int_regs & (1 << X86_ESI)) {
3332 x86_pop_reg (code, X86_ESI);
3334 if (cfg->used_int_regs & (1 << X86_EDI)) {
3335 x86_pop_reg (code, X86_EDI);
3337 if (cfg->used_int_regs & (1 << X86_EBX)) {
3338 x86_pop_reg (code, X86_EBX);
3344 if (sig->call_convention == MONO_CALL_STDCALL) {
3345 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
3347 stack_to_pop = arch_get_argument_info (sig, sig->param_count, arg_info);
3350 if (MONO_TYPE_ISSTRUCT (cfg->method->signature->ret))
3356 x86_ret_imm (code, stack_to_pop);
3360 /* add code to raise exceptions */
3361 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3362 switch (patch_info->type) {
3363 case MONO_PATCH_INFO_EXC:
3364 x86_patch (patch_info->ip.i + cfg->native_code, code);
3365 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);
3366 x86_push_imm (code, patch_info->data.target);
3367 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_METHOD_REL, (gpointer)patch_info->ip.i);
3368 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
3369 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3370 patch_info->data.name = "mono_arch_throw_exception_by_name";
3371 patch_info->ip.i = code - cfg->native_code;
3372 x86_jump_code (code, 0);
3380 cfg->code_len = code - cfg->native_code;
3382 g_assert (cfg->code_len < cfg->code_size);
3387 mono_arch_flush_icache (guint8 *code, gint size)
3393 * Support for fast access to the thread-local lmf structure using the GS
3394 * segment register on NPTL + kernel 2.6.x.
3397 static gboolean tls_offset_inited = FALSE;
3399 #ifdef HAVE_KW_THREAD
3400 static __thread gpointer mono_lmf_addr;
3404 mono_arch_get_lmf_addr (void)
3406 #ifdef HAVE_KW_THREAD
3407 return mono_lmf_addr;
3409 g_assert_not_reached ();
3415 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3417 if (!tls_offset_inited) {
3420 tls_offset_inited = TRUE;
3422 if (getenv ("MONO_NPTL")) {
3424 * Determine the offset of mono_lfm_addr inside the TLS structures
3425 * by disassembling the function above.
3427 code = (guint8*)&mono_arch_get_lmf_addr;
3429 /* This is generated by gcc 3.3.2 */
3430 if ((code [0] == 0x55) && (code [1] == 0x89) && (code [2] == 0xe5) &&
3431 (code [3] == 0x65) && (code [4] == 0xa1) && (code [5] == 0x00) &&
3432 (code [6] == 0x00) && (code [7] == 0x00) && (code [8] == 0x00) &&
3433 (code [9] == 0x8b) && (code [10] == 0x80)) {
3434 lmf_tls_offset = *(int*)&(code [11]);
3439 #ifdef HAVE_KW_THREAD
3440 mono_lmf_addr = &tls->lmf;