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>
30 int mono_exc_esp_offset = 0;
31 static int tls_mode = TLS_MODE_DETECT;
32 static int lmf_pthread_key = -1;
33 static int monothread_key = -1;
34 static int monodomain_key = -1;
37 offsets_from_pthread_key (guint32 key, int *offset2)
41 *offset2 = idx2 * sizeof (gpointer);
42 return 284 + idx1 * sizeof (gpointer);
45 #define emit_linuxthreads_tls(code,dreg,key) do {\
47 off1 = offsets_from_pthread_key ((key), &off2); \
48 ppc_lwz ((code), (dreg), off1, ppc_r2); \
49 ppc_lwz ((code), (dreg), off2, (dreg)); \
52 #define emit_darwing5_tls(code,dreg,key) do {\
53 int off1 = 0x48 + key * sizeof (gpointer); \
54 ppc_mfspr ((code), (dreg), 104); \
55 ppc_lwz ((code), (dreg), off1, (dreg)); \
58 /* FIXME: ensure the sc call preserves all but r3 */
59 #define emit_darwing4_tls(code,dreg,key) do {\
60 int off1 = 0x48 + key * sizeof (gpointer); \
61 if ((dreg) != ppc_r3) ppc_mr ((code), ppc_r11, ppc_r3); \
62 ppc_li ((code), ppc_r0, 0x7FF2); \
64 ppc_lwz ((code), (dreg), off1, ppc_r3); \
65 if ((dreg) != ppc_r3) ppc_mr ((code), ppc_r3, ppc_r11); \
68 #define emit_tls_access(code,dreg,key) do { \
70 case TLS_MODE_LTHREADS: emit_linuxthreads_tls(code,dreg,key); break; \
71 case TLS_MODE_DARWIN_G5: emit_darwing5_tls(code,dreg,key); break; \
72 case TLS_MODE_DARWIN_G4: emit_darwing4_tls(code,dreg,key); break; \
73 default: g_assert_not_reached (); \
78 mono_arch_regname (int reg) {
79 static const char * rnames[] = {
80 "ppc_r0", "ppc_sp", "ppc_r2", "ppc_r3", "ppc_r4",
81 "ppc_r5", "ppc_r6", "ppc_r7", "ppc_r8", "ppc_r9",
82 "ppc_r10", "ppc_r11", "ppc_r12", "ppc_r13", "ppc_r14",
83 "ppc_r15", "ppc_r16", "ppc_r17", "ppc_r18", "ppc_r19",
84 "ppc_r20", "ppc_r21", "ppc_r22", "ppc_r23", "ppc_r24",
85 "ppc_r25", "ppc_r26", "ppc_r27", "ppc_r28", "ppc_r29",
88 if (reg >= 0 && reg < 32)
93 /* this function overwrites r0, r11, r12 */
95 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
97 /* unrolled, use the counter in big */
98 if (size > sizeof (gpointer) * 5) {
99 int shifted = size >> 2;
100 guint8 *copy_loop_start, *copy_loop_jump;
102 ppc_load (code, ppc_r0, shifted);
103 ppc_mtctr (code, ppc_r0);
104 g_assert (sreg == ppc_r11);
105 ppc_addi (code, ppc_r12, dreg, (doffset - 4));
106 ppc_addi (code, ppc_r11, sreg, (soffset - 4));
107 copy_loop_start = code;
108 ppc_lwzu (code, ppc_r0, ppc_r11, 4);
109 ppc_stwu (code, ppc_r0, 4, ppc_r12);
110 copy_loop_jump = code;
111 ppc_bc (code, PPC_BR_DEC_CTR_NONZERO, 0, 0);
112 ppc_patch (copy_loop_jump, copy_loop_start);
114 doffset = soffset = 0;
118 ppc_lwz (code, ppc_r0, soffset, sreg);
119 ppc_stw (code, ppc_r0, doffset, dreg);
125 ppc_lhz (code, ppc_r0, soffset, sreg);
126 ppc_sth (code, ppc_r0, doffset, dreg);
132 ppc_lbz (code, ppc_r0, soffset, sreg);
133 ppc_stb (code, ppc_r0, doffset, dreg);
142 * mono_arch_get_argument_info:
143 * @csig: a method signature
144 * @param_count: the number of parameters to consider
145 * @arg_info: an array to store the result infos
147 * Gathers information on parameters such as size, alignment and
148 * padding. arg_info should be large enought to hold param_count + 1 entries.
150 * Returns the size of the activation frame.
153 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
155 int k, frame_size = 0;
156 int size, align, pad;
159 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
160 frame_size += sizeof (gpointer);
164 arg_info [0].offset = offset;
167 frame_size += sizeof (gpointer);
171 arg_info [0].size = frame_size;
173 for (k = 0; k < param_count; k++) {
176 size = mono_type_native_stack_size (csig->params [k], &align);
178 size = mono_type_stack_size (csig->params [k], &align);
180 /* ignore alignment for now */
183 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
184 arg_info [k].pad = pad;
186 arg_info [k + 1].pad = 0;
187 arg_info [k + 1].size = size;
189 arg_info [k + 1].offset = offset;
193 align = MONO_ARCH_FRAME_ALIGNMENT;
194 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
195 arg_info [k].pad = pad;
201 * Initialize the cpu to execute managed code.
204 mono_arch_cpu_init (void)
209 * This function returns the optimizations supported on this cpu.
212 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
216 /* no ppc-specific optimizations yet */
222 is_regsize_var (MonoType *t) {
225 t = mono_type_get_underlying_type (t);
232 case MONO_TYPE_FNPTR:
234 case MONO_TYPE_OBJECT:
235 case MONO_TYPE_STRING:
236 case MONO_TYPE_CLASS:
237 case MONO_TYPE_SZARRAY:
238 case MONO_TYPE_ARRAY:
240 case MONO_TYPE_VALUETYPE:
247 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
252 for (i = 0; i < cfg->num_varinfo; i++) {
253 MonoInst *ins = cfg->varinfo [i];
254 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
257 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
260 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
263 /* we can only allocate 32 bit values */
264 if (is_regsize_var (ins->inst_vtype)) {
265 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
266 g_assert (i == vmv->idx);
267 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
274 #define USE_EXTRA_TEMPS ((1<<30) | (1<<29))
275 //#define USE_EXTRA_TEMPS 0
278 mono_arch_get_global_int_regs (MonoCompile *cfg)
282 if (cfg->frame_reg != ppc_sp)
287 for (i = 13; i < top; ++i)
288 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
294 * mono_arch_regalloc_cost:
296 * Return the cost, in number of memory references, of the action of
297 * allocating the variable VMV into a register during global register
301 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
307 // code from ppc/tramp.c, try to keep in sync
308 #define MIN_CACHE_LINE 8
311 mono_arch_flush_icache (guint8 *code, gint size)
317 /* use dcbf for smp support, later optimize for UP, see pem._64bit.d20030611.pdf page 211 */
319 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
320 asm ("dcbf 0,%0;" : : "r"(p) : "memory");
323 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
324 asm ("dcbst 0,%0;" : : "r"(p) : "memory");
329 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
330 asm ("icbi 0,%0; sync;" : : "r"(p) : "memory");
336 #define NOT_IMPLEMENTED(x) \
337 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
340 #define ALWAYS_ON_STACK(s) s
341 #define FP_ALSO_IN_REG(s) s
343 #define ALWAYS_ON_STACK(s)
344 #define FP_ALSO_IN_REG(s)
345 #define ALIGN_DOUBLES
358 guint16 vtsize; /* in param area */
360 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
361 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
376 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
379 if (*gr >= 3 + PPC_NUM_REG_ARGS) {
380 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
381 ainfo->reg = ppc_sp; /* in the caller */
382 ainfo->regtype = RegTypeBase;
385 ALWAYS_ON_STACK (*stack_size += 4);
389 if (*gr >= 3 + PPC_NUM_REG_ARGS - 1) {
391 //*stack_size += (*stack_size % 8);
393 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
394 ainfo->reg = ppc_sp; /* in the caller */
395 ainfo->regtype = RegTypeBase;
402 ALWAYS_ON_STACK (*stack_size += 8);
411 /* size == 4 is checked already */
413 has_only_a_r4_field (MonoClass *klass)
418 while ((f = mono_class_get_fields (klass, &iter))) {
419 if (!(f->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
420 if (!f->type->byref && f->type->type == MONO_TYPE_R4)
430 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
433 int n = sig->hasthis + sig->param_count;
435 guint32 stack_size = 0;
436 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
438 fr = PPC_FIRST_FPARG_REG;
439 gr = PPC_FIRST_ARG_REG;
441 /* FIXME: handle returning a struct */
442 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
443 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
444 cinfo->struct_ret = PPC_FIRST_ARG_REG;
449 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
452 DEBUG(printf("params: %d\n", sig->param_count));
453 for (i = 0; i < sig->param_count; ++i) {
454 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
455 /* Prevent implicit arguments and sig_cookie from
456 being passed in registers */
457 gr = PPC_LAST_ARG_REG + 1;
458 /* Emit the signature cookie just before the implicit arguments */
459 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
461 DEBUG(printf("param %d: ", i));
462 if (sig->params [i]->byref) {
463 DEBUG(printf("byref\n"));
464 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
468 simpletype = mono_type_get_underlying_type (sig->params [i])->type;
470 switch (simpletype) {
471 case MONO_TYPE_BOOLEAN:
474 cinfo->args [n].size = 1;
475 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
481 cinfo->args [n].size = 2;
482 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
487 cinfo->args [n].size = 4;
488 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
494 case MONO_TYPE_FNPTR:
495 case MONO_TYPE_CLASS:
496 case MONO_TYPE_OBJECT:
497 case MONO_TYPE_STRING:
498 case MONO_TYPE_SZARRAY:
499 case MONO_TYPE_ARRAY:
500 cinfo->args [n].size = sizeof (gpointer);
501 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
504 case MONO_TYPE_VALUETYPE: {
507 klass = mono_class_from_mono_type (sig->params [i]);
509 size = mono_class_native_size (klass, NULL);
511 size = mono_class_value_size (klass, NULL);
513 if (size == 4 && has_only_a_r4_field (klass)) {
514 cinfo->args [n].size = 4;
516 /* It was 7, now it is 8 in LinuxPPC */
517 if (fr <= PPC_LAST_FPARG_REG) {
518 cinfo->args [n].regtype = RegTypeFP;
519 cinfo->args [n].reg = fr;
521 FP_ALSO_IN_REG (gr ++);
522 ALWAYS_ON_STACK (stack_size += 4);
524 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
525 cinfo->args [n].regtype = RegTypeBase;
526 cinfo->args [n].reg = ppc_sp; /* in the caller*/
533 DEBUG(printf ("load %d bytes struct\n",
534 mono_class_native_size (sig->params [i]->data.klass, NULL)));
535 #if PPC_PASS_STRUCTS_BY_VALUE
537 int align_size = size;
539 align_size += (sizeof (gpointer) - 1);
540 align_size &= ~(sizeof (gpointer) - 1);
541 nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
542 cinfo->args [n].regtype = RegTypeStructByVal;
543 if (gr > PPC_LAST_ARG_REG || (size >= 3 && size % 4 != 0)) {
544 cinfo->args [n].size = 0;
545 cinfo->args [n].vtsize = nwords;
547 int rest = PPC_LAST_ARG_REG - gr + 1;
548 int n_in_regs = rest >= nwords? nwords: rest;
549 cinfo->args [n].size = n_in_regs;
550 cinfo->args [n].vtsize = nwords - n_in_regs;
551 cinfo->args [n].reg = gr;
554 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
555 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
556 stack_size += nwords * sizeof (gpointer);
559 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
560 cinfo->args [n].regtype = RegTypeStructByAddr;
565 case MONO_TYPE_TYPEDBYREF: {
566 int size = sizeof (MonoTypedRef);
567 /* keep in sync or merge with the valuetype case */
568 #if PPC_PASS_STRUCTS_BY_VALUE
570 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
571 cinfo->args [n].regtype = RegTypeStructByVal;
572 if (gr <= PPC_LAST_ARG_REG) {
573 int rest = PPC_LAST_ARG_REG - gr + 1;
574 int n_in_regs = rest >= nwords? nwords: rest;
575 cinfo->args [n].size = n_in_regs;
576 cinfo->args [n].vtsize = nwords - n_in_regs;
577 cinfo->args [n].reg = gr;
580 cinfo->args [n].size = 0;
581 cinfo->args [n].vtsize = nwords;
583 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
584 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
585 stack_size += nwords * sizeof (gpointer);
588 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
589 cinfo->args [n].regtype = RegTypeStructByAddr;
596 cinfo->args [n].size = 8;
597 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
601 cinfo->args [n].size = 4;
603 /* It was 7, now it is 8 in LinuxPPC */
604 if (fr <= PPC_LAST_FPARG_REG) {
605 cinfo->args [n].regtype = RegTypeFP;
606 cinfo->args [n].reg = fr;
608 FP_ALSO_IN_REG (gr ++);
609 ALWAYS_ON_STACK (stack_size += 4);
611 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
612 cinfo->args [n].regtype = RegTypeBase;
613 cinfo->args [n].reg = ppc_sp; /* in the caller*/
619 cinfo->args [n].size = 8;
620 /* It was 7, now it is 8 in LinuxPPC */
621 if (fr <= PPC_LAST_FPARG_REG) {
622 cinfo->args [n].regtype = RegTypeFP;
623 cinfo->args [n].reg = fr;
625 FP_ALSO_IN_REG (gr += 2);
626 ALWAYS_ON_STACK (stack_size += 8);
628 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
629 cinfo->args [n].regtype = RegTypeBase;
630 cinfo->args [n].reg = ppc_sp; /* in the caller*/
636 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
641 simpletype = mono_type_get_underlying_type (sig->ret)->type;
643 switch (simpletype) {
644 case MONO_TYPE_BOOLEAN:
655 case MONO_TYPE_FNPTR:
656 case MONO_TYPE_CLASS:
657 case MONO_TYPE_OBJECT:
658 case MONO_TYPE_SZARRAY:
659 case MONO_TYPE_ARRAY:
660 case MONO_TYPE_STRING:
661 cinfo->ret.reg = ppc_r3;
665 cinfo->ret.reg = ppc_r3;
669 cinfo->ret.reg = ppc_f1;
670 cinfo->ret.regtype = RegTypeFP;
672 case MONO_TYPE_VALUETYPE:
674 case MONO_TYPE_TYPEDBYREF:
678 g_error ("Can't handle as return value 0x%x", sig->ret->type);
682 /* align stack size to 16 */
683 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
684 stack_size = (stack_size + 15) & ~15;
686 cinfo->stack_usage = stack_size;
692 * Set var information according to the calling convention. ppc version.
693 * The locals var stuff should most likely be split in another method.
696 mono_arch_allocate_vars (MonoCompile *m)
698 MonoMethodSignature *sig;
699 MonoMethodHeader *header;
701 int i, offset, size, align, curinst;
702 int frame_reg = ppc_sp;
705 /* allow room for the vararg method args: void* and long/double */
706 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
707 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
708 /* this is bug #60332: remove when #59509 is fixed, so no weird vararg
709 * call convs needs to be handled this way.
711 if (m->flags & MONO_CFG_HAS_VARARGS)
712 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
713 /* gtk-sharp and other broken code will dllimport vararg functions even with
714 * non-varargs signatures. Since there is little hope people will get this right
715 * we assume they won't.
717 if (m->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE)
718 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
720 header = mono_method_get_header (m->method);
723 * We use the frame register also for any method that has
724 * exception clauses. This way, when the handlers are called,
725 * the code will reference local variables using the frame reg instead of
726 * the stack pointer: if we had to restore the stack pointer, we'd
727 * corrupt the method frames that are already on the stack (since
728 * filters get called before stack unwinding happens) when the filter
729 * code would call any method (this also applies to finally etc.).
731 if ((m->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
733 m->frame_reg = frame_reg;
734 if (frame_reg != ppc_sp) {
735 m->used_int_regs |= 1 << frame_reg;
738 sig = mono_method_signature (m->method);
742 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
743 m->ret->opcode = OP_REGVAR;
744 m->ret->inst_c0 = ppc_r3;
746 /* FIXME: handle long and FP values */
747 switch (mono_type_get_underlying_type (sig->ret)->type) {
751 m->ret->opcode = OP_REGVAR;
752 m->ret->inst_c0 = ppc_r3;
756 /* local vars are at a positive offset from the stack pointer */
758 * also note that if the function uses alloca, we use ppc_r31
759 * to point at the local variables.
761 offset = PPC_MINIMAL_STACK_SIZE; /* linkage area */
762 /* align the offset to 16 bytes: not sure this is needed here */
764 //offset &= ~(16 - 1);
766 /* add parameter area size for called functions */
767 offset += m->param_area;
771 /* allow room to save the return value */
772 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
775 /* the MonoLMF structure is stored just below the stack pointer */
778 /* this stuff should not be needed on ppc and the new jit,
779 * because a call on ppc to the handlers doesn't change the
780 * stack pointer and the jist doesn't manipulate the stack pointer
781 * for operations involving valuetypes.
783 /* reserve space to store the esp */
784 offset += sizeof (gpointer);
786 /* this is a global constant */
787 mono_exc_esp_offset = offset;
789 if (sig->call_convention == MONO_CALL_VARARG) {
790 m->sig_cookie = PPC_STACK_PARAM_OFFSET;
793 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
795 offset += sizeof(gpointer) - 1;
796 offset &= ~(sizeof(gpointer) - 1);
797 inst->inst_offset = offset;
798 inst->opcode = OP_REGOFFSET;
799 inst->inst_basereg = frame_reg;
800 offset += sizeof(gpointer);
801 if (sig->call_convention == MONO_CALL_VARARG)
802 m->sig_cookie += sizeof (gpointer);
805 curinst = m->locals_start;
806 for (i = curinst; i < m->num_varinfo; ++i) {
807 inst = m->varinfo [i];
808 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
811 /* inst->unused indicates native sized value types, this is used by the
812 * pinvoke wrappers when they call functions returning structure */
813 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
814 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), &align);
816 size = mono_type_size (inst->inst_vtype, &align);
819 offset &= ~(align - 1);
820 inst->inst_offset = offset;
821 inst->opcode = OP_REGOFFSET;
822 inst->inst_basereg = frame_reg;
824 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
829 inst = m->varinfo [curinst];
830 if (inst->opcode != OP_REGVAR) {
831 inst->opcode = OP_REGOFFSET;
832 inst->inst_basereg = frame_reg;
833 offset += sizeof (gpointer) - 1;
834 offset &= ~(sizeof (gpointer) - 1);
835 inst->inst_offset = offset;
836 offset += sizeof (gpointer);
837 if (sig->call_convention == MONO_CALL_VARARG)
838 m->sig_cookie += sizeof (gpointer);
843 for (i = 0; i < sig->param_count; ++i) {
844 inst = m->varinfo [curinst];
845 if (inst->opcode != OP_REGVAR) {
846 inst->opcode = OP_REGOFFSET;
847 inst->inst_basereg = frame_reg;
848 size = mono_type_size (sig->params [i], &align);
850 offset &= ~(align - 1);
851 inst->inst_offset = offset;
853 if ((sig->call_convention == MONO_CALL_VARARG) && (i < sig->sentinelpos))
854 m->sig_cookie += size;
859 /* align the offset to 16 bytes */
864 m->stack_offset = offset;
868 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
869 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
873 * take the arguments and generate the arch-specific
874 * instructions to properly call the function in call.
875 * This includes pushing, moving arguments to the right register
877 * Issue: who does the spilling if needed, and when?
880 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
882 MonoMethodSignature *sig;
887 sig = call->signature;
888 n = sig->param_count + sig->hasthis;
890 cinfo = calculate_sizes (sig, sig->pinvoke);
891 if (cinfo->struct_ret)
892 call->used_iregs |= 1 << cinfo->struct_ret;
894 for (i = 0; i < n; ++i) {
895 ainfo = cinfo->args + i;
896 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
898 cfg->disable_aot = TRUE;
900 MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
901 sig_arg->inst_p0 = call->signature;
903 MONO_INST_NEW (cfg, arg, OP_OUTARG);
904 arg->inst_imm = cinfo->sig_cookie.offset;
905 arg->inst_left = sig_arg;
907 /* prepend, so they get reversed */
908 arg->next = call->out_args;
909 call->out_args = arg;
911 if (is_virtual && i == 0) {
912 /* the argument will be attached to the call instrucion */
914 call->used_iregs |= 1 << ainfo->reg;
916 MONO_INST_NEW (cfg, arg, OP_OUTARG);
918 arg->cil_code = in->cil_code;
920 arg->type = in->type;
921 /* prepend, we'll need to reverse them later */
922 arg->next = call->out_args;
923 call->out_args = arg;
924 if (ainfo->regtype == RegTypeGeneral) {
925 arg->unused = ainfo->reg;
926 call->used_iregs |= 1 << ainfo->reg;
927 if (arg->type == STACK_I8)
928 call->used_iregs |= 1 << (ainfo->reg + 1);
929 } else if (ainfo->regtype == RegTypeStructByAddr) {
930 /* FIXME: where si the data allocated? */
931 arg->unused = ainfo->reg;
932 call->used_iregs |= 1 << ainfo->reg;
933 } else if (ainfo->regtype == RegTypeStructByVal) {
935 /* mark the used regs */
936 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
937 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
939 arg->opcode = OP_OUTARG_VT;
940 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
941 arg->inst_imm = ainfo->offset;
942 } else if (ainfo->regtype == RegTypeBase) {
943 arg->opcode = OP_OUTARG;
944 arg->unused = ainfo->reg | (ainfo->size << 8);
945 arg->inst_imm = ainfo->offset;
946 } else if (ainfo->regtype == RegTypeFP) {
947 arg->opcode = OP_OUTARG_R8;
948 arg->unused = ainfo->reg;
949 call->used_fregs |= 1 << ainfo->reg;
950 if (ainfo->size == 4) {
951 arg->opcode = OP_OUTARG_R8;
952 /* we reduce the precision */
954 MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
955 conv->inst_left = arg->inst_left;
956 arg->inst_left = conv;*/
959 g_assert_not_reached ();
964 * Reverse the call->out_args list.
967 MonoInst *prev = NULL, *list = call->out_args, *next;
974 call->out_args = prev;
976 call->stack_usage = cinfo->stack_usage;
977 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
978 cfg->flags |= MONO_CFG_HAS_CALLS;
980 * should set more info in call, such as the stack space
981 * used by the args that needs to be added back to esp
989 * Allow tracing to work with this interface (with an optional argument)
993 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
997 ppc_load (code, ppc_r3, cfg->method);
998 ppc_li (code, ppc_r4, 0); /* NULL ebp for now */
999 ppc_load (code, ppc_r0, func);
1000 ppc_mtlr (code, ppc_r0);
1014 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
1017 int save_mode = SAVE_NONE;
1019 MonoMethod *method = cfg->method;
1020 int rtype = mono_type_get_underlying_type (mono_method_signature (method)->ret)->type;
1021 int save_offset = PPC_STACK_PARAM_OFFSET + cfg->param_area;
1025 offset = code - cfg->native_code;
1026 /* we need about 16 instructions */
1027 if (offset > (cfg->code_size - 16 * 4)) {
1028 cfg->code_size *= 2;
1029 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1030 code = cfg->native_code + offset;
1034 case MONO_TYPE_VOID:
1035 /* special case string .ctor icall */
1036 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
1037 save_mode = SAVE_ONE;
1039 save_mode = SAVE_NONE;
1043 save_mode = SAVE_TWO;
1047 save_mode = SAVE_FP;
1049 case MONO_TYPE_VALUETYPE:
1050 save_mode = SAVE_STRUCT;
1053 save_mode = SAVE_ONE;
1057 switch (save_mode) {
1059 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
1060 ppc_stw (code, ppc_r4, save_offset + 4, cfg->frame_reg);
1061 if (enable_arguments) {
1062 ppc_mr (code, ppc_r5, ppc_r4);
1063 ppc_mr (code, ppc_r4, ppc_r3);
1067 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
1068 if (enable_arguments) {
1069 ppc_mr (code, ppc_r4, ppc_r3);
1073 ppc_stfd (code, ppc_f1, save_offset, cfg->frame_reg);
1074 if (enable_arguments) {
1075 /* FIXME: what reg? */
1076 ppc_fmr (code, ppc_f3, ppc_f1);
1077 ppc_lwz (code, ppc_r4, save_offset, cfg->frame_reg);
1078 ppc_lwz (code, ppc_r5, save_offset + 4, cfg->frame_reg);
1082 if (enable_arguments) {
1083 /* FIXME: get the actual address */
1084 ppc_mr (code, ppc_r4, ppc_r3);
1092 ppc_load (code, ppc_r3, cfg->method);
1093 ppc_load (code, ppc_r0, func);
1094 ppc_mtlr (code, ppc_r0);
1097 switch (save_mode) {
1099 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
1100 ppc_lwz (code, ppc_r4, save_offset + 4, cfg->frame_reg);
1103 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
1106 ppc_lfd (code, ppc_f1, save_offset, cfg->frame_reg);
1116 * Conditional branches have a small offset, so if it is likely overflowed,
1117 * we do a branch to the end of the method (uncond branches have much larger
1118 * offsets) where we perform the conditional and jump back unconditionally.
1119 * It's slightly slower, since we add two uncond branches, but it's very simple
1120 * with the current patch implementation and such large methods are likely not
1121 * going to be perf critical anyway.
1130 #define EMIT_COND_BRANCH_FLAGS(ins,b0,b1) \
1131 if (ins->flags & MONO_INST_BRLABEL) { \
1132 if (0 && ins->inst_i0->inst_c0) { \
1133 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff); \
1135 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1136 ppc_bc (code, (b0), (b1), 0); \
1139 if (0 && ins->inst_true_bb->native_offset) { \
1140 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
1142 int br_disp = ins->inst_true_bb->max_offset - offset; \
1143 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
1144 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
1145 ovfj->bb = ins->inst_true_bb; \
1146 ovfj->ip_offset = 0; \
1147 ovfj->b0_cond = (b0); \
1148 ovfj->b1_cond = (b1); \
1149 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB_OVF, ovfj); \
1152 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1153 ppc_bc (code, (b0), (b1), 0); \
1158 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_b0_table [(cond)], branch_b1_table [(cond)])
1160 /* emit an exception if condition is fail
1162 * We assign the extra code used to throw the implicit exceptions
1163 * to cfg->bb_exit as far as the big branch handling is concerned
1165 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(b0,b1,exc_name) \
1167 int br_disp = cfg->bb_exit->max_offset - offset; \
1168 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
1169 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
1171 ovfj->ip_offset = code - cfg->native_code; \
1172 ovfj->b0_cond = (b0); \
1173 ovfj->b1_cond = (b1); \
1174 /* FIXME: test this code */ \
1175 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj); \
1177 cfg->bb_exit->max_offset += 24; \
1179 mono_add_patch_info (cfg, code - cfg->native_code, \
1180 MONO_PATCH_INFO_EXC, exc_name); \
1181 ppc_bcl (code, (b0), (b1), 0); \
1185 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_b0_table [(cond)], branch_b1_table [(cond)], (exc_name))
1188 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1190 MonoInst *ins, *last_ins = NULL;
1195 switch (ins->opcode) {
1197 /* remove unnecessary multiplication with 1 */
1198 if (ins->inst_imm == 1) {
1199 if (ins->dreg != ins->sreg1) {
1200 ins->opcode = OP_MOVE;
1202 last_ins->next = ins->next;
1207 int power2 = mono_is_power_of_two (ins->inst_imm);
1209 ins->opcode = OP_SHL_IMM;
1210 ins->inst_imm = power2;
1214 case OP_LOAD_MEMBASE:
1215 case OP_LOADI4_MEMBASE:
1217 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1218 * OP_LOAD_MEMBASE offset(basereg), reg
1220 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1221 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1222 ins->inst_basereg == last_ins->inst_destbasereg &&
1223 ins->inst_offset == last_ins->inst_offset) {
1224 if (ins->dreg == last_ins->sreg1) {
1225 last_ins->next = ins->next;
1229 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1230 ins->opcode = OP_MOVE;
1231 ins->sreg1 = last_ins->sreg1;
1235 * Note: reg1 must be different from the basereg in the second load
1236 * OP_LOAD_MEMBASE offset(basereg), reg1
1237 * OP_LOAD_MEMBASE offset(basereg), reg2
1239 * OP_LOAD_MEMBASE offset(basereg), reg1
1240 * OP_MOVE reg1, reg2
1242 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1243 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1244 ins->inst_basereg != last_ins->dreg &&
1245 ins->inst_basereg == last_ins->inst_basereg &&
1246 ins->inst_offset == last_ins->inst_offset) {
1248 if (ins->dreg == last_ins->dreg) {
1249 last_ins->next = ins->next;
1253 ins->opcode = OP_MOVE;
1254 ins->sreg1 = last_ins->dreg;
1257 //g_assert_not_reached ();
1261 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1262 * OP_LOAD_MEMBASE offset(basereg), reg
1264 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1265 * OP_ICONST reg, imm
1267 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1268 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1269 ins->inst_basereg == last_ins->inst_destbasereg &&
1270 ins->inst_offset == last_ins->inst_offset) {
1271 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1272 ins->opcode = OP_ICONST;
1273 ins->inst_c0 = last_ins->inst_imm;
1274 g_assert_not_reached (); // check this rule
1278 case OP_LOADU1_MEMBASE:
1279 case OP_LOADI1_MEMBASE:
1280 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1281 ins->inst_basereg == last_ins->inst_destbasereg &&
1282 ins->inst_offset == last_ins->inst_offset) {
1283 if (ins->dreg == last_ins->sreg1) {
1284 last_ins->next = ins->next;
1288 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1289 ins->opcode = OP_MOVE;
1290 ins->sreg1 = last_ins->sreg1;
1294 case OP_LOADU2_MEMBASE:
1295 case OP_LOADI2_MEMBASE:
1296 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1297 ins->inst_basereg == last_ins->inst_destbasereg &&
1298 ins->inst_offset == last_ins->inst_offset) {
1299 if (ins->dreg == last_ins->sreg1) {
1300 last_ins->next = ins->next;
1304 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1305 ins->opcode = OP_MOVE;
1306 ins->sreg1 = last_ins->sreg1;
1314 ins->opcode = OP_MOVE;
1318 if (ins->dreg == ins->sreg1) {
1320 last_ins->next = ins->next;
1325 * OP_MOVE sreg, dreg
1326 * OP_MOVE dreg, sreg
1328 if (last_ins && last_ins->opcode == OP_MOVE &&
1329 ins->sreg1 == last_ins->dreg &&
1330 ins->dreg == last_ins->sreg1) {
1331 last_ins->next = ins->next;
1340 bb->last_ins = last_ins;
1344 * the branch_b0_table should maintain the order of these
1358 branch_b0_table [] = {
1373 branch_b1_table [] = {
1388 * returns the offset used by spillvar. It allocates a new
1389 * spill variable if necessary.
1392 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1394 MonoSpillInfo **si, *info;
1397 si = &cfg->spill_info;
1399 while (i <= spillvar) {
1402 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1404 info->offset = cfg->stack_offset;
1405 cfg->stack_offset += sizeof (gpointer);
1409 return (*si)->offset;
1415 g_assert_not_reached ();
1420 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1422 MonoSpillInfo **si, *info;
1425 si = &cfg->spill_info_float;
1427 while (i <= spillvar) {
1430 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1432 cfg->stack_offset += 7;
1433 cfg->stack_offset &= ~7;
1434 info->offset = cfg->stack_offset;
1435 cfg->stack_offset += sizeof (double);
1439 return (*si)->offset;
1445 g_assert_not_reached ();
1450 #define DEBUG(a) if (cfg->verbose_level > 1) a
1452 /* use ppc_r3-ppc_10,ppc_r12 as temp registers, f1-f13 for FP registers */
1453 #define PPC_CALLER_REGS ((0xff<<3) | (1<<12) | USE_EXTRA_TEMPS)
1454 #define PPC_CALLER_FREGS (0x3ffe)
1456 #define reg_is_freeable(r) (PPC_CALLER_REGS & 1 << (r))
1457 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 13)
1466 static const char*const * ins_spec = ppcg4;
1469 print_ins (int i, MonoInst *ins)
1471 const char *spec = ins_spec [ins->opcode];
1472 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1473 if (spec [MONO_INST_DEST]) {
1474 if (ins->dreg >= MONO_MAX_IREGS)
1475 g_print (" R%d <-", ins->dreg);
1477 g_print (" %s <-", mono_arch_regname (ins->dreg));
1479 if (spec [MONO_INST_SRC1]) {
1480 if (ins->sreg1 >= MONO_MAX_IREGS)
1481 g_print (" R%d", ins->sreg1);
1483 g_print (" %s", mono_arch_regname (ins->sreg1));
1485 if (spec [MONO_INST_SRC2]) {
1486 if (ins->sreg2 >= MONO_MAX_IREGS)
1487 g_print (" R%d", ins->sreg2);
1489 g_print (" %s", mono_arch_regname (ins->sreg2));
1491 if (spec [MONO_INST_CLOB])
1492 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1497 print_regtrack (RegTrack *t, int num)
1503 for (i = 0; i < num; ++i) {
1506 if (i >= MONO_MAX_IREGS) {
1507 g_snprintf (buf, sizeof(buf), "R%d", i);
1510 r = mono_arch_regname (i);
1511 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1515 typedef struct InstList InstList;
1523 static inline InstList*
1524 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1526 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1536 * Force the spilling of the variable in the symbolic register 'reg'.
1539 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1544 sel = cfg->rs->iassign [reg];
1545 /*i = cfg->rs->isymbolic [sel];
1546 g_assert (i == reg);*/
1548 spill = ++cfg->spill_count;
1549 cfg->rs->iassign [i] = -spill - 1;
1550 mono_regstate_free_int (cfg->rs, sel);
1551 /* we need to create a spill var and insert a load to sel after the current instruction */
1552 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1554 load->inst_basereg = cfg->frame_reg;
1555 load->inst_offset = mono_spillvar_offset (cfg, spill);
1557 while (ins->next != item->prev->data)
1560 load->next = ins->next;
1562 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1563 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1564 g_assert (i == sel);
1570 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1575 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));
1576 /* exclude the registers in the current instruction */
1577 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1578 if (ins->sreg1 >= MONO_MAX_IREGS)
1579 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1581 regmask &= ~ (1 << ins->sreg1);
1582 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1584 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1585 if (ins->sreg2 >= MONO_MAX_IREGS)
1586 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1588 regmask &= ~ (1 << ins->sreg2);
1589 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1591 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1592 regmask &= ~ (1 << ins->dreg);
1593 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1596 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1597 g_assert (regmask); /* need at least a register we can free */
1599 /* we should track prev_use and spill the register that's farther */
1600 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1601 if (regmask & (1 << i)) {
1603 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1607 i = cfg->rs->isymbolic [sel];
1608 spill = ++cfg->spill_count;
1609 cfg->rs->iassign [i] = -spill - 1;
1610 mono_regstate_free_int (cfg->rs, sel);
1611 /* we need to create a spill var and insert a load to sel after the current instruction */
1612 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1614 load->inst_basereg = cfg->frame_reg;
1615 load->inst_offset = mono_spillvar_offset (cfg, spill);
1617 while (ins->next != item->prev->data)
1620 load->next = ins->next;
1622 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1623 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1624 g_assert (i == sel);
1630 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1635 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));
1636 /* exclude the registers in the current instruction */
1637 if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1638 if (ins->sreg1 >= MONO_MAX_FREGS)
1639 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1641 regmask &= ~ (1 << ins->sreg1);
1642 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1644 if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1645 if (ins->sreg2 >= MONO_MAX_FREGS)
1646 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1648 regmask &= ~ (1 << ins->sreg2);
1649 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1651 if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1652 regmask &= ~ (1 << ins->dreg);
1653 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1656 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1657 g_assert (regmask); /* need at least a register we can free */
1659 /* we should track prev_use and spill the register that's farther */
1660 for (i = 0; i < MONO_MAX_FREGS; ++i) {
1661 if (regmask & (1 << i)) {
1663 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1667 i = cfg->rs->fsymbolic [sel];
1668 spill = ++cfg->spill_count;
1669 cfg->rs->fassign [i] = -spill - 1;
1670 mono_regstate_free_float(cfg->rs, sel);
1671 /* we need to create a spill var and insert a load to sel after the current instruction */
1672 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1674 load->inst_basereg = cfg->frame_reg;
1675 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1677 while (ins->next != item->prev->data)
1680 load->next = ins->next;
1682 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)));
1683 i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1684 g_assert (i == sel);
1690 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1693 MONO_INST_NEW (cfg, copy, OP_MOVE);
1697 copy->next = ins->next;
1700 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1705 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1708 MONO_INST_NEW (cfg, copy, OP_FMOVE);
1712 copy->next = ins->next;
1715 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1720 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1723 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1725 store->inst_destbasereg = cfg->frame_reg;
1726 store->inst_offset = mono_spillvar_offset (cfg, spill);
1728 store->next = ins->next;
1731 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)));
1736 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1739 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1741 store->inst_destbasereg = cfg->frame_reg;
1742 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1744 store->next = ins->next;
1747 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)));
1752 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1755 g_assert (item->next);
1756 prev = item->next->data;
1758 while (prev->next != ins)
1760 to_insert->next = ins;
1761 prev->next = to_insert;
1763 * needed otherwise in the next instruction we can add an ins to the
1764 * end and that would get past this instruction.
1766 item->data = to_insert;
1770 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1772 int val = cfg->rs->iassign [sym_reg];
1776 /* the register gets spilled after this inst */
1779 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1781 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1782 cfg->rs->iassign [sym_reg] = val;
1783 /* add option to store before the instruction for src registers */
1785 create_spilled_store (cfg, spill, val, sym_reg, ins);
1787 cfg->rs->isymbolic [val] = sym_reg;
1792 * Local register allocation.
1793 * We first scan the list of instructions and we save the liveness info of
1794 * each register (when the register is first used, when it's value is set etc.).
1795 * We also reverse the list of instructions (in the InstList list) because assigning
1796 * registers backwards allows for more tricks to be used.
1799 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1802 MonoRegState *rs = cfg->rs;
1804 RegTrack *reginfo, *reginfof;
1805 RegTrack *reginfo1, *reginfo2, *reginfod;
1806 InstList *tmp, *reversed = NULL;
1808 guint32 src1_mask, src2_mask, dest_mask;
1809 guint32 cur_iregs, cur_fregs;
1813 rs->next_vireg = bb->max_ireg;
1814 rs->next_vfreg = bb->max_freg;
1815 mono_regstate_assign (rs);
1816 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1817 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1818 rs->ifree_mask = PPC_CALLER_REGS;
1819 rs->ffree_mask = PPC_CALLER_FREGS;
1823 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1824 /* forward pass on the instructions to collect register liveness info */
1826 spec = ins_spec [ins->opcode];
1827 DEBUG (print_ins (i, ins));
1828 /*if (spec [MONO_INST_CLOB] == 'c') {
1829 MonoCallInst * call = (MonoCallInst*)ins;
1832 if (spec [MONO_INST_SRC1]) {
1833 if (spec [MONO_INST_SRC1] == 'f')
1834 reginfo1 = reginfof;
1837 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1838 reginfo1 [ins->sreg1].last_use = i;
1842 if (spec [MONO_INST_SRC2]) {
1843 if (spec [MONO_INST_SRC2] == 'f')
1844 reginfo2 = reginfof;
1847 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1848 reginfo2 [ins->sreg2].last_use = i;
1852 if (spec [MONO_INST_DEST]) {
1853 if (spec [MONO_INST_DEST] == 'f')
1854 reginfod = reginfof;
1857 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1858 reginfod [ins->dreg].killed_in = i;
1859 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1860 reginfod [ins->dreg].last_use = i;
1861 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1862 reginfod [ins->dreg].born_in = i;
1863 if (spec [MONO_INST_DEST] == 'l') {
1864 /* result in eax:edx, the virtual register is allocated sequentially */
1865 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1866 reginfod [ins->dreg + 1].last_use = i;
1867 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1868 reginfod [ins->dreg + 1].born_in = i;
1873 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1878 cur_iregs = PPC_CALLER_REGS;
1879 cur_fregs = PPC_CALLER_FREGS;
1881 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1882 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1885 int prev_dreg, prev_sreg1, prev_sreg2;
1888 spec = ins_spec [ins->opcode];
1889 DEBUG (g_print ("processing:"));
1890 DEBUG (print_ins (i, ins));
1891 /* make the register available for allocation: FIXME add fp reg */
1892 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1893 cur_iregs |= 1 << ins->dreg;
1894 DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1895 } else if (ins->opcode == OP_SETFREG) {
1896 cur_fregs |= 1 << ins->dreg;
1897 DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1898 } else if (spec [MONO_INST_CLOB] == 'c') {
1899 MonoCallInst *cinst = (MonoCallInst*)ins;
1900 DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1901 DEBUG (g_print ("excluding fpregs 0x%x from cur_fregs (0x%x)\n", cinst->used_fregs, cur_fregs));
1902 cur_iregs &= ~cinst->used_iregs;
1903 cur_fregs &= ~cinst->used_fregs;
1904 DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1905 DEBUG (g_print ("available cur_fregs: 0x%x\n", cur_fregs));
1906 /* registers used by the calling convention are excluded from
1907 * allocation: they will be selectively enabled when they are
1908 * assigned by the special SETREG opcodes.
1911 dest_mask = src1_mask = src2_mask = cur_iregs;
1912 /* update for use with FP regs... */
1913 if (spec [MONO_INST_DEST] == 'f') {
1914 dest_mask = cur_fregs;
1915 if (ins->dreg >= MONO_MAX_FREGS) {
1916 val = rs->fassign [ins->dreg];
1917 prev_dreg = ins->dreg;
1921 /* the register gets spilled after this inst */
1924 val = mono_regstate_alloc_float (rs, dest_mask);
1926 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1927 rs->fassign [ins->dreg] = val;
1929 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1931 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1932 rs->fsymbolic [val] = prev_dreg;
1934 if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != ppc_f1) {
1935 /* this instruction only outputs to ppc_f1, need to copy */
1936 create_copy_ins_float (cfg, ins->dreg, ppc_f1, ins);
1941 if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1942 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1943 mono_regstate_free_float (rs, ins->dreg);
1945 } else if (ins->dreg >= MONO_MAX_IREGS) {
1946 val = rs->iassign [ins->dreg];
1947 prev_dreg = ins->dreg;
1951 /* the register gets spilled after this inst */
1954 val = mono_regstate_alloc_int (rs, dest_mask);
1956 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1957 rs->iassign [ins->dreg] = val;
1959 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1961 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1962 rs->isymbolic [val] = prev_dreg;
1964 if (spec [MONO_INST_DEST] == 'l') {
1965 int hreg = prev_dreg + 1;
1966 val = rs->iassign [hreg];
1970 /* the register gets spilled after this inst */
1973 val = mono_regstate_alloc_int (rs, dest_mask);
1975 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1976 rs->iassign [hreg] = val;
1978 create_spilled_store (cfg, spill, val, hreg, ins);
1980 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1981 rs->isymbolic [val] = hreg;
1982 /* FIXME:? ins->dreg = val; */
1983 if (ins->dreg == ppc_r4) {
1985 create_copy_ins (cfg, val, ppc_r3, ins);
1986 } else if (ins->dreg == ppc_r3) {
1987 if (val == ppc_r4) {
1989 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1990 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1991 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1993 /* two forced copies */
1994 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1995 create_copy_ins (cfg, val, ppc_r3, ins);
1998 if (val == ppc_r3) {
1999 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
2001 /* two forced copies */
2002 create_copy_ins (cfg, val, ppc_r3, ins);
2003 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
2006 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
2007 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
2008 mono_regstate_free_int (rs, val);
2010 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != ppc_r3 && spec [MONO_INST_CLOB] != 'd') {
2011 /* this instruction only outputs to ppc_r3, need to copy */
2012 create_copy_ins (cfg, ins->dreg, ppc_r3, ins);
2017 if (spec [MONO_INST_DEST] == 'f' && freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i)) {
2018 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
2019 mono_regstate_free_float (rs, ins->dreg);
2020 } else if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
2021 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
2022 mono_regstate_free_int (rs, ins->dreg);
2024 if (spec [MONO_INST_SRC1] == 'f') {
2025 src1_mask = cur_fregs;
2026 if (ins->sreg1 >= MONO_MAX_FREGS) {
2027 val = rs->fassign [ins->sreg1];
2028 prev_sreg1 = ins->sreg1;
2032 /* the register gets spilled after this inst */
2035 //g_assert (val == -1); /* source cannot be spilled */
2036 val = mono_regstate_alloc_float (rs, src1_mask);
2038 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
2039 rs->fassign [ins->sreg1] = val;
2040 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2042 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
2043 insert_before_ins (ins, tmp, store);
2046 rs->fsymbolic [val] = prev_sreg1;
2051 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
2052 val = rs->iassign [ins->sreg1];
2053 prev_sreg1 = ins->sreg1;
2057 /* the register gets spilled after this inst */
2060 if (0 && ins->opcode == OP_MOVE) {
2062 * small optimization: the dest register is already allocated
2063 * but the src one is not: we can simply assign the same register
2064 * here and peephole will get rid of the instruction later.
2065 * This optimization may interfere with the clobbering handling:
2066 * it removes a mov operation that will be added again to handle clobbering.
2067 * There are also some other issues that should with make testjit.
2069 mono_regstate_alloc_int (rs, 1 << ins->dreg);
2070 val = rs->iassign [ins->sreg1] = ins->dreg;
2071 //g_assert (val >= 0);
2072 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2074 //g_assert (val == -1); /* source cannot be spilled */
2075 val = mono_regstate_alloc_int (rs, src1_mask);
2077 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
2078 rs->iassign [ins->sreg1] = val;
2079 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
2082 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
2083 insert_before_ins (ins, tmp, store);
2086 rs->isymbolic [val] = prev_sreg1;
2091 if (spec [MONO_INST_SRC2] == 'f') {
2092 src2_mask = cur_fregs;
2093 if (ins->sreg2 >= MONO_MAX_FREGS) {
2094 val = rs->fassign [ins->sreg2];
2095 prev_sreg2 = ins->sreg2;
2099 /* the register gets spilled after this inst */
2102 val = mono_regstate_alloc_float (rs, src2_mask);
2104 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
2105 rs->fassign [ins->sreg2] = val;
2106 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
2108 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
2110 rs->fsymbolic [val] = prev_sreg2;
2115 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
2116 val = rs->iassign [ins->sreg2];
2117 prev_sreg2 = ins->sreg2;
2121 /* the register gets spilled after this inst */
2124 val = mono_regstate_alloc_int (rs, src2_mask);
2126 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
2127 rs->iassign [ins->sreg2] = val;
2128 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
2130 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
2132 rs->isymbolic [val] = prev_sreg2;
2138 if (spec [MONO_INST_CLOB] == 'c') {
2140 guint32 clob_mask = PPC_CALLER_REGS;
2141 for (j = 0; j < MONO_MAX_IREGS; ++j) {
2143 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
2144 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
2148 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2149 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2150 mono_regstate_free_int (rs, ins->sreg1);
2152 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2153 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2154 mono_regstate_free_int (rs, ins->sreg2);
2157 //DEBUG (print_ins (i, ins));
2160 cfg->max_ireg = MAX (cfg->max_ireg, rs->max_ireg);
2164 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2166 /* sreg is a float, dreg is an integer reg. ppc_f0 is used a scratch */
2167 ppc_fctiwz (code, ppc_f0, sreg);
2168 ppc_stfd (code, ppc_f0, -8, ppc_sp);
2169 ppc_lwz (code, dreg, -4, ppc_sp);
2172 ppc_andid (code, dreg, dreg, 0xff);
2174 ppc_andid (code, dreg, dreg, 0xffff);
2177 ppc_extsb (code, dreg, dreg);
2179 ppc_extsh (code, dreg, dreg);
2184 static unsigned char*
2185 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2188 int sreg = tree->sreg1;
2189 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2190 if (tree->flags & MONO_INST_INIT) {
2192 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2193 x86_push_reg (code, X86_EAX);
2196 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2197 x86_push_reg (code, X86_ECX);
2200 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2201 x86_push_reg (code, X86_EDI);
2205 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2206 if (sreg != X86_ECX)
2207 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2208 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2210 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2212 x86_prefix (code, X86_REP_PREFIX);
2215 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2216 x86_pop_reg (code, X86_EDI);
2217 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2218 x86_pop_reg (code, X86_ECX);
2219 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2220 x86_pop_reg (code, X86_EAX);
2233 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2236 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2237 PatchData *pdata = (PatchData*)user_data;
2238 guchar *code = data;
2239 guint32 *thunks = data;
2240 guint32 *endthunks = (guint32*)(code + bsize);
2244 int difflow, diffhigh;
2246 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
2247 difflow = (char*)pdata->code - (char*)thunks;
2248 diffhigh = (char*)pdata->code - (char*)endthunks;
2249 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
2252 templ = (guchar*)load;
2253 ppc_lis (templ, ppc_r0, (guint32)(pdata->target) >> 16);
2254 ppc_ori (templ, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2256 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2257 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2258 while (thunks < endthunks) {
2259 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2260 if ((thunks [0] == load [0]) && (thunks [1] == load [1])) {
2261 ppc_patch (pdata->code, (guchar*)thunks);
2262 mono_arch_flush_icache (pdata->code, 4);
2265 } else if ((thunks [0] == 0) && (thunks [1] == 0)) {
2266 /* found a free slot instead: emit thunk */
2267 code = (guchar*)thunks;
2268 ppc_lis (code, ppc_r0, (guint32)(pdata->target) >> 16);
2269 ppc_ori (code, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2270 ppc_mtctr (code, ppc_r0);
2271 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2272 mono_arch_flush_icache ((guchar*)thunks, 16);
2274 ppc_patch (pdata->code, (guchar*)thunks);
2275 mono_arch_flush_icache (pdata->code, 4);
2279 /* skip 16 bytes, the size of the thunk */
2283 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2289 handle_thunk (int absolute, guchar *code, guchar *target) {
2290 MonoDomain *domain = mono_domain_get ();
2294 pdata.target = target;
2295 pdata.absolute = absolute;
2298 mono_domain_lock (domain);
2299 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2302 /* this uses the first available slot */
2304 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2306 mono_domain_unlock (domain);
2308 if (pdata.found != 1)
2309 g_print ("thunk failed for %p from %p\n", target, code);
2310 g_assert (pdata.found == 1);
2314 ppc_patch (guchar *code, guchar *target)
2316 guint32 ins = *(guint32*)code;
2317 guint32 prim = ins >> 26;
2320 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2322 // prefer relative branches, they are more position independent (e.g. for AOT compilation).
2323 gint diff = target - code;
2325 if (diff <= 33554431){
2326 ins = (18 << 26) | (diff) | (ins & 1);
2327 *(guint32*)code = ins;
2331 /* diff between 0 and -33554432 */
2332 if (diff >= -33554432){
2333 ins = (18 << 26) | (diff & ~0xfc000000) | (ins & 1);
2334 *(guint32*)code = ins;
2339 if ((glong)target >= 0){
2340 if ((glong)target <= 33554431){
2341 ins = (18 << 26) | ((guint32) target) | (ins & 1) | 2;
2342 *(guint32*)code = ins;
2346 if ((glong)target >= -33554432){
2347 ins = (18 << 26) | (((guint32)target) & ~0xfc000000) | (ins & 1) | 2;
2348 *(guint32*)code = ins;
2353 handle_thunk (TRUE, code, target);
2356 g_assert_not_reached ();
2363 guint32 li = (guint32)target;
2364 ins = (ins & 0xffff0000) | (ins & 3);
2365 ovf = li & 0xffff0000;
2366 if (ovf != 0 && ovf != 0xffff0000)
2367 g_assert_not_reached ();
2370 // FIXME: assert the top bits of li are 0
2372 gint diff = target - code;
2373 ins = (ins & 0xffff0000) | (ins & 3);
2374 ovf = diff & 0xffff0000;
2375 if (ovf != 0 && ovf != 0xffff0000)
2376 g_assert_not_reached ();
2380 *(guint32*)code = ins;
2384 if (prim == 15 || ins == 0x4e800021) {
2386 /* the trampoline code will try to patch the blrl */
2387 if (ins == 0x4e800021) {
2390 /* this is the lis/ori/mtlr/blrl sequence */
2391 seq = (guint32*)code;
2392 g_assert ((seq [0] >> 26) == 15);
2393 g_assert ((seq [1] >> 26) == 24);
2394 g_assert ((seq [2] >> 26) == 31);
2395 g_assert (seq [3] == 0x4e800021);
2396 /* FIXME: make this thread safe */
2397 ppc_lis (code, ppc_r0, (guint32)(target) >> 16);
2398 ppc_ori (code, ppc_r0, ppc_r0, (guint32)(target) & 0xffff);
2399 mono_arch_flush_icache (code - 8, 8);
2401 g_assert_not_reached ();
2403 // g_print ("patched with 0x%08x\n", ins);
2407 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2412 guint8 *code = cfg->native_code + cfg->code_len;
2413 MonoInst *last_ins = NULL;
2414 guint last_offset = 0;
2417 if (cfg->opt & MONO_OPT_PEEPHOLE)
2418 peephole_pass (cfg, bb);
2420 /* we don't align basic blocks of loops on ppc */
2422 if (cfg->verbose_level > 2)
2423 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2425 cpos = bb->max_offset;
2427 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2428 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2429 //g_assert (!mono_compile_aot);
2432 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2433 /* this is not thread save, but good enough */
2434 /* fixme: howto handle overflows? */
2435 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2440 offset = code - cfg->native_code;
2442 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2444 if (offset > (cfg->code_size - max_len - 16)) {
2445 cfg->code_size *= 2;
2446 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2447 code = cfg->native_code + offset;
2449 // if (ins->cil_code)
2450 // g_print ("cil code\n");
2451 mono_debug_record_line_number (cfg, ins, offset);
2453 switch (ins->opcode) {
2455 emit_tls_access (code, ins->dreg, ins->inst_offset);
2458 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2459 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
2462 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2463 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
2465 case OP_MEMORY_BARRIER:
2468 case OP_STOREI1_MEMBASE_IMM:
2469 ppc_li (code, ppc_r0, ins->inst_imm);
2470 if (ppc_is_imm16 (ins->inst_offset)) {
2471 ppc_stb (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2473 ppc_load (code, ppc_r11, ins->inst_offset);
2474 ppc_stbx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2477 case OP_STOREI2_MEMBASE_IMM:
2478 ppc_li (code, ppc_r0, ins->inst_imm);
2479 if (ppc_is_imm16 (ins->inst_offset)) {
2480 ppc_sth (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2482 ppc_load (code, ppc_r11, ins->inst_offset);
2483 ppc_sthx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2486 case OP_STORE_MEMBASE_IMM:
2487 case OP_STOREI4_MEMBASE_IMM:
2488 ppc_load (code, ppc_r0, ins->inst_imm);
2489 if (ppc_is_imm16 (ins->inst_offset)) {
2490 ppc_stw (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2492 ppc_load (code, ppc_r11, ins->inst_offset);
2493 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2496 case OP_STOREI1_MEMBASE_REG:
2497 if (ppc_is_imm16 (ins->inst_offset)) {
2498 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2500 ppc_load (code, ppc_r11, ins->inst_offset);
2501 ppc_stbx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2504 case OP_STOREI2_MEMBASE_REG:
2505 if (ppc_is_imm16 (ins->inst_offset)) {
2506 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2508 ppc_load (code, ppc_r11, ins->inst_offset);
2509 ppc_sthx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2512 case OP_STORE_MEMBASE_REG:
2513 case OP_STOREI4_MEMBASE_REG:
2514 if (ppc_is_imm16 (ins->inst_offset)) {
2515 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2517 ppc_load (code, ppc_r11, ins->inst_offset);
2518 ppc_stwx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2524 g_assert_not_reached ();
2525 //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2528 g_assert_not_reached ();
2529 //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2530 //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2532 case OP_LOAD_MEMBASE:
2533 case OP_LOADI4_MEMBASE:
2534 case OP_LOADU4_MEMBASE:
2535 if (ppc_is_imm16 (ins->inst_offset)) {
2536 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2538 ppc_load (code, ppc_r11, ins->inst_offset);
2539 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2542 case OP_LOADI1_MEMBASE:
2543 case OP_LOADU1_MEMBASE:
2544 if (ppc_is_imm16 (ins->inst_offset)) {
2545 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2547 ppc_load (code, ppc_r11, ins->inst_offset);
2548 ppc_lbzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2550 if (ins->opcode == OP_LOADI1_MEMBASE)
2551 ppc_extsb (code, ins->dreg, ins->dreg);
2553 case OP_LOADU2_MEMBASE:
2554 if (ppc_is_imm16 (ins->inst_offset)) {
2555 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2557 ppc_load (code, ppc_r11, ins->inst_offset);
2558 ppc_lhzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2561 case OP_LOADI2_MEMBASE:
2562 if (ppc_is_imm16 (ins->inst_offset)) {
2563 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2565 ppc_load (code, ppc_r11, ins->inst_offset);
2566 ppc_lhax (code, ins->dreg, ppc_r11, ins->inst_basereg);
2570 ppc_extsb (code, ins->dreg, ins->sreg1);
2573 ppc_extsh (code, ins->dreg, ins->sreg1);
2576 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
2579 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
2583 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2584 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2585 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN)))
2586 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2588 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2590 case OP_COMPARE_IMM:
2592 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2593 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2594 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN))) {
2595 if (ppc_is_uimm16 (ins->inst_imm)) {
2596 ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2598 ppc_load (code, ppc_r11, ins->inst_imm);
2599 ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2602 if (ppc_is_imm16 (ins->inst_imm)) {
2603 ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2605 ppc_load (code, ppc_r11, ins->inst_imm);
2606 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2610 case OP_X86_TEST_NULL:
2611 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2617 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2620 ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2623 ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2626 if (ppc_is_imm16 (ins->inst_imm)) {
2627 ppc_addic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2629 ppc_load (code, ppc_r11, ins->inst_imm);
2630 ppc_addc (code, ins->dreg, ins->sreg1, ppc_r11);
2634 if (ppc_is_imm16 (ins->inst_imm)) {
2635 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2637 ppc_load (code, ppc_r11, ins->inst_imm);
2638 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2642 ppc_load (code, ppc_r11, ins->inst_imm);
2643 ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2646 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2648 ppc_addo (code, ins->dreg, ins->sreg1, ins->sreg2);
2649 ppc_mfspr (code, ppc_r0, ppc_xer);
2650 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2651 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2653 case CEE_ADD_OVF_UN:
2654 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2656 ppc_addco (code, ins->dreg, ins->sreg1, ins->sreg2);
2657 ppc_mfspr (code, ppc_r0, ppc_xer);
2658 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2659 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2662 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2664 ppc_subfo (code, ins->dreg, ins->sreg2, ins->sreg1);
2665 ppc_mfspr (code, ppc_r0, ppc_xer);
2666 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2667 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2669 case CEE_SUB_OVF_UN:
2670 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2672 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2673 ppc_mfspr (code, ppc_r0, ppc_xer);
2674 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2675 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2677 case OP_ADD_OVF_CARRY:
2678 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2680 ppc_addeo (code, ins->dreg, ins->sreg1, ins->sreg2);
2681 ppc_mfspr (code, ppc_r0, ppc_xer);
2682 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2683 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2685 case OP_ADD_OVF_UN_CARRY:
2686 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2688 ppc_addeo (code, ins->dreg, ins->sreg1, ins->sreg2);
2689 ppc_mfspr (code, ppc_r0, ppc_xer);
2690 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2691 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2693 case OP_SUB_OVF_CARRY:
2694 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2696 ppc_subfeo (code, ins->dreg, ins->sreg2, ins->sreg1);
2697 ppc_mfspr (code, ppc_r0, ppc_xer);
2698 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2699 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2701 case OP_SUB_OVF_UN_CARRY:
2702 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2704 ppc_subfeo (code, ins->dreg, ins->sreg2, ins->sreg1);
2705 ppc_mfspr (code, ppc_r0, ppc_xer);
2706 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2707 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2710 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2713 ppc_load (code, ppc_r11, ins->inst_imm);
2714 ppc_subfc (code, ins->dreg, ppc_r11, ins->sreg1);
2717 ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2720 ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2723 // we add the negated value
2724 if (ppc_is_imm16 (-ins->inst_imm))
2725 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2727 ppc_load (code, ppc_r11, ins->inst_imm);
2728 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2732 ppc_load (code, ppc_r11, ins->inst_imm);
2733 ppc_subfe (code, ins->dreg, ppc_r11, ins->sreg1);
2736 g_assert (ppc_is_imm16 (ins->inst_imm));
2737 ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2740 ppc_subfze (code, ins->dreg, ins->sreg1);
2743 /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2744 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2747 if (!(ins->inst_imm & 0xffff0000)) {
2748 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2749 } else if (!(ins->inst_imm & 0xffff)) {
2750 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2752 ppc_load (code, ppc_r11, ins->inst_imm);
2753 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2757 guint32 *divisor_is_m1;
2758 /* XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2760 ppc_cmpi (code, 0, 0, ins->sreg2, -1);
2761 divisor_is_m1 = code;
2762 ppc_bc (code, PPC_BR_FALSE | PPC_BR_LIKELY, PPC_BR_EQ, 0);
2763 ppc_lis (code, ppc_r11, 0x8000);
2764 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2765 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "ArithmeticException");
2766 ppc_patch (divisor_is_m1, code);
2767 /* XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2769 ppc_divwod (code, ins->dreg, ins->sreg1, ins->sreg2);
2770 ppc_mfspr (code, ppc_r0, ppc_xer);
2771 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2772 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2776 ppc_divwuod (code, ins->dreg, ins->sreg1, ins->sreg2);
2777 ppc_mfspr (code, ppc_r0, ppc_xer);
2778 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2779 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2782 g_assert_not_reached ();
2784 ppc_load (code, ppc_r11, ins->inst_imm);
2785 ppc_divwod (code, ins->dreg, ins->sreg1, ppc_r11);
2786 ppc_mfspr (code, ppc_r0, ppc_xer);
2787 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2788 /* FIXME: use OverflowException for 0x80000000/-1 */
2789 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2793 guint32 *divisor_is_m1;
2794 ppc_cmpi (code, 0, 0, ins->sreg2, -1);
2795 divisor_is_m1 = code;
2796 ppc_bc (code, PPC_BR_FALSE | PPC_BR_LIKELY, PPC_BR_EQ, 0);
2797 ppc_lis (code, ppc_r11, 0x8000);
2798 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2799 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "ArithmeticException");
2800 ppc_patch (divisor_is_m1, code);
2801 ppc_divwod (code, ppc_r11, ins->sreg1, ins->sreg2);
2802 ppc_mfspr (code, ppc_r0, ppc_xer);
2803 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2804 /* FIXME: use OverflowException for 0x80000000/-1 */
2805 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2806 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2807 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2811 ppc_divwuod (code, ppc_r11, ins->sreg1, ins->sreg2);
2812 ppc_mfspr (code, ppc_r0, ppc_xer);
2813 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2814 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2815 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2816 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2819 g_assert_not_reached ();
2821 ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2824 if (!(ins->inst_imm & 0xffff0000)) {
2825 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2826 } else if (!(ins->inst_imm & 0xffff)) {
2827 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2829 ppc_load (code, ppc_r11, ins->inst_imm);
2830 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2834 ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2837 if (!(ins->inst_imm & 0xffff0000)) {
2838 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2839 } else if (!(ins->inst_imm & 0xffff)) {
2840 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2842 ppc_load (code, ppc_r11, ins->inst_imm);
2843 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2847 ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2850 ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2851 //ppc_load (code, ppc_r11, ins->inst_imm);
2852 //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2855 ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2858 // there is also ppc_srawi
2859 //ppc_load (code, ppc_r11, ins->inst_imm);
2860 //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2861 ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2864 /*ppc_load (code, ppc_r11, ins->inst_imm);
2865 ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);*/
2866 ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0x1f)), (ins->inst_imm & 0x1f), 31);
2869 ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2872 ppc_not (code, ins->dreg, ins->sreg1);
2875 ppc_neg (code, ins->dreg, ins->sreg1);
2878 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2881 if (ppc_is_imm16 (ins->inst_imm)) {
2882 ppc_mulli (code, ins->dreg, ins->sreg1, ins->inst_imm);
2884 ppc_load (code, ppc_r11, ins->inst_imm);
2885 ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2889 /* we annot use mcrxr, since it's not implemented on some processors
2890 * XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2892 ppc_mullwo (code, ins->dreg, ins->sreg1, ins->sreg2);
2893 ppc_mfspr (code, ppc_r0, ppc_xer);
2894 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2895 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2897 case CEE_MUL_OVF_UN:
2898 /* we first multiply to get the high word and compare to 0
2899 * to set the flags, then the result is discarded and then
2900 * we multiply to get the lower * bits result
2902 ppc_mulhwu (code, ppc_r0, ins->sreg1, ins->sreg2);
2903 ppc_cmpi (code, 0, 0, ppc_r0, 0);
2904 EMIT_COND_SYSTEM_EXCEPTION (CEE_BNE_UN - CEE_BEQ, "OverflowException");
2905 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2909 ppc_load (code, ins->dreg, ins->inst_c0);
2912 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2913 ppc_lis (code, ins->dreg, 0);
2914 ppc_ori (code, ins->dreg, ins->dreg, 0);
2920 ppc_mr (code, ins->dreg, ins->sreg1);
2923 int saved = ins->sreg1;
2924 if (ins->sreg1 == ppc_r3) {
2925 ppc_mr (code, ppc_r0, ins->sreg1);
2928 if (ins->sreg2 != ppc_r3)
2929 ppc_mr (code, ppc_r3, ins->sreg2);
2930 if (saved != ppc_r4)
2931 ppc_mr (code, ppc_r4, saved);
2936 ppc_fmr (code, ins->dreg, ins->sreg1);
2938 case OP_FCONV_TO_R4:
2939 ppc_frsp (code, ins->dreg, ins->sreg1);
2945 * Keep in sync with mono_arch_emit_epilog
2947 g_assert (!cfg->method->save_lmf);
2948 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2949 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
2950 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
2952 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
2953 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
2955 ppc_mtlr (code, ppc_r0);
2957 if (ppc_is_imm16 (cfg->stack_usage)) {
2958 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2960 ppc_load (code, ppc_r11, cfg->stack_usage);
2961 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
2963 if (!cfg->method->save_lmf) {
2964 /*for (i = 31; i >= 14; --i) {
2965 if (cfg->used_float_regs & (1 << i)) {
2966 pos += sizeof (double);
2967 ppc_lfd (code, i, -pos, cfg->frame_reg);
2970 for (i = 31; i >= 13; --i) {
2971 if (cfg->used_int_regs & (1 << i)) {
2972 pos += sizeof (gulong);
2973 ppc_lwz (code, i, -pos, cfg->frame_reg);
2977 /* FIXME restore from MonoLMF: though this can't happen yet */
2979 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2984 /* ensure ins->sreg1 is not NULL */
2985 ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2988 if (ppc_is_imm16 (cfg->sig_cookie + cfg->stack_usage)) {
2989 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->sig_cookie + cfg->stack_usage);
2991 ppc_load (code, ppc_r11, cfg->sig_cookie + cfg->stack_usage);
2992 ppc_add (code, ppc_r11, cfg->frame_reg, ppc_r11);
2994 ppc_stw (code, ppc_r11, 0, ins->sreg1);
3002 call = (MonoCallInst*)ins;
3003 if (ins->flags & MONO_INST_HAS_METHOD)
3004 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
3006 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
3007 if (cfg->method->dynamic) {
3008 ppc_lis (code, ppc_r0, 0);
3009 ppc_ori (code, ppc_r0, ppc_r0, 0);
3010 ppc_mtlr (code, ppc_r0);
3019 case OP_VOIDCALL_REG:
3021 ppc_mtlr (code, ins->sreg1);
3024 case OP_FCALL_MEMBASE:
3025 case OP_LCALL_MEMBASE:
3026 case OP_VCALL_MEMBASE:
3027 case OP_VOIDCALL_MEMBASE:
3028 case OP_CALL_MEMBASE:
3029 ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
3030 ppc_mtlr (code, ppc_r0);
3034 g_assert_not_reached ();
3037 guint32 * zero_loop_jump, zero_loop_start;
3038 /* keep alignment */
3039 int alloca_waste = PPC_STACK_PARAM_OFFSET + cfg->param_area + 31;
3040 int area_offset = alloca_waste;
3042 ppc_addi (code, ppc_r11, ins->sreg1, alloca_waste + 31);
3043 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 0, 27);
3044 /* use ctr to store the number of words to 0 if needed */
3045 if (ins->flags & MONO_INST_INIT) {
3046 /* we zero 4 bytes at a time */
3047 ppc_addi (code, ppc_r0, ins->sreg1, 3);
3048 ppc_srawi (code, ppc_r0, ppc_r0, 2);
3049 ppc_mtctr (code, ppc_r0);
3051 ppc_lwz (code, ppc_r0, 0, ppc_sp);
3052 ppc_neg (code, ppc_r11, ppc_r11);
3053 ppc_stwux (code, ppc_r0, ppc_sp, ppc_r11);
3055 if (ins->flags & MONO_INST_INIT) {
3056 /* adjust the dest reg by -4 so we can use stwu */
3057 ppc_addi (code, ins->dreg, ppc_sp, (area_offset - 4));
3058 ppc_li (code, ppc_r11, 0);
3059 zero_loop_start = code;
3060 ppc_stwu (code, ppc_r11, 4, ins->dreg);
3061 zero_loop_jump = code;
3062 ppc_bc (code, PPC_BR_DEC_CTR_NONZERO, 0, 0);
3063 ppc_patch (zero_loop_jump, zero_loop_start);
3065 ppc_addi (code, ins->dreg, ppc_sp, area_offset);
3073 ppc_mr (code, ppc_r3, ins->sreg1);
3074 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3075 (gpointer)"mono_arch_throw_exception");
3076 if (cfg->method->dynamic) {
3077 ppc_lis (code, ppc_r0, 0);
3078 ppc_ori (code, ppc_r0, ppc_r0, 0);
3079 ppc_mtlr (code, ppc_r0);
3088 ppc_mr (code, ppc_r3, ins->sreg1);
3089 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3090 (gpointer)"mono_arch_rethrow_exception");
3091 if (cfg->method->dynamic) {
3092 ppc_lis (code, ppc_r0, 0);
3093 ppc_ori (code, ppc_r0, ppc_r0, 0);
3094 ppc_mtlr (code, ppc_r0);
3101 case OP_START_HANDLER:
3102 ppc_mflr (code, ppc_r0);
3103 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
3104 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
3106 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
3107 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_left->inst_basereg);
3111 if (ins->sreg1 != ppc_r3)
3112 ppc_mr (code, ppc_r3, ins->sreg1);
3113 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
3114 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
3116 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
3117 ppc_lwzx (code, ppc_r0, ins->inst_left->inst_basereg, ppc_r11);
3119 ppc_mtlr (code, ppc_r0);
3122 case CEE_ENDFINALLY:
3123 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
3124 ppc_mtlr (code, ppc_r0);
3127 case OP_CALL_HANDLER:
3128 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3132 ins->inst_c0 = code - cfg->native_code;
3135 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
3136 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
3138 if (ins->flags & MONO_INST_BRLABEL) {
3139 /*if (ins->inst_i0->inst_c0) {
3141 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
3143 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
3147 /*if (ins->inst_target_bb->native_offset) {
3149 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
3151 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3157 ppc_mtctr (code, ins->sreg1);
3158 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
3161 ppc_li (code, ins->dreg, 0);
3162 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
3163 ppc_li (code, ins->dreg, 1);
3167 ppc_li (code, ins->dreg, 1);
3168 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3169 ppc_li (code, ins->dreg, 0);
3173 ppc_li (code, ins->dreg, 1);
3174 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3175 ppc_li (code, ins->dreg, 0);
3177 case OP_COND_EXC_EQ:
3178 case OP_COND_EXC_NE_UN:
3179 case OP_COND_EXC_LT:
3180 case OP_COND_EXC_LT_UN:
3181 case OP_COND_EXC_GT:
3182 case OP_COND_EXC_GT_UN:
3183 case OP_COND_EXC_GE:
3184 case OP_COND_EXC_GE_UN:
3185 case OP_COND_EXC_LE:
3186 case OP_COND_EXC_LE_UN:
3187 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
3190 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
3192 /*ppc_mfspr (code, ppc_r0, ppc_xer);
3193 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
3194 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3196 case OP_COND_EXC_OV:
3197 /*ppc_mcrxr (code, 0);
3198 EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
3200 case OP_COND_EXC_NC:
3201 case OP_COND_EXC_NO:
3202 g_assert_not_reached ();
3214 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
3217 /* floating point opcodes */
3219 ppc_load (code, ppc_r11, ins->inst_p0);
3220 ppc_lfd (code, ins->dreg, 0, ppc_r11);
3223 ppc_load (code, ppc_r11, ins->inst_p0);
3224 ppc_lfs (code, ins->dreg, 0, ppc_r11);
3226 case OP_STORER8_MEMBASE_REG:
3227 if (ppc_is_imm16 (ins->inst_offset)) {
3228 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
3230 ppc_load (code, ppc_r11, ins->inst_offset);
3231 ppc_stfdx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
3234 case OP_LOADR8_MEMBASE:
3235 if (ppc_is_imm16 (ins->inst_offset)) {
3236 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
3238 ppc_load (code, ppc_r11, ins->inst_offset);
3239 ppc_lfdx (code, ins->dreg, ppc_r11, ins->inst_basereg);
3242 case OP_STORER4_MEMBASE_REG:
3243 ppc_frsp (code, ins->sreg1, ins->sreg1);
3244 if (ppc_is_imm16 (ins->inst_offset)) {
3245 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
3247 ppc_load (code, ppc_r11, ins->inst_offset);
3248 ppc_stfsx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
3251 case OP_LOADR4_MEMBASE:
3252 if (ppc_is_imm16 (ins->inst_offset)) {
3253 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
3255 ppc_load (code, ppc_r11, ins->inst_offset);
3256 ppc_lfsx (code, ins->dreg, ppc_r11, ins->inst_basereg);
3259 case CEE_CONV_R_UN: {
3260 static const guint64 adjust_val = 0x4330000000000000ULL;
3261 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3262 ppc_stw (code, ppc_r0, -8, ppc_sp);
3263 ppc_stw (code, ins->sreg1, -4, ppc_sp);
3264 ppc_load (code, ppc_r11, &adjust_val);
3265 ppc_lfd (code, ins->dreg, -8, ppc_sp);
3266 ppc_lfd (code, ppc_f0, 0, ppc_r11);
3267 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3270 case CEE_CONV_R4: /* FIXME: change precision */
3272 static const guint64 adjust_val = 0x4330000080000000ULL;
3273 // addis is special for ppc_r0
3274 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3275 ppc_stw (code, ppc_r0, -8, ppc_sp);
3276 ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
3277 ppc_stw (code, ppc_r11, -4, ppc_sp);
3278 ppc_lfd (code, ins->dreg, -8, ppc_sp);
3279 ppc_load (code, ppc_r11, &adjust_val);
3280 ppc_lfd (code, ppc_f0, 0, ppc_r11);
3281 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3284 case OP_X86_FP_LOAD_I8:
3285 g_assert_not_reached ();
3286 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
3288 case OP_X86_FP_LOAD_I4:
3289 g_assert_not_reached ();
3290 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
3292 case OP_FCONV_TO_I1:
3293 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
3295 case OP_FCONV_TO_U1:
3296 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
3298 case OP_FCONV_TO_I2:
3299 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
3301 case OP_FCONV_TO_U2:
3302 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
3304 case OP_FCONV_TO_I4:
3306 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
3308 case OP_FCONV_TO_U4:
3310 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
3312 case OP_FCONV_TO_I8:
3313 case OP_FCONV_TO_U8:
3314 g_assert_not_reached ();
3315 /* Implemented as helper calls */
3317 case OP_LCONV_TO_R_UN:
3318 g_assert_not_reached ();
3319 /* Implemented as helper calls */
3321 case OP_LCONV_TO_OVF_I: {
3322 guint32 *negative_branch, *msword_positive_branch, *msword_negative_branch, *ovf_ex_target;
3323 // Check if its negative
3324 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
3325 negative_branch = code;
3326 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 0);
3327 // Its positive msword == 0
3328 ppc_cmpi (code, 0, 0, ins->sreg2, 0);
3329 msword_positive_branch = code;
3330 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
3332 ovf_ex_target = code;
3333 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_ALWAYS, 0, "OverflowException");
3335 ppc_patch (negative_branch, code);
3336 ppc_cmpi (code, 0, 0, ins->sreg2, -1);
3337 msword_negative_branch = code;
3338 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 0);
3339 ppc_patch (msword_negative_branch, ovf_ex_target);
3341 ppc_patch (msword_positive_branch, code);
3342 if (ins->dreg != ins->sreg1)
3343 ppc_mr (code, ins->dreg, ins->sreg1);
3347 ppc_fsqrtd (code, ins->dreg, ins->sreg1);
3350 ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
3353 ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
3356 ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
3359 ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
3362 ppc_fneg (code, ins->dreg, ins->sreg1);
3366 g_assert_not_reached ();
3369 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3372 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3373 ppc_li (code, ins->dreg, 0);
3374 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
3375 ppc_li (code, ins->dreg, 1);
3378 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3379 ppc_li (code, ins->dreg, 1);
3380 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3381 ppc_li (code, ins->dreg, 0);
3384 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3385 ppc_li (code, ins->dreg, 1);
3386 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3387 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3388 ppc_li (code, ins->dreg, 0);
3391 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3392 ppc_li (code, ins->dreg, 1);
3393 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3394 ppc_li (code, ins->dreg, 0);
3397 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3398 ppc_li (code, ins->dreg, 1);
3399 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3400 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3401 ppc_li (code, ins->dreg, 0);
3404 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
3407 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
3410 EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
3413 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3414 EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
3417 EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
3420 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3421 EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
3424 EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
3427 EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
3430 EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
3433 EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
3435 case CEE_CKFINITE: {
3436 ppc_stfd (code, ins->sreg1, -8, ppc_sp);
3437 ppc_lwz (code, ppc_r11, -8, ppc_sp);
3438 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
3439 ppc_addis (code, ppc_r11, ppc_r11, -32752);
3440 ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
3441 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");
3445 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3446 g_assert_not_reached ();
3449 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
3450 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3451 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3452 g_assert_not_reached ();
3458 last_offset = offset;
3463 cfg->code_len = code - cfg->native_code;
3467 mono_arch_register_lowlevel_calls (void)
3471 #define patch_lis_ori(ip,val) do {\
3472 guint16 *__lis_ori = (guint16*)(ip); \
3473 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
3474 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
3478 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3480 MonoJumpInfo *patch_info;
3482 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3483 unsigned char *ip = patch_info->ip.i + code;
3484 const unsigned char *target;
3486 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3488 switch (patch_info->type) {
3489 case MONO_PATCH_INFO_IP:
3490 patch_lis_ori (ip, ip);
3492 case MONO_PATCH_INFO_METHOD_REL:
3493 g_assert_not_reached ();
3494 *((gpointer *)(ip)) = code + patch_info->data.offset;
3496 case MONO_PATCH_INFO_SWITCH: {
3497 gpointer *table = (gpointer *)patch_info->data.table->table;
3500 // FIXME: inspect code to get the register
3501 ppc_load (ip, ppc_r11, table);
3502 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3504 for (i = 0; i < patch_info->data.table->table_size; i++) {
3505 table [i] = (int)patch_info->data.table->table [i] + code;
3507 /* we put into the table the absolute address, no need for ppc_patch in this case */
3510 case MONO_PATCH_INFO_METHODCONST:
3511 case MONO_PATCH_INFO_CLASS:
3512 case MONO_PATCH_INFO_IMAGE:
3513 case MONO_PATCH_INFO_FIELD:
3514 case MONO_PATCH_INFO_VTABLE:
3515 case MONO_PATCH_INFO_IID:
3516 case MONO_PATCH_INFO_SFLDA:
3517 case MONO_PATCH_INFO_LDSTR:
3518 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3519 case MONO_PATCH_INFO_LDTOKEN:
3520 /* from OP_AOTCONST : lis + ori */
3521 patch_lis_ori (ip, target);
3523 case MONO_PATCH_INFO_R4:
3524 case MONO_PATCH_INFO_R8:
3525 g_assert_not_reached ();
3526 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3528 case MONO_PATCH_INFO_EXC_NAME:
3529 g_assert_not_reached ();
3530 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3532 case MONO_PATCH_INFO_NONE:
3533 case MONO_PATCH_INFO_BB_OVF:
3534 case MONO_PATCH_INFO_EXC_OVF:
3535 /* everything is dealt with at epilog output time */
3540 ppc_patch (ip, target);
3545 * Stack frame layout:
3547 * ------------------- sp
3548 * MonoLMF structure or saved registers
3549 * -------------------
3551 * -------------------
3553 * -------------------
3554 * optional 8 bytes for tracing
3555 * -------------------
3556 * param area size is cfg->param_area
3557 * -------------------
3558 * linkage area size is PPC_STACK_PARAM_OFFSET
3559 * ------------------- sp
3563 mono_arch_emit_prolog (MonoCompile *cfg)
3565 MonoMethod *method = cfg->method;
3567 MonoMethodSignature *sig;
3569 int alloc_size, pos, max_offset, i;
3575 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3578 sig = mono_method_signature (method);
3579 cfg->code_size = 256 + sig->param_count * 20;
3580 code = cfg->native_code = g_malloc (cfg->code_size);
3582 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3583 ppc_mflr (code, ppc_r0);
3584 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3586 if (cfg->max_ireg >= 29)
3587 cfg->used_int_regs |= USE_EXTRA_TEMPS;
3589 alloc_size = cfg->stack_offset;
3592 if (!method->save_lmf) {
3593 /*for (i = 31; i >= 14; --i) {
3594 if (cfg->used_float_regs & (1 << i)) {
3595 pos += sizeof (gdouble);
3596 ppc_stfd (code, i, -pos, ppc_sp);
3599 for (i = 31; i >= 13; --i) {
3600 if (cfg->used_int_regs & (1 << i)) {
3601 pos += sizeof (gulong);
3602 ppc_stw (code, i, -pos, ppc_sp);
3607 pos += sizeof (MonoLMF);
3609 ofs = -pos + G_STRUCT_OFFSET(MonoLMF, iregs);
3610 ppc_stmw (code, ppc_r13, ppc_r1, ofs);
3611 for (i = 14; i < 32; i++) {
3612 ppc_stfd (code, i, (-pos + G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble))), ppc_r1);
3616 // align to PPC_STACK_ALIGNMENT bytes
3617 if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
3618 alloc_size += PPC_STACK_ALIGNMENT - 1;
3619 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
3622 cfg->stack_usage = alloc_size;
3623 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
3625 if (ppc_is_imm16 (-alloc_size)) {
3626 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3628 ppc_load (code, ppc_r11, -alloc_size);
3629 ppc_stwux (code, ppc_sp, ppc_sp, ppc_r11);
3632 if (cfg->frame_reg != ppc_sp)
3633 ppc_mr (code, cfg->frame_reg, ppc_sp);
3635 /* compute max_offset in order to use short forward jumps
3636 * we always do it on ppc because the immediate displacement
3637 * for jumps is too small
3640 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3641 MonoInst *ins = bb->code;
3642 bb->max_offset = max_offset;
3644 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3648 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3653 /* load arguments allocated to register from the stack */
3656 cinfo = calculate_sizes (sig, sig->pinvoke);
3658 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3659 ArgInfo *ainfo = &cinfo->ret;
3661 if (ppc_is_imm16 (inst->inst_offset)) {
3662 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3664 ppc_load (code, ppc_r11, inst->inst_offset);
3665 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3668 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3669 ArgInfo *ainfo = cinfo->args + i;
3670 inst = cfg->varinfo [pos];
3672 if (cfg->verbose_level > 2)
3673 g_print ("Saving argument %d (type: %d)\n", i, ainfo->regtype);
3674 if (inst->opcode == OP_REGVAR) {
3675 if (ainfo->regtype == RegTypeGeneral)
3676 ppc_mr (code, inst->dreg, ainfo->reg);
3677 else if (ainfo->regtype == RegTypeFP)
3678 ppc_fmr (code, inst->dreg, ainfo->reg);
3679 else if (ainfo->regtype == RegTypeBase) {
3680 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3681 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3683 g_assert_not_reached ();
3685 if (cfg->verbose_level > 2)
3686 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3688 /* the argument should be put on the stack: FIXME handle size != word */
3689 if (ainfo->regtype == RegTypeGeneral) {
3690 switch (ainfo->size) {
3692 if (ppc_is_imm16 (inst->inst_offset)) {
3693 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3695 ppc_load (code, ppc_r11, inst->inst_offset);
3696 ppc_stbx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3700 if (ppc_is_imm16 (inst->inst_offset)) {
3701 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3703 ppc_load (code, ppc_r11, inst->inst_offset);
3704 ppc_sthx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3708 if (ppc_is_imm16 (inst->inst_offset + 4)) {
3709 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3710 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3712 ppc_load (code, ppc_r11, inst->inst_offset);
3713 ppc_add (code, ppc_r11, ppc_r11, inst->inst_basereg);
3714 ppc_stw (code, ainfo->reg, 0, ppc_r11);
3715 ppc_stw (code, ainfo->reg + 1, 4, ppc_r11);
3719 if (ppc_is_imm16 (inst->inst_offset)) {
3720 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3722 ppc_load (code, ppc_r11, inst->inst_offset);
3723 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3727 } else if (ainfo->regtype == RegTypeBase) {
3728 /* load the previous stack pointer in r11 */
3729 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3730 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3731 switch (ainfo->size) {
3733 if (ppc_is_imm16 (inst->inst_offset)) {
3734 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3736 ppc_load (code, ppc_r11, inst->inst_offset);
3737 ppc_stbx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3741 if (ppc_is_imm16 (inst->inst_offset)) {
3742 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3744 ppc_load (code, ppc_r11, inst->inst_offset);
3745 ppc_sthx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3749 if (ppc_is_imm16 (inst->inst_offset + 4)) {
3750 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3751 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3752 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3755 g_assert_not_reached ();
3759 if (ppc_is_imm16 (inst->inst_offset)) {
3760 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3762 ppc_load (code, ppc_r11, inst->inst_offset);
3763 ppc_stwx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3767 } else if (ainfo->regtype == RegTypeFP) {
3768 g_assert (ppc_is_imm16 (inst->inst_offset));
3769 if (ainfo->size == 8)
3770 ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3771 else if (ainfo->size == 4)
3772 ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3774 g_assert_not_reached ();
3775 } else if (ainfo->regtype == RegTypeStructByVal) {
3776 int doffset = inst->inst_offset;
3780 g_assert (ppc_is_imm16 (inst->inst_offset));
3781 g_assert (ppc_is_imm16 (inst->inst_offset + ainfo->size * sizeof (gpointer)));
3782 if (mono_class_from_mono_type (inst->inst_vtype))
3783 size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
3784 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3786 Darwin handles 1 and 2 byte structs specially by loading h/b into the arg
3787 register. Should this case include linux/ppc?
3791 ppc_sth (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3793 ppc_stb (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3796 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3797 soffset += sizeof (gpointer);
3798 doffset += sizeof (gpointer);
3800 if (ainfo->vtsize) {
3801 /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3802 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3803 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3804 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3806 } else if (ainfo->regtype == RegTypeStructByAddr) {
3807 g_assert (ppc_is_imm16 (inst->inst_offset));
3808 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3809 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3811 g_assert_not_reached ();
3816 if (method->save_lmf) {
3818 if (lmf_pthread_key != -1) {
3819 emit_tls_access (code, ppc_r3, lmf_pthread_key);
3820 if (G_STRUCT_OFFSET (MonoJitTlsData, lmf))
3821 ppc_addi (code, ppc_r3, ppc_r3, G_STRUCT_OFFSET (MonoJitTlsData, lmf));
3823 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3824 (gpointer)"mono_get_lmf_addr");
3825 if (cfg->method->dynamic) {
3826 ppc_lis (code, ppc_r0, 0);
3827 ppc_ori (code, ppc_r0, ppc_r0, 0);
3828 ppc_mtlr (code, ppc_r0);
3834 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3835 /* lmf_offset is the offset from the previous stack pointer,
3836 * alloc_size is the total stack space allocated, so the offset
3837 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
3838 * The pointer to the struct is put in ppc_r11 (new_lmf).
3839 * The callee-saved registers are already in the MonoLMF structure
3841 ppc_addi (code, ppc_r11, ppc_sp, alloc_size - lmf_offset);
3842 /* ppc_r3 is the result from mono_get_lmf_addr () */
3843 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3844 /* new_lmf->previous_lmf = *lmf_addr */
3845 ppc_lwz (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3846 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3847 /* *(lmf_addr) = r11 */
3848 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3849 /* save method info */
3850 ppc_load (code, ppc_r0, method);
3851 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3852 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3853 /* save the current IP */
3854 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3855 ppc_load (code, ppc_r0, 0x01010101);
3856 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3860 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3862 cfg->code_len = code - cfg->native_code;
3863 g_assert (cfg->code_len < cfg->code_size);
3870 mono_arch_emit_epilog (MonoCompile *cfg)
3872 MonoJumpInfo *patch_info;
3873 MonoMethod *method = cfg->method;
3875 int max_epilog_size = 16 + 20*4;
3878 if (cfg->method->save_lmf)
3879 max_epilog_size += 128;
3881 if (mono_jit_trace_calls != NULL)
3882 max_epilog_size += 50;
3884 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3885 max_epilog_size += 50;
3887 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
3888 cfg->code_size *= 2;
3889 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3890 mono_jit_stats.code_reallocs++;
3894 * Keep in sync with CEE_JMP
3896 code = cfg->native_code + cfg->code_len;
3898 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3899 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3903 if (method->save_lmf) {
3905 pos += sizeof (MonoLMF);
3907 /* save the frame reg in r8 */
3908 ppc_mr (code, ppc_r8, cfg->frame_reg);
3909 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage - lmf_offset);
3910 /* r5 = previous_lmf */
3911 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3913 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3914 /* *(lmf_addr) = previous_lmf */
3915 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3916 /* FIXME: speedup: there is no actual need to restore the registers if
3917 * we didn't actually change them (idea from Zoltan).
3920 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3922 /*for (i = 14; i < 32; i++) {
3923 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3925 g_assert (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET));
3926 /* use the saved copy of the frame reg in r8 */
3927 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3928 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, ppc_r8);
3929 ppc_mtlr (code, ppc_r0);
3931 ppc_addic (code, ppc_sp, ppc_r8, cfg->stack_usage);
3933 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3934 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
3935 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3937 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
3938 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
3940 ppc_mtlr (code, ppc_r0);
3942 if (ppc_is_imm16 (cfg->stack_usage)) {
3943 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3945 ppc_load (code, ppc_r11, cfg->stack_usage);
3946 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
3949 /*for (i = 31; i >= 14; --i) {
3950 if (cfg->used_float_regs & (1 << i)) {
3951 pos += sizeof (double);
3952 ppc_lfd (code, i, -pos, ppc_sp);
3955 for (i = 31; i >= 13; --i) {
3956 if (cfg->used_int_regs & (1 << i)) {
3957 pos += sizeof (gulong);
3958 ppc_lwz (code, i, -pos, ppc_sp);
3964 cfg->code_len = code - cfg->native_code;
3966 g_assert (cfg->code_len < cfg->code_size);
3970 /* remove once throw_exception_by_name is eliminated */
3972 exception_id_by_name (const char *name)
3974 if (strcmp (name, "IndexOutOfRangeException") == 0)
3975 return MONO_EXC_INDEX_OUT_OF_RANGE;
3976 if (strcmp (name, "OverflowException") == 0)
3977 return MONO_EXC_OVERFLOW;
3978 if (strcmp (name, "ArithmeticException") == 0)
3979 return MONO_EXC_ARITHMETIC;
3980 if (strcmp (name, "DivideByZeroException") == 0)
3981 return MONO_EXC_DIVIDE_BY_ZERO;
3982 if (strcmp (name, "InvalidCastException") == 0)
3983 return MONO_EXC_INVALID_CAST;
3984 if (strcmp (name, "NullReferenceException") == 0)
3985 return MONO_EXC_NULL_REF;
3986 if (strcmp (name, "ArrayTypeMismatchException") == 0)
3987 return MONO_EXC_ARRAY_TYPE_MISMATCH;
3988 g_error ("Unknown intrinsic exception %s\n", name);
3992 mono_arch_emit_exceptions (MonoCompile *cfg)
3994 MonoJumpInfo *patch_info;
3997 const guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM] = {NULL};
3998 guint8 exc_throw_found [MONO_EXC_INTRINS_NUM] = {0};
4001 int max_epilog_size = 50;
4003 /* count the number of exception infos */
4006 * make sure we have enough space for exceptions
4007 * 24 is the simulated call to throw_exception_by_name
4009 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4010 if (patch_info->type == MONO_PATCH_INFO_EXC) {
4011 i = exception_id_by_name (patch_info->data.target);
4012 if (!exc_throw_found [i]) {
4013 max_epilog_size += 12;
4014 exc_throw_found [i] = TRUE;
4016 } else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
4017 max_epilog_size += 12;
4018 else if (patch_info->type == MONO_PATCH_INFO_EXC_OVF)
4019 max_epilog_size += 12;
4022 while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4023 cfg->code_size *= 2;
4024 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4025 mono_jit_stats.code_reallocs++;
4028 code = cfg->native_code + cfg->code_len;
4030 /* add code to raise exceptions */
4031 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4032 switch (patch_info->type) {
4033 case MONO_PATCH_INFO_BB_OVF: {
4034 MonoOvfJump *ovfj = patch_info->data.target;
4035 unsigned char *ip = patch_info->ip.i + cfg->native_code;
4036 /* patch the initial jump */
4037 ppc_patch (ip, code);
4038 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
4040 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
4041 /* jump back to the true target */
4043 ip = ovfj->bb->native_offset + cfg->native_code;
4044 ppc_patch (code - 4, ip);
4047 case MONO_PATCH_INFO_EXC_OVF: {
4048 MonoOvfJump *ovfj = patch_info->data.target;
4049 unsigned char *ip = patch_info->ip.i + cfg->native_code;
4050 /* patch the initial jump */
4051 ppc_patch (ip, code);
4052 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
4054 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
4055 /* jump back to the true target */
4057 ip = cfg->native_code + ovfj->ip_offset + 4;
4058 ppc_patch (code - 4, ip);
4061 case MONO_PATCH_INFO_EXC: {
4062 unsigned char *ip = patch_info->ip.i + cfg->native_code;
4063 i = exception_id_by_name (patch_info->data.target);
4064 if (exc_throw_pos [i]) {
4065 ppc_patch (ip, exc_throw_pos [i]);
4066 patch_info->type = MONO_PATCH_INFO_NONE;
4069 exc_throw_pos [i] = code;
4071 ppc_patch (ip, code);
4072 /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
4073 ppc_load (code, ppc_r3, patch_info->data.target);
4074 /* we got here from a conditional call, so the calling ip is set in lr already */
4075 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4076 patch_info->data.name = "mono_arch_throw_exception_by_name";
4077 patch_info->ip.i = code - cfg->native_code;
4087 cfg->code_len = code - cfg->native_code;
4089 g_assert (cfg->code_len < cfg->code_size);
4094 try_offset_access (void *value, guint32 idx)
4096 register void* me __asm__ ("r2");
4097 void ***p = (void***)((char*)me + 284);
4098 int idx1 = idx / 32;
4099 int idx2 = idx % 32;
4102 if (value != p[idx1][idx2])
4108 setup_tls_access (void)
4111 guint32 *ins, *code;
4112 guint32 cmplwi_1023, li_0x48, blr_ins;
4113 if (tls_mode == TLS_MODE_FAILED)
4116 if (g_getenv ("MONO_NO_TLS")) {
4117 tls_mode = TLS_MODE_FAILED;
4121 if (tls_mode == TLS_MODE_DETECT) {
4122 ins = (guint32*)pthread_getspecific;
4123 /* uncond branch to the real method */
4124 if ((*ins >> 26) == 18) {
4126 val = (*ins & ~3) << 6;
4130 ins = (guint32*)val;
4132 ins = (guint32*) ((char*)ins + val);
4135 code = &cmplwi_1023;
4136 ppc_cmpli (code, 0, 0, ppc_r3, 1023);
4138 ppc_li (code, ppc_r4, 0x48);
4141 if (*ins == cmplwi_1023) {
4142 int found_lwz_284 = 0;
4143 for (ptk = 0; ptk < 20; ++ptk) {
4145 if (!*ins || *ins == blr_ins)
4147 if ((guint16)*ins == 284 && (*ins >> 26) == 32) {
4152 if (!found_lwz_284) {
4153 tls_mode = TLS_MODE_FAILED;
4156 tls_mode = TLS_MODE_LTHREADS;
4157 } else if (*ins == li_0x48) {
4159 /* uncond branch to the real method */
4160 if ((*ins >> 26) == 18) {
4162 val = (*ins & ~3) << 6;
4166 ins = (guint32*)val;
4168 ins = (guint32*) ((char*)ins + val);
4171 ppc_li (code, ppc_r0, 0x7FF2);
4172 if (ins [1] == val) {
4173 /* Darwin on G4, implement */
4174 tls_mode = TLS_MODE_FAILED;
4178 ppc_mfspr (code, ppc_r3, 104);
4179 if (ins [1] != val) {
4180 tls_mode = TLS_MODE_FAILED;
4183 tls_mode = TLS_MODE_DARWIN_G5;
4186 tls_mode = TLS_MODE_FAILED;
4190 tls_mode = TLS_MODE_FAILED;
4194 if (monodomain_key == -1) {
4195 ptk = mono_domain_get_tls_key ();
4197 ptk = mono_pthread_key_for_tls (ptk);
4199 monodomain_key = ptk;
4203 if (lmf_pthread_key == -1) {
4204 ptk = mono_pthread_key_for_tls (mono_jit_tls_id);
4206 /*g_print ("MonoLMF at: %d\n", ptk);*/
4207 /*if (!try_offset_access (mono_get_lmf_addr (), ptk)) {
4208 init_tls_failed = 1;
4211 lmf_pthread_key = ptk;
4214 if (monothread_key == -1) {
4215 ptk = mono_thread_get_tls_key ();
4217 ptk = mono_pthread_key_for_tls (ptk);
4219 monothread_key = ptk;
4220 /*g_print ("thread inited: %d\n", ptk);*/
4223 /*g_print ("thread not inited yet %d\n", ptk);*/
4229 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4231 setup_tls_access ();
4235 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4240 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
4242 int this_dreg = ppc_r3;
4247 /* add the this argument */
4248 if (this_reg != -1) {
4250 MONO_INST_NEW (cfg, this, OP_SETREG);
4251 this->type = this_type;
4252 this->sreg1 = this_reg;
4253 this->dreg = this_dreg;
4254 mono_bblock_add_inst (cfg->cbb, this);
4259 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
4260 vtarg->type = STACK_MP;
4261 vtarg->sreg1 = vt_reg;
4262 vtarg->dreg = ppc_r3;
4263 mono_bblock_add_inst (cfg->cbb, vtarg);
4268 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4270 MonoInst *ins = NULL;
4272 if (cmethod->klass == mono_defaults.thread_class &&
4273 strcmp (cmethod->name, "MemoryBarrier") == 0) {
4274 MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4276 /*if (cmethod->klass == mono_defaults.math_class) {
4277 if (strcmp (cmethod->name, "Sqrt") == 0) {
4278 MONO_INST_NEW (cfg, ins, OP_SQRT);
4279 ins->inst_i0 = args [0];
4286 mono_arch_print_tree (MonoInst *tree, int arity)
4291 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4295 setup_tls_access ();
4296 if (monodomain_key == -1)
4299 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
4300 ins->inst_offset = monodomain_key;
4305 mono_arch_get_thread_intrinsic (MonoCompile* cfg)
4309 setup_tls_access ();
4310 if (monothread_key == -1)
4313 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
4314 ins->inst_offset = monothread_key;