2 * mini-x86.c: x86 backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2003 Ximian, Inc.
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/profiler-private.h>
20 #include <mono/utils/mono-math.h>
27 /* On windows, these hold the key returned by TlsAlloc () */
28 static gint lmf_tls_offset = -1;
29 static gint lmf_addr_tls_offset = -1;
30 static gint appdomain_tls_offset = -1;
31 static gint thread_tls_offset = -1;
34 static gboolean optimize_for_xen = TRUE;
36 #define optimize_for_xen 0
40 static gboolean is_win32 = TRUE;
42 static gboolean is_win32 = FALSE;
45 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
50 /* Under windows, the default pinvoke calling convention is stdcall */
51 #define CALLCONV_IS_STDCALL(sig) ((((sig)->call_convention) == MONO_CALL_STDCALL) || ((sig)->pinvoke && ((sig)->call_convention) == MONO_CALL_DEFAULT))
53 #define CALLCONV_IS_STDCALL(sig) (((sig)->call_convention) == MONO_CALL_STDCALL)
56 #define NOT_IMPLEMENTED g_assert_not_reached ()
59 mono_arch_regname (int reg) {
61 case X86_EAX: return "%eax";
62 case X86_EBX: return "%ebx";
63 case X86_ECX: return "%ecx";
64 case X86_EDX: return "%edx";
65 case X86_ESP: return "%esp"; case X86_EBP: return "%ebp";
66 case X86_EDI: return "%edi";
67 case X86_ESI: return "%esi";
73 mono_arch_fregname (int reg) {
93 /* Only if storage == ArgValuetypeInReg */
94 ArgStorage pair_storage [2];
103 gboolean need_stack_align;
104 guint32 stack_align_amount;
112 #define FLOAT_PARAM_REGS 0
114 static X86_Reg_No param_regs [] = { 0 };
116 #ifdef PLATFORM_WIN32
117 static X86_Reg_No return_regs [] = { X86_EAX, X86_EDX };
121 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
123 ainfo->offset = *stack_size;
125 if (*gr >= PARAM_REGS) {
126 ainfo->storage = ArgOnStack;
127 (*stack_size) += sizeof (gpointer);
130 ainfo->storage = ArgInIReg;
131 ainfo->reg = param_regs [*gr];
137 add_general_pair (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo)
139 ainfo->offset = *stack_size;
141 g_assert (PARAM_REGS == 0);
143 ainfo->storage = ArgOnStack;
144 (*stack_size) += sizeof (gpointer) * 2;
148 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean is_double)
150 ainfo->offset = *stack_size;
152 if (*gr >= FLOAT_PARAM_REGS) {
153 ainfo->storage = ArgOnStack;
154 (*stack_size) += is_double ? 8 : 4;
157 /* A double register */
159 ainfo->storage = ArgInDoubleSSEReg;
161 ainfo->storage = ArgInFloatSSEReg;
169 add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type,
171 guint32 *gr, guint32 *fr, guint32 *stack_size)
176 klass = mono_class_from_mono_type (type);
178 size = mono_type_native_stack_size (&klass->byval_arg, NULL);
180 size = mono_type_stack_size (&klass->byval_arg, NULL);
182 #ifdef PLATFORM_WIN32
183 if (sig->pinvoke && is_return) {
184 MonoMarshalType *info;
187 * the exact rules are not very well documented, the code below seems to work with the
188 * code generated by gcc 3.3.3 -mno-cygwin.
190 info = mono_marshal_load_type_info (klass);
193 ainfo->pair_storage [0] = ainfo->pair_storage [1] = ArgNone;
195 /* Special case structs with only a float member */
196 if ((info->native_size == 8) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R8)) {
197 ainfo->storage = ArgValuetypeInReg;
198 ainfo->pair_storage [0] = ArgOnDoubleFpStack;
201 if ((info->native_size == 4) && (info->num_fields == 1) && (info->fields [0].field->type->type == MONO_TYPE_R4)) {
202 ainfo->storage = ArgValuetypeInReg;
203 ainfo->pair_storage [0] = ArgOnFloatFpStack;
206 if ((info->native_size == 1) || (info->native_size == 2) || (info->native_size == 4) || (info->native_size == 8)) {
207 ainfo->storage = ArgValuetypeInReg;
208 ainfo->pair_storage [0] = ArgInIReg;
209 ainfo->pair_regs [0] = return_regs [0];
210 if (info->native_size > 4) {
211 ainfo->pair_storage [1] = ArgInIReg;
212 ainfo->pair_regs [1] = return_regs [1];
219 ainfo->offset = *stack_size;
220 ainfo->storage = ArgOnStack;
221 *stack_size += ALIGN_TO (size, sizeof (gpointer));
227 * Obtain information about a call according to the calling convention.
228 * For x86 ELF, see the "System V Application Binary Interface Intel386
229 * Architecture Processor Supplment, Fourth Edition" document for more
231 * For x86 win32, see ???.
234 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
238 int n = sig->hasthis + sig->param_count;
239 guint32 stack_size = 0;
242 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
249 ret_type = mono_type_get_underlying_type (sig->ret);
250 switch (ret_type->type) {
251 case MONO_TYPE_BOOLEAN:
262 case MONO_TYPE_FNPTR:
263 case MONO_TYPE_CLASS:
264 case MONO_TYPE_OBJECT:
265 case MONO_TYPE_SZARRAY:
266 case MONO_TYPE_ARRAY:
267 case MONO_TYPE_STRING:
268 cinfo->ret.storage = ArgInIReg;
269 cinfo->ret.reg = X86_EAX;
273 cinfo->ret.storage = ArgInIReg;
274 cinfo->ret.reg = X86_EAX;
277 cinfo->ret.storage = ArgOnFloatFpStack;
280 cinfo->ret.storage = ArgOnDoubleFpStack;
282 case MONO_TYPE_GENERICINST:
283 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
284 cinfo->ret.storage = ArgInIReg;
285 cinfo->ret.reg = X86_EAX;
289 case MONO_TYPE_VALUETYPE: {
290 guint32 tmp_gr = 0, tmp_fr = 0, tmp_stacksize = 0;
292 add_valuetype (sig, &cinfo->ret, sig->ret, TRUE, &tmp_gr, &tmp_fr, &tmp_stacksize);
293 if (cinfo->ret.storage == ArgOnStack)
294 /* The caller passes the address where the value is stored */
295 add_general (&gr, &stack_size, &cinfo->ret);
298 case MONO_TYPE_TYPEDBYREF:
299 /* Same as a valuetype with size 24 */
300 add_general (&gr, &stack_size, &cinfo->ret);
304 cinfo->ret.storage = ArgNone;
307 g_error ("Can't handle as return value 0x%x", sig->ret->type);
313 add_general (&gr, &stack_size, cinfo->args + 0);
315 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
317 fr = FLOAT_PARAM_REGS;
319 /* Emit the signature cookie just before the implicit arguments */
320 add_general (&gr, &stack_size, &cinfo->sig_cookie);
323 for (i = 0; i < sig->param_count; ++i) {
324 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
327 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
328 /* We allways pass the sig cookie on the stack for simplicity */
330 * Prevent implicit arguments + the sig cookie from being passed
334 fr = FLOAT_PARAM_REGS;
336 /* Emit the signature cookie just before the implicit arguments */
337 add_general (&gr, &stack_size, &cinfo->sig_cookie);
340 if (sig->params [i]->byref) {
341 add_general (&gr, &stack_size, ainfo);
344 ptype = mono_type_get_underlying_type (sig->params [i]);
345 switch (ptype->type) {
346 case MONO_TYPE_BOOLEAN:
349 add_general (&gr, &stack_size, ainfo);
354 add_general (&gr, &stack_size, ainfo);
358 add_general (&gr, &stack_size, ainfo);
363 case MONO_TYPE_FNPTR:
364 case MONO_TYPE_CLASS:
365 case MONO_TYPE_OBJECT:
366 case MONO_TYPE_STRING:
367 case MONO_TYPE_SZARRAY:
368 case MONO_TYPE_ARRAY:
369 add_general (&gr, &stack_size, ainfo);
371 case MONO_TYPE_GENERICINST:
372 if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
373 add_general (&gr, &stack_size, ainfo);
377 case MONO_TYPE_VALUETYPE:
378 add_valuetype (sig, ainfo, sig->params [i], FALSE, &gr, &fr, &stack_size);
380 case MONO_TYPE_TYPEDBYREF:
381 stack_size += sizeof (MonoTypedRef);
382 ainfo->storage = ArgOnStack;
386 add_general_pair (&gr, &stack_size, ainfo);
389 add_float (&fr, &stack_size, ainfo, FALSE);
392 add_float (&fr, &stack_size, ainfo, TRUE);
395 g_error ("unexpected type 0x%x", ptype->type);
396 g_assert_not_reached ();
400 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
402 fr = FLOAT_PARAM_REGS;
404 /* Emit the signature cookie just before the implicit arguments */
405 add_general (&gr, &stack_size, &cinfo->sig_cookie);
408 #if defined(__APPLE__)
409 if ((stack_size % 16) != 0) {
410 cinfo->need_stack_align = TRUE;
411 stack_size += cinfo->stack_align_amount = 16-(stack_size % 16);
415 cinfo->stack_usage = stack_size;
416 cinfo->reg_usage = gr;
417 cinfo->freg_usage = fr;
422 * mono_arch_get_argument_info:
423 * @csig: a method signature
424 * @param_count: the number of parameters to consider
425 * @arg_info: an array to store the result infos
427 * Gathers information on parameters such as size, alignment and
428 * padding. arg_info should be large enought to hold param_count + 1 entries.
430 * Returns the size of the activation frame.
433 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
435 int k, frame_size = 0;
441 cinfo = get_call_info (csig, FALSE);
443 if (MONO_TYPE_ISSTRUCT (csig->ret) && (cinfo->ret.storage == ArgOnStack)) {
444 frame_size += sizeof (gpointer);
448 arg_info [0].offset = offset;
451 frame_size += sizeof (gpointer);
455 arg_info [0].size = frame_size;
457 for (k = 0; k < param_count; k++) {
460 size = mono_type_native_stack_size (csig->params [k], &align);
463 size = mono_type_stack_size (csig->params [k], &ialign);
467 /* ignore alignment for now */
470 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
471 arg_info [k].pad = pad;
473 arg_info [k + 1].pad = 0;
474 arg_info [k + 1].size = size;
476 arg_info [k + 1].offset = offset;
480 align = MONO_ARCH_FRAME_ALIGNMENT;
481 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
482 arg_info [k].pad = pad;
489 static const guchar cpuid_impl [] = {
490 0x55, /* push %ebp */
491 0x89, 0xe5, /* mov %esp,%ebp */
492 0x53, /* push %ebx */
493 0x8b, 0x45, 0x08, /* mov 0x8(%ebp),%eax */
494 0x0f, 0xa2, /* cpuid */
495 0x50, /* push %eax */
496 0x8b, 0x45, 0x10, /* mov 0x10(%ebp),%eax */
497 0x89, 0x18, /* mov %ebx,(%eax) */
498 0x8b, 0x45, 0x14, /* mov 0x14(%ebp),%eax */
499 0x89, 0x08, /* mov %ecx,(%eax) */
500 0x8b, 0x45, 0x18, /* mov 0x18(%ebp),%eax */
501 0x89, 0x10, /* mov %edx,(%eax) */
503 0x8b, 0x55, 0x0c, /* mov 0xc(%ebp),%edx */
504 0x89, 0x02, /* mov %eax,(%edx) */
510 typedef void (*CpuidFunc) (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx);
513 cpuid (int id, int* p_eax, int* p_ebx, int* p_ecx, int* p_edx)
517 __asm__ __volatile__ (
520 "movl %%eax, %%edx\n"
521 "xorl $0x200000, %%eax\n"
526 "xorl %%edx, %%eax\n"
527 "andl $0x200000, %%eax\n"
549 /* Have to use the code manager to get around WinXP DEP */
550 MonoCodeManager *codeman = mono_code_manager_new_dynamic ();
552 void *ptr = mono_code_manager_reserve (codeman, sizeof (cpuid_impl));
553 memcpy (ptr, cpuid_impl, sizeof (cpuid_impl));
555 func = (CpuidFunc)ptr;
556 func (id, p_eax, p_ebx, p_ecx, p_edx);
558 mono_code_manager_destroy (codeman);
561 * We use this approach because of issues with gcc and pic code, see:
562 * http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7329
563 __asm__ __volatile__ ("cpuid"
564 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
573 * Initialize the cpu to execute managed code.
576 mono_arch_cpu_init (void)
578 /* spec compliance requires running with double precision */
582 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
583 fpcw &= ~X86_FPCW_PRECC_MASK;
584 fpcw |= X86_FPCW_PREC_DOUBLE;
585 __asm__ __volatile__ ("fldcw %0\n": : "m" (fpcw));
586 __asm__ __volatile__ ("fnstcw %0\n": "=m" (fpcw));
588 _control87 (_PC_53, MCW_PC);
593 * This function returns the optimizations supported on this cpu.
596 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
598 int eax, ebx, ecx, edx;
602 /* Feature Flags function, flags returned in EDX. */
603 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
604 if (edx & (1 << 15)) {
605 opts |= MONO_OPT_CMOV;
607 opts |= MONO_OPT_FCMOV;
609 *exclude_mask |= MONO_OPT_FCMOV;
611 *exclude_mask |= MONO_OPT_CMOV;
617 * Determine whenever the trap whose info is in SIGINFO is caused by
621 mono_arch_is_int_overflow (void *sigctx, void *info)
626 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
628 ip = (guint8*)ctx.eip;
630 if ((ip [0] == 0xf7) && (x86_modrm_mod (ip [1]) == 0x3) && (x86_modrm_reg (ip [1]) == 0x7)) {
634 switch (x86_modrm_rm (ip [1])) {
654 g_assert_not_reached ();
666 is_regsize_var (MonoType *t) {
669 switch (mono_type_get_underlying_type (t)->type) {
675 case MONO_TYPE_FNPTR:
677 case MONO_TYPE_OBJECT:
678 case MONO_TYPE_STRING:
679 case MONO_TYPE_CLASS:
680 case MONO_TYPE_SZARRAY:
681 case MONO_TYPE_ARRAY:
683 case MONO_TYPE_GENERICINST:
684 if (!mono_type_generic_inst_is_valuetype (t))
687 case MONO_TYPE_VALUETYPE:
694 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
699 for (i = 0; i < cfg->num_varinfo; i++) {
700 MonoInst *ins = cfg->varinfo [i];
701 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
704 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
707 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) ||
708 (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
711 /* we dont allocate I1 to registers because there is no simply way to sign extend
712 * 8bit quantities in caller saved registers on x86 */
713 if (is_regsize_var (ins->inst_vtype) || (ins->inst_vtype->type == MONO_TYPE_BOOLEAN) ||
714 (ins->inst_vtype->type == MONO_TYPE_U1) || (ins->inst_vtype->type == MONO_TYPE_U2)||
715 (ins->inst_vtype->type == MONO_TYPE_I2) || (ins->inst_vtype->type == MONO_TYPE_CHAR)) {
716 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
717 g_assert (i == vmv->idx);
718 vars = g_list_prepend (vars, vmv);
722 vars = mono_varlist_sort (cfg, vars, 0);
728 mono_arch_get_global_int_regs (MonoCompile *cfg)
732 /* we can use 3 registers for global allocation */
733 regs = g_list_prepend (regs, (gpointer)X86_EBX);
734 regs = g_list_prepend (regs, (gpointer)X86_ESI);
735 regs = g_list_prepend (regs, (gpointer)X86_EDI);
741 * mono_arch_regalloc_cost:
743 * Return the cost, in number of memory references, of the action of
744 * allocating the variable VMV into a register during global register
748 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
750 MonoInst *ins = cfg->varinfo [vmv->idx];
752 if (cfg->method->save_lmf)
753 /* The register is already saved */
754 return (ins->opcode == OP_ARG) ? 1 : 0;
756 /* push+pop+possible load if it is an argument */
757 return (ins->opcode == OP_ARG) ? 3 : 2;
761 * Set var information according to the calling convention. X86 version.
762 * The locals var stuff should most likely be split in another method.
765 mono_arch_allocate_vars (MonoCompile *cfg)
767 MonoMethodSignature *sig;
768 MonoMethodHeader *header;
770 guint32 locals_stack_size, locals_stack_align;
775 header = mono_method_get_header (cfg->method);
776 sig = mono_method_signature (cfg->method);
778 cinfo = get_call_info (sig, FALSE);
780 cfg->frame_reg = MONO_ARCH_BASEREG;
783 /* Reserve space to save LMF and caller saved registers */
785 if (cfg->method->save_lmf) {
786 offset += sizeof (MonoLMF);
788 if (cfg->used_int_regs & (1 << X86_EBX)) {
792 if (cfg->used_int_regs & (1 << X86_EDI)) {
796 if (cfg->used_int_regs & (1 << X86_ESI)) {
801 switch (cinfo->ret.storage) {
802 case ArgValuetypeInReg:
803 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
805 cfg->ret->opcode = OP_REGOFFSET;
806 cfg->ret->inst_basereg = X86_EBP;
807 cfg->ret->inst_offset = - offset;
813 /* Allocate locals */
814 offsets = mono_allocate_stack_slots (cfg, &locals_stack_size, &locals_stack_align);
815 if (locals_stack_align) {
816 offset += (locals_stack_align - 1);
817 offset &= ~(locals_stack_align - 1);
819 for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
820 if (offsets [i] != -1) {
821 MonoInst *inst = cfg->varinfo [i];
822 inst->opcode = OP_REGOFFSET;
823 inst->inst_basereg = X86_EBP;
824 inst->inst_offset = - (offset + offsets [i]);
825 //printf ("allocated local %d to ", i); mono_print_tree_nl (inst);
829 offset += locals_stack_size;
833 * Allocate arguments+return value
836 switch (cinfo->ret.storage) {
838 cfg->ret->opcode = OP_REGOFFSET;
839 cfg->ret->inst_basereg = X86_EBP;
840 cfg->ret->inst_offset = cinfo->ret.offset + ARGS_OFFSET;
842 case ArgValuetypeInReg:
845 cfg->ret->opcode = OP_REGVAR;
846 cfg->ret->inst_c0 = cinfo->ret.reg;
849 case ArgOnFloatFpStack:
850 case ArgOnDoubleFpStack:
853 g_assert_not_reached ();
856 if (sig->call_convention == MONO_CALL_VARARG) {
857 g_assert (cinfo->sig_cookie.storage == ArgOnStack);
858 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
861 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
862 ArgInfo *ainfo = &cinfo->args [i];
863 inst = cfg->varinfo [i];
864 if (inst->opcode != OP_REGVAR) {
865 inst->opcode = OP_REGOFFSET;
866 inst->inst_basereg = X86_EBP;
868 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
871 offset += (MONO_ARCH_FRAME_ALIGNMENT - 1);
872 offset &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
874 cfg->stack_offset = offset;
880 mono_arch_create_vars (MonoCompile *cfg)
882 MonoMethodSignature *sig;
885 sig = mono_method_signature (cfg->method);
887 cinfo = get_call_info (sig, FALSE);
889 if (cinfo->ret.storage == ArgValuetypeInReg)
890 cfg->ret_var_is_local = TRUE;
895 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
896 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
900 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call)
903 MonoMethodSignature *tmp_sig;
906 /* FIXME: Add support for signature tokens to AOT */
907 cfg->disable_aot = TRUE;
908 MONO_INST_NEW (cfg, arg, OP_OUTARG);
911 * mono_ArgIterator_Setup assumes the signature cookie is
912 * passed first and all the arguments which were before it are
913 * passed on the stack after the signature. So compensate by
914 * passing a different signature.
916 tmp_sig = mono_metadata_signature_dup (call->signature);
917 tmp_sig->param_count -= call->signature->sentinelpos;
918 tmp_sig->sentinelpos = 0;
919 memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
921 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
922 sig_arg->inst_p0 = tmp_sig;
924 arg->inst_left = sig_arg;
925 arg->type = STACK_PTR;
926 /* prepend, so they get reversed */
927 arg->next = call->out_args;
928 call->out_args = arg;
932 * take the arguments and generate the arch-specific
933 * instructions to properly call the function in call.
934 * This includes pushing, moving arguments to the right register
938 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
940 MonoMethodSignature *sig;
945 sig = call->signature;
946 n = sig->param_count + sig->hasthis;
948 cinfo = get_call_info (sig, FALSE);
950 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
951 sentinelpos = sig->sentinelpos + (is_virtual ? 1 : 0);
953 for (i = 0; i < n; ++i) {
954 ArgInfo *ainfo = cinfo->args + i;
956 /* Emit the signature cookie just before the implicit arguments */
957 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (i == sentinelpos)) {
958 emit_sig_cookie (cfg, call);
961 if (is_virtual && i == 0) {
962 /* the argument will be attached to the call instrucion */
967 if (i >= sig->hasthis)
968 t = sig->params [i - sig->hasthis];
970 t = &mono_defaults.int_class->byval_arg;
971 t = mono_type_get_underlying_type (t);
973 MONO_INST_NEW (cfg, arg, OP_OUTARG);
975 arg->cil_code = in->cil_code;
977 arg->type = in->type;
978 /* prepend, so they get reversed */
979 arg->next = call->out_args;
980 call->out_args = arg;
982 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(t))) {
985 if (t->type == MONO_TYPE_TYPEDBYREF) {
986 size = sizeof (MonoTypedRef);
987 align = sizeof (gpointer);
991 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
994 size = mono_type_stack_size (&in->klass->byval_arg, &ialign);
997 arg->opcode = OP_OUTARG_VT;
998 arg->klass = in->klass;
999 arg->backend.is_pinvoke = sig->pinvoke;
1000 arg->inst_imm = size;
1003 switch (ainfo->storage) {
1005 arg->opcode = OP_OUTARG;
1007 if (t->type == MONO_TYPE_R4)
1008 arg->opcode = OP_OUTARG_R4;
1010 if (t->type == MONO_TYPE_R8)
1011 arg->opcode = OP_OUTARG_R8;
1015 g_assert_not_reached ();
1021 /* Handle the case where there are no implicit arguments */
1022 if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sentinelpos)) {
1023 emit_sig_cookie (cfg, call);
1026 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1027 if (cinfo->ret.storage == ArgValuetypeInReg) {
1028 MonoInst *zero_inst;
1030 * After the call, the struct is in registers, but needs to be saved to the memory pointed
1031 * to by vt_arg in this_vret_args. This means that vt_arg needs to be saved somewhere
1032 * before calling the function. So we add a dummy instruction to represent pushing the
1033 * struct return address to the stack. The return address will be saved to this stack slot
1034 * by the code emitted in this_vret_args.
1036 MONO_INST_NEW (cfg, arg, OP_OUTARG);
1037 MONO_INST_NEW (cfg, zero_inst, OP_ICONST);
1038 zero_inst->inst_p0 = 0;
1039 arg->inst_left = zero_inst;
1040 arg->type = STACK_PTR;
1041 /* prepend, so they get reversed */
1042 arg->next = call->out_args;
1043 call->out_args = arg;
1046 /* if the function returns a struct, the called method already does a ret $0x4 */
1047 if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret))
1048 cinfo->stack_usage -= 4;
1051 call->stack_usage = cinfo->stack_usage;
1053 #if defined(__APPLE__)
1054 if (cinfo->need_stack_align) {
1055 MONO_INST_NEW (cfg, arg, OP_X86_OUTARG_ALIGN_STACK);
1056 arg->inst_c0 = cinfo->stack_align_amount;
1057 arg->next = call->out_args;
1058 call->out_args = arg;
1068 * Allow tracing to work with this interface (with an optional argument)
1071 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1075 /* if some args are passed in registers, we need to save them here */
1076 x86_push_reg (code, X86_EBP);
1078 if (cfg->compile_aot) {
1079 x86_push_imm (code, cfg->method);
1080 x86_mov_reg_imm (code, X86_EAX, func);
1081 x86_call_reg (code, X86_EAX);
1083 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, cfg->method);
1084 x86_push_imm (code, cfg->method);
1085 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1086 x86_call_code (code, 0);
1088 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1102 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1105 int arg_size = 0, save_mode = SAVE_NONE;
1106 MonoMethod *method = cfg->method;
1108 switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
1109 case MONO_TYPE_VOID:
1110 /* special case string .ctor icall */
1111 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1112 save_mode = SAVE_EAX;
1114 save_mode = SAVE_NONE;
1118 save_mode = SAVE_EAX_EDX;
1122 save_mode = SAVE_FP;
1124 case MONO_TYPE_GENERICINST:
1125 if (!mono_type_generic_inst_is_valuetype (mono_method_signature (method)->ret)) {
1126 save_mode = SAVE_EAX;
1130 case MONO_TYPE_VALUETYPE:
1131 save_mode = SAVE_STRUCT;
1134 save_mode = SAVE_EAX;
1138 switch (save_mode) {
1140 x86_push_reg (code, X86_EDX);
1141 x86_push_reg (code, X86_EAX);
1142 if (enable_arguments) {
1143 x86_push_reg (code, X86_EDX);
1144 x86_push_reg (code, X86_EAX);
1149 x86_push_reg (code, X86_EAX);
1150 if (enable_arguments) {
1151 x86_push_reg (code, X86_EAX);
1156 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1157 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1158 if (enable_arguments) {
1159 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1160 x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
1165 if (enable_arguments) {
1166 x86_push_membase (code, X86_EBP, 8);
1175 if (cfg->compile_aot) {
1176 x86_push_imm (code, method);
1177 x86_mov_reg_imm (code, X86_EAX, func);
1178 x86_call_reg (code, X86_EAX);
1180 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_METHODCONST, method);
1181 x86_push_imm (code, method);
1182 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1183 x86_call_code (code, 0);
1185 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1187 switch (save_mode) {
1189 x86_pop_reg (code, X86_EAX);
1190 x86_pop_reg (code, X86_EDX);
1193 x86_pop_reg (code, X86_EAX);
1196 x86_fld_membase (code, X86_ESP, 0, TRUE);
1197 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1207 #define EMIT_COND_BRANCH(ins,cond,sign) \
1208 if (ins->flags & MONO_INST_BRLABEL) { \
1209 if (ins->inst_i0->inst_c0) { \
1210 x86_branch (code, cond, cfg->native_code + ins->inst_i0->inst_c0, sign); \
1212 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1213 if ((cfg->opt & MONO_OPT_BRANCH) && \
1214 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos)) \
1215 x86_branch8 (code, cond, 0, sign); \
1217 x86_branch32 (code, cond, 0, sign); \
1220 if (ins->inst_true_bb->native_offset) { \
1221 x86_branch (code, cond, cfg->native_code + ins->inst_true_bb->native_offset, sign); \
1223 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1224 if ((cfg->opt & MONO_OPT_BRANCH) && \
1225 x86_is_imm8 (ins->inst_true_bb->max_offset - cpos)) \
1226 x86_branch8 (code, cond, 0, sign); \
1228 x86_branch32 (code, cond, 0, sign); \
1233 * Emit an exception if condition is fail and
1234 * if possible do a directly branch to target
1236 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1238 MonoInst *tins = mono_branch_optimize_exception_target (cfg, bb, exc_name); \
1239 if (tins == NULL) { \
1240 mono_add_patch_info (cfg, code - cfg->native_code, \
1241 MONO_PATCH_INFO_EXC, exc_name); \
1242 x86_branch32 (code, cond, 0, signed); \
1244 EMIT_COND_BRANCH (tins, cond, signed); \
1248 #define EMIT_FPCOMPARE(code) do { \
1249 x86_fcompp (code); \
1250 x86_fnstsw (code); \
1255 emit_call (MonoCompile *cfg, guint8 *code, guint32 patch_type, gconstpointer data)
1257 mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
1258 x86_call_code (code, 0);
1263 /* FIXME: Add more instructions */
1264 #define INST_IGNORES_CFLAGS(ins) (((ins)->opcode == CEE_BR) || ((ins)->opcode == OP_STORE_MEMBASE_IMM) || ((ins)->opcode == OP_STOREI4_MEMBASE_REG))
1267 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1269 MonoInst *ins, *last_ins = NULL;
1274 switch (ins->opcode) {
1276 /* reg = 0 -> XOR (reg, reg) */
1277 /* XOR sets cflags on x86, so we cant do it always */
1278 if (ins->inst_c0 == 0 && ins->next && INST_IGNORES_CFLAGS (ins->next)) {
1279 ins->opcode = CEE_XOR;
1280 ins->sreg1 = ins->dreg;
1281 ins->sreg2 = ins->dreg;
1285 /* remove unnecessary multiplication with 1 */
1286 if (ins->inst_imm == 1) {
1287 if (ins->dreg != ins->sreg1) {
1288 ins->opcode = OP_MOVE;
1290 last_ins->next = ins->next;
1296 case OP_COMPARE_IMM:
1297 /* OP_COMPARE_IMM (reg, 0)
1299 * OP_X86_TEST_NULL (reg)
1302 ins->opcode = OP_X86_TEST_NULL;
1304 case OP_X86_COMPARE_MEMBASE_IMM:
1306 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1307 * OP_X86_COMPARE_MEMBASE_IMM offset(basereg), imm
1309 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1310 * OP_COMPARE_IMM reg, imm
1312 * Note: if imm = 0 then OP_COMPARE_IMM replaced with OP_X86_TEST_NULL
1314 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG) &&
1315 ins->inst_basereg == last_ins->inst_destbasereg &&
1316 ins->inst_offset == last_ins->inst_offset) {
1317 ins->opcode = OP_COMPARE_IMM;
1318 ins->sreg1 = last_ins->sreg1;
1320 /* check if we can remove cmp reg,0 with test null */
1322 ins->opcode = OP_X86_TEST_NULL;
1326 case OP_LOAD_MEMBASE:
1327 case OP_LOADI4_MEMBASE:
1329 * Note: if reg1 = reg2 the load op is removed
1331 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1332 * OP_LOAD_MEMBASE offset(basereg), reg2
1334 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1335 * OP_MOVE reg1, reg2
1337 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1338 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1339 ins->inst_basereg == last_ins->inst_destbasereg &&
1340 ins->inst_offset == last_ins->inst_offset) {
1341 if (ins->dreg == last_ins->sreg1) {
1342 last_ins->next = ins->next;
1346 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1347 ins->opcode = OP_MOVE;
1348 ins->sreg1 = last_ins->sreg1;
1352 * Note: reg1 must be different from the basereg in the second load
1353 * Note: if reg1 = reg2 is equal then second load is removed
1355 * OP_LOAD_MEMBASE offset(basereg), reg1
1356 * OP_LOAD_MEMBASE offset(basereg), reg2
1358 * OP_LOAD_MEMBASE offset(basereg), reg1
1359 * OP_MOVE reg1, reg2
1361 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1362 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1363 ins->inst_basereg != last_ins->dreg &&
1364 ins->inst_basereg == last_ins->inst_basereg &&
1365 ins->inst_offset == last_ins->inst_offset) {
1367 if (ins->dreg == last_ins->dreg) {
1368 last_ins->next = ins->next;
1372 ins->opcode = OP_MOVE;
1373 ins->sreg1 = last_ins->dreg;
1376 //g_assert_not_reached ();
1380 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1381 * OP_LOAD_MEMBASE offset(basereg), reg
1383 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1384 * OP_ICONST reg, imm
1386 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1387 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1388 ins->inst_basereg == last_ins->inst_destbasereg &&
1389 ins->inst_offset == last_ins->inst_offset) {
1390 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1391 ins->opcode = OP_ICONST;
1392 ins->inst_c0 = last_ins->inst_imm;
1393 g_assert_not_reached (); // check this rule
1397 case OP_LOADU1_MEMBASE:
1398 case OP_LOADI1_MEMBASE:
1400 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1401 * OP_LOAD_MEMBASE offset(basereg), reg2
1403 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1404 * CONV_I2/U2 reg1, reg2
1406 if (last_ins && X86_IS_BYTE_REG (last_ins->sreg1) &&
1407 (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1408 ins->inst_basereg == last_ins->inst_destbasereg &&
1409 ins->inst_offset == last_ins->inst_offset) {
1410 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? CEE_CONV_I1 : CEE_CONV_U1;
1411 ins->sreg1 = last_ins->sreg1;
1414 case OP_LOADU2_MEMBASE:
1415 case OP_LOADI2_MEMBASE:
1417 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1418 * OP_LOAD_MEMBASE offset(basereg), reg2
1420 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
1421 * CONV_I2/U2 reg1, reg2
1423 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1424 ins->inst_basereg == last_ins->inst_destbasereg &&
1425 ins->inst_offset == last_ins->inst_offset) {
1426 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? CEE_CONV_I2 : CEE_CONV_U2;
1427 ins->sreg1 = last_ins->sreg1;
1438 if (ins->dreg == ins->sreg1) {
1440 last_ins->next = ins->next;
1447 * OP_MOVE sreg, dreg
1448 * OP_MOVE dreg, sreg
1450 if (last_ins && last_ins->opcode == OP_MOVE &&
1451 ins->sreg1 == last_ins->dreg &&
1452 ins->dreg == last_ins->sreg1) {
1453 last_ins->next = ins->next;
1459 case OP_X86_PUSH_MEMBASE:
1460 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG ||
1461 last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1462 ins->inst_basereg == last_ins->inst_destbasereg &&
1463 ins->inst_offset == last_ins->inst_offset) {
1464 ins->opcode = OP_X86_PUSH;
1465 ins->sreg1 = last_ins->sreg1;
1472 bb->last_ins = last_ins;
1476 branch_cc_table [] = {
1477 X86_CC_EQ, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1478 X86_CC_NE, X86_CC_GE, X86_CC_GT, X86_CC_LE, X86_CC_LT,
1479 X86_CC_O, X86_CC_NO, X86_CC_C, X86_CC_NC
1482 static const char*const * ins_spec = x86_desc;
1484 /*#include "cprop.c"*/
1486 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1488 mono_local_regalloc (cfg, bb);
1491 static unsigned char*
1492 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1494 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
1495 x86_fnstcw_membase(code, X86_ESP, 0);
1496 x86_mov_reg_membase (code, dreg, X86_ESP, 0, 2);
1497 x86_alu_reg_imm (code, X86_OR, dreg, 0xc00);
1498 x86_mov_membase_reg (code, X86_ESP, 2, dreg, 2);
1499 x86_fldcw_membase (code, X86_ESP, 2);
1501 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
1502 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
1503 x86_pop_reg (code, dreg);
1504 /* FIXME: need the high register
1505 * x86_pop_reg (code, dreg_high);
1508 x86_push_reg (code, X86_EAX); // SP = SP - 4
1509 x86_fist_pop_membase (code, X86_ESP, 0, FALSE);
1510 x86_pop_reg (code, dreg);
1512 x86_fldcw_membase (code, X86_ESP, 0);
1513 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
1516 x86_widen_reg (code, dreg, dreg, is_signed, FALSE);
1518 x86_widen_reg (code, dreg, dreg, is_signed, TRUE);
1522 static unsigned char*
1523 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1525 int sreg = tree->sreg1;
1526 int need_touch = FALSE;
1528 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
1537 * If requested stack size is larger than one page,
1538 * perform stack-touch operation
1541 * Generate stack probe code.
1542 * Under Windows, it is necessary to allocate one page at a time,
1543 * "touching" stack after each successful sub-allocation. This is
1544 * because of the way stack growth is implemented - there is a
1545 * guard page before the lowest stack page that is currently commited.
1546 * Stack normally grows sequentially so OS traps access to the
1547 * guard page and commits more pages when needed.
1549 x86_test_reg_imm (code, sreg, ~0xFFF);
1550 br[0] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1552 br[2] = code; /* loop */
1553 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
1554 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
1557 * By the end of the loop, sreg2 is smaller than 0x1000, so the init routine
1558 * that follows only initializes the last part of the area.
1560 /* Same as the init code below with size==0x1000 */
1561 if (tree->flags & MONO_INST_INIT) {
1562 x86_push_reg (code, X86_EAX);
1563 x86_push_reg (code, X86_ECX);
1564 x86_push_reg (code, X86_EDI);
1565 x86_mov_reg_imm (code, X86_ECX, (0x1000 >> 2));
1566 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1567 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
1569 x86_prefix (code, X86_REP_PREFIX);
1571 x86_pop_reg (code, X86_EDI);
1572 x86_pop_reg (code, X86_ECX);
1573 x86_pop_reg (code, X86_EAX);
1576 x86_alu_reg_imm (code, X86_SUB, sreg, 0x1000);
1577 x86_alu_reg_imm (code, X86_CMP, sreg, 0x1000);
1578 br[3] = code; x86_branch8 (code, X86_CC_AE, 0, FALSE);
1579 x86_patch (br[3], br[2]);
1580 x86_test_reg_reg (code, sreg, sreg);
1581 br[4] = code; x86_branch8 (code, X86_CC_Z, 0, FALSE);
1582 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1584 br[1] = code; x86_jump8 (code, 0);
1586 x86_patch (br[0], code);
1587 x86_alu_reg_reg (code, X86_SUB, X86_ESP, sreg);
1588 x86_patch (br[1], code);
1589 x86_patch (br[4], code);
1592 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1594 if (tree->flags & MONO_INST_INIT) {
1596 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1597 x86_push_reg (code, X86_EAX);
1600 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1601 x86_push_reg (code, X86_ECX);
1604 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1605 x86_push_reg (code, X86_EDI);
1609 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1610 if (sreg != X86_ECX)
1611 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1612 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1614 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1616 x86_prefix (code, X86_REP_PREFIX);
1619 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1620 x86_pop_reg (code, X86_EDI);
1621 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1622 x86_pop_reg (code, X86_ECX);
1623 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1624 x86_pop_reg (code, X86_EAX);
1631 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
1636 /* Move return value to the target register */
1637 switch (ins->opcode) {
1640 case OP_CALL_MEMBASE:
1641 if (ins->dreg != X86_EAX)
1642 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
1646 case OP_VCALL_MEMBASE:
1647 cinfo = get_call_info (((MonoCallInst*)ins)->signature, FALSE);
1648 if (cinfo->ret.storage == ArgValuetypeInReg) {
1649 /* Pop the destination address from the stack */
1650 x86_pop_reg (code, X86_ECX);
1652 for (quad = 0; quad < 2; quad ++) {
1653 switch (cinfo->ret.pair_storage [quad]) {
1655 g_assert (cinfo->ret.pair_regs [quad] != X86_ECX);
1656 x86_mov_membase_reg (code, X86_ECX, (quad * sizeof (gpointer)), cinfo->ret.pair_regs [quad], sizeof (gpointer));
1661 g_assert_not_reached ();
1675 * @code: buffer to store code to
1676 * @dreg: hard register where to place the result
1677 * @tls_offset: offset info
1679 * emit_tls_get emits in @code the native code that puts in the dreg register
1680 * the item in the thread local storage identified by tls_offset.
1682 * Returns: a pointer to the end of the stored code
1685 emit_tls_get (guint8* code, int dreg, int tls_offset)
1687 #ifdef PLATFORM_WIN32
1689 * See the Under the Hood article in the May 1996 issue of Microsoft Systems
1690 * Journal and/or a disassembly of the TlsGet () function.
1692 g_assert (tls_offset < 64);
1693 x86_prefix (code, X86_FS_PREFIX);
1694 x86_mov_reg_mem (code, dreg, 0x18, 4);
1695 /* Dunno what this does but TlsGetValue () contains it */
1696 x86_alu_membase_imm (code, X86_AND, dreg, 0x34, 0);
1697 x86_mov_reg_membase (code, dreg, dreg, 3600 + (tls_offset * 4), 4);
1699 if (optimize_for_xen) {
1700 x86_prefix (code, X86_GS_PREFIX);
1701 x86_mov_reg_mem (code, dreg, 0, 4);
1702 x86_mov_reg_membase (code, dreg, dreg, tls_offset, 4);
1704 x86_prefix (code, X86_GS_PREFIX);
1705 x86_mov_reg_mem (code, dreg, tls_offset, 4);
1712 * emit_load_volatile_arguments:
1714 * Load volatile arguments from the stack to the original input registers.
1715 * Required before a tail call.
1718 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
1720 MonoMethod *method = cfg->method;
1721 MonoMethodSignature *sig;
1726 /* FIXME: Generate intermediate code instead */
1728 sig = mono_method_signature (method);
1730 cinfo = get_call_info (sig, FALSE);
1732 /* This is the opposite of the code in emit_prolog */
1734 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1735 ArgInfo *ainfo = cinfo->args + i;
1737 inst = cfg->varinfo [i];
1739 if (sig->hasthis && (i == 0))
1740 arg_type = &mono_defaults.object_class->byval_arg;
1742 arg_type = sig->params [i - sig->hasthis];
1745 * On x86, the arguments are either in their original stack locations, or in
1748 if (inst->opcode == OP_REGVAR) {
1749 g_assert (ainfo->storage == ArgOnStack);
1751 x86_mov_membase_reg (code, X86_EBP, inst->inst_offset, inst->dreg, 4);
1760 #define REAL_PRINT_REG(text,reg) \
1761 mono_assert (reg >= 0); \
1762 x86_push_reg (code, X86_EAX); \
1763 x86_push_reg (code, X86_EDX); \
1764 x86_push_reg (code, X86_ECX); \
1765 x86_push_reg (code, reg); \
1766 x86_push_imm (code, reg); \
1767 x86_push_imm (code, text " %d %p\n"); \
1768 x86_mov_reg_imm (code, X86_EAX, printf); \
1769 x86_call_reg (code, X86_EAX); \
1770 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 3*4); \
1771 x86_pop_reg (code, X86_ECX); \
1772 x86_pop_reg (code, X86_EDX); \
1773 x86_pop_reg (code, X86_EAX);
1775 /* benchmark and set based on cpu */
1776 #define LOOP_ALIGNMENT 8
1777 #define bb_is_loop_start(bb) ((bb)->loop_body_start && (bb)->nesting)
1780 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1785 guint8 *code = cfg->native_code + cfg->code_len;
1786 MonoInst *last_ins = NULL;
1787 guint last_offset = 0;
1790 if (cfg->opt & MONO_OPT_PEEPHOLE)
1791 peephole_pass (cfg, bb);
1793 if (cfg->opt & MONO_OPT_LOOP) {
1794 int pad, align = LOOP_ALIGNMENT;
1795 /* set alignment depending on cpu */
1796 if (bb_is_loop_start (bb) && (pad = (cfg->code_len & (align - 1)))) {
1798 /*g_print ("adding %d pad at %x to loop in %s\n", pad, cfg->code_len, cfg->method->name);*/
1799 x86_padding (code, pad);
1800 cfg->code_len += pad;
1801 bb->native_offset = cfg->code_len;
1805 if (cfg->verbose_level > 2)
1806 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1808 cpos = bb->max_offset;
1810 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1811 MonoProfileCoverageInfo *cov = cfg->coverage_info;
1812 g_assert (!cfg->compile_aot);
1815 cov->data [bb->dfn].cil_code = bb->cil_code;
1816 /* this is not thread save, but good enough */
1817 x86_inc_mem (code, &cov->data [bb->dfn].count);
1820 offset = code - cfg->native_code;
1822 mono_debug_open_block (cfg, bb, offset);
1826 offset = code - cfg->native_code;
1828 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1830 if (offset > (cfg->code_size - max_len - 16)) {
1831 cfg->code_size *= 2;
1832 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1833 code = cfg->native_code + offset;
1834 mono_jit_stats.code_reallocs++;
1837 mono_debug_record_line_number (cfg, ins, offset);
1839 switch (ins->opcode) {
1841 x86_mul_reg (code, ins->sreg2, TRUE);
1844 x86_mul_reg (code, ins->sreg2, FALSE);
1846 case OP_X86_SETEQ_MEMBASE:
1847 case OP_X86_SETNE_MEMBASE:
1848 x86_set_membase (code, ins->opcode == OP_X86_SETEQ_MEMBASE ? X86_CC_EQ : X86_CC_NE,
1849 ins->inst_basereg, ins->inst_offset, TRUE);
1851 case OP_STOREI1_MEMBASE_IMM:
1852 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 1);
1854 case OP_STOREI2_MEMBASE_IMM:
1855 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 2);
1857 case OP_STORE_MEMBASE_IMM:
1858 case OP_STOREI4_MEMBASE_IMM:
1859 x86_mov_membase_imm (code, ins->inst_destbasereg, ins->inst_offset, ins->inst_imm, 4);
1861 case OP_STOREI1_MEMBASE_REG:
1862 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 1);
1864 case OP_STOREI2_MEMBASE_REG:
1865 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 2);
1867 case OP_STORE_MEMBASE_REG:
1868 case OP_STOREI4_MEMBASE_REG:
1869 x86_mov_membase_reg (code, ins->inst_destbasereg, ins->inst_offset, ins->sreg1, 4);
1874 x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
1877 x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
1878 x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
1880 case OP_LOAD_MEMBASE:
1881 case OP_LOADI4_MEMBASE:
1882 case OP_LOADU4_MEMBASE:
1883 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, 4);
1885 case OP_LOADU1_MEMBASE:
1886 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, FALSE);
1888 case OP_LOADI1_MEMBASE:
1889 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, FALSE);
1891 case OP_LOADU2_MEMBASE:
1892 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, FALSE, TRUE);
1894 case OP_LOADI2_MEMBASE:
1895 x86_widen_membase (code, ins->dreg, ins->inst_basereg, ins->inst_offset, TRUE, TRUE);
1898 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
1901 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
1904 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, FALSE);
1907 x86_widen_reg (code, ins->dreg, ins->sreg1, FALSE, TRUE);
1910 x86_alu_reg_reg (code, X86_CMP, ins->sreg1, ins->sreg2);
1912 case OP_COMPARE_IMM:
1913 x86_alu_reg_imm (code, X86_CMP, ins->sreg1, ins->inst_imm);
1915 case OP_X86_COMPARE_MEMBASE_REG:
1916 x86_alu_membase_reg (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->sreg2);
1918 case OP_X86_COMPARE_MEMBASE_IMM:
1919 x86_alu_membase_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1921 case OP_X86_COMPARE_MEMBASE8_IMM:
1922 x86_alu_membase8_imm (code, X86_CMP, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1924 case OP_X86_COMPARE_REG_MEMBASE:
1925 x86_alu_reg_membase (code, X86_CMP, ins->sreg1, ins->sreg2, ins->inst_offset);
1927 case OP_X86_COMPARE_MEM_IMM:
1928 x86_alu_mem_imm (code, X86_CMP, ins->inst_offset, ins->inst_imm);
1930 case OP_X86_TEST_NULL:
1931 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
1933 case OP_X86_ADD_MEMBASE_IMM:
1934 x86_alu_membase_imm (code, X86_ADD, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1936 case OP_X86_ADD_MEMBASE:
1937 x86_alu_reg_membase (code, X86_ADD, ins->sreg1, ins->sreg2, ins->inst_offset);
1939 case OP_X86_SUB_MEMBASE_IMM:
1940 x86_alu_membase_imm (code, X86_SUB, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1942 case OP_X86_SUB_MEMBASE:
1943 x86_alu_reg_membase (code, X86_SUB, ins->sreg1, ins->sreg2, ins->inst_offset);
1945 case OP_X86_AND_MEMBASE_IMM:
1946 x86_alu_membase_imm (code, X86_AND, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1948 case OP_X86_OR_MEMBASE_IMM:
1949 x86_alu_membase_imm (code, X86_OR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1951 case OP_X86_XOR_MEMBASE_IMM:
1952 x86_alu_membase_imm (code, X86_XOR, ins->inst_basereg, ins->inst_offset, ins->inst_imm);
1954 case OP_X86_INC_MEMBASE:
1955 x86_inc_membase (code, ins->inst_basereg, ins->inst_offset);
1957 case OP_X86_INC_REG:
1958 x86_inc_reg (code, ins->dreg);
1960 case OP_X86_DEC_MEMBASE:
1961 x86_dec_membase (code, ins->inst_basereg, ins->inst_offset);
1963 case OP_X86_DEC_REG:
1964 x86_dec_reg (code, ins->dreg);
1966 case OP_X86_MUL_MEMBASE:
1967 x86_imul_reg_membase (code, ins->sreg1, ins->sreg2, ins->inst_offset);
1970 x86_breakpoint (code);
1974 x86_alu_reg_reg (code, X86_ADD, ins->sreg1, ins->sreg2);
1977 x86_alu_reg_reg (code, X86_ADC, ins->sreg1, ins->sreg2);
1981 x86_alu_reg_imm (code, X86_ADD, ins->dreg, ins->inst_imm);
1984 x86_alu_reg_imm (code, X86_ADC, ins->dreg, ins->inst_imm);
1988 x86_alu_reg_reg (code, X86_SUB, ins->sreg1, ins->sreg2);
1991 x86_alu_reg_reg (code, X86_SBB, ins->sreg1, ins->sreg2);
1995 x86_alu_reg_imm (code, X86_SUB, ins->dreg, ins->inst_imm);
1998 x86_alu_reg_imm (code, X86_SBB, ins->dreg, ins->inst_imm);
2001 x86_alu_reg_reg (code, X86_AND, ins->sreg1, ins->sreg2);
2004 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ins->inst_imm);
2008 x86_div_reg (code, ins->sreg2, TRUE);
2011 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2012 x86_div_reg (code, ins->sreg2, FALSE);
2015 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2017 x86_div_reg (code, ins->sreg2, TRUE);
2021 x86_div_reg (code, ins->sreg2, TRUE);
2024 x86_alu_reg_reg (code, X86_XOR, X86_EDX, X86_EDX);
2025 x86_div_reg (code, ins->sreg2, FALSE);
2028 x86_mov_reg_imm (code, ins->sreg2, ins->inst_imm);
2030 x86_div_reg (code, ins->sreg2, TRUE);
2033 x86_alu_reg_reg (code, X86_OR, ins->sreg1, ins->sreg2);
2036 x86_alu_reg_imm (code, X86_OR, ins->sreg1, ins->inst_imm);
2039 x86_alu_reg_reg (code, X86_XOR, ins->sreg1, ins->sreg2);
2042 x86_alu_reg_imm (code, X86_XOR, ins->sreg1, ins->inst_imm);
2045 g_assert (ins->sreg2 == X86_ECX);
2046 x86_shift_reg (code, X86_SHL, ins->dreg);
2049 g_assert (ins->sreg2 == X86_ECX);
2050 x86_shift_reg (code, X86_SAR, ins->dreg);
2053 x86_shift_reg_imm (code, X86_SAR, ins->dreg, ins->inst_imm);
2056 x86_shift_reg_imm (code, X86_SHR, ins->dreg, ins->inst_imm);
2059 g_assert (ins->sreg2 == X86_ECX);
2060 x86_shift_reg (code, X86_SHR, ins->dreg);
2063 x86_shift_reg_imm (code, X86_SHL, ins->dreg, ins->inst_imm);
2066 guint8 *jump_to_end;
2068 /* handle shifts below 32 bits */
2069 x86_shld_reg (code, ins->backend.reg3, ins->sreg1);
2070 x86_shift_reg (code, X86_SHL, ins->sreg1);
2072 x86_test_reg_imm (code, X86_ECX, 32);
2073 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2075 /* handle shift over 32 bit */
2076 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2077 x86_clear_reg (code, ins->sreg1);
2079 x86_patch (jump_to_end, code);
2083 guint8 *jump_to_end;
2085 /* handle shifts below 32 bits */
2086 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2087 x86_shift_reg (code, X86_SAR, ins->backend.reg3);
2089 x86_test_reg_imm (code, X86_ECX, 32);
2090 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2092 /* handle shifts over 31 bits */
2093 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2094 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 31);
2096 x86_patch (jump_to_end, code);
2100 guint8 *jump_to_end;
2102 /* handle shifts below 32 bits */
2103 x86_shrd_reg (code, ins->sreg1, ins->backend.reg3);
2104 x86_shift_reg (code, X86_SHR, ins->backend.reg3);
2106 x86_test_reg_imm (code, X86_ECX, 32);
2107 jump_to_end = code; x86_branch8 (code, X86_CC_EQ, 0, FALSE);
2109 /* handle shifts over 31 bits */
2110 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2111 x86_clear_reg (code, ins->backend.reg3);
2113 x86_patch (jump_to_end, code);
2117 if (ins->inst_imm >= 32) {
2118 x86_mov_reg_reg (code, ins->backend.reg3, ins->sreg1, 4);
2119 x86_clear_reg (code, ins->sreg1);
2120 x86_shift_reg_imm (code, X86_SHL, ins->backend.reg3, ins->inst_imm - 32);
2122 x86_shld_reg_imm (code, ins->backend.reg3, ins->sreg1, ins->inst_imm);
2123 x86_shift_reg_imm (code, X86_SHL, ins->sreg1, ins->inst_imm);
2127 if (ins->inst_imm >= 32) {
2128 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2129 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, 0x1f);
2130 x86_shift_reg_imm (code, X86_SAR, ins->sreg1, ins->inst_imm - 32);
2132 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2133 x86_shift_reg_imm (code, X86_SAR, ins->backend.reg3, ins->inst_imm);
2136 case OP_LSHR_UN_IMM:
2137 if (ins->inst_imm >= 32) {
2138 x86_mov_reg_reg (code, ins->sreg1, ins->backend.reg3, 4);
2139 x86_clear_reg (code, ins->backend.reg3);
2140 x86_shift_reg_imm (code, X86_SHR, ins->sreg1, ins->inst_imm - 32);
2142 x86_shrd_reg_imm (code, ins->sreg1, ins->backend.reg3, ins->inst_imm);
2143 x86_shift_reg_imm (code, X86_SHR, ins->backend.reg3, ins->inst_imm);
2147 x86_not_reg (code, ins->sreg1);
2150 x86_neg_reg (code, ins->sreg1);
2153 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, FALSE);
2156 x86_widen_reg (code, ins->dreg, ins->sreg1, TRUE, TRUE);
2159 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2162 switch (ins->inst_imm) {
2166 if (ins->dreg != ins->sreg1)
2167 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2168 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2171 /* LEA r1, [r2 + r2*2] */
2172 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2175 /* LEA r1, [r2 + r2*4] */
2176 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2179 /* LEA r1, [r2 + r2*2] */
2181 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2182 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2185 /* LEA r1, [r2 + r2*8] */
2186 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 3);
2189 /* LEA r1, [r2 + r2*4] */
2191 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2192 x86_alu_reg_reg (code, X86_ADD, ins->dreg, ins->dreg);
2195 /* LEA r1, [r2 + r2*2] */
2197 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 1);
2198 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2201 /* LEA r1, [r2 + r2*4] */
2202 /* LEA r1, [r1 + r1*4] */
2203 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2204 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2207 /* LEA r1, [r2 + r2*4] */
2209 /* LEA r1, [r1 + r1*4] */
2210 x86_lea_memindex (code, ins->dreg, ins->sreg1, 0, ins->sreg1, 2);
2211 x86_shift_reg_imm (code, X86_SHL, ins->dreg, 2);
2212 x86_lea_memindex (code, ins->dreg, ins->dreg, 0, ins->dreg, 2);
2215 x86_imul_reg_reg_imm (code, ins->dreg, ins->sreg1, ins->inst_imm);
2220 x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2221 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2223 case CEE_MUL_OVF_UN: {
2224 /* the mul operation and the exception check should most likely be split */
2225 int non_eax_reg, saved_eax = FALSE, saved_edx = FALSE;
2226 /*g_assert (ins->sreg2 == X86_EAX);
2227 g_assert (ins->dreg == X86_EAX);*/
2228 if (ins->sreg2 == X86_EAX) {
2229 non_eax_reg = ins->sreg1;
2230 } else if (ins->sreg1 == X86_EAX) {
2231 non_eax_reg = ins->sreg2;
2233 /* no need to save since we're going to store to it anyway */
2234 if (ins->dreg != X86_EAX) {
2236 x86_push_reg (code, X86_EAX);
2238 x86_mov_reg_reg (code, X86_EAX, ins->sreg1, 4);
2239 non_eax_reg = ins->sreg2;
2241 if (ins->dreg == X86_EDX) {
2244 x86_push_reg (code, X86_EAX);
2246 } else if (ins->dreg != X86_EAX) {
2248 x86_push_reg (code, X86_EDX);
2250 x86_mul_reg (code, non_eax_reg, FALSE);
2251 /* save before the check since pop and mov don't change the flags */
2252 if (ins->dreg != X86_EAX)
2253 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
2255 x86_pop_reg (code, X86_EDX);
2257 x86_pop_reg (code, X86_EAX);
2258 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2262 x86_mov_reg_imm (code, ins->dreg, ins->inst_c0);
2265 g_assert_not_reached ();
2266 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2267 x86_mov_reg_imm (code, ins->dreg, 0);
2269 case OP_LOAD_GOTADDR:
2270 x86_call_imm (code, 0);
2272 * The patch needs to point to the pop, since the GOT offset needs
2273 * to be added to that address.
2275 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_GOT_OFFSET, NULL);
2276 x86_pop_reg (code, ins->dreg);
2277 x86_alu_reg_imm (code, X86_ADD, ins->dreg, 0xf0f0f0f0);
2280 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2281 x86_mov_reg_membase (code, ins->dreg, ins->inst_basereg, 0xf0f0f0f0, 4);
2283 case OP_X86_PUSH_GOT_ENTRY:
2284 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_right->inst_i1, ins->inst_right->inst_p0);
2285 x86_push_membase (code, ins->inst_basereg, 0xf0f0f0f0);
2289 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2292 g_assert_not_reached ();
2295 * Note: this 'frame destruction' logic is useful for tail calls, too.
2296 * Keep in sync with the code in emit_epilog.
2300 /* FIXME: no tracing support... */
2301 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2302 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2303 /* reset offset to make max_len work */
2304 offset = code - cfg->native_code;
2306 g_assert (!cfg->method->save_lmf);
2308 code = emit_load_volatile_arguments (cfg, code);
2310 if (cfg->used_int_regs & (1 << X86_EBX))
2312 if (cfg->used_int_regs & (1 << X86_EDI))
2314 if (cfg->used_int_regs & (1 << X86_ESI))
2317 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
2319 if (cfg->used_int_regs & (1 << X86_ESI))
2320 x86_pop_reg (code, X86_ESI);
2321 if (cfg->used_int_regs & (1 << X86_EDI))
2322 x86_pop_reg (code, X86_EDI);
2323 if (cfg->used_int_regs & (1 << X86_EBX))
2324 x86_pop_reg (code, X86_EBX);
2326 /* restore ESP/EBP */
2328 offset = code - cfg->native_code;
2329 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2330 x86_jump32 (code, 0);
2334 /* ensure ins->sreg1 is not NULL
2335 * note that cmp DWORD PTR [eax], eax is one byte shorter than
2336 * cmp DWORD PTR [eax], 0
2338 x86_alu_membase_reg (code, X86_CMP, ins->sreg1, 0, ins->sreg1);
2341 int hreg = ins->sreg1 == X86_EAX? X86_ECX: X86_EAX;
2342 x86_push_reg (code, hreg);
2343 x86_lea_membase (code, hreg, X86_EBP, cfg->sig_cookie);
2344 x86_mov_membase_reg (code, ins->sreg1, 0, hreg, 4);
2345 x86_pop_reg (code, hreg);
2353 call = (MonoCallInst*)ins;
2354 if (ins->flags & MONO_INST_HAS_METHOD)
2355 code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2357 code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2358 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2359 /* a pop is one byte, while an add reg, imm is 3. So if there are 4 or 8
2360 * bytes to pop, we want to use pops. GCC does this (note it won't happen
2361 * for P4 or i686 because gcc will avoid using pop push at all. But we aren't
2362 * smart enough to do that optimization yet
2364 * It turns out that on my P4, doing two pops for 8 bytes on the stack makes
2365 * mcs botstrap slow down. However, doing 1 pop for 4 bytes creates a small,
2366 * (most likely from locality benefits). People with other processors should
2367 * check on theirs to see what happens.
2369 if (call->stack_usage == 4) {
2370 /* we want to use registers that won't get used soon, so use
2371 * ecx, as eax will get allocated first. edx is used by long calls,
2372 * so we can't use that.
2375 x86_pop_reg (code, X86_ECX);
2377 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2380 code = emit_move_return_value (cfg, ins, code);
2385 case OP_VOIDCALL_REG:
2387 call = (MonoCallInst*)ins;
2388 x86_call_reg (code, ins->sreg1);
2389 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2390 if (call->stack_usage == 4)
2391 x86_pop_reg (code, X86_ECX);
2393 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2395 code = emit_move_return_value (cfg, ins, code);
2397 case OP_FCALL_MEMBASE:
2398 case OP_LCALL_MEMBASE:
2399 case OP_VCALL_MEMBASE:
2400 case OP_VOIDCALL_MEMBASE:
2401 case OP_CALL_MEMBASE:
2402 call = (MonoCallInst*)ins;
2403 x86_call_membase (code, ins->sreg1, ins->inst_offset);
2404 if (call->stack_usage && !CALLCONV_IS_STDCALL (call->signature)) {
2405 if (call->stack_usage == 4)
2406 x86_pop_reg (code, X86_ECX);
2408 x86_alu_reg_imm (code, X86_ADD, X86_ESP, call->stack_usage);
2410 code = emit_move_return_value (cfg, ins, code);
2414 x86_push_reg (code, ins->sreg1);
2416 case OP_X86_PUSH_IMM:
2417 x86_push_imm (code, ins->inst_imm);
2419 case OP_X86_PUSH_MEMBASE:
2420 x86_push_membase (code, ins->inst_basereg, ins->inst_offset);
2422 case OP_X86_PUSH_OBJ:
2423 x86_alu_reg_imm (code, X86_SUB, X86_ESP, ins->inst_imm);
2424 x86_push_reg (code, X86_EDI);
2425 x86_push_reg (code, X86_ESI);
2426 x86_push_reg (code, X86_ECX);
2427 if (ins->inst_offset)
2428 x86_lea_membase (code, X86_ESI, ins->inst_basereg, ins->inst_offset);
2430 x86_mov_reg_reg (code, X86_ESI, ins->inst_basereg, 4);
2431 x86_lea_membase (code, X86_EDI, X86_ESP, 12);
2432 x86_mov_reg_imm (code, X86_ECX, (ins->inst_imm >> 2));
2434 x86_prefix (code, X86_REP_PREFIX);
2436 x86_pop_reg (code, X86_ECX);
2437 x86_pop_reg (code, X86_ESI);
2438 x86_pop_reg (code, X86_EDI);
2441 x86_lea_memindex (code, ins->dreg, ins->sreg1, ins->inst_imm, ins->sreg2, ins->backend.shift_amount);
2443 case OP_X86_LEA_MEMBASE:
2444 x86_lea_membase (code, ins->dreg, ins->sreg1, ins->inst_imm);
2447 x86_xchg_reg_reg (code, ins->sreg1, ins->sreg2, 4);
2450 /* keep alignment */
2451 x86_alu_reg_imm (code, X86_ADD, ins->sreg1, MONO_ARCH_FRAME_ALIGNMENT - 1);
2452 x86_alu_reg_imm (code, X86_AND, ins->sreg1, ~(MONO_ARCH_FRAME_ALIGNMENT - 1));
2453 code = mono_emit_stack_alloc (code, ins);
2454 x86_mov_reg_reg (code, ins->dreg, X86_ESP, 4);
2460 x86_push_reg (code, ins->sreg1);
2461 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2462 (gpointer)"mono_arch_throw_exception");
2466 x86_push_reg (code, ins->sreg1);
2467 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD,
2468 (gpointer)"mono_arch_rethrow_exception");
2471 case OP_CALL_HANDLER:
2474 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 12);
2476 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2477 x86_call_imm (code, 0);
2479 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2483 ins->inst_c0 = code - cfg->native_code;
2486 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2487 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2489 if (ins->flags & MONO_INST_BRLABEL) {
2490 if (ins->inst_i0->inst_c0) {
2491 x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2493 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2494 if ((cfg->opt & MONO_OPT_BRANCH) &&
2495 x86_is_imm8 (ins->inst_i0->inst_c1 - cpos))
2496 x86_jump8 (code, 0);
2498 x86_jump32 (code, 0);
2501 if (ins->inst_target_bb->native_offset) {
2502 x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2504 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2505 if ((cfg->opt & MONO_OPT_BRANCH) &&
2506 x86_is_imm8 (ins->inst_target_bb->max_offset - cpos))
2507 x86_jump8 (code, 0);
2509 x86_jump32 (code, 0);
2514 x86_jump_reg (code, ins->sreg1);
2517 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2518 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2521 x86_set_reg (code, X86_CC_LT, ins->dreg, TRUE);
2522 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2525 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2526 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2529 x86_set_reg (code, X86_CC_GT, ins->dreg, TRUE);
2530 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2533 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2534 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2537 x86_set_reg (code, X86_CC_NE, ins->dreg, TRUE);
2538 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2540 case OP_COND_EXC_EQ:
2541 case OP_COND_EXC_NE_UN:
2542 case OP_COND_EXC_LT:
2543 case OP_COND_EXC_LT_UN:
2544 case OP_COND_EXC_GT:
2545 case OP_COND_EXC_GT_UN:
2546 case OP_COND_EXC_GE:
2547 case OP_COND_EXC_GE_UN:
2548 case OP_COND_EXC_LE:
2549 case OP_COND_EXC_LE_UN:
2550 case OP_COND_EXC_OV:
2551 case OP_COND_EXC_NO:
2553 case OP_COND_EXC_NC:
2554 EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2566 EMIT_COND_BRANCH (ins, branch_cc_table [ins->opcode - CEE_BEQ], (ins->opcode < CEE_BNE_UN));
2569 /* floating point opcodes */
2571 double d = *(double *)ins->inst_p0;
2573 if ((d == 0.0) && (mono_signbit (d) == 0)) {
2575 } else if (d == 1.0) {
2578 if (cfg->compile_aot) {
2579 guint32 *val = (guint32*)&d;
2580 x86_push_imm (code, val [1]);
2581 x86_push_imm (code, val [0]);
2582 x86_fld_membase (code, X86_ESP, 0, TRUE);
2583 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
2586 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R8, ins->inst_p0);
2587 x86_fld (code, NULL, TRUE);
2593 float f = *(float *)ins->inst_p0;
2595 if ((f == 0.0) && (mono_signbit (f) == 0)) {
2597 } else if (f == 1.0) {
2600 if (cfg->compile_aot) {
2601 guint32 val = *(guint32*)&f;
2602 x86_push_imm (code, val);
2603 x86_fld_membase (code, X86_ESP, 0, FALSE);
2604 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2607 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_R4, ins->inst_p0);
2608 x86_fld (code, NULL, FALSE);
2613 case OP_STORER8_MEMBASE_REG:
2614 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, TRUE, TRUE);
2616 case OP_LOADR8_SPILL_MEMBASE:
2617 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2620 case OP_LOADR8_MEMBASE:
2621 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2623 case OP_STORER4_MEMBASE_REG:
2624 x86_fst_membase (code, ins->inst_destbasereg, ins->inst_offset, FALSE, TRUE);
2626 case OP_LOADR4_MEMBASE:
2627 x86_fld_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2629 case CEE_CONV_R4: /* FIXME: change precision */
2631 x86_push_reg (code, ins->sreg1);
2632 x86_fild_membase (code, X86_ESP, 0, FALSE);
2633 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2635 case OP_X86_FP_LOAD_I8:
2636 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2638 case OP_X86_FP_LOAD_I4:
2639 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2641 case OP_FCONV_TO_I1:
2642 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2644 case OP_FCONV_TO_U1:
2645 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2647 case OP_FCONV_TO_I2:
2648 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2650 case OP_FCONV_TO_U2:
2651 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2653 case OP_FCONV_TO_I4:
2655 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2657 case OP_FCONV_TO_I8:
2658 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2659 x86_fnstcw_membase(code, X86_ESP, 0);
2660 x86_mov_reg_membase (code, ins->dreg, X86_ESP, 0, 2);
2661 x86_alu_reg_imm (code, X86_OR, ins->dreg, 0xc00);
2662 x86_mov_membase_reg (code, X86_ESP, 2, ins->dreg, 2);
2663 x86_fldcw_membase (code, X86_ESP, 2);
2664 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2665 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2666 x86_pop_reg (code, ins->dreg);
2667 x86_pop_reg (code, ins->backend.reg3);
2668 x86_fldcw_membase (code, X86_ESP, 0);
2669 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2671 case OP_LCONV_TO_R_UN: {
2672 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2675 /* load 64bit integer to FP stack */
2676 x86_push_imm (code, 0);
2677 x86_push_reg (code, ins->sreg2);
2678 x86_push_reg (code, ins->sreg1);
2679 x86_fild_membase (code, X86_ESP, 0, TRUE);
2680 /* store as 80bit FP value */
2681 x86_fst80_membase (code, X86_ESP, 0);
2683 /* test if lreg is negative */
2684 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2685 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2687 /* add correction constant mn */
2688 x86_fld80_mem (code, mn);
2689 x86_fld80_membase (code, X86_ESP, 0);
2690 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2691 x86_fst80_membase (code, X86_ESP, 0);
2693 x86_patch (br, code);
2695 x86_fld80_membase (code, X86_ESP, 0);
2696 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2700 case OP_LCONV_TO_OVF_I: {
2701 guint8 *br [3], *label [1];
2705 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2707 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2709 /* If the low word top bit is set, see if we are negative */
2710 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2711 /* We are not negative (no top bit set, check for our top word to be zero */
2712 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2713 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2716 /* throw exception */
2717 tins = mono_branch_optimize_exception_target (cfg, bb, "OverflowException");
2719 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, tins->inst_true_bb);
2720 if ((cfg->opt & MONO_OPT_BRANCH) && x86_is_imm8 (tins->inst_true_bb->max_offset - cpos))
2721 x86_jump8 (code, 0);
2723 x86_jump32 (code, 0);
2725 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2726 x86_jump32 (code, 0);
2730 x86_patch (br [0], code);
2731 /* our top bit is set, check that top word is 0xfffffff */
2732 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2734 x86_patch (br [1], code);
2735 /* nope, emit exception */
2736 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2737 x86_patch (br [2], label [0]);
2739 if (ins->dreg != ins->sreg1)
2740 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2744 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2747 x86_fp_op_reg (code, X86_FSUB, 1, TRUE);
2750 x86_fp_op_reg (code, X86_FMUL, 1, TRUE);
2753 x86_fp_op_reg (code, X86_FDIV, 1, TRUE);
2761 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2766 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2773 * it really doesn't make sense to inline all this code,
2774 * it's here just to show that things may not be as simple
2777 guchar *check_pos, *end_tan, *pop_jump;
2778 x86_push_reg (code, X86_EAX);
2781 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
2783 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2784 x86_fstp (code, 0); /* pop the 1.0 */
2786 x86_jump8 (code, 0);
2788 x86_fp_op (code, X86_FADD, 0);
2792 x86_test_reg_imm (code, X86_EAX, X86_FP_C2);
2794 x86_branch8 (code, X86_CC_NE, 0, FALSE);
2797 x86_patch (pop_jump, code);
2798 x86_fstp (code, 0); /* pop the 1.0 */
2799 x86_patch (check_pos, code);
2800 x86_patch (end_tan, code);
2802 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2803 x86_pop_reg (code, X86_EAX);
2810 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2821 x86_push_reg (code, X86_EAX);
2822 /* we need to exchange ST(0) with ST(1) */
2825 /* this requires a loop, because fprem somtimes
2826 * returns a partial remainder */
2828 /* looks like MS is using fprem instead of the IEEE compatible fprem1 */
2829 /* x86_fprem1 (code); */
2832 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_C2);
2834 x86_branch8 (code, X86_CC_NE, l1 - l2, FALSE);
2839 x86_pop_reg (code, X86_EAX);
2843 if (cfg->opt & MONO_OPT_FCMOV) {
2844 x86_fcomip (code, 1);
2848 /* this overwrites EAX */
2849 EMIT_FPCOMPARE(code);
2850 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2853 if (cfg->opt & MONO_OPT_FCMOV) {
2854 /* zeroing the register at the start results in
2855 * shorter and faster code (we can also remove the widening op)
2857 guchar *unordered_check;
2858 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2859 x86_fcomip (code, 1);
2861 unordered_check = code;
2862 x86_branch8 (code, X86_CC_P, 0, FALSE);
2863 x86_set_reg (code, X86_CC_EQ, ins->dreg, FALSE);
2864 x86_patch (unordered_check, code);
2867 if (ins->dreg != X86_EAX)
2868 x86_push_reg (code, X86_EAX);
2870 EMIT_FPCOMPARE(code);
2871 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2872 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2873 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2874 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2876 if (ins->dreg != X86_EAX)
2877 x86_pop_reg (code, X86_EAX);
2881 if (cfg->opt & MONO_OPT_FCMOV) {
2882 /* zeroing the register at the start results in
2883 * shorter and faster code (we can also remove the widening op)
2885 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2886 x86_fcomip (code, 1);
2888 if (ins->opcode == OP_FCLT_UN) {
2889 guchar *unordered_check = code;
2890 guchar *jump_to_end;
2891 x86_branch8 (code, X86_CC_P, 0, FALSE);
2892 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2894 x86_jump8 (code, 0);
2895 x86_patch (unordered_check, code);
2896 x86_inc_reg (code, ins->dreg);
2897 x86_patch (jump_to_end, code);
2899 x86_set_reg (code, X86_CC_GT, ins->dreg, FALSE);
2903 if (ins->dreg != X86_EAX)
2904 x86_push_reg (code, X86_EAX);
2906 EMIT_FPCOMPARE(code);
2907 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2908 if (ins->opcode == OP_FCLT_UN) {
2909 guchar *is_not_zero_check, *end_jump;
2910 is_not_zero_check = code;
2911 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2913 x86_jump8 (code, 0);
2914 x86_patch (is_not_zero_check, code);
2915 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
2917 x86_patch (end_jump, code);
2919 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2920 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2922 if (ins->dreg != X86_EAX)
2923 x86_pop_reg (code, X86_EAX);
2927 if (cfg->opt & MONO_OPT_FCMOV) {
2928 /* zeroing the register at the start results in
2929 * shorter and faster code (we can also remove the widening op)
2931 guchar *unordered_check;
2932 x86_alu_reg_reg (code, X86_XOR, ins->dreg, ins->dreg);
2933 x86_fcomip (code, 1);
2935 if (ins->opcode == OP_FCGT) {
2936 unordered_check = code;
2937 x86_branch8 (code, X86_CC_P, 0, FALSE);
2938 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2939 x86_patch (unordered_check, code);
2941 x86_set_reg (code, X86_CC_LT, ins->dreg, FALSE);
2945 if (ins->dreg != X86_EAX)
2946 x86_push_reg (code, X86_EAX);
2948 EMIT_FPCOMPARE(code);
2949 x86_alu_reg_imm (code, X86_AND, X86_EAX, X86_FP_CC_MASK);
2950 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
2951 if (ins->opcode == OP_FCGT_UN) {
2952 guchar *is_not_zero_check, *end_jump;
2953 is_not_zero_check = code;
2954 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
2956 x86_jump8 (code, 0);
2957 x86_patch (is_not_zero_check, code);
2958 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
2960 x86_patch (end_jump, code);
2962 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2963 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2965 if (ins->dreg != X86_EAX)
2966 x86_pop_reg (code, X86_EAX);
2969 if (cfg->opt & MONO_OPT_FCMOV) {
2970 guchar *jump = code;
2971 x86_branch8 (code, X86_CC_P, 0, TRUE);
2972 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2973 x86_patch (jump, code);
2976 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2977 EMIT_COND_BRANCH (ins, X86_CC_EQ, TRUE);
2980 /* Branch if C013 != 100 */
2981 if (cfg->opt & MONO_OPT_FCMOV) {
2982 /* branch if !ZF or (PF|CF) */
2983 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2984 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
2985 EMIT_COND_BRANCH (ins, X86_CC_B, FALSE);
2988 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
2989 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
2992 if (cfg->opt & MONO_OPT_FCMOV) {
2993 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
2996 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
2999 if (cfg->opt & MONO_OPT_FCMOV) {
3000 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3001 EMIT_COND_BRANCH (ins, X86_CC_GT, FALSE);
3004 if (ins->opcode == OP_FBLT_UN) {
3005 guchar *is_not_zero_check, *end_jump;
3006 is_not_zero_check = code;
3007 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3009 x86_jump8 (code, 0);
3010 x86_patch (is_not_zero_check, code);
3011 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3013 x86_patch (end_jump, code);
3015 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3019 if (cfg->opt & MONO_OPT_FCMOV) {
3020 EMIT_COND_BRANCH (ins, X86_CC_LT, FALSE);
3023 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3024 if (ins->opcode == OP_FBGT_UN) {
3025 guchar *is_not_zero_check, *end_jump;
3026 is_not_zero_check = code;
3027 x86_branch8 (code, X86_CC_NZ, 0, TRUE);
3029 x86_jump8 (code, 0);
3030 x86_patch (is_not_zero_check, code);
3031 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_CC_MASK);
3033 x86_patch (end_jump, code);
3035 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3038 /* Branch if C013 == 100 or 001 */
3039 if (cfg->opt & MONO_OPT_FCMOV) {
3042 /* skip branch if C1=1 */
3044 x86_branch8 (code, X86_CC_P, 0, FALSE);
3045 /* branch if (C0 | C3) = 1 */
3046 EMIT_COND_BRANCH (ins, X86_CC_BE, FALSE);
3047 x86_patch (br1, code);
3050 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3051 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3052 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C3);
3053 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3056 /* Branch if C013 == 000 */
3057 if (cfg->opt & MONO_OPT_FCMOV) {
3058 EMIT_COND_BRANCH (ins, X86_CC_LE, FALSE);
3061 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3064 /* Branch if C013=000 or 100 */
3065 if (cfg->opt & MONO_OPT_FCMOV) {
3068 /* skip branch if C1=1 */
3070 x86_branch8 (code, X86_CC_P, 0, FALSE);
3071 /* branch if C0=0 */
3072 EMIT_COND_BRANCH (ins, X86_CC_NB, FALSE);
3073 x86_patch (br1, code);
3076 x86_alu_reg_imm (code, X86_AND, X86_EAX, (X86_FP_C0|X86_FP_C1));
3077 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0);
3078 EMIT_COND_BRANCH (ins, X86_CC_EQ, FALSE);
3081 /* Branch if C013 != 001 */
3082 if (cfg->opt & MONO_OPT_FCMOV) {
3083 EMIT_COND_BRANCH (ins, X86_CC_P, FALSE);
3084 EMIT_COND_BRANCH (ins, X86_CC_GE, FALSE);
3087 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3088 EMIT_COND_BRANCH (ins, X86_CC_NE, FALSE);
3090 case CEE_CKFINITE: {
3091 x86_push_reg (code, X86_EAX);
3094 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
3095 x86_alu_reg_imm (code, X86_CMP, X86_EAX, X86_FP_C0);
3096 x86_pop_reg (code, X86_EAX);
3097 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
3101 code = emit_tls_get (code, ins->dreg, ins->inst_offset);
3104 case OP_MEMORY_BARRIER: {
3105 /* Not needed on x86 */
3108 case OP_ATOMIC_ADD_I4: {
3109 int dreg = ins->dreg;
3111 if (dreg == ins->inst_basereg) {
3112 x86_push_reg (code, ins->sreg2);
3116 if (dreg != ins->sreg2)
3117 x86_mov_reg_reg (code, ins->dreg, ins->sreg2, 4);
3119 x86_prefix (code, X86_LOCK_PREFIX);
3120 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3122 if (dreg != ins->dreg) {
3123 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3124 x86_pop_reg (code, dreg);
3129 case OP_ATOMIC_ADD_NEW_I4: {
3130 int dreg = ins->dreg;
3132 /* hack: limit in regalloc, dreg != sreg1 && dreg != sreg2 */
3133 if (ins->sreg2 == dreg) {
3134 if (dreg == X86_EBX) {
3136 if (ins->inst_basereg == X86_EDI)
3140 if (ins->inst_basereg == X86_EBX)
3143 } else if (ins->inst_basereg == dreg) {
3144 if (dreg == X86_EBX) {
3146 if (ins->sreg2 == X86_EDI)
3150 if (ins->sreg2 == X86_EBX)
3155 if (dreg != ins->dreg) {
3156 x86_push_reg (code, dreg);
3159 x86_mov_reg_reg (code, dreg, ins->sreg2, 4);
3160 x86_prefix (code, X86_LOCK_PREFIX);
3161 x86_xadd_membase_reg (code, ins->inst_basereg, ins->inst_offset, dreg, 4);
3162 /* dreg contains the old value, add with sreg2 value */
3163 x86_alu_reg_reg (code, X86_ADD, dreg, ins->sreg2);
3165 if (ins->dreg != dreg) {
3166 x86_mov_reg_reg (code, ins->dreg, dreg, 4);
3167 x86_pop_reg (code, dreg);
3172 case OP_ATOMIC_EXCHANGE_I4: {
3174 int sreg2 = ins->sreg2;
3175 int breg = ins->inst_basereg;
3177 /* cmpxchg uses eax as comperand, need to make sure we can use it
3178 * hack to overcome limits in x86 reg allocator
3179 * (req: dreg == eax and sreg2 != eax and breg != eax)
3181 if (ins->dreg != X86_EAX)
3182 x86_push_reg (code, X86_EAX);
3184 /* We need the EAX reg for the cmpxchg */
3185 if (ins->sreg2 == X86_EAX) {
3186 x86_push_reg (code, X86_EDX);
3187 x86_mov_reg_reg (code, X86_EDX, X86_EAX, 4);
3191 if (breg == X86_EAX) {
3192 x86_push_reg (code, X86_ESI);
3193 x86_mov_reg_reg (code, X86_ESI, X86_EAX, 4);
3197 x86_mov_reg_membase (code, X86_EAX, breg, ins->inst_offset, 4);
3199 br [0] = code; x86_prefix (code, X86_LOCK_PREFIX);
3200 x86_cmpxchg_membase_reg (code, breg, ins->inst_offset, sreg2);
3201 br [1] = code; x86_branch8 (code, X86_CC_NE, -1, FALSE);
3202 x86_patch (br [1], br [0]);
3204 if (breg != ins->inst_basereg)
3205 x86_pop_reg (code, X86_ESI);
3207 if (ins->dreg != X86_EAX) {
3208 x86_mov_reg_reg (code, ins->dreg, X86_EAX, 4);
3209 x86_pop_reg (code, X86_EAX);
3212 if (ins->sreg2 != sreg2)
3213 x86_pop_reg (code, X86_EDX);
3218 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3219 g_assert_not_reached ();
3222 if ((code - cfg->native_code - offset) > max_len) {
3223 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3224 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3225 g_assert_not_reached ();
3231 last_offset = offset;
3236 cfg->code_len = code - cfg->native_code;
3240 mono_arch_register_lowlevel_calls (void)
3245 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3247 MonoJumpInfo *patch_info;
3248 gboolean compile_aot = !run_cctors;
3250 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3251 unsigned char *ip = patch_info->ip.i + code;
3252 const unsigned char *target;
3254 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3257 switch (patch_info->type) {
3258 case MONO_PATCH_INFO_BB:
3259 case MONO_PATCH_INFO_LABEL:
3262 /* No need to patch these */
3267 switch (patch_info->type) {
3268 case MONO_PATCH_INFO_IP:
3269 *((gconstpointer *)(ip)) = target;
3271 case MONO_PATCH_INFO_CLASS_INIT: {
3273 /* Might already been changed to a nop */
3274 x86_call_code (code, 0);
3275 x86_patch (ip, target);
3278 case MONO_PATCH_INFO_ABS:
3279 case MONO_PATCH_INFO_METHOD:
3280 case MONO_PATCH_INFO_METHOD_JUMP:
3281 case MONO_PATCH_INFO_INTERNAL_METHOD:
3282 case MONO_PATCH_INFO_BB:
3283 case MONO_PATCH_INFO_LABEL:
3284 x86_patch (ip, target);
3286 case MONO_PATCH_INFO_NONE:
3289 guint32 offset = mono_arch_get_patch_offset (ip);
3290 *((gconstpointer *)(ip + offset)) = target;
3298 mono_arch_emit_prolog (MonoCompile *cfg)
3300 MonoMethod *method = cfg->method;
3302 MonoMethodSignature *sig;
3304 int alloc_size, pos, max_offset, i;
3307 cfg->code_size = MAX (mono_method_get_header (method)->code_size * 4, 256);
3308 code = cfg->native_code = g_malloc (cfg->code_size);
3310 x86_push_reg (code, X86_EBP);
3311 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
3313 alloc_size = cfg->stack_offset;
3316 if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
3317 /* Might need to attach the thread to the JIT */
3318 if (lmf_tls_offset != -1) {
3321 code = emit_tls_get ( code, X86_EAX, lmf_tls_offset);
3322 x86_test_reg_reg (code, X86_EAX, X86_EAX);
3324 x86_branch8 (code, X86_CC_NE, 0, 0);
3325 x86_push_imm (code, cfg->domain);
3326 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
3327 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3328 x86_patch (buf, code);
3329 #ifdef PLATFORM_WIN32
3330 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3331 /* FIXME: Add a separate key for LMF to avoid this */
3332 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
3335 g_assert (!cfg->compile_aot);
3336 x86_push_imm (code, cfg->domain);
3337 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
3338 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
3342 if (method->save_lmf) {
3343 pos += sizeof (MonoLMF);
3345 /* save the current IP */
3346 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3347 x86_push_imm_template (code);
3349 /* save all caller saved regs */
3350 x86_push_reg (code, X86_EBP);
3351 x86_push_reg (code, X86_ESI);
3352 x86_push_reg (code, X86_EDI);
3353 x86_push_reg (code, X86_EBX);
3355 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
3357 * Optimized version which uses the mono_lmf TLS variable instead of indirection
3358 * through the mono_lmf_addr TLS variable.
3360 /* %eax = previous_lmf */
3361 x86_prefix (code, X86_GS_PREFIX);
3362 x86_mov_reg_mem (code, X86_EAX, lmf_tls_offset, 4);
3363 /* skip method_info + lmf */
3364 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
3365 /* push previous_lmf */
3366 x86_push_reg (code, X86_EAX);
3368 x86_prefix (code, X86_GS_PREFIX);
3369 x86_mov_mem_reg (code, lmf_tls_offset, X86_ESP, 4);
3371 /* get the address of lmf for the current thread */
3373 * This is performance critical so we try to use some tricks to make
3377 if (lmf_addr_tls_offset != -1) {
3378 /* Load lmf quicky using the GS register */
3379 code = emit_tls_get (code, X86_EAX, lmf_addr_tls_offset);
3380 #ifdef PLATFORM_WIN32
3381 /* The TLS key actually contains a pointer to the MonoJitTlsData structure */
3382 /* FIXME: Add a separate key for LMF to avoid this */
3383 x86_alu_reg_imm (code, X86_ADD, X86_EAX, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
3386 code = emit_call (cfg, code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_get_lmf_addr");
3389 /* Skip method info */
3390 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
3393 x86_push_reg (code, X86_EAX);
3394 /* push *lfm (previous_lmf) */
3395 x86_push_membase (code, X86_EAX, 0);
3397 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3401 if (cfg->used_int_regs & (1 << X86_EBX)) {
3402 x86_push_reg (code, X86_EBX);
3406 if (cfg->used_int_regs & (1 << X86_EDI)) {
3407 x86_push_reg (code, X86_EDI);
3411 if (cfg->used_int_regs & (1 << X86_ESI)) {
3412 x86_push_reg (code, X86_ESI);
3420 /* the original alloc_size is already aligned: there is %ebp and retip pushed, so realign */
3422 int tot = alloc_size + pos + 4 + 4; /* ret ip + ebp */
3434 /* See mono_emit_stack_alloc */
3435 #if defined(PLATFORM_WIN32) || defined(MONO_ARCH_SIGSEGV_ON_ALTSTACK)
3436 guint32 remaining_size = alloc_size;
3437 while (remaining_size >= 0x1000) {
3438 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 0x1000);
3439 x86_test_membase_reg (code, X86_ESP, 0, X86_ESP);
3440 remaining_size -= 0x1000;
3443 x86_alu_reg_imm (code, X86_SUB, X86_ESP, remaining_size);
3445 x86_alu_reg_imm (code, X86_SUB, X86_ESP, alloc_size);
3450 /* check the stack is aligned */
3451 x86_mov_reg_reg (code, X86_EDX, X86_ESP, 4);
3452 x86_alu_reg_imm (code, X86_AND, X86_EDX, 15);
3453 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
3454 x86_branch_disp (code, X86_CC_EQ, 3, FALSE);
3455 x86_breakpoint (code);
3458 /* compute max_offset in order to use short forward jumps */
3460 if (cfg->opt & MONO_OPT_BRANCH) {
3461 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3462 MonoInst *ins = bb->code;
3463 bb->max_offset = max_offset;
3465 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3467 /* max alignment for loops */
3468 if ((cfg->opt & MONO_OPT_LOOP) && bb_is_loop_start (bb))
3469 max_offset += LOOP_ALIGNMENT;
3472 if (ins->opcode == OP_LABEL)
3473 ins->inst_c1 = max_offset;
3475 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3481 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3482 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3484 /* load arguments allocated to register from the stack */
3485 sig = mono_method_signature (method);
3488 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3489 inst = cfg->varinfo [pos];
3490 if (inst->opcode == OP_REGVAR) {
3491 x86_mov_reg_membase (code, inst->dreg, X86_EBP, inst->inst_offset, 4);
3492 if (cfg->verbose_level > 2)
3493 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3498 cfg->code_len = code - cfg->native_code;
3504 mono_arch_emit_epilog (MonoCompile *cfg)
3506 MonoMethod *method = cfg->method;
3507 MonoMethodSignature *sig = mono_method_signature (method);
3509 guint32 stack_to_pop;
3511 int max_epilog_size = 16;
3514 if (cfg->method->save_lmf)
3515 max_epilog_size += 128;
3517 if (mono_jit_trace_calls != NULL)
3518 max_epilog_size += 50;
3520 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3521 cfg->code_size *= 2;
3522 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3523 mono_jit_stats.code_reallocs++;
3526 code = cfg->native_code + cfg->code_len;
3528 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3529 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3531 /* the code restoring the registers must be kept in sync with CEE_JMP */
3534 if (method->save_lmf) {
3535 gint32 prev_lmf_reg;
3536 gint32 lmf_offset = -sizeof (MonoLMF);
3538 if ((lmf_tls_offset != -1) && !is_win32 && !optimize_for_xen) {
3540 * Optimized version which uses the mono_lmf TLS variable instead of indirection
3541 * through the mono_lmf_addr TLS variable.
3543 /* reg = previous_lmf */
3544 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
3546 /* lmf = previous_lmf */
3547 x86_prefix (code, X86_GS_PREFIX);
3548 x86_mov_mem_reg (code, lmf_tls_offset, X86_ECX, 4);
3550 /* Find a spare register */
3551 switch (sig->ret->type) {
3554 prev_lmf_reg = X86_EDI;
3555 cfg->used_int_regs |= (1 << X86_EDI);
3558 prev_lmf_reg = X86_EDX;
3562 /* reg = previous_lmf */
3563 x86_mov_reg_membase (code, prev_lmf_reg, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), 4);
3566 x86_mov_reg_membase (code, X86_ECX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), 4);
3568 /* *(lmf) = previous_lmf */
3569 x86_mov_membase_reg (code, X86_ECX, 0, prev_lmf_reg, 4);
3572 /* restore caller saved regs */
3573 if (cfg->used_int_regs & (1 << X86_EBX)) {
3574 x86_mov_reg_membase (code, X86_EBX, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebx), 4);
3577 if (cfg->used_int_regs & (1 << X86_EDI)) {
3578 x86_mov_reg_membase (code, X86_EDI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, edi), 4);
3580 if (cfg->used_int_regs & (1 << X86_ESI)) {
3581 x86_mov_reg_membase (code, X86_ESI, X86_EBP, lmf_offset + G_STRUCT_OFFSET (MonoLMF, esi), 4);
3584 /* EBP is restored by LEAVE */
3586 if (cfg->used_int_regs & (1 << X86_EBX)) {
3589 if (cfg->used_int_regs & (1 << X86_EDI)) {
3592 if (cfg->used_int_regs & (1 << X86_ESI)) {
3597 x86_lea_membase (code, X86_ESP, X86_EBP, pos);
3599 if (cfg->used_int_regs & (1 << X86_ESI)) {
3600 x86_pop_reg (code, X86_ESI);
3602 if (cfg->used_int_regs & (1 << X86_EDI)) {
3603 x86_pop_reg (code, X86_EDI);
3605 if (cfg->used_int_regs & (1 << X86_EBX)) {
3606 x86_pop_reg (code, X86_EBX);
3610 /* Load returned vtypes into registers if needed */
3611 cinfo = get_call_info (sig, FALSE);
3612 if (cinfo->ret.storage == ArgValuetypeInReg) {
3613 for (quad = 0; quad < 2; quad ++) {
3614 switch (cinfo->ret.pair_storage [quad]) {
3616 x86_mov_reg_membase (code, cinfo->ret.pair_regs [quad], cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), 4);
3618 case ArgOnFloatFpStack:
3619 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), FALSE);
3621 case ArgOnDoubleFpStack:
3622 x86_fld_membase (code, cfg->ret->inst_basereg, cfg->ret->inst_offset + (quad * sizeof (gpointer)), TRUE);
3627 g_assert_not_reached ();
3634 if (CALLCONV_IS_STDCALL (sig)) {
3635 MonoJitArgumentInfo *arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
3637 stack_to_pop = mono_arch_get_argument_info (sig, sig->param_count, arg_info);
3638 } else if (MONO_TYPE_ISSTRUCT (mono_method_signature (cfg->method)->ret) && (cinfo->ret.storage == ArgOnStack))
3644 x86_ret_imm (code, stack_to_pop);
3650 cfg->code_len = code - cfg->native_code;
3652 g_assert (cfg->code_len < cfg->code_size);
3656 mono_arch_emit_exceptions (MonoCompile *cfg)
3658 MonoJumpInfo *patch_info;
3661 MonoClass *exc_classes [16];
3662 guint8 *exc_throw_start [16], *exc_throw_end [16];
3666 /* Compute needed space */
3667 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3668 if (patch_info->type == MONO_PATCH_INFO_EXC)
3673 * make sure we have enough space for exceptions
3674 * 16 is the size of two push_imm instructions and a call
3676 if (cfg->compile_aot)
3677 code_size = exc_count * 32;
3679 code_size = exc_count * 16;
3681 while (cfg->code_len + code_size > (cfg->code_size - 16)) {
3682 cfg->code_size *= 2;
3683 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3684 mono_jit_stats.code_reallocs++;
3687 code = cfg->native_code + cfg->code_len;
3690 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3691 switch (patch_info->type) {
3692 case MONO_PATCH_INFO_EXC: {
3693 MonoClass *exc_class;
3697 x86_patch (patch_info->ip.i + cfg->native_code, code);
3699 exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
3700 g_assert (exc_class);
3701 throw_ip = patch_info->ip.i;
3703 /* Find a throw sequence for the same exception class */
3704 for (i = 0; i < nthrows; ++i)
3705 if (exc_classes [i] == exc_class)
3708 x86_push_imm (code, (exc_throw_end [i] - cfg->native_code) - throw_ip);
3709 x86_jump_code (code, exc_throw_start [i]);
3710 patch_info->type = MONO_PATCH_INFO_NONE;
3715 /* Compute size of code following the push <OFFSET> */
3718 if ((code - cfg->native_code) - throw_ip < 126 - size) {
3719 /* Use the shorter form */
3721 x86_push_imm (code, 0);
3725 x86_push_imm (code, 0xf0f0f0f0);
3730 exc_classes [nthrows] = exc_class;
3731 exc_throw_start [nthrows] = code;
3734 x86_push_imm (code, exc_class->type_token);
3735 patch_info->data.name = "mono_arch_throw_corlib_exception";
3736 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3737 patch_info->ip.i = code - cfg->native_code;
3738 x86_call_code (code, 0);
3739 x86_push_imm (buf, (code - cfg->native_code) - throw_ip);
3744 exc_throw_end [nthrows] = code;
3756 cfg->code_len = code - cfg->native_code;
3758 g_assert (cfg->code_len < cfg->code_size);
3762 mono_arch_flush_icache (guint8 *code, gint size)
3768 mono_arch_flush_register_windows (void)
3773 * Support for fast access to the thread-local lmf structure using the GS
3774 * segment register on NPTL + kernel 2.6.x.
3777 static gboolean tls_offset_inited = FALSE;
3780 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3782 if (!tls_offset_inited) {
3783 if (!getenv ("MONO_NO_TLS")) {
3784 #ifdef PLATFORM_WIN32
3786 * We need to init this multiple times, since when we are first called, the key might not
3787 * be initialized yet.
3789 appdomain_tls_offset = mono_domain_get_tls_key ();
3790 lmf_tls_offset = mono_get_jit_tls_key ();
3791 thread_tls_offset = mono_thread_get_tls_key ();
3793 /* Only 64 tls entries can be accessed using inline code */
3794 if (appdomain_tls_offset >= 64)
3795 appdomain_tls_offset = -1;
3796 if (lmf_tls_offset >= 64)
3797 lmf_tls_offset = -1;
3798 if (thread_tls_offset >= 64)
3799 thread_tls_offset = -1;
3802 optimize_for_xen = access ("/proc/xen", F_OK) == 0;
3804 tls_offset_inited = TRUE;
3805 appdomain_tls_offset = mono_domain_get_tls_offset ();
3806 lmf_tls_offset = mono_get_lmf_tls_offset ();
3807 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
3808 thread_tls_offset = mono_thread_get_tls_offset ();
3815 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3820 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3822 MonoCallInst *call = (MonoCallInst*)inst;
3823 CallInfo *cinfo = get_call_info (inst->signature, FALSE);
3825 /* add the this argument */
3826 if (this_reg != -1) {
3827 if (cinfo->args [0].storage == ArgInIReg) {
3829 MONO_INST_NEW (cfg, this, OP_MOVE);
3830 this->type = this_type;
3831 this->sreg1 = this_reg;
3832 this->dreg = mono_regstate_next_int (cfg->rs);
3833 mono_bblock_add_inst (cfg->cbb, this);
3835 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, cinfo->args [0].reg, FALSE);
3839 MONO_INST_NEW (cfg, this, OP_OUTARG);
3840 this->type = this_type;
3841 this->sreg1 = this_reg;
3842 mono_bblock_add_inst (cfg->cbb, this);
3849 if (cinfo->ret.storage == ArgValuetypeInReg) {
3851 * The valuetype is in EAX:EDX after the call, needs to be copied to
3852 * the stack. Save the address here, so the call instruction can
3855 MONO_INST_NEW (cfg, vtarg, OP_STORE_MEMBASE_REG);
3856 vtarg->inst_destbasereg = X86_ESP;
3857 vtarg->inst_offset = inst->stack_usage;
3858 vtarg->sreg1 = vt_reg;
3859 mono_bblock_add_inst (cfg->cbb, vtarg);
3861 else if (cinfo->ret.storage == ArgInIReg) {
3862 /* The return address is passed in a register */
3863 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
3864 vtarg->sreg1 = vt_reg;
3865 vtarg->dreg = mono_regstate_next_int (cfg->rs);
3866 mono_bblock_add_inst (cfg->cbb, vtarg);
3868 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
3871 MONO_INST_NEW (cfg, vtarg, OP_OUTARG);
3872 vtarg->type = STACK_MP;
3873 vtarg->sreg1 = vt_reg;
3874 mono_bblock_add_inst (cfg->cbb, vtarg);
3882 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3884 MonoInst *ins = NULL;
3886 if (cmethod->klass == mono_defaults.math_class) {
3887 if (strcmp (cmethod->name, "Sin") == 0) {
3888 MONO_INST_NEW (cfg, ins, OP_SIN);
3889 ins->inst_i0 = args [0];
3890 } else if (strcmp (cmethod->name, "Cos") == 0) {
3891 MONO_INST_NEW (cfg, ins, OP_COS);
3892 ins->inst_i0 = args [0];
3893 } else if (strcmp (cmethod->name, "Tan") == 0) {
3894 MONO_INST_NEW (cfg, ins, OP_TAN);
3895 ins->inst_i0 = args [0];
3896 } else if (strcmp (cmethod->name, "Atan") == 0) {
3897 MONO_INST_NEW (cfg, ins, OP_ATAN);
3898 ins->inst_i0 = args [0];
3899 } else if (strcmp (cmethod->name, "Sqrt") == 0) {
3900 MONO_INST_NEW (cfg, ins, OP_SQRT);
3901 ins->inst_i0 = args [0];
3902 } else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8) {
3903 MONO_INST_NEW (cfg, ins, OP_ABS);
3904 ins->inst_i0 = args [0];
3907 /* OP_FREM is not IEEE compatible */
3908 else if (strcmp (cmethod->name, "IEEERemainder") == 0) {
3909 MONO_INST_NEW (cfg, ins, OP_FREM);
3910 ins->inst_i0 = args [0];
3911 ins->inst_i1 = args [1];
3914 } else if (cmethod->klass == mono_defaults.thread_class &&
3915 strcmp (cmethod->name, "MemoryBarrier") == 0) {
3916 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
3917 } else if(cmethod->klass->image == mono_defaults.corlib &&
3918 (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
3919 (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
3921 if (strcmp (cmethod->name, "Increment") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3922 MonoInst *ins_iconst;
3924 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
3925 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
3926 ins_iconst->inst_c0 = 1;
3928 ins->inst_i0 = args [0];
3929 ins->inst_i1 = ins_iconst;
3930 } else if (strcmp (cmethod->name, "Decrement") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3931 MonoInst *ins_iconst;
3933 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
3934 MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
3935 ins_iconst->inst_c0 = -1;
3937 ins->inst_i0 = args [0];
3938 ins->inst_i1 = ins_iconst;
3939 } else if (strcmp (cmethod->name, "Exchange") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3940 MONO_INST_NEW (cfg, ins, OP_ATOMIC_EXCHANGE_I4);
3942 ins->inst_i0 = args [0];
3943 ins->inst_i1 = args [1];
3944 } else if (strcmp (cmethod->name, "Add") == 0 && fsig->params [0]->type == MONO_TYPE_I4) {
3945 MONO_INST_NEW (cfg, ins, OP_ATOMIC_ADD_NEW_I4);
3947 ins->inst_i0 = args [0];
3948 ins->inst_i1 = args [1];
3957 mono_arch_print_tree (MonoInst *tree, int arity)
3962 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
3966 if (appdomain_tls_offset == -1)
3969 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
3970 ins->inst_offset = appdomain_tls_offset;
3974 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
3978 if (thread_tls_offset == -1)
3981 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
3982 ins->inst_offset = thread_tls_offset;
3987 mono_arch_get_patch_offset (guint8 *code)
3989 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 0x2))
3991 else if ((code [0] == 0xba))
3993 else if ((code [0] == 0x68))
3996 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x6))
3997 /* push <OFFSET>(<REG>) */
3999 else if ((code [0] == 0xff) && (x86_modrm_reg (code [1]) == 0x2))
4000 /* call *<OFFSET>(<REG>) */
4002 else if ((code [0] == 0xdd) || (code [0] == 0xd9))
4005 else if ((code [0] == 0x58) && (code [1] == 0x05))
4006 /* pop %eax; add <OFFSET>, %eax */
4008 else if ((code [0] >= 0x58) && (code [0] <= 0x58 + X86_NREG) && (code [1] == 0x81))
4009 /* pop <REG>; add <OFFSET>, <REG> */
4012 g_assert_not_reached ();
4018 mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
4023 /* go to the start of the call instruction
4025 * address_byte = (m << 6) | (o << 3) | reg
4026 * call opcode: 0xff address_byte displacement
4028 * 0xff m=2,o=2 imm32
4033 * A given byte sequence can match more than case here, so we have to be
4034 * really careful about the ordering of the cases. Longer sequences
4037 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)) {
4039 * This is an interface call
4040 * 8b 80 0c e8 ff ff mov 0xffffe80c(%eax),%eax
4041 * ff 10 call *(%eax)
4043 reg = x86_modrm_rm (code [5]);
4045 } else if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
4046 reg = code [4] & 0x07;
4047 disp = (signed char)code [5];
4049 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
4050 reg = code [1] & 0x07;
4051 disp = *((gint32*)(code + 2));
4052 } else if ((code [1] == 0xe8)) {
4054 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
4056 * This is a interface call
4057 * 8b 40 30 mov 0x30(%eax),%eax
4058 * ff 10 call *(%eax)
4061 reg = code [5] & 0x07;
4067 return (gpointer*)(((gint32)(regs [reg])) + disp);
4071 mono_arch_get_delegate_method_ptr_addr (guint8* code, gpointer *regs)
4077 if ((code [0] == 0x8b) && (x86_modrm_mod (code [1]) == 3) && (x86_modrm_reg (code [1]) == X86_EAX) && (code [2] == 0x8b) && (code [3] == 0x40) && (code [5] == 0xff) && (code [6] == 0xd0)) {
4078 reg = x86_modrm_rm (code [1]);
4084 return (gpointer*)(((gint32)(regs [reg])) + disp);