2 * mini-ppc.c: PowerPC backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/debug-helpers.h>
20 int mono_exc_esp_offset = 0;
23 mono_arch_regname (int reg) {
24 static const char * rnames[] = {
25 "ppc_r0", "ppc_sp", "ppc_r2", "ppc_r3", "ppc_r4",
26 "ppc_r5", "ppc_r6", "ppc_r7", "ppc_r8", "ppc_r9",
27 "ppc_r10", "ppc_r11", "ppc_r12", "ppc_r13", "ppc_r14",
28 "ppc_r15", "ppc_r16", "ppc_r17", "ppc_r18", "ppc_r19",
29 "ppc_r20", "ppc_r21", "ppc_r22", "ppc_r23", "ppc_r24",
30 "ppc_r25", "ppc_r26", "ppc_r27", "ppc_r28", "ppc_r29",
33 if (reg >= 0 && reg < 32)
38 /* this function overwrites r0 */
40 emit_memcpy (guint32 *code, int size, int dreg, int doffset, int sreg, int soffset)
42 /* unrolled, use the counter in big */
44 ppc_lwz (code, ppc_r0, soffset, sreg);
45 ppc_stw (code, ppc_r0, doffset, dreg);
51 ppc_lhz (code, ppc_r0, soffset, sreg);
52 ppc_sth (code, ppc_r0, doffset, dreg);
58 ppc_lbz (code, ppc_r0, soffset, sreg);
59 ppc_stb (code, ppc_r0, doffset, dreg);
71 } MonoJitArgumentInfo;
74 * arch_get_argument_info:
75 * @csig: a method signature
76 * @param_count: the number of parameters to consider
77 * @arg_info: an array to store the result infos
79 * Gathers information on parameters such as size, alignment and
80 * padding. arg_info should be large enought to hold param_count + 1 entries.
82 * Returns the size of the activation frame.
85 arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
87 int k, frame_size = 0;
91 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
92 frame_size += sizeof (gpointer);
96 arg_info [0].offset = offset;
99 frame_size += sizeof (gpointer);
103 arg_info [0].size = frame_size;
105 for (k = 0; k < param_count; k++) {
108 size = mono_type_native_stack_size (csig->params [k], &align);
110 size = mono_type_stack_size (csig->params [k], &align);
112 /* ignore alignment for now */
115 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
116 arg_info [k].pad = pad;
118 arg_info [k + 1].pad = 0;
119 arg_info [k + 1].size = size;
121 arg_info [k + 1].offset = offset;
125 align = MONO_ARCH_FRAME_ALIGNMENT;
126 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
127 arg_info [k].pad = pad;
132 static int indent_level = 0;
134 static void indent (int diff) {
135 int v = indent_level;
139 indent_level += diff;
143 enter_method (MonoMethod *method, char *ebp)
148 MonoJitArgumentInfo *arg_info;
149 MonoMethodSignature *sig;
152 fname = mono_method_full_name (method, TRUE);
154 printf ("ENTER: %s(", fname);
158 printf (") ip: %p\n", __builtin_return_address (1));
161 if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
162 g_error ("unaligned stack detected (%p)", ebp);
165 sig = method->signature;
167 arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
169 arch_get_argument_info (sig, sig->param_count, arg_info);
171 if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
172 g_assert (!method->signature->ret->byref);
174 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
177 if (method->signature->hasthis) {
178 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
179 if (method->klass->valuetype) {
180 printf ("value:%p, ", *this);
182 o = *((MonoObject **)this);
185 class = o->vtable->klass;
187 if (class == mono_defaults.string_class) {
188 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
190 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
193 printf ("this:NULL, ");
197 for (i = 0; i < method->signature->param_count; ++i) {
198 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
199 int size = arg_info [i + 1].size;
201 MonoType *type = method->signature->params [i];
204 printf ("[BYREF:%p], ", *cpos);
205 } else switch (type->type) {
209 printf ("%p, ", (gpointer)*((int *)(cpos)));
211 case MONO_TYPE_BOOLEAN:
219 printf ("%d, ", *((int *)(cpos)));
221 case MONO_TYPE_STRING: {
222 MonoString *s = *((MonoString **)cpos);
224 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
225 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
227 printf ("[STRING:null], ");
230 case MONO_TYPE_CLASS:
231 case MONO_TYPE_OBJECT: {
232 o = *((MonoObject **)cpos);
234 class = o->vtable->klass;
236 if (class == mono_defaults.string_class) {
237 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
238 } else if (class == mono_defaults.int32_class) {
239 printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
241 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
243 printf ("%p, ", *((gpointer *)(cpos)));
248 case MONO_TYPE_FNPTR:
249 case MONO_TYPE_ARRAY:
250 case MONO_TYPE_SZARRAY:
251 printf ("%p, ", *((gpointer *)(cpos)));
254 printf ("%lld, ", *((gint64 *)(cpos)));
257 printf ("%f, ", *((float *)(cpos)));
260 printf ("%f, ", *((double *)(cpos)));
262 case MONO_TYPE_VALUETYPE:
264 for (j = 0; j < size; j++)
265 printf ("%02x,", *((guint8*)cpos +j));
277 leave_method (MonoMethod *method, ...)
283 va_start(ap, method);
285 fname = mono_method_full_name (method, TRUE);
287 printf ("LEAVE: %s", fname);
290 type = method->signature->ret;
293 switch (type->type) {
296 case MONO_TYPE_BOOLEAN: {
297 int eax = va_arg (ap, int);
299 printf ("TRUE:%d", eax);
314 int eax = va_arg (ap, int);
315 printf ("EAX=%d", eax);
318 case MONO_TYPE_STRING: {
319 MonoString *s = va_arg (ap, MonoString *);
322 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
323 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
325 printf ("[STRING:null], ");
328 case MONO_TYPE_CLASS:
329 case MONO_TYPE_OBJECT: {
330 MonoObject *o = va_arg (ap, MonoObject *);
333 if (o->vtable->klass == mono_defaults.boolean_class) {
334 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
335 } else if (o->vtable->klass == mono_defaults.int32_class) {
336 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));
337 } else if (o->vtable->klass == mono_defaults.int64_class) {
338 printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));
340 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
342 printf ("[OBJECT:%p]", o);
347 case MONO_TYPE_FNPTR:
348 case MONO_TYPE_ARRAY:
349 case MONO_TYPE_SZARRAY: {
350 gpointer p = va_arg (ap, gpointer);
351 printf ("result=%p", p);
355 gint64 l = va_arg (ap, gint64);
356 printf ("lresult=%lld", l);
360 double f = va_arg (ap, double);
361 printf ("FP=%f\n", f);
364 case MONO_TYPE_VALUETYPE:
365 if (type->data.klass->enumtype) {
366 type = type->data.klass->enum_basetype;
369 guint8 *p = va_arg (ap, gpointer);
371 size = mono_type_size (type, &align);
373 for (j = 0; p && j < size; j++)
374 printf ("%02x,", p [j]);
379 printf ("(unknown return type %x)", method->signature->ret->type);
382 printf (" ip: %p\n", __builtin_return_address (1));
386 * Initialize the cpu to execute managed code.
389 mono_arch_cpu_init (void)
394 * This function returns the optimizations supported on this cpu.
397 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
401 /* no ppc-specific optimizations yet */
402 *exclude_mask = MONO_OPT_INLINE|MONO_OPT_LINEARS;
407 is_regsize_var (MonoType *t) {
416 case MONO_TYPE_OBJECT:
417 case MONO_TYPE_STRING:
418 case MONO_TYPE_CLASS:
419 case MONO_TYPE_SZARRAY:
420 case MONO_TYPE_ARRAY:
422 case MONO_TYPE_VALUETYPE:
423 if (t->data.klass->enumtype)
424 return is_regsize_var (t->data.klass->enum_basetype);
431 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
436 for (i = 0; i < cfg->num_varinfo; i++) {
437 MonoInst *ins = cfg->varinfo [i];
438 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
441 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
444 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
447 /* we can only allocate 32 bit values */
448 if (is_regsize_var (ins->inst_vtype)) {
449 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
450 g_assert (i == vmv->idx);
451 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
458 #define USE_EXTRA_TEMPS ((1<<30) | (1<<29))
459 //#define USE_EXTRA_TEMPS 0
462 mono_arch_get_global_int_regs (MonoCompile *cfg)
466 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
471 for (i = 13; i < top; ++i)
472 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
477 // code from ppc/tramp.c, try to keep in sync
478 #define MIN_CACHE_LINE 8
481 mono_arch_flush_icache (guint8 *code, gint size)
487 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
488 asm ("dcbst 0,%0;" : : "r"(p) : "memory");
492 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
493 asm ("icbi 0,%0; sync;" : : "r"(p) : "memory");
499 #define NOT_IMPLEMENTED(x) \
500 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
503 #define GENERAL_REGS 8
505 #define ALWAYS_ON_STACK(s) s
506 #define FP_ALSO_IN_REG(s) s
508 #define ALWAYS_ON_STACK(s)
509 #define FP_ALSO_IN_REG(s) s
510 #define ALIGN_DOUBLES
523 guint16 vtsize; /* in param area */
525 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
526 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
540 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
543 if (*gr >= 3 + GENERAL_REGS) {
544 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
545 ainfo->reg = ppc_sp; /* in the caller */
546 ainfo->regtype = RegTypeBase;
549 ALWAYS_ON_STACK (*stack_size += 4);
553 if (*gr >= 3 + GENERAL_REGS - 1) {
555 //*stack_size += (*stack_size % 8);
557 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
558 ainfo->reg = ppc_sp; /* in the caller */
559 ainfo->regtype = RegTypeBase;
566 ALWAYS_ON_STACK (*stack_size += 8);
575 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
578 int n = sig->hasthis + sig->param_count;
580 guint32 stack_size = 0;
581 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
583 fr = PPC_FIRST_FPARG_REG;
584 gr = PPC_FIRST_ARG_REG;
586 /* FIXME: handle returning a struct */
587 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
588 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
589 cinfo->struct_ret = PPC_FIRST_ARG_REG;
594 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
597 DEBUG(printf("params: %d\n", sig->param_count));
598 for (i = 0; i < sig->param_count; ++i) {
599 DEBUG(printf("param %d: ", i));
600 if (sig->params [i]->byref) {
601 DEBUG(printf("byref\n"));
602 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
606 simpletype = sig->params [i]->type;
608 switch (simpletype) {
609 case MONO_TYPE_BOOLEAN:
612 cinfo->args [n].size = 1;
613 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
619 cinfo->args [n].size = 2;
620 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
625 cinfo->args [n].size = 4;
626 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
632 case MONO_TYPE_FNPTR:
633 case MONO_TYPE_CLASS:
634 case MONO_TYPE_OBJECT:
635 case MONO_TYPE_STRING:
636 case MONO_TYPE_SZARRAY:
637 case MONO_TYPE_ARRAY:
638 cinfo->args [n].size = sizeof (gpointer);
639 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
642 case MONO_TYPE_VALUETYPE: {
644 if (sig->params [i]->data.klass->enumtype) {
645 simpletype = sig->params [i]->data.klass->enum_basetype->type;
648 size = mono_class_value_size (sig->params [i]->data.klass, NULL);
649 DEBUG(printf ("load %d bytes struct\n",
650 mono_class_value_size (sig->params [i]->data.klass, NULL)));
651 #if PPC_PASS_STRUCTS_BY_VALUE
653 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
654 cinfo->args [n].regtype = RegTypeStructByVal;
655 if (gr <= PPC_LAST_ARG_REG) {
656 int rest = PPC_LAST_ARG_REG - gr + 1;
657 int n_in_regs = rest >= nwords? nwords: rest;
658 cinfo->args [n].size = n_in_regs;
659 cinfo->args [n].vtsize = nwords - n_in_regs;
660 cinfo->args [n].reg = gr;
663 cinfo->args [n].size = 0;
664 cinfo->args [n].vtsize = nwords;
666 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
667 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
668 stack_size += nwords * sizeof (gpointer);
671 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
672 cinfo->args [n].regtype = RegTypeStructByAddr;
677 case MONO_TYPE_TYPEDBYREF: {
678 int size = sizeof (MonoTypedRef);
679 /* keep in sync or merge with the valuetype case */
680 #if PPC_PASS_STRUCTS_BY_VALUE
682 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
683 cinfo->args [n].regtype = RegTypeStructByVal;
684 if (gr <= PPC_LAST_ARG_REG) {
685 int rest = PPC_LAST_ARG_REG - gr + 1;
686 int n_in_regs = rest >= nwords? nwords: rest;
687 cinfo->args [n].size = n_in_regs;
688 cinfo->args [n].vtsize = nwords - n_in_regs;
689 cinfo->args [n].reg = gr;
692 cinfo->args [n].size = 0;
693 cinfo->args [n].vtsize = nwords;
695 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
696 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
697 stack_size += nwords * sizeof (gpointer);
700 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
701 cinfo->args [n].regtype = RegTypeStructByAddr;
708 cinfo->args [n].size = 8;
709 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
713 cinfo->args [n].size = 4;
715 cinfo->args [n].regtype = RegTypeFP;
716 cinfo->args [n].reg = fr;
718 FP_ALSO_IN_REG (gr ++);
719 ALWAYS_ON_STACK (stack_size += 4);
721 NOT_IMPLEMENTED ("R4 arg");
726 cinfo->args [n].size = 8;
728 cinfo->args [n].regtype = RegTypeFP;
729 cinfo->args [n].reg = fr;
731 FP_ALSO_IN_REG (gr += 2);
732 ALWAYS_ON_STACK (stack_size += 8);
734 NOT_IMPLEMENTED ("R8 arg");
739 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
744 simpletype = sig->ret->type;
746 switch (simpletype) {
747 case MONO_TYPE_BOOLEAN:
758 case MONO_TYPE_FNPTR:
759 case MONO_TYPE_CLASS:
760 case MONO_TYPE_OBJECT:
761 case MONO_TYPE_SZARRAY:
762 case MONO_TYPE_ARRAY:
763 case MONO_TYPE_STRING:
764 cinfo->ret.reg = ppc_r3;
768 cinfo->ret.reg = ppc_r3;
772 cinfo->ret.reg = ppc_f1;
773 cinfo->ret.regtype = RegTypeFP;
775 case MONO_TYPE_VALUETYPE:
776 if (sig->ret->data.klass->enumtype) {
777 simpletype = sig->ret->data.klass->enum_basetype->type;
781 case MONO_TYPE_TYPEDBYREF:
785 g_error ("Can't handle as return value 0x%x", sig->ret->type);
789 /* align stack size to 16 */
790 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
791 stack_size = (stack_size + 15) & ~15;
793 cinfo->stack_usage = stack_size;
799 * Set var information according to the calling convention. ppc version.
800 * The locals var stuff should most likely be split in another method.
803 mono_arch_allocate_vars (MonoCompile *m)
805 MonoMethodSignature *sig;
806 MonoMethodHeader *header;
808 int i, offset, size, align, curinst;
809 int frame_reg = ppc_sp;
812 * FIXME: we'll use the frame register also for any method that has
813 * filter clauses. This way, when the handlers are called,
814 * the code will reference local variables using the frame reg instead of
815 * the stack pointer: if we had to restore the stack pointer, we'd
816 * corrupt the method frames that are already on the stack (since
817 * filters get called before stack unwinding happens) when the filter
818 * code would call any method.
820 if (m->flags & MONO_CFG_HAS_ALLOCA)
822 m->frame_reg = frame_reg;
824 header = ((MonoMethodNormal *)m->method)->header;
826 sig = m->method->signature;
830 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
831 m->ret->opcode = OP_REGVAR;
832 m->ret->inst_c0 = ppc_r3;
834 /* FIXME: handle long and FP values */
835 switch (sig->ret->type) {
839 m->ret->opcode = OP_REGVAR;
840 m->ret->inst_c0 = ppc_r3;
844 /* local vars are at a positive offset from the stack pointer */
846 * also note that if the function uses alloca, we use ppc_r31
847 * to point at the local variables.
849 offset = PPC_MINIMAL_STACK_SIZE; /* linkage area */
850 /* align the offset to 16 bytes: not sure this is needed here */
852 //offset &= ~(16 - 1);
854 /* add parameter area size for called functions */
855 offset += m->param_area;
859 /* FIXME: check how to handle this stuff... reserve space to save LMF and caller saved registers */
860 if (m->method->save_lmf)
861 offset += sizeof (MonoLMF);
864 /* this stuff should not be needed on ppc and the new jit,
865 * because a call on ppc to the handlers doesn't change the
866 * stack pointer and the jist doesn't manipulate the stack pointer
867 * for operations involving valuetypes.
869 /* reserve space to store the esp */
870 offset += sizeof (gpointer);
872 /* this is a global constant */
873 mono_exc_esp_offset = offset;
876 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
878 offset += sizeof(gpointer) - 1;
879 offset &= ~(sizeof(gpointer) - 1);
880 inst->inst_offset = offset;
881 inst->opcode = OP_REGOFFSET;
882 inst->inst_basereg = frame_reg;
883 offset += sizeof(gpointer);
885 curinst = m->locals_start;
886 for (i = curinst; i < m->num_varinfo; ++i) {
887 inst = m->varinfo [i];
888 if (inst->opcode == OP_REGVAR)
891 /* inst->unused indicates native sized value types, this is used by the
892 * pinvoke wrappers when they call functions returning structure */
893 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
894 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
896 size = mono_type_size (inst->inst_vtype, &align);
899 offset &= ~(align - 1);
900 inst->inst_offset = offset;
901 inst->opcode = OP_REGOFFSET;
902 inst->inst_basereg = frame_reg;
904 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
909 inst = m->varinfo [curinst];
910 if (inst->opcode != OP_REGVAR) {
911 inst->opcode = OP_REGOFFSET;
912 inst->inst_basereg = frame_reg;
913 offset += sizeof (gpointer) - 1;
914 offset &= ~(sizeof (gpointer) - 1);
915 inst->inst_offset = offset;
916 offset += sizeof (gpointer);
921 for (i = 0; i < sig->param_count; ++i) {
922 inst = m->varinfo [curinst];
923 if (inst->opcode != OP_REGVAR) {
924 inst->opcode = OP_REGOFFSET;
925 inst->inst_basereg = frame_reg;
926 size = mono_type_size (sig->params [i], &align);
928 offset &= ~(align - 1);
929 inst->inst_offset = offset;
935 /* align the offset to 16 bytes */
940 m->stack_offset = offset;
944 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
945 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
949 * take the arguments and generate the arch-specific
950 * instructions to properly call the function in call.
951 * This includes pushing, moving arguments to the right register
953 * Issue: who does the spilling if needed, and when?
956 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
958 MonoMethodSignature *sig;
964 sig = call->signature;
965 n = sig->param_count + sig->hasthis;
967 cinfo = calculate_sizes (sig, sig->pinvoke);
968 if (cinfo->struct_ret)
969 call->used_iregs |= 1 << cinfo->struct_ret;
971 for (i = 0; i < n; ++i) {
972 ainfo = cinfo->args + i;
973 if (is_virtual && i == 0) {
974 /* the argument will be attached to the call instrucion */
976 call->used_iregs |= 1 << ainfo->reg;
978 MONO_INST_NEW (cfg, arg, OP_OUTARG);
980 arg->cil_code = in->cil_code;
982 arg->type = in->type;
983 /* prepend, we'll need to reverse them later */
984 arg->next = call->out_args;
985 call->out_args = arg;
986 if (ainfo->regtype == RegTypeGeneral) {
987 arg->unused = ainfo->reg;
988 call->used_iregs |= 1 << ainfo->reg;
989 if (arg->type == STACK_I8)
990 call->used_iregs |= 1 << (ainfo->reg + 1);
991 } else if (ainfo->regtype == RegTypeStructByAddr) {
992 /* FIXME: where si the data allocated? */
993 arg->unused = ainfo->reg;
994 call->used_iregs |= 1 << ainfo->reg;
995 } else if (ainfo->regtype == RegTypeStructByVal) {
997 /* mark the used regs */
998 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
999 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
1001 arg->opcode = OP_OUTARG_VT;
1002 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
1003 arg->inst_imm = ainfo->offset;
1004 } else if (ainfo->regtype == RegTypeBase) {
1005 arg->opcode = OP_OUTARG;
1006 arg->unused = ainfo->reg | (ainfo->size << 8);
1007 arg->inst_imm = ainfo->offset;
1008 } else if (ainfo->regtype == RegTypeFP) {
1009 arg->opcode = OP_OUTARG_R8;
1010 arg->unused = ainfo->reg;
1011 call->used_fregs |= 1 << ainfo->reg;
1012 if (ainfo->size == 4) {
1013 /* we reduce the precision */
1015 MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
1016 conv->inst_left = arg->inst_left;
1017 arg->inst_left = conv;
1020 g_assert_not_reached ();
1025 * Reverse the call->out_args list.
1028 MonoInst *prev = NULL, *list = call->out_args, *next;
1035 call->out_args = prev;
1037 call->stack_usage = cinfo->stack_usage;
1038 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
1039 cfg->flags |= MONO_CFG_HAS_CALLS;
1041 * should set more info in call, such as the stack space
1042 * used by the args that needs to be added back to esp
1050 * Allow tracing to work with this interface (with an optional argument)
1054 * This may be needed on some archs or for debugging support.
1057 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
1059 /* no stack room needed now (may be needed for FASTCALL-trace support) */
1061 /* split prolog-epilog requirements? */
1062 *code = 50; /* max bytes needed: check this number */
1066 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1070 ppc_load (code, ppc_r3, cfg->method);
1071 ppc_li (code, ppc_r4, 0); /* NULL ebp for now */
1072 ppc_load (code, ppc_r0, func);
1073 ppc_mtlr (code, ppc_r0);
1087 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1090 int save_mode = SAVE_NONE;
1091 MonoMethod *method = cfg->method;
1092 int rtype = method->signature->ret->type;
1096 case MONO_TYPE_VOID:
1097 /* special case string .ctor icall */
1098 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1099 save_mode = SAVE_ONE;
1101 save_mode = SAVE_NONE;
1105 save_mode = SAVE_TWO;
1109 save_mode = SAVE_FP;
1111 case MONO_TYPE_VALUETYPE:
1112 if (method->signature->ret->data.klass->enumtype) {
1113 rtype = method->signature->ret->data.klass->enum_basetype->type;
1116 save_mode = SAVE_STRUCT;
1119 save_mode = SAVE_ONE;
1123 switch (save_mode) {
1125 ppc_stw (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
1126 ppc_stw (code, ppc_r4, cfg->stack_usage - 4, cfg->frame_reg);
1127 if (enable_arguments) {
1128 ppc_mr (code, ppc_r5, ppc_r4);
1129 ppc_mr (code, ppc_r4, ppc_r3);
1133 ppc_stw (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
1134 if (enable_arguments) {
1135 ppc_mr (code, ppc_r4, ppc_r3);
1139 ppc_stfd (code, ppc_f1, cfg->stack_usage - 8, cfg->frame_reg);
1140 if (enable_arguments) {
1141 /* FIXME: what reg? */
1142 ppc_fmr (code, ppc_f3, ppc_f1);
1143 ppc_lwz (code, ppc_r4, cfg->stack_usage - 8, cfg->frame_reg);
1144 ppc_lwz (code, ppc_r5, cfg->stack_usage - 4, cfg->frame_reg);
1148 if (enable_arguments) {
1149 /* FIXME: get the actual address */
1150 ppc_mr (code, ppc_r4, ppc_r3);
1158 ppc_load (code, ppc_r3, cfg->method);
1159 ppc_load (code, ppc_r0, func);
1160 ppc_mtlr (code, ppc_r0);
1163 switch (save_mode) {
1165 ppc_lwz (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
1166 ppc_lwz (code, ppc_r4, cfg->stack_usage - 4, cfg->frame_reg);
1169 ppc_lwz (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
1172 ppc_lfd (code, ppc_f1, cfg->stack_usage - 8, cfg->frame_reg);
1182 * Conditional branches have a small offset, so if it is likely overflowed,
1183 * we do a branch to the end of the method (uncond branches have much larger
1184 * offsets) where we perform the conditional and jump back unconditionally.
1185 * It's slightly slower, since we add two uncond branches, but it's very simple
1186 * with the current patch implementation and such large methods are likely not
1187 * going to be perf critical anyway.
1195 #define EMIT_COND_BRANCH(ins,cond) \
1196 if (ins->flags & MONO_INST_BRLABEL) { \
1197 if (0 && ins->inst_i0->inst_c0) { \
1198 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff); \
1200 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1201 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0); \
1204 if (0 && ins->inst_true_bb->native_offset) { \
1205 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
1207 int br_disp = ins->inst_true_bb->max_offset - offset; \
1208 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
1209 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
1210 ovfj->bb = ins->inst_true_bb; \
1211 ovfj->b0_cond = branch_b0_table [cond]; \
1212 ovfj->b1_cond = branch_b1_table [cond]; \
1213 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB_OVF, ovfj); \
1216 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1217 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0); \
1222 /* emit an exception if condition is fail */
1223 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1225 mono_add_patch_info (cfg, code - cfg->native_code, \
1226 MONO_PATCH_INFO_EXC, exc_name); \
1227 x86_branch32 (code, cond, 0, signed); \
1231 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1233 MonoInst *ins, *last_ins = NULL;
1238 switch (ins->opcode) {
1240 /* remove unnecessary multiplication with 1 */
1241 if (ins->inst_imm == 1) {
1242 if (ins->dreg != ins->sreg1) {
1243 ins->opcode = OP_MOVE;
1245 last_ins->next = ins->next;
1251 case OP_LOAD_MEMBASE:
1252 case OP_LOADI4_MEMBASE:
1254 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1255 * OP_LOAD_MEMBASE offset(basereg), reg
1257 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1258 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1259 ins->inst_basereg == last_ins->inst_destbasereg &&
1260 ins->inst_offset == last_ins->inst_offset) {
1261 if (ins->dreg == last_ins->sreg1) {
1262 last_ins->next = ins->next;
1266 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1267 ins->opcode = OP_MOVE;
1268 ins->sreg1 = last_ins->sreg1;
1272 * Note: reg1 must be different from the basereg in the second load
1273 * OP_LOAD_MEMBASE offset(basereg), reg1
1274 * OP_LOAD_MEMBASE offset(basereg), reg2
1276 * OP_LOAD_MEMBASE offset(basereg), reg1
1277 * OP_MOVE reg1, reg2
1279 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1280 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1281 ins->inst_basereg != last_ins->dreg &&
1282 ins->inst_basereg == last_ins->inst_basereg &&
1283 ins->inst_offset == last_ins->inst_offset) {
1285 if (ins->dreg == last_ins->dreg) {
1286 last_ins->next = ins->next;
1290 ins->opcode = OP_MOVE;
1291 ins->sreg1 = last_ins->dreg;
1294 //g_assert_not_reached ();
1298 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1299 * OP_LOAD_MEMBASE offset(basereg), reg
1301 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1302 * OP_ICONST reg, imm
1304 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1305 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1306 ins->inst_basereg == last_ins->inst_destbasereg &&
1307 ins->inst_offset == last_ins->inst_offset) {
1308 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1309 ins->opcode = OP_ICONST;
1310 ins->inst_c0 = last_ins->inst_imm;
1311 g_assert_not_reached (); // check this rule
1315 case OP_LOADU1_MEMBASE:
1316 case OP_LOADI1_MEMBASE:
1317 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1318 ins->inst_basereg == last_ins->inst_destbasereg &&
1319 ins->inst_offset == last_ins->inst_offset) {
1320 if (ins->dreg == last_ins->sreg1) {
1321 last_ins->next = ins->next;
1325 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1326 ins->opcode = OP_MOVE;
1327 ins->sreg1 = last_ins->sreg1;
1331 case OP_LOADU2_MEMBASE:
1332 case OP_LOADI2_MEMBASE:
1333 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1334 ins->inst_basereg == last_ins->inst_destbasereg &&
1335 ins->inst_offset == last_ins->inst_offset) {
1336 if (ins->dreg == last_ins->sreg1) {
1337 last_ins->next = ins->next;
1341 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1342 ins->opcode = OP_MOVE;
1343 ins->sreg1 = last_ins->sreg1;
1353 if (ins->dreg == ins->sreg1) {
1355 last_ins->next = ins->next;
1360 * OP_MOVE sreg, dreg
1361 * OP_MOVE dreg, sreg
1363 if (last_ins && last_ins->opcode == OP_MOVE &&
1364 ins->sreg1 == last_ins->dreg &&
1365 ins->dreg == last_ins->sreg1) {
1366 last_ins->next = ins->next;
1375 bb->last_ins = last_ins;
1379 * the branch_b0_table should maintain the order of these
1393 branch_b0_table [] = {
1408 branch_b1_table [] = {
1423 * returns the offset used by spillvar. It allocates a new
1424 * spill variable if necessary.
1427 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1429 MonoSpillInfo **si, *info;
1432 si = &cfg->spill_info;
1434 while (i <= spillvar) {
1437 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1439 info->offset = cfg->stack_offset;
1440 cfg->stack_offset += sizeof (gpointer);
1444 return (*si)->offset;
1450 g_assert_not_reached ();
1455 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1457 MonoSpillInfo **si, *info;
1460 si = &cfg->spill_info_float;
1462 while (i <= spillvar) {
1465 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1467 cfg->stack_offset += 7;
1468 cfg->stack_offset &= ~7;
1469 info->offset = cfg->stack_offset;
1470 cfg->stack_offset += sizeof (double);
1474 return (*si)->offset;
1480 g_assert_not_reached ();
1485 #define DEBUG(a) if (cfg->verbose_level > 1) a
1487 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1488 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 14)
1497 static const char*const * ins_spec = ppcg4;
1500 print_ins (int i, MonoInst *ins)
1502 const char *spec = ins_spec [ins->opcode];
1503 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1504 if (spec [MONO_INST_DEST]) {
1505 if (ins->dreg >= MONO_MAX_IREGS)
1506 g_print (" R%d <-", ins->dreg);
1508 g_print (" %s <-", mono_arch_regname (ins->dreg));
1510 if (spec [MONO_INST_SRC1]) {
1511 if (ins->sreg1 >= MONO_MAX_IREGS)
1512 g_print (" R%d", ins->sreg1);
1514 g_print (" %s", mono_arch_regname (ins->sreg1));
1516 if (spec [MONO_INST_SRC2]) {
1517 if (ins->sreg2 >= MONO_MAX_IREGS)
1518 g_print (" R%d", ins->sreg2);
1520 g_print (" %s", mono_arch_regname (ins->sreg2));
1522 if (spec [MONO_INST_CLOB])
1523 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1528 print_regtrack (RegTrack *t, int num)
1534 for (i = 0; i < num; ++i) {
1537 if (i >= MONO_MAX_IREGS) {
1538 g_snprintf (buf, sizeof(buf), "R%d", i);
1541 r = mono_arch_regname (i);
1542 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1546 typedef struct InstList InstList;
1554 static inline InstList*
1555 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1557 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1567 * Force the spilling of the variable in the symbolic register 'reg'.
1570 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1575 sel = cfg->rs->iassign [reg];
1576 /*i = cfg->rs->isymbolic [sel];
1577 g_assert (i == reg);*/
1579 spill = ++cfg->spill_count;
1580 cfg->rs->iassign [i] = -spill - 1;
1581 mono_regstate_free_int (cfg->rs, sel);
1582 /* we need to create a spill var and insert a load to sel after the current instruction */
1583 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1585 load->inst_basereg = cfg->frame_reg;
1586 load->inst_offset = mono_spillvar_offset (cfg, spill);
1588 while (ins->next != item->prev->data)
1591 load->next = ins->next;
1593 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1594 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1595 g_assert (i == sel);
1601 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1606 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));
1607 /* exclude the registers in the current instruction */
1608 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1609 if (ins->sreg1 >= MONO_MAX_IREGS)
1610 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1612 regmask &= ~ (1 << ins->sreg1);
1613 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1615 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1616 if (ins->sreg2 >= MONO_MAX_IREGS)
1617 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1619 regmask &= ~ (1 << ins->sreg2);
1620 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1622 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1623 regmask &= ~ (1 << ins->dreg);
1624 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1627 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1628 g_assert (regmask); /* need at least a register we can free */
1630 /* we should track prev_use and spill the register that's farther */
1631 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1632 if (regmask & (1 << i)) {
1634 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1638 i = cfg->rs->isymbolic [sel];
1639 spill = ++cfg->spill_count;
1640 cfg->rs->iassign [i] = -spill - 1;
1641 mono_regstate_free_int (cfg->rs, sel);
1642 /* we need to create a spill var and insert a load to sel after the current instruction */
1643 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1645 load->inst_basereg = cfg->frame_reg;
1646 load->inst_offset = mono_spillvar_offset (cfg, spill);
1648 while (ins->next != item->prev->data)
1651 load->next = ins->next;
1653 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1654 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1655 g_assert (i == sel);
1661 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1666 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));
1667 /* exclude the registers in the current instruction */
1668 if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1669 if (ins->sreg1 >= MONO_MAX_FREGS)
1670 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1672 regmask &= ~ (1 << ins->sreg1);
1673 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1675 if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1676 if (ins->sreg2 >= MONO_MAX_FREGS)
1677 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1679 regmask &= ~ (1 << ins->sreg2);
1680 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1682 if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1683 regmask &= ~ (1 << ins->dreg);
1684 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1687 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1688 g_assert (regmask); /* need at least a register we can free */
1690 /* we should track prev_use and spill the register that's farther */
1691 for (i = 0; i < MONO_MAX_FREGS; ++i) {
1692 if (regmask & (1 << i)) {
1694 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1698 i = cfg->rs->fsymbolic [sel];
1699 spill = ++cfg->spill_count;
1700 cfg->rs->fassign [i] = -spill - 1;
1701 mono_regstate_free_float(cfg->rs, sel);
1702 /* we need to create a spill var and insert a load to sel after the current instruction */
1703 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1705 load->inst_basereg = cfg->frame_reg;
1706 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1708 while (ins->next != item->prev->data)
1711 load->next = ins->next;
1713 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1714 i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1715 g_assert (i == sel);
1721 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1724 MONO_INST_NEW (cfg, copy, OP_MOVE);
1728 copy->next = ins->next;
1731 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1736 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1739 MONO_INST_NEW (cfg, copy, OP_FMOVE);
1743 copy->next = ins->next;
1746 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1751 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1754 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1756 store->inst_destbasereg = cfg->frame_reg;
1757 store->inst_offset = mono_spillvar_offset (cfg, spill);
1759 store->next = ins->next;
1762 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)));
1767 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1770 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1772 store->inst_destbasereg = cfg->frame_reg;
1773 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1775 store->next = ins->next;
1778 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)));
1783 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1786 g_assert (item->next);
1787 prev = item->next->data;
1789 while (prev->next != ins)
1791 to_insert->next = ins;
1792 prev->next = to_insert;
1794 * needed otherwise in the next instruction we can add an ins to the
1795 * end and that would get past this instruction.
1797 item->data = to_insert;
1801 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1803 int val = cfg->rs->iassign [sym_reg];
1807 /* the register gets spilled after this inst */
1810 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1812 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1813 cfg->rs->iassign [sym_reg] = val;
1814 /* add option to store before the instruction for src registers */
1816 create_spilled_store (cfg, spill, val, sym_reg, ins);
1818 cfg->rs->isymbolic [val] = sym_reg;
1822 /* use ppc_r3-ppc_10 as temp registers */
1823 #define PPC_CALLER_REGS ((0xff<<3) | USE_EXTRA_TEMPS)
1824 #define PPC_CALLER_FREGS (0xff<<2)
1827 * Local register allocation.
1828 * We first scan the list of instructions and we save the liveness info of
1829 * each register (when the register is first used, when it's value is set etc.).
1830 * We also reverse the list of instructions (in the InstList list) because assigning
1831 * registers backwards allows for more tricks to be used.
1834 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1837 MonoRegState *rs = cfg->rs;
1839 RegTrack *reginfo, *reginfof;
1840 RegTrack *reginfo1, *reginfo2, *reginfod;
1841 InstList *tmp, *reversed = NULL;
1843 guint32 src1_mask, src2_mask, dest_mask;
1844 guint32 cur_iregs, cur_fregs;
1848 rs->next_vireg = bb->max_ireg;
1849 rs->next_vfreg = bb->max_freg;
1850 mono_regstate_assign (rs);
1851 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1852 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1853 rs->ifree_mask = PPC_CALLER_REGS;
1854 rs->ffree_mask = PPC_CALLER_FREGS;
1858 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1859 /* forward pass on the instructions to collect register liveness info */
1861 spec = ins_spec [ins->opcode];
1862 DEBUG (print_ins (i, ins));
1863 if (spec [MONO_INST_CLOB] == 'c') {
1864 MonoCallInst * call = (MonoCallInst*)ins;
1867 if (spec [MONO_INST_SRC1]) {
1868 if (spec [MONO_INST_SRC1] == 'f')
1869 reginfo1 = reginfof;
1872 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1873 reginfo1 [ins->sreg1].last_use = i;
1877 if (spec [MONO_INST_SRC2]) {
1878 if (spec [MONO_INST_SRC2] == 'f')
1879 reginfo2 = reginfof;
1882 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1883 reginfo2 [ins->sreg2].last_use = i;
1887 if (spec [MONO_INST_DEST]) {
1888 if (spec [MONO_INST_DEST] == 'f')
1889 reginfod = reginfof;
1892 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1893 reginfod [ins->dreg].killed_in = i;
1894 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1895 reginfod [ins->dreg].last_use = i;
1896 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1897 reginfod [ins->dreg].born_in = i;
1898 if (spec [MONO_INST_DEST] == 'l') {
1899 /* result in eax:edx, the virtual register is allocated sequentially */
1900 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1901 reginfod [ins->dreg + 1].last_use = i;
1902 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1903 reginfod [ins->dreg + 1].born_in = i;
1908 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1913 cur_iregs = PPC_CALLER_REGS;
1914 cur_fregs = PPC_CALLER_FREGS;
1916 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1917 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1920 int prev_dreg, prev_sreg1, prev_sreg2;
1923 spec = ins_spec [ins->opcode];
1924 DEBUG (g_print ("processing:"));
1925 DEBUG (print_ins (i, ins));
1926 /* make the register available for allocation: FIXME add fp reg */
1927 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1928 cur_iregs |= 1 << ins->dreg;
1929 DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1930 } else if (ins->opcode == OP_SETFREG) {
1931 cur_fregs |= 1 << ins->dreg;
1932 DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1933 } else if (spec [MONO_INST_CLOB] == 'c') {
1934 MonoCallInst *cinst = (MonoCallInst*)ins;
1935 DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1936 cur_iregs &= ~cinst->used_iregs;
1937 cur_fregs &= ~cinst->used_fregs;
1938 DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1939 /* registers used by the calling convention are excluded from
1940 * allocation: they will be selectively enabled when they are
1941 * assigned by the special SETREG opcodes.
1944 dest_mask = src1_mask = src2_mask = cur_iregs;
1945 /* update for use with FP regs... */
1946 if (spec [MONO_INST_DEST] == 'f') {
1947 if (ins->dreg >= MONO_MAX_FREGS) {
1948 val = rs->fassign [ins->dreg];
1949 prev_dreg = ins->dreg;
1953 /* the register gets spilled after this inst */
1956 val = mono_regstate_alloc_float (rs, dest_mask);
1958 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1959 rs->fassign [ins->dreg] = val;
1961 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1963 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1964 rs->fsymbolic [val] = prev_dreg;
1966 if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != ppc_f1) {
1967 /* this instruction only outputs to ppc_f3, need to copy */
1968 create_copy_ins_float (cfg, ins->dreg, ppc_f1, ins);
1973 if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1974 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1975 mono_regstate_free_float (rs, ins->dreg);
1977 } else if (ins->dreg >= MONO_MAX_IREGS) {
1978 val = rs->iassign [ins->dreg];
1979 prev_dreg = ins->dreg;
1983 /* the register gets spilled after this inst */
1986 val = mono_regstate_alloc_int (rs, dest_mask);
1988 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1989 rs->iassign [ins->dreg] = val;
1991 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1993 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1994 rs->isymbolic [val] = prev_dreg;
1996 if (spec [MONO_INST_DEST] == 'l') {
1997 int hreg = prev_dreg + 1;
1998 val = rs->iassign [hreg];
2002 /* the register gets spilled after this inst */
2005 val = mono_regstate_alloc_int (rs, dest_mask);
2007 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
2008 rs->iassign [hreg] = val;
2010 create_spilled_store (cfg, spill, val, hreg, ins);
2012 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
2013 rs->isymbolic [val] = hreg;
2014 /* FIXME:? ins->dreg = val; */
2015 if (ins->dreg == ppc_r4) {
2017 create_copy_ins (cfg, val, ppc_r3, ins);
2018 } else if (ins->dreg == ppc_r3) {
2019 if (val == ppc_r4) {
2021 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
2022 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
2023 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
2025 /* two forced copies */
2026 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
2027 create_copy_ins (cfg, val, ppc_r3, ins);
2030 if (val == ppc_r3) {
2031 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
2033 /* two forced copies */
2034 create_copy_ins (cfg, val, ppc_r3, ins);
2035 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
2038 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
2039 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
2040 mono_regstate_free_int (rs, val);
2042 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != ppc_r3 && spec [MONO_INST_CLOB] != 'd') {
2043 /* this instruction only outputs to ppc_r3, need to copy */
2044 create_copy_ins (cfg, ins->dreg, ppc_r3, ins);
2049 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
2050 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
2051 mono_regstate_free_int (rs, ins->dreg);
2053 if (spec [MONO_INST_SRC1] == 'f') {
2054 if (ins->sreg1 >= MONO_MAX_FREGS) {
2055 val = rs->fassign [ins->sreg1];
2056 prev_sreg1 = ins->sreg1;
2060 /* the register gets spilled after this inst */
2063 //g_assert (val == -1); /* source cannot be spilled */
2064 val = mono_regstate_alloc_float (rs, src1_mask);
2066 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
2067 rs->fassign [ins->sreg1] = val;
2068 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2070 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
2071 insert_before_ins (ins, tmp, store);
2074 rs->fsymbolic [val] = prev_sreg1;
2079 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
2080 val = rs->iassign [ins->sreg1];
2081 prev_sreg1 = ins->sreg1;
2085 /* the register gets spilled after this inst */
2088 if (0 && ins->opcode == OP_MOVE) {
2090 * small optimization: the dest register is already allocated
2091 * but the src one is not: we can simply assign the same register
2092 * here and peephole will get rid of the instruction later.
2093 * This optimization may interfere with the clobbering handling:
2094 * it removes a mov operation that will be added again to handle clobbering.
2095 * There are also some other issues that should with make testjit.
2097 mono_regstate_alloc_int (rs, 1 << ins->dreg);
2098 val = rs->iassign [ins->sreg1] = ins->dreg;
2099 //g_assert (val >= 0);
2100 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2102 //g_assert (val == -1); /* source cannot be spilled */
2103 val = mono_regstate_alloc_int (rs, src1_mask);
2105 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
2106 rs->iassign [ins->sreg1] = val;
2107 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2110 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
2111 insert_before_ins (ins, tmp, store);
2114 rs->isymbolic [val] = prev_sreg1;
2119 if (spec [MONO_INST_SRC2] == 'f') {
2120 if (ins->sreg2 >= MONO_MAX_FREGS) {
2121 val = rs->fassign [ins->sreg2];
2122 prev_sreg2 = ins->sreg2;
2126 /* the register gets spilled after this inst */
2129 val = mono_regstate_alloc_float (rs, src2_mask);
2131 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
2132 rs->fassign [ins->sreg2] = val;
2133 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
2135 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
2137 rs->fsymbolic [val] = prev_sreg2;
2142 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
2143 val = rs->iassign [ins->sreg2];
2144 prev_sreg2 = ins->sreg2;
2148 /* the register gets spilled after this inst */
2151 val = mono_regstate_alloc_int (rs, src2_mask);
2153 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
2154 rs->iassign [ins->sreg2] = val;
2155 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
2157 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
2159 rs->isymbolic [val] = prev_sreg2;
2165 if (spec [MONO_INST_CLOB] == 'c') {
2167 guint32 clob_mask = PPC_CALLER_REGS;
2168 for (j = 0; j < MONO_MAX_IREGS; ++j) {
2170 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
2171 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
2175 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2176 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2177 mono_regstate_free_int (rs, ins->sreg1);
2179 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2180 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2181 mono_regstate_free_int (rs, ins->sreg2);
2184 //DEBUG (print_ins (i, ins));
2190 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2192 /* sreg is a float, dreg is an integer reg. ppc_f1 is used a scratch */
2193 ppc_fctiwz (code, ppc_f1, sreg);
2194 ppc_stfd (code, ppc_f1, -8, ppc_sp);
2195 ppc_lwz (code, dreg, -4, ppc_sp);
2198 ppc_andid (code, dreg, dreg, 0xff);
2200 ppc_andid (code, dreg, dreg, 0xffff);
2203 ppc_extsb (code, dreg, dreg);
2205 ppc_extsh (code, dreg, dreg);
2210 static unsigned char*
2211 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2214 int sreg = tree->sreg1;
2215 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2216 if (tree->flags & MONO_INST_INIT) {
2218 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2219 x86_push_reg (code, X86_EAX);
2222 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2223 x86_push_reg (code, X86_ECX);
2226 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2227 x86_push_reg (code, X86_EDI);
2231 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2232 if (sreg != X86_ECX)
2233 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2234 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2236 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2238 x86_prefix (code, X86_REP_PREFIX);
2241 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2242 x86_pop_reg (code, X86_EDI);
2243 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2244 x86_pop_reg (code, X86_ECX);
2245 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2246 x86_pop_reg (code, X86_EAX);
2253 ppc_patch (guchar *code, guchar *target)
2255 guint32 ins = *(guint32*)code;
2256 guint32 prim = ins >> 26;
2259 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2263 guint32 li = (guint32)target;
2264 ovf = li & 0xfc000000;
2265 if ((li & 3) || ovf)
2266 g_assert_not_reached ();
2267 ins = prim << 26 | (ins & 3);
2270 gint diff = target - code;
2271 ovf = diff & 0xfc000000;
2272 if ((diff & 3) || (ovf != 0 && ovf != 0xfc000000))
2273 g_assert_not_reached ();
2274 ins = prim << 26 | (ins & 3);
2276 diff &= ~(63 << 26);
2279 *(guint32*)code = ins;
2280 } else if (prim == 16) {
2283 guint32 li = (guint32)target;
2284 ins = (ins & 0xffff0000) | (ins & 3);
2285 ovf = li & 0xffff0000;
2286 if (ovf != 0 && ovf != 0xffff0000)
2287 g_assert_not_reached ();
2290 // FIXME: assert the top bits of li are 0
2292 gint diff = target - code;
2293 ins = (ins & 0xffff0000) | (ins & 3);
2294 ovf = diff & 0xffff0000;
2295 if (ovf != 0 && ovf != 0xffff0000)
2296 g_assert_not_reached ();
2300 *(guint32*)code = ins;
2302 g_assert_not_reached ();
2304 // g_print ("patched with 0x%08x\n", ins);
2308 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2313 guint8 *code = cfg->native_code + cfg->code_len;
2314 MonoInst *last_ins = NULL;
2315 guint last_offset = 0;
2318 if (cfg->opt & MONO_OPT_PEEPHOLE)
2319 peephole_pass (cfg, bb);
2321 /* we don't align basic blocks of loops on ppc */
2323 if (cfg->verbose_level > 2)
2324 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2326 cpos = bb->max_offset;
2328 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2329 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2330 //g_assert (!mono_compile_aot);
2333 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2334 /* this is not thread save, but good enough */
2335 /* fixme: howto handle overflows? */
2336 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2341 offset = code - cfg->native_code;
2343 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2345 if (offset > (cfg->code_size - max_len - 16)) {
2346 cfg->code_size *= 2;
2347 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2348 code = cfg->native_code + offset;
2350 // if (ins->cil_code)
2351 // g_print ("cil code\n");
2353 switch (ins->opcode) {
2354 case OP_STOREI1_MEMBASE_IMM:
2355 ppc_li (code, ppc_r11, ins->inst_imm);
2356 g_assert (ppc_is_imm16 (ins->inst_offset));
2357 ppc_stb (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2359 case OP_STOREI2_MEMBASE_IMM:
2360 ppc_li (code, ppc_r11, ins->inst_imm);
2361 g_assert (ppc_is_imm16 (ins->inst_offset));
2362 ppc_sth (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2364 case OP_STORE_MEMBASE_IMM:
2365 case OP_STOREI4_MEMBASE_IMM:
2366 ppc_load (code, ppc_r11, ins->inst_imm);
2367 g_assert (ppc_is_imm16 (ins->inst_offset));
2368 ppc_stw (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2370 case OP_STOREI1_MEMBASE_REG:
2371 g_assert (ppc_is_imm16 (ins->inst_offset));
2372 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2374 case OP_STOREI2_MEMBASE_REG:
2375 g_assert (ppc_is_imm16 (ins->inst_offset));
2376 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2378 case OP_STORE_MEMBASE_REG:
2379 case OP_STOREI4_MEMBASE_REG:
2380 g_assert (ppc_is_imm16 (ins->inst_offset));
2381 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2386 g_assert_not_reached ();
2387 //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2390 g_assert_not_reached ();
2391 //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2392 //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2394 case OP_LOAD_MEMBASE:
2395 case OP_LOADI4_MEMBASE:
2396 case OP_LOADU4_MEMBASE:
2397 if (ppc_is_imm16 (ins->inst_offset)) {
2398 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2400 ppc_load (code, ppc_r11, ins->inst_offset);
2401 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2404 case OP_LOADU1_MEMBASE:
2405 g_assert (ppc_is_imm16 (ins->inst_offset));
2406 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2408 case OP_LOADI1_MEMBASE:
2409 g_assert (ppc_is_imm16 (ins->inst_offset));
2410 // FIXME: sign extend
2411 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2413 case OP_LOADU2_MEMBASE:
2414 g_assert (ppc_is_imm16 (ins->inst_offset));
2415 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2417 case OP_LOADI2_MEMBASE:
2418 g_assert (ppc_is_imm16 (ins->inst_offset));
2419 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2422 ppc_extsb (code, ins->dreg, ins->sreg1);
2425 ppc_extsh (code, ins->dreg, ins->sreg1);
2428 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
2431 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
2434 if (ins->next && (ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN))
2435 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2437 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2439 case OP_COMPARE_IMM:
2440 if (ins->next && ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) {
2441 if (ppc_is_uimm16 (ins->inst_imm)) {
2442 ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2444 ppc_load (code, ppc_r11, ins->inst_imm);
2445 ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2448 if (ppc_is_imm16 (ins->inst_imm)) {
2449 ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2451 ppc_load (code, ppc_r11, ins->inst_imm);
2452 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2456 case OP_X86_TEST_NULL:
2457 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2463 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2466 ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2469 ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2472 if (ppc_is_imm16 (ins->inst_imm)) {
2473 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2475 ppc_load (code, ppc_r11, ins->inst_imm);
2476 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2480 ppc_load (code, ppc_r11, ins->inst_imm);
2481 ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2484 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2487 ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2490 ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2493 // we add the negated value
2494 if (ppc_is_imm16 (-ins->inst_imm))
2495 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2497 ppc_load (code, ppc_r11, ins->inst_imm);
2498 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2502 ppc_load (code, ppc_r11, ins->inst_imm);
2503 ppc_subfe (code, ins->dreg, ins->sreg2, ppc_r11);
2506 g_assert (ppc_is_imm16 (ins->inst_imm));
2507 ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2510 ppc_subfze (code, ins->dreg, ins->sreg1);
2513 /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2514 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2517 if (!(ins->inst_imm & 0xffff0000)) {
2518 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2519 } else if (!(ins->inst_imm & 0xffff)) {
2520 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2522 ppc_load (code, ppc_r11, ins->inst_imm);
2523 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2527 ppc_divw (code, ins->dreg, ins->sreg1, ins->sreg2);
2530 ppc_divwu (code, ins->dreg, ins->sreg1, ins->sreg2);
2533 ppc_load (code, ppc_r11, ins->inst_imm);
2534 ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2537 ppc_divw (code, ppc_r11, ins->sreg1, ins->sreg2);
2538 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2539 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2542 ppc_divwu (code, ppc_r11, ins->sreg1, ins->sreg2);
2543 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2544 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2547 ppc_load (code, ppc_r11, ins->inst_imm);
2548 ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2549 ppc_mullw (code, ins->dreg, ins->dreg, ppc_r11);
2550 ppc_subf (code, ins->dreg, ins->dreg, ins->sreg1);
2553 ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2556 if (!(ins->inst_imm & 0xffff0000)) {
2557 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2558 } else if (!(ins->inst_imm & 0xffff)) {
2559 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2561 ppc_load (code, ppc_r11, ins->inst_imm);
2562 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2566 ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2569 if (!(ins->inst_imm & 0xffff0000)) {
2570 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2571 } else if (!(ins->inst_imm & 0xffff)) {
2572 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2574 ppc_load (code, ppc_r11, ins->inst_imm);
2575 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2579 ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2582 ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2583 //ppc_load (code, ppc_r11, ins->inst_imm);
2584 //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2587 ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2590 // there is also ppc_srawi
2591 //ppc_load (code, ppc_r11, ins->inst_imm);
2592 //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2593 ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2596 ppc_load (code, ppc_r11, ins->inst_imm);
2597 ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);
2598 //ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0xf)), (ins->inst_imm & 0xf), 31);
2601 ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2604 ppc_not (code, ins->dreg, ins->sreg1);
2607 ppc_neg (code, ins->dreg, ins->sreg1);
2610 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2613 ppc_load (code, ppc_r11, ins->inst_imm);
2614 ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2617 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2618 //g_assert_not_reached ();
2619 //x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2620 //EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2622 case CEE_MUL_OVF_UN:
2623 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2624 //FIXME: g_assert_not_reached ();
2628 ppc_load (code, ins->dreg, ins->inst_c0);
2631 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2632 ppc_lis (code, ins->dreg, 0);
2633 ppc_ori (code, ins->dreg, ins->dreg, 0);
2639 ppc_mr (code, ins->dreg, ins->sreg1);
2642 int saved = ins->sreg1;
2643 if (ins->sreg1 == ppc_r3) {
2644 ppc_mr (code, ppc_r0, ins->sreg1);
2647 if (ins->sreg2 != ppc_r3)
2648 ppc_mr (code, ppc_r3, ins->sreg2);
2649 if (saved != ppc_r4)
2650 ppc_mr (code, ppc_r4, saved);
2655 ppc_fmr (code, ins->dreg, ins->sreg1);
2657 case OP_FCONV_TO_R4:
2658 ppc_frsp (code, ins->dreg, ins->sreg1);
2661 g_assert_not_reached ();
2664 /* ensure ins->sreg1 is not NULL */
2665 ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2672 call = (MonoCallInst*)ins;
2673 if (ins->flags & MONO_INST_HAS_METHOD)
2674 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2676 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2682 case OP_VOIDCALL_REG:
2684 ppc_mtlr (code, ins->sreg1);
2687 case OP_FCALL_MEMBASE:
2688 case OP_LCALL_MEMBASE:
2689 case OP_VCALL_MEMBASE:
2690 case OP_VOIDCALL_MEMBASE:
2691 case OP_CALL_MEMBASE:
2692 ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2693 ppc_mtlr (code, ppc_r0);
2697 g_assert_not_reached ();
2700 /* keep alignment */
2701 ppc_addi (code, ppc_r0, ins->sreg1, PPC_STACK_ALIGNMENT-1);
2702 ppc_rlwinm (code, ppc_r0, ppc_r0, 0, 0, 27);
2703 ppc_lwz (code, ppc_r11, 0, ppc_sp);
2704 ppc_neg (code, ppc_r0, ppc_r0);
2705 ppc_stwux (code, ppc_r11, ppc_r0, ppc_sp);
2706 ppc_addi (code, ins->dreg, ppc_sp, PPC_STACK_PARAM_OFFSET);
2712 ppc_mr (code, ppc_r3, ins->sreg1);
2713 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2714 (gpointer)"mono_arch_throw_exception");
2718 case OP_START_HANDLER:
2719 ppc_mflr (code, ppc_r0);
2720 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2723 if (ins->sreg1 != ppc_r3)
2724 ppc_mr (code, ppc_r3, ins->sreg1);
2725 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2726 ppc_mtlr (code, ppc_r0);
2729 case CEE_ENDFINALLY:
2730 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2731 ppc_mtlr (code, ppc_r0);
2734 case OP_CALL_HANDLER:
2735 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2739 ins->inst_c0 = code - cfg->native_code;
2742 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2743 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2745 if (ins->flags & MONO_INST_BRLABEL) {
2746 /*if (ins->inst_i0->inst_c0) {
2748 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2750 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2754 /*if (ins->inst_target_bb->native_offset) {
2756 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2758 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2764 ppc_mtctr (code, ins->sreg1);
2765 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2768 ppc_li (code, ins->dreg, 0);
2769 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2770 ppc_li (code, ins->dreg, 1);
2774 ppc_li (code, ins->dreg, 1);
2775 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2776 ppc_li (code, ins->dreg, 0);
2780 ppc_li (code, ins->dreg, 1);
2781 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2782 ppc_li (code, ins->dreg, 0);
2784 case OP_COND_EXC_EQ:
2785 case OP_COND_EXC_NE_UN:
2786 case OP_COND_EXC_LT:
2787 case OP_COND_EXC_LT_UN:
2788 case OP_COND_EXC_GT:
2789 case OP_COND_EXC_GT_UN:
2790 case OP_COND_EXC_GE:
2791 case OP_COND_EXC_GE_UN:
2792 case OP_COND_EXC_LE:
2793 case OP_COND_EXC_LE_UN:
2794 case OP_COND_EXC_OV:
2795 case OP_COND_EXC_NO:
2797 case OP_COND_EXC_NC:
2798 //EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2799 // (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2811 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2814 /* floating point opcodes */
2816 ppc_load (code, ppc_r11, ins->inst_p0);
2817 ppc_lfd (code, ins->dreg, 0, ppc_r11);
2820 ppc_load (code, ppc_r11, ins->inst_p0);
2821 ppc_lfs (code, ins->dreg, 0, ppc_r11);
2823 case OP_STORER8_MEMBASE_REG:
2824 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2826 case OP_LOADR8_MEMBASE:
2827 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2829 case OP_STORER4_MEMBASE_REG:
2830 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2832 case OP_LOADR4_MEMBASE:
2833 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2835 case CEE_CONV_R_UN: {
2836 static const guint64 adjust_val = 0x4330000000000000UL;
2837 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2838 ppc_stw (code, ppc_r0, -8, ppc_sp);
2839 ppc_stw (code, ins->sreg1, -4, ppc_sp);
2840 ppc_load (code, ppc_r11, &adjust_val);
2841 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2842 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2845 case CEE_CONV_R4: /* FIXME: change precision */
2847 static const guint64 adjust_val = 0x4330000080000000UL;
2848 // addis is special for ppc_r0
2849 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2850 ppc_stw (code, ppc_r0, -8, ppc_sp);
2851 ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
2852 ppc_stw (code, ppc_r11, -4, ppc_sp);
2853 ppc_lfd (code, ins->dreg, -8, ppc_sp);
2854 ppc_load (code, ppc_r11, &adjust_val);
2855 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2856 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2859 case OP_X86_FP_LOAD_I8:
2860 g_assert_not_reached ();
2861 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
2863 case OP_X86_FP_LOAD_I4:
2864 g_assert_not_reached ();
2865 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
2867 case OP_FCONV_TO_I1:
2868 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
2870 case OP_FCONV_TO_U1:
2871 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
2873 case OP_FCONV_TO_I2:
2874 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
2876 case OP_FCONV_TO_U2:
2877 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
2879 case OP_FCONV_TO_I4:
2881 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
2883 case OP_FCONV_TO_U4:
2885 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
2887 case OP_FCONV_TO_I8:
2888 case OP_FCONV_TO_U8:
2889 g_assert_not_reached ();
2890 /* Implemented as helper calls */
2892 case OP_LCONV_TO_R_UN:
2893 g_assert_not_reached ();
2894 /* Implemented as helper calls */
2896 case OP_LCONV_TO_OVF_I: {
2897 ppc_mr (code, ins->dreg, ins->sreg1);
2898 /* FIXME: emit exception if needed */
2902 ppc_fsqrtd (code, ins->dreg, ins->sreg1);
2905 ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
2908 ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
2911 ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
2914 ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
2917 ppc_fneg (code, ins->dreg, ins->sreg1);
2921 g_assert_not_reached ();
2924 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2927 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2928 ppc_li (code, ins->dreg, 0);
2929 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2930 ppc_li (code, ins->dreg, 1);
2933 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2934 ppc_li (code, ins->dreg, 1);
2935 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2936 ppc_li (code, ins->dreg, 0);
2939 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2940 ppc_li (code, ins->dreg, 1);
2941 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2942 ppc_li (code, ins->dreg, 0);
2945 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2946 ppc_li (code, ins->dreg, 1);
2947 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2948 ppc_li (code, ins->dreg, 0);
2951 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2952 ppc_li (code, ins->dreg, 1);
2953 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2954 ppc_li (code, ins->dreg, 0);
2957 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
2960 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
2963 EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
2966 EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
2969 EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
2972 EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
2975 EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
2978 EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
2981 EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
2984 EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
2986 case CEE_CKFINITE: {
2987 ppc_stfd (code, ins->sreg1, -8, ppc_sp);
2988 ppc_lwz (code, ppc_r0, -8, ppc_sp);
2989 ppc_rlwinm (code, ppc_r0, ppc_r0, 0, 1, 31);
2990 ppc_xoris (code, ppc_r11, ppc_r0, 0x7ff0);
2991 ppc_neg (code, ppc_r0, ppc_r11);
2992 ppc_rlwinm (code, ppc_r0, ppc_r0, 1, 31, 31);
2993 g_assert_not_reached ();
2994 /*x86_push_reg (code, X86_EAX);
2997 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2998 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2999 x86_pop_reg (code, X86_EAX);
3000 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");*/
3004 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3005 g_assert_not_reached ();
3008 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
3009 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3010 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3011 g_assert_not_reached ();
3017 last_offset = offset;
3022 cfg->code_len = code - cfg->native_code;
3026 mono_arch_register_lowlevel_calls (void)
3028 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
3029 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
3032 #define patch_lis_ori(ip,val) do {\
3033 guint16 *__lis_ori = (guint16*)(ip); \
3034 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
3035 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
3039 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3041 MonoJumpInfo *patch_info;
3043 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3044 unsigned char *ip = patch_info->ip.i + code;
3045 const unsigned char *target = NULL;
3047 switch (patch_info->type) {
3048 case MONO_PATCH_INFO_BB:
3049 target = patch_info->data.bb->native_offset + code;
3051 case MONO_PATCH_INFO_ABS:
3052 target = patch_info->data.target;
3054 case MONO_PATCH_INFO_LABEL:
3055 target = patch_info->data.inst->inst_c0 + code;
3057 case MONO_PATCH_INFO_IP:
3058 patch_lis_ori (ip, ip);
3060 case MONO_PATCH_INFO_METHOD_REL:
3061 g_assert_not_reached ();
3062 *((gpointer *)(ip)) = code + patch_info->data.offset;
3064 case MONO_PATCH_INFO_INTERNAL_METHOD: {
3065 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
3067 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
3068 g_assert_not_reached ();
3070 target = mi->wrapper;
3073 case MONO_PATCH_INFO_METHOD_JUMP:
3074 g_assert_not_reached ();
3076 case MONO_PATCH_INFO_METHOD:
3077 if (patch_info->data.method == method) {
3080 /* get the trampoline to the method from the domain */
3081 target = mono_arch_create_jit_trampoline (patch_info->data.method);
3084 case MONO_PATCH_INFO_SWITCH: {
3085 gpointer *table = (gpointer *)patch_info->data.target;
3088 // FIXME: inspect code to get the register
3089 ppc_load (ip, ppc_r11, patch_info->data.target);
3090 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3092 for (i = 0; i < patch_info->table_size; i++) {
3093 table [i] = (int)patch_info->data.table [i] + code;
3095 /* we put into the table the absolute address, no need for ppc_patch in this case */
3098 case MONO_PATCH_INFO_METHODCONST:
3099 case MONO_PATCH_INFO_CLASS:
3100 case MONO_PATCH_INFO_IMAGE:
3101 case MONO_PATCH_INFO_FIELD:
3102 /* from OP_AOTCONST : lis + ori */
3103 patch_lis_ori (ip, patch_info->data.target);
3105 case MONO_PATCH_INFO_R4:
3106 case MONO_PATCH_INFO_R8:
3107 g_assert_not_reached ();
3108 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3110 case MONO_PATCH_INFO_IID:
3111 mono_class_init (patch_info->data.klass);
3112 patch_lis_ori (ip, patch_info->data.klass->interface_id);
3114 case MONO_PATCH_INFO_VTABLE:
3115 target = mono_class_vtable (domain, patch_info->data.klass);
3116 patch_lis_ori (ip, target);
3118 case MONO_PATCH_INFO_CLASS_INIT:
3119 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
3121 case MONO_PATCH_INFO_SFLDA: {
3122 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
3123 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
3124 /* Done by the generated code */
3128 mono_runtime_class_init (vtable);
3130 target = (char*)vtable->data + patch_info->data.field->offset;
3131 patch_lis_ori (ip, target);
3134 case MONO_PATCH_INFO_EXC_NAME:
3135 g_assert_not_reached ();
3136 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3138 case MONO_PATCH_INFO_LDSTR:
3139 target = mono_ldstr (domain, patch_info->data.token->image,
3140 mono_metadata_token_index (patch_info->data.token->token));
3141 patch_lis_ori (ip, target);
3143 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
3145 MonoClass *handle_class;
3147 handle = mono_ldtoken (patch_info->data.token->image,
3148 patch_info->data.token->token, &handle_class);
3149 mono_class_init (handle_class);
3150 mono_class_init (mono_class_from_mono_type (handle));
3152 patch_lis_ori (ip, handle);
3155 case MONO_PATCH_INFO_LDTOKEN: {
3157 MonoClass *handle_class;
3159 handle = mono_ldtoken (patch_info->data.token->image,
3160 patch_info->data.token->token, &handle_class);
3161 mono_class_init (handle_class);
3163 patch_lis_ori (ip, handle);
3166 case MONO_PATCH_INFO_BB_OVF:
3167 /* everything is dealt with at epilog output time */
3170 g_assert_not_reached ();
3172 ppc_patch (ip, target);
3177 mono_arch_max_epilog_size (MonoCompile *cfg)
3179 int exc_count = 0, max_epilog_size = 16 + 20*4;
3180 MonoJumpInfo *patch_info;
3182 if (cfg->method->save_lmf)
3183 max_epilog_size += 128;
3185 if (mono_jit_trace_calls != NULL)
3186 max_epilog_size += 50;
3188 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3189 max_epilog_size += 50;
3191 /* count the number of exception infos */
3194 * make sure we have enough space for exceptions
3195 * 16 is the size of two push_imm instructions and a call
3197 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3198 if (patch_info->type == MONO_PATCH_INFO_EXC)
3199 max_epilog_size += 16;
3200 else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
3201 max_epilog_size += 12;
3204 return max_epilog_size;
3208 mono_arch_emit_prolog (MonoCompile *cfg)
3210 MonoMethod *method = cfg->method;
3212 MonoMethodSignature *sig;
3214 int alloc_size, pos, max_offset, i;
3219 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3222 cfg->code_size = 256;
3223 code = cfg->native_code = g_malloc (cfg->code_size);
3225 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3226 ppc_mflr (code, ppc_r0);
3227 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3229 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
3230 cfg->used_int_regs |= 1 << 31;
3232 cfg->used_int_regs |= USE_EXTRA_TEMPS;
3234 alloc_size = cfg->stack_offset;
3236 /* reserve room to save return value */
3240 if (!method->save_lmf) {
3241 for (i = 13; i < 32; ++i) {
3242 if (cfg->used_int_regs & (1 << i)) {
3243 pos += sizeof (gulong);
3244 ppc_stw (code, i, -pos, ppc_sp);
3247 /*for (i = 14; i < 32; ++i) {
3248 if (cfg->used_float_regs & (1 << i)) {
3249 pos += sizeof (gdouble);
3250 ppc_stfd (code, i, -pos, ppc_sp);
3255 // align to PPC_STACK_ALIGNMENT bytes
3256 if (alloc_size & (PPC_STACK_ALIGNMENT - 1))
3257 alloc_size += PPC_STACK_ALIGNMENT - (alloc_size & (PPC_STACK_ALIGNMENT - 1));
3259 cfg->stack_usage = alloc_size;
3260 g_assert (ppc_is_imm16 (-alloc_size));
3262 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3263 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
3264 ppc_mr (code, ppc_r31, ppc_sp);
3266 /* compute max_offset in order to use short forward jumps
3267 * we always do it on ppc because the immediate displacement
3268 * for jumps is too small
3271 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3272 MonoInst *ins = bb->code;
3273 bb->max_offset = max_offset;
3275 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3279 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3284 /* load arguments allocated to register from the stack */
3285 sig = method->signature;
3288 cinfo = calculate_sizes (sig, sig->pinvoke);
3290 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3291 ArgInfo *ainfo = &cinfo->ret;
3293 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3295 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3296 ArgInfo *ainfo = cinfo->args + i;
3297 inst = cfg->varinfo [pos];
3299 if (inst->opcode == OP_REGVAR) {
3300 if (ainfo->regtype == RegTypeGeneral)
3301 ppc_mr (code, inst->dreg, ainfo->reg);
3302 else if (ainfo->regtype == RegTypeFP)
3303 ppc_fmr (code, inst->dreg, ainfo->reg);
3304 else if (ainfo->regtype == RegTypeBase) {
3305 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3306 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3308 g_assert_not_reached ();
3310 if (cfg->verbose_level > 2)
3311 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3313 /* the argument should be put on the stack: FIXME handle size != word */
3314 if (ainfo->regtype == RegTypeGeneral) {
3315 switch (ainfo->size) {
3317 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3320 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3323 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3324 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3327 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3329 } else if (ainfo->regtype == RegTypeBase) {
3330 /* load the previous stack pointer in r11 */
3331 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3332 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3333 switch (ainfo->size) {
3335 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3338 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3341 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3342 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3343 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3346 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3348 } else if (ainfo->regtype == RegTypeFP) {
3349 if (ainfo->size == 8)
3350 ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3351 else if (ainfo->size == 4)
3352 ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3354 g_assert_not_reached ();
3355 } else if (ainfo->regtype == RegTypeStructByVal) {
3356 int doffset = inst->inst_offset;
3359 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3360 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3361 soffset += sizeof (gpointer);
3362 doffset += sizeof (gpointer);
3364 if (ainfo->vtsize) {
3365 /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3366 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3367 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3368 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3370 } else if (ainfo->regtype == RegTypeStructByAddr) {
3371 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3372 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3374 g_assert_not_reached ();
3379 if (method->save_lmf) {
3381 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3382 (gpointer)"mono_get_lmf_addr");
3384 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3385 ppc_addi (code, ppc_r11, ppc_sp, PPC_MINIMAL_STACK_SIZE + cfg->param_area);
3386 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3387 /* new_lmf->previous_lmf = *lmf_addr */
3388 ppc_lwz (code, ppc_r0, 0, ppc_r3);
3389 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3390 /* *(lmf_addr) = r11 */
3391 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3392 /* save method info */
3393 ppc_load (code, ppc_r0, method);
3394 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3395 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3396 /* save the current IP */
3397 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3398 ppc_load (code, ppc_r0, 0x01010101);
3399 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3400 ppc_stmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3401 for (i = 14; i < 32; i++) {
3402 ppc_stfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3407 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
3409 cfg->code_len = code - cfg->native_code;
3416 mono_arch_emit_epilog (MonoCompile *cfg)
3418 MonoJumpInfo *patch_info;
3419 MonoMethod *method = cfg->method;
3423 code = cfg->native_code + cfg->code_len;
3425 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3426 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
3432 if (method->save_lmf) {
3433 ppc_addi (code, ppc_r11, cfg->frame_reg, PPC_MINIMAL_STACK_SIZE + cfg->param_area);
3434 /* r5 = previous_lmf */
3435 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3437 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3438 /* *(lmf_addr) = previous_lmf */
3439 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3441 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3443 for (i = 14; i < 32; i++) {
3444 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3448 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3449 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3450 ppc_mtlr (code, ppc_r0);
3452 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3453 if (!method->save_lmf) {
3454 for (i = 13; i < 32; ++i) {
3455 if (cfg->used_int_regs & (1 << i)) {
3457 ppc_lwz (code, i, -pos, cfg->frame_reg);
3463 /* add code to raise exceptions */
3464 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3465 switch (patch_info->type) {
3466 case MONO_PATCH_INFO_BB_OVF: {
3467 MonoOvfJump *ovfj = patch_info->data.target;
3468 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3469 /* patch the initial jump */
3470 ppc_patch (ip, code);
3471 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3473 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3474 /* jump back to the true target */
3476 ip = ovfj->bb->native_offset + cfg->native_code;
3477 ppc_patch (code - 4, ip);
3480 case MONO_PATCH_INFO_EXC:
3481 /*x86_patch (patch_info->ip.i + cfg->native_code, code);
3482 x86_push_imm (code, patch_info->data.target);
3483 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
3484 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3485 patch_info->data.name = "mono_arch_throw_exception_by_name";
3486 patch_info->ip.i = code - cfg->native_code;
3487 x86_jump_code (code, 0);*/
3495 cfg->code_len = code - cfg->native_code;
3497 g_assert (cfg->code_len < cfg->code_size);
3502 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3507 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3509 int this_dreg = ppc_r3;
3514 /* add the this argument */
3515 if (this_reg != -1) {
3517 MONO_INST_NEW (cfg, this, OP_SETREG);
3518 this->type = this_type;
3519 this->sreg1 = this_reg;
3520 this->dreg = this_dreg;
3521 mono_bblock_add_inst (cfg->cbb, this);
3526 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3527 vtarg->type = STACK_MP;
3528 vtarg->sreg1 = vt_reg;
3529 vtarg->dreg = ppc_r3;
3530 mono_bblock_add_inst (cfg->cbb, vtarg);
3535 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3537 /* optional instruction, need to detect it
3538 if (cmethod->klass == mono_defaults.math_class) {
3539 if (strcmp (cmethod->name, "Sqrt") == 0)