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.
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/profiler-private.h>
20 #include <mono/utils/mono-math.h>
27 /* On windows, these hold the key returned by TlsAlloc () */
28 static gint lmf_tls_offset = -1;
29 static gint lmf_addr_tls_offset = -1;
30 static gint appdomain_tls_offset = -1;
31 static gint thread_tls_offset = -1;
34 static gboolean optimize_for_xen = TRUE;
36 #define optimize_for_xen 0
40 static gboolean is_win32 = TRUE;
42 static gboolean is_win32 = FALSE;
45 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
50 /* Under windows, the default pinvoke calling convention is stdcall */
51 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
53 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
56 #define NOT_IMPLEMENTED g_assert_not_reached ()
59 mono_arch_regname (int reg) {
61 case X86_EAX: return "%eax";
62 case X86_EBX: return "%ebx";
63 case X86_ECX: return "%ecx";
64 case X86_EDX: return "%edx";
65 case X86_ESP: return "%esp"; case X86_EBP: return "%ebp";
66 case X86_EDI: return "%edi";
67 case X86_ESI: return "%esi";
73 mono_arch_fregname (int reg) {
93 /* Only if storage == ArgValuetypeInReg */
94 ArgStorage pair_storage [2];
103 gboolean need_stack_align;
104 guint32 stack_align_amount;
112 #define FLOAT_PARAM_REGS 0
114 static X86_Reg_No param_regs [] = { 0 };
116 #if defined(PLATFORM_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
117 #define SMALL_STRUCTS_IN_REGS
118 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
122 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
124 ainfo->offset = *stack_size;
126 if (*gr >= PARAM_REGS) {
127 ainfo->storage = ArgOnStack;
128 (*stack_size) += sizeof (gpointer);
131 ainfo->storage = ArgInIReg;
132 ainfo->reg = param_regs [*gr];
138 add_general_pair (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
140 ainfo->offset = *stack_size;
142 g_assert (PARAM_REGS == 0);
144 ainfo->storage = ArgOnStack;
145 (*stack_size) += sizeof (gpointer) * 2;
149 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
151 ainfo->offset = *stack_size;
153 if (*gr >= FLOAT_PARAM_REGS) {
154 ainfo->storage = ArgOnStack;
155 (*stack_size) += is_double ? 8 : 4;
158 /* A double register */
160 ainfo->storage = ArgInDoubleSSEReg;
162 ainfo->storage = ArgInFloatSSEReg;
170 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
172 guint32 *gr, guint32 *fr, guint32 *stack_size)
177 klass = mono_class_from_mono_type (type);
179 size = mono_type_native_stack_size (&klass->byval_arg, NULL);
181 size = mono_type_stack_size (&klass->byval_arg, NULL);
183 #ifdef SMALL_STRUCTS_IN_REGS
184 if (sig->pinvoke && is_return) {
185 MonoMarshalType *info;
188 * the exact rules are not very well documented, the code below seems to work with the
189 * code generated by gcc 3.3.3 -mno-cygwin.
191 info = mono_marshal_load_type_info (klass);
194 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
196 /* Special case structs with only a float member */
197 if ((info->native_size == 8) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R8)) {
198 ainfo->storage = ArgValuetypeInReg;
199 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
202 if ((info->native_size == 4) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R4)) {
203 ainfo->storage = ArgValuetypeInReg;
204 ainfo->pair_storage [0] = ArgOnFloatFpStack;
207 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
208 ainfo->storage = ArgValuetypeInReg;
209 ainfo->pair_storage [0] = ArgInIReg;
210 ainfo->pair_regs [0] = return_regs [0];
211 if (info->native_size > 4) {
212 ainfo->pair_storage [1] = ArgInIReg;
213 ainfo->pair_regs [1] = return_regs [1];
220 ainfo->offset = *stack_size;
221 ainfo->storage = ArgOnStack;
222 *stack_size += ALIGN_TO (size, sizeof (gpointer));
228 * Obtain information about a call according to the calling convention.
229 * For x86 ELF, see the "System V Application Binary Interface Intel386
230 * Architecture Processor Supplment, Fourth Edition" document for more
232 * For x86 win32, see ???.
235 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
239 int n = sig->hasthis + sig->param_count;
240 guint32 stack_size = 0;
243 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
250 ret_type = mono_type_get_underlying_type (sig->ret);
251 switch (ret_type->type) {
252 case MONO_TYPE_BOOLEAN:
263 case MONO_TYPE_FNPTR:
264 case MONO_TYPE_CLASS:
265 case MONO_TYPE_OBJECT:
266 case MONO_TYPE_SZARRAY:
267 case MONO_TYPE_ARRAY:
268 case MONO_TYPE_STRING:
269 cinfo->ret.storage = ArgInIReg;
270 cinfo->ret.reg = X86_EAX;
274 cinfo->ret.storage = ArgInIReg;
275 cinfo->ret.reg = X86_EAX;
278 cinfo->ret.storage = ArgOnFloatFpStack;
281 cinfo->ret.storage = ArgOnDoubleFpStack;
283 case MONO_TYPE_GENERICINST:
284 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
285 cinfo->ret.storage = ArgInIReg;
286 cinfo->ret.reg = X86_EAX;
290 case MONO_TYPE_VALUETYPE: {
291 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
293 add_valuetype (sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
294 if (cinfo->ret.storage == ArgOnStack)
295 /* The caller passes the address where the value is stored */
296 add_general (&gr, &stack_size, &cinfo->ret);
299 case MONO_TYPE_TYPEDBYREF:
300 /* Same as a valuetype with size 24 */
301 add_general (&gr, &stack_size, &cinfo->ret);
305 cinfo->ret.storage = ArgNone;
308 g_error ("Can't handle as return value 0x%x", sig->ret->type);
314 add_general (&gr, &stack_size, cinfo->args + 0);
316 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
318 fr = FLOAT_PARAM_REGS;
320 /* Emit the signature cookie just before the implicit arguments */
321 add_general (&gr, &stack_size, &cinfo->sig_cookie);
324 for (i = 0; i < sig->param_count; ++i) {
325 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
328 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
329 /* We allways pass the sig cookie on the stack for simplicity */
331 * Prevent implicit arguments + the sig cookie from being passed
335 fr = FLOAT_PARAM_REGS;
337 /* Emit the signature cookie just before the implicit arguments */
338 add_general (&gr, &stack_size, &cinfo->sig_cookie);
341 if (sig->params [i]->byref) {
342 add_general (&gr, &stack_size, ainfo);
345 ptype = mono_type_get_underlying_type (sig->params [i]);
346 switch (ptype->type) {
347 case MONO_TYPE_BOOLEAN:
350 add_general (&gr, &stack_size, ainfo);
355 add_general (&gr, &stack_size, ainfo);
359 add_general (&gr, &stack_size, ainfo);
364 case MONO_TYPE_FNPTR:
365 case MONO_TYPE_CLASS:
366 case MONO_TYPE_OBJECT:
367 case MONO_TYPE_STRING:
368 case MONO_TYPE_SZARRAY:
369 case MONO_TYPE_ARRAY:
370 add_general (&gr, &stack_size, ainfo);
372 case MONO_TYPE_GENERICINST:
373 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
374 add_general (&gr, &stack_size, ainfo);
378 case MONO_TYPE_VALUETYPE:
379 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
381 case MONO_TYPE_TYPEDBYREF:
382 stack_size += sizeof (MonoTypedRef);
383 ainfo->storage = ArgOnStack;
387 add_general_pair (&gr, &stack_size, ainfo);
390 add_float (&fr, &stack_size, ainfo, FALSE);
393 add_float (&fr, &stack_size, ainfo, TRUE);
396 g_error ("unexpected type 0x%x", ptype->type);
397 g_assert_not_reached ();
401 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
403 fr = FLOAT_PARAM_REGS;
405 /* Emit the signature cookie just before the implicit arguments */
406 add_general (&gr, &stack_size, &cinfo->sig_cookie);
409 #if defined(__APPLE__)
410 if ((stack_size % 16) != 0) {
411 cinfo->need_stack_align = TRUE;
412 stack_size += cinfo->stack_align_amount = 16-(stack_size % 16);
416 cinfo->stack_usage = stack_size;
417 cinfo->reg_usage = gr;
418 cinfo->freg_usage = fr;
423 * mono_arch_get_argument_info:
424 * @csig: a method signature
425 * @param_count: the number of parameters to consider
426 * @arg_info: an array to store the result infos
428 * Gathers information on parameters such as size, alignment and
429 * padding. arg_info should be large enought to hold param_count + 1 entries.
431 * Returns the size of the activation frame.
434 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
436 int k, frame_size = 0;
442 cinfo = get_call_info (csig, FALSE);
444 if (MONO_TYPE_ISSTRUCT (csig->ret) && (cinfo->ret.storage == ArgOnStack)) {
445 frame_size += sizeof (gpointer);
449 arg_info [0].offset = offset;
452 frame_size += sizeof (gpointer);
456 arg_info [0].size = frame_size;
458 for (k = 0; k < param_count; k++) {
461 size = mono_type_native_stack_size (csig->params [k], &align);
464 size = mono_type_stack_size (csig->params [k], &ialign);
468 /* ignore alignment for now */
471 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
472 arg_info [k].pad = pad;
474 arg_info [k + 1].pad = 0;
475 arg_info [k + 1].size = size;
477 arg_info [k + 1].offset = offset;
481 align = MONO_ARCH_FRAME_ALIGNMENT;
482 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
483 arg_info [k].pad = pad;
490 static const guchar cpuid_impl [] = {
491 0x55, /* push %ebp */
492 0x89, 0xe5, /* mov %esp,%ebp */
493 0x53, /* push %ebx */
494 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
495 0x0f, 0xa2, /* cpuid */
496 0x50, /* push %eax */
497 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
498 0x89, 0x18, /* mov %ebx,(%eax) */
499 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
500 0x89, 0x08, /* mov %ecx,(%eax) */
501 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
502 0x89, 0x10, /* mov %edx,(%eax) */
504 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
505 0x89, 0x02, /* mov %eax,(%edx) */
511 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
514 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
518 __asm__ __volatile__ (
521 "movl %%eax, %%edx\n"
522 "xorl $0x200000, %%eax\n"
527 "xorl %%edx, %%eax\n"
528 "andl $0x200000, %%eax\n"
550 /* Have to use the code manager to get around WinXP DEP */
551 static CpuidFunc func = NULL;
554 ptr = mono_global_codeman_reserve (sizeof (cpuid_impl));
555 memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
556 func = (CpuidFunc)ptr;
558 func (id, p_eax, p_ebx, p_ecx, p_edx);
561 * We use this approach because of issues with gcc and pic code, see:
562 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
563 __asm__ __volatile__ ("cpuid"
564 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
573 * Initialize the cpu to execute managed code.
576 mono_arch_cpu_init (void)
578 /* spec compliance requires running with double precision */
582 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
583 fpcw &= ~X86_FPCW_PRECC_MASK;
584 fpcw |= X86_FPCW_PREC_DOUBLE;
585 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
586 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
588 _control87 (_PC_53, MCW_PC);
593 * This function returns the optimizations supported on this cpu.
596 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
598 int eax, ebx, ecx, edx;
602 /* Feature Flags function, flags returned in EDX. */
603 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
604 if (edx & (1 << 15)) {
605 opts |= MONO_OPT_CMOV;
607 opts |= MONO_OPT_FCMOV;
609 *exclude_mask |= MONO_OPT_FCMOV;
611 *exclude_mask |= MONO_OPT_CMOV;
617 * Determine whenever the trap whose info is in SIGINFO is caused by
621 mono_arch_is_int_overflow (void *sigctx, void *info)
626 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
628 ip = (guint8*)ctx.eip;
630 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
634 switch (x86_modrm_rm (ip [1])) {
654 g_assert_not_reached ();
666 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
671 for (i = 0; i < cfg->num_varinfo; i++) {
672 MonoInst *ins = cfg->varinfo [i];
673 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
676 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
679 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
680 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
683 /* we dont allocate I1 to registers because there is no simply way to sign extend
684 * 8bit quantities in caller saved registers on x86 */
685 if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
686 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
687 g_assert (i == vmv->idx);
688 vars = g_list_prepend (vars, vmv);
692 vars = mono_varlist_sort (cfg, vars, 0);
698 mono_arch_get_global_int_regs (MonoCompile *cfg)
702 /* we can use 3 registers for global allocation */
703 regs = g_list_prepend (regs, (gpointer)X86_EBX);
704 regs = g_list_prepend (regs, (gpointer)X86_ESI);
705 regs = g_list_prepend (regs, (gpointer)X86_EDI);
711 * mono_arch_regalloc_cost:
713 * Return the cost, in number of memory references, of the action of
714 * allocating the variable VMV into a register during global register
718 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
720 MonoInst *ins = cfg->varinfo [vmv->idx];
722 if (cfg->method->save_lmf)
723 /* The register is already saved */
724 return (ins->opcode == OP_ARG) ? 1 : 0;
726 /* push+pop+possible load if it is an argument */
727 return (ins->opcode == OP_ARG) ? 3 : 2;
731 * Set var information according to the calling convention. X86 version.
732 * The locals var stuff should most likely be split in another method.
735 mono_arch_allocate_vars (MonoCompile *cfg)
737 MonoMethodSignature *sig;
738 MonoMethodHeader *header;
740 guint32 locals_stack_size, locals_stack_align;
745 header = mono_method_get_header (cfg->method);
746 sig = mono_method_signature (cfg->method);
748 cinfo = get_call_info (sig, FALSE);
750 cfg->frame_reg = MONO_ARCH_BASEREG;
753 /* Reserve space to save LMF and caller saved registers */
755 if (cfg->method->save_lmf) {
756 offset += sizeof (MonoLMF);
758 if (cfg->used_int_regs & (1 << X86_EBX)) {
762 if (cfg->used_int_regs & (1 << X86_EDI)) {
766 if (cfg->used_int_regs & (1 << X86_ESI)) {
771 switch (cinfo->ret.storage) {
772 case ArgValuetypeInReg:
773 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
775 cfg->ret->opcode = OP_REGOFFSET;
776 cfg->ret->inst_basereg = X86_EBP;
777 cfg->ret->inst_offset = - offset;
783 /* Allocate locals */
784 offsets = mono_allocate_stack_slots (cfg, &locals_stack_size, &locals_stack_align);
785 if (locals_stack_align) {
786 offset += (locals_stack_align - 1);
787 offset &= ~(locals_stack_align - 1);
789 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
790 if (offsets [i] != -1) {
791 MonoInst *inst = cfg->varinfo [i];
792 inst->opcode = OP_REGOFFSET;
793 inst->inst_basereg = X86_EBP;
794 inst->inst_offset = - (offset + offsets [i]);
795 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
798 offset += locals_stack_size;
802 * Allocate arguments+return value
805 switch (cinfo->ret.storage) {
807 cfg->ret->opcode = OP_REGOFFSET;
808 cfg->ret->inst_basereg = X86_EBP;
809 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
811 case ArgValuetypeInReg:
814 cfg->ret->opcode = OP_REGVAR;
815 cfg->ret->inst_c0 = cinfo->ret.reg;
818 case ArgOnFloatFpStack:
819 case ArgOnDoubleFpStack:
822 g_assert_not_reached ();
825 if (sig->call_convention == MONO_CALL_VARARG) {
826 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
827 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
830 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
831 ArgInfo *ainfo = &cinfo->args [i];
832 inst = cfg->varinfo [i];
833 if (inst->opcode != OP_REGVAR) {
834 inst->opcode = OP_REGOFFSET;
835 inst->inst_basereg = X86_EBP;
837 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
840 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
841 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
843 cfg->stack_offset = offset;
849 mono_arch_create_vars (MonoCompile *cfg)
851 MonoMethodSignature *sig;
854 sig = mono_method_signature (cfg->method);
856 cinfo = get_call_info (sig, FALSE);
858 if (cinfo->ret.storage == ArgValuetypeInReg)
859 cfg->ret_var_is_local = TRUE;
864 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
865 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
869 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call)
872 MonoMethodSignature *tmp_sig;
875 /* FIXME: Add support for signature tokens to AOT */
876 cfg->disable_aot = TRUE;
877 MONO_INST_NEW (cfg, arg, OP_OUTARG);
880 * mono_ArgIterator_Setup assumes the signature cookie is
881 * passed first and all the arguments which were before it are
882 * passed on the stack after the signature. So compensate by
883 * passing a different signature.
885 tmp_sig = mono_metadata_signature_dup (call->signature);
886 tmp_sig->param_count -= call->signature->sentinelpos;
887 tmp_sig->sentinelpos = 0;
888 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
890 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
891 sig_arg->inst_p0 = tmp_sig;
893 arg->inst_left = sig_arg;
894 arg->type = STACK_PTR;
895 /* prepend, so they get reversed */
896 arg->next = call->out_args;
897 call->out_args = arg;
901 * take the arguments and generate the arch-specific
902 * instructions to properly call the function in call.
903 * This includes pushing, moving arguments to the right register
907 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
909 MonoMethodSignature *sig;
914 sig = call->signature;
915 n = sig->param_count + sig->hasthis;
917 cinfo = get_call_info (sig, FALSE);
919 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
920 sentinelpos = sig->sentinelpos + (is_virtual ? 1 : 0);
922 for (i = 0; i < n; ++i) {
923 ArgInfo *ainfo = cinfo->args + i;
925 /* Emit the signature cookie just before the implicit arguments */
926 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
927 emit_sig_cookie (cfg, call);
930 if (is_virtual && i == 0) {
931 /* the argument will be attached to the call instrucion */
936 if (i >= sig->hasthis)
937 t = sig->params [i - sig->hasthis];
939 t = &mono_defaults.int_class->byval_arg;
940 t = mono_type_get_underlying_type (t);
942 MONO_INST_NEW (cfg, arg, OP_OUTARG);
944 arg->cil_code = in->cil_code;
946 arg->type = in->type;
947 /* prepend, so they get reversed */
948 arg->next = call->out_args;
949 call->out_args = arg;
951 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
954 if (t->type == MONO_TYPE_TYPEDBYREF) {
955 size = sizeof (MonoTypedRef);
956 align = sizeof (gpointer);
960 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
963 size = mono_type_stack_size (&in->klass->byval_arg, &ialign);
966 arg->opcode = OP_OUTARG_VT;
967 arg->klass = in->klass;
968 arg->backend.is_pinvoke = sig->pinvoke;
969 arg->inst_imm = size;
972 switch (ainfo->storage) {
974 arg->opcode = OP_OUTARG;
976 if (t->type == MONO_TYPE_R4)
977 arg->opcode = OP_OUTARG_R4;
979 if (t->type == MONO_TYPE_R8)
980 arg->opcode = OP_OUTARG_R8;
984 g_assert_not_reached ();
990 /* Handle the case where there are no implicit arguments */
991 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
992 emit_sig_cookie (cfg, call);
995 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
996 if (cinfo->ret.storage == ArgValuetypeInReg) {
999 * After the call, the struct is in registers, but needs to be saved to the memory pointed
1000 * to by vt_arg in this_vret_args. This means that vt_arg needs to be saved somewhere
1001 * before calling the function. So we add a dummy instruction to represent pushing the
1002 * struct return address to the stack. The return address will be saved to this stack slot
1003 * by the code emitted in this_vret_args.
1005 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1006 MONO_INST_NEW (cfg, zero_inst, OP_ICONST);
1007 zero_inst->inst_p0 = 0;
1008 arg->inst_left = zero_inst;
1009 arg->type = STACK_PTR;
1010 /* prepend, so they get reversed */
1011 arg->next = call->out_args;
1012 call->out_args = arg;
1015 /* if the function returns a struct, the called method already does a ret $0x4 */
1016 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
1017 cinfo->stack_usage -= 4;
1020 call->stack_usage = cinfo->stack_usage;
1022 #if defined(__APPLE__)
1023 if (cinfo->need_stack_align) {
1024 MONO_INST_NEW (cfg, arg, OP_X86_OUTARG_ALIGN_STACK);
1025 arg->inst_c0 = cinfo->stack_align_amount;
1026 arg->next = call->out_args;
1027 call->out_args = arg;
1037 * Allow tracing to work with this interface (with an optional argument)
1040 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1045 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1048 /* if some args are passed in registers, we need to save them here */
1049 x86_push_reg (code, X86_EBP);
1051 if (cfg->compile_aot) {
1052 x86_push_imm (code, cfg->method);
1053 x86_mov_reg_imm (code, X86_EAX, func);
1054 x86_call_reg (code, X86_EAX);
1056 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1057 x86_push_imm (code, cfg->method);
1058 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1059 x86_call_code (code, 0);
1062 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 16);
1064 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1079 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1082 int arg_size = 0, save_mode = SAVE_NONE;
1083 MonoMethod *method = cfg->method;
1085 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
1086 case MONO_TYPE_VOID:
1087 /* special case string .ctor icall */
1088 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1089 save_mode = SAVE_EAX;
1091 save_mode = SAVE_NONE;
1095 save_mode = SAVE_EAX_EDX;
1099 save_mode = SAVE_FP;
1101 case MONO_TYPE_GENERICINST:
1102 if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method)->ret)) {
1103 save_mode = SAVE_EAX;
1107 case MONO_TYPE_VALUETYPE:
1108 save_mode = SAVE_STRUCT;
1111 save_mode = SAVE_EAX;
1115 switch (save_mode) {
1117 x86_push_reg (code, X86_EDX);
1118 x86_push_reg (code, X86_EAX);
1119 if (enable_arguments) {
1120 x86_push_reg (code, X86_EDX);
1121 x86_push_reg (code, X86_EAX);
1126 x86_push_reg (code, X86_EAX);
1127 if (enable_arguments) {
1128 x86_push_reg (code, X86_EAX);
1133 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1134 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1135 if (enable_arguments) {
1136 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1137 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1142 if (enable_arguments) {
1143 x86_push_membase (code, X86_EBP, 8);
1152 if (cfg->compile_aot) {
1153 x86_push_imm (code, method);
1154 x86_mov_reg_imm (code, X86_EAX, func);
1155 x86_call_reg (code, X86_EAX);
1157 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1158 x86_push_imm (code, method);
1159 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1160 x86_call_code (code, 0);
1162 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1164 switch (save_mode) {
1166 x86_pop_reg (code, X86_EAX);
1167 x86_pop_reg (code, X86_EDX);
1170 x86_pop_reg (code, X86_EAX);
1173 x86_fld_membase (code, X86_ESP, 0, TRUE);
1174 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1184 #define EMIT_COND_BRANCH(ins,cond,sign) \
1185 if (ins->flags & MONO_INST_BRLABEL) { \
1186 if (ins->inst_i0->inst_c0) { \
1187 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
1189 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1190 if ((cfg->opt & MONO_OPT_BRANCH) && \
1191 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
1192 x86_branch8 (code, cond, 0, sign); \
1194 x86_branch32 (code, cond, 0, sign); \
1197 if (ins->inst_true_bb->native_offset) { \
1198 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1200 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1201 if ((cfg->opt & MONO_OPT_BRANCH) && \
1202 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1203 x86_branch8 (code, cond, 0, sign); \
1205 x86_branch32 (code, cond, 0, sign); \
1210 * Emit an exception if condition is fail and
1211 * if possible do a directly branch to target
1213 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1215 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1216 if (tins == NULL) { \
1217 mono_add_patch_info (cfg, code - cfg->native_code, \
1218 MONO_PATCH_INFO_EXC, exc_name); \
1219 x86_branch32 (code, cond, 0, signed); \
1221 EMIT_COND_BRANCH (tins, cond, signed); \
1225 #define EMIT_FPCOMPARE(code) do { \
1226 x86_fcompp (code); \
1227 x86_fnstsw (code); \
1232 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1234 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1235 x86_call_code (code, 0);
1240 /* FIXME: Add more instructions */
1241 #define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_STORE_MEMBASE_IMM) || ((ins)->opcode == OP_STOREI4_MEMBASE_REG))
1244 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1246 MonoInst *ins, *last_ins = NULL;
1251 switch (ins->opcode) {
1253 /* reg = 0 -> XOR (reg, reg) */
1254 /* XOR sets cflags on x86, so we cant do it always */
1255 if (ins->inst_c0 == 0 && ins->next && INST_IGNORES_CFLAGS (ins->next)) {
1256 ins->opcode = CEE_XOR;
1257 ins->sreg1 = ins->dreg;
1258 ins->sreg2 = ins->dreg;
1262 /* remove unnecessary multiplication with 1 */
1263 if (ins->inst_imm == 1) {
1264 if (ins->dreg != ins->sreg1) {
1265 ins->opcode = OP_MOVE;
1267 last_ins->next = ins->next;
1273 case OP_COMPARE_IMM:
1274 /* OP_COMPARE_IMM (reg, 0)
1276 * OP_X86_TEST_NULL (reg)
1279 ins->opcode = OP_X86_TEST_NULL;
1281 case OP_X86_COMPARE_MEMBASE_IMM:
1283 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1284 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1286 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1287 * OP_COMPARE_IMM reg, imm
1289 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1291 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1292 ins->inst_basereg == last_ins->inst_destbasereg &&
1293 ins->inst_offset == last_ins->inst_offset) {
1294 ins->opcode = OP_COMPARE_IMM;
1295 ins->sreg1 = last_ins->sreg1;
1297 /* check if we can remove cmp reg,0 with test null */
1299 ins->opcode = OP_X86_TEST_NULL;
1303 case OP_LOAD_MEMBASE:
1304 case OP_LOADI4_MEMBASE:
1306 * Note: if reg1 = reg2 the load op is removed
1308 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1309 * OP_LOAD_MEMBASE offset(basereg), reg2
1311 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1312 * OP_MOVE reg1, reg2
1314 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1315 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1316 ins->inst_basereg == last_ins->inst_destbasereg &&
1317 ins->inst_offset == last_ins->inst_offset) {
1318 if (ins->dreg == last_ins->sreg1) {
1319 last_ins->next = ins->next;
1323 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1324 ins->opcode = OP_MOVE;
1325 ins->sreg1 = last_ins->sreg1;
1329 * Note: reg1 must be different from the basereg in the second load
1330 * Note: if reg1 = reg2 is equal then second load is removed
1332 * OP_LOAD_MEMBASE offset(basereg), reg1
1333 * OP_LOAD_MEMBASE offset(basereg), reg2
1335 * OP_LOAD_MEMBASE offset(basereg), reg1
1336 * OP_MOVE reg1, reg2
1338 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1339 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1340 ins->inst_basereg != last_ins->dreg &&
1341 ins->inst_basereg == last_ins->inst_basereg &&
1342 ins->inst_offset == last_ins->inst_offset) {
1344 if (ins->dreg == last_ins->dreg) {
1345 last_ins->next = ins->next;
1349 ins->opcode = OP_MOVE;
1350 ins->sreg1 = last_ins->dreg;
1353 //g_assert_not_reached ();
1357 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1358 * OP_LOAD_MEMBASE offset(basereg), reg
1360 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1361 * OP_ICONST reg, imm
1363 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1364 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1365 ins->inst_basereg == last_ins->inst_destbasereg &&
1366 ins->inst_offset == last_ins->inst_offset) {
1367 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1368 ins->opcode = OP_ICONST;
1369 ins->inst_c0 = last_ins->inst_imm;
1370 g_assert_not_reached (); // check this rule
1374 case OP_LOADU1_MEMBASE:
1375 case OP_LOADI1_MEMBASE:
1377 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1378 * OP_LOAD_MEMBASE offset(basereg), reg2
1380 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1381 * CONV_I2/U2 reg1, reg2
1383 if (last_ins && X86_IS_BYTE_REG (last_ins->sreg1) &&
1384 (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1385 ins->inst_basereg == last_ins->inst_destbasereg &&
1386 ins->inst_offset == last_ins->inst_offset) {
1387 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? CEE_CONV_I1 : CEE_CONV_U1;
1388 ins->sreg1 = last_ins->sreg1;
1391 case OP_LOADU2_MEMBASE:
1392 case OP_LOADI2_MEMBASE:
1394 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1395 * OP_LOAD_MEMBASE offset(basereg), reg2
1397 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1398 * CONV_I2/U2 reg1, reg2
1400 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1401 ins->inst_basereg == last_ins->inst_destbasereg &&
1402 ins->inst_offset == last_ins->inst_offset) {
1403 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? CEE_CONV_I2 : CEE_CONV_U2;
1404 ins->sreg1 = last_ins->sreg1;
1415 if (ins->dreg == ins->sreg1) {
1417 last_ins->next = ins->next;
1424 * OP_MOVE sreg, dreg
1425 * OP_MOVE dreg, sreg
1427 if (last_ins && last_ins->opcode == OP_MOVE &&
1428 ins->sreg1 == last_ins->dreg &&
1429 ins->dreg == last_ins->sreg1) {
1430 last_ins->next = ins->next;
1436 case OP_X86_PUSH_MEMBASE:
1437 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1438 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1439 ins->inst_basereg == last_ins->inst_destbasereg &&
1440 ins->inst_offset == last_ins->inst_offset) {
1441 ins->opcode = OP_X86_PUSH;
1442 ins->sreg1 = last_ins->sreg1;
1449 bb->last_ins = last_ins;
1453 branch_cc_table [] = {
1454 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1455 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1456 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1459 /*#include "cprop.c"*/
1461 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1463 mono_local_regalloc (cfg, bb);
1466 static unsigned char*
1467 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1469 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1470 x86_fnstcw_membase(code, X86_ESP, 0);
1471 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1472 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1473 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1474 x86_fldcw_membase (code, X86_ESP, 2);
1476 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1477 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1478 x86_pop_reg (code, dreg);
1479 /* FIXME: need the high register
1480 * x86_pop_reg (code, dreg_high);
1483 x86_push_reg (code, X86_EAX); // SP = SP - 4
1484 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1485 x86_pop_reg (code, dreg);
1487 x86_fldcw_membase (code, X86_ESP, 0);
1488 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1491 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1493 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1497 static unsigned char*
1498 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1500 int sreg = tree->sreg1;
1501 int need_touch = FALSE;
1503 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
1512 * If requested stack size is larger than one page,
1513 * perform stack-touch operation
1516 * Generate stack probe code.
1517 * Under Windows, it is necessary to allocate one page at a time,
1518 * "touching" stack after each successful sub-allocation. This is
1519 * because of the way stack growth is implemented - there is a
1520 * guard page before the lowest stack page that is currently commited.
1521 * Stack normally grows sequentially so OS traps access to the
1522 * guard page and commits more pages when needed.
1524 x86_test_reg_imm (code, sreg, ~0xFFF);
1525 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1527 br[2] = code; /* loop */
1528 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
1529 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
1532 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
1533 * that follows only initializes the last part of the area.
1535 /* Same as the init code below with size==0x1000 */
1536 if (tree->flags & MONO_INST_INIT) {
1537 x86_push_reg (code, X86_EAX);
1538 x86_push_reg (code, X86_ECX);
1539 x86_push_reg (code, X86_EDI);
1540 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
1541 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1542 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
1544 x86_prefix (code, X86_REP_PREFIX);
1546 x86_pop_reg (code, X86_EDI);
1547 x86_pop_reg (code, X86_ECX);
1548 x86_pop_reg (code, X86_EAX);
1551 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
1552 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
1553 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
1554 x86_patch (br[3], br[2]);
1555 x86_test_reg_reg (code, sreg, sreg);
1556 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1557 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1559 br[1] = code; x86_jump8 (code, 0);
1561 x86_patch (br[0], code);
1562 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1563 x86_patch (br[1], code);
1564 x86_patch (br[4], code);
1567 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1569 if (tree->flags & MONO_INST_INIT) {
1571 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1572 x86_push_reg (code, X86_EAX);
1575 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1576 x86_push_reg (code, X86_ECX);
1579 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1580 x86_push_reg (code, X86_EDI);
1584 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1585 if (sreg != X86_ECX)
1586 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1587 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1589 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1591 x86_prefix (code, X86_REP_PREFIX);
1594 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1595 x86_pop_reg (code, X86_EDI);
1596 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1597 x86_pop_reg (code, X86_ECX);
1598 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1599 x86_pop_reg (code, X86_EAX);
1606 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
1611 /* Move return value to the target register */
1612 switch (ins->opcode) {
1615 case OP_CALL_MEMBASE:
1616 if (ins->dreg != X86_EAX)
1617 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
1621 case OP_VCALL_MEMBASE:
1622 cinfo = get_call_info (((MonoCallInst*)ins)->signature, FALSE);
1623 if (cinfo->ret.storage == ArgValuetypeInReg) {
1624 /* Pop the destination address from the stack */
1625 x86_pop_reg (code, X86_ECX);
1627 for (quad = 0; quad < 2; quad ++) {
1628 switch (cinfo->ret.pair_storage [quad]) {
1630 g_assert (cinfo->ret.pair_regs [quad] != X86_ECX);
1631 x86_mov_membase_reg (code, X86_ECX, (quad * sizeof (gpointer)), cinfo->ret.pair_regs [quad], sizeof (gpointer));
1636 g_assert_not_reached ();
1650 * @code: buffer to store code to
1651 * @dreg: hard register where to place the result
1652 * @tls_offset: offset info
1654 * emit_tls_get emits in @code the native code that puts in the dreg register
1655 * the item in the thread local storage identified by tls_offset.
1657 * Returns: a pointer to the end of the stored code
1660 emit_tls_get (guint8* code, int dreg, int tls_offset)
1662 #ifdef PLATFORM_WIN32
1664 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
1665 * Journal and/or a disassembly of the TlsGet () function.
1667 g_assert (tls_offset < 64);
1668 x86_prefix (code, X86_FS_PREFIX);
1669 x86_mov_reg_mem (code, dreg, 0x18, 4);
1670 /* Dunno what this does but TlsGetValue () contains it */
1671 x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
1672 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
1674 if (optimize_for_xen) {
1675 x86_prefix (code, X86_GS_PREFIX);
1676 x86_mov_reg_mem (code, dreg, 0, 4);
1677 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
1679 x86_prefix (code, X86_GS_PREFIX);
1680 x86_mov_reg_mem (code, dreg, tls_offset, 4);
1687 * emit_load_volatile_arguments:
1689 * Load volatile arguments from the stack to the original input registers.
1690 * Required before a tail call.
1693 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
1695 MonoMethod *method = cfg->method;
1696 MonoMethodSignature *sig;
1701 /* FIXME: Generate intermediate code instead */
1703 sig = mono_method_signature (method);
1705 cinfo = get_call_info (sig, FALSE);
1707 /* This is the opposite of the code in emit_prolog */
1709 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1710 ArgInfo *ainfo = cinfo->args + i;
1712 inst = cfg->varinfo [i];
1714 if (sig->hasthis && (i == 0))
1715 arg_type = &mono_defaults.object_class->byval_arg;
1717 arg_type = sig->params [i - sig->hasthis];
1720 * On x86, the arguments are either in their original stack locations, or in
1723 if (inst->opcode == OP_REGVAR) {
1724 g_assert (ainfo->storage == ArgOnStack);
1726 x86_mov_membase_reg (code, X86_EBP, inst->inst_offset, inst->dreg, 4);
1735 #define REAL_PRINT_REG(text,reg) \
1736 mono_assert (reg >= 0); \
1737 x86_push_reg (code, X86_EAX); \
1738 x86_push_reg (code, X86_EDX); \
1739 x86_push_reg (code, X86_ECX); \
1740 x86_push_reg (code, reg); \
1741 x86_push_imm (code, reg); \
1742 x86_push_imm (code, text " %d %p\n"); \
1743 x86_mov_reg_imm (code, X86_EAX, printf); \
1744 x86_call_reg (code, X86_EAX); \
1745 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
1746 x86_pop_reg (code, X86_ECX); \
1747 x86_pop_reg (code, X86_EDX); \
1748 x86_pop_reg (code, X86_EAX);
1750 /* benchmark and set based on cpu */
1751 #define LOOP_ALIGNMENT 8
1752 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
1755 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1760 guint8 *code = cfg->native_code + cfg->code_len;
1761 MonoInst *last_ins = NULL;
1762 guint last_offset = 0;
1765 if (cfg->opt & MONO_OPT_PEEPHOLE)
1766 peephole_pass (cfg, bb);
1768 if (cfg->opt & MONO_OPT_LOOP) {
1769 int pad, align = LOOP_ALIGNMENT;
1770 /* set alignment depending on cpu */
1771 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
1773 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
1774 x86_padding (code, pad);
1775 cfg->code_len += pad;
1776 bb->native_offset = cfg->code_len;
1780 if (cfg->verbose_level > 2)
1781 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1783 cpos = bb->max_offset;
1785 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1786 MonoProfileCoverageInfo *cov = cfg->coverage_info;
1787 g_assert (!cfg->compile_aot);
1790 cov->data [bb->dfn].cil_code = bb->cil_code;
1791 /* this is not thread save, but good enough */
1792 x86_inc_mem (code, &cov->data [bb->dfn].count);
1795 offset = code - cfg->native_code;
1797 mono_debug_open_block (cfg, bb, offset);
1801 offset = code - cfg->native_code;
1803 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
1805 if (offset > (cfg->code_size - max_len - 16)) {
1806 cfg->code_size *= 2;
1807 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1808 code = cfg->native_code + offset;
1809 mono_jit_stats.code_reallocs++;
1812 mono_debug_record_line_number (cfg, ins, offset);
1814 switch (ins->opcode) {
1816 x86_mul_reg (code, ins->sreg2, TRUE);
1819 x86_mul_reg (code, ins->sreg2, FALSE);
1821 case OP_X86_SETEQ_MEMBASE:
1822 case OP_X86_SETNE_MEMBASE:
1823 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
1824 ins->inst_basereg, ins->inst_offset, TRUE);
1826 case OP_STOREI1_MEMBASE_IMM:
1827 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
1829 case OP_STOREI2_MEMBASE_IMM:
1830 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
1832 case OP_STORE_MEMBASE_IMM:
1833 case OP_STOREI4_MEMBASE_IMM:
1834 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
1836 case OP_STOREI1_MEMBASE_REG:
1837 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
1839 case OP_STOREI2_MEMBASE_REG:
1840 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
1842 case OP_STORE_MEMBASE_REG:
1843 case OP_STOREI4_MEMBASE_REG:
1844 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
1849 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
1852 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
1853 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
1855 case OP_LOAD_MEMBASE:
1856 case OP_LOADI4_MEMBASE:
1857 case OP_LOADU4_MEMBASE:
1858 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
1860 case OP_LOADU1_MEMBASE:
1861 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
1863 case OP_LOADI1_MEMBASE:
1864 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
1866 case OP_LOADU2_MEMBASE:
1867 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
1869 case OP_LOADI2_MEMBASE:
1870 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
1873 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
1876 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
1879 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
1882 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
1885 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
1887 case OP_COMPARE_IMM:
1888 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
1890 case OP_X86_COMPARE_MEMBASE_REG:
1891 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
1893 case OP_X86_COMPARE_MEMBASE_IMM:
1894 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1896 case OP_X86_COMPARE_MEMBASE8_IMM:
1897 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1899 case OP_X86_COMPARE_REG_MEMBASE:
1900 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
1902 case OP_X86_COMPARE_MEM_IMM:
1903 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
1905 case OP_X86_TEST_NULL:
1906 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
1908 case OP_X86_ADD_MEMBASE_IMM:
1909 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1911 case OP_X86_ADD_MEMBASE:
1912 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
1914 case OP_X86_SUB_MEMBASE_IMM:
1915 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1917 case OP_X86_SUB_MEMBASE:
1918 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
1920 case OP_X86_AND_MEMBASE_IMM:
1921 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1923 case OP_X86_OR_MEMBASE_IMM:
1924 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1926 case OP_X86_XOR_MEMBASE_IMM:
1927 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1929 case OP_X86_INC_MEMBASE:
1930 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
1932 case OP_X86_INC_REG:
1933 x86_inc_reg (code, ins->dreg);
1935 case OP_X86_DEC_MEMBASE:
1936 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
1938 case OP_X86_DEC_REG:
1939 x86_dec_reg (code, ins->dreg);
1941 case OP_X86_MUL_MEMBASE:
1942 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
1945 x86_breakpoint (code);
1949 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
1952 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
1956 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
1959 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
1963 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
1966 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
1970 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
1973 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
1976 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
1979 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
1983 x86_div_reg (code, ins->sreg2, TRUE);
1986 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
1987 x86_div_reg (code, ins->sreg2, FALSE);
1990 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
1992 x86_div_reg (code, ins->sreg2, TRUE);
1996 x86_div_reg (code, ins->sreg2, TRUE);
1999 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2000 x86_div_reg (code, ins->sreg2, FALSE);
2003 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2005 x86_div_reg (code, ins->sreg2, TRUE);
2008 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2011 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2014 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2017 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2020 g_assert (ins->sreg2 == X86_ECX);
2021 x86_shift_reg (code, X86_SHL, ins->dreg);
2024 g_assert (ins->sreg2 == X86_ECX);
2025 x86_shift_reg (code, X86_SAR, ins->dreg);
2028 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2031 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2034 g_assert (ins->sreg2 == X86_ECX);
2035 x86_shift_reg (code, X86_SHR, ins->dreg);
2038 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2041 guint8 *jump_to_end;
2043 /* handle shifts below 32 bits */
2044 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
2045 x86_shift_reg (code, X86_SHL, ins->sreg1);
2047 x86_test_reg_imm (code, X86_ECX, 32);
2048 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2050 /* handle shift over 32 bit */
2051 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2052 x86_clear_reg (code, ins->sreg1);
2054 x86_patch (jump_to_end, code);
2058 guint8 *jump_to_end;
2060 /* handle shifts below 32 bits */
2061 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2062 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
2064 x86_test_reg_imm (code, X86_ECX, 32);
2065 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2067 /* handle shifts over 31 bits */
2068 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2069 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
2071 x86_patch (jump_to_end, code);
2075 guint8 *jump_to_end;
2077 /* handle shifts below 32 bits */
2078 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2079 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
2081 x86_test_reg_imm (code, X86_ECX, 32);
2082 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2084 /* handle shifts over 31 bits */
2085 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2086 x86_clear_reg (code, ins->backend.reg3);
2088 x86_patch (jump_to_end, code);
2092 if (ins->inst_imm >= 32) {
2093 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2094 x86_clear_reg (code, ins->sreg1);
2095 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
2097 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
2098 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
2102 if (ins->inst_imm >= 32) {
2103 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2104 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
2105 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
2107 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2108 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
2111 case OP_LSHR_UN_IMM:
2112 if (ins->inst_imm >= 32) {
2113 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2114 x86_clear_reg (code, ins->backend.reg3);
2115 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
2117 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2118 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
2122 x86_not_reg (code, ins->sreg1);
2125 x86_neg_reg (code, ins->sreg1);
2128 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2131 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2134 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2137 switch (ins->inst_imm) {
2141 if (ins->dreg != ins->sreg1)
2142 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2143 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2146 /* LEA r1, [r2 + r2*2] */
2147 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2150 /* LEA r1, [r2 + r2*4] */
2151 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2154 /* LEA r1, [r2 + r2*2] */
2156 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2157 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2160 /* LEA r1, [r2 + r2*8] */
2161 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
2164 /* LEA r1, [r2 + r2*4] */
2166 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2167 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2170 /* LEA r1, [r2 + r2*2] */
2172 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2173 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2176 /* LEA r1, [r2 + r2*4] */
2177 /* LEA r1, [r1 + r1*4] */
2178 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2179 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2182 /* LEA r1, [r2 + r2*4] */
2184 /* LEA r1, [r1 + r1*4] */
2185 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2186 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2187 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2190 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2195 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2196 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2198 case CEE_MUL_OVF_UN: {
2199 /* the mul operation and the exception check should most likely be split */
2200 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2201 /*g_assert (ins->sreg2 == X86_EAX);
2202 g_assert (ins->dreg == X86_EAX);*/
2203 if (ins->sreg2 == X86_EAX) {
2204 non_eax_reg = ins->sreg1;
2205 } else if (ins->sreg1 == X86_EAX) {
2206 non_eax_reg = ins->sreg2;
2208 /* no need to save since we're going to store to it anyway */
2209 if (ins->dreg != X86_EAX) {
2211 x86_push_reg (code, X86_EAX);
2213 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2214 non_eax_reg = ins->sreg2;
2216 if (ins->dreg == X86_EDX) {
2219 x86_push_reg (code, X86_EAX);
2221 } else if (ins->dreg != X86_EAX) {
2223 x86_push_reg (code, X86_EDX);
2225 x86_mul_reg (code, non_eax_reg, FALSE);
2226 /* save before the check since pop and mov don't change the flags */
2227 if (ins->dreg != X86_EAX)
2228 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2230 x86_pop_reg (code, X86_EDX);
2232 x86_pop_reg (code, X86_EAX);
2233 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2237 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2240 g_assert_not_reached ();
2241 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2242 x86_mov_reg_imm (code, ins->dreg, 0);
2244 case OP_LOAD_GOTADDR:
2245 x86_call_imm (code, 0);
2247 * The patch needs to point to the pop, since the GOT offset needs
2248 * to be added to that address.
2250 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
2251 x86_pop_reg (code, ins->dreg);
2252 x86_alu_reg_imm (code, X86_ADD, ins->dreg, 0xf0f0f0f0);
2255 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2256 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
2258 case OP_X86_PUSH_GOT_ENTRY:
2259 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2260 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
2264 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2267 g_assert_not_reached ();
2270 * Note: this 'frame destruction' logic is useful for tail calls, too.
2271 * Keep in sync with the code in emit_epilog.
2275 /* FIXME: no tracing support... */
2276 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2277 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2278 /* reset offset to make max_len work */
2279 offset = code - cfg->native_code;
2281 g_assert (!cfg->method->save_lmf);
2283 code = emit_load_volatile_arguments (cfg, code);
2285 if (cfg->used_int_regs & (1 << X86_EBX))
2287 if (cfg->used_int_regs & (1 << X86_EDI))
2289 if (cfg->used_int_regs & (1 << X86_ESI))
2292 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2294 if (cfg->used_int_regs & (1 << X86_ESI))
2295 x86_pop_reg (code, X86_ESI);
2296 if (cfg->used_int_regs & (1 << X86_EDI))
2297 x86_pop_reg (code, X86_EDI);
2298 if (cfg->used_int_regs & (1 << X86_EBX))
2299 x86_pop_reg (code, X86_EBX);
2301 /* restore ESP/EBP */
2303 offset = code - cfg->native_code;
2304 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2305 x86_jump32 (code, 0);
2309 /* ensure ins->sreg1 is not NULL
2310 * note that cmp DWORD PTR [eax], eax is one byte shorter than
2311 * cmp DWORD PTR [eax], 0
2313 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
2316 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2317 x86_push_reg (code, hreg);
2318 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2319 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2320 x86_pop_reg (code, hreg);
2328 call = (MonoCallInst*)ins;
2329 if (ins->flags & MONO_INST_HAS_METHOD)
2330 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2332 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2333 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2334 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
2335 * bytes to pop, we want to use pops. GCC does this (note it won't happen
2336 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
2337 * smart enough to do that optimization yet
2339 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
2340 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
2341 * (most likely from locality benefits). People with other processors should
2342 * check on theirs to see what happens.
2344 if (call->stack_usage == 4) {
2345 /* we want to use registers that won't get used soon, so use
2346 * ecx, as eax will get allocated first. edx is used by long calls,
2347 * so we can't use that.
2350 x86_pop_reg (code, X86_ECX);
2352 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2355 code = emit_move_return_value (cfg, ins, code);
2360 case OP_VOIDCALL_REG:
2362 call = (MonoCallInst*)ins;
2363 x86_call_reg (code, ins->sreg1);
2364 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2365 if (call->stack_usage == 4)
2366 x86_pop_reg (code, X86_ECX);
2368 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2370 code = emit_move_return_value (cfg, ins, code);
2372 case OP_FCALL_MEMBASE:
2373 case OP_LCALL_MEMBASE:
2374 case OP_VCALL_MEMBASE:
2375 case OP_VOIDCALL_MEMBASE:
2376 case OP_CALL_MEMBASE:
2377 call = (MonoCallInst*)ins;
2378 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2379 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2380 if (call->stack_usage == 4)
2381 x86_pop_reg (code, X86_ECX);
2383 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2385 code = emit_move_return_value (cfg, ins, code);
2389 x86_push_reg (code, ins->sreg1);
2391 case OP_X86_PUSH_IMM:
2392 x86_push_imm (code, ins->inst_imm);
2394 case OP_X86_PUSH_MEMBASE:
2395 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2397 case OP_X86_PUSH_OBJ:
2398 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2399 x86_push_reg (code, X86_EDI);
2400 x86_push_reg (code, X86_ESI);
2401 x86_push_reg (code, X86_ECX);
2402 if (ins->inst_offset)
2403 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2405 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2406 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2407 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2409 x86_prefix (code, X86_REP_PREFIX);
2411 x86_pop_reg (code, X86_ECX);
2412 x86_pop_reg (code, X86_ESI);
2413 x86_pop_reg (code, X86_EDI);
2416 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
2418 case OP_X86_LEA_MEMBASE:
2419 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
2422 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2425 /* keep alignment */
2426 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
2427 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
2428 code = mono_emit_stack_alloc (code, ins);
2429 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2435 x86_push_reg (code, ins->sreg1);
2436 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2437 (gpointer)"mono_arch_throw_exception");
2441 x86_push_reg (code, ins->sreg1);
2442 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2443 (gpointer)"mono_arch_rethrow_exception");
2446 case OP_CALL_HANDLER:
2449 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 12);
2451 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2452 x86_call_imm (code, 0);
2454 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2458 ins->inst_c0 = code - cfg->native_code;
2461 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2462 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2464 if (ins->flags & MONO_INST_BRLABEL) {
2465 if (ins->inst_i0->inst_c0) {
2466 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2468 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2469 if ((cfg->opt & MONO_OPT_BRANCH) &&
2470 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
2471 x86_jump8 (code, 0);
2473 x86_jump32 (code, 0);
2476 if (ins->inst_target_bb->native_offset) {
2477 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2479 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2480 if ((cfg->opt & MONO_OPT_BRANCH) &&
2481 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
2482 x86_jump8 (code, 0);
2484 x86_jump32 (code, 0);
2489 x86_jump_reg (code, ins->sreg1);
2492 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2493 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2496 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
2497 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2500 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2501 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2504 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
2505 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2508 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2509 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2512 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
2513 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2515 case OP_COND_EXC_EQ:
2516 case OP_COND_EXC_NE_UN:
2517 case OP_COND_EXC_LT:
2518 case OP_COND_EXC_LT_UN:
2519 case OP_COND_EXC_GT:
2520 case OP_COND_EXC_GT_UN:
2521 case OP_COND_EXC_GE:
2522 case OP_COND_EXC_GE_UN:
2523 case OP_COND_EXC_LE:
2524 case OP_COND_EXC_LE_UN:
2525 case OP_COND_EXC_OV:
2526 case OP_COND_EXC_NO:
2528 case OP_COND_EXC_NC:
2529 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2541 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
2544 /* floating point opcodes */
2546 double d = *(double *)ins->inst_p0;
2548 if ((d == 0.0) && (mono_signbit (d) == 0)) {
2550 } else if (d == 1.0) {
2553 if (cfg->compile_aot) {
2554 guint32 *val = (guint32*)&d;
2555 x86_push_imm (code, val [1]);
2556 x86_push_imm (code, val [0]);
2557 x86_fld_membase (code, X86_ESP, 0, TRUE);
2558 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2561 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
2562 x86_fld (code, NULL, TRUE);
2568 float f = *(float *)ins->inst_p0;
2570 if ((f == 0.0) && (mono_signbit (f) == 0)) {
2572 } else if (f == 1.0) {
2575 if (cfg->compile_aot) {
2576 guint32 val = *(guint32*)&f;
2577 x86_push_imm (code, val);
2578 x86_fld_membase (code, X86_ESP, 0, FALSE);
2579 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2582 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
2583 x86_fld (code, NULL, FALSE);
2588 case OP_STORER8_MEMBASE_REG:
2589 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
2591 case OP_LOADR8_SPILL_MEMBASE:
2592 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2595 case OP_LOADR8_MEMBASE:
2596 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2598 case OP_STORER4_MEMBASE_REG:
2599 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
2601 case OP_LOADR4_MEMBASE:
2602 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2604 case CEE_CONV_R4: /* FIXME: change precision */
2606 x86_push_reg (code, ins->sreg1);
2607 x86_fild_membase (code, X86_ESP, 0, FALSE);
2608 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2610 case OP_X86_FP_LOAD_I8:
2611 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2613 case OP_X86_FP_LOAD_I4:
2614 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2616 case OP_FCONV_TO_I1:
2617 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2619 case OP_FCONV_TO_U1:
2620 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2622 case OP_FCONV_TO_I2:
2623 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2625 case OP_FCONV_TO_U2:
2626 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2628 case OP_FCONV_TO_I4:
2630 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2632 case OP_FCONV_TO_I8:
2633 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2634 x86_fnstcw_membase(code, X86_ESP, 0);
2635 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
2636 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
2637 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
2638 x86_fldcw_membase (code, X86_ESP, 2);
2639 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2640 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2641 x86_pop_reg (code, ins->dreg);
2642 x86_pop_reg (code, ins->backend.reg3);
2643 x86_fldcw_membase (code, X86_ESP, 0);
2644 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2646 case OP_LCONV_TO_R_UN: {
2647 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2650 /* load 64bit integer to FP stack */
2651 x86_push_imm (code, 0);
2652 x86_push_reg (code, ins->sreg2);
2653 x86_push_reg (code, ins->sreg1);
2654 x86_fild_membase (code, X86_ESP, 0, TRUE);
2655 /* store as 80bit FP value */
2656 x86_fst80_membase (code, X86_ESP, 0);
2658 /* test if lreg is negative */
2659 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2660 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2662 /* add correction constant mn */
2663 x86_fld80_mem (code, mn);
2664 x86_fld80_membase (code, X86_ESP, 0);
2665 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2666 x86_fst80_membase (code, X86_ESP, 0);
2668 x86_patch (br, code);
2670 x86_fld80_membase (code, X86_ESP, 0);
2671 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2675 case OP_LCONV_TO_OVF_I: {
2676 guint8 *br [3], *label [1];
2680 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2682 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2684 /* If the low word top bit is set, see if we are negative */
2685 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2686 /* We are not negative (no top bit set, check for our top word to be zero */
2687 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2688 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2691 /* throw exception */
2692 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
2694 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
2695 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
2696 x86_jump8 (code, 0);
2698 x86_jump32 (code, 0);
2700 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2701 x86_jump32 (code, 0);
2705 x86_patch (br [0], code);
2706 /* our top bit is set, check that top word is 0xfffffff */
2707 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2709 x86_patch (br [1], code);
2710 /* nope, emit exception */
2711 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2712 x86_patch (br [2], label [0]);
2714 if (ins->dreg != ins->sreg1)
2715 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2719 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2722 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
2725 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
2728 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
2736 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2741 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2748 * it really doesn't make sense to inline all this code,
2749 * it's here just to show that things may not be as simple
2752 guchar *check_pos, *end_tan, *pop_jump;
2753 x86_push_reg (code, X86_EAX);
2756 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
2758 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2759 x86_fstp (code, 0); /* pop the 1.0 */
2761 x86_jump8 (code, 0);
2763 x86_fp_op (code, X86_FADD, 0);
2767 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
2769 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2772 x86_patch (pop_jump, code);
2773 x86_fstp (code, 0); /* pop the 1.0 */
2774 x86_patch (check_pos, code);
2775 x86_patch (end_tan, code);
2777 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2778 x86_pop_reg (code, X86_EAX);
2785 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2796 x86_push_reg (code, X86_EAX);
2797 /* we need to exchange ST(0) with ST(1) */
2800 /* this requires a loop, because fprem somtimes
2801 * returns a partial remainder */
2803 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
2804 /* x86_fprem1 (code); */
2807 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
2809 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
2814 x86_pop_reg (code, X86_EAX);
2818 if (cfg->opt & MONO_OPT_FCMOV) {
2819 x86_fcomip (code, 1);
2823 /* this overwrites EAX */
2824 EMIT_FPCOMPARE(code);
2825 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2828 if (cfg->opt & MONO_OPT_FCMOV) {
2829 /* zeroing the register at the start results in
2830 * shorter and faster code (we can also remove the widening op)
2832 guchar *unordered_check;
2833 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2834 x86_fcomip (code, 1);
2836 unordered_check = code;
2837 x86_branch8 (code, X86_CC_P, 0, FALSE);
2838 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
2839 x86_patch (unordered_check, code);
2842 if (ins->dreg != X86_EAX)
2843 x86_push_reg (code, X86_EAX);
2845 EMIT_FPCOMPARE(code);
2846 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2847 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2848 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2849 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2851 if (ins->dreg != X86_EAX)
2852 x86_pop_reg (code, X86_EAX);
2856 if (cfg->opt & MONO_OPT_FCMOV) {
2857 /* zeroing the register at the start results in
2858 * shorter and faster code (we can also remove the widening op)
2860 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2861 x86_fcomip (code, 1);
2863 if (ins->opcode == OP_FCLT_UN) {
2864 guchar *unordered_check = code;
2865 guchar *jump_to_end;
2866 x86_branch8 (code, X86_CC_P, 0, FALSE);
2867 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2869 x86_jump8 (code, 0);
2870 x86_patch (unordered_check, code);
2871 x86_inc_reg (code, ins->dreg);
2872 x86_patch (jump_to_end, code);
2874 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2878 if (ins->dreg != X86_EAX)
2879 x86_push_reg (code, X86_EAX);
2881 EMIT_FPCOMPARE(code);
2882 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2883 if (ins->opcode == OP_FCLT_UN) {
2884 guchar *is_not_zero_check, *end_jump;
2885 is_not_zero_check = code;
2886 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2888 x86_jump8 (code, 0);
2889 x86_patch (is_not_zero_check, code);
2890 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
2892 x86_patch (end_jump, code);
2894 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2895 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2897 if (ins->dreg != X86_EAX)
2898 x86_pop_reg (code, X86_EAX);
2902 if (cfg->opt & MONO_OPT_FCMOV) {
2903 /* zeroing the register at the start results in
2904 * shorter and faster code (we can also remove the widening op)
2906 guchar *unordered_check;
2907 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2908 x86_fcomip (code, 1);
2910 if (ins->opcode == OP_FCGT) {
2911 unordered_check = code;
2912 x86_branch8 (code, X86_CC_P, 0, FALSE);
2913 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2914 x86_patch (unordered_check, code);
2916 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2920 if (ins->dreg != X86_EAX)
2921 x86_push_reg (code, X86_EAX);
2923 EMIT_FPCOMPARE(code);
2924 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2925 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
2926 if (ins->opcode == OP_FCGT_UN) {
2927 guchar *is_not_zero_check, *end_jump;
2928 is_not_zero_check = code;
2929 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2931 x86_jump8 (code, 0);
2932 x86_patch (is_not_zero_check, code);
2933 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
2935 x86_patch (end_jump, code);
2937 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2938 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2940 if (ins->dreg != X86_EAX)
2941 x86_pop_reg (code, X86_EAX);
2944 if (cfg->opt & MONO_OPT_FCMOV) {
2945 guchar *jump = code;
2946 x86_branch8 (code, X86_CC_P, 0, TRUE);
2947 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2948 x86_patch (jump, code);
2951 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2952 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
2955 /* Branch if C013 != 100 */
2956 if (cfg->opt & MONO_OPT_FCMOV) {
2957 /* branch if !ZF or (PF|CF) */
2958 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2959 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2960 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
2963 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
2964 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2967 if (cfg->opt & MONO_OPT_FCMOV) {
2968 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2971 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2974 if (cfg->opt & MONO_OPT_FCMOV) {
2975 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2976 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2979 if (ins->opcode == OP_FBLT_UN) {
2980 guchar *is_not_zero_check, *end_jump;
2981 is_not_zero_check = code;
2982 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2984 x86_jump8 (code, 0);
2985 x86_patch (is_not_zero_check, code);
2986 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
2988 x86_patch (end_jump, code);
2990 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2994 if (cfg->opt & MONO_OPT_FCMOV) {
2995 if (ins->opcode == OP_FBGT) {
2998 /* skip branch if C1=1 */
3000 x86_branch8 (code, X86_CC_P, 0, FALSE);
3001 /* branch if (C0 | C3) = 1 */
3002 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3003 x86_patch (br1, code);
3005 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3009 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3010 if (ins->opcode == OP_FBGT_UN) {
3011 guchar *is_not_zero_check, *end_jump;
3012 is_not_zero_check = code;
3013 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3015 x86_jump8 (code, 0);
3016 x86_patch (is_not_zero_check, code);
3017 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3019 x86_patch (end_jump, code);
3021 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3024 /* Branch if C013 == 100 or 001 */
3025 if (cfg->opt & MONO_OPT_FCMOV) {
3028 /* skip branch if C1=1 */
3030 x86_branch8 (code, X86_CC_P, 0, FALSE);
3031 /* branch if (C0 | C3) = 1 */
3032 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
3033 x86_patch (br1, code);
3036 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3037 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3038 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3039 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3042 /* Branch if C013 == 000 */
3043 if (cfg->opt & MONO_OPT_FCMOV) {
3044 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
3047 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3050 /* Branch if C013=000 or 100 */
3051 if (cfg->opt & MONO_OPT_FCMOV) {
3054 /* skip branch if C1=1 */
3056 x86_branch8 (code, X86_CC_P, 0, FALSE);
3057 /* branch if C0=0 */
3058 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
3059 x86_patch (br1, code);
3062 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
3063 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
3064 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3067 /* Branch if C013 != 001 */
3068 if (cfg->opt & MONO_OPT_FCMOV) {
3069 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3070 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
3073 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3074 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3076 case CEE_CKFINITE: {
3077 x86_push_reg (code, X86_EAX);
3080 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
3081 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3082 x86_pop_reg (code, X86_EAX);
3083 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
3087 code = emit_tls_get (code, ins->dreg, ins->inst_offset);
3090 case OP_MEMORY_BARRIER: {
3091 /* Not needed on x86 */
3094 case OP_ATOMIC_ADD_I4: {
3095 int dreg = ins->dreg;
3097 if (dreg == ins->inst_basereg) {
3098 x86_push_reg (code, ins->sreg2);
3102 if (dreg != ins->sreg2)
3103 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
3105 x86_prefix (code, X86_LOCK_PREFIX);
3106 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3108 if (dreg != ins->dreg) {
3109 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3110 x86_pop_reg (code, dreg);
3115 case OP_ATOMIC_ADD_NEW_I4: {
3116 int dreg = ins->dreg;
3118 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
3119 if (ins->sreg2 == dreg) {
3120 if (dreg == X86_EBX) {
3122 if (ins->inst_basereg == X86_EDI)
3126 if (ins->inst_basereg == X86_EBX)
3129 } else if (ins->inst_basereg == dreg) {
3130 if (dreg == X86_EBX) {
3132 if (ins->sreg2 == X86_EDI)
3136 if (ins->sreg2 == X86_EBX)
3141 if (dreg != ins->dreg) {
3142 x86_push_reg (code, dreg);
3145 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
3146 x86_prefix (code, X86_LOCK_PREFIX);
3147 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3148 /* dreg contains the old value, add with sreg2 value */
3149 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
3151 if (ins->dreg != dreg) {
3152 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3153 x86_pop_reg (code, dreg);
3158 case OP_ATOMIC_EXCHANGE_I4: {
3160 int sreg2 = ins->sreg2;
3161 int breg = ins->inst_basereg;
3163 /* cmpxchg uses eax as comperand, need to make sure we can use it
3164 * hack to overcome limits in x86 reg allocator
3165 * (req: dreg == eax and sreg2 != eax and breg != eax)
3167 if (ins->dreg != X86_EAX)
3168 x86_push_reg (code, X86_EAX);
3170 /* We need the EAX reg for the cmpxchg */
3171 if (ins->sreg2 == X86_EAX) {
3172 x86_push_reg (code, X86_EDX);
3173 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
3177 if (breg == X86_EAX) {
3178 x86_push_reg (code, X86_ESI);
3179 x86_mov_reg_reg (code, X86_ESI, X86_EAX, 4);
3183 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
3185 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
3186 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
3187 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
3188 x86_patch (br [1], br [0]);
3190 if (breg != ins->inst_basereg)
3191 x86_pop_reg (code, X86_ESI);
3193 if (ins->dreg != X86_EAX) {
3194 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3195 x86_pop_reg (code, X86_EAX);
3198 if (ins->sreg2 != sreg2)
3199 x86_pop_reg (code, X86_EDX);
3204 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3205 g_assert_not_reached ();
3208 if ((code - cfg->native_code - offset) > max_len) {
3209 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3210 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3211 g_assert_not_reached ();
3217 last_offset = offset;
3222 cfg->code_len = code - cfg->native_code;
3226 mono_arch_register_lowlevel_calls (void)
3231 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3233 MonoJumpInfo *patch_info;
3234 gboolean compile_aot = !run_cctors;
3236 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3237 unsigned char *ip = patch_info->ip.i + code;
3238 const unsigned char *target;
3240 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3243 switch (patch_info->type) {
3244 case MONO_PATCH_INFO_BB:
3245 case MONO_PATCH_INFO_LABEL:
3248 /* No need to patch these */
3253 switch (patch_info->type) {
3254 case MONO_PATCH_INFO_IP:
3255 *((gconstpointer *)(ip)) = target;
3257 case MONO_PATCH_INFO_CLASS_INIT: {
3259 /* Might already been changed to a nop */
3260 x86_call_code (code, 0);
3261 x86_patch (ip, target);
3264 case MONO_PATCH_INFO_ABS:
3265 case MONO_PATCH_INFO_METHOD:
3266 case MONO_PATCH_INFO_METHOD_JUMP:
3267 case MONO_PATCH_INFO_INTERNAL_METHOD:
3268 case MONO_PATCH_INFO_BB:
3269 case MONO_PATCH_INFO_LABEL:
3270 x86_patch (ip, target);
3272 case MONO_PATCH_INFO_NONE:
3275 guint32 offset = mono_arch_get_patch_offset (ip);
3276 *((gconstpointer *)(ip + offset)) = target;
3284 mono_arch_emit_prolog (MonoCompile *cfg)
3286 MonoMethod *method = cfg->method;
3288 MonoMethodSignature *sig;
3290 int alloc_size, pos, max_offset, i;
3293 cfg->code_size = MAX (mono_method_get_header (method)->code_size * 4, 256);
3294 code = cfg->native_code = g_malloc (cfg->code_size);
3296 x86_push_reg (code, X86_EBP);
3297 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
3299 alloc_size = cfg->stack_offset;
3302 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
3303 /* Might need to attach the thread to the JIT */
3304 if (lmf_tls_offset != -1) {
3307 code = emit_tls_get ( code, X86_EAX, lmf_tls_offset);
3308 x86_test_reg_reg (code, X86_EAX, X86_EAX);
3310 x86_branch8 (code, X86_CC_NE, 0, 0);
3311 x86_push_imm (code, cfg->domain);
3312 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
3313 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3314 x86_patch (buf, code);
3315 #ifdef PLATFORM_WIN32
3316 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3317 /* FIXME: Add a separate key for LMF to avoid this */
3318 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
3321 g_assert (!cfg->compile_aot);
3322 x86_push_imm (code, cfg->domain);
3323 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
3324 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3328 if (method->save_lmf) {
3329 pos += sizeof (MonoLMF);
3331 /* save the current IP */
3332 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3333 x86_push_imm_template (code);
3335 /* save all caller saved regs */
3336 x86_push_reg (code, X86_EBP);
3337 x86_push_reg (code, X86_ESI);
3338 x86_push_reg (code, X86_EDI);
3339 x86_push_reg (code, X86_EBX);
3341 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
3343 * Optimized version which uses the mono_lmf TLS variable instead of indirection
3344 * through the mono_lmf_addr TLS variable.
3346 /* %eax = previous_lmf */
3347 x86_prefix (code, X86_GS_PREFIX);
3348 x86_mov_reg_mem (code, X86_EAX, lmf_tls_offset, 4);
3349 /* skip method_info + lmf */
3350 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3351 /* push previous_lmf */
3352 x86_push_reg (code, X86_EAX);
3354 x86_prefix (code, X86_GS_PREFIX);
3355 x86_mov_mem_reg (code, lmf_tls_offset, X86_ESP, 4);
3357 /* get the address of lmf for the current thread */
3359 * This is performance critical so we try to use some tricks to make
3363 if (lmf_addr_tls_offset != -1) {
3364 /* Load lmf quicky using the GS register */
3365 code = emit_tls_get (code, X86_EAX, lmf_addr_tls_offset);
3366 #ifdef PLATFORM_WIN32
3367 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3368 /* FIXME: Add a separate key for LMF to avoid this */
3369 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
3372 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
3375 /* Skip method info */
3376 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3379 x86_push_reg (code, X86_EAX);
3380 /* push *lfm (previous_lmf) */
3381 x86_push_membase (code, X86_EAX, 0);
3383 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3387 if (cfg->used_int_regs & (1 << X86_EBX)) {
3388 x86_push_reg (code, X86_EBX);
3392 if (cfg->used_int_regs & (1 << X86_EDI)) {
3393 x86_push_reg (code, X86_EDI);
3397 if (cfg->used_int_regs & (1 << X86_ESI)) {
3398 x86_push_reg (code, X86_ESI);
3406 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
3408 int tot = alloc_size + pos + 4 + 4; /* ret ip + ebp */
3420 /* See mono_emit_stack_alloc */
3421 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3422 guint32 remaining_size = alloc_size;
3423 while (remaining_size >= 0x1000) {
3424 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
3425 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
3426 remaining_size -= 0x1000;
3429 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
3431 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
3436 /* check the stack is aligned */
3437 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
3438 x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
3439 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
3440 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
3441 x86_breakpoint (code);
3444 /* compute max_offset in order to use short forward jumps */
3446 if (cfg->opt & MONO_OPT_BRANCH) {
3447 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3448 MonoInst *ins = bb->code;
3449 bb->max_offset = max_offset;
3451 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3453 /* max alignment for loops */
3454 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
3455 max_offset += LOOP_ALIGNMENT;
3458 if (ins->opcode == OP_LABEL)
3459 ins->inst_c1 = max_offset;
3461 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3467 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3468 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3470 /* load arguments allocated to register from the stack */
3471 sig = mono_method_signature (method);
3474 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3475 inst = cfg->varinfo [pos];
3476 if (inst->opcode == OP_REGVAR) {
3477 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
3478 if (cfg->verbose_level > 2)
3479 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3484 cfg->code_len = code - cfg->native_code;
3490 mono_arch_emit_epilog (MonoCompile *cfg)
3492 MonoMethod *method = cfg->method;
3493 MonoMethodSignature *sig = mono_method_signature (method);
3495 guint32 stack_to_pop;
3497 int max_epilog_size = 16;
3500 if (cfg->method->save_lmf)
3501 max_epilog_size += 128;
3503 if (mono_jit_trace_calls != NULL)
3504 max_epilog_size += 50;
3506 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3507 cfg->code_size *= 2;
3508 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3509 mono_jit_stats.code_reallocs++;
3512 code = cfg->native_code + cfg->code_len;
3514 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3515 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3517 /* the code restoring the registers must be kept in sync with CEE_JMP */
3520 if (method->save_lmf) {
3521 gint32 prev_lmf_reg;
3522 gint32 lmf_offset = -sizeof (MonoLMF);
3524 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
3526 * Optimized version which uses the mono_lmf TLS variable instead of indirection
3527 * through the mono_lmf_addr TLS variable.
3529 /* reg = previous_lmf */
3530 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
3532 /* lmf = previous_lmf */
3533 x86_prefix (code, X86_GS_PREFIX);
3534 x86_mov_mem_reg (code, lmf_tls_offset, X86_ECX, 4);
3536 /* Find a spare register */
3537 switch (sig->ret->type) {
3540 prev_lmf_reg = X86_EDI;
3541 cfg->used_int_regs |= (1 << X86_EDI);
3544 prev_lmf_reg = X86_EDX;
3548 /* reg = previous_lmf */
3549 x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
3552 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 4);
3554 /* *(lmf) = previous_lmf */
3555 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
3558 /* restore caller saved regs */
3559 if (cfg->used_int_regs & (1 << X86_EBX)) {
3560 x86_mov_reg_membase (code, X86_EBX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), 4);
3563 if (cfg->used_int_regs & (1 << X86_EDI)) {
3564 x86_mov_reg_membase (code, X86_EDI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), 4);
3566 if (cfg->used_int_regs & (1 << X86_ESI)) {
3567 x86_mov_reg_membase (code, X86_ESI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), 4);
3570 /* EBP is restored by LEAVE */
3572 if (cfg->used_int_regs & (1 << X86_EBX)) {
3575 if (cfg->used_int_regs & (1 << X86_EDI)) {
3578 if (cfg->used_int_regs & (1 << X86_ESI)) {
3583 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3585 if (cfg->used_int_regs & (1 << X86_ESI)) {
3586 x86_pop_reg (code, X86_ESI);
3588 if (cfg->used_int_regs & (1 << X86_EDI)) {
3589 x86_pop_reg (code, X86_EDI);
3591 if (cfg->used_int_regs & (1 << X86_EBX)) {
3592 x86_pop_reg (code, X86_EBX);
3596 /* Load returned vtypes into registers if needed */
3597 cinfo = get_call_info (sig, FALSE);
3598 if (cinfo->ret.storage == ArgValuetypeInReg) {
3599 for (quad = 0; quad < 2; quad ++) {
3600 switch (cinfo->ret.pair_storage [quad]) {
3602 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
3604 case ArgOnFloatFpStack:
3605 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
3607 case ArgOnDoubleFpStack:
3608 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
3613 g_assert_not_reached ();
3620 if (CALLCONV_IS_STDCALL (sig)) {
3621 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
3623 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
3624 } else if (MONO_TYPE_ISSTRUCT (mono_method_signature (cfg->method)->ret) && (cinfo->ret.storage == ArgOnStack))
3630 x86_ret_imm (code, stack_to_pop);
3636 cfg->code_len = code - cfg->native_code;
3638 g_assert (cfg->code_len < cfg->code_size);
3642 mono_arch_emit_exceptions (MonoCompile *cfg)
3644 MonoJumpInfo *patch_info;
3647 MonoClass *exc_classes [16];
3648 guint8 *exc_throw_start [16], *exc_throw_end [16];
3652 /* Compute needed space */
3653 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3654 if (patch_info->type == MONO_PATCH_INFO_EXC)
3659 * make sure we have enough space for exceptions
3660 * 16 is the size of two push_imm instructions and a call
3662 if (cfg->compile_aot)
3663 code_size = exc_count * 32;
3665 code_size = exc_count * 16;
3667 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
3668 cfg->code_size *= 2;
3669 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3670 mono_jit_stats.code_reallocs++;
3673 code = cfg->native_code + cfg->code_len;
3676 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3677 switch (patch_info->type) {
3678 case MONO_PATCH_INFO_EXC: {
3679 MonoClass *exc_class;
3683 x86_patch (patch_info->ip.i + cfg->native_code, code);
3685 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
3686 g_assert (exc_class);
3687 throw_ip = patch_info->ip.i;
3689 /* Find a throw sequence for the same exception class */
3690 for (i = 0; i < nthrows; ++i)
3691 if (exc_classes [i] == exc_class)
3694 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
3695 x86_jump_code (code, exc_throw_start [i]);
3696 patch_info->type = MONO_PATCH_INFO_NONE;
3701 /* Compute size of code following the push <OFFSET> */
3704 if ((code - cfg->native_code) - throw_ip < 126 - size) {
3705 /* Use the shorter form */
3707 x86_push_imm (code, 0);
3711 x86_push_imm (code, 0xf0f0f0f0);
3716 exc_classes [nthrows] = exc_class;
3717 exc_throw_start [nthrows] = code;
3720 x86_push_imm (code, exc_class->type_token);
3721 patch_info->data.name = "mono_arch_throw_corlib_exception";
3722 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3723 patch_info->ip.i = code - cfg->native_code;
3724 x86_call_code (code, 0);
3725 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
3730 exc_throw_end [nthrows] = code;
3742 cfg->code_len = code - cfg->native_code;
3744 g_assert (cfg->code_len < cfg->code_size);
3748 mono_arch_flush_icache (guint8 *code, gint size)
3754 mono_arch_flush_register_windows (void)
3759 * Support for fast access to the thread-local lmf structure using the GS
3760 * segment register on NPTL + kernel 2.6.x.
3763 static gboolean tls_offset_inited = FALSE;
3766 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3768 if (!tls_offset_inited) {
3769 if (!getenv ("MONO_NO_TLS")) {
3770 #ifdef PLATFORM_WIN32
3772 * We need to init this multiple times, since when we are first called, the key might not
3773 * be initialized yet.
3775 appdomain_tls_offset = mono_domain_get_tls_key ();
3776 lmf_tls_offset = mono_get_jit_tls_key ();
3777 thread_tls_offset = mono_thread_get_tls_key ();
3779 /* Only 64 tls entries can be accessed using inline code */
3780 if (appdomain_tls_offset >= 64)
3781 appdomain_tls_offset = -1;
3782 if (lmf_tls_offset >= 64)
3783 lmf_tls_offset = -1;
3784 if (thread_tls_offset >= 64)
3785 thread_tls_offset = -1;
3788 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
3790 tls_offset_inited = TRUE;
3791 appdomain_tls_offset = mono_domain_get_tls_offset ();
3792 lmf_tls_offset = mono_get_lmf_tls_offset ();
3793 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
3794 thread_tls_offset = mono_thread_get_tls_offset ();
3801 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3806 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3808 MonoCallInst *call = (MonoCallInst*)inst;
3809 CallInfo *cinfo = get_call_info (inst->signature, FALSE);
3811 /* add the this argument */
3812 if (this_reg != -1) {
3813 if (cinfo->args [0].storage == ArgInIReg) {
3815 MONO_INST_NEW (cfg, this, OP_MOVE);
3816 this->type = this_type;
3817 this->sreg1 = this_reg;
3818 this->dreg = mono_regstate_next_int (cfg->rs);
3819 mono_bblock_add_inst (cfg->cbb, this);
3821 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, cinfo->args [0].reg, FALSE);
3825 MONO_INST_NEW (cfg, this, OP_OUTARG);
3826 this->type = this_type;
3827 this->sreg1 = this_reg;
3828 mono_bblock_add_inst (cfg->cbb, this);
3835 if (cinfo->ret.storage == ArgValuetypeInReg) {
3837 * The valuetype is in EAX:EDX after the call, needs to be copied to
3838 * the stack. Save the address here, so the call instruction can
3841 MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
3842 vtarg->inst_destbasereg = X86_ESP;
3843 vtarg->inst_offset = inst->stack_usage;
3844 vtarg->sreg1 = vt_reg;
3845 mono_bblock_add_inst (cfg->cbb, vtarg);
3847 else if (cinfo->ret.storage == ArgInIReg) {
3848 /* The return address is passed in a register */
3849 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
3850 vtarg->sreg1 = vt_reg;
3851 vtarg->dreg = mono_regstate_next_int (cfg->rs);
3852 mono_bblock_add_inst (cfg->cbb, vtarg);
3854 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
3857 MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
3858 vtarg->type = STACK_MP;
3859 vtarg->sreg1 = vt_reg;
3860 mono_bblock_add_inst (cfg->cbb, vtarg);
3868 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3870 MonoInst *ins = NULL;
3872 if (cmethod->klass == mono_defaults.math_class) {
3873 if (strcmp (cmethod->name, "Sin") == 0) {
3874 MONO_INST_NEW (cfg, ins, OP_SIN);
3875 ins->inst_i0 = args [0];
3876 } else if (strcmp (cmethod->name, "Cos") == 0) {
3877 MONO_INST_NEW (cfg, ins, OP_COS);
3878 ins->inst_i0 = args [0];
3879 } else if (strcmp (cmethod->name, "Tan") == 0) {
3880 MONO_INST_NEW (cfg, ins, OP_TAN);
3881 ins->inst_i0 = args [0];
3882 } else if (strcmp (cmethod->name, "Atan") == 0) {
3883 MONO_INST_NEW (cfg, ins, OP_ATAN);
3884 ins->inst_i0 = args [0];
3885 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
3886 MONO_INST_NEW (cfg, ins, OP_SQRT);
3887 ins->inst_i0 = args [0];
3888 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
3889 MONO_INST_NEW (cfg, ins, OP_ABS);
3890 ins->inst_i0 = args [0];
3893 /* OP_FREM is not IEEE compatible */
3894 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
3895 MONO_INST_NEW (cfg, ins, OP_FREM);
3896 ins->inst_i0 = args [0];
3897 ins->inst_i1 = args [1];
3900 } else if (cmethod->klass == mono_defaults.thread_class &&
3901 strcmp (cmethod->name, "MemoryBarrier") == 0) {
3902 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
3903 } else if(cmethod->klass->image == mono_defaults.corlib &&
3904 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
3905 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
3907 if (strcmp (cmethod->name, "Increment") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3908 MonoInst *ins_iconst;
3910 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
3911 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
3912 ins_iconst->inst_c0 = 1;
3914 ins->inst_i0 = args [0];
3915 ins->inst_i1 = ins_iconst;
3916 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3917 MonoInst *ins_iconst;
3919 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
3920 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
3921 ins_iconst->inst_c0 = -1;
3923 ins->inst_i0 = args [0];
3924 ins->inst_i1 = ins_iconst;
3925 } else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3926 MONO_INST_NEW (cfg, ins, OP_ATOMIC_EXCHANGE_I4);
3928 ins->inst_i0 = args [0];
3929 ins->inst_i1 = args [1];
3930 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3931 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
3933 ins->inst_i0 = args [0];
3934 ins->inst_i1 = args [1];
3943 mono_arch_print_tree (MonoInst *tree, int arity)
3948 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
3952 if (appdomain_tls_offset == -1)
3955 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
3956 ins->inst_offset = appdomain_tls_offset;
3960 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
3964 if (thread_tls_offset == -1)
3967 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
3968 ins->inst_offset = thread_tls_offset;
3973 mono_arch_get_patch_offset (guint8 *code)
3975 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
3977 else if ((code [0] == 0xba))
3979 else if ((code [0] == 0x68))
3982 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
3983 /* push <OFFSET>(<REG>) */
3985 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
3986 /* call *<OFFSET>(<REG>) */
3988 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
3991 else if ((code [0] == 0x58) && (code [1] == 0x05))
3992 /* pop %eax; add <OFFSET>, %eax */
3994 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
3995 /* pop <REG>; add <OFFSET>, <REG> */
3998 g_assert_not_reached ();
4004 mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
4009 /* go to the start of the call instruction
4011 * address_byte = (m << 6) | (o << 3) | reg
4012 * call opcode: 0xff address_byte displacement
4014 * 0xff m=2,o=2 imm32
4019 * A given byte sequence can match more than case here, so we have to be
4020 * really careful about the ordering of the cases. Longer sequences
4023 if ((code [-2] == 0x8b) && (x86_modrm_mod (code [-1]) == 0x2) && (code [4] == 0xff) && (x86_modrm_reg (code [5]) == 0x2) && (x86_modrm_mod (code [5]) == 0x0)) {
4025 * This is an interface call
4026 * 8b 80 0c e8 ff ff mov 0xffffe80c(%eax),%eax
4027 * ff 10 call *(%eax)
4029 reg = x86_modrm_rm (code [5]);
4031 } else if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
4032 reg = code [4] & 0x07;
4033 disp = (signed char)code [5];
4035 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
4036 reg = code [1] & 0x07;
4037 disp = *((gint32*)(code + 2));
4038 } else if ((code [1] == 0xe8)) {
4040 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
4042 * This is a interface call
4043 * 8b 40 30 mov 0x30(%eax),%eax
4044 * ff 10 call *(%eax)
4047 reg = code [5] & 0x07;
4053 return (gpointer*)(((gint32)(regs [reg])) + disp);
4057 mono_arch_get_delegate_method_ptr_addr (guint8* code, gpointer *regs)
4063 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)) {
4064 reg = x86_modrm_rm (code [1]);
4070 return (gpointer*)(((gint32)(regs [reg])) + disp);