2 * mini-ppc.c: PowerPC backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/debug-helpers.h>
21 int mono_exc_esp_offset = 0;
24 mono_arch_regname (int reg) {
25 static const char * rnames[] = {
26 "ppc_r0", "ppc_sp", "ppc_r2", "ppc_r3", "ppc_r4",
27 "ppc_r5", "ppc_r6", "ppc_r7", "ppc_r8", "ppc_r9",
28 "ppc_r10", "ppc_r11", "ppc_r12", "ppc_r13", "ppc_r14",
29 "ppc_r15", "ppc_r16", "ppc_r17", "ppc_r18", "ppc_r19",
30 "ppc_r20", "ppc_r21", "ppc_r22", "ppc_r23", "ppc_r24",
31 "ppc_r25", "ppc_r26", "ppc_r27", "ppc_r28", "ppc_r29",
34 if (reg >= 0 && reg < 32)
39 /* this function overwrites r0 */
41 emit_memcpy (guint32 *code, int size, int dreg, int doffset, int sreg, int soffset)
43 /* unrolled, use the counter in big */
45 ppc_lwz (code, ppc_r0, soffset, sreg);
46 ppc_stw (code, ppc_r0, doffset, dreg);
52 ppc_lhz (code, ppc_r0, soffset, sreg);
53 ppc_sth (code, ppc_r0, doffset, dreg);
59 ppc_lbz (code, ppc_r0, soffset, sreg);
60 ppc_stb (code, ppc_r0, doffset, dreg);
69 * mono_arch_get_argument_info:
70 * @csig: a method signature
71 * @param_count: the number of parameters to consider
72 * @arg_info: an array to store the result infos
74 * Gathers information on parameters such as size, alignment and
75 * padding. arg_info should be large enought to hold param_count + 1 entries.
77 * Returns the size of the activation frame.
80 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
82 int k, frame_size = 0;
86 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
87 frame_size += sizeof (gpointer);
91 arg_info [0].offset = offset;
94 frame_size += sizeof (gpointer);
98 arg_info [0].size = frame_size;
100 for (k = 0; k < param_count; k++) {
103 size = mono_type_native_stack_size (csig->params [k], &align);
105 size = mono_type_stack_size (csig->params [k], &align);
107 /* ignore alignment for now */
110 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
111 arg_info [k].pad = pad;
113 arg_info [k + 1].pad = 0;
114 arg_info [k + 1].size = size;
116 arg_info [k + 1].offset = offset;
120 align = MONO_ARCH_FRAME_ALIGNMENT;
121 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
122 arg_info [k].pad = pad;
128 * Initialize the cpu to execute managed code.
131 mono_arch_cpu_init (void)
136 * This function returns the optimizations supported on this cpu.
139 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
143 /* no ppc-specific optimizations yet */
145 *exclude_mask = MONO_OPT_INLINE|MONO_OPT_LINEARS;
150 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, 16);
579 header = ((MonoMethodNormal *)m->method)->header;
582 * We use the frame register also for any method that has
583 * exception clauses. This way, when the handlers are called,
584 * the code will reference local variables using the frame reg instead of
585 * the stack pointer: if we had to restore the stack pointer, we'd
586 * corrupt the method frames that are already on the stack (since
587 * filters get called before stack unwinding happens) when the filter
588 * code would call any method (this also applies to finally etc.).
590 if ((m->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
592 m->frame_reg = frame_reg;
593 if (frame_reg != ppc_sp) {
594 m->used_int_regs |= 1 << frame_reg;
597 sig = m->method->signature;
601 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
602 m->ret->opcode = OP_REGVAR;
603 m->ret->inst_c0 = ppc_r3;
605 /* FIXME: handle long and FP values */
606 switch (sig->ret->type) {
610 m->ret->opcode = OP_REGVAR;
611 m->ret->inst_c0 = ppc_r3;
615 /* local vars are at a positive offset from the stack pointer */
617 * also note that if the function uses alloca, we use ppc_r31
618 * to point at the local variables.
620 offset = PPC_MINIMAL_STACK_SIZE; /* linkage area */
621 /* align the offset to 16 bytes: not sure this is needed here */
623 //offset &= ~(16 - 1);
625 /* add parameter area size for called functions */
626 offset += m->param_area;
630 /* allow room to save the return value */
631 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
634 /* the MonoLMF structure is stored just below the stack pointer */
637 /* this stuff should not be needed on ppc and the new jit,
638 * because a call on ppc to the handlers doesn't change the
639 * stack pointer and the jist doesn't manipulate the stack pointer
640 * for operations involving valuetypes.
642 /* reserve space to store the esp */
643 offset += sizeof (gpointer);
645 /* this is a global constant */
646 mono_exc_esp_offset = offset;
649 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
651 offset += sizeof(gpointer) - 1;
652 offset &= ~(sizeof(gpointer) - 1);
653 inst->inst_offset = offset;
654 inst->opcode = OP_REGOFFSET;
655 inst->inst_basereg = frame_reg;
656 offset += sizeof(gpointer);
658 curinst = m->locals_start;
659 for (i = curinst; i < m->num_varinfo; ++i) {
660 inst = m->varinfo [i];
661 if (inst->opcode == OP_REGVAR)
664 /* inst->unused indicates native sized value types, this is used by the
665 * pinvoke wrappers when they call functions returning structure */
666 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
667 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
669 size = mono_type_size (inst->inst_vtype, &align);
672 offset &= ~(align - 1);
673 inst->inst_offset = offset;
674 inst->opcode = OP_REGOFFSET;
675 inst->inst_basereg = frame_reg;
677 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
682 inst = m->varinfo [curinst];
683 if (inst->opcode != OP_REGVAR) {
684 inst->opcode = OP_REGOFFSET;
685 inst->inst_basereg = frame_reg;
686 offset += sizeof (gpointer) - 1;
687 offset &= ~(sizeof (gpointer) - 1);
688 inst->inst_offset = offset;
689 offset += sizeof (gpointer);
694 for (i = 0; i < sig->param_count; ++i) {
695 inst = m->varinfo [curinst];
696 if (inst->opcode != OP_REGVAR) {
697 inst->opcode = OP_REGOFFSET;
698 inst->inst_basereg = frame_reg;
699 size = mono_type_size (sig->params [i], &align);
701 offset &= ~(align - 1);
702 inst->inst_offset = offset;
708 /* align the offset to 16 bytes */
713 m->stack_offset = offset;
717 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
718 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
722 * take the arguments and generate the arch-specific
723 * instructions to properly call the function in call.
724 * This includes pushing, moving arguments to the right register
726 * Issue: who does the spilling if needed, and when?
729 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
731 MonoMethodSignature *sig;
737 sig = call->signature;
738 n = sig->param_count + sig->hasthis;
740 cinfo = calculate_sizes (sig, sig->pinvoke);
741 if (cinfo->struct_ret)
742 call->used_iregs |= 1 << cinfo->struct_ret;
744 for (i = 0; i < n; ++i) {
745 ainfo = cinfo->args + i;
746 if (is_virtual && i == 0) {
747 /* the argument will be attached to the call instrucion */
749 call->used_iregs |= 1 << ainfo->reg;
751 MONO_INST_NEW (cfg, arg, OP_OUTARG);
753 arg->cil_code = in->cil_code;
755 arg->type = in->type;
756 /* prepend, we'll need to reverse them later */
757 arg->next = call->out_args;
758 call->out_args = arg;
759 if (ainfo->regtype == RegTypeGeneral) {
760 arg->unused = ainfo->reg;
761 call->used_iregs |= 1 << ainfo->reg;
762 if (arg->type == STACK_I8)
763 call->used_iregs |= 1 << (ainfo->reg + 1);
764 } else if (ainfo->regtype == RegTypeStructByAddr) {
765 /* FIXME: where si the data allocated? */
766 arg->unused = ainfo->reg;
767 call->used_iregs |= 1 << ainfo->reg;
768 } else if (ainfo->regtype == RegTypeStructByVal) {
770 /* mark the used regs */
771 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
772 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
774 arg->opcode = OP_OUTARG_VT;
775 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
776 arg->inst_imm = ainfo->offset;
777 } else if (ainfo->regtype == RegTypeBase) {
778 arg->opcode = OP_OUTARG;
779 arg->unused = ainfo->reg | (ainfo->size << 8);
780 arg->inst_imm = ainfo->offset;
781 } else if (ainfo->regtype == RegTypeFP) {
782 arg->opcode = OP_OUTARG_R8;
783 arg->unused = ainfo->reg;
784 call->used_fregs |= 1 << ainfo->reg;
785 if (ainfo->size == 4) {
786 arg->opcode = OP_OUTARG_R8;
787 /* we reduce the precision */
789 MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
790 conv->inst_left = arg->inst_left;
791 arg->inst_left = conv;*/
794 g_assert_not_reached ();
799 * Reverse the call->out_args list.
802 MonoInst *prev = NULL, *list = call->out_args, *next;
809 call->out_args = prev;
811 call->stack_usage = cinfo->stack_usage;
812 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
813 cfg->flags |= MONO_CFG_HAS_CALLS;
815 * should set more info in call, such as the stack space
816 * used by the args that needs to be added back to esp
824 * Allow tracing to work with this interface (with an optional argument)
828 * This may be needed on some archs or for debugging support.
831 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
833 /* no stack room needed now (may be needed for FASTCALL-trace support) */
835 /* split prolog-epilog requirements? */
836 *code = 50; /* max bytes needed: check this number */
840 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
844 ppc_load (code, ppc_r3, cfg->method);
845 ppc_li (code, ppc_r4, 0); /* NULL ebp for now */
846 ppc_load (code, ppc_r0, func);
847 ppc_mtlr (code, ppc_r0);
861 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
864 int save_mode = SAVE_NONE;
865 MonoMethod *method = cfg->method;
866 int rtype = method->signature->ret->type;
867 int save_offset = PPC_STACK_PARAM_OFFSET + cfg->param_area;
874 /* special case string .ctor icall */
875 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
876 save_mode = SAVE_ONE;
878 save_mode = SAVE_NONE;
882 save_mode = SAVE_TWO;
888 case MONO_TYPE_VALUETYPE:
889 if (method->signature->ret->data.klass->enumtype) {
890 rtype = method->signature->ret->data.klass->enum_basetype->type;
893 save_mode = SAVE_STRUCT;
896 save_mode = SAVE_ONE;
902 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
903 ppc_stw (code, ppc_r4, save_offset + 4, cfg->frame_reg);
904 if (enable_arguments) {
905 ppc_mr (code, ppc_r5, ppc_r4);
906 ppc_mr (code, ppc_r4, ppc_r3);
910 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
911 if (enable_arguments) {
912 ppc_mr (code, ppc_r4, ppc_r3);
916 ppc_stfd (code, ppc_f1, save_offset, cfg->frame_reg);
917 if (enable_arguments) {
918 /* FIXME: what reg? */
919 ppc_fmr (code, ppc_f3, ppc_f1);
920 ppc_lwz (code, ppc_r4, save_offset, cfg->frame_reg);
921 ppc_lwz (code, ppc_r5, save_offset + 4, cfg->frame_reg);
925 if (enable_arguments) {
926 /* FIXME: get the actual address */
927 ppc_mr (code, ppc_r4, ppc_r3);
935 ppc_load (code, ppc_r3, cfg->method);
936 ppc_load (code, ppc_r0, func);
937 ppc_mtlr (code, ppc_r0);
942 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
943 ppc_lwz (code, ppc_r4, save_offset + 4, cfg->frame_reg);
946 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
949 ppc_lfd (code, ppc_f1, save_offset, cfg->frame_reg);
959 * Conditional branches have a small offset, so if it is likely overflowed,
960 * we do a branch to the end of the method (uncond branches have much larger
961 * offsets) where we perform the conditional and jump back unconditionally.
962 * It's slightly slower, since we add two uncond branches, but it's very simple
963 * with the current patch implementation and such large methods are likely not
964 * going to be perf critical anyway.
973 #define EMIT_COND_BRANCH_FLAGS(ins,b0,b1) \
974 if (ins->flags & MONO_INST_BRLABEL) { \
975 if (0 && ins->inst_i0->inst_c0) { \
976 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff); \
978 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
979 ppc_bc (code, (b0), (b1), 0); \
982 if (0 && ins->inst_true_bb->native_offset) { \
983 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
985 int br_disp = ins->inst_true_bb->max_offset - offset; \
986 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
987 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
988 ovfj->bb = ins->inst_true_bb; \
990 ovfj->b0_cond = (b0); \
991 ovfj->b1_cond = (b1); \
992 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB_OVF, ovfj); \
995 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
996 ppc_bc (code, (b0), (b1), 0); \
1001 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_b0_table [(cond)], branch_b1_table [(cond)])
1003 /* emit an exception if condition is fail
1005 * We assign the extra code used to throw the implicit exceptions
1006 * to cfg->bb_exit as far as the big branch handling is concerned
1008 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(b0,b1,exc_name) \
1010 int br_disp = cfg->bb_exit->max_offset - offset; \
1011 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
1012 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
1015 ovfj->b0_cond = (b0); \
1016 ovfj->b1_cond = (b1); \
1017 /* FIXME: test this code */ \
1018 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj); \
1020 cfg->bb_exit->max_offset += 24; \
1022 mono_add_patch_info (cfg, code - cfg->native_code, \
1023 MONO_PATCH_INFO_EXC, exc_name); \
1024 ppc_bc (code, (b0), (b1), 0); \
1028 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_b0_table [(cond)], branch_b1_table [(cond)], (exc_name))
1031 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1033 MonoInst *ins, *last_ins = NULL;
1038 switch (ins->opcode) {
1040 /* remove unnecessary multiplication with 1 */
1041 if (ins->inst_imm == 1) {
1042 if (ins->dreg != ins->sreg1) {
1043 ins->opcode = OP_MOVE;
1045 last_ins->next = ins->next;
1051 case OP_LOAD_MEMBASE:
1052 case OP_LOADI4_MEMBASE:
1054 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1055 * OP_LOAD_MEMBASE offset(basereg), reg
1057 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1058 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1059 ins->inst_basereg == last_ins->inst_destbasereg &&
1060 ins->inst_offset == last_ins->inst_offset) {
1061 if (ins->dreg == last_ins->sreg1) {
1062 last_ins->next = ins->next;
1066 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1067 ins->opcode = OP_MOVE;
1068 ins->sreg1 = last_ins->sreg1;
1072 * Note: reg1 must be different from the basereg in the second load
1073 * OP_LOAD_MEMBASE offset(basereg), reg1
1074 * OP_LOAD_MEMBASE offset(basereg), reg2
1076 * OP_LOAD_MEMBASE offset(basereg), reg1
1077 * OP_MOVE reg1, reg2
1079 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1080 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1081 ins->inst_basereg != last_ins->dreg &&
1082 ins->inst_basereg == last_ins->inst_basereg &&
1083 ins->inst_offset == last_ins->inst_offset) {
1085 if (ins->dreg == last_ins->dreg) {
1086 last_ins->next = ins->next;
1090 ins->opcode = OP_MOVE;
1091 ins->sreg1 = last_ins->dreg;
1094 //g_assert_not_reached ();
1098 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1099 * OP_LOAD_MEMBASE offset(basereg), reg
1101 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1102 * OP_ICONST reg, imm
1104 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1105 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1106 ins->inst_basereg == last_ins->inst_destbasereg &&
1107 ins->inst_offset == last_ins->inst_offset) {
1108 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1109 ins->opcode = OP_ICONST;
1110 ins->inst_c0 = last_ins->inst_imm;
1111 g_assert_not_reached (); // check this rule
1115 case OP_LOADU1_MEMBASE:
1116 case OP_LOADI1_MEMBASE:
1117 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1118 ins->inst_basereg == last_ins->inst_destbasereg &&
1119 ins->inst_offset == last_ins->inst_offset) {
1120 if (ins->dreg == last_ins->sreg1) {
1121 last_ins->next = ins->next;
1125 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1126 ins->opcode = OP_MOVE;
1127 ins->sreg1 = last_ins->sreg1;
1131 case OP_LOADU2_MEMBASE:
1132 case OP_LOADI2_MEMBASE:
1133 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1134 ins->inst_basereg == last_ins->inst_destbasereg &&
1135 ins->inst_offset == last_ins->inst_offset) {
1136 if (ins->dreg == last_ins->sreg1) {
1137 last_ins->next = ins->next;
1141 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1142 ins->opcode = OP_MOVE;
1143 ins->sreg1 = last_ins->sreg1;
1150 ins->opcode = OP_MOVE;
1154 if (ins->dreg == ins->sreg1) {
1156 last_ins->next = ins->next;
1161 * OP_MOVE sreg, dreg
1162 * OP_MOVE dreg, sreg
1164 if (last_ins && last_ins->opcode == OP_MOVE &&
1165 ins->sreg1 == last_ins->dreg &&
1166 ins->dreg == last_ins->sreg1) {
1167 last_ins->next = ins->next;
1176 bb->last_ins = last_ins;
1180 * the branch_b0_table should maintain the order of these
1194 branch_b0_table [] = {
1209 branch_b1_table [] = {
1224 * returns the offset used by spillvar. It allocates a new
1225 * spill variable if necessary.
1228 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1230 MonoSpillInfo **si, *info;
1233 si = &cfg->spill_info;
1235 while (i <= spillvar) {
1238 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1240 info->offset = cfg->stack_offset;
1241 cfg->stack_offset += sizeof (gpointer);
1245 return (*si)->offset;
1251 g_assert_not_reached ();
1256 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1258 MonoSpillInfo **si, *info;
1261 si = &cfg->spill_info_float;
1263 while (i <= spillvar) {
1266 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1268 cfg->stack_offset += 7;
1269 cfg->stack_offset &= ~7;
1270 info->offset = cfg->stack_offset;
1271 cfg->stack_offset += sizeof (double);
1275 return (*si)->offset;
1281 g_assert_not_reached ();
1286 #define DEBUG(a) if (cfg->verbose_level > 1) a
1288 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1289 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 13)
1298 static const char*const * ins_spec = ppcg4;
1301 print_ins (int i, MonoInst *ins)
1303 const char *spec = ins_spec [ins->opcode];
1304 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1305 if (spec [MONO_INST_DEST]) {
1306 if (ins->dreg >= MONO_MAX_IREGS)
1307 g_print (" R%d <-", ins->dreg);
1309 g_print (" %s <-", mono_arch_regname (ins->dreg));
1311 if (spec [MONO_INST_SRC1]) {
1312 if (ins->sreg1 >= MONO_MAX_IREGS)
1313 g_print (" R%d", ins->sreg1);
1315 g_print (" %s", mono_arch_regname (ins->sreg1));
1317 if (spec [MONO_INST_SRC2]) {
1318 if (ins->sreg2 >= MONO_MAX_IREGS)
1319 g_print (" R%d", ins->sreg2);
1321 g_print (" %s", mono_arch_regname (ins->sreg2));
1323 if (spec [MONO_INST_CLOB])
1324 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1329 print_regtrack (RegTrack *t, int num)
1335 for (i = 0; i < num; ++i) {
1338 if (i >= MONO_MAX_IREGS) {
1339 g_snprintf (buf, sizeof(buf), "R%d", i);
1342 r = mono_arch_regname (i);
1343 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1347 typedef struct InstList InstList;
1355 static inline InstList*
1356 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1358 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1368 * Force the spilling of the variable in the symbolic register 'reg'.
1371 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1376 sel = cfg->rs->iassign [reg];
1377 /*i = cfg->rs->isymbolic [sel];
1378 g_assert (i == reg);*/
1380 spill = ++cfg->spill_count;
1381 cfg->rs->iassign [i] = -spill - 1;
1382 mono_regstate_free_int (cfg->rs, sel);
1383 /* we need to create a spill var and insert a load to sel after the current instruction */
1384 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1386 load->inst_basereg = cfg->frame_reg;
1387 load->inst_offset = mono_spillvar_offset (cfg, spill);
1389 while (ins->next != item->prev->data)
1392 load->next = ins->next;
1394 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1395 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1396 g_assert (i == sel);
1402 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1407 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));
1408 /* exclude the registers in the current instruction */
1409 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1410 if (ins->sreg1 >= MONO_MAX_IREGS)
1411 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1413 regmask &= ~ (1 << ins->sreg1);
1414 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1416 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1417 if (ins->sreg2 >= MONO_MAX_IREGS)
1418 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1420 regmask &= ~ (1 << ins->sreg2);
1421 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1423 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1424 regmask &= ~ (1 << ins->dreg);
1425 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1428 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1429 g_assert (regmask); /* need at least a register we can free */
1431 /* we should track prev_use and spill the register that's farther */
1432 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1433 if (regmask & (1 << i)) {
1435 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1439 i = cfg->rs->isymbolic [sel];
1440 spill = ++cfg->spill_count;
1441 cfg->rs->iassign [i] = -spill - 1;
1442 mono_regstate_free_int (cfg->rs, sel);
1443 /* we need to create a spill var and insert a load to sel after the current instruction */
1444 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1446 load->inst_basereg = cfg->frame_reg;
1447 load->inst_offset = mono_spillvar_offset (cfg, spill);
1449 while (ins->next != item->prev->data)
1452 load->next = ins->next;
1454 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1455 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1456 g_assert (i == sel);
1462 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1467 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));
1468 /* exclude the registers in the current instruction */
1469 if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1470 if (ins->sreg1 >= MONO_MAX_FREGS)
1471 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1473 regmask &= ~ (1 << ins->sreg1);
1474 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1476 if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1477 if (ins->sreg2 >= MONO_MAX_FREGS)
1478 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1480 regmask &= ~ (1 << ins->sreg2);
1481 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1483 if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1484 regmask &= ~ (1 << ins->dreg);
1485 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1488 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1489 g_assert (regmask); /* need at least a register we can free */
1491 /* we should track prev_use and spill the register that's farther */
1492 for (i = 0; i < MONO_MAX_FREGS; ++i) {
1493 if (regmask & (1 << i)) {
1495 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1499 i = cfg->rs->fsymbolic [sel];
1500 spill = ++cfg->spill_count;
1501 cfg->rs->fassign [i] = -spill - 1;
1502 mono_regstate_free_float(cfg->rs, sel);
1503 /* we need to create a spill var and insert a load to sel after the current instruction */
1504 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1506 load->inst_basereg = cfg->frame_reg;
1507 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1509 while (ins->next != item->prev->data)
1512 load->next = ins->next;
1514 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)));
1515 i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1516 g_assert (i == sel);
1522 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1525 MONO_INST_NEW (cfg, copy, OP_MOVE);
1529 copy->next = ins->next;
1532 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1537 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1540 MONO_INST_NEW (cfg, copy, OP_FMOVE);
1544 copy->next = ins->next;
1547 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1552 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1555 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1557 store->inst_destbasereg = cfg->frame_reg;
1558 store->inst_offset = mono_spillvar_offset (cfg, spill);
1560 store->next = ins->next;
1563 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)));
1568 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1571 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1573 store->inst_destbasereg = cfg->frame_reg;
1574 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1576 store->next = ins->next;
1579 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)));
1584 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1587 g_assert (item->next);
1588 prev = item->next->data;
1590 while (prev->next != ins)
1592 to_insert->next = ins;
1593 prev->next = to_insert;
1595 * needed otherwise in the next instruction we can add an ins to the
1596 * end and that would get past this instruction.
1598 item->data = to_insert;
1602 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1604 int val = cfg->rs->iassign [sym_reg];
1608 /* the register gets spilled after this inst */
1611 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1613 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1614 cfg->rs->iassign [sym_reg] = val;
1615 /* add option to store before the instruction for src registers */
1617 create_spilled_store (cfg, spill, val, sym_reg, ins);
1619 cfg->rs->isymbolic [val] = sym_reg;
1623 /* use ppc_r3-ppc_10,ppc_r12 as temp registers, f1-f13 for FP registers */
1624 #define PPC_CALLER_REGS ((0xff<<3) | (1<<12) | USE_EXTRA_TEMPS)
1625 #define PPC_CALLER_FREGS (0x3ffe)
1628 * Local register allocation.
1629 * We first scan the list of instructions and we save the liveness info of
1630 * each register (when the register is first used, when it's value is set etc.).
1631 * We also reverse the list of instructions (in the InstList list) because assigning
1632 * registers backwards allows for more tricks to be used.
1635 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1638 MonoRegState *rs = cfg->rs;
1640 RegTrack *reginfo, *reginfof;
1641 RegTrack *reginfo1, *reginfo2, *reginfod;
1642 InstList *tmp, *reversed = NULL;
1644 guint32 src1_mask, src2_mask, dest_mask;
1645 guint32 cur_iregs, cur_fregs;
1649 rs->next_vireg = bb->max_ireg;
1650 rs->next_vfreg = bb->max_freg;
1651 mono_regstate_assign (rs);
1652 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1653 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1654 rs->ifree_mask = PPC_CALLER_REGS;
1655 rs->ffree_mask = PPC_CALLER_FREGS;
1659 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1660 /* forward pass on the instructions to collect register liveness info */
1662 spec = ins_spec [ins->opcode];
1663 DEBUG (print_ins (i, ins));
1664 if (spec [MONO_INST_CLOB] == 'c') {
1665 MonoCallInst * call = (MonoCallInst*)ins;
1668 if (spec [MONO_INST_SRC1]) {
1669 if (spec [MONO_INST_SRC1] == 'f')
1670 reginfo1 = reginfof;
1673 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1674 reginfo1 [ins->sreg1].last_use = i;
1678 if (spec [MONO_INST_SRC2]) {
1679 if (spec [MONO_INST_SRC2] == 'f')
1680 reginfo2 = reginfof;
1683 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1684 reginfo2 [ins->sreg2].last_use = i;
1688 if (spec [MONO_INST_DEST]) {
1689 if (spec [MONO_INST_DEST] == 'f')
1690 reginfod = reginfof;
1693 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1694 reginfod [ins->dreg].killed_in = i;
1695 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1696 reginfod [ins->dreg].last_use = i;
1697 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1698 reginfod [ins->dreg].born_in = i;
1699 if (spec [MONO_INST_DEST] == 'l') {
1700 /* result in eax:edx, the virtual register is allocated sequentially */
1701 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1702 reginfod [ins->dreg + 1].last_use = i;
1703 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1704 reginfod [ins->dreg + 1].born_in = i;
1709 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1714 cur_iregs = PPC_CALLER_REGS;
1715 cur_fregs = PPC_CALLER_FREGS;
1717 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1718 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1721 int prev_dreg, prev_sreg1, prev_sreg2;
1724 spec = ins_spec [ins->opcode];
1725 DEBUG (g_print ("processing:"));
1726 DEBUG (print_ins (i, ins));
1727 /* make the register available for allocation: FIXME add fp reg */
1728 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1729 cur_iregs |= 1 << ins->dreg;
1730 DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1731 } else if (ins->opcode == OP_SETFREG) {
1732 cur_fregs |= 1 << ins->dreg;
1733 DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1734 } else if (spec [MONO_INST_CLOB] == 'c') {
1735 MonoCallInst *cinst = (MonoCallInst*)ins;
1736 DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1737 DEBUG (g_print ("excluding fpregs 0x%x from cur_fregs (0x%x)\n", cinst->used_fregs, cur_fregs));
1738 cur_iregs &= ~cinst->used_iregs;
1739 cur_fregs &= ~cinst->used_fregs;
1740 DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1741 DEBUG (g_print ("available cur_fregs: 0x%x\n", cur_fregs));
1742 /* registers used by the calling convention are excluded from
1743 * allocation: they will be selectively enabled when they are
1744 * assigned by the special SETREG opcodes.
1747 dest_mask = src1_mask = src2_mask = cur_iregs;
1748 /* update for use with FP regs... */
1749 if (spec [MONO_INST_DEST] == 'f') {
1750 dest_mask = cur_fregs;
1751 if (ins->dreg >= MONO_MAX_FREGS) {
1752 val = rs->fassign [ins->dreg];
1753 prev_dreg = ins->dreg;
1757 /* the register gets spilled after this inst */
1760 val = mono_regstate_alloc_float (rs, dest_mask);
1762 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1763 rs->fassign [ins->dreg] = val;
1765 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1767 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1768 rs->fsymbolic [val] = prev_dreg;
1770 if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != ppc_f1) {
1771 /* this instruction only outputs to ppc_f1, need to copy */
1772 create_copy_ins_float (cfg, ins->dreg, ppc_f1, ins);
1777 if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1778 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1779 mono_regstate_free_float (rs, ins->dreg);
1781 } else if (ins->dreg >= MONO_MAX_IREGS) {
1782 val = rs->iassign [ins->dreg];
1783 prev_dreg = ins->dreg;
1787 /* the register gets spilled after this inst */
1790 val = mono_regstate_alloc_int (rs, dest_mask);
1792 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1793 rs->iassign [ins->dreg] = val;
1795 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1797 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1798 rs->isymbolic [val] = prev_dreg;
1800 if (spec [MONO_INST_DEST] == 'l') {
1801 int hreg = prev_dreg + 1;
1802 val = rs->iassign [hreg];
1806 /* the register gets spilled after this inst */
1809 val = mono_regstate_alloc_int (rs, dest_mask);
1811 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1812 rs->iassign [hreg] = val;
1814 create_spilled_store (cfg, spill, val, hreg, ins);
1816 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1817 rs->isymbolic [val] = hreg;
1818 /* FIXME:? ins->dreg = val; */
1819 if (ins->dreg == ppc_r4) {
1821 create_copy_ins (cfg, val, ppc_r3, ins);
1822 } else if (ins->dreg == ppc_r3) {
1823 if (val == ppc_r4) {
1825 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1826 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1827 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1829 /* two forced copies */
1830 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1831 create_copy_ins (cfg, val, ppc_r3, ins);
1834 if (val == ppc_r3) {
1835 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1837 /* two forced copies */
1838 create_copy_ins (cfg, val, ppc_r3, ins);
1839 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1842 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
1843 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1844 mono_regstate_free_int (rs, val);
1846 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != ppc_r3 && spec [MONO_INST_CLOB] != 'd') {
1847 /* this instruction only outputs to ppc_r3, need to copy */
1848 create_copy_ins (cfg, ins->dreg, ppc_r3, ins);
1853 if (spec [MONO_INST_DEST] == 'f' && freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i)) {
1854 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1855 mono_regstate_free_float (rs, ins->dreg);
1856 } else if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
1857 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1858 mono_regstate_free_int (rs, ins->dreg);
1860 if (spec [MONO_INST_SRC1] == 'f') {
1861 src1_mask = cur_fregs;
1862 if (ins->sreg1 >= MONO_MAX_FREGS) {
1863 val = rs->fassign [ins->sreg1];
1864 prev_sreg1 = ins->sreg1;
1868 /* the register gets spilled after this inst */
1871 //g_assert (val == -1); /* source cannot be spilled */
1872 val = mono_regstate_alloc_float (rs, src1_mask);
1874 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1875 rs->fassign [ins->sreg1] = val;
1876 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1878 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
1879 insert_before_ins (ins, tmp, store);
1882 rs->fsymbolic [val] = prev_sreg1;
1887 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
1888 val = rs->iassign [ins->sreg1];
1889 prev_sreg1 = ins->sreg1;
1893 /* the register gets spilled after this inst */
1896 if (0 && ins->opcode == OP_MOVE) {
1898 * small optimization: the dest register is already allocated
1899 * but the src one is not: we can simply assign the same register
1900 * here and peephole will get rid of the instruction later.
1901 * This optimization may interfere with the clobbering handling:
1902 * it removes a mov operation that will be added again to handle clobbering.
1903 * There are also some other issues that should with make testjit.
1905 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1906 val = rs->iassign [ins->sreg1] = ins->dreg;
1907 //g_assert (val >= 0);
1908 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1910 //g_assert (val == -1); /* source cannot be spilled */
1911 val = mono_regstate_alloc_int (rs, src1_mask);
1913 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1914 rs->iassign [ins->sreg1] = val;
1915 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1918 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1919 insert_before_ins (ins, tmp, store);
1922 rs->isymbolic [val] = prev_sreg1;
1927 if (spec [MONO_INST_SRC2] == 'f') {
1928 src2_mask = cur_fregs;
1929 if (ins->sreg2 >= MONO_MAX_FREGS) {
1930 val = rs->fassign [ins->sreg2];
1931 prev_sreg2 = ins->sreg2;
1935 /* the register gets spilled after this inst */
1938 val = mono_regstate_alloc_float (rs, src2_mask);
1940 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1941 rs->fassign [ins->sreg2] = val;
1942 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1944 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
1946 rs->fsymbolic [val] = prev_sreg2;
1951 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
1952 val = rs->iassign [ins->sreg2];
1953 prev_sreg2 = ins->sreg2;
1957 /* the register gets spilled after this inst */
1960 val = mono_regstate_alloc_int (rs, src2_mask);
1962 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1963 rs->iassign [ins->sreg2] = val;
1964 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1966 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1968 rs->isymbolic [val] = prev_sreg2;
1974 if (spec [MONO_INST_CLOB] == 'c') {
1976 guint32 clob_mask = PPC_CALLER_REGS;
1977 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1979 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1980 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1984 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1985 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1986 mono_regstate_free_int (rs, ins->sreg1);
1988 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1989 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1990 mono_regstate_free_int (rs, ins->sreg2);
1993 //DEBUG (print_ins (i, ins));
1999 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2001 /* sreg is a float, dreg is an integer reg. ppc_f0 is used a scratch */
2002 ppc_fctiwz (code, ppc_f0, sreg);
2003 ppc_stfd (code, ppc_f0, -8, ppc_sp);
2004 ppc_lwz (code, dreg, -4, ppc_sp);
2007 ppc_andid (code, dreg, dreg, 0xff);
2009 ppc_andid (code, dreg, dreg, 0xffff);
2012 ppc_extsb (code, dreg, dreg);
2014 ppc_extsh (code, dreg, dreg);
2019 static unsigned char*
2020 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2023 int sreg = tree->sreg1;
2024 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2025 if (tree->flags & MONO_INST_INIT) {
2027 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2028 x86_push_reg (code, X86_EAX);
2031 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2032 x86_push_reg (code, X86_ECX);
2035 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2036 x86_push_reg (code, X86_EDI);
2040 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2041 if (sreg != X86_ECX)
2042 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2043 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2045 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2047 x86_prefix (code, X86_REP_PREFIX);
2050 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2051 x86_pop_reg (code, X86_EDI);
2052 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2053 x86_pop_reg (code, X86_ECX);
2054 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2055 x86_pop_reg (code, X86_EAX);
2068 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2071 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2072 PatchData *pdata = (PatchData*)user_data;
2073 guchar *code = data;
2074 guint32 *thunks = data;
2075 guint32 *endthunks = (guint32*)(code + bsize);
2080 if (!pdata->absolute) {
2081 g_assert (!is_call_imm (pdata->target - pdata->code));
2082 /* make sure a jump is possible from the code to the thunk area */
2083 i = pdata->code - code;
2084 if (!is_call_imm (i))
2086 i = pdata->code + csize - code;
2087 if (!is_call_imm (i))
2091 templ = (guchar*)load;
2092 ppc_lis (templ, ppc_r0, (guint32)(pdata->target) >> 16);
2093 ppc_ori (templ, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2095 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2096 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2097 while (thunks < endthunks) {
2098 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2099 if ((thunks [0] == load [0]) && (thunks [1] == load [1])) {
2100 ppc_patch (pdata->code, (guchar*)thunks);
2101 mono_arch_flush_icache (pdata->code, 4);
2104 } else if ((thunks [0] == 0) && (thunks [1] == 0)) {
2105 /* found a free slot instead: emit thunk */
2106 code = (guchar*)thunks;
2107 ppc_lis (code, ppc_r0, (guint32)(pdata->target) >> 16);
2108 ppc_ori (code, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2109 ppc_mtctr (code, ppc_r0);
2110 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2111 mono_arch_flush_icache ((guchar*)thunks, 16);
2113 ppc_patch (pdata->code, (guchar*)thunks);
2114 mono_arch_flush_icache (pdata->code, 4);
2118 /* skip 16 bytes, the size of the thunk */
2122 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2128 handle_thunk (int absolute, guchar *code, guchar *target) {
2129 MonoDomain *domain = mono_domain_get ();
2133 pdata.target = target;
2134 pdata.absolute = absolute;
2137 mono_domain_lock (domain);
2138 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2141 /* this uses the first available slot */
2143 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2145 mono_domain_unlock (domain);
2147 if (pdata.found != 1)
2148 g_print ("thunk failed for %p from %p\n", target, code);
2149 g_assert (pdata.found == 1);
2153 ppc_patch (guchar *code, guchar *target)
2155 guint32 ins = *(guint32*)code;
2156 guint32 prim = ins >> 26;
2159 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2162 if (target < 33554431){
2163 ins = (18 << 26) | ((guint32) target) | (ins & 1) | 2;
2164 *(guint32*)code = ins;
2168 if (target > -33554432){
2169 ins = (18 << 26) | (((guint32)target) & 0xfc000000) | (ins & 1) | 2;
2170 *(guint32*)code = ins;
2175 gint diff = target - code;
2177 if (diff < 33554431){
2178 ins = (18 << 26) | (diff) | (ins & 1);
2179 *(guint32*)code = ins;
2182 handle_thunk (TRUE, code, target);
2186 /* diff between 0 and -33554432 */
2187 if (diff > -33554432){
2188 ins = (18 << 26) | (diff & ~0xfc000000) | (ins & 1);
2189 *(guint32*)code = ins;
2192 handle_thunk (TRUE, code, target);
2196 g_assert_not_reached ();
2199 #if OLD_REFERENCE_CODE
2202 gint diff = (gint)target;
2203 if ((diff < -33554432) || (diff > 33554431)) {
2204 diff = target - code;
2205 if (is_call_imm (diff)) {
2206 handle_thunk (TRUE, code, target);
2209 /* change it to relative */
2212 ins = prim << 26 | (ins & 3);
2213 diff &= ~0xfc000003;
2216 gint diff = target - code;
2217 if (is_call_imm (target)) {
2218 /* we change it into an absolute reference */
2219 ins = prim << 26 | (ins & 3) | 2;
2220 diff = (gint)target;
2221 diff &= ~0xfc000003;
2223 *(guint32*)code = ins;
2226 if (!is_call_imm (diff)) {
2227 handle_thunk (FALSE, code, target);
2230 ins = prim << 26 | (ins & 3);
2231 diff &= ~0xfc000003;
2234 *(guint32*)code = ins;
2241 guint32 li = (guint32)target;
2242 ins = (ins & 0xffff0000) | (ins & 3);
2243 ovf = li & 0xffff0000;
2244 if (ovf != 0 && ovf != 0xffff0000)
2245 g_assert_not_reached ();
2248 // FIXME: assert the top bits of li are 0
2250 gint diff = target - code;
2251 ins = (ins & 0xffff0000) | (ins & 3);
2252 ovf = diff & 0xffff0000;
2253 if (ovf != 0 && ovf != 0xffff0000)
2254 g_assert_not_reached ();
2258 *(guint32*)code = ins;
2260 g_assert_not_reached ();
2262 // g_print ("patched with 0x%08x\n", ins);
2266 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2271 guint8 *code = cfg->native_code + cfg->code_len;
2272 MonoInst *last_ins = NULL;
2273 guint last_offset = 0;
2276 if (cfg->opt & MONO_OPT_PEEPHOLE)
2277 peephole_pass (cfg, bb);
2279 /* we don't align basic blocks of loops on ppc */
2281 if (cfg->verbose_level > 2)
2282 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2284 cpos = bb->max_offset;
2286 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2287 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2288 //g_assert (!mono_compile_aot);
2291 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2292 /* this is not thread save, but good enough */
2293 /* fixme: howto handle overflows? */
2294 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2299 offset = code - cfg->native_code;
2301 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2303 if (offset > (cfg->code_size - max_len - 16)) {
2304 cfg->code_size *= 2;
2305 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2306 code = cfg->native_code + offset;
2308 // if (ins->cil_code)
2309 // g_print ("cil code\n");
2311 switch (ins->opcode) {
2313 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2314 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
2317 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2318 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
2320 case OP_STOREI1_MEMBASE_IMM:
2321 ppc_li (code, ppc_r0, ins->inst_imm);
2322 if (ppc_is_imm16 (ins->inst_offset)) {
2323 ppc_stb (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2325 ppc_load (code, ppc_r11, ins->inst_offset);
2326 ppc_stbx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2329 case OP_STOREI2_MEMBASE_IMM:
2330 ppc_li (code, ppc_r0, ins->inst_imm);
2331 if (ppc_is_imm16 (ins->inst_offset)) {
2332 ppc_sth (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2334 ppc_load (code, ppc_r11, ins->inst_offset);
2335 ppc_sthx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2338 case OP_STORE_MEMBASE_IMM:
2339 case OP_STOREI4_MEMBASE_IMM:
2340 ppc_load (code, ppc_r0, ins->inst_imm);
2341 if (ppc_is_imm16 (ins->inst_offset)) {
2342 ppc_stw (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2344 ppc_load (code, ppc_r11, ins->inst_offset);
2345 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2348 case OP_STOREI1_MEMBASE_REG:
2349 if (ppc_is_imm16 (ins->inst_offset)) {
2350 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2352 ppc_load (code, ppc_r11, ins->inst_offset);
2353 ppc_stbx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2356 case OP_STOREI2_MEMBASE_REG:
2357 if (ppc_is_imm16 (ins->inst_offset)) {
2358 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2360 ppc_load (code, ppc_r11, ins->inst_offset);
2361 ppc_sthx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2364 case OP_STORE_MEMBASE_REG:
2365 case OP_STOREI4_MEMBASE_REG:
2366 if (ppc_is_imm16 (ins->inst_offset)) {
2367 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2369 ppc_load (code, ppc_r11, ins->inst_offset);
2370 ppc_stwx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2376 g_assert_not_reached ();
2377 //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2380 g_assert_not_reached ();
2381 //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2382 //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2384 case OP_LOAD_MEMBASE:
2385 case OP_LOADI4_MEMBASE:
2386 case OP_LOADU4_MEMBASE:
2387 if (ppc_is_imm16 (ins->inst_offset)) {
2388 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2390 ppc_load (code, ppc_r11, ins->inst_offset);
2391 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2394 case OP_LOADI1_MEMBASE:
2395 case OP_LOADU1_MEMBASE:
2396 if (ppc_is_imm16 (ins->inst_offset)) {
2397 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2399 ppc_load (code, ppc_r11, ins->inst_offset);
2400 ppc_lbzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2402 if (ins->opcode == OP_LOADI1_MEMBASE)
2403 ppc_extsb (code, ins->dreg, ins->dreg);
2405 case OP_LOADU2_MEMBASE:
2406 if (ppc_is_imm16 (ins->inst_offset)) {
2407 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2409 ppc_load (code, ppc_r11, ins->inst_offset);
2410 ppc_lhzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2413 case OP_LOADI2_MEMBASE:
2414 if (ppc_is_imm16 (ins->inst_offset)) {
2415 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2417 ppc_load (code, ppc_r11, ins->inst_offset);
2418 ppc_lhax (code, ins->dreg, ppc_r11, ins->inst_basereg);
2422 ppc_extsb (code, ins->dreg, ins->sreg1);
2425 ppc_extsh (code, ins->dreg, ins->sreg1);
2428 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
2431 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
2435 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2436 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2437 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN)))
2438 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2440 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2442 case OP_COMPARE_IMM:
2444 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2445 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2446 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN))) {
2447 if (ppc_is_uimm16 (ins->inst_imm)) {
2448 ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2450 ppc_load (code, ppc_r11, ins->inst_imm);
2451 ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2454 if (ppc_is_imm16 (ins->inst_imm)) {
2455 ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2457 ppc_load (code, ppc_r11, ins->inst_imm);
2458 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2462 case OP_X86_TEST_NULL:
2463 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2469 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2472 ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2475 ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2478 if (ppc_is_imm16 (ins->inst_imm)) {
2479 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2481 ppc_load (code, ppc_r11, ins->inst_imm);
2482 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2486 ppc_load (code, ppc_r11, ins->inst_imm);
2487 ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2491 /* clear summary overflow */
2492 ppc_crxor (code, 28, 28, 28);
2494 ppc_addod (code, ins->dreg, ins->sreg1, ins->sreg2);
2496 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "OverflowException");
2498 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2501 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2504 ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2507 ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2510 // we add the negated value
2511 if (ppc_is_imm16 (-ins->inst_imm))
2512 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2514 ppc_load (code, ppc_r11, ins->inst_imm);
2515 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2519 ppc_load (code, ppc_r11, ins->inst_imm);
2520 ppc_subfe (code, ins->dreg, ins->sreg2, ppc_r11);
2523 g_assert (ppc_is_imm16 (ins->inst_imm));
2524 ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2527 ppc_subfze (code, ins->dreg, ins->sreg1);
2530 /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2531 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2534 if (!(ins->inst_imm & 0xffff0000)) {
2535 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2536 } else if (!(ins->inst_imm & 0xffff)) {
2537 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2539 ppc_load (code, ppc_r11, ins->inst_imm);
2540 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2544 /* clear the summary overflow flag */
2545 ppc_crxor (code, 28, 28, 28);
2546 ppc_divwod (code, ins->dreg, ins->sreg1, ins->sreg2);
2547 /* FIXME: use OverflowException for 0x80000000/-1 */
2548 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2551 /* clear the summary overflow flag */
2552 ppc_crxor (code, 28, 28, 28);
2553 ppc_divwuod (code, ins->dreg, ins->sreg1, ins->sreg2);
2554 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2557 ppc_load (code, ppc_r11, ins->inst_imm);
2558 /* clear the summary overflow flag */
2559 ppc_crxor (code, 28, 28, 28);
2560 ppc_divwod (code, ins->dreg, ins->sreg1, ppc_r11);
2561 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2564 /* clear the summary overflow flag */
2565 ppc_crxor (code, 28, 28, 28);
2566 ppc_divwod (code, ppc_r11, ins->sreg1, ins->sreg2);
2567 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2568 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2569 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2572 /* clear the summary overflow flag */
2573 ppc_crxor (code, 28, 28, 28);
2574 ppc_divwuod (code, ppc_r11, ins->sreg1, ins->sreg2);
2575 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2576 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2577 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2580 ppc_load (code, ppc_r11, ins->inst_imm);
2581 /* clear the summary overflow flag */
2582 ppc_crxor (code, 28, 28, 28);
2583 ppc_divwod (code, ins->dreg, ins->sreg1, ppc_r11);
2584 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2585 ppc_mullw (code, ins->dreg, ins->dreg, ppc_r11);
2586 ppc_subf (code, ins->dreg, ins->dreg, ins->sreg1);
2589 ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2592 if (!(ins->inst_imm & 0xffff0000)) {
2593 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2594 } else if (!(ins->inst_imm & 0xffff)) {
2595 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2597 ppc_load (code, ppc_r11, ins->inst_imm);
2598 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2602 ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2605 if (!(ins->inst_imm & 0xffff0000)) {
2606 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2607 } else if (!(ins->inst_imm & 0xffff)) {
2608 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2610 ppc_load (code, ppc_r11, ins->inst_imm);
2611 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2615 ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2618 ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2619 //ppc_load (code, ppc_r11, ins->inst_imm);
2620 //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2623 ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2626 // there is also ppc_srawi
2627 //ppc_load (code, ppc_r11, ins->inst_imm);
2628 //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2629 ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2632 ppc_load (code, ppc_r11, ins->inst_imm);
2633 ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);
2634 //ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0xf)), (ins->inst_imm & 0xf), 31);
2637 ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2640 ppc_not (code, ins->dreg, ins->sreg1);
2643 ppc_neg (code, ins->dreg, ins->sreg1);
2646 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2649 ppc_load (code, ppc_r11, ins->inst_imm);
2650 ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2653 ppc_mullwo (code, ins->dreg, ins->sreg1, ins->sreg2);
2654 ppc_mcrxr (code, 0);
2655 EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2657 case CEE_MUL_OVF_UN:
2658 /* we first multiply to get the high word and compare to 0
2659 * to set the flags, then the result is discarded and then
2660 * we multiply to get the lower * bits result
2662 ppc_mulhwu (code, ppc_r0, ins->sreg1, ins->sreg2);
2663 ppc_cmpi (code, 0, 0, ppc_r0, 0);
2664 EMIT_COND_SYSTEM_EXCEPTION (CEE_BNE_UN - CEE_BEQ, ins->inst_p1);
2665 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2669 ppc_load (code, ins->dreg, ins->inst_c0);
2672 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2673 ppc_lis (code, ins->dreg, 0);
2674 ppc_ori (code, ins->dreg, ins->dreg, 0);
2680 ppc_mr (code, ins->dreg, ins->sreg1);
2683 int saved = ins->sreg1;
2684 if (ins->sreg1 == ppc_r3) {
2685 ppc_mr (code, ppc_r0, ins->sreg1);
2688 if (ins->sreg2 != ppc_r3)
2689 ppc_mr (code, ppc_r3, ins->sreg2);
2690 if (saved != ppc_r4)
2691 ppc_mr (code, ppc_r4, saved);
2696 ppc_fmr (code, ins->dreg, ins->sreg1);
2698 case OP_FCONV_TO_R4:
2699 ppc_frsp (code, ins->dreg, ins->sreg1);
2705 * Keep in sync with mono_arch_emit_epilog
2707 g_assert (!cfg->method->save_lmf);
2708 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2709 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
2710 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
2712 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
2713 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
2715 ppc_mtlr (code, ppc_r0);
2717 if (ppc_is_imm16 (cfg->stack_usage)) {
2718 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2720 ppc_load (code, ppc_r11, cfg->stack_usage);
2721 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
2723 if (!cfg->method->save_lmf) {
2724 /*for (i = 31; i >= 14; --i) {
2725 if (cfg->used_float_regs & (1 << i)) {
2726 pos += sizeof (double);
2727 ppc_lfd (code, i, -pos, cfg->frame_reg);
2730 for (i = 31; i >= 13; --i) {
2731 if (cfg->used_int_regs & (1 << i)) {
2732 pos += sizeof (gulong);
2733 ppc_lwz (code, i, -pos, cfg->frame_reg);
2737 /* FIXME restore from MonoLMF: though this can't happen yet */
2739 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2744 /* ensure ins->sreg1 is not NULL */
2745 ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2752 call = (MonoCallInst*)ins;
2753 if (ins->flags & MONO_INST_HAS_METHOD)
2754 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2756 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2762 case OP_VOIDCALL_REG:
2764 ppc_mtlr (code, ins->sreg1);
2767 case OP_FCALL_MEMBASE:
2768 case OP_LCALL_MEMBASE:
2769 case OP_VCALL_MEMBASE:
2770 case OP_VOIDCALL_MEMBASE:
2771 case OP_CALL_MEMBASE:
2772 ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2773 ppc_mtlr (code, ppc_r0);
2777 g_assert_not_reached ();
2780 /* keep alignment */
2781 int alloca_waste = PPC_STACK_PARAM_OFFSET + cfg->param_area + 15;
2782 ppc_addi (code, ppc_r11, ins->sreg1, alloca_waste);
2783 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 0, 27);
2784 ppc_lwz (code, ppc_r0, 0, ppc_sp);
2785 ppc_neg (code, ppc_r11, ppc_r11);
2786 ppc_stwux (code, ppc_r0, ppc_sp, ppc_r11);
2787 ppc_addi (code, ins->dreg, ppc_sp, PPC_STACK_PARAM_OFFSET + cfg->param_area);
2795 ppc_mr (code, ppc_r3, ins->sreg1);
2796 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2797 (gpointer)"mono_arch_throw_exception");
2801 case OP_START_HANDLER:
2802 ppc_mflr (code, ppc_r0);
2803 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2804 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2806 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2807 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_left->inst_basereg);
2811 if (ins->sreg1 != ppc_r3)
2812 ppc_mr (code, ppc_r3, ins->sreg1);
2813 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2814 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2816 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2817 ppc_lwzx (code, ppc_r0, ins->inst_left->inst_basereg, ppc_r11);
2819 ppc_mtlr (code, ppc_r0);
2822 case CEE_ENDFINALLY:
2823 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2824 ppc_mtlr (code, ppc_r0);
2827 case OP_CALL_HANDLER:
2828 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2832 ins->inst_c0 = code - cfg->native_code;
2835 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2836 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2838 if (ins->flags & MONO_INST_BRLABEL) {
2839 /*if (ins->inst_i0->inst_c0) {
2841 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2843 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2847 /*if (ins->inst_target_bb->native_offset) {
2849 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2851 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2857 ppc_mtctr (code, ins->sreg1);
2858 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2861 ppc_li (code, ins->dreg, 0);
2862 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2863 ppc_li (code, ins->dreg, 1);
2867 ppc_li (code, ins->dreg, 1);
2868 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2869 ppc_li (code, ins->dreg, 0);
2873 ppc_li (code, ins->dreg, 1);
2874 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2875 ppc_li (code, ins->dreg, 0);
2877 case OP_COND_EXC_EQ:
2878 case OP_COND_EXC_NE_UN:
2879 case OP_COND_EXC_LT:
2880 case OP_COND_EXC_LT_UN:
2881 case OP_COND_EXC_GT:
2882 case OP_COND_EXC_GT_UN:
2883 case OP_COND_EXC_GE:
2884 case OP_COND_EXC_GE_UN:
2885 case OP_COND_EXC_LE:
2886 case OP_COND_EXC_LE_UN:
2887 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2890 /* move XER [0-3] (SO, OV, CA) into CR
2891 * this translates to LT, GT, EQ.
2892 * FIXME: test for all the conditions occourring
2894 ppc_mcrxr (code, 0);
2895 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, ins->inst_p1);
2897 case OP_COND_EXC_OV:
2898 ppc_mcrxr (code, 0);
2899 EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2901 case OP_COND_EXC_NC:
2902 case OP_COND_EXC_NO:
2903 g_assert_not_reached ();
2915 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2918 /* floating point opcodes */
2920 ppc_load (code, ppc_r11, ins->inst_p0);
2921 ppc_lfd (code, ins->dreg, 0, ppc_r11);
2924 ppc_load (code, ppc_r11, ins->inst_p0);
2925 ppc_lfs (code, ins->dreg, 0, ppc_r11);
2927 case OP_STORER8_MEMBASE_REG:
2928 if (ppc_is_imm16 (ins->inst_offset)) {
2929 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2931 ppc_load (code, ppc_r11, ins->inst_offset);
2932 ppc_stfdx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2935 case OP_LOADR8_MEMBASE:
2936 if (ppc_is_imm16 (ins->inst_offset)) {
2937 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2939 ppc_load (code, ppc_r11, ins->inst_offset);
2940 ppc_lfdx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2943 case OP_STORER4_MEMBASE_REG:
2944 if (ppc_is_imm16 (ins->inst_offset)) {
2945 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2947 ppc_load (code, ppc_r11, ins->inst_offset);
2948 ppc_stfsx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2951 case OP_LOADR4_MEMBASE:
2952 if (ppc_is_imm16 (ins->inst_offset)) {
2953 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2955 ppc_load (code, ppc_r11, ins->inst_offset);
2956 ppc_lfsx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2959 case CEE_CONV_R_UN: {
2960 static const guint64 adjust_val = 0x4330000000000000UL;
2961 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2962 ppc_stw (code, ppc_r0, -8, ppc_sp);
2963 ppc_stw (code, ins->sreg1, -4, ppc_sp);
2964 ppc_load (code, ppc_r11, &adjust_val);
2965 ppc_lfd (code, ins->dreg, -8, ppc_sp);
2966 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2967 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2970 case CEE_CONV_R4: /* FIXME: change precision */
2972 static const guint64 adjust_val = 0x4330000080000000UL;
2973 // addis is special for ppc_r0
2974 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2975 ppc_stw (code, ppc_r0, -8, ppc_sp);
2976 ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
2977 ppc_stw (code, ppc_r11, -4, ppc_sp);
2978 ppc_lfd (code, ins->dreg, -8, ppc_sp);
2979 ppc_load (code, ppc_r11, &adjust_val);
2980 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2981 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2984 case OP_X86_FP_LOAD_I8:
2985 g_assert_not_reached ();
2986 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
2988 case OP_X86_FP_LOAD_I4:
2989 g_assert_not_reached ();
2990 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
2992 case OP_FCONV_TO_I1:
2993 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
2995 case OP_FCONV_TO_U1:
2996 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
2998 case OP_FCONV_TO_I2:
2999 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
3001 case OP_FCONV_TO_U2:
3002 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
3004 case OP_FCONV_TO_I4:
3006 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
3008 case OP_FCONV_TO_U4:
3010 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
3012 case OP_FCONV_TO_I8:
3013 case OP_FCONV_TO_U8:
3014 g_assert_not_reached ();
3015 /* Implemented as helper calls */
3017 case OP_LCONV_TO_R_UN:
3018 g_assert_not_reached ();
3019 /* Implemented as helper calls */
3021 case OP_LCONV_TO_OVF_I: {
3022 ppc_mr (code, ins->dreg, ins->sreg1);
3023 /* FIXME: emit exception if needed */
3027 ppc_fsqrtd (code, ins->dreg, ins->sreg1);
3030 ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
3033 ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
3036 ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
3039 ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
3042 ppc_fneg (code, ins->dreg, ins->sreg1);
3046 g_assert_not_reached ();
3049 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3052 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3053 ppc_li (code, ins->dreg, 0);
3054 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
3055 ppc_li (code, ins->dreg, 1);
3058 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3059 ppc_li (code, ins->dreg, 1);
3060 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3061 ppc_li (code, ins->dreg, 0);
3064 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3065 ppc_li (code, ins->dreg, 1);
3066 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3067 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3068 ppc_li (code, ins->dreg, 0);
3071 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3072 ppc_li (code, ins->dreg, 1);
3073 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3074 ppc_li (code, ins->dreg, 0);
3077 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3078 ppc_li (code, ins->dreg, 1);
3079 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3080 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3081 ppc_li (code, ins->dreg, 0);
3084 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
3087 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
3090 EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
3093 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3094 EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
3097 EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
3100 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3101 EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
3104 EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
3107 EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
3110 EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
3113 EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
3115 case CEE_CKFINITE: {
3116 ppc_stfd (code, ins->sreg1, -8, ppc_sp);
3117 ppc_lwz (code, ppc_r11, -8, ppc_sp);
3118 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
3119 ppc_addis (code, ppc_r11, ppc_r11, -32752);
3120 ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
3121 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");
3125 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3126 g_assert_not_reached ();
3129 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
3130 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3131 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3132 g_assert_not_reached ();
3138 last_offset = offset;
3143 cfg->code_len = code - cfg->native_code;
3147 mono_arch_register_lowlevel_calls (void)
3151 #define patch_lis_ori(ip,val) do {\
3152 guint16 *__lis_ori = (guint16*)(ip); \
3153 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
3154 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
3158 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3160 MonoJumpInfo *patch_info;
3162 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3163 unsigned char *ip = patch_info->ip.i + code;
3164 const unsigned char *target = NULL;
3166 switch (patch_info->type) {
3167 case MONO_PATCH_INFO_BB:
3168 target = patch_info->data.bb->native_offset + code;
3170 case MONO_PATCH_INFO_ABS:
3171 target = patch_info->data.target;
3173 case MONO_PATCH_INFO_LABEL:
3174 target = patch_info->data.inst->inst_c0 + code;
3176 case MONO_PATCH_INFO_IP:
3177 patch_lis_ori (ip, ip);
3179 case MONO_PATCH_INFO_METHOD_REL:
3180 g_assert_not_reached ();
3181 *((gpointer *)(ip)) = code + patch_info->data.offset;
3183 case MONO_PATCH_INFO_INTERNAL_METHOD: {
3184 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
3186 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
3187 g_assert_not_reached ();
3189 target = mono_icall_get_wrapper (mi);
3192 case MONO_PATCH_INFO_METHOD_JUMP: {
3195 /* get the trampoline to the method from the domain */
3196 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
3197 if (!domain->jump_target_hash)
3198 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
3199 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
3200 list = g_slist_prepend (list, ip);
3201 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
3204 case MONO_PATCH_INFO_METHOD:
3205 if (patch_info->data.method == method) {
3208 /* get the trampoline to the method from the domain */
3209 target = mono_arch_create_jit_trampoline (patch_info->data.method);
3212 case MONO_PATCH_INFO_SWITCH: {
3213 gpointer *table = (gpointer *)patch_info->data.target;
3216 // FIXME: inspect code to get the register
3217 ppc_load (ip, ppc_r11, patch_info->data.target);
3218 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3220 for (i = 0; i < patch_info->table_size; i++) {
3221 table [i] = (int)patch_info->data.table [i] + code;
3223 /* we put into the table the absolute address, no need for ppc_patch in this case */
3226 case MONO_PATCH_INFO_METHODCONST:
3227 case MONO_PATCH_INFO_CLASS:
3228 case MONO_PATCH_INFO_IMAGE:
3229 case MONO_PATCH_INFO_FIELD:
3230 /* from OP_AOTCONST : lis + ori */
3231 patch_lis_ori (ip, patch_info->data.target);
3233 case MONO_PATCH_INFO_R4:
3234 case MONO_PATCH_INFO_R8:
3235 g_assert_not_reached ();
3236 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3238 case MONO_PATCH_INFO_IID:
3239 mono_class_init (patch_info->data.klass);
3240 patch_lis_ori (ip, patch_info->data.klass->interface_id);
3242 case MONO_PATCH_INFO_VTABLE:
3243 target = mono_class_vtable (domain, patch_info->data.klass);
3244 patch_lis_ori (ip, target);
3246 case MONO_PATCH_INFO_CLASS_INIT:
3247 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
3249 case MONO_PATCH_INFO_SFLDA: {
3250 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
3251 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
3252 /* Done by the generated code */
3256 mono_runtime_class_init (vtable);
3258 target = (char*)vtable->data + patch_info->data.field->offset;
3259 patch_lis_ori (ip, target);
3262 case MONO_PATCH_INFO_EXC_NAME:
3263 g_assert_not_reached ();
3264 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3266 case MONO_PATCH_INFO_LDSTR:
3267 target = mono_ldstr (domain, patch_info->data.token->image,
3268 mono_metadata_token_index (patch_info->data.token->token));
3269 patch_lis_ori (ip, target);
3271 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
3273 MonoClass *handle_class;
3275 handle = mono_ldtoken (patch_info->data.token->image,
3276 patch_info->data.token->token, &handle_class, NULL);
3277 mono_class_init (handle_class);
3278 mono_class_init (mono_class_from_mono_type (handle));
3280 patch_lis_ori (ip, handle);
3283 case MONO_PATCH_INFO_LDTOKEN: {
3285 MonoClass *handle_class;
3287 handle = mono_ldtoken (patch_info->data.token->image,
3288 patch_info->data.token->token, &handle_class, NULL);
3289 mono_class_init (handle_class);
3291 patch_lis_ori (ip, handle);
3294 case MONO_PATCH_INFO_BB_OVF:
3295 case MONO_PATCH_INFO_EXC_OVF:
3296 /* everything is dealt with at epilog output time */
3299 g_assert_not_reached ();
3301 ppc_patch (ip, target);
3306 mono_arch_max_epilog_size (MonoCompile *cfg)
3308 int exc_count = 0, max_epilog_size = 16 + 20*4;
3309 MonoJumpInfo *patch_info;
3311 if (cfg->method->save_lmf)
3312 max_epilog_size += 128;
3314 if (mono_jit_trace_calls != NULL)
3315 max_epilog_size += 50;
3317 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3318 max_epilog_size += 50;
3320 /* count the number of exception infos */
3323 * make sure we have enough space for exceptions
3324 * 24 is the simulated call to throw_exception_by_name
3326 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3327 if (patch_info->type == MONO_PATCH_INFO_EXC)
3328 max_epilog_size += 24;
3329 else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
3330 max_epilog_size += 12;
3331 else if (patch_info->type == MONO_PATCH_INFO_EXC_OVF)
3332 max_epilog_size += 12;
3335 return max_epilog_size;
3339 * Stack frame layout:
3341 * ------------------- sp
3342 * MonoLMF structure or saved registers
3343 * -------------------
3345 * -------------------
3346 * optional 8 bytes for tracing
3347 * -------------------
3348 * param area size is cfg->param_area
3349 * -------------------
3350 * linkage area size is PPC_STACK_PARAM_OFFSET
3351 * ------------------- sp
3355 mono_arch_emit_prolog (MonoCompile *cfg)
3357 MonoMethod *method = cfg->method;
3359 MonoMethodSignature *sig;
3361 int alloc_size, pos, max_offset, i;
3367 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3370 cfg->code_size = 256;
3371 code = cfg->native_code = g_malloc (cfg->code_size);
3373 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3374 ppc_mflr (code, ppc_r0);
3375 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3377 cfg->used_int_regs |= USE_EXTRA_TEMPS;
3379 alloc_size = cfg->stack_offset;
3382 if (!method->save_lmf) {
3383 /*for (i = 31; i >= 14; --i) {
3384 if (cfg->used_float_regs & (1 << i)) {
3385 pos += sizeof (gdouble);
3386 ppc_stfd (code, i, -pos, ppc_sp);
3389 for (i = 31; i >= 13; --i) {
3390 if (cfg->used_int_regs & (1 << i)) {
3391 pos += sizeof (gulong);
3392 ppc_stw (code, i, -pos, ppc_sp);
3397 pos += sizeof (MonoLMF);
3399 ofs = -pos + G_STRUCT_OFFSET(MonoLMF, iregs);
3400 ppc_stmw (code, ppc_r13, ppc_r1, ofs);
3401 for (i = 14; i < 32; i++) {
3402 ppc_stfd (code, i, (-pos + G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble))), ppc_r1);
3406 // align to PPC_STACK_ALIGNMENT bytes
3407 if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
3408 alloc_size += PPC_STACK_ALIGNMENT - 1;
3409 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
3412 cfg->stack_usage = alloc_size;
3413 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
3415 if (ppc_is_imm16 (-alloc_size)) {
3416 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3418 ppc_load (code, ppc_r11, -alloc_size);
3419 ppc_stwux (code, ppc_sp, ppc_sp, ppc_r11);
3422 if (cfg->frame_reg != ppc_sp)
3423 ppc_mr (code, cfg->frame_reg, ppc_sp);
3425 /* compute max_offset in order to use short forward jumps
3426 * we always do it on ppc because the immediate displacement
3427 * for jumps is too small
3430 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3431 MonoInst *ins = bb->code;
3432 bb->max_offset = max_offset;
3434 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3438 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3443 /* load arguments allocated to register from the stack */
3444 sig = method->signature;
3447 cinfo = calculate_sizes (sig, sig->pinvoke);
3449 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3450 ArgInfo *ainfo = &cinfo->ret;
3452 if (ppc_is_imm16 (inst->inst_offset)) {
3453 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3455 ppc_load (code, ppc_r11, inst->inst_offset);
3456 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3459 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3460 ArgInfo *ainfo = cinfo->args + i;
3461 inst = cfg->varinfo [pos];
3463 if (inst->opcode == OP_REGVAR) {
3464 if (ainfo->regtype == RegTypeGeneral)
3465 ppc_mr (code, inst->dreg, ainfo->reg);
3466 else if (ainfo->regtype == RegTypeFP)
3467 ppc_fmr (code, inst->dreg, ainfo->reg);
3468 else if (ainfo->regtype == RegTypeBase) {
3469 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3470 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3472 g_assert_not_reached ();
3474 if (cfg->verbose_level > 2)
3475 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3477 /* the argument should be put on the stack: FIXME handle size != word */
3478 if (ainfo->regtype == RegTypeGeneral) {
3479 switch (ainfo->size) {
3481 if (ppc_is_imm16 (inst->inst_offset)) {
3482 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3484 ppc_load (code, ppc_r11, inst->inst_offset);
3485 ppc_stbx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3489 if (ppc_is_imm16 (inst->inst_offset)) {
3490 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3492 ppc_load (code, ppc_r11, inst->inst_offset);
3493 ppc_sthx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3497 if (ppc_is_imm16 (inst->inst_offset + 4)) {
3498 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3499 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3501 ppc_load (code, ppc_r11, inst->inst_offset);
3502 ppc_add (code, ppc_r11, ppc_r11, inst->inst_basereg);
3503 ppc_stw (code, ainfo->reg, 0, ppc_r11);
3504 ppc_stw (code, ainfo->reg + 1, 4, ppc_r11);
3508 if (ppc_is_imm16 (inst->inst_offset)) {
3509 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3511 ppc_load (code, ppc_r11, inst->inst_offset);
3512 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3516 } else if (ainfo->regtype == RegTypeBase) {
3517 /* load the previous stack pointer in r11 */
3518 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3519 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3520 switch (ainfo->size) {
3522 if (ppc_is_imm16 (inst->inst_offset)) {
3523 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3525 ppc_load (code, ppc_r11, inst->inst_offset);
3526 ppc_stbx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3530 if (ppc_is_imm16 (inst->inst_offset)) {
3531 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3533 ppc_load (code, ppc_r11, inst->inst_offset);
3534 ppc_sthx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3538 if (ppc_is_imm16 (inst->inst_offset + 4)) {
3539 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3540 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3541 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3544 g_assert_not_reached ();
3548 if (ppc_is_imm16 (inst->inst_offset)) {
3549 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3551 ppc_load (code, ppc_r11, inst->inst_offset);
3552 ppc_stwx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3556 } else if (ainfo->regtype == RegTypeFP) {
3557 g_assert (ppc_is_imm16 (inst->inst_offset));
3558 if (ainfo->size == 8)
3559 ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3560 else if (ainfo->size == 4)
3561 ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3563 g_assert_not_reached ();
3564 } else if (ainfo->regtype == RegTypeStructByVal) {
3565 int doffset = inst->inst_offset;
3568 g_assert (ppc_is_imm16 (inst->inst_offset));
3569 g_assert (ppc_is_imm16 (inst->inst_offset + ainfo->size * sizeof (gpointer)));
3570 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3571 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3572 soffset += sizeof (gpointer);
3573 doffset += sizeof (gpointer);
3575 if (ainfo->vtsize) {
3576 /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3577 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3578 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3579 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3581 } else if (ainfo->regtype == RegTypeStructByAddr) {
3582 g_assert (ppc_is_imm16 (inst->inst_offset));
3583 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3584 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3586 g_assert_not_reached ();
3591 if (method->save_lmf) {
3593 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3594 (gpointer)"mono_get_lmf_addr");
3596 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3597 /* lmf_offset is the offset from the previous stack pointer,
3598 * alloc_size is the total stack space allocated, so the offset
3599 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
3600 * The pointer to the struct is put in ppc_r11.
3602 ppc_addi (code, ppc_r11, ppc_sp, alloc_size - lmf_offset);
3603 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3604 /* new_lmf->previous_lmf = *lmf_addr */
3605 ppc_lwz (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3606 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3607 /* *(lmf_addr) = r11 */
3608 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3609 /* save method info */
3610 ppc_load (code, ppc_r0, method);
3611 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3612 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3613 /* save the current IP */
3614 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3615 ppc_load (code, ppc_r0, 0x01010101);
3616 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3620 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3622 cfg->code_len = code - cfg->native_code;
3629 mono_arch_emit_epilog (MonoCompile *cfg)
3631 MonoJumpInfo *patch_info;
3632 MonoMethod *method = cfg->method;
3637 * Keep in sync with CEE_JMP
3639 code = cfg->native_code + cfg->code_len;
3641 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3642 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3646 if (method->save_lmf) {
3648 pos += sizeof (MonoLMF);
3650 /* save the frame reg in r8 */
3651 ppc_mr (code, ppc_r8, cfg->frame_reg);
3652 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage - lmf_offset);
3653 /* r5 = previous_lmf */
3654 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3656 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3657 /* *(lmf_addr) = previous_lmf */
3658 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3660 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3662 for (i = 14; i < 32; i++) {
3663 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3665 g_assert (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET));
3666 /* use the saved copy of the frame reg in r8 */
3667 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3668 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, ppc_r8);
3669 ppc_mtlr (code, ppc_r0);
3671 ppc_addic (code, ppc_sp, ppc_r8, cfg->stack_usage);
3673 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3674 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
3675 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3677 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
3678 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
3680 ppc_mtlr (code, ppc_r0);
3682 if (ppc_is_imm16 (cfg->stack_usage)) {
3683 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3685 ppc_load (code, ppc_r11, cfg->stack_usage);
3686 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
3689 /*for (i = 31; i >= 14; --i) {
3690 if (cfg->used_float_regs & (1 << i)) {
3691 pos += sizeof (double);
3692 ppc_lfd (code, i, -pos, ppc_sp);
3695 for (i = 31; i >= 13; --i) {
3696 if (cfg->used_int_regs & (1 << i)) {
3697 pos += sizeof (gulong);
3698 ppc_lwz (code, i, -pos, ppc_sp);
3704 /* add code to raise exceptions */
3705 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3706 switch (patch_info->type) {
3707 case MONO_PATCH_INFO_BB_OVF: {
3708 MonoOvfJump *ovfj = patch_info->data.target;
3709 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3710 /* patch the initial jump */
3711 ppc_patch (ip, code);
3712 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3714 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3715 /* jump back to the true target */
3717 ip = ovfj->bb->native_offset + cfg->native_code;
3718 ppc_patch (code - 4, ip);
3721 case MONO_PATCH_INFO_EXC_OVF: {
3722 MonoOvfJump *ovfj = patch_info->data.target;
3723 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3724 /* patch the initial jump */
3725 ppc_patch (ip, code);
3726 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3728 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3729 /* jump back to the true target */
3731 ip = (char*)ovfj->ip + 4;
3732 ppc_patch (code - 4, ip);
3735 case MONO_PATCH_INFO_EXC: {
3736 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3737 ppc_patch (ip, code);
3738 /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3739 ppc_load (code, ppc_r3, patch_info->data.target);
3740 /* simulate a call from ip */
3741 ppc_load (code, ppc_r0, ip + 4);
3742 ppc_mtlr (code, ppc_r0);
3743 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3744 patch_info->data.name = "mono_arch_throw_exception_by_name";
3745 patch_info->ip.i = code - cfg->native_code;
3755 cfg->code_len = code - cfg->native_code;
3757 g_assert (cfg->code_len < cfg->code_size);
3762 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3767 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3772 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3774 int this_dreg = ppc_r3;
3779 /* add the this argument */
3780 if (this_reg != -1) {
3782 MONO_INST_NEW (cfg, this, OP_SETREG);
3783 this->type = this_type;
3784 this->sreg1 = this_reg;
3785 this->dreg = this_dreg;
3786 mono_bblock_add_inst (cfg->cbb, this);
3791 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3792 vtarg->type = STACK_MP;
3793 vtarg->sreg1 = vt_reg;
3794 vtarg->dreg = ppc_r3;
3795 mono_bblock_add_inst (cfg->cbb, vtarg);
3800 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3802 /* optional instruction, need to detect it
3803 if (cmethod->klass == mono_defaults.math_class) {
3804 if (strcmp (cmethod->name, "Sqrt") == 0)
3812 mono_arch_print_tree (MonoInst *tree, int arity)