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 (guint8 *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;
149 is_regsize_var (MonoType *t) {
159 case MONO_TYPE_OBJECT:
160 case MONO_TYPE_STRING:
161 case MONO_TYPE_CLASS:
162 case MONO_TYPE_SZARRAY:
163 case MONO_TYPE_ARRAY:
165 case MONO_TYPE_VALUETYPE:
166 if (t->data.klass->enumtype)
167 return is_regsize_var (t->data.klass->enum_basetype);
174 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
179 for (i = 0; i < cfg->num_varinfo; i++) {
180 MonoInst *ins = cfg->varinfo [i];
181 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
184 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
187 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
190 /* we can only allocate 32 bit values */
191 if (is_regsize_var (ins->inst_vtype)) {
192 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
193 g_assert (i == vmv->idx);
194 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
201 #define USE_EXTRA_TEMPS ((1<<30) | (1<<29))
202 //#define USE_EXTRA_TEMPS 0
205 mono_arch_get_global_int_regs (MonoCompile *cfg)
209 if (cfg->frame_reg != ppc_sp)
214 for (i = 13; i < top; ++i)
215 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
221 * mono_arch_regalloc_cost:
223 * Return the cost, in number of memory references, of the action of
224 * allocating the variable VMV into a register during global register
228 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
234 // code from ppc/tramp.c, try to keep in sync
235 #define MIN_CACHE_LINE 8
238 mono_arch_flush_icache (guint8 *code, gint size)
244 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
245 asm ("dcbst 0,%0;" : : "r"(p) : "memory");
249 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
250 asm ("icbi 0,%0; sync;" : : "r"(p) : "memory");
256 #define NOT_IMPLEMENTED(x) \
257 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
260 #define ALWAYS_ON_STACK(s) s
261 #define FP_ALSO_IN_REG(s) s
263 #define ALWAYS_ON_STACK(s)
264 #define FP_ALSO_IN_REG(s) s
265 #define ALIGN_DOUBLES
278 guint16 vtsize; /* in param area */
280 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
281 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
295 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
298 if (*gr >= 3 + PPC_NUM_REG_ARGS) {
299 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
300 ainfo->reg = ppc_sp; /* in the caller */
301 ainfo->regtype = RegTypeBase;
304 ALWAYS_ON_STACK (*stack_size += 4);
308 if (*gr >= 3 + PPC_NUM_REG_ARGS - 1) {
310 //*stack_size += (*stack_size % 8);
312 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
313 ainfo->reg = ppc_sp; /* in the caller */
314 ainfo->regtype = RegTypeBase;
321 ALWAYS_ON_STACK (*stack_size += 8);
330 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
333 int n = sig->hasthis + sig->param_count;
335 guint32 stack_size = 0;
336 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
338 fr = PPC_FIRST_FPARG_REG;
339 gr = PPC_FIRST_ARG_REG;
341 /* FIXME: handle returning a struct */
342 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
343 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
344 cinfo->struct_ret = PPC_FIRST_ARG_REG;
349 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
352 DEBUG(printf("params: %d\n", sig->param_count));
353 for (i = 0; i < sig->param_count; ++i) {
354 DEBUG(printf("param %d: ", i));
355 if (sig->params [i]->byref) {
356 DEBUG(printf("byref\n"));
357 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
361 simpletype = sig->params [i]->type;
363 switch (simpletype) {
364 case MONO_TYPE_BOOLEAN:
367 cinfo->args [n].size = 1;
368 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
374 cinfo->args [n].size = 2;
375 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
380 cinfo->args [n].size = 4;
381 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
387 case MONO_TYPE_FNPTR:
388 case MONO_TYPE_CLASS:
389 case MONO_TYPE_OBJECT:
390 case MONO_TYPE_STRING:
391 case MONO_TYPE_SZARRAY:
392 case MONO_TYPE_ARRAY:
393 cinfo->args [n].size = sizeof (gpointer);
394 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
397 case MONO_TYPE_VALUETYPE: {
399 if (sig->params [i]->data.klass->enumtype) {
400 simpletype = sig->params [i]->data.klass->enum_basetype->type;
403 size = mono_class_value_size (sig->params [i]->data.klass, NULL);
404 DEBUG(printf ("load %d bytes struct\n",
405 mono_class_value_size (sig->params [i]->data.klass, NULL)));
406 #if PPC_PASS_STRUCTS_BY_VALUE
408 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
409 cinfo->args [n].regtype = RegTypeStructByVal;
410 if (gr <= PPC_LAST_ARG_REG) {
411 int rest = PPC_LAST_ARG_REG - gr + 1;
412 int n_in_regs = rest >= nwords? nwords: rest;
413 cinfo->args [n].size = n_in_regs;
414 cinfo->args [n].vtsize = nwords - n_in_regs;
415 cinfo->args [n].reg = gr;
418 cinfo->args [n].size = 0;
419 cinfo->args [n].vtsize = nwords;
421 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
422 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
423 stack_size += nwords * sizeof (gpointer);
426 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
427 cinfo->args [n].regtype = RegTypeStructByAddr;
432 case MONO_TYPE_TYPEDBYREF: {
433 int size = sizeof (MonoTypedRef);
434 /* keep in sync or merge with the valuetype case */
435 #if PPC_PASS_STRUCTS_BY_VALUE
437 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
438 cinfo->args [n].regtype = RegTypeStructByVal;
439 if (gr <= PPC_LAST_ARG_REG) {
440 int rest = PPC_LAST_ARG_REG - gr + 1;
441 int n_in_regs = rest >= nwords? nwords: rest;
442 cinfo->args [n].size = n_in_regs;
443 cinfo->args [n].vtsize = nwords - n_in_regs;
444 cinfo->args [n].reg = gr;
447 cinfo->args [n].size = 0;
448 cinfo->args [n].vtsize = nwords;
450 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
451 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
452 stack_size += nwords * sizeof (gpointer);
455 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
456 cinfo->args [n].regtype = RegTypeStructByAddr;
463 cinfo->args [n].size = 8;
464 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
468 cinfo->args [n].size = 4;
470 /* It was 7, now it is 8 in LinuxPPC */
471 if (fr <= PPC_LAST_FPARG_REG) {
472 cinfo->args [n].regtype = RegTypeFP;
473 cinfo->args [n].reg = fr;
475 FP_ALSO_IN_REG (gr ++);
476 ALWAYS_ON_STACK (stack_size += 4);
478 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
479 cinfo->args [n].regtype = RegTypeBase;
480 cinfo->args [n].reg = ppc_sp; /* in the caller*/
486 cinfo->args [n].size = 8;
487 /* It was 7, now it is 8 in LinuxPPC */
488 if (fr <= PPC_LAST_FPARG_REG) {
489 cinfo->args [n].regtype = RegTypeFP;
490 cinfo->args [n].reg = fr;
492 FP_ALSO_IN_REG (gr += 2);
493 ALWAYS_ON_STACK (stack_size += 8);
495 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
496 cinfo->args [n].regtype = RegTypeBase;
497 cinfo->args [n].reg = ppc_sp; /* in the caller*/
503 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
508 simpletype = sig->ret->type;
510 switch (simpletype) {
511 case MONO_TYPE_BOOLEAN:
522 case MONO_TYPE_FNPTR:
523 case MONO_TYPE_CLASS:
524 case MONO_TYPE_OBJECT:
525 case MONO_TYPE_SZARRAY:
526 case MONO_TYPE_ARRAY:
527 case MONO_TYPE_STRING:
528 cinfo->ret.reg = ppc_r3;
532 cinfo->ret.reg = ppc_r3;
536 cinfo->ret.reg = ppc_f1;
537 cinfo->ret.regtype = RegTypeFP;
539 case MONO_TYPE_VALUETYPE:
540 if (sig->ret->data.klass->enumtype) {
541 simpletype = sig->ret->data.klass->enum_basetype->type;
545 case MONO_TYPE_TYPEDBYREF:
549 g_error ("Can't handle as return value 0x%x", sig->ret->type);
553 /* align stack size to 16 */
554 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
555 stack_size = (stack_size + 15) & ~15;
557 cinfo->stack_usage = stack_size;
563 * Set var information according to the calling convention. ppc version.
564 * The locals var stuff should most likely be split in another method.
567 mono_arch_allocate_vars (MonoCompile *m)
569 MonoMethodSignature *sig;
570 MonoMethodHeader *header;
572 int i, offset, size, align, curinst;
573 int frame_reg = ppc_sp;
575 /* allow room for the vararg method args: void* and long/double */
576 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
577 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
578 /* this is bug #60332: remove when #59509 is fixed, so no weird vararg
579 * call convs needs to be handled this way.
581 if (m->flags & MONO_CFG_HAS_VARARGS)
582 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
584 header = ((MonoMethodNormal *)m->method)->header;
587 * We use the frame register also for any method that has
588 * exception clauses. This way, when the handlers are called,
589 * the code will reference local variables using the frame reg instead of
590 * the stack pointer: if we had to restore the stack pointer, we'd
591 * corrupt the method frames that are already on the stack (since
592 * filters get called before stack unwinding happens) when the filter
593 * code would call any method (this also applies to finally etc.).
595 if ((m->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
597 m->frame_reg = frame_reg;
598 if (frame_reg != ppc_sp) {
599 m->used_int_regs |= 1 << frame_reg;
602 sig = m->method->signature;
606 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
607 m->ret->opcode = OP_REGVAR;
608 m->ret->inst_c0 = ppc_r3;
610 /* FIXME: handle long and FP values */
611 switch (sig->ret->type) {
615 m->ret->opcode = OP_REGVAR;
616 m->ret->inst_c0 = ppc_r3;
620 /* local vars are at a positive offset from the stack pointer */
622 * also note that if the function uses alloca, we use ppc_r31
623 * to point at the local variables.
625 offset = PPC_MINIMAL_STACK_SIZE; /* linkage area */
626 /* align the offset to 16 bytes: not sure this is needed here */
628 //offset &= ~(16 - 1);
630 /* add parameter area size for called functions */
631 offset += m->param_area;
635 /* allow room to save the return value */
636 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
639 /* the MonoLMF structure is stored just below the stack pointer */
642 /* this stuff should not be needed on ppc and the new jit,
643 * because a call on ppc to the handlers doesn't change the
644 * stack pointer and the jist doesn't manipulate the stack pointer
645 * for operations involving valuetypes.
647 /* reserve space to store the esp */
648 offset += sizeof (gpointer);
650 /* this is a global constant */
651 mono_exc_esp_offset = offset;
654 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
656 offset += sizeof(gpointer) - 1;
657 offset &= ~(sizeof(gpointer) - 1);
658 inst->inst_offset = offset;
659 inst->opcode = OP_REGOFFSET;
660 inst->inst_basereg = frame_reg;
661 offset += sizeof(gpointer);
663 curinst = m->locals_start;
664 for (i = curinst; i < m->num_varinfo; ++i) {
665 inst = m->varinfo [i];
666 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
669 /* inst->unused indicates native sized value types, this is used by the
670 * pinvoke wrappers when they call functions returning structure */
671 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
672 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
674 size = mono_type_size (inst->inst_vtype, &align);
677 offset &= ~(align - 1);
678 inst->inst_offset = offset;
679 inst->opcode = OP_REGOFFSET;
680 inst->inst_basereg = frame_reg;
682 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
687 inst = m->varinfo [curinst];
688 if (inst->opcode != OP_REGVAR) {
689 inst->opcode = OP_REGOFFSET;
690 inst->inst_basereg = frame_reg;
691 offset += sizeof (gpointer) - 1;
692 offset &= ~(sizeof (gpointer) - 1);
693 inst->inst_offset = offset;
694 offset += sizeof (gpointer);
699 for (i = 0; i < sig->param_count; ++i) {
700 inst = m->varinfo [curinst];
701 if (inst->opcode != OP_REGVAR) {
702 inst->opcode = OP_REGOFFSET;
703 inst->inst_basereg = frame_reg;
704 size = mono_type_size (sig->params [i], &align);
706 offset &= ~(align - 1);
707 inst->inst_offset = offset;
713 /* align the offset to 16 bytes */
718 m->stack_offset = offset;
722 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
723 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
727 * take the arguments and generate the arch-specific
728 * instructions to properly call the function in call.
729 * This includes pushing, moving arguments to the right register
731 * Issue: who does the spilling if needed, and when?
734 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
736 MonoMethodSignature *sig;
741 sig = call->signature;
742 n = sig->param_count + sig->hasthis;
744 cinfo = calculate_sizes (sig, sig->pinvoke);
745 if (cinfo->struct_ret)
746 call->used_iregs |= 1 << cinfo->struct_ret;
748 for (i = 0; i < n; ++i) {
749 ainfo = cinfo->args + i;
750 if (is_virtual && i == 0) {
751 /* the argument will be attached to the call instrucion */
753 call->used_iregs |= 1 << ainfo->reg;
755 MONO_INST_NEW (cfg, arg, OP_OUTARG);
757 arg->cil_code = in->cil_code;
759 arg->type = in->type;
760 /* prepend, we'll need to reverse them later */
761 arg->next = call->out_args;
762 call->out_args = arg;
763 if (ainfo->regtype == RegTypeGeneral) {
764 arg->unused = ainfo->reg;
765 call->used_iregs |= 1 << ainfo->reg;
766 if (arg->type == STACK_I8)
767 call->used_iregs |= 1 << (ainfo->reg + 1);
768 } else if (ainfo->regtype == RegTypeStructByAddr) {
769 /* FIXME: where si the data allocated? */
770 arg->unused = ainfo->reg;
771 call->used_iregs |= 1 << ainfo->reg;
772 } else if (ainfo->regtype == RegTypeStructByVal) {
774 /* mark the used regs */
775 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
776 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
778 arg->opcode = OP_OUTARG_VT;
779 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
780 arg->inst_imm = ainfo->offset;
781 } else if (ainfo->regtype == RegTypeBase) {
782 arg->opcode = OP_OUTARG;
783 arg->unused = ainfo->reg | (ainfo->size << 8);
784 arg->inst_imm = ainfo->offset;
785 } else if (ainfo->regtype == RegTypeFP) {
786 arg->opcode = OP_OUTARG_R8;
787 arg->unused = ainfo->reg;
788 call->used_fregs |= 1 << ainfo->reg;
789 if (ainfo->size == 4) {
790 arg->opcode = OP_OUTARG_R8;
791 /* we reduce the precision */
793 MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
794 conv->inst_left = arg->inst_left;
795 arg->inst_left = conv;*/
798 g_assert_not_reached ();
803 * Reverse the call->out_args list.
806 MonoInst *prev = NULL, *list = call->out_args, *next;
813 call->out_args = prev;
815 call->stack_usage = cinfo->stack_usage;
816 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
817 cfg->flags |= MONO_CFG_HAS_CALLS;
819 * should set more info in call, such as the stack space
820 * used by the args that needs to be added back to esp
828 * Allow tracing to work with this interface (with an optional argument)
832 * This may be needed on some archs or for debugging support.
835 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
837 /* no stack room needed now (may be needed for FASTCALL-trace support) */
839 /* split prolog-epilog requirements? */
840 *code = 50; /* max bytes needed: check this number */
844 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
848 ppc_load (code, ppc_r3, cfg->method);
849 ppc_li (code, ppc_r4, 0); /* NULL ebp for now */
850 ppc_load (code, ppc_r0, func);
851 ppc_mtlr (code, ppc_r0);
865 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
868 int save_mode = SAVE_NONE;
869 MonoMethod *method = cfg->method;
870 int rtype = method->signature->ret->type;
871 int save_offset = PPC_STACK_PARAM_OFFSET + cfg->param_area;
878 /* special case string .ctor icall */
879 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
880 save_mode = SAVE_ONE;
882 save_mode = SAVE_NONE;
886 save_mode = SAVE_TWO;
892 case MONO_TYPE_VALUETYPE:
893 if (method->signature->ret->data.klass->enumtype) {
894 rtype = method->signature->ret->data.klass->enum_basetype->type;
897 save_mode = SAVE_STRUCT;
900 save_mode = SAVE_ONE;
906 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
907 ppc_stw (code, ppc_r4, save_offset + 4, cfg->frame_reg);
908 if (enable_arguments) {
909 ppc_mr (code, ppc_r5, ppc_r4);
910 ppc_mr (code, ppc_r4, ppc_r3);
914 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
915 if (enable_arguments) {
916 ppc_mr (code, ppc_r4, ppc_r3);
920 ppc_stfd (code, ppc_f1, save_offset, cfg->frame_reg);
921 if (enable_arguments) {
922 /* FIXME: what reg? */
923 ppc_fmr (code, ppc_f3, ppc_f1);
924 ppc_lwz (code, ppc_r4, save_offset, cfg->frame_reg);
925 ppc_lwz (code, ppc_r5, save_offset + 4, cfg->frame_reg);
929 if (enable_arguments) {
930 /* FIXME: get the actual address */
931 ppc_mr (code, ppc_r4, ppc_r3);
939 ppc_load (code, ppc_r3, cfg->method);
940 ppc_load (code, ppc_r0, func);
941 ppc_mtlr (code, ppc_r0);
946 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
947 ppc_lwz (code, ppc_r4, save_offset + 4, cfg->frame_reg);
950 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
953 ppc_lfd (code, ppc_f1, save_offset, cfg->frame_reg);
963 * Conditional branches have a small offset, so if it is likely overflowed,
964 * we do a branch to the end of the method (uncond branches have much larger
965 * offsets) where we perform the conditional and jump back unconditionally.
966 * It's slightly slower, since we add two uncond branches, but it's very simple
967 * with the current patch implementation and such large methods are likely not
968 * going to be perf critical anyway.
977 #define EMIT_COND_BRANCH_FLAGS(ins,b0,b1) \
978 if (ins->flags & MONO_INST_BRLABEL) { \
979 if (0 && ins->inst_i0->inst_c0) { \
980 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff); \
982 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
983 ppc_bc (code, (b0), (b1), 0); \
986 if (0 && ins->inst_true_bb->native_offset) { \
987 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
989 int br_disp = ins->inst_true_bb->max_offset - offset; \
990 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
991 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
992 ovfj->bb = ins->inst_true_bb; \
994 ovfj->b0_cond = (b0); \
995 ovfj->b1_cond = (b1); \
996 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB_OVF, ovfj); \
999 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1000 ppc_bc (code, (b0), (b1), 0); \
1005 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_b0_table [(cond)], branch_b1_table [(cond)])
1007 /* emit an exception if condition is fail
1009 * We assign the extra code used to throw the implicit exceptions
1010 * to cfg->bb_exit as far as the big branch handling is concerned
1012 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(b0,b1,exc_name) \
1014 int br_disp = cfg->bb_exit->max_offset - offset; \
1015 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
1016 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
1019 ovfj->b0_cond = (b0); \
1020 ovfj->b1_cond = (b1); \
1021 /* FIXME: test this code */ \
1022 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj); \
1024 cfg->bb_exit->max_offset += 24; \
1026 mono_add_patch_info (cfg, code - cfg->native_code, \
1027 MONO_PATCH_INFO_EXC, exc_name); \
1028 ppc_bc (code, (b0), (b1), 0); \
1032 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_b0_table [(cond)], branch_b1_table [(cond)], (exc_name))
1035 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1037 MonoInst *ins, *last_ins = NULL;
1042 switch (ins->opcode) {
1044 /* remove unnecessary multiplication with 1 */
1045 if (ins->inst_imm == 1) {
1046 if (ins->dreg != ins->sreg1) {
1047 ins->opcode = OP_MOVE;
1049 last_ins->next = ins->next;
1054 int power2 = mono_is_power_of_two (ins->inst_imm);
1056 ins->opcode = OP_SHL_IMM;
1057 ins->inst_imm = power2;
1061 case OP_LOAD_MEMBASE:
1062 case OP_LOADI4_MEMBASE:
1064 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1065 * OP_LOAD_MEMBASE offset(basereg), reg
1067 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1068 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1069 ins->inst_basereg == last_ins->inst_destbasereg &&
1070 ins->inst_offset == last_ins->inst_offset) {
1071 if (ins->dreg == last_ins->sreg1) {
1072 last_ins->next = ins->next;
1076 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1077 ins->opcode = OP_MOVE;
1078 ins->sreg1 = last_ins->sreg1;
1082 * Note: reg1 must be different from the basereg in the second load
1083 * OP_LOAD_MEMBASE offset(basereg), reg1
1084 * OP_LOAD_MEMBASE offset(basereg), reg2
1086 * OP_LOAD_MEMBASE offset(basereg), reg1
1087 * OP_MOVE reg1, reg2
1089 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1090 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1091 ins->inst_basereg != last_ins->dreg &&
1092 ins->inst_basereg == last_ins->inst_basereg &&
1093 ins->inst_offset == last_ins->inst_offset) {
1095 if (ins->dreg == last_ins->dreg) {
1096 last_ins->next = ins->next;
1100 ins->opcode = OP_MOVE;
1101 ins->sreg1 = last_ins->dreg;
1104 //g_assert_not_reached ();
1108 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1109 * OP_LOAD_MEMBASE offset(basereg), reg
1111 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1112 * OP_ICONST reg, imm
1114 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1115 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1116 ins->inst_basereg == last_ins->inst_destbasereg &&
1117 ins->inst_offset == last_ins->inst_offset) {
1118 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1119 ins->opcode = OP_ICONST;
1120 ins->inst_c0 = last_ins->inst_imm;
1121 g_assert_not_reached (); // check this rule
1125 case OP_LOADU1_MEMBASE:
1126 case OP_LOADI1_MEMBASE:
1127 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1128 ins->inst_basereg == last_ins->inst_destbasereg &&
1129 ins->inst_offset == last_ins->inst_offset) {
1130 if (ins->dreg == last_ins->sreg1) {
1131 last_ins->next = ins->next;
1135 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1136 ins->opcode = OP_MOVE;
1137 ins->sreg1 = last_ins->sreg1;
1141 case OP_LOADU2_MEMBASE:
1142 case OP_LOADI2_MEMBASE:
1143 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1144 ins->inst_basereg == last_ins->inst_destbasereg &&
1145 ins->inst_offset == last_ins->inst_offset) {
1146 if (ins->dreg == last_ins->sreg1) {
1147 last_ins->next = ins->next;
1151 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1152 ins->opcode = OP_MOVE;
1153 ins->sreg1 = last_ins->sreg1;
1161 ins->opcode = OP_MOVE;
1165 if (ins->dreg == ins->sreg1) {
1167 last_ins->next = ins->next;
1172 * OP_MOVE sreg, dreg
1173 * OP_MOVE dreg, sreg
1175 if (last_ins && last_ins->opcode == OP_MOVE &&
1176 ins->sreg1 == last_ins->dreg &&
1177 ins->dreg == last_ins->sreg1) {
1178 last_ins->next = ins->next;
1187 bb->last_ins = last_ins;
1191 * the branch_b0_table should maintain the order of these
1205 branch_b0_table [] = {
1220 branch_b1_table [] = {
1235 * returns the offset used by spillvar. It allocates a new
1236 * spill variable if necessary.
1239 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1241 MonoSpillInfo **si, *info;
1244 si = &cfg->spill_info;
1246 while (i <= spillvar) {
1249 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1251 info->offset = cfg->stack_offset;
1252 cfg->stack_offset += sizeof (gpointer);
1256 return (*si)->offset;
1262 g_assert_not_reached ();
1267 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1269 MonoSpillInfo **si, *info;
1272 si = &cfg->spill_info_float;
1274 while (i <= spillvar) {
1277 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1279 cfg->stack_offset += 7;
1280 cfg->stack_offset &= ~7;
1281 info->offset = cfg->stack_offset;
1282 cfg->stack_offset += sizeof (double);
1286 return (*si)->offset;
1292 g_assert_not_reached ();
1297 #define DEBUG(a) if (cfg->verbose_level > 1) a
1299 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1300 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 13)
1309 static const char*const * ins_spec = ppcg4;
1312 print_ins (int i, MonoInst *ins)
1314 const char *spec = ins_spec [ins->opcode];
1315 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1316 if (spec [MONO_INST_DEST]) {
1317 if (ins->dreg >= MONO_MAX_IREGS)
1318 g_print (" R%d <-", ins->dreg);
1320 g_print (" %s <-", mono_arch_regname (ins->dreg));
1322 if (spec [MONO_INST_SRC1]) {
1323 if (ins->sreg1 >= MONO_MAX_IREGS)
1324 g_print (" R%d", ins->sreg1);
1326 g_print (" %s", mono_arch_regname (ins->sreg1));
1328 if (spec [MONO_INST_SRC2]) {
1329 if (ins->sreg2 >= MONO_MAX_IREGS)
1330 g_print (" R%d", ins->sreg2);
1332 g_print (" %s", mono_arch_regname (ins->sreg2));
1334 if (spec [MONO_INST_CLOB])
1335 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1340 print_regtrack (RegTrack *t, int num)
1346 for (i = 0; i < num; ++i) {
1349 if (i >= MONO_MAX_IREGS) {
1350 g_snprintf (buf, sizeof(buf), "R%d", i);
1353 r = mono_arch_regname (i);
1354 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1358 typedef struct InstList InstList;
1366 static inline InstList*
1367 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1369 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1379 * Force the spilling of the variable in the symbolic register 'reg'.
1382 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1387 sel = cfg->rs->iassign [reg];
1388 /*i = cfg->rs->isymbolic [sel];
1389 g_assert (i == reg);*/
1391 spill = ++cfg->spill_count;
1392 cfg->rs->iassign [i] = -spill - 1;
1393 mono_regstate_free_int (cfg->rs, sel);
1394 /* we need to create a spill var and insert a load to sel after the current instruction */
1395 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1397 load->inst_basereg = cfg->frame_reg;
1398 load->inst_offset = mono_spillvar_offset (cfg, spill);
1400 while (ins->next != item->prev->data)
1403 load->next = ins->next;
1405 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1406 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1407 g_assert (i == sel);
1413 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1418 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));
1419 /* exclude the registers in the current instruction */
1420 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1421 if (ins->sreg1 >= MONO_MAX_IREGS)
1422 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1424 regmask &= ~ (1 << ins->sreg1);
1425 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1427 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1428 if (ins->sreg2 >= MONO_MAX_IREGS)
1429 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1431 regmask &= ~ (1 << ins->sreg2);
1432 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1434 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1435 regmask &= ~ (1 << ins->dreg);
1436 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1439 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1440 g_assert (regmask); /* need at least a register we can free */
1442 /* we should track prev_use and spill the register that's farther */
1443 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1444 if (regmask & (1 << i)) {
1446 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1450 i = cfg->rs->isymbolic [sel];
1451 spill = ++cfg->spill_count;
1452 cfg->rs->iassign [i] = -spill - 1;
1453 mono_regstate_free_int (cfg->rs, sel);
1454 /* we need to create a spill var and insert a load to sel after the current instruction */
1455 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1457 load->inst_basereg = cfg->frame_reg;
1458 load->inst_offset = mono_spillvar_offset (cfg, spill);
1460 while (ins->next != item->prev->data)
1463 load->next = ins->next;
1465 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1466 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1467 g_assert (i == sel);
1473 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1478 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));
1479 /* exclude the registers in the current instruction */
1480 if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1481 if (ins->sreg1 >= MONO_MAX_FREGS)
1482 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1484 regmask &= ~ (1 << ins->sreg1);
1485 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1487 if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1488 if (ins->sreg2 >= MONO_MAX_FREGS)
1489 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1491 regmask &= ~ (1 << ins->sreg2);
1492 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1494 if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1495 regmask &= ~ (1 << ins->dreg);
1496 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1499 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1500 g_assert (regmask); /* need at least a register we can free */
1502 /* we should track prev_use and spill the register that's farther */
1503 for (i = 0; i < MONO_MAX_FREGS; ++i) {
1504 if (regmask & (1 << i)) {
1506 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1510 i = cfg->rs->fsymbolic [sel];
1511 spill = ++cfg->spill_count;
1512 cfg->rs->fassign [i] = -spill - 1;
1513 mono_regstate_free_float(cfg->rs, sel);
1514 /* we need to create a spill var and insert a load to sel after the current instruction */
1515 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1517 load->inst_basereg = cfg->frame_reg;
1518 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1520 while (ins->next != item->prev->data)
1523 load->next = ins->next;
1525 DEBUG (g_print ("SPILLED LOAD FP (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1526 i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1527 g_assert (i == sel);
1533 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1536 MONO_INST_NEW (cfg, copy, OP_MOVE);
1540 copy->next = ins->next;
1543 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1548 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1551 MONO_INST_NEW (cfg, copy, OP_FMOVE);
1555 copy->next = ins->next;
1558 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1563 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1566 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1568 store->inst_destbasereg = cfg->frame_reg;
1569 store->inst_offset = mono_spillvar_offset (cfg, spill);
1571 store->next = ins->next;
1574 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)));
1579 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1582 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1584 store->inst_destbasereg = cfg->frame_reg;
1585 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1587 store->next = ins->next;
1590 DEBUG (g_print ("SPILLED STORE FP (%d at 0x%08x(%%sp)) R%d (from %s)\n", spill, store->inst_offset, prev_reg, mono_arch_regname (reg)));
1595 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1598 g_assert (item->next);
1599 prev = item->next->data;
1601 while (prev->next != ins)
1603 to_insert->next = ins;
1604 prev->next = to_insert;
1606 * needed otherwise in the next instruction we can add an ins to the
1607 * end and that would get past this instruction.
1609 item->data = to_insert;
1613 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1615 int val = cfg->rs->iassign [sym_reg];
1619 /* the register gets spilled after this inst */
1622 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1624 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1625 cfg->rs->iassign [sym_reg] = val;
1626 /* add option to store before the instruction for src registers */
1628 create_spilled_store (cfg, spill, val, sym_reg, ins);
1630 cfg->rs->isymbolic [val] = sym_reg;
1634 /* use ppc_r3-ppc_10,ppc_r12 as temp registers, f1-f13 for FP registers */
1635 #define PPC_CALLER_REGS ((0xff<<3) | (1<<12) | USE_EXTRA_TEMPS)
1636 #define PPC_CALLER_FREGS (0x3ffe)
1639 * Local register allocation.
1640 * We first scan the list of instructions and we save the liveness info of
1641 * each register (when the register is first used, when it's value is set etc.).
1642 * We also reverse the list of instructions (in the InstList list) because assigning
1643 * registers backwards allows for more tricks to be used.
1646 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1649 MonoRegState *rs = cfg->rs;
1651 RegTrack *reginfo, *reginfof;
1652 RegTrack *reginfo1, *reginfo2, *reginfod;
1653 InstList *tmp, *reversed = NULL;
1655 guint32 src1_mask, src2_mask, dest_mask;
1656 guint32 cur_iregs, cur_fregs;
1660 rs->next_vireg = bb->max_ireg;
1661 rs->next_vfreg = bb->max_freg;
1662 mono_regstate_assign (rs);
1663 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1664 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1665 rs->ifree_mask = PPC_CALLER_REGS;
1666 rs->ffree_mask = PPC_CALLER_FREGS;
1670 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1671 /* forward pass on the instructions to collect register liveness info */
1673 spec = ins_spec [ins->opcode];
1674 DEBUG (print_ins (i, ins));
1675 /*if (spec [MONO_INST_CLOB] == 'c') {
1676 MonoCallInst * call = (MonoCallInst*)ins;
1679 if (spec [MONO_INST_SRC1]) {
1680 if (spec [MONO_INST_SRC1] == 'f')
1681 reginfo1 = reginfof;
1684 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1685 reginfo1 [ins->sreg1].last_use = i;
1689 if (spec [MONO_INST_SRC2]) {
1690 if (spec [MONO_INST_SRC2] == 'f')
1691 reginfo2 = reginfof;
1694 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1695 reginfo2 [ins->sreg2].last_use = i;
1699 if (spec [MONO_INST_DEST]) {
1700 if (spec [MONO_INST_DEST] == 'f')
1701 reginfod = reginfof;
1704 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1705 reginfod [ins->dreg].killed_in = i;
1706 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1707 reginfod [ins->dreg].last_use = i;
1708 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1709 reginfod [ins->dreg].born_in = i;
1710 if (spec [MONO_INST_DEST] == 'l') {
1711 /* result in eax:edx, the virtual register is allocated sequentially */
1712 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1713 reginfod [ins->dreg + 1].last_use = i;
1714 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1715 reginfod [ins->dreg + 1].born_in = i;
1720 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1725 cur_iregs = PPC_CALLER_REGS;
1726 cur_fregs = PPC_CALLER_FREGS;
1728 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1729 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1732 int prev_dreg, prev_sreg1, prev_sreg2;
1735 spec = ins_spec [ins->opcode];
1736 DEBUG (g_print ("processing:"));
1737 DEBUG (print_ins (i, ins));
1738 /* make the register available for allocation: FIXME add fp reg */
1739 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1740 cur_iregs |= 1 << ins->dreg;
1741 DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1742 } else if (ins->opcode == OP_SETFREG) {
1743 cur_fregs |= 1 << ins->dreg;
1744 DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1745 } else if (spec [MONO_INST_CLOB] == 'c') {
1746 MonoCallInst *cinst = (MonoCallInst*)ins;
1747 DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1748 DEBUG (g_print ("excluding fpregs 0x%x from cur_fregs (0x%x)\n", cinst->used_fregs, cur_fregs));
1749 cur_iregs &= ~cinst->used_iregs;
1750 cur_fregs &= ~cinst->used_fregs;
1751 DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1752 DEBUG (g_print ("available cur_fregs: 0x%x\n", cur_fregs));
1753 /* registers used by the calling convention are excluded from
1754 * allocation: they will be selectively enabled when they are
1755 * assigned by the special SETREG opcodes.
1758 dest_mask = src1_mask = src2_mask = cur_iregs;
1759 /* update for use with FP regs... */
1760 if (spec [MONO_INST_DEST] == 'f') {
1761 dest_mask = cur_fregs;
1762 if (ins->dreg >= MONO_MAX_FREGS) {
1763 val = rs->fassign [ins->dreg];
1764 prev_dreg = ins->dreg;
1768 /* the register gets spilled after this inst */
1771 val = mono_regstate_alloc_float (rs, dest_mask);
1773 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1774 rs->fassign [ins->dreg] = val;
1776 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1778 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1779 rs->fsymbolic [val] = prev_dreg;
1781 if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != ppc_f1) {
1782 /* this instruction only outputs to ppc_f1, need to copy */
1783 create_copy_ins_float (cfg, ins->dreg, ppc_f1, ins);
1788 if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1789 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1790 mono_regstate_free_float (rs, ins->dreg);
1792 } else if (ins->dreg >= MONO_MAX_IREGS) {
1793 val = rs->iassign [ins->dreg];
1794 prev_dreg = ins->dreg;
1798 /* the register gets spilled after this inst */
1801 val = mono_regstate_alloc_int (rs, dest_mask);
1803 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1804 rs->iassign [ins->dreg] = val;
1806 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1808 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1809 rs->isymbolic [val] = prev_dreg;
1811 if (spec [MONO_INST_DEST] == 'l') {
1812 int hreg = prev_dreg + 1;
1813 val = rs->iassign [hreg];
1817 /* the register gets spilled after this inst */
1820 val = mono_regstate_alloc_int (rs, dest_mask);
1822 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1823 rs->iassign [hreg] = val;
1825 create_spilled_store (cfg, spill, val, hreg, ins);
1827 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1828 rs->isymbolic [val] = hreg;
1829 /* FIXME:? ins->dreg = val; */
1830 if (ins->dreg == ppc_r4) {
1832 create_copy_ins (cfg, val, ppc_r3, ins);
1833 } else if (ins->dreg == ppc_r3) {
1834 if (val == ppc_r4) {
1836 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1837 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1838 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1840 /* two forced copies */
1841 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1842 create_copy_ins (cfg, val, ppc_r3, ins);
1845 if (val == ppc_r3) {
1846 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1848 /* two forced copies */
1849 create_copy_ins (cfg, val, ppc_r3, ins);
1850 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1853 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
1854 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1855 mono_regstate_free_int (rs, val);
1857 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != ppc_r3 && spec [MONO_INST_CLOB] != 'd') {
1858 /* this instruction only outputs to ppc_r3, need to copy */
1859 create_copy_ins (cfg, ins->dreg, ppc_r3, ins);
1864 if (spec [MONO_INST_DEST] == 'f' && freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i)) {
1865 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1866 mono_regstate_free_float (rs, ins->dreg);
1867 } else if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
1868 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1869 mono_regstate_free_int (rs, ins->dreg);
1871 if (spec [MONO_INST_SRC1] == 'f') {
1872 src1_mask = cur_fregs;
1873 if (ins->sreg1 >= MONO_MAX_FREGS) {
1874 val = rs->fassign [ins->sreg1];
1875 prev_sreg1 = ins->sreg1;
1879 /* the register gets spilled after this inst */
1882 //g_assert (val == -1); /* source cannot be spilled */
1883 val = mono_regstate_alloc_float (rs, src1_mask);
1885 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1886 rs->fassign [ins->sreg1] = val;
1887 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1889 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
1890 insert_before_ins (ins, tmp, store);
1893 rs->fsymbolic [val] = prev_sreg1;
1898 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
1899 val = rs->iassign [ins->sreg1];
1900 prev_sreg1 = ins->sreg1;
1904 /* the register gets spilled after this inst */
1907 if (0 && ins->opcode == OP_MOVE) {
1909 * small optimization: the dest register is already allocated
1910 * but the src one is not: we can simply assign the same register
1911 * here and peephole will get rid of the instruction later.
1912 * This optimization may interfere with the clobbering handling:
1913 * it removes a mov operation that will be added again to handle clobbering.
1914 * There are also some other issues that should with make testjit.
1916 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1917 val = rs->iassign [ins->sreg1] = ins->dreg;
1918 //g_assert (val >= 0);
1919 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1921 //g_assert (val == -1); /* source cannot be spilled */
1922 val = mono_regstate_alloc_int (rs, src1_mask);
1924 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1925 rs->iassign [ins->sreg1] = val;
1926 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1929 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1930 insert_before_ins (ins, tmp, store);
1933 rs->isymbolic [val] = prev_sreg1;
1938 if (spec [MONO_INST_SRC2] == 'f') {
1939 src2_mask = cur_fregs;
1940 if (ins->sreg2 >= MONO_MAX_FREGS) {
1941 val = rs->fassign [ins->sreg2];
1942 prev_sreg2 = ins->sreg2;
1946 /* the register gets spilled after this inst */
1949 val = mono_regstate_alloc_float (rs, src2_mask);
1951 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1952 rs->fassign [ins->sreg2] = val;
1953 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1955 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
1957 rs->fsymbolic [val] = prev_sreg2;
1962 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
1963 val = rs->iassign [ins->sreg2];
1964 prev_sreg2 = ins->sreg2;
1968 /* the register gets spilled after this inst */
1971 val = mono_regstate_alloc_int (rs, src2_mask);
1973 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1974 rs->iassign [ins->sreg2] = val;
1975 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1977 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1979 rs->isymbolic [val] = prev_sreg2;
1985 if (spec [MONO_INST_CLOB] == 'c') {
1987 guint32 clob_mask = PPC_CALLER_REGS;
1988 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1990 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1991 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1995 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1996 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1997 mono_regstate_free_int (rs, ins->sreg1);
1999 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2000 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2001 mono_regstate_free_int (rs, ins->sreg2);
2004 //DEBUG (print_ins (i, ins));
2010 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2012 /* sreg is a float, dreg is an integer reg. ppc_f0 is used a scratch */
2013 ppc_fctiwz (code, ppc_f0, sreg);
2014 ppc_stfd (code, ppc_f0, -8, ppc_sp);
2015 ppc_lwz (code, dreg, -4, ppc_sp);
2018 ppc_andid (code, dreg, dreg, 0xff);
2020 ppc_andid (code, dreg, dreg, 0xffff);
2023 ppc_extsb (code, dreg, dreg);
2025 ppc_extsh (code, dreg, dreg);
2030 static unsigned char*
2031 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2034 int sreg = tree->sreg1;
2035 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2036 if (tree->flags & MONO_INST_INIT) {
2038 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2039 x86_push_reg (code, X86_EAX);
2042 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2043 x86_push_reg (code, X86_ECX);
2046 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2047 x86_push_reg (code, X86_EDI);
2051 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2052 if (sreg != X86_ECX)
2053 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2054 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2056 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2058 x86_prefix (code, X86_REP_PREFIX);
2061 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2062 x86_pop_reg (code, X86_EDI);
2063 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2064 x86_pop_reg (code, X86_ECX);
2065 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2066 x86_pop_reg (code, X86_EAX);
2079 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2082 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2083 PatchData *pdata = (PatchData*)user_data;
2084 guchar *code = data;
2085 guint32 *thunks = data;
2086 guint32 *endthunks = (guint32*)(code + bsize);
2090 int difflow, diffhigh;
2092 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
2093 difflow = (char*)pdata->code - (char*)thunks;
2094 diffhigh = (char*)pdata->code - (char*)endthunks;
2095 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
2098 templ = (guchar*)load;
2099 ppc_lis (templ, ppc_r0, (guint32)(pdata->target) >> 16);
2100 ppc_ori (templ, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2102 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2103 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2104 while (thunks < endthunks) {
2105 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2106 if ((thunks [0] == load [0]) && (thunks [1] == load [1])) {
2107 ppc_patch (pdata->code, (guchar*)thunks);
2108 mono_arch_flush_icache (pdata->code, 4);
2111 } else if ((thunks [0] == 0) && (thunks [1] == 0)) {
2112 /* found a free slot instead: emit thunk */
2113 code = (guchar*)thunks;
2114 ppc_lis (code, ppc_r0, (guint32)(pdata->target) >> 16);
2115 ppc_ori (code, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2116 ppc_mtctr (code, ppc_r0);
2117 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2118 mono_arch_flush_icache ((guchar*)thunks, 16);
2120 ppc_patch (pdata->code, (guchar*)thunks);
2121 mono_arch_flush_icache (pdata->code, 4);
2125 /* skip 16 bytes, the size of the thunk */
2129 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2135 handle_thunk (int absolute, guchar *code, guchar *target) {
2136 MonoDomain *domain = mono_domain_get ();
2140 pdata.target = target;
2141 pdata.absolute = absolute;
2144 mono_domain_lock (domain);
2145 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2148 /* this uses the first available slot */
2150 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2152 mono_domain_unlock (domain);
2154 if (pdata.found != 1)
2155 g_print ("thunk failed for %p from %p\n", target, code);
2156 g_assert (pdata.found == 1);
2160 ppc_patch (guchar *code, guchar *target)
2162 guint32 ins = *(guint32*)code;
2163 guint32 prim = ins >> 26;
2166 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2168 // prefer relative branches, they are more position independent (e.g. for AOT compilation).
2169 gint diff = target - code;
2171 if (diff <= 33554431){
2172 ins = (18 << 26) | (diff) | (ins & 1);
2173 *(guint32*)code = ins;
2177 /* diff between 0 and -33554432 */
2178 if (diff >= -33554432){
2179 ins = (18 << 26) | (diff & ~0xfc000000) | (ins & 1);
2180 *(guint32*)code = ins;
2185 if ((glong)target >= 0){
2186 if ((glong)target <= 33554431){
2187 ins = (18 << 26) | ((guint32) target) | (ins & 1) | 2;
2188 *(guint32*)code = ins;
2192 if ((glong)target >= -33554432){
2193 ins = (18 << 26) | (((guint32)target) & ~0xfc000000) | (ins & 1) | 2;
2194 *(guint32*)code = ins;
2199 handle_thunk (TRUE, code, target);
2202 g_assert_not_reached ();
2209 guint32 li = (guint32)target;
2210 ins = (ins & 0xffff0000) | (ins & 3);
2211 ovf = li & 0xffff0000;
2212 if (ovf != 0 && ovf != 0xffff0000)
2213 g_assert_not_reached ();
2216 // FIXME: assert the top bits of li are 0
2218 gint diff = target - code;
2219 ins = (ins & 0xffff0000) | (ins & 3);
2220 ovf = diff & 0xffff0000;
2221 if (ovf != 0 && ovf != 0xffff0000)
2222 g_assert_not_reached ();
2226 *(guint32*)code = ins;
2228 g_assert_not_reached ();
2230 // g_print ("patched with 0x%08x\n", ins);
2234 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2239 guint8 *code = cfg->native_code + cfg->code_len;
2240 MonoInst *last_ins = NULL;
2241 guint last_offset = 0;
2244 if (cfg->opt & MONO_OPT_PEEPHOLE)
2245 peephole_pass (cfg, bb);
2247 /* we don't align basic blocks of loops on ppc */
2249 if (cfg->verbose_level > 2)
2250 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2252 cpos = bb->max_offset;
2254 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2255 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2256 //g_assert (!mono_compile_aot);
2259 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2260 /* this is not thread save, but good enough */
2261 /* fixme: howto handle overflows? */
2262 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2267 offset = code - cfg->native_code;
2269 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2271 if (offset > (cfg->code_size - max_len - 16)) {
2272 cfg->code_size *= 2;
2273 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2274 code = cfg->native_code + offset;
2276 // if (ins->cil_code)
2277 // g_print ("cil code\n");
2279 switch (ins->opcode) {
2281 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2282 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
2285 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2286 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
2288 case OP_STOREI1_MEMBASE_IMM:
2289 ppc_li (code, ppc_r0, ins->inst_imm);
2290 if (ppc_is_imm16 (ins->inst_offset)) {
2291 ppc_stb (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2293 ppc_load (code, ppc_r11, ins->inst_offset);
2294 ppc_stbx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2297 case OP_STOREI2_MEMBASE_IMM:
2298 ppc_li (code, ppc_r0, ins->inst_imm);
2299 if (ppc_is_imm16 (ins->inst_offset)) {
2300 ppc_sth (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2302 ppc_load (code, ppc_r11, ins->inst_offset);
2303 ppc_sthx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2306 case OP_STORE_MEMBASE_IMM:
2307 case OP_STOREI4_MEMBASE_IMM:
2308 ppc_load (code, ppc_r0, ins->inst_imm);
2309 if (ppc_is_imm16 (ins->inst_offset)) {
2310 ppc_stw (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2312 ppc_load (code, ppc_r11, ins->inst_offset);
2313 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2316 case OP_STOREI1_MEMBASE_REG:
2317 if (ppc_is_imm16 (ins->inst_offset)) {
2318 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2320 ppc_load (code, ppc_r11, ins->inst_offset);
2321 ppc_stbx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2324 case OP_STOREI2_MEMBASE_REG:
2325 if (ppc_is_imm16 (ins->inst_offset)) {
2326 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2328 ppc_load (code, ppc_r11, ins->inst_offset);
2329 ppc_sthx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2332 case OP_STORE_MEMBASE_REG:
2333 case OP_STOREI4_MEMBASE_REG:
2334 if (ppc_is_imm16 (ins->inst_offset)) {
2335 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2337 ppc_load (code, ppc_r11, ins->inst_offset);
2338 ppc_stwx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2344 g_assert_not_reached ();
2345 //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2348 g_assert_not_reached ();
2349 //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2350 //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2352 case OP_LOAD_MEMBASE:
2353 case OP_LOADI4_MEMBASE:
2354 case OP_LOADU4_MEMBASE:
2355 if (ppc_is_imm16 (ins->inst_offset)) {
2356 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2358 ppc_load (code, ppc_r11, ins->inst_offset);
2359 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2362 case OP_LOADI1_MEMBASE:
2363 case OP_LOADU1_MEMBASE:
2364 if (ppc_is_imm16 (ins->inst_offset)) {
2365 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2367 ppc_load (code, ppc_r11, ins->inst_offset);
2368 ppc_lbzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2370 if (ins->opcode == OP_LOADI1_MEMBASE)
2371 ppc_extsb (code, ins->dreg, ins->dreg);
2373 case OP_LOADU2_MEMBASE:
2374 if (ppc_is_imm16 (ins->inst_offset)) {
2375 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2377 ppc_load (code, ppc_r11, ins->inst_offset);
2378 ppc_lhzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2381 case OP_LOADI2_MEMBASE:
2382 if (ppc_is_imm16 (ins->inst_offset)) {
2383 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2385 ppc_load (code, ppc_r11, ins->inst_offset);
2386 ppc_lhax (code, ins->dreg, ppc_r11, ins->inst_basereg);
2390 ppc_extsb (code, ins->dreg, ins->sreg1);
2393 ppc_extsh (code, ins->dreg, ins->sreg1);
2396 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
2399 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
2403 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2404 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2405 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN)))
2406 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2408 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2410 case OP_COMPARE_IMM:
2412 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2413 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2414 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN))) {
2415 if (ppc_is_uimm16 (ins->inst_imm)) {
2416 ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2418 ppc_load (code, ppc_r11, ins->inst_imm);
2419 ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2422 if (ppc_is_imm16 (ins->inst_imm)) {
2423 ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2425 ppc_load (code, ppc_r11, ins->inst_imm);
2426 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2430 case OP_X86_TEST_NULL:
2431 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2437 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2440 ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2443 ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2446 if (ppc_is_imm16 (ins->inst_imm)) {
2447 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2449 ppc_load (code, ppc_r11, ins->inst_imm);
2450 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2454 ppc_load (code, ppc_r11, ins->inst_imm);
2455 ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2458 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2460 ppc_addo (code, ins->dreg, ins->sreg1, ins->sreg2);
2461 ppc_mfspr (code, ppc_r0, ppc_xer);
2462 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2463 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2465 case CEE_ADD_OVF_UN:
2466 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2468 ppc_addco (code, ins->dreg, ins->sreg1, ins->sreg2);
2469 ppc_mfspr (code, ppc_r0, ppc_xer);
2470 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2471 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2474 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2476 ppc_subfo (code, ins->dreg, ins->sreg2, ins->sreg1);
2477 ppc_mfspr (code, ppc_r0, ppc_xer);
2478 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2479 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2481 case CEE_SUB_OVF_UN:
2482 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2484 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2485 ppc_mfspr (code, ppc_r0, ppc_xer);
2486 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2487 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2489 case OP_ADD_OVF_CARRY:
2490 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2492 ppc_addeo (code, ins->dreg, ins->sreg1, ins->sreg2);
2493 ppc_mfspr (code, ppc_r0, ppc_xer);
2494 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2495 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2497 case OP_ADD_OVF_UN_CARRY:
2498 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2500 ppc_addeo (code, ins->dreg, ins->sreg1, ins->sreg2);
2501 ppc_mfspr (code, ppc_r0, ppc_xer);
2502 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2503 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2505 case OP_SUB_OVF_CARRY:
2506 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2508 ppc_subfeo (code, ins->dreg, ins->sreg2, ins->sreg1);
2509 ppc_mfspr (code, ppc_r0, ppc_xer);
2510 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2511 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2513 case OP_SUB_OVF_UN_CARRY:
2514 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2516 ppc_subfeo (code, ins->dreg, ins->sreg2, ins->sreg1);
2517 ppc_mfspr (code, ppc_r0, ppc_xer);
2518 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2519 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2522 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2525 ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2528 ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2531 // we add the negated value
2532 if (ppc_is_imm16 (-ins->inst_imm))
2533 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2535 ppc_load (code, ppc_r11, ins->inst_imm);
2536 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2540 ppc_load (code, ppc_r11, ins->inst_imm);
2541 ppc_subfe (code, ins->dreg, ins->sreg2, ppc_r11);
2544 g_assert (ppc_is_imm16 (ins->inst_imm));
2545 ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2548 ppc_subfze (code, ins->dreg, ins->sreg1);
2551 /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2552 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2555 if (!(ins->inst_imm & 0xffff0000)) {
2556 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2557 } else if (!(ins->inst_imm & 0xffff)) {
2558 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2560 ppc_load (code, ppc_r11, ins->inst_imm);
2561 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2565 /* XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2567 ppc_divwod (code, ins->dreg, ins->sreg1, ins->sreg2);
2568 ppc_mfspr (code, ppc_r0, ppc_xer);
2569 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2570 /* FIXME: use OverflowException for 0x80000000/-1 */
2571 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2574 ppc_divwuod (code, ins->dreg, ins->sreg1, ins->sreg2);
2575 ppc_mfspr (code, ppc_r0, ppc_xer);
2576 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2577 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2580 g_assert_not_reached ();
2582 ppc_load (code, ppc_r11, ins->inst_imm);
2583 ppc_divwod (code, ins->dreg, ins->sreg1, ppc_r11);
2584 ppc_mfspr (code, ppc_r0, ppc_xer);
2585 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2586 /* FIXME: use OverflowException for 0x80000000/-1 */
2587 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2591 ppc_divwod (code, ppc_r11, ins->sreg1, ins->sreg2);
2592 ppc_mfspr (code, ppc_r0, ppc_xer);
2593 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2594 /* FIXME: use OverflowException for 0x80000000/-1 */
2595 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2596 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2597 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2600 ppc_divwuod (code, ppc_r11, ins->sreg1, ins->sreg2);
2601 ppc_mfspr (code, ppc_r0, ppc_xer);
2602 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2603 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2604 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2605 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2608 g_assert_not_reached ();
2610 ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2613 if (!(ins->inst_imm & 0xffff0000)) {
2614 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2615 } else if (!(ins->inst_imm & 0xffff)) {
2616 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2618 ppc_load (code, ppc_r11, ins->inst_imm);
2619 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2623 ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2626 if (!(ins->inst_imm & 0xffff0000)) {
2627 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2628 } else if (!(ins->inst_imm & 0xffff)) {
2629 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2631 ppc_load (code, ppc_r11, ins->inst_imm);
2632 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2636 ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2639 ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2640 //ppc_load (code, ppc_r11, ins->inst_imm);
2641 //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2644 ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2647 // there is also ppc_srawi
2648 //ppc_load (code, ppc_r11, ins->inst_imm);
2649 //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2650 ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2653 /*ppc_load (code, ppc_r11, ins->inst_imm);
2654 ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);*/
2655 ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0x1f)), (ins->inst_imm & 0x1f), 31);
2658 ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2661 ppc_not (code, ins->dreg, ins->sreg1);
2664 ppc_neg (code, ins->dreg, ins->sreg1);
2667 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2670 ppc_load (code, ppc_r11, ins->inst_imm);
2671 ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2674 /* we annot use mcrxr, since it's not implemented on some processors
2675 * XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2677 ppc_mullwo (code, ins->dreg, ins->sreg1, ins->sreg2);
2678 ppc_mfspr (code, ppc_r0, ppc_xer);
2679 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2680 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2682 case CEE_MUL_OVF_UN:
2683 /* we first multiply to get the high word and compare to 0
2684 * to set the flags, then the result is discarded and then
2685 * we multiply to get the lower * bits result
2687 ppc_mulhwu (code, ppc_r0, ins->sreg1, ins->sreg2);
2688 ppc_cmpi (code, 0, 0, ppc_r0, 0);
2689 EMIT_COND_SYSTEM_EXCEPTION (CEE_BNE_UN - CEE_BEQ, ins->inst_p1);
2690 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2694 ppc_load (code, ins->dreg, ins->inst_c0);
2697 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2698 ppc_lis (code, ins->dreg, 0);
2699 ppc_ori (code, ins->dreg, ins->dreg, 0);
2705 ppc_mr (code, ins->dreg, ins->sreg1);
2708 int saved = ins->sreg1;
2709 if (ins->sreg1 == ppc_r3) {
2710 ppc_mr (code, ppc_r0, ins->sreg1);
2713 if (ins->sreg2 != ppc_r3)
2714 ppc_mr (code, ppc_r3, ins->sreg2);
2715 if (saved != ppc_r4)
2716 ppc_mr (code, ppc_r4, saved);
2721 ppc_fmr (code, ins->dreg, ins->sreg1);
2723 case OP_FCONV_TO_R4:
2724 ppc_frsp (code, ins->dreg, ins->sreg1);
2730 * Keep in sync with mono_arch_emit_epilog
2732 g_assert (!cfg->method->save_lmf);
2733 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2734 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
2735 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
2737 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
2738 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
2740 ppc_mtlr (code, ppc_r0);
2742 if (ppc_is_imm16 (cfg->stack_usage)) {
2743 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2745 ppc_load (code, ppc_r11, cfg->stack_usage);
2746 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
2748 if (!cfg->method->save_lmf) {
2749 /*for (i = 31; i >= 14; --i) {
2750 if (cfg->used_float_regs & (1 << i)) {
2751 pos += sizeof (double);
2752 ppc_lfd (code, i, -pos, cfg->frame_reg);
2755 for (i = 31; i >= 13; --i) {
2756 if (cfg->used_int_regs & (1 << i)) {
2757 pos += sizeof (gulong);
2758 ppc_lwz (code, i, -pos, cfg->frame_reg);
2762 /* FIXME restore from MonoLMF: though this can't happen yet */
2764 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2769 /* ensure ins->sreg1 is not NULL */
2770 ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2773 /* FIXME: implement */
2780 call = (MonoCallInst*)ins;
2781 if (ins->flags & MONO_INST_HAS_METHOD)
2782 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2784 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2790 case OP_VOIDCALL_REG:
2792 ppc_mtlr (code, ins->sreg1);
2795 case OP_FCALL_MEMBASE:
2796 case OP_LCALL_MEMBASE:
2797 case OP_VCALL_MEMBASE:
2798 case OP_VOIDCALL_MEMBASE:
2799 case OP_CALL_MEMBASE:
2800 ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2801 ppc_mtlr (code, ppc_r0);
2805 g_assert_not_reached ();
2808 /* keep alignment */
2809 int alloca_waste = PPC_STACK_PARAM_OFFSET + cfg->param_area + 31;
2810 int area_offset = alloca_waste;
2812 ppc_addi (code, ppc_r11, ins->sreg1, alloca_waste);
2813 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 0, 27);
2814 ppc_lwz (code, ppc_r0, 0, ppc_sp);
2815 ppc_neg (code, ppc_r11, ppc_r11);
2816 ppc_stwux (code, ppc_r0, ppc_sp, ppc_r11);
2817 ppc_addi (code, ins->dreg, ppc_sp, area_offset);
2825 ppc_mr (code, ppc_r3, ins->sreg1);
2826 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2827 (gpointer)"mono_arch_throw_exception");
2831 case OP_START_HANDLER:
2832 ppc_mflr (code, ppc_r0);
2833 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2834 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2836 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2837 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_left->inst_basereg);
2841 if (ins->sreg1 != ppc_r3)
2842 ppc_mr (code, ppc_r3, ins->sreg1);
2843 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2844 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2846 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2847 ppc_lwzx (code, ppc_r0, ins->inst_left->inst_basereg, ppc_r11);
2849 ppc_mtlr (code, ppc_r0);
2852 case CEE_ENDFINALLY:
2853 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2854 ppc_mtlr (code, ppc_r0);
2857 case OP_CALL_HANDLER:
2858 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2862 ins->inst_c0 = code - cfg->native_code;
2865 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2866 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2868 if (ins->flags & MONO_INST_BRLABEL) {
2869 /*if (ins->inst_i0->inst_c0) {
2871 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2873 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2877 /*if (ins->inst_target_bb->native_offset) {
2879 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2881 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2887 ppc_mtctr (code, ins->sreg1);
2888 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2891 ppc_li (code, ins->dreg, 0);
2892 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2893 ppc_li (code, ins->dreg, 1);
2897 ppc_li (code, ins->dreg, 1);
2898 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2899 ppc_li (code, ins->dreg, 0);
2903 ppc_li (code, ins->dreg, 1);
2904 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2905 ppc_li (code, ins->dreg, 0);
2907 case OP_COND_EXC_EQ:
2908 case OP_COND_EXC_NE_UN:
2909 case OP_COND_EXC_LT:
2910 case OP_COND_EXC_LT_UN:
2911 case OP_COND_EXC_GT:
2912 case OP_COND_EXC_GT_UN:
2913 case OP_COND_EXC_GE:
2914 case OP_COND_EXC_GE_UN:
2915 case OP_COND_EXC_LE:
2916 case OP_COND_EXC_LE_UN:
2917 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2920 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2922 /*ppc_mfspr (code, ppc_r0, ppc_xer);
2923 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2924 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2926 case OP_COND_EXC_OV:
2927 /*ppc_mcrxr (code, 0);
2928 EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2930 case OP_COND_EXC_NC:
2931 case OP_COND_EXC_NO:
2932 g_assert_not_reached ();
2944 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2947 /* floating point opcodes */
2949 ppc_load (code, ppc_r11, ins->inst_p0);
2950 ppc_lfd (code, ins->dreg, 0, ppc_r11);
2953 ppc_load (code, ppc_r11, ins->inst_p0);
2954 ppc_lfs (code, ins->dreg, 0, ppc_r11);
2956 case OP_STORER8_MEMBASE_REG:
2957 if (ppc_is_imm16 (ins->inst_offset)) {
2958 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2960 ppc_load (code, ppc_r11, ins->inst_offset);
2961 ppc_stfdx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2964 case OP_LOADR8_MEMBASE:
2965 if (ppc_is_imm16 (ins->inst_offset)) {
2966 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2968 ppc_load (code, ppc_r11, ins->inst_offset);
2969 ppc_lfdx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2972 case OP_STORER4_MEMBASE_REG:
2973 if (ppc_is_imm16 (ins->inst_offset)) {
2974 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2976 ppc_load (code, ppc_r11, ins->inst_offset);
2977 ppc_stfsx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2980 case OP_LOADR4_MEMBASE:
2981 if (ppc_is_imm16 (ins->inst_offset)) {
2982 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2984 ppc_load (code, ppc_r11, ins->inst_offset);
2985 ppc_lfsx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2988 case CEE_CONV_R_UN: {
2989 static const guint64 adjust_val = 0x4330000000000000ULL;
2990 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2991 ppc_stw (code, ppc_r0, -8, ppc_sp);
2992 ppc_stw (code, ins->sreg1, -4, ppc_sp);
2993 ppc_load (code, ppc_r11, &adjust_val);
2994 ppc_lfd (code, ins->dreg, -8, ppc_sp);
2995 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2996 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2999 case CEE_CONV_R4: /* FIXME: change precision */
3001 static const guint64 adjust_val = 0x4330000080000000ULL;
3002 // addis is special for ppc_r0
3003 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3004 ppc_stw (code, ppc_r0, -8, ppc_sp);
3005 ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
3006 ppc_stw (code, ppc_r11, -4, ppc_sp);
3007 ppc_lfd (code, ins->dreg, -8, ppc_sp);
3008 ppc_load (code, ppc_r11, &adjust_val);
3009 ppc_lfd (code, ppc_f0, 0, ppc_r11);
3010 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3013 case OP_X86_FP_LOAD_I8:
3014 g_assert_not_reached ();
3015 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
3017 case OP_X86_FP_LOAD_I4:
3018 g_assert_not_reached ();
3019 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
3021 case OP_FCONV_TO_I1:
3022 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
3024 case OP_FCONV_TO_U1:
3025 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
3027 case OP_FCONV_TO_I2:
3028 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
3030 case OP_FCONV_TO_U2:
3031 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
3033 case OP_FCONV_TO_I4:
3035 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
3037 case OP_FCONV_TO_U4:
3039 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
3041 case OP_FCONV_TO_I8:
3042 case OP_FCONV_TO_U8:
3043 g_assert_not_reached ();
3044 /* Implemented as helper calls */
3046 case OP_LCONV_TO_R_UN:
3047 g_assert_not_reached ();
3048 /* Implemented as helper calls */
3050 case OP_LCONV_TO_OVF_I: {
3051 ppc_mr (code, ins->dreg, ins->sreg1);
3052 /* FIXME: emit exception if needed */
3056 ppc_fsqrtd (code, ins->dreg, ins->sreg1);
3059 ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
3062 ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
3065 ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
3068 ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
3071 ppc_fneg (code, ins->dreg, ins->sreg1);
3075 g_assert_not_reached ();
3078 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3081 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3082 ppc_li (code, ins->dreg, 0);
3083 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
3084 ppc_li (code, ins->dreg, 1);
3087 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3088 ppc_li (code, ins->dreg, 1);
3089 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3090 ppc_li (code, ins->dreg, 0);
3093 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3094 ppc_li (code, ins->dreg, 1);
3095 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3096 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3097 ppc_li (code, ins->dreg, 0);
3100 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3101 ppc_li (code, ins->dreg, 1);
3102 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3103 ppc_li (code, ins->dreg, 0);
3106 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3107 ppc_li (code, ins->dreg, 1);
3108 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3109 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3110 ppc_li (code, ins->dreg, 0);
3113 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
3116 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
3119 EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
3122 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3123 EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
3126 EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
3129 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3130 EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
3133 EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
3136 EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
3139 EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
3142 EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
3144 case CEE_CKFINITE: {
3145 ppc_stfd (code, ins->sreg1, -8, ppc_sp);
3146 ppc_lwz (code, ppc_r11, -8, ppc_sp);
3147 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
3148 ppc_addis (code, ppc_r11, ppc_r11, -32752);
3149 ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
3150 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");
3154 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3155 g_assert_not_reached ();
3158 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
3159 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3160 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3161 g_assert_not_reached ();
3167 last_offset = offset;
3172 cfg->code_len = code - cfg->native_code;
3176 mono_arch_register_lowlevel_calls (void)
3180 #define patch_lis_ori(ip,val) do {\
3181 guint16 *__lis_ori = (guint16*)(ip); \
3182 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
3183 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
3187 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3189 MonoJumpInfo *patch_info;
3191 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3192 unsigned char *ip = patch_info->ip.i + code;
3193 const unsigned char *target;
3195 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3197 switch (patch_info->type) {
3198 case MONO_PATCH_INFO_IP:
3199 patch_lis_ori (ip, ip);
3201 case MONO_PATCH_INFO_METHOD_REL:
3202 g_assert_not_reached ();
3203 *((gpointer *)(ip)) = code + patch_info->data.offset;
3205 case MONO_PATCH_INFO_SWITCH: {
3206 gpointer *table = (gpointer *)patch_info->data.target;
3209 // FIXME: inspect code to get the register
3210 ppc_load (ip, ppc_r11, patch_info->data.target);
3211 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3213 for (i = 0; i < patch_info->table_size; i++) {
3214 table [i] = (int)patch_info->data.table [i] + code;
3216 /* we put into the table the absolute address, no need for ppc_patch in this case */
3219 case MONO_PATCH_INFO_METHODCONST:
3220 case MONO_PATCH_INFO_CLASS:
3221 case MONO_PATCH_INFO_IMAGE:
3222 case MONO_PATCH_INFO_FIELD:
3223 case MONO_PATCH_INFO_VTABLE:
3224 case MONO_PATCH_INFO_IID:
3225 case MONO_PATCH_INFO_SFLDA:
3226 case MONO_PATCH_INFO_LDSTR:
3227 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3228 case MONO_PATCH_INFO_LDTOKEN:
3229 /* from OP_AOTCONST : lis + ori */
3230 patch_lis_ori (ip, target);
3232 case MONO_PATCH_INFO_R4:
3233 case MONO_PATCH_INFO_R8:
3234 g_assert_not_reached ();
3235 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3237 case MONO_PATCH_INFO_EXC_NAME:
3238 g_assert_not_reached ();
3239 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3241 case MONO_PATCH_INFO_BB_OVF:
3242 case MONO_PATCH_INFO_EXC_OVF:
3243 /* everything is dealt with at epilog output time */
3248 ppc_patch (ip, target);
3253 mono_arch_max_epilog_size (MonoCompile *cfg)
3255 int max_epilog_size = 16 + 20*4;
3256 MonoJumpInfo *patch_info;
3258 if (cfg->method->save_lmf)
3259 max_epilog_size += 128;
3261 if (mono_jit_trace_calls != NULL)
3262 max_epilog_size += 50;
3264 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3265 max_epilog_size += 50;
3267 /* count the number of exception infos */
3270 * make sure we have enough space for exceptions
3271 * 24 is the simulated call to throw_exception_by_name
3273 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3274 if (patch_info->type == MONO_PATCH_INFO_EXC)
3275 max_epilog_size += 24;
3276 else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
3277 max_epilog_size += 12;
3278 else if (patch_info->type == MONO_PATCH_INFO_EXC_OVF)
3279 max_epilog_size += 12;
3282 return max_epilog_size;
3286 * Stack frame layout:
3288 * ------------------- sp
3289 * MonoLMF structure or saved registers
3290 * -------------------
3292 * -------------------
3294 * -------------------
3295 * optional 8 bytes for tracing
3296 * -------------------
3297 * param area size is cfg->param_area
3298 * -------------------
3299 * linkage area size is PPC_STACK_PARAM_OFFSET
3300 * ------------------- sp
3304 mono_arch_emit_prolog (MonoCompile *cfg)
3306 MonoMethod *method = cfg->method;
3308 MonoMethodSignature *sig;
3310 int alloc_size, pos, max_offset, i;
3316 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3319 cfg->code_size = 256;
3320 code = cfg->native_code = g_malloc (cfg->code_size);
3322 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3323 ppc_mflr (code, ppc_r0);
3324 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3326 cfg->used_int_regs |= USE_EXTRA_TEMPS;
3328 alloc_size = cfg->stack_offset;
3331 if (!method->save_lmf) {
3332 /*for (i = 31; i >= 14; --i) {
3333 if (cfg->used_float_regs & (1 << i)) {
3334 pos += sizeof (gdouble);
3335 ppc_stfd (code, i, -pos, ppc_sp);
3338 for (i = 31; i >= 13; --i) {
3339 if (cfg->used_int_regs & (1 << i)) {
3340 pos += sizeof (gulong);
3341 ppc_stw (code, i, -pos, ppc_sp);
3346 pos += sizeof (MonoLMF);
3348 ofs = -pos + G_STRUCT_OFFSET(MonoLMF, iregs);
3349 ppc_stmw (code, ppc_r13, ppc_r1, ofs);
3350 for (i = 14; i < 32; i++) {
3351 ppc_stfd (code, i, (-pos + G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble))), ppc_r1);
3355 // align to PPC_STACK_ALIGNMENT bytes
3356 if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
3357 alloc_size += PPC_STACK_ALIGNMENT - 1;
3358 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
3361 cfg->stack_usage = alloc_size;
3362 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
3364 if (ppc_is_imm16 (-alloc_size)) {
3365 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3367 ppc_load (code, ppc_r11, -alloc_size);
3368 ppc_stwux (code, ppc_sp, ppc_sp, ppc_r11);
3371 if (cfg->frame_reg != ppc_sp)
3372 ppc_mr (code, cfg->frame_reg, ppc_sp);
3374 /* compute max_offset in order to use short forward jumps
3375 * we always do it on ppc because the immediate displacement
3376 * for jumps is too small
3379 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3380 MonoInst *ins = bb->code;
3381 bb->max_offset = max_offset;
3383 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3387 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3392 /* load arguments allocated to register from the stack */
3393 sig = method->signature;
3396 cinfo = calculate_sizes (sig, sig->pinvoke);
3398 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3399 ArgInfo *ainfo = &cinfo->ret;
3401 if (ppc_is_imm16 (inst->inst_offset)) {
3402 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3404 ppc_load (code, ppc_r11, inst->inst_offset);
3405 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3408 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3409 ArgInfo *ainfo = cinfo->args + i;
3410 inst = cfg->varinfo [pos];
3412 if (inst->opcode == OP_REGVAR) {
3413 if (ainfo->regtype == RegTypeGeneral)
3414 ppc_mr (code, inst->dreg, ainfo->reg);
3415 else if (ainfo->regtype == RegTypeFP)
3416 ppc_fmr (code, inst->dreg, ainfo->reg);
3417 else if (ainfo->regtype == RegTypeBase) {
3418 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3419 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3421 g_assert_not_reached ();
3423 if (cfg->verbose_level > 2)
3424 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3426 /* the argument should be put on the stack: FIXME handle size != word */
3427 if (ainfo->regtype == RegTypeGeneral) {
3428 switch (ainfo->size) {
3430 if (ppc_is_imm16 (inst->inst_offset)) {
3431 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3433 ppc_load (code, ppc_r11, inst->inst_offset);
3434 ppc_stbx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3438 if (ppc_is_imm16 (inst->inst_offset)) {
3439 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3441 ppc_load (code, ppc_r11, inst->inst_offset);
3442 ppc_sthx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3446 if (ppc_is_imm16 (inst->inst_offset + 4)) {
3447 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3448 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3450 ppc_load (code, ppc_r11, inst->inst_offset);
3451 ppc_add (code, ppc_r11, ppc_r11, inst->inst_basereg);
3452 ppc_stw (code, ainfo->reg, 0, ppc_r11);
3453 ppc_stw (code, ainfo->reg + 1, 4, ppc_r11);
3457 if (ppc_is_imm16 (inst->inst_offset)) {
3458 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3460 ppc_load (code, ppc_r11, inst->inst_offset);
3461 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3465 } else if (ainfo->regtype == RegTypeBase) {
3466 /* load the previous stack pointer in r11 */
3467 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3468 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3469 switch (ainfo->size) {
3471 if (ppc_is_imm16 (inst->inst_offset)) {
3472 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3474 ppc_load (code, ppc_r11, inst->inst_offset);
3475 ppc_stbx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3479 if (ppc_is_imm16 (inst->inst_offset)) {
3480 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3482 ppc_load (code, ppc_r11, inst->inst_offset);
3483 ppc_sthx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3487 if (ppc_is_imm16 (inst->inst_offset + 4)) {
3488 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3489 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3490 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3493 g_assert_not_reached ();
3497 if (ppc_is_imm16 (inst->inst_offset)) {
3498 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3500 ppc_load (code, ppc_r11, inst->inst_offset);
3501 ppc_stwx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3505 } else if (ainfo->regtype == RegTypeFP) {
3506 g_assert (ppc_is_imm16 (inst->inst_offset));
3507 if (ainfo->size == 8)
3508 ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3509 else if (ainfo->size == 4)
3510 ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3512 g_assert_not_reached ();
3513 } else if (ainfo->regtype == RegTypeStructByVal) {
3514 int doffset = inst->inst_offset;
3517 g_assert (ppc_is_imm16 (inst->inst_offset));
3518 g_assert (ppc_is_imm16 (inst->inst_offset + ainfo->size * sizeof (gpointer)));
3519 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3520 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3521 soffset += sizeof (gpointer);
3522 doffset += sizeof (gpointer);
3524 if (ainfo->vtsize) {
3525 /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3526 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3527 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3528 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3530 } else if (ainfo->regtype == RegTypeStructByAddr) {
3531 g_assert (ppc_is_imm16 (inst->inst_offset));
3532 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3533 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3535 g_assert_not_reached ();
3540 if (method->save_lmf) {
3542 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3543 (gpointer)"mono_get_lmf_addr");
3545 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3546 /* lmf_offset is the offset from the previous stack pointer,
3547 * alloc_size is the total stack space allocated, so the offset
3548 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
3549 * The pointer to the struct is put in ppc_r11 (new_lmf).
3550 * The callee-saved registers are already in the MonoLMF structure
3552 ppc_addi (code, ppc_r11, ppc_sp, alloc_size - lmf_offset);
3553 /* ppc_r3 is the result from mono_get_lmf_addr () */
3554 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3555 /* new_lmf->previous_lmf = *lmf_addr */
3556 ppc_lwz (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3557 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3558 /* *(lmf_addr) = r11 */
3559 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3560 /* save method info */
3561 ppc_load (code, ppc_r0, method);
3562 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3563 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3564 /* save the current IP */
3565 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3566 ppc_load (code, ppc_r0, 0x01010101);
3567 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3571 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3573 cfg->code_len = code - cfg->native_code;
3580 mono_arch_emit_epilog (MonoCompile *cfg)
3582 MonoJumpInfo *patch_info;
3583 MonoMethod *method = cfg->method;
3588 * Keep in sync with CEE_JMP
3590 code = cfg->native_code + cfg->code_len;
3592 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3593 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3597 if (method->save_lmf) {
3599 pos += sizeof (MonoLMF);
3601 /* save the frame reg in r8 */
3602 ppc_mr (code, ppc_r8, cfg->frame_reg);
3603 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage - lmf_offset);
3604 /* r5 = previous_lmf */
3605 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3607 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3608 /* *(lmf_addr) = previous_lmf */
3609 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3610 /* FIXME: speedup: there is no actual need to restore the registers if
3611 * we didn't actually change them (idea from Zoltan).
3614 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3616 /*for (i = 14; i < 32; i++) {
3617 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3619 g_assert (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET));
3620 /* use the saved copy of the frame reg in r8 */
3621 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3622 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, ppc_r8);
3623 ppc_mtlr (code, ppc_r0);
3625 ppc_addic (code, ppc_sp, ppc_r8, cfg->stack_usage);
3627 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3628 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
3629 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3631 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
3632 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
3634 ppc_mtlr (code, ppc_r0);
3636 if (ppc_is_imm16 (cfg->stack_usage)) {
3637 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3639 ppc_load (code, ppc_r11, cfg->stack_usage);
3640 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
3643 /*for (i = 31; i >= 14; --i) {
3644 if (cfg->used_float_regs & (1 << i)) {
3645 pos += sizeof (double);
3646 ppc_lfd (code, i, -pos, ppc_sp);
3649 for (i = 31; i >= 13; --i) {
3650 if (cfg->used_int_regs & (1 << i)) {
3651 pos += sizeof (gulong);
3652 ppc_lwz (code, i, -pos, ppc_sp);
3658 /* add code to raise exceptions */
3659 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3660 switch (patch_info->type) {
3661 case MONO_PATCH_INFO_BB_OVF: {
3662 MonoOvfJump *ovfj = patch_info->data.target;
3663 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3664 /* patch the initial jump */
3665 ppc_patch (ip, code);
3666 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3668 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3669 /* jump back to the true target */
3671 ip = ovfj->bb->native_offset + cfg->native_code;
3672 ppc_patch (code - 4, ip);
3675 case MONO_PATCH_INFO_EXC_OVF: {
3676 MonoOvfJump *ovfj = patch_info->data.target;
3677 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3678 /* patch the initial jump */
3679 ppc_patch (ip, code);
3680 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3682 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3683 /* jump back to the true target */
3685 ip = (char*)ovfj->ip + 4;
3686 ppc_patch (code - 4, ip);
3689 case MONO_PATCH_INFO_EXC: {
3690 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3691 ppc_patch (ip, code);
3692 /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3693 ppc_load (code, ppc_r3, patch_info->data.target);
3694 /* simulate a call from ip */
3695 ppc_load (code, ppc_r0, ip + 4);
3696 ppc_mtlr (code, ppc_r0);
3697 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3698 patch_info->data.name = "mono_arch_throw_exception_by_name";
3699 patch_info->ip.i = code - cfg->native_code;
3709 cfg->code_len = code - cfg->native_code;
3711 g_assert (cfg->code_len < cfg->code_size);
3716 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3721 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3726 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3728 int this_dreg = ppc_r3;
3733 /* add the this argument */
3734 if (this_reg != -1) {
3736 MONO_INST_NEW (cfg, this, OP_SETREG);
3737 this->type = this_type;
3738 this->sreg1 = this_reg;
3739 this->dreg = this_dreg;
3740 mono_bblock_add_inst (cfg->cbb, this);
3745 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3746 vtarg->type = STACK_MP;
3747 vtarg->sreg1 = vt_reg;
3748 vtarg->dreg = ppc_r3;
3749 mono_bblock_add_inst (cfg->cbb, vtarg);
3754 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3756 /* optional instruction, need to detect it
3757 if (cmethod->klass == mono_defaults.math_class) {
3758 if (strcmp (cmethod->name, "Sqrt") == 0)
3766 mono_arch_print_tree (MonoInst *tree, int arity)