Don't run test-318 with gmcs.
[mono.git] / mono / mini / mini-ppc.c
1 /*
2  * mini-ppc.c: PowerPC backend for the Mono code generator
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *
8  * (C) 2003 Ximian, Inc.
9  */
10 #include "mini.h"
11 #include <string.h>
12
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/debug-helpers.h>
15
16 #include "mini-ppc.h"
17 #include "inssel.h"
18 #include "cpu-g4.h"
19 #include "trace.h"
20
21 int mono_exc_esp_offset = 0;
22
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"
33         };
34         if (reg >= 0 && reg < 32)
35                 return rnames [reg];
36         return "unknown";
37 }
38
39 /* this function overwrites r0 */
40 static guint8*
41 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
42 {
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;
50         }
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;
57         }
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;
64         }
65         return code;
66 }
67
68 /*
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
73  *
74  * Gathers information on parameters such as size, alignment and
75  * padding. arg_info should be large enought to hold param_count + 1 entries. 
76  *
77  * Returns the size of the activation frame.
78  */
79 int
80 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
81 {
82         int k, frame_size = 0;
83         int size, align, pad;
84         int offset = 8;
85
86         if (MONO_TYPE_ISSTRUCT (csig->ret)) { 
87                 frame_size += sizeof (gpointer);
88                 offset += 4;
89         }
90
91         arg_info [0].offset = offset;
92
93         if (csig->hasthis) {
94                 frame_size += sizeof (gpointer);
95                 offset += 4;
96         }
97
98         arg_info [0].size = frame_size;
99
100         for (k = 0; k < param_count; k++) {
101                 
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);
106
107                 /* ignore alignment for now */
108                 align = 1;
109
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;
118         }
119
120         align = MONO_ARCH_FRAME_ALIGNMENT;
121         frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
122         arg_info [k].pad = pad;
123
124         return frame_size;
125 }
126
127 /*
128  * Initialize the cpu to execute managed code.
129  */
130 void
131 mono_arch_cpu_init (void)
132 {
133 }
134
135 /*
136  * This function returns the optimizations supported on this cpu.
137  */
138 guint32
139 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
140 {
141         guint32 opts = 0;
142
143         /* no ppc-specific optimizations yet */
144         *exclude_mask = MONO_OPT_INLINE;
145         return opts;
146 }
147
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;
169         }
170         return FALSE;
171 }
172
173 GList *
174 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
175 {
176         GList *vars = NULL;
177         int i;
178
179         for (i = 0; i < cfg->num_varinfo; i++) {
180                 MonoInst *ins = cfg->varinfo [i];
181                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
182
183                 /* unused vars */
184                 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
185                         continue;
186
187                 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
188                         continue;
189
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);
195                 }
196         }
197
198         return vars;
199 }
200
201 #define USE_EXTRA_TEMPS ((1<<30) | (1<<29))
202 //#define USE_EXTRA_TEMPS 0
203
204 GList *
205 mono_arch_get_global_int_regs (MonoCompile *cfg)
206 {
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));
216
217         return regs;
218 }
219
220 /*
221  * mono_arch_regalloc_cost:
222  *
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.
226  */
227 guint32
228 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
229 {
230         /* FIXME: */
231         return 2;
232 }
233
234 // code from ppc/tramp.c, try to keep in sync
235 #define MIN_CACHE_LINE 8
236
237 void
238 mono_arch_flush_icache (guint8 *code, gint size)
239 {
240         guint i;
241         guint8 *p;
242
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");
246         }
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");
251         }
252         asm ("sync");
253         asm ("isync");
254 }
255
256 #define NOT_IMPLEMENTED(x) \
257                 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
258
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
267
268 enum {
269         RegTypeGeneral,
270         RegTypeBase,
271         RegTypeFP,
272         RegTypeStructByVal,
273         RegTypeStructByAddr
274 };
275
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;
283
284 typedef struct {
285         int nargs;
286         guint32 stack_usage;
287         guint32 struct_ret;
288         ArgInfo ret;
289         ArgInfo args [1];
290 } CallInfo;
291
292 #define DEBUG(a)
293
294 static void inline
295 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
296 {
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;
306                 }
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;
323                 }
324                 (*gr) ++;
325         }
326         (*gr) ++;
327 }
328
329 static CallInfo*
330 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
331 {
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);
337
338         fr = PPC_FIRST_FPARG_REG;
339         gr = PPC_FIRST_ARG_REG;
340
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;
345         }
346
347         n = 0;
348         if (sig->hasthis) {
349                 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
350                 n++;
351         }
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;
360                 }
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;
402                         }
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
410                         {
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;
427                                 }
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);
431                         }
432 #else
433                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
434                         cinfo->args [n].regtype = RegTypeStructByAddr;
435 #endif
436                         n++;
437                         break;
438                 }
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
443                         {
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;
456                                 }
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);
460                         }
461 #else
462                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
463                         cinfo->args [n].regtype = RegTypeStructByAddr;
464 #endif
465                         n++;
466                         break;
467                 }
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;
476
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;
489                         }
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;
506                         }
507                         n++;
508                         break;
509                 default:
510                         g_error ("Can't trampoline 0x%x", sig->params [i]->type);
511                 }
512         }
513
514         {
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;
550                         }
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);
557                 }
558         }
559
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;
563
564         cinfo->stack_usage = stack_size;
565         return cinfo;
566 }
567
568
569 /*
570  * Set var information according to the calling convention. ppc version.
571  * The locals var stuff should most likely be split in another method.
572  */
573 void
574 mono_arch_allocate_vars (MonoCompile *m)
575 {
576         MonoMethodSignature *sig;
577         MonoMethodHeader *header;
578         MonoInst *inst;
579         int i, offset, size, align, curinst;
580         int frame_reg = ppc_sp;
581
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.
587          */
588         if (m->flags & MONO_CFG_HAS_VARARGS)
589                 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
590
591         header = mono_method_get_header (m->method);
592
593         /* 
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.).
601          */ 
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;
607         }
608
609         sig = m->method->signature;
610         
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;
625                 }
626         }
627         /* local vars are at a positive offset from the stack pointer */
628         /* 
629          * also note that if the function uses alloca, we use ppc_r31
630          * to point at the local variables.
631          */
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);
636
637         /* add parameter area size for called functions */
638         offset += m->param_area;
639         offset += 16 - 1;
640         offset &= ~(16 - 1);
641
642         /* allow room to save the return value */
643         if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
644                 offset += 8;
645
646         /* the MonoLMF structure is stored just below the stack pointer */
647
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.
653          */
654         /* reserve space to store the esp */
655         offset += sizeof (gpointer);
656
657         /* this is a global constant */
658         mono_exc_esp_offset = offset;
659 #endif
660
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);
669         }
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;
675
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);
682
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);
690         }
691
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);
702                 }
703                 curinst++;
704         }
705
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;
716                 }
717                 curinst++;
718         }
719
720         /* align the offset to 16 bytes */
721         offset += 16 - 1;
722         offset &= ~(16 - 1);
723
724         /* change sign? */
725         m->stack_offset = offset;
726
727 }
728
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 
731  */
732
733 /* 
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?
739  */
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;
747
748         sig = call->signature;
749         n = sig->param_count + sig->hasthis;
750         
751         cinfo = calculate_sizes (sig, sig->pinvoke);
752         if (cinfo->struct_ret)
753                 call->used_iregs |= 1 << cinfo->struct_ret;
754
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);
784                                 }
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;*/
803                                 }
804                         } else {
805                                 g_assert_not_reached ();
806                         }
807                 }
808         }
809         /*
810          * Reverse the call->out_args list.
811          */
812         {
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;
819                 }
820                 call->out_args = prev;
821         }
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;
825         /* 
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
828          */
829
830         g_free (cinfo);
831         return call;
832 }
833
834 /*
835  * Allow tracing to work with this interface (with an optional argument)
836  */
837
838 /*
839  * This may be needed on some archs or for debugging support.
840  */
841 void
842 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
843 {
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 */
848 }
849
850 void*
851 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
852 {
853         guchar *code = p;
854
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;
861 }
862
863 enum {
864         SAVE_NONE,
865         SAVE_STRUCT,
866         SAVE_ONE,
867         SAVE_TWO,
868         SAVE_FP
869 };
870
871 void*
872 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
873 {
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;
881         
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;
903                 }
904                 save_mode = SAVE_STRUCT;
905                 break;
906         default:
907                 save_mode = SAVE_ONE;
908                 break;
909         }
910
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);
918                 }
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);
924                 }
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);
933                 }
934                 break;
935         case SAVE_STRUCT:
936                 if (enable_arguments) {
937                         /* FIXME: get the actual address  */
938                         ppc_mr (code, ppc_r4, ppc_r3);
939                 }
940                 break;
941         case SAVE_NONE:
942         default:
943                 break;
944         }
945
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);
950
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;
965         }
966
967         return code;
968 }
969 /*
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.
976  */
977 typedef struct {
978         MonoBasicBlock *bb;
979         void *ip;
980         guint16 b0_cond;
981         guint16 b1_cond;
982 } MonoOvfJump;
983
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);   \
991         } \
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);   \
1008                 }       \
1009         } \
1010 }
1011
1012 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_b0_table [(cond)], branch_b1_table [(cond)])
1013
1014 /* emit an exception if condition is fail
1015  *
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
1018  */
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);   \
1036                 }       \
1037         } while (0); 
1038
1039 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_b0_table [(cond)], branch_b1_table [(cond)], (exc_name))
1040
1041 static void
1042 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1043 {
1044         MonoInst *ins, *last_ins = NULL;
1045         ins = bb->code;
1046
1047         while (ins) {
1048
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;
1059                                 }
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;
1065                                 }
1066                         }
1067                         break;
1068                 case OP_LOAD_MEMBASE:
1069                 case OP_LOADI4_MEMBASE:
1070                         /* 
1071                          * OP_STORE_MEMBASE_REG reg, offset(basereg) 
1072                          * OP_LOAD_MEMBASE offset(basereg), reg
1073                          */
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;
1086                                 }
1087
1088                         /* 
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
1095                          */
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) {
1101
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;
1109                                 }
1110
1111                                 //g_assert_not_reached ();
1112
1113 #if 0
1114                         /* 
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
1120                          */
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
1130                         }
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;
1145                                 }
1146                         }
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;
1161                                 }
1162                         }
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;
1169                         /* 
1170                          * OP_MOVE reg, reg 
1171                          */
1172                         if (ins->dreg == ins->sreg1) {
1173                                 if (last_ins)
1174                                         last_ins->next = ins->next;                             
1175                                 ins = ins->next;
1176                                 continue;
1177                         }
1178                         /* 
1179                          * OP_MOVE sreg, dreg 
1180                          * OP_MOVE dreg, sreg
1181                          */
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;
1188                         }
1189                         break;
1190                 }
1191                 last_ins = ins;
1192                 ins = ins->next;
1193         }
1194         bb->last_ins = last_ins;
1195 }
1196
1197 /* 
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:
1210  */
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, 
1218         
1219         PPC_BR_FALSE, 
1220         PPC_BR_FALSE, 
1221         PPC_BR_TRUE, 
1222         PPC_BR_FALSE,
1223         PPC_BR_TRUE
1224 };
1225
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, 
1233         
1234         PPC_BR_EQ, 
1235         PPC_BR_LT, 
1236         PPC_BR_GT, 
1237         PPC_BR_GT,
1238         PPC_BR_LT 
1239 };
1240
1241 /*
1242  * returns the offset used by spillvar. It allocates a new
1243  * spill variable if necessary. 
1244  */
1245 static int
1246 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1247 {
1248         MonoSpillInfo **si, *info;
1249         int i = 0;
1250
1251         si = &cfg->spill_info; 
1252         
1253         while (i <= spillvar) {
1254
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);
1260                 }
1261
1262                 if (i == spillvar)
1263                         return (*si)->offset;
1264
1265                 i++;
1266                 si = &(*si)->next;
1267         }
1268
1269         g_assert_not_reached ();
1270         return 0;
1271 }
1272
1273 static int
1274 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1275 {
1276         MonoSpillInfo **si, *info;
1277         int i = 0;
1278
1279         si = &cfg->spill_info_float; 
1280         
1281         while (i <= spillvar) {
1282
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);
1290                 }
1291
1292                 if (i == spillvar)
1293                         return (*si)->offset;
1294
1295                 i++;
1296                 si = &(*si)->next;
1297         }
1298
1299         g_assert_not_reached ();
1300         return 0;
1301 }
1302
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)
1309
1310 #define reg_is_freeable(r) (PPC_CALLER_REGS & 1 << (r))
1311 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 13)
1312
1313 typedef struct {
1314         int born_in;
1315         int killed_in;
1316         int last_use;
1317         int prev_use;
1318 } RegTrack;
1319
1320 static const char*const * ins_spec = ppcg4;
1321
1322 static void
1323 print_ins (int i, MonoInst *ins)
1324 {
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));
1332         }
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));
1338         }
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));
1344         }
1345         if (spec [MONO_INST_CLOB])
1346                 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1347         g_print ("\n");
1348 }
1349
1350 static void
1351 print_regtrack (RegTrack *t, int num)
1352 {
1353         int i;
1354         char buf [32];
1355         const char *r;
1356         
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);
1366         }
1367 }
1368
1369 typedef struct InstList InstList;
1370
1371 struct InstList {
1372         InstList *prev;
1373         InstList *next;
1374         MonoInst *data;
1375 };
1376
1377 static inline InstList*
1378 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1379 {
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;
1387 }
1388
1389 /*
1390  * Force the spilling of the variable in the symbolic register 'reg'.
1391  */
1392 static int
1393 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1394 {
1395         MonoInst *load;
1396         int i, sel, spill;
1397         
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;
1413         }
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);
1419
1420         return sel;
1421 }
1422
1423 static int
1424 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1425 {
1426         MonoInst *load;
1427         int i, sel, spill;
1428
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)));
1437         }
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));
1444         }
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)));
1448         }
1449
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;
1459                 }
1460         }
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;
1473         }
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);
1479         
1480         return sel;
1481 }
1482
1483 static int
1484 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1485 {
1486         MonoInst *load;
1487         int i, sel, spill;
1488
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)));
1497         }
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));
1504         }
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)));
1508         }
1509
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;
1519                 }
1520         }
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;
1533         }
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);
1539         
1540         return sel;
1541 }
1542
1543 static MonoInst*
1544 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1545 {
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;
1553         }
1554         DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1555         return copy;
1556 }
1557
1558 static MonoInst*
1559 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1560 {
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;
1568         }
1569         DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1570         return copy;
1571 }
1572
1573 static MonoInst*
1574 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1575 {
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;
1584         }
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;
1587 }
1588
1589 static MonoInst*
1590 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1591 {
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;
1600         }
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;
1603 }
1604
1605 static void
1606 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1607 {
1608         MonoInst *prev;
1609         g_assert (item->next);
1610         prev = item->next->data;
1611
1612         while (prev->next != ins)
1613                 prev = prev->next;
1614         to_insert->next = ins;
1615         prev->next = to_insert;
1616         /* 
1617          * needed otherwise in the next instruction we can add an ins to the 
1618          * end and that would get past this instruction.
1619          */
1620         item->data = to_insert; 
1621 }
1622
1623 static int
1624 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1625 {
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;
1632                 }
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);
1640         }
1641         cfg->rs->isymbolic [val] = sym_reg;
1642         return val;
1643 }
1644
1645 /*
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.
1651  */
1652 void
1653 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1654 {
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;
1664
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;
1674
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;
1685                 }*/
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;
1695                 }
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;
1705                 }
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;
1723                         }
1724                 } else {
1725                         ins->dreg = -1;
1726                 }
1727                 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1728                 ++i;
1729                 ins = ins->next;
1730         }
1731
1732         cur_iregs = PPC_CALLER_REGS;
1733         cur_fregs = PPC_CALLER_FREGS;
1734
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.
1763                          */
1764                 }
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;
1777                                         }
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);
1784                                 }
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);
1791                                 }
1792                         } else {
1793                                 prev_dreg = -1;
1794                         }
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);
1798                         }
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;
1807                                 }
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);
1814                         }
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;
1826                                         }
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);
1833                                 }
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);
1850                                         }
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);
1858                                         }
1859                                 }
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);
1863                                 }
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);
1867                         }
1868                 } else {
1869                         prev_dreg = -1;
1870                 }
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);
1877                 }
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;
1888                                         }
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);
1898                                         }
1899                                 }
1900                                 rs->fsymbolic [val] = prev_sreg1;
1901                                 ins->sreg1 = val;
1902                         } else {
1903                                 prev_sreg1 = -1;
1904                         }
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;
1913                                 }
1914                                 if (0 && ins->opcode == OP_MOVE) {
1915                                         /* 
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.
1922                                          */
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));
1934                                 }
1935                                 if (spill) {
1936                                         MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1937                                         insert_before_ins (ins, tmp, store);
1938                                 }
1939                         }
1940                         rs->isymbolic [val] = prev_sreg1;
1941                         ins->sreg1 = val;
1942                 } else {
1943                         prev_sreg1 = -1;
1944                 }
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;
1955                                         }
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);
1963                                 }
1964                                 rs->fsymbolic [val] = prev_sreg2;
1965                                 ins->sreg2 = val;
1966                         } else {
1967                                 prev_sreg2 = -1;
1968                         }
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;
1977                                 }
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);
1985                         }
1986                         rs->isymbolic [val] = prev_sreg2;
1987                         ins->sreg2 = val;
1988                 } else {
1989                         prev_sreg2 = -1;
1990                 }
1991
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));
1999                                 }
2000                         }
2001                 }
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);
2005                 }
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);
2009                 }*/
2010                 
2011                 //DEBUG (print_ins (i, ins));
2012                 tmp = tmp->next;
2013         }
2014         cfg->max_ireg = MAX (cfg->max_ireg, rs->max_ireg);
2015 }
2016
2017 static guchar*
2018 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2019 {
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);
2034         }
2035         return code;
2036 }
2037
2038 static unsigned char*
2039 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2040 {
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;
2049                 }
2050                 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2051                         x86_push_reg (code, X86_ECX);
2052                         offset += 4;
2053                 }
2054                 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2055                         x86_push_reg (code, X86_EDI);
2056                         offset += 4;
2057                 }
2058                 
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);
2063                                 
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);
2068                 
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);
2075         }
2076 #endif
2077         return code;
2078 }
2079
2080 typedef struct {
2081         guchar *code;
2082         guchar *target;
2083         int absolute;
2084         int found;
2085 } PatchData;
2086
2087 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2088
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;
2099
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;
2105
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);
2109
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);
2127
2128                                 ppc_patch (pdata->code, (guchar*)thunks);
2129                                 mono_arch_flush_icache (pdata->code, 4);
2130                                 pdata->found = 1;
2131                                 return 1;
2132                         }
2133                         /* skip 16 bytes, the size of the thunk */
2134                         thunks += 4;
2135                         count++;
2136                 }
2137                 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2138         }
2139         return 0;
2140 }
2141
2142 static void
2143 handle_thunk (int absolute, guchar *code, guchar *target) {
2144         MonoDomain *domain = mono_domain_get ();
2145         PatchData pdata;
2146
2147         pdata.code = code;
2148         pdata.target = target;
2149         pdata.absolute = absolute;
2150         pdata.found = 0;
2151
2152         mono_domain_lock (domain);
2153         mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2154
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);
2159         }
2160         mono_domain_unlock (domain);
2161
2162         if (pdata.found != 1)
2163                 g_print ("thunk failed for %p from %p\n", target, code);
2164         g_assert (pdata.found == 1);
2165 }
2166
2167 void
2168 ppc_patch (guchar *code, guchar *target)
2169 {
2170         guint32 ins = *(guint32*)code;
2171         guint32 prim = ins >> 26;
2172         guint32 ovf;
2173
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;
2183                         }
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;
2190                         }
2191                 }
2192                 
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;
2198                         }
2199                 } else {
2200                         if ((glong)target >= -33554432){
2201                                 ins = (18 << 26) | (((guint32)target) & ~0xfc000000) | (ins & 1) | 2;
2202                                 *(guint32*)code = ins;
2203                                 return;
2204                         }
2205                 }
2206
2207                 handle_thunk (TRUE, code, target);
2208                 return;
2209
2210                 g_assert_not_reached ();
2211         }
2212         
2213         
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;
2233                 }
2234                 *(guint32*)code = ins;
2235         } else {
2236                 g_assert_not_reached ();
2237         }
2238 //      g_print ("patched with 0x%08x\n", ins);
2239 }
2240
2241 void
2242 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2243 {
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;
2251
2252         if (cfg->opt & MONO_OPT_PEEPHOLE)
2253                 peephole_pass (cfg, bb);
2254
2255         /* we don't align basic blocks of loops on ppc */
2256
2257         if (cfg->verbose_level > 2)
2258                 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2259
2260         cpos = bb->max_offset;
2261
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); 
2271         }
2272
2273         ins = bb->code;
2274         while (ins) {
2275                 offset = code - cfg->native_code;
2276
2277                 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2278
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;
2283                 }
2284         //      if (ins->cil_code)
2285         //              g_print ("cil code\n");
2286                 mono_debug_record_line_number (cfg, ins, offset);
2287
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);
2304                         }
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);
2313                         }
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);
2323                         }
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);
2331                         }
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);
2339                         }
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);
2348                         }
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);
2369                         }
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);
2378                         }
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);
2388                         }
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);
2396                         }
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);
2429                                 }
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);
2436                                 }
2437                         }
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);
2460                         }
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);
2468                         }
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
2476                          */
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
2484                          */
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
2492                          */
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
2500                          */
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
2508                          */
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
2516                          */
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
2524                          */
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
2532                          */
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);
2558                         }
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);
2583                         }
2584                         break;
2585                 case CEE_DIV:
2586                          /* XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2587                          */
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);
2641                         }
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);
2654                         }
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);
2696                         }
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]
2701                          */
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
2711                          */
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;
2737                         }
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;
2743                 }
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;
2753                         
2754                         /*
2755                          * Keep in sync with mono_arch_emit_epilog
2756                          */
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);
2764                                 }
2765                                 ppc_mtlr (code, ppc_r0);
2766                         }
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);
2772                         }
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);
2778                                         }
2779                                 }*/
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);
2784                                         }
2785                                 }
2786                         } else {
2787                                 /* FIXME restore from MonoLMF: though this can't happen yet */
2788                         }
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;
2792                 }
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;
2844                 }
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;
2855                 }
2856                 case OP_RETHROW: {
2857                         //ppc_break (code);
2858                         ppc_mr (code, ppc_r3, ins->sreg1);
2859                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
2860                                              (gpointer)"mono_arch_rethrow_exception");
2861                         ppc_bl (code, 0);
2862                         break;
2863                 }
2864                 case OP_START_HANDLER:
2865                         ppc_mflr (code, ppc_r0);
2866                         if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2867                                 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2868                         } else {
2869                                 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2870                                 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_left->inst_basereg);
2871                         }
2872                         break;
2873                 case OP_ENDFILTER:
2874                         if (ins->sreg1 != ppc_r3)
2875                                 ppc_mr (code, ppc_r3, ins->sreg1);
2876                         if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2877                                 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2878                         } else {
2879                                 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2880                                 ppc_lwzx (code, ppc_r0, ins->inst_left->inst_basereg, ppc_r11);
2881                         }
2882                         ppc_mtlr (code, ppc_r0);
2883                         ppc_blr (code);
2884                         break;
2885                 case CEE_ENDFINALLY:
2886                         ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2887                         ppc_mtlr (code, ppc_r0);
2888                         ppc_blr (code);
2889                         break;
2890                 case OP_CALL_HANDLER: 
2891                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2892                         ppc_bl (code, 0);
2893                         break;
2894                 case OP_LABEL:
2895                         ins->inst_c0 = code - cfg->native_code;
2896                         break;
2897                 case CEE_BR:
2898                         //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2899                         //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2900                         //break;
2901                         if (ins->flags & MONO_INST_BRLABEL) {
2902                                 /*if (ins->inst_i0->inst_c0) {
2903                                         ppc_b (code, 0);
2904                                         //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2905                                 } else*/ {
2906                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2907                                         ppc_b (code, 0);
2908                                 }
2909                         } else {
2910                                 /*if (ins->inst_target_bb->native_offset) {
2911                                         ppc_b (code, 0);
2912                                         //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset); 
2913                                 } else*/ {
2914                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2915                                         ppc_b (code, 0);
2916                                 } 
2917                         }
2918                         break;
2919                 case OP_BR_REG:
2920                         ppc_mtctr (code, ins->sreg1);
2921                         ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2922                         break;
2923                 case OP_CEQ:
2924                         ppc_li (code, ins->dreg, 0);
2925                         ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2926                         ppc_li (code, ins->dreg, 1);
2927                         break;
2928                 case OP_CLT:
2929                 case OP_CLT_UN:
2930                         ppc_li (code, ins->dreg, 1);
2931                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2932                         ppc_li (code, ins->dreg, 0);
2933                         break;
2934                 case OP_CGT:
2935                 case OP_CGT_UN:
2936                         ppc_li (code, ins->dreg, 1);
2937                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2938                         ppc_li (code, ins->dreg, 0);
2939                         break;
2940                 case OP_COND_EXC_EQ:
2941                 case OP_COND_EXC_NE_UN:
2942                 case OP_COND_EXC_LT:
2943                 case OP_COND_EXC_LT_UN:
2944                 case OP_COND_EXC_GT:
2945                 case OP_COND_EXC_GT_UN:
2946                 case OP_COND_EXC_GE:
2947                 case OP_COND_EXC_GE_UN:
2948                 case OP_COND_EXC_LE:
2949                 case OP_COND_EXC_LE_UN:
2950                         EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2951                         break;
2952                 case OP_COND_EXC_C:
2953                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2954                          */
2955                         /*ppc_mfspr (code, ppc_r0, ppc_xer);
2956                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2957                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2958                         break;*/
2959                 case OP_COND_EXC_OV:
2960                         /*ppc_mcrxr (code, 0);
2961                         EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2962                         break;*/
2963                 case OP_COND_EXC_NC:
2964                 case OP_COND_EXC_NO:
2965                         g_assert_not_reached ();
2966                         break;
2967                 case CEE_BEQ:
2968                 case CEE_BNE_UN:
2969                 case CEE_BLT:
2970                 case CEE_BLT_UN:
2971                 case CEE_BGT:
2972                 case CEE_BGT_UN:
2973                 case CEE_BGE:
2974                 case CEE_BGE_UN:
2975                 case CEE_BLE:
2976                 case CEE_BLE_UN:
2977                         EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2978                         break;
2979
2980                 /* floating point opcodes */
2981                 case OP_R8CONST:
2982                         ppc_load (code, ppc_r11, ins->inst_p0);
2983                         ppc_lfd (code, ins->dreg, 0, ppc_r11);
2984                         break;
2985                 case OP_R4CONST:
2986                         ppc_load (code, ppc_r11, ins->inst_p0);
2987                         ppc_lfs (code, ins->dreg, 0, ppc_r11);
2988                         break;
2989                 case OP_STORER8_MEMBASE_REG:
2990                         if (ppc_is_imm16 (ins->inst_offset)) {
2991                                 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2992                         } else {
2993                                 ppc_load (code, ppc_r11, ins->inst_offset);
2994                                 ppc_stfdx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2995                         }
2996                         break;
2997                 case OP_LOADR8_MEMBASE:
2998                         if (ppc_is_imm16 (ins->inst_offset)) {
2999                                 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
3000                         } else {
3001                                 ppc_load (code, ppc_r11, ins->inst_offset);
3002                                 ppc_lfdx (code, ins->dreg, ppc_r11, ins->inst_basereg);
3003                         }
3004                         break;
3005                 case OP_STORER4_MEMBASE_REG:
3006                         if (ppc_is_imm16 (ins->inst_offset)) {
3007                                 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
3008                         } else {
3009                                 ppc_load (code, ppc_r11, ins->inst_offset);
3010                                 ppc_stfsx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
3011                         }
3012                         break;
3013                 case OP_LOADR4_MEMBASE:
3014                         if (ppc_is_imm16 (ins->inst_offset)) {
3015                                 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
3016                         } else {
3017                                 ppc_load (code, ppc_r11, ins->inst_offset);
3018                                 ppc_lfsx (code, ins->dreg, ppc_r11, ins->inst_basereg);
3019                         }
3020                         break;
3021                 case CEE_CONV_R_UN: {
3022                         static const guint64 adjust_val = 0x4330000000000000ULL;
3023                         ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3024                         ppc_stw (code, ppc_r0, -8, ppc_sp);
3025                         ppc_stw (code, ins->sreg1, -4, ppc_sp);
3026                         ppc_load (code, ppc_r11, &adjust_val);
3027                         ppc_lfd (code, ins->dreg, -8, ppc_sp);
3028                         ppc_lfd (code, ppc_f0, 0, ppc_r11);
3029                         ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3030                         break;
3031                 }
3032                 case CEE_CONV_R4: /* FIXME: change precision */
3033                 case CEE_CONV_R8: {
3034                         static const guint64 adjust_val = 0x4330000080000000ULL;
3035                         // addis is special for ppc_r0
3036                         ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3037                         ppc_stw (code, ppc_r0, -8, ppc_sp);
3038                         ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
3039                         ppc_stw (code, ppc_r11, -4, ppc_sp);
3040                         ppc_lfd (code, ins->dreg, -8, ppc_sp);
3041                         ppc_load (code, ppc_r11, &adjust_val);
3042                         ppc_lfd (code, ppc_f0, 0, ppc_r11);
3043                         ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3044                         break;
3045                 }
3046                 case OP_X86_FP_LOAD_I8:
3047                         g_assert_not_reached ();
3048                         /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
3049                         break;
3050                 case OP_X86_FP_LOAD_I4:
3051                         g_assert_not_reached ();
3052                         /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
3053                         break;
3054                 case OP_FCONV_TO_I1:
3055                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
3056                         break;
3057                 case OP_FCONV_TO_U1:
3058                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
3059                         break;
3060                 case OP_FCONV_TO_I2:
3061                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
3062                         break;
3063                 case OP_FCONV_TO_U2:
3064                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
3065                         break;
3066                 case OP_FCONV_TO_I4:
3067                 case OP_FCONV_TO_I:
3068                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
3069                         break;
3070                 case OP_FCONV_TO_U4:
3071                 case OP_FCONV_TO_U:
3072                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
3073                         break;
3074                 case OP_FCONV_TO_I8:
3075                 case OP_FCONV_TO_U8:
3076                         g_assert_not_reached ();
3077                         /* Implemented as helper calls */
3078                         break;
3079                 case OP_LCONV_TO_R_UN:
3080                         g_assert_not_reached ();
3081                         /* Implemented as helper calls */
3082                         break;
3083                 case OP_LCONV_TO_OVF_I: {
3084                         ppc_mr (code, ins->dreg, ins->sreg1);
3085                         /* FIXME: emit exception if needed */
3086                         break;
3087                 }
3088                 case OP_SQRT:
3089                         ppc_fsqrtd (code, ins->dreg, ins->sreg1);
3090                         break;
3091                 case OP_FADD:
3092                         ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
3093                         break;
3094                 case OP_FSUB:
3095                         ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
3096                         break;          
3097                 case OP_FMUL:
3098                         ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
3099                         break;          
3100                 case OP_FDIV:
3101                         ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
3102                         break;          
3103                 case OP_FNEG:
3104                         ppc_fneg (code, ins->dreg, ins->sreg1);
3105                         break;          
3106                 case OP_FREM:
3107                         /* emulated */
3108                         g_assert_not_reached ();
3109                         break;
3110                 case OP_FCOMPARE:
3111                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3112                         break;
3113                 case OP_FCEQ:
3114                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3115                         ppc_li (code, ins->dreg, 0);
3116                         ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
3117                         ppc_li (code, ins->dreg, 1);
3118                         break;
3119                 case OP_FCLT:
3120                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3121                         ppc_li (code, ins->dreg, 1);
3122                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3123                         ppc_li (code, ins->dreg, 0);
3124                         break;
3125                 case OP_FCLT_UN:
3126                         ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3127                         ppc_li (code, ins->dreg, 1);
3128                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3129                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3130                         ppc_li (code, ins->dreg, 0);
3131                         break;
3132                 case OP_FCGT:
3133                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3134                         ppc_li (code, ins->dreg, 1);
3135                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3136                         ppc_li (code, ins->dreg, 0);
3137                         break;
3138                 case OP_FCGT_UN:
3139                         ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3140                         ppc_li (code, ins->dreg, 1);
3141                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3142                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3143                         ppc_li (code, ins->dreg, 0);
3144                         break;
3145                 case OP_FBEQ:
3146                         EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
3147                         break;
3148                 case OP_FBNE_UN:
3149                         EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
3150                         break;
3151                 case OP_FBLT:
3152                         EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
3153                         break;
3154                 case OP_FBLT_UN:
3155                         EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3156                         EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
3157                         break;
3158                 case OP_FBGT:
3159                         EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
3160                         break;
3161                 case OP_FBGT_UN:
3162                         EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3163                         EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
3164                         break;
3165                 case OP_FBGE:
3166                         EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
3167                         break;
3168                 case OP_FBGE_UN:
3169                         EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
3170                         break;
3171                 case OP_FBLE:
3172                         EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
3173                         break;
3174                 case OP_FBLE_UN:
3175                         EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
3176                         break;
3177                 case CEE_CKFINITE: {
3178                         ppc_stfd (code, ins->sreg1, -8, ppc_sp);
3179                         ppc_lwz (code, ppc_r11, -8, ppc_sp);
3180                         ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
3181                         ppc_addis (code, ppc_r11, ppc_r11, -32752);
3182                         ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
3183                         EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");
3184                         break;
3185                 }
3186                 default:
3187                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3188                         g_assert_not_reached ();
3189                 }
3190
3191                 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
3192                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3193                                    mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3194                         g_assert_not_reached ();
3195                 }
3196                
3197                 cpos += max_len;
3198
3199                 last_ins = ins;
3200                 last_offset = offset;
3201                 
3202                 ins = ins->next;
3203         }
3204
3205         cfg->code_len = code - cfg->native_code;
3206 }
3207
3208 void
3209 mono_arch_register_lowlevel_calls (void)
3210 {
3211 }
3212
3213 #define patch_lis_ori(ip,val) do {\
3214                 guint16 *__lis_ori = (guint16*)(ip);    \
3215                 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff;      \
3216                 __lis_ori [3] = ((guint32)(val)) & 0xffff;      \
3217         } while (0)
3218
3219 void
3220 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3221 {
3222         MonoJumpInfo *patch_info;
3223
3224         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3225                 unsigned char *ip = patch_info->ip.i + code;
3226                 const unsigned char *target;
3227
3228                 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3229
3230                 switch (patch_info->type) {
3231                 case MONO_PATCH_INFO_IP:
3232                         patch_lis_ori (ip, ip);
3233                         continue;
3234                 case MONO_PATCH_INFO_METHOD_REL:
3235                         g_assert_not_reached ();
3236                         *((gpointer *)(ip)) = code + patch_info->data.offset;
3237                         continue;
3238                 case MONO_PATCH_INFO_SWITCH: {
3239                         gpointer *table = (gpointer *)patch_info->data.target;
3240                         int i;
3241
3242                         // FIXME: inspect code to get the register
3243                         ppc_load (ip, ppc_r11, patch_info->data.target);
3244                         //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3245
3246                         for (i = 0; i < patch_info->table_size; i++) {
3247                                 table [i] = (int)patch_info->data.table [i] + code;
3248                         }
3249                         /* we put into the table the absolute address, no need for ppc_patch in this case */
3250                         continue;
3251                 }
3252                 case MONO_PATCH_INFO_METHODCONST:
3253                 case MONO_PATCH_INFO_CLASS:
3254                 case MONO_PATCH_INFO_IMAGE:
3255                 case MONO_PATCH_INFO_FIELD:
3256                 case MONO_PATCH_INFO_VTABLE:
3257                 case MONO_PATCH_INFO_IID:
3258                 case MONO_PATCH_INFO_SFLDA:
3259                 case MONO_PATCH_INFO_LDSTR:
3260                 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3261                 case MONO_PATCH_INFO_LDTOKEN:
3262                         /* from OP_AOTCONST : lis + ori */
3263                         patch_lis_ori (ip, target);
3264                         continue;
3265                 case MONO_PATCH_INFO_R4:
3266                 case MONO_PATCH_INFO_R8:
3267                         g_assert_not_reached ();
3268                         *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3269                         continue;
3270                 case MONO_PATCH_INFO_EXC_NAME:
3271                         g_assert_not_reached ();
3272                         *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3273                         continue;
3274                 case MONO_PATCH_INFO_BB_OVF:
3275                 case MONO_PATCH_INFO_EXC_OVF:
3276                         /* everything is dealt with at epilog output time */
3277                         continue;
3278                 default:
3279                         break;
3280                 }
3281                 ppc_patch (ip, target);
3282         }
3283 }
3284
3285 int
3286 mono_arch_max_epilog_size (MonoCompile *cfg)
3287 {
3288         int max_epilog_size = 16 + 20*4;
3289         MonoJumpInfo *patch_info;
3290         
3291         if (cfg->method->save_lmf)
3292                 max_epilog_size += 128;
3293         
3294         if (mono_jit_trace_calls != NULL)
3295                 max_epilog_size += 50;
3296
3297         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3298                 max_epilog_size += 50;
3299
3300         /* count the number of exception infos */
3301      
3302         /* 
3303          * make sure we have enough space for exceptions
3304          * 24 is the simulated call to throw_exception_by_name
3305          */
3306         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3307                 if (patch_info->type == MONO_PATCH_INFO_EXC)
3308                         max_epilog_size += 24;
3309                 else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
3310                         max_epilog_size += 12;
3311                 else if (patch_info->type == MONO_PATCH_INFO_EXC_OVF)
3312                         max_epilog_size += 12;
3313         }
3314
3315         return max_epilog_size;
3316 }
3317
3318 /*
3319  * Stack frame layout:
3320  * 
3321  *   ------------------- sp
3322  *      MonoLMF structure or saved registers
3323  *   -------------------
3324  *      spilled regs
3325  *   -------------------
3326  *      locals
3327  *   -------------------
3328  *      optional 8 bytes for tracing
3329  *   -------------------
3330  *      param area             size is cfg->param_area
3331  *   -------------------
3332  *      linkage area           size is PPC_STACK_PARAM_OFFSET
3333  *   ------------------- sp
3334  *      red zone
3335  */
3336 guint8 *
3337 mono_arch_emit_prolog (MonoCompile *cfg)
3338 {
3339         MonoMethod *method = cfg->method;
3340         MonoBasicBlock *bb;
3341         MonoMethodSignature *sig;
3342         MonoInst *inst;
3343         int alloc_size, pos, max_offset, i;
3344         guint8 *code;
3345         CallInfo *cinfo;
3346         int tracing = 0;
3347         int lmf_offset = 0;
3348
3349         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3350                 tracing = 1;
3351
3352         cfg->code_size = 256;
3353         code = cfg->native_code = g_malloc (cfg->code_size);
3354
3355         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3356                 ppc_mflr (code, ppc_r0);
3357                 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3358         }
3359         if (cfg->max_ireg >= 29)
3360                 cfg->used_int_regs |= USE_EXTRA_TEMPS;
3361
3362         alloc_size = cfg->stack_offset;
3363         pos = 0;
3364
3365         if (!method->save_lmf) {
3366                 /*for (i = 31; i >= 14; --i) {
3367                         if (cfg->used_float_regs & (1 << i)) {
3368                                 pos += sizeof (gdouble);
3369                                 ppc_stfd (code, i, -pos, ppc_sp);
3370                         }
3371                 }*/
3372                 for (i = 31; i >= 13; --i) {
3373                         if (cfg->used_int_regs & (1 << i)) {
3374                                 pos += sizeof (gulong);
3375                                 ppc_stw (code, i, -pos, ppc_sp);
3376                         }
3377                 }
3378         } else {
3379                 int ofs;
3380                 pos += sizeof (MonoLMF);
3381                 lmf_offset = pos;
3382                 ofs = -pos + G_STRUCT_OFFSET(MonoLMF, iregs);
3383                 ppc_stmw (code, ppc_r13, ppc_r1, ofs);
3384                 for (i = 14; i < 32; i++) {
3385                         ppc_stfd (code, i, (-pos + G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble))), ppc_r1);
3386                 }
3387         }
3388         alloc_size += pos;
3389         // align to PPC_STACK_ALIGNMENT bytes
3390         if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
3391                 alloc_size += PPC_STACK_ALIGNMENT - 1;
3392                 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
3393         }
3394
3395         cfg->stack_usage = alloc_size;
3396         g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
3397         if (alloc_size) {
3398                 if (ppc_is_imm16 (-alloc_size)) {
3399                         ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3400                 } else {
3401                         ppc_load (code, ppc_r11, -alloc_size);
3402                         ppc_stwux (code, ppc_sp, ppc_sp, ppc_r11);
3403                 }
3404         }
3405         if (cfg->frame_reg != ppc_sp)
3406                 ppc_mr (code, cfg->frame_reg, ppc_sp);
3407
3408         /* compute max_offset in order to use short forward jumps
3409          * we always do it on ppc because the immediate displacement
3410          * for jumps is too small 
3411          */
3412         max_offset = 0;
3413         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3414                 MonoInst *ins = bb->code;
3415                 bb->max_offset = max_offset;
3416
3417                 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3418                         max_offset += 6; 
3419
3420                 while (ins) {
3421                         max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3422                         ins = ins->next;
3423                 }
3424         }
3425
3426         /* load arguments allocated to register from the stack */
3427         sig = method->signature;
3428         pos = 0;
3429
3430         cinfo = calculate_sizes (sig, sig->pinvoke);
3431
3432         if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3433                 ArgInfo *ainfo = &cinfo->ret;
3434                 inst = cfg->ret;
3435                 if (ppc_is_imm16 (inst->inst_offset)) {
3436                         ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3437                 } else {
3438                         ppc_load (code, ppc_r11, inst->inst_offset);
3439                         ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3440                 }
3441         }
3442         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3443                 ArgInfo *ainfo = cinfo->args + i;
3444                 inst = cfg->varinfo [pos];
3445                 
3446                 if (cfg->verbose_level > 2)
3447                         g_print ("Saving argument %d (type: %d)\n", i, ainfo->regtype);
3448                 if (inst->opcode == OP_REGVAR) {
3449                         if (ainfo->regtype == RegTypeGeneral)
3450                                 ppc_mr (code, inst->dreg, ainfo->reg);
3451                         else if (ainfo->regtype == RegTypeFP)
3452                                 ppc_fmr (code, inst->dreg, ainfo->reg);
3453                         else if (ainfo->regtype == RegTypeBase) {
3454                                 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3455                                 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3456                         } else
3457                                 g_assert_not_reached ();
3458
3459                         if (cfg->verbose_level > 2)
3460                                 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3461                 } else {
3462                         /* the argument should be put on the stack: FIXME handle size != word  */
3463                         if (ainfo->regtype == RegTypeGeneral) {
3464                                 switch (ainfo->size) {
3465                                 case 1:
3466                                         if (ppc_is_imm16 (inst->inst_offset)) {
3467                                                 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3468                                         } else {
3469                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3470                                                 ppc_stbx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3471                                         }
3472                                         break;
3473                                 case 2:
3474                                         if (ppc_is_imm16 (inst->inst_offset)) {
3475                                                 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3476                                         } else {
3477                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3478                                                 ppc_sthx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3479                                         }
3480                                         break;
3481                                 case 8:
3482                                         if (ppc_is_imm16 (inst->inst_offset + 4)) {
3483                                                 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3484                                                 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3485                                         } else {
3486                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3487                                                 ppc_add (code, ppc_r11, ppc_r11, inst->inst_basereg);
3488                                                 ppc_stw (code, ainfo->reg, 0, ppc_r11);
3489                                                 ppc_stw (code, ainfo->reg + 1, 4, ppc_r11);
3490                                         }
3491                                         break;
3492                                 default:
3493                                         if (ppc_is_imm16 (inst->inst_offset)) {
3494                                                 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3495                                         } else {
3496                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3497                                                 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3498                                         }
3499                                         break;
3500                                 }
3501                         } else if (ainfo->regtype == RegTypeBase) {
3502                                 /* load the previous stack pointer in r11 */
3503                                 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3504                                 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3505                                 switch (ainfo->size) {
3506                                 case 1:
3507                                         if (ppc_is_imm16 (inst->inst_offset)) {
3508                                                 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3509                                         } else {
3510                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3511                                                 ppc_stbx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3512                                         }
3513                                         break;
3514                                 case 2:
3515                                         if (ppc_is_imm16 (inst->inst_offset)) {
3516                                                 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3517                                         } else {
3518                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3519                                                 ppc_sthx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3520                                         }
3521                                         break;
3522                                 case 8:
3523                                         if (ppc_is_imm16 (inst->inst_offset + 4)) {
3524                                                 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3525                                                 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3526                                                 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3527                                         } else {
3528                                                 /* FIXME */
3529                                                 g_assert_not_reached ();
3530                                         }
3531                                         break;
3532                                 default:
3533                                         if (ppc_is_imm16 (inst->inst_offset)) {
3534                                                 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3535                                         } else {
3536                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3537                                                 ppc_stwx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3538                                         }
3539                                         break;
3540                                 }
3541                         } else if (ainfo->regtype == RegTypeFP) {
3542                                 g_assert (ppc_is_imm16 (inst->inst_offset));
3543                                 if (ainfo->size == 8)
3544                                         ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3545                                 else if (ainfo->size == 4)
3546                                         ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3547                                 else
3548                                         g_assert_not_reached ();
3549                         } else if (ainfo->regtype == RegTypeStructByVal) {
3550                                 int doffset = inst->inst_offset;
3551                                 int soffset = 0;
3552                                 int cur_reg;
3553                                 int size = 0;
3554                                 g_assert (ppc_is_imm16 (inst->inst_offset));
3555                                 g_assert (ppc_is_imm16 (inst->inst_offset + ainfo->size * sizeof (gpointer)));
3556                                 if (inst->inst_vtype->data.klass)
3557                                         size = mono_class_native_size (inst->inst_vtype->data.klass, NULL);
3558                                 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3559 /*
3560 Darwin handles 1 and 2 byte structs specially by loading h/b into the arg
3561 register.  Should this case include linux/ppc?
3562 */
3563 #if __APPLE__
3564                                         if (size == 2)
3565                                                 ppc_sth (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3566                                         else if (size == 1)
3567                                                 ppc_stb (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3568                                         else 
3569 #endif
3570                                                 ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3571                                         soffset += sizeof (gpointer);
3572                                         doffset += sizeof (gpointer);
3573                                 }
3574                                 if (ainfo->vtsize) {
3575                                         /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3576                                         ppc_lwz (code, ppc_r11, 0, ppc_sp);
3577                                         /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3578                                         code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3579                                 }
3580                         } else if (ainfo->regtype == RegTypeStructByAddr) {
3581                                 g_assert (ppc_is_imm16 (inst->inst_offset));
3582                                 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3583                                 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3584                         } else
3585                                 g_assert_not_reached ();
3586                 }
3587                 pos++;
3588         }
3589
3590         if (method->save_lmf) {
3591
3592                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3593                                      (gpointer)"mono_get_lmf_addr");
3594                 ppc_bl (code, 0);
3595                 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3596                 /* lmf_offset is the offset from the previous stack pointer,
3597                  * alloc_size is the total stack space allocated, so the offset
3598                  * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
3599                  * The pointer to the struct is put in ppc_r11 (new_lmf).
3600                  * The callee-saved registers are already in the MonoLMF structure
3601                  */
3602                 ppc_addi (code, ppc_r11, ppc_sp, alloc_size - lmf_offset);
3603                 /* ppc_r3 is the result from mono_get_lmf_addr () */
3604                 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3605                 /* new_lmf->previous_lmf = *lmf_addr */
3606                 ppc_lwz (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3607                 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3608                 /* *(lmf_addr) = r11 */
3609                 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3610                 /* save method info */
3611                 ppc_load (code, ppc_r0, method);
3612                 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3613                 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3614                 /* save the current IP */
3615                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3616                 ppc_load (code, ppc_r0, 0x01010101);
3617                 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3618         }
3619
3620         if (tracing)
3621                 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3622
3623         cfg->code_len = code - cfg->native_code;
3624         g_free (cinfo);
3625
3626         return code;
3627 }
3628
3629 void
3630 mono_arch_emit_epilog (MonoCompile *cfg)
3631 {
3632         MonoJumpInfo *patch_info;
3633         MonoMethod *method = cfg->method;
3634         int pos, i;
3635         guint8 *code;
3636
3637         /*
3638          * Keep in sync with CEE_JMP
3639          */
3640         code = cfg->native_code + cfg->code_len;
3641
3642         if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3643                 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3644         }
3645         pos = 0;
3646
3647         if (method->save_lmf) {
3648                 int lmf_offset;
3649                 pos +=  sizeof (MonoLMF);
3650                 lmf_offset = pos;
3651                 /* save the frame reg in r8 */
3652                 ppc_mr (code, ppc_r8, cfg->frame_reg);
3653                 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage - lmf_offset);
3654                 /* r5 = previous_lmf */
3655                 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3656                 /* r6 = lmf_addr */
3657                 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3658                 /* *(lmf_addr) = previous_lmf */
3659                 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3660                 /* FIXME: speedup: there is no actual need to restore the registers if
3661                  * we didn't actually change them (idea from Zoltan).
3662                  */
3663                 /* restore iregs */
3664                 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3665                 /* restore fregs */
3666                 /*for (i = 14; i < 32; i++) {
3667                         ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3668                 }*/
3669                 g_assert (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET));
3670                 /* use the saved copy of the frame reg in r8 */
3671                 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3672                         ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, ppc_r8);
3673                         ppc_mtlr (code, ppc_r0);
3674                 }
3675                 ppc_addic (code, ppc_sp, ppc_r8, cfg->stack_usage);
3676         } else {
3677                 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3678                         if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
3679                                 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3680                         } else {
3681                                 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
3682                                 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
3683                         }
3684                         ppc_mtlr (code, ppc_r0);
3685                 }
3686                 if (ppc_is_imm16 (cfg->stack_usage)) {
3687                         ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3688                 } else {
3689                         ppc_load (code, ppc_r11, cfg->stack_usage);
3690                         ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
3691                 }
3692
3693                 /*for (i = 31; i >= 14; --i) {
3694                         if (cfg->used_float_regs & (1 << i)) {
3695                                 pos += sizeof (double);
3696                                 ppc_lfd (code, i, -pos, ppc_sp);
3697                         }
3698                 }*/
3699                 for (i = 31; i >= 13; --i) {
3700                         if (cfg->used_int_regs & (1 << i)) {
3701                                 pos += sizeof (gulong);
3702                                 ppc_lwz (code, i, -pos, ppc_sp);
3703                         }
3704                 }
3705         }
3706         ppc_blr (code);
3707
3708         /* add code to raise exceptions */
3709         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3710                 switch (patch_info->type) {
3711                 case MONO_PATCH_INFO_BB_OVF: {
3712                         MonoOvfJump *ovfj = patch_info->data.target;
3713                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
3714                         /* patch the initial jump */
3715                         ppc_patch (ip, code);
3716                         ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3717                         ppc_b (code, 0);
3718                         ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3719                         /* jump back to the true target */
3720                         ppc_b (code, 0);
3721                         ip = ovfj->bb->native_offset + cfg->native_code;
3722                         ppc_patch (code - 4, ip);
3723                         break;
3724                 }
3725                 case MONO_PATCH_INFO_EXC_OVF: {
3726                         MonoOvfJump *ovfj = patch_info->data.target;
3727                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
3728                         /* patch the initial jump */
3729                         ppc_patch (ip, code);
3730                         ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3731                         ppc_b (code, 0);
3732                         ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3733                         /* jump back to the true target */
3734                         ppc_b (code, 0);
3735                         ip = (char*)ovfj->ip + 4;
3736                         ppc_patch (code - 4, ip);
3737                         break;
3738                 }
3739                 case MONO_PATCH_INFO_EXC: {
3740                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
3741                         ppc_patch (ip, code);
3742                         /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3743                         ppc_load (code, ppc_r3, patch_info->data.target);
3744                         /* simulate a call from ip */
3745                         ppc_load (code, ppc_r0, ip + 4);
3746                         ppc_mtlr (code, ppc_r0);
3747                         patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3748                         patch_info->data.name = "mono_arch_throw_exception_by_name";
3749                         patch_info->ip.i = code - cfg->native_code;
3750                         ppc_b (code, 0);
3751                         break;
3752                 }
3753                 default:
3754                         /* do nothing */
3755                         break;
3756                 }
3757         }
3758
3759         cfg->code_len = code - cfg->native_code;
3760
3761         g_assert (cfg->code_len < cfg->code_size);
3762
3763 }
3764
3765 void
3766 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3767 {
3768 }
3769
3770 void
3771 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3772 {
3773 }
3774
3775 void
3776 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3777 {
3778         int this_dreg = ppc_r3;
3779         
3780         if (vt_reg != -1)
3781                 this_dreg = ppc_r4;
3782
3783         /* add the this argument */
3784         if (this_reg != -1) {
3785                 MonoInst *this;
3786                 MONO_INST_NEW (cfg, this, OP_SETREG);
3787                 this->type = this_type;
3788                 this->sreg1 = this_reg;
3789                 this->dreg = this_dreg;
3790                 mono_bblock_add_inst (cfg->cbb, this);
3791         }
3792
3793         if (vt_reg != -1) {
3794                 MonoInst *vtarg;
3795                 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3796                 vtarg->type = STACK_MP;
3797                 vtarg->sreg1 = vt_reg;
3798                 vtarg->dreg = ppc_r3;
3799                 mono_bblock_add_inst (cfg->cbb, vtarg);
3800         }
3801 }
3802
3803 gint
3804 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3805 {
3806         /* optional instruction, need to detect it
3807         if (cmethod->klass == mono_defaults.math_class) {
3808                 if (strcmp (cmethod->name, "Sqrt") == 0)
3809                         return OP_SQRT;
3810         }*/
3811         return -1;
3812 }
3813
3814
3815 gboolean
3816 mono_arch_print_tree (MonoInst *tree, int arity)
3817 {
3818         return 0;
3819 }
3820
3821 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
3822 {
3823         return NULL;
3824 }
3825
3826 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
3827 {
3828         return NULL;
3829 }