2 * mini-ppc.c: PowerPC backend for the Mono code generator
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/debug-helpers.h>
21 int mono_exc_esp_offset = 0;
24 mono_arch_regname (int reg) {
25 static const char * rnames[] = {
26 "ppc_r0", "ppc_sp", "ppc_r2", "ppc_r3", "ppc_r4",
27 "ppc_r5", "ppc_r6", "ppc_r7", "ppc_r8", "ppc_r9",
28 "ppc_r10", "ppc_r11", "ppc_r12", "ppc_r13", "ppc_r14",
29 "ppc_r15", "ppc_r16", "ppc_r17", "ppc_r18", "ppc_r19",
30 "ppc_r20", "ppc_r21", "ppc_r22", "ppc_r23", "ppc_r24",
31 "ppc_r25", "ppc_r26", "ppc_r27", "ppc_r28", "ppc_r29",
34 if (reg >= 0 && reg < 32)
39 /* this function overwrites r0 */
41 emit_memcpy (guint32 *code, int size, int dreg, int doffset, int sreg, int soffset)
43 /* unrolled, use the counter in big */
45 ppc_lwz (code, ppc_r0, soffset, sreg);
46 ppc_stw (code, ppc_r0, doffset, dreg);
52 ppc_lhz (code, ppc_r0, soffset, sreg);
53 ppc_sth (code, ppc_r0, doffset, dreg);
59 ppc_lbz (code, ppc_r0, soffset, sreg);
60 ppc_stb (code, ppc_r0, doffset, dreg);
69 * mono_arch_get_argument_info:
70 * @csig: a method signature
71 * @param_count: the number of parameters to consider
72 * @arg_info: an array to store the result infos
74 * Gathers information on parameters such as size, alignment and
75 * padding. arg_info should be large enought to hold param_count + 1 entries.
77 * Returns the size of the activation frame.
80 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
82 int k, frame_size = 0;
86 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
87 frame_size += sizeof (gpointer);
91 arg_info [0].offset = offset;
94 frame_size += sizeof (gpointer);
98 arg_info [0].size = frame_size;
100 for (k = 0; k < param_count; k++) {
103 size = mono_type_native_stack_size (csig->params [k], &align);
105 size = mono_type_stack_size (csig->params [k], &align);
107 /* ignore alignment for now */
110 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
111 arg_info [k].pad = pad;
113 arg_info [k + 1].pad = 0;
114 arg_info [k + 1].size = size;
116 arg_info [k + 1].offset = offset;
120 align = MONO_ARCH_FRAME_ALIGNMENT;
121 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
122 arg_info [k].pad = pad;
128 * Initialize the cpu to execute managed code.
131 mono_arch_cpu_init (void)
136 * This function returns the optimizations supported on this cpu.
139 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
143 /* no ppc-specific optimizations yet */
144 *exclude_mask = MONO_OPT_INLINE|MONO_OPT_LINEARS;
149 is_regsize_var (MonoType *t) {
158 case MONO_TYPE_OBJECT:
159 case MONO_TYPE_STRING:
160 case MONO_TYPE_CLASS:
161 case MONO_TYPE_SZARRAY:
162 case MONO_TYPE_ARRAY:
164 case MONO_TYPE_VALUETYPE:
165 if (t->data.klass->enumtype)
166 return is_regsize_var (t->data.klass->enum_basetype);
173 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
178 for (i = 0; i < cfg->num_varinfo; i++) {
179 MonoInst *ins = cfg->varinfo [i];
180 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
183 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
186 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
189 /* we can only allocate 32 bit values */
190 if (is_regsize_var (ins->inst_vtype)) {
191 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
192 g_assert (i == vmv->idx);
193 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
200 #define USE_EXTRA_TEMPS ((1<<30) | (1<<29))
201 //#define USE_EXTRA_TEMPS 0
204 mono_arch_get_global_int_regs (MonoCompile *cfg)
208 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
213 for (i = 13; i < top; ++i)
214 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
220 * mono_arch_regalloc_cost:
222 * Return the cost, in number of memory references, of the action of
223 * allocating the variable VMV into a register during global register
227 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
233 // code from ppc/tramp.c, try to keep in sync
234 #define MIN_CACHE_LINE 8
237 mono_arch_flush_icache (guint8 *code, gint size)
243 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
244 asm ("dcbst 0,%0;" : : "r"(p) : "memory");
248 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
249 asm ("icbi 0,%0; sync;" : : "r"(p) : "memory");
255 #define NOT_IMPLEMENTED(x) \
256 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
259 #define ALWAYS_ON_STACK(s) s
260 #define FP_ALSO_IN_REG(s) s
262 #define ALWAYS_ON_STACK(s)
263 #define FP_ALSO_IN_REG(s) s
264 #define ALIGN_DOUBLES
277 guint16 vtsize; /* in param area */
279 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
280 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
294 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
297 if (*gr >= 3 + PPC_NUM_REG_ARGS) {
298 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
299 ainfo->reg = ppc_sp; /* in the caller */
300 ainfo->regtype = RegTypeBase;
303 ALWAYS_ON_STACK (*stack_size += 4);
307 if (*gr >= 3 + PPC_NUM_REG_ARGS - 1) {
309 //*stack_size += (*stack_size % 8);
311 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
312 ainfo->reg = ppc_sp; /* in the caller */
313 ainfo->regtype = RegTypeBase;
320 ALWAYS_ON_STACK (*stack_size += 8);
329 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
332 int n = sig->hasthis + sig->param_count;
334 guint32 stack_size = 0;
335 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
337 fr = PPC_FIRST_FPARG_REG;
338 gr = PPC_FIRST_ARG_REG;
340 /* FIXME: handle returning a struct */
341 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
342 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
343 cinfo->struct_ret = PPC_FIRST_ARG_REG;
348 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
351 DEBUG(printf("params: %d\n", sig->param_count));
352 for (i = 0; i < sig->param_count; ++i) {
353 DEBUG(printf("param %d: ", i));
354 if (sig->params [i]->byref) {
355 DEBUG(printf("byref\n"));
356 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
360 simpletype = sig->params [i]->type;
362 switch (simpletype) {
363 case MONO_TYPE_BOOLEAN:
366 cinfo->args [n].size = 1;
367 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
373 cinfo->args [n].size = 2;
374 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
379 cinfo->args [n].size = 4;
380 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
386 case MONO_TYPE_FNPTR:
387 case MONO_TYPE_CLASS:
388 case MONO_TYPE_OBJECT:
389 case MONO_TYPE_STRING:
390 case MONO_TYPE_SZARRAY:
391 case MONO_TYPE_ARRAY:
392 cinfo->args [n].size = sizeof (gpointer);
393 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
396 case MONO_TYPE_VALUETYPE: {
398 if (sig->params [i]->data.klass->enumtype) {
399 simpletype = sig->params [i]->data.klass->enum_basetype->type;
402 size = mono_class_value_size (sig->params [i]->data.klass, NULL);
403 DEBUG(printf ("load %d bytes struct\n",
404 mono_class_value_size (sig->params [i]->data.klass, NULL)));
405 #if PPC_PASS_STRUCTS_BY_VALUE
407 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
408 cinfo->args [n].regtype = RegTypeStructByVal;
409 if (gr <= PPC_LAST_ARG_REG) {
410 int rest = PPC_LAST_ARG_REG - gr + 1;
411 int n_in_regs = rest >= nwords? nwords: rest;
412 cinfo->args [n].size = n_in_regs;
413 cinfo->args [n].vtsize = nwords - n_in_regs;
414 cinfo->args [n].reg = gr;
417 cinfo->args [n].size = 0;
418 cinfo->args [n].vtsize = nwords;
420 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
421 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
422 stack_size += nwords * sizeof (gpointer);
425 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
426 cinfo->args [n].regtype = RegTypeStructByAddr;
431 case MONO_TYPE_TYPEDBYREF: {
432 int size = sizeof (MonoTypedRef);
433 /* keep in sync or merge with the valuetype case */
434 #if PPC_PASS_STRUCTS_BY_VALUE
436 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
437 cinfo->args [n].regtype = RegTypeStructByVal;
438 if (gr <= PPC_LAST_ARG_REG) {
439 int rest = PPC_LAST_ARG_REG - gr + 1;
440 int n_in_regs = rest >= nwords? nwords: rest;
441 cinfo->args [n].size = n_in_regs;
442 cinfo->args [n].vtsize = nwords - n_in_regs;
443 cinfo->args [n].reg = gr;
446 cinfo->args [n].size = 0;
447 cinfo->args [n].vtsize = nwords;
449 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
450 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
451 stack_size += nwords * sizeof (gpointer);
454 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
455 cinfo->args [n].regtype = RegTypeStructByAddr;
462 cinfo->args [n].size = 8;
463 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
467 cinfo->args [n].size = 4;
469 /* It was 7, now it is 8 in LinuxPPC */
470 if (fr <= PPC_LAST_FPARG_REG) {
471 cinfo->args [n].regtype = RegTypeFP;
472 cinfo->args [n].reg = fr;
474 FP_ALSO_IN_REG (gr ++);
475 ALWAYS_ON_STACK (stack_size += 4);
477 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
478 cinfo->args [n].regtype = RegTypeBase;
479 cinfo->args [n].reg = ppc_sp; /* in the caller*/
485 cinfo->args [n].size = 8;
486 /* It was 7, now it is 8 in LinuxPPC */
487 if (fr <= PPC_LAST_FPARG_REG) {
488 cinfo->args [n].regtype = RegTypeFP;
489 cinfo->args [n].reg = fr;
491 FP_ALSO_IN_REG (gr += 2);
492 ALWAYS_ON_STACK (stack_size += 8);
494 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
495 cinfo->args [n].regtype = RegTypeBase;
496 cinfo->args [n].reg = ppc_sp; /* in the caller*/
502 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
507 simpletype = sig->ret->type;
509 switch (simpletype) {
510 case MONO_TYPE_BOOLEAN:
521 case MONO_TYPE_FNPTR:
522 case MONO_TYPE_CLASS:
523 case MONO_TYPE_OBJECT:
524 case MONO_TYPE_SZARRAY:
525 case MONO_TYPE_ARRAY:
526 case MONO_TYPE_STRING:
527 cinfo->ret.reg = ppc_r3;
531 cinfo->ret.reg = ppc_r3;
535 cinfo->ret.reg = ppc_f1;
536 cinfo->ret.regtype = RegTypeFP;
538 case MONO_TYPE_VALUETYPE:
539 if (sig->ret->data.klass->enumtype) {
540 simpletype = sig->ret->data.klass->enum_basetype->type;
544 case MONO_TYPE_TYPEDBYREF:
548 g_error ("Can't handle as return value 0x%x", sig->ret->type);
552 /* align stack size to 16 */
553 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
554 stack_size = (stack_size + 15) & ~15;
556 cinfo->stack_usage = stack_size;
562 * Set var information according to the calling convention. ppc version.
563 * The locals var stuff should most likely be split in another method.
566 mono_arch_allocate_vars (MonoCompile *m)
568 MonoMethodSignature *sig;
569 MonoMethodHeader *header;
571 int i, offset, size, align, curinst;
572 int frame_reg = ppc_sp;
575 * FIXME: we'll use the frame register also for any method that has
576 * filter clauses. This way, when the handlers are called,
577 * the code will reference local variables using the frame reg instead of
578 * the stack pointer: if we had to restore the stack pointer, we'd
579 * corrupt the method frames that are already on the stack (since
580 * filters get called before stack unwinding happens) when the filter
581 * code would call any method.
583 if (m->flags & MONO_CFG_HAS_ALLOCA)
585 m->frame_reg = frame_reg;
587 header = ((MonoMethodNormal *)m->method)->header;
589 sig = m->method->signature;
593 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
594 m->ret->opcode = OP_REGVAR;
595 m->ret->inst_c0 = ppc_r3;
597 /* FIXME: handle long and FP values */
598 switch (sig->ret->type) {
602 m->ret->opcode = OP_REGVAR;
603 m->ret->inst_c0 = ppc_r3;
607 /* local vars are at a positive offset from the stack pointer */
609 * also note that if the function uses alloca, we use ppc_r31
610 * to point at the local variables.
612 offset = PPC_MINIMAL_STACK_SIZE; /* linkage area */
613 /* align the offset to 16 bytes: not sure this is needed here */
615 //offset &= ~(16 - 1);
617 /* add parameter area size for called functions */
618 offset += m->param_area;
622 /* FIXME: check how to handle this stuff... reserve space to save LMF and caller saved registers */
623 if (m->method->save_lmf)
624 offset += sizeof (MonoLMF);
627 /* this stuff should not be needed on ppc and the new jit,
628 * because a call on ppc to the handlers doesn't change the
629 * stack pointer and the jist doesn't manipulate the stack pointer
630 * for operations involving valuetypes.
632 /* reserve space to store the esp */
633 offset += sizeof (gpointer);
635 /* this is a global constant */
636 mono_exc_esp_offset = offset;
639 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
641 offset += sizeof(gpointer) - 1;
642 offset &= ~(sizeof(gpointer) - 1);
643 inst->inst_offset = offset;
644 inst->opcode = OP_REGOFFSET;
645 inst->inst_basereg = frame_reg;
646 offset += sizeof(gpointer);
648 curinst = m->locals_start;
649 for (i = curinst; i < m->num_varinfo; ++i) {
650 inst = m->varinfo [i];
651 if (inst->opcode == OP_REGVAR)
654 /* inst->unused indicates native sized value types, this is used by the
655 * pinvoke wrappers when they call functions returning structure */
656 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
657 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
659 size = mono_type_size (inst->inst_vtype, &align);
662 offset &= ~(align - 1);
663 inst->inst_offset = offset;
664 inst->opcode = OP_REGOFFSET;
665 inst->inst_basereg = frame_reg;
667 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
672 inst = m->varinfo [curinst];
673 if (inst->opcode != OP_REGVAR) {
674 inst->opcode = OP_REGOFFSET;
675 inst->inst_basereg = frame_reg;
676 offset += sizeof (gpointer) - 1;
677 offset &= ~(sizeof (gpointer) - 1);
678 inst->inst_offset = offset;
679 offset += sizeof (gpointer);
684 for (i = 0; i < sig->param_count; ++i) {
685 inst = m->varinfo [curinst];
686 if (inst->opcode != OP_REGVAR) {
687 inst->opcode = OP_REGOFFSET;
688 inst->inst_basereg = frame_reg;
689 size = mono_type_size (sig->params [i], &align);
691 offset &= ~(align - 1);
692 inst->inst_offset = offset;
698 /* align the offset to 16 bytes */
703 m->stack_offset = offset;
707 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
708 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
712 * take the arguments and generate the arch-specific
713 * instructions to properly call the function in call.
714 * This includes pushing, moving arguments to the right register
716 * Issue: who does the spilling if needed, and when?
719 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
721 MonoMethodSignature *sig;
727 sig = call->signature;
728 n = sig->param_count + sig->hasthis;
730 cinfo = calculate_sizes (sig, sig->pinvoke);
731 if (cinfo->struct_ret)
732 call->used_iregs |= 1 << cinfo->struct_ret;
734 for (i = 0; i < n; ++i) {
735 ainfo = cinfo->args + i;
736 if (is_virtual && i == 0) {
737 /* the argument will be attached to the call instrucion */
739 call->used_iregs |= 1 << ainfo->reg;
741 MONO_INST_NEW (cfg, arg, OP_OUTARG);
743 arg->cil_code = in->cil_code;
745 arg->type = in->type;
746 /* prepend, we'll need to reverse them later */
747 arg->next = call->out_args;
748 call->out_args = arg;
749 if (ainfo->regtype == RegTypeGeneral) {
750 arg->unused = ainfo->reg;
751 call->used_iregs |= 1 << ainfo->reg;
752 if (arg->type == STACK_I8)
753 call->used_iregs |= 1 << (ainfo->reg + 1);
754 } else if (ainfo->regtype == RegTypeStructByAddr) {
755 /* FIXME: where si the data allocated? */
756 arg->unused = ainfo->reg;
757 call->used_iregs |= 1 << ainfo->reg;
758 } else if (ainfo->regtype == RegTypeStructByVal) {
760 /* mark the used regs */
761 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
762 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
764 arg->opcode = OP_OUTARG_VT;
765 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
766 arg->inst_imm = ainfo->offset;
767 } else if (ainfo->regtype == RegTypeBase) {
768 arg->opcode = OP_OUTARG;
769 arg->unused = ainfo->reg | (ainfo->size << 8);
770 arg->inst_imm = ainfo->offset;
771 } else if (ainfo->regtype == RegTypeFP) {
772 arg->opcode = OP_OUTARG_R8;
773 arg->unused = ainfo->reg;
774 call->used_fregs |= 1 << ainfo->reg;
775 if (ainfo->size == 4) {
776 arg->opcode = OP_OUTARG_R8;
777 /* we reduce the precision */
779 MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
780 conv->inst_left = arg->inst_left;
781 arg->inst_left = conv;*/
784 g_assert_not_reached ();
789 * Reverse the call->out_args list.
792 MonoInst *prev = NULL, *list = call->out_args, *next;
799 call->out_args = prev;
801 call->stack_usage = cinfo->stack_usage;
802 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
803 cfg->flags |= MONO_CFG_HAS_CALLS;
805 * should set more info in call, such as the stack space
806 * used by the args that needs to be added back to esp
814 * Allow tracing to work with this interface (with an optional argument)
818 * This may be needed on some archs or for debugging support.
821 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
823 /* no stack room needed now (may be needed for FASTCALL-trace support) */
825 /* split prolog-epilog requirements? */
826 *code = 50; /* max bytes needed: check this number */
830 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
834 ppc_load (code, ppc_r3, cfg->method);
835 ppc_li (code, ppc_r4, 0); /* NULL ebp for now */
836 ppc_load (code, ppc_r0, func);
837 ppc_mtlr (code, ppc_r0);
851 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
854 int save_mode = SAVE_NONE;
855 MonoMethod *method = cfg->method;
856 int rtype = method->signature->ret->type;
861 /* special case string .ctor icall */
862 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
863 save_mode = SAVE_ONE;
865 save_mode = SAVE_NONE;
869 save_mode = SAVE_TWO;
875 case MONO_TYPE_VALUETYPE:
876 if (method->signature->ret->data.klass->enumtype) {
877 rtype = method->signature->ret->data.klass->enum_basetype->type;
880 save_mode = SAVE_STRUCT;
883 save_mode = SAVE_ONE;
889 ppc_stw (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
890 ppc_stw (code, ppc_r4, cfg->stack_usage - 4, cfg->frame_reg);
891 if (enable_arguments) {
892 ppc_mr (code, ppc_r5, ppc_r4);
893 ppc_mr (code, ppc_r4, ppc_r3);
897 ppc_stw (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
898 if (enable_arguments) {
899 ppc_mr (code, ppc_r4, ppc_r3);
903 ppc_stfd (code, ppc_f1, cfg->stack_usage - 8, cfg->frame_reg);
904 if (enable_arguments) {
905 /* FIXME: what reg? */
906 ppc_fmr (code, ppc_f3, ppc_f1);
907 ppc_lwz (code, ppc_r4, cfg->stack_usage - 8, cfg->frame_reg);
908 ppc_lwz (code, ppc_r5, cfg->stack_usage - 4, cfg->frame_reg);
912 if (enable_arguments) {
913 /* FIXME: get the actual address */
914 ppc_mr (code, ppc_r4, ppc_r3);
922 ppc_load (code, ppc_r3, cfg->method);
923 ppc_load (code, ppc_r0, func);
924 ppc_mtlr (code, ppc_r0);
929 ppc_lwz (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
930 ppc_lwz (code, ppc_r4, cfg->stack_usage - 4, cfg->frame_reg);
933 ppc_lwz (code, ppc_r3, cfg->stack_usage - 8, cfg->frame_reg);
936 ppc_lfd (code, ppc_f1, cfg->stack_usage - 8, cfg->frame_reg);
946 * Conditional branches have a small offset, so if it is likely overflowed,
947 * we do a branch to the end of the method (uncond branches have much larger
948 * offsets) where we perform the conditional and jump back unconditionally.
949 * It's slightly slower, since we add two uncond branches, but it's very simple
950 * with the current patch implementation and such large methods are likely not
951 * going to be perf critical anyway.
960 #define EMIT_COND_BRANCH_FLAGS(ins,b0,b1) \
961 if (ins->flags & MONO_INST_BRLABEL) { \
962 if (0 && ins->inst_i0->inst_c0) { \
963 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff); \
965 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
966 ppc_bc (code, (b0), (b1), 0); \
969 if (0 && ins->inst_true_bb->native_offset) { \
970 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
972 int br_disp = ins->inst_true_bb->max_offset - offset; \
973 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
974 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
975 ovfj->bb = ins->inst_true_bb; \
977 ovfj->b0_cond = (b0); \
978 ovfj->b1_cond = (b1); \
979 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB_OVF, ovfj); \
982 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
983 ppc_bc (code, (b0), (b1), 0); \
988 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_b0_table [(cond)], branch_b1_table [(cond)])
990 /* emit an exception if condition is fail
992 * We assign the extra code used to throw the implicit exceptions
993 * to cfg->bb_exit as far as the big branch handling is concerned
995 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(b0,b1,exc_name) \
997 int br_disp = cfg->bb_exit->max_offset - offset; \
998 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
999 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
1002 ovfj->b0_cond = (b0); \
1003 ovfj->b1_cond = (b1); \
1004 /* FIXME: test this code */ \
1005 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj); \
1007 cfg->bb_exit->max_offset += 24; \
1009 mono_add_patch_info (cfg, code - cfg->native_code, \
1010 MONO_PATCH_INFO_EXC, exc_name); \
1011 ppc_bc (code, (b0), (b1), 0); \
1015 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_b0_table [(cond)], branch_b1_table [(cond)], (exc_name))
1018 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1020 MonoInst *ins, *last_ins = NULL;
1025 switch (ins->opcode) {
1027 /* remove unnecessary multiplication with 1 */
1028 if (ins->inst_imm == 1) {
1029 if (ins->dreg != ins->sreg1) {
1030 ins->opcode = OP_MOVE;
1032 last_ins->next = ins->next;
1038 case OP_LOAD_MEMBASE:
1039 case OP_LOADI4_MEMBASE:
1041 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1042 * OP_LOAD_MEMBASE offset(basereg), reg
1044 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1045 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1046 ins->inst_basereg == last_ins->inst_destbasereg &&
1047 ins->inst_offset == last_ins->inst_offset) {
1048 if (ins->dreg == last_ins->sreg1) {
1049 last_ins->next = ins->next;
1053 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1054 ins->opcode = OP_MOVE;
1055 ins->sreg1 = last_ins->sreg1;
1059 * Note: reg1 must be different from the basereg in the second load
1060 * OP_LOAD_MEMBASE offset(basereg), reg1
1061 * OP_LOAD_MEMBASE offset(basereg), reg2
1063 * OP_LOAD_MEMBASE offset(basereg), reg1
1064 * OP_MOVE reg1, reg2
1066 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1067 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1068 ins->inst_basereg != last_ins->dreg &&
1069 ins->inst_basereg == last_ins->inst_basereg &&
1070 ins->inst_offset == last_ins->inst_offset) {
1072 if (ins->dreg == last_ins->dreg) {
1073 last_ins->next = ins->next;
1077 ins->opcode = OP_MOVE;
1078 ins->sreg1 = last_ins->dreg;
1081 //g_assert_not_reached ();
1085 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1086 * OP_LOAD_MEMBASE offset(basereg), reg
1088 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1089 * OP_ICONST reg, imm
1091 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1092 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1093 ins->inst_basereg == last_ins->inst_destbasereg &&
1094 ins->inst_offset == last_ins->inst_offset) {
1095 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1096 ins->opcode = OP_ICONST;
1097 ins->inst_c0 = last_ins->inst_imm;
1098 g_assert_not_reached (); // check this rule
1102 case OP_LOADU1_MEMBASE:
1103 case OP_LOADI1_MEMBASE:
1104 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1105 ins->inst_basereg == last_ins->inst_destbasereg &&
1106 ins->inst_offset == last_ins->inst_offset) {
1107 if (ins->dreg == last_ins->sreg1) {
1108 last_ins->next = ins->next;
1112 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1113 ins->opcode = OP_MOVE;
1114 ins->sreg1 = last_ins->sreg1;
1118 case OP_LOADU2_MEMBASE:
1119 case OP_LOADI2_MEMBASE:
1120 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1121 ins->inst_basereg == last_ins->inst_destbasereg &&
1122 ins->inst_offset == last_ins->inst_offset) {
1123 if (ins->dreg == last_ins->sreg1) {
1124 last_ins->next = ins->next;
1128 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1129 ins->opcode = OP_MOVE;
1130 ins->sreg1 = last_ins->sreg1;
1137 ins->opcode = OP_MOVE;
1141 if (ins->dreg == ins->sreg1) {
1143 last_ins->next = ins->next;
1148 * OP_MOVE sreg, dreg
1149 * OP_MOVE dreg, sreg
1151 if (last_ins && last_ins->opcode == OP_MOVE &&
1152 ins->sreg1 == last_ins->dreg &&
1153 ins->dreg == last_ins->sreg1) {
1154 last_ins->next = ins->next;
1163 bb->last_ins = last_ins;
1167 * the branch_b0_table should maintain the order of these
1181 branch_b0_table [] = {
1196 branch_b1_table [] = {
1211 * returns the offset used by spillvar. It allocates a new
1212 * spill variable if necessary.
1215 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1217 MonoSpillInfo **si, *info;
1220 si = &cfg->spill_info;
1222 while (i <= spillvar) {
1225 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1227 info->offset = cfg->stack_offset;
1228 cfg->stack_offset += sizeof (gpointer);
1232 return (*si)->offset;
1238 g_assert_not_reached ();
1243 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1245 MonoSpillInfo **si, *info;
1248 si = &cfg->spill_info_float;
1250 while (i <= spillvar) {
1253 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1255 cfg->stack_offset += 7;
1256 cfg->stack_offset &= ~7;
1257 info->offset = cfg->stack_offset;
1258 cfg->stack_offset += sizeof (double);
1262 return (*si)->offset;
1268 g_assert_not_reached ();
1273 #define DEBUG(a) if (cfg->verbose_level > 1) a
1275 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1276 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 13)
1285 static const char*const * ins_spec = ppcg4;
1288 print_ins (int i, MonoInst *ins)
1290 const char *spec = ins_spec [ins->opcode];
1291 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1292 if (spec [MONO_INST_DEST]) {
1293 if (ins->dreg >= MONO_MAX_IREGS)
1294 g_print (" R%d <-", ins->dreg);
1296 g_print (" %s <-", mono_arch_regname (ins->dreg));
1298 if (spec [MONO_INST_SRC1]) {
1299 if (ins->sreg1 >= MONO_MAX_IREGS)
1300 g_print (" R%d", ins->sreg1);
1302 g_print (" %s", mono_arch_regname (ins->sreg1));
1304 if (spec [MONO_INST_SRC2]) {
1305 if (ins->sreg2 >= MONO_MAX_IREGS)
1306 g_print (" R%d", ins->sreg2);
1308 g_print (" %s", mono_arch_regname (ins->sreg2));
1310 if (spec [MONO_INST_CLOB])
1311 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1316 print_regtrack (RegTrack *t, int num)
1322 for (i = 0; i < num; ++i) {
1325 if (i >= MONO_MAX_IREGS) {
1326 g_snprintf (buf, sizeof(buf), "R%d", i);
1329 r = mono_arch_regname (i);
1330 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1334 typedef struct InstList InstList;
1342 static inline InstList*
1343 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1345 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1355 * Force the spilling of the variable in the symbolic register 'reg'.
1358 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1363 sel = cfg->rs->iassign [reg];
1364 /*i = cfg->rs->isymbolic [sel];
1365 g_assert (i == reg);*/
1367 spill = ++cfg->spill_count;
1368 cfg->rs->iassign [i] = -spill - 1;
1369 mono_regstate_free_int (cfg->rs, sel);
1370 /* we need to create a spill var and insert a load to sel after the current instruction */
1371 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1373 load->inst_basereg = cfg->frame_reg;
1374 load->inst_offset = mono_spillvar_offset (cfg, spill);
1376 while (ins->next != item->prev->data)
1379 load->next = ins->next;
1381 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1382 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1383 g_assert (i == sel);
1389 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1394 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));
1395 /* exclude the registers in the current instruction */
1396 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1397 if (ins->sreg1 >= MONO_MAX_IREGS)
1398 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1400 regmask &= ~ (1 << ins->sreg1);
1401 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1403 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1404 if (ins->sreg2 >= MONO_MAX_IREGS)
1405 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1407 regmask &= ~ (1 << ins->sreg2);
1408 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1410 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1411 regmask &= ~ (1 << ins->dreg);
1412 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1415 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1416 g_assert (regmask); /* need at least a register we can free */
1418 /* we should track prev_use and spill the register that's farther */
1419 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1420 if (regmask & (1 << i)) {
1422 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1426 i = cfg->rs->isymbolic [sel];
1427 spill = ++cfg->spill_count;
1428 cfg->rs->iassign [i] = -spill - 1;
1429 mono_regstate_free_int (cfg->rs, sel);
1430 /* we need to create a spill var and insert a load to sel after the current instruction */
1431 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1433 load->inst_basereg = cfg->frame_reg;
1434 load->inst_offset = mono_spillvar_offset (cfg, spill);
1436 while (ins->next != item->prev->data)
1439 load->next = ins->next;
1441 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1442 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1443 g_assert (i == sel);
1449 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1454 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));
1455 /* exclude the registers in the current instruction */
1456 if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1457 if (ins->sreg1 >= MONO_MAX_FREGS)
1458 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1460 regmask &= ~ (1 << ins->sreg1);
1461 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1463 if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1464 if (ins->sreg2 >= MONO_MAX_FREGS)
1465 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1467 regmask &= ~ (1 << ins->sreg2);
1468 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1470 if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1471 regmask &= ~ (1 << ins->dreg);
1472 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1475 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1476 g_assert (regmask); /* need at least a register we can free */
1478 /* we should track prev_use and spill the register that's farther */
1479 for (i = 0; i < MONO_MAX_FREGS; ++i) {
1480 if (regmask & (1 << i)) {
1482 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1486 i = cfg->rs->fsymbolic [sel];
1487 spill = ++cfg->spill_count;
1488 cfg->rs->fassign [i] = -spill - 1;
1489 mono_regstate_free_float(cfg->rs, sel);
1490 /* we need to create a spill var and insert a load to sel after the current instruction */
1491 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1493 load->inst_basereg = cfg->frame_reg;
1494 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1496 while (ins->next != item->prev->data)
1499 load->next = ins->next;
1501 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)));
1502 i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1503 g_assert (i == sel);
1509 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1512 MONO_INST_NEW (cfg, copy, OP_MOVE);
1516 copy->next = ins->next;
1519 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1524 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1527 MONO_INST_NEW (cfg, copy, OP_FMOVE);
1531 copy->next = ins->next;
1534 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1539 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1542 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1544 store->inst_destbasereg = cfg->frame_reg;
1545 store->inst_offset = mono_spillvar_offset (cfg, spill);
1547 store->next = ins->next;
1550 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)));
1555 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1558 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1560 store->inst_destbasereg = cfg->frame_reg;
1561 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1563 store->next = ins->next;
1566 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)));
1571 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1574 g_assert (item->next);
1575 prev = item->next->data;
1577 while (prev->next != ins)
1579 to_insert->next = ins;
1580 prev->next = to_insert;
1582 * needed otherwise in the next instruction we can add an ins to the
1583 * end and that would get past this instruction.
1585 item->data = to_insert;
1589 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1591 int val = cfg->rs->iassign [sym_reg];
1595 /* the register gets spilled after this inst */
1598 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1600 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1601 cfg->rs->iassign [sym_reg] = val;
1602 /* add option to store before the instruction for src registers */
1604 create_spilled_store (cfg, spill, val, sym_reg, ins);
1606 cfg->rs->isymbolic [val] = sym_reg;
1610 /* use ppc_r3-ppc_10,ppc_r12 as temp registers, f1-f13 for FP registers */
1611 #define PPC_CALLER_REGS ((0xff<<3) | (1<<12) | USE_EXTRA_TEMPS)
1612 #define PPC_CALLER_FREGS (0x3ffe)
1615 * Local register allocation.
1616 * We first scan the list of instructions and we save the liveness info of
1617 * each register (when the register is first used, when it's value is set etc.).
1618 * We also reverse the list of instructions (in the InstList list) because assigning
1619 * registers backwards allows for more tricks to be used.
1622 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1625 MonoRegState *rs = cfg->rs;
1627 RegTrack *reginfo, *reginfof;
1628 RegTrack *reginfo1, *reginfo2, *reginfod;
1629 InstList *tmp, *reversed = NULL;
1631 guint32 src1_mask, src2_mask, dest_mask;
1632 guint32 cur_iregs, cur_fregs;
1636 rs->next_vireg = bb->max_ireg;
1637 rs->next_vfreg = bb->max_freg;
1638 mono_regstate_assign (rs);
1639 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1640 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1641 rs->ifree_mask = PPC_CALLER_REGS;
1642 rs->ffree_mask = PPC_CALLER_FREGS;
1646 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1647 /* forward pass on the instructions to collect register liveness info */
1649 spec = ins_spec [ins->opcode];
1650 DEBUG (print_ins (i, ins));
1651 if (spec [MONO_INST_CLOB] == 'c') {
1652 MonoCallInst * call = (MonoCallInst*)ins;
1655 if (spec [MONO_INST_SRC1]) {
1656 if (spec [MONO_INST_SRC1] == 'f')
1657 reginfo1 = reginfof;
1660 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1661 reginfo1 [ins->sreg1].last_use = i;
1665 if (spec [MONO_INST_SRC2]) {
1666 if (spec [MONO_INST_SRC2] == 'f')
1667 reginfo2 = reginfof;
1670 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1671 reginfo2 [ins->sreg2].last_use = i;
1675 if (spec [MONO_INST_DEST]) {
1676 if (spec [MONO_INST_DEST] == 'f')
1677 reginfod = reginfof;
1680 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1681 reginfod [ins->dreg].killed_in = i;
1682 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1683 reginfod [ins->dreg].last_use = i;
1684 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1685 reginfod [ins->dreg].born_in = i;
1686 if (spec [MONO_INST_DEST] == 'l') {
1687 /* result in eax:edx, the virtual register is allocated sequentially */
1688 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1689 reginfod [ins->dreg + 1].last_use = i;
1690 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1691 reginfod [ins->dreg + 1].born_in = i;
1696 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1701 cur_iregs = PPC_CALLER_REGS;
1702 cur_fregs = PPC_CALLER_FREGS;
1704 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1705 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1708 int prev_dreg, prev_sreg1, prev_sreg2;
1711 spec = ins_spec [ins->opcode];
1712 DEBUG (g_print ("processing:"));
1713 DEBUG (print_ins (i, ins));
1714 /* make the register available for allocation: FIXME add fp reg */
1715 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1716 cur_iregs |= 1 << ins->dreg;
1717 DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1718 } else if (ins->opcode == OP_SETFREG) {
1719 cur_fregs |= 1 << ins->dreg;
1720 DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1721 } else if (spec [MONO_INST_CLOB] == 'c') {
1722 MonoCallInst *cinst = (MonoCallInst*)ins;
1723 DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1724 DEBUG (g_print ("excluding fpregs 0x%x from cur_fregs (0x%x)\n", cinst->used_fregs, cur_fregs));
1725 cur_iregs &= ~cinst->used_iregs;
1726 cur_fregs &= ~cinst->used_fregs;
1727 DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1728 DEBUG (g_print ("available cur_fregs: 0x%x\n", cur_fregs));
1729 /* registers used by the calling convention are excluded from
1730 * allocation: they will be selectively enabled when they are
1731 * assigned by the special SETREG opcodes.
1734 dest_mask = src1_mask = src2_mask = cur_iregs;
1735 /* update for use with FP regs... */
1736 if (spec [MONO_INST_DEST] == 'f') {
1737 dest_mask = cur_fregs;
1738 if (ins->dreg >= MONO_MAX_FREGS) {
1739 val = rs->fassign [ins->dreg];
1740 prev_dreg = ins->dreg;
1744 /* the register gets spilled after this inst */
1747 val = mono_regstate_alloc_float (rs, dest_mask);
1749 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1750 rs->fassign [ins->dreg] = val;
1752 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1754 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1755 rs->fsymbolic [val] = prev_dreg;
1757 if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != ppc_f1) {
1758 /* this instruction only outputs to ppc_f1, need to copy */
1759 create_copy_ins_float (cfg, ins->dreg, ppc_f1, ins);
1764 if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1765 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1766 mono_regstate_free_float (rs, ins->dreg);
1768 } else if (ins->dreg >= MONO_MAX_IREGS) {
1769 val = rs->iassign [ins->dreg];
1770 prev_dreg = ins->dreg;
1774 /* the register gets spilled after this inst */
1777 val = mono_regstate_alloc_int (rs, dest_mask);
1779 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1780 rs->iassign [ins->dreg] = val;
1782 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1784 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1785 rs->isymbolic [val] = prev_dreg;
1787 if (spec [MONO_INST_DEST] == 'l') {
1788 int hreg = prev_dreg + 1;
1789 val = rs->iassign [hreg];
1793 /* the register gets spilled after this inst */
1796 val = mono_regstate_alloc_int (rs, dest_mask);
1798 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1799 rs->iassign [hreg] = val;
1801 create_spilled_store (cfg, spill, val, hreg, ins);
1803 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1804 rs->isymbolic [val] = hreg;
1805 /* FIXME:? ins->dreg = val; */
1806 if (ins->dreg == ppc_r4) {
1808 create_copy_ins (cfg, val, ppc_r3, ins);
1809 } else if (ins->dreg == ppc_r3) {
1810 if (val == ppc_r4) {
1812 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1813 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1814 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1816 /* two forced copies */
1817 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1818 create_copy_ins (cfg, val, ppc_r3, ins);
1821 if (val == ppc_r3) {
1822 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1824 /* two forced copies */
1825 create_copy_ins (cfg, val, ppc_r3, ins);
1826 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1829 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
1830 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1831 mono_regstate_free_int (rs, val);
1833 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != ppc_r3 && spec [MONO_INST_CLOB] != 'd') {
1834 /* this instruction only outputs to ppc_r3, need to copy */
1835 create_copy_ins (cfg, ins->dreg, ppc_r3, ins);
1840 if (spec [MONO_INST_DEST] == 'f' && freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i)) {
1841 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1842 mono_regstate_free_float (rs, ins->dreg);
1843 } else if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
1844 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1845 mono_regstate_free_int (rs, ins->dreg);
1847 if (spec [MONO_INST_SRC1] == 'f') {
1848 src1_mask = cur_fregs;
1849 if (ins->sreg1 >= MONO_MAX_FREGS) {
1850 val = rs->fassign [ins->sreg1];
1851 prev_sreg1 = ins->sreg1;
1855 /* the register gets spilled after this inst */
1858 //g_assert (val == -1); /* source cannot be spilled */
1859 val = mono_regstate_alloc_float (rs, src1_mask);
1861 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1862 rs->fassign [ins->sreg1] = val;
1863 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1865 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
1866 insert_before_ins (ins, tmp, store);
1869 rs->fsymbolic [val] = prev_sreg1;
1874 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
1875 val = rs->iassign [ins->sreg1];
1876 prev_sreg1 = ins->sreg1;
1880 /* the register gets spilled after this inst */
1883 if (0 && ins->opcode == OP_MOVE) {
1885 * small optimization: the dest register is already allocated
1886 * but the src one is not: we can simply assign the same register
1887 * here and peephole will get rid of the instruction later.
1888 * This optimization may interfere with the clobbering handling:
1889 * it removes a mov operation that will be added again to handle clobbering.
1890 * There are also some other issues that should with make testjit.
1892 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1893 val = rs->iassign [ins->sreg1] = ins->dreg;
1894 //g_assert (val >= 0);
1895 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1897 //g_assert (val == -1); /* source cannot be spilled */
1898 val = mono_regstate_alloc_int (rs, src1_mask);
1900 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1901 rs->iassign [ins->sreg1] = val;
1902 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1905 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1906 insert_before_ins (ins, tmp, store);
1909 rs->isymbolic [val] = prev_sreg1;
1914 if (spec [MONO_INST_SRC2] == 'f') {
1915 src2_mask = cur_fregs;
1916 if (ins->sreg2 >= MONO_MAX_FREGS) {
1917 val = rs->fassign [ins->sreg2];
1918 prev_sreg2 = ins->sreg2;
1922 /* the register gets spilled after this inst */
1925 val = mono_regstate_alloc_float (rs, src2_mask);
1927 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1928 rs->fassign [ins->sreg2] = val;
1929 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1931 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
1933 rs->fsymbolic [val] = prev_sreg2;
1938 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
1939 val = rs->iassign [ins->sreg2];
1940 prev_sreg2 = ins->sreg2;
1944 /* the register gets spilled after this inst */
1947 val = mono_regstate_alloc_int (rs, src2_mask);
1949 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1950 rs->iassign [ins->sreg2] = val;
1951 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1953 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1955 rs->isymbolic [val] = prev_sreg2;
1961 if (spec [MONO_INST_CLOB] == 'c') {
1963 guint32 clob_mask = PPC_CALLER_REGS;
1964 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1966 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1967 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1971 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1972 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1973 mono_regstate_free_int (rs, ins->sreg1);
1975 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1976 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1977 mono_regstate_free_int (rs, ins->sreg2);
1980 //DEBUG (print_ins (i, ins));
1986 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
1988 /* sreg is a float, dreg is an integer reg. ppc_f0 is used a scratch */
1989 ppc_fctiwz (code, ppc_f0, sreg);
1990 ppc_stfd (code, ppc_f0, -8, ppc_sp);
1991 ppc_lwz (code, dreg, -4, ppc_sp);
1994 ppc_andid (code, dreg, dreg, 0xff);
1996 ppc_andid (code, dreg, dreg, 0xffff);
1999 ppc_extsb (code, dreg, dreg);
2001 ppc_extsh (code, dreg, dreg);
2006 static unsigned char*
2007 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2010 int sreg = tree->sreg1;
2011 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2012 if (tree->flags & MONO_INST_INIT) {
2014 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2015 x86_push_reg (code, X86_EAX);
2018 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2019 x86_push_reg (code, X86_ECX);
2022 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2023 x86_push_reg (code, X86_EDI);
2027 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2028 if (sreg != X86_ECX)
2029 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2030 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2032 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2034 x86_prefix (code, X86_REP_PREFIX);
2037 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2038 x86_pop_reg (code, X86_EDI);
2039 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2040 x86_pop_reg (code, X86_ECX);
2041 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2042 x86_pop_reg (code, X86_EAX);
2055 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2058 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2059 PatchData *pdata = (PatchData*)user_data;
2060 guchar *code = data;
2061 guint32 *thunks = data;
2062 guint32 *endthunks = (guint32*)(code + bsize);
2067 if (!pdata->absolute) {
2068 g_assert (!is_call_imm (pdata->target - pdata->code));
2069 /* make sure a jump is possible from the code to the thunk area */
2070 i = pdata->code - code;
2071 if (!is_call_imm (i))
2073 i = pdata->code + csize - code;
2074 if (!is_call_imm (i))
2078 templ = (guchar*)load;
2079 ppc_lis (templ, ppc_r0, (guint32)(pdata->target) >> 16);
2080 ppc_ori (templ, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2082 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2083 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2084 while (thunks < endthunks) {
2085 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2086 if ((thunks [0] == load [0]) && (thunks [1] == load [1])) {
2087 ppc_patch (pdata->code, (guchar*)thunks);
2088 mono_arch_flush_icache (pdata->code, 4);
2091 } else if ((thunks [0] == 0) && (thunks [1] == 0)) {
2092 /* found a free slot instead: emit thunk */
2093 code = (guchar*)thunks;
2094 ppc_lis (code, ppc_r0, (guint32)(pdata->target) >> 16);
2095 ppc_ori (code, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2096 ppc_mtctr (code, ppc_r0);
2097 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2098 mono_arch_flush_icache ((guchar*)thunks, 16);
2100 ppc_patch (pdata->code, (guchar*)thunks);
2101 mono_arch_flush_icache (pdata->code, 4);
2105 /* skip 16 bytes, the size of the thunk */
2109 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2115 handle_thunk (int absolute, guchar *code, guchar *target) {
2116 MonoDomain *domain = mono_domain_get ();
2120 pdata.target = target;
2121 pdata.absolute = absolute;
2124 mono_domain_lock (domain);
2125 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2128 /* this uses the first available slot */
2130 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2132 mono_domain_unlock (domain);
2134 if (pdata.found != 1)
2135 g_print ("thunk failed for %p from %p\n", target, code);
2136 g_assert (pdata.found == 1);
2140 ppc_patch (guchar *code, guchar *target)
2142 guint32 ins = *(guint32*)code;
2143 guint32 prim = ins >> 26;
2146 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2149 if (target < 33554431){
2150 ins = (18 << 26) | ((guint32) target) | (ins & 1) | 2;
2151 *(guint32*)code = ins;
2155 if (target > -33554432){
2156 ins = (18 << 26) | (((guint32)target) & 0xfc000000) | (ins & 1) | 2;
2157 *(guint32*)code = ins;
2162 gint diff = target - code;
2164 if (diff < 33554431){
2165 ins = (18 << 26) | (diff) | (ins & 1);
2166 *(guint32*)code = ins;
2169 handle_thunk (TRUE, code, target);
2173 /* diff between 0 and -33554432 */
2174 if (diff > -33554432){
2175 ins = (18 << 26) | (diff & ~0xfc000000) | (ins & 1);
2176 *(guint32*)code = ins;
2179 handle_thunk (TRUE, code, target);
2183 g_assert_not_reached ();
2186 #if OLD_REFERENCE_CODE
2189 gint diff = (gint)target;
2190 if ((diff < -33554432) || (diff > 33554431)) {
2191 diff = target - code;
2192 if (is_call_imm (diff)) {
2193 handle_thunk (TRUE, code, target);
2196 /* change it to relative */
2199 ins = prim << 26 | (ins & 3);
2200 diff &= ~0xfc000003;
2203 gint diff = target - code;
2204 if (is_call_imm (target)) {
2205 /* we change it into an absolute reference */
2206 ins = prim << 26 | (ins & 3) | 2;
2207 diff = (gint)target;
2208 diff &= ~0xfc000003;
2210 *(guint32*)code = ins;
2213 if (!is_call_imm (diff)) {
2214 handle_thunk (FALSE, code, target);
2217 ins = prim << 26 | (ins & 3);
2218 diff &= ~0xfc000003;
2221 *(guint32*)code = ins;
2228 guint32 li = (guint32)target;
2229 ins = (ins & 0xffff0000) | (ins & 3);
2230 ovf = li & 0xffff0000;
2231 if (ovf != 0 && ovf != 0xffff0000)
2232 g_assert_not_reached ();
2235 // FIXME: assert the top bits of li are 0
2237 gint diff = target - code;
2238 ins = (ins & 0xffff0000) | (ins & 3);
2239 ovf = diff & 0xffff0000;
2240 if (ovf != 0 && ovf != 0xffff0000)
2241 g_assert_not_reached ();
2245 *(guint32*)code = ins;
2247 g_assert_not_reached ();
2249 // g_print ("patched with 0x%08x\n", ins);
2253 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2258 guint8 *code = cfg->native_code + cfg->code_len;
2259 MonoInst *last_ins = NULL;
2260 guint last_offset = 0;
2263 if (cfg->opt & MONO_OPT_PEEPHOLE)
2264 peephole_pass (cfg, bb);
2266 /* we don't align basic blocks of loops on ppc */
2268 if (cfg->verbose_level > 2)
2269 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2271 cpos = bb->max_offset;
2273 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2274 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2275 //g_assert (!mono_compile_aot);
2278 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2279 /* this is not thread save, but good enough */
2280 /* fixme: howto handle overflows? */
2281 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2286 offset = code - cfg->native_code;
2288 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2290 if (offset > (cfg->code_size - max_len - 16)) {
2291 cfg->code_size *= 2;
2292 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2293 code = cfg->native_code + offset;
2295 // if (ins->cil_code)
2296 // g_print ("cil code\n");
2298 switch (ins->opcode) {
2300 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2301 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
2304 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2305 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
2307 case OP_STOREI1_MEMBASE_IMM:
2308 ppc_li (code, ppc_r11, ins->inst_imm);
2309 g_assert (ppc_is_imm16 (ins->inst_offset));
2310 ppc_stb (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2312 case OP_STOREI2_MEMBASE_IMM:
2313 ppc_li (code, ppc_r11, ins->inst_imm);
2314 g_assert (ppc_is_imm16 (ins->inst_offset));
2315 ppc_sth (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2317 case OP_STORE_MEMBASE_IMM:
2318 case OP_STOREI4_MEMBASE_IMM:
2319 ppc_load (code, ppc_r11, ins->inst_imm);
2320 g_assert (ppc_is_imm16 (ins->inst_offset));
2321 ppc_stw (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
2323 case OP_STOREI1_MEMBASE_REG:
2324 g_assert (ppc_is_imm16 (ins->inst_offset));
2325 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2327 case OP_STOREI2_MEMBASE_REG:
2328 g_assert (ppc_is_imm16 (ins->inst_offset));
2329 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2331 case OP_STORE_MEMBASE_REG:
2332 case OP_STOREI4_MEMBASE_REG:
2333 g_assert (ppc_is_imm16 (ins->inst_offset));
2334 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2339 g_assert_not_reached ();
2340 //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2343 g_assert_not_reached ();
2344 //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2345 //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2347 case OP_LOAD_MEMBASE:
2348 case OP_LOADI4_MEMBASE:
2349 case OP_LOADU4_MEMBASE:
2350 if (ppc_is_imm16 (ins->inst_offset)) {
2351 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2353 ppc_load (code, ppc_r11, ins->inst_offset);
2354 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2357 case OP_LOADU1_MEMBASE:
2358 g_assert (ppc_is_imm16 (ins->inst_offset));
2359 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2361 case OP_LOADI1_MEMBASE:
2362 g_assert (ppc_is_imm16 (ins->inst_offset));
2363 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2364 ppc_extsb (code, ins->dreg, ins->dreg);
2366 case OP_LOADU2_MEMBASE:
2367 g_assert (ppc_is_imm16 (ins->inst_offset));
2368 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2370 case OP_LOADI2_MEMBASE:
2371 g_assert (ppc_is_imm16 (ins->inst_offset));
2372 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2375 ppc_extsb (code, ins->dreg, ins->sreg1);
2378 ppc_extsh (code, ins->dreg, ins->sreg1);
2381 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
2384 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
2388 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2389 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2390 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN)))
2391 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2393 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2395 case OP_COMPARE_IMM:
2397 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2398 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2399 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN))) {
2400 if (ppc_is_uimm16 (ins->inst_imm)) {
2401 ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2403 ppc_load (code, ppc_r11, ins->inst_imm);
2404 ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2407 if (ppc_is_imm16 (ins->inst_imm)) {
2408 ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2410 ppc_load (code, ppc_r11, ins->inst_imm);
2411 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2415 case OP_X86_TEST_NULL:
2416 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2422 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2425 ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2428 ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2431 if (ppc_is_imm16 (ins->inst_imm)) {
2432 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2434 ppc_load (code, ppc_r11, ins->inst_imm);
2435 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2439 ppc_load (code, ppc_r11, ins->inst_imm);
2440 ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2444 /* clear summary overflow */
2445 ppc_crxor (code, 28, 28, 28);
2447 ppc_addod (code, ins->dreg, ins->sreg1, ins->sreg2);
2449 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "OverflowException");
2451 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2454 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2457 ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2460 ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2463 // we add the negated value
2464 if (ppc_is_imm16 (-ins->inst_imm))
2465 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2467 ppc_load (code, ppc_r11, ins->inst_imm);
2468 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2472 ppc_load (code, ppc_r11, ins->inst_imm);
2473 ppc_subfe (code, ins->dreg, ins->sreg2, ppc_r11);
2476 g_assert (ppc_is_imm16 (ins->inst_imm));
2477 ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2480 ppc_subfze (code, ins->dreg, ins->sreg1);
2483 /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2484 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2487 if (!(ins->inst_imm & 0xffff0000)) {
2488 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2489 } else if (!(ins->inst_imm & 0xffff)) {
2490 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2492 ppc_load (code, ppc_r11, ins->inst_imm);
2493 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2497 /* clear the summary overflow flag */
2498 ppc_crxor (code, 28, 28, 28);
2499 ppc_divwod (code, ins->dreg, ins->sreg1, ins->sreg2);
2500 /* FIXME: use OverflowException for 0x80000000/-1 */
2501 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2504 /* clear the summary overflow flag */
2505 ppc_crxor (code, 28, 28, 28);
2506 ppc_divwuod (code, ins->dreg, ins->sreg1, ins->sreg2);
2507 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2510 ppc_load (code, ppc_r11, ins->inst_imm);
2511 /* clear the summary overflow flag */
2512 ppc_crxor (code, 28, 28, 28);
2513 ppc_divwod (code, ins->dreg, ins->sreg1, ppc_r11);
2514 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2517 /* clear the summary overflow flag */
2518 ppc_crxor (code, 28, 28, 28);
2519 ppc_divwod (code, ppc_r11, ins->sreg1, ins->sreg2);
2520 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2521 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2522 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2525 /* clear the summary overflow flag */
2526 ppc_crxor (code, 28, 28, 28);
2527 ppc_divwuod (code, ppc_r11, ins->sreg1, ins->sreg2);
2528 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2529 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2530 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2533 ppc_load (code, ppc_r11, ins->inst_imm);
2534 /* clear the summary overflow flag */
2535 ppc_crxor (code, 28, 28, 28);
2536 ppc_divwod (code, ins->dreg, ins->sreg1, ppc_r11);
2537 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_SO, "DivideByZeroException");
2538 ppc_mullw (code, ins->dreg, ins->dreg, ppc_r11);
2539 ppc_subf (code, ins->dreg, ins->dreg, ins->sreg1);
2542 ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2545 if (!(ins->inst_imm & 0xffff0000)) {
2546 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2547 } else if (!(ins->inst_imm & 0xffff)) {
2548 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2550 ppc_load (code, ppc_r11, ins->inst_imm);
2551 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2555 ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2558 if (!(ins->inst_imm & 0xffff0000)) {
2559 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2560 } else if (!(ins->inst_imm & 0xffff)) {
2561 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2563 ppc_load (code, ppc_r11, ins->inst_imm);
2564 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2568 ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2571 ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2572 //ppc_load (code, ppc_r11, ins->inst_imm);
2573 //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2576 ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2579 // there is also ppc_srawi
2580 //ppc_load (code, ppc_r11, ins->inst_imm);
2581 //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2582 ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2585 ppc_load (code, ppc_r11, ins->inst_imm);
2586 ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);
2587 //ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0xf)), (ins->inst_imm & 0xf), 31);
2590 ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2593 ppc_not (code, ins->dreg, ins->sreg1);
2596 ppc_neg (code, ins->dreg, ins->sreg1);
2599 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2602 ppc_load (code, ppc_r11, ins->inst_imm);
2603 ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2606 ppc_mullwo (code, ins->dreg, ins->sreg1, ins->sreg2);
2607 ppc_mcrxr (code, 0);
2608 EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2610 case CEE_MUL_OVF_UN:
2611 /* we first multiply to get the high word and compare to 0
2612 * to set the flags, then the result is discarded and then
2613 * we multiply to get the lower * bits result
2615 ppc_mulhwu (code, ppc_r0, ins->sreg1, ins->sreg2);
2616 ppc_cmpi (code, 0, 0, ppc_r0, 0);
2617 EMIT_COND_SYSTEM_EXCEPTION (CEE_BNE_UN - CEE_BEQ, ins->inst_p1);
2618 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2622 ppc_load (code, ins->dreg, ins->inst_c0);
2625 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2626 ppc_lis (code, ins->dreg, 0);
2627 ppc_ori (code, ins->dreg, ins->dreg, 0);
2633 ppc_mr (code, ins->dreg, ins->sreg1);
2636 int saved = ins->sreg1;
2637 if (ins->sreg1 == ppc_r3) {
2638 ppc_mr (code, ppc_r0, ins->sreg1);
2641 if (ins->sreg2 != ppc_r3)
2642 ppc_mr (code, ppc_r3, ins->sreg2);
2643 if (saved != ppc_r4)
2644 ppc_mr (code, ppc_r4, saved);
2649 ppc_fmr (code, ins->dreg, ins->sreg1);
2651 case OP_FCONV_TO_R4:
2652 ppc_frsp (code, ins->dreg, ins->sreg1);
2658 * Keep in sync with mono_arch_emit_epilog
2660 g_assert (!cfg->method->save_lmf);
2661 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2662 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
2663 ppc_mtlr (code, ppc_r0);
2665 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2666 if (!cfg->method->save_lmf) {
2667 for (i = 13; i < 32; ++i) {
2668 if (cfg->used_int_regs & (1 << i)) {
2670 ppc_lwz (code, i, -pos, cfg->frame_reg);
2674 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2679 /* ensure ins->sreg1 is not NULL */
2680 ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2687 call = (MonoCallInst*)ins;
2688 if (ins->flags & MONO_INST_HAS_METHOD)
2689 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2691 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2697 case OP_VOIDCALL_REG:
2699 ppc_mtlr (code, ins->sreg1);
2702 case OP_FCALL_MEMBASE:
2703 case OP_LCALL_MEMBASE:
2704 case OP_VCALL_MEMBASE:
2705 case OP_VOIDCALL_MEMBASE:
2706 case OP_CALL_MEMBASE:
2707 ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2708 ppc_mtlr (code, ppc_r0);
2712 g_assert_not_reached ();
2715 /* keep alignment */
2716 ppc_addi (code, ppc_r0, ins->sreg1, PPC_STACK_ALIGNMENT-1);
2717 ppc_rlwinm (code, ppc_r0, ppc_r0, 0, 0, 27);
2718 ppc_lwz (code, ppc_r11, 0, ppc_sp);
2719 ppc_neg (code, ppc_r0, ppc_r0);
2720 ppc_stwux (code, ppc_r11, ppc_r0, ppc_sp);
2721 ppc_addi (code, ins->dreg, ppc_sp, PPC_STACK_PARAM_OFFSET);
2728 ppc_mr (code, ppc_r3, ins->sreg1);
2729 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2730 (gpointer)"mono_arch_throw_exception");
2734 case OP_START_HANDLER:
2735 ppc_mflr (code, ppc_r0);
2736 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2739 if (ins->sreg1 != ppc_r3)
2740 ppc_mr (code, ppc_r3, ins->sreg1);
2741 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2742 ppc_mtlr (code, ppc_r0);
2745 case CEE_ENDFINALLY:
2746 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2747 ppc_mtlr (code, ppc_r0);
2750 case OP_CALL_HANDLER:
2751 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2755 ins->inst_c0 = code - cfg->native_code;
2758 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2759 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2761 if (ins->flags & MONO_INST_BRLABEL) {
2762 /*if (ins->inst_i0->inst_c0) {
2764 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2766 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2770 /*if (ins->inst_target_bb->native_offset) {
2772 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2774 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2780 ppc_mtctr (code, ins->sreg1);
2781 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2784 ppc_li (code, ins->dreg, 0);
2785 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2786 ppc_li (code, ins->dreg, 1);
2790 ppc_li (code, ins->dreg, 1);
2791 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2792 ppc_li (code, ins->dreg, 0);
2796 ppc_li (code, ins->dreg, 1);
2797 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2798 ppc_li (code, ins->dreg, 0);
2800 case OP_COND_EXC_EQ:
2801 case OP_COND_EXC_NE_UN:
2802 case OP_COND_EXC_LT:
2803 case OP_COND_EXC_LT_UN:
2804 case OP_COND_EXC_GT:
2805 case OP_COND_EXC_GT_UN:
2806 case OP_COND_EXC_GE:
2807 case OP_COND_EXC_GE_UN:
2808 case OP_COND_EXC_LE:
2809 case OP_COND_EXC_LE_UN:
2810 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2813 /* move XER [0-3] (SO, OV, CA) into CR
2814 * this translates to LT, GT, EQ.
2815 * FIXME: test for all the conditions occourring
2817 ppc_mcrxr (code, 0);
2818 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, ins->inst_p1);
2820 case OP_COND_EXC_OV:
2821 ppc_mcrxr (code, 0);
2822 EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2824 case OP_COND_EXC_NC:
2825 case OP_COND_EXC_NO:
2826 g_assert_not_reached ();
2838 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2841 /* floating point opcodes */
2843 ppc_load (code, ppc_r11, ins->inst_p0);
2844 ppc_lfd (code, ins->dreg, 0, ppc_r11);
2847 ppc_load (code, ppc_r11, ins->inst_p0);
2848 ppc_lfs (code, ins->dreg, 0, ppc_r11);
2850 case OP_STORER8_MEMBASE_REG:
2851 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2853 case OP_LOADR8_MEMBASE:
2854 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2856 case OP_STORER4_MEMBASE_REG:
2857 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2859 case OP_LOADR4_MEMBASE:
2860 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2862 case CEE_CONV_R_UN: {
2863 static const guint64 adjust_val = 0x4330000000000000UL;
2864 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2865 ppc_stw (code, ppc_r0, -8, ppc_sp);
2866 ppc_stw (code, ins->sreg1, -4, ppc_sp);
2867 ppc_load (code, ppc_r11, &adjust_val);
2868 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2869 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2872 case CEE_CONV_R4: /* FIXME: change precision */
2874 static const guint64 adjust_val = 0x4330000080000000UL;
2875 // addis is special for ppc_r0
2876 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2877 ppc_stw (code, ppc_r0, -8, ppc_sp);
2878 ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
2879 ppc_stw (code, ppc_r11, -4, ppc_sp);
2880 ppc_lfd (code, ins->dreg, -8, ppc_sp);
2881 ppc_load (code, ppc_r11, &adjust_val);
2882 ppc_lfd (code, ppc_f0, 0, ppc_r11);
2883 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2886 case OP_X86_FP_LOAD_I8:
2887 g_assert_not_reached ();
2888 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
2890 case OP_X86_FP_LOAD_I4:
2891 g_assert_not_reached ();
2892 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
2894 case OP_FCONV_TO_I1:
2895 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
2897 case OP_FCONV_TO_U1:
2898 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
2900 case OP_FCONV_TO_I2:
2901 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
2903 case OP_FCONV_TO_U2:
2904 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
2906 case OP_FCONV_TO_I4:
2908 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
2910 case OP_FCONV_TO_U4:
2912 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
2914 case OP_FCONV_TO_I8:
2915 case OP_FCONV_TO_U8:
2916 g_assert_not_reached ();
2917 /* Implemented as helper calls */
2919 case OP_LCONV_TO_R_UN:
2920 g_assert_not_reached ();
2921 /* Implemented as helper calls */
2923 case OP_LCONV_TO_OVF_I: {
2924 ppc_mr (code, ins->dreg, ins->sreg1);
2925 /* FIXME: emit exception if needed */
2929 ppc_fsqrtd (code, ins->dreg, ins->sreg1);
2932 ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
2935 ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
2938 ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
2941 ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
2944 ppc_fneg (code, ins->dreg, ins->sreg1);
2948 g_assert_not_reached ();
2951 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2954 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2955 ppc_li (code, ins->dreg, 0);
2956 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2957 ppc_li (code, ins->dreg, 1);
2960 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2961 ppc_li (code, ins->dreg, 1);
2962 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2963 ppc_li (code, ins->dreg, 0);
2966 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2967 ppc_li (code, ins->dreg, 1);
2968 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
2969 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2970 ppc_li (code, ins->dreg, 0);
2973 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
2974 ppc_li (code, ins->dreg, 1);
2975 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2976 ppc_li (code, ins->dreg, 0);
2979 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
2980 ppc_li (code, ins->dreg, 1);
2981 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
2982 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2983 ppc_li (code, ins->dreg, 0);
2986 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
2989 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
2992 EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
2995 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
2996 EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
2999 EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
3002 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3003 EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
3006 EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
3009 EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
3012 EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
3015 EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
3017 case CEE_CKFINITE: {
3018 ppc_stfd (code, ins->sreg1, -8, ppc_sp);
3019 ppc_lwz (code, ppc_r11, -8, ppc_sp);
3020 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
3021 ppc_addis (code, ppc_r11, ppc_r11, -32752);
3022 ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
3023 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");
3027 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3028 g_assert_not_reached ();
3031 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
3032 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3033 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3034 g_assert_not_reached ();
3040 last_offset = offset;
3045 cfg->code_len = code - cfg->native_code;
3049 mono_arch_register_lowlevel_calls (void)
3053 #define patch_lis_ori(ip,val) do {\
3054 guint16 *__lis_ori = (guint16*)(ip); \
3055 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
3056 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
3060 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3062 MonoJumpInfo *patch_info;
3064 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3065 unsigned char *ip = patch_info->ip.i + code;
3066 const unsigned char *target = NULL;
3068 switch (patch_info->type) {
3069 case MONO_PATCH_INFO_BB:
3070 target = patch_info->data.bb->native_offset + code;
3072 case MONO_PATCH_INFO_ABS:
3073 target = patch_info->data.target;
3075 case MONO_PATCH_INFO_LABEL:
3076 target = patch_info->data.inst->inst_c0 + code;
3078 case MONO_PATCH_INFO_IP:
3079 patch_lis_ori (ip, ip);
3081 case MONO_PATCH_INFO_METHOD_REL:
3082 g_assert_not_reached ();
3083 *((gpointer *)(ip)) = code + patch_info->data.offset;
3085 case MONO_PATCH_INFO_INTERNAL_METHOD: {
3086 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
3088 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
3089 g_assert_not_reached ();
3091 target = mono_icall_get_wrapper (mi);
3094 case MONO_PATCH_INFO_METHOD_JUMP: {
3097 /* get the trampoline to the method from the domain */
3098 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
3099 if (!domain->jump_target_hash)
3100 domain->jump_target_hash = g_hash_table_new (NULL, NULL);
3101 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
3102 list = g_slist_prepend (list, ip);
3103 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
3106 case MONO_PATCH_INFO_METHOD:
3107 if (patch_info->data.method == method) {
3110 /* get the trampoline to the method from the domain */
3111 target = mono_arch_create_jit_trampoline (patch_info->data.method);
3114 case MONO_PATCH_INFO_SWITCH: {
3115 gpointer *table = (gpointer *)patch_info->data.target;
3118 // FIXME: inspect code to get the register
3119 ppc_load (ip, ppc_r11, patch_info->data.target);
3120 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3122 for (i = 0; i < patch_info->table_size; i++) {
3123 table [i] = (int)patch_info->data.table [i] + code;
3125 /* we put into the table the absolute address, no need for ppc_patch in this case */
3128 case MONO_PATCH_INFO_METHODCONST:
3129 case MONO_PATCH_INFO_CLASS:
3130 case MONO_PATCH_INFO_IMAGE:
3131 case MONO_PATCH_INFO_FIELD:
3132 /* from OP_AOTCONST : lis + ori */
3133 patch_lis_ori (ip, patch_info->data.target);
3135 case MONO_PATCH_INFO_R4:
3136 case MONO_PATCH_INFO_R8:
3137 g_assert_not_reached ();
3138 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3140 case MONO_PATCH_INFO_IID:
3141 mono_class_init (patch_info->data.klass);
3142 patch_lis_ori (ip, patch_info->data.klass->interface_id);
3144 case MONO_PATCH_INFO_VTABLE:
3145 target = mono_class_vtable (domain, patch_info->data.klass);
3146 patch_lis_ori (ip, target);
3148 case MONO_PATCH_INFO_CLASS_INIT:
3149 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
3151 case MONO_PATCH_INFO_SFLDA: {
3152 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
3153 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (vtable->klass, method))
3154 /* Done by the generated code */
3158 mono_runtime_class_init (vtable);
3160 target = (char*)vtable->data + patch_info->data.field->offset;
3161 patch_lis_ori (ip, target);
3164 case MONO_PATCH_INFO_EXC_NAME:
3165 g_assert_not_reached ();
3166 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3168 case MONO_PATCH_INFO_LDSTR:
3169 target = mono_ldstr (domain, patch_info->data.token->image,
3170 mono_metadata_token_index (patch_info->data.token->token));
3171 patch_lis_ori (ip, target);
3173 case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
3175 MonoClass *handle_class;
3177 handle = mono_ldtoken (patch_info->data.token->image,
3178 patch_info->data.token->token, &handle_class, NULL);
3179 mono_class_init (handle_class);
3180 mono_class_init (mono_class_from_mono_type (handle));
3182 patch_lis_ori (ip, handle);
3185 case MONO_PATCH_INFO_LDTOKEN: {
3187 MonoClass *handle_class;
3189 handle = mono_ldtoken (patch_info->data.token->image,
3190 patch_info->data.token->token, &handle_class, NULL);
3191 mono_class_init (handle_class);
3193 patch_lis_ori (ip, handle);
3196 case MONO_PATCH_INFO_BB_OVF:
3197 case MONO_PATCH_INFO_EXC_OVF:
3198 /* everything is dealt with at epilog output time */
3201 g_assert_not_reached ();
3203 ppc_patch (ip, target);
3208 mono_arch_max_epilog_size (MonoCompile *cfg)
3210 int exc_count = 0, max_epilog_size = 16 + 20*4;
3211 MonoJumpInfo *patch_info;
3213 if (cfg->method->save_lmf)
3214 max_epilog_size += 128;
3216 if (mono_jit_trace_calls != NULL)
3217 max_epilog_size += 50;
3219 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3220 max_epilog_size += 50;
3222 /* count the number of exception infos */
3225 * make sure we have enough space for exceptions
3226 * 24 is the simulated call to throw_exception_by_name
3228 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3229 if (patch_info->type == MONO_PATCH_INFO_EXC)
3230 max_epilog_size += 24;
3231 else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
3232 max_epilog_size += 12;
3233 else if (patch_info->type == MONO_PATCH_INFO_EXC_OVF)
3234 max_epilog_size += 12;
3237 return max_epilog_size;
3241 mono_arch_emit_prolog (MonoCompile *cfg)
3243 MonoMethod *method = cfg->method;
3245 MonoMethodSignature *sig;
3247 int alloc_size, pos, max_offset, i;
3252 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3255 cfg->code_size = 256;
3256 code = cfg->native_code = g_malloc (cfg->code_size);
3258 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3259 ppc_mflr (code, ppc_r0);
3260 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3262 if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
3263 cfg->used_int_regs |= 1 << 31;
3265 cfg->used_int_regs |= USE_EXTRA_TEMPS;
3267 alloc_size = cfg->stack_offset;
3269 /* reserve room to save return value */
3273 if (!method->save_lmf) {
3274 for (i = 13; i < 32; ++i) {
3275 if (cfg->used_int_regs & (1 << i)) {
3276 pos += sizeof (gulong);
3277 ppc_stw (code, i, -pos, ppc_sp);
3280 /*for (i = 14; i < 32; ++i) {
3281 if (cfg->used_float_regs & (1 << i)) {
3282 pos += sizeof (gdouble);
3283 ppc_stfd (code, i, -pos, ppc_sp);
3288 // align to PPC_STACK_ALIGNMENT bytes
3289 if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
3290 alloc_size += PPC_STACK_ALIGNMENT - 1;
3291 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
3294 cfg->stack_usage = alloc_size;
3295 g_assert (ppc_is_imm16 (-alloc_size));
3296 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
3298 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3299 if (cfg->flags & MONO_CFG_HAS_ALLOCA)
3300 ppc_mr (code, ppc_r31, ppc_sp);
3302 /* compute max_offset in order to use short forward jumps
3303 * we always do it on ppc because the immediate displacement
3304 * for jumps is too small
3307 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3308 MonoInst *ins = bb->code;
3309 bb->max_offset = max_offset;
3311 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3315 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3320 /* load arguments allocated to register from the stack */
3321 sig = method->signature;
3324 cinfo = calculate_sizes (sig, sig->pinvoke);
3326 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3327 ArgInfo *ainfo = &cinfo->ret;
3329 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3331 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3332 ArgInfo *ainfo = cinfo->args + i;
3333 inst = cfg->varinfo [pos];
3335 if (inst->opcode == OP_REGVAR) {
3336 if (ainfo->regtype == RegTypeGeneral)
3337 ppc_mr (code, inst->dreg, ainfo->reg);
3338 else if (ainfo->regtype == RegTypeFP)
3339 ppc_fmr (code, inst->dreg, ainfo->reg);
3340 else if (ainfo->regtype == RegTypeBase) {
3341 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3342 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3344 g_assert_not_reached ();
3346 if (cfg->verbose_level > 2)
3347 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3349 /* the argument should be put on the stack: FIXME handle size != word */
3350 if (ainfo->regtype == RegTypeGeneral) {
3351 switch (ainfo->size) {
3353 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3356 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3359 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3360 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3363 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3365 } else if (ainfo->regtype == RegTypeBase) {
3366 /* load the previous stack pointer in r11 */
3367 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3368 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3369 switch (ainfo->size) {
3371 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3374 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3377 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3378 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3379 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3382 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3384 } else if (ainfo->regtype == RegTypeFP) {
3385 if (ainfo->size == 8)
3386 ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3387 else if (ainfo->size == 4)
3388 ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3390 g_assert_not_reached ();
3391 } else if (ainfo->regtype == RegTypeStructByVal) {
3392 int doffset = inst->inst_offset;
3395 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3396 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3397 soffset += sizeof (gpointer);
3398 doffset += sizeof (gpointer);
3400 if (ainfo->vtsize) {
3401 /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3402 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3403 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3404 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3406 } else if (ainfo->regtype == RegTypeStructByAddr) {
3407 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3408 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3410 g_assert_not_reached ();
3415 if (method->save_lmf) {
3417 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3418 (gpointer)"mono_get_lmf_addr");
3420 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3421 pos = PPC_MINIMAL_STACK_SIZE + cfg->param_area;
3424 ppc_addi (code, ppc_r11, ppc_sp, pos);
3425 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3426 /* new_lmf->previous_lmf = *lmf_addr */
3427 ppc_lwz (code, ppc_r0, 0, ppc_r3);
3428 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3429 /* *(lmf_addr) = r11 */
3430 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3431 /* save method info */
3432 ppc_load (code, ppc_r0, method);
3433 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3434 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3435 /* save the current IP */
3436 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3437 ppc_load (code, ppc_r0, 0x01010101);
3438 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3439 ppc_stmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3440 for (i = 14; i < 32; i++) {
3441 ppc_stfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3446 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3448 cfg->code_len = code - cfg->native_code;
3455 mono_arch_emit_epilog (MonoCompile *cfg)
3457 MonoJumpInfo *patch_info;
3458 MonoMethod *method = cfg->method;
3463 * Keep in sync with CEE_JMP
3465 code = cfg->native_code + cfg->code_len;
3467 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3468 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3474 if (method->save_lmf) {
3475 int ofst = PPC_MINIMAL_STACK_SIZE + cfg->param_area;
3478 ppc_addi (code, ppc_r11, cfg->frame_reg, ofst);
3479 /* r5 = previous_lmf */
3480 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3482 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3483 /* *(lmf_addr) = previous_lmf */
3484 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3486 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3488 for (i = 14; i < 32; i++) {
3489 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3493 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3494 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3495 ppc_mtlr (code, ppc_r0);
3497 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3498 if (!method->save_lmf) {
3499 for (i = 13; i < 32; ++i) {
3500 if (cfg->used_int_regs & (1 << i)) {
3502 ppc_lwz (code, i, -pos, cfg->frame_reg);
3508 /* add code to raise exceptions */
3509 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3510 switch (patch_info->type) {
3511 case MONO_PATCH_INFO_BB_OVF: {
3512 MonoOvfJump *ovfj = patch_info->data.target;
3513 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3514 /* patch the initial jump */
3515 ppc_patch (ip, code);
3516 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3518 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3519 /* jump back to the true target */
3521 ip = ovfj->bb->native_offset + cfg->native_code;
3522 ppc_patch (code - 4, ip);
3525 case MONO_PATCH_INFO_EXC_OVF: {
3526 MonoOvfJump *ovfj = patch_info->data.target;
3527 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3528 /* patch the initial jump */
3529 ppc_patch (ip, code);
3530 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3532 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3533 /* jump back to the true target */
3535 ip = (char*)ovfj->ip + 4;
3536 ppc_patch (code - 4, ip);
3539 case MONO_PATCH_INFO_EXC: {
3540 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3541 ppc_patch (ip, code);
3542 /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3543 ppc_load (code, ppc_r3, patch_info->data.target);
3544 /* simulate a call from ip */
3545 ppc_load (code, ppc_r0, ip + 4);
3546 ppc_mtlr (code, ppc_r0);
3547 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3548 patch_info->data.name = "mono_arch_throw_exception_by_name";
3549 patch_info->ip.i = code - cfg->native_code;
3559 cfg->code_len = code - cfg->native_code;
3561 g_assert (cfg->code_len < cfg->code_size);
3566 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3571 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3576 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3578 int this_dreg = ppc_r3;
3583 /* add the this argument */
3584 if (this_reg != -1) {
3586 MONO_INST_NEW (cfg, this, OP_SETREG);
3587 this->type = this_type;
3588 this->sreg1 = this_reg;
3589 this->dreg = this_dreg;
3590 mono_bblock_add_inst (cfg->cbb, this);
3595 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3596 vtarg->type = STACK_MP;
3597 vtarg->sreg1 = vt_reg;
3598 vtarg->dreg = ppc_r3;
3599 mono_bblock_add_inst (cfg->cbb, vtarg);
3604 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3606 /* optional instruction, need to detect it
3607 if (cmethod->klass == mono_defaults.math_class) {
3608 if (strcmp (cmethod->name, "Sqrt") == 0)
3616 mono_arch_print_tree (MonoInst *tree, int arity)