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.
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/threads.h>
21 #include <mono/metadata/profiler-private.h>
22 #include <mono/utils/mono-math.h>
29 /* On windows, these hold the key returned by TlsAlloc () */
30 static gint lmf_tls_offset = -1;
31 static gint lmf_addr_tls_offset = -1;
32 static gint appdomain_tls_offset = -1;
33 static gint thread_tls_offset = -1;
36 static gboolean optimize_for_xen = TRUE;
38 #define optimize_for_xen 0
42 static gboolean is_win32 = TRUE;
44 static gboolean is_win32 = FALSE;
47 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
52 /* Under windows, the default pinvoke calling convention is stdcall */
53 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
55 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
58 #define NOT_IMPLEMENTED g_assert_not_reached ()
61 mono_arch_regname (int reg) {
63 case X86_EAX: return "%eax";
64 case X86_EBX: return "%ebx";
65 case X86_ECX: return "%ecx";
66 case X86_EDX: return "%edx";
67 case X86_ESP: return "%esp"; case X86_EBP: return "%ebp";
68 case X86_EDI: return "%edi";
69 case X86_ESI: return "%esi";
75 mono_arch_fregname (int reg) {
95 /* Only if storage == ArgValuetypeInReg */
96 ArgStorage pair_storage [2];
105 gboolean need_stack_align;
106 guint32 stack_align_amount;
114 #define FLOAT_PARAM_REGS 0
116 static X86_Reg_No param_regs [] = { 0 };
118 #if defined(PLATFORM_WIN32) || defined(__APPLE__) || defined(__FreeBSD__)
119 #define SMALL_STRUCTS_IN_REGS
120 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
124 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
126 ainfo->offset = *stack_size;
128 if (*gr >= PARAM_REGS) {
129 ainfo->storage = ArgOnStack;
130 (*stack_size) += sizeof (gpointer);
133 ainfo->storage = ArgInIReg;
134 ainfo->reg = param_regs [*gr];
140 add_general_pair (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
142 ainfo->offset = *stack_size;
144 g_assert (PARAM_REGS == 0);
146 ainfo->storage = ArgOnStack;
147 (*stack_size) += sizeof (gpointer) * 2;
151 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
153 ainfo->offset = *stack_size;
155 if (*gr >= FLOAT_PARAM_REGS) {
156 ainfo->storage = ArgOnStack;
157 (*stack_size) += is_double ? 8 : 4;
160 /* A double register */
162 ainfo->storage = ArgInDoubleSSEReg;
164 ainfo->storage = ArgInFloatSSEReg;
172 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
174 guint32 *gr, guint32 *fr, guint32 *stack_size)
179 klass = mono_class_from_mono_type (type);
181 size = mono_type_native_stack_size (&klass->byval_arg, NULL);
183 size = mono_type_stack_size (&klass->byval_arg, NULL);
185 #ifdef SMALL_STRUCTS_IN_REGS
186 if (sig->pinvoke && is_return) {
187 MonoMarshalType *info;
190 * the exact rules are not very well documented, the code below seems to work with the
191 * code generated by gcc 3.3.3 -mno-cygwin.
193 info = mono_marshal_load_type_info (klass);
196 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
198 /* Special case structs with only a float member */
199 if ((info->native_size == 8) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R8)) {
200 ainfo->storage = ArgValuetypeInReg;
201 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
204 if ((info->native_size == 4) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R4)) {
205 ainfo->storage = ArgValuetypeInReg;
206 ainfo->pair_storage [0] = ArgOnFloatFpStack;
209 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
210 ainfo->storage = ArgValuetypeInReg;
211 ainfo->pair_storage [0] = ArgInIReg;
212 ainfo->pair_regs [0] = return_regs [0];
213 if (info->native_size > 4) {
214 ainfo->pair_storage [1] = ArgInIReg;
215 ainfo->pair_regs [1] = return_regs [1];
222 ainfo->offset = *stack_size;
223 ainfo->storage = ArgOnStack;
224 *stack_size += ALIGN_TO (size, sizeof (gpointer));
230 * Obtain information about a call according to the calling convention.
231 * For x86 ELF, see the "System V Application Binary Interface Intel386
232 * Architecture Processor Supplment, Fourth Edition" document for more
234 * For x86 win32, see ???.
237 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig, gboolean is_pinvoke)
241 int n = sig->hasthis + sig->param_count;
242 guint32 stack_size = 0;
246 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
248 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
255 ret_type = mono_type_get_underlying_type (sig->ret);
256 switch (ret_type->type) {
257 case MONO_TYPE_BOOLEAN:
268 case MONO_TYPE_FNPTR:
269 case MONO_TYPE_CLASS:
270 case MONO_TYPE_OBJECT:
271 case MONO_TYPE_SZARRAY:
272 case MONO_TYPE_ARRAY:
273 case MONO_TYPE_STRING:
274 cinfo->ret.storage = ArgInIReg;
275 cinfo->ret.reg = X86_EAX;
279 cinfo->ret.storage = ArgInIReg;
280 cinfo->ret.reg = X86_EAX;
283 cinfo->ret.storage = ArgOnFloatFpStack;
286 cinfo->ret.storage = ArgOnDoubleFpStack;
288 case MONO_TYPE_GENERICINST:
289 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
290 cinfo->ret.storage = ArgInIReg;
291 cinfo->ret.reg = X86_EAX;
295 case MONO_TYPE_VALUETYPE: {
296 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
298 add_valuetype (sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
299 if (cinfo->ret.storage == ArgOnStack)
300 /* The caller passes the address where the value is stored */
301 add_general (&gr, &stack_size, &cinfo->ret);
304 case MONO_TYPE_TYPEDBYREF:
305 /* Same as a valuetype with size 24 */
306 add_general (&gr, &stack_size, &cinfo->ret);
310 cinfo->ret.storage = ArgNone;
313 g_error ("Can't handle as return value 0x%x", sig->ret->type);
319 add_general (&gr, &stack_size, cinfo->args + 0);
321 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
323 fr = FLOAT_PARAM_REGS;
325 /* Emit the signature cookie just before the implicit arguments */
326 add_general (&gr, &stack_size, &cinfo->sig_cookie);
329 for (i = 0; i < sig->param_count; ++i) {
330 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
333 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
334 /* We allways pass the sig cookie on the stack for simplicity */
336 * Prevent implicit arguments + the sig cookie from being passed
340 fr = FLOAT_PARAM_REGS;
342 /* Emit the signature cookie just before the implicit arguments */
343 add_general (&gr, &stack_size, &cinfo->sig_cookie);
346 if (sig->params [i]->byref) {
347 add_general (&gr, &stack_size, ainfo);
350 ptype = mono_type_get_underlying_type (sig->params [i]);
351 switch (ptype->type) {
352 case MONO_TYPE_BOOLEAN:
355 add_general (&gr, &stack_size, ainfo);
360 add_general (&gr, &stack_size, ainfo);
364 add_general (&gr, &stack_size, ainfo);
369 case MONO_TYPE_FNPTR:
370 case MONO_TYPE_CLASS:
371 case MONO_TYPE_OBJECT:
372 case MONO_TYPE_STRING:
373 case MONO_TYPE_SZARRAY:
374 case MONO_TYPE_ARRAY:
375 add_general (&gr, &stack_size, ainfo);
377 case MONO_TYPE_GENERICINST:
378 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
379 add_general (&gr, &stack_size, ainfo);
383 case MONO_TYPE_VALUETYPE:
384 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
386 case MONO_TYPE_TYPEDBYREF:
387 stack_size += sizeof (MonoTypedRef);
388 ainfo->storage = ArgOnStack;
392 add_general_pair (&gr, &stack_size, ainfo);
395 add_float (&fr, &stack_size, ainfo, FALSE);
398 add_float (&fr, &stack_size, ainfo, TRUE);
401 g_error ("unexpected type 0x%x", ptype->type);
402 g_assert_not_reached ();
406 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
408 fr = FLOAT_PARAM_REGS;
410 /* Emit the signature cookie just before the implicit arguments */
411 add_general (&gr, &stack_size, &cinfo->sig_cookie);
414 #if defined(__APPLE__)
415 if ((stack_size % 16) != 0) {
416 cinfo->need_stack_align = TRUE;
417 stack_size += cinfo->stack_align_amount = 16-(stack_size % 16);
421 cinfo->stack_usage = stack_size;
422 cinfo->reg_usage = gr;
423 cinfo->freg_usage = fr;
428 * mono_arch_get_argument_info:
429 * @csig: a method signature
430 * @param_count: the number of parameters to consider
431 * @arg_info: an array to store the result infos
433 * Gathers information on parameters such as size, alignment and
434 * padding. arg_info should be large enought to hold param_count + 1 entries.
436 * Returns the size of the activation frame.
439 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
441 int k, frame_size = 0;
447 cinfo = get_call_info (NULL, csig, FALSE);
449 if (MONO_TYPE_ISSTRUCT (csig->ret) && (cinfo->ret.storage == ArgOnStack)) {
450 frame_size += sizeof (gpointer);
454 arg_info [0].offset = offset;
457 frame_size += sizeof (gpointer);
461 arg_info [0].size = frame_size;
463 for (k = 0; k < param_count; k++) {
466 size = mono_type_native_stack_size (csig->params [k], &align);
469 size = mono_type_stack_size (csig->params [k], &ialign);
473 /* ignore alignment for now */
476 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
477 arg_info [k].pad = pad;
479 arg_info [k + 1].pad = 0;
480 arg_info [k + 1].size = size;
482 arg_info [k + 1].offset = offset;
486 align = MONO_ARCH_FRAME_ALIGNMENT;
487 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
488 arg_info [k].pad = pad;
495 static const guchar cpuid_impl [] = {
496 0x55, /* push %ebp */
497 0x89, 0xe5, /* mov %esp,%ebp */
498 0x53, /* push %ebx */
499 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
500 0x0f, 0xa2, /* cpuid */
501 0x50, /* push %eax */
502 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
503 0x89, 0x18, /* mov %ebx,(%eax) */
504 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
505 0x89, 0x08, /* mov %ecx,(%eax) */
506 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
507 0x89, 0x10, /* mov %edx,(%eax) */
509 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
510 0x89, 0x02, /* mov %eax,(%edx) */
516 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
519 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
523 __asm__ __volatile__ (
526 "movl %%eax, %%edx\n"
527 "xorl $0x200000, %%eax\n"
532 "xorl %%edx, %%eax\n"
533 "andl $0x200000, %%eax\n"
555 /* Have to use the code manager to get around WinXP DEP */
556 static CpuidFunc func = NULL;
559 ptr = mono_global_codeman_reserve (sizeof (cpuid_impl));
560 memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
561 func = (CpuidFunc)ptr;
563 func (id, p_eax, p_ebx, p_ecx, p_edx);
566 * We use this approach because of issues with gcc and pic code, see:
567 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
568 __asm__ __volatile__ ("cpuid"
569 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
578 * Initialize the cpu to execute managed code.
581 mono_arch_cpu_init (void)
583 /* spec compliance requires running with double precision */
587 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
588 fpcw &= ~X86_FPCW_PRECC_MASK;
589 fpcw |= X86_FPCW_PREC_DOUBLE;
590 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
591 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
593 _control87 (_PC_53, MCW_PC);
598 * This function returns the optimizations supported on this cpu.
601 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
603 int eax, ebx, ecx, edx;
607 /* Feature Flags function, flags returned in EDX. */
608 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
609 if (edx & (1 << 15)) {
610 opts |= MONO_OPT_CMOV;
612 opts |= MONO_OPT_FCMOV;
614 *exclude_mask |= MONO_OPT_FCMOV;
616 *exclude_mask |= MONO_OPT_CMOV;
618 opts |= MONO_OPT_SSE2;
620 *exclude_mask |= MONO_OPT_SSE2;
626 * Determine whenever the trap whose info is in SIGINFO is caused by
630 mono_arch_is_int_overflow (void *sigctx, void *info)
635 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
637 ip = (guint8*)ctx.eip;
639 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
643 switch (x86_modrm_rm (ip [1])) {
663 g_assert_not_reached ();
675 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
680 for (i = 0; i < cfg->num_varinfo; i++) {
681 MonoInst *ins = cfg->varinfo [i];
682 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
685 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
688 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
689 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
692 /* we dont allocate I1 to registers because there is no simply way to sign extend
693 * 8bit quantities in caller saved registers on x86 */
694 if (mono_is_regsize_var (ins->inst_vtype) && (ins->inst_vtype->type != MONO_TYPE_I1)) {
695 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
696 g_assert (i == vmv->idx);
697 vars = g_list_prepend (vars, vmv);
701 vars = mono_varlist_sort (cfg, vars, 0);
707 mono_arch_get_global_int_regs (MonoCompile *cfg)
711 /* we can use 3 registers for global allocation */
712 regs = g_list_prepend (regs, (gpointer)X86_EBX);
713 regs = g_list_prepend (regs, (gpointer)X86_ESI);
714 regs = g_list_prepend (regs, (gpointer)X86_EDI);
720 * mono_arch_regalloc_cost:
722 * Return the cost, in number of memory references, of the action of
723 * allocating the variable VMV into a register during global register
727 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
729 MonoInst *ins = cfg->varinfo [vmv->idx];
731 if (cfg->method->save_lmf)
732 /* The register is already saved */
733 return (ins->opcode == OP_ARG) ? 1 : 0;
735 /* push+pop+possible load if it is an argument */
736 return (ins->opcode == OP_ARG) ? 3 : 2;
740 * Set var information according to the calling convention. X86 version.
741 * The locals var stuff should most likely be split in another method.
744 mono_arch_allocate_vars (MonoCompile *cfg)
746 MonoMethodSignature *sig;
747 MonoMethodHeader *header;
749 guint32 locals_stack_size, locals_stack_align;
754 header = mono_method_get_header (cfg->method);
755 sig = mono_method_signature (cfg->method);
757 cinfo = get_call_info (cfg->mempool, sig, FALSE);
759 cfg->frame_reg = MONO_ARCH_BASEREG;
762 /* Reserve space to save LMF and caller saved registers */
764 if (cfg->method->save_lmf) {
765 offset += sizeof (MonoLMF);
767 if (cfg->used_int_regs & (1 << X86_EBX)) {
771 if (cfg->used_int_regs & (1 << X86_EDI)) {
775 if (cfg->used_int_regs & (1 << X86_ESI)) {
780 switch (cinfo->ret.storage) {
781 case ArgValuetypeInReg:
782 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
784 cfg->ret->opcode = OP_REGOFFSET;
785 cfg->ret->inst_basereg = X86_EBP;
786 cfg->ret->inst_offset = - offset;
792 /* Allocate locals */
793 offsets = mono_allocate_stack_slots (cfg, &locals_stack_size, &locals_stack_align);
794 if (locals_stack_align) {
795 offset += (locals_stack_align - 1);
796 offset &= ~(locals_stack_align - 1);
798 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
799 if (offsets [i] != -1) {
800 MonoInst *inst = cfg->varinfo [i];
801 inst->opcode = OP_REGOFFSET;
802 inst->inst_basereg = X86_EBP;
803 inst->inst_offset = - (offset + offsets [i]);
804 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
807 offset += locals_stack_size;
811 * Allocate arguments+return value
814 switch (cinfo->ret.storage) {
816 cfg->ret->opcode = OP_REGOFFSET;
817 cfg->ret->inst_basereg = X86_EBP;
818 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
820 case ArgValuetypeInReg:
823 cfg->ret->opcode = OP_REGVAR;
824 cfg->ret->inst_c0 = cinfo->ret.reg;
827 case ArgOnFloatFpStack:
828 case ArgOnDoubleFpStack:
831 g_assert_not_reached ();
834 if (sig->call_convention == MONO_CALL_VARARG) {
835 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
836 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
839 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
840 ArgInfo *ainfo = &cinfo->args [i];
841 inst = cfg->args [i];
842 if (inst->opcode != OP_REGVAR) {
843 inst->opcode = OP_REGOFFSET;
844 inst->inst_basereg = X86_EBP;
846 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
849 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
850 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
852 cfg->stack_offset = offset;
856 mono_arch_create_vars (MonoCompile *cfg)
858 MonoMethodSignature *sig;
861 sig = mono_method_signature (cfg->method);
863 cinfo = get_call_info (cfg->mempool, sig, FALSE);
865 if (cinfo->ret.storage == ArgValuetypeInReg)
866 cfg->ret_var_is_local = TRUE;
869 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
870 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
874 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call)
877 MonoMethodSignature *tmp_sig;
880 /* FIXME: Add support for signature tokens to AOT */
881 cfg->disable_aot = TRUE;
882 MONO_INST_NEW (cfg, arg, OP_OUTARG);
885 * mono_ArgIterator_Setup assumes the signature cookie is
886 * passed first and all the arguments which were before it are
887 * passed on the stack after the signature. So compensate by
888 * passing a different signature.
890 tmp_sig = mono_metadata_signature_dup (call->signature);
891 tmp_sig->param_count -= call->signature->sentinelpos;
892 tmp_sig->sentinelpos = 0;
893 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
895 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
896 sig_arg->inst_p0 = tmp_sig;
898 arg->inst_left = sig_arg;
899 arg->type = STACK_PTR;
900 /* prepend, so they get reversed */
901 arg->next = call->out_args;
902 call->out_args = arg;
906 * take the arguments and generate the arch-specific
907 * instructions to properly call the function in call.
908 * This includes pushing, moving arguments to the right register
912 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
914 MonoMethodSignature *sig;
919 sig = call->signature;
920 n = sig->param_count + sig->hasthis;
922 cinfo = get_call_info (cfg->mempool, sig, FALSE);
924 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
925 sentinelpos = sig->sentinelpos + (is_virtual ? 1 : 0);
927 for (i = 0; i < n; ++i) {
928 ArgInfo *ainfo = cinfo->args + i;
930 /* Emit the signature cookie just before the implicit arguments */
931 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
932 emit_sig_cookie (cfg, call);
935 if (is_virtual && i == 0) {
936 /* the argument will be attached to the call instrucion */
941 if (i >= sig->hasthis)
942 t = sig->params [i - sig->hasthis];
944 t = &mono_defaults.int_class->byval_arg;
945 t = mono_type_get_underlying_type (t);
947 MONO_INST_NEW (cfg, arg, OP_OUTARG);
949 arg->cil_code = in->cil_code;
951 arg->type = in->type;
952 /* prepend, so they get reversed */
953 arg->next = call->out_args;
954 call->out_args = arg;
956 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
959 if (t->type == MONO_TYPE_TYPEDBYREF) {
960 size = sizeof (MonoTypedRef);
961 align = sizeof (gpointer);
965 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
968 size = mono_type_stack_size (&in->klass->byval_arg, &ialign);
971 arg->opcode = OP_OUTARG_VT;
972 arg->klass = in->klass;
973 arg->backend.is_pinvoke = sig->pinvoke;
974 arg->inst_imm = size;
977 switch (ainfo->storage) {
979 arg->opcode = OP_OUTARG;
981 if (t->type == MONO_TYPE_R4)
982 arg->opcode = OP_OUTARG_R4;
984 if (t->type == MONO_TYPE_R8)
985 arg->opcode = OP_OUTARG_R8;
989 g_assert_not_reached ();
995 /* Handle the case where there are no implicit arguments */
996 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
997 emit_sig_cookie (cfg, call);
1000 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1001 if (cinfo->ret.storage == ArgValuetypeInReg) {
1002 MonoInst *zero_inst;
1004 * After the call, the struct is in registers, but needs to be saved to the memory pointed
1005 * to by vt_arg in this_vret_args. This means that vt_arg needs to be saved somewhere
1006 * before calling the function. So we add a dummy instruction to represent pushing the
1007 * struct return address to the stack. The return address will be saved to this stack slot
1008 * by the code emitted in this_vret_args.
1010 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1011 MONO_INST_NEW (cfg, zero_inst, OP_ICONST);
1012 zero_inst->inst_p0 = 0;
1013 arg->inst_left = zero_inst;
1014 arg->type = STACK_PTR;
1015 /* prepend, so they get reversed */
1016 arg->next = call->out_args;
1017 call->out_args = arg;
1020 /* if the function returns a struct, the called method already does a ret $0x4 */
1021 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
1022 cinfo->stack_usage -= 4;
1025 call->stack_usage = cinfo->stack_usage;
1027 #if defined(__APPLE__)
1028 if (cinfo->need_stack_align) {
1029 MONO_INST_NEW (cfg, arg, OP_X86_OUTARG_ALIGN_STACK);
1030 arg->inst_c0 = cinfo->stack_align_amount;
1031 arg->next = call->out_args;
1032 call->out_args = arg;
1040 * Allow tracing to work with this interface (with an optional argument)
1043 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1048 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1051 /* if some args are passed in registers, we need to save them here */
1052 x86_push_reg (code, X86_EBP);
1054 if (cfg->compile_aot) {
1055 x86_push_imm (code, cfg->method);
1056 x86_mov_reg_imm (code, X86_EAX, func);
1057 x86_call_reg (code, X86_EAX);
1059 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1060 x86_push_imm (code, cfg->method);
1061 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1062 x86_call_code (code, 0);
1065 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 16);
1067 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1082 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1085 int arg_size = 0, save_mode = SAVE_NONE;
1086 MonoMethod *method = cfg->method;
1088 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
1089 case MONO_TYPE_VOID:
1090 /* special case string .ctor icall */
1091 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1092 save_mode = SAVE_EAX;
1094 save_mode = SAVE_NONE;
1098 save_mode = SAVE_EAX_EDX;
1102 save_mode = SAVE_FP;
1104 case MONO_TYPE_GENERICINST:
1105 if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method)->ret)) {
1106 save_mode = SAVE_EAX;
1110 case MONO_TYPE_VALUETYPE:
1111 save_mode = SAVE_STRUCT;
1114 save_mode = SAVE_EAX;
1118 switch (save_mode) {
1120 x86_push_reg (code, X86_EDX);
1121 x86_push_reg (code, X86_EAX);
1122 if (enable_arguments) {
1123 x86_push_reg (code, X86_EDX);
1124 x86_push_reg (code, X86_EAX);
1129 x86_push_reg (code, X86_EAX);
1130 if (enable_arguments) {
1131 x86_push_reg (code, X86_EAX);
1136 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1137 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1138 if (enable_arguments) {
1139 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1140 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1145 if (enable_arguments) {
1146 x86_push_membase (code, X86_EBP, 8);
1155 if (cfg->compile_aot) {
1156 x86_push_imm (code, method);
1157 x86_mov_reg_imm (code, X86_EAX, func);
1158 x86_call_reg (code, X86_EAX);
1160 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1161 x86_push_imm (code, method);
1162 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1163 x86_call_code (code, 0);
1165 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1167 switch (save_mode) {
1169 x86_pop_reg (code, X86_EAX);
1170 x86_pop_reg (code, X86_EDX);
1173 x86_pop_reg (code, X86_EAX);
1176 x86_fld_membase (code, X86_ESP, 0, TRUE);
1177 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1187 #define EMIT_COND_BRANCH(ins,cond,sign) \
1188 if (ins->flags & MONO_INST_BRLABEL) { \
1189 if (ins->inst_i0->inst_c0) { \
1190 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
1192 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1193 if ((cfg->opt & MONO_OPT_BRANCH) && \
1194 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
1195 x86_branch8 (code, cond, 0, sign); \
1197 x86_branch32 (code, cond, 0, sign); \
1200 if (ins->inst_true_bb->native_offset) { \
1201 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1203 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1204 if ((cfg->opt & MONO_OPT_BRANCH) && \
1205 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1206 x86_branch8 (code, cond, 0, sign); \
1208 x86_branch32 (code, cond, 0, sign); \
1213 * Emit an exception if condition is fail and
1214 * if possible do a directly branch to target
1216 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1218 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1219 if (tins == NULL) { \
1220 mono_add_patch_info (cfg, code - cfg->native_code, \
1221 MONO_PATCH_INFO_EXC, exc_name); \
1222 x86_branch32 (code, cond, 0, signed); \
1224 EMIT_COND_BRANCH (tins, cond, signed); \
1228 #define EMIT_FPCOMPARE(code) do { \
1229 x86_fcompp (code); \
1230 x86_fnstsw (code); \
1235 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1237 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1238 x86_call_code (code, 0);
1243 #define INST_IGNORES_CFLAGS(opcode) (!(((opcode) == OP_ADC) || ((opcode) == OP_IADC) || ((opcode) == OP_ADC_IMM) || ((opcode) == OP_IADC_IMM) || ((opcode) == OP_SBB) || ((opcode) == OP_ISBB) || ((opcode) == OP_SBB_IMM) || ((opcode) == OP_ISBB_IMM)))
1248 * Perform peephole opts which should/can be performed before local regalloc
1251 peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1253 MonoInst *ins, *last_ins = NULL;
1257 switch (ins->opcode) {
1260 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1262 * X86_LEA is like ADD, but doesn't have the
1263 * sreg1==dreg restriction.
1265 ins->opcode = OP_X86_LEA_MEMBASE;
1266 ins->inst_basereg = ins->sreg1;
1267 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1268 ins->opcode = OP_X86_INC_REG;
1272 if ((ins->sreg1 < MONO_MAX_IREGS) && (ins->dreg >= MONO_MAX_IREGS)) {
1273 ins->opcode = OP_X86_LEA_MEMBASE;
1274 ins->inst_basereg = ins->sreg1;
1275 ins->inst_imm = -ins->inst_imm;
1276 } else if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1277 ins->opcode = OP_X86_DEC_REG;
1279 case OP_COMPARE_IMM:
1280 case OP_ICOMPARE_IMM:
1281 /* OP_COMPARE_IMM (reg, 0)
1283 * OP_X86_TEST_NULL (reg)
1286 ins->opcode = OP_X86_TEST_NULL;
1288 case OP_X86_COMPARE_MEMBASE_IMM:
1290 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1291 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1293 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1294 * OP_COMPARE_IMM reg, imm
1296 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1298 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1299 ins->inst_basereg == last_ins->inst_destbasereg &&
1300 ins->inst_offset == last_ins->inst_offset) {
1301 ins->opcode = OP_COMPARE_IMM;
1302 ins->sreg1 = last_ins->sreg1;
1304 /* check if we can remove cmp reg,0 with test null */
1306 ins->opcode = OP_X86_TEST_NULL;
1310 case OP_LOAD_MEMBASE:
1311 case OP_LOADI4_MEMBASE:
1313 * Note: if reg1 = reg2 the load op is removed
1315 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1316 * OP_LOAD_MEMBASE offset(basereg), reg2
1318 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1319 * OP_MOVE reg1, reg2
1321 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1322 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1323 ins->inst_basereg == last_ins->inst_destbasereg &&
1324 ins->inst_offset == last_ins->inst_offset) {
1325 if (ins->dreg == last_ins->sreg1) {
1326 last_ins->next = ins->next;
1330 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1331 ins->opcode = OP_MOVE;
1332 ins->sreg1 = last_ins->sreg1;
1336 * Note: reg1 must be different from the basereg in the second load
1337 * Note: if reg1 = reg2 is equal then second load is removed
1339 * OP_LOAD_MEMBASE offset(basereg), reg1
1340 * OP_LOAD_MEMBASE offset(basereg), reg2
1342 * OP_LOAD_MEMBASE offset(basereg), reg1
1343 * OP_MOVE reg1, reg2
1345 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1346 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1347 ins->inst_basereg != last_ins->dreg &&
1348 ins->inst_basereg == last_ins->inst_basereg &&
1349 ins->inst_offset == last_ins->inst_offset) {
1351 if (ins->dreg == last_ins->dreg) {
1352 last_ins->next = ins->next;
1356 ins->opcode = OP_MOVE;
1357 ins->sreg1 = last_ins->dreg;
1360 //g_assert_not_reached ();
1364 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1365 * OP_LOAD_MEMBASE offset(basereg), reg
1367 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1368 * OP_ICONST reg, imm
1370 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1371 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1372 ins->inst_basereg == last_ins->inst_destbasereg &&
1373 ins->inst_offset == last_ins->inst_offset) {
1374 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1375 ins->opcode = OP_ICONST;
1376 ins->inst_c0 = last_ins->inst_imm;
1377 g_assert_not_reached (); // check this rule
1381 case OP_LOADU1_MEMBASE:
1382 case OP_LOADI1_MEMBASE:
1384 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1385 * OP_LOAD_MEMBASE offset(basereg), reg2
1387 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1388 * CONV_I2/U2 reg1, reg2
1390 if (last_ins && X86_IS_BYTE_REG (last_ins->sreg1) &&
1391 (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1392 ins->inst_basereg == last_ins->inst_destbasereg &&
1393 ins->inst_offset == last_ins->inst_offset) {
1394 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? CEE_CONV_I1 : CEE_CONV_U1;
1395 ins->sreg1 = last_ins->sreg1;
1398 case OP_LOADU2_MEMBASE:
1399 case OP_LOADI2_MEMBASE:
1401 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1402 * OP_LOAD_MEMBASE offset(basereg), reg2
1404 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1405 * CONV_I2/U2 reg1, reg2
1407 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1408 ins->inst_basereg == last_ins->inst_destbasereg &&
1409 ins->inst_offset == last_ins->inst_offset) {
1410 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? CEE_CONV_I2 : CEE_CONV_U2;
1411 ins->sreg1 = last_ins->sreg1;
1416 case OP_ICONV_TO_I4:
1423 if (ins->dreg == ins->sreg1) {
1425 last_ins->next = ins->next;
1432 * OP_MOVE sreg, dreg
1433 * OP_MOVE dreg, sreg
1435 if (last_ins && last_ins->opcode == OP_MOVE &&
1436 ins->sreg1 == last_ins->dreg &&
1437 ins->dreg == last_ins->sreg1) {
1438 last_ins->next = ins->next;
1444 case OP_X86_PUSH_MEMBASE:
1445 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1446 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1447 ins->inst_basereg == last_ins->inst_destbasereg &&
1448 ins->inst_offset == last_ins->inst_offset) {
1449 ins->opcode = OP_X86_PUSH;
1450 ins->sreg1 = last_ins->sreg1;
1457 bb->last_ins = last_ins;
1461 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1463 MonoInst *ins, *last_ins = NULL;
1468 switch (ins->opcode) {
1470 /* reg = 0 -> XOR (reg, reg) */
1471 /* XOR sets cflags on x86, so we cant do it always */
1472 if (ins->inst_c0 == 0 && (!ins->next || (ins->next && INST_IGNORES_CFLAGS (ins->next->opcode)))) {
1475 ins->opcode = OP_IXOR;
1476 ins->sreg1 = ins->dreg;
1477 ins->sreg2 = ins->dreg;
1480 * Convert succeeding STORE_MEMBASE_IMM 0 ins to STORE_MEMBASE_REG
1481 * since it takes 3 bytes instead of 7.
1483 for (ins2 = ins->next; ins2; ins2 = ins2->next) {
1484 if ((ins2->opcode == OP_STORE_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1485 ins2->opcode = OP_STORE_MEMBASE_REG;
1486 ins2->sreg1 = ins->dreg;
1488 else if ((ins2->opcode == OP_STOREI4_MEMBASE_IMM) && (ins2->inst_imm == 0)) {
1489 ins2->opcode = OP_STOREI4_MEMBASE_REG;
1490 ins2->sreg1 = ins->dreg;
1492 else if ((ins2->opcode == OP_STOREI1_MEMBASE_IMM) || (ins2->opcode == OP_STOREI2_MEMBASE_IMM)) {
1493 /* Continue iteration */
1502 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1503 ins->opcode = OP_X86_INC_REG;
1507 if ((ins->inst_imm == 1) && (ins->dreg == ins->sreg1))
1508 ins->opcode = OP_X86_DEC_REG;
1510 case OP_X86_COMPARE_MEMBASE_IMM:
1512 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1513 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1515 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1516 * OP_COMPARE_IMM reg, imm
1518 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1520 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1521 ins->inst_basereg == last_ins->inst_destbasereg &&
1522 ins->inst_offset == last_ins->inst_offset) {
1523 ins->opcode = OP_COMPARE_IMM;
1524 ins->sreg1 = last_ins->sreg1;
1526 /* check if we can remove cmp reg,0 with test null */
1528 ins->opcode = OP_X86_TEST_NULL;
1532 case OP_LOAD_MEMBASE:
1533 case OP_LOADI4_MEMBASE:
1535 * Note: if reg1 = reg2 the load op is removed
1537 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1538 * OP_LOAD_MEMBASE offset(basereg), reg2
1540 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1541 * OP_MOVE reg1, reg2
1543 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1544 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1545 ins->inst_basereg == last_ins->inst_destbasereg &&
1546 ins->inst_offset == last_ins->inst_offset) {
1547 if (ins->dreg == last_ins->sreg1) {
1548 last_ins->next = ins->next;
1552 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1553 ins->opcode = OP_MOVE;
1554 ins->sreg1 = last_ins->sreg1;
1558 * Note: reg1 must be different from the basereg in the second load
1559 * Note: if reg1 = reg2 is equal then second load is removed
1561 * OP_LOAD_MEMBASE offset(basereg), reg1
1562 * OP_LOAD_MEMBASE offset(basereg), reg2
1564 * OP_LOAD_MEMBASE offset(basereg), reg1
1565 * OP_MOVE reg1, reg2
1567 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1568 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1569 ins->inst_basereg != last_ins->dreg &&
1570 ins->inst_basereg == last_ins->inst_basereg &&
1571 ins->inst_offset == last_ins->inst_offset) {
1573 if (ins->dreg == last_ins->dreg) {
1574 last_ins->next = ins->next;
1578 ins->opcode = OP_MOVE;
1579 ins->sreg1 = last_ins->dreg;
1582 //g_assert_not_reached ();
1586 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1587 * OP_LOAD_MEMBASE offset(basereg), reg
1589 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1590 * OP_ICONST reg, imm
1592 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1593 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1594 ins->inst_basereg == last_ins->inst_destbasereg &&
1595 ins->inst_offset == last_ins->inst_offset) {
1596 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1597 ins->opcode = OP_ICONST;
1598 ins->inst_c0 = last_ins->inst_imm;
1599 g_assert_not_reached (); // check this rule
1603 case OP_LOADU1_MEMBASE:
1604 case OP_LOADI1_MEMBASE:
1606 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1607 * OP_LOAD_MEMBASE offset(basereg), reg2
1609 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1610 * CONV_I2/U2 reg1, reg2
1612 if (last_ins && X86_IS_BYTE_REG (last_ins->sreg1) &&
1613 (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1614 ins->inst_basereg == last_ins->inst_destbasereg &&
1615 ins->inst_offset == last_ins->inst_offset) {
1616 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? CEE_CONV_I1 : CEE_CONV_U1;
1617 ins->sreg1 = last_ins->sreg1;
1620 case OP_LOADU2_MEMBASE:
1621 case OP_LOADI2_MEMBASE:
1623 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1624 * OP_LOAD_MEMBASE offset(basereg), reg2
1626 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1627 * CONV_I2/U2 reg1, reg2
1629 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1630 ins->inst_basereg == last_ins->inst_destbasereg &&
1631 ins->inst_offset == last_ins->inst_offset) {
1632 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? CEE_CONV_I2 : CEE_CONV_U2;
1633 ins->sreg1 = last_ins->sreg1;
1638 case OP_ICONV_TO_I4:
1645 if (ins->dreg == ins->sreg1) {
1647 last_ins->next = ins->next;
1654 * OP_MOVE sreg, dreg
1655 * OP_MOVE dreg, sreg
1657 if (last_ins && last_ins->opcode == OP_MOVE &&
1658 ins->sreg1 == last_ins->dreg &&
1659 ins->dreg == last_ins->sreg1) {
1660 last_ins->next = ins->next;
1665 case OP_X86_PUSH_MEMBASE:
1666 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1667 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1668 ins->inst_basereg == last_ins->inst_destbasereg &&
1669 ins->inst_offset == last_ins->inst_offset) {
1670 ins->opcode = OP_X86_PUSH;
1671 ins->sreg1 = last_ins->sreg1;
1678 bb->last_ins = last_ins;
1682 branch_cc_table [] = {
1683 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1684 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1685 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1688 /* Maps CMP_... constants to X86_CC_... constants */
1691 X86_CC_EQ, X86_CC_NE, X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT,
1692 X86_CC_LE, X86_CC_GE, X86_CC_LT, X86_CC_GT
1696 cc_signed_table [] = {
1697 TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1698 FALSE, FALSE, FALSE, FALSE
1702 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1704 if (cfg->opt & MONO_OPT_PEEPHOLE)
1705 peephole_pass_1 (cfg, bb);
1707 mono_local_regalloc (cfg, bb);
1710 static unsigned char*
1711 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1713 #define XMM_TEMP_REG 0
1714 if (cfg->opt & MONO_OPT_SSE2 && size < 8) {
1715 /* optimize by assigning a local var for this use so we avoid
1716 * the stack manipulations */
1717 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1718 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1719 x86_movsd_reg_membase (code, XMM_TEMP_REG, X86_ESP, 0);
1720 x86_cvttsd2si (code, dreg, XMM_TEMP_REG);
1721 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1723 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1725 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1728 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1729 x86_fnstcw_membase(code, X86_ESP, 0);
1730 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1731 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1732 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1733 x86_fldcw_membase (code, X86_ESP, 2);
1735 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1736 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1737 x86_pop_reg (code, dreg);
1738 /* FIXME: need the high register
1739 * x86_pop_reg (code, dreg_high);
1742 x86_push_reg (code, X86_EAX); // SP = SP - 4
1743 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1744 x86_pop_reg (code, dreg);
1746 x86_fldcw_membase (code, X86_ESP, 0);
1747 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1750 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1752 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1756 static unsigned char*
1757 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1759 int sreg = tree->sreg1;
1760 int need_touch = FALSE;
1762 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
1771 * If requested stack size is larger than one page,
1772 * perform stack-touch operation
1775 * Generate stack probe code.
1776 * Under Windows, it is necessary to allocate one page at a time,
1777 * "touching" stack after each successful sub-allocation. This is
1778 * because of the way stack growth is implemented - there is a
1779 * guard page before the lowest stack page that is currently commited.
1780 * Stack normally grows sequentially so OS traps access to the
1781 * guard page and commits more pages when needed.
1783 x86_test_reg_imm (code, sreg, ~0xFFF);
1784 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1786 br[2] = code; /* loop */
1787 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
1788 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
1791 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
1792 * that follows only initializes the last part of the area.
1794 /* Same as the init code below with size==0x1000 */
1795 if (tree->flags & MONO_INST_INIT) {
1796 x86_push_reg (code, X86_EAX);
1797 x86_push_reg (code, X86_ECX);
1798 x86_push_reg (code, X86_EDI);
1799 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
1800 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1801 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
1803 x86_prefix (code, X86_REP_PREFIX);
1805 x86_pop_reg (code, X86_EDI);
1806 x86_pop_reg (code, X86_ECX);
1807 x86_pop_reg (code, X86_EAX);
1810 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
1811 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
1812 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
1813 x86_patch (br[3], br[2]);
1814 x86_test_reg_reg (code, sreg, sreg);
1815 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1816 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1818 br[1] = code; x86_jump8 (code, 0);
1820 x86_patch (br[0], code);
1821 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1822 x86_patch (br[1], code);
1823 x86_patch (br[4], code);
1826 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1828 if (tree->flags & MONO_INST_INIT) {
1830 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1831 x86_push_reg (code, X86_EAX);
1834 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1835 x86_push_reg (code, X86_ECX);
1838 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1839 x86_push_reg (code, X86_EDI);
1843 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1844 if (sreg != X86_ECX)
1845 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1846 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1848 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1850 x86_prefix (code, X86_REP_PREFIX);
1853 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1854 x86_pop_reg (code, X86_EDI);
1855 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1856 x86_pop_reg (code, X86_ECX);
1857 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1858 x86_pop_reg (code, X86_EAX);
1865 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
1870 /* Move return value to the target register */
1871 switch (ins->opcode) {
1874 case OP_CALL_MEMBASE:
1875 if (ins->dreg != X86_EAX)
1876 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
1880 case OP_VCALL_MEMBASE:
1881 cinfo = get_call_info (cfg->mempool, ((MonoCallInst*)ins)->signature, FALSE);
1882 if (cinfo->ret.storage == ArgValuetypeInReg) {
1883 /* Pop the destination address from the stack */
1884 x86_pop_reg (code, X86_ECX);
1886 for (quad = 0; quad < 2; quad ++) {
1887 switch (cinfo->ret.pair_storage [quad]) {
1889 g_assert (cinfo->ret.pair_regs [quad] != X86_ECX);
1890 x86_mov_membase_reg (code, X86_ECX, (quad * sizeof (gpointer)), cinfo->ret.pair_regs [quad], sizeof (gpointer));
1895 g_assert_not_reached ();
1908 * @code: buffer to store code to
1909 * @dreg: hard register where to place the result
1910 * @tls_offset: offset info
1912 * emit_tls_get emits in @code the native code that puts in the dreg register
1913 * the item in the thread local storage identified by tls_offset.
1915 * Returns: a pointer to the end of the stored code
1918 emit_tls_get (guint8* code, int dreg, int tls_offset)
1920 #ifdef PLATFORM_WIN32
1922 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
1923 * Journal and/or a disassembly of the TlsGet () function.
1925 g_assert (tls_offset < 64);
1926 x86_prefix (code, X86_FS_PREFIX);
1927 x86_mov_reg_mem (code, dreg, 0x18, 4);
1928 /* Dunno what this does but TlsGetValue () contains it */
1929 x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
1930 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
1932 if (optimize_for_xen) {
1933 x86_prefix (code, X86_GS_PREFIX);
1934 x86_mov_reg_mem (code, dreg, 0, 4);
1935 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
1937 x86_prefix (code, X86_GS_PREFIX);
1938 x86_mov_reg_mem (code, dreg, tls_offset, 4);
1945 * emit_load_volatile_arguments:
1947 * Load volatile arguments from the stack to the original input registers.
1948 * Required before a tail call.
1951 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
1953 MonoMethod *method = cfg->method;
1954 MonoMethodSignature *sig;
1959 /* FIXME: Generate intermediate code instead */
1961 sig = mono_method_signature (method);
1963 cinfo = get_call_info (cfg->mempool, sig, FALSE);
1965 /* This is the opposite of the code in emit_prolog */
1967 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1968 ArgInfo *ainfo = cinfo->args + i;
1970 inst = cfg->args [i];
1972 if (sig->hasthis && (i == 0))
1973 arg_type = &mono_defaults.object_class->byval_arg;
1975 arg_type = sig->params [i - sig->hasthis];
1978 * On x86, the arguments are either in their original stack locations, or in
1981 if (inst->opcode == OP_REGVAR) {
1982 g_assert (ainfo->storage == ArgOnStack);
1984 x86_mov_membase_reg (code, X86_EBP, inst->inst_offset, inst->dreg, 4);
1991 #define REAL_PRINT_REG(text,reg) \
1992 mono_assert (reg >= 0); \
1993 x86_push_reg (code, X86_EAX); \
1994 x86_push_reg (code, X86_EDX); \
1995 x86_push_reg (code, X86_ECX); \
1996 x86_push_reg (code, reg); \
1997 x86_push_imm (code, reg); \
1998 x86_push_imm (code, text " %d %p\n"); \
1999 x86_mov_reg_imm (code, X86_EAX, printf); \
2000 x86_call_reg (code, X86_EAX); \
2001 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
2002 x86_pop_reg (code, X86_ECX); \
2003 x86_pop_reg (code, X86_EDX); \
2004 x86_pop_reg (code, X86_EAX);
2006 /* benchmark and set based on cpu */
2007 #define LOOP_ALIGNMENT 8
2008 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
2011 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2016 guint8 *code = cfg->native_code + cfg->code_len;
2017 MonoInst *last_ins = NULL;
2018 guint last_offset = 0;
2021 if (cfg->opt & MONO_OPT_PEEPHOLE)
2022 peephole_pass (cfg, bb);
2024 if (cfg->opt & MONO_OPT_LOOP) {
2025 int pad, align = LOOP_ALIGNMENT;
2026 /* set alignment depending on cpu */
2027 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
2029 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
2030 x86_padding (code, pad);
2031 cfg->code_len += pad;
2032 bb->native_offset = cfg->code_len;
2036 if (cfg->verbose_level > 2)
2037 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2039 cpos = bb->max_offset;
2041 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2042 MonoProfileCoverageInfo *cov = cfg->coverage_info;
2043 g_assert (!cfg->compile_aot);
2046 cov->data [bb->dfn].cil_code = bb->cil_code;
2047 /* this is not thread save, but good enough */
2048 x86_inc_mem (code, &cov->data [bb->dfn].count);
2051 offset = code - cfg->native_code;
2053 mono_debug_open_block (cfg, bb, offset);
2057 offset = code - cfg->native_code;
2059 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2061 if (offset > (cfg->code_size - max_len - 16)) {
2062 cfg->code_size *= 2;
2063 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2064 code = cfg->native_code + offset;
2065 mono_jit_stats.code_reallocs++;
2068 mono_debug_record_line_number (cfg, ins, offset);
2070 switch (ins->opcode) {
2072 x86_mul_reg (code, ins->sreg2, TRUE);
2075 x86_mul_reg (code, ins->sreg2, FALSE);
2077 case OP_X86_SETEQ_MEMBASE:
2078 case OP_X86_SETNE_MEMBASE:
2079 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
2080 ins->inst_basereg, ins->inst_offset, TRUE);
2082 case OP_STOREI1_MEMBASE_IMM:
2083 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
2085 case OP_STOREI2_MEMBASE_IMM:
2086 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
2088 case OP_STORE_MEMBASE_IMM:
2089 case OP_STOREI4_MEMBASE_IMM:
2090 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
2092 case OP_STOREI1_MEMBASE_REG:
2093 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
2095 case OP_STOREI2_MEMBASE_REG:
2096 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
2098 case OP_STORE_MEMBASE_REG:
2099 case OP_STOREI4_MEMBASE_REG:
2100 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
2105 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2108 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2109 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2111 case OP_LOAD_MEMBASE:
2112 case OP_LOADI4_MEMBASE:
2113 case OP_LOADU4_MEMBASE:
2114 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
2116 case OP_LOADU1_MEMBASE:
2117 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
2119 case OP_LOADI1_MEMBASE:
2120 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
2122 case OP_LOADU2_MEMBASE:
2123 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
2125 case OP_LOADI2_MEMBASE:
2126 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
2129 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2132 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2135 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
2138 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
2141 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
2143 case OP_COMPARE_IMM:
2144 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
2146 case OP_X86_COMPARE_MEMBASE_REG:
2147 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
2149 case OP_X86_COMPARE_MEMBASE_IMM:
2150 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2152 case OP_X86_COMPARE_MEMBASE8_IMM:
2153 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2155 case OP_X86_COMPARE_REG_MEMBASE:
2156 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
2158 case OP_X86_COMPARE_MEM_IMM:
2159 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
2161 case OP_X86_TEST_NULL:
2162 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2164 case OP_X86_ADD_MEMBASE_IMM:
2165 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2167 case OP_X86_ADD_MEMBASE:
2168 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
2170 case OP_X86_SUB_MEMBASE_IMM:
2171 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2173 case OP_X86_SUB_MEMBASE:
2174 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
2176 case OP_X86_AND_MEMBASE_IMM:
2177 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2179 case OP_X86_OR_MEMBASE_IMM:
2180 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2182 case OP_X86_XOR_MEMBASE_IMM:
2183 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
2185 case OP_X86_INC_MEMBASE:
2186 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
2188 case OP_X86_INC_REG:
2189 x86_inc_reg (code, ins->dreg);
2191 case OP_X86_DEC_MEMBASE:
2192 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
2194 case OP_X86_DEC_REG:
2195 x86_dec_reg (code, ins->dreg);
2197 case OP_X86_MUL_MEMBASE:
2198 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
2201 x86_breakpoint (code);
2205 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
2208 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
2212 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
2215 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
2219 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
2222 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
2226 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
2229 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2232 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2235 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2239 x86_div_reg (code, ins->sreg2, TRUE);
2242 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2243 x86_div_reg (code, ins->sreg2, FALSE);
2246 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2248 x86_div_reg (code, ins->sreg2, TRUE);
2252 x86_div_reg (code, ins->sreg2, TRUE);
2255 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2256 x86_div_reg (code, ins->sreg2, FALSE);
2259 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2261 x86_div_reg (code, ins->sreg2, TRUE);
2264 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2267 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2271 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2274 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2277 g_assert (ins->sreg2 == X86_ECX);
2278 x86_shift_reg (code, X86_SHL, ins->dreg);
2281 g_assert (ins->sreg2 == X86_ECX);
2282 x86_shift_reg (code, X86_SAR, ins->dreg);
2285 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2288 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2291 g_assert (ins->sreg2 == X86_ECX);
2292 x86_shift_reg (code, X86_SHR, ins->dreg);
2295 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2298 guint8 *jump_to_end;
2300 /* handle shifts below 32 bits */
2301 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
2302 x86_shift_reg (code, X86_SHL, ins->sreg1);
2304 x86_test_reg_imm (code, X86_ECX, 32);
2305 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2307 /* handle shift over 32 bit */
2308 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2309 x86_clear_reg (code, ins->sreg1);
2311 x86_patch (jump_to_end, code);
2315 guint8 *jump_to_end;
2317 /* handle shifts below 32 bits */
2318 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2319 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
2321 x86_test_reg_imm (code, X86_ECX, 32);
2322 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2324 /* handle shifts over 31 bits */
2325 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2326 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
2328 x86_patch (jump_to_end, code);
2332 guint8 *jump_to_end;
2334 /* handle shifts below 32 bits */
2335 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2336 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
2338 x86_test_reg_imm (code, X86_ECX, 32);
2339 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2341 /* handle shifts over 31 bits */
2342 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2343 x86_clear_reg (code, ins->backend.reg3);
2345 x86_patch (jump_to_end, code);
2349 if (ins->inst_imm >= 32) {
2350 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2351 x86_clear_reg (code, ins->sreg1);
2352 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
2354 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
2355 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
2359 if (ins->inst_imm >= 32) {
2360 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2361 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
2362 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
2364 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2365 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
2368 case OP_LSHR_UN_IMM:
2369 if (ins->inst_imm >= 32) {
2370 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2371 x86_clear_reg (code, ins->backend.reg3);
2372 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
2374 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2375 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
2379 x86_not_reg (code, ins->sreg1);
2382 x86_neg_reg (code, ins->sreg1);
2385 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2388 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2391 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2394 switch (ins->inst_imm) {
2398 if (ins->dreg != ins->sreg1)
2399 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2400 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2403 /* LEA r1, [r2 + r2*2] */
2404 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2407 /* LEA r1, [r2 + r2*4] */
2408 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2411 /* LEA r1, [r2 + r2*2] */
2413 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2414 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2417 /* LEA r1, [r2 + r2*8] */
2418 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
2421 /* LEA r1, [r2 + r2*4] */
2423 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2424 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2427 /* LEA r1, [r2 + r2*2] */
2429 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2430 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2433 /* LEA r1, [r2 + r2*4] */
2434 /* LEA r1, [r1 + r1*4] */
2435 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2436 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2439 /* LEA r1, [r2 + r2*4] */
2441 /* LEA r1, [r1 + r1*4] */
2442 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2443 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2444 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2447 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2452 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2453 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2455 case CEE_MUL_OVF_UN: {
2456 /* the mul operation and the exception check should most likely be split */
2457 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2458 /*g_assert (ins->sreg2 == X86_EAX);
2459 g_assert (ins->dreg == X86_EAX);*/
2460 if (ins->sreg2 == X86_EAX) {
2461 non_eax_reg = ins->sreg1;
2462 } else if (ins->sreg1 == X86_EAX) {
2463 non_eax_reg = ins->sreg2;
2465 /* no need to save since we're going to store to it anyway */
2466 if (ins->dreg != X86_EAX) {
2468 x86_push_reg (code, X86_EAX);
2470 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2471 non_eax_reg = ins->sreg2;
2473 if (ins->dreg == X86_EDX) {
2476 x86_push_reg (code, X86_EAX);
2478 } else if (ins->dreg != X86_EAX) {
2480 x86_push_reg (code, X86_EDX);
2482 x86_mul_reg (code, non_eax_reg, FALSE);
2483 /* save before the check since pop and mov don't change the flags */
2484 if (ins->dreg != X86_EAX)
2485 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2487 x86_pop_reg (code, X86_EDX);
2489 x86_pop_reg (code, X86_EAX);
2490 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2494 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2497 g_assert_not_reached ();
2498 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2499 x86_mov_reg_imm (code, ins->dreg, 0);
2501 case OP_LOAD_GOTADDR:
2502 x86_call_imm (code, 0);
2504 * The patch needs to point to the pop, since the GOT offset needs
2505 * to be added to that address.
2507 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
2508 x86_pop_reg (code, ins->dreg);
2509 x86_alu_reg_imm (code, X86_ADD, ins->dreg, 0xf0f0f0f0);
2512 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2513 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
2515 case OP_X86_PUSH_GOT_ENTRY:
2516 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2517 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
2521 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2524 g_assert_not_reached ();
2527 * Note: this 'frame destruction' logic is useful for tail calls, too.
2528 * Keep in sync with the code in emit_epilog.
2532 /* FIXME: no tracing support... */
2533 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2534 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2535 /* reset offset to make max_len work */
2536 offset = code - cfg->native_code;
2538 g_assert (!cfg->method->save_lmf);
2540 code = emit_load_volatile_arguments (cfg, code);
2542 if (cfg->used_int_regs & (1 << X86_EBX))
2544 if (cfg->used_int_regs & (1 << X86_EDI))
2546 if (cfg->used_int_regs & (1 << X86_ESI))
2549 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2551 if (cfg->used_int_regs & (1 << X86_ESI))
2552 x86_pop_reg (code, X86_ESI);
2553 if (cfg->used_int_regs & (1 << X86_EDI))
2554 x86_pop_reg (code, X86_EDI);
2555 if (cfg->used_int_regs & (1 << X86_EBX))
2556 x86_pop_reg (code, X86_EBX);
2558 /* restore ESP/EBP */
2560 offset = code - cfg->native_code;
2561 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2562 x86_jump32 (code, 0);
2566 /* ensure ins->sreg1 is not NULL
2567 * note that cmp DWORD PTR [eax], eax is one byte shorter than
2568 * cmp DWORD PTR [eax], 0
2570 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
2573 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2574 x86_push_reg (code, hreg);
2575 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2576 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2577 x86_pop_reg (code, hreg);
2585 call = (MonoCallInst*)ins;
2586 if (ins->flags & MONO_INST_HAS_METHOD)
2587 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2589 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2590 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2591 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
2592 * bytes to pop, we want to use pops. GCC does this (note it won't happen
2593 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
2594 * smart enough to do that optimization yet
2596 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
2597 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
2598 * (most likely from locality benefits). People with other processors should
2599 * check on theirs to see what happens.
2601 if (call->stack_usage == 4) {
2602 /* we want to use registers that won't get used soon, so use
2603 * ecx, as eax will get allocated first. edx is used by long calls,
2604 * so we can't use that.
2607 x86_pop_reg (code, X86_ECX);
2609 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2612 code = emit_move_return_value (cfg, ins, code);
2617 case OP_VOIDCALL_REG:
2619 call = (MonoCallInst*)ins;
2620 x86_call_reg (code, ins->sreg1);
2621 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2622 if (call->stack_usage == 4)
2623 x86_pop_reg (code, X86_ECX);
2625 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2627 code = emit_move_return_value (cfg, ins, code);
2629 case OP_FCALL_MEMBASE:
2630 case OP_LCALL_MEMBASE:
2631 case OP_VCALL_MEMBASE:
2632 case OP_VOIDCALL_MEMBASE:
2633 case OP_CALL_MEMBASE:
2634 call = (MonoCallInst*)ins;
2635 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2636 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2637 if (call->stack_usage == 4)
2638 x86_pop_reg (code, X86_ECX);
2640 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2642 code = emit_move_return_value (cfg, ins, code);
2646 x86_push_reg (code, ins->sreg1);
2648 case OP_X86_PUSH_IMM:
2649 x86_push_imm (code, ins->inst_imm);
2651 case OP_X86_PUSH_MEMBASE:
2652 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2654 case OP_X86_PUSH_OBJ:
2655 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2656 x86_push_reg (code, X86_EDI);
2657 x86_push_reg (code, X86_ESI);
2658 x86_push_reg (code, X86_ECX);
2659 if (ins->inst_offset)
2660 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2662 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2663 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2664 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2666 x86_prefix (code, X86_REP_PREFIX);
2668 x86_pop_reg (code, X86_ECX);
2669 x86_pop_reg (code, X86_ESI);
2670 x86_pop_reg (code, X86_EDI);
2673 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
2675 case OP_X86_LEA_MEMBASE:
2676 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
2679 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2682 /* keep alignment */
2683 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_LOCALLOC_ALIGNMENT - 1);
2684 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1));
2685 code = mono_emit_stack_alloc (code, ins);
2686 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2692 x86_push_reg (code, ins->sreg1);
2693 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2694 (gpointer)"mono_arch_throw_exception");
2698 x86_push_reg (code, ins->sreg1);
2699 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2700 (gpointer)"mono_arch_rethrow_exception");
2703 case OP_CALL_HANDLER:
2706 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 12);
2708 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2709 x86_call_imm (code, 0);
2711 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2715 ins->inst_c0 = code - cfg->native_code;
2718 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2719 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2721 if (ins->flags & MONO_INST_BRLABEL) {
2722 if (ins->inst_i0->inst_c0) {
2723 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2725 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2726 if ((cfg->opt & MONO_OPT_BRANCH) &&
2727 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
2728 x86_jump8 (code, 0);
2730 x86_jump32 (code, 0);
2733 if (ins->inst_target_bb->native_offset) {
2734 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2736 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2737 if ((cfg->opt & MONO_OPT_BRANCH) &&
2738 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
2739 x86_jump8 (code, 0);
2741 x86_jump32 (code, 0);
2746 x86_jump_reg (code, ins->sreg1);
2754 x86_set_reg (code, cc_table [mono_opcode_to_cond (ins->opcode)], ins->dreg, cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
2755 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2757 case OP_COND_EXC_EQ:
2758 case OP_COND_EXC_NE_UN:
2759 case OP_COND_EXC_LT:
2760 case OP_COND_EXC_LT_UN:
2761 case OP_COND_EXC_GT:
2762 case OP_COND_EXC_GT_UN:
2763 case OP_COND_EXC_GE:
2764 case OP_COND_EXC_GE_UN:
2765 case OP_COND_EXC_LE:
2766 case OP_COND_EXC_LE_UN:
2767 EMIT_COND_SYSTEM_EXCEPTION (cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)], ins->inst_p1);
2769 case OP_COND_EXC_OV:
2770 case OP_COND_EXC_NO:
2772 case OP_COND_EXC_NC:
2773 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2785 EMIT_COND_BRANCH (ins, cc_table [mono_opcode_to_cond (ins->opcode)], cc_signed_table [mono_opcode_to_cond (ins->opcode)]);
2788 /* floating point opcodes */
2790 double d = *(double *)ins->inst_p0;
2792 if ((d == 0.0) && (mono_signbit (d) == 0)) {
2794 } else if (d == 1.0) {
2797 if (cfg->compile_aot) {
2798 guint32 *val = (guint32*)&d;
2799 x86_push_imm (code, val [1]);
2800 x86_push_imm (code, val [0]);
2801 x86_fld_membase (code, X86_ESP, 0, TRUE);
2802 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2805 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
2806 x86_fld (code, NULL, TRUE);
2812 float f = *(float *)ins->inst_p0;
2814 if ((f == 0.0) && (mono_signbit (f) == 0)) {
2816 } else if (f == 1.0) {
2819 if (cfg->compile_aot) {
2820 guint32 val = *(guint32*)&f;
2821 x86_push_imm (code, val);
2822 x86_fld_membase (code, X86_ESP, 0, FALSE);
2823 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2826 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
2827 x86_fld (code, NULL, FALSE);
2832 case OP_STORER8_MEMBASE_REG:
2833 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
2835 case OP_LOADR8_SPILL_MEMBASE:
2836 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2839 case OP_LOADR8_MEMBASE:
2840 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2842 case OP_STORER4_MEMBASE_REG:
2843 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
2845 case OP_LOADR4_MEMBASE:
2846 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2848 case CEE_CONV_R4: /* FIXME: change precision */
2850 x86_push_reg (code, ins->sreg1);
2851 x86_fild_membase (code, X86_ESP, 0, FALSE);
2852 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2854 case OP_X86_FP_LOAD_I8:
2855 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2857 case OP_X86_FP_LOAD_I4:
2858 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2860 case OP_FCONV_TO_I1:
2861 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2863 case OP_FCONV_TO_U1:
2864 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2866 case OP_FCONV_TO_I2:
2867 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2869 case OP_FCONV_TO_U2:
2870 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2872 case OP_FCONV_TO_I4:
2874 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2876 case OP_FCONV_TO_I8:
2877 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2878 x86_fnstcw_membase(code, X86_ESP, 0);
2879 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
2880 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
2881 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
2882 x86_fldcw_membase (code, X86_ESP, 2);
2883 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2884 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2885 x86_pop_reg (code, ins->dreg);
2886 x86_pop_reg (code, ins->backend.reg3);
2887 x86_fldcw_membase (code, X86_ESP, 0);
2888 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2890 case OP_LCONV_TO_R_UN: {
2891 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2894 /* load 64bit integer to FP stack */
2895 x86_push_imm (code, 0);
2896 x86_push_reg (code, ins->sreg2);
2897 x86_push_reg (code, ins->sreg1);
2898 x86_fild_membase (code, X86_ESP, 0, TRUE);
2899 /* store as 80bit FP value */
2900 x86_fst80_membase (code, X86_ESP, 0);
2902 /* test if lreg is negative */
2903 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2904 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2906 /* add correction constant mn */
2907 x86_fld80_mem (code, mn);
2908 x86_fld80_membase (code, X86_ESP, 0);
2909 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2910 x86_fst80_membase (code, X86_ESP, 0);
2912 x86_patch (br, code);
2914 x86_fld80_membase (code, X86_ESP, 0);
2915 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2919 case OP_LCONV_TO_OVF_I: {
2920 guint8 *br [3], *label [1];
2924 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2926 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2928 /* If the low word top bit is set, see if we are negative */
2929 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2930 /* We are not negative (no top bit set, check for our top word to be zero */
2931 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2932 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2935 /* throw exception */
2936 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
2938 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
2939 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
2940 x86_jump8 (code, 0);
2942 x86_jump32 (code, 0);
2944 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2945 x86_jump32 (code, 0);
2949 x86_patch (br [0], code);
2950 /* our top bit is set, check that top word is 0xfffffff */
2951 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2953 x86_patch (br [1], code);
2954 /* nope, emit exception */
2955 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2956 x86_patch (br [2], label [0]);
2958 if (ins->dreg != ins->sreg1)
2959 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2963 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2966 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
2969 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
2972 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
2980 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2985 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2992 * it really doesn't make sense to inline all this code,
2993 * it's here just to show that things may not be as simple
2996 guchar *check_pos, *end_tan, *pop_jump;
2997 x86_push_reg (code, X86_EAX);
3000 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3002 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3003 x86_fstp (code, 0); /* pop the 1.0 */
3005 x86_jump8 (code, 0);
3007 x86_fp_op (code, X86_FADD, 0);
3011 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
3013 x86_branch8 (code, X86_CC_NE, 0, FALSE);
3016 x86_patch (pop_jump, code);
3017 x86_fstp (code, 0); /* pop the 1.0 */
3018 x86_patch (check_pos, code);
3019 x86_patch (end_tan, code);
3021 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3022 x86_pop_reg (code, X86_EAX);
3029 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
3040 x86_push_reg (code, X86_EAX);
3041 /* we need to exchange ST(0) with ST(1) */
3044 /* this requires a loop, because fprem somtimes
3045 * returns a partial remainder */
3047 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
3048 /* x86_fprem1 (code); */
3051 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
3053 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
3058 x86_pop_reg (code, X86_EAX);
3062 if (cfg->opt & MONO_OPT_FCMOV) {
3063 x86_fcomip (code, 1);
3067 /* this overwrites EAX */
3068 EMIT_FPCOMPARE(code);
3069 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3072 if (cfg->opt & MONO_OPT_FCMOV) {
3073 /* zeroing the register at the start results in
3074 * shorter and faster code (we can also remove the widening op)
3076 guchar *unordered_check;
3077 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3078 x86_fcomip (code, 1);
3080 unordered_check = code;
3081 x86_branch8 (code, X86_CC_P, 0, FALSE);
3082 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
3083 x86_patch (unordered_check, code);
3086 if (ins->dreg != X86_EAX)
3087 x86_push_reg (code, X86_EAX);
3089 EMIT_FPCOMPARE(code);
3090 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3091 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3092 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3093 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3095 if (ins->dreg != X86_EAX)
3096 x86_pop_reg (code, X86_EAX);
3100 if (cfg->opt & MONO_OPT_FCMOV) {
3101 /* zeroing the register at the start results in
3102 * shorter and faster code (we can also remove the widening op)
3104 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3105 x86_fcomip (code, 1);
3107 if (ins->opcode == OP_FCLT_UN) {
3108 guchar *unordered_check = code;
3109 guchar *jump_to_end;
3110 x86_branch8 (code, X86_CC_P, 0, FALSE);
3111 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3113 x86_jump8 (code, 0);
3114 x86_patch (unordered_check, code);
3115 x86_inc_reg (code, ins->dreg);
3116 x86_patch (jump_to_end, code);
3118 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
3122 if (ins->dreg != X86_EAX)
3123 x86_push_reg (code, X86_EAX);
3125 EMIT_FPCOMPARE(code);
3126 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3127 if (ins->opcode == OP_FCLT_UN) {
3128 guchar *is_not_zero_check, *end_jump;
3129 is_not_zero_check = code;
3130 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3132 x86_jump8 (code, 0);
3133 x86_patch (is_not_zero_check, code);
3134 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3136 x86_patch (end_jump, code);
3138 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3139 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3141 if (ins->dreg != X86_EAX)
3142 x86_pop_reg (code, X86_EAX);
3146 if (cfg->opt & MONO_OPT_FCMOV) {
3147 /* zeroing the register at the start results in
3148 * shorter and faster code (we can also remove the widening op)
3150 guchar *unordered_check;
3151 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
3152 x86_fcomip (code, 1);
3154 if (ins->opcode == OP_FCGT) {
3155 unordered_check = code;
3156 x86_branch8 (code, X86_CC_P, 0, FALSE);
3157 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3158 x86_patch (unordered_check, code);
3160 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
3164 if (ins->dreg != X86_EAX)
3165 x86_push_reg (code, X86_EAX);
3167 EMIT_FPCOMPARE(code);
3168 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
3169 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3170 if (ins->opcode == OP_FCGT_UN) {
3171 guchar *is_not_zero_check, *end_jump;
3172 is_not_zero_check = code;
3173 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3175 x86_jump8 (code, 0);
3176 x86_patch (is_not_zero_check, code);
3177 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3179 x86_patch (end_jump, code);
3181 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
3182 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
3184 if (ins->dreg != X86_EAX)
3185 x86_pop_reg (code, X86_EAX);
3188 if (cfg->opt & MONO_OPT_FCMOV) {
3189 guchar *jump = code;
3190 x86_branch8 (code, X86_CC_P, 0, TRUE);
3191 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3192 x86_patch (jump, code);
3195 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
3196 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
3199 /* Branch if C013 != 100 */
3200 if (cfg->opt & MONO_OPT_FCMOV) {
3201 /* branch if !ZF or (PF|CF) */
3202 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3203 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3204 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
3207 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3208 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3211 if (cfg->opt & MONO_OPT_FCMOV) {
3212 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3215 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3218 if (cfg->opt & MONO_OPT_FCMOV) {
3219 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3220 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3223 if (ins->opcode == OP_FBLT_UN) {
3224 guchar *is_not_zero_check, *end_jump;
3225 is_not_zero_check = code;
3226 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3228 x86_jump8 (code, 0);
3229 x86_patch (is_not_zero_check, code);
3230 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3232 x86_patch (end_jump, code);
3234 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3238 if (cfg->opt & MONO_OPT_FCMOV) {
3239 if (ins->opcode == OP_FBGT) {
3242 /* skip branch if C1=1 */
3244 x86_branch8 (code, X86_CC_P, 0, FALSE);
3245 /* branch if (C0 | C3) = 1 */
3246 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3247 x86_patch (br1, code);
3249 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3253 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3254 if (ins->opcode == OP_FBGT_UN) {
3255 guchar *is_not_zero_check, *end_jump;
3256 is_not_zero_check = code;
3257 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3259 x86_jump8 (code, 0);
3260 x86_patch (is_not_zero_check, code);
3261 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3263 x86_patch (end_jump, code);
3265 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3268 /* Branch if C013 == 100 or 001 */
3269 if (cfg->opt & MONO_OPT_FCMOV) {
3272 /* skip branch if C1=1 */
3274 x86_branch8 (code, X86_CC_P, 0, FALSE);
3275 /* branch if (C0 | C3) = 1 */
3276 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
3277 x86_patch (br1, code);
3280 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3281 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3282 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3283 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3286 /* Branch if C013 == 000 */
3287 if (cfg->opt & MONO_OPT_FCMOV) {
3288 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
3291 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3294 /* Branch if C013=000 or 100 */
3295 if (cfg->opt & MONO_OPT_FCMOV) {
3298 /* skip branch if C1=1 */
3300 x86_branch8 (code, X86_CC_P, 0, FALSE);
3301 /* branch if C0=0 */
3302 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
3303 x86_patch (br1, code);
3306 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
3307 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
3308 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3311 /* Branch if C013 != 001 */
3312 if (cfg->opt & MONO_OPT_FCMOV) {
3313 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3314 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
3317 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3318 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3321 x86_push_reg (code, X86_EAX);
3324 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
3325 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3326 x86_pop_reg (code, X86_EAX);
3327 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
3331 code = emit_tls_get (code, ins->dreg, ins->inst_offset);
3334 case OP_MEMORY_BARRIER: {
3335 /* Not needed on x86 */
3338 case OP_ATOMIC_ADD_I4: {
3339 int dreg = ins->dreg;
3341 if (dreg == ins->inst_basereg) {
3342 x86_push_reg (code, ins->sreg2);
3346 if (dreg != ins->sreg2)
3347 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
3349 x86_prefix (code, X86_LOCK_PREFIX);
3350 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3352 if (dreg != ins->dreg) {
3353 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3354 x86_pop_reg (code, dreg);
3359 case OP_ATOMIC_ADD_NEW_I4: {
3360 int dreg = ins->dreg;
3362 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
3363 if (ins->sreg2 == dreg) {
3364 if (dreg == X86_EBX) {
3366 if (ins->inst_basereg == X86_EDI)
3370 if (ins->inst_basereg == X86_EBX)
3373 } else if (ins->inst_basereg == dreg) {
3374 if (dreg == X86_EBX) {
3376 if (ins->sreg2 == X86_EDI)
3380 if (ins->sreg2 == X86_EBX)
3385 if (dreg != ins->dreg) {
3386 x86_push_reg (code, dreg);
3389 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
3390 x86_prefix (code, X86_LOCK_PREFIX);
3391 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3392 /* dreg contains the old value, add with sreg2 value */
3393 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
3395 if (ins->dreg != dreg) {
3396 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3397 x86_pop_reg (code, dreg);
3402 case OP_ATOMIC_EXCHANGE_I4: {
3404 int sreg2 = ins->sreg2;
3405 int breg = ins->inst_basereg;
3407 /* cmpxchg uses eax as comperand, need to make sure we can use it
3408 * hack to overcome limits in x86 reg allocator
3409 * (req: dreg == eax and sreg2 != eax and breg != eax)
3411 if (ins->dreg != X86_EAX)
3412 x86_push_reg (code, X86_EAX);
3414 /* We need the EAX reg for the cmpxchg */
3415 if (ins->sreg2 == X86_EAX) {
3416 x86_push_reg (code, X86_EDX);
3417 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
3421 if (breg == X86_EAX) {
3422 x86_push_reg (code, X86_ESI);
3423 x86_mov_reg_reg (code, X86_ESI, X86_EAX, 4);
3427 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
3429 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
3430 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
3431 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
3432 x86_patch (br [1], br [0]);
3434 if (breg != ins->inst_basereg)
3435 x86_pop_reg (code, X86_ESI);
3437 if (ins->dreg != X86_EAX) {
3438 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3439 x86_pop_reg (code, X86_EAX);
3442 if (ins->sreg2 != sreg2)
3443 x86_pop_reg (code, X86_EDX);
3448 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3449 g_assert_not_reached ();
3452 if ((code - cfg->native_code - offset) > max_len) {
3453 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3454 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3455 g_assert_not_reached ();
3461 last_offset = offset;
3466 cfg->code_len = code - cfg->native_code;
3470 mono_arch_register_lowlevel_calls (void)
3475 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3477 MonoJumpInfo *patch_info;
3478 gboolean compile_aot = !run_cctors;
3480 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3481 unsigned char *ip = patch_info->ip.i + code;
3482 const unsigned char *target;
3484 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3487 switch (patch_info->type) {
3488 case MONO_PATCH_INFO_BB:
3489 case MONO_PATCH_INFO_LABEL:
3492 /* No need to patch these */
3497 switch (patch_info->type) {
3498 case MONO_PATCH_INFO_IP:
3499 *((gconstpointer *)(ip)) = target;
3501 case MONO_PATCH_INFO_CLASS_INIT: {
3503 /* Might already been changed to a nop */
3504 x86_call_code (code, 0);
3505 x86_patch (ip, target);
3508 case MONO_PATCH_INFO_ABS:
3509 case MONO_PATCH_INFO_METHOD:
3510 case MONO_PATCH_INFO_METHOD_JUMP:
3511 case MONO_PATCH_INFO_INTERNAL_METHOD:
3512 case MONO_PATCH_INFO_BB:
3513 case MONO_PATCH_INFO_LABEL:
3514 x86_patch (ip, target);
3516 case MONO_PATCH_INFO_NONE:
3519 guint32 offset = mono_arch_get_patch_offset (ip);
3520 *((gconstpointer *)(ip + offset)) = target;
3528 mono_arch_emit_prolog (MonoCompile *cfg)
3530 MonoMethod *method = cfg->method;
3532 MonoMethodSignature *sig;
3534 int alloc_size, pos, max_offset, i;
3537 cfg->code_size = MAX (mono_method_get_header (method)->code_size * 4, 256);
3538 code = cfg->native_code = g_malloc (cfg->code_size);
3540 x86_push_reg (code, X86_EBP);
3541 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
3543 alloc_size = cfg->stack_offset;
3546 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
3547 /* Might need to attach the thread to the JIT */
3548 if (lmf_tls_offset != -1) {
3551 code = emit_tls_get ( code, X86_EAX, lmf_tls_offset);
3552 x86_test_reg_reg (code, X86_EAX, X86_EAX);
3554 x86_branch8 (code, X86_CC_NE, 0, 0);
3555 x86_push_imm (code, cfg->domain);
3556 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
3557 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3558 x86_patch (buf, code);
3559 #ifdef PLATFORM_WIN32
3560 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3561 /* FIXME: Add a separate key for LMF to avoid this */
3562 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
3565 g_assert (!cfg->compile_aot);
3566 x86_push_imm (code, cfg->domain);
3567 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
3568 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3572 if (method->save_lmf) {
3573 pos += sizeof (MonoLMF);
3575 /* save the current IP */
3576 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3577 x86_push_imm_template (code);
3579 /* save all caller saved regs */
3580 x86_push_reg (code, X86_EBP);
3581 x86_push_reg (code, X86_ESI);
3582 x86_push_reg (code, X86_EDI);
3583 x86_push_reg (code, X86_EBX);
3585 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
3587 * Optimized version which uses the mono_lmf TLS variable instead of indirection
3588 * through the mono_lmf_addr TLS variable.
3590 /* %eax = previous_lmf */
3591 x86_prefix (code, X86_GS_PREFIX);
3592 x86_mov_reg_mem (code, X86_EAX, lmf_tls_offset, 4);
3593 /* skip method_info + lmf */
3594 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3595 /* push previous_lmf */
3596 x86_push_reg (code, X86_EAX);
3598 x86_prefix (code, X86_GS_PREFIX);
3599 x86_mov_mem_reg (code, lmf_tls_offset, X86_ESP, 4);
3601 /* get the address of lmf for the current thread */
3603 * This is performance critical so we try to use some tricks to make
3607 if (lmf_addr_tls_offset != -1) {
3608 /* Load lmf quicky using the GS register */
3609 code = emit_tls_get (code, X86_EAX, lmf_addr_tls_offset);
3610 #ifdef PLATFORM_WIN32
3611 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3612 /* FIXME: Add a separate key for LMF to avoid this */
3613 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
3616 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
3619 /* Skip method info */
3620 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3623 x86_push_reg (code, X86_EAX);
3624 /* push *lfm (previous_lmf) */
3625 x86_push_membase (code, X86_EAX, 0);
3627 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3631 if (cfg->used_int_regs & (1 << X86_EBX)) {
3632 x86_push_reg (code, X86_EBX);
3636 if (cfg->used_int_regs & (1 << X86_EDI)) {
3637 x86_push_reg (code, X86_EDI);
3641 if (cfg->used_int_regs & (1 << X86_ESI)) {
3642 x86_push_reg (code, X86_ESI);
3650 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
3652 int tot = alloc_size + pos + 4 + 4; /* ret ip + ebp */
3664 /* See mono_emit_stack_alloc */
3665 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3666 guint32 remaining_size = alloc_size;
3667 while (remaining_size >= 0x1000) {
3668 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
3669 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
3670 remaining_size -= 0x1000;
3673 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
3675 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
3680 /* check the stack is aligned */
3681 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
3682 x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
3683 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
3684 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
3685 x86_breakpoint (code);
3688 /* compute max_offset in order to use short forward jumps */
3690 if (cfg->opt & MONO_OPT_BRANCH) {
3691 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3692 MonoInst *ins = bb->code;
3693 bb->max_offset = max_offset;
3695 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3697 /* max alignment for loops */
3698 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
3699 max_offset += LOOP_ALIGNMENT;
3702 if (ins->opcode == OP_LABEL)
3703 ins->inst_c1 = max_offset;
3705 max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3711 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3712 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3714 /* load arguments allocated to register from the stack */
3715 sig = mono_method_signature (method);
3718 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3719 inst = cfg->args [pos];
3720 if (inst->opcode == OP_REGVAR) {
3721 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
3722 if (cfg->verbose_level > 2)
3723 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3728 cfg->code_len = code - cfg->native_code;
3734 mono_arch_emit_epilog (MonoCompile *cfg)
3736 MonoMethod *method = cfg->method;
3737 MonoMethodSignature *sig = mono_method_signature (method);
3739 guint32 stack_to_pop;
3741 int max_epilog_size = 16;
3744 if (cfg->method->save_lmf)
3745 max_epilog_size += 128;
3747 if (mono_jit_trace_calls != NULL)
3748 max_epilog_size += 50;
3750 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3751 cfg->code_size *= 2;
3752 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3753 mono_jit_stats.code_reallocs++;
3756 code = cfg->native_code + cfg->code_len;
3758 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3759 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3761 /* the code restoring the registers must be kept in sync with OP_JMP */
3764 if (method->save_lmf) {
3765 gint32 prev_lmf_reg;
3766 gint32 lmf_offset = -sizeof (MonoLMF);
3768 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
3770 * Optimized version which uses the mono_lmf TLS variable instead of indirection
3771 * through the mono_lmf_addr TLS variable.
3773 /* reg = previous_lmf */
3774 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
3776 /* lmf = previous_lmf */
3777 x86_prefix (code, X86_GS_PREFIX);
3778 x86_mov_mem_reg (code, lmf_tls_offset, X86_ECX, 4);
3780 /* Find a spare register */
3781 switch (sig->ret->type) {
3784 prev_lmf_reg = X86_EDI;
3785 cfg->used_int_regs |= (1 << X86_EDI);
3788 prev_lmf_reg = X86_EDX;
3792 /* reg = previous_lmf */
3793 x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
3796 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 4);
3798 /* *(lmf) = previous_lmf */
3799 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
3802 /* restore caller saved regs */
3803 if (cfg->used_int_regs & (1 << X86_EBX)) {
3804 x86_mov_reg_membase (code, X86_EBX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), 4);
3807 if (cfg->used_int_regs & (1 << X86_EDI)) {
3808 x86_mov_reg_membase (code, X86_EDI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), 4);
3810 if (cfg->used_int_regs & (1 << X86_ESI)) {
3811 x86_mov_reg_membase (code, X86_ESI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), 4);
3814 /* EBP is restored by LEAVE */
3816 if (cfg->used_int_regs & (1 << X86_EBX)) {
3819 if (cfg->used_int_regs & (1 << X86_EDI)) {
3822 if (cfg->used_int_regs & (1 << X86_ESI)) {
3827 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3829 if (cfg->used_int_regs & (1 << X86_ESI)) {
3830 x86_pop_reg (code, X86_ESI);
3832 if (cfg->used_int_regs & (1 << X86_EDI)) {
3833 x86_pop_reg (code, X86_EDI);
3835 if (cfg->used_int_regs & (1 << X86_EBX)) {
3836 x86_pop_reg (code, X86_EBX);
3840 /* Load returned vtypes into registers if needed */
3841 cinfo = get_call_info (cfg->mempool, sig, FALSE);
3842 if (cinfo->ret.storage == ArgValuetypeInReg) {
3843 for (quad = 0; quad < 2; quad ++) {
3844 switch (cinfo->ret.pair_storage [quad]) {
3846 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
3848 case ArgOnFloatFpStack:
3849 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
3851 case ArgOnDoubleFpStack:
3852 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
3857 g_assert_not_reached ();
3864 if (CALLCONV_IS_STDCALL (sig)) {
3865 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
3867 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
3868 } else if (MONO_TYPE_ISSTRUCT (mono_method_signature (cfg->method)->ret) && (cinfo->ret.storage == ArgOnStack))
3874 x86_ret_imm (code, stack_to_pop);
3878 cfg->code_len = code - cfg->native_code;
3880 g_assert (cfg->code_len < cfg->code_size);
3884 mono_arch_emit_exceptions (MonoCompile *cfg)
3886 MonoJumpInfo *patch_info;
3889 MonoClass *exc_classes [16];
3890 guint8 *exc_throw_start [16], *exc_throw_end [16];
3894 /* Compute needed space */
3895 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3896 if (patch_info->type == MONO_PATCH_INFO_EXC)
3901 * make sure we have enough space for exceptions
3902 * 16 is the size of two push_imm instructions and a call
3904 if (cfg->compile_aot)
3905 code_size = exc_count * 32;
3907 code_size = exc_count * 16;
3909 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
3910 cfg->code_size *= 2;
3911 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3912 mono_jit_stats.code_reallocs++;
3915 code = cfg->native_code + cfg->code_len;
3918 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3919 switch (patch_info->type) {
3920 case MONO_PATCH_INFO_EXC: {
3921 MonoClass *exc_class;
3925 x86_patch (patch_info->ip.i + cfg->native_code, code);
3927 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
3928 g_assert (exc_class);
3929 throw_ip = patch_info->ip.i;
3931 /* Find a throw sequence for the same exception class */
3932 for (i = 0; i < nthrows; ++i)
3933 if (exc_classes [i] == exc_class)
3936 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
3937 x86_jump_code (code, exc_throw_start [i]);
3938 patch_info->type = MONO_PATCH_INFO_NONE;
3943 /* Compute size of code following the push <OFFSET> */
3946 if ((code - cfg->native_code) - throw_ip < 126 - size) {
3947 /* Use the shorter form */
3949 x86_push_imm (code, 0);
3953 x86_push_imm (code, 0xf0f0f0f0);
3958 exc_classes [nthrows] = exc_class;
3959 exc_throw_start [nthrows] = code;
3962 x86_push_imm (code, exc_class->type_token - MONO_TOKEN_TYPE_DEF);
3963 patch_info->data.name = "mono_arch_throw_corlib_exception";
3964 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3965 patch_info->ip.i = code - cfg->native_code;
3966 x86_call_code (code, 0);
3967 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
3972 exc_throw_end [nthrows] = code;
3984 cfg->code_len = code - cfg->native_code;
3986 g_assert (cfg->code_len < cfg->code_size);
3990 mono_arch_flush_icache (guint8 *code, gint size)
3996 mono_arch_flush_register_windows (void)
4001 * Support for fast access to the thread-local lmf structure using the GS
4002 * segment register on NPTL + kernel 2.6.x.
4005 static gboolean tls_offset_inited = FALSE;
4008 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4010 if (!tls_offset_inited) {
4011 if (!getenv ("MONO_NO_TLS")) {
4012 #ifdef PLATFORM_WIN32
4014 * We need to init this multiple times, since when we are first called, the key might not
4015 * be initialized yet.
4017 appdomain_tls_offset = mono_domain_get_tls_key ();
4018 lmf_tls_offset = mono_get_jit_tls_key ();
4019 thread_tls_offset = mono_thread_get_tls_key ();
4021 /* Only 64 tls entries can be accessed using inline code */
4022 if (appdomain_tls_offset >= 64)
4023 appdomain_tls_offset = -1;
4024 if (lmf_tls_offset >= 64)
4025 lmf_tls_offset = -1;
4026 if (thread_tls_offset >= 64)
4027 thread_tls_offset = -1;
4030 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
4032 tls_offset_inited = TRUE;
4033 appdomain_tls_offset = mono_domain_get_tls_offset ();
4034 lmf_tls_offset = mono_get_lmf_tls_offset ();
4035 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
4036 thread_tls_offset = mono_thread_get_tls_offset ();
4043 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4048 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
4050 MonoCallInst *call = (MonoCallInst*)inst;
4051 CallInfo *cinfo = get_call_info (cfg->mempool, inst->signature, FALSE);
4053 /* add the this argument */
4054 if (this_reg != -1) {
4055 if (cinfo->args [0].storage == ArgInIReg) {
4057 MONO_INST_NEW (cfg, this, OP_MOVE);
4058 this->type = this_type;
4059 this->sreg1 = this_reg;
4060 this->dreg = mono_regstate_next_int (cfg->rs);
4061 mono_bblock_add_inst (cfg->cbb, this);
4063 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, cinfo->args [0].reg, FALSE);
4067 MONO_INST_NEW (cfg, this, OP_OUTARG);
4068 this->type = this_type;
4069 this->sreg1 = this_reg;
4070 mono_bblock_add_inst (cfg->cbb, this);
4077 if (cinfo->ret.storage == ArgValuetypeInReg) {
4079 * The valuetype is in EAX:EDX after the call, needs to be copied to
4080 * the stack. Save the address here, so the call instruction can
4083 MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
4084 vtarg->inst_destbasereg = X86_ESP;
4085 vtarg->inst_offset = inst->stack_usage;
4086 vtarg->sreg1 = vt_reg;
4087 mono_bblock_add_inst (cfg->cbb, vtarg);
4089 else if (cinfo->ret.storage == ArgInIReg) {
4090 /* The return address is passed in a register */
4091 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
4092 vtarg->sreg1 = vt_reg;
4093 vtarg->dreg = mono_regstate_next_int (cfg->rs);
4094 mono_bblock_add_inst (cfg->cbb, vtarg);
4096 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
4099 MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
4100 vtarg->type = STACK_MP;
4101 vtarg->sreg1 = vt_reg;
4102 mono_bblock_add_inst (cfg->cbb, vtarg);
4108 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4110 MonoInst *ins = NULL;
4112 if (cmethod->klass == mono_defaults.math_class) {
4113 if (strcmp (cmethod->name, "Sin") == 0) {
4114 MONO_INST_NEW (cfg, ins, OP_SIN);
4115 ins->inst_i0 = args [0];
4116 } else if (strcmp (cmethod->name, "Cos") == 0) {
4117 MONO_INST_NEW (cfg, ins, OP_COS);
4118 ins->inst_i0 = args [0];
4119 } else if (strcmp (cmethod->name, "Tan") == 0) {
4120 MONO_INST_NEW (cfg, ins, OP_TAN);
4121 ins->inst_i0 = args [0];
4122 } else if (strcmp (cmethod->name, "Atan") == 0) {
4123 MONO_INST_NEW (cfg, ins, OP_ATAN);
4124 ins->inst_i0 = args [0];
4125 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
4126 MONO_INST_NEW (cfg, ins, OP_SQRT);
4127 ins->inst_i0 = args [0];
4128 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
4129 MONO_INST_NEW (cfg, ins, OP_ABS);
4130 ins->inst_i0 = args [0];
4133 /* OP_FREM is not IEEE compatible */
4134 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
4135 MONO_INST_NEW (cfg, ins, OP_FREM);
4136 ins->inst_i0 = args [0];
4137 ins->inst_i1 = args [1];
4140 } else if (cmethod->klass == mono_defaults.thread_class &&
4141 strcmp (cmethod->name, "MemoryBarrier") == 0) {
4142 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4143 } else if(cmethod->klass->image == mono_defaults.corlib &&
4144 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
4145 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
4147 if (strcmp (cmethod->name, "Increment") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4148 MonoInst *ins_iconst;
4150 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
4151 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4152 ins_iconst->inst_c0 = 1;
4154 ins->inst_i0 = args [0];
4155 ins->inst_i1 = ins_iconst;
4156 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4157 MonoInst *ins_iconst;
4159 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
4160 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4161 ins_iconst->inst_c0 = -1;
4163 ins->inst_i0 = args [0];
4164 ins->inst_i1 = ins_iconst;
4165 } else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4166 MONO_INST_NEW (cfg, ins, OP_ATOMIC_EXCHANGE_I4);
4168 ins->inst_i0 = args [0];
4169 ins->inst_i1 = args [1];
4170 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
4171 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
4173 ins->inst_i0 = args [0];
4174 ins->inst_i1 = args [1];
4183 mono_arch_print_tree (MonoInst *tree, int arity)
4188 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4192 if (appdomain_tls_offset == -1)
4195 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
4196 ins->inst_offset = appdomain_tls_offset;
4200 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
4204 if (thread_tls_offset == -1)
4207 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
4208 ins->inst_offset = thread_tls_offset;
4213 mono_arch_get_patch_offset (guint8 *code)
4215 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
4217 else if ((code [0] == 0xba))
4219 else if ((code [0] == 0x68))
4222 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
4223 /* push <OFFSET>(<REG>) */
4225 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
4226 /* call *<OFFSET>(<REG>) */
4228 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
4231 else if ((code [0] == 0x58) && (code [1] == 0x05))
4232 /* pop %eax; add <OFFSET>, %eax */
4234 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
4235 /* pop <REG>; add <OFFSET>, <REG> */
4238 g_assert_not_reached ();
4244 mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
4249 /* go to the start of the call instruction
4251 * address_byte = (m << 6) | (o << 3) | reg
4252 * call opcode: 0xff address_byte displacement
4254 * 0xff m=2,o=2 imm32
4259 * A given byte sequence can match more than case here, so we have to be
4260 * really careful about the ordering of the cases. Longer sequences
4263 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)) {
4265 * This is an interface call
4266 * 8b 80 0c e8 ff ff mov 0xffffe80c(%eax),%eax
4267 * ff 10 call *(%eax)
4269 reg = x86_modrm_rm (code [5]);
4271 } else if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
4272 reg = code [4] & 0x07;
4273 disp = (signed char)code [5];
4275 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
4276 reg = code [1] & 0x07;
4277 disp = *((gint32*)(code + 2));
4278 } else if ((code [1] == 0xe8)) {
4280 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
4282 * This is a interface call
4283 * 8b 40 30 mov 0x30(%eax),%eax
4284 * ff 10 call *(%eax)
4287 reg = code [5] & 0x07;
4293 return (gpointer*)(((gint32)(regs [reg])) + disp);
4297 mono_arch_get_this_arg_from_call (MonoMethodSignature *sig, gssize *regs, guint8 *code)
4299 guint32 esp = regs [X86_ESP];
4303 cinfo = get_call_info (NULL, sig, FALSE);
4306 * The stack looks like:
4309 * <possible vtype return address>
4311 * <4 pointers pushed by mono_arch_create_trampoline_code ()>
4313 res = (((MonoObject**)esp) [5 + (cinfo->args [0].offset / 4)]);
4319 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
4321 guint8 *code, *start;
4322 MonoDomain *domain = mono_domain_get ();
4324 /* FIXME: Support more cases */
4325 if (MONO_TYPE_ISSTRUCT (sig->ret))
4329 * The stack contains:
4335 mono_domain_lock (domain);
4336 start = code = mono_code_manager_reserve (domain->code_mp, 64);
4337 mono_domain_unlock (domain);
4339 /* Replace the this argument with the target */
4340 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
4341 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, target), 4);
4342 x86_mov_membase_reg (code, X86_ESP, 4, X86_ECX, 4);
4343 x86_jump_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
4345 g_assert ((code - start) < 64);
4347 if (sig->param_count == 0) {
4348 mono_domain_lock (domain);
4349 start = code = mono_code_manager_reserve (domain->code_mp, 32 + (sig->param_count * 8));
4350 mono_domain_unlock (domain);
4352 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
4353 x86_jump_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
4356 * The code below does not work in the presence of exceptions, since it
4357 * creates a new frame.
4361 for (i = 0; i < sig->param_count; ++i)
4362 if (!mono_is_regsize_var (sig->params [i]))
4365 mono_domain_lock (domain);
4366 start = code = mono_code_manager_reserve (domain->code_mp, 32 + (sig->param_count * 8));
4367 mono_domain_unlock (domain);
4369 /* Load this == delegate */
4370 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
4372 /* Push arguments in opposite order, taking changes in ESP into account */
4373 for (i = 0; i < sig->param_count; ++i)
4374 x86_push_membase (code, X86_ESP, 4 + (sig->param_count * 4));
4376 /* Call the delegate */
4377 x86_call_membase (code, X86_EAX, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
4378 if (sig->param_count > 0)
4379 x86_alu_reg_imm (code, X86_ADD, X86_ESP, sig->param_count * 4);