2 * mini-sparc.c: Sparc backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
9 * Christopher Taylor (ct@gentoo.org)
10 * Mark Crichton (crichton@gimp.org)
12 * (C) 2003 Ximian, Inc.
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/debug-helpers.h>
20 #include "mini-sparc.h"
22 #include "cpu-sparc.h"
24 int mono_exc_esp_offset = 0;
27 mono_arch_regname (int reg) {
28 static const char * rnames[] = {
29 "sparc_g0", "sparc_g1", "sparc_g2", "sparc_g3", "sparc_g4",
30 "sparc_g5", "sparc_g6", "sparc_g7", "sparc_o0", "sparc_o1",
31 "sparc_o2", "sparc_o3", "sparc_o4", "sparc_o5", "sparc_sp",
32 "sparc_call", "sparc_l0", "sparc_l1", "sparc_l2", "sparc_l3",
33 "sparc_l4", "sparc_l5", "sparc_l6", "sparc_l7", "sparc_i0",
34 "sparc_i1", "sparc_i2", "sparc_i3", "sparc_i4", "sparc_i5",
35 "sparc_fp", "sparc_retadr"
37 if (reg >= 0 && reg < 32)
46 } MonoJitArgumentInfo;
49 * arch_get_argument_info:
50 * @csig: a method signature
51 * @param_count: the number of parameters to consider
52 * @arg_info: an array to store the result infos
54 * Gathers information on parameters such as size, alignment and
55 * padding. arg_info should be large enought to hold param_count + 1 entries.
57 * Returns the size of the activation frame.
60 arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
62 int k, frame_size = 0;
66 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
67 frame_size += sizeof (gpointer);
71 arg_info [0].offset = offset;
74 frame_size += sizeof (gpointer);
78 arg_info [0].size = frame_size;
80 for (k = 0; k < param_count; k++) {
83 size = mono_type_native_stack_size (csig->params [k], &align);
85 size = mono_type_stack_size (csig->params [k], &align);
87 /* ignore alignment for now */
90 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
91 arg_info [k].pad = pad;
93 arg_info [k + 1].pad = 0;
94 arg_info [k + 1].size = size;
96 arg_info [k + 1].offset = offset;
100 align = MONO_ARCH_FRAME_ALIGNMENT;
101 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
102 arg_info [k].pad = pad;
107 static int indent_level = 0;
109 static void indent (int diff) {
110 int v = indent_level;
114 indent_level += diff;
118 enter_method (MonoMethod *method, char *ebp)
123 MonoJitArgumentInfo *arg_info;
124 MonoMethodSignature *sig;
127 fname = mono_method_full_name (method, TRUE);
129 printf ("ENTER: %s(", fname);
132 if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
133 g_error ("unaligned stack detected (%p)", ebp);
136 sig = method->signature;
138 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
140 arch_get_argument_info (sig, sig->param_count, arg_info);
142 if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
143 g_assert (!method->signature->ret->byref);
145 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
148 if (method->signature->hasthis) {
149 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
150 if (method->klass->valuetype) {
151 printf ("value:%p, ", *this);
153 o = *((MonoObject **)this);
156 class = o->vtable->klass;
158 if (class == mono_defaults.string_class) {
159 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
161 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
164 printf ("this:NULL, ");
168 for (i = 0; i < method->signature->param_count; ++i) {
169 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
170 int size = arg_info [i + 1].size;
172 MonoType *type = method->signature->params [i];
175 printf ("[BYREF:%p], ", *cpos);
176 } else switch (type->type) {
180 printf ("%p, ", (gpointer)*((int *)(cpos)));
182 case MONO_TYPE_BOOLEAN:
190 printf ("%d, ", *((int *)(cpos)));
192 case MONO_TYPE_STRING: {
193 MonoString *s = *((MonoString **)cpos);
195 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
196 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
198 printf ("[STRING:null], ");
201 case MONO_TYPE_CLASS:
202 case MONO_TYPE_OBJECT: {
203 o = *((MonoObject **)cpos);
205 class = o->vtable->klass;
207 if (class == mono_defaults.string_class) {
208 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
209 } else if (class == mono_defaults.int32_class) {
210 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
212 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
214 printf ("%p, ", *((gpointer *)(cpos)));
219 case MONO_TYPE_FNPTR:
220 case MONO_TYPE_ARRAY:
221 case MONO_TYPE_SZARRAY:
222 printf ("%p, ", *((gpointer *)(cpos)));
225 printf ("%lld, ", *((gint64 *)(cpos)));
228 printf ("%f, ", *((float *)(cpos)));
231 printf ("%f, ", *((double *)(cpos)));
233 case MONO_TYPE_VALUETYPE:
235 for (j = 0; j < size; j++)
236 printf ("%02x,", *((guint8*)cpos +j));
248 leave_method (MonoMethod *method, ...)
254 va_start(ap, method);
256 fname = mono_method_full_name (method, TRUE);
258 printf ("LEAVE: %s", fname);
261 type = method->signature->ret;
264 switch (type->type) {
267 case MONO_TYPE_BOOLEAN: {
268 int eax = va_arg (ap, int);
270 printf ("TRUE:%d", eax);
285 int eax = va_arg (ap, int);
286 printf ("EAX=%d", eax);
289 case MONO_TYPE_STRING: {
290 MonoString *s = va_arg (ap, MonoString *);
293 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
294 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
296 printf ("[STRING:null], ");
299 case MONO_TYPE_CLASS:
300 case MONO_TYPE_OBJECT: {
301 MonoObject *o = va_arg (ap, MonoObject *);
304 if (o->vtable->klass == mono_defaults.boolean_class) {
305 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
306 } else if (o->vtable->klass == mono_defaults.int32_class) {
307 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
308 } else if (o->vtable->klass == mono_defaults.int64_class) {
309 printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
311 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
313 printf ("[OBJECT:%p]", o);
318 case MONO_TYPE_FNPTR:
319 case MONO_TYPE_ARRAY:
320 case MONO_TYPE_SZARRAY: {
321 gpointer p = va_arg (ap, gpointer);
322 printf ("result=%p", p);
326 gint64 l = va_arg (ap, gint64);
327 printf ("lresult=%lld", l);
331 double f = va_arg (ap, double);
332 printf ("FP=%f\n", f);
335 case MONO_TYPE_VALUETYPE:
336 if (type->data.klass->enumtype) {
337 type = type->data.klass->enum_basetype;
340 guint8 *p = va_arg (ap, gpointer);
342 size = mono_type_size (type, &align);
344 for (j = 0; p && j < size; j++)
345 printf ("%02x,", p [j]);
350 printf ("(unknown return type %x)", method->signature->ret->type);
357 * Initialize the cpu to execute managed code.
360 mono_arch_cpu_init (void)
365 * This function returns the optimizations supported on this cpu.
368 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
376 is_regsize_var (MonoType *t) {
385 case MONO_TYPE_OBJECT:
386 case MONO_TYPE_STRING:
387 case MONO_TYPE_CLASS:
388 case MONO_TYPE_SZARRAY:
389 case MONO_TYPE_ARRAY:
391 case MONO_TYPE_VALUETYPE:
392 if (t->data.klass->enumtype)
393 return is_regsize_var (t->data.klass->enum_basetype);
400 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
405 for (i = 0; i < cfg->num_varinfo; i++) {
406 MonoInst *ins = cfg->varinfo [i];
407 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
410 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
413 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
416 /* we can only allocate 32 bit values */
417 if (is_regsize_var (ins->inst_vtype)) {
418 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
419 g_assert (i == vmv->idx);
420 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
428 mono_arch_get_global_int_regs (MonoCompile *cfg)
432 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
434 for (i = 13; i < top; ++i)
435 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
440 #define flushi(addr) __asm__ __volatile__ ("flush %0"::"r"(addr):"memory")
443 mono_arch_flush_icache (guint8 *code, gint size)
447 for (i = 0; i < (size/2); i++)
448 flushi(code + (i*8));
452 #define NOT_IMPLEMENTED(x) \
453 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
459 #define GENERAL_REGS 8
461 #define MINIMAL_STACK_SIZE 10
462 #define ALWAYS_ON_STACK(s) s
463 #define FP_ALSO_IN_REG(s) s
464 #define RET_ADDR_OFFSET 8
465 #define STACK_PARAM_OFFSET 24
467 #define MINIMAL_STACK_SIZE 5
468 #define ALWAYS_ON_STACK(s)
469 #define FP_ALSO_IN_REG(s) s
470 #define ALIGN_DOUBLES
471 #define RET_ADDR_OFFSET 4
472 #define STACK_PARAM_OFFSET 8
478 gint8 regtype; /* 0 general, 1 basereg, 2 floating point register */
491 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
494 if (*gr >= 3 + GENERAL_REGS) {
495 ainfo->offset = *stack_size;
496 ainfo->reg = sparc_sp; /* in the caller */
500 ALWAYS_ON_STACK (*stack_size += 4);
504 if (*gr >= 3 + GENERAL_REGS - 1) {
505 ainfo->offset = *stack_size;
506 ainfo->reg = sparc_sp; /* in the caller */
510 *stack_size += (*stack_size % 8);
513 ALWAYS_ON_STACK (*stack_size += 8);
526 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
529 int n = sig->hasthis + sig->param_count;
531 guint32 stack_size = 0;
532 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
537 /* FIXME: handle returning a struct */
541 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
544 DEBUG(printf("params: %d\n", sig->param_count));
545 for (i = 0; i < sig->param_count; ++i) {
546 DEBUG(printf("param %d: ", i));
547 if (sig->params [i]->byref) {
548 DEBUG(printf("byref\n"));
549 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
553 simpletype = sig->params [i]->type;
555 switch (simpletype) {
556 case MONO_TYPE_BOOLEAN:
567 case MONO_TYPE_CLASS:
568 case MONO_TYPE_OBJECT:
569 case MONO_TYPE_STRING:
570 case MONO_TYPE_SZARRAY:
571 case MONO_TYPE_ARRAY:
572 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
575 case MONO_TYPE_VALUETYPE: {
577 if (sig->params [i]->data.klass->enumtype) {
578 simpletype = sig->params [i]->data.klass->enum_basetype->type;
582 size = mono_class_value_size (sig->params [i]->data.klass, NULL);
584 DEBUG(printf ("copy %d bytes struct on stack\n",
585 mono_class_value_size (sig->params [i]->data.klass, NULL)));
586 *stack_size += (size + 3) & (~3);
587 if (gr > 3 + GENERAL_REGS) {
591 DEBUG(printf ("load %d bytes struct\n",
592 mono_class_value_size (sig->params [i]->data.klass, NULL)));
593 add_general (&gr, stack_size, code_size, TRUE);
596 g_assert_not_reached ();
601 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
607 FP_ALSO_IN_REG (gr ++);
608 ALWAYS_ON_STACK (stack_size += 4);
610 NOT_IMPLEMENTED ("R4 arg");
617 FP_ALSO_IN_REG (gr += 2);
618 ALWAYS_ON_STACK (stack_size += 8);
620 NOT_IMPLEMENTED ("R8 arg");
625 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
630 simpletype = sig->ret->type;
632 switch (simpletype) {
633 case MONO_TYPE_BOOLEAN:
643 case MONO_TYPE_CLASS:
644 case MONO_TYPE_OBJECT:
645 case MONO_TYPE_SZARRAY:
646 case MONO_TYPE_ARRAY:
647 case MONO_TYPE_STRING:
648 cinfo->ret.reg = sparc_i0;
652 cinfo->ret.reg = sparc_i0;
656 cinfo->ret.reg = sparc_f0;
657 cinfo->ret.regtype = 2;
659 case MONO_TYPE_VALUETYPE:
660 if (sig->ret->data.klass->enumtype) {
661 simpletype = sig->ret->data.klass->enum_basetype->type;
668 g_error ("Can't handle as return value 0x%x", sig->ret->type);
672 /* align stack size to 16 */
673 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
674 stack_size = (stack_size + 15) & ~15;
676 cinfo->stack_usage = stack_size;
682 * Set var information according to the calling convention. sparc version.
683 * The locals var stuff should most likely be split in another method.
686 mono_arch_allocate_vars (MonoCompile *m)
688 MonoMethodSignature *sig;
689 MonoMethodHeader *header;
691 int i, offset, size, align, curinst;
692 int frame_reg = sparc_sp;
694 if (m->flags & MONO_CFG_HAS_ALLOCA)
695 frame_reg = sparc_l7;
696 m->frame_reg = frame_reg;
698 header = ((MonoMethodNormal *)m->method)->header;
700 sig = m->method->signature;
704 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
705 m->ret->opcode = OP_REGVAR;
706 m->ret->inst_c0 = sparc_i0;
708 /* FIXME: handle long and FP values */
709 switch (sig->ret->type) {
713 m->ret->opcode = OP_REGVAR;
714 m->ret->inst_c0 = sparc_i0;
718 /* local vars are at a positive offset from the stack pointer */
720 * also note that if the function uses alloca, we use sparc_l7
721 * to point at the local variables.
723 offset = 24; /* linkage area */
724 /* align the offset to 16 bytes: not sure this is needed here */
726 //offset &= ~(16 - 1);
728 /* add parameter area size for called functions */
729 offset += m->param_area;
733 /* FIXME: check how to handle this stuff... reserve space to save LMF and caller saved registers */
734 offset += sizeof (MonoLMF);
737 /* this stuff should not be needed on ppc and the new jit,
738 * because a call on ppc to the handlers doesn't change the
739 * stack pointer and the jist doesn't manipulate the stack pointer
740 * for operations involving valuetypes.
742 /* reserve space to store the esp */
743 offset += sizeof (gpointer);
745 /* this is a global constant */
746 mono_exc_esp_offset = offset;
749 curinst = m->locals_start;
750 for (i = curinst; i < m->num_varinfo; ++i) {
751 inst = m->varinfo [i];
752 if (inst->opcode == OP_REGVAR)
755 /* inst->unused indicates native sized value types, this is used by the
756 * pinvoke wrappers when they call functions returning structure */
757 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
758 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
760 size = mono_type_size (inst->inst_vtype, &align);
763 offset &= ~(align - 1);
764 inst->inst_offset = offset;
765 inst->opcode = OP_REGOFFSET;
766 inst->inst_basereg = frame_reg;
768 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
773 inst = m->varinfo [curinst];
774 if (inst->opcode != OP_REGVAR) {
775 inst->opcode = OP_REGOFFSET;
776 inst->inst_basereg = frame_reg;
777 offset += sizeof (gpointer) - 1;
778 offset &= ~(sizeof (gpointer) - 1);
779 inst->inst_offset = offset;
780 offset += sizeof (gpointer);
785 for (i = 0; i < sig->param_count; ++i) {
786 inst = m->varinfo [curinst];
787 if (inst->opcode != OP_REGVAR) {
788 inst->opcode = OP_REGOFFSET;
789 inst->inst_basereg = frame_reg;
790 size = mono_type_size (sig->params [i], &align);
792 offset &= ~(align - 1);
793 inst->inst_offset = offset;
799 /* align the offset to 16 bytes */
804 m->stack_offset = offset;
808 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
809 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
813 * take the arguments and generate the arch-specific
814 * instructions to properly call the function in call.
815 * This includes pushing, moving arguments to the right register
817 * Issue: who does the spilling if needed, and when?
820 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
822 MonoMethodSignature *sig;
828 sig = call->signature;
829 n = sig->param_count + sig->hasthis;
831 cinfo = calculate_sizes (sig, sig->pinvoke);
833 for (i = 0; i < n; ++i) {
834 ainfo = cinfo->args + i;
835 if (is_virtual && i == 0) {
836 /* the argument will be attached to the call instrucion */
839 MONO_INST_NEW (cfg, arg, OP_OUTARG);
841 arg->cil_code = in->cil_code;
843 arg->type = in->type;
844 /* prepend, we'll need to reverse them later */
845 arg->next = call->out_args;
846 call->out_args = arg;
847 if (ainfo->regtype == 0) {
848 arg->unused = ainfo->reg;
849 call->used_iregs |= 1 << ainfo->reg;
850 } else if (ainfo->regtype == 1) {
851 g_assert_not_reached ();
852 } else if (ainfo->regtype == 2) {
853 arg->opcode = OP_OUTARG_R8;
854 arg->unused = ainfo->reg;
855 call->used_fregs |= 1 << ainfo->reg;
857 g_assert_not_reached ();
862 * Reverse the call->out_args list.
865 MonoInst *prev = NULL, *list = call->out_args, *next;
873 call->stack_usage = cinfo->stack_usage;
874 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
875 cfg->flags |= MONO_CFG_HAS_CALLS;
877 * should set more info in call, such as the stack space
878 * used by the args that needs to be added back to esp
886 * Allow tracing to work with this interface (with an optional argument)
890 * This may be needed on some archs or for debugging support.
893 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
895 /* no stack room needed now (may be needed for FASTCALL-trace support) */
897 /* split prolog-epilog requirements? */
898 *code = 50; /* max bytes needed: check this number */
902 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
906 /* if some args are passed in registers, we need to save them here */
907 x86_push_reg (code, X86_EBP);
908 x86_push_imm (code, cfg->method);
909 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
910 x86_call_code (code, 0);
911 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
925 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
928 int arg_size = 0, save_mode = SAVE_NONE;
929 MonoMethod *method = cfg->method;
930 int rtype = method->signature->ret->type;
935 /* special case string .ctor icall */
936 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
937 save_mode = SAVE_ONE;
939 save_mode = SAVE_NONE;
943 save_mode = SAVE_TWO;
949 case MONO_TYPE_VALUETYPE:
950 if (method->signature->ret->data.klass->enumtype) {
951 rtype = method->signature->ret->data.klass->enum_basetype->type;
954 save_mode = SAVE_STRUCT;
957 save_mode = SAVE_ONE;
963 //x86_push_reg (code, X86_EDX);
964 //x86_push_reg (code, X86_EAX);
965 if (enable_arguments) {
966 //x86_push_reg (code, X86_EDX);
967 //x86_push_reg (code, X86_EAX);
972 //x86_push_reg (code, X86_EAX);
973 if (enable_arguments) {
974 //x86_push_reg (code, X86_EAX);
979 //x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
980 ///x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
981 if (enable_arguments) {
982 //x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
983 //x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
988 if (enable_arguments) {
989 //x86_push_membase (code, X86_EBP, 8);
998 /*x86_push_imm (code, method);
999 mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1000 x86_call_code (code, 0);
1001 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1004 switch (save_mode) {
1006 //x86_pop_reg (code, X86_EAX);
1007 //x86_pop_reg (code, X86_EDX);
1010 //x86_pop_reg (code, X86_EAX);
1013 //x86_fld_membase (code, X86_ESP, 0, TRUE);
1014 //x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1024 #define EMIT_COND_BRANCH(ins,cond) \
1025 if (ins->flags & MONO_INST_BRLABEL) { \
1026 if (ins->inst_i0->inst_c0) { \
1028 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1031 if (0 && ins->inst_true_bb->native_offset) { \
1033 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1037 /* emit an exception if condition is fail */
1038 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1040 mono_add_patch_info (cfg, code - cfg->native_code, \
1041 MONO_PATCH_INFO_EXC, exc_name); \
1042 x86_branch32 (code, cond, 0, signed); \
1045 #define EMIT_FPCOMPARE(code) do { \
1046 x86_fcompp (code); \
1047 x86_fnstsw (code); \
1048 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500); \
1052 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1054 MonoInst *ins, *last_ins = NULL;
1057 /* short circuit this for now */
1062 switch (ins->opcode) {
1064 /* remove unnecessary multiplication with 1 */
1065 if (ins->inst_imm == 1) {
1066 if (ins->dreg != ins->sreg1) {
1067 ins->opcode = OP_MOVE;
1069 last_ins->next = ins->next;
1075 case OP_LOAD_MEMBASE:
1076 case OP_LOADI4_MEMBASE:
1078 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1079 * OP_LOAD_MEMBASE offset(basereg), reg
1081 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1082 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1083 ins->inst_basereg == last_ins->inst_destbasereg &&
1084 ins->inst_offset == last_ins->inst_offset) {
1085 if (ins->dreg == last_ins->sreg1) {
1086 last_ins->next = ins->next;
1090 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1091 ins->opcode = OP_MOVE;
1092 ins->sreg1 = last_ins->sreg1;
1096 * Note: reg1 must be different from the basereg in the second load
1097 * OP_LOAD_MEMBASE offset(basereg), reg1
1098 * OP_LOAD_MEMBASE offset(basereg), reg2
1100 * OP_LOAD_MEMBASE offset(basereg), reg1
1101 * OP_MOVE reg1, reg2
1103 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1104 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1105 ins->inst_basereg != last_ins->dreg &&
1106 ins->inst_basereg == last_ins->inst_basereg &&
1107 ins->inst_offset == last_ins->inst_offset) {
1109 if (ins->dreg == last_ins->dreg) {
1110 last_ins->next = ins->next;
1114 ins->opcode = OP_MOVE;
1115 ins->sreg1 = last_ins->dreg;
1118 //g_assert_not_reached ();
1122 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1123 * OP_LOAD_MEMBASE offset(basereg), reg
1125 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1126 * OP_ICONST reg, imm
1128 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1129 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1130 ins->inst_basereg == last_ins->inst_destbasereg &&
1131 ins->inst_offset == last_ins->inst_offset) {
1132 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1133 ins->opcode = OP_ICONST;
1134 ins->inst_c0 = last_ins->inst_imm;
1135 g_assert_not_reached (); // check this rule
1139 case OP_LOADU1_MEMBASE:
1140 case OP_LOADI1_MEMBASE:
1141 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1142 ins->inst_basereg == last_ins->inst_destbasereg &&
1143 ins->inst_offset == last_ins->inst_offset) {
1144 if (ins->dreg == last_ins->sreg1) {
1145 last_ins->next = ins->next;
1149 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1150 ins->opcode = OP_MOVE;
1151 ins->sreg1 = last_ins->sreg1;
1155 case OP_LOADU2_MEMBASE:
1156 case OP_LOADI2_MEMBASE:
1157 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1158 ins->inst_basereg == last_ins->inst_destbasereg &&
1159 ins->inst_offset == last_ins->inst_offset) {
1160 if (ins->dreg == last_ins->sreg1) {
1161 last_ins->next = ins->next;
1165 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1166 ins->opcode = OP_MOVE;
1167 ins->sreg1 = last_ins->sreg1;
1177 if (ins->dreg == ins->sreg1) {
1179 last_ins->next = ins->next;
1184 * OP_MOVE sreg, dreg
1185 * OP_MOVE dreg, sreg
1187 if (last_ins && last_ins->opcode == OP_MOVE &&
1188 ins->sreg1 == last_ins->dreg &&
1189 ins->dreg == last_ins->sreg1) {
1190 last_ins->next = ins->next;
1199 bb->last_ins = last_ins;
1203 * returns the offset used by spillvar. It allocates a new
1204 * spill variable if necessary. Likely incorrect for sparc.
1207 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1209 MonoSpillInfo **si, *info;
1212 si = &cfg->spill_info;
1214 while (i <= spillvar) {
1217 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1219 cfg->stack_offset -= sizeof (gpointer);
1220 info->offset = cfg->stack_offset;
1224 return (*si)->offset;
1230 g_assert_not_reached ();
1235 #define DEBUG(a) if (cfg->verbose_level > 1) a
1237 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1246 static const char*const * ins_spec = sparc_desc;
1249 print_ins (int i, MonoInst *ins)
1251 const char *spec = ins_spec [ins->opcode];
1252 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1253 if (spec [MONO_INST_DEST]) {
1254 if (ins->dreg >= MONO_MAX_IREGS)
1255 g_print (" R%d <-", ins->dreg);
1257 g_print (" %s <-", mono_arch_regname (ins->dreg));
1259 if (spec [MONO_INST_SRC1]) {
1260 if (ins->sreg1 >= MONO_MAX_IREGS)
1261 g_print (" R%d", ins->sreg1);
1263 g_print (" %s", mono_arch_regname (ins->sreg1));
1265 if (spec [MONO_INST_SRC2]) {
1266 if (ins->sreg2 >= MONO_MAX_IREGS)
1267 g_print (" R%d", ins->sreg2);
1269 g_print (" %s", mono_arch_regname (ins->sreg2));
1271 if (spec [MONO_INST_CLOB])
1272 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1277 print_regtrack (RegTrack *t, int num)
1283 for (i = 0; i < num; ++i) {
1286 if (i >= MONO_MAX_IREGS) {
1287 g_snprintf (buf, sizeof(buf), "R%d", i);
1290 r = mono_arch_regname (i);
1291 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1295 typedef struct InstList InstList;
1303 static inline InstList*
1304 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1306 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1316 * Force the spilling of the variable in the symbolic register 'reg'.
1319 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1324 sel = cfg->rs->iassign [reg];
1325 /*i = cfg->rs->isymbolic [sel];
1326 g_assert (i == reg);*/
1328 spill = ++cfg->spill_count;
1329 cfg->rs->iassign [i] = -spill - 1;
1330 mono_regstate_free_int (cfg->rs, sel);
1331 /* we need to create a spill var and insert a load to sel after the current instruction */
1332 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1334 load->inst_basereg = cfg->frame_reg;
1335 load->inst_offset = mono_spillvar_offset (cfg, spill);
1337 while (ins->next != item->prev->data)
1340 load->next = ins->next;
1342 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1343 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1344 g_assert (i == sel);
1350 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1355 DEBUG (g_print ("start regmask to assign R%d: 0x%08x (R%d <- R%d R%d)\n", reg, regmask, ins->dreg, ins->sreg1, ins->sreg2));
1356 /* exclude the registers in the current instruction */
1357 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1358 if (ins->sreg1 >= MONO_MAX_IREGS)
1359 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1361 regmask &= ~ (1 << ins->sreg1);
1362 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1364 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1365 if (ins->sreg2 >= MONO_MAX_IREGS)
1366 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1368 regmask &= ~ (1 << ins->sreg2);
1369 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1371 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1372 regmask &= ~ (1 << ins->dreg);
1373 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1376 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1377 g_assert (regmask); /* need at least a register we can free */
1379 /* we should track prev_use and spill the register that's farther */
1380 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1381 if (regmask & (1 << i)) {
1383 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1387 i = cfg->rs->isymbolic [sel];
1388 spill = ++cfg->spill_count;
1389 cfg->rs->iassign [i] = -spill - 1;
1390 mono_regstate_free_int (cfg->rs, sel);
1391 /* we need to create a spill var and insert a load to sel after the current instruction */
1392 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1394 load->inst_basereg = cfg->frame_reg;
1395 load->inst_offset = mono_spillvar_offset (cfg, spill);
1397 while (ins->next != item->prev->data)
1400 load->next = ins->next;
1402 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1403 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1404 g_assert (i == sel);
1410 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1413 MONO_INST_NEW (cfg, copy, OP_MOVE);
1417 copy->next = ins->next;
1420 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1425 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1428 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1430 store->inst_destbasereg = sparc_sp;
1431 store->inst_offset = mono_spillvar_offset (cfg, spill);
1433 store->next = ins->next;
1436 DEBUG (g_print ("SPILLED STORE (%d at 0x%08x(%%sp)) R%d (from %s)\n", spill, store->inst_offset, prev_reg, mono_arch_regname (reg)));
1441 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1444 g_assert (item->next);
1445 prev = item->next->data;
1447 while (prev->next != ins)
1449 to_insert->next = ins;
1450 prev->next = to_insert;
1452 * needed otherwise in the next instruction we can add an ins to the
1453 * end and that would get past this instruction.
1455 item->data = to_insert;
1459 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1461 int val = cfg->rs->iassign [sym_reg];
1465 /* the register gets spilled after this inst */
1468 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1470 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1471 cfg->rs->iassign [sym_reg] = val;
1472 /* add option to store before the instruction for src registers */
1474 create_spilled_store (cfg, spill, val, sym_reg, ins);
1476 cfg->rs->isymbolic [val] = sym_reg;
1481 * Local register allocation.
1482 * We first scan the list of instructions and we save the liveness info of
1483 * each register (when the register is first used, when it's value is set etc.).
1484 * We also reverse the list of instructions (in the InstList list) because assigning
1485 * registers backwards allows for more tricks to be used.
1488 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1491 MonoRegState *rs = cfg->rs;
1492 int i, val, fpcount;
1493 RegTrack *reginfo, *reginfof;
1494 RegTrack *reginfo1, *reginfo2, *reginfod;
1495 InstList *tmp, *reversed = NULL;
1497 guint32 src1_mask, src2_mask, dest_mask;
1501 rs->next_vireg = bb->max_ireg;
1502 rs->next_vfreg = bb->max_freg;
1503 mono_regstate_assign (rs);
1504 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1505 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1506 rs->ifree_mask = 0xdeadbeef; /* FIXME */
1510 fpcount = 0; /* FIXME: track fp stack utilization */
1511 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1512 /* forward pass on the instructions to collect register liveness info */
1514 spec = ins_spec [ins->opcode];
1515 DEBUG (print_ins (i, ins));
1516 if (spec [MONO_INST_SRC1]) {
1517 if (spec [MONO_INST_SRC1] == 'f')
1518 reginfo1 = reginfof;
1521 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1522 reginfo1 [ins->sreg1].last_use = i;
1526 if (spec [MONO_INST_SRC2]) {
1527 if (spec [MONO_INST_SRC2] == 'f')
1528 reginfo2 = reginfof;
1531 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1532 reginfo2 [ins->sreg2].last_use = i;
1536 if (spec [MONO_INST_DEST]) {
1537 if (spec [MONO_INST_DEST] == 'f')
1538 reginfod = reginfof;
1541 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1542 reginfod [ins->dreg].killed_in = i;
1543 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1544 reginfod [ins->dreg].last_use = i;
1545 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1546 reginfod [ins->dreg].born_in = i;
1547 if (spec [MONO_INST_DEST] == 'l') {
1548 /* result in eax:edx, the virtual register is allocated sequentially */
1549 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1550 reginfod [ins->dreg + 1].last_use = i;
1551 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1552 reginfod [ins->dreg + 1].born_in = i;
1557 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1562 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1563 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1566 int prev_dreg, prev_sreg1, prev_sreg2;
1567 dest_mask = src1_mask = src2_mask = 0xdeadbeef; /* FIXME */
1570 spec = ins_spec [ins->opcode];
1571 DEBUG (g_print ("processing:"));
1572 DEBUG (print_ins (i, ins));
1573 /* update for use with FP regs... */
1574 if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
1575 val = rs->iassign [ins->dreg];
1576 prev_dreg = ins->dreg;
1580 /* the register gets spilled after this inst */
1583 val = mono_regstate_alloc_int (rs, dest_mask);
1585 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1586 rs->iassign [ins->dreg] = val;
1588 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1590 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1591 rs->isymbolic [val] = prev_dreg;
1593 if (spec [MONO_INST_DEST] == 'l') {
1594 int hreg = prev_dreg + 1;
1595 val = rs->iassign [hreg];
1599 /* the register gets spilled after this inst */
1602 val = mono_regstate_alloc_int (rs, dest_mask);
1604 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1605 rs->iassign [hreg] = val;
1607 create_spilled_store (cfg, spill, val, hreg, ins);
1609 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1610 rs->isymbolic [val] = hreg;
1611 /* FIXME:? ins->dreg = val; */
1612 if (ins->dreg == sparc_l1) {
1613 if (val != sparc_l0)
1614 create_copy_ins (cfg, val, sparc_l0, ins);
1615 } else if (ins->dreg == sparc_l0) {
1616 if (val == sparc_l1) {
1618 create_copy_ins (cfg, sparc_l2, sparc_l0, ins);
1619 create_copy_ins (cfg, sparc_l0, sparc_l1, ins);
1620 create_copy_ins (cfg, sparc_l1, sparc_l2, ins);
1622 /* two forced copies */
1623 create_copy_ins (cfg, val, sparc_l0, ins);
1624 create_copy_ins (cfg, ins->dreg, sparc_l1, ins);
1627 if (val == sparc_l0) {
1628 create_copy_ins (cfg, ins->dreg, sparc_l1, ins);
1630 /* two forced copies */
1631 create_copy_ins (cfg, val, sparc_l0, ins);
1632 create_copy_ins (cfg, ins->dreg, sparc_l1, ins);
1635 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1636 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1637 mono_regstate_free_int (rs, val);
1643 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1644 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1645 mono_regstate_free_int (rs, ins->dreg);
1647 if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
1648 val = rs->iassign [ins->sreg1];
1649 prev_sreg1 = ins->sreg1;
1653 /* the register gets spilled after this inst */
1656 if (0 && ins->opcode == OP_MOVE) {
1658 * small optimization: the dest register is already allocated
1659 * but the src one is not: we can simply assign the same register
1660 * here and peephole will get rid of the instruction later.
1661 * This optimization may interfere with the clobbering handling:
1662 * it removes a mov operation that will be added again to handle clobbering.
1663 * There are also some other issues that should with make testjit.
1665 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1666 val = rs->iassign [ins->sreg1] = ins->dreg;
1667 //g_assert (val >= 0);
1668 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1670 //g_assert (val == -1); /* source cannot be spilled */
1671 val = mono_regstate_alloc_int (rs, src1_mask);
1673 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1674 rs->iassign [ins->sreg1] = val;
1675 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1678 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1679 insert_before_ins (ins, tmp, store);
1682 rs->isymbolic [val] = prev_sreg1;
1687 if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
1688 val = rs->iassign [ins->sreg2];
1689 prev_sreg2 = ins->sreg2;
1693 /* the register gets spilled after this inst */
1696 val = mono_regstate_alloc_int (rs, src2_mask);
1698 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1699 rs->iassign [ins->sreg2] = val;
1700 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1702 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1704 rs->isymbolic [val] = prev_sreg2;
1710 if (spec [MONO_INST_CLOB] == 'c') {
1712 guint32 clob_mask = 0xdeadbeef; /* FIXME */
1713 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1715 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1716 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1720 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1721 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1722 mono_regstate_free_int (rs, ins->sreg1);
1724 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1725 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1726 mono_regstate_free_int (rs, ins->sreg2);
1729 //DEBUG (print_ins (i, ins));
1735 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1740 static unsigned char*
1741 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1747 sparc_patch (guchar *code, guchar *target)
1749 guint32 ins = *(guint32*)code;
1750 guint32 prim = ins >> 26;
1752 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1756 guint32 li = (guint32)target;
1757 ins = prim << 26 | (ins & 3);
1759 // FIXME: assert the top bits of li are 0
1761 gint diff = target - code;
1762 ins = prim << 26 | (ins & 3);
1764 diff &= ~(63 << 26);
1767 *(guint32*)code = ins;
1768 } else if (prim == 16) {
1771 guint32 li = (guint32)target;
1772 ins = (ins & 0xffff0000) | (ins & 3);
1775 // FIXME: assert the top bits of li are 0
1777 gint diff = target - code;
1778 ins = (ins & 0xffff0000) | (ins & 3);
1782 *(guint32*)code = ins;
1784 g_assert_not_reached ();
1786 // g_print ("patched with 0x%08x\n", ins);
1790 * Some conventions used in the following code.
1791 * 1) We're assuming a V9 CPU. We will check for that later.
1792 * In reality, we're mostly sticking with V8 instructions...
1793 * 2) The only scratch registers we have are o7 and g1. We try to
1794 * stick to o7 when we can, and use g1 when necessary.
1798 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1803 guint8 *code = cfg->native_code + cfg->code_len;
1804 MonoInst *last_ins = NULL;
1805 guint last_offset = 0;
1808 if (cfg->opt & MONO_OPT_PEEPHOLE)
1809 peephole_pass (cfg, bb);
1813 * various stratgies to align BBs. Using real loop detection or simply
1814 * aligning every block leads to more consistent benchmark results,
1815 * but usually slows down the code
1816 * we should do the alignment outside this function or we should adjust
1817 * bb->native offset as well or the code is effectively slowed down!
1819 /* align all blocks */
1820 // if ((pad = (cfg->code_len & (align - 1)))) {
1821 /* poor man loop start detection */
1822 // if (bb->code && bb->in_count && bb->in_bb [0]->cil_code > bb->cil_code && (pad = (cfg->code_len & (align - 1)))) {
1823 /* consider real loop detection and nesting level */
1824 // if (bb->loop_blocks && bb->nesting < 3 && (pad = (cfg->code_len & (align - 1)))) {
1825 /* consider real loop detection */
1826 if (/*bb->loop_blocks &&*/ (pad = (cfg->code_len & (align - 1)))) {
1828 x86_padding (code, pad);
1829 cfg->code_len += pad;
1830 bb->native_offset = cfg->code_len;
1834 if (cfg->verbose_level > 2)
1835 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1837 cpos = bb->max_offset;
1839 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1840 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
1841 //g_assert (!mono_compile_aot);
1844 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
1845 /* this is not thread save, but good enough */
1846 /* fixme: howto handle overflows? */
1847 //x86_inc_mem (code, &cov->data [bb->dfn].count);
1852 offset = code - cfg->native_code;
1854 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1856 if (offset > (cfg->code_size - max_len - 16)) {
1857 cfg->code_size *= 2;
1858 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1859 code = cfg->native_code + offset;
1861 // if (ins->cil_code)
1862 // g_print ("cil code\n");
1864 switch (ins->opcode) {
1865 case OP_STOREI1_MEMBASE_IMM:
1866 sparc_set (code, ins->inst_imm, sparc_o7);
1867 // g_assert (ppc_is_imm16 (ins->inst_offset));
1868 sparc_stb_imm (code, sparc_o7, ins->inst_offset, ins->inst_destbasereg);
1870 case OP_STOREI2_MEMBASE_IMM:
1871 sparc_set (code, ins->inst_imm, sparc_o7);
1872 // g_assert (ppc_is_imm16 (ins->inst_offset));
1873 sparc_sth_imm (code, sparc_o7, ins->inst_offset, ins->inst_destbasereg);
1875 case OP_STORE_MEMBASE_IMM:
1876 case OP_STOREI4_MEMBASE_IMM:
1877 sparc_set (code, ins->inst_imm, sparc_o7);
1878 // g_assert (ppc_is_imm16 (ins->inst_offset));
1879 sparc_st_imm (code, sparc_o7, ins->inst_offset, ins->inst_destbasereg);
1881 case OP_STOREI1_MEMBASE_REG:
1882 // g_assert (ppc_is_imm16 (ins->inst_offset));
1883 sparc_stb_imm (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1885 case OP_STOREI2_MEMBASE_REG:
1886 // g_assert (ppc_is_imm16 (ins->inst_offset));
1887 sparc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1889 case OP_STORE_MEMBASE_REG:
1890 case OP_STOREI4_MEMBASE_REG:
1891 // g_assert (ppc_is_imm16 (ins->inst_offset));
1892 sparc_st (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1897 sparc_ld (code, ins->inst_p0, sparc_g0, ins->dreg);
1899 /* The cast IS BAD (maybe). But it needs to be done... */
1901 sparc_set (code, (guint)ins->inst_p0, ins->dreg);
1902 sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
1904 case OP_LOAD_MEMBASE:
1905 case OP_LOADI4_MEMBASE:
1906 case OP_LOADU4_MEMBASE:
1907 if (TRUE) { /* FIXME */
1908 sparc_ld_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1910 sparc_ld (code, sparc_l0, 0, ins->inst_offset);
1911 sparc_ld (code, ins->dreg, 0, sparc_l0);
1914 case OP_LOADU1_MEMBASE:
1915 // g_assert (ppc_is_imm16 (ins->inst_offset));
1916 sparc_ldub_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1918 case OP_LOADI1_MEMBASE:
1919 // g_assert (ppc_is_imm16 (ins->inst_offset));
1920 sparc_ldsb_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1922 case OP_LOADU2_MEMBASE:
1923 // g_assert (ppc_is_imm16 (ins->inst_offset));
1924 sparc_lduh_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1926 case OP_LOADI2_MEMBASE:
1927 sparc_ldsh_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1930 sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
1931 sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
1934 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
1935 sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
1937 /* GCC does this one differently. Don't ask me WHY. */
1939 sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
1942 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
1943 sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
1946 sparc_cmp (code, ins->sreg1, ins->sreg2);
1948 case OP_COMPARE_IMM:
1949 if (TRUE) { /* FIXME */
1950 sparc_cmp_imm (code, ins->sreg1, (ins->inst_imm & 0x1fff));
1952 sparc_set (code, ins->inst_imm, sparc_o7);
1953 sparc_cmp (code, ins->sreg1, sparc_o7);
1956 case OP_X86_TEST_NULL:
1957 sparc_cmp_imm (code, ins->sreg1, 0);
1960 g_assert_not_reached();
1963 sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
1966 sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1969 sparc_addx (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1972 if (TRUE) { /* FIXME */
1973 sparc_add_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1975 sparc_ld (code, sparc_l0, 0, ins->inst_imm);
1976 sparc_add (code, 0, ins->dreg, ins->sreg1, sparc_l0);
1980 sparc_addx (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1983 sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
1986 sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1989 sparc_subx (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1992 // we add the negated value
1993 // g_assert (ppc_is_imm16 (-ins->inst_imm));
1994 sparc_add_imm (code, FALSE, ins->sreg1, -ins->inst_imm, ins->dreg);
1997 sparc_subx_imm (code, FALSE, ins->sreg2, ins->inst_imm, ins->dreg);
2000 sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2003 sparc_and_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
2006 sparc_sdiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2009 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2012 sparc_sdiv_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
2015 sparc_sdiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2016 sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2017 sparc_sub (code, FALSE, sparc_o7, ins->sreg1, ins->dreg);
2020 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2021 sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2022 sparc_sub (code, FALSE, sparc_o7, ins->sreg1, ins->dreg);
2025 sparc_sdiv_imm (code, FALSE, ins->sreg1, ins->inst_imm, sparc_o7);
2026 sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
2027 sparc_sub (code, FALSE, sparc_o7, ins->sreg1, ins->dreg);
2030 sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2033 sparc_set (code, ins->inst_imm, sparc_o7);
2034 sparc_or (code, FALSE, ins->sreg2, sparc_o7, ins->dreg);
2037 sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2040 sparc_set (code, ins->inst_imm, sparc_o7);
2041 sparc_xor (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2044 sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
2047 sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2050 sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
2053 sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2056 sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2059 sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
2062 /* can't use sparc_not */
2063 sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
2066 /* can't use sparc_neg */
2067 sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
2070 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2073 sparc_smul_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
2076 /* FIXME: this isn't right, I don't think */
2077 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2079 case CEE_MUL_OVF_UN:
2080 /* FIXME: This isn't right either */
2081 sparc_umul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2085 sparc_set (code, ins->inst_c0, ins->dreg);
2088 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_CLASS, (gpointer)ins->inst_c0);
2091 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_IMAGE, (gpointer)ins->inst_c0);
2097 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2100 g_assert_not_reached ();
2102 * Copied roughly from x86. Probably doesn't work
2105 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2106 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2107 /* reset offset to make max_len work */
2108 offset = code - cfg->native_code;
2110 g_assert (!cfg->method->save_lmf);
2112 offset = code - cfg->native_code;
2113 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2114 sparc_jmp_imm (code, sparc_g0, 0);
2118 /* ensure ins->sreg1 is not NULL */
2119 sparc_cmp_imm (code, ins->sreg1, 0);
2126 call = (MonoCallInst*)ins;
2127 if (ins->flags & MONO_INST_HAS_METHOD)
2128 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2130 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2131 sparc_call_simple (code, 0);
2136 case OP_VOIDCALL_REG:
2138 call = (MonoCallInst*)ins;
2139 sparc_call (code, ins->sreg1, sparc_g0);
2140 /* FIXME: yea, a store in g0 is a GOOD IDEA */
2141 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2142 sparc_add_imm (code, FALSE, sparc_sp, call->stack_usage, sparc_g0);
2144 case OP_FCALL_MEMBASE:
2145 case OP_LCALL_MEMBASE:
2146 case OP_VCALL_MEMBASE:
2147 case OP_VOIDCALL_MEMBASE:
2148 case OP_CALL_MEMBASE:
2149 call = (MonoCallInst*)ins;
2150 sparc_call_imm (code, ins->sreg1, ins->inst_offset);
2151 /* FIXME: yea, a store in g0 is a GOOD IDEA */
2152 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2153 sparc_add_imm (code, FALSE, sparc_sp, call->stack_usage, sparc_g0);
2158 /* FIXME: This can be SO far wrong! */
2159 sparc_st (code, ins->sreg1, sparc_g0, sparc_sp);
2160 sparc_sub_imm (code, FALSE, sparc_sp, 4, sparc_sp);
2163 /* keep alignment */
2164 #define MONO_FRAME_ALIGNMENT 32
2165 sparc_add_imm (code, 0, ins->sreg1, MONO_FRAME_ALIGNMENT-1, sparc_o7);
2166 //ppc_rlwinm (code, sparc_l0, sparc_l0, 0, 0, 27);
2167 //ppc_lwz (code, sparc_l0, 0, ppc_sp);
2168 // Fix semantics to negate to another reg? FIXME
2169 //sparc_neg (code, sparc_l0, sparc_l0);
2170 //ppc_stwux (code, ppc_sp, sparc_l0, ppc_sp);
2171 //ppc_mr (code, ins->dreg, ppc_sp);
2172 g_assert_not_reached ();
2178 sparc_mov_reg_reg (code, ins->sreg1, sparc_sp);
2179 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2180 (gpointer)"throw_exception");
2181 sparc_call_simple (code, 0);
2185 if (ins->sreg1 != sparc_sp)
2186 sparc_mov_reg_reg (code, ins->sreg1, sparc_sp);
2189 case CEE_ENDFINALLY:
2192 case OP_CALL_HANDLER:
2193 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2194 sparc_call_simple (code, 0);
2197 ins->inst_c0 = code - cfg->native_code;
2200 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2201 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2203 if (ins->flags & MONO_INST_BRLABEL) {
2204 if (ins->inst_i0->inst_c0) {
2205 sparc_call_simple (code, 0);
2207 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2208 sparc_call_simple (code, 0);
2211 if (ins->inst_target_bb->native_offset) {
2212 sparc_call_simple (code, 0);
2214 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2215 sparc_call_simple (code, 0);
2220 sparc_jmp (code, ins->sreg1, sparc_g0);
2223 //ppc_li (code, ins->dreg, 0);
2224 //ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2225 //ppc_li (code, ins->dreg, 1);
2229 //ppc_li (code, ins->dreg, 1);
2230 //ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2231 //ppc_li (code, ins->dreg, 0);
2235 //ppc_li (code, ins->dreg, 1);
2236 //ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2237 //ppc_li (code, ins->dreg, 0);
2239 case OP_COND_EXC_EQ:
2240 case OP_COND_EXC_NE_UN:
2241 case OP_COND_EXC_LT:
2242 case OP_COND_EXC_LT_UN:
2243 case OP_COND_EXC_GT:
2244 case OP_COND_EXC_GT_UN:
2245 case OP_COND_EXC_GE:
2246 case OP_COND_EXC_GE_UN:
2247 case OP_COND_EXC_LE:
2248 case OP_COND_EXC_LE_UN:
2249 case OP_COND_EXC_OV:
2250 case OP_COND_EXC_NO:
2252 case OP_COND_EXC_NC:
2253 //EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2254 // (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2266 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2269 /* floating point opcodes */
2271 //ppc_load (code, sparc_l0, ins->inst_p0);
2272 //ppc_lfd (code, ins->dreg, 0, sparc_l0);
2275 //ppc_load (code, sparc_l0, ins->inst_p0);
2276 //ppc_lfs (code, ins->dreg, 0, sparc_l0);
2278 case OP_STORER8_MEMBASE_REG:
2279 //ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2281 case OP_LOADR8_MEMBASE:
2282 //ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2284 case OP_STORER4_MEMBASE_REG:
2285 //ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2287 case OP_LOADR4_MEMBASE:
2288 //ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2290 case CEE_CONV_R4: /* FIXME: change precision */
2292 g_assert_not_reached ();
2293 //x86_push_reg (code, ins->sreg1);
2294 //x86_fild_membase (code, X86_ESP, 0, FALSE);
2295 //x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2297 case OP_X86_FP_LOAD_I8:
2298 g_assert_not_reached ();
2299 //x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2301 case OP_X86_FP_LOAD_I4:
2302 g_assert_not_reached ();
2303 //x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2305 case OP_FCONV_TO_I1:
2306 g_assert_not_reached ();
2307 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2309 case OP_FCONV_TO_U1:
2310 g_assert_not_reached ();
2311 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2313 case OP_FCONV_TO_I2:
2314 g_assert_not_reached ();
2315 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2317 case OP_FCONV_TO_U2:
2318 g_assert_not_reached ();
2319 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2321 case OP_FCONV_TO_I4:
2323 g_assert_not_reached ();
2324 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2326 case OP_FCONV_TO_U4:
2328 g_assert_not_reached ();
2329 code = emit_float_to_int (cfg, code, ins->dreg, 4, FALSE);
2331 case OP_FCONV_TO_I8:
2332 case OP_FCONV_TO_U8:
2333 g_assert_not_reached ();
2334 /*x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2335 x86_fnstcw_membase(code, X86_ESP, 0);
2336 x86_mov_reg_membase (code, ins->inst_dreg_low, X86_ESP, 0, 2);
2337 x86_alu_reg_imm (code, X86_OR, ins->inst_dreg_low, 0xc00);
2338 x86_mov_membase_reg (code, X86_ESP, 2, ins->inst_dreg_low, 2);
2339 x86_fldcw_membase (code, X86_ESP, 2);
2340 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2341 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2342 x86_pop_reg (code, ins->inst_dreg_low);
2343 x86_pop_reg (code, ins->inst_dreg_high);
2344 x86_fldcw_membase (code, X86_ESP, 0);
2345 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);*/
2347 case OP_LCONV_TO_R_UN: {
2349 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2352 /* load 64bit integer to FP stack */
2353 x86_push_imm (code, 0);
2354 x86_push_reg (code, ins->sreg2);
2355 x86_push_reg (code, ins->sreg1);
2356 x86_fild_membase (code, X86_ESP, 0, TRUE);
2357 /* store as 80bit FP value */
2358 x86_fst80_membase (code, X86_ESP, 0);
2360 /* test if lreg is negative */
2361 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2362 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2364 /* add correction constant mn */
2365 x86_fld80_mem (code, mn);
2366 x86_fld80_membase (code, X86_ESP, 0);
2367 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2368 x86_fst80_membase (code, X86_ESP, 0);
2370 x86_patch (br, code);
2372 x86_fld80_membase (code, X86_ESP, 0);
2373 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2375 g_assert_not_reached ();
2378 case OP_LCONV_TO_OVF_I: {
2380 guint8 *br [3], *label [1];
2383 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2385 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2387 /* If the low word top bit is set, see if we are negative */
2388 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2389 /* We are not negative (no top bit set, check for our top word to be zero */
2390 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2391 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2394 /* throw exception */
2395 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2396 x86_jump32 (code, 0);
2398 x86_patch (br [0], code);
2399 /* our top bit is set, check that top word is 0xfffffff */
2400 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2402 x86_patch (br [1], code);
2403 /* nope, emit exception */
2404 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2405 x86_patch (br [2], label [0]);
2407 if (ins->dreg != ins->sreg1)
2408 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2410 g_assert_not_reached ();
2414 sparc_fadds( code, ins->sreg1, ins->sreg2, ins->dreg);
2417 sparc_fsubs( code, ins->sreg1, ins->sreg2, ins->dreg );
2420 sparc_fmuls( code, ins->sreg1, ins->sreg2, ins->dreg );
2423 sparc_fdivs( code, ins->sreg1, ins->sreg2, ins->dreg );
2426 sparc_fnegs( code, ins->sreg1, ins->sreg2, ins->dreg );
2429 g_assert_not_reached ();
2432 sparc_fcmps( code, ins->sreg1, ins->sreg2 );
2433 /* this overwrites EAX */
2434 //EMIT_FPCOMPARE(code);
2437 g_assert_not_reached();
2438 /*if (ins->dreg != X86_EAX)
2439 x86_push_reg (code, X86_EAX);
2441 EMIT_FPCOMPARE(code);
2442 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2443 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2444 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2446 if (ins->dreg != X86_EAX)
2447 x86_pop_reg (code, X86_EAX);*/
2451 g_assert_not_reached ();
2452 /*if (ins->dreg != X86_EAX)
2453 x86_push_reg (code, X86_EAX);
2455 EMIT_FPCOMPARE(code);
2456 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2457 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2459 if (ins->dreg != X86_EAX)
2460 x86_pop_reg (code, X86_EAX);*/
2464 g_assert_not_reached ();
2465 /*if (ins->dreg != X86_EAX)
2466 x86_push_reg (code, X86_EAX);
2468 EMIT_FPCOMPARE(code);
2469 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2470 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2471 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2473 if (ins->dreg != X86_EAX)
2474 x86_pop_reg (code, X86_EAX);*/
2477 g_assert_not_reached ();
2480 g_assert_not_reached ();
2484 g_assert_not_reached ();
2488 g_assert_not_reached ();
2492 g_assert_not_reached ();
2496 g_assert_not_reached ();
2498 case CEE_CKFINITE: {
2500 g_assert_not_reached ();
2501 //x86_push_reg (code, X86_EAX);
2503 //x86_fnstsw (code);
2504 //x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2505 //x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2506 //x86_pop_reg (code, X86_EAX);
2507 //EMIT_COND_SYSTEM_EXCEPTION (FALSE, FALSE, "ArithmeticException");
2511 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2512 g_assert_not_reached ();
2515 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2516 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2517 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2518 g_assert_not_reached ();
2524 last_offset = offset;
2529 cfg->code_len = code - cfg->native_code;
2533 mono_arch_register_lowlevel_calls (void)
2535 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2536 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2540 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji)
2542 MonoJumpInfo *patch_info;
2544 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2545 unsigned char *ip = patch_info->ip.i + code;
2546 const unsigned char *target = NULL;
2548 switch (patch_info->type) {
2549 case MONO_PATCH_INFO_BB:
2550 target = patch_info->data.bb->native_offset + code;
2552 case MONO_PATCH_INFO_ABS:
2553 target = patch_info->data.target;
2555 case MONO_PATCH_INFO_LABEL:
2556 target = patch_info->data.inst->inst_c0 + code;
2558 case MONO_PATCH_INFO_IP:
2559 *((gpointer *)(ip)) = ip;
2561 case MONO_PATCH_INFO_INTERNAL_METHOD: {
2562 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
2564 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
2565 g_assert_not_reached ();
2567 target = mi->wrapper;
2570 case MONO_PATCH_INFO_METHOD:
2571 if (patch_info->data.method == method) {
2574 /* get the trampoline to the method from the domain */
2575 target = mono_arch_create_jit_trampoline (patch_info->data.method);
2578 case MONO_PATCH_INFO_SWITCH: {
2579 gpointer *table = (gpointer *)patch_info->data.target;
2582 // FIXME: inspect code to get the register
2583 //ppc_load (ip, sparc_l0, patch_info->data.target);
2584 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
2586 for (i = 0; i < patch_info->table_size; i++) {
2587 table [i] = (int)patch_info->data.table [i] + code;
2589 /* we put into the table the absolute address, no need for sparc_patch in this case */
2592 case MONO_PATCH_INFO_METHODCONST:
2593 case MONO_PATCH_INFO_CLASS:
2594 case MONO_PATCH_INFO_IMAGE:
2595 case MONO_PATCH_INFO_FIELD:
2596 g_assert_not_reached ();
2597 *((gconstpointer *)(ip + 1)) = patch_info->data.target;
2599 case MONO_PATCH_INFO_R4:
2600 case MONO_PATCH_INFO_R8:
2601 g_assert_not_reached ();
2602 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
2605 g_assert_not_reached ();
2607 sparc_patch (ip, target);
2612 mono_arch_max_epilog_size (MonoCompile *cfg)
2614 int exc_count = 0, max_epilog_size = 16 + 20*4;
2615 MonoJumpInfo *patch_info;
2617 if (cfg->method->save_lmf)
2618 max_epilog_size += 128;
2620 if (mono_jit_trace_calls != NULL)
2621 max_epilog_size += 50;
2623 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2624 max_epilog_size += 50;
2626 /* count the number of exception infos */
2628 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2629 if (patch_info->type == MONO_PATCH_INFO_EXC)
2634 * make sure we have enough space for exceptions
2635 * 16 is the size of two push_imm instructions and a call
2637 max_epilog_size += exc_count*16;
2639 return max_epilog_size;
2643 mono_arch_emit_prolog (MonoCompile *cfg)
2645 MonoMethod *method = cfg->method;
2647 MonoMethodSignature *sig;
2649 int alloc_size, pos, max_offset, i;
2653 cfg->code_size = 256;
2654 code = cfg->native_code = g_malloc (cfg->code_size);
2656 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2657 //ppc_mflr (code, sparc_l0);
2658 //ppc_stw (code, sparc_l0, 8, ppc_sp);
2660 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
2661 cfg->used_int_regs |= 1 << 31;
2664 alloc_size = cfg->stack_offset;
2667 if (method->save_lmf) {
2669 pos += sizeof (MonoLMF);
2671 /* save the current IP */
2672 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2673 x86_push_imm (code, 0);
2675 /* save all caller saved regs */
2676 x86_push_reg (code, X86_EBX);
2677 x86_push_reg (code, X86_EDI);
2678 x86_push_reg (code, X86_ESI);
2679 x86_push_reg (code, X86_EBP);
2681 /* save method info */
2682 x86_push_imm (code, method);
2684 /* get the address of lmf for the current thread */
2685 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2686 (gpointer)"get_lmf_addr");
2687 x86_call_code (code, 0);
2690 x86_push_reg (code, X86_EAX);
2691 /* push *lfm (previous_lmf) */
2692 x86_push_membase (code, X86_EAX, 0);
2694 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
2698 for (i = 13; i < 32; ++i) {
2699 if (cfg->used_int_regs & (1 << i)) {
2701 //ppc_stw (code, i, -pos, ppc_sp);
2707 // align to 16 bytes
2708 if (alloc_size & (16 - 1))
2709 alloc_size += 16 - (alloc_size & (16 - 1));
2711 cfg->stack_usage = alloc_size;
2713 //ppc_stwu (code, sparc_sp, -alloc_size, sparc_sp);
2714 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
2715 //ppc_mr (code, sparc_l7, sparc_sp);
2717 /* compute max_offset in order to use short forward jumps */
2719 if (cfg->opt & MONO_OPT_BRANCH) {
2720 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2721 MonoInst *ins = bb->code;
2722 bb->max_offset = max_offset;
2724 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2728 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2734 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2735 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
2737 /* load arguments allocated to register from the stack */
2738 sig = method->signature;
2741 cinfo = calculate_sizes (sig, sig->pinvoke);
2743 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2744 ArgInfo *ainfo = cinfo->args + i;
2745 inst = cfg->varinfo [pos];
2747 if (inst->opcode == OP_REGVAR) {
2748 g_assert (!ainfo->regtype); // fine for now
2749 //ppc_mr (code, inst->dreg, ainfo->reg);
2750 if (cfg->verbose_level > 2)
2751 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
2753 /* the argument should be put on the stack: FIXME handle size != word */
2754 //ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2759 cfg->code_len = code - cfg->native_code;
2765 mono_arch_emit_epilog (MonoCompile *cfg)
2767 MonoJumpInfo *patch_info;
2768 MonoMethod *method = cfg->method;
2772 code = cfg->native_code + cfg->code_len;
2774 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2775 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
2780 if (method->save_lmf) {
2781 pos = -sizeof (MonoLMF);
2784 if (method->save_lmf) {
2786 /* ebx = previous_lmf */
2787 x86_pop_reg (code, X86_EBX);
2789 x86_pop_reg (code, X86_EDI);
2790 /* *(lmf) = previous_lmf */
2791 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
2793 /* discard method info */
2794 x86_pop_reg (code, X86_ESI);
2796 /* restore caller saved regs */
2797 x86_pop_reg (code, X86_EBP);
2798 x86_pop_reg (code, X86_ESI);
2799 x86_pop_reg (code, X86_EDI);
2800 x86_pop_reg (code, X86_EBX);
2804 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2805 //ppc_lwz (code, sparc_l0, cfg->stack_usage + 8, cfg->frame_reg);
2806 //ppc_mtlr (code, sparc_l0);
2808 //ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2809 for (i = 13; i < 32; ++i) {
2810 if (cfg->used_int_regs & (1 << i)) {
2812 //ppc_lwz (code, i, -pos, cfg->frame_reg);
2817 /* add code to raise exceptions */
2818 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2819 switch (patch_info->type) {
2820 case MONO_PATCH_INFO_EXC:
2821 /*x86_patch (patch_info->ip.i + cfg->native_code, code);
2822 x86_push_imm (code, patch_info->data.target);
2823 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
2824 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2825 patch_info->data.name = "throw_exception_by_name";
2826 patch_info->ip.i = code - cfg->native_code;
2827 x86_jump_code (code, 0);*/
2835 cfg->code_len = code - cfg->native_code;
2837 g_assert (cfg->code_len < cfg->code_size);
2842 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
2847 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
2849 int this_dreg = sparc_o0;
2852 this_dreg = sparc_o1;
2854 /* add the this argument */
2855 if (this_reg != -1) {
2857 MONO_INST_NEW (cfg, this, OP_SETREG);
2858 this->type = this_type;
2859 this->sreg1 = this_reg;
2860 this->dreg = this_dreg;
2861 mono_bblock_add_inst (cfg->cbb, this);
2866 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
2867 vtarg->type = STACK_MP;
2868 vtarg->sreg1 = vt_reg;
2869 vtarg->dreg = sparc_o0;
2870 mono_bblock_add_inst (cfg->cbb, vtarg);
2876 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)