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 #ifndef PLATFORM_WIN32
20 #include <mono/metadata/appdomain.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/threads.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/utils/mono-math.h>
29 #include "cpu-pentium.h"
31 /* On windows, these hold the key returned by TlsAlloc () */
32 static gint lmf_tls_offset = -1;
33 static gint appdomain_tls_offset = -1;
34 static gint thread_tls_offset = -1;
36 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
39 /* Under windows, the default pinvoke calling convention is stdcall */
40 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
42 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
45 #define SIGNAL_STACK_SIZE (64 * 1024)
47 #define NOT_IMPLEMENTED g_assert_not_reached ()
50 mono_arch_regname (int reg) {
52 case X86_EAX: return "%eax";
53 case X86_EBX: return "%ebx";
54 case X86_ECX: return "%ecx";
55 case X86_EDX: return "%edx";
56 case X86_ESP: return "%esp"; case X86_EBP: return "%ebp";
57 case X86_EDI: return "%edi";
58 case X86_ESI: return "%esi";
64 mono_arch_fregname (int reg) {
84 /* Only if storage == ArgValuetypeInReg */
85 ArgStorage pair_storage [2];
94 gboolean need_stack_align;
102 #define FLOAT_PARAM_REGS 0
104 static X86_Reg_No param_regs [] = { 0 };
106 #ifdef PLATFORM_WIN32
107 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
111 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
113 ainfo->offset = *stack_size;
115 if (*gr >= PARAM_REGS) {
116 ainfo->storage = ArgOnStack;
117 (*stack_size) += sizeof (gpointer);
120 ainfo->storage = ArgInIReg;
121 ainfo->reg = param_regs [*gr];
127 add_general_pair (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
129 ainfo->offset = *stack_size;
131 g_assert (PARAM_REGS == 0);
133 ainfo->storage = ArgOnStack;
134 (*stack_size) += sizeof (gpointer) * 2;
138 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
140 ainfo->offset = *stack_size;
142 if (*gr >= FLOAT_PARAM_REGS) {
143 ainfo->storage = ArgOnStack;
144 (*stack_size) += sizeof (gpointer);
147 /* A double register */
149 ainfo->storage = ArgInDoubleSSEReg;
151 ainfo->storage = ArgInFloatSSEReg;
159 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
161 guint32 *gr, guint32 *fr, guint32 *stack_size)
166 klass = mono_class_from_mono_type (type);
168 size = mono_type_native_stack_size (&klass->byval_arg, NULL);
170 size = mono_type_stack_size (&klass->byval_arg, NULL);
172 #ifdef PLATFORM_WIN32
173 if (sig->pinvoke && is_return) {
174 MonoMarshalType *info;
177 * the exact rules are not very well documented, the code below seems to work with the
178 * code generated by gcc 3.3.3 -mno-cygwin.
180 info = mono_marshal_load_type_info (klass);
183 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
185 /* Special case structs with only a float member */
186 if ((info->native_size == 8) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R8)) {
187 ainfo->storage = ArgValuetypeInReg;
188 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
191 if ((info->native_size == 4) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R4)) {
192 ainfo->storage = ArgValuetypeInReg;
193 ainfo->pair_storage [0] = ArgOnFloatFpStack;
196 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
197 ainfo->storage = ArgValuetypeInReg;
198 ainfo->pair_storage [0] = ArgInIReg;
199 ainfo->pair_regs [0] = return_regs [0];
200 if (info->native_size > 4) {
201 ainfo->pair_storage [1] = ArgInIReg;
202 ainfo->pair_regs [1] = return_regs [1];
209 ainfo->offset = *stack_size;
210 ainfo->storage = ArgOnStack;
211 *stack_size += ALIGN_TO (size, sizeof (gpointer));
217 * Obtain information about a call according to the calling convention.
218 * For x86 ELF, see the "System V Application Binary Interface Intel386
219 * Architecture Processor Supplment, Fourth Edition" document for more
221 * For x86 win32, see ???.
224 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
228 int n = sig->hasthis + sig->param_count;
229 guint32 stack_size = 0;
232 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
239 ret_type = mono_type_get_underlying_type (sig->ret);
240 switch (ret_type->type) {
241 case MONO_TYPE_BOOLEAN:
252 case MONO_TYPE_FNPTR:
253 case MONO_TYPE_CLASS:
254 case MONO_TYPE_OBJECT:
255 case MONO_TYPE_SZARRAY:
256 case MONO_TYPE_ARRAY:
257 case MONO_TYPE_STRING:
258 cinfo->ret.storage = ArgInIReg;
259 cinfo->ret.reg = X86_EAX;
263 cinfo->ret.storage = ArgInIReg;
264 cinfo->ret.reg = X86_EAX;
267 cinfo->ret.storage = ArgOnFloatFpStack;
270 cinfo->ret.storage = ArgOnDoubleFpStack;
272 case MONO_TYPE_VALUETYPE: {
273 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
275 add_valuetype (sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
276 if (cinfo->ret.storage == ArgOnStack)
277 /* The caller passes the address where the value is stored */
278 add_general (&gr, &stack_size, &cinfo->ret);
281 case MONO_TYPE_TYPEDBYREF:
282 /* Same as a valuetype with size 24 */
283 add_general (&gr, &stack_size, &cinfo->ret);
287 cinfo->ret.storage = ArgNone;
290 g_error ("Can't handle as return value 0x%x", sig->ret->type);
296 add_general (&gr, &stack_size, cinfo->args + 0);
298 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
300 fr = FLOAT_PARAM_REGS;
302 /* Emit the signature cookie just before the implicit arguments */
303 add_general (&gr, &stack_size, &cinfo->sig_cookie);
306 for (i = 0; i < sig->param_count; ++i) {
307 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
310 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
311 /* We allways pass the sig cookie on the stack for simplicity */
313 * Prevent implicit arguments + the sig cookie from being passed
317 fr = FLOAT_PARAM_REGS;
319 /* Emit the signature cookie just before the implicit arguments */
320 add_general (&gr, &stack_size, &cinfo->sig_cookie);
323 if (sig->params [i]->byref) {
324 add_general (&gr, &stack_size, ainfo);
327 ptype = mono_type_get_underlying_type (sig->params [i]);
328 switch (ptype->type) {
329 case MONO_TYPE_BOOLEAN:
332 add_general (&gr, &stack_size, ainfo);
337 add_general (&gr, &stack_size, ainfo);
341 add_general (&gr, &stack_size, ainfo);
346 case MONO_TYPE_FNPTR:
347 case MONO_TYPE_CLASS:
348 case MONO_TYPE_OBJECT:
349 case MONO_TYPE_STRING:
350 case MONO_TYPE_SZARRAY:
351 case MONO_TYPE_ARRAY:
352 add_general (&gr, &stack_size, ainfo);
354 case MONO_TYPE_VALUETYPE:
355 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
357 case MONO_TYPE_TYPEDBYREF:
358 stack_size += sizeof (MonoTypedRef);
359 ainfo->storage = ArgOnStack;
363 add_general_pair (&gr, &stack_size, ainfo);
366 add_float (&fr, &stack_size, ainfo, FALSE);
369 add_float (&fr, &stack_size, ainfo, TRUE);
372 g_error ("unexpected type 0x%x", ptype->type);
373 g_assert_not_reached ();
377 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
379 fr = FLOAT_PARAM_REGS;
381 /* Emit the signature cookie just before the implicit arguments */
382 add_general (&gr, &stack_size, &cinfo->sig_cookie);
385 cinfo->stack_usage = stack_size;
386 cinfo->reg_usage = gr;
387 cinfo->freg_usage = fr;
392 * mono_arch_get_argument_info:
393 * @csig: a method signature
394 * @param_count: the number of parameters to consider
395 * @arg_info: an array to store the result infos
397 * Gathers information on parameters such as size, alignment and
398 * padding. arg_info should be large enought to hold param_count + 1 entries.
400 * Returns the size of the activation frame.
403 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
405 int k, frame_size = 0;
406 int size, align, pad;
410 cinfo = get_call_info (csig, FALSE);
412 if (MONO_TYPE_ISSTRUCT (csig->ret) && (cinfo->ret.storage == ArgOnStack)) {
413 frame_size += sizeof (gpointer);
417 arg_info [0].offset = offset;
420 frame_size += sizeof (gpointer);
424 arg_info [0].size = frame_size;
426 for (k = 0; k < param_count; k++) {
429 size = mono_type_native_stack_size (csig->params [k], &align);
431 size = mono_type_stack_size (csig->params [k], &align);
433 /* ignore alignment for now */
436 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
437 arg_info [k].pad = pad;
439 arg_info [k + 1].pad = 0;
440 arg_info [k + 1].size = size;
442 arg_info [k + 1].offset = offset;
446 align = MONO_ARCH_FRAME_ALIGNMENT;
447 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
448 arg_info [k].pad = pad;
455 static const guchar cpuid_impl [] = {
456 0x55, /* push %ebp */
457 0x89, 0xe5, /* mov %esp,%ebp */
458 0x53, /* push %ebx */
459 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
460 0x0f, 0xa2, /* cpuid */
461 0x50, /* push %eax */
462 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
463 0x89, 0x18, /* mov %ebx,(%eax) */
464 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
465 0x89, 0x08, /* mov %ecx,(%eax) */
466 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
467 0x89, 0x10, /* mov %edx,(%eax) */
469 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
470 0x89, 0x02, /* mov %eax,(%edx) */
476 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
479 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
483 __asm__ __volatile__ (
486 "movl %%eax, %%edx\n"
487 "xorl $0x200000, %%eax\n"
492 "xorl %%edx, %%eax\n"
493 "andl $0x200000, %%eax\n"
515 /* Have to use the code manager to get around WinXP DEP */
516 MonoCodeManager *codeman = mono_code_manager_new_dynamic ();
518 void *ptr = mono_code_manager_reserve (codeman, sizeof (cpuid_impl));
519 memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
521 func = (CpuidFunc)ptr;
522 func (id, p_eax, p_ebx, p_ecx, p_edx);
524 mono_code_manager_destroy (codeman);
527 * We use this approach because of issues with gcc and pic code, see:
528 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
529 __asm__ __volatile__ ("cpuid"
530 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
539 * Initialize the cpu to execute managed code.
542 mono_arch_cpu_init (void)
544 /* spec compliance requires running with double precision */
548 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
549 fpcw &= ~X86_FPCW_PRECC_MASK;
550 fpcw |= X86_FPCW_PREC_DOUBLE;
551 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
552 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
554 _control87 (_PC_53, MCW_PC);
559 * This function returns the optimizations supported on this cpu.
562 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
564 int eax, ebx, ecx, edx;
568 /* Feature Flags function, flags returned in EDX. */
569 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
570 if (edx & (1 << 15)) {
571 opts |= MONO_OPT_CMOV;
573 opts |= MONO_OPT_FCMOV;
575 *exclude_mask |= MONO_OPT_FCMOV;
577 *exclude_mask |= MONO_OPT_CMOV;
583 * Determine whenever the trap whose info is in SIGINFO is caused by
587 mono_arch_is_int_overflow (void *sigctx, void *info)
592 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
594 ip = (guint8*)ctx.eip;
596 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
600 switch (x86_modrm_rm (ip [1])) {
620 g_assert_not_reached ();
632 is_regsize_var (MonoType *t) {
635 switch (mono_type_get_underlying_type (t)->type) {
641 case MONO_TYPE_FNPTR:
643 case MONO_TYPE_OBJECT:
644 case MONO_TYPE_STRING:
645 case MONO_TYPE_CLASS:
646 case MONO_TYPE_SZARRAY:
647 case MONO_TYPE_ARRAY:
649 case MONO_TYPE_VALUETYPE:
656 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
661 for (i = 0; i < cfg->num_varinfo; i++) {
662 MonoInst *ins = cfg->varinfo [i];
663 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
666 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
669 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
670 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
673 /* we dont allocate I1 to registers because there is no simply way to sign extend
674 * 8bit quantities in caller saved registers on x86 */
675 if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) ||
676 (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
677 (ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
678 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
679 g_assert (i == vmv->idx);
680 vars = g_list_prepend (vars, vmv);
684 vars = mono_varlist_sort (cfg, vars, 0);
690 mono_arch_get_global_int_regs (MonoCompile *cfg)
694 /* we can use 3 registers for global allocation */
695 regs = g_list_prepend (regs, (gpointer)X86_EBX);
696 regs = g_list_prepend (regs, (gpointer)X86_ESI);
697 regs = g_list_prepend (regs, (gpointer)X86_EDI);
703 * mono_arch_regalloc_cost:
705 * Return the cost, in number of memory references, of the action of
706 * allocating the variable VMV into a register during global register
710 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
712 MonoInst *ins = cfg->varinfo [vmv->idx];
714 if (cfg->method->save_lmf)
715 /* The register is already saved */
716 return (ins->opcode == OP_ARG) ? 1 : 0;
718 /* push+pop+possible load if it is an argument */
719 return (ins->opcode == OP_ARG) ? 3 : 2;
723 * Set var information according to the calling convention. X86 version.
724 * The locals var stuff should most likely be split in another method.
727 mono_arch_allocate_vars (MonoCompile *cfg)
729 MonoMethodSignature *sig;
730 MonoMethodHeader *header;
732 guint32 locals_stack_size, locals_stack_align;
733 int i, offset, curinst, size, align;
737 header = mono_method_get_header (cfg->method);
738 sig = mono_method_signature (cfg->method);
743 cinfo = get_call_info (sig, FALSE);
745 switch (cinfo->ret.storage) {
747 cfg->ret->opcode = OP_REGOFFSET;
748 cfg->ret->inst_basereg = X86_EBP;
749 cfg->ret->inst_offset = offset;
750 offset += sizeof (gpointer);
752 case ArgValuetypeInReg:
755 cfg->ret->opcode = OP_REGVAR;
756 cfg->ret->inst_c0 = cinfo->ret.reg;
759 case ArgOnFloatFpStack:
760 case ArgOnDoubleFpStack:
763 g_assert_not_reached ();
767 inst = cfg->varinfo [curinst];
768 if (inst->opcode != OP_REGVAR) {
769 inst->opcode = OP_REGOFFSET;
770 inst->inst_basereg = X86_EBP;
772 inst->inst_offset = offset;
773 offset += sizeof (gpointer);
777 if (sig->call_convention == MONO_CALL_VARARG) {
778 cfg->sig_cookie = offset;
779 offset += sizeof (gpointer);
782 for (i = 0; i < sig->param_count; ++i) {
783 inst = cfg->varinfo [curinst];
784 if (inst->opcode != OP_REGVAR) {
785 inst->opcode = OP_REGOFFSET;
786 inst->inst_basereg = X86_EBP;
788 inst->inst_offset = offset;
789 size = mono_type_size (sig->params [i], &align);
798 /* reserve space to save LMF and caller saved registers */
800 if (cfg->method->save_lmf) {
801 offset += sizeof (MonoLMF);
803 if (cfg->used_int_regs & (1 << X86_EBX)) {
807 if (cfg->used_int_regs & (1 << X86_EDI)) {
811 if (cfg->used_int_regs & (1 << X86_ESI)) {
816 switch (cinfo->ret.storage) {
817 case ArgValuetypeInReg:
818 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
820 cfg->ret->opcode = OP_REGOFFSET;
821 cfg->ret->inst_basereg = X86_EBP;
822 cfg->ret->inst_offset = - offset;
828 /* Allocate locals */
829 offsets = mono_allocate_stack_slots (cfg, &locals_stack_size, &locals_stack_align);
830 if (locals_stack_align) {
831 offset += (locals_stack_align - 1);
832 offset &= ~(locals_stack_align - 1);
834 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
835 if (offsets [i] != -1) {
836 MonoInst *inst = cfg->varinfo [i];
837 inst->opcode = OP_REGOFFSET;
838 inst->inst_basereg = X86_EBP;
839 inst->inst_offset = - (offset + offsets [i]);
840 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
844 offset += locals_stack_size;
846 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
847 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
851 cfg->frame_reg = MONO_ARCH_BASEREG;
853 cfg->stack_offset = offset;
857 mono_arch_create_vars (MonoCompile *cfg)
859 MonoMethodSignature *sig;
862 sig = mono_method_signature (cfg->method);
864 cinfo = get_call_info (sig, FALSE);
866 if (cinfo->ret.storage == ArgValuetypeInReg)
867 cfg->ret_var_is_local = TRUE;
872 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
873 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
877 * take the arguments and generate the arch-specific
878 * instructions to properly call the function in call.
879 * This includes pushing, moving arguments to the right register
881 * Issue: who does the spilling if needed, and when?
884 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
886 MonoMethodSignature *sig;
887 int i, n, stack_size, type;
892 /* add the vararg cookie before the non-implicit args */
893 if (call->signature->call_convention == MONO_CALL_VARARG) {
895 /* FIXME: Add support for signature tokens to AOT */
896 cfg->disable_aot = TRUE;
897 MONO_INST_NEW (cfg, arg, OP_OUTARG);
898 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
899 sig_arg->inst_p0 = call->signature;
900 arg->inst_left = sig_arg;
901 arg->type = STACK_PTR;
902 /* prepend, so they get reversed */
903 arg->next = call->out_args;
904 call->out_args = arg;
905 stack_size += sizeof (gpointer);
907 sig = call->signature;
908 n = sig->param_count + sig->hasthis;
910 cinfo = get_call_info (sig, FALSE);
912 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
913 if (cinfo->ret.storage == ArgOnStack)
914 stack_size += sizeof (gpointer);
917 for (i = 0; i < n; ++i) {
918 if (is_virtual && i == 0) {
919 /* the argument will be attached to the call instrucion */
923 MONO_INST_NEW (cfg, arg, OP_OUTARG);
925 arg->cil_code = in->cil_code;
927 arg->type = in->type;
928 /* prepend, so they get reversed */
929 arg->next = call->out_args;
930 call->out_args = arg;
931 if (i >= sig->hasthis) {
932 MonoType *t = sig->params [i - sig->hasthis];
933 ptype = mono_type_get_underlying_type (t);
938 /* FIXME: validate arguments... */
942 case MONO_TYPE_BOOLEAN:
950 case MONO_TYPE_STRING:
951 case MONO_TYPE_CLASS:
952 case MONO_TYPE_OBJECT:
954 case MONO_TYPE_FNPTR:
955 case MONO_TYPE_ARRAY:
956 case MONO_TYPE_SZARRAY:
965 arg->opcode = OP_OUTARG_R4;
969 arg->opcode = OP_OUTARG_R8;
971 case MONO_TYPE_VALUETYPE: {
974 size = mono_type_native_stack_size (&in->klass->byval_arg, NULL);
976 size = mono_type_stack_size (&in->klass->byval_arg, NULL);
979 arg->opcode = OP_OUTARG_VT;
980 arg->klass = in->klass;
981 arg->unused = sig->pinvoke;
982 arg->inst_imm = size;
985 case MONO_TYPE_TYPEDBYREF:
986 stack_size += sizeof (MonoTypedRef);
987 arg->opcode = OP_OUTARG_VT;
988 arg->klass = in->klass;
989 arg->unused = sig->pinvoke;
990 arg->inst_imm = sizeof (MonoTypedRef);
993 g_error ("unknown type 0x%02x in mono_arch_call_opcode\n", type);
996 /* the this argument */
1002 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1003 if (cinfo->ret.storage == ArgValuetypeInReg) {
1004 MonoInst *zero_inst;
1006 * After the call, the struct is in registers, but needs to be saved to the memory pointed
1007 * to by vt_arg in this_vret_args. This means that vt_arg needs to be saved somewhere
1008 * before calling the function. So we add a dummy instruction to represent pushing the
1009 * struct return address to the stack. The return address will be saved to this stack slot
1010 * by the code emitted in this_vret_args.
1012 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1013 MONO_INST_NEW (cfg, zero_inst, OP_ICONST);
1014 zero_inst->inst_p0 = 0;
1015 arg->inst_left = zero_inst;
1016 arg->type = STACK_PTR;
1017 /* prepend, so they get reversed */
1018 arg->next = call->out_args;
1019 call->out_args = arg;
1022 /* if the function returns a struct, the called method already does a ret $0x4 */
1023 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
1027 call->stack_usage = stack_size;
1031 * should set more info in call, such as the stack space
1032 * used by the args that needs to be added back to esp
1039 * Allow tracing to work with this interface (with an optional argument)
1042 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1046 /* if some args are passed in registers, we need to save them here */
1047 x86_push_reg (code, X86_EBP);
1049 if (cfg->compile_aot) {
1050 x86_push_imm (code, cfg->method);
1051 x86_mov_reg_imm (code, X86_EAX, func);
1052 x86_call_reg (code, X86_EAX);
1054 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1055 x86_push_imm (code, cfg->method);
1056 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1057 x86_call_code (code, 0);
1059 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1073 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1076 int arg_size = 0, save_mode = SAVE_NONE;
1077 MonoMethod *method = cfg->method;
1079 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
1080 case MONO_TYPE_VOID:
1081 /* special case string .ctor icall */
1082 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1083 save_mode = SAVE_EAX;
1085 save_mode = SAVE_NONE;
1089 save_mode = SAVE_EAX_EDX;
1093 save_mode = SAVE_FP;
1095 case MONO_TYPE_VALUETYPE:
1096 save_mode = SAVE_STRUCT;
1099 save_mode = SAVE_EAX;
1103 switch (save_mode) {
1105 x86_push_reg (code, X86_EDX);
1106 x86_push_reg (code, X86_EAX);
1107 if (enable_arguments) {
1108 x86_push_reg (code, X86_EDX);
1109 x86_push_reg (code, X86_EAX);
1114 x86_push_reg (code, X86_EAX);
1115 if (enable_arguments) {
1116 x86_push_reg (code, X86_EAX);
1121 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1122 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1123 if (enable_arguments) {
1124 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1125 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1130 if (enable_arguments) {
1131 x86_push_membase (code, X86_EBP, 8);
1140 if (cfg->compile_aot) {
1141 x86_push_imm (code, method);
1142 x86_mov_reg_imm (code, X86_EAX, func);
1143 x86_call_reg (code, X86_EAX);
1145 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1146 x86_push_imm (code, method);
1147 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1148 x86_call_code (code, 0);
1150 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1152 switch (save_mode) {
1154 x86_pop_reg (code, X86_EAX);
1155 x86_pop_reg (code, X86_EDX);
1158 x86_pop_reg (code, X86_EAX);
1161 x86_fld_membase (code, X86_ESP, 0, TRUE);
1162 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1172 #define EMIT_COND_BRANCH(ins,cond,sign) \
1173 if (ins->flags & MONO_INST_BRLABEL) { \
1174 if (ins->inst_i0->inst_c0) { \
1175 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
1177 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1178 if ((cfg->opt & MONO_OPT_BRANCH) && \
1179 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
1180 x86_branch8 (code, cond, 0, sign); \
1182 x86_branch32 (code, cond, 0, sign); \
1185 if (ins->inst_true_bb->native_offset) { \
1186 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1188 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1189 if ((cfg->opt & MONO_OPT_BRANCH) && \
1190 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1191 x86_branch8 (code, cond, 0, sign); \
1193 x86_branch32 (code, cond, 0, sign); \
1197 /* emit an exception if condition is fail */
1198 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1200 mono_add_patch_info (cfg, code - cfg->native_code, \
1201 MONO_PATCH_INFO_EXC, exc_name); \
1202 x86_branch32 (code, cond, 0, signed); \
1205 #define EMIT_FPCOMPARE(code) do { \
1206 x86_fcompp (code); \
1207 x86_fnstsw (code); \
1212 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1214 if (cfg->compile_aot) {
1215 guint32 got_reg = X86_EAX;
1217 if (cfg->compile_aot) {
1219 * Since the patches are generated by the back end, there is
1220 * no way to generate a got_var at this point.
1222 g_assert (cfg->got_var);
1224 if (cfg->got_var->opcode == OP_REGOFFSET)
1225 x86_mov_reg_membase (code, X86_EAX, cfg->got_var->inst_basereg, cfg->got_var->inst_offset, 4);
1227 got_reg = cfg->got_var->dreg;
1230 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1231 x86_call_membase (code, got_reg, 0xf0f0f0f0);
1234 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1235 x86_call_code (code, 0);
1241 /* FIXME: Add more instructions */
1242 #define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_STORE_MEMBASE_IMM) || ((ins)->opcode == OP_STOREI4_MEMBASE_REG))
1245 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1247 MonoInst *ins, *last_ins = NULL;
1252 switch (ins->opcode) {
1254 /* reg = 0 -> XOR (reg, reg) */
1255 /* XOR sets cflags on x86, so we cant do it always */
1256 if (ins->inst_c0 == 0 && ins->next && INST_IGNORES_CFLAGS (ins->next)) {
1257 ins->opcode = CEE_XOR;
1258 ins->sreg1 = ins->dreg;
1259 ins->sreg2 = ins->dreg;
1263 /* remove unnecessary multiplication with 1 */
1264 if (ins->inst_imm == 1) {
1265 if (ins->dreg != ins->sreg1) {
1266 ins->opcode = OP_MOVE;
1268 last_ins->next = ins->next;
1274 case OP_COMPARE_IMM:
1275 /* OP_COMPARE_IMM (reg, 0)
1277 * OP_X86_TEST_NULL (reg)
1280 ins->opcode = OP_X86_TEST_NULL;
1282 case OP_X86_COMPARE_MEMBASE_IMM:
1284 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1285 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1287 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1288 * OP_COMPARE_IMM reg, imm
1290 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1292 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1293 ins->inst_basereg == last_ins->inst_destbasereg &&
1294 ins->inst_offset == last_ins->inst_offset) {
1295 ins->opcode = OP_COMPARE_IMM;
1296 ins->sreg1 = last_ins->sreg1;
1298 /* check if we can remove cmp reg,0 with test null */
1300 ins->opcode = OP_X86_TEST_NULL;
1304 case OP_LOAD_MEMBASE:
1305 case OP_LOADI4_MEMBASE:
1307 * Note: if reg1 = reg2 the load op is removed
1309 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1310 * OP_LOAD_MEMBASE offset(basereg), reg2
1312 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1313 * OP_MOVE reg1, reg2
1315 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1316 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1317 ins->inst_basereg == last_ins->inst_destbasereg &&
1318 ins->inst_offset == last_ins->inst_offset) {
1319 if (ins->dreg == last_ins->sreg1) {
1320 last_ins->next = ins->next;
1324 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1325 ins->opcode = OP_MOVE;
1326 ins->sreg1 = last_ins->sreg1;
1330 * Note: reg1 must be different from the basereg in the second load
1331 * Note: if reg1 = reg2 is equal then second load is removed
1333 * OP_LOAD_MEMBASE offset(basereg), reg1
1334 * OP_LOAD_MEMBASE offset(basereg), reg2
1336 * OP_LOAD_MEMBASE offset(basereg), reg1
1337 * OP_MOVE reg1, reg2
1339 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1340 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1341 ins->inst_basereg != last_ins->dreg &&
1342 ins->inst_basereg == last_ins->inst_basereg &&
1343 ins->inst_offset == last_ins->inst_offset) {
1345 if (ins->dreg == last_ins->dreg) {
1346 last_ins->next = ins->next;
1350 ins->opcode = OP_MOVE;
1351 ins->sreg1 = last_ins->dreg;
1354 //g_assert_not_reached ();
1358 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1359 * OP_LOAD_MEMBASE offset(basereg), reg
1361 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1362 * OP_ICONST reg, imm
1364 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1365 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1366 ins->inst_basereg == last_ins->inst_destbasereg &&
1367 ins->inst_offset == last_ins->inst_offset) {
1368 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1369 ins->opcode = OP_ICONST;
1370 ins->inst_c0 = last_ins->inst_imm;
1371 g_assert_not_reached (); // check this rule
1375 case OP_LOADU1_MEMBASE:
1376 case OP_LOADI1_MEMBASE:
1378 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1379 * OP_LOAD_MEMBASE offset(basereg), reg2
1381 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1382 * CONV_I2/U2 reg1, reg2
1384 if (last_ins && X86_IS_BYTE_REG (last_ins->sreg1) &&
1385 (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1386 ins->inst_basereg == last_ins->inst_destbasereg &&
1387 ins->inst_offset == last_ins->inst_offset) {
1388 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? CEE_CONV_I1 : CEE_CONV_U1;
1389 ins->sreg1 = last_ins->sreg1;
1392 case OP_LOADU2_MEMBASE:
1393 case OP_LOADI2_MEMBASE:
1395 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1396 * OP_LOAD_MEMBASE offset(basereg), reg2
1398 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1399 * CONV_I2/U2 reg1, reg2
1401 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1402 ins->inst_basereg == last_ins->inst_destbasereg &&
1403 ins->inst_offset == last_ins->inst_offset) {
1404 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? CEE_CONV_I2 : CEE_CONV_U2;
1405 ins->sreg1 = last_ins->sreg1;
1416 if (ins->dreg == ins->sreg1) {
1418 last_ins->next = ins->next;
1425 * OP_MOVE sreg, dreg
1426 * OP_MOVE dreg, sreg
1428 if (last_ins && last_ins->opcode == OP_MOVE &&
1429 ins->sreg1 == last_ins->dreg &&
1430 ins->dreg == last_ins->sreg1) {
1431 last_ins->next = ins->next;
1437 case OP_X86_PUSH_MEMBASE:
1438 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1439 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1440 ins->inst_basereg == last_ins->inst_destbasereg &&
1441 ins->inst_offset == last_ins->inst_offset) {
1442 ins->opcode = OP_X86_PUSH;
1443 ins->sreg1 = last_ins->sreg1;
1450 bb->last_ins = last_ins;
1454 branch_cc_table [] = {
1455 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1456 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1457 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1460 static const char*const * ins_spec = pentium_desc;
1462 /*#include "cprop.c"*/
1464 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1466 mono_local_regalloc (cfg, bb);
1469 static unsigned char*
1470 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1472 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1473 x86_fnstcw_membase(code, X86_ESP, 0);
1474 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1475 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1476 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1477 x86_fldcw_membase (code, X86_ESP, 2);
1479 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1480 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1481 x86_pop_reg (code, dreg);
1482 /* FIXME: need the high register
1483 * x86_pop_reg (code, dreg_high);
1486 x86_push_reg (code, X86_EAX); // SP = SP - 4
1487 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1488 x86_pop_reg (code, dreg);
1490 x86_fldcw_membase (code, X86_ESP, 0);
1491 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1494 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1496 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1500 static unsigned char*
1501 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1503 int sreg = tree->sreg1;
1504 int need_touch = FALSE;
1506 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
1515 * If requested stack size is larger than one page,
1516 * perform stack-touch operation
1519 * Generate stack probe code.
1520 * Under Windows, it is necessary to allocate one page at a time,
1521 * "touching" stack after each successful sub-allocation. This is
1522 * because of the way stack growth is implemented - there is a
1523 * guard page before the lowest stack page that is currently commited.
1524 * Stack normally grows sequentially so OS traps access to the
1525 * guard page and commits more pages when needed.
1527 x86_test_reg_imm (code, sreg, ~0xFFF);
1528 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1530 br[2] = code; /* loop */
1531 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
1532 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
1535 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
1536 * that follows only initializes the last part of the area.
1538 /* Same as the init code below with size==0x1000 */
1539 if (tree->flags & MONO_INST_INIT) {
1540 x86_push_reg (code, X86_EAX);
1541 x86_push_reg (code, X86_ECX);
1542 x86_push_reg (code, X86_EDI);
1543 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
1544 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1545 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
1547 x86_prefix (code, X86_REP_PREFIX);
1549 x86_pop_reg (code, X86_EDI);
1550 x86_pop_reg (code, X86_ECX);
1551 x86_pop_reg (code, X86_EAX);
1554 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
1555 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
1556 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
1557 x86_patch (br[3], br[2]);
1558 x86_test_reg_reg (code, sreg, sreg);
1559 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1560 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1562 br[1] = code; x86_jump8 (code, 0);
1564 x86_patch (br[0], code);
1565 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1566 x86_patch (br[1], code);
1567 x86_patch (br[4], code);
1570 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1572 if (tree->flags & MONO_INST_INIT) {
1574 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1575 x86_push_reg (code, X86_EAX);
1578 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1579 x86_push_reg (code, X86_ECX);
1582 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1583 x86_push_reg (code, X86_EDI);
1587 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1588 if (sreg != X86_ECX)
1589 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1590 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1592 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1594 x86_prefix (code, X86_REP_PREFIX);
1597 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1598 x86_pop_reg (code, X86_EDI);
1599 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1600 x86_pop_reg (code, X86_ECX);
1601 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1602 x86_pop_reg (code, X86_EAX);
1609 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
1614 /* Move return value to the target register */
1615 switch (ins->opcode) {
1618 case OP_CALL_MEMBASE:
1619 if (ins->dreg != X86_EAX)
1620 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
1624 case OP_VCALL_MEMBASE:
1625 cinfo = get_call_info (((MonoCallInst*)ins)->signature, FALSE);
1626 if (cinfo->ret.storage == ArgValuetypeInReg) {
1627 /* Pop the destination address from the stack */
1628 x86_pop_reg (code, X86_ECX);
1630 for (quad = 0; quad < 2; quad ++) {
1631 switch (cinfo->ret.pair_storage [quad]) {
1633 g_assert (cinfo->ret.pair_regs [quad] != X86_ECX);
1634 x86_mov_membase_reg (code, X86_ECX, (quad * sizeof (gpointer)), cinfo->ret.pair_regs [quad], sizeof (gpointer));
1639 g_assert_not_reached ();
1652 emit_tls_get (guint8* code, int dreg, int tls_offset)
1654 #ifdef PLATFORM_WIN32
1656 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
1657 * Journal and/or a disassembly of the TlsGet () function.
1659 g_assert (tls_offset < 64);
1660 x86_prefix (code, X86_FS_PREFIX);
1661 x86_mov_reg_mem (code, dreg, 0x18, 4);
1662 /* Dunno what this does but TlsGetValue () contains it */
1663 x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
1664 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
1666 x86_prefix (code, X86_GS_PREFIX);
1667 x86_mov_reg_mem (code, dreg, tls_offset, 4);
1672 #define REAL_PRINT_REG(text,reg) \
1673 mono_assert (reg >= 0); \
1674 x86_push_reg (code, X86_EAX); \
1675 x86_push_reg (code, X86_EDX); \
1676 x86_push_reg (code, X86_ECX); \
1677 x86_push_reg (code, reg); \
1678 x86_push_imm (code, reg); \
1679 x86_push_imm (code, text " %d %p\n"); \
1680 x86_mov_reg_imm (code, X86_EAX, printf); \
1681 x86_call_reg (code, X86_EAX); \
1682 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
1683 x86_pop_reg (code, X86_ECX); \
1684 x86_pop_reg (code, X86_EDX); \
1685 x86_pop_reg (code, X86_EAX);
1687 /* benchmark and set based on cpu */
1688 #define LOOP_ALIGNMENT 8
1689 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
1692 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1697 guint8 *code = cfg->native_code + cfg->code_len;
1698 MonoInst *last_ins = NULL;
1699 guint last_offset = 0;
1702 if (cfg->opt & MONO_OPT_PEEPHOLE)
1703 peephole_pass (cfg, bb);
1705 if (cfg->opt & MONO_OPT_LOOP) {
1706 int pad, align = LOOP_ALIGNMENT;
1707 /* set alignment depending on cpu */
1708 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
1710 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
1711 x86_padding (code, pad);
1712 cfg->code_len += pad;
1713 bb->native_offset = cfg->code_len;
1717 if (cfg->verbose_level > 2)
1718 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1720 cpos = bb->max_offset;
1722 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1723 MonoProfileCoverageInfo *cov = cfg->coverage_info;
1724 g_assert (!cfg->compile_aot);
1727 cov->data [bb->dfn].cil_code = bb->cil_code;
1728 /* this is not thread save, but good enough */
1729 x86_inc_mem (code, &cov->data [bb->dfn].count);
1732 offset = code - cfg->native_code;
1736 offset = code - cfg->native_code;
1738 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1740 if (offset > (cfg->code_size - max_len - 16)) {
1741 cfg->code_size *= 2;
1742 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1743 code = cfg->native_code + offset;
1744 mono_jit_stats.code_reallocs++;
1747 mono_debug_record_line_number (cfg, ins, offset);
1749 switch (ins->opcode) {
1751 x86_mul_reg (code, ins->sreg2, TRUE);
1754 x86_mul_reg (code, ins->sreg2, FALSE);
1756 case OP_X86_SETEQ_MEMBASE:
1757 case OP_X86_SETNE_MEMBASE:
1758 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
1759 ins->inst_basereg, ins->inst_offset, TRUE);
1761 case OP_STOREI1_MEMBASE_IMM:
1762 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
1764 case OP_STOREI2_MEMBASE_IMM:
1765 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
1767 case OP_STORE_MEMBASE_IMM:
1768 case OP_STOREI4_MEMBASE_IMM:
1769 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
1771 case OP_STOREI1_MEMBASE_REG:
1772 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
1774 case OP_STOREI2_MEMBASE_REG:
1775 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
1777 case OP_STORE_MEMBASE_REG:
1778 case OP_STOREI4_MEMBASE_REG:
1779 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
1784 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
1787 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
1788 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
1790 case OP_LOAD_MEMBASE:
1791 case OP_LOADI4_MEMBASE:
1792 case OP_LOADU4_MEMBASE:
1793 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
1795 case OP_LOADU1_MEMBASE:
1796 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
1798 case OP_LOADI1_MEMBASE:
1799 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
1801 case OP_LOADU2_MEMBASE:
1802 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
1804 case OP_LOADI2_MEMBASE:
1805 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
1808 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
1811 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
1814 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
1817 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
1820 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
1822 case OP_COMPARE_IMM:
1823 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
1825 case OP_X86_COMPARE_MEMBASE_REG:
1826 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
1828 case OP_X86_COMPARE_MEMBASE_IMM:
1829 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1831 case OP_X86_COMPARE_MEMBASE8_IMM:
1832 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1834 case OP_X86_COMPARE_REG_MEMBASE:
1835 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
1837 case OP_X86_COMPARE_MEM_IMM:
1838 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
1840 case OP_X86_TEST_NULL:
1841 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
1843 case OP_X86_ADD_MEMBASE_IMM:
1844 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1846 case OP_X86_ADD_MEMBASE:
1847 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
1849 case OP_X86_SUB_MEMBASE_IMM:
1850 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1852 case OP_X86_SUB_MEMBASE:
1853 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
1855 case OP_X86_INC_MEMBASE:
1856 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
1858 case OP_X86_INC_REG:
1859 x86_inc_reg (code, ins->dreg);
1861 case OP_X86_DEC_MEMBASE:
1862 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
1864 case OP_X86_DEC_REG:
1865 x86_dec_reg (code, ins->dreg);
1867 case OP_X86_MUL_MEMBASE:
1868 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
1871 x86_breakpoint (code);
1875 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
1878 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
1882 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
1885 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
1889 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
1892 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
1896 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
1899 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
1902 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
1905 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
1909 x86_div_reg (code, ins->sreg2, TRUE);
1912 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
1913 x86_div_reg (code, ins->sreg2, FALSE);
1916 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
1918 x86_div_reg (code, ins->sreg2, TRUE);
1922 x86_div_reg (code, ins->sreg2, TRUE);
1925 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
1926 x86_div_reg (code, ins->sreg2, FALSE);
1929 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
1931 x86_div_reg (code, ins->sreg2, TRUE);
1934 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
1937 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
1940 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
1943 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
1946 g_assert (ins->sreg2 == X86_ECX);
1947 x86_shift_reg (code, X86_SHL, ins->dreg);
1950 g_assert (ins->sreg2 == X86_ECX);
1951 x86_shift_reg (code, X86_SAR, ins->dreg);
1954 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
1957 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
1960 g_assert (ins->sreg2 == X86_ECX);
1961 x86_shift_reg (code, X86_SHR, ins->dreg);
1964 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
1967 guint8 *jump_to_end;
1969 /* handle shifts below 32 bits */
1970 x86_shld_reg (code, ins->unused, ins->sreg1);
1971 x86_shift_reg (code, X86_SHL, ins->sreg1);
1973 x86_test_reg_imm (code, X86_ECX, 32);
1974 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
1976 /* handle shift over 32 bit */
1977 x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
1978 x86_clear_reg (code, ins->sreg1);
1980 x86_patch (jump_to_end, code);
1984 guint8 *jump_to_end;
1986 /* handle shifts below 32 bits */
1987 x86_shrd_reg (code, ins->sreg1, ins->unused);
1988 x86_shift_reg (code, X86_SAR, ins->unused);
1990 x86_test_reg_imm (code, X86_ECX, 32);
1991 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
1993 /* handle shifts over 31 bits */
1994 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
1995 x86_shift_reg_imm (code, X86_SAR, ins->unused, 31);
1997 x86_patch (jump_to_end, code);
2001 guint8 *jump_to_end;
2003 /* handle shifts below 32 bits */
2004 x86_shrd_reg (code, ins->sreg1, ins->unused);
2005 x86_shift_reg (code, X86_SHR, ins->unused);
2007 x86_test_reg_imm (code, X86_ECX, 32);
2008 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2010 /* handle shifts over 31 bits */
2011 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
2012 x86_shift_reg_imm (code, X86_SHR, ins->unused, 31);
2014 x86_patch (jump_to_end, code);
2018 if (ins->inst_imm >= 32) {
2019 x86_mov_reg_reg (code, ins->unused, ins->sreg1, 4);
2020 x86_clear_reg (code, ins->sreg1);
2021 x86_shift_reg_imm (code, X86_SHL, ins->unused, ins->inst_imm - 32);
2023 x86_shld_reg_imm (code, ins->unused, ins->sreg1, ins->inst_imm);
2024 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
2028 if (ins->inst_imm >= 32) {
2029 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
2030 x86_shift_reg_imm (code, X86_SAR, ins->unused, 0x1f);
2031 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
2033 x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
2034 x86_shift_reg_imm (code, X86_SAR, ins->unused, ins->inst_imm);
2037 case OP_LSHR_UN_IMM:
2038 if (ins->inst_imm >= 32) {
2039 x86_mov_reg_reg (code, ins->sreg1, ins->unused, 4);
2040 x86_clear_reg (code, ins->unused);
2041 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
2043 x86_shrd_reg_imm (code, ins->sreg1, ins->unused, ins->inst_imm);
2044 x86_shift_reg_imm (code, X86_SHR, ins->unused, ins->inst_imm);
2048 x86_not_reg (code, ins->sreg1);
2051 x86_neg_reg (code, ins->sreg1);
2054 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2057 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2060 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2063 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2066 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2067 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2069 case CEE_MUL_OVF_UN: {
2070 /* the mul operation and the exception check should most likely be split */
2071 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2072 /*g_assert (ins->sreg2 == X86_EAX);
2073 g_assert (ins->dreg == X86_EAX);*/
2074 if (ins->sreg2 == X86_EAX) {
2075 non_eax_reg = ins->sreg1;
2076 } else if (ins->sreg1 == X86_EAX) {
2077 non_eax_reg = ins->sreg2;
2079 /* no need to save since we're going to store to it anyway */
2080 if (ins->dreg != X86_EAX) {
2082 x86_push_reg (code, X86_EAX);
2084 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2085 non_eax_reg = ins->sreg2;
2087 if (ins->dreg == X86_EDX) {
2090 x86_push_reg (code, X86_EAX);
2092 } else if (ins->dreg != X86_EAX) {
2094 x86_push_reg (code, X86_EDX);
2096 x86_mul_reg (code, non_eax_reg, FALSE);
2097 /* save before the check since pop and mov don't change the flags */
2098 if (ins->dreg != X86_EAX)
2099 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2101 x86_pop_reg (code, X86_EDX);
2103 x86_pop_reg (code, X86_EAX);
2104 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2108 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2111 g_assert_not_reached ();
2112 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2113 x86_mov_reg_imm (code, ins->dreg, 0);
2115 case OP_LOAD_GOTADDR:
2116 x86_call_imm (code, 0);
2118 * The patch needs to point to the pop, since the GOT offset needs
2119 * to be added to that address.
2121 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
2122 x86_pop_reg (code, ins->dreg);
2123 x86_alu_reg_imm (code, X86_ADD, ins->dreg, 0xf0f0f0f0);
2126 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2127 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
2129 case OP_X86_PUSH_GOT_ENTRY:
2130 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2131 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
2135 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2138 g_assert_not_reached ();
2141 * Note: this 'frame destruction' logic is useful for tail calls, too.
2142 * Keep in sync with the code in emit_epilog.
2146 /* FIXME: no tracing support... */
2147 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2148 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2149 /* reset offset to make max_len work */
2150 offset = code - cfg->native_code;
2152 g_assert (!cfg->method->save_lmf);
2154 if (cfg->used_int_regs & (1 << X86_EBX))
2156 if (cfg->used_int_regs & (1 << X86_EDI))
2158 if (cfg->used_int_regs & (1 << X86_ESI))
2161 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2163 if (cfg->used_int_regs & (1 << X86_ESI))
2164 x86_pop_reg (code, X86_ESI);
2165 if (cfg->used_int_regs & (1 << X86_EDI))
2166 x86_pop_reg (code, X86_EDI);
2167 if (cfg->used_int_regs & (1 << X86_EBX))
2168 x86_pop_reg (code, X86_EBX);
2170 /* restore ESP/EBP */
2172 offset = code - cfg->native_code;
2173 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2174 x86_jump32 (code, 0);
2178 /* ensure ins->sreg1 is not NULL
2179 * note that cmp DWORD PTR [eax], eax is one byte shorter than
2180 * cmp DWORD PTR [eax], 0
2182 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
2185 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2186 x86_push_reg (code, hreg);
2187 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2188 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2189 x86_pop_reg (code, hreg);
2197 call = (MonoCallInst*)ins;
2198 if (ins->flags & MONO_INST_HAS_METHOD)
2199 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2201 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2202 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2203 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
2204 * bytes to pop, we want to use pops. GCC does this (note it won't happen
2205 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
2206 * smart enough to do that optimization yet
2208 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
2209 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
2210 * (most likely from locality benefits). People with other processors should
2211 * check on theirs to see what happens.
2213 if (call->stack_usage == 4) {
2214 /* we want to use registers that won't get used soon, so use
2215 * ecx, as eax will get allocated first. edx is used by long calls,
2216 * so we can't use that.
2219 x86_pop_reg (code, X86_ECX);
2221 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2224 code = emit_move_return_value (cfg, ins, code);
2229 case OP_VOIDCALL_REG:
2231 call = (MonoCallInst*)ins;
2232 x86_call_reg (code, ins->sreg1);
2233 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2234 if (call->stack_usage == 4)
2235 x86_pop_reg (code, X86_ECX);
2237 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2239 code = emit_move_return_value (cfg, ins, code);
2241 case OP_FCALL_MEMBASE:
2242 case OP_LCALL_MEMBASE:
2243 case OP_VCALL_MEMBASE:
2244 case OP_VOIDCALL_MEMBASE:
2245 case OP_CALL_MEMBASE:
2246 call = (MonoCallInst*)ins;
2247 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2248 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2249 if (call->stack_usage == 4)
2250 x86_pop_reg (code, X86_ECX);
2252 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2254 code = emit_move_return_value (cfg, ins, code);
2258 x86_push_reg (code, ins->sreg1);
2260 case OP_X86_PUSH_IMM:
2261 x86_push_imm (code, ins->inst_imm);
2263 case OP_X86_PUSH_MEMBASE:
2264 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2266 case OP_X86_PUSH_OBJ:
2267 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2268 x86_push_reg (code, X86_EDI);
2269 x86_push_reg (code, X86_ESI);
2270 x86_push_reg (code, X86_ECX);
2271 if (ins->inst_offset)
2272 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2274 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2275 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2276 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2278 x86_prefix (code, X86_REP_PREFIX);
2280 x86_pop_reg (code, X86_ECX);
2281 x86_pop_reg (code, X86_ESI);
2282 x86_pop_reg (code, X86_EDI);
2285 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->unused);
2287 case OP_X86_LEA_MEMBASE:
2288 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
2291 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2294 /* keep alignment */
2295 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
2296 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
2297 code = mono_emit_stack_alloc (code, ins);
2298 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2304 x86_push_reg (code, ins->sreg1);
2305 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2306 (gpointer)"mono_arch_throw_exception");
2310 x86_push_reg (code, ins->sreg1);
2311 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2312 (gpointer)"mono_arch_rethrow_exception");
2315 case OP_CALL_HANDLER:
2316 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2317 x86_call_imm (code, 0);
2320 ins->inst_c0 = code - cfg->native_code;
2323 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2324 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2326 if (ins->flags & MONO_INST_BRLABEL) {
2327 if (ins->inst_i0->inst_c0) {
2328 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2330 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2331 if ((cfg->opt & MONO_OPT_BRANCH) &&
2332 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
2333 x86_jump8 (code, 0);
2335 x86_jump32 (code, 0);
2338 if (ins->inst_target_bb->native_offset) {
2339 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2341 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2342 if ((cfg->opt & MONO_OPT_BRANCH) &&
2343 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
2344 x86_jump8 (code, 0);
2346 x86_jump32 (code, 0);
2351 x86_jump_reg (code, ins->sreg1);
2354 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2355 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2358 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
2359 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2362 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2363 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2366 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
2367 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2370 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2371 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2374 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
2375 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2377 case OP_COND_EXC_EQ:
2378 case OP_COND_EXC_NE_UN:
2379 case OP_COND_EXC_LT:
2380 case OP_COND_EXC_LT_UN:
2381 case OP_COND_EXC_GT:
2382 case OP_COND_EXC_GT_UN:
2383 case OP_COND_EXC_GE:
2384 case OP_COND_EXC_GE_UN:
2385 case OP_COND_EXC_LE:
2386 case OP_COND_EXC_LE_UN:
2387 case OP_COND_EXC_OV:
2388 case OP_COND_EXC_NO:
2390 case OP_COND_EXC_NC:
2391 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2392 (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2404 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
2407 /* floating point opcodes */
2409 double d = *(double *)ins->inst_p0;
2411 if ((d == 0.0) && (mono_signbit (d) == 0)) {
2413 } else if (d == 1.0) {
2416 if (cfg->compile_aot) {
2417 guint32 *val = (guint32*)&d;
2418 x86_push_imm (code, val [1]);
2419 x86_push_imm (code, val [0]);
2420 x86_fld_membase (code, X86_ESP, 0, TRUE);
2421 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2424 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
2425 x86_fld (code, NULL, TRUE);
2431 float f = *(float *)ins->inst_p0;
2433 if ((f == 0.0) && (mono_signbit (f) == 0)) {
2435 } else if (f == 1.0) {
2438 if (cfg->compile_aot) {
2439 guint32 val = *(guint32*)&f;
2440 x86_push_imm (code, val);
2441 x86_fld_membase (code, X86_ESP, 0, FALSE);
2442 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2445 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
2446 x86_fld (code, NULL, FALSE);
2451 case OP_STORER8_MEMBASE_REG:
2452 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
2454 case OP_LOADR8_SPILL_MEMBASE:
2455 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2458 case OP_LOADR8_MEMBASE:
2459 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2461 case OP_STORER4_MEMBASE_REG:
2462 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
2464 case OP_LOADR4_MEMBASE:
2465 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2467 case CEE_CONV_R4: /* FIXME: change precision */
2469 x86_push_reg (code, ins->sreg1);
2470 x86_fild_membase (code, X86_ESP, 0, FALSE);
2471 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2473 case OP_X86_FP_LOAD_I8:
2474 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2476 case OP_X86_FP_LOAD_I4:
2477 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2479 case OP_FCONV_TO_I1:
2480 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2482 case OP_FCONV_TO_U1:
2483 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2485 case OP_FCONV_TO_I2:
2486 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2488 case OP_FCONV_TO_U2:
2489 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2491 case OP_FCONV_TO_I4:
2493 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2495 case OP_FCONV_TO_I8:
2496 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2497 x86_fnstcw_membase(code, X86_ESP, 0);
2498 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
2499 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
2500 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
2501 x86_fldcw_membase (code, X86_ESP, 2);
2502 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2503 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2504 x86_pop_reg (code, ins->dreg);
2505 x86_pop_reg (code, ins->unused);
2506 x86_fldcw_membase (code, X86_ESP, 0);
2507 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2509 case OP_LCONV_TO_R_UN: {
2510 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2513 /* load 64bit integer to FP stack */
2514 x86_push_imm (code, 0);
2515 x86_push_reg (code, ins->sreg2);
2516 x86_push_reg (code, ins->sreg1);
2517 x86_fild_membase (code, X86_ESP, 0, TRUE);
2518 /* store as 80bit FP value */
2519 x86_fst80_membase (code, X86_ESP, 0);
2521 /* test if lreg is negative */
2522 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2523 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2525 /* add correction constant mn */
2526 x86_fld80_mem (code, mn);
2527 x86_fld80_membase (code, X86_ESP, 0);
2528 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2529 x86_fst80_membase (code, X86_ESP, 0);
2531 x86_patch (br, code);
2533 x86_fld80_membase (code, X86_ESP, 0);
2534 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2538 case OP_LCONV_TO_OVF_I: {
2539 guint8 *br [3], *label [1];
2542 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2544 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2546 /* If the low word top bit is set, see if we are negative */
2547 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2548 /* We are not negative (no top bit set, check for our top word to be zero */
2549 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2550 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2553 /* throw exception */
2554 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2555 x86_jump32 (code, 0);
2557 x86_patch (br [0], code);
2558 /* our top bit is set, check that top word is 0xfffffff */
2559 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2561 x86_patch (br [1], code);
2562 /* nope, emit exception */
2563 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2564 x86_patch (br [2], label [0]);
2566 if (ins->dreg != ins->sreg1)
2567 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2571 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2574 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
2577 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
2580 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
2588 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2593 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2600 * it really doesn't make sense to inline all this code,
2601 * it's here just to show that things may not be as simple
2604 guchar *check_pos, *end_tan, *pop_jump;
2605 x86_push_reg (code, X86_EAX);
2608 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
2610 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2611 x86_fstp (code, 0); /* pop the 1.0 */
2613 x86_jump8 (code, 0);
2615 x86_fp_op (code, X86_FADD, 0);
2619 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
2621 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2624 x86_patch (pop_jump, code);
2625 x86_fstp (code, 0); /* pop the 1.0 */
2626 x86_patch (check_pos, code);
2627 x86_patch (end_tan, code);
2629 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2630 x86_pop_reg (code, X86_EAX);
2637 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2648 x86_push_reg (code, X86_EAX);
2649 /* we need to exchange ST(0) with ST(1) */
2652 /* this requires a loop, because fprem somtimes
2653 * returns a partial remainder */
2655 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
2656 /* x86_fprem1 (code); */
2659 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
2661 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
2666 x86_pop_reg (code, X86_EAX);
2670 if (cfg->opt & MONO_OPT_FCMOV) {
2671 x86_fcomip (code, 1);
2675 /* this overwrites EAX */
2676 EMIT_FPCOMPARE(code);
2677 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2680 if (cfg->opt & MONO_OPT_FCMOV) {
2681 /* zeroing the register at the start results in
2682 * shorter and faster code (we can also remove the widening op)
2684 guchar *unordered_check;
2685 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2686 x86_fcomip (code, 1);
2688 unordered_check = code;
2689 x86_branch8 (code, X86_CC_P, 0, FALSE);
2690 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
2691 x86_patch (unordered_check, code);
2694 if (ins->dreg != X86_EAX)
2695 x86_push_reg (code, X86_EAX);
2697 EMIT_FPCOMPARE(code);
2698 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2699 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2700 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2701 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2703 if (ins->dreg != X86_EAX)
2704 x86_pop_reg (code, X86_EAX);
2708 if (cfg->opt & MONO_OPT_FCMOV) {
2709 /* zeroing the register at the start results in
2710 * shorter and faster code (we can also remove the widening op)
2712 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2713 x86_fcomip (code, 1);
2715 if (ins->opcode == OP_FCLT_UN) {
2716 guchar *unordered_check = code;
2717 guchar *jump_to_end;
2718 x86_branch8 (code, X86_CC_P, 0, FALSE);
2719 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2721 x86_jump8 (code, 0);
2722 x86_patch (unordered_check, code);
2723 x86_inc_reg (code, ins->dreg);
2724 x86_patch (jump_to_end, code);
2726 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2730 if (ins->dreg != X86_EAX)
2731 x86_push_reg (code, X86_EAX);
2733 EMIT_FPCOMPARE(code);
2734 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2735 if (ins->opcode == OP_FCLT_UN) {
2736 guchar *is_not_zero_check, *end_jump;
2737 is_not_zero_check = code;
2738 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2740 x86_jump8 (code, 0);
2741 x86_patch (is_not_zero_check, code);
2742 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
2744 x86_patch (end_jump, code);
2746 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2747 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2749 if (ins->dreg != X86_EAX)
2750 x86_pop_reg (code, X86_EAX);
2754 if (cfg->opt & MONO_OPT_FCMOV) {
2755 /* zeroing the register at the start results in
2756 * shorter and faster code (we can also remove the widening op)
2758 guchar *unordered_check;
2759 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2760 x86_fcomip (code, 1);
2762 if (ins->opcode == OP_FCGT) {
2763 unordered_check = code;
2764 x86_branch8 (code, X86_CC_P, 0, FALSE);
2765 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2766 x86_patch (unordered_check, code);
2768 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2772 if (ins->dreg != X86_EAX)
2773 x86_push_reg (code, X86_EAX);
2775 EMIT_FPCOMPARE(code);
2776 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2777 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
2778 if (ins->opcode == OP_FCGT_UN) {
2779 guchar *is_not_zero_check, *end_jump;
2780 is_not_zero_check = code;
2781 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2783 x86_jump8 (code, 0);
2784 x86_patch (is_not_zero_check, code);
2785 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
2787 x86_patch (end_jump, code);
2789 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2790 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2792 if (ins->dreg != X86_EAX)
2793 x86_pop_reg (code, X86_EAX);
2796 if (cfg->opt & MONO_OPT_FCMOV) {
2797 guchar *jump = code;
2798 x86_branch8 (code, X86_CC_P, 0, TRUE);
2799 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2800 x86_patch (jump, code);
2803 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2804 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
2807 /* Branch if C013 != 100 */
2808 if (cfg->opt & MONO_OPT_FCMOV) {
2809 /* branch if !ZF or (PF|CF) */
2810 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2811 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2812 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
2815 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
2816 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2819 if (cfg->opt & MONO_OPT_FCMOV) {
2820 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2823 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2826 if (cfg->opt & MONO_OPT_FCMOV) {
2827 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2828 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2831 if (ins->opcode == OP_FBLT_UN) {
2832 guchar *is_not_zero_check, *end_jump;
2833 is_not_zero_check = code;
2834 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2836 x86_jump8 (code, 0);
2837 x86_patch (is_not_zero_check, code);
2838 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
2840 x86_patch (end_jump, code);
2842 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2846 if (cfg->opt & MONO_OPT_FCMOV) {
2847 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
2850 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
2851 if (ins->opcode == OP_FBGT_UN) {
2852 guchar *is_not_zero_check, *end_jump;
2853 is_not_zero_check = code;
2854 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2856 x86_jump8 (code, 0);
2857 x86_patch (is_not_zero_check, code);
2858 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
2860 x86_patch (end_jump, code);
2862 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2865 /* Branch if C013 == 100 or 001 */
2866 if (cfg->opt & MONO_OPT_FCMOV) {
2869 /* skip branch if C1=1 */
2871 x86_branch8 (code, X86_CC_P, 0, FALSE);
2872 /* branch if (C0 | C3) = 1 */
2873 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
2874 x86_patch (br1, code);
2877 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
2878 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2879 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
2880 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2883 /* Branch if C013 == 000 */
2884 if (cfg->opt & MONO_OPT_FCMOV) {
2885 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
2888 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2891 /* Branch if C013=000 or 100 */
2892 if (cfg->opt & MONO_OPT_FCMOV) {
2895 /* skip branch if C1=1 */
2897 x86_branch8 (code, X86_CC_P, 0, FALSE);
2898 /* branch if C0=0 */
2899 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
2900 x86_patch (br1, code);
2903 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
2904 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
2905 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2908 /* Branch if C013 != 001 */
2909 if (cfg->opt & MONO_OPT_FCMOV) {
2910 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2911 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
2914 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
2915 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2917 case CEE_CKFINITE: {
2918 x86_push_reg (code, X86_EAX);
2921 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2922 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
2923 x86_pop_reg (code, X86_EAX);
2924 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
2928 code = emit_tls_get (code, ins->dreg, ins->inst_offset);
2931 case OP_MEMORY_BARRIER: {
2932 /* Not needed on x86 */
2935 case OP_ATOMIC_ADD_I4: {
2936 int dreg = ins->dreg;
2938 if (dreg == ins->inst_basereg) {
2939 x86_push_reg (code, ins->sreg2);
2943 if (dreg != ins->sreg2)
2944 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
2946 x86_prefix (code, X86_LOCK_PREFIX);
2947 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
2949 if (dreg != ins->dreg) {
2950 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
2951 x86_pop_reg (code, dreg);
2956 case OP_ATOMIC_ADD_NEW_I4: {
2957 int dreg = ins->dreg;
2959 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
2960 if (ins->sreg2 == dreg) {
2961 if (dreg == X86_EBX) {
2963 if (ins->inst_basereg == X86_EDI)
2967 if (ins->inst_basereg == X86_EBX)
2970 } else if (ins->inst_basereg == dreg) {
2971 if (dreg == X86_EBX) {
2973 if (ins->sreg2 == X86_EDI)
2977 if (ins->sreg2 == X86_EBX)
2982 if (dreg != ins->dreg) {
2983 x86_push_reg (code, dreg);
2986 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
2987 x86_prefix (code, X86_LOCK_PREFIX);
2988 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
2989 /* dreg contains the old value, add with sreg2 value */
2990 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
2992 if (ins->dreg != dreg) {
2993 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
2994 x86_pop_reg (code, dreg);
2999 case OP_ATOMIC_EXCHANGE_I4: {
3001 int sreg2 = ins->sreg2;
3002 int breg = ins->inst_basereg;
3004 /* cmpxchg uses eax as comperand, need to make sure we can use it
3005 * hack to overcome limits in x86 reg allocator
3006 * (req: dreg == eax and sreg2 != eax and breg != eax)
3008 if (ins->dreg != X86_EAX)
3009 x86_push_reg (code, X86_EAX);
3011 /* We need the EAX reg for the cmpxchg */
3012 if (ins->sreg2 == X86_EAX) {
3013 x86_push_reg (code, X86_EDX);
3014 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
3018 if (breg == X86_EAX) {
3019 x86_push_reg (code, X86_ESI);
3020 x86_mov_reg_reg (code, X86_ESI, X86_EAX, 4);
3024 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
3026 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
3027 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
3028 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
3029 x86_patch (br [1], br [0]);
3031 if (breg != ins->inst_basereg)
3032 x86_pop_reg (code, X86_ESI);
3034 if (ins->dreg != X86_EAX) {
3035 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3036 x86_pop_reg (code, X86_EAX);
3039 if (ins->sreg2 != sreg2)
3040 x86_pop_reg (code, X86_EDX);
3045 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3046 g_assert_not_reached ();
3049 if ((code - cfg->native_code - offset) > max_len) {
3050 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3051 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3052 g_assert_not_reached ();
3058 last_offset = offset;
3063 cfg->code_len = code - cfg->native_code;
3067 mono_arch_register_lowlevel_calls (void)
3072 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3074 MonoJumpInfo *patch_info;
3075 gboolean compile_aot = !run_cctors;
3077 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3078 unsigned char *ip = patch_info->ip.i + code;
3079 const unsigned char *target;
3081 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3084 switch (patch_info->type) {
3085 case MONO_PATCH_INFO_BB:
3086 case MONO_PATCH_INFO_LABEL:
3089 /* No need to patch these */
3094 switch (patch_info->type) {
3095 case MONO_PATCH_INFO_IP:
3096 *((gconstpointer *)(ip)) = target;
3098 case MONO_PATCH_INFO_CLASS_INIT: {
3100 /* Might already been changed to a nop */
3101 x86_call_code (code, 0);
3102 x86_patch (ip, target);
3105 case MONO_PATCH_INFO_ABS:
3106 case MONO_PATCH_INFO_METHOD:
3107 case MONO_PATCH_INFO_METHOD_JUMP:
3108 case MONO_PATCH_INFO_INTERNAL_METHOD:
3109 case MONO_PATCH_INFO_BB:
3110 case MONO_PATCH_INFO_LABEL:
3111 x86_patch (ip, target);
3113 case MONO_PATCH_INFO_NONE:
3116 guint32 offset = mono_arch_get_patch_offset (ip);
3117 *((gconstpointer *)(ip + offset)) = target;
3125 mono_arch_emit_prolog (MonoCompile *cfg)
3127 MonoMethod *method = cfg->method;
3129 MonoMethodSignature *sig;
3131 int alloc_size, pos, max_offset, i;
3134 cfg->code_size = MAX (mono_method_get_header (method)->code_size * 4, 256);
3135 code = cfg->native_code = g_malloc (cfg->code_size);
3137 x86_push_reg (code, X86_EBP);
3138 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
3140 alloc_size = cfg->stack_offset;
3143 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
3144 /* Might need to attach the thread to the JIT */
3145 if (lmf_tls_offset != -1) {
3148 code = emit_tls_get ( code, X86_EAX, lmf_tls_offset);
3149 x86_test_reg_reg (code, X86_EAX, X86_EAX);
3151 x86_branch8 (code, X86_CC_NE, 0, 0);
3152 x86_push_imm (code, cfg->domain);
3153 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
3154 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3155 x86_patch (buf, code);
3156 #ifdef PLATFORM_WIN32
3157 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3158 /* FIXME: Add a separate key for LMF to avoid this */
3159 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
3163 g_assert (!cfg->compile_aot);
3164 x86_push_imm (code, cfg->domain);
3165 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
3166 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3170 if (method->save_lmf) {
3171 pos += sizeof (MonoLMF);
3173 /* save the current IP */
3174 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3175 x86_push_imm_template (code);
3177 /* save all caller saved regs */
3178 x86_push_reg (code, X86_EBP);
3179 x86_push_reg (code, X86_ESI);
3180 x86_push_reg (code, X86_EDI);
3181 x86_push_reg (code, X86_EBX);
3183 /* save method info */
3184 x86_push_imm (code, method);
3186 /* get the address of lmf for the current thread */
3188 * This is performance critical so we try to use some tricks to make
3191 if (lmf_tls_offset != -1) {
3192 /* Load lmf quicky using the GS register */
3193 code = emit_tls_get (code, X86_EAX, lmf_tls_offset);
3194 #ifdef PLATFORM_WIN32
3195 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3196 /* FIXME: Add a separate key for LMF to avoid this */
3197 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
3201 if (cfg->compile_aot) {
3202 /* The GOT var does not exist yet */
3203 x86_call_imm (code, 0);
3204 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
3205 x86_pop_reg (code, X86_EAX);
3206 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 0);
3207 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
3208 x86_call_membase (code, X86_EAX, 0xf0f0f0f0);
3211 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
3215 x86_push_reg (code, X86_EAX);
3216 /* push *lfm (previous_lmf) */
3217 x86_push_membase (code, X86_EAX, 0);
3219 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3222 if (cfg->used_int_regs & (1 << X86_EBX)) {
3223 x86_push_reg (code, X86_EBX);
3227 if (cfg->used_int_regs & (1 << X86_EDI)) {
3228 x86_push_reg (code, X86_EDI);
3232 if (cfg->used_int_regs & (1 << X86_ESI)) {
3233 x86_push_reg (code, X86_ESI);
3241 /* See mono_emit_stack_alloc */
3242 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3243 guint32 remaining_size = alloc_size;
3244 while (remaining_size >= 0x1000) {
3245 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
3246 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
3247 remaining_size -= 0x1000;
3250 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
3252 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
3256 /* compute max_offset in order to use short forward jumps */
3258 if (cfg->opt & MONO_OPT_BRANCH) {
3259 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3260 MonoInst *ins = bb->code;
3261 bb->max_offset = max_offset;
3263 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3265 /* max alignment for loops */
3266 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
3267 max_offset += LOOP_ALIGNMENT;
3270 if (ins->opcode == OP_LABEL)
3271 ins->inst_c1 = max_offset;
3273 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3279 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3280 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3282 /* load arguments allocated to register from the stack */
3283 sig = mono_method_signature (method);
3286 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3287 inst = cfg->varinfo [pos];
3288 if (inst->opcode == OP_REGVAR) {
3289 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
3290 if (cfg->verbose_level > 2)
3291 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3296 cfg->code_len = code - cfg->native_code;
3302 mono_arch_emit_epilog (MonoCompile *cfg)
3304 MonoMethod *method = cfg->method;
3305 MonoMethodSignature *sig = mono_method_signature (method);
3307 guint32 stack_to_pop;
3309 int max_epilog_size = 16;
3312 if (cfg->method->save_lmf)
3313 max_epilog_size += 128;
3315 if (mono_jit_trace_calls != NULL)
3316 max_epilog_size += 50;
3318 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3319 cfg->code_size *= 2;
3320 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3321 mono_jit_stats.code_reallocs++;
3324 code = cfg->native_code + cfg->code_len;
3326 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3327 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3329 /* the code restoring the registers must be kept in sync with CEE_JMP */
3332 if (method->save_lmf) {
3333 gint32 prev_lmf_reg;
3335 /* Find a spare register */
3336 switch (sig->ret->type) {
3339 prev_lmf_reg = X86_EDI;
3340 cfg->used_int_regs |= (1 << X86_EDI);
3343 prev_lmf_reg = X86_EDX;
3347 /* reg = previous_lmf */
3348 x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, -32, 4);
3351 x86_mov_reg_membase (code, X86_ECX, X86_EBP, -28, 4);
3353 /* *(lmf) = previous_lmf */
3354 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
3356 /* restore caller saved regs */
3357 if (cfg->used_int_regs & (1 << X86_EBX)) {
3358 x86_mov_reg_membase (code, X86_EBX, X86_EBP, -20, 4);
3361 if (cfg->used_int_regs & (1 << X86_EDI)) {
3362 x86_mov_reg_membase (code, X86_EDI, X86_EBP, -16, 4);
3364 if (cfg->used_int_regs & (1 << X86_ESI)) {
3365 x86_mov_reg_membase (code, X86_ESI, X86_EBP, -12, 4);
3368 /* EBP is restored by LEAVE */
3370 if (cfg->used_int_regs & (1 << X86_EBX)) {
3373 if (cfg->used_int_regs & (1 << X86_EDI)) {
3376 if (cfg->used_int_regs & (1 << X86_ESI)) {
3381 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3383 if (cfg->used_int_regs & (1 << X86_ESI)) {
3384 x86_pop_reg (code, X86_ESI);
3386 if (cfg->used_int_regs & (1 << X86_EDI)) {
3387 x86_pop_reg (code, X86_EDI);
3389 if (cfg->used_int_regs & (1 << X86_EBX)) {
3390 x86_pop_reg (code, X86_EBX);
3394 /* Load returned vtypes into registers if needed */
3395 cinfo = get_call_info (sig, FALSE);
3396 if (cinfo->ret.storage == ArgValuetypeInReg) {
3397 for (quad = 0; quad < 2; quad ++) {
3398 switch (cinfo->ret.pair_storage [quad]) {
3400 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
3402 case ArgOnFloatFpStack:
3403 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
3405 case ArgOnDoubleFpStack:
3406 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
3411 g_assert_not_reached ();
3418 if (CALLCONV_IS_STDCALL (sig)) {
3419 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
3421 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
3422 } else if (MONO_TYPE_ISSTRUCT (mono_method_signature (cfg->method)->ret) && (cinfo->ret.storage == ArgOnStack))
3428 x86_ret_imm (code, stack_to_pop);
3434 cfg->code_len = code - cfg->native_code;
3436 g_assert (cfg->code_len < cfg->code_size);
3440 mono_arch_emit_exceptions (MonoCompile *cfg)
3442 MonoJumpInfo *patch_info;
3445 MonoClass *exc_classes [16];
3446 guint8 *exc_throw_start [16], *exc_throw_end [16];
3450 /* Compute needed space */
3451 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3452 if (patch_info->type == MONO_PATCH_INFO_EXC)
3457 * make sure we have enough space for exceptions
3458 * 16 is the size of two push_imm instructions and a call
3460 if (cfg->compile_aot)
3461 code_size = exc_count * 32;
3463 code_size = exc_count * 16;
3465 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
3466 cfg->code_size *= 2;
3467 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3468 mono_jit_stats.code_reallocs++;
3471 code = cfg->native_code + cfg->code_len;
3474 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3475 switch (patch_info->type) {
3476 case MONO_PATCH_INFO_EXC: {
3477 MonoClass *exc_class;
3481 x86_patch (patch_info->ip.i + cfg->native_code, code);
3483 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
3484 g_assert (exc_class);
3485 throw_ip = patch_info->ip.i;
3487 /* Find a throw sequence for the same exception class */
3488 for (i = 0; i < nthrows; ++i)
3489 if (exc_classes [i] == exc_class)
3492 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
3493 x86_jump_code (code, exc_throw_start [i]);
3494 patch_info->type = MONO_PATCH_INFO_NONE;
3497 guint32 got_reg = X86_EAX;
3500 /* Compute size of code following the push <OFFSET> */
3501 if (cfg->compile_aot) {
3505 else if (cfg->got_var->opcode == OP_REGOFFSET)
3511 if ((code - cfg->native_code) - throw_ip < 126 - size) {
3512 /* Use the shorter form */
3514 x86_push_imm (code, 0);
3518 x86_push_imm (code, 0xf0f0f0f0);
3523 exc_classes [nthrows] = exc_class;
3524 exc_throw_start [nthrows] = code;
3527 if (cfg->compile_aot) {
3529 * Since the patches are generated by the back end, there is * no way to generate a got_var at this point.
3531 if (!cfg->got_var) {
3532 x86_call_imm (code, 0);
3533 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
3534 x86_pop_reg (code, X86_EAX);
3535 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 0);
3538 if (cfg->got_var->opcode == OP_REGOFFSET)
3539 x86_mov_reg_membase (code, X86_EAX, cfg->got_var->inst_basereg, cfg->got_var->inst_offset, 4);
3541 got_reg = cfg->got_var->dreg;
3545 x86_push_imm (code, exc_class->type_token);
3546 patch_info->data.name = "mono_arch_throw_corlib_exception";
3547 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3548 patch_info->ip.i = code - cfg->native_code;
3549 if (cfg->compile_aot)
3550 x86_call_membase (code, got_reg, 0xf0f0f0f0);
3552 x86_call_code (code, 0);
3553 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
3558 exc_throw_end [nthrows] = code;
3570 cfg->code_len = code - cfg->native_code;
3572 g_assert (cfg->code_len < cfg->code_size);
3576 mono_arch_flush_icache (guint8 *code, gint size)
3582 mono_arch_flush_register_windows (void)
3586 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3589 setup_stack (MonoJitTlsData *tls)
3591 pthread_t self = pthread_self();
3592 pthread_attr_t attr;
3594 struct sigaltstack sa;
3595 guint8 *staddr = NULL;
3596 guint8 *current = (guint8*)&staddr;
3598 if (mono_running_on_valgrind ())
3601 /* Determine stack boundaries */
3602 pthread_attr_init( &attr );
3603 #ifdef HAVE_PTHREAD_GETATTR_NP
3604 pthread_getattr_np( self, &attr );
3606 #ifdef HAVE_PTHREAD_ATTR_GET_NP
3607 pthread_attr_get_np( self, &attr );
3609 pthread_attr_getstacksize( &attr, &stsize );
3611 #error "Not implemented"
3615 pthread_attr_getstack( &attr, (void**)&staddr, &stsize );
3620 g_assert ((current > staddr) && (current < staddr + stsize));
3622 tls->end_of_stack = staddr + stsize;
3625 * threads created by nptl does not seem to have a guard page, and
3626 * since the main thread is not created by us, we can't even set one.
3627 * Increasing stsize fools the SIGSEGV signal handler into thinking this
3628 * is a stack overflow exception.
3630 tls->stack_size = stsize + getpagesize ();
3632 /* Setup an alternate signal stack */
3633 tls->signal_stack = mmap (0, SIGNAL_STACK_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
3634 tls->signal_stack_size = SIGNAL_STACK_SIZE;
3636 g_assert (tls->signal_stack);
3638 sa.ss_sp = tls->signal_stack;
3639 sa.ss_size = SIGNAL_STACK_SIZE;
3640 sa.ss_flags = SS_ONSTACK;
3641 sigaltstack (&sa, NULL);
3647 * Support for fast access to the thread-local lmf structure using the GS
3648 * segment register on NPTL + kernel 2.6.x.
3651 static gboolean tls_offset_inited = FALSE;
3654 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3656 if (!tls_offset_inited) {
3657 if (!getenv ("MONO_NO_TLS")) {
3658 #ifdef PLATFORM_WIN32
3660 * We need to init this multiple times, since when we are first called, the key might not
3661 * be initialized yet.
3663 appdomain_tls_offset = mono_domain_get_tls_key ();
3664 lmf_tls_offset = mono_get_jit_tls_key ();
3665 thread_tls_offset = mono_thread_get_tls_key ();
3667 /* Only 64 tls entries can be accessed using inline code */
3668 if (appdomain_tls_offset >= 64)
3669 appdomain_tls_offset = -1;
3670 if (lmf_tls_offset >= 64)
3671 lmf_tls_offset = -1;
3672 if (thread_tls_offset >= 64)
3673 thread_tls_offset = -1;
3675 tls_offset_inited = TRUE;
3676 appdomain_tls_offset = mono_domain_get_tls_offset ();
3677 lmf_tls_offset = mono_get_lmf_tls_offset ();
3678 thread_tls_offset = mono_thread_get_tls_offset ();
3683 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3689 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3691 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3692 struct sigaltstack sa;
3694 sa.ss_sp = tls->signal_stack;
3695 sa.ss_size = SIGNAL_STACK_SIZE;
3696 sa.ss_flags = SS_DISABLE;
3697 sigaltstack (&sa, NULL);
3699 if (tls->signal_stack)
3700 munmap (tls->signal_stack, SIGNAL_STACK_SIZE);
3705 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3708 /* add the this argument */
3709 if (this_reg != -1) {
3711 MONO_INST_NEW (cfg, this, OP_OUTARG);
3712 this->type = this_type;
3713 this->sreg1 = this_reg;
3714 mono_bblock_add_inst (cfg->cbb, this);
3718 CallInfo * cinfo = get_call_info (inst->signature, FALSE);
3721 if (cinfo->ret.storage == ArgValuetypeInReg) {
3723 * The valuetype is in EAX:EDX after the call, needs to be copied to
3724 * the stack. Save the address here, so the call instruction can
3727 MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
3728 vtarg->inst_destbasereg = X86_ESP;
3729 vtarg->inst_offset = inst->stack_usage;
3730 vtarg->sreg1 = vt_reg;
3731 mono_bblock_add_inst (cfg->cbb, vtarg);
3735 MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
3736 vtarg->type = STACK_MP;
3737 vtarg->sreg1 = vt_reg;
3738 mono_bblock_add_inst (cfg->cbb, vtarg);
3747 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3749 MonoInst *ins = NULL;
3751 if (cmethod->klass == mono_defaults.math_class) {
3752 if (strcmp (cmethod->name, "Sin") == 0) {
3753 MONO_INST_NEW (cfg, ins, OP_SIN);
3754 ins->inst_i0 = args [0];
3755 } else if (strcmp (cmethod->name, "Cos") == 0) {
3756 MONO_INST_NEW (cfg, ins, OP_COS);
3757 ins->inst_i0 = args [0];
3758 } else if (strcmp (cmethod->name, "Tan") == 0) {
3759 MONO_INST_NEW (cfg, ins, OP_TAN);
3760 ins->inst_i0 = args [0];
3761 } else if (strcmp (cmethod->name, "Atan") == 0) {
3762 MONO_INST_NEW (cfg, ins, OP_ATAN);
3763 ins->inst_i0 = args [0];
3764 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
3765 MONO_INST_NEW (cfg, ins, OP_SQRT);
3766 ins->inst_i0 = args [0];
3767 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
3768 MONO_INST_NEW (cfg, ins, OP_ABS);
3769 ins->inst_i0 = args [0];
3772 /* OP_FREM is not IEEE compatible */
3773 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
3774 MONO_INST_NEW (cfg, ins, OP_FREM);
3775 ins->inst_i0 = args [0];
3776 ins->inst_i1 = args [1];
3779 } else if (cmethod->klass == mono_defaults.thread_class &&
3780 strcmp (cmethod->name, "MemoryBarrier") == 0) {
3781 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
3782 } else if(cmethod->klass->image == mono_defaults.corlib &&
3783 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
3784 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
3786 if (strcmp (cmethod->name, "Increment") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3787 MonoInst *ins_iconst;
3789 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
3790 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
3791 ins_iconst->inst_c0 = 1;
3793 ins->inst_i0 = args [0];
3794 ins->inst_i1 = ins_iconst;
3795 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3796 MonoInst *ins_iconst;
3798 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
3799 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
3800 ins_iconst->inst_c0 = -1;
3802 ins->inst_i0 = args [0];
3803 ins->inst_i1 = ins_iconst;
3804 } else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3805 MONO_INST_NEW (cfg, ins, OP_ATOMIC_EXCHANGE_I4);
3807 ins->inst_i0 = args [0];
3808 ins->inst_i1 = args [1];
3809 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3810 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_I4);
3812 ins->inst_i0 = args [0];
3813 ins->inst_i1 = args [1];
3822 mono_arch_print_tree (MonoInst *tree, int arity)
3827 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
3831 if (appdomain_tls_offset == -1)
3834 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
3835 ins->inst_offset = appdomain_tls_offset;
3839 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
3843 if (thread_tls_offset == -1)
3846 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
3847 ins->inst_offset = thread_tls_offset;
3852 mono_arch_get_patch_offset (guint8 *code)
3854 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
3856 else if ((code [0] == 0xba))
3858 else if ((code [0] == 0x68))
3861 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
3862 /* push <OFFSET>(<REG>) */
3864 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
3865 /* call *<OFFSET>(<REG>) */
3867 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
3870 else if ((code [0] == 0x58) && (code [1] == 0x05))
3871 /* pop %eax; add <OFFSET>, %eax */
3873 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
3874 /* pop <REG>; add <OFFSET>, <REG> */
3877 g_assert_not_reached ();
3883 mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
3888 /* go to the start of the call instruction
3890 * address_byte = (m << 6) | (o << 3) | reg
3891 * call opcode: 0xff address_byte displacement
3893 * 0xff m=2,o=2 imm32
3896 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
3897 reg = code [4] & 0x07;
3898 disp = (signed char)code [5];
3900 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
3901 reg = code [1] & 0x07;
3902 disp = *((gint32*)(code + 2));
3903 } else if ((code [1] == 0xe8)) {
3905 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
3907 * This is a interface call: should check the above code can't catch it earlier
3908 * 8b 40 30 mov 0x30(%eax),%eax
3909 * ff 10 call *(%eax)
3912 reg = code [5] & 0x07;
3918 return (gpointer*)(((gint32)(regs [reg])) + disp);
3922 mono_arch_get_delegate_method_ptr_addr (guint8* code, gpointer *regs)
3928 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 3) && (x86_modrm_reg (code [1]) == X86_EAX) && (code [2] == 0x8b) && (code [3] == 0x40) && (code [5] == 0xff) && (code [6] == 0xd0)) {
3929 reg = x86_modrm_rm (code [1]);
3935 return (gpointer*)(((gint32)(regs [reg])) + disp);