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 #define DEBUG(a) if (cfg->verbose_level > 1) a
1205 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1214 static const char*const * ins_spec = sparc_desc;
1217 print_ins (int i, MonoInst *ins)
1219 const char *spec = ins_spec [ins->opcode];
1220 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1221 if (spec [MONO_INST_DEST]) {
1222 if (ins->dreg >= MONO_MAX_IREGS)
1223 g_print (" R%d <-", ins->dreg);
1225 g_print (" %s <-", mono_arch_regname (ins->dreg));
1227 if (spec [MONO_INST_SRC1]) {
1228 if (ins->sreg1 >= MONO_MAX_IREGS)
1229 g_print (" R%d", ins->sreg1);
1231 g_print (" %s", mono_arch_regname (ins->sreg1));
1233 if (spec [MONO_INST_SRC2]) {
1234 if (ins->sreg2 >= MONO_MAX_IREGS)
1235 g_print (" R%d", ins->sreg2);
1237 g_print (" %s", mono_arch_regname (ins->sreg2));
1239 if (spec [MONO_INST_CLOB])
1240 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1245 print_regtrack (RegTrack *t, int num)
1251 for (i = 0; i < num; ++i) {
1254 if (i >= MONO_MAX_IREGS) {
1255 g_snprintf (buf, sizeof(buf), "R%d", i);
1258 r = mono_arch_regname (i);
1259 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1263 typedef struct InstList InstList;
1271 static inline InstList*
1272 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1274 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1284 * Force the spilling of the variable in the symbolic register 'reg'.
1287 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1292 sel = cfg->rs->iassign [reg];
1293 /*i = cfg->rs->isymbolic [sel];
1294 g_assert (i == reg);*/
1296 spill = ++cfg->spill_count;
1297 cfg->rs->iassign [i] = -spill - 1;
1298 mono_regstate_free_int (cfg->rs, sel);
1299 /* we need to create a spill var and insert a load to sel after the current instruction */
1300 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1302 load->inst_basereg = cfg->frame_reg;
1303 load->inst_offset = mono_spillvar_offset (cfg, spill);
1305 while (ins->next != item->prev->data)
1308 load->next = ins->next;
1310 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1311 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1312 g_assert (i == sel);
1318 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1323 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));
1324 /* exclude the registers in the current instruction */
1325 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1326 if (ins->sreg1 >= MONO_MAX_IREGS)
1327 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1329 regmask &= ~ (1 << ins->sreg1);
1330 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1332 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1333 if (ins->sreg2 >= MONO_MAX_IREGS)
1334 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1336 regmask &= ~ (1 << ins->sreg2);
1337 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1339 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1340 regmask &= ~ (1 << ins->dreg);
1341 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1344 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1345 g_assert (regmask); /* need at least a register we can free */
1347 /* we should track prev_use and spill the register that's farther */
1348 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1349 if (regmask & (1 << i)) {
1351 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1355 i = cfg->rs->isymbolic [sel];
1356 spill = ++cfg->spill_count;
1357 cfg->rs->iassign [i] = -spill - 1;
1358 mono_regstate_free_int (cfg->rs, sel);
1359 /* we need to create a spill var and insert a load to sel after the current instruction */
1360 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1362 load->inst_basereg = cfg->frame_reg;
1363 load->inst_offset = mono_spillvar_offset (cfg, spill);
1365 while (ins->next != item->prev->data)
1368 load->next = ins->next;
1370 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1371 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1372 g_assert (i == sel);
1378 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1381 MONO_INST_NEW (cfg, copy, OP_MOVE);
1385 copy->next = ins->next;
1388 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1393 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1396 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1398 store->inst_destbasereg = sparc_sp;
1399 store->inst_offset = mono_spillvar_offset (cfg, spill);
1401 store->next = ins->next;
1404 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)));
1409 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1412 g_assert (item->next);
1413 prev = item->next->data;
1415 while (prev->next != ins)
1417 to_insert->next = ins;
1418 prev->next = to_insert;
1420 * needed otherwise in the next instruction we can add an ins to the
1421 * end and that would get past this instruction.
1423 item->data = to_insert;
1427 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1429 int val = cfg->rs->iassign [sym_reg];
1433 /* the register gets spilled after this inst */
1436 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1438 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1439 cfg->rs->iassign [sym_reg] = val;
1440 /* add option to store before the instruction for src registers */
1442 create_spilled_store (cfg, spill, val, sym_reg, ins);
1444 cfg->rs->isymbolic [val] = sym_reg;
1449 * Local register allocation.
1450 * We first scan the list of instructions and we save the liveness info of
1451 * each register (when the register is first used, when it's value is set etc.).
1452 * We also reverse the list of instructions (in the InstList list) because assigning
1453 * registers backwards allows for more tricks to be used.
1456 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1459 MonoRegState *rs = cfg->rs;
1460 int i, val, fpcount;
1461 RegTrack *reginfo, *reginfof;
1462 RegTrack *reginfo1, *reginfo2, *reginfod;
1463 InstList *tmp, *reversed = NULL;
1465 guint32 src1_mask, src2_mask, dest_mask;
1469 rs->next_vireg = bb->max_ireg;
1470 rs->next_vfreg = bb->max_freg;
1471 mono_regstate_assign (rs);
1472 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1473 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1474 rs->ifree_mask = 0xdeadbeef; /* FIXME */
1478 fpcount = 0; /* FIXME: track fp stack utilization */
1479 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1480 /* forward pass on the instructions to collect register liveness info */
1482 spec = ins_spec [ins->opcode];
1483 DEBUG (print_ins (i, ins));
1484 if (spec [MONO_INST_SRC1]) {
1485 if (spec [MONO_INST_SRC1] == 'f')
1486 reginfo1 = reginfof;
1489 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1490 reginfo1 [ins->sreg1].last_use = i;
1494 if (spec [MONO_INST_SRC2]) {
1495 if (spec [MONO_INST_SRC2] == 'f')
1496 reginfo2 = reginfof;
1499 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1500 reginfo2 [ins->sreg2].last_use = i;
1504 if (spec [MONO_INST_DEST]) {
1505 if (spec [MONO_INST_DEST] == 'f')
1506 reginfod = reginfof;
1509 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1510 reginfod [ins->dreg].killed_in = i;
1511 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1512 reginfod [ins->dreg].last_use = i;
1513 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1514 reginfod [ins->dreg].born_in = i;
1515 if (spec [MONO_INST_DEST] == 'l') {
1516 /* result in eax:edx, the virtual register is allocated sequentially */
1517 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1518 reginfod [ins->dreg + 1].last_use = i;
1519 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1520 reginfod [ins->dreg + 1].born_in = i;
1525 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1530 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1531 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1534 int prev_dreg, prev_sreg1, prev_sreg2;
1535 dest_mask = src1_mask = src2_mask = 0xdeadbeef; /* FIXME */
1538 spec = ins_spec [ins->opcode];
1539 DEBUG (g_print ("processing:"));
1540 DEBUG (print_ins (i, ins));
1541 /* update for use with FP regs... */
1542 if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
1543 val = rs->iassign [ins->dreg];
1544 prev_dreg = ins->dreg;
1548 /* the register gets spilled after this inst */
1551 val = mono_regstate_alloc_int (rs, dest_mask);
1553 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1554 rs->iassign [ins->dreg] = val;
1556 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1558 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1559 rs->isymbolic [val] = prev_dreg;
1561 if (spec [MONO_INST_DEST] == 'l') {
1562 int hreg = prev_dreg + 1;
1563 val = rs->iassign [hreg];
1567 /* the register gets spilled after this inst */
1570 val = mono_regstate_alloc_int (rs, dest_mask);
1572 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1573 rs->iassign [hreg] = val;
1575 create_spilled_store (cfg, spill, val, hreg, ins);
1577 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1578 rs->isymbolic [val] = hreg;
1579 /* FIXME:? ins->dreg = val; */
1580 if (ins->dreg == sparc_l1) {
1581 if (val != sparc_l0)
1582 create_copy_ins (cfg, val, sparc_l0, ins);
1583 } else if (ins->dreg == sparc_l0) {
1584 if (val == sparc_l1) {
1586 create_copy_ins (cfg, sparc_l2, sparc_l0, ins);
1587 create_copy_ins (cfg, sparc_l0, sparc_l1, ins);
1588 create_copy_ins (cfg, sparc_l1, sparc_l2, ins);
1590 /* two forced copies */
1591 create_copy_ins (cfg, val, sparc_l0, ins);
1592 create_copy_ins (cfg, ins->dreg, sparc_l1, ins);
1595 if (val == sparc_l0) {
1596 create_copy_ins (cfg, ins->dreg, sparc_l1, ins);
1598 /* two forced copies */
1599 create_copy_ins (cfg, val, sparc_l0, ins);
1600 create_copy_ins (cfg, ins->dreg, sparc_l1, ins);
1603 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1604 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1605 mono_regstate_free_int (rs, val);
1611 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1612 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1613 mono_regstate_free_int (rs, ins->dreg);
1615 if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
1616 val = rs->iassign [ins->sreg1];
1617 prev_sreg1 = ins->sreg1;
1621 /* the register gets spilled after this inst */
1624 if (0 && ins->opcode == OP_MOVE) {
1626 * small optimization: the dest register is already allocated
1627 * but the src one is not: we can simply assign the same register
1628 * here and peephole will get rid of the instruction later.
1629 * This optimization may interfere with the clobbering handling:
1630 * it removes a mov operation that will be added again to handle clobbering.
1631 * There are also some other issues that should with make testjit.
1633 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1634 val = rs->iassign [ins->sreg1] = ins->dreg;
1635 //g_assert (val >= 0);
1636 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1638 //g_assert (val == -1); /* source cannot be spilled */
1639 val = mono_regstate_alloc_int (rs, src1_mask);
1641 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1642 rs->iassign [ins->sreg1] = val;
1643 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1646 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1647 insert_before_ins (ins, tmp, store);
1650 rs->isymbolic [val] = prev_sreg1;
1655 if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
1656 val = rs->iassign [ins->sreg2];
1657 prev_sreg2 = ins->sreg2;
1661 /* the register gets spilled after this inst */
1664 val = mono_regstate_alloc_int (rs, src2_mask);
1666 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1667 rs->iassign [ins->sreg2] = val;
1668 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1670 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1672 rs->isymbolic [val] = prev_sreg2;
1678 if (spec [MONO_INST_CLOB] == 'c') {
1680 guint32 clob_mask = 0xdeadbeef; /* FIXME */
1681 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1683 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1684 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1688 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1689 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1690 mono_regstate_free_int (rs, ins->sreg1);
1692 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1693 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1694 mono_regstate_free_int (rs, ins->sreg2);
1697 //DEBUG (print_ins (i, ins));
1703 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1708 static unsigned char*
1709 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1715 sparc_patch (guchar *code, guchar *target)
1717 guint32 ins = *(guint32*)code;
1718 guint32 prim = ins >> 26;
1720 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1724 guint32 li = (guint32)target;
1725 ins = prim << 26 | (ins & 3);
1727 // FIXME: assert the top bits of li are 0
1729 gint diff = target - code;
1730 ins = prim << 26 | (ins & 3);
1732 diff &= ~(63 << 26);
1735 *(guint32*)code = ins;
1736 } else if (prim == 16) {
1739 guint32 li = (guint32)target;
1740 ins = (ins & 0xffff0000) | (ins & 3);
1743 // FIXME: assert the top bits of li are 0
1745 gint diff = target - code;
1746 ins = (ins & 0xffff0000) | (ins & 3);
1750 *(guint32*)code = ins;
1752 g_assert_not_reached ();
1754 // g_print ("patched with 0x%08x\n", ins);
1758 * Some conventions used in the following code.
1759 * 1) We're assuming a V9 CPU. We will check for that later.
1760 * In reality, we're mostly sticking with V8 instructions...
1761 * 2) The only scratch registers we have are o7 and g1. We try to
1762 * stick to o7 when we can, and use g1 when necessary.
1766 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1771 guint8 *code = cfg->native_code + cfg->code_len;
1772 MonoInst *last_ins = NULL;
1773 guint last_offset = 0;
1776 if (cfg->opt & MONO_OPT_PEEPHOLE)
1777 peephole_pass (cfg, bb);
1781 * various stratgies to align BBs. Using real loop detection or simply
1782 * aligning every block leads to more consistent benchmark results,
1783 * but usually slows down the code
1784 * we should do the alignment outside this function or we should adjust
1785 * bb->native offset as well or the code is effectively slowed down!
1787 /* align all blocks */
1788 // if ((pad = (cfg->code_len & (align - 1)))) {
1789 /* poor man loop start detection */
1790 // if (bb->code && bb->in_count && bb->in_bb [0]->cil_code > bb->cil_code && (pad = (cfg->code_len & (align - 1)))) {
1791 /* consider real loop detection and nesting level */
1792 // if (bb->loop_blocks && bb->nesting < 3 && (pad = (cfg->code_len & (align - 1)))) {
1793 /* consider real loop detection */
1794 if (/*bb->loop_blocks &&*/ (pad = (cfg->code_len & (align - 1)))) {
1796 x86_padding (code, pad);
1797 cfg->code_len += pad;
1798 bb->native_offset = cfg->code_len;
1802 if (cfg->verbose_level > 2)
1803 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1805 cpos = bb->max_offset;
1807 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1808 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
1809 //g_assert (!mono_compile_aot);
1812 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
1813 /* this is not thread save, but good enough */
1814 /* fixme: howto handle overflows? */
1815 //x86_inc_mem (code, &cov->data [bb->dfn].count);
1820 offset = code - cfg->native_code;
1822 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1824 if (offset > (cfg->code_size - max_len - 16)) {
1825 cfg->code_size *= 2;
1826 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1827 code = cfg->native_code + offset;
1829 // if (ins->cil_code)
1830 // g_print ("cil code\n");
1832 switch (ins->opcode) {
1833 case OP_STOREI1_MEMBASE_IMM:
1834 sparc_set (code, ins->inst_imm, sparc_o7);
1835 // g_assert (ppc_is_imm16 (ins->inst_offset));
1836 sparc_stb_imm (code, sparc_o7, ins->inst_offset, ins->inst_destbasereg);
1838 case OP_STOREI2_MEMBASE_IMM:
1839 sparc_set (code, ins->inst_imm, sparc_o7);
1840 // g_assert (ppc_is_imm16 (ins->inst_offset));
1841 sparc_sth_imm (code, sparc_o7, ins->inst_offset, ins->inst_destbasereg);
1843 case OP_STORE_MEMBASE_IMM:
1844 case OP_STOREI4_MEMBASE_IMM:
1845 sparc_set (code, ins->inst_imm, sparc_o7);
1846 // g_assert (ppc_is_imm16 (ins->inst_offset));
1847 sparc_st_imm (code, sparc_o7, ins->inst_offset, ins->inst_destbasereg);
1849 case OP_STOREI1_MEMBASE_REG:
1850 // g_assert (ppc_is_imm16 (ins->inst_offset));
1851 sparc_stb_imm (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1853 case OP_STOREI2_MEMBASE_REG:
1854 // g_assert (ppc_is_imm16 (ins->inst_offset));
1855 sparc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1857 case OP_STORE_MEMBASE_REG:
1858 case OP_STOREI4_MEMBASE_REG:
1859 // g_assert (ppc_is_imm16 (ins->inst_offset));
1860 sparc_st (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1865 sparc_ld (code, ins->inst_p0, sparc_g0, ins->dreg);
1867 /* The cast IS BAD (maybe). But it needs to be done... */
1869 sparc_set (code, (guint)ins->inst_p0, ins->dreg);
1870 sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
1872 case OP_LOAD_MEMBASE:
1873 case OP_LOADI4_MEMBASE:
1874 case OP_LOADU4_MEMBASE:
1875 if (TRUE) { /* FIXME */
1876 sparc_ld_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1878 sparc_ld (code, sparc_l0, 0, ins->inst_offset);
1879 sparc_ld (code, ins->dreg, 0, sparc_l0);
1882 case OP_LOADU1_MEMBASE:
1883 // g_assert (ppc_is_imm16 (ins->inst_offset));
1884 sparc_ldub_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1886 case OP_LOADI1_MEMBASE:
1887 // g_assert (ppc_is_imm16 (ins->inst_offset));
1888 sparc_ldsb_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1890 case OP_LOADU2_MEMBASE:
1891 // g_assert (ppc_is_imm16 (ins->inst_offset));
1892 sparc_lduh_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1894 case OP_LOADI2_MEMBASE:
1895 sparc_ldsh_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1898 sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
1899 sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
1902 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
1903 sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
1905 /* GCC does this one differently. Don't ask me WHY. */
1907 sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
1910 sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
1911 sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
1914 sparc_cmp (code, ins->sreg1, ins->sreg2);
1916 case OP_COMPARE_IMM:
1917 if (TRUE) { /* FIXME */
1918 sparc_cmp_imm (code, ins->sreg1, (ins->inst_imm & 0x1fff));
1920 sparc_set (code, ins->inst_imm, sparc_o7);
1921 sparc_cmp (code, ins->sreg1, sparc_o7);
1924 case OP_X86_TEST_NULL:
1925 sparc_cmp_imm (code, ins->sreg1, 0);
1928 g_assert_not_reached();
1931 sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
1934 sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1937 sparc_addx (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1940 if (TRUE) { /* FIXME */
1941 sparc_add_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1943 sparc_ld (code, sparc_l0, 0, ins->inst_imm);
1944 sparc_add (code, 0, ins->dreg, ins->sreg1, sparc_l0);
1948 sparc_addx (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1951 sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
1954 sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1957 sparc_subx (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1960 // we add the negated value
1961 // g_assert (ppc_is_imm16 (-ins->inst_imm));
1962 sparc_add_imm (code, FALSE, ins->sreg1, -ins->inst_imm, ins->dreg);
1965 sparc_subx_imm (code, FALSE, ins->sreg2, ins->inst_imm, ins->dreg);
1968 sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1971 sparc_and_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1974 sparc_sdiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1977 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1980 sparc_sdiv_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1983 sparc_sdiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
1984 sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
1985 sparc_sub (code, FALSE, sparc_o7, ins->sreg1, ins->dreg);
1988 sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
1989 sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
1990 sparc_sub (code, FALSE, sparc_o7, ins->sreg1, ins->dreg);
1993 sparc_sdiv_imm (code, FALSE, ins->sreg1, ins->inst_imm, sparc_o7);
1994 sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
1995 sparc_sub (code, FALSE, sparc_o7, ins->sreg1, ins->dreg);
1998 sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2001 sparc_set (code, ins->inst_imm, sparc_o7);
2002 sparc_or (code, FALSE, ins->sreg2, sparc_o7, ins->dreg);
2005 sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2008 sparc_set (code, ins->inst_imm, sparc_o7);
2009 sparc_xor (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2012 sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
2015 sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2018 sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
2021 sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2024 sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2027 sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
2030 /* can't use sparc_not */
2031 sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
2034 /* can't use sparc_neg */
2035 sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
2038 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2041 sparc_smul_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
2044 /* FIXME: this isn't right, I don't think */
2045 sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2047 case CEE_MUL_OVF_UN:
2048 /* FIXME: This isn't right either */
2049 sparc_umul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2053 sparc_set (code, ins->inst_c0, ins->dreg);
2056 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_CLASS, (gpointer)ins->inst_c0);
2059 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_IMAGE, (gpointer)ins->inst_c0);
2065 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2068 g_assert_not_reached ();
2070 * Copied roughly from x86. Probably doesn't work
2073 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2074 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2075 /* reset offset to make max_len work */
2076 offset = code - cfg->native_code;
2078 g_assert (!cfg->method->save_lmf);
2080 offset = code - cfg->native_code;
2081 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2082 sparc_jmp_imm (code, sparc_g0, 0);
2086 /* ensure ins->sreg1 is not NULL */
2087 sparc_cmp_imm (code, ins->sreg1, 0);
2094 call = (MonoCallInst*)ins;
2095 if (ins->flags & MONO_INST_HAS_METHOD)
2096 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2098 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2099 sparc_call_simple (code, 0);
2104 case OP_VOIDCALL_REG:
2106 call = (MonoCallInst*)ins;
2107 sparc_call (code, ins->sreg1, sparc_g0);
2108 /* FIXME: yea, a store in g0 is a GOOD IDEA */
2109 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2110 sparc_add_imm (code, FALSE, sparc_sp, call->stack_usage, sparc_g0);
2112 case OP_FCALL_MEMBASE:
2113 case OP_LCALL_MEMBASE:
2114 case OP_VCALL_MEMBASE:
2115 case OP_VOIDCALL_MEMBASE:
2116 case OP_CALL_MEMBASE:
2117 call = (MonoCallInst*)ins;
2118 sparc_call_imm (code, ins->sreg1, ins->inst_offset);
2119 /* FIXME: yea, a store in g0 is a GOOD IDEA */
2120 if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2121 sparc_add_imm (code, FALSE, sparc_sp, call->stack_usage, sparc_g0);
2126 /* FIXME: This can be SO far wrong! */
2127 sparc_st (code, ins->sreg1, sparc_g0, sparc_sp);
2128 sparc_sub_imm (code, FALSE, sparc_sp, 4, sparc_sp);
2131 /* keep alignment */
2132 #define MONO_FRAME_ALIGNMENT 32
2133 sparc_add_imm (code, 0, ins->sreg1, MONO_FRAME_ALIGNMENT-1, sparc_o7);
2134 //ppc_rlwinm (code, sparc_l0, sparc_l0, 0, 0, 27);
2135 //ppc_lwz (code, sparc_l0, 0, ppc_sp);
2136 // Fix semantics to negate to another reg? FIXME
2137 //sparc_neg (code, sparc_l0, sparc_l0);
2138 //ppc_stwux (code, ppc_sp, sparc_l0, ppc_sp);
2139 //ppc_mr (code, ins->dreg, ppc_sp);
2140 g_assert_not_reached ();
2146 sparc_mov_reg_reg (code, ins->sreg1, sparc_sp);
2147 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2148 (gpointer)"throw_exception");
2149 sparc_call_simple (code, 0);
2153 if (ins->sreg1 != sparc_sp)
2154 sparc_mov_reg_reg (code, ins->sreg1, sparc_sp);
2157 case CEE_ENDFINALLY:
2160 case OP_CALL_HANDLER:
2161 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2162 sparc_call_simple (code, 0);
2165 ins->inst_c0 = code - cfg->native_code;
2168 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2169 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2171 if (ins->flags & MONO_INST_BRLABEL) {
2172 if (ins->inst_i0->inst_c0) {
2173 sparc_call_simple (code, 0);
2175 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2176 sparc_call_simple (code, 0);
2179 if (ins->inst_target_bb->native_offset) {
2180 sparc_call_simple (code, 0);
2182 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2183 sparc_call_simple (code, 0);
2188 sparc_jmp (code, ins->sreg1, sparc_g0);
2191 //ppc_li (code, ins->dreg, 0);
2192 //ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2193 //ppc_li (code, ins->dreg, 1);
2197 //ppc_li (code, ins->dreg, 1);
2198 //ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2199 //ppc_li (code, ins->dreg, 0);
2203 //ppc_li (code, ins->dreg, 1);
2204 //ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2205 //ppc_li (code, ins->dreg, 0);
2207 case OP_COND_EXC_EQ:
2208 case OP_COND_EXC_NE_UN:
2209 case OP_COND_EXC_LT:
2210 case OP_COND_EXC_LT_UN:
2211 case OP_COND_EXC_GT:
2212 case OP_COND_EXC_GT_UN:
2213 case OP_COND_EXC_GE:
2214 case OP_COND_EXC_GE_UN:
2215 case OP_COND_EXC_LE:
2216 case OP_COND_EXC_LE_UN:
2217 case OP_COND_EXC_OV:
2218 case OP_COND_EXC_NO:
2220 case OP_COND_EXC_NC:
2221 //EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2222 // (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2234 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2237 /* floating point opcodes */
2239 //ppc_load (code, sparc_l0, ins->inst_p0);
2240 //ppc_lfd (code, ins->dreg, 0, sparc_l0);
2243 //ppc_load (code, sparc_l0, ins->inst_p0);
2244 //ppc_lfs (code, ins->dreg, 0, sparc_l0);
2246 case OP_STORER8_MEMBASE_REG:
2247 //ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2249 case OP_LOADR8_MEMBASE:
2250 //ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2252 case OP_STORER4_MEMBASE_REG:
2253 //ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2255 case OP_LOADR4_MEMBASE:
2256 //ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2258 case CEE_CONV_R4: /* FIXME: change precision */
2260 g_assert_not_reached ();
2261 //x86_push_reg (code, ins->sreg1);
2262 //x86_fild_membase (code, X86_ESP, 0, FALSE);
2263 //x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2265 case OP_X86_FP_LOAD_I8:
2266 g_assert_not_reached ();
2267 //x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2269 case OP_X86_FP_LOAD_I4:
2270 g_assert_not_reached ();
2271 //x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2273 case OP_FCONV_TO_I1:
2274 g_assert_not_reached ();
2275 code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2277 case OP_FCONV_TO_U1:
2278 g_assert_not_reached ();
2279 code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2281 case OP_FCONV_TO_I2:
2282 g_assert_not_reached ();
2283 code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2285 case OP_FCONV_TO_U2:
2286 g_assert_not_reached ();
2287 code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2289 case OP_FCONV_TO_I4:
2291 g_assert_not_reached ();
2292 code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2294 case OP_FCONV_TO_U4:
2296 g_assert_not_reached ();
2297 code = emit_float_to_int (cfg, code, ins->dreg, 4, FALSE);
2299 case OP_FCONV_TO_I8:
2300 case OP_FCONV_TO_U8:
2301 g_assert_not_reached ();
2302 /*x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2303 x86_fnstcw_membase(code, X86_ESP, 0);
2304 x86_mov_reg_membase (code, ins->inst_dreg_low, X86_ESP, 0, 2);
2305 x86_alu_reg_imm (code, X86_OR, ins->inst_dreg_low, 0xc00);
2306 x86_mov_membase_reg (code, X86_ESP, 2, ins->inst_dreg_low, 2);
2307 x86_fldcw_membase (code, X86_ESP, 2);
2308 x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2309 x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2310 x86_pop_reg (code, ins->inst_dreg_low);
2311 x86_pop_reg (code, ins->inst_dreg_high);
2312 x86_fldcw_membase (code, X86_ESP, 0);
2313 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);*/
2315 case OP_LCONV_TO_R_UN: {
2317 static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2320 /* load 64bit integer to FP stack */
2321 x86_push_imm (code, 0);
2322 x86_push_reg (code, ins->sreg2);
2323 x86_push_reg (code, ins->sreg1);
2324 x86_fild_membase (code, X86_ESP, 0, TRUE);
2325 /* store as 80bit FP value */
2326 x86_fst80_membase (code, X86_ESP, 0);
2328 /* test if lreg is negative */
2329 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2330 br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2332 /* add correction constant mn */
2333 x86_fld80_mem (code, mn);
2334 x86_fld80_membase (code, X86_ESP, 0);
2335 x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2336 x86_fst80_membase (code, X86_ESP, 0);
2338 x86_patch (br, code);
2340 x86_fld80_membase (code, X86_ESP, 0);
2341 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2343 g_assert_not_reached ();
2346 case OP_LCONV_TO_OVF_I: {
2348 guint8 *br [3], *label [1];
2351 * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2353 x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2355 /* If the low word top bit is set, see if we are negative */
2356 br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2357 /* We are not negative (no top bit set, check for our top word to be zero */
2358 x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2359 br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2362 /* throw exception */
2363 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2364 x86_jump32 (code, 0);
2366 x86_patch (br [0], code);
2367 /* our top bit is set, check that top word is 0xfffffff */
2368 x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2370 x86_patch (br [1], code);
2371 /* nope, emit exception */
2372 br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2373 x86_patch (br [2], label [0]);
2375 if (ins->dreg != ins->sreg1)
2376 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2378 g_assert_not_reached ();
2382 //ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
2385 //ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
2388 //ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
2391 //ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
2394 //ppc_fneg (code, ins->dreg, ins->sreg1);
2397 g_assert_not_reached ();
2400 g_assert_not_reached ();
2401 /* this overwrites EAX */
2402 //EMIT_FPCOMPARE(code);
2405 g_assert_not_reached ();
2406 /*if (ins->dreg != X86_EAX)
2407 x86_push_reg (code, X86_EAX);
2409 EMIT_FPCOMPARE(code);
2410 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2411 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2412 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2414 if (ins->dreg != X86_EAX)
2415 x86_pop_reg (code, X86_EAX);*/
2419 g_assert_not_reached ();
2420 /*if (ins->dreg != X86_EAX)
2421 x86_push_reg (code, X86_EAX);
2423 EMIT_FPCOMPARE(code);
2424 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2425 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2427 if (ins->dreg != X86_EAX)
2428 x86_pop_reg (code, X86_EAX);*/
2432 g_assert_not_reached ();
2433 /*if (ins->dreg != X86_EAX)
2434 x86_push_reg (code, X86_EAX);
2436 EMIT_FPCOMPARE(code);
2437 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2438 x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2439 x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2441 if (ins->dreg != X86_EAX)
2442 x86_pop_reg (code, X86_EAX);*/
2445 g_assert_not_reached ();
2448 g_assert_not_reached ();
2452 g_assert_not_reached ();
2456 g_assert_not_reached ();
2460 g_assert_not_reached ();
2464 g_assert_not_reached ();
2466 case CEE_CKFINITE: {
2468 g_assert_not_reached ();
2469 //x86_push_reg (code, X86_EAX);
2471 //x86_fnstsw (code);
2472 //x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2473 //x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2474 //x86_pop_reg (code, X86_EAX);
2475 //EMIT_COND_SYSTEM_EXCEPTION (FALSE, FALSE, "ArithmeticException");
2479 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2480 g_assert_not_reached ();
2483 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2484 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2485 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2486 g_assert_not_reached ();
2492 last_offset = offset;
2497 cfg->code_len = code - cfg->native_code;
2501 mono_arch_register_lowlevel_calls (void)
2503 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2504 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2508 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji)
2510 MonoJumpInfo *patch_info;
2512 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2513 unsigned char *ip = patch_info->ip.i + code;
2514 const unsigned char *target = NULL;
2516 switch (patch_info->type) {
2517 case MONO_PATCH_INFO_BB:
2518 target = patch_info->data.bb->native_offset + code;
2520 case MONO_PATCH_INFO_ABS:
2521 target = patch_info->data.target;
2523 case MONO_PATCH_INFO_LABEL:
2524 target = patch_info->data.inst->inst_c0 + code;
2526 case MONO_PATCH_INFO_IP:
2527 *((gpointer *)(ip)) = ip;
2529 case MONO_PATCH_INFO_INTERNAL_METHOD: {
2530 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
2532 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
2533 g_assert_not_reached ();
2535 target = mi->wrapper;
2538 case MONO_PATCH_INFO_METHOD:
2539 if (patch_info->data.method == method) {
2542 /* get the trampoline to the method from the domain */
2543 target = mono_arch_create_jit_trampoline (patch_info->data.method);
2546 case MONO_PATCH_INFO_SWITCH: {
2547 gpointer *table = (gpointer *)patch_info->data.target;
2550 // FIXME: inspect code to get the register
2551 //ppc_load (ip, sparc_l0, patch_info->data.target);
2552 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
2554 for (i = 0; i < patch_info->table_size; i++) {
2555 table [i] = (int)patch_info->data.table [i] + code;
2557 /* we put into the table the absolute address, no need for sparc_patch in this case */
2560 case MONO_PATCH_INFO_METHODCONST:
2561 case MONO_PATCH_INFO_CLASS:
2562 case MONO_PATCH_INFO_IMAGE:
2563 case MONO_PATCH_INFO_FIELD:
2564 g_assert_not_reached ();
2565 *((gconstpointer *)(ip + 1)) = patch_info->data.target;
2567 case MONO_PATCH_INFO_R4:
2568 case MONO_PATCH_INFO_R8:
2569 g_assert_not_reached ();
2570 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
2573 g_assert_not_reached ();
2575 sparc_patch (ip, target);
2580 mono_arch_max_epilog_size (MonoCompile *cfg)
2582 int exc_count = 0, max_epilog_size = 16 + 20*4;
2583 MonoJumpInfo *patch_info;
2585 if (cfg->method->save_lmf)
2586 max_epilog_size += 128;
2588 if (mono_jit_trace_calls)
2589 max_epilog_size += 50;
2591 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2592 max_epilog_size += 50;
2594 /* count the number of exception infos */
2596 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2597 if (patch_info->type == MONO_PATCH_INFO_EXC)
2602 * make sure we have enough space for exceptions
2603 * 16 is the size of two push_imm instructions and a call
2605 max_epilog_size += exc_count*16;
2607 return max_epilog_size;
2611 mono_arch_emit_prolog (MonoCompile *cfg)
2613 MonoMethod *method = cfg->method;
2615 MonoMethodSignature *sig;
2617 int alloc_size, pos, max_offset, i;
2621 cfg->code_size = 256;
2622 code = cfg->native_code = g_malloc (cfg->code_size);
2624 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2625 //ppc_mflr (code, sparc_l0);
2626 //ppc_stw (code, sparc_l0, 8, ppc_sp);
2628 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
2629 cfg->used_int_regs |= 1 << 31;
2632 alloc_size = cfg->stack_offset;
2635 if (method->save_lmf) {
2637 pos += sizeof (MonoLMF);
2639 /* save the current IP */
2640 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2641 x86_push_imm (code, 0);
2643 /* save all caller saved regs */
2644 x86_push_reg (code, X86_EBX);
2645 x86_push_reg (code, X86_EDI);
2646 x86_push_reg (code, X86_ESI);
2647 x86_push_reg (code, X86_EBP);
2649 /* save method info */
2650 x86_push_imm (code, method);
2652 /* get the address of lmf for the current thread */
2653 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2654 (gpointer)"get_lmf_addr");
2655 x86_call_code (code, 0);
2658 x86_push_reg (code, X86_EAX);
2659 /* push *lfm (previous_lmf) */
2660 x86_push_membase (code, X86_EAX, 0);
2662 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
2666 for (i = 13; i < 32; ++i) {
2667 if (cfg->used_int_regs & (1 << i)) {
2669 //ppc_stw (code, i, -pos, ppc_sp);
2675 // align to 16 bytes
2676 if (alloc_size & (16 - 1))
2677 alloc_size += 16 - (alloc_size & (16 - 1));
2679 cfg->stack_usage = alloc_size;
2681 //ppc_stwu (code, sparc_sp, -alloc_size, sparc_sp);
2682 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
2683 //ppc_mr (code, sparc_l7, sparc_sp);
2685 /* compute max_offset in order to use short forward jumps */
2687 if (cfg->opt & MONO_OPT_BRANCH) {
2688 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2689 MonoInst *ins = bb->code;
2690 bb->max_offset = max_offset;
2692 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2696 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2702 if (mono_jit_trace_calls)
2703 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
2705 /* load arguments allocated to register from the stack */
2706 sig = method->signature;
2709 cinfo = calculate_sizes (sig, sig->pinvoke);
2711 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2712 ArgInfo *ainfo = cinfo->args + i;
2713 inst = cfg->varinfo [pos];
2715 if (inst->opcode == OP_REGVAR) {
2716 g_assert (!ainfo->regtype); // fine for now
2717 //ppc_mr (code, inst->dreg, ainfo->reg);
2718 if (cfg->verbose_level > 2)
2719 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
2721 /* the argument should be put on the stack: FIXME handle size != word */
2722 //ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2727 cfg->code_len = code - cfg->native_code;
2733 mono_arch_emit_epilog (MonoCompile *cfg)
2735 MonoJumpInfo *patch_info;
2736 MonoMethod *method = cfg->method;
2740 code = cfg->native_code + cfg->code_len;
2742 if (mono_jit_trace_calls)
2743 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
2748 if (method->save_lmf) {
2749 pos = -sizeof (MonoLMF);
2752 if (method->save_lmf) {
2754 /* ebx = previous_lmf */
2755 x86_pop_reg (code, X86_EBX);
2757 x86_pop_reg (code, X86_EDI);
2758 /* *(lmf) = previous_lmf */
2759 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
2761 /* discard method info */
2762 x86_pop_reg (code, X86_ESI);
2764 /* restore caller saved regs */
2765 x86_pop_reg (code, X86_EBP);
2766 x86_pop_reg (code, X86_ESI);
2767 x86_pop_reg (code, X86_EDI);
2768 x86_pop_reg (code, X86_EBX);
2772 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2773 //ppc_lwz (code, sparc_l0, cfg->stack_usage + 8, cfg->frame_reg);
2774 //ppc_mtlr (code, sparc_l0);
2776 //ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2777 for (i = 13; i < 32; ++i) {
2778 if (cfg->used_int_regs & (1 << i)) {
2780 //ppc_lwz (code, i, -pos, cfg->frame_reg);
2785 /* add code to raise exceptions */
2786 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2787 switch (patch_info->type) {
2788 case MONO_PATCH_INFO_EXC:
2789 /*x86_patch (patch_info->ip.i + cfg->native_code, code);
2790 x86_push_imm (code, patch_info->data.target);
2791 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
2792 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2793 patch_info->data.name = "throw_exception_by_name";
2794 patch_info->ip.i = code - cfg->native_code;
2795 x86_jump_code (code, 0);*/
2803 cfg->code_len = code - cfg->native_code;
2805 g_assert (cfg->code_len < cfg->code_size);