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 */
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);
459 mono_arch_get_global_int_regs (MonoCompile *cfg)
463 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
465 for (i = 13; i < top; ++i)
466 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
471 // code from ppc/tramp.c, try to keep in sync
472 #define MIN_CACHE_LINE 8
475 mono_arch_flush_icache (guint8 *code, gint size)
481 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
482 asm ("dcbst 0,%0;" : : "r"(p) : "memory");
486 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
487 asm ("icbi 0,%0; sync;" : : "r"(p) : "memory");
493 #define NOT_IMPLEMENTED(x) \
494 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
500 #define GENERAL_REGS 8
502 #define MINIMAL_STACK_SIZE 10
503 #define ALWAYS_ON_STACK(s) s
504 #define FP_ALSO_IN_REG(s) s
505 #define RET_ADDR_OFFSET 8
506 #define STACK_PARAM_OFFSET 24
508 #define MINIMAL_STACK_SIZE 5
509 #define ALWAYS_ON_STACK(s)
510 #define FP_ALSO_IN_REG(s) s
511 #define ALIGN_DOUBLES
512 #define RET_ADDR_OFFSET 4
513 #define STACK_PARAM_OFFSET 8
526 guint16 vtsize; /* in param area */
528 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
529 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
543 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
546 if (*gr >= 3 + GENERAL_REGS) {
547 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
548 ainfo->reg = ppc_sp; /* in the caller */
549 ainfo->regtype = RegTypeBase;
552 ALWAYS_ON_STACK (*stack_size += 4);
556 if (*gr >= 3 + GENERAL_REGS - 1) {
557 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
558 ainfo->reg = ppc_sp; /* in the caller */
559 ainfo->regtype = RegTypeBase;
562 *stack_size += (*stack_size % 8);
565 ALWAYS_ON_STACK (*stack_size += 8);
578 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
581 int n = sig->hasthis + sig->param_count;
583 guint32 stack_size = 0;
584 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
586 fr = PPC_FIRST_FPARG_REG;
587 gr = PPC_FIRST_ARG_REG;
589 /* FIXME: handle returning a struct */
590 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
591 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
592 cinfo->struct_ret = PPC_FIRST_ARG_REG;
597 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
600 DEBUG(printf("params: %d\n", sig->param_count));
601 for (i = 0; i < sig->param_count; ++i) {
602 DEBUG(printf("param %d: ", i));
603 if (sig->params [i]->byref) {
604 DEBUG(printf("byref\n"));
605 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
609 simpletype = sig->params [i]->type;
611 switch (simpletype) {
612 case MONO_TYPE_BOOLEAN:
615 cinfo->args [n].size = 1;
616 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
622 cinfo->args [n].size = 2;
623 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
628 cinfo->args [n].size = 4;
629 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
635 case MONO_TYPE_CLASS:
636 case MONO_TYPE_OBJECT:
637 case MONO_TYPE_STRING:
638 case MONO_TYPE_SZARRAY:
639 case MONO_TYPE_ARRAY:
640 cinfo->args [n].size = sizeof (gpointer);
641 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
644 case MONO_TYPE_VALUETYPE: {
646 if (sig->params [i]->data.klass->enumtype) {
647 simpletype = sig->params [i]->data.klass->enum_basetype->type;
650 size = mono_class_value_size (sig->params [i]->data.klass, NULL);
651 DEBUG(printf ("load %d bytes struct\n",
652 mono_class_value_size (sig->params [i]->data.klass, NULL)));
653 #if PPC_PASS_STRUCTS_BY_VALUE
655 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
656 cinfo->args [n].regtype = RegTypeStructByVal;
657 if (gr <= PPC_LAST_ARG_REG) {
658 int rest = PPC_LAST_ARG_REG - gr + 1;
659 int n_in_regs = rest >= nwords? nwords: rest;
660 cinfo->args [n].size = n_in_regs;
661 cinfo->args [n].vtsize = nwords - n_in_regs;
662 cinfo->args [n].reg = gr;
665 cinfo->args [n].size = 0;
666 cinfo->args [n].vtsize = nwords;
668 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
669 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
670 stack_size += nwords * sizeof (gpointer);
673 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
674 cinfo->args [n].regtype = RegTypeStructByAddr;
681 cinfo->args [n].size = 8;
682 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
686 cinfo->args [n].size = 4;
688 cinfo->args [n].regtype = RegTypeFP;
689 cinfo->args [n].reg = fr;
691 FP_ALSO_IN_REG (gr ++);
692 ALWAYS_ON_STACK (stack_size += 4);
694 NOT_IMPLEMENTED ("R4 arg");
699 cinfo->args [n].size = 8;
701 cinfo->args [n].regtype = RegTypeFP;
702 cinfo->args [n].reg = fr;
704 FP_ALSO_IN_REG (gr += 2);
705 ALWAYS_ON_STACK (stack_size += 8);
707 NOT_IMPLEMENTED ("R8 arg");
712 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
717 simpletype = sig->ret->type;
719 switch (simpletype) {
720 case MONO_TYPE_BOOLEAN:
731 case MONO_TYPE_FNPTR:
732 case MONO_TYPE_CLASS:
733 case MONO_TYPE_OBJECT:
734 case MONO_TYPE_SZARRAY:
735 case MONO_TYPE_ARRAY:
736 case MONO_TYPE_STRING:
737 cinfo->ret.reg = ppc_r3;
741 cinfo->ret.reg = ppc_r3;
745 cinfo->ret.reg = ppc_f1;
746 cinfo->ret.regtype = RegTypeFP;
748 case MONO_TYPE_VALUETYPE:
749 if (sig->ret->data.klass->enumtype) {
750 simpletype = sig->ret->data.klass->enum_basetype->type;
757 g_error ("Can't handle as return value 0x%x", sig->ret->type);
761 /* align stack size to 16 */
762 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
763 stack_size = (stack_size + 15) & ~15;
765 cinfo->stack_usage = stack_size;
771 * Set var information according to the calling convention. ppc version.
772 * The locals var stuff should most likely be split in another method.
775 mono_arch_allocate_vars (MonoCompile *m)
777 MonoMethodSignature *sig;
778 MonoMethodHeader *header;
780 int i, offset, size, align, curinst;
781 int frame_reg = ppc_sp;
783 if (m->flags & MONO_CFG_HAS_ALLOCA)
785 m->frame_reg = frame_reg;
787 header = ((MonoMethodNormal *)m->method)->header;
789 sig = m->method->signature;
793 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
794 m->ret->opcode = OP_REGVAR;
795 m->ret->inst_c0 = ppc_r3;
797 /* FIXME: handle long and FP values */
798 switch (sig->ret->type) {
802 m->ret->opcode = OP_REGVAR;
803 m->ret->inst_c0 = ppc_r3;
807 /* local vars are at a positive offset from the stack pointer */
809 * also note that if the function uses alloca, we use ppc_r31
810 * to point at the local variables.
812 offset = PPC_MINIMAL_STACK_SIZE; /* linkage area */
813 /* align the offset to 16 bytes: not sure this is needed here */
815 //offset &= ~(16 - 1);
817 /* add parameter area size for called functions */
818 offset += m->param_area;
822 /* FIXME: check how to handle this stuff... reserve space to save LMF and caller saved registers */
823 offset += sizeof (MonoLMF);
826 /* this stuff should not be needed on ppc and the new jit,
827 * because a call on ppc to the handlers doesn't change the
828 * stack pointer and the jist doesn't manipulate the stack pointer
829 * for operations involving valuetypes.
831 /* reserve space to store the esp */
832 offset += sizeof (gpointer);
834 /* this is a global constant */
835 mono_exc_esp_offset = offset;
838 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
840 offset += sizeof(gpointer) - 1;
841 offset &= ~(sizeof(gpointer) - 1);
842 inst->inst_offset = offset;
843 inst->opcode = OP_REGOFFSET;
844 inst->inst_basereg = frame_reg;
845 offset += sizeof(gpointer);
847 curinst = m->locals_start;
848 for (i = curinst; i < m->num_varinfo; ++i) {
849 inst = m->varinfo [i];
850 if (inst->opcode == OP_REGVAR)
853 /* inst->unused indicates native sized value types, this is used by the
854 * pinvoke wrappers when they call functions returning structure */
855 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
856 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
858 size = mono_type_size (inst->inst_vtype, &align);
861 offset &= ~(align - 1);
862 inst->inst_offset = offset;
863 inst->opcode = OP_REGOFFSET;
864 inst->inst_basereg = frame_reg;
866 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
871 inst = m->varinfo [curinst];
872 if (inst->opcode != OP_REGVAR) {
873 inst->opcode = OP_REGOFFSET;
874 inst->inst_basereg = frame_reg;
875 offset += sizeof (gpointer) - 1;
876 offset &= ~(sizeof (gpointer) - 1);
877 inst->inst_offset = offset;
878 offset += sizeof (gpointer);
883 for (i = 0; i < sig->param_count; ++i) {
884 inst = m->varinfo [curinst];
885 if (inst->opcode != OP_REGVAR) {
886 inst->opcode = OP_REGOFFSET;
887 inst->inst_basereg = frame_reg;
888 size = mono_type_size (sig->params [i], &align);
890 offset &= ~(align - 1);
891 inst->inst_offset = offset;
897 /* align the offset to 16 bytes */
902 m->stack_offset = offset;
906 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
907 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
911 * take the arguments and generate the arch-specific
912 * instructions to properly call the function in call.
913 * This includes pushing, moving arguments to the right register
915 * Issue: who does the spilling if needed, and when?
918 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
920 MonoMethodSignature *sig;
926 sig = call->signature;
927 n = sig->param_count + sig->hasthis;
929 cinfo = calculate_sizes (sig, sig->pinvoke);
930 if (cinfo->struct_ret)
931 call->used_iregs |= 1 << cinfo->struct_ret;
933 for (i = 0; i < n; ++i) {
934 ainfo = cinfo->args + i;
935 if (is_virtual && i == 0) {
936 /* the argument will be attached to the call instrucion */
938 call->used_iregs |= 1 << ainfo->reg;
940 MONO_INST_NEW (cfg, arg, OP_OUTARG);
942 arg->cil_code = in->cil_code;
944 arg->type = in->type;
945 /* prepend, we'll need to reverse them later */
946 arg->next = call->out_args;
947 call->out_args = arg;
948 if (ainfo->regtype == RegTypeGeneral) {
949 arg->unused = ainfo->reg;
950 call->used_iregs |= 1 << ainfo->reg;
951 if (arg->type == STACK_I8)
952 call->used_iregs |= 1 << (ainfo->reg + 1);
953 } else if (ainfo->regtype == RegTypeStructByAddr) {
954 /* FIXME: where si the data allocated? */
955 arg->unused = ainfo->reg;
956 call->used_iregs |= 1 << ainfo->reg;
957 } else if (ainfo->regtype == RegTypeStructByVal) {
959 /* mark the used regs */
960 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
961 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
963 arg->opcode = OP_OUTARG_VT;
964 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
965 arg->inst_imm = ainfo->offset;
966 } else if (ainfo->regtype == RegTypeBase) {
967 arg->opcode = OP_OUTARG_VT;
968 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
969 arg->inst_imm = ainfo->offset;
970 } else if (ainfo->regtype == RegTypeFP) {
971 arg->opcode = OP_OUTARG_R8;
972 arg->unused = ainfo->reg;
973 call->used_fregs |= 1 << ainfo->reg;
974 if (ainfo->size == 4) {
975 /* we reduce the precision */
977 MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
978 conv->inst_left = arg->inst_left;
979 arg->inst_left = conv;
982 g_assert_not_reached ();
987 * Reverse the call->out_args list.
990 MonoInst *prev = NULL, *list = call->out_args, *next;
997 call->out_args = prev;
999 call->stack_usage = cinfo->stack_usage;
1000 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
1001 cfg->flags |= MONO_CFG_HAS_CALLS;
1003 * should set more info in call, such as the stack space
1004 * used by the args that needs to be added back to esp
1012 * Allow tracing to work with this interface (with an optional argument)
1016 * This may be needed on some archs or for debugging support.
1019 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
1021 /* no stack room needed now (may be needed for FASTCALL-trace support) */
1023 /* split prolog-epilog requirements? */
1024 *code = 50; /* max bytes needed: check this number */
1028 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1032 ppc_load (code, ppc_r3, cfg->method);
1033 ppc_li (code, ppc_r4, 0); /* NULL ebp for now */
1034 ppc_load (code, ppc_r0, func);
1035 ppc_mtlr (code, ppc_r0);
1049 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1052 int save_mode = SAVE_NONE;
1053 MonoMethod *method = cfg->method;
1054 int rtype = method->signature->ret->type;
1058 case MONO_TYPE_VOID:
1059 /* special case string .ctor icall */
1060 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1061 save_mode = SAVE_ONE;
1063 save_mode = SAVE_NONE;
1067 save_mode = SAVE_TWO;
1071 save_mode = SAVE_FP;
1073 case MONO_TYPE_VALUETYPE:
1074 if (method->signature->ret->data.klass->enumtype) {
1075 rtype = method->signature->ret->data.klass->enum_basetype->type;
1078 save_mode = SAVE_STRUCT;
1081 save_mode = SAVE_ONE;
1085 switch (save_mode) {
1087 ppc_stw (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
1088 ppc_stw (code, ppc_r4, cfg->stack_usage - 4, cfg->frame_reg);
1089 if (enable_arguments) {
1090 ppc_mr (code, ppc_r5, ppc_r4);
1091 ppc_mr (code, ppc_r4, ppc_r3);
1095 ppc_stw (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
1096 if (enable_arguments) {
1097 ppc_mr (code, ppc_r4, ppc_r3);
1101 ppc_stfd (code, ppc_f1, cfg->stack_usage - 8, cfg->frame_reg);
1102 if (enable_arguments) {
1103 /* FIXME: what reg? */
1104 ppc_fmr (code, ppc_f3, ppc_f1);
1105 ppc_lwz (code, ppc_r4, cfg->stack_usage - 8, cfg->frame_reg);
1106 ppc_lwz (code, ppc_r5, cfg->stack_usage - 4, cfg->frame_reg);
1110 if (enable_arguments) {
1111 /* FIXME: get the actual address */
1112 ppc_mr (code, ppc_r4, ppc_r3);
1120 ppc_load (code, ppc_r3, cfg->method);
1121 ppc_load (code, ppc_r0, func);
1122 ppc_mtlr (code, ppc_r0);
1125 switch (save_mode) {
1127 ppc_lwz (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
1128 ppc_lwz (code, ppc_r4, cfg->stack_usage - 4, cfg->frame_reg);
1131 ppc_lwz (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
1134 ppc_lfd (code, ppc_f1, cfg->stack_usage - 8, cfg->frame_reg);
1144 #define EMIT_COND_BRANCH(ins,cond) \
1145 if (ins->flags & MONO_INST_BRLABEL) { \
1146 if (0 && ins->inst_i0->inst_c0) { \
1147 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff); \
1149 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1150 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0); \
1153 if (0 && ins->inst_true_bb->native_offset) { \
1154 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
1156 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1157 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0); \
1161 /* emit an exception if condition is fail */
1162 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name) \
1164 mono_add_patch_info (cfg, code - cfg->native_code, \
1165 MONO_PATCH_INFO_EXC, exc_name); \
1166 x86_branch32 (code, cond, 0, signed); \
1170 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1172 MonoInst *ins, *last_ins = NULL;
1177 switch (ins->opcode) {
1179 /* remove unnecessary multiplication with 1 */
1180 if (ins->inst_imm == 1) {
1181 if (ins->dreg != ins->sreg1) {
1182 ins->opcode = OP_MOVE;
1184 last_ins->next = ins->next;
1190 case OP_LOAD_MEMBASE:
1191 case OP_LOADI4_MEMBASE:
1193 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1194 * OP_LOAD_MEMBASE offset(basereg), reg
1196 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1197 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1198 ins->inst_basereg == last_ins->inst_destbasereg &&
1199 ins->inst_offset == last_ins->inst_offset) {
1200 if (ins->dreg == last_ins->sreg1) {
1201 last_ins->next = ins->next;
1205 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1206 ins->opcode = OP_MOVE;
1207 ins->sreg1 = last_ins->sreg1;
1211 * Note: reg1 must be different from the basereg in the second load
1212 * OP_LOAD_MEMBASE offset(basereg), reg1
1213 * OP_LOAD_MEMBASE offset(basereg), reg2
1215 * OP_LOAD_MEMBASE offset(basereg), reg1
1216 * OP_MOVE reg1, reg2
1218 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1219 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1220 ins->inst_basereg != last_ins->dreg &&
1221 ins->inst_basereg == last_ins->inst_basereg &&
1222 ins->inst_offset == last_ins->inst_offset) {
1224 if (ins->dreg == last_ins->dreg) {
1225 last_ins->next = ins->next;
1229 ins->opcode = OP_MOVE;
1230 ins->sreg1 = last_ins->dreg;
1233 //g_assert_not_reached ();
1237 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1238 * OP_LOAD_MEMBASE offset(basereg), reg
1240 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1241 * OP_ICONST reg, imm
1243 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1244 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1245 ins->inst_basereg == last_ins->inst_destbasereg &&
1246 ins->inst_offset == last_ins->inst_offset) {
1247 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1248 ins->opcode = OP_ICONST;
1249 ins->inst_c0 = last_ins->inst_imm;
1250 g_assert_not_reached (); // check this rule
1254 case OP_LOADU1_MEMBASE:
1255 case OP_LOADI1_MEMBASE:
1256 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1257 ins->inst_basereg == last_ins->inst_destbasereg &&
1258 ins->inst_offset == last_ins->inst_offset) {
1259 if (ins->dreg == last_ins->sreg1) {
1260 last_ins->next = ins->next;
1264 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1265 ins->opcode = OP_MOVE;
1266 ins->sreg1 = last_ins->sreg1;
1270 case OP_LOADU2_MEMBASE:
1271 case OP_LOADI2_MEMBASE:
1272 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1273 ins->inst_basereg == last_ins->inst_destbasereg &&
1274 ins->inst_offset == last_ins->inst_offset) {
1275 if (ins->dreg == last_ins->sreg1) {
1276 last_ins->next = ins->next;
1280 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1281 ins->opcode = OP_MOVE;
1282 ins->sreg1 = last_ins->sreg1;
1292 if (ins->dreg == ins->sreg1) {
1294 last_ins->next = ins->next;
1299 * OP_MOVE sreg, dreg
1300 * OP_MOVE dreg, sreg
1302 if (last_ins && last_ins->opcode == OP_MOVE &&
1303 ins->sreg1 == last_ins->dreg &&
1304 ins->dreg == last_ins->sreg1) {
1305 last_ins->next = ins->next;
1314 bb->last_ins = last_ins;
1318 * the branch_b0_table should maintain the order of these
1332 branch_b0_table [] = {
1347 branch_b1_table [] = {
1362 * returns the offset used by spillvar. It allocates a new
1363 * spill variable if necessary.
1366 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1368 MonoSpillInfo **si, *info;
1371 si = &cfg->spill_info;
1373 while (i <= spillvar) {
1376 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1378 info->offset = cfg->stack_offset;
1379 cfg->stack_offset += sizeof (gpointer);
1383 return (*si)->offset;
1389 g_assert_not_reached ();
1394 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1396 MonoSpillInfo **si, *info;
1399 si = &cfg->spill_info_float;
1401 while (i <= spillvar) {
1404 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1406 cfg->stack_offset += 7;
1407 cfg->stack_offset &= ~7;
1408 info->offset = cfg->stack_offset;
1409 cfg->stack_offset += sizeof (double);
1413 return (*si)->offset;
1419 g_assert_not_reached ();
1424 #define DEBUG(a) if (cfg->verbose_level > 1) a
1426 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1427 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 14)
1436 static const char*const * ins_spec = ppcg4;
1439 print_ins (int i, MonoInst *ins)
1441 const char *spec = ins_spec [ins->opcode];
1442 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1443 if (spec [MONO_INST_DEST]) {
1444 if (ins->dreg >= MONO_MAX_IREGS)
1445 g_print (" R%d <-", ins->dreg);
1447 g_print (" %s <-", mono_arch_regname (ins->dreg));
1449 if (spec [MONO_INST_SRC1]) {
1450 if (ins->sreg1 >= MONO_MAX_IREGS)
1451 g_print (" R%d", ins->sreg1);
1453 g_print (" %s", mono_arch_regname (ins->sreg1));
1455 if (spec [MONO_INST_SRC2]) {
1456 if (ins->sreg2 >= MONO_MAX_IREGS)
1457 g_print (" R%d", ins->sreg2);
1459 g_print (" %s", mono_arch_regname (ins->sreg2));
1461 if (spec [MONO_INST_CLOB])
1462 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1467 print_regtrack (RegTrack *t, int num)
1473 for (i = 0; i < num; ++i) {
1476 if (i >= MONO_MAX_IREGS) {
1477 g_snprintf (buf, sizeof(buf), "R%d", i);
1480 r = mono_arch_regname (i);
1481 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1485 typedef struct InstList InstList;
1493 static inline InstList*
1494 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1496 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1506 * Force the spilling of the variable in the symbolic register 'reg'.
1509 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1514 sel = cfg->rs->iassign [reg];
1515 /*i = cfg->rs->isymbolic [sel];
1516 g_assert (i == reg);*/
1518 spill = ++cfg->spill_count;
1519 cfg->rs->iassign [i] = -spill - 1;
1520 mono_regstate_free_int (cfg->rs, sel);
1521 /* we need to create a spill var and insert a load to sel after the current instruction */
1522 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1524 load->inst_basereg = cfg->frame_reg;
1525 load->inst_offset = mono_spillvar_offset (cfg, spill);
1527 while (ins->next != item->prev->data)
1530 load->next = ins->next;
1532 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1533 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1534 g_assert (i == sel);
1540 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1545 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));
1546 /* exclude the registers in the current instruction */
1547 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1548 if (ins->sreg1 >= MONO_MAX_IREGS)
1549 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1551 regmask &= ~ (1 << ins->sreg1);
1552 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1554 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1555 if (ins->sreg2 >= MONO_MAX_IREGS)
1556 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1558 regmask &= ~ (1 << ins->sreg2);
1559 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1561 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1562 regmask &= ~ (1 << ins->dreg);
1563 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1566 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1567 g_assert (regmask); /* need at least a register we can free */
1569 /* we should track prev_use and spill the register that's farther */
1570 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1571 if (regmask & (1 << i)) {
1573 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1577 i = cfg->rs->isymbolic [sel];
1578 spill = ++cfg->spill_count;
1579 cfg->rs->iassign [i] = -spill - 1;
1580 mono_regstate_free_int (cfg->rs, sel);
1581 /* we need to create a spill var and insert a load to sel after the current instruction */
1582 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1584 load->inst_basereg = cfg->frame_reg;
1585 load->inst_offset = mono_spillvar_offset (cfg, spill);
1587 while (ins->next != item->prev->data)
1590 load->next = ins->next;
1592 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1593 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1594 g_assert (i == sel);
1600 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1605 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));
1606 /* exclude the registers in the current instruction */
1607 if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1608 if (ins->sreg1 >= MONO_MAX_FREGS)
1609 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1611 regmask &= ~ (1 << ins->sreg1);
1612 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1614 if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1615 if (ins->sreg2 >= MONO_MAX_FREGS)
1616 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1618 regmask &= ~ (1 << ins->sreg2);
1619 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1621 if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1622 regmask &= ~ (1 << ins->dreg);
1623 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1626 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1627 g_assert (regmask); /* need at least a register we can free */
1629 /* we should track prev_use and spill the register that's farther */
1630 for (i = 0; i < MONO_MAX_FREGS; ++i) {
1631 if (regmask & (1 << i)) {
1633 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1637 i = cfg->rs->fsymbolic [sel];
1638 spill = ++cfg->spill_count;
1639 cfg->rs->fassign [i] = -spill - 1;
1640 mono_regstate_free_float(cfg->rs, sel);
1641 /* we need to create a spill var and insert a load to sel after the current instruction */
1642 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1644 load->inst_basereg = cfg->frame_reg;
1645 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1647 while (ins->next != item->prev->data)
1650 load->next = ins->next;
1652 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1653 i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1654 g_assert (i == sel);
1660 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1663 MONO_INST_NEW (cfg, copy, OP_MOVE);
1667 copy->next = ins->next;
1670 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1675 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1678 MONO_INST_NEW (cfg, copy, OP_FMOVE);
1682 copy->next = ins->next;
1685 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1690 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1693 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1695 store->inst_destbasereg = cfg->frame_reg;
1696 store->inst_offset = mono_spillvar_offset (cfg, spill);
1698 store->next = ins->next;
1701 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)));
1706 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1709 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1711 store->inst_destbasereg = cfg->frame_reg;
1712 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1714 store->next = ins->next;
1717 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)));
1722 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1725 g_assert (item->next);
1726 prev = item->next->data;
1728 while (prev->next != ins)
1730 to_insert->next = ins;
1731 prev->next = to_insert;
1733 * needed otherwise in the next instruction we can add an ins to the
1734 * end and that would get past this instruction.
1736 item->data = to_insert;
1740 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1742 int val = cfg->rs->iassign [sym_reg];
1746 /* the register gets spilled after this inst */
1749 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1751 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1752 cfg->rs->iassign [sym_reg] = val;
1753 /* add option to store before the instruction for src registers */
1755 create_spilled_store (cfg, spill, val, sym_reg, ins);
1757 cfg->rs->isymbolic [val] = sym_reg;
1761 /* use ppc_r3-ppc_10 as temp registers */
1762 #define PPC_CALLER_REGS (0xff<<3)
1763 #define PPC_CALLER_FREGS (0xff<<2)
1766 * Local register allocation.
1767 * We first scan the list of instructions and we save the liveness info of
1768 * each register (when the register is first used, when it's value is set etc.).
1769 * We also reverse the list of instructions (in the InstList list) because assigning
1770 * registers backwards allows for more tricks to be used.
1773 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1776 MonoRegState *rs = cfg->rs;
1778 RegTrack *reginfo, *reginfof;
1779 RegTrack *reginfo1, *reginfo2, *reginfod;
1780 InstList *tmp, *reversed = NULL;
1782 guint32 src1_mask, src2_mask, dest_mask;
1783 guint32 cur_iregs, cur_fregs;
1787 rs->next_vireg = bb->max_ireg;
1788 rs->next_vfreg = bb->max_freg;
1789 mono_regstate_assign (rs);
1790 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1791 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1792 rs->ifree_mask = PPC_CALLER_REGS;
1793 rs->ffree_mask = PPC_CALLER_FREGS;
1797 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1798 /* forward pass on the instructions to collect register liveness info */
1800 spec = ins_spec [ins->opcode];
1801 DEBUG (print_ins (i, ins));
1802 if (spec [MONO_INST_CLOB] == 'c') {
1803 MonoCallInst * call = (MonoCallInst*)ins;
1806 if (spec [MONO_INST_SRC1]) {
1807 if (spec [MONO_INST_SRC1] == 'f')
1808 reginfo1 = reginfof;
1811 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1812 reginfo1 [ins->sreg1].last_use = i;
1816 if (spec [MONO_INST_SRC2]) {
1817 if (spec [MONO_INST_SRC2] == 'f')
1818 reginfo2 = reginfof;
1821 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1822 reginfo2 [ins->sreg2].last_use = i;
1826 if (spec [MONO_INST_DEST]) {
1827 if (spec [MONO_INST_DEST] == 'f')
1828 reginfod = reginfof;
1831 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1832 reginfod [ins->dreg].killed_in = i;
1833 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1834 reginfod [ins->dreg].last_use = i;
1835 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1836 reginfod [ins->dreg].born_in = i;
1837 if (spec [MONO_INST_DEST] == 'l') {
1838 /* result in eax:edx, the virtual register is allocated sequentially */
1839 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1840 reginfod [ins->dreg + 1].last_use = i;
1841 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1842 reginfod [ins->dreg + 1].born_in = i;
1847 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1852 cur_iregs = PPC_CALLER_REGS;
1853 cur_fregs = PPC_CALLER_FREGS;
1855 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1856 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1859 int prev_dreg, prev_sreg1, prev_sreg2;
1862 spec = ins_spec [ins->opcode];
1863 DEBUG (g_print ("processing:"));
1864 DEBUG (print_ins (i, ins));
1865 /* make the register available for allocation: FIXME add fp reg */
1866 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1867 cur_iregs |= 1 << ins->dreg;
1868 DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1869 } else if (ins->opcode == OP_SETFREG) {
1870 cur_fregs |= 1 << ins->dreg;
1871 DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1872 } else if (spec [MONO_INST_CLOB] == 'c') {
1873 MonoCallInst *cinst = (MonoCallInst*)ins;
1874 DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1875 cur_iregs &= ~cinst->used_iregs;
1876 cur_fregs &= ~cinst->used_fregs;
1877 DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1878 /* registers used by the calling convention are excluded from
1879 * allocation: they will be selectively enabled when they are
1880 * assigned by the special SETREG opcodes.
1883 dest_mask = src1_mask = src2_mask = cur_iregs;
1884 /* update for use with FP regs... */
1885 if (spec [MONO_INST_DEST] == 'f') {
1886 if (ins->dreg >= MONO_MAX_FREGS) {
1887 val = rs->fassign [ins->dreg];
1888 prev_dreg = ins->dreg;
1892 /* the register gets spilled after this inst */
1895 val = mono_regstate_alloc_float (rs, dest_mask);
1897 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1898 rs->fassign [ins->dreg] = val;
1900 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1902 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1903 rs->fsymbolic [val] = prev_dreg;
1905 if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != ppc_f1) {
1906 /* this instruction only outputs to ppc_f3, need to copy */
1907 create_copy_ins_float (cfg, ins->dreg, ppc_f1, ins);
1912 if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1913 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1914 mono_regstate_free_float (rs, ins->dreg);
1916 } else if (ins->dreg >= MONO_MAX_IREGS) {
1917 val = rs->iassign [ins->dreg];
1918 prev_dreg = ins->dreg;
1922 /* the register gets spilled after this inst */
1925 val = mono_regstate_alloc_int (rs, dest_mask);
1927 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1928 rs->iassign [ins->dreg] = val;
1930 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1932 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1933 rs->isymbolic [val] = prev_dreg;
1935 if (spec [MONO_INST_DEST] == 'l') {
1936 int hreg = prev_dreg + 1;
1937 val = rs->iassign [hreg];
1941 /* the register gets spilled after this inst */
1944 val = mono_regstate_alloc_int (rs, dest_mask);
1946 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1947 rs->iassign [hreg] = val;
1949 create_spilled_store (cfg, spill, val, hreg, ins);
1951 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1952 rs->isymbolic [val] = hreg;
1953 /* FIXME:? ins->dreg = val; */
1954 if (ins->dreg == ppc_r4) {
1956 create_copy_ins (cfg, val, ppc_r3, ins);
1957 } else if (ins->dreg == ppc_r3) {
1958 if (val == ppc_r4) {
1960 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1961 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1962 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1964 /* two forced copies */
1965 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1966 create_copy_ins (cfg, val, ppc_r3, ins);
1969 if (val == ppc_r3) {
1970 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1972 /* two forced copies */
1973 create_copy_ins (cfg, val, ppc_r3, ins);
1974 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1977 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
1978 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1979 mono_regstate_free_int (rs, val);
1981 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != ppc_r3 && spec [MONO_INST_CLOB] != 'd') {
1982 /* this instruction only outputs to ppc_r3, need to copy */
1983 create_copy_ins (cfg, ins->dreg, ppc_r3, ins);
1988 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
1989 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1990 mono_regstate_free_int (rs, ins->dreg);
1992 if (spec [MONO_INST_SRC1] == 'f') {
1993 if (ins->sreg1 >= MONO_MAX_FREGS) {
1994 val = rs->fassign [ins->sreg1];
1995 prev_sreg1 = ins->sreg1;
1999 /* the register gets spilled after this inst */
2002 //g_assert (val == -1); /* source cannot be spilled */
2003 val = mono_regstate_alloc_float (rs, src1_mask);
2005 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
2006 rs->fassign [ins->sreg1] = val;
2007 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2009 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
2010 insert_before_ins (ins, tmp, store);
2013 rs->fsymbolic [val] = prev_sreg1;
2018 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
2019 val = rs->iassign [ins->sreg1];
2020 prev_sreg1 = ins->sreg1;
2024 /* the register gets spilled after this inst */
2027 if (0 && ins->opcode == OP_MOVE) {
2029 * small optimization: the dest register is already allocated
2030 * but the src one is not: we can simply assign the same register
2031 * here and peephole will get rid of the instruction later.
2032 * This optimization may interfere with the clobbering handling:
2033 * it removes a mov operation that will be added again to handle clobbering.
2034 * There are also some other issues that should with make testjit.
2036 mono_regstate_alloc_int (rs, 1 << ins->dreg);
2037 val = rs->iassign [ins->sreg1] = ins->dreg;
2038 //g_assert (val >= 0);
2039 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2041 //g_assert (val == -1); /* source cannot be spilled */
2042 val = mono_regstate_alloc_int (rs, src1_mask);
2044 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
2045 rs->iassign [ins->sreg1] = val;
2046 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2049 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
2050 insert_before_ins (ins, tmp, store);
2053 rs->isymbolic [val] = prev_sreg1;
2058 if (spec [MONO_INST_SRC2] == 'f') {
2059 if (ins->sreg2 >= MONO_MAX_FREGS) {
2060 val = rs->fassign [ins->sreg2];
2061 prev_sreg2 = ins->sreg2;
2065 /* the register gets spilled after this inst */
2068 val = mono_regstate_alloc_float (rs, src2_mask);
2070 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
2071 rs->fassign [ins->sreg2] = val;
2072 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
2074 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
2076 rs->fsymbolic [val] = prev_sreg2;
2081 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
2082 val = rs->iassign [ins->sreg2];
2083 prev_sreg2 = ins->sreg2;
2087 /* the register gets spilled after this inst */
2090 val = mono_regstate_alloc_int (rs, src2_mask);
2092 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
2093 rs->iassign [ins->sreg2] = val;
2094 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
2096 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
2098 rs->isymbolic [val] = prev_sreg2;
2104 if (spec [MONO_INST_CLOB] == 'c') {
2106 guint32 clob_mask = PPC_CALLER_REGS;
2107 for (j = 0; j < MONO_MAX_IREGS; ++j) {
2109 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
2110 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
2114 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2115 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2116 mono_regstate_free_int (rs, ins->sreg1);
2118 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2119 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2120 mono_regstate_free_int (rs, ins->sreg2);
2123 //DEBUG (print_ins (i, ins));
2129 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2131 /* sreg is a float, dreg is an integer reg. ppc_f1 is used a scratch */
2132 ppc_fctiwz (code, ppc_f1, sreg);
2133 ppc_stfd (code, ppc_f1, -8, ppc_sp);
2134 ppc_lwz (code, dreg, -4, ppc_sp);
2137 ppc_andid (code, dreg, dreg, 0xff);
2139 ppc_andid (code, dreg, dreg, 0xffff);
2142 ppc_extsb (code, dreg, dreg);
2144 ppc_extsh (code, dreg, dreg);
2149 static unsigned char*
2150 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2153 int sreg = tree->sreg1;
2154 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2155 if (tree->flags & MONO_INST_INIT) {
2157 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2158 x86_push_reg (code, X86_EAX);
2161 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2162 x86_push_reg (code, X86_ECX);
2165 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2166 x86_push_reg (code, X86_EDI);
2170 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2171 if (sreg != X86_ECX)
2172 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2173 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2175 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2177 x86_prefix (code, X86_REP_PREFIX);
2180 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2181 x86_pop_reg (code, X86_EDI);
2182 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2183 x86_pop_reg (code, X86_ECX);
2184 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2185 x86_pop_reg (code, X86_EAX);
2192 ppc_patch (guchar *code, guchar *target)
2194 guint32 ins = *(guint32*)code;
2195 guint32 prim = ins >> 26;
2197 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2201 guint32 li = (guint32)target;
2202 ins = prim << 26 | (ins & 3);
2204 // FIXME: assert the top bits of li are 0
2206 gint diff = target - code;
2207 ins = prim << 26 | (ins & 3);
2209 diff &= ~(63 << 26);
2212 *(guint32*)code = ins;
2213 } else if (prim == 16) {
2216 guint32 li = (guint32)target;
2217 ins = (ins & 0xffff0000) | (ins & 3);
2220 // FIXME: assert the top bits of li are 0
2222 gint diff = target - code;
2223 ins = (ins & 0xffff0000) | (ins & 3);
2227 *(guint32*)code = ins;
2229 g_assert_not_reached ();
2231 // g_print ("patched with 0x%08x\n", ins);
2235 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2240 guint8 *code = cfg->native_code + cfg->code_len;
2241 MonoInst *last_ins = NULL;
2242 guint last_offset = 0;
2245 if (cfg->opt & MONO_OPT_PEEPHOLE)
2246 peephole_pass (cfg, bb);
2248 /* we don't align basic blocks of loops on ppc */
2250 if (cfg->verbose_level > 2)
2251 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2253 cpos = bb->max_offset;
2255 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2256 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2257 //g_assert (!mono_compile_aot);
2260 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2261 /* this is not thread save, but good enough */
2262 /* fixme: howto handle overflows? */
2263 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2268 offset = code - cfg->native_code;
2270 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2272 if (offset > (cfg->code_size - max_len - 16)) {
2273 cfg->code_size *= 2;
2274 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2275 code = cfg->native_code + offset;
2277 // if (ins->cil_code)
2278 // g_print ("cil code\n");
2280 switch (ins->opcode) {
2281 case OP_STOREI1_MEMBASE_IMM:
2282 ppc_li (code, ppc_r11, ins->inst_imm);
2283 g_assert (ppc_is_imm16 (ins->inst_offset));
2284 ppc_stb (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2286 case OP_STOREI2_MEMBASE_IMM:
2287 ppc_li (code, ppc_r11, ins->inst_imm);
2288 g_assert (ppc_is_imm16 (ins->inst_offset));
2289 ppc_sth (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2291 case OP_STORE_MEMBASE_IMM:
2292 case OP_STOREI4_MEMBASE_IMM:
2293 ppc_load (code, ppc_r11, ins->inst_imm);
2294 g_assert (ppc_is_imm16 (ins->inst_offset));
2295 ppc_stw (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2297 case OP_STOREI1_MEMBASE_REG:
2298 g_assert (ppc_is_imm16 (ins->inst_offset));
2299 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2301 case OP_STOREI2_MEMBASE_REG:
2302 g_assert (ppc_is_imm16 (ins->inst_offset));
2303 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2305 case OP_STORE_MEMBASE_REG:
2306 case OP_STOREI4_MEMBASE_REG:
2307 g_assert (ppc_is_imm16 (ins->inst_offset));
2308 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2313 g_assert_not_reached ();
2314 //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2317 g_assert_not_reached ();
2318 //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2319 //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2321 case OP_LOAD_MEMBASE:
2322 case OP_LOADI4_MEMBASE:
2323 case OP_LOADU4_MEMBASE:
2324 if (ppc_is_imm16 (ins->inst_offset)) {
2325 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2327 ppc_load (code, ppc_r11, ins->inst_offset);
2328 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2331 case OP_LOADU1_MEMBASE:
2332 g_assert (ppc_is_imm16 (ins->inst_offset));
2333 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2335 case OP_LOADI1_MEMBASE:
2336 g_assert (ppc_is_imm16 (ins->inst_offset));
2337 // FIXME: sign extend
2338 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2340 case OP_LOADU2_MEMBASE:
2341 g_assert (ppc_is_imm16 (ins->inst_offset));
2342 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2344 case OP_LOADI2_MEMBASE:
2345 g_assert (ppc_is_imm16 (ins->inst_offset));
2346 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2349 ppc_extsb (code, ins->dreg, ins->sreg1);
2352 ppc_extsh (code, ins->dreg, ins->sreg1);
2355 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
2358 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
2361 if (ins->next && (ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN))
2362 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2364 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2366 case OP_COMPARE_IMM:
2367 if (ins->next && ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) {
2368 if (ppc_is_uimm16 (ins->inst_imm)) {
2369 ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2371 ppc_load (code, ppc_r11, ins->inst_imm);
2372 ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2375 if (ppc_is_imm16 (ins->inst_imm)) {
2376 ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2378 ppc_load (code, ppc_r11, ins->inst_imm);
2379 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2383 case OP_X86_TEST_NULL:
2384 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2390 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2393 ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2396 ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2399 if (ppc_is_imm16 (ins->inst_imm)) {
2400 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2402 ppc_load (code, ppc_r11, ins->inst_imm);
2403 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2407 ppc_load (code, ppc_r11, ins->inst_imm);
2408 ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2411 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2414 ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2417 ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2420 // we add the negated value
2421 if (ppc_is_imm16 (-ins->inst_imm))
2422 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2424 ppc_load (code, ppc_r11, ins->inst_imm);
2425 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2429 ppc_load (code, ppc_r11, ins->inst_imm);
2430 ppc_subfe (code, ins->dreg, ins->sreg2, ppc_r11);
2433 g_assert (ppc_is_imm16 (ins->inst_imm));
2434 ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2437 ppc_subfze (code, ins->dreg, ins->sreg1);
2440 /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2441 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2444 if (!(ins->inst_imm & 0xffff0000)) {
2445 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2446 } else if (!(ins->inst_imm & 0xffff)) {
2447 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2449 ppc_load (code, ppc_r11, ins->inst_imm);
2450 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2454 ppc_divw (code, ins->dreg, ins->sreg1, ins->sreg2);
2457 ppc_divwu (code, ins->dreg, ins->sreg1, ins->sreg2);
2460 ppc_load (code, ppc_r11, ins->inst_imm);
2461 ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2464 ppc_divw (code, ppc_r11, ins->sreg1, ins->sreg2);
2465 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2466 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2469 ppc_divwu (code, ppc_r11, ins->sreg1, ins->sreg2);
2470 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2471 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2474 ppc_load (code, ppc_r11, ins->inst_imm);
2475 ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2476 ppc_mullw (code, ins->dreg, ins->dreg, ppc_r11);
2477 ppc_subf (code, ins->dreg, ins->dreg, ins->sreg1);
2480 ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2483 if (!(ins->inst_imm & 0xffff0000)) {
2484 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2485 } else if (!(ins->inst_imm & 0xffff)) {
2486 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2488 ppc_load (code, ppc_r11, ins->inst_imm);
2489 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2493 ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2496 if (!(ins->inst_imm & 0xffff0000)) {
2497 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2498 } else if (!(ins->inst_imm & 0xffff)) {
2499 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2501 ppc_load (code, ppc_r11, ins->inst_imm);
2502 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2506 ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2509 ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2510 //ppc_load (code, ppc_r11, ins->inst_imm);
2511 //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2514 ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2517 // there is also ppc_srawi
2518 //ppc_load (code, ppc_r11, ins->inst_imm);
2519 //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2520 ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2523 ppc_load (code, ppc_r11, ins->inst_imm);
2524 ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);
2525 //ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0xf)), (ins->inst_imm & 0xf), 31);
2528 ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2531 ppc_not (code, ins->dreg, ins->sreg1);
2534 ppc_neg (code, ins->dreg, ins->sreg1);
2537 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2540 ppc_load (code, ppc_r11, ins->inst_imm);
2541 ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2544 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2545 //g_assert_not_reached ();
2546 //x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2547 //EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2549 case CEE_MUL_OVF_UN:
2550 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2551 //FIXME: g_assert_not_reached ();
2555 ppc_load (code, ins->dreg, ins->inst_c0);
2558 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_CLASS, (gpointer)ins->inst_c0);
2559 ppc_load (code, ins->dreg, 0xff00ff00);
2562 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_IMAGE, (gpointer)ins->inst_c0);
2563 ppc_load (code, ins->dreg, 0xff00ff00);
2569 ppc_mr (code, ins->dreg, ins->sreg1);
2572 int saved = ins->sreg1;
2573 if (ins->sreg1 == ppc_r3) {
2574 ppc_mr (code, ppc_r0, ins->sreg1);
2577 if (ins->sreg2 != ppc_r3)
2578 ppc_mr (code, ppc_r3, ins->sreg2);
2579 if (saved != ppc_r4)
2580 ppc_mr (code, ppc_r4, saved);
2585 ppc_fmr (code, ins->dreg, ins->sreg1);
2587 case OP_FCONV_TO_R4:
2588 ppc_frsp (code, ins->dreg, ins->sreg1);
2591 g_assert_not_reached ();
2594 /* ensure ins->sreg1 is not NULL */
2595 ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2602 call = (MonoCallInst*)ins;
2603 if (ins->flags & MONO_INST_HAS_METHOD)
2604 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2606 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2612 case OP_VOIDCALL_REG:
2614 ppc_mtlr (code, ins->sreg1);
2617 case OP_FCALL_MEMBASE:
2618 case OP_LCALL_MEMBASE:
2619 case OP_VCALL_MEMBASE:
2620 case OP_VOIDCALL_MEMBASE:
2621 case OP_CALL_MEMBASE:
2622 ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2623 ppc_mtlr (code, ppc_r0);
2627 g_assert_not_reached ();
2630 /* keep alignment */
2631 ppc_addi (code, ppc_r0, ins->sreg1, PPC_STACK_ALIGNMENT-1);
2632 ppc_rlwinm (code, ppc_r0, ppc_r0, 0, 0, 27);
2633 ppc_lwz (code, ppc_r11, 0, ppc_sp);
2634 ppc_neg (code, ppc_r0, ppc_r0);
2635 ppc_stwux (code, ppc_r11, ppc_r0, ppc_sp);
2636 ppc_addi (code, ins->dreg, ppc_sp, PPC_STACK_PARAM_OFFSET);
2642 ppc_mr (code, ppc_r3, ins->sreg1);
2643 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2644 (gpointer)"mono_arch_throw_exception");
2648 case OP_START_HANDLER:
2649 ppc_mflr (code, ppc_r0);
2650 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2653 if (ins->sreg1 != ppc_r3)
2654 ppc_mr (code, ppc_r3, ins->sreg1);
2655 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2656 ppc_mtlr (code, ppc_r0);
2659 case CEE_ENDFINALLY:
2660 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2661 ppc_mtlr (code, ppc_r0);
2664 case OP_CALL_HANDLER:
2665 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2669 ins->inst_c0 = code - cfg->native_code;
2672 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2673 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2675 if (ins->flags & MONO_INST_BRLABEL) {
2676 /*if (ins->inst_i0->inst_c0) {
2678 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2680 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2684 /*if (ins->inst_target_bb->native_offset) {
2686 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2688 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2694 ppc_mtctr (code, ins->sreg1);
2695 ppc_bcctr (code, 20, 0);
2698 ppc_li (code, ins->dreg, 0);
2699 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2700 ppc_li (code, ins->dreg, 1);
2704 ppc_li (code, ins->dreg, 1);
2705 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2706 ppc_li (code, ins->dreg, 0);
2710 ppc_li (code, ins->dreg, 1);
2711 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2712 ppc_li (code, ins->dreg, 0);
2714 case OP_COND_EXC_EQ:
2715 case OP_COND_EXC_NE_UN:
2716 case OP_COND_EXC_LT:
2717 case OP_COND_EXC_LT_UN:
2718 case OP_COND_EXC_GT:
2719 case OP_COND_EXC_GT_UN:
2720 case OP_COND_EXC_GE:
2721 case OP_COND_EXC_GE_UN:
2722 case OP_COND_EXC_LE:
2723 case OP_COND_EXC_LE_UN:
2724 case OP_COND_EXC_OV:
2725 case OP_COND_EXC_NO:
2727 case OP_COND_EXC_NC:
2728 //EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ],
2729 // (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2741 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2744 /* floating point opcodes */
2746 ppc_load (code, ppc_r11, ins->inst_p0);
2747 ppc_lfd (code, ins->dreg, 0, ppc_r11);
2750 ppc_load (code, ppc_r11, ins->inst_p0);
2751 ppc_lfs (code, ins->dreg, 0, ppc_r11);
2753 case OP_STORER8_MEMBASE_REG:
2754 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2756 case OP_LOADR8_MEMBASE:
2757 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2759 case OP_STORER4_MEMBASE_REG:
2760 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2762 case OP_LOADR4_MEMBASE:
2763 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2765 case CEE_CONV_R4: /* FIXME: change precision */
2767 static const guint64 adjust_val = 0x4330000000000000UL;
2768 ppc_li (code, ppc_r0, 0);
2769 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2770 ppc_stw (code, ins->sreg1, -8, ppc_sp);
2771 ppc_xoris (code, ppc_r11, ins->sreg1, 0x8000);
2772 ppc_stw (code, ppc_r11, -4, ppc_sp);
2773 ppc_lfd (code, ins->dreg, -8, ppc_sp);
2774 ppc_li (code, ppc_r11, &adjust_val);
2775 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2776 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2779 case OP_X86_FP_LOAD_I8:
2780 g_assert_not_reached ();
2781 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2783 case OP_X86_FP_LOAD_I4:
2784 g_assert_not_reached ();
2785 x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2787 case OP_FCONV_TO_I1:
2788 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
2790 case OP_FCONV_TO_U1:
2791 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
2793 case OP_FCONV_TO_I2:
2794 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
2796 case OP_FCONV_TO_U2:
2797 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
2799 case OP_FCONV_TO_I4:
2801 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
2803 case OP_FCONV_TO_U4:
2805 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
2807 case OP_FCONV_TO_I8:
2808 case OP_FCONV_TO_U8:
2809 g_assert_not_reached ();
2810 /* Implemented as helper calls */
2812 case OP_LCONV_TO_R_UN:
2813 g_assert_not_reached ();
2814 /* Implemented as helper calls */
2816 case OP_LCONV_TO_OVF_I: {
2817 ppc_mr (code, ins->dreg, ins->sreg1);
2818 /* FIXME: emit exception if needed */
2822 ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
2825 ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
2828 ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
2831 ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
2834 ppc_fneg (code, ins->dreg, ins->sreg1);
2838 g_assert_not_reached ();
2841 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2844 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2845 ppc_li (code, ins->dreg, 0);
2846 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2847 ppc_li (code, ins->dreg, 1);
2850 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2851 ppc_li (code, ins->dreg, 1);
2852 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2853 ppc_li (code, ins->dreg, 0);
2856 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2857 ppc_li (code, ins->dreg, 1);
2858 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2859 ppc_li (code, ins->dreg, 0);
2862 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2863 ppc_li (code, ins->dreg, 1);
2864 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2865 ppc_li (code, ins->dreg, 0);
2868 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2869 ppc_li (code, ins->dreg, 1);
2870 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2871 ppc_li (code, ins->dreg, 0);
2874 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
2877 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
2880 EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
2883 EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
2886 EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
2889 EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
2892 EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
2895 EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
2898 EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
2901 EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
2903 case CEE_CKFINITE: {
2904 ppc_stfd (code, ins->sreg1, -8, ppc_sp);
2905 ppc_lwz (code, ppc_r0, -8, ppc_sp);
2906 ppc_rlwinm (code, ppc_r0, ppc_r0, 0, 1, 31);
2907 ppc_xoris (code, ppc_r11, ppc_r0, 0x7ff0);
2908 ppc_neg (code, ppc_r0, ppc_r11);
2909 ppc_rlwinm (code, ppc_r0, ppc_r0, 1, 31, 31);
2910 g_assert_not_reached ();
2911 x86_push_reg (code, X86_EAX);
2914 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2915 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2916 x86_pop_reg (code, X86_EAX);
2917 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
2921 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2922 g_assert_not_reached ();
2925 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2926 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2927 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2928 g_assert_not_reached ();
2934 last_offset = offset;
2939 cfg->code_len = code - cfg->native_code;
2943 mono_arch_register_lowlevel_calls (void)
2945 mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2946 mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2950 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
2952 MonoJumpInfo *patch_info;
2954 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2955 unsigned char *ip = patch_info->ip.i + code;
2956 const unsigned char *target = NULL;
2958 switch (patch_info->type) {
2959 case MONO_PATCH_INFO_BB:
2960 target = patch_info->data.bb->native_offset + code;
2962 case MONO_PATCH_INFO_ABS:
2963 target = patch_info->data.target;
2965 case MONO_PATCH_INFO_LABEL:
2966 target = patch_info->data.inst->inst_c0 + code;
2968 case MONO_PATCH_INFO_IP:
2969 *((gpointer *)(ip)) = ip;
2971 case MONO_PATCH_INFO_METHOD_REL:
2972 *((gpointer *)(ip)) = code + patch_info->data.offset;
2974 case MONO_PATCH_INFO_INTERNAL_METHOD: {
2975 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
2977 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
2978 g_assert_not_reached ();
2980 target = mi->wrapper;
2983 case MONO_PATCH_INFO_METHOD_JUMP:
2984 g_assert_not_reached ();
2986 case MONO_PATCH_INFO_METHOD:
2987 if (patch_info->data.method == method) {
2990 /* get the trampoline to the method from the domain */
2991 target = mono_arch_create_jit_trampoline (patch_info->data.method);
2994 case MONO_PATCH_INFO_SWITCH: {
2995 gpointer *table = (gpointer *)patch_info->data.target;
2998 // FIXME: inspect code to get the register
2999 ppc_load (ip, ppc_r11, patch_info->data.target);
3000 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3002 for (i = 0; i < patch_info->table_size; i++) {
3003 table [i] = (int)patch_info->data.table [i] + code;
3005 /* we put into the table the absolute address, no need for ppc_patch in this case */
3008 case MONO_PATCH_INFO_METHODCONST:
3009 case MONO_PATCH_INFO_CLASS:
3010 case MONO_PATCH_INFO_IMAGE:
3011 case MONO_PATCH_INFO_FIELD:
3012 g_assert_not_reached ();
3013 *((gconstpointer *)(ip + 1)) = patch_info->data.target;
3015 case MONO_PATCH_INFO_R4:
3016 case MONO_PATCH_INFO_R8:
3017 g_assert_not_reached ();
3018 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3020 case MONO_PATCH_INFO_IID:
3021 g_assert_not_reached ();
3022 mono_class_init (patch_info->data.klass);
3023 *((guint32 *)(ip + 1)) = patch_info->data.klass->interface_id;
3025 case MONO_PATCH_INFO_VTABLE:
3026 g_assert_not_reached ();
3027 *((gconstpointer *)(ip + 1)) = mono_class_vtable (domain, patch_info->data.klass);
3029 case MONO_PATCH_INFO_CLASS_INIT:
3030 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
3032 case MONO_PATCH_INFO_SFLDA: {
3033 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
3034 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
3035 /* Done by the generated code */
3039 mono_runtime_class_init (vtable);
3041 g_assert_not_reached ();
3042 *((gconstpointer *)(ip + 1)) =
3043 (char*)vtable->data + patch_info->data.field->offset;
3046 case MONO_PATCH_INFO_EXC_NAME:
3047 g_assert_not_reached ();
3048 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3050 case MONO_PATCH_INFO_LDSTR:
3051 g_assert_not_reached ();
3052 *((gconstpointer *)(ip + 1)) =
3053 mono_ldstr (domain, patch_info->data.token->image,
3054 mono_metadata_token_index (patch_info->data.token->token));
3056 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
3058 MonoClass *handle_class;
3060 handle = mono_ldtoken (patch_info->data.token->image,
3061 patch_info->data.token->token, &handle_class);
3062 mono_class_init (handle_class);
3063 mono_class_init (mono_class_from_mono_type (handle));
3065 g_assert_not_reached ();
3066 *((gconstpointer *)(ip + 1)) =
3067 mono_type_get_object (domain, handle);
3070 case MONO_PATCH_INFO_LDTOKEN: {
3072 MonoClass *handle_class;
3074 handle = mono_ldtoken (patch_info->data.token->image,
3075 patch_info->data.token->token, &handle_class);
3076 mono_class_init (handle_class);
3078 g_assert_not_reached ();
3079 *((gconstpointer *)(ip + 1)) = handle;
3083 g_assert_not_reached ();
3085 ppc_patch (ip, target);
3090 mono_arch_max_epilog_size (MonoCompile *cfg)
3092 int exc_count = 0, max_epilog_size = 16 + 20*4;
3093 MonoJumpInfo *patch_info;
3095 if (cfg->method->save_lmf)
3096 max_epilog_size += 128;
3098 if (mono_jit_trace_calls != NULL)
3099 max_epilog_size += 50;
3101 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3102 max_epilog_size += 50;
3104 /* count the number of exception infos */
3106 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3107 if (patch_info->type == MONO_PATCH_INFO_EXC)
3112 * make sure we have enough space for exceptions
3113 * 16 is the size of two push_imm instructions and a call
3115 max_epilog_size += exc_count*16;
3117 return max_epilog_size;
3121 mono_arch_emit_prolog (MonoCompile *cfg)
3123 MonoMethod *method = cfg->method;
3125 MonoMethodSignature *sig;
3127 int alloc_size, pos, max_offset, i;
3132 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3135 cfg->code_size = 256;
3136 code = cfg->native_code = g_malloc (cfg->code_size);
3138 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3139 ppc_mflr (code, ppc_r0);
3140 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3142 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
3143 cfg->used_int_regs |= 1 << 31;
3146 alloc_size = cfg->stack_offset;
3148 /* reserve room to save return value */
3152 if (method->save_lmf) {
3154 pos += sizeof (MonoLMF);
3156 /* save the current IP */
3157 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3158 x86_push_imm (code, 0);
3160 /* save all caller saved regs */
3161 x86_push_reg (code, X86_EBX);
3162 x86_push_reg (code, X86_EDI);
3163 x86_push_reg (code, X86_ESI);
3164 x86_push_reg (code, X86_EBP);
3166 /* save method info */
3167 x86_push_imm (code, method);
3169 /* get the address of lmf for the current thread */
3170 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3171 (gpointer)"get_lmf_addr");
3172 x86_call_code (code, 0);
3175 x86_push_reg (code, X86_EAX);
3176 /* push *lfm (previous_lmf) */
3177 x86_push_membase (code, X86_EAX, 0);
3179 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
3183 for (i = 13; i < 32; ++i) {
3184 if (cfg->used_int_regs & (1 << i)) {
3186 ppc_stw (code, i, -pos, ppc_sp);
3192 // align to PPC_STACK_ALIGNMENT bytes
3193 if (alloc_size & (PPC_STACK_ALIGNMENT - 1))
3194 alloc_size += PPC_STACK_ALIGNMENT - (alloc_size & (PPC_STACK_ALIGNMENT - 1));
3196 cfg->stack_usage = alloc_size;
3197 g_assert (ppc_is_imm16 (-alloc_size));
3199 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3200 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
3201 ppc_mr (code, ppc_r31, ppc_sp);
3203 /* compute max_offset in order to use short forward jumps */
3205 if (cfg->opt & MONO_OPT_BRANCH) {
3206 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3207 MonoInst *ins = bb->code;
3208 bb->max_offset = max_offset;
3210 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3214 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3220 /* load arguments allocated to register from the stack */
3221 sig = method->signature;
3224 cinfo = calculate_sizes (sig, sig->pinvoke);
3226 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3227 ArgInfo *ainfo = &cinfo->ret;
3229 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3231 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3232 ArgInfo *ainfo = cinfo->args + i;
3233 inst = cfg->varinfo [pos];
3235 if (inst->opcode == OP_REGVAR) {
3236 if (ainfo->regtype == RegTypeGeneral)
3237 ppc_mr (code, inst->dreg, ainfo->reg);
3238 else if (ainfo->regtype == RegTypeFP)
3239 ppc_fmr (code, inst->dreg, ainfo->reg);
3241 g_assert_not_reached ();
3242 if (cfg->verbose_level > 2)
3243 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3245 /* the argument should be put on the stack: FIXME handle size != word */
3246 if (ainfo->regtype == RegTypeGeneral) {
3247 switch (ainfo->size) {
3249 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3252 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3255 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3256 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3259 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3261 } else if (ainfo->regtype == RegTypeFP) {
3262 if (ainfo->size == 8)
3263 ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3264 else if (ainfo->size == 4)
3265 ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3267 g_assert_not_reached ();
3268 } else if (ainfo->regtype == RegTypeStructByVal) {
3269 int doffset = inst->inst_offset;
3272 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3273 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3274 soffset += sizeof (gpointer);
3275 doffset += sizeof (gpointer);
3277 if (ainfo->vtsize) {
3278 /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3279 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3280 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3281 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3283 } else if (ainfo->regtype == RegTypeStructByAddr) {
3284 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3285 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3287 g_assert_not_reached ();
3293 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
3295 cfg->code_len = code - cfg->native_code;
3302 mono_arch_emit_epilog (MonoCompile *cfg)
3304 MonoJumpInfo *patch_info;
3305 MonoMethod *method = cfg->method;
3309 code = cfg->native_code + cfg->code_len;
3311 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3312 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
3318 if (method->save_lmf) {
3319 pos = -sizeof (MonoLMF);
3322 if (method->save_lmf) {
3324 /* ebx = previous_lmf */
3325 x86_pop_reg (code, X86_EBX);
3327 x86_pop_reg (code, X86_EDI);
3328 /* *(lmf) = previous_lmf */
3329 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
3331 /* discard method info */
3332 x86_pop_reg (code, X86_ESI);
3334 /* restore caller saved regs */
3335 x86_pop_reg (code, X86_EBP);
3336 x86_pop_reg (code, X86_ESI);
3337 x86_pop_reg (code, X86_EDI);
3338 x86_pop_reg (code, X86_EBX);
3342 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3343 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3344 ppc_mtlr (code, ppc_r0);
3346 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3347 for (i = 13; i < 32; ++i) {
3348 if (cfg->used_int_regs & (1 << i)) {
3350 ppc_lwz (code, i, -pos, cfg->frame_reg);
3355 /* add code to raise exceptions */
3356 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3357 switch (patch_info->type) {
3358 case MONO_PATCH_INFO_EXC:
3359 /*x86_patch (patch_info->ip.i + cfg->native_code, code);
3360 x86_push_imm (code, patch_info->data.target);
3361 x86_push_imm (code, patch_info->ip.i + cfg->native_code);
3362 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3363 patch_info->data.name = "mono_arch_throw_exception_by_name";
3364 patch_info->ip.i = code - cfg->native_code;
3365 x86_jump_code (code, 0);*/
3373 cfg->code_len = code - cfg->native_code;
3375 g_assert (cfg->code_len < cfg->code_size);
3380 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3385 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3387 int this_dreg = ppc_r3;
3392 /* add the this argument */
3393 if (this_reg != -1) {
3395 MONO_INST_NEW (cfg, this, OP_SETREG);
3396 this->type = this_type;
3397 this->sreg1 = this_reg;
3398 this->dreg = this_dreg;
3399 mono_bblock_add_inst (cfg->cbb, this);
3404 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3405 vtarg->type = STACK_MP;
3406 vtarg->sreg1 = vt_reg;
3407 vtarg->dreg = ppc_r3;
3408 mono_bblock_add_inst (cfg->cbb, vtarg);