2 * mini-x86.c: x86 backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2003 Ximian, Inc.
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/debug-helpers.h>
17 #include <mono/metadata/profiler-private.h>
18 #include <mono/utils/mono-math.h>
23 #include "cpu-pentium.h"
25 static gint lmf_tls_offset = -1;
28 /* Under windows, the default pinvoke calling convention is stdcall */
29 #define CALLCONV_IS_STDCALL(call_conv) (((call_conv) == MONO_CALL_STDCALL) || ((call_conv) == MONO_CALL_DEFAULT))
31 #define CALLCONV_IS_STDCALL(call_conv) ((call_conv) == MONO_CALL_STDCALL)
34 #define SIGNAL_STACK_SIZE (64 * 1024)
36 static gpointer mono_arch_get_lmf_addr (void);
39 mono_arch_regname (int reg) {
41 case X86_EAX: return "%eax";
42 case X86_EBX: return "%ebx";
43 case X86_ECX: return "%ecx";
44 case X86_EDX: return "%edx";
45 case X86_ESP: return "%esp"; case X86_EBP: return "%ebp";
46 case X86_EDI: return "%edi";
47 case X86_ESI: return "%esi";
53 * mono_arch_get_argument_info:
54 * @csig: a method signature
55 * @param_count: the number of parameters to consider
56 * @arg_info: an array to store the result infos
58 * Gathers information on parameters such as size, alignment and
59 * padding. arg_info should be large enought to hold param_count + 1 entries.
61 * Returns the size of the activation frame.
64 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
66 int k, frame_size = 0;
70 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
71 frame_size += sizeof (gpointer);
75 arg_info [0].offset = offset;
78 frame_size += sizeof (gpointer);
82 arg_info [0].size = frame_size;
84 for (k = 0; k < param_count; k++) {
87 size = mono_type_native_stack_size (csig->params [k], &align);
89 size = mono_type_stack_size (csig->params [k], &align);
91 /* ignore alignment for now */
94 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
95 arg_info [k].pad = pad;
97 arg_info [k + 1].pad = 0;
98 arg_info [k + 1].size = size;
100 arg_info [k + 1].offset = offset;
104 align = MONO_ARCH_FRAME_ALIGNMENT;
105 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
106 arg_info [k].pad = pad;
111 static const guchar cpuid_impl [] = {
112 0x55, /* push %ebp */
113 0x89, 0xe5, /* mov %esp,%ebp */
114 0x53, /* push %ebx */
115 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
116 0x0f, 0xa2, /* cpuid */
117 0x50, /* push %eax */
118 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
119 0x89, 0x18, /* mov %ebx,(%eax) */
120 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
121 0x89, 0x08, /* mov %ecx,(%eax) */
122 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
123 0x89, 0x10, /* mov %edx,(%eax) */
125 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
126 0x89, 0x02, /* mov %eax,(%edx) */
132 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
135 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
138 __asm__ __volatile__ (
141 "movl %%eax, %%edx\n"
142 "xorl $0x200000, %%eax\n"
147 "xorl %%edx, %%eax\n"
148 "andl $0x200000, %%eax\n"
156 CpuidFunc func = (CpuidFunc)cpuid_impl;
157 func (id, p_eax, p_ebx, p_ecx, p_edx);
159 * We use this approach because of issues with gcc and pic code, see:
160 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
161 __asm__ __volatile__ ("cpuid"
162 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
171 * Initialize the cpu to execute managed code.
174 mono_arch_cpu_init (void)
178 /* spec compliance requires running with double precision */
179 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
180 fpcw &= ~X86_FPCW_PRECC_MASK;
181 fpcw |= X86_FPCW_PREC_DOUBLE;
182 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
183 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
188 * This function returns the optimizations supported on this cpu.
191 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
193 int eax, ebx, ecx, edx;
197 /* Feature Flags function, flags returned in EDX. */
198 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
199 if (edx & (1 << 15)) {
200 opts |= MONO_OPT_CMOV;
202 opts |= MONO_OPT_FCMOV;
204 *exclude_mask |= MONO_OPT_FCMOV;
206 *exclude_mask |= MONO_OPT_CMOV;
212 is_regsize_var (MonoType *t) {
222 case MONO_TYPE_OBJECT:
223 case MONO_TYPE_STRING:
224 case MONO_TYPE_CLASS:
225 case MONO_TYPE_SZARRAY:
226 case MONO_TYPE_ARRAY:
228 case MONO_TYPE_VALUETYPE:
229 if (t->data.klass->enumtype)
230 return is_regsize_var (t->data.klass->enum_basetype);
237 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
242 for (i = 0; i < cfg->num_varinfo; i++) {
243 MonoInst *ins = cfg->varinfo [i];
244 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
247 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
250 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
251 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
254 /* we dont allocate I1 to registers because there is no simply way to sign extend
255 * 8bit quantities in caller saved registers on x86 */
256 if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) ||
257 (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
258 (ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
259 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
260 g_assert (i == vmv->idx);
261 vars = g_list_prepend (vars, vmv);
265 vars = mono_varlist_sort (cfg, vars, 0);
271 mono_arch_get_global_int_regs (MonoCompile *cfg)
275 /* we can use 3 registers for global allocation */
276 regs = g_list_prepend (regs, (gpointer)X86_EBX);
277 regs = g_list_prepend (regs, (gpointer)X86_ESI);
278 regs = g_list_prepend (regs, (gpointer)X86_EDI);
284 * mono_arch_regalloc_cost:
286 * Return the cost, in number of memory references, of the action of
287 * allocating the variable VMV into a register during global register
291 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
293 MonoInst *ins = cfg->varinfo [vmv->idx];
295 if (cfg->method->save_lmf)
296 /* The register is already saved */
297 return (ins->opcode == OP_ARG) ? 1 : 0;
299 /* push+pop+possible load if it is an argument */
300 return (ins->opcode == OP_ARG) ? 3 : 2;
304 * Set var information according to the calling convention. X86 version.
305 * The locals var stuff should most likely be split in another method.
308 mono_arch_allocate_vars (MonoCompile *m)
310 MonoMethodSignature *sig;
311 MonoMethodHeader *header;
313 int i, offset, size, align, curinst;
315 header = ((MonoMethodNormal *)m->method)->header;
317 sig = m->method->signature;
321 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
322 m->ret->opcode = OP_REGOFFSET;
323 m->ret->inst_basereg = X86_EBP;
324 m->ret->inst_offset = offset;
325 offset += sizeof (gpointer);
327 /* FIXME: handle long and FP values */
328 switch (sig->ret->type) {
332 m->ret->opcode = OP_REGVAR;
333 m->ret->inst_c0 = X86_EAX;
338 inst = m->varinfo [curinst];
339 if (inst->opcode != OP_REGVAR) {
340 inst->opcode = OP_REGOFFSET;
341 inst->inst_basereg = X86_EBP;
343 inst->inst_offset = offset;
344 offset += sizeof (gpointer);
348 if (sig->call_convention == MONO_CALL_VARARG) {
349 m->sig_cookie = offset;
350 offset += sizeof (gpointer);
353 for (i = 0; i < sig->param_count; ++i) {
354 inst = m->varinfo [curinst];
355 if (inst->opcode != OP_REGVAR) {
356 inst->opcode = OP_REGOFFSET;
357 inst->inst_basereg = X86_EBP;
359 inst->inst_offset = offset;
360 size = mono_type_size (sig->params [i], &align);
369 /* reserve space to save LMF and caller saved registers */
371 if (m->method->save_lmf) {
372 offset += sizeof (MonoLMF);
374 if (m->used_int_regs & (1 << X86_EBX)) {
378 if (m->used_int_regs & (1 << X86_EDI)) {
382 if (m->used_int_regs & (1 << X86_ESI)) {
387 for (i = curinst; i < m->num_varinfo; ++i) {
388 inst = m->varinfo [i];
390 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
393 /* inst->unused indicates native sized value types, this is used by the
394 * pinvoke wrappers when they call functions returning structure */
395 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
396 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
398 size = mono_type_size (inst->inst_vtype, &align);
402 offset &= ~(align - 1);
403 inst->opcode = OP_REGOFFSET;
404 inst->inst_basereg = X86_EBP;
405 inst->inst_offset = -offset;
406 //g_print ("allocating local %d to %d\n", i, -offset);
408 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
409 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
412 m->stack_offset = -offset;
415 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
416 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
420 * take the arguments and generate the arch-specific
421 * instructions to properly call the function in call.
422 * This includes pushing, moving arguments to the right register
424 * Issue: who does the spilling if needed, and when?
427 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
429 MonoMethodSignature *sig;
430 int i, n, stack_size, type;
434 /* add the vararg cookie before the non-implicit args */
435 if (call->signature->call_convention == MONO_CALL_VARARG) {
437 /* FIXME: Add support for signature tokens to AOT */
438 cfg->disable_aot = TRUE;
439 MONO_INST_NEW (cfg, arg, OP_OUTARG);
440 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
441 sig_arg->inst_p0 = call->signature;
442 arg->inst_left = sig_arg;
443 arg->type = STACK_PTR;
444 /* prepend, so they get reversed */
445 arg->next = call->out_args;
446 call->out_args = arg;
447 stack_size += sizeof (gpointer);
449 sig = call->signature;
450 n = sig->param_count + sig->hasthis;
452 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
453 stack_size += sizeof (gpointer);
454 for (i = 0; i < n; ++i) {
455 if (is_virtual && i == 0) {
456 /* the argument will be attached to the call instrucion */
460 MONO_INST_NEW (cfg, arg, OP_OUTARG);
462 arg->cil_code = in->cil_code;
464 arg->type = in->type;
465 /* prepend, so they get reversed */
466 arg->next = call->out_args;
467 call->out_args = arg;
468 if (i >= sig->hasthis) {
469 ptype = sig->params [i - sig->hasthis];
475 /* FIXME: validate arguments... */
479 case MONO_TYPE_BOOLEAN:
487 case MONO_TYPE_STRING:
488 case MONO_TYPE_CLASS:
489 case MONO_TYPE_OBJECT:
491 case MONO_TYPE_FNPTR:
492 case MONO_TYPE_ARRAY:
493 case MONO_TYPE_SZARRAY:
502 arg->opcode = OP_OUTARG_R4;
506 arg->opcode = OP_OUTARG_R8;
508 case MONO_TYPE_VALUETYPE:
509 if (MONO_TYPE_ISSTRUCT (ptype)) {
512 size = mono_type_native_stack_size (&in->klass->byval_arg, NULL);
514 size = mono_type_stack_size (&in->klass->byval_arg, NULL);
517 arg->opcode = OP_OUTARG_VT;
518 arg->klass = in->klass;
519 arg->unused = sig->pinvoke;
520 arg->inst_imm = size;
522 type = ptype->data.klass->enum_basetype->type;
526 case MONO_TYPE_TYPEDBYREF:
527 stack_size += sizeof (MonoTypedRef);
528 arg->opcode = OP_OUTARG_VT;
529 arg->klass = in->klass;
530 arg->unused = sig->pinvoke;
531 arg->inst_imm = sizeof (MonoTypedRef);
533 case MONO_TYPE_GENERICINST:
534 type = ptype->data.generic_inst->generic_type->type;
538 g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
541 /* the this argument */
546 /* if the function returns a struct, the called method already does a ret $0x4 */
547 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
549 call->stack_usage = stack_size;
551 * should set more info in call, such as the stack space
552 * used by the args that needs to be added back to esp
559 * Allow tracing to work with this interface (with an optional argument)
563 * This may be needed on some archs or for debugging support.
566 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
568 /* no stack room needed now (may be needed for FASTCALL-trace support) */
570 /* split prolog-epilog requirements? */
571 *code = 50; /* max bytes needed: check this number */
575 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
579 /* if some args are passed in registers, we need to save them here */
580 x86_push_reg (code, X86_EBP);
581 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
582 x86_push_imm (code, cfg->method);
583 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
584 x86_call_code (code, 0);
585 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
599 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
602 int arg_size = 0, save_mode = SAVE_NONE;
603 MonoMethod *method = cfg->method;
604 int rtype = method->signature->ret->type;
609 /* special case string .ctor icall */
610 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
611 save_mode = SAVE_EAX;
613 save_mode = SAVE_NONE;
617 save_mode = SAVE_EAX_EDX;
623 case MONO_TYPE_VALUETYPE:
624 if (method->signature->ret->data.klass->enumtype) {
625 rtype = method->signature->ret->data.klass->enum_basetype->type;
628 save_mode = SAVE_STRUCT;
631 save_mode = SAVE_EAX;
637 x86_push_reg (code, X86_EDX);
638 x86_push_reg (code, X86_EAX);
639 if (enable_arguments) {
640 x86_push_reg (code, X86_EDX);
641 x86_push_reg (code, X86_EAX);
646 x86_push_reg (code, X86_EAX);
647 if (enable_arguments) {
648 x86_push_reg (code, X86_EAX);
653 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
654 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
655 if (enable_arguments) {
656 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
657 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
662 if (enable_arguments) {
663 x86_push_membase (code, X86_EBP, 8);
673 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
674 x86_push_imm (code, method);
675 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
676 x86_call_code (code, 0);
677 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
681 x86_pop_reg (code, X86_EAX);
682 x86_pop_reg (code, X86_EDX);
685 x86_pop_reg (code, X86_EAX);
688 x86_fld_membase (code, X86_ESP, 0, TRUE);
689 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
699 #define EMIT_COND_BRANCH(ins,cond,sign) \
700 if (ins->flags & MONO_INST_BRLABEL) { \
701 if (ins->inst_i0->inst_c0) { \
702 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
704 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
705 x86_branch32 (code, cond, 0, sign); \
708 if (ins->inst_true_bb->native_offset) { \
709 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
711 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
712 if ((cfg->opt & MONO_OPT_BRANCH) && \
713 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
714 x86_branch8 (code, cond, 0, sign); \
716 x86_branch32 (code, cond, 0, sign); \
720 /* emit an exception if condition is fail */
721 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
723 mono_add_patch_info (cfg, code - cfg->native_code, \
724 MONO_PATCH_INFO_EXC, exc_name); \
725 x86_branch32 (code, cond, 0, signed); \
728 #define EMIT_FPCOMPARE(code) do { \
733 /* FIXME: Add more instructions */
734 #define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_STORE_MEMBASE_IMM))
737 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
739 MonoInst *ins, *last_ins = NULL;
744 switch (ins->opcode) {
746 /* reg = 0 -> XOR (reg, reg) */
747 /* XOR sets cflags on x86, so we cant do it always */
748 if (ins->inst_c0 == 0 && ins->next && INST_IGNORES_CFLAGS (ins->next)) {
749 ins->opcode = CEE_XOR;
750 ins->sreg1 = ins->dreg;
751 ins->sreg2 = ins->dreg;
755 /* remove unnecessary multiplication with 1 */
756 if (ins->inst_imm == 1) {
757 if (ins->dreg != ins->sreg1) {
758 ins->opcode = OP_MOVE;
760 last_ins->next = ins->next;
767 /* OP_COMPARE_IMM (reg, 0)
769 * OP_X86_TEST_NULL (reg)
771 if (ins->inst_imm == 0 && ins->next &&
772 (ins->next->opcode == CEE_BEQ || ins->next->opcode == CEE_BNE_UN ||
773 ins->next->opcode == OP_CEQ)) {
774 ins->opcode = OP_X86_TEST_NULL;
777 case OP_X86_COMPARE_MEMBASE_IMM:
779 * OP_STORE_MEMBASE_REG reg, offset(basereg)
780 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
782 * OP_STORE_MEMBASE_REG reg, offset(basereg)
783 * OP_COMPARE_IMM reg, imm
785 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
787 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
788 ins->inst_basereg == last_ins->inst_destbasereg &&
789 ins->inst_offset == last_ins->inst_offset) {
790 ins->opcode = OP_COMPARE_IMM;
791 ins->sreg1 = last_ins->sreg1;
793 /* check if we can remove cmp reg,0 with test null */
794 if (ins->inst_imm == 0 && ins->next &&
795 (ins->next->opcode == CEE_BEQ || ins->next->opcode == CEE_BNE_UN ||
796 ins->next->opcode == OP_CEQ)) {
797 ins->opcode = OP_X86_TEST_NULL;
802 case OP_LOAD_MEMBASE:
803 case OP_LOADI4_MEMBASE:
805 * Note: if reg1 = reg2 the load op is removed
807 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
808 * OP_LOAD_MEMBASE offset(basereg), reg2
810 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
813 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
814 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
815 ins->inst_basereg == last_ins->inst_destbasereg &&
816 ins->inst_offset == last_ins->inst_offset) {
817 if (ins->dreg == last_ins->sreg1) {
818 last_ins->next = ins->next;
822 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
823 ins->opcode = OP_MOVE;
824 ins->sreg1 = last_ins->sreg1;
828 * Note: reg1 must be different from the basereg in the second load
829 * Note: if reg1 = reg2 is equal then second load is removed
831 * OP_LOAD_MEMBASE offset(basereg), reg1
832 * OP_LOAD_MEMBASE offset(basereg), reg2
834 * OP_LOAD_MEMBASE offset(basereg), reg1
837 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
838 || last_ins->opcode == OP_LOAD_MEMBASE) &&
839 ins->inst_basereg != last_ins->dreg &&
840 ins->inst_basereg == last_ins->inst_basereg &&
841 ins->inst_offset == last_ins->inst_offset) {
843 if (ins->dreg == last_ins->dreg) {
844 last_ins->next = ins->next;
848 ins->opcode = OP_MOVE;
849 ins->sreg1 = last_ins->dreg;
852 //g_assert_not_reached ();
856 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
857 * OP_LOAD_MEMBASE offset(basereg), reg
859 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
862 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
863 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
864 ins->inst_basereg == last_ins->inst_destbasereg &&
865 ins->inst_offset == last_ins->inst_offset) {
866 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
867 ins->opcode = OP_ICONST;
868 ins->inst_c0 = last_ins->inst_imm;
869 g_assert_not_reached (); // check this rule
873 case OP_LOADU1_MEMBASE:
874 case OP_LOADI1_MEMBASE:
876 * Note: if reg1 = reg2 the load op is removed
878 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
879 * OP_LOAD_MEMBASE offset(basereg), reg2
881 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
884 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
885 ins->inst_basereg == last_ins->inst_destbasereg &&
886 ins->inst_offset == last_ins->inst_offset) {
887 if (ins->dreg == last_ins->sreg1) {
888 last_ins->next = ins->next;
892 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
893 ins->opcode = OP_MOVE;
894 ins->sreg1 = last_ins->sreg1;
898 case OP_LOADU2_MEMBASE:
899 case OP_LOADI2_MEMBASE:
901 * Note: if reg1 = reg2 the load op is removed
903 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
904 * OP_LOAD_MEMBASE offset(basereg), reg2
906 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
909 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
910 ins->inst_basereg == last_ins->inst_destbasereg &&
911 ins->inst_offset == last_ins->inst_offset) {
912 if (ins->dreg == last_ins->sreg1) {
913 last_ins->next = ins->next;
917 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
918 ins->opcode = OP_MOVE;
919 ins->sreg1 = last_ins->sreg1;
931 if (ins->dreg == ins->sreg1) {
933 last_ins->next = ins->next;
943 if (last_ins && last_ins->opcode == OP_MOVE &&
944 ins->sreg1 == last_ins->dreg &&
945 ins->dreg == last_ins->sreg1) {
946 last_ins->next = ins->next;
955 bb->last_ins = last_ins;
959 branch_cc_table [] = {
960 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
961 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
962 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
965 #define DEBUG(a) if (cfg->verbose_level > 1) a
969 * returns the offset used by spillvar. It allocates a new
970 * spill variable if necessary.
973 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
975 MonoSpillInfo **si, *info;
978 si = &cfg->spill_info;
980 while (i <= spillvar) {
983 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
985 cfg->stack_offset -= sizeof (gpointer);
986 info->offset = cfg->stack_offset;
990 return (*si)->offset;
996 g_assert_not_reached ();
1001 * returns the offset used by spillvar. It allocates a new
1002 * spill float variable if necessary.
1003 * (same as mono_spillvar_offset but for float)
1006 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1008 MonoSpillInfo **si, *info;
1011 si = &cfg->spill_info_float;
1013 while (i <= spillvar) {
1016 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1018 cfg->stack_offset -= sizeof (double);
1019 info->offset = cfg->stack_offset;
1023 return (*si)->offset;
1029 g_assert_not_reached ();
1034 * Creates a store for spilled floating point items
1037 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, MonoInst *ins)
1040 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1042 store->inst_destbasereg = X86_EBP;
1043 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1045 DEBUG (g_print ("SPILLED FLOAT STORE (%d at 0x%08x(%%sp)) (from %d)\n", spill, store->inst_offset, reg));
1050 * Creates a load for spilled floating point items
1053 create_spilled_load_float (MonoCompile *cfg, int spill, int reg, MonoInst *ins)
1056 MONO_INST_NEW (cfg, load, OP_LOADR8_SPILL_MEMBASE);
1058 load->inst_basereg = X86_EBP;
1059 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1061 DEBUG (g_print ("SPILLED FLOAT LOAD (%d at 0x%08x(%%sp)) (from %d)\n", spill, load->inst_offset, reg));
1065 #define reg_is_freeable(r) ((r) >= 0 && (r) <= 7 && X86_IS_CALLEE ((r)))
1072 int flags; /* used to track fp spill/load */
1075 static const char*const * ins_spec = pentium_desc;
1078 print_ins (int i, MonoInst *ins)
1080 const char *spec = ins_spec [ins->opcode];
1081 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1082 if (spec [MONO_INST_DEST]) {
1083 if (ins->dreg >= MONO_MAX_IREGS)
1084 g_print (" R%d <-", ins->dreg);
1086 g_print (" %s <-", mono_arch_regname (ins->dreg));
1088 if (spec [MONO_INST_SRC1]) {
1089 if (ins->sreg1 >= MONO_MAX_IREGS)
1090 g_print (" R%d", ins->sreg1);
1092 g_print (" %s", mono_arch_regname (ins->sreg1));
1094 if (spec [MONO_INST_SRC2]) {
1095 if (ins->sreg2 >= MONO_MAX_IREGS)
1096 g_print (" R%d", ins->sreg2);
1098 g_print (" %s", mono_arch_regname (ins->sreg2));
1100 if (spec [MONO_INST_CLOB])
1101 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1106 print_regtrack (RegTrack *t, int num)
1112 for (i = 0; i < num; ++i) {
1115 if (i >= MONO_MAX_IREGS) {
1116 g_snprintf (buf, sizeof(buf), "R%d", i);
1119 r = mono_arch_regname (i);
1120 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1124 typedef struct InstList InstList;
1132 static inline InstList*
1133 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1135 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1145 * Force the spilling of the variable in the symbolic register 'reg'.
1148 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1153 sel = cfg->rs->iassign [reg];
1154 /*i = cfg->rs->isymbolic [sel];
1155 g_assert (i == reg);*/
1157 spill = ++cfg->spill_count;
1158 cfg->rs->iassign [i] = -spill - 1;
1159 mono_regstate_free_int (cfg->rs, sel);
1160 /* we need to create a spill var and insert a load to sel after the current instruction */
1161 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1163 load->inst_basereg = X86_EBP;
1164 load->inst_offset = mono_spillvar_offset (cfg, spill);
1166 while (ins->next != item->prev->data)
1169 load->next = ins->next;
1171 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1172 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1173 g_assert (i == sel);
1179 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1184 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));
1185 /* exclude the registers in the current instruction */
1186 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1187 if (ins->sreg1 >= MONO_MAX_IREGS)
1188 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1190 regmask &= ~ (1 << ins->sreg1);
1191 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1193 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1194 if (ins->sreg2 >= MONO_MAX_IREGS)
1195 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1197 regmask &= ~ (1 << ins->sreg2);
1198 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1200 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1201 regmask &= ~ (1 << ins->dreg);
1202 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1205 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1206 g_assert (regmask); /* need at least a register we can free */
1208 /* we should track prev_use and spill the register that's farther */
1209 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1210 if (regmask & (1 << i)) {
1212 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1216 i = cfg->rs->isymbolic [sel];
1217 spill = ++cfg->spill_count;
1218 cfg->rs->iassign [i] = -spill - 1;
1219 mono_regstate_free_int (cfg->rs, sel);
1220 /* we need to create a spill var and insert a load to sel after the current instruction */
1221 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1223 load->inst_basereg = X86_EBP;
1224 load->inst_offset = mono_spillvar_offset (cfg, spill);
1226 while (ins->next != item->prev->data)
1229 load->next = ins->next;
1231 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%ebp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1232 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1233 g_assert (i == sel);
1239 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1242 MONO_INST_NEW (cfg, copy, OP_MOVE);
1246 copy->next = ins->next;
1249 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1254 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1257 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1259 store->inst_destbasereg = X86_EBP;
1260 store->inst_offset = mono_spillvar_offset (cfg, spill);
1262 store->next = ins->next;
1265 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)));
1270 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1274 prev = item->next->data;
1276 while (prev->next != ins)
1278 to_insert->next = ins;
1279 prev->next = to_insert;
1281 to_insert->next = ins;
1284 * needed otherwise in the next instruction we can add an ins to the
1285 * end and that would get past this instruction.
1287 item->data = to_insert;
1293 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1295 int val = cfg->rs->iassign [sym_reg];
1299 /* the register gets spilled after this inst */
1302 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1304 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1305 cfg->rs->iassign [sym_reg] = val;
1306 /* add option to store before the instruction for src registers */
1308 create_spilled_store (cfg, spill, val, sym_reg, ins);
1310 cfg->rs->isymbolic [val] = sym_reg;
1315 /* flags used in reginfo->flags */
1316 #define MONO_X86_FP_NEEDS_LOAD_SPILL 1
1317 #define MONO_X86_FP_NEEDS_SPILL 2
1318 #define MONO_X86_FP_NEEDS_LOAD 4
1320 /*#include "cprop.c"*/
1323 * Local register allocation.
1324 * We first scan the list of instructions and we save the liveness info of
1325 * each register (when the register is first used, when it's value is set etc.).
1326 * We also reverse the list of instructions (in the InstList list) because assigning
1327 * registers backwards allows for more tricks to be used.
1330 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1333 MonoRegState *rs = cfg->rs;
1334 int i, val, fpcount;
1335 RegTrack *reginfo, *reginfof;
1336 RegTrack *reginfo1, *reginfo2, *reginfod;
1337 InstList *tmp, *reversed = NULL;
1339 guint32 src1_mask, src2_mask, dest_mask;
1340 GList *fspill_list = NULL;
1345 rs->next_vireg = bb->max_ireg;
1346 rs->next_vfreg = bb->max_freg;
1347 mono_regstate_assign (rs);
1348 reginfo = g_malloc0 (sizeof (RegTrack) * rs->next_vireg);
1349 reginfof = g_malloc0 (sizeof (RegTrack) * rs->next_vfreg);
1350 rs->ifree_mask = X86_CALLEE_REGS;
1354 /*if (cfg->opt & MONO_OPT_COPYPROP)
1355 local_copy_prop (cfg, ins);*/
1359 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1360 /* forward pass on the instructions to collect register liveness info */
1362 spec = ins_spec [ins->opcode];
1364 DEBUG (print_ins (i, ins));
1366 if (spec [MONO_INST_SRC1]) {
1367 if (spec [MONO_INST_SRC1] == 'f') {
1369 reginfo1 = reginfof;
1371 spill = g_list_first (fspill_list);
1372 if (spill && fpcount < MONO_MAX_FREGS) {
1373 reginfo1 [ins->sreg1].flags |= MONO_X86_FP_NEEDS_LOAD;
1374 fspill_list = g_list_remove (fspill_list, spill->data);
1380 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1381 reginfo1 [ins->sreg1].last_use = i;
1382 if (spec [MONO_INST_SRC1] == 'L') {
1383 /* The virtual register is allocated sequentially */
1384 reginfo1 [ins->sreg1 + 1].prev_use = reginfo1 [ins->sreg1 + 1].last_use;
1385 reginfo1 [ins->sreg1 + 1].last_use = i;
1386 if (reginfo1 [ins->sreg1 + 1].born_in == 0 || reginfo1 [ins->sreg1 + 1].born_in > i)
1387 reginfo1 [ins->sreg1 + 1].born_in = i;
1392 if (spec [MONO_INST_SRC2]) {
1393 if (spec [MONO_INST_SRC2] == 'f') {
1395 reginfo2 = reginfof;
1396 spill = g_list_first (fspill_list);
1398 reginfo2 [ins->sreg2].flags |= MONO_X86_FP_NEEDS_LOAD;
1399 fspill_list = g_list_remove (fspill_list, spill->data);
1400 if (fpcount >= MONO_MAX_FREGS) {
1402 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
1403 reginfo2 [ins->sreg2].flags |= MONO_X86_FP_NEEDS_LOAD_SPILL;
1410 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1411 reginfo2 [ins->sreg2].last_use = i;
1412 if (spec [MONO_INST_SRC2] == 'L') {
1413 /* The virtual register is allocated sequentially */
1414 reginfo2 [ins->sreg2 + 1].prev_use = reginfo2 [ins->sreg2 + 1].last_use;
1415 reginfo2 [ins->sreg2 + 1].last_use = i;
1416 if (reginfo2 [ins->sreg2 + 1].born_in == 0 || reginfo2 [ins->sreg2 + 1].born_in > i)
1417 reginfo2 [ins->sreg2 + 1].born_in = i;
1422 if (spec [MONO_INST_DEST]) {
1423 if (spec [MONO_INST_DEST] == 'f') {
1424 reginfod = reginfof;
1425 if (fpcount >= MONO_MAX_FREGS) {
1426 reginfod [ins->dreg].flags |= MONO_X86_FP_NEEDS_SPILL;
1428 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
1435 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1436 reginfod [ins->dreg].killed_in = i;
1437 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1438 reginfod [ins->dreg].last_use = i;
1439 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1440 reginfod [ins->dreg].born_in = i;
1441 if (spec [MONO_INST_DEST] == 'l' || spec [MONO_INST_DEST] == 'L') {
1442 /* The virtual register is allocated sequentially */
1443 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1444 reginfod [ins->dreg + 1].last_use = i;
1445 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1446 reginfod [ins->dreg + 1].born_in = i;
1451 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1456 // todo: check if we have anything left on fp stack, in verify mode?
1459 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1460 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1463 int prev_dreg, prev_sreg1, prev_sreg2, clob_dreg;
1464 dest_mask = src1_mask = src2_mask = X86_CALLEE_REGS;
1467 spec = ins_spec [ins->opcode];
1470 DEBUG (g_print ("processing:"));
1471 DEBUG (print_ins (i, ins));
1472 if (spec [MONO_INST_CLOB] == 's') {
1473 if (rs->ifree_mask & (1 << X86_ECX)) {
1474 DEBUG (g_print ("\tshortcut assignment of R%d to ECX\n", ins->sreg2));
1475 rs->iassign [ins->sreg2] = X86_ECX;
1476 rs->isymbolic [X86_ECX] = ins->sreg2;
1477 ins->sreg2 = X86_ECX;
1478 rs->ifree_mask &= ~ (1 << X86_ECX);
1480 int need_ecx_spill = TRUE;
1482 * we first check if src1/dreg is already assigned a register
1483 * and then we force a spill of the var assigned to ECX.
1485 /* the destination register can't be ECX */
1486 dest_mask &= ~ (1 << X86_ECX);
1487 src1_mask &= ~ (1 << X86_ECX);
1488 val = rs->iassign [ins->dreg];
1490 * the destination register is already assigned to ECX:
1491 * we need to allocate another register for it and then
1492 * copy from this to ECX.
1494 if (val == X86_ECX && ins->dreg != ins->sreg2) {
1495 int new_dest = mono_regstate_alloc_int (rs, dest_mask);
1497 new_dest = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1498 g_assert (new_dest >= 0);
1499 DEBUG (g_print ("\tclob:s changing dreg from R%d to %s (val = %d)\n", ins->dreg, mono_arch_regname (new_dest), val));
1500 clob_dreg = ins->dreg;
1501 ins->dreg = new_dest;
1502 create_copy_ins (cfg, X86_ECX, new_dest, ins);
1503 need_ecx_spill = FALSE;
1504 /*DEBUG (g_print ("\tforced spill of R%d\n", ins->dreg));
1505 val = get_register_force_spilling (cfg, tmp, ins, ins->dreg);
1506 rs->iassign [ins->dreg] = val;
1507 rs->isymbolic [val] = prev_dreg;
1510 val = rs->iassign [ins->sreg1];
1511 if (val == X86_ECX) {
1512 g_assert_not_reached ();
1513 } else if (val >= 0) {
1515 * the first src reg was already assigned to a register,
1516 * we need to copy it to the dest register because the
1517 * shift instruction clobbers the first operand.
1519 MonoInst *copy = create_copy_ins (cfg, ins->dreg, val, NULL);
1520 DEBUG (g_print ("\tclob:s moved sreg1 from R%d to R%d\n", val, ins->dreg));
1521 insert_before_ins (ins, tmp, copy);
1523 val = rs->iassign [ins->sreg2];
1524 if (val >= 0 && val != X86_ECX) {
1525 MonoInst *move = create_copy_ins (cfg, X86_ECX, val, NULL);
1526 DEBUG (g_print ("\tmoved arg from R%d (%d) to ECX\n", val, ins->sreg2));
1528 g_assert_not_reached ();
1529 /* FIXME: where is move connected to the instruction list? */
1530 //tmp->prev->data->next = move;
1532 if (need_ecx_spill && !(rs->ifree_mask & (1 << X86_ECX))) {
1533 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_ECX]));
1534 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_ECX]);
1535 mono_regstate_free_int (rs, X86_ECX);
1537 /* force-set sreg2 */
1538 rs->iassign [ins->sreg2] = X86_ECX;
1539 rs->isymbolic [X86_ECX] = ins->sreg2;
1540 ins->sreg2 = X86_ECX;
1541 rs->ifree_mask &= ~ (1 << X86_ECX);
1543 } else if (spec [MONO_INST_CLOB] == 'd') { /* division */
1544 int dest_reg = X86_EAX;
1545 int clob_reg = X86_EDX;
1546 if (spec [MONO_INST_DEST] == 'd') {
1547 dest_reg = X86_EDX; /* reminder */
1550 val = rs->iassign [ins->dreg];
1551 if (0 && val >= 0 && val != dest_reg && !(rs->ifree_mask & (1 << dest_reg))) {
1552 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1553 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1554 mono_regstate_free_int (rs, dest_reg);
1558 /* the register gets spilled after this inst */
1559 int spill = -val -1;
1560 dest_mask = 1 << clob_reg;
1561 prev_dreg = ins->dreg;
1562 val = mono_regstate_alloc_int (rs, dest_mask);
1564 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1565 rs->iassign [ins->dreg] = val;
1567 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1568 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1569 rs->isymbolic [val] = prev_dreg;
1571 if (val != dest_reg) { /* force a copy */
1572 create_copy_ins (cfg, val, dest_reg, ins);
1575 DEBUG (g_print ("\tshortcut assignment of R%d to %s\n", ins->dreg, mono_arch_regname (dest_reg)));
1576 prev_dreg = ins->dreg;
1577 rs->iassign [ins->dreg] = dest_reg;
1578 rs->isymbolic [dest_reg] = ins->dreg;
1579 ins->dreg = dest_reg;
1580 rs->ifree_mask &= ~ (1 << dest_reg);
1583 //DEBUG (g_print ("dest reg in div assigned: %s\n", mono_arch_regname (val)));
1584 if (val != dest_reg) { /* force a copy */
1585 create_copy_ins (cfg, val, dest_reg, ins);
1586 if (!(rs->ifree_mask & (1 << dest_reg)) && rs->isymbolic [dest_reg] >= MONO_MAX_IREGS) {
1587 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [dest_reg]));
1588 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [dest_reg]);
1589 mono_regstate_free_int (rs, dest_reg);
1593 if (!(rs->ifree_mask & (1 << clob_reg)) && (clob_reg != val) && (rs->isymbolic [clob_reg] >= 8)) {
1594 DEBUG (g_print ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
1595 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [clob_reg]);
1596 mono_regstate_free_int (rs, clob_reg);
1598 src1_mask = 1 << X86_EAX;
1599 src2_mask = 1 << X86_ECX;
1601 if (spec [MONO_INST_DEST] == 'l') {
1602 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1603 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1604 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1605 mono_regstate_free_int (rs, X86_EAX);
1607 if (!(rs->ifree_mask & (1 << X86_EDX))) {
1608 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EDX]));
1609 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EDX]);
1610 mono_regstate_free_int (rs, X86_EDX);
1615 if (spec [MONO_INST_DEST] == 'f') {
1616 if (reginfof [ins->dreg].flags & MONO_X86_FP_NEEDS_SPILL) {
1619 spill_node = g_list_first (fspill_list);
1620 g_assert (spill_node);
1622 store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->dreg, ins);
1623 insert_before_ins (ins, tmp, store);
1624 fspill_list = g_list_remove (fspill_list, spill_node->data);
1627 } else if (spec [MONO_INST_DEST] == 'L') {
1629 val = rs->iassign [ins->dreg];
1630 /* check special case when dreg have been moved from ecx (clob shift) */
1631 if (spec [MONO_INST_CLOB] == 's' && clob_dreg != -1)
1632 hreg = clob_dreg + 1;
1634 hreg = ins->dreg + 1;
1636 /* base prev_dreg on fixed hreg, handle clob case */
1637 prev_dreg = hreg - 1;
1642 /* the register gets spilled after this inst */
1645 val = mono_regstate_alloc_int (rs, dest_mask);
1646 if (val < 0) /* todo: should we force reg into eax, for opt reasons? */
1647 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1648 rs->iassign [ins->dreg] = val;
1650 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1653 DEBUG (g_print ("\tassigned dreg (long) %s to dest R%d\n", mono_arch_regname (val), hreg - 1));
1655 rs->isymbolic [val] = hreg - 1;
1658 val = rs->iassign [hreg];
1662 /* the register gets spilled after this inst */
1665 val = mono_regstate_alloc_int (rs, dest_mask);
1666 if (val < 0) /* todo: should we force reg into edx, for opt reasons? */
1667 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1668 rs->iassign [hreg] = val;
1670 create_spilled_store (cfg, spill, val, hreg, ins);
1673 DEBUG (g_print ("\tassigned hreg (long) %s to dest R%d\n", mono_arch_regname (val), hreg));
1674 rs->isymbolic [val] = hreg;
1675 /* save reg allocating into unused */
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);
1683 else if (ins->dreg >= MONO_MAX_IREGS) {
1684 val = rs->iassign [ins->dreg];
1685 prev_dreg = ins->dreg;
1689 /* the register gets spilled after this inst */
1692 val = mono_regstate_alloc_int (rs, dest_mask);
1694 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1695 rs->iassign [ins->dreg] = val;
1697 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1699 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1700 rs->isymbolic [val] = prev_dreg;
1702 /* handle cases where lreg needs to be eax:edx */
1703 if (spec [MONO_INST_DEST] == 'l') {
1704 int hreg = prev_dreg + 1;
1705 val = rs->iassign [hreg];
1709 /* the register gets spilled after this inst */
1712 val = mono_regstate_alloc_int (rs, dest_mask);
1714 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1715 rs->iassign [hreg] = val;
1717 create_spilled_store (cfg, spill, val, hreg, ins);
1719 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1720 rs->isymbolic [val] = hreg;
1721 if (ins->dreg == X86_EAX) {
1723 create_copy_ins (cfg, val, X86_EDX, ins);
1724 } else if (ins->dreg == X86_EDX) {
1725 if (val == X86_EAX) {
1727 g_assert_not_reached ();
1729 /* two forced copies */
1730 create_copy_ins (cfg, val, X86_EDX, ins);
1731 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1734 if (val == X86_EDX) {
1735 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1737 /* two forced copies */
1738 create_copy_ins (cfg, val, X86_EDX, ins);
1739 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1742 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1743 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1744 mono_regstate_free_int (rs, val);
1746 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != X86_EAX && spec [MONO_INST_CLOB] != 'd') {
1747 /* this instruction only outputs to EAX, need to copy */
1748 create_copy_ins (cfg, ins->dreg, X86_EAX, ins);
1749 } else if (spec [MONO_INST_DEST] == 'd' && ins->dreg != X86_EDX && spec [MONO_INST_CLOB] != 'd') {
1750 create_copy_ins (cfg, ins->dreg, X86_EDX, ins);
1753 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1754 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1755 mono_regstate_free_int (rs, ins->dreg);
1757 /* put src1 in EAX if it needs to be */
1758 if (spec [MONO_INST_SRC1] == 'a') {
1759 if (!(rs->ifree_mask & (1 << X86_EAX))) {
1760 DEBUG (g_print ("\tforced spill of R%d\n", rs->isymbolic [X86_EAX]));
1761 get_register_force_spilling (cfg, tmp, ins, rs->isymbolic [X86_EAX]);
1762 mono_regstate_free_int (rs, X86_EAX);
1764 /* force-set sreg1 */
1765 rs->iassign [ins->sreg1] = X86_EAX;
1766 rs->isymbolic [X86_EAX] = ins->sreg1;
1767 ins->sreg1 = X86_EAX;
1768 rs->ifree_mask &= ~ (1 << X86_EAX);
1772 if (spec [MONO_INST_SRC1] == 'f') {
1773 if (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD) {
1775 MonoInst *store = NULL;
1777 if (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD_SPILL) {
1779 spill_node = g_list_first (fspill_list);
1780 g_assert (spill_node);
1782 store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->sreg1, ins);
1783 fspill_list = g_list_remove (fspill_list, spill_node->data);
1787 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
1788 load = create_spilled_load_float (cfg, fspill, ins->sreg1, ins);
1789 insert_before_ins (ins, tmp, load);
1791 insert_before_ins (load, tmp, store);
1793 } else if ((spec [MONO_INST_DEST] == 'L') && (spec [MONO_INST_SRC1] == 'L')) {
1794 /* force source to be same as dest */
1795 rs->iassign [ins->sreg1] = ins->dreg;
1796 rs->iassign [ins->sreg1 + 1] = ins->unused;
1798 DEBUG (g_print ("\tassigned sreg1 (long) %s to sreg1 R%d\n", mono_arch_regname (ins->dreg), ins->sreg1));
1799 DEBUG (g_print ("\tassigned sreg1 (long-high) %s to sreg1 R%d\n", mono_arch_regname (ins->unused), ins->sreg1 + 1));
1801 ins->sreg1 = ins->dreg;
1802 /* no need for this, we know that src1=dest in this cases */
1803 /*ins->inst_c0 = ins->unused;*/
1805 /* make sure that we remove them from free mask */
1806 rs->ifree_mask &= ~ (1 << ins->dreg);
1807 rs->ifree_mask &= ~ (1 << ins->unused);
1809 else if (ins->sreg1 >= MONO_MAX_IREGS) {
1810 val = rs->iassign [ins->sreg1];
1811 prev_sreg1 = ins->sreg1;
1815 /* the register gets spilled after this inst */
1818 if (0 && ins->opcode == OP_MOVE) {
1820 * small optimization: the dest register is already allocated
1821 * but the src one is not: we can simply assign the same register
1822 * here and peephole will get rid of the instruction later.
1823 * This optimization may interfere with the clobbering handling:
1824 * it removes a mov operation that will be added again to handle clobbering.
1825 * There are also some other issues that should with make testjit.
1827 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1828 val = rs->iassign [ins->sreg1] = ins->dreg;
1829 //g_assert (val >= 0);
1830 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1832 //g_assert (val == -1); /* source cannot be spilled */
1833 val = mono_regstate_alloc_int (rs, src1_mask);
1835 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1836 rs->iassign [ins->sreg1] = val;
1837 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1840 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1841 insert_before_ins (ins, tmp, store);
1844 rs->isymbolic [val] = prev_sreg1;
1849 /* handle clobbering of sreg1 */
1850 if ((spec [MONO_INST_CLOB] == '1' || spec [MONO_INST_CLOB] == 's') && ins->dreg != ins->sreg1) {
1851 MonoInst *copy = create_copy_ins (cfg, ins->dreg, ins->sreg1, NULL);
1852 DEBUG (g_print ("\tneed to copy sreg1 %s to dreg %s\n", mono_arch_regname (ins->sreg1), mono_arch_regname (ins->dreg)));
1853 if (ins->sreg2 == -1 || spec [MONO_INST_CLOB] == 's') {
1854 /* note: the copy is inserted before the current instruction! */
1855 insert_before_ins (ins, tmp, copy);
1856 /* we set sreg1 to dest as well */
1857 prev_sreg1 = ins->sreg1 = ins->dreg;
1859 /* inserted after the operation */
1860 copy->next = ins->next;
1865 if (spec [MONO_INST_SRC2] == 'f') {
1866 if (reginfof [ins->sreg2].flags & MONO_X86_FP_NEEDS_LOAD) {
1868 MonoInst *store = NULL;
1870 if (reginfof [ins->sreg2].flags & MONO_X86_FP_NEEDS_LOAD_SPILL) {
1873 spill_node = g_list_first (fspill_list);
1874 g_assert (spill_node);
1875 if (spec [MONO_INST_SRC1] == 'f' && (reginfof [ins->sreg1].flags & MONO_X86_FP_NEEDS_LOAD_SPILL))
1876 spill_node = g_list_next (spill_node);
1878 store = create_spilled_store_float (cfg, GPOINTER_TO_INT (spill_node->data), ins->sreg2, ins);
1879 fspill_list = g_list_remove (fspill_list, spill_node->data);
1883 fspill_list = g_list_prepend (fspill_list, GINT_TO_POINTER(fspill));
1884 load = create_spilled_load_float (cfg, fspill, ins->sreg2, ins);
1885 insert_before_ins (ins, tmp, load);
1887 insert_before_ins (load, tmp, store);
1890 else if (ins->sreg2 >= MONO_MAX_IREGS) {
1891 val = rs->iassign [ins->sreg2];
1892 prev_sreg2 = ins->sreg2;
1896 /* the register gets spilled after this inst */
1899 val = mono_regstate_alloc_int (rs, src2_mask);
1901 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1902 rs->iassign [ins->sreg2] = val;
1903 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1905 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1907 rs->isymbolic [val] = prev_sreg2;
1909 if (spec [MONO_INST_CLOB] == 's' && ins->sreg2 != X86_ECX) {
1910 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]));
1916 if (spec [MONO_INST_CLOB] == 'c') {
1918 guint32 clob_mask = X86_CALLEE_REGS;
1919 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1921 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1922 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1926 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1927 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1928 mono_regstate_free_int (rs, ins->sreg1);
1930 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1931 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1932 mono_regstate_free_int (rs, ins->sreg2);
1935 //DEBUG (print_ins (i, ins));
1936 /* this may result from a insert_before call */
1938 bb->code = tmp->data;
1944 g_list_free (fspill_list);
1947 static unsigned char*
1948 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1950 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1951 x86_fnstcw_membase(code, X86_ESP, 0);
1952 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1953 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1954 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1955 x86_fldcw_membase (code, X86_ESP, 2);
1957 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1958 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1959 x86_pop_reg (code, dreg);
1960 /* FIXME: need the high register
1961 * x86_pop_reg (code, dreg_high);
1964 x86_push_reg (code, X86_EAX); // SP = SP - 4
1965 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1966 x86_pop_reg (code, dreg);
1968 x86_fldcw_membase (code, X86_ESP, 0);
1969 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1972 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1974 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1978 static unsigned char*
1979 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1981 int sreg = tree->sreg1;
1982 #ifdef PLATFORM_WIN32
1987 * If requested stack size is larger than one page,
1988 * perform stack-touch operation
1991 * Generate stack probe code.
1992 * Under Windows, it is necessary to allocate one page at a time,
1993 * "touching" stack after each successful sub-allocation. This is
1994 * because of the way stack growth is implemented - there is a
1995 * guard page before the lowest stack page that is currently commited.
1996 * Stack normally grows sequentially so OS traps access to the
1997 * guard page and commits more pages when needed.
1999 x86_test_reg_imm (code, sreg, ~0xFFF);
2000 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2002 br[2] = code; /* loop */
2003 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
2004 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
2005 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
2006 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
2007 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
2008 x86_patch (br[3], br[2]);
2009 x86_test_reg_reg (code, sreg, sreg);
2010 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
2011 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2013 br[1] = code; x86_jump8 (code, 0);
2015 x86_patch (br[0], code);
2016 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
2017 x86_patch (br[1], code);
2018 x86_patch (br[4], code);
2019 #else /* PLATFORM_WIN32 */
2020 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2022 if (tree->flags & MONO_INST_INIT) {
2024 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2025 x86_push_reg (code, X86_EAX);
2028 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2029 x86_push_reg (code, X86_ECX);
2032 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2033 x86_push_reg (code, X86_EDI);
2037 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2038 if (sreg != X86_ECX)
2039 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2040 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2042 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2044 x86_prefix (code, X86_REP_PREFIX);
2047 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2048 x86_pop_reg (code, X86_EDI);
2049 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2050 x86_pop_reg (code, X86_ECX);
2051 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2052 x86_pop_reg (code, X86_EAX);
2057 #define REAL_PRINT_REG(text,reg) \
2058 mono_assert (reg >= 0); \
2059 x86_push_reg (code, X86_EAX); \
2060 x86_push_reg (code, X86_EDX); \
2061 x86_push_reg (code, X86_ECX); \
2062 x86_push_reg (code, reg); \
2063 x86_push_imm (code, reg); \
2064 x86_push_imm (code, text " %d %p\n"); \
2065 x86_mov_reg_imm (code, X86_EAX, printf); \
2066 x86_call_reg (code, X86_EAX); \
2067 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
2068 x86_pop_reg (code, X86_ECX); \
2069 x86_pop_reg (code, X86_EDX); \
2070 x86_pop_reg (code, X86_EAX);
2073 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2078 guint8 *code = cfg->native_code + cfg->code_len;
2079 MonoInst *last_ins = NULL;
2080 guint last_offset = 0;
2083 if (cfg->opt & MONO_OPT_PEEPHOLE)
2084 peephole_pass (cfg, bb);
2086 if (cfg->opt & MONO_OPT_LOOP) {
2088 /* set alignment depending on cpu */
2089 if (bb->nesting && (bb->in_count == 1) && (pad = (cfg->code_len & (align - 1)))) {
2091 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2092 x86_padding (code, pad);
2093 cfg->code_len += pad;
2094 bb->native_offset = cfg->code_len;
2098 if (cfg->verbose_level > 2)
2099 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2101 cpos = bb->max_offset;
2103 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2104 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2105 g_assert (!mono_compile_aot);
2108 cov->data [bb->dfn].cil_code = bb->cil_code;
2109 /* this is not thread save, but good enough */
2110 x86_inc_mem (code, &cov->data [bb->dfn].count);
2113 offset = code - cfg->native_code;
2117 offset = code - cfg->native_code;
2119 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2121 if (offset > (cfg->code_size - max_len - 16)) {
2122 cfg->code_size *= 2;
2123 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2124 code = cfg->native_code + offset;
2125 mono_jit_stats.code_reallocs++;
2128 mono_debug_record_line_number (cfg, ins, offset);
2130 switch (ins->opcode) {
2132 x86_mul_reg (code, ins->sreg2, TRUE);
2135 x86_mul_reg (code, ins->sreg2, FALSE);
2137 case OP_X86_SETEQ_MEMBASE:
2138 x86_set_membase (code, X86_CC_EQ, ins->inst_basereg, ins->inst_offset, TRUE);
2140 case OP_STOREI1_MEMBASE_IMM:
2141 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2143 case OP_STOREI2_MEMBASE_IMM:
2144 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2146 case OP_STORE_MEMBASE_IMM:
2147 case OP_STOREI4_MEMBASE_IMM:
2148 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2150 case OP_STOREI1_MEMBASE_REG:
2151 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2153 case OP_STOREI2_MEMBASE_REG:
2154 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2156 case OP_STORE_MEMBASE_REG:
2157 case OP_STOREI4_MEMBASE_REG:
2158 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2163 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2166 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2167 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2169 case OP_LOAD_MEMBASE:
2170 case OP_LOADI4_MEMBASE:
2171 case OP_LOADU4_MEMBASE:
2172 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2174 case OP_LOADU1_MEMBASE:
2175 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2177 case OP_LOADI1_MEMBASE:
2178 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2180 case OP_LOADU2_MEMBASE:
2181 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2183 case OP_LOADI2_MEMBASE:
2184 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2187 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2190 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2193 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2196 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2199 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2201 case OP_COMPARE_IMM:
2202 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2204 case OP_X86_COMPARE_MEMBASE_REG:
2205 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2207 case OP_X86_COMPARE_MEMBASE_IMM:
2208 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2210 case OP_X86_COMPARE_REG_MEMBASE:
2211 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2213 case OP_X86_TEST_NULL:
2214 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2216 case OP_X86_ADD_MEMBASE_IMM:
2217 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2219 case OP_X86_ADD_MEMBASE:
2220 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2222 case OP_X86_SUB_MEMBASE_IMM:
2223 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2225 case OP_X86_SUB_MEMBASE:
2226 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2228 case OP_X86_INC_MEMBASE:
2229 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2231 case OP_X86_INC_REG:
2232 x86_inc_reg (code, ins->dreg);
2234 case OP_X86_DEC_MEMBASE:
2235 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2237 case OP_X86_DEC_REG:
2238 x86_dec_reg (code, ins->dreg);
2240 case OP_X86_MUL_MEMBASE:
2241 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2244 x86_breakpoint (code);
2248 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2251 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2254 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2257 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2261 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2264 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2267 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2270 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2273 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2276 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2280 x86_div_reg (code, ins->sreg2, TRUE);
2283 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2284 x86_div_reg (code, ins->sreg2, FALSE);
2287 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2289 x86_div_reg (code, ins->sreg2, TRUE);
2293 x86_div_reg (code, ins->sreg2, TRUE);
2296 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2297 x86_div_reg (code, ins->sreg2, FALSE);
2300 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2302 x86_div_reg (code, ins->sreg2, TRUE);
2305 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2308 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2311 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2314 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2317 g_assert (ins->sreg2 == X86_ECX);
2318 x86_shift_reg (code, X86_SHL, ins->dreg);
2321 g_assert (ins->sreg2 == X86_ECX);
2322 x86_shift_reg (code, X86_SAR, ins->dreg);
2325 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2328 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2331 g_assert (ins->sreg2 == X86_ECX);
2332 x86_shift_reg (code, X86_SHR, ins->dreg);
2335 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2338 guint8 *jump_to_end;
2340 /* handle shifts below 32 bits */
2341 x86_shld_reg (code, ins->unused, ins->sreg1);
2342 x86_shift_reg (code, X86_SHL, ins->sreg1);
2344 x86_test_reg_imm (code, X86_ECX, 32);
2345 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2347 /* handle shift over 32 bit */
2348 x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
2349 x86_clear_reg (code, ins->sreg1);
2351 x86_patch (jump_to_end, code);
2355 guint8 *jump_to_end;
2357 /* handle shifts below 32 bits */
2358 x86_shrd_reg (code, ins->sreg1, ins->unused);
2359 x86_shift_reg (code, X86_SAR, ins->unused);
2361 x86_test_reg_imm (code, X86_ECX, 32);
2362 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2364 /* handle shifts over 31 bits */
2365 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
2366 x86_shift_reg_imm (code, X86_SAR, ins->unused, 31);
2368 x86_patch (jump_to_end, code);
2372 guint8 *jump_to_end;
2374 /* handle shifts below 32 bits */
2375 x86_shrd_reg (code, ins->sreg1, ins->unused);
2376 x86_shift_reg (code, X86_SHR, ins->unused);
2378 x86_test_reg_imm (code, X86_ECX, 32);
2379 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2381 /* handle shifts over 31 bits */
2382 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
2383 x86_shift_reg_imm (code, X86_SHR, ins->unused, 31);
2385 x86_patch (jump_to_end, code);
2389 if (ins->inst_imm >= 32) {
2390 x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
2391 x86_clear_reg (code, ins->sreg1);
2392 x86_shift_reg_imm (code, X86_SHL, ins->unused, ins->inst_imm - 32);
2394 x86_shld_reg_imm (code, ins->unused, ins->sreg1, ins->inst_imm);
2395 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
2399 if (ins->inst_imm >= 32) {
2400 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
2401 x86_shift_reg_imm (code, X86_SAR, ins->unused, 0x1f);
2402 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
2404 x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
2405 x86_shift_reg_imm (code, X86_SAR, ins->unused, ins->inst_imm);
2408 case OP_LSHR_UN_IMM:
2409 if (ins->inst_imm >= 32) {
2410 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
2411 x86_clear_reg (code, ins->unused);
2412 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
2414 x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
2415 x86_shift_reg_imm (code, X86_SHR, ins->unused, ins->inst_imm);
2419 x86_not_reg (code, ins->sreg1);
2422 x86_neg_reg (code, ins->sreg1);
2425 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2428 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2431 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2434 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2437 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2438 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2440 case CEE_MUL_OVF_UN: {
2441 /* the mul operation and the exception check should most likely be split */
2442 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2443 /*g_assert (ins->sreg2 == X86_EAX);
2444 g_assert (ins->dreg == X86_EAX);*/
2445 if (ins->sreg2 == X86_EAX) {
2446 non_eax_reg = ins->sreg1;
2447 } else if (ins->sreg1 == X86_EAX) {
2448 non_eax_reg = ins->sreg2;
2450 /* no need to save since we're going to store to it anyway */
2451 if (ins->dreg != X86_EAX) {
2453 x86_push_reg (code, X86_EAX);
2455 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2456 non_eax_reg = ins->sreg2;
2458 if (ins->dreg == X86_EDX) {
2461 x86_push_reg (code, X86_EAX);
2463 } else if (ins->dreg != X86_EAX) {
2465 x86_push_reg (code, X86_EDX);
2467 x86_mul_reg (code, non_eax_reg, FALSE);
2468 /* save before the check since pop and mov don't change the flags */
2469 if (ins->dreg != X86_EAX)
2470 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2472 x86_pop_reg (code, X86_EDX);
2474 x86_pop_reg (code, X86_EAX);
2475 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2479 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2482 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2483 x86_mov_reg_imm (code, ins->dreg, 0);
2487 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2490 g_assert_not_reached ();
2493 * Note: this 'frame destruction' logic is useful for tail calls, too.
2494 * Keep in sync with the code in emit_epilog.
2498 /* FIXME: no tracing support... */
2499 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2500 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2501 /* reset offset to make max_len work */
2502 offset = code - cfg->native_code;
2504 g_assert (!cfg->method->save_lmf);
2506 if (cfg->used_int_regs & (1 << X86_EBX))
2508 if (cfg->used_int_regs & (1 << X86_EDI))
2510 if (cfg->used_int_regs & (1 << X86_ESI))
2513 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2515 if (cfg->used_int_regs & (1 << X86_ESI))
2516 x86_pop_reg (code, X86_ESI);
2517 if (cfg->used_int_regs & (1 << X86_EDI))
2518 x86_pop_reg (code, X86_EDI);
2519 if (cfg->used_int_regs & (1 << X86_EBX))
2520 x86_pop_reg (code, X86_EBX);
2522 /* restore ESP/EBP */
2524 offset = code - cfg->native_code;
2525 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2526 x86_jump32 (code, 0);
2530 /* ensure ins->sreg1 is not NULL */
2531 x86_alu_membase_imm (code, X86_CMP, ins->sreg1, 0, 0);
2534 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2535 x86_push_reg (code, hreg);
2536 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2537 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2538 x86_pop_reg (code, hreg);
2546 call = (MonoCallInst*)ins;
2547 if (ins->flags & MONO_INST_HAS_METHOD)
2548 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2550 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2552 x86_call_code (code, 0);
2553 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
2554 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2559 case OP_VOIDCALL_REG:
2561 call = (MonoCallInst*)ins;
2562 x86_call_reg (code, ins->sreg1);
2563 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
2564 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2566 case OP_FCALL_MEMBASE:
2567 case OP_LCALL_MEMBASE:
2568 case OP_VCALL_MEMBASE:
2569 case OP_VOIDCALL_MEMBASE:
2570 case OP_CALL_MEMBASE:
2571 call = (MonoCallInst*)ins;
2572 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2573 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature->call_convention))
2574 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2578 x86_push_reg (code, ins->sreg1);
2580 case OP_X86_PUSH_IMM:
2581 x86_push_imm (code, ins->inst_imm);
2583 case OP_X86_PUSH_MEMBASE:
2584 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2586 case OP_X86_PUSH_OBJ:
2587 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2588 x86_push_reg (code, X86_EDI);
2589 x86_push_reg (code, X86_ESI);
2590 x86_push_reg (code, X86_ECX);
2591 if (ins->inst_offset)
2592 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2594 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2595 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2596 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2598 x86_prefix (code, X86_REP_PREFIX);
2600 x86_pop_reg (code, X86_ECX);
2601 x86_pop_reg (code, X86_ESI);
2602 x86_pop_reg (code, X86_EDI);
2605 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
2607 case OP_X86_LEA_MEMBASE:
2608 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
2611 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2614 /* keep alignment */
2615 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
2616 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
2617 code = mono_emit_stack_alloc (code, ins);
2618 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2624 x86_push_reg (code, ins->sreg1);
2625 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2626 (gpointer)"mono_arch_throw_exception");
2627 x86_call_code (code, 0);
2630 case OP_CALL_HANDLER:
2631 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2632 x86_call_imm (code, 0);
2635 ins->inst_c0 = code - cfg->native_code;
2638 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2639 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2641 if (ins->flags & MONO_INST_BRLABEL) {
2642 if (ins->inst_i0->inst_c0) {
2643 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2645 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2646 x86_jump32 (code, 0);
2649 if (ins->inst_target_bb->native_offset) {
2650 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2652 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2653 if ((cfg->opt & MONO_OPT_BRANCH) &&
2654 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
2655 x86_jump8 (code, 0);
2657 x86_jump32 (code, 0);
2662 x86_jump_reg (code, ins->sreg1);
2665 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2666 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2669 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
2670 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2673 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2674 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2677 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
2678 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2681 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2682 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2684 case OP_COND_EXC_EQ:
2685 case OP_COND_EXC_NE_UN:
2686 case OP_COND_EXC_LT:
2687 case OP_COND_EXC_LT_UN:
2688 case OP_COND_EXC_GT:
2689 case OP_COND_EXC_GT_UN:
2690 case OP_COND_EXC_GE:
2691 case OP_COND_EXC_GE_UN:
2692 case OP_COND_EXC_LE:
2693 case OP_COND_EXC_LE_UN:
2694 case OP_COND_EXC_OV:
2695 case OP_COND_EXC_NO:
2697 case OP_COND_EXC_NC:
2698 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2699 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2711 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
2714 /* floating point opcodes */
2716 double d = *(double *)ins->inst_p0;
2718 if ((d == 0.0) && (mono_signbit (d) == 0)) {
2720 } else if (d == 1.0) {
2723 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
2724 x86_fld (code, NULL, TRUE);
2729 float f = *(float *)ins->inst_p0;
2731 if ((f == 0.0) && (mono_signbit (f) == 0)) {
2733 } else if (f == 1.0) {
2736 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
2737 x86_fld (code, NULL, FALSE);
2741 case OP_STORER8_MEMBASE_REG:
2742 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
2744 case OP_LOADR8_SPILL_MEMBASE:
2745 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2748 case OP_LOADR8_MEMBASE:
2749 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2751 case OP_STORER4_MEMBASE_REG:
2752 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
2754 case OP_LOADR4_MEMBASE:
2755 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2757 case CEE_CONV_R4: /* FIXME: change precision */
2759 x86_push_reg (code, ins->sreg1);
2760 x86_fild_membase (code, X86_ESP, 0, FALSE);
2761 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2763 case OP_X86_FP_LOAD_I8:
2764 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2766 case OP_X86_FP_LOAD_I4:
2767 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2769 case OP_FCONV_TO_I1:
2770 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2772 case OP_FCONV_TO_U1:
2773 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2775 case OP_FCONV_TO_I2:
2776 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2778 case OP_FCONV_TO_U2:
2779 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2781 case OP_FCONV_TO_I4:
2783 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2785 case OP_FCONV_TO_I8:
2786 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2787 x86_fnstcw_membase(code, X86_ESP, 0);
2788 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
2789 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
2790 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
2791 x86_fldcw_membase (code, X86_ESP, 2);
2792 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2793 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2794 x86_pop_reg (code, ins->dreg);
2795 x86_pop_reg (code, ins->unused);
2796 x86_fldcw_membase (code, X86_ESP, 0);
2797 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2799 case OP_LCONV_TO_R_UN: {
2800 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2803 /* load 64bit integer to FP stack */
2804 x86_push_imm (code, 0);
2805 x86_push_reg (code, ins->sreg2);
2806 x86_push_reg (code, ins->sreg1);
2807 x86_fild_membase (code, X86_ESP, 0, TRUE);
2808 /* store as 80bit FP value */
2809 x86_fst80_membase (code, X86_ESP, 0);
2811 /* test if lreg is negative */
2812 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2813 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2815 /* add correction constant mn */
2816 x86_fld80_mem (code, mn);
2817 x86_fld80_membase (code, X86_ESP, 0);
2818 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2819 x86_fst80_membase (code, X86_ESP, 0);
2821 x86_patch (br, code);
2823 x86_fld80_membase (code, X86_ESP, 0);
2824 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2828 case OP_LCONV_TO_OVF_I: {
2829 guint8 *br [3], *label [1];
2832 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2834 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2836 /* If the low word top bit is set, see if we are negative */
2837 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2838 /* We are not negative (no top bit set, check for our top word to be zero */
2839 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2840 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2843 /* throw exception */
2844 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2845 x86_jump32 (code, 0);
2847 x86_patch (br [0], code);
2848 /* our top bit is set, check that top word is 0xfffffff */
2849 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2851 x86_patch (br [1], code);
2852 /* nope, emit exception */
2853 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2854 x86_patch (br [2], label [0]);
2856 if (ins->dreg != ins->sreg1)
2857 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2861 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2864 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
2867 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
2870 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
2886 * it really doesn't make sense to inline all this code,
2887 * it's here just to show that things may not be as simple
2890 guchar *check_pos, *end_tan, *pop_jump;
2891 x86_push_reg (code, X86_EAX);
2894 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
2896 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2897 x86_fstp (code, 0); /* pop the 1.0 */
2899 x86_jump8 (code, 0);
2901 x86_fp_op (code, X86_FADD, 0);
2905 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
2907 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2910 x86_patch (pop_jump, code);
2911 x86_fstp (code, 0); /* pop the 1.0 */
2912 x86_patch (check_pos, code);
2913 x86_patch (end_tan, code);
2914 x86_pop_reg (code, X86_EAX);
2930 x86_push_reg (code, X86_EAX);
2931 /* we need to exchange ST(0) with ST(1) */
2934 /* this requires a loop, because fprem somtimes
2935 * returns a partial remainder */
2937 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
2938 /* x86_fprem1 (code); */
2941 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
2943 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
2948 x86_pop_reg (code, X86_EAX);
2952 if (cfg->opt & MONO_OPT_FCMOV) {
2953 x86_fcomip (code, 1);
2957 /* this overwrites EAX */
2958 EMIT_FPCOMPARE(code);
2959 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2962 if (cfg->opt & MONO_OPT_FCMOV) {
2963 /* zeroing the register at the start results in
2964 * shorter and faster code (we can also remove the widening op)
2966 guchar *unordered_check;
2967 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2968 x86_fcomip (code, 1);
2970 unordered_check = code;
2971 x86_branch8 (code, X86_CC_P, 0, FALSE);
2972 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
2973 x86_patch (unordered_check, code);
2976 if (ins->dreg != X86_EAX)
2977 x86_push_reg (code, X86_EAX);
2979 EMIT_FPCOMPARE(code);
2980 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2981 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2982 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2983 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2985 if (ins->dreg != X86_EAX)
2986 x86_pop_reg (code, X86_EAX);
2990 if (cfg->opt & MONO_OPT_FCMOV) {
2991 /* zeroing the register at the start results in
2992 * shorter and faster code (we can also remove the widening op)
2994 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2995 x86_fcomip (code, 1);
2997 if (ins->opcode == OP_FCLT_UN) {
2998 guchar *unordered_check = code;
2999 guchar *jump_to_end;
3000 x86_branch8 (code, X86_CC_P, 0, FALSE);
3001 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3003 x86_jump8 (code, 0);
3004 x86_patch (unordered_check, code);
3005 x86_inc_reg (code, ins->dreg);
3006 x86_patch (jump_to_end, code);
3008 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3012 if (ins->dreg != X86_EAX)
3013 x86_push_reg (code, X86_EAX);
3015 EMIT_FPCOMPARE(code);
3016 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3017 if (ins->opcode == OP_FCLT_UN) {
3018 guchar *is_not_zero_check, *end_jump;
3019 is_not_zero_check = code;
3020 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3022 x86_jump8 (code, 0);
3023 x86_patch (is_not_zero_check, code);
3024 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3026 x86_patch (end_jump, code);
3028 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3029 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3031 if (ins->dreg != X86_EAX)
3032 x86_pop_reg (code, X86_EAX);
3036 if (cfg->opt & MONO_OPT_FCMOV) {
3037 /* zeroing the register at the start results in
3038 * shorter and faster code (we can also remove the widening op)
3040 guchar *unordered_check;
3041 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3042 x86_fcomip (code, 1);
3044 if (ins->opcode == OP_FCGT) {
3045 unordered_check = code;
3046 x86_branch8 (code, X86_CC_P, 0, FALSE);
3047 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3048 x86_patch (unordered_check, code);
3050 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3054 if (ins->dreg != X86_EAX)
3055 x86_push_reg (code, X86_EAX);
3057 EMIT_FPCOMPARE(code);
3058 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3059 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3060 if (ins->opcode == OP_FCGT_UN) {
3061 guchar *is_not_zero_check, *end_jump;
3062 is_not_zero_check = code;
3063 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3065 x86_jump8 (code, 0);
3066 x86_patch (is_not_zero_check, code);
3067 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3069 x86_patch (end_jump, code);
3071 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3072 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3074 if (ins->dreg != X86_EAX)
3075 x86_pop_reg (code, X86_EAX);
3078 if (cfg->opt & MONO_OPT_FCMOV) {
3079 guchar *jump = code;
3080 x86_branch8 (code, X86_CC_P, 0, TRUE);
3081 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3082 x86_patch (jump, code);
3085 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3086 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
3089 /* Branch if C013 != 100 */
3090 if (cfg->opt & MONO_OPT_FCMOV) {
3091 /* branch if !ZF or (PF|CF) */
3092 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3093 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3094 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
3097 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3098 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3101 if (cfg->opt & MONO_OPT_FCMOV) {
3102 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3105 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3108 if (cfg->opt & MONO_OPT_FCMOV) {
3109 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3110 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3113 if (ins->opcode == OP_FBLT_UN) {
3114 guchar *is_not_zero_check, *end_jump;
3115 is_not_zero_check = code;
3116 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3118 x86_jump8 (code, 0);
3119 x86_patch (is_not_zero_check, code);
3120 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3122 x86_patch (end_jump, code);
3124 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3128 if (cfg->opt & MONO_OPT_FCMOV) {
3129 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3132 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3133 if (ins->opcode == OP_FBGT_UN) {
3134 guchar *is_not_zero_check, *end_jump;
3135 is_not_zero_check = code;
3136 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3138 x86_jump8 (code, 0);
3139 x86_patch (is_not_zero_check, code);
3140 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3142 x86_patch (end_jump, code);
3144 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3147 /* Branch if C013 == 100 or 001 */
3148 if (cfg->opt & MONO_OPT_FCMOV) {
3151 /* skip branch if C1=1 */
3153 x86_branch8 (code, X86_CC_P, 0, FALSE);
3154 /* branch if (C0 | C3) = 1 */
3155 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
3156 x86_patch (br1, code);
3159 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3160 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3161 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3162 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3165 /* Branch if C013 == 000 */
3166 if (cfg->opt & MONO_OPT_FCMOV) {
3167 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
3170 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3173 /* Branch if C013=000 or 100 */
3174 if (cfg->opt & MONO_OPT_FCMOV) {
3177 /* skip branch if C1=1 */
3179 x86_branch8 (code, X86_CC_P, 0, FALSE);
3180 /* branch if C0=0 */
3181 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
3182 x86_patch (br1, code);
3185 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
3186 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
3187 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3190 /* Branch if C013 != 001 */
3191 if (cfg->opt & MONO_OPT_FCMOV) {
3192 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3193 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
3196 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3197 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3199 case CEE_CKFINITE: {
3200 x86_push_reg (code, X86_EAX);
3203 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
3204 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3205 x86_pop_reg (code, X86_EAX);
3206 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
3210 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3211 g_assert_not_reached ();
3214 if ((code - cfg->native_code - offset) > max_len) {
3215 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3216 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3217 g_assert_not_reached ();
3223 last_offset = offset;
3228 cfg->code_len = code - cfg->native_code;
3232 mono_arch_register_lowlevel_calls (void)
3234 mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
3238 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3240 MonoJumpInfo *patch_info;
3242 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3243 unsigned char *ip = patch_info->ip.i + code;
3244 const unsigned char *target;
3246 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3248 switch (patch_info->type) {
3249 case MONO_PATCH_INFO_IP:
3250 *((gconstpointer *)(ip)) = target;
3252 case MONO_PATCH_INFO_METHOD_REL:
3253 *((gconstpointer *)(ip)) = target;
3255 case MONO_PATCH_INFO_SWITCH: {
3256 *((gconstpointer *)(ip + 2)) = target;
3257 /* we put into the table the absolute address, no need for x86_patch in this case */
3260 case MONO_PATCH_INFO_IID:
3261 *((guint32 *)(ip + 1)) = (guint32)target;
3263 case MONO_PATCH_INFO_CLASS_INIT: {
3265 /* Might already been changed to a nop */
3266 x86_call_imm (code, 0);
3269 case MONO_PATCH_INFO_R4:
3270 case MONO_PATCH_INFO_R8:
3271 *((gconstpointer *)(ip + 2)) = target;
3273 case MONO_PATCH_INFO_METHODCONST:
3274 case MONO_PATCH_INFO_CLASS:
3275 case MONO_PATCH_INFO_IMAGE:
3276 case MONO_PATCH_INFO_FIELD:
3277 case MONO_PATCH_INFO_VTABLE:
3278 case MONO_PATCH_INFO_SFLDA:
3279 case MONO_PATCH_INFO_EXC_NAME:
3280 case MONO_PATCH_INFO_LDSTR:
3281 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3282 case MONO_PATCH_INFO_LDTOKEN:
3283 *((gconstpointer *)(ip + 1)) = target;
3288 x86_patch (ip, target);
3293 mono_arch_max_epilog_size (MonoCompile *cfg)
3295 int exc_count = 0, max_epilog_size = 16;
3296 MonoJumpInfo *patch_info;
3298 if (cfg->method->save_lmf)
3299 max_epilog_size += 128;
3301 if (mono_jit_trace_calls != NULL)
3302 max_epilog_size += 50;
3304 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3305 max_epilog_size += 50;
3307 /* count the number of exception infos */
3309 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3310 if (patch_info->type == MONO_PATCH_INFO_EXC)
3315 * make sure we have enough space for exceptions
3316 * 16 is the size of two push_imm instructions and a call
3318 max_epilog_size += exc_count*16;
3320 return max_epilog_size;
3324 mono_arch_emit_prolog (MonoCompile *cfg)
3326 MonoMethod *method = cfg->method;
3328 MonoMethodSignature *sig;
3330 int alloc_size, pos, max_offset, i;
3333 cfg->code_size = MAX (((MonoMethodNormal *)method)->header->code_size * 4, 256);
3334 code = cfg->native_code = g_malloc (cfg->code_size);
3336 x86_push_reg (code, X86_EBP);
3337 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
3339 alloc_size = - cfg->stack_offset;
3342 if (method->save_lmf) {
3343 pos += sizeof (MonoLMF);
3345 /* save the current IP */
3346 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3347 x86_push_imm (code, 0);
3349 /* save all caller saved regs */
3350 x86_push_reg (code, X86_EBX);
3351 x86_push_reg (code, X86_EDI);
3352 x86_push_reg (code, X86_ESI);
3353 x86_push_reg (code, X86_EBP);
3355 /* save method info */
3356 x86_push_imm (code, method);
3358 /* get the address of lmf for the current thread */
3360 * This is performance critical so we try to use some tricks to make
3363 if (lmf_tls_offset != -1) {
3364 /* Load lmf quicky using the GS register */
3365 x86_prefix (code, X86_GS_PREFIX);
3366 x86_mov_reg_mem (code, X86_EAX, 0, 4);
3367 x86_mov_reg_membase (code, X86_EAX, X86_EAX, lmf_tls_offset, 4);
3370 #ifdef HAVE_KW_THREAD
3371 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3372 (gpointer)"mono_arch_get_lmf_addr");
3374 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3375 (gpointer)"mono_get_lmf_addr");
3377 x86_call_code (code, 0);
3381 x86_push_reg (code, X86_EAX);
3382 /* push *lfm (previous_lmf) */
3383 x86_push_membase (code, X86_EAX, 0);
3385 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3388 if (cfg->used_int_regs & (1 << X86_EBX)) {
3389 x86_push_reg (code, X86_EBX);
3393 if (cfg->used_int_regs & (1 << X86_EDI)) {
3394 x86_push_reg (code, X86_EDI);
3398 if (cfg->used_int_regs & (1 << X86_ESI)) {
3399 x86_push_reg (code, X86_ESI);
3407 /* See mono_emit_stack_alloc */
3408 #ifdef PLATFORM_WIN32
3409 guint32 remaining_size = alloc_size;
3410 while (remaining_size >= 0x1000) {
3411 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
3412 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
3413 remaining_size -= 0x1000;
3416 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
3418 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
3422 /* compute max_offset in order to use short forward jumps */
3424 if (cfg->opt & MONO_OPT_BRANCH) {
3425 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3426 MonoInst *ins = bb->code;
3427 bb->max_offset = max_offset;
3429 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3433 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3439 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3440 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3442 /* load arguments allocated to register from the stack */
3443 sig = method->signature;
3446 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3447 inst = cfg->varinfo [pos];
3448 if (inst->opcode == OP_REGVAR) {
3449 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
3450 if (cfg->verbose_level > 2)
3451 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3456 cfg->code_len = code - cfg->native_code;
3462 mono_arch_emit_epilog (MonoCompile *cfg)
3464 MonoJumpInfo *patch_info;
3465 MonoMethod *method = cfg->method;
3466 MonoMethodSignature *sig = method->signature;
3468 guint32 stack_to_pop;
3471 code = cfg->native_code + cfg->code_len;
3473 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3474 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3476 /* the code restoring the registers must be kept in sync with CEE_JMP */
3479 if (method->save_lmf) {
3480 pos = -sizeof (MonoLMF);
3482 if (cfg->used_int_regs & (1 << X86_EBX)) {
3485 if (cfg->used_int_regs & (1 << X86_EDI)) {
3488 if (cfg->used_int_regs & (1 << X86_ESI)) {
3494 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3496 if (method->save_lmf) {
3497 /* ebx = previous_lmf */
3498 x86_pop_reg (code, X86_EBX);
3500 x86_pop_reg (code, X86_EDI);
3501 /* *(lmf) = previous_lmf */
3502 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
3504 /* discard method info */
3505 x86_pop_reg (code, X86_ESI);
3507 /* restore caller saved regs */
3508 x86_pop_reg (code, X86_EBP);
3509 x86_pop_reg (code, X86_ESI);
3510 x86_pop_reg (code, X86_EDI);
3511 x86_pop_reg (code, X86_EBX);
3515 if (cfg->used_int_regs & (1 << X86_ESI)) {
3516 x86_pop_reg (code, X86_ESI);
3518 if (cfg->used_int_regs & (1 << X86_EDI)) {
3519 x86_pop_reg (code, X86_EDI);
3521 if (cfg->used_int_regs & (1 << X86_EBX)) {
3522 x86_pop_reg (code, X86_EBX);
3528 if (CALLCONV_IS_STDCALL (sig->call_convention)) {
3529 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
3531 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
3532 } else if (MONO_TYPE_ISSTRUCT (cfg->method->signature->ret))
3538 x86_ret_imm (code, stack_to_pop);
3542 /* add code to raise exceptions */
3543 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3544 switch (patch_info->type) {
3545 case MONO_PATCH_INFO_EXC:
3546 x86_patch (patch_info->ip.i + cfg->native_code, code);
3547 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);
3548 x86_push_imm (code, patch_info->data.target);
3549 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_METHOD_REL, (gpointer)patch_info->ip.i);
3550 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
3551 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3552 patch_info->data.name = "mono_arch_throw_exception_by_name";
3553 patch_info->ip.i = code - cfg->native_code;
3554 x86_jump_code (code, 0);
3562 cfg->code_len = code - cfg->native_code;
3564 g_assert (cfg->code_len < cfg->code_size);
3569 mono_arch_flush_icache (guint8 *code, gint size)
3575 mono_arch_flush_register_windows (void)
3580 * Support for fast access to the thread-local lmf structure using the GS
3581 * segment register on NPTL + kernel 2.6.x.
3584 static gboolean tls_offset_inited = FALSE;
3586 #ifdef HAVE_KW_THREAD
3587 static __thread gpointer mono_lmf_addr;
3591 mono_arch_get_lmf_addr (void)
3593 #ifdef HAVE_KW_THREAD
3594 return mono_lmf_addr;
3596 g_assert_not_reached ();
3602 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3604 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3605 pthread_t self = pthread_self();
3606 pthread_attr_t attr;
3607 void *staddr = NULL;
3609 struct sigaltstack sa;
3612 if (!tls_offset_inited) {
3615 tls_offset_inited = TRUE;
3617 if (getenv ("MONO_NPTL")) {
3619 * Determine the offset of mono_lfm_addr inside the TLS structures
3620 * by disassembling the function above.
3622 code = (guint8*)&mono_arch_get_lmf_addr;
3624 /* This is generated by gcc 3.3.2 */
3625 if ((code [0] == 0x55) && (code [1] == 0x89) && (code [2] == 0xe5) &&
3626 (code [3] == 0x65) && (code [4] == 0xa1) && (code [5] == 0x00) &&
3627 (code [6] == 0x00) && (code [7] == 0x00) && (code [8] == 0x00) &&
3628 (code [9] == 0x8b) && (code [10] == 0x80)) {
3629 lmf_tls_offset = *(int*)&(code [11]);
3634 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3636 /* Determine stack boundaries */
3637 if (!mono_running_on_valgrind ()) {
3638 #ifdef HAVE_PTHREAD_GETATTR_NP
3639 pthread_getattr_np( self, &attr );
3641 #ifdef HAVE_PTHREAD_ATTR_GET_NP
3642 pthread_attr_get_np( self, &attr );
3644 #error "Not implemented"
3647 pthread_attr_getstack( &attr, &staddr, &stsize );
3651 * staddr seems to be wrong for the main thread, so we keep the value in
3654 tls->stack_size = stsize;
3656 /* Setup an alternate signal stack */
3657 tls->signal_stack = g_malloc (SIGNAL_STACK_SIZE);
3658 tls->signal_stack_size = SIGNAL_STACK_SIZE;
3660 sa.ss_sp = tls->signal_stack;
3661 sa.ss_size = SIGNAL_STACK_SIZE;
3662 sa.ss_flags = SS_ONSTACK;
3663 sigaltstack (&sa, NULL);
3666 #ifdef HAVE_KW_THREAD
3667 mono_lmf_addr = &tls->lmf;
3672 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3674 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3675 struct sigaltstack sa;
3677 sa.ss_sp = tls->signal_stack;
3678 sa.ss_size = SIGNAL_STACK_SIZE;
3679 sa.ss_flags = SS_DISABLE;
3680 sigaltstack (&sa, NULL);
3682 if (tls->signal_stack)
3683 g_free (tls->signal_stack);
3688 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3691 /* add the this argument */
3692 if (this_reg != -1) {
3694 MONO_INST_NEW (cfg, this, OP_OUTARG);
3695 this->type = this_type;
3696 this->sreg1 = this_reg;
3697 mono_bblock_add_inst (cfg->cbb, this);
3702 MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
3703 vtarg->type = STACK_MP;
3704 vtarg->sreg1 = vt_reg;
3705 mono_bblock_add_inst (cfg->cbb, vtarg);
3711 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3713 if (cmethod->klass == mono_defaults.math_class) {
3714 if (strcmp (cmethod->name, "Sin") == 0)
3716 else if (strcmp (cmethod->name, "Cos") == 0)
3718 else if (strcmp (cmethod->name, "Tan") == 0)
3720 else if (strcmp (cmethod->name, "Atan") == 0)
3722 else if (strcmp (cmethod->name, "Sqrt") == 0)
3724 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
3727 /* OP_FREM is not IEEE compatible */
3728 else if (strcmp (cmethod->name, "IEEERemainder") == 0)
3741 mono_arch_print_tree (MonoInst *tree, int arity)