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>
21 int mono_exc_esp_offset = 0;
24 mono_arch_regname (int reg) {
25 static const char * rnames[] = {
26 "ppc_r0", "ppc_sp", "ppc_r2", "ppc_r3", "ppc_r4",
27 "ppc_r5", "ppc_r6", "ppc_r7", "ppc_r8", "ppc_r9",
28 "ppc_r10", "ppc_r11", "ppc_r12", "ppc_r13", "ppc_r14",
29 "ppc_r15", "ppc_r16", "ppc_r17", "ppc_r18", "ppc_r19",
30 "ppc_r20", "ppc_r21", "ppc_r22", "ppc_r23", "ppc_r24",
31 "ppc_r25", "ppc_r26", "ppc_r27", "ppc_r28", "ppc_r29",
34 if (reg >= 0 && reg < 32)
39 /* this function overwrites r0 */
41 emit_memcpy (guint32 *code, int size, int dreg, int doffset, int sreg, int soffset)
43 /* unrolled, use the counter in big */
45 ppc_lwz (code, ppc_r0, soffset, sreg);
46 ppc_stw (code, ppc_r0, doffset, dreg);
52 ppc_lhz (code, ppc_r0, soffset, sreg);
53 ppc_sth (code, ppc_r0, doffset, dreg);
59 ppc_lbz (code, ppc_r0, soffset, sreg);
60 ppc_stb (code, ppc_r0, doffset, dreg);
69 * mono_arch_get_argument_info:
70 * @csig: a method signature
71 * @param_count: the number of parameters to consider
72 * @arg_info: an array to store the result infos
74 * Gathers information on parameters such as size, alignment and
75 * padding. arg_info should be large enought to hold param_count + 1 entries.
77 * Returns the size of the activation frame.
80 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
82 int k, frame_size = 0;
86 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
87 frame_size += sizeof (gpointer);
91 arg_info [0].offset = offset;
94 frame_size += sizeof (gpointer);
98 arg_info [0].size = frame_size;
100 for (k = 0; k < param_count; k++) {
103 size = mono_type_native_stack_size (csig->params [k], &align);
105 size = mono_type_stack_size (csig->params [k], &align);
107 /* ignore alignment for now */
110 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
111 arg_info [k].pad = pad;
113 arg_info [k + 1].pad = 0;
114 arg_info [k + 1].size = size;
116 arg_info [k + 1].offset = offset;
120 align = MONO_ARCH_FRAME_ALIGNMENT;
121 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
122 arg_info [k].pad = pad;
128 * Initialize the cpu to execute managed code.
131 mono_arch_cpu_init (void)
136 * This function returns the optimizations supported on this cpu.
139 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
143 /* no ppc-specific optimizations yet */
144 *exclude_mask = MONO_OPT_INLINE|MONO_OPT_LINEARS;
149 is_regsize_var (MonoType *t) {
158 case MONO_TYPE_OBJECT:
159 case MONO_TYPE_STRING:
160 case MONO_TYPE_CLASS:
161 case MONO_TYPE_SZARRAY:
162 case MONO_TYPE_ARRAY:
164 case MONO_TYPE_VALUETYPE:
165 if (t->data.klass->enumtype)
166 return is_regsize_var (t->data.klass->enum_basetype);
173 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
178 for (i = 0; i < cfg->num_varinfo; i++) {
179 MonoInst *ins = cfg->varinfo [i];
180 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
183 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
186 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
189 /* we can only allocate 32 bit values */
190 if (is_regsize_var (ins->inst_vtype)) {
191 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
192 g_assert (i == vmv->idx);
193 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
200 #define USE_EXTRA_TEMPS ((1<<30) | (1<<29))
201 //#define USE_EXTRA_TEMPS 0
204 mono_arch_get_global_int_regs (MonoCompile *cfg)
208 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
213 for (i = 13; i < top; ++i)
214 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
220 * mono_arch_regalloc_cost:
222 * Return the cost, in number of memory references, of the action of
223 * allocating the variable VMV into a register during global register
227 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
233 // code from ppc/tramp.c, try to keep in sync
234 #define MIN_CACHE_LINE 8
237 mono_arch_flush_icache (guint8 *code, gint size)
243 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
244 asm ("dcbst 0,%0;" : : "r"(p) : "memory");
248 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
249 asm ("icbi 0,%0; sync;" : : "r"(p) : "memory");
255 #define NOT_IMPLEMENTED(x) \
256 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
259 #define ALWAYS_ON_STACK(s) s
260 #define FP_ALSO_IN_REG(s) s
262 #define ALWAYS_ON_STACK(s)
263 #define FP_ALSO_IN_REG(s) s
264 #define ALIGN_DOUBLES
277 guint16 vtsize; /* in param area */
279 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
280 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
294 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
297 if (*gr >= 3 + PPC_NUM_REG_ARGS) {
298 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
299 ainfo->reg = ppc_sp; /* in the caller */
300 ainfo->regtype = RegTypeBase;
303 ALWAYS_ON_STACK (*stack_size += 4);
307 if (*gr >= 3 + PPC_NUM_REG_ARGS - 1) {
309 //*stack_size += (*stack_size % 8);
311 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
312 ainfo->reg = ppc_sp; /* in the caller */
313 ainfo->regtype = RegTypeBase;
320 ALWAYS_ON_STACK (*stack_size += 8);
329 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
332 int n = sig->hasthis + sig->param_count;
334 guint32 stack_size = 0;
335 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
337 fr = PPC_FIRST_FPARG_REG;
338 gr = PPC_FIRST_ARG_REG;
340 /* FIXME: handle returning a struct */
341 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
342 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
343 cinfo->struct_ret = PPC_FIRST_ARG_REG;
348 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
351 DEBUG(printf("params: %d\n", sig->param_count));
352 for (i = 0; i < sig->param_count; ++i) {
353 DEBUG(printf("param %d: ", i));
354 if (sig->params [i]->byref) {
355 DEBUG(printf("byref\n"));
356 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
360 simpletype = sig->params [i]->type;
362 switch (simpletype) {
363 case MONO_TYPE_BOOLEAN:
366 cinfo->args [n].size = 1;
367 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
373 cinfo->args [n].size = 2;
374 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
379 cinfo->args [n].size = 4;
380 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
386 case MONO_TYPE_FNPTR:
387 case MONO_TYPE_CLASS:
388 case MONO_TYPE_OBJECT:
389 case MONO_TYPE_STRING:
390 case MONO_TYPE_SZARRAY:
391 case MONO_TYPE_ARRAY:
392 cinfo->args [n].size = sizeof (gpointer);
393 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
396 case MONO_TYPE_VALUETYPE: {
398 if (sig->params [i]->data.klass->enumtype) {
399 simpletype = sig->params [i]->data.klass->enum_basetype->type;
402 size = mono_class_value_size (sig->params [i]->data.klass, NULL);
403 DEBUG(printf ("load %d bytes struct\n",
404 mono_class_value_size (sig->params [i]->data.klass, NULL)));
405 #if PPC_PASS_STRUCTS_BY_VALUE
407 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
408 cinfo->args [n].regtype = RegTypeStructByVal;
409 if (gr <= PPC_LAST_ARG_REG) {
410 int rest = PPC_LAST_ARG_REG - gr + 1;
411 int n_in_regs = rest >= nwords? nwords: rest;
412 cinfo->args [n].size = n_in_regs;
413 cinfo->args [n].vtsize = nwords - n_in_regs;
414 cinfo->args [n].reg = gr;
417 cinfo->args [n].size = 0;
418 cinfo->args [n].vtsize = nwords;
420 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
421 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
422 stack_size += nwords * sizeof (gpointer);
425 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
426 cinfo->args [n].regtype = RegTypeStructByAddr;
431 case MONO_TYPE_TYPEDBYREF: {
432 int size = sizeof (MonoTypedRef);
433 /* keep in sync or merge with the valuetype case */
434 #if PPC_PASS_STRUCTS_BY_VALUE
436 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
437 cinfo->args [n].regtype = RegTypeStructByVal;
438 if (gr <= PPC_LAST_ARG_REG) {
439 int rest = PPC_LAST_ARG_REG - gr + 1;
440 int n_in_regs = rest >= nwords? nwords: rest;
441 cinfo->args [n].size = n_in_regs;
442 cinfo->args [n].vtsize = nwords - n_in_regs;
443 cinfo->args [n].reg = gr;
446 cinfo->args [n].size = 0;
447 cinfo->args [n].vtsize = nwords;
449 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
450 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
451 stack_size += nwords * sizeof (gpointer);
454 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
455 cinfo->args [n].regtype = RegTypeStructByAddr;
462 cinfo->args [n].size = 8;
463 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
467 cinfo->args [n].size = 4;
469 /* It was 7, now it is 8 in LinuxPPC */
470 if (fr <= PPC_LAST_FPARG_REG) {
471 cinfo->args [n].regtype = RegTypeFP;
472 cinfo->args [n].reg = fr;
474 FP_ALSO_IN_REG (gr ++);
475 ALWAYS_ON_STACK (stack_size += 4);
477 cinfo->args [n].offset = stack_size;
478 cinfo->args [n].regtype = RegTypeBase;
479 cinfo->args [n].reg = ppc_sp; /* in the caller*/
485 cinfo->args [n].size = 8;
486 /* It was 7, now it is 8 in LinuxPPC */
487 if (fr <= PPC_LAST_FPARG_REG) {
488 cinfo->args [n].regtype = RegTypeFP;
489 cinfo->args [n].reg = fr;
491 FP_ALSO_IN_REG (gr += 2);
492 ALWAYS_ON_STACK (stack_size += 8);
494 cinfo->args [n].offset = stack_size;
495 cinfo->args [n].regtype = RegTypeBase;
496 cinfo->args [n].reg = ppc_sp; /* in the caller*/
502 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
507 simpletype = sig->ret->type;
509 switch (simpletype) {
510 case MONO_TYPE_BOOLEAN:
521 case MONO_TYPE_FNPTR:
522 case MONO_TYPE_CLASS:
523 case MONO_TYPE_OBJECT:
524 case MONO_TYPE_SZARRAY:
525 case MONO_TYPE_ARRAY:
526 case MONO_TYPE_STRING:
527 cinfo->ret.reg = ppc_r3;
531 cinfo->ret.reg = ppc_r3;
535 cinfo->ret.reg = ppc_f1;
536 cinfo->ret.regtype = RegTypeFP;
538 case MONO_TYPE_VALUETYPE:
539 if (sig->ret->data.klass->enumtype) {
540 simpletype = sig->ret->data.klass->enum_basetype->type;
544 case MONO_TYPE_TYPEDBYREF:
548 g_error ("Can't handle as return value 0x%x", sig->ret->type);
552 /* align stack size to 16 */
553 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
554 stack_size = (stack_size + 15) & ~15;
556 cinfo->stack_usage = stack_size;
562 * Set var information according to the calling convention. ppc version.
563 * The locals var stuff should most likely be split in another method.
566 mono_arch_allocate_vars (MonoCompile *m)
568 MonoMethodSignature *sig;
569 MonoMethodHeader *header;
571 int i, offset, size, align, curinst;
572 int frame_reg = ppc_sp;
575 * FIXME: we'll use the frame register also for any method that has
576 * filter clauses. This way, when the handlers are called,
577 * the code will reference local variables using the frame reg instead of
578 * the stack pointer: if we had to restore the stack pointer, we'd
579 * corrupt the method frames that are already on the stack (since
580 * filters get called before stack unwinding happens) when the filter
581 * code would call any method.
583 if (m->flags & MONO_CFG_HAS_ALLOCA)
585 m->frame_reg = frame_reg;
587 header = ((MonoMethodNormal *)m->method)->header;
589 sig = m->method->signature;
593 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
594 m->ret->opcode = OP_REGVAR;
595 m->ret->inst_c0 = ppc_r3;
597 /* FIXME: handle long and FP values */
598 switch (sig->ret->type) {
602 m->ret->opcode = OP_REGVAR;
603 m->ret->inst_c0 = ppc_r3;
607 /* local vars are at a positive offset from the stack pointer */
609 * also note that if the function uses alloca, we use ppc_r31
610 * to point at the local variables.
612 offset = PPC_MINIMAL_STACK_SIZE; /* linkage area */
613 /* align the offset to 16 bytes: not sure this is needed here */
615 //offset &= ~(16 - 1);
617 /* add parameter area size for called functions */
618 offset += m->param_area;
622 /* FIXME: check how to handle this stuff... reserve space to save LMF and caller saved registers */
623 if (m->method->save_lmf)
624 offset += sizeof (MonoLMF);
627 /* this stuff should not be needed on ppc and the new jit,
628 * because a call on ppc to the handlers doesn't change the
629 * stack pointer and the jist doesn't manipulate the stack pointer
630 * for operations involving valuetypes.
632 /* reserve space to store the esp */
633 offset += sizeof (gpointer);
635 /* this is a global constant */
636 mono_exc_esp_offset = offset;
639 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
641 offset += sizeof(gpointer) - 1;
642 offset &= ~(sizeof(gpointer) - 1);
643 inst->inst_offset = offset;
644 inst->opcode = OP_REGOFFSET;
645 inst->inst_basereg = frame_reg;
646 offset += sizeof(gpointer);
648 curinst = m->locals_start;
649 for (i = curinst; i < m->num_varinfo; ++i) {
650 inst = m->varinfo [i];
651 if (inst->opcode == OP_REGVAR)
654 /* inst->unused indicates native sized value types, this is used by the
655 * pinvoke wrappers when they call functions returning structure */
656 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
657 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
659 size = mono_type_size (inst->inst_vtype, &align);
662 offset &= ~(align - 1);
663 inst->inst_offset = offset;
664 inst->opcode = OP_REGOFFSET;
665 inst->inst_basereg = frame_reg;
667 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
672 inst = m->varinfo [curinst];
673 if (inst->opcode != OP_REGVAR) {
674 inst->opcode = OP_REGOFFSET;
675 inst->inst_basereg = frame_reg;
676 offset += sizeof (gpointer) - 1;
677 offset &= ~(sizeof (gpointer) - 1);
678 inst->inst_offset = offset;
679 offset += sizeof (gpointer);
684 for (i = 0; i < sig->param_count; ++i) {
685 inst = m->varinfo [curinst];
686 if (inst->opcode != OP_REGVAR) {
687 inst->opcode = OP_REGOFFSET;
688 inst->inst_basereg = frame_reg;
689 size = mono_type_size (sig->params [i], &align);
691 offset &= ~(align - 1);
692 inst->inst_offset = offset;
698 /* align the offset to 16 bytes */
703 m->stack_offset = offset;
707 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
708 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
712 * take the arguments and generate the arch-specific
713 * instructions to properly call the function in call.
714 * This includes pushing, moving arguments to the right register
716 * Issue: who does the spilling if needed, and when?
719 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
721 MonoMethodSignature *sig;
727 sig = call->signature;
728 n = sig->param_count + sig->hasthis;
730 cinfo = calculate_sizes (sig, sig->pinvoke);
731 if (cinfo->struct_ret)
732 call->used_iregs |= 1 << cinfo->struct_ret;
734 for (i = 0; i < n; ++i) {
735 ainfo = cinfo->args + i;
736 if (is_virtual && i == 0) {
737 /* the argument will be attached to the call instrucion */
739 call->used_iregs |= 1 << ainfo->reg;
741 MONO_INST_NEW (cfg, arg, OP_OUTARG);
743 arg->cil_code = in->cil_code;
745 arg->type = in->type;
746 /* prepend, we'll need to reverse them later */
747 arg->next = call->out_args;
748 call->out_args = arg;
749 if (ainfo->regtype == RegTypeGeneral) {
750 arg->unused = ainfo->reg;
751 call->used_iregs |= 1 << ainfo->reg;
752 if (arg->type == STACK_I8)
753 call->used_iregs |= 1 << (ainfo->reg + 1);
754 } else if (ainfo->regtype == RegTypeStructByAddr) {
755 /* FIXME: where si the data allocated? */
756 arg->unused = ainfo->reg;
757 call->used_iregs |= 1 << ainfo->reg;
758 } else if (ainfo->regtype == RegTypeStructByVal) {
760 /* mark the used regs */
761 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
762 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
764 arg->opcode = OP_OUTARG_VT;
765 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
766 arg->inst_imm = ainfo->offset;
767 } else if (ainfo->regtype == RegTypeBase) {
768 arg->opcode = OP_OUTARG;
769 arg->unused = ainfo->reg | (ainfo->size << 8);
770 arg->inst_imm = ainfo->offset;
771 } else if (ainfo->regtype == RegTypeFP) {
772 arg->opcode = OP_OUTARG_R8;
773 arg->unused = ainfo->reg;
774 call->used_fregs |= 1 << ainfo->reg;
775 if (ainfo->size == 4) {
776 /* we reduce the precision */
778 MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
779 conv->inst_left = arg->inst_left;
780 arg->inst_left = conv;
783 g_assert_not_reached ();
788 * Reverse the call->out_args list.
791 MonoInst *prev = NULL, *list = call->out_args, *next;
798 call->out_args = prev;
800 call->stack_usage = cinfo->stack_usage;
801 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
802 cfg->flags |= MONO_CFG_HAS_CALLS;
804 * should set more info in call, such as the stack space
805 * used by the args that needs to be added back to esp
813 * Allow tracing to work with this interface (with an optional argument)
817 * This may be needed on some archs or for debugging support.
820 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
822 /* no stack room needed now (may be needed for FASTCALL-trace support) */
824 /* split prolog-epilog requirements? */
825 *code = 50; /* max bytes needed: check this number */
829 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
833 ppc_load (code, ppc_r3, cfg->method);
834 ppc_li (code, ppc_r4, 0); /* NULL ebp for now */
835 ppc_load (code, ppc_r0, func);
836 ppc_mtlr (code, ppc_r0);
850 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
853 int save_mode = SAVE_NONE;
854 MonoMethod *method = cfg->method;
855 int rtype = method->signature->ret->type;
860 /* special case string .ctor icall */
861 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
862 save_mode = SAVE_ONE;
864 save_mode = SAVE_NONE;
868 save_mode = SAVE_TWO;
874 case MONO_TYPE_VALUETYPE:
875 if (method->signature->ret->data.klass->enumtype) {
876 rtype = method->signature->ret->data.klass->enum_basetype->type;
879 save_mode = SAVE_STRUCT;
882 save_mode = SAVE_ONE;
888 ppc_stw (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
889 ppc_stw (code, ppc_r4, cfg->stack_usage - 4, cfg->frame_reg);
890 if (enable_arguments) {
891 ppc_mr (code, ppc_r5, ppc_r4);
892 ppc_mr (code, ppc_r4, ppc_r3);
896 ppc_stw (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
897 if (enable_arguments) {
898 ppc_mr (code, ppc_r4, ppc_r3);
902 ppc_stfd (code, ppc_f1, cfg->stack_usage - 8, cfg->frame_reg);
903 if (enable_arguments) {
904 /* FIXME: what reg? */
905 ppc_fmr (code, ppc_f3, ppc_f1);
906 ppc_lwz (code, ppc_r4, cfg->stack_usage - 8, cfg->frame_reg);
907 ppc_lwz (code, ppc_r5, cfg->stack_usage - 4, cfg->frame_reg);
911 if (enable_arguments) {
912 /* FIXME: get the actual address */
913 ppc_mr (code, ppc_r4, ppc_r3);
921 ppc_load (code, ppc_r3, cfg->method);
922 ppc_load (code, ppc_r0, func);
923 ppc_mtlr (code, ppc_r0);
928 ppc_lwz (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
929 ppc_lwz (code, ppc_r4, cfg->stack_usage - 4, cfg->frame_reg);
932 ppc_lwz (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
935 ppc_lfd (code, ppc_f1, cfg->stack_usage - 8, cfg->frame_reg);
945 * Conditional branches have a small offset, so if it is likely overflowed,
946 * we do a branch to the end of the method (uncond branches have much larger
947 * offsets) where we perform the conditional and jump back unconditionally.
948 * It's slightly slower, since we add two uncond branches, but it's very simple
949 * with the current patch implementation and such large methods are likely not
950 * going to be perf critical anyway.
959 #define EMIT_COND_BRANCH(ins,cond) \
960 if (ins->flags & MONO_INST_BRLABEL) { \
961 if (0 && ins->inst_i0->inst_c0) { \
962 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff); \
964 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
965 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0); \
968 if (0 && ins->inst_true_bb->native_offset) { \
969 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
971 int br_disp = ins->inst_true_bb->max_offset - offset; \
972 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
973 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
974 ovfj->bb = ins->inst_true_bb; \
976 ovfj->b0_cond = branch_b0_table [cond]; \
977 ovfj->b1_cond = branch_b1_table [cond]; \
978 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB_OVF, ovfj); \
981 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
982 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0); \
987 /* emit an exception if condition is fail
989 * We assign the extra code used to throw the implicit exceptions
990 * to cfg->bb_exit as far as the big branch handling is concerned
992 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) \
994 int br_disp = cfg->bb_exit->max_offset - offset; \
995 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
996 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
999 ovfj->b0_cond = branch_b0_table [cond]; \
1000 ovfj->b1_cond = branch_b1_table [cond]; \
1001 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj); \
1003 cfg->bb_exit->max_offset += 24; \
1005 mono_add_patch_info (cfg, code - cfg->native_code, \
1006 MONO_PATCH_INFO_EXC, exc_name); \
1007 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0); \
1012 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1014 MonoInst *ins, *last_ins = NULL;
1019 switch (ins->opcode) {
1021 /* remove unnecessary multiplication with 1 */
1022 if (ins->inst_imm == 1) {
1023 if (ins->dreg != ins->sreg1) {
1024 ins->opcode = OP_MOVE;
1026 last_ins->next = ins->next;
1032 case OP_LOAD_MEMBASE:
1033 case OP_LOADI4_MEMBASE:
1035 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1036 * OP_LOAD_MEMBASE offset(basereg), reg
1038 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1039 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1040 ins->inst_basereg == last_ins->inst_destbasereg &&
1041 ins->inst_offset == last_ins->inst_offset) {
1042 if (ins->dreg == last_ins->sreg1) {
1043 last_ins->next = ins->next;
1047 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1048 ins->opcode = OP_MOVE;
1049 ins->sreg1 = last_ins->sreg1;
1053 * Note: reg1 must be different from the basereg in the second load
1054 * OP_LOAD_MEMBASE offset(basereg), reg1
1055 * OP_LOAD_MEMBASE offset(basereg), reg2
1057 * OP_LOAD_MEMBASE offset(basereg), reg1
1058 * OP_MOVE reg1, reg2
1060 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1061 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1062 ins->inst_basereg != last_ins->dreg &&
1063 ins->inst_basereg == last_ins->inst_basereg &&
1064 ins->inst_offset == last_ins->inst_offset) {
1066 if (ins->dreg == last_ins->dreg) {
1067 last_ins->next = ins->next;
1071 ins->opcode = OP_MOVE;
1072 ins->sreg1 = last_ins->dreg;
1075 //g_assert_not_reached ();
1079 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1080 * OP_LOAD_MEMBASE offset(basereg), reg
1082 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1083 * OP_ICONST reg, imm
1085 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1086 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1087 ins->inst_basereg == last_ins->inst_destbasereg &&
1088 ins->inst_offset == last_ins->inst_offset) {
1089 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1090 ins->opcode = OP_ICONST;
1091 ins->inst_c0 = last_ins->inst_imm;
1092 g_assert_not_reached (); // check this rule
1096 case OP_LOADU1_MEMBASE:
1097 case OP_LOADI1_MEMBASE:
1098 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1099 ins->inst_basereg == last_ins->inst_destbasereg &&
1100 ins->inst_offset == last_ins->inst_offset) {
1101 if (ins->dreg == last_ins->sreg1) {
1102 last_ins->next = ins->next;
1106 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1107 ins->opcode = OP_MOVE;
1108 ins->sreg1 = last_ins->sreg1;
1112 case OP_LOADU2_MEMBASE:
1113 case OP_LOADI2_MEMBASE:
1114 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1115 ins->inst_basereg == last_ins->inst_destbasereg &&
1116 ins->inst_offset == last_ins->inst_offset) {
1117 if (ins->dreg == last_ins->sreg1) {
1118 last_ins->next = ins->next;
1122 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1123 ins->opcode = OP_MOVE;
1124 ins->sreg1 = last_ins->sreg1;
1131 ins->opcode = OP_MOVE;
1135 if (ins->dreg == ins->sreg1) {
1137 last_ins->next = ins->next;
1142 * OP_MOVE sreg, dreg
1143 * OP_MOVE dreg, sreg
1145 if (last_ins && last_ins->opcode == OP_MOVE &&
1146 ins->sreg1 == last_ins->dreg &&
1147 ins->dreg == last_ins->sreg1) {
1148 last_ins->next = ins->next;
1157 bb->last_ins = last_ins;
1161 * the branch_b0_table should maintain the order of these
1175 branch_b0_table [] = {
1190 branch_b1_table [] = {
1205 * returns the offset used by spillvar. It allocates a new
1206 * spill variable if necessary.
1209 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1211 MonoSpillInfo **si, *info;
1214 si = &cfg->spill_info;
1216 while (i <= spillvar) {
1219 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1221 info->offset = cfg->stack_offset;
1222 cfg->stack_offset += sizeof (gpointer);
1226 return (*si)->offset;
1232 g_assert_not_reached ();
1237 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1239 MonoSpillInfo **si, *info;
1242 si = &cfg->spill_info_float;
1244 while (i <= spillvar) {
1247 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1249 cfg->stack_offset += 7;
1250 cfg->stack_offset &= ~7;
1251 info->offset = cfg->stack_offset;
1252 cfg->stack_offset += sizeof (double);
1256 return (*si)->offset;
1262 g_assert_not_reached ();
1267 #define DEBUG(a) if (cfg->verbose_level > 1) a
1269 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1270 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 14)
1279 static const char*const * ins_spec = ppcg4;
1282 print_ins (int i, MonoInst *ins)
1284 const char *spec = ins_spec [ins->opcode];
1285 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1286 if (spec [MONO_INST_DEST]) {
1287 if (ins->dreg >= MONO_MAX_IREGS)
1288 g_print (" R%d <-", ins->dreg);
1290 g_print (" %s <-", mono_arch_regname (ins->dreg));
1292 if (spec [MONO_INST_SRC1]) {
1293 if (ins->sreg1 >= MONO_MAX_IREGS)
1294 g_print (" R%d", ins->sreg1);
1296 g_print (" %s", mono_arch_regname (ins->sreg1));
1298 if (spec [MONO_INST_SRC2]) {
1299 if (ins->sreg2 >= MONO_MAX_IREGS)
1300 g_print (" R%d", ins->sreg2);
1302 g_print (" %s", mono_arch_regname (ins->sreg2));
1304 if (spec [MONO_INST_CLOB])
1305 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1310 print_regtrack (RegTrack *t, int num)
1316 for (i = 0; i < num; ++i) {
1319 if (i >= MONO_MAX_IREGS) {
1320 g_snprintf (buf, sizeof(buf), "R%d", i);
1323 r = mono_arch_regname (i);
1324 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1328 typedef struct InstList InstList;
1336 static inline InstList*
1337 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1339 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1349 * Force the spilling of the variable in the symbolic register 'reg'.
1352 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1357 sel = cfg->rs->iassign [reg];
1358 /*i = cfg->rs->isymbolic [sel];
1359 g_assert (i == reg);*/
1361 spill = ++cfg->spill_count;
1362 cfg->rs->iassign [i] = -spill - 1;
1363 mono_regstate_free_int (cfg->rs, sel);
1364 /* we need to create a spill var and insert a load to sel after the current instruction */
1365 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1367 load->inst_basereg = cfg->frame_reg;
1368 load->inst_offset = mono_spillvar_offset (cfg, spill);
1370 while (ins->next != item->prev->data)
1373 load->next = ins->next;
1375 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1376 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1377 g_assert (i == sel);
1383 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1388 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));
1389 /* exclude the registers in the current instruction */
1390 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1391 if (ins->sreg1 >= MONO_MAX_IREGS)
1392 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1394 regmask &= ~ (1 << ins->sreg1);
1395 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1397 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1398 if (ins->sreg2 >= MONO_MAX_IREGS)
1399 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1401 regmask &= ~ (1 << ins->sreg2);
1402 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1404 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1405 regmask &= ~ (1 << ins->dreg);
1406 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1409 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1410 g_assert (regmask); /* need at least a register we can free */
1412 /* we should track prev_use and spill the register that's farther */
1413 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1414 if (regmask & (1 << i)) {
1416 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1420 i = cfg->rs->isymbolic [sel];
1421 spill = ++cfg->spill_count;
1422 cfg->rs->iassign [i] = -spill - 1;
1423 mono_regstate_free_int (cfg->rs, sel);
1424 /* we need to create a spill var and insert a load to sel after the current instruction */
1425 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1427 load->inst_basereg = cfg->frame_reg;
1428 load->inst_offset = mono_spillvar_offset (cfg, spill);
1430 while (ins->next != item->prev->data)
1433 load->next = ins->next;
1435 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1436 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1437 g_assert (i == sel);
1443 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1448 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));
1449 /* exclude the registers in the current instruction */
1450 if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1451 if (ins->sreg1 >= MONO_MAX_FREGS)
1452 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1454 regmask &= ~ (1 << ins->sreg1);
1455 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1457 if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1458 if (ins->sreg2 >= MONO_MAX_FREGS)
1459 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1461 regmask &= ~ (1 << ins->sreg2);
1462 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1464 if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1465 regmask &= ~ (1 << ins->dreg);
1466 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1469 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1470 g_assert (regmask); /* need at least a register we can free */
1472 /* we should track prev_use and spill the register that's farther */
1473 for (i = 0; i < MONO_MAX_FREGS; ++i) {
1474 if (regmask & (1 << i)) {
1476 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1480 i = cfg->rs->fsymbolic [sel];
1481 spill = ++cfg->spill_count;
1482 cfg->rs->fassign [i] = -spill - 1;
1483 mono_regstate_free_float(cfg->rs, sel);
1484 /* we need to create a spill var and insert a load to sel after the current instruction */
1485 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1487 load->inst_basereg = cfg->frame_reg;
1488 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1490 while (ins->next != item->prev->data)
1493 load->next = ins->next;
1495 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1496 i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1497 g_assert (i == sel);
1503 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1506 MONO_INST_NEW (cfg, copy, OP_MOVE);
1510 copy->next = ins->next;
1513 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1518 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1521 MONO_INST_NEW (cfg, copy, OP_FMOVE);
1525 copy->next = ins->next;
1528 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1533 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1536 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1538 store->inst_destbasereg = cfg->frame_reg;
1539 store->inst_offset = mono_spillvar_offset (cfg, spill);
1541 store->next = ins->next;
1544 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)));
1549 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1552 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1554 store->inst_destbasereg = cfg->frame_reg;
1555 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1557 store->next = ins->next;
1560 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)));
1565 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1568 g_assert (item->next);
1569 prev = item->next->data;
1571 while (prev->next != ins)
1573 to_insert->next = ins;
1574 prev->next = to_insert;
1576 * needed otherwise in the next instruction we can add an ins to the
1577 * end and that would get past this instruction.
1579 item->data = to_insert;
1583 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1585 int val = cfg->rs->iassign [sym_reg];
1589 /* the register gets spilled after this inst */
1592 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1594 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1595 cfg->rs->iassign [sym_reg] = val;
1596 /* add option to store before the instruction for src registers */
1598 create_spilled_store (cfg, spill, val, sym_reg, ins);
1600 cfg->rs->isymbolic [val] = sym_reg;
1604 /* use ppc_r3-ppc_10,ppc_12 as temp registers */
1605 #define PPC_CALLER_REGS ((0xff<<3) | (1<<12) | USE_EXTRA_TEMPS)
1606 #define PPC_CALLER_FREGS (0xff<<2)
1609 * Local register allocation.
1610 * We first scan the list of instructions and we save the liveness info of
1611 * each register (when the register is first used, when it's value is set etc.).
1612 * We also reverse the list of instructions (in the InstList list) because assigning
1613 * registers backwards allows for more tricks to be used.
1616 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1619 MonoRegState *rs = cfg->rs;
1621 RegTrack *reginfo, *reginfof;
1622 RegTrack *reginfo1, *reginfo2, *reginfod;
1623 InstList *tmp, *reversed = NULL;
1625 guint32 src1_mask, src2_mask, dest_mask;
1626 guint32 cur_iregs, cur_fregs;
1630 rs->next_vireg = bb->max_ireg;
1631 rs->next_vfreg = bb->max_freg;
1632 mono_regstate_assign (rs);
1633 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1634 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1635 rs->ifree_mask = PPC_CALLER_REGS;
1636 rs->ffree_mask = PPC_CALLER_FREGS;
1640 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1641 /* forward pass on the instructions to collect register liveness info */
1643 spec = ins_spec [ins->opcode];
1644 DEBUG (print_ins (i, ins));
1645 if (spec [MONO_INST_CLOB] == 'c') {
1646 MonoCallInst * call = (MonoCallInst*)ins;
1649 if (spec [MONO_INST_SRC1]) {
1650 if (spec [MONO_INST_SRC1] == 'f')
1651 reginfo1 = reginfof;
1654 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1655 reginfo1 [ins->sreg1].last_use = i;
1659 if (spec [MONO_INST_SRC2]) {
1660 if (spec [MONO_INST_SRC2] == 'f')
1661 reginfo2 = reginfof;
1664 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1665 reginfo2 [ins->sreg2].last_use = i;
1669 if (spec [MONO_INST_DEST]) {
1670 if (spec [MONO_INST_DEST] == 'f')
1671 reginfod = reginfof;
1674 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1675 reginfod [ins->dreg].killed_in = i;
1676 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1677 reginfod [ins->dreg].last_use = i;
1678 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1679 reginfod [ins->dreg].born_in = i;
1680 if (spec [MONO_INST_DEST] == 'l') {
1681 /* result in eax:edx, the virtual register is allocated sequentially */
1682 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1683 reginfod [ins->dreg + 1].last_use = i;
1684 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1685 reginfod [ins->dreg + 1].born_in = i;
1690 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1695 cur_iregs = PPC_CALLER_REGS;
1696 cur_fregs = PPC_CALLER_FREGS;
1698 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1699 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1702 int prev_dreg, prev_sreg1, prev_sreg2;
1705 spec = ins_spec [ins->opcode];
1706 DEBUG (g_print ("processing:"));
1707 DEBUG (print_ins (i, ins));
1708 /* make the register available for allocation: FIXME add fp reg */
1709 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1710 cur_iregs |= 1 << ins->dreg;
1711 DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1712 } else if (ins->opcode == OP_SETFREG) {
1713 cur_fregs |= 1 << ins->dreg;
1714 DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1715 } else if (spec [MONO_INST_CLOB] == 'c') {
1716 MonoCallInst *cinst = (MonoCallInst*)ins;
1717 DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1718 cur_iregs &= ~cinst->used_iregs;
1719 cur_fregs &= ~cinst->used_fregs;
1720 DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1721 /* registers used by the calling convention are excluded from
1722 * allocation: they will be selectively enabled when they are
1723 * assigned by the special SETREG opcodes.
1726 dest_mask = src1_mask = src2_mask = cur_iregs;
1727 /* update for use with FP regs... */
1728 if (spec [MONO_INST_DEST] == 'f') {
1729 if (ins->dreg >= MONO_MAX_FREGS) {
1730 val = rs->fassign [ins->dreg];
1731 prev_dreg = ins->dreg;
1735 /* the register gets spilled after this inst */
1738 val = mono_regstate_alloc_float (rs, dest_mask);
1740 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1741 rs->fassign [ins->dreg] = val;
1743 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1745 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1746 rs->fsymbolic [val] = prev_dreg;
1748 if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != ppc_f1) {
1749 /* this instruction only outputs to ppc_f3, need to copy */
1750 create_copy_ins_float (cfg, ins->dreg, ppc_f1, ins);
1755 if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1756 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1757 mono_regstate_free_float (rs, ins->dreg);
1759 } else if (ins->dreg >= MONO_MAX_IREGS) {
1760 val = rs->iassign [ins->dreg];
1761 prev_dreg = ins->dreg;
1765 /* the register gets spilled after this inst */
1768 val = mono_regstate_alloc_int (rs, dest_mask);
1770 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1771 rs->iassign [ins->dreg] = val;
1773 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1775 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1776 rs->isymbolic [val] = prev_dreg;
1778 if (spec [MONO_INST_DEST] == 'l') {
1779 int hreg = prev_dreg + 1;
1780 val = rs->iassign [hreg];
1784 /* the register gets spilled after this inst */
1787 val = mono_regstate_alloc_int (rs, dest_mask);
1789 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1790 rs->iassign [hreg] = val;
1792 create_spilled_store (cfg, spill, val, hreg, ins);
1794 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1795 rs->isymbolic [val] = hreg;
1796 /* FIXME:? ins->dreg = val; */
1797 if (ins->dreg == ppc_r4) {
1799 create_copy_ins (cfg, val, ppc_r3, ins);
1800 } else if (ins->dreg == ppc_r3) {
1801 if (val == ppc_r4) {
1803 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1804 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1805 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1807 /* two forced copies */
1808 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1809 create_copy_ins (cfg, val, ppc_r3, ins);
1812 if (val == ppc_r3) {
1813 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1815 /* two forced copies */
1816 create_copy_ins (cfg, val, ppc_r3, ins);
1817 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1820 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
1821 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1822 mono_regstate_free_int (rs, val);
1824 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != ppc_r3 && spec [MONO_INST_CLOB] != 'd') {
1825 /* this instruction only outputs to ppc_r3, need to copy */
1826 create_copy_ins (cfg, ins->dreg, ppc_r3, ins);
1831 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
1832 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1833 mono_regstate_free_int (rs, ins->dreg);
1835 if (spec [MONO_INST_SRC1] == 'f') {
1836 if (ins->sreg1 >= MONO_MAX_FREGS) {
1837 val = rs->fassign [ins->sreg1];
1838 prev_sreg1 = ins->sreg1;
1842 /* the register gets spilled after this inst */
1845 //g_assert (val == -1); /* source cannot be spilled */
1846 val = mono_regstate_alloc_float (rs, src1_mask);
1848 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1849 rs->fassign [ins->sreg1] = val;
1850 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1852 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
1853 insert_before_ins (ins, tmp, store);
1856 rs->fsymbolic [val] = prev_sreg1;
1861 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
1862 val = rs->iassign [ins->sreg1];
1863 prev_sreg1 = ins->sreg1;
1867 /* the register gets spilled after this inst */
1870 if (0 && ins->opcode == OP_MOVE) {
1872 * small optimization: the dest register is already allocated
1873 * but the src one is not: we can simply assign the same register
1874 * here and peephole will get rid of the instruction later.
1875 * This optimization may interfere with the clobbering handling:
1876 * it removes a mov operation that will be added again to handle clobbering.
1877 * There are also some other issues that should with make testjit.
1879 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1880 val = rs->iassign [ins->sreg1] = ins->dreg;
1881 //g_assert (val >= 0);
1882 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1884 //g_assert (val == -1); /* source cannot be spilled */
1885 val = mono_regstate_alloc_int (rs, src1_mask);
1887 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1888 rs->iassign [ins->sreg1] = val;
1889 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1892 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1893 insert_before_ins (ins, tmp, store);
1896 rs->isymbolic [val] = prev_sreg1;
1901 if (spec [MONO_INST_SRC2] == 'f') {
1902 if (ins->sreg2 >= MONO_MAX_FREGS) {
1903 val = rs->fassign [ins->sreg2];
1904 prev_sreg2 = ins->sreg2;
1908 /* the register gets spilled after this inst */
1911 val = mono_regstate_alloc_float (rs, src2_mask);
1913 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1914 rs->fassign [ins->sreg2] = val;
1915 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1917 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
1919 rs->fsymbolic [val] = prev_sreg2;
1924 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
1925 val = rs->iassign [ins->sreg2];
1926 prev_sreg2 = ins->sreg2;
1930 /* the register gets spilled after this inst */
1933 val = mono_regstate_alloc_int (rs, src2_mask);
1935 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1936 rs->iassign [ins->sreg2] = val;
1937 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1939 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1941 rs->isymbolic [val] = prev_sreg2;
1947 if (spec [MONO_INST_CLOB] == 'c') {
1949 guint32 clob_mask = PPC_CALLER_REGS;
1950 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1952 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1953 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1957 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1958 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1959 mono_regstate_free_int (rs, ins->sreg1);
1961 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1962 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1963 mono_regstate_free_int (rs, ins->sreg2);
1966 //DEBUG (print_ins (i, ins));
1972 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
1974 /* sreg is a float, dreg is an integer reg. ppc_f1 is used a scratch */
1975 ppc_fctiwz (code, ppc_f1, sreg);
1976 ppc_stfd (code, ppc_f1, -8, ppc_sp);
1977 ppc_lwz (code, dreg, -4, ppc_sp);
1980 ppc_andid (code, dreg, dreg, 0xff);
1982 ppc_andid (code, dreg, dreg, 0xffff);
1985 ppc_extsb (code, dreg, dreg);
1987 ppc_extsh (code, dreg, dreg);
1992 static unsigned char*
1993 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1996 int sreg = tree->sreg1;
1997 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1998 if (tree->flags & MONO_INST_INIT) {
2000 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2001 x86_push_reg (code, X86_EAX);
2004 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2005 x86_push_reg (code, X86_ECX);
2008 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2009 x86_push_reg (code, X86_EDI);
2013 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2014 if (sreg != X86_ECX)
2015 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2016 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2018 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2020 x86_prefix (code, X86_REP_PREFIX);
2023 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2024 x86_pop_reg (code, X86_EDI);
2025 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2026 x86_pop_reg (code, X86_ECX);
2027 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2028 x86_pop_reg (code, X86_EAX);
2041 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2044 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2045 PatchData *pdata = (PatchData*)user_data;
2046 guchar *code = data;
2047 guint32 *thunks = data;
2048 guint32 *endthunks = (guint32*)(code + bsize);
2053 if (!pdata->absolute) {
2054 g_assert (!is_call_imm (pdata->target - pdata->code));
2055 /* make sure a jump is possible from the code to the thunk area */
2056 i = pdata->code - code;
2057 if (!is_call_imm (i))
2059 i = pdata->code + csize - code;
2060 if (!is_call_imm (i))
2064 templ = (guchar*)load;
2065 ppc_lis (templ, ppc_r0, (guint32)(pdata->target) >> 16);
2066 ppc_ori (templ, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2068 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2069 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2070 while (thunks < endthunks) {
2071 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2072 if ((thunks [0] == load [0]) && (thunks [1] == load [1])) {
2073 ppc_patch (pdata->code, (guchar*)thunks);
2074 mono_arch_flush_icache (pdata->code, 4);
2077 } else if ((thunks [0] == 0) && (thunks [1] == 0)) {
2078 /* found a free slot instead: emit thunk */
2079 code = (guchar*)thunks;
2080 ppc_lis (code, ppc_r0, (guint32)(pdata->target) >> 16);
2081 ppc_ori (code, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2082 ppc_mtctr (code, ppc_r0);
2083 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2084 mono_arch_flush_icache ((guchar*)thunks, 16);
2086 ppc_patch (pdata->code, (guchar*)thunks);
2087 mono_arch_flush_icache (pdata->code, 4);
2091 /* skip 16 bytes, the size of the thunk */
2095 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2101 handle_thunk (int absolute, guchar *code, guchar *target) {
2102 MonoDomain *domain = mono_domain_get ();
2106 pdata.target = target;
2107 pdata.absolute = absolute;
2110 mono_domain_lock (domain);
2111 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2114 /* this uses the first available slot */
2116 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2118 mono_domain_unlock (domain);
2120 if (pdata.found != 1)
2121 g_print ("thunk failed for %p from %p\n", target, code);
2122 g_assert (pdata.found == 1);
2126 ppc_patch (guchar *code, guchar *target)
2128 guint32 ins = *(guint32*)code;
2129 guint32 prim = ins >> 26;
2132 // g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2136 gint diff = (gint)target;
2137 if ((diff < -33554432) || (diff > 33554431)) {
2138 diff = target - code;
2139 if (is_call_imm (diff)) {
2140 handle_thunk (TRUE, code, target);
2143 /* change it to relative */
2146 ins = prim << 26 | (ins & 3);
2147 diff &= ~0xfc000003;
2150 gint diff = target - code;
2151 if (is_call_imm (target)) {
2152 /* we change it into an absolute reference */
2153 ins = prim << 26 | (ins & 3) | 2;
2154 diff = (gint)target;
2155 diff &= ~0xfc000003;
2157 *(guint32*)code = ins;
2160 if (!is_call_imm (diff)) {
2161 handle_thunk (FALSE, code, target);
2164 ins = prim << 26 | (ins & 3);
2165 diff &= ~0xfc000003;
2168 *(guint32*)code = ins;
2169 } else if (prim == 16) {
2172 guint32 li = (guint32)target;
2173 ins = (ins & 0xffff0000) | (ins & 3);
2174 ovf = li & 0xffff0000;
2175 if (ovf != 0 && ovf != 0xffff0000)
2176 g_assert_not_reached ();
2179 // FIXME: assert the top bits of li are 0
2181 gint diff = target - code;
2182 ins = (ins & 0xffff0000) | (ins & 3);
2183 ovf = diff & 0xffff0000;
2184 if (ovf != 0 && ovf != 0xffff0000)
2185 g_assert_not_reached ();
2189 *(guint32*)code = ins;
2191 g_assert_not_reached ();
2193 // g_print ("patched with 0x%08x\n", ins);
2197 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2202 guint8 *code = cfg->native_code + cfg->code_len;
2203 MonoInst *last_ins = NULL;
2204 guint last_offset = 0;
2207 if (cfg->opt & MONO_OPT_PEEPHOLE)
2208 peephole_pass (cfg, bb);
2210 /* we don't align basic blocks of loops on ppc */
2212 if (cfg->verbose_level > 2)
2213 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2215 cpos = bb->max_offset;
2217 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2218 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2219 //g_assert (!mono_compile_aot);
2222 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2223 /* this is not thread save, but good enough */
2224 /* fixme: howto handle overflows? */
2225 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2230 offset = code - cfg->native_code;
2232 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2234 if (offset > (cfg->code_size - max_len - 16)) {
2235 cfg->code_size *= 2;
2236 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2237 code = cfg->native_code + offset;
2239 // if (ins->cil_code)
2240 // g_print ("cil code\n");
2242 switch (ins->opcode) {
2244 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2245 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
2248 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2249 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
2251 case OP_STOREI1_MEMBASE_IMM:
2252 ppc_li (code, ppc_r11, ins->inst_imm);
2253 g_assert (ppc_is_imm16 (ins->inst_offset));
2254 ppc_stb (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2256 case OP_STOREI2_MEMBASE_IMM:
2257 ppc_li (code, ppc_r11, ins->inst_imm);
2258 g_assert (ppc_is_imm16 (ins->inst_offset));
2259 ppc_sth (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2261 case OP_STORE_MEMBASE_IMM:
2262 case OP_STOREI4_MEMBASE_IMM:
2263 ppc_load (code, ppc_r11, ins->inst_imm);
2264 g_assert (ppc_is_imm16 (ins->inst_offset));
2265 ppc_stw (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2267 case OP_STOREI1_MEMBASE_REG:
2268 g_assert (ppc_is_imm16 (ins->inst_offset));
2269 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2271 case OP_STOREI2_MEMBASE_REG:
2272 g_assert (ppc_is_imm16 (ins->inst_offset));
2273 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2275 case OP_STORE_MEMBASE_REG:
2276 case OP_STOREI4_MEMBASE_REG:
2277 g_assert (ppc_is_imm16 (ins->inst_offset));
2278 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2283 g_assert_not_reached ();
2284 //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2287 g_assert_not_reached ();
2288 //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2289 //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2291 case OP_LOAD_MEMBASE:
2292 case OP_LOADI4_MEMBASE:
2293 case OP_LOADU4_MEMBASE:
2294 if (ppc_is_imm16 (ins->inst_offset)) {
2295 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2297 ppc_load (code, ppc_r11, ins->inst_offset);
2298 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2301 case OP_LOADU1_MEMBASE:
2302 g_assert (ppc_is_imm16 (ins->inst_offset));
2303 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2305 case OP_LOADI1_MEMBASE:
2306 g_assert (ppc_is_imm16 (ins->inst_offset));
2307 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2308 ppc_extsb (code, ins->dreg, ins->dreg);
2310 case OP_LOADU2_MEMBASE:
2311 g_assert (ppc_is_imm16 (ins->inst_offset));
2312 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2314 case OP_LOADI2_MEMBASE:
2315 g_assert (ppc_is_imm16 (ins->inst_offset));
2316 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2319 ppc_extsb (code, ins->dreg, ins->sreg1);
2322 ppc_extsh (code, ins->dreg, ins->sreg1);
2325 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
2328 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
2332 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2333 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2334 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN)))
2335 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2337 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2339 case OP_COMPARE_IMM:
2341 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2342 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2343 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN))) {
2344 if (ppc_is_uimm16 (ins->inst_imm)) {
2345 ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2347 ppc_load (code, ppc_r11, ins->inst_imm);
2348 ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2351 if (ppc_is_imm16 (ins->inst_imm)) {
2352 ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2354 ppc_load (code, ppc_r11, ins->inst_imm);
2355 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2359 case OP_X86_TEST_NULL:
2360 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2366 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2369 ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2372 ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2375 if (ppc_is_imm16 (ins->inst_imm)) {
2376 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2378 ppc_load (code, ppc_r11, ins->inst_imm);
2379 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2383 ppc_load (code, ppc_r11, ins->inst_imm);
2384 ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2387 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2390 ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2393 ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2396 // we add the negated value
2397 if (ppc_is_imm16 (-ins->inst_imm))
2398 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2400 ppc_load (code, ppc_r11, ins->inst_imm);
2401 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2405 ppc_load (code, ppc_r11, ins->inst_imm);
2406 ppc_subfe (code, ins->dreg, ins->sreg2, ppc_r11);
2409 g_assert (ppc_is_imm16 (ins->inst_imm));
2410 ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2413 ppc_subfze (code, ins->dreg, ins->sreg1);
2416 /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2417 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2420 if (!(ins->inst_imm & 0xffff0000)) {
2421 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2422 } else if (!(ins->inst_imm & 0xffff)) {
2423 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2425 ppc_load (code, ppc_r11, ins->inst_imm);
2426 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2430 ppc_divw (code, ins->dreg, ins->sreg1, ins->sreg2);
2433 ppc_divwu (code, ins->dreg, ins->sreg1, ins->sreg2);
2436 ppc_load (code, ppc_r11, ins->inst_imm);
2437 ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2440 ppc_divw (code, ppc_r11, ins->sreg1, ins->sreg2);
2441 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2442 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2445 ppc_divwu (code, ppc_r11, ins->sreg1, ins->sreg2);
2446 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2447 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2450 ppc_load (code, ppc_r11, ins->inst_imm);
2451 ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2452 ppc_mullw (code, ins->dreg, ins->dreg, ppc_r11);
2453 ppc_subf (code, ins->dreg, ins->dreg, ins->sreg1);
2456 ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2459 if (!(ins->inst_imm & 0xffff0000)) {
2460 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2461 } else if (!(ins->inst_imm & 0xffff)) {
2462 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2464 ppc_load (code, ppc_r11, ins->inst_imm);
2465 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2469 ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2472 if (!(ins->inst_imm & 0xffff0000)) {
2473 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2474 } else if (!(ins->inst_imm & 0xffff)) {
2475 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2477 ppc_load (code, ppc_r11, ins->inst_imm);
2478 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2482 ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2485 ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2486 //ppc_load (code, ppc_r11, ins->inst_imm);
2487 //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2490 ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2493 // there is also ppc_srawi
2494 //ppc_load (code, ppc_r11, ins->inst_imm);
2495 //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2496 ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2499 ppc_load (code, ppc_r11, ins->inst_imm);
2500 ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);
2501 //ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0xf)), (ins->inst_imm & 0xf), 31);
2504 ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2507 ppc_not (code, ins->dreg, ins->sreg1);
2510 ppc_neg (code, ins->dreg, ins->sreg1);
2513 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2516 ppc_load (code, ppc_r11, ins->inst_imm);
2517 ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2520 ppc_mullwo (code, ins->dreg, ins->sreg1, ins->sreg2);
2521 ppc_mcrxr (code, 0);
2522 EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2524 case CEE_MUL_OVF_UN:
2525 /* we first multiply to get the high word and compare to 0
2526 * to set the flags, then the result is discarded and then
2527 * we multiply to get the lower * bits result
2529 ppc_mulhwu (code, ppc_r0, ins->sreg1, ins->sreg2);
2530 ppc_cmpi (code, 0, 0, ppc_r0, 0);
2531 EMIT_COND_SYSTEM_EXCEPTION (CEE_BNE_UN - CEE_BEQ, ins->inst_p1);
2532 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2536 ppc_load (code, ins->dreg, ins->inst_c0);
2539 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2540 ppc_lis (code, ins->dreg, 0);
2541 ppc_ori (code, ins->dreg, ins->dreg, 0);
2547 ppc_mr (code, ins->dreg, ins->sreg1);
2550 int saved = ins->sreg1;
2551 if (ins->sreg1 == ppc_r3) {
2552 ppc_mr (code, ppc_r0, ins->sreg1);
2555 if (ins->sreg2 != ppc_r3)
2556 ppc_mr (code, ppc_r3, ins->sreg2);
2557 if (saved != ppc_r4)
2558 ppc_mr (code, ppc_r4, saved);
2563 ppc_fmr (code, ins->dreg, ins->sreg1);
2565 case OP_FCONV_TO_R4:
2566 ppc_frsp (code, ins->dreg, ins->sreg1);
2572 * Keep in sync with mono_arch_emit_epilog
2574 g_assert (!cfg->method->save_lmf);
2575 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2576 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
2577 ppc_mtlr (code, ppc_r0);
2579 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2580 if (!cfg->method->save_lmf) {
2581 for (i = 13; i < 32; ++i) {
2582 if (cfg->used_int_regs & (1 << i)) {
2584 ppc_lwz (code, i, -pos, cfg->frame_reg);
2588 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2593 /* ensure ins->sreg1 is not NULL */
2594 ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2601 call = (MonoCallInst*)ins;
2602 if (ins->flags & MONO_INST_HAS_METHOD)
2603 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2605 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2611 case OP_VOIDCALL_REG:
2613 ppc_mtlr (code, ins->sreg1);
2616 case OP_FCALL_MEMBASE:
2617 case OP_LCALL_MEMBASE:
2618 case OP_VCALL_MEMBASE:
2619 case OP_VOIDCALL_MEMBASE:
2620 case OP_CALL_MEMBASE:
2621 ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2622 ppc_mtlr (code, ppc_r0);
2626 g_assert_not_reached ();
2629 /* keep alignment */
2630 ppc_addi (code, ppc_r0, ins->sreg1, PPC_STACK_ALIGNMENT-1);
2631 ppc_rlwinm (code, ppc_r0, ppc_r0, 0, 0, 27);
2632 ppc_lwz (code, ppc_r11, 0, ppc_sp);
2633 ppc_neg (code, ppc_r0, ppc_r0);
2634 ppc_stwux (code, ppc_r11, ppc_r0, ppc_sp);
2635 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, PPC_BR_ALWAYS, 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 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2727 /* move XER [0-3] (SO, OV, CA) into CR
2728 * this translates to LT, GT, EQ.
2729 * FIXME: test for all the conditions occourring
2731 ppc_mcrxr (code, 0);
2732 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, ins->inst_p1);
2734 case OP_COND_EXC_OV:
2735 ppc_mcrxr (code, 0);
2736 EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2738 case OP_COND_EXC_NC:
2739 case OP_COND_EXC_NO:
2740 g_assert_not_reached ();
2752 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2755 /* floating point opcodes */
2757 ppc_load (code, ppc_r11, ins->inst_p0);
2758 ppc_lfd (code, ins->dreg, 0, ppc_r11);
2761 ppc_load (code, ppc_r11, ins->inst_p0);
2762 ppc_lfs (code, ins->dreg, 0, ppc_r11);
2764 case OP_STORER8_MEMBASE_REG:
2765 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2767 case OP_LOADR8_MEMBASE:
2768 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2770 case OP_STORER4_MEMBASE_REG:
2771 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2773 case OP_LOADR4_MEMBASE:
2774 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2776 case CEE_CONV_R_UN: {
2777 static const guint64 adjust_val = 0x4330000000000000UL;
2778 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2779 ppc_stw (code, ppc_r0, -8, ppc_sp);
2780 ppc_stw (code, ins->sreg1, -4, ppc_sp);
2781 ppc_load (code, ppc_r11, &adjust_val);
2782 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2783 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2786 case CEE_CONV_R4: /* FIXME: change precision */
2788 static const guint64 adjust_val = 0x4330000080000000UL;
2789 // addis is special for ppc_r0
2790 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2791 ppc_stw (code, ppc_r0, -8, ppc_sp);
2792 ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
2793 ppc_stw (code, ppc_r11, -4, ppc_sp);
2794 ppc_lfd (code, ins->dreg, -8, ppc_sp);
2795 ppc_load (code, ppc_r11, &adjust_val);
2796 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2797 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2800 case OP_X86_FP_LOAD_I8:
2801 g_assert_not_reached ();
2802 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
2804 case OP_X86_FP_LOAD_I4:
2805 g_assert_not_reached ();
2806 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
2808 case OP_FCONV_TO_I1:
2809 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
2811 case OP_FCONV_TO_U1:
2812 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
2814 case OP_FCONV_TO_I2:
2815 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
2817 case OP_FCONV_TO_U2:
2818 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
2820 case OP_FCONV_TO_I4:
2822 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
2824 case OP_FCONV_TO_U4:
2826 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
2828 case OP_FCONV_TO_I8:
2829 case OP_FCONV_TO_U8:
2830 g_assert_not_reached ();
2831 /* Implemented as helper calls */
2833 case OP_LCONV_TO_R_UN:
2834 g_assert_not_reached ();
2835 /* Implemented as helper calls */
2837 case OP_LCONV_TO_OVF_I: {
2838 ppc_mr (code, ins->dreg, ins->sreg1);
2839 /* FIXME: emit exception if needed */
2843 ppc_fsqrtd (code, ins->dreg, ins->sreg1);
2846 ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
2849 ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
2852 ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
2855 ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
2858 ppc_fneg (code, ins->dreg, ins->sreg1);
2862 g_assert_not_reached ();
2865 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2868 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2869 ppc_li (code, ins->dreg, 0);
2870 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2871 ppc_li (code, ins->dreg, 1);
2874 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2875 ppc_li (code, ins->dreg, 1);
2876 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2877 ppc_li (code, ins->dreg, 0);
2880 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2881 ppc_li (code, ins->dreg, 1);
2882 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2883 ppc_li (code, ins->dreg, 0);
2886 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2887 ppc_li (code, ins->dreg, 1);
2888 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2889 ppc_li (code, ins->dreg, 0);
2892 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2893 ppc_li (code, ins->dreg, 1);
2894 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2895 ppc_li (code, ins->dreg, 0);
2898 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
2901 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
2904 EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
2907 EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
2910 EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
2913 EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
2916 EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
2919 EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
2922 EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
2925 EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
2927 case CEE_CKFINITE: {
2928 ppc_stfd (code, ins->sreg1, -8, ppc_sp);
2929 ppc_lwz (code, ppc_r0, -8, ppc_sp);
2930 ppc_rlwinm (code, ppc_r0, ppc_r0, 0, 1, 31);
2931 ppc_xoris (code, ppc_r11, ppc_r0, 0x7ff0);
2932 ppc_neg (code, ppc_r0, ppc_r11);
2933 ppc_rlwinm (code, ppc_r0, ppc_r0, 1, 31, 31);
2934 g_assert_not_reached ();
2935 /*x86_push_reg (code, X86_EAX);
2938 x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2939 x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2940 x86_pop_reg (code, X86_EAX);
2941 EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");*/
2945 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2946 g_assert_not_reached ();
2949 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2950 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2951 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2952 g_assert_not_reached ();
2958 last_offset = offset;
2963 cfg->code_len = code - cfg->native_code;
2967 mono_arch_register_lowlevel_calls (void)
2971 #define patch_lis_ori(ip,val) do {\
2972 guint16 *__lis_ori = (guint16*)(ip); \
2973 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
2974 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
2978 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
2980 MonoJumpInfo *patch_info;
2982 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2983 unsigned char *ip = patch_info->ip.i + code;
2984 const unsigned char *target = NULL;
2986 switch (patch_info->type) {
2987 case MONO_PATCH_INFO_BB:
2988 target = patch_info->data.bb->native_offset + code;
2990 case MONO_PATCH_INFO_ABS:
2991 target = patch_info->data.target;
2993 case MONO_PATCH_INFO_LABEL:
2994 target = patch_info->data.inst->inst_c0 + code;
2996 case MONO_PATCH_INFO_IP:
2997 patch_lis_ori (ip, ip);
2999 case MONO_PATCH_INFO_METHOD_REL:
3000 g_assert_not_reached ();
3001 *((gpointer *)(ip)) = code + patch_info->data.offset;
3003 case MONO_PATCH_INFO_INTERNAL_METHOD: {
3004 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
3006 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
3007 g_assert_not_reached ();
3009 target = mono_icall_get_wrapper (mi);
3012 case MONO_PATCH_INFO_METHOD_JUMP: {
3015 /* get the trampoline to the method from the domain */
3016 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
3017 if (!domain->jump_target_hash)
3018 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
3019 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
3020 list = g_slist_prepend (list, ip);
3021 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
3024 case MONO_PATCH_INFO_METHOD:
3025 if (patch_info->data.method == method) {
3028 /* get the trampoline to the method from the domain */
3029 target = mono_arch_create_jit_trampoline (patch_info->data.method);
3032 case MONO_PATCH_INFO_SWITCH: {
3033 gpointer *table = (gpointer *)patch_info->data.target;
3036 // FIXME: inspect code to get the register
3037 ppc_load (ip, ppc_r11, patch_info->data.target);
3038 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3040 for (i = 0; i < patch_info->table_size; i++) {
3041 table [i] = (int)patch_info->data.table [i] + code;
3043 /* we put into the table the absolute address, no need for ppc_patch in this case */
3046 case MONO_PATCH_INFO_METHODCONST:
3047 case MONO_PATCH_INFO_CLASS:
3048 case MONO_PATCH_INFO_IMAGE:
3049 case MONO_PATCH_INFO_FIELD:
3050 /* from OP_AOTCONST : lis + ori */
3051 patch_lis_ori (ip, patch_info->data.target);
3053 case MONO_PATCH_INFO_R4:
3054 case MONO_PATCH_INFO_R8:
3055 g_assert_not_reached ();
3056 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3058 case MONO_PATCH_INFO_IID:
3059 mono_class_init (patch_info->data.klass);
3060 patch_lis_ori (ip, patch_info->data.klass->interface_id);
3062 case MONO_PATCH_INFO_VTABLE:
3063 target = mono_class_vtable (domain, patch_info->data.klass);
3064 patch_lis_ori (ip, target);
3066 case MONO_PATCH_INFO_CLASS_INIT:
3067 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
3069 case MONO_PATCH_INFO_SFLDA: {
3070 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
3071 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
3072 /* Done by the generated code */
3076 mono_runtime_class_init (vtable);
3078 target = (char*)vtable->data + patch_info->data.field->offset;
3079 patch_lis_ori (ip, target);
3082 case MONO_PATCH_INFO_EXC_NAME:
3083 g_assert_not_reached ();
3084 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3086 case MONO_PATCH_INFO_LDSTR:
3087 target = mono_ldstr (domain, patch_info->data.token->image,
3088 mono_metadata_token_index (patch_info->data.token->token));
3089 patch_lis_ori (ip, target);
3091 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
3093 MonoClass *handle_class;
3095 handle = mono_ldtoken (patch_info->data.token->image,
3096 patch_info->data.token->token, &handle_class, NULL);
3097 mono_class_init (handle_class);
3098 mono_class_init (mono_class_from_mono_type (handle));
3100 patch_lis_ori (ip, handle);
3103 case MONO_PATCH_INFO_LDTOKEN: {
3105 MonoClass *handle_class;
3107 handle = mono_ldtoken (patch_info->data.token->image,
3108 patch_info->data.token->token, &handle_class, NULL);
3109 mono_class_init (handle_class);
3111 patch_lis_ori (ip, handle);
3114 case MONO_PATCH_INFO_BB_OVF:
3115 case MONO_PATCH_INFO_EXC_OVF:
3116 /* everything is dealt with at epilog output time */
3119 g_assert_not_reached ();
3121 ppc_patch (ip, target);
3126 mono_arch_max_epilog_size (MonoCompile *cfg)
3128 int exc_count = 0, max_epilog_size = 16 + 20*4;
3129 MonoJumpInfo *patch_info;
3131 if (cfg->method->save_lmf)
3132 max_epilog_size += 128;
3134 if (mono_jit_trace_calls != NULL)
3135 max_epilog_size += 50;
3137 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3138 max_epilog_size += 50;
3140 /* count the number of exception infos */
3143 * make sure we have enough space for exceptions
3144 * 24 is the simulated call to throw_exception_by_name
3146 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3147 if (patch_info->type == MONO_PATCH_INFO_EXC)
3148 max_epilog_size += 24;
3149 else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
3150 max_epilog_size += 12;
3151 else if (patch_info->type == MONO_PATCH_INFO_EXC_OVF)
3152 max_epilog_size += 12;
3155 return max_epilog_size;
3159 mono_arch_emit_prolog (MonoCompile *cfg)
3161 MonoMethod *method = cfg->method;
3163 MonoMethodSignature *sig;
3165 int alloc_size, pos, max_offset, i;
3170 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3173 cfg->code_size = 256;
3174 code = cfg->native_code = g_malloc (cfg->code_size);
3176 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3177 ppc_mflr (code, ppc_r0);
3178 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3180 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
3181 cfg->used_int_regs |= 1 << 31;
3183 cfg->used_int_regs |= USE_EXTRA_TEMPS;
3185 alloc_size = cfg->stack_offset;
3187 /* reserve room to save return value */
3191 if (!method->save_lmf) {
3192 for (i = 13; i < 32; ++i) {
3193 if (cfg->used_int_regs & (1 << i)) {
3194 pos += sizeof (gulong);
3195 ppc_stw (code, i, -pos, ppc_sp);
3198 /*for (i = 14; i < 32; ++i) {
3199 if (cfg->used_float_regs & (1 << i)) {
3200 pos += sizeof (gdouble);
3201 ppc_stfd (code, i, -pos, ppc_sp);
3206 // align to PPC_STACK_ALIGNMENT bytes
3207 if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
3208 alloc_size += PPC_STACK_ALIGNMENT - 1;
3209 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
3212 cfg->stack_usage = alloc_size;
3213 g_assert (ppc_is_imm16 (-alloc_size));
3214 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
3216 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3217 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
3218 ppc_mr (code, ppc_r31, ppc_sp);
3220 /* compute max_offset in order to use short forward jumps
3221 * we always do it on ppc because the immediate displacement
3222 * for jumps is too small
3225 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3226 MonoInst *ins = bb->code;
3227 bb->max_offset = max_offset;
3229 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3233 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3238 /* load arguments allocated to register from the stack */
3239 sig = method->signature;
3242 cinfo = calculate_sizes (sig, sig->pinvoke);
3244 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3245 ArgInfo *ainfo = &cinfo->ret;
3247 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3249 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3250 ArgInfo *ainfo = cinfo->args + i;
3251 inst = cfg->varinfo [pos];
3253 if (inst->opcode == OP_REGVAR) {
3254 if (ainfo->regtype == RegTypeGeneral)
3255 ppc_mr (code, inst->dreg, ainfo->reg);
3256 else if (ainfo->regtype == RegTypeFP)
3257 ppc_fmr (code, inst->dreg, ainfo->reg);
3258 else if (ainfo->regtype == RegTypeBase) {
3259 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3260 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3262 g_assert_not_reached ();
3264 if (cfg->verbose_level > 2)
3265 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3267 /* the argument should be put on the stack: FIXME handle size != word */
3268 if (ainfo->regtype == RegTypeGeneral) {
3269 switch (ainfo->size) {
3271 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3274 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3277 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3278 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3281 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3283 } else if (ainfo->regtype == RegTypeBase) {
3284 /* load the previous stack pointer in r11 */
3285 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3286 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3287 switch (ainfo->size) {
3289 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3292 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3295 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3296 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3297 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3300 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3302 } else if (ainfo->regtype == RegTypeFP) {
3303 if (ainfo->size == 8)
3304 ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3305 else if (ainfo->size == 4)
3306 ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3308 g_assert_not_reached ();
3309 } else if (ainfo->regtype == RegTypeStructByVal) {
3310 int doffset = inst->inst_offset;
3313 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3314 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3315 soffset += sizeof (gpointer);
3316 doffset += sizeof (gpointer);
3318 if (ainfo->vtsize) {
3319 /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3320 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3321 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3322 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3324 } else if (ainfo->regtype == RegTypeStructByAddr) {
3325 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3326 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3328 g_assert_not_reached ();
3333 if (method->save_lmf) {
3335 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3336 (gpointer)"mono_get_lmf_addr");
3338 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3339 pos = PPC_MINIMAL_STACK_SIZE + cfg->param_area;
3342 ppc_addi (code, ppc_r11, ppc_sp, pos);
3343 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3344 /* new_lmf->previous_lmf = *lmf_addr */
3345 ppc_lwz (code, ppc_r0, 0, ppc_r3);
3346 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3347 /* *(lmf_addr) = r11 */
3348 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3349 /* save method info */
3350 ppc_load (code, ppc_r0, method);
3351 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3352 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3353 /* save the current IP */
3354 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3355 ppc_load (code, ppc_r0, 0x01010101);
3356 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3357 ppc_stmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3358 for (i = 14; i < 32; i++) {
3359 ppc_stfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3364 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3366 cfg->code_len = code - cfg->native_code;
3373 mono_arch_emit_epilog (MonoCompile *cfg)
3375 MonoJumpInfo *patch_info;
3376 MonoMethod *method = cfg->method;
3381 * Keep in sync with CEE_JMP
3383 code = cfg->native_code + cfg->code_len;
3385 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3386 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3392 if (method->save_lmf) {
3393 int ofst = PPC_MINIMAL_STACK_SIZE + cfg->param_area;
3396 ppc_addi (code, ppc_r11, cfg->frame_reg, ofst);
3397 /* r5 = previous_lmf */
3398 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3400 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3401 /* *(lmf_addr) = previous_lmf */
3402 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3404 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3406 for (i = 14; i < 32; i++) {
3407 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3411 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3412 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3413 ppc_mtlr (code, ppc_r0);
3415 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3416 if (!method->save_lmf) {
3417 for (i = 13; i < 32; ++i) {
3418 if (cfg->used_int_regs & (1 << i)) {
3420 ppc_lwz (code, i, -pos, cfg->frame_reg);
3426 /* add code to raise exceptions */
3427 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3428 switch (patch_info->type) {
3429 case MONO_PATCH_INFO_BB_OVF: {
3430 MonoOvfJump *ovfj = patch_info->data.target;
3431 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3432 /* patch the initial jump */
3433 ppc_patch (ip, code);
3434 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3436 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3437 /* jump back to the true target */
3439 ip = ovfj->bb->native_offset + cfg->native_code;
3440 ppc_patch (code - 4, ip);
3443 case MONO_PATCH_INFO_EXC_OVF: {
3444 MonoOvfJump *ovfj = patch_info->data.target;
3445 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3446 /* patch the initial jump */
3447 ppc_patch (ip, code);
3448 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3450 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3451 /* jump back to the true target */
3453 ip = (char*)ovfj->ip + 4;
3454 ppc_patch (code - 4, ip);
3457 case MONO_PATCH_INFO_EXC: {
3458 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3459 ppc_patch (ip, code);
3460 /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3461 ppc_load (code, ppc_r3, patch_info->data.target);
3462 /* simulate a call from ip */
3463 ppc_load (code, ppc_r0, ip + 4);
3464 ppc_mtlr (code, ppc_r0);
3465 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3466 patch_info->data.name = "mono_arch_throw_exception_by_name";
3467 patch_info->ip.i = code - cfg->native_code;
3477 cfg->code_len = code - cfg->native_code;
3479 g_assert (cfg->code_len < cfg->code_size);
3484 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3489 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3494 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3496 int this_dreg = ppc_r3;
3501 /* add the this argument */
3502 if (this_reg != -1) {
3504 MONO_INST_NEW (cfg, this, OP_SETREG);
3505 this->type = this_type;
3506 this->sreg1 = this_reg;
3507 this->dreg = this_dreg;
3508 mono_bblock_add_inst (cfg->cbb, this);
3513 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3514 vtarg->type = STACK_MP;
3515 vtarg->sreg1 = vt_reg;
3516 vtarg->dreg = ppc_r3;
3517 mono_bblock_add_inst (cfg->cbb, vtarg);
3522 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3524 /* optional instruction, need to detect it
3525 if (cmethod->klass == mono_defaults.math_class) {
3526 if (strcmp (cmethod->name, "Sqrt") == 0)
3534 mono_arch_print_tree (MonoInst *tree, int arity)