2004-11-07 Ben Maurer <bmaurer@ximian.com>
[mono-project.git] / mono / mini / mini-ppc.c
blobc6218e034362f48260ad0e3c99cb45ddca984205
1 /*
2 * mini-ppc.c: PowerPC backend for the Mono code generator
4 * Authors:
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
9 */
10 #include "mini.h"
11 #include <string.h>
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/debug-helpers.h>
16 #include "mini-ppc.h"
17 #include "inssel.h"
18 #include "cpu-g4.h"
19 #include "trace.h"
21 int mono_exc_esp_offset = 0;
23 const char*
24 mono_arch_regname (int reg) {
25 static const char * rnames[] = {
26 "ppc_r0", "ppc_sp", "ppc_r2", "ppc_r3", "ppc_r4",
27 "ppc_r5", "ppc_r6", "ppc_r7", "ppc_r8", "ppc_r9",
28 "ppc_r10", "ppc_r11", "ppc_r12", "ppc_r13", "ppc_r14",
29 "ppc_r15", "ppc_r16", "ppc_r17", "ppc_r18", "ppc_r19",
30 "ppc_r20", "ppc_r21", "ppc_r22", "ppc_r23", "ppc_r24",
31 "ppc_r25", "ppc_r26", "ppc_r27", "ppc_r28", "ppc_r29",
32 "ppc_r30", "ppc_r31"
34 if (reg >= 0 && reg < 32)
35 return rnames [reg];
36 return "unknown";
39 /* this function overwrites r0 */
40 static guint8*
41 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
43 /* unrolled, use the counter in big */
44 while (size >= 4) {
45 ppc_lwz (code, ppc_r0, soffset, sreg);
46 ppc_stw (code, ppc_r0, doffset, dreg);
47 size -= 4;
48 soffset += 4;
49 doffset += 4;
51 while (size >= 2) {
52 ppc_lhz (code, ppc_r0, soffset, sreg);
53 ppc_sth (code, ppc_r0, doffset, dreg);
54 size -= 2;
55 soffset += 2;
56 doffset += 2;
58 while (size >= 1) {
59 ppc_lbz (code, ppc_r0, soffset, sreg);
60 ppc_stb (code, ppc_r0, doffset, dreg);
61 size -= 1;
62 soffset += 1;
63 doffset += 1;
65 return code;
69 * mono_arch_get_argument_info:
70 * @csig: a method signature
71 * @param_count: the number of parameters to consider
72 * @arg_info: an array to store the result infos
74 * Gathers information on parameters such as size, alignment and
75 * padding. arg_info should be large enought to hold param_count + 1 entries.
77 * Returns the size of the activation frame.
79 int
80 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
82 int k, frame_size = 0;
83 int size, align, pad;
84 int offset = 8;
86 if (MONO_TYPE_ISSTRUCT (csig->ret)) {
87 frame_size += sizeof (gpointer);
88 offset += 4;
91 arg_info [0].offset = offset;
93 if (csig->hasthis) {
94 frame_size += sizeof (gpointer);
95 offset += 4;
98 arg_info [0].size = frame_size;
100 for (k = 0; k < param_count; k++) {
102 if (csig->pinvoke)
103 size = mono_type_native_stack_size (csig->params [k], &align);
104 else
105 size = mono_type_stack_size (csig->params [k], &align);
107 /* ignore alignment for now */
108 align = 1;
110 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
111 arg_info [k].pad = pad;
112 frame_size += size;
113 arg_info [k + 1].pad = 0;
114 arg_info [k + 1].size = size;
115 offset += pad;
116 arg_info [k + 1].offset = offset;
117 offset += size;
120 align = MONO_ARCH_FRAME_ALIGNMENT;
121 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
122 arg_info [k].pad = pad;
124 return frame_size;
128 * Initialize the cpu to execute managed code.
130 void
131 mono_arch_cpu_init (void)
136 * This function returns the optimizations supported on this cpu.
138 guint32
139 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
141 guint32 opts = 0;
143 /* no ppc-specific optimizations yet */
144 *exclude_mask = MONO_OPT_INLINE;
145 return opts;
148 static gboolean
149 is_regsize_var (MonoType *t) {
150 if (t->byref)
151 return TRUE;
152 switch (t->type) {
153 case MONO_TYPE_I4:
154 case MONO_TYPE_U4:
155 case MONO_TYPE_I:
156 case MONO_TYPE_U:
157 case MONO_TYPE_PTR:
158 return TRUE;
159 case MONO_TYPE_OBJECT:
160 case MONO_TYPE_STRING:
161 case MONO_TYPE_CLASS:
162 case MONO_TYPE_SZARRAY:
163 case MONO_TYPE_ARRAY:
164 return TRUE;
165 case MONO_TYPE_VALUETYPE:
166 if (t->data.klass->enumtype)
167 return is_regsize_var (t->data.klass->enum_basetype);
168 return FALSE;
170 return FALSE;
173 GList *
174 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
176 GList *vars = NULL;
177 int i;
179 for (i = 0; i < cfg->num_varinfo; i++) {
180 MonoInst *ins = cfg->varinfo [i];
181 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
183 /* unused vars */
184 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
185 continue;
187 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
188 continue;
190 /* we can only allocate 32 bit values */
191 if (is_regsize_var (ins->inst_vtype)) {
192 g_assert (MONO_VARINFO (cfg, i)->reg == -1);
193 g_assert (i == vmv->idx);
194 vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
198 return vars;
201 #define USE_EXTRA_TEMPS ((1<<30) | (1<<29))
202 //#define USE_EXTRA_TEMPS 0
204 GList *
205 mono_arch_get_global_int_regs (MonoCompile *cfg)
207 GList *regs = NULL;
208 int i, top = 32;
209 if (cfg->frame_reg != ppc_sp)
210 top = 31;
211 #if USE_EXTRA_TEMPS
212 top = 29;
213 #endif
214 for (i = 13; i < top; ++i)
215 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
217 return regs;
221 * mono_arch_regalloc_cost:
223 * Return the cost, in number of memory references, of the action of
224 * allocating the variable VMV into a register during global register
225 * allocation.
227 guint32
228 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
230 /* FIXME: */
231 return 2;
234 // code from ppc/tramp.c, try to keep in sync
235 #define MIN_CACHE_LINE 8
237 void
238 mono_arch_flush_icache (guint8 *code, gint size)
240 guint i;
241 guint8 *p;
243 p = code;
244 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
245 asm ("dcbst 0,%0;" : : "r"(p) : "memory");
247 asm ("sync");
248 p = code;
249 for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
250 asm ("icbi 0,%0; sync;" : : "r"(p) : "memory");
252 asm ("sync");
253 asm ("isync");
256 #define NOT_IMPLEMENTED(x) \
257 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
259 #ifdef __APPLE__
260 #define ALWAYS_ON_STACK(s) s
261 #define FP_ALSO_IN_REG(s) s
262 #else
263 #define ALWAYS_ON_STACK(s)
264 #define FP_ALSO_IN_REG(s) s
265 #define ALIGN_DOUBLES
266 #endif
268 enum {
269 RegTypeGeneral,
270 RegTypeBase,
271 RegTypeFP,
272 RegTypeStructByVal,
273 RegTypeStructByAddr
276 typedef struct {
277 gint32 offset;
278 guint16 vtsize; /* in param area */
279 guint8 reg;
280 guint8 regtype : 4; /* 0 general, 1 basereg, 2 floating point register, see RegType* */
281 guint8 size : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
282 } ArgInfo;
284 typedef struct {
285 int nargs;
286 guint32 stack_usage;
287 guint32 struct_ret;
288 ArgInfo ret;
289 ArgInfo args [1];
290 } CallInfo;
292 #define DEBUG(a)
294 static void inline
295 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
297 if (simple) {
298 if (*gr >= 3 + PPC_NUM_REG_ARGS) {
299 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
300 ainfo->reg = ppc_sp; /* in the caller */
301 ainfo->regtype = RegTypeBase;
302 *stack_size += 4;
303 } else {
304 ALWAYS_ON_STACK (*stack_size += 4);
305 ainfo->reg = *gr;
307 } else {
308 if (*gr >= 3 + PPC_NUM_REG_ARGS - 1) {
309 #ifdef ALIGN_DOUBLES
310 //*stack_size += (*stack_size % 8);
311 #endif
312 ainfo->offset = PPC_STACK_PARAM_OFFSET + *stack_size;
313 ainfo->reg = ppc_sp; /* in the caller */
314 ainfo->regtype = RegTypeBase;
315 *stack_size += 8;
316 } else {
317 #ifdef ALIGN_DOUBLES
318 if (!((*gr) & 1))
319 (*gr) ++;
320 #endif
321 ALWAYS_ON_STACK (*stack_size += 8);
322 ainfo->reg = *gr;
324 (*gr) ++;
326 (*gr) ++;
329 static CallInfo*
330 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
332 guint i, fr, gr;
333 int n = sig->hasthis + sig->param_count;
334 guint32 simpletype;
335 guint32 stack_size = 0;
336 CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
338 fr = PPC_FIRST_FPARG_REG;
339 gr = PPC_FIRST_ARG_REG;
341 /* FIXME: handle returning a struct */
342 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
343 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
344 cinfo->struct_ret = PPC_FIRST_ARG_REG;
347 n = 0;
348 if (sig->hasthis) {
349 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
350 n++;
352 DEBUG(printf("params: %d\n", sig->param_count));
353 for (i = 0; i < sig->param_count; ++i) {
354 DEBUG(printf("param %d: ", i));
355 if (sig->params [i]->byref) {
356 DEBUG(printf("byref\n"));
357 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
358 n++;
359 continue;
361 simpletype = sig->params [i]->type;
362 enum_calc_size:
363 switch (simpletype) {
364 case MONO_TYPE_BOOLEAN:
365 case MONO_TYPE_I1:
366 case MONO_TYPE_U1:
367 cinfo->args [n].size = 1;
368 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
369 n++;
370 break;
371 case MONO_TYPE_CHAR:
372 case MONO_TYPE_I2:
373 case MONO_TYPE_U2:
374 cinfo->args [n].size = 2;
375 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
376 n++;
377 break;
378 case MONO_TYPE_I4:
379 case MONO_TYPE_U4:
380 cinfo->args [n].size = 4;
381 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
382 n++;
383 break;
384 case MONO_TYPE_I:
385 case MONO_TYPE_U:
386 case MONO_TYPE_PTR:
387 case MONO_TYPE_FNPTR:
388 case MONO_TYPE_CLASS:
389 case MONO_TYPE_OBJECT:
390 case MONO_TYPE_STRING:
391 case MONO_TYPE_SZARRAY:
392 case MONO_TYPE_ARRAY:
393 cinfo->args [n].size = sizeof (gpointer);
394 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
395 n++;
396 break;
397 case MONO_TYPE_VALUETYPE: {
398 gint size;
399 if (sig->params [i]->data.klass->enumtype) {
400 simpletype = sig->params [i]->data.klass->enum_basetype->type;
401 goto enum_calc_size;
403 if (is_pinvoke)
404 size = mono_class_native_size (sig->params [i]->data.klass, NULL);
405 else
406 size = mono_class_value_size (sig->params [i]->data.klass, NULL);
407 DEBUG(printf ("load %d bytes struct\n",
408 mono_class_native_size (sig->params [i]->data.klass, NULL)));
409 #if PPC_PASS_STRUCTS_BY_VALUE
411 int align_size = size;
412 int nwords = 0;
413 align_size += (sizeof (gpointer) - 1);
414 align_size &= ~(sizeof (gpointer) - 1);
415 nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
416 cinfo->args [n].regtype = RegTypeStructByVal;
417 if (gr > PPC_LAST_ARG_REG || (size >= 3 && size % 4 != 0)) {
418 cinfo->args [n].size = 0;
419 cinfo->args [n].vtsize = nwords;
420 } else {
421 int rest = PPC_LAST_ARG_REG - gr + 1;
422 int n_in_regs = rest >= nwords? nwords: rest;
423 cinfo->args [n].size = n_in_regs;
424 cinfo->args [n].vtsize = nwords - n_in_regs;
425 cinfo->args [n].reg = gr;
426 gr += n_in_regs;
428 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
429 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
430 stack_size += nwords * sizeof (gpointer);
432 #else
433 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
434 cinfo->args [n].regtype = RegTypeStructByAddr;
435 #endif
436 n++;
437 break;
439 case MONO_TYPE_TYPEDBYREF: {
440 int size = sizeof (MonoTypedRef);
441 /* keep in sync or merge with the valuetype case */
442 #if PPC_PASS_STRUCTS_BY_VALUE
444 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
445 cinfo->args [n].regtype = RegTypeStructByVal;
446 if (gr <= PPC_LAST_ARG_REG) {
447 int rest = PPC_LAST_ARG_REG - gr + 1;
448 int n_in_regs = rest >= nwords? nwords: rest;
449 cinfo->args [n].size = n_in_regs;
450 cinfo->args [n].vtsize = nwords - n_in_regs;
451 cinfo->args [n].reg = gr;
452 gr += n_in_regs;
453 } else {
454 cinfo->args [n].size = 0;
455 cinfo->args [n].vtsize = nwords;
457 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
458 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
459 stack_size += nwords * sizeof (gpointer);
461 #else
462 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
463 cinfo->args [n].regtype = RegTypeStructByAddr;
464 #endif
465 n++;
466 break;
468 case MONO_TYPE_U8:
469 case MONO_TYPE_I8:
470 cinfo->args [n].size = 8;
471 add_general (&gr, &stack_size, cinfo->args + n, FALSE);
472 n++;
473 break;
474 case MONO_TYPE_R4:
475 cinfo->args [n].size = 4;
477 /* It was 7, now it is 8 in LinuxPPC */
478 if (fr <= PPC_LAST_FPARG_REG) {
479 cinfo->args [n].regtype = RegTypeFP;
480 cinfo->args [n].reg = fr;
481 fr ++;
482 FP_ALSO_IN_REG (gr ++);
483 ALWAYS_ON_STACK (stack_size += 4);
484 } else {
485 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
486 cinfo->args [n].regtype = RegTypeBase;
487 cinfo->args [n].reg = ppc_sp; /* in the caller*/
488 stack_size += 4;
490 n++;
491 break;
492 case MONO_TYPE_R8:
493 cinfo->args [n].size = 8;
494 /* It was 7, now it is 8 in LinuxPPC */
495 if (fr <= PPC_LAST_FPARG_REG) {
496 cinfo->args [n].regtype = RegTypeFP;
497 cinfo->args [n].reg = fr;
498 fr ++;
499 FP_ALSO_IN_REG (gr += 2);
500 ALWAYS_ON_STACK (stack_size += 8);
501 } else {
502 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
503 cinfo->args [n].regtype = RegTypeBase;
504 cinfo->args [n].reg = ppc_sp; /* in the caller*/
505 stack_size += 8;
507 n++;
508 break;
509 default:
510 g_error ("Can't trampoline 0x%x", sig->params [i]->type);
515 simpletype = sig->ret->type;
516 enum_retvalue:
517 switch (simpletype) {
518 case MONO_TYPE_BOOLEAN:
519 case MONO_TYPE_I1:
520 case MONO_TYPE_U1:
521 case MONO_TYPE_I2:
522 case MONO_TYPE_U2:
523 case MONO_TYPE_CHAR:
524 case MONO_TYPE_I4:
525 case MONO_TYPE_U4:
526 case MONO_TYPE_I:
527 case MONO_TYPE_U:
528 case MONO_TYPE_PTR:
529 case MONO_TYPE_FNPTR:
530 case MONO_TYPE_CLASS:
531 case MONO_TYPE_OBJECT:
532 case MONO_TYPE_SZARRAY:
533 case MONO_TYPE_ARRAY:
534 case MONO_TYPE_STRING:
535 cinfo->ret.reg = ppc_r3;
536 break;
537 case MONO_TYPE_U8:
538 case MONO_TYPE_I8:
539 cinfo->ret.reg = ppc_r3;
540 break;
541 case MONO_TYPE_R4:
542 case MONO_TYPE_R8:
543 cinfo->ret.reg = ppc_f1;
544 cinfo->ret.regtype = RegTypeFP;
545 break;
546 case MONO_TYPE_VALUETYPE:
547 if (sig->ret->data.klass->enumtype) {
548 simpletype = sig->ret->data.klass->enum_basetype->type;
549 goto enum_retvalue;
551 break;
552 case MONO_TYPE_TYPEDBYREF:
553 case MONO_TYPE_VOID:
554 break;
555 default:
556 g_error ("Can't handle as return value 0x%x", sig->ret->type);
560 /* align stack size to 16 */
561 DEBUG (printf (" stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
562 stack_size = (stack_size + 15) & ~15;
564 cinfo->stack_usage = stack_size;
565 return cinfo;
570 * Set var information according to the calling convention. ppc version.
571 * The locals var stuff should most likely be split in another method.
573 void
574 mono_arch_allocate_vars (MonoCompile *m)
576 MonoMethodSignature *sig;
577 MonoMethodHeader *header;
578 MonoInst *inst;
579 int i, offset, size, align, curinst;
580 int frame_reg = ppc_sp;
582 /* allow room for the vararg method args: void* and long/double */
583 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
584 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
585 /* this is bug #60332: remove when #59509 is fixed, so no weird vararg
586 * call convs needs to be handled this way.
588 if (m->flags & MONO_CFG_HAS_VARARGS)
589 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
591 header = mono_method_get_header (m->method);
594 * We use the frame register also for any method that has
595 * exception clauses. This way, when the handlers are called,
596 * the code will reference local variables using the frame reg instead of
597 * the stack pointer: if we had to restore the stack pointer, we'd
598 * corrupt the method frames that are already on the stack (since
599 * filters get called before stack unwinding happens) when the filter
600 * code would call any method (this also applies to finally etc.).
602 if ((m->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
603 frame_reg = ppc_r31;
604 m->frame_reg = frame_reg;
605 if (frame_reg != ppc_sp) {
606 m->used_int_regs |= 1 << frame_reg;
609 sig = m->method->signature;
611 offset = 0;
612 curinst = 0;
613 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
614 m->ret->opcode = OP_REGVAR;
615 m->ret->inst_c0 = ppc_r3;
616 } else {
617 /* FIXME: handle long and FP values */
618 switch (sig->ret->type) {
619 case MONO_TYPE_VOID:
620 break;
621 default:
622 m->ret->opcode = OP_REGVAR;
623 m->ret->inst_c0 = ppc_r3;
624 break;
627 /* local vars are at a positive offset from the stack pointer */
629 * also note that if the function uses alloca, we use ppc_r31
630 * to point at the local variables.
632 offset = PPC_MINIMAL_STACK_SIZE; /* linkage area */
633 /* align the offset to 16 bytes: not sure this is needed here */
634 //offset += 16 - 1;
635 //offset &= ~(16 - 1);
637 /* add parameter area size for called functions */
638 offset += m->param_area;
639 offset += 16 - 1;
640 offset &= ~(16 - 1);
642 /* allow room to save the return value */
643 if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
644 offset += 8;
646 /* the MonoLMF structure is stored just below the stack pointer */
648 #if 0
649 /* this stuff should not be needed on ppc and the new jit,
650 * because a call on ppc to the handlers doesn't change the
651 * stack pointer and the jist doesn't manipulate the stack pointer
652 * for operations involving valuetypes.
654 /* reserve space to store the esp */
655 offset += sizeof (gpointer);
657 /* this is a global constant */
658 mono_exc_esp_offset = offset;
659 #endif
661 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
662 inst = m->ret;
663 offset += sizeof(gpointer) - 1;
664 offset &= ~(sizeof(gpointer) - 1);
665 inst->inst_offset = offset;
666 inst->opcode = OP_REGOFFSET;
667 inst->inst_basereg = frame_reg;
668 offset += sizeof(gpointer);
670 curinst = m->locals_start;
671 for (i = curinst; i < m->num_varinfo; ++i) {
672 inst = m->varinfo [i];
673 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
674 continue;
676 /* inst->unused indicates native sized value types, this is used by the
677 * pinvoke wrappers when they call functions returning structure */
678 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
679 size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
680 else
681 size = mono_type_size (inst->inst_vtype, &align);
683 offset += align - 1;
684 offset &= ~(align - 1);
685 inst->inst_offset = offset;
686 inst->opcode = OP_REGOFFSET;
687 inst->inst_basereg = frame_reg;
688 offset += size;
689 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
692 curinst = 0;
693 if (sig->hasthis) {
694 inst = m->varinfo [curinst];
695 if (inst->opcode != OP_REGVAR) {
696 inst->opcode = OP_REGOFFSET;
697 inst->inst_basereg = frame_reg;
698 offset += sizeof (gpointer) - 1;
699 offset &= ~(sizeof (gpointer) - 1);
700 inst->inst_offset = offset;
701 offset += sizeof (gpointer);
703 curinst++;
706 for (i = 0; i < sig->param_count; ++i) {
707 inst = m->varinfo [curinst];
708 if (inst->opcode != OP_REGVAR) {
709 inst->opcode = OP_REGOFFSET;
710 inst->inst_basereg = frame_reg;
711 size = mono_type_size (sig->params [i], &align);
712 offset += align - 1;
713 offset &= ~(align - 1);
714 inst->inst_offset = offset;
715 offset += size;
717 curinst++;
720 /* align the offset to 16 bytes */
721 offset += 16 - 1;
722 offset &= ~(16 - 1);
724 /* change sign? */
725 m->stack_offset = offset;
729 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
730 * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info
734 * take the arguments and generate the arch-specific
735 * instructions to properly call the function in call.
736 * This includes pushing, moving arguments to the right register
737 * etc.
738 * Issue: who does the spilling if needed, and when?
740 MonoCallInst*
741 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
742 MonoInst *arg, *in;
743 MonoMethodSignature *sig;
744 int i, n;
745 CallInfo *cinfo;
746 ArgInfo *ainfo;
748 sig = call->signature;
749 n = sig->param_count + sig->hasthis;
751 cinfo = calculate_sizes (sig, sig->pinvoke);
752 if (cinfo->struct_ret)
753 call->used_iregs |= 1 << cinfo->struct_ret;
755 for (i = 0; i < n; ++i) {
756 ainfo = cinfo->args + i;
757 if (is_virtual && i == 0) {
758 /* the argument will be attached to the call instrucion */
759 in = call->args [i];
760 call->used_iregs |= 1 << ainfo->reg;
761 } else {
762 MONO_INST_NEW (cfg, arg, OP_OUTARG);
763 in = call->args [i];
764 arg->cil_code = in->cil_code;
765 arg->inst_left = in;
766 arg->type = in->type;
767 /* prepend, we'll need to reverse them later */
768 arg->next = call->out_args;
769 call->out_args = arg;
770 if (ainfo->regtype == RegTypeGeneral) {
771 arg->unused = ainfo->reg;
772 call->used_iregs |= 1 << ainfo->reg;
773 if (arg->type == STACK_I8)
774 call->used_iregs |= 1 << (ainfo->reg + 1);
775 } else if (ainfo->regtype == RegTypeStructByAddr) {
776 /* FIXME: where si the data allocated? */
777 arg->unused = ainfo->reg;
778 call->used_iregs |= 1 << ainfo->reg;
779 } else if (ainfo->regtype == RegTypeStructByVal) {
780 int cur_reg;
781 /* mark the used regs */
782 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
783 call->used_iregs |= 1 << (ainfo->reg + cur_reg);
785 arg->opcode = OP_OUTARG_VT;
786 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
787 arg->inst_imm = ainfo->offset;
788 } else if (ainfo->regtype == RegTypeBase) {
789 arg->opcode = OP_OUTARG;
790 arg->unused = ainfo->reg | (ainfo->size << 8);
791 arg->inst_imm = ainfo->offset;
792 } else if (ainfo->regtype == RegTypeFP) {
793 arg->opcode = OP_OUTARG_R8;
794 arg->unused = ainfo->reg;
795 call->used_fregs |= 1 << ainfo->reg;
796 if (ainfo->size == 4) {
797 arg->opcode = OP_OUTARG_R8;
798 /* we reduce the precision */
799 /*MonoInst *conv;
800 MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
801 conv->inst_left = arg->inst_left;
802 arg->inst_left = conv;*/
804 } else {
805 g_assert_not_reached ();
810 * Reverse the call->out_args list.
813 MonoInst *prev = NULL, *list = call->out_args, *next;
814 while (list) {
815 next = list->next;
816 list->next = prev;
817 prev = list;
818 list = next;
820 call->out_args = prev;
822 call->stack_usage = cinfo->stack_usage;
823 cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
824 cfg->flags |= MONO_CFG_HAS_CALLS;
826 * should set more info in call, such as the stack space
827 * used by the args that needs to be added back to esp
830 g_free (cinfo);
831 return call;
835 * Allow tracing to work with this interface (with an optional argument)
839 * This may be needed on some archs or for debugging support.
841 void
842 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
844 /* no stack room needed now (may be needed for FASTCALL-trace support) */
845 *stack = 0;
846 /* split prolog-epilog requirements? */
847 *code = 50; /* max bytes needed: check this number */
850 void*
851 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
853 guchar *code = p;
855 ppc_load (code, ppc_r3, cfg->method);
856 ppc_li (code, ppc_r4, 0); /* NULL ebp for now */
857 ppc_load (code, ppc_r0, func);
858 ppc_mtlr (code, ppc_r0);
859 ppc_blrl (code);
860 return code;
863 enum {
864 SAVE_NONE,
865 SAVE_STRUCT,
866 SAVE_ONE,
867 SAVE_TWO,
868 SAVE_FP
871 void*
872 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
874 guchar *code = p;
875 int save_mode = SAVE_NONE;
876 MonoMethod *method = cfg->method;
877 int rtype = method->signature->ret->type;
878 int save_offset = PPC_STACK_PARAM_OFFSET + cfg->param_area;
879 save_offset += 15;
880 save_offset &= ~15;
882 handle_enum:
883 switch (rtype) {
884 case MONO_TYPE_VOID:
885 /* special case string .ctor icall */
886 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
887 save_mode = SAVE_ONE;
888 else
889 save_mode = SAVE_NONE;
890 break;
891 case MONO_TYPE_I8:
892 case MONO_TYPE_U8:
893 save_mode = SAVE_TWO;
894 break;
895 case MONO_TYPE_R4:
896 case MONO_TYPE_R8:
897 save_mode = SAVE_FP;
898 break;
899 case MONO_TYPE_VALUETYPE:
900 if (method->signature->ret->data.klass->enumtype) {
901 rtype = method->signature->ret->data.klass->enum_basetype->type;
902 goto handle_enum;
904 save_mode = SAVE_STRUCT;
905 break;
906 default:
907 save_mode = SAVE_ONE;
908 break;
911 switch (save_mode) {
912 case SAVE_TWO:
913 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
914 ppc_stw (code, ppc_r4, save_offset + 4, cfg->frame_reg);
915 if (enable_arguments) {
916 ppc_mr (code, ppc_r5, ppc_r4);
917 ppc_mr (code, ppc_r4, ppc_r3);
919 break;
920 case SAVE_ONE:
921 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
922 if (enable_arguments) {
923 ppc_mr (code, ppc_r4, ppc_r3);
925 break;
926 case SAVE_FP:
927 ppc_stfd (code, ppc_f1, save_offset, cfg->frame_reg);
928 if (enable_arguments) {
929 /* FIXME: what reg? */
930 ppc_fmr (code, ppc_f3, ppc_f1);
931 ppc_lwz (code, ppc_r4, save_offset, cfg->frame_reg);
932 ppc_lwz (code, ppc_r5, save_offset + 4, cfg->frame_reg);
934 break;
935 case SAVE_STRUCT:
936 if (enable_arguments) {
937 /* FIXME: get the actual address */
938 ppc_mr (code, ppc_r4, ppc_r3);
940 break;
941 case SAVE_NONE:
942 default:
943 break;
946 ppc_load (code, ppc_r3, cfg->method);
947 ppc_load (code, ppc_r0, func);
948 ppc_mtlr (code, ppc_r0);
949 ppc_blrl (code);
951 switch (save_mode) {
952 case SAVE_TWO:
953 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
954 ppc_lwz (code, ppc_r4, save_offset + 4, cfg->frame_reg);
955 break;
956 case SAVE_ONE:
957 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
958 break;
959 case SAVE_FP:
960 ppc_lfd (code, ppc_f1, save_offset, cfg->frame_reg);
961 break;
962 case SAVE_NONE:
963 default:
964 break;
967 return code;
970 * Conditional branches have a small offset, so if it is likely overflowed,
971 * we do a branch to the end of the method (uncond branches have much larger
972 * offsets) where we perform the conditional and jump back unconditionally.
973 * It's slightly slower, since we add two uncond branches, but it's very simple
974 * with the current patch implementation and such large methods are likely not
975 * going to be perf critical anyway.
977 typedef struct {
978 MonoBasicBlock *bb;
979 void *ip;
980 guint16 b0_cond;
981 guint16 b1_cond;
982 } MonoOvfJump;
984 #define EMIT_COND_BRANCH_FLAGS(ins,b0,b1) \
985 if (ins->flags & MONO_INST_BRLABEL) { \
986 if (0 && ins->inst_i0->inst_c0) { \
987 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff); \
988 } else { \
989 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
990 ppc_bc (code, (b0), (b1), 0); \
992 } else { \
993 if (0 && ins->inst_true_bb->native_offset) { \
994 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
995 } else { \
996 int br_disp = ins->inst_true_bb->max_offset - offset; \
997 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
998 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
999 ovfj->bb = ins->inst_true_bb; \
1000 ovfj->ip = NULL; \
1001 ovfj->b0_cond = (b0); \
1002 ovfj->b1_cond = (b1); \
1003 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB_OVF, ovfj); \
1004 ppc_b (code, 0); \
1005 } else { \
1006 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1007 ppc_bc (code, (b0), (b1), 0); \
1012 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_b0_table [(cond)], branch_b1_table [(cond)])
1014 /* emit an exception if condition is fail
1016 * We assign the extra code used to throw the implicit exceptions
1017 * to cfg->bb_exit as far as the big branch handling is concerned
1019 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(b0,b1,exc_name) \
1020 do { \
1021 int br_disp = cfg->bb_exit->max_offset - offset; \
1022 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
1023 MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
1024 ovfj->bb = NULL; \
1025 ovfj->ip = code; \
1026 ovfj->b0_cond = (b0); \
1027 ovfj->b1_cond = (b1); \
1028 /* FIXME: test this code */ \
1029 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj); \
1030 ppc_b (code, 0); \
1031 cfg->bb_exit->max_offset += 24; \
1032 } else { \
1033 mono_add_patch_info (cfg, code - cfg->native_code, \
1034 MONO_PATCH_INFO_EXC, exc_name); \
1035 ppc_bc (code, (b0), (b1), 0); \
1037 } while (0);
1039 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_b0_table [(cond)], branch_b1_table [(cond)], (exc_name))
1041 static void
1042 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1044 MonoInst *ins, *last_ins = NULL;
1045 ins = bb->code;
1047 while (ins) {
1049 switch (ins->opcode) {
1050 case OP_MUL_IMM:
1051 /* remove unnecessary multiplication with 1 */
1052 if (ins->inst_imm == 1) {
1053 if (ins->dreg != ins->sreg1) {
1054 ins->opcode = OP_MOVE;
1055 } else {
1056 last_ins->next = ins->next;
1057 ins = ins->next;
1058 continue;
1060 } else {
1061 int power2 = mono_is_power_of_two (ins->inst_imm);
1062 if (power2 > 0) {
1063 ins->opcode = OP_SHL_IMM;
1064 ins->inst_imm = power2;
1067 break;
1068 case OP_LOAD_MEMBASE:
1069 case OP_LOADI4_MEMBASE:
1071 * OP_STORE_MEMBASE_REG reg, offset(basereg)
1072 * OP_LOAD_MEMBASE offset(basereg), reg
1074 if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG
1075 || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1076 ins->inst_basereg == last_ins->inst_destbasereg &&
1077 ins->inst_offset == last_ins->inst_offset) {
1078 if (ins->dreg == last_ins->sreg1) {
1079 last_ins->next = ins->next;
1080 ins = ins->next;
1081 continue;
1082 } else {
1083 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1084 ins->opcode = OP_MOVE;
1085 ins->sreg1 = last_ins->sreg1;
1089 * Note: reg1 must be different from the basereg in the second load
1090 * OP_LOAD_MEMBASE offset(basereg), reg1
1091 * OP_LOAD_MEMBASE offset(basereg), reg2
1092 * -->
1093 * OP_LOAD_MEMBASE offset(basereg), reg1
1094 * OP_MOVE reg1, reg2
1096 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1097 || last_ins->opcode == OP_LOAD_MEMBASE) &&
1098 ins->inst_basereg != last_ins->dreg &&
1099 ins->inst_basereg == last_ins->inst_basereg &&
1100 ins->inst_offset == last_ins->inst_offset) {
1102 if (ins->dreg == last_ins->dreg) {
1103 last_ins->next = ins->next;
1104 ins = ins->next;
1105 continue;
1106 } else {
1107 ins->opcode = OP_MOVE;
1108 ins->sreg1 = last_ins->dreg;
1111 //g_assert_not_reached ();
1113 #if 0
1115 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1116 * OP_LOAD_MEMBASE offset(basereg), reg
1117 * -->
1118 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
1119 * OP_ICONST reg, imm
1121 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1122 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1123 ins->inst_basereg == last_ins->inst_destbasereg &&
1124 ins->inst_offset == last_ins->inst_offset) {
1125 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1126 ins->opcode = OP_ICONST;
1127 ins->inst_c0 = last_ins->inst_imm;
1128 g_assert_not_reached (); // check this rule
1129 #endif
1131 break;
1132 case OP_LOADU1_MEMBASE:
1133 case OP_LOADI1_MEMBASE:
1134 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1135 ins->inst_basereg == last_ins->inst_destbasereg &&
1136 ins->inst_offset == last_ins->inst_offset) {
1137 if (ins->dreg == last_ins->sreg1) {
1138 last_ins->next = ins->next;
1139 ins = ins->next;
1140 continue;
1141 } else {
1142 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1143 ins->opcode = OP_MOVE;
1144 ins->sreg1 = last_ins->sreg1;
1147 break;
1148 case OP_LOADU2_MEMBASE:
1149 case OP_LOADI2_MEMBASE:
1150 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1151 ins->inst_basereg == last_ins->inst_destbasereg &&
1152 ins->inst_offset == last_ins->inst_offset) {
1153 if (ins->dreg == last_ins->sreg1) {
1154 last_ins->next = ins->next;
1155 ins = ins->next;
1156 continue;
1157 } else {
1158 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1159 ins->opcode = OP_MOVE;
1160 ins->sreg1 = last_ins->sreg1;
1163 break;
1164 case CEE_CONV_I4:
1165 case CEE_CONV_U4:
1166 case OP_MOVE:
1167 case OP_SETREG:
1168 ins->opcode = OP_MOVE;
1170 * OP_MOVE reg, reg
1172 if (ins->dreg == ins->sreg1) {
1173 if (last_ins)
1174 last_ins->next = ins->next;
1175 ins = ins->next;
1176 continue;
1179 * OP_MOVE sreg, dreg
1180 * OP_MOVE dreg, sreg
1182 if (last_ins && last_ins->opcode == OP_MOVE &&
1183 ins->sreg1 == last_ins->dreg &&
1184 ins->dreg == last_ins->sreg1) {
1185 last_ins->next = ins->next;
1186 ins = ins->next;
1187 continue;
1189 break;
1191 last_ins = ins;
1192 ins = ins->next;
1194 bb->last_ins = last_ins;
1198 * the branch_b0_table should maintain the order of these
1199 * opcodes.
1200 case CEE_BEQ:
1201 case CEE_BGE:
1202 case CEE_BGT:
1203 case CEE_BLE:
1204 case CEE_BLT:
1205 case CEE_BNE_UN:
1206 case CEE_BGE_UN:
1207 case CEE_BGT_UN:
1208 case CEE_BLE_UN:
1209 case CEE_BLT_UN:
1211 static const guchar
1212 branch_b0_table [] = {
1213 PPC_BR_TRUE,
1214 PPC_BR_FALSE,
1215 PPC_BR_TRUE,
1216 PPC_BR_FALSE,
1217 PPC_BR_TRUE,
1219 PPC_BR_FALSE,
1220 PPC_BR_FALSE,
1221 PPC_BR_TRUE,
1222 PPC_BR_FALSE,
1223 PPC_BR_TRUE
1226 static const guchar
1227 branch_b1_table [] = {
1228 PPC_BR_EQ,
1229 PPC_BR_LT,
1230 PPC_BR_GT,
1231 PPC_BR_GT,
1232 PPC_BR_LT,
1234 PPC_BR_EQ,
1235 PPC_BR_LT,
1236 PPC_BR_GT,
1237 PPC_BR_GT,
1238 PPC_BR_LT
1242 * returns the offset used by spillvar. It allocates a new
1243 * spill variable if necessary.
1245 static int
1246 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1248 MonoSpillInfo **si, *info;
1249 int i = 0;
1251 si = &cfg->spill_info;
1253 while (i <= spillvar) {
1255 if (!*si) {
1256 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1257 info->next = NULL;
1258 info->offset = cfg->stack_offset;
1259 cfg->stack_offset += sizeof (gpointer);
1262 if (i == spillvar)
1263 return (*si)->offset;
1265 i++;
1266 si = &(*si)->next;
1269 g_assert_not_reached ();
1270 return 0;
1273 static int
1274 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1276 MonoSpillInfo **si, *info;
1277 int i = 0;
1279 si = &cfg->spill_info_float;
1281 while (i <= spillvar) {
1283 if (!*si) {
1284 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1285 info->next = NULL;
1286 cfg->stack_offset += 7;
1287 cfg->stack_offset &= ~7;
1288 info->offset = cfg->stack_offset;
1289 cfg->stack_offset += sizeof (double);
1292 if (i == spillvar)
1293 return (*si)->offset;
1295 i++;
1296 si = &(*si)->next;
1299 g_assert_not_reached ();
1300 return 0;
1303 #undef DEBUG
1304 #define DEBUG(a) if (cfg->verbose_level > 1) a
1305 //#define DEBUG(a)
1306 /* use ppc_r3-ppc_10,ppc_r12 as temp registers, f1-f13 for FP registers */
1307 #define PPC_CALLER_REGS ((0xff<<3) | (1<<12) | USE_EXTRA_TEMPS)
1308 #define PPC_CALLER_FREGS (0x3ffe)
1310 #define reg_is_freeable(r) (PPC_CALLER_REGS & 1 << (r))
1311 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 13)
1313 typedef struct {
1314 int born_in;
1315 int killed_in;
1316 int last_use;
1317 int prev_use;
1318 } RegTrack;
1320 static const char*const * ins_spec = ppcg4;
1322 static void
1323 print_ins (int i, MonoInst *ins)
1325 const char *spec = ins_spec [ins->opcode];
1326 g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1327 if (spec [MONO_INST_DEST]) {
1328 if (ins->dreg >= MONO_MAX_IREGS)
1329 g_print (" R%d <-", ins->dreg);
1330 else
1331 g_print (" %s <-", mono_arch_regname (ins->dreg));
1333 if (spec [MONO_INST_SRC1]) {
1334 if (ins->sreg1 >= MONO_MAX_IREGS)
1335 g_print (" R%d", ins->sreg1);
1336 else
1337 g_print (" %s", mono_arch_regname (ins->sreg1));
1339 if (spec [MONO_INST_SRC2]) {
1340 if (ins->sreg2 >= MONO_MAX_IREGS)
1341 g_print (" R%d", ins->sreg2);
1342 else
1343 g_print (" %s", mono_arch_regname (ins->sreg2));
1345 if (spec [MONO_INST_CLOB])
1346 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1347 g_print ("\n");
1350 static void
1351 print_regtrack (RegTrack *t, int num)
1353 int i;
1354 char buf [32];
1355 const char *r;
1357 for (i = 0; i < num; ++i) {
1358 if (!t [i].born_in)
1359 continue;
1360 if (i >= MONO_MAX_IREGS) {
1361 g_snprintf (buf, sizeof(buf), "R%d", i);
1362 r = buf;
1363 } else
1364 r = mono_arch_regname (i);
1365 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1369 typedef struct InstList InstList;
1371 struct InstList {
1372 InstList *prev;
1373 InstList *next;
1374 MonoInst *data;
1377 static inline InstList*
1378 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1380 InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1381 item->data = data;
1382 item->prev = NULL;
1383 item->next = list;
1384 if (list)
1385 list->prev = item;
1386 return item;
1390 * Force the spilling of the variable in the symbolic register 'reg'.
1392 static int
1393 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1395 MonoInst *load;
1396 int i, sel, spill;
1398 sel = cfg->rs->iassign [reg];
1399 /*i = cfg->rs->isymbolic [sel];
1400 g_assert (i == reg);*/
1401 i = reg;
1402 spill = ++cfg->spill_count;
1403 cfg->rs->iassign [i] = -spill - 1;
1404 mono_regstate_free_int (cfg->rs, sel);
1405 /* we need to create a spill var and insert a load to sel after the current instruction */
1406 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1407 load->dreg = sel;
1408 load->inst_basereg = cfg->frame_reg;
1409 load->inst_offset = mono_spillvar_offset (cfg, spill);
1410 if (item->prev) {
1411 while (ins->next != item->prev->data)
1412 ins = ins->next;
1414 load->next = ins->next;
1415 ins->next = load;
1416 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1417 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1418 g_assert (i == sel);
1420 return sel;
1423 static int
1424 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1426 MonoInst *load;
1427 int i, sel, spill;
1429 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));
1430 /* exclude the registers in the current instruction */
1431 if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1432 if (ins->sreg1 >= MONO_MAX_IREGS)
1433 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1434 else
1435 regmask &= ~ (1 << ins->sreg1);
1436 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1438 if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1439 if (ins->sreg2 >= MONO_MAX_IREGS)
1440 regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1441 else
1442 regmask &= ~ (1 << ins->sreg2);
1443 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1445 if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1446 regmask &= ~ (1 << ins->dreg);
1447 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1450 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1451 g_assert (regmask); /* need at least a register we can free */
1452 sel = -1;
1453 /* we should track prev_use and spill the register that's farther */
1454 for (i = 0; i < MONO_MAX_IREGS; ++i) {
1455 if (regmask & (1 << i)) {
1456 sel = i;
1457 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1458 break;
1461 i = cfg->rs->isymbolic [sel];
1462 spill = ++cfg->spill_count;
1463 cfg->rs->iassign [i] = -spill - 1;
1464 mono_regstate_free_int (cfg->rs, sel);
1465 /* we need to create a spill var and insert a load to sel after the current instruction */
1466 MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1467 load->dreg = sel;
1468 load->inst_basereg = cfg->frame_reg;
1469 load->inst_offset = mono_spillvar_offset (cfg, spill);
1470 if (item->prev) {
1471 while (ins->next != item->prev->data)
1472 ins = ins->next;
1474 load->next = ins->next;
1475 ins->next = load;
1476 DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1477 i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1478 g_assert (i == sel);
1480 return sel;
1483 static int
1484 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1486 MonoInst *load;
1487 int i, sel, spill;
1489 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));
1490 /* exclude the registers in the current instruction */
1491 if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1492 if (ins->sreg1 >= MONO_MAX_FREGS)
1493 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1494 else
1495 regmask &= ~ (1 << ins->sreg1);
1496 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1498 if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1499 if (ins->sreg2 >= MONO_MAX_FREGS)
1500 regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1501 else
1502 regmask &= ~ (1 << ins->sreg2);
1503 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1505 if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1506 regmask &= ~ (1 << ins->dreg);
1507 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1510 DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1511 g_assert (regmask); /* need at least a register we can free */
1512 sel = -1;
1513 /* we should track prev_use and spill the register that's farther */
1514 for (i = 0; i < MONO_MAX_FREGS; ++i) {
1515 if (regmask & (1 << i)) {
1516 sel = i;
1517 DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1518 break;
1521 i = cfg->rs->fsymbolic [sel];
1522 spill = ++cfg->spill_count;
1523 cfg->rs->fassign [i] = -spill - 1;
1524 mono_regstate_free_float(cfg->rs, sel);
1525 /* we need to create a spill var and insert a load to sel after the current instruction */
1526 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1527 load->dreg = sel;
1528 load->inst_basereg = cfg->frame_reg;
1529 load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1530 if (item->prev) {
1531 while (ins->next != item->prev->data)
1532 ins = ins->next;
1534 load->next = ins->next;
1535 ins->next = load;
1536 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)));
1537 i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1538 g_assert (i == sel);
1540 return sel;
1543 static MonoInst*
1544 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1546 MonoInst *copy;
1547 MONO_INST_NEW (cfg, copy, OP_MOVE);
1548 copy->dreg = dest;
1549 copy->sreg1 = src;
1550 if (ins) {
1551 copy->next = ins->next;
1552 ins->next = copy;
1554 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1555 return copy;
1558 static MonoInst*
1559 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1561 MonoInst *copy;
1562 MONO_INST_NEW (cfg, copy, OP_FMOVE);
1563 copy->dreg = dest;
1564 copy->sreg1 = src;
1565 if (ins) {
1566 copy->next = ins->next;
1567 ins->next = copy;
1569 DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1570 return copy;
1573 static MonoInst*
1574 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1576 MonoInst *store;
1577 MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1578 store->sreg1 = reg;
1579 store->inst_destbasereg = cfg->frame_reg;
1580 store->inst_offset = mono_spillvar_offset (cfg, spill);
1581 if (ins) {
1582 store->next = ins->next;
1583 ins->next = store;
1585 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)));
1586 return store;
1589 static MonoInst*
1590 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1592 MonoInst *store;
1593 MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1594 store->sreg1 = reg;
1595 store->inst_destbasereg = cfg->frame_reg;
1596 store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1597 if (ins) {
1598 store->next = ins->next;
1599 ins->next = store;
1601 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)));
1602 return store;
1605 static void
1606 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1608 MonoInst *prev;
1609 g_assert (item->next);
1610 prev = item->next->data;
1612 while (prev->next != ins)
1613 prev = prev->next;
1614 to_insert->next = ins;
1615 prev->next = to_insert;
1617 * needed otherwise in the next instruction we can add an ins to the
1618 * end and that would get past this instruction.
1620 item->data = to_insert;
1623 static int
1624 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1626 int val = cfg->rs->iassign [sym_reg];
1627 if (val < 0) {
1628 int spill = 0;
1629 if (val < -1) {
1630 /* the register gets spilled after this inst */
1631 spill = -val -1;
1633 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1634 if (val < 0)
1635 val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1636 cfg->rs->iassign [sym_reg] = val;
1637 /* add option to store before the instruction for src registers */
1638 if (spill)
1639 create_spilled_store (cfg, spill, val, sym_reg, ins);
1641 cfg->rs->isymbolic [val] = sym_reg;
1642 return val;
1646 * Local register allocation.
1647 * We first scan the list of instructions and we save the liveness info of
1648 * each register (when the register is first used, when it's value is set etc.).
1649 * We also reverse the list of instructions (in the InstList list) because assigning
1650 * registers backwards allows for more tricks to be used.
1652 void
1653 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1655 MonoInst *ins;
1656 MonoRegState *rs = cfg->rs;
1657 int i, val;
1658 RegTrack *reginfo, *reginfof;
1659 RegTrack *reginfo1, *reginfo2, *reginfod;
1660 InstList *tmp, *reversed = NULL;
1661 const char *spec;
1662 guint32 src1_mask, src2_mask, dest_mask;
1663 guint32 cur_iregs, cur_fregs;
1665 if (!bb->code)
1666 return;
1667 rs->next_vireg = bb->max_ireg;
1668 rs->next_vfreg = bb->max_freg;
1669 mono_regstate_assign (rs);
1670 reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1671 reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1672 rs->ifree_mask = PPC_CALLER_REGS;
1673 rs->ffree_mask = PPC_CALLER_FREGS;
1675 ins = bb->code;
1676 i = 1;
1677 DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1678 /* forward pass on the instructions to collect register liveness info */
1679 while (ins) {
1680 spec = ins_spec [ins->opcode];
1681 DEBUG (print_ins (i, ins));
1682 /*if (spec [MONO_INST_CLOB] == 'c') {
1683 MonoCallInst * call = (MonoCallInst*)ins;
1684 int j;
1686 if (spec [MONO_INST_SRC1]) {
1687 if (spec [MONO_INST_SRC1] == 'f')
1688 reginfo1 = reginfof;
1689 else
1690 reginfo1 = reginfo;
1691 reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1692 reginfo1 [ins->sreg1].last_use = i;
1693 } else {
1694 ins->sreg1 = -1;
1696 if (spec [MONO_INST_SRC2]) {
1697 if (spec [MONO_INST_SRC2] == 'f')
1698 reginfo2 = reginfof;
1699 else
1700 reginfo2 = reginfo;
1701 reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1702 reginfo2 [ins->sreg2].last_use = i;
1703 } else {
1704 ins->sreg2 = -1;
1706 if (spec [MONO_INST_DEST]) {
1707 if (spec [MONO_INST_DEST] == 'f')
1708 reginfod = reginfof;
1709 else
1710 reginfod = reginfo;
1711 if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1712 reginfod [ins->dreg].killed_in = i;
1713 reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1714 reginfod [ins->dreg].last_use = i;
1715 if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1716 reginfod [ins->dreg].born_in = i;
1717 if (spec [MONO_INST_DEST] == 'l') {
1718 /* result in eax:edx, the virtual register is allocated sequentially */
1719 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1720 reginfod [ins->dreg + 1].last_use = i;
1721 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1722 reginfod [ins->dreg + 1].born_in = i;
1724 } else {
1725 ins->dreg = -1;
1727 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1728 ++i;
1729 ins = ins->next;
1732 cur_iregs = PPC_CALLER_REGS;
1733 cur_fregs = PPC_CALLER_FREGS;
1735 DEBUG (print_regtrack (reginfo, rs->next_vireg));
1736 DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1737 tmp = reversed;
1738 while (tmp) {
1739 int prev_dreg, prev_sreg1, prev_sreg2;
1740 --i;
1741 ins = tmp->data;
1742 spec = ins_spec [ins->opcode];
1743 DEBUG (g_print ("processing:"));
1744 DEBUG (print_ins (i, ins));
1745 /* make the register available for allocation: FIXME add fp reg */
1746 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1747 cur_iregs |= 1 << ins->dreg;
1748 DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1749 } else if (ins->opcode == OP_SETFREG) {
1750 cur_fregs |= 1 << ins->dreg;
1751 DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1752 } else if (spec [MONO_INST_CLOB] == 'c') {
1753 MonoCallInst *cinst = (MonoCallInst*)ins;
1754 DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1755 DEBUG (g_print ("excluding fpregs 0x%x from cur_fregs (0x%x)\n", cinst->used_fregs, cur_fregs));
1756 cur_iregs &= ~cinst->used_iregs;
1757 cur_fregs &= ~cinst->used_fregs;
1758 DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1759 DEBUG (g_print ("available cur_fregs: 0x%x\n", cur_fregs));
1760 /* registers used by the calling convention are excluded from
1761 * allocation: they will be selectively enabled when they are
1762 * assigned by the special SETREG opcodes.
1765 dest_mask = src1_mask = src2_mask = cur_iregs;
1766 /* update for use with FP regs... */
1767 if (spec [MONO_INST_DEST] == 'f') {
1768 dest_mask = cur_fregs;
1769 if (ins->dreg >= MONO_MAX_FREGS) {
1770 val = rs->fassign [ins->dreg];
1771 prev_dreg = ins->dreg;
1772 if (val < 0) {
1773 int spill = 0;
1774 if (val < -1) {
1775 /* the register gets spilled after this inst */
1776 spill = -val -1;
1778 val = mono_regstate_alloc_float (rs, dest_mask);
1779 if (val < 0)
1780 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1781 rs->fassign [ins->dreg] = val;
1782 if (spill)
1783 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1785 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1786 rs->fsymbolic [val] = prev_dreg;
1787 ins->dreg = val;
1788 if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != ppc_f1) {
1789 /* this instruction only outputs to ppc_f1, need to copy */
1790 create_copy_ins_float (cfg, ins->dreg, ppc_f1, ins);
1792 } else {
1793 prev_dreg = -1;
1795 if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1796 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1797 mono_regstate_free_float (rs, ins->dreg);
1799 } else if (ins->dreg >= MONO_MAX_IREGS) {
1800 val = rs->iassign [ins->dreg];
1801 prev_dreg = ins->dreg;
1802 if (val < 0) {
1803 int spill = 0;
1804 if (val < -1) {
1805 /* the register gets spilled after this inst */
1806 spill = -val -1;
1808 val = mono_regstate_alloc_int (rs, dest_mask);
1809 if (val < 0)
1810 val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1811 rs->iassign [ins->dreg] = val;
1812 if (spill)
1813 create_spilled_store (cfg, spill, val, prev_dreg, ins);
1815 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1816 rs->isymbolic [val] = prev_dreg;
1817 ins->dreg = val;
1818 if (spec [MONO_INST_DEST] == 'l') {
1819 int hreg = prev_dreg + 1;
1820 val = rs->iassign [hreg];
1821 if (val < 0) {
1822 int spill = 0;
1823 if (val < -1) {
1824 /* the register gets spilled after this inst */
1825 spill = -val -1;
1827 val = mono_regstate_alloc_int (rs, dest_mask);
1828 if (val < 0)
1829 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1830 rs->iassign [hreg] = val;
1831 if (spill)
1832 create_spilled_store (cfg, spill, val, hreg, ins);
1834 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1835 rs->isymbolic [val] = hreg;
1836 /* FIXME:? ins->dreg = val; */
1837 if (ins->dreg == ppc_r4) {
1838 if (val != ppc_r3)
1839 create_copy_ins (cfg, val, ppc_r3, ins);
1840 } else if (ins->dreg == ppc_r3) {
1841 if (val == ppc_r4) {
1842 /* swap */
1843 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1844 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1845 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1846 } else {
1847 /* two forced copies */
1848 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1849 create_copy_ins (cfg, val, ppc_r3, ins);
1851 } else {
1852 if (val == ppc_r3) {
1853 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1854 } else {
1855 /* two forced copies */
1856 create_copy_ins (cfg, val, ppc_r3, ins);
1857 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1860 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
1861 DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1862 mono_regstate_free_int (rs, val);
1864 } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != ppc_r3 && spec [MONO_INST_CLOB] != 'd') {
1865 /* this instruction only outputs to ppc_r3, need to copy */
1866 create_copy_ins (cfg, ins->dreg, ppc_r3, ins);
1868 } else {
1869 prev_dreg = -1;
1871 if (spec [MONO_INST_DEST] == 'f' && freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i)) {
1872 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1873 mono_regstate_free_float (rs, ins->dreg);
1874 } else if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
1875 DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1876 mono_regstate_free_int (rs, ins->dreg);
1878 if (spec [MONO_INST_SRC1] == 'f') {
1879 src1_mask = cur_fregs;
1880 if (ins->sreg1 >= MONO_MAX_FREGS) {
1881 val = rs->fassign [ins->sreg1];
1882 prev_sreg1 = ins->sreg1;
1883 if (val < 0) {
1884 int spill = 0;
1885 if (val < -1) {
1886 /* the register gets spilled after this inst */
1887 spill = -val -1;
1889 //g_assert (val == -1); /* source cannot be spilled */
1890 val = mono_regstate_alloc_float (rs, src1_mask);
1891 if (val < 0)
1892 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1893 rs->fassign [ins->sreg1] = val;
1894 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1895 if (spill) {
1896 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
1897 insert_before_ins (ins, tmp, store);
1900 rs->fsymbolic [val] = prev_sreg1;
1901 ins->sreg1 = val;
1902 } else {
1903 prev_sreg1 = -1;
1905 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
1906 val = rs->iassign [ins->sreg1];
1907 prev_sreg1 = ins->sreg1;
1908 if (val < 0) {
1909 int spill = 0;
1910 if (val < -1) {
1911 /* the register gets spilled after this inst */
1912 spill = -val -1;
1914 if (0 && ins->opcode == OP_MOVE) {
1916 * small optimization: the dest register is already allocated
1917 * but the src one is not: we can simply assign the same register
1918 * here and peephole will get rid of the instruction later.
1919 * This optimization may interfere with the clobbering handling:
1920 * it removes a mov operation that will be added again to handle clobbering.
1921 * There are also some other issues that should with make testjit.
1923 mono_regstate_alloc_int (rs, 1 << ins->dreg);
1924 val = rs->iassign [ins->sreg1] = ins->dreg;
1925 //g_assert (val >= 0);
1926 DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1927 } else {
1928 //g_assert (val == -1); /* source cannot be spilled */
1929 val = mono_regstate_alloc_int (rs, src1_mask);
1930 if (val < 0)
1931 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1932 rs->iassign [ins->sreg1] = val;
1933 DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1935 if (spill) {
1936 MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1937 insert_before_ins (ins, tmp, store);
1940 rs->isymbolic [val] = prev_sreg1;
1941 ins->sreg1 = val;
1942 } else {
1943 prev_sreg1 = -1;
1945 if (spec [MONO_INST_SRC2] == 'f') {
1946 src2_mask = cur_fregs;
1947 if (ins->sreg2 >= MONO_MAX_FREGS) {
1948 val = rs->fassign [ins->sreg2];
1949 prev_sreg2 = ins->sreg2;
1950 if (val < 0) {
1951 int spill = 0;
1952 if (val < -1) {
1953 /* the register gets spilled after this inst */
1954 spill = -val -1;
1956 val = mono_regstate_alloc_float (rs, src2_mask);
1957 if (val < 0)
1958 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1959 rs->fassign [ins->sreg2] = val;
1960 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1961 if (spill)
1962 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
1964 rs->fsymbolic [val] = prev_sreg2;
1965 ins->sreg2 = val;
1966 } else {
1967 prev_sreg2 = -1;
1969 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
1970 val = rs->iassign [ins->sreg2];
1971 prev_sreg2 = ins->sreg2;
1972 if (val < 0) {
1973 int spill = 0;
1974 if (val < -1) {
1975 /* the register gets spilled after this inst */
1976 spill = -val -1;
1978 val = mono_regstate_alloc_int (rs, src2_mask);
1979 if (val < 0)
1980 val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1981 rs->iassign [ins->sreg2] = val;
1982 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1983 if (spill)
1984 create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1986 rs->isymbolic [val] = prev_sreg2;
1987 ins->sreg2 = val;
1988 } else {
1989 prev_sreg2 = -1;
1992 if (spec [MONO_INST_CLOB] == 'c') {
1993 int j, s;
1994 guint32 clob_mask = PPC_CALLER_REGS;
1995 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1996 s = 1 << j;
1997 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1998 //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
2002 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2003 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2004 mono_regstate_free_int (rs, ins->sreg1);
2006 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2007 DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2008 mono_regstate_free_int (rs, ins->sreg2);
2011 //DEBUG (print_ins (i, ins));
2012 tmp = tmp->next;
2014 cfg->max_ireg = MAX (cfg->max_ireg, rs->max_ireg);
2017 static guchar*
2018 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2020 /* sreg is a float, dreg is an integer reg. ppc_f0 is used a scratch */
2021 ppc_fctiwz (code, ppc_f0, sreg);
2022 ppc_stfd (code, ppc_f0, -8, ppc_sp);
2023 ppc_lwz (code, dreg, -4, ppc_sp);
2024 if (!is_signed) {
2025 if (size == 1)
2026 ppc_andid (code, dreg, dreg, 0xff);
2027 else if (size == 2)
2028 ppc_andid (code, dreg, dreg, 0xffff);
2029 } else {
2030 if (size == 1)
2031 ppc_extsb (code, dreg, dreg);
2032 else if (size == 2)
2033 ppc_extsh (code, dreg, dreg);
2035 return code;
2038 static unsigned char*
2039 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2041 #if 0
2042 int sreg = tree->sreg1;
2043 x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2044 if (tree->flags & MONO_INST_INIT) {
2045 int offset = 0;
2046 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2047 x86_push_reg (code, X86_EAX);
2048 offset += 4;
2050 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2051 x86_push_reg (code, X86_ECX);
2052 offset += 4;
2054 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2055 x86_push_reg (code, X86_EDI);
2056 offset += 4;
2059 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2060 if (sreg != X86_ECX)
2061 x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2062 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2064 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2065 x86_cld (code);
2066 x86_prefix (code, X86_REP_PREFIX);
2067 x86_stosl (code);
2069 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2070 x86_pop_reg (code, X86_EDI);
2071 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2072 x86_pop_reg (code, X86_ECX);
2073 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2074 x86_pop_reg (code, X86_EAX);
2076 #endif
2077 return code;
2080 typedef struct {
2081 guchar *code;
2082 guchar *target;
2083 int absolute;
2084 int found;
2085 } PatchData;
2087 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2089 static int
2090 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2091 PatchData *pdata = (PatchData*)user_data;
2092 guchar *code = data;
2093 guint32 *thunks = data;
2094 guint32 *endthunks = (guint32*)(code + bsize);
2095 guint32 load [2];
2096 guchar *templ;
2097 int i, count = 0;
2098 int difflow, diffhigh;
2100 /* always ensure a call from pdata->code can reach to the thunks without further thunks */
2101 difflow = (char*)pdata->code - (char*)thunks;
2102 diffhigh = (char*)pdata->code - (char*)endthunks;
2103 if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
2104 return 0;
2106 templ = (guchar*)load;
2107 ppc_lis (templ, ppc_r0, (guint32)(pdata->target) >> 16);
2108 ppc_ori (templ, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2110 //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2111 if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2112 while (thunks < endthunks) {
2113 //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2114 if ((thunks [0] == load [0]) && (thunks [1] == load [1])) {
2115 ppc_patch (pdata->code, (guchar*)thunks);
2116 mono_arch_flush_icache (pdata->code, 4);
2117 pdata->found = 1;
2118 return 1;
2119 } else if ((thunks [0] == 0) && (thunks [1] == 0)) {
2120 /* found a free slot instead: emit thunk */
2121 code = (guchar*)thunks;
2122 ppc_lis (code, ppc_r0, (guint32)(pdata->target) >> 16);
2123 ppc_ori (code, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2124 ppc_mtctr (code, ppc_r0);
2125 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2126 mono_arch_flush_icache ((guchar*)thunks, 16);
2128 ppc_patch (pdata->code, (guchar*)thunks);
2129 mono_arch_flush_icache (pdata->code, 4);
2130 pdata->found = 1;
2131 return 1;
2133 /* skip 16 bytes, the size of the thunk */
2134 thunks += 4;
2135 count++;
2137 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2139 return 0;
2142 static void
2143 handle_thunk (int absolute, guchar *code, guchar *target) {
2144 MonoDomain *domain = mono_domain_get ();
2145 PatchData pdata;
2147 pdata.code = code;
2148 pdata.target = target;
2149 pdata.absolute = absolute;
2150 pdata.found = 0;
2152 mono_domain_lock (domain);
2153 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2155 if (!pdata.found) {
2156 /* this uses the first available slot */
2157 pdata.found = 2;
2158 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2160 mono_domain_unlock (domain);
2162 if (pdata.found != 1)
2163 g_print ("thunk failed for %p from %p\n", target, code);
2164 g_assert (pdata.found == 1);
2167 void
2168 ppc_patch (guchar *code, guchar *target)
2170 guint32 ins = *(guint32*)code;
2171 guint32 prim = ins >> 26;
2172 guint32 ovf;
2174 //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2175 if (prim == 18) {
2176 // prefer relative branches, they are more position independent (e.g. for AOT compilation).
2177 gint diff = target - code;
2178 if (diff >= 0){
2179 if (diff <= 33554431){
2180 ins = (18 << 26) | (diff) | (ins & 1);
2181 *(guint32*)code = ins;
2182 return;
2184 } else {
2185 /* diff between 0 and -33554432 */
2186 if (diff >= -33554432){
2187 ins = (18 << 26) | (diff & ~0xfc000000) | (ins & 1);
2188 *(guint32*)code = ins;
2189 return;
2193 if ((glong)target >= 0){
2194 if ((glong)target <= 33554431){
2195 ins = (18 << 26) | ((guint32) target) | (ins & 1) | 2;
2196 *(guint32*)code = ins;
2197 return;
2199 } else {
2200 if ((glong)target >= -33554432){
2201 ins = (18 << 26) | (((guint32)target) & ~0xfc000000) | (ins & 1) | 2;
2202 *(guint32*)code = ins;
2203 return;
2207 handle_thunk (TRUE, code, target);
2208 return;
2210 g_assert_not_reached ();
2214 if (prim == 16) {
2215 // absolute address
2216 if (ins & 2) {
2217 guint32 li = (guint32)target;
2218 ins = (ins & 0xffff0000) | (ins & 3);
2219 ovf = li & 0xffff0000;
2220 if (ovf != 0 && ovf != 0xffff0000)
2221 g_assert_not_reached ();
2222 li &= 0xffff;
2223 ins |= li;
2224 // FIXME: assert the top bits of li are 0
2225 } else {
2226 gint diff = target - code;
2227 ins = (ins & 0xffff0000) | (ins & 3);
2228 ovf = diff & 0xffff0000;
2229 if (ovf != 0 && ovf != 0xffff0000)
2230 g_assert_not_reached ();
2231 diff &= 0xffff;
2232 ins |= diff;
2234 *(guint32*)code = ins;
2235 } else {
2236 g_assert_not_reached ();
2238 // g_print ("patched with 0x%08x\n", ins);
2241 void
2242 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2244 MonoInst *ins;
2245 MonoCallInst *call;
2246 guint offset;
2247 guint8 *code = cfg->native_code + cfg->code_len;
2248 MonoInst *last_ins = NULL;
2249 guint last_offset = 0;
2250 int max_len, cpos;
2252 if (cfg->opt & MONO_OPT_PEEPHOLE)
2253 peephole_pass (cfg, bb);
2255 /* we don't align basic blocks of loops on ppc */
2257 if (cfg->verbose_level > 2)
2258 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2260 cpos = bb->max_offset;
2262 if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2263 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2264 //g_assert (!mono_compile_aot);
2265 //cpos += 6;
2266 //if (bb->cil_code)
2267 // cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2268 /* this is not thread save, but good enough */
2269 /* fixme: howto handle overflows? */
2270 //x86_inc_mem (code, &cov->data [bb->dfn].count);
2273 ins = bb->code;
2274 while (ins) {
2275 offset = code - cfg->native_code;
2277 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2279 if (offset > (cfg->code_size - max_len - 16)) {
2280 cfg->code_size *= 2;
2281 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2282 code = cfg->native_code + offset;
2284 // if (ins->cil_code)
2285 // g_print ("cil code\n");
2286 mono_debug_record_line_number (cfg, ins, offset);
2288 switch (ins->opcode) {
2289 case OP_BIGMUL:
2290 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2291 ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
2292 break;
2293 case OP_BIGMUL_UN:
2294 ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2295 ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
2296 break;
2297 case OP_STOREI1_MEMBASE_IMM:
2298 ppc_li (code, ppc_r0, ins->inst_imm);
2299 if (ppc_is_imm16 (ins->inst_offset)) {
2300 ppc_stb (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2301 } else {
2302 ppc_load (code, ppc_r11, ins->inst_offset);
2303 ppc_stbx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2305 break;
2306 case OP_STOREI2_MEMBASE_IMM:
2307 ppc_li (code, ppc_r0, ins->inst_imm);
2308 if (ppc_is_imm16 (ins->inst_offset)) {
2309 ppc_sth (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2310 } else {
2311 ppc_load (code, ppc_r11, ins->inst_offset);
2312 ppc_sthx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2314 break;
2315 case OP_STORE_MEMBASE_IMM:
2316 case OP_STOREI4_MEMBASE_IMM:
2317 ppc_load (code, ppc_r0, ins->inst_imm);
2318 if (ppc_is_imm16 (ins->inst_offset)) {
2319 ppc_stw (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2320 } else {
2321 ppc_load (code, ppc_r11, ins->inst_offset);
2322 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2324 break;
2325 case OP_STOREI1_MEMBASE_REG:
2326 if (ppc_is_imm16 (ins->inst_offset)) {
2327 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2328 } else {
2329 ppc_load (code, ppc_r11, ins->inst_offset);
2330 ppc_stbx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2332 break;
2333 case OP_STOREI2_MEMBASE_REG:
2334 if (ppc_is_imm16 (ins->inst_offset)) {
2335 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2336 } else {
2337 ppc_load (code, ppc_r11, ins->inst_offset);
2338 ppc_sthx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2340 break;
2341 case OP_STORE_MEMBASE_REG:
2342 case OP_STOREI4_MEMBASE_REG:
2343 if (ppc_is_imm16 (ins->inst_offset)) {
2344 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2345 } else {
2346 ppc_load (code, ppc_r11, ins->inst_offset);
2347 ppc_stwx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2349 break;
2350 case CEE_LDIND_I:
2351 case CEE_LDIND_I4:
2352 case CEE_LDIND_U4:
2353 g_assert_not_reached ();
2354 //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2355 break;
2356 case OP_LOADU4_MEM:
2357 g_assert_not_reached ();
2358 //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2359 //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2360 break;
2361 case OP_LOAD_MEMBASE:
2362 case OP_LOADI4_MEMBASE:
2363 case OP_LOADU4_MEMBASE:
2364 if (ppc_is_imm16 (ins->inst_offset)) {
2365 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2366 } else {
2367 ppc_load (code, ppc_r11, ins->inst_offset);
2368 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2370 break;
2371 case OP_LOADI1_MEMBASE:
2372 case OP_LOADU1_MEMBASE:
2373 if (ppc_is_imm16 (ins->inst_offset)) {
2374 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2375 } else {
2376 ppc_load (code, ppc_r11, ins->inst_offset);
2377 ppc_lbzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2379 if (ins->opcode == OP_LOADI1_MEMBASE)
2380 ppc_extsb (code, ins->dreg, ins->dreg);
2381 break;
2382 case OP_LOADU2_MEMBASE:
2383 if (ppc_is_imm16 (ins->inst_offset)) {
2384 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2385 } else {
2386 ppc_load (code, ppc_r11, ins->inst_offset);
2387 ppc_lhzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2389 break;
2390 case OP_LOADI2_MEMBASE:
2391 if (ppc_is_imm16 (ins->inst_offset)) {
2392 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2393 } else {
2394 ppc_load (code, ppc_r11, ins->inst_offset);
2395 ppc_lhax (code, ins->dreg, ppc_r11, ins->inst_basereg);
2397 break;
2398 case CEE_CONV_I1:
2399 ppc_extsb (code, ins->dreg, ins->sreg1);
2400 break;
2401 case CEE_CONV_I2:
2402 ppc_extsh (code, ins->dreg, ins->sreg1);
2403 break;
2404 case CEE_CONV_U1:
2405 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
2406 break;
2407 case CEE_CONV_U2:
2408 ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
2409 break;
2410 case OP_COMPARE:
2411 if (ins->next &&
2412 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2413 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2414 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN)))
2415 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2416 else
2417 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2418 break;
2419 case OP_COMPARE_IMM:
2420 if (ins->next &&
2421 ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2422 (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2423 (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN))) {
2424 if (ppc_is_uimm16 (ins->inst_imm)) {
2425 ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2426 } else {
2427 ppc_load (code, ppc_r11, ins->inst_imm);
2428 ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2430 } else {
2431 if (ppc_is_imm16 (ins->inst_imm)) {
2432 ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2433 } else {
2434 ppc_load (code, ppc_r11, ins->inst_imm);
2435 ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2438 break;
2439 case OP_X86_TEST_NULL:
2440 ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2441 break;
2442 case CEE_BREAK:
2443 ppc_break (code);
2444 break;
2445 case OP_ADDCC:
2446 ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2447 break;
2448 case CEE_ADD:
2449 ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2450 break;
2451 case OP_ADC:
2452 ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2453 break;
2454 case OP_ADDCC_IMM:
2455 if (ppc_is_imm16 (ins->inst_imm)) {
2456 ppc_addic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2457 } else {
2458 ppc_load (code, ppc_r11, ins->inst_imm);
2459 ppc_addc (code, ins->dreg, ins->sreg1, ppc_r11);
2461 break;
2462 case OP_ADD_IMM:
2463 if (ppc_is_imm16 (ins->inst_imm)) {
2464 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2465 } else {
2466 ppc_load (code, ppc_r11, ins->inst_imm);
2467 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2469 break;
2470 case OP_ADC_IMM:
2471 ppc_load (code, ppc_r11, ins->inst_imm);
2472 ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2473 break;
2474 case CEE_ADD_OVF:
2475 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2477 ppc_addo (code, ins->dreg, ins->sreg1, ins->sreg2);
2478 ppc_mfspr (code, ppc_r0, ppc_xer);
2479 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2480 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2481 break;
2482 case CEE_ADD_OVF_UN:
2483 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2485 ppc_addco (code, ins->dreg, ins->sreg1, ins->sreg2);
2486 ppc_mfspr (code, ppc_r0, ppc_xer);
2487 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2488 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2489 break;
2490 case CEE_SUB_OVF:
2491 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2493 ppc_subfo (code, ins->dreg, ins->sreg2, ins->sreg1);
2494 ppc_mfspr (code, ppc_r0, ppc_xer);
2495 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2496 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2497 break;
2498 case CEE_SUB_OVF_UN:
2499 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2501 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2502 ppc_mfspr (code, ppc_r0, ppc_xer);
2503 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2504 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2505 break;
2506 case OP_ADD_OVF_CARRY:
2507 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2509 ppc_addeo (code, ins->dreg, ins->sreg1, ins->sreg2);
2510 ppc_mfspr (code, ppc_r0, ppc_xer);
2511 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2512 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2513 break;
2514 case OP_ADD_OVF_UN_CARRY:
2515 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2517 ppc_addeo (code, ins->dreg, ins->sreg1, ins->sreg2);
2518 ppc_mfspr (code, ppc_r0, ppc_xer);
2519 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2520 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2521 break;
2522 case OP_SUB_OVF_CARRY:
2523 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2525 ppc_subfeo (code, ins->dreg, ins->sreg2, ins->sreg1);
2526 ppc_mfspr (code, ppc_r0, ppc_xer);
2527 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2528 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2529 break;
2530 case OP_SUB_OVF_UN_CARRY:
2531 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2533 ppc_subfeo (code, ins->dreg, ins->sreg2, ins->sreg1);
2534 ppc_mfspr (code, ppc_r0, ppc_xer);
2535 ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2536 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2537 break;
2538 case OP_SUBCC:
2539 ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2540 break;
2541 case OP_SUBCC_IMM:
2542 ppc_load (code, ppc_r11, ins->inst_imm);
2543 ppc_subfc (code, ins->dreg, ppc_r11, ins->sreg1);
2544 break;
2545 case CEE_SUB:
2546 ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2547 break;
2548 case OP_SBB:
2549 ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2550 break;
2551 case OP_SUB_IMM:
2552 // we add the negated value
2553 if (ppc_is_imm16 (-ins->inst_imm))
2554 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2555 else {
2556 ppc_load (code, ppc_r11, ins->inst_imm);
2557 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2559 break;
2560 case OP_SBB_IMM:
2561 ppc_load (code, ppc_r11, ins->inst_imm);
2562 ppc_subfe (code, ins->dreg, ppc_r11, ins->sreg1);
2563 break;
2564 case OP_PPC_SUBFIC:
2565 g_assert (ppc_is_imm16 (ins->inst_imm));
2566 ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2567 break;
2568 case OP_PPC_SUBFZE:
2569 ppc_subfze (code, ins->dreg, ins->sreg1);
2570 break;
2571 case CEE_AND:
2572 /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2573 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2574 break;
2575 case OP_AND_IMM:
2576 if (!(ins->inst_imm & 0xffff0000)) {
2577 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2578 } else if (!(ins->inst_imm & 0xffff)) {
2579 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2580 } else {
2581 ppc_load (code, ppc_r11, ins->inst_imm);
2582 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2584 break;
2585 case CEE_DIV:
2586 /* XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2588 ppc_divwod (code, ins->dreg, ins->sreg1, ins->sreg2);
2589 ppc_mfspr (code, ppc_r0, ppc_xer);
2590 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2591 /* FIXME: use OverflowException for 0x80000000/-1 */
2592 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2593 break;
2594 case CEE_DIV_UN:
2595 ppc_divwuod (code, ins->dreg, ins->sreg1, ins->sreg2);
2596 ppc_mfspr (code, ppc_r0, ppc_xer);
2597 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2598 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2599 break;
2600 case OP_DIV_IMM:
2601 g_assert_not_reached ();
2602 #if 0
2603 ppc_load (code, ppc_r11, ins->inst_imm);
2604 ppc_divwod (code, ins->dreg, ins->sreg1, ppc_r11);
2605 ppc_mfspr (code, ppc_r0, ppc_xer);
2606 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2607 /* FIXME: use OverflowException for 0x80000000/-1 */
2608 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2609 break;
2610 #endif
2611 case CEE_REM:
2612 ppc_divwod (code, ppc_r11, ins->sreg1, ins->sreg2);
2613 ppc_mfspr (code, ppc_r0, ppc_xer);
2614 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2615 /* FIXME: use OverflowException for 0x80000000/-1 */
2616 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2617 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2618 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2619 break;
2620 case CEE_REM_UN:
2621 ppc_divwuod (code, ppc_r11, ins->sreg1, ins->sreg2);
2622 ppc_mfspr (code, ppc_r0, ppc_xer);
2623 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2624 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2625 ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2626 ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2627 break;
2628 case OP_REM_IMM:
2629 g_assert_not_reached ();
2630 case CEE_OR:
2631 ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2632 break;
2633 case OP_OR_IMM:
2634 if (!(ins->inst_imm & 0xffff0000)) {
2635 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2636 } else if (!(ins->inst_imm & 0xffff)) {
2637 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2638 } else {
2639 ppc_load (code, ppc_r11, ins->inst_imm);
2640 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2642 break;
2643 case CEE_XOR:
2644 ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2645 break;
2646 case OP_XOR_IMM:
2647 if (!(ins->inst_imm & 0xffff0000)) {
2648 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2649 } else if (!(ins->inst_imm & 0xffff)) {
2650 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2651 } else {
2652 ppc_load (code, ppc_r11, ins->inst_imm);
2653 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2655 break;
2656 case CEE_SHL:
2657 ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2658 break;
2659 case OP_SHL_IMM:
2660 ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2661 //ppc_load (code, ppc_r11, ins->inst_imm);
2662 //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2663 break;
2664 case CEE_SHR:
2665 ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2666 break;
2667 case OP_SHR_IMM:
2668 // there is also ppc_srawi
2669 //ppc_load (code, ppc_r11, ins->inst_imm);
2670 //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2671 ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2672 break;
2673 case OP_SHR_UN_IMM:
2674 /*ppc_load (code, ppc_r11, ins->inst_imm);
2675 ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);*/
2676 ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0x1f)), (ins->inst_imm & 0x1f), 31);
2677 break;
2678 case CEE_SHR_UN:
2679 ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2680 break;
2681 case CEE_NOT:
2682 ppc_not (code, ins->dreg, ins->sreg1);
2683 break;
2684 case CEE_NEG:
2685 ppc_neg (code, ins->dreg, ins->sreg1);
2686 break;
2687 case CEE_MUL:
2688 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2689 break;
2690 case OP_MUL_IMM:
2691 if (ppc_is_imm16 (ins->inst_imm)) {
2692 ppc_mulli (code, ins->dreg, ins->sreg1, ins->inst_imm);
2693 } else {
2694 ppc_load (code, ppc_r11, ins->inst_imm);
2695 ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2697 break;
2698 case CEE_MUL_OVF:
2699 /* we annot use mcrxr, since it's not implemented on some processors
2700 * XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2702 ppc_mullwo (code, ins->dreg, ins->sreg1, ins->sreg2);
2703 ppc_mfspr (code, ppc_r0, ppc_xer);
2704 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2705 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2706 break;
2707 case CEE_MUL_OVF_UN:
2708 /* we first multiply to get the high word and compare to 0
2709 * to set the flags, then the result is discarded and then
2710 * we multiply to get the lower * bits result
2712 ppc_mulhwu (code, ppc_r0, ins->sreg1, ins->sreg2);
2713 ppc_cmpi (code, 0, 0, ppc_r0, 0);
2714 EMIT_COND_SYSTEM_EXCEPTION (CEE_BNE_UN - CEE_BEQ, "OverflowException");
2715 ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2716 break;
2717 case OP_ICONST:
2718 case OP_SETREGIMM:
2719 ppc_load (code, ins->dreg, ins->inst_c0);
2720 break;
2721 case OP_AOTCONST:
2722 mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2723 ppc_lis (code, ins->dreg, 0);
2724 ppc_ori (code, ins->dreg, ins->dreg, 0);
2725 break;
2726 case CEE_CONV_I4:
2727 case CEE_CONV_U4:
2728 case OP_MOVE:
2729 case OP_SETREG:
2730 ppc_mr (code, ins->dreg, ins->sreg1);
2731 break;
2732 case OP_SETLRET: {
2733 int saved = ins->sreg1;
2734 if (ins->sreg1 == ppc_r3) {
2735 ppc_mr (code, ppc_r0, ins->sreg1);
2736 saved = ppc_r0;
2738 if (ins->sreg2 != ppc_r3)
2739 ppc_mr (code, ppc_r3, ins->sreg2);
2740 if (saved != ppc_r4)
2741 ppc_mr (code, ppc_r4, saved);
2742 break;
2744 case OP_SETFREG:
2745 case OP_FMOVE:
2746 ppc_fmr (code, ins->dreg, ins->sreg1);
2747 break;
2748 case OP_FCONV_TO_R4:
2749 ppc_frsp (code, ins->dreg, ins->sreg1);
2750 break;
2751 case CEE_JMP: {
2752 int i, pos = 0;
2755 * Keep in sync with mono_arch_emit_epilog
2757 g_assert (!cfg->method->save_lmf);
2758 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2759 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
2760 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
2761 } else {
2762 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
2763 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
2765 ppc_mtlr (code, ppc_r0);
2767 if (ppc_is_imm16 (cfg->stack_usage)) {
2768 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2769 } else {
2770 ppc_load (code, ppc_r11, cfg->stack_usage);
2771 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
2773 if (!cfg->method->save_lmf) {
2774 /*for (i = 31; i >= 14; --i) {
2775 if (cfg->used_float_regs & (1 << i)) {
2776 pos += sizeof (double);
2777 ppc_lfd (code, i, -pos, cfg->frame_reg);
2780 for (i = 31; i >= 13; --i) {
2781 if (cfg->used_int_regs & (1 << i)) {
2782 pos += sizeof (gulong);
2783 ppc_lwz (code, i, -pos, cfg->frame_reg);
2786 } else {
2787 /* FIXME restore from MonoLMF: though this can't happen yet */
2789 mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2790 ppc_b (code, 0);
2791 break;
2793 case OP_CHECK_THIS:
2794 /* ensure ins->sreg1 is not NULL */
2795 ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2796 break;
2797 case OP_ARGLIST:
2798 /* FIXME: implement */
2799 break;
2800 case OP_FCALL:
2801 case OP_LCALL:
2802 case OP_VCALL:
2803 case OP_VOIDCALL:
2804 case CEE_CALL:
2805 call = (MonoCallInst*)ins;
2806 if (ins->flags & MONO_INST_HAS_METHOD)
2807 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2808 else
2809 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2810 ppc_bl (code, 0);
2811 break;
2812 case OP_FCALL_REG:
2813 case OP_LCALL_REG:
2814 case OP_VCALL_REG:
2815 case OP_VOIDCALL_REG:
2816 case OP_CALL_REG:
2817 ppc_mtlr (code, ins->sreg1);
2818 ppc_blrl (code);
2819 break;
2820 case OP_FCALL_MEMBASE:
2821 case OP_LCALL_MEMBASE:
2822 case OP_VCALL_MEMBASE:
2823 case OP_VOIDCALL_MEMBASE:
2824 case OP_CALL_MEMBASE:
2825 ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2826 ppc_mtlr (code, ppc_r0);
2827 ppc_blrl (code);
2828 break;
2829 case OP_OUTARG:
2830 g_assert_not_reached ();
2831 break;
2832 case OP_LOCALLOC: {
2833 /* keep alignment */
2834 int alloca_waste = PPC_STACK_PARAM_OFFSET + cfg->param_area + 31;
2835 int area_offset = alloca_waste;
2836 area_offset &= ~31;
2837 ppc_addi (code, ppc_r11, ins->sreg1, alloca_waste + 31);
2838 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 0, 27);
2839 ppc_lwz (code, ppc_r0, 0, ppc_sp);
2840 ppc_neg (code, ppc_r11, ppc_r11);
2841 ppc_stwux (code, ppc_r0, ppc_sp, ppc_r11);
2842 ppc_addi (code, ins->dreg, ppc_sp, area_offset);
2843 break;
2845 case CEE_RET:
2846 ppc_blr (code);
2847 break;
2848 case CEE_THROW: {
2849 //ppc_break (code);
2850 ppc_mr (code, ppc_r3, ins->sreg1);
2851 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
2852 (gpointer)"mono_arch_throw_exception");
2853 ppc_bl (code, 0);
2854 break;
2856 case OP_START_HANDLER:
2857 ppc_mflr (code, ppc_r0);
2858 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2859 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2860 } else {
2861 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2862 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_left->inst_basereg);
2864 break;
2865 case OP_ENDFILTER:
2866 if (ins->sreg1 != ppc_r3)
2867 ppc_mr (code, ppc_r3, ins->sreg1);
2868 if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2869 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2870 } else {
2871 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2872 ppc_lwzx (code, ppc_r0, ins->inst_left->inst_basereg, ppc_r11);
2874 ppc_mtlr (code, ppc_r0);
2875 ppc_blr (code);
2876 break;
2877 case CEE_ENDFINALLY:
2878 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2879 ppc_mtlr (code, ppc_r0);
2880 ppc_blr (code);
2881 break;
2882 case OP_CALL_HANDLER:
2883 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2884 ppc_bl (code, 0);
2885 break;
2886 case OP_LABEL:
2887 ins->inst_c0 = code - cfg->native_code;
2888 break;
2889 case CEE_BR:
2890 //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2891 //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2892 //break;
2893 if (ins->flags & MONO_INST_BRLABEL) {
2894 /*if (ins->inst_i0->inst_c0) {
2895 ppc_b (code, 0);
2896 //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2897 } else*/ {
2898 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2899 ppc_b (code, 0);
2901 } else {
2902 /*if (ins->inst_target_bb->native_offset) {
2903 ppc_b (code, 0);
2904 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset);
2905 } else*/ {
2906 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2907 ppc_b (code, 0);
2910 break;
2911 case OP_BR_REG:
2912 ppc_mtctr (code, ins->sreg1);
2913 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2914 break;
2915 case OP_CEQ:
2916 ppc_li (code, ins->dreg, 0);
2917 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2918 ppc_li (code, ins->dreg, 1);
2919 break;
2920 case OP_CLT:
2921 case OP_CLT_UN:
2922 ppc_li (code, ins->dreg, 1);
2923 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2924 ppc_li (code, ins->dreg, 0);
2925 break;
2926 case OP_CGT:
2927 case OP_CGT_UN:
2928 ppc_li (code, ins->dreg, 1);
2929 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2930 ppc_li (code, ins->dreg, 0);
2931 break;
2932 case OP_COND_EXC_EQ:
2933 case OP_COND_EXC_NE_UN:
2934 case OP_COND_EXC_LT:
2935 case OP_COND_EXC_LT_UN:
2936 case OP_COND_EXC_GT:
2937 case OP_COND_EXC_GT_UN:
2938 case OP_COND_EXC_GE:
2939 case OP_COND_EXC_GE_UN:
2940 case OP_COND_EXC_LE:
2941 case OP_COND_EXC_LE_UN:
2942 EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2943 break;
2944 case OP_COND_EXC_C:
2945 /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2947 /*ppc_mfspr (code, ppc_r0, ppc_xer);
2948 ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2949 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2950 break;*/
2951 case OP_COND_EXC_OV:
2952 /*ppc_mcrxr (code, 0);
2953 EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2954 break;*/
2955 case OP_COND_EXC_NC:
2956 case OP_COND_EXC_NO:
2957 g_assert_not_reached ();
2958 break;
2959 case CEE_BEQ:
2960 case CEE_BNE_UN:
2961 case CEE_BLT:
2962 case CEE_BLT_UN:
2963 case CEE_BGT:
2964 case CEE_BGT_UN:
2965 case CEE_BGE:
2966 case CEE_BGE_UN:
2967 case CEE_BLE:
2968 case CEE_BLE_UN:
2969 EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2970 break;
2972 /* floating point opcodes */
2973 case OP_R8CONST:
2974 ppc_load (code, ppc_r11, ins->inst_p0);
2975 ppc_lfd (code, ins->dreg, 0, ppc_r11);
2976 break;
2977 case OP_R4CONST:
2978 ppc_load (code, ppc_r11, ins->inst_p0);
2979 ppc_lfs (code, ins->dreg, 0, ppc_r11);
2980 break;
2981 case OP_STORER8_MEMBASE_REG:
2982 if (ppc_is_imm16 (ins->inst_offset)) {
2983 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2984 } else {
2985 ppc_load (code, ppc_r11, ins->inst_offset);
2986 ppc_stfdx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2988 break;
2989 case OP_LOADR8_MEMBASE:
2990 if (ppc_is_imm16 (ins->inst_offset)) {
2991 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2992 } else {
2993 ppc_load (code, ppc_r11, ins->inst_offset);
2994 ppc_lfdx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2996 break;
2997 case OP_STORER4_MEMBASE_REG:
2998 if (ppc_is_imm16 (ins->inst_offset)) {
2999 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
3000 } else {
3001 ppc_load (code, ppc_r11, ins->inst_offset);
3002 ppc_stfsx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
3004 break;
3005 case OP_LOADR4_MEMBASE:
3006 if (ppc_is_imm16 (ins->inst_offset)) {
3007 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
3008 } else {
3009 ppc_load (code, ppc_r11, ins->inst_offset);
3010 ppc_lfsx (code, ins->dreg, ppc_r11, ins->inst_basereg);
3012 break;
3013 case CEE_CONV_R_UN: {
3014 static const guint64 adjust_val = 0x4330000000000000ULL;
3015 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3016 ppc_stw (code, ppc_r0, -8, ppc_sp);
3017 ppc_stw (code, ins->sreg1, -4, ppc_sp);
3018 ppc_load (code, ppc_r11, &adjust_val);
3019 ppc_lfd (code, ins->dreg, -8, ppc_sp);
3020 ppc_lfd (code, ppc_f0, 0, ppc_r11);
3021 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3022 break;
3024 case CEE_CONV_R4: /* FIXME: change precision */
3025 case CEE_CONV_R8: {
3026 static const guint64 adjust_val = 0x4330000080000000ULL;
3027 // addis is special for ppc_r0
3028 ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3029 ppc_stw (code, ppc_r0, -8, ppc_sp);
3030 ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
3031 ppc_stw (code, ppc_r11, -4, ppc_sp);
3032 ppc_lfd (code, ins->dreg, -8, ppc_sp);
3033 ppc_load (code, ppc_r11, &adjust_val);
3034 ppc_lfd (code, ppc_f0, 0, ppc_r11);
3035 ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3036 break;
3038 case OP_X86_FP_LOAD_I8:
3039 g_assert_not_reached ();
3040 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
3041 break;
3042 case OP_X86_FP_LOAD_I4:
3043 g_assert_not_reached ();
3044 /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
3045 break;
3046 case OP_FCONV_TO_I1:
3047 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
3048 break;
3049 case OP_FCONV_TO_U1:
3050 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
3051 break;
3052 case OP_FCONV_TO_I2:
3053 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
3054 break;
3055 case OP_FCONV_TO_U2:
3056 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
3057 break;
3058 case OP_FCONV_TO_I4:
3059 case OP_FCONV_TO_I:
3060 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
3061 break;
3062 case OP_FCONV_TO_U4:
3063 case OP_FCONV_TO_U:
3064 code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
3065 break;
3066 case OP_FCONV_TO_I8:
3067 case OP_FCONV_TO_U8:
3068 g_assert_not_reached ();
3069 /* Implemented as helper calls */
3070 break;
3071 case OP_LCONV_TO_R_UN:
3072 g_assert_not_reached ();
3073 /* Implemented as helper calls */
3074 break;
3075 case OP_LCONV_TO_OVF_I: {
3076 ppc_mr (code, ins->dreg, ins->sreg1);
3077 /* FIXME: emit exception if needed */
3078 break;
3080 case OP_SQRT:
3081 ppc_fsqrtd (code, ins->dreg, ins->sreg1);
3082 break;
3083 case OP_FADD:
3084 ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
3085 break;
3086 case OP_FSUB:
3087 ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
3088 break;
3089 case OP_FMUL:
3090 ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
3091 break;
3092 case OP_FDIV:
3093 ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
3094 break;
3095 case OP_FNEG:
3096 ppc_fneg (code, ins->dreg, ins->sreg1);
3097 break;
3098 case OP_FREM:
3099 /* emulated */
3100 g_assert_not_reached ();
3101 break;
3102 case OP_FCOMPARE:
3103 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3104 break;
3105 case OP_FCEQ:
3106 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3107 ppc_li (code, ins->dreg, 0);
3108 ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
3109 ppc_li (code, ins->dreg, 1);
3110 break;
3111 case OP_FCLT:
3112 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3113 ppc_li (code, ins->dreg, 1);
3114 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3115 ppc_li (code, ins->dreg, 0);
3116 break;
3117 case OP_FCLT_UN:
3118 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3119 ppc_li (code, ins->dreg, 1);
3120 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3121 ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3122 ppc_li (code, ins->dreg, 0);
3123 break;
3124 case OP_FCGT:
3125 ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3126 ppc_li (code, ins->dreg, 1);
3127 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3128 ppc_li (code, ins->dreg, 0);
3129 break;
3130 case OP_FCGT_UN:
3131 ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3132 ppc_li (code, ins->dreg, 1);
3133 ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3134 ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3135 ppc_li (code, ins->dreg, 0);
3136 break;
3137 case OP_FBEQ:
3138 EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
3139 break;
3140 case OP_FBNE_UN:
3141 EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
3142 break;
3143 case OP_FBLT:
3144 EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
3145 break;
3146 case OP_FBLT_UN:
3147 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3148 EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
3149 break;
3150 case OP_FBGT:
3151 EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
3152 break;
3153 case OP_FBGT_UN:
3154 EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3155 EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
3156 break;
3157 case OP_FBGE:
3158 EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
3159 break;
3160 case OP_FBGE_UN:
3161 EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
3162 break;
3163 case OP_FBLE:
3164 EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
3165 break;
3166 case OP_FBLE_UN:
3167 EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
3168 break;
3169 case CEE_CKFINITE: {
3170 ppc_stfd (code, ins->sreg1, -8, ppc_sp);
3171 ppc_lwz (code, ppc_r11, -8, ppc_sp);
3172 ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
3173 ppc_addis (code, ppc_r11, ppc_r11, -32752);
3174 ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
3175 EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");
3176 break;
3178 default:
3179 g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3180 g_assert_not_reached ();
3183 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
3184 g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3185 mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3186 g_assert_not_reached ();
3189 cpos += max_len;
3191 last_ins = ins;
3192 last_offset = offset;
3194 ins = ins->next;
3197 cfg->code_len = code - cfg->native_code;
3200 void
3201 mono_arch_register_lowlevel_calls (void)
3205 #define patch_lis_ori(ip,val) do {\
3206 guint16 *__lis_ori = (guint16*)(ip); \
3207 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff; \
3208 __lis_ori [3] = ((guint32)(val)) & 0xffff; \
3209 } while (0)
3211 void
3212 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3214 MonoJumpInfo *patch_info;
3216 for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3217 unsigned char *ip = patch_info->ip.i + code;
3218 const unsigned char *target;
3220 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3222 switch (patch_info->type) {
3223 case MONO_PATCH_INFO_IP:
3224 patch_lis_ori (ip, ip);
3225 continue;
3226 case MONO_PATCH_INFO_METHOD_REL:
3227 g_assert_not_reached ();
3228 *((gpointer *)(ip)) = code + patch_info->data.offset;
3229 continue;
3230 case MONO_PATCH_INFO_SWITCH: {
3231 gpointer *table = (gpointer *)patch_info->data.target;
3232 int i;
3234 // FIXME: inspect code to get the register
3235 ppc_load (ip, ppc_r11, patch_info->data.target);
3236 //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3238 for (i = 0; i < patch_info->table_size; i++) {
3239 table [i] = (int)patch_info->data.table [i] + code;
3241 /* we put into the table the absolute address, no need for ppc_patch in this case */
3242 continue;
3244 case MONO_PATCH_INFO_METHODCONST:
3245 case MONO_PATCH_INFO_CLASS:
3246 case MONO_PATCH_INFO_IMAGE:
3247 case MONO_PATCH_INFO_FIELD:
3248 case MONO_PATCH_INFO_VTABLE:
3249 case MONO_PATCH_INFO_IID:
3250 case MONO_PATCH_INFO_SFLDA:
3251 case MONO_PATCH_INFO_LDSTR:
3252 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3253 case MONO_PATCH_INFO_LDTOKEN:
3254 /* from OP_AOTCONST : lis + ori */
3255 patch_lis_ori (ip, target);
3256 continue;
3257 case MONO_PATCH_INFO_R4:
3258 case MONO_PATCH_INFO_R8:
3259 g_assert_not_reached ();
3260 *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3261 continue;
3262 case MONO_PATCH_INFO_EXC_NAME:
3263 g_assert_not_reached ();
3264 *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3265 continue;
3266 case MONO_PATCH_INFO_BB_OVF:
3267 case MONO_PATCH_INFO_EXC_OVF:
3268 /* everything is dealt with at epilog output time */
3269 continue;
3270 default:
3271 break;
3273 ppc_patch (ip, target);
3278 mono_arch_max_epilog_size (MonoCompile *cfg)
3280 int max_epilog_size = 16 + 20*4;
3281 MonoJumpInfo *patch_info;
3283 if (cfg->method->save_lmf)
3284 max_epilog_size += 128;
3286 if (mono_jit_trace_calls != NULL)
3287 max_epilog_size += 50;
3289 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3290 max_epilog_size += 50;
3292 /* count the number of exception infos */
3295 * make sure we have enough space for exceptions
3296 * 24 is the simulated call to throw_exception_by_name
3298 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3299 if (patch_info->type == MONO_PATCH_INFO_EXC)
3300 max_epilog_size += 24;
3301 else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
3302 max_epilog_size += 12;
3303 else if (patch_info->type == MONO_PATCH_INFO_EXC_OVF)
3304 max_epilog_size += 12;
3307 return max_epilog_size;
3311 * Stack frame layout:
3313 * ------------------- sp
3314 * MonoLMF structure or saved registers
3315 * -------------------
3316 * spilled regs
3317 * -------------------
3318 * locals
3319 * -------------------
3320 * optional 8 bytes for tracing
3321 * -------------------
3322 * param area size is cfg->param_area
3323 * -------------------
3324 * linkage area size is PPC_STACK_PARAM_OFFSET
3325 * ------------------- sp
3326 * red zone
3328 guint8 *
3329 mono_arch_emit_prolog (MonoCompile *cfg)
3331 MonoMethod *method = cfg->method;
3332 MonoBasicBlock *bb;
3333 MonoMethodSignature *sig;
3334 MonoInst *inst;
3335 int alloc_size, pos, max_offset, i;
3336 guint8 *code;
3337 CallInfo *cinfo;
3338 int tracing = 0;
3339 int lmf_offset = 0;
3341 if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3342 tracing = 1;
3344 cfg->code_size = 256;
3345 code = cfg->native_code = g_malloc (cfg->code_size);
3347 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3348 ppc_mflr (code, ppc_r0);
3349 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3351 if (cfg->max_ireg >= 29)
3352 cfg->used_int_regs |= USE_EXTRA_TEMPS;
3354 alloc_size = cfg->stack_offset;
3355 pos = 0;
3357 if (!method->save_lmf) {
3358 /*for (i = 31; i >= 14; --i) {
3359 if (cfg->used_float_regs & (1 << i)) {
3360 pos += sizeof (gdouble);
3361 ppc_stfd (code, i, -pos, ppc_sp);
3364 for (i = 31; i >= 13; --i) {
3365 if (cfg->used_int_regs & (1 << i)) {
3366 pos += sizeof (gulong);
3367 ppc_stw (code, i, -pos, ppc_sp);
3370 } else {
3371 int ofs;
3372 pos += sizeof (MonoLMF);
3373 lmf_offset = pos;
3374 ofs = -pos + G_STRUCT_OFFSET(MonoLMF, iregs);
3375 ppc_stmw (code, ppc_r13, ppc_r1, ofs);
3376 for (i = 14; i < 32; i++) {
3377 ppc_stfd (code, i, (-pos + G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble))), ppc_r1);
3380 alloc_size += pos;
3381 // align to PPC_STACK_ALIGNMENT bytes
3382 if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
3383 alloc_size += PPC_STACK_ALIGNMENT - 1;
3384 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
3387 cfg->stack_usage = alloc_size;
3388 g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
3389 if (alloc_size) {
3390 if (ppc_is_imm16 (-alloc_size)) {
3391 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3392 } else {
3393 ppc_load (code, ppc_r11, -alloc_size);
3394 ppc_stwux (code, ppc_sp, ppc_sp, ppc_r11);
3397 if (cfg->frame_reg != ppc_sp)
3398 ppc_mr (code, cfg->frame_reg, ppc_sp);
3400 /* compute max_offset in order to use short forward jumps
3401 * we always do it on ppc because the immediate displacement
3402 * for jumps is too small
3404 max_offset = 0;
3405 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3406 MonoInst *ins = bb->code;
3407 bb->max_offset = max_offset;
3409 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3410 max_offset += 6;
3412 while (ins) {
3413 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3414 ins = ins->next;
3418 /* load arguments allocated to register from the stack */
3419 sig = method->signature;
3420 pos = 0;
3422 cinfo = calculate_sizes (sig, sig->pinvoke);
3424 if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3425 ArgInfo *ainfo = &cinfo->ret;
3426 inst = cfg->ret;
3427 if (ppc_is_imm16 (inst->inst_offset)) {
3428 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3429 } else {
3430 ppc_load (code, ppc_r11, inst->inst_offset);
3431 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3434 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3435 ArgInfo *ainfo = cinfo->args + i;
3436 inst = cfg->varinfo [pos];
3438 if (cfg->verbose_level > 2)
3439 g_print ("Saving argument %d (type: %d)\n", i, ainfo->regtype);
3440 if (inst->opcode == OP_REGVAR) {
3441 if (ainfo->regtype == RegTypeGeneral)
3442 ppc_mr (code, inst->dreg, ainfo->reg);
3443 else if (ainfo->regtype == RegTypeFP)
3444 ppc_fmr (code, inst->dreg, ainfo->reg);
3445 else if (ainfo->regtype == RegTypeBase) {
3446 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3447 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3448 } else
3449 g_assert_not_reached ();
3451 if (cfg->verbose_level > 2)
3452 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3453 } else {
3454 /* the argument should be put on the stack: FIXME handle size != word */
3455 if (ainfo->regtype == RegTypeGeneral) {
3456 switch (ainfo->size) {
3457 case 1:
3458 if (ppc_is_imm16 (inst->inst_offset)) {
3459 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3460 } else {
3461 ppc_load (code, ppc_r11, inst->inst_offset);
3462 ppc_stbx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3464 break;
3465 case 2:
3466 if (ppc_is_imm16 (inst->inst_offset)) {
3467 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3468 } else {
3469 ppc_load (code, ppc_r11, inst->inst_offset);
3470 ppc_sthx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3472 break;
3473 case 8:
3474 if (ppc_is_imm16 (inst->inst_offset + 4)) {
3475 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3476 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3477 } else {
3478 ppc_load (code, ppc_r11, inst->inst_offset);
3479 ppc_add (code, ppc_r11, ppc_r11, inst->inst_basereg);
3480 ppc_stw (code, ainfo->reg, 0, ppc_r11);
3481 ppc_stw (code, ainfo->reg + 1, 4, ppc_r11);
3483 break;
3484 default:
3485 if (ppc_is_imm16 (inst->inst_offset)) {
3486 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3487 } else {
3488 ppc_load (code, ppc_r11, inst->inst_offset);
3489 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3491 break;
3493 } else if (ainfo->regtype == RegTypeBase) {
3494 /* load the previous stack pointer in r11 */
3495 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3496 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3497 switch (ainfo->size) {
3498 case 1:
3499 if (ppc_is_imm16 (inst->inst_offset)) {
3500 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3501 } else {
3502 ppc_load (code, ppc_r11, inst->inst_offset);
3503 ppc_stbx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3505 break;
3506 case 2:
3507 if (ppc_is_imm16 (inst->inst_offset)) {
3508 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3509 } else {
3510 ppc_load (code, ppc_r11, inst->inst_offset);
3511 ppc_sthx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3513 break;
3514 case 8:
3515 if (ppc_is_imm16 (inst->inst_offset + 4)) {
3516 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3517 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3518 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3519 } else {
3520 /* FIXME */
3521 g_assert_not_reached ();
3523 break;
3524 default:
3525 if (ppc_is_imm16 (inst->inst_offset)) {
3526 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3527 } else {
3528 ppc_load (code, ppc_r11, inst->inst_offset);
3529 ppc_stwx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3531 break;
3533 } else if (ainfo->regtype == RegTypeFP) {
3534 g_assert (ppc_is_imm16 (inst->inst_offset));
3535 if (ainfo->size == 8)
3536 ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3537 else if (ainfo->size == 4)
3538 ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3539 else
3540 g_assert_not_reached ();
3541 } else if (ainfo->regtype == RegTypeStructByVal) {
3542 int doffset = inst->inst_offset;
3543 int soffset = 0;
3544 int cur_reg;
3545 int size = 0;
3546 g_assert (ppc_is_imm16 (inst->inst_offset));
3547 g_assert (ppc_is_imm16 (inst->inst_offset + ainfo->size * sizeof (gpointer)));
3548 if (inst->inst_vtype->data.klass)
3549 size = mono_class_native_size (inst->inst_vtype->data.klass, NULL);
3550 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3552 Darwin handles 1 and 2 byte structs specially by loading h/b into the arg
3553 register. Should this case include linux/ppc?
3555 #if __APPLE__
3556 if (size == 2)
3557 ppc_sth (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3558 else if (size == 1)
3559 ppc_stb (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3560 else
3561 #endif
3562 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3563 soffset += sizeof (gpointer);
3564 doffset += sizeof (gpointer);
3566 if (ainfo->vtsize) {
3567 /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3568 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3569 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3570 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3572 } else if (ainfo->regtype == RegTypeStructByAddr) {
3573 g_assert (ppc_is_imm16 (inst->inst_offset));
3574 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3575 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3576 } else
3577 g_assert_not_reached ();
3579 pos++;
3582 if (method->save_lmf) {
3584 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
3585 (gpointer)"mono_get_lmf_addr");
3586 ppc_bl (code, 0);
3587 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3588 /* lmf_offset is the offset from the previous stack pointer,
3589 * alloc_size is the total stack space allocated, so the offset
3590 * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
3591 * The pointer to the struct is put in ppc_r11 (new_lmf).
3592 * The callee-saved registers are already in the MonoLMF structure
3594 ppc_addi (code, ppc_r11, ppc_sp, alloc_size - lmf_offset);
3595 /* ppc_r3 is the result from mono_get_lmf_addr () */
3596 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3597 /* new_lmf->previous_lmf = *lmf_addr */
3598 ppc_lwz (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3599 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3600 /* *(lmf_addr) = r11 */
3601 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3602 /* save method info */
3603 ppc_load (code, ppc_r0, method);
3604 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3605 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3606 /* save the current IP */
3607 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3608 ppc_load (code, ppc_r0, 0x01010101);
3609 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3612 if (tracing)
3613 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3615 cfg->code_len = code - cfg->native_code;
3616 g_free (cinfo);
3618 return code;
3621 void
3622 mono_arch_emit_epilog (MonoCompile *cfg)
3624 MonoJumpInfo *patch_info;
3625 MonoMethod *method = cfg->method;
3626 int pos, i;
3627 guint8 *code;
3630 * Keep in sync with CEE_JMP
3632 code = cfg->native_code + cfg->code_len;
3634 if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3635 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3637 pos = 0;
3639 if (method->save_lmf) {
3640 int lmf_offset;
3641 pos += sizeof (MonoLMF);
3642 lmf_offset = pos;
3643 /* save the frame reg in r8 */
3644 ppc_mr (code, ppc_r8, cfg->frame_reg);
3645 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage - lmf_offset);
3646 /* r5 = previous_lmf */
3647 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3648 /* r6 = lmf_addr */
3649 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3650 /* *(lmf_addr) = previous_lmf */
3651 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3652 /* FIXME: speedup: there is no actual need to restore the registers if
3653 * we didn't actually change them (idea from Zoltan).
3655 /* restore iregs */
3656 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3657 /* restore fregs */
3658 /*for (i = 14; i < 32; i++) {
3659 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3661 g_assert (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET));
3662 /* use the saved copy of the frame reg in r8 */
3663 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3664 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, ppc_r8);
3665 ppc_mtlr (code, ppc_r0);
3667 ppc_addic (code, ppc_sp, ppc_r8, cfg->stack_usage);
3668 } else {
3669 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3670 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
3671 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3672 } else {
3673 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
3674 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
3676 ppc_mtlr (code, ppc_r0);
3678 if (ppc_is_imm16 (cfg->stack_usage)) {
3679 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3680 } else {
3681 ppc_load (code, ppc_r11, cfg->stack_usage);
3682 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
3685 /*for (i = 31; i >= 14; --i) {
3686 if (cfg->used_float_regs & (1 << i)) {
3687 pos += sizeof (double);
3688 ppc_lfd (code, i, -pos, ppc_sp);
3691 for (i = 31; i >= 13; --i) {
3692 if (cfg->used_int_regs & (1 << i)) {
3693 pos += sizeof (gulong);
3694 ppc_lwz (code, i, -pos, ppc_sp);
3698 ppc_blr (code);
3700 /* add code to raise exceptions */
3701 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3702 switch (patch_info->type) {
3703 case MONO_PATCH_INFO_BB_OVF: {
3704 MonoOvfJump *ovfj = patch_info->data.target;
3705 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3706 /* patch the initial jump */
3707 ppc_patch (ip, code);
3708 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3709 ppc_b (code, 0);
3710 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3711 /* jump back to the true target */
3712 ppc_b (code, 0);
3713 ip = ovfj->bb->native_offset + cfg->native_code;
3714 ppc_patch (code - 4, ip);
3715 break;
3717 case MONO_PATCH_INFO_EXC_OVF: {
3718 MonoOvfJump *ovfj = patch_info->data.target;
3719 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3720 /* patch the initial jump */
3721 ppc_patch (ip, code);
3722 ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3723 ppc_b (code, 0);
3724 ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3725 /* jump back to the true target */
3726 ppc_b (code, 0);
3727 ip = (char*)ovfj->ip + 4;
3728 ppc_patch (code - 4, ip);
3729 break;
3731 case MONO_PATCH_INFO_EXC: {
3732 unsigned char *ip = patch_info->ip.i + cfg->native_code;
3733 ppc_patch (ip, code);
3734 /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3735 ppc_load (code, ppc_r3, patch_info->data.target);
3736 /* simulate a call from ip */
3737 ppc_load (code, ppc_r0, ip + 4);
3738 ppc_mtlr (code, ppc_r0);
3739 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3740 patch_info->data.name = "mono_arch_throw_exception_by_name";
3741 patch_info->ip.i = code - cfg->native_code;
3742 ppc_b (code, 0);
3743 break;
3745 default:
3746 /* do nothing */
3747 break;
3751 cfg->code_len = code - cfg->native_code;
3753 g_assert (cfg->code_len < cfg->code_size);
3757 void
3758 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3762 void
3763 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3767 void
3768 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3770 int this_dreg = ppc_r3;
3772 if (vt_reg != -1)
3773 this_dreg = ppc_r4;
3775 /* add the this argument */
3776 if (this_reg != -1) {
3777 MonoInst *this;
3778 MONO_INST_NEW (cfg, this, OP_SETREG);
3779 this->type = this_type;
3780 this->sreg1 = this_reg;
3781 this->dreg = this_dreg;
3782 mono_bblock_add_inst (cfg->cbb, this);
3785 if (vt_reg != -1) {
3786 MonoInst *vtarg;
3787 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3788 vtarg->type = STACK_MP;
3789 vtarg->sreg1 = vt_reg;
3790 vtarg->dreg = ppc_r3;
3791 mono_bblock_add_inst (cfg->cbb, vtarg);
3795 gint
3796 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3798 /* optional instruction, need to detect it
3799 if (cmethod->klass == mono_defaults.math_class) {
3800 if (strcmp (cmethod->name, "Sqrt") == 0)
3801 return OP_SQRT;
3803 return -1;
3807 gboolean
3808 mono_arch_print_tree (MonoInst *tree, int arity)
3810 return 0;
3813 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
3815 return NULL;
3818 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
3820 return NULL;