This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 FALSE;
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                         size = mono_class_value_size (sig->params [i]->data.klass, NULL);
404                         DEBUG(printf ("load %d bytes struct\n",
405                                       mono_class_value_size (sig->params [i]->data.klass, NULL)));
406 #if PPC_PASS_STRUCTS_BY_VALUE
407                         {
408                                 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
409                                 cinfo->args [n].regtype = RegTypeStructByVal;
410                                 if (gr <= PPC_LAST_ARG_REG) {
411                                         int rest = PPC_LAST_ARG_REG - gr + 1;
412                                         int n_in_regs = rest >= nwords? nwords: rest;
413                                         cinfo->args [n].size = n_in_regs;
414                                         cinfo->args [n].vtsize = nwords - n_in_regs;
415                                         cinfo->args [n].reg = gr;
416                                         gr += n_in_regs;
417                                 } else {
418                                         cinfo->args [n].size = 0;
419                                         cinfo->args [n].vtsize = nwords;
420                                 }
421                                 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
422                                 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
423                                 stack_size += nwords * sizeof (gpointer);
424                         }
425 #else
426                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
427                         cinfo->args [n].regtype = RegTypeStructByAddr;
428 #endif
429                         n++;
430                         break;
431                 }
432                 case MONO_TYPE_TYPEDBYREF: {
433                         int size = sizeof (MonoTypedRef);
434                         /* keep in sync or merge with the valuetype case */
435 #if PPC_PASS_STRUCTS_BY_VALUE
436                         {
437                                 int nwords = (size + sizeof (gpointer) -1 ) / sizeof (gpointer);
438                                 cinfo->args [n].regtype = RegTypeStructByVal;
439                                 if (gr <= PPC_LAST_ARG_REG) {
440                                         int rest = PPC_LAST_ARG_REG - gr + 1;
441                                         int n_in_regs = rest >= nwords? nwords: rest;
442                                         cinfo->args [n].size = n_in_regs;
443                                         cinfo->args [n].vtsize = nwords - n_in_regs;
444                                         cinfo->args [n].reg = gr;
445                                         gr += n_in_regs;
446                                 } else {
447                                         cinfo->args [n].size = 0;
448                                         cinfo->args [n].vtsize = nwords;
449                                 }
450                                 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
451                                 /*g_print ("offset for arg %d at %d\n", n, PPC_STACK_PARAM_OFFSET + stack_size);*/
452                                 stack_size += nwords * sizeof (gpointer);
453                         }
454 #else
455                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
456                         cinfo->args [n].regtype = RegTypeStructByAddr;
457 #endif
458                         n++;
459                         break;
460                 }
461                 case MONO_TYPE_U8:
462                 case MONO_TYPE_I8:
463                         cinfo->args [n].size = 8;
464                         add_general (&gr, &stack_size, cinfo->args + n, FALSE);
465                         n++;
466                         break;
467                 case MONO_TYPE_R4:
468                         cinfo->args [n].size = 4;
469
470                         /* It was 7, now it is 8 in LinuxPPC */
471                         if (fr <= PPC_LAST_FPARG_REG) {
472                                 cinfo->args [n].regtype = RegTypeFP;
473                                 cinfo->args [n].reg = fr;
474                                 fr ++;
475                                 FP_ALSO_IN_REG (gr ++);
476                                 ALWAYS_ON_STACK (stack_size += 4);
477                         } else {
478                                 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
479                                 cinfo->args [n].regtype = RegTypeBase;
480                                 cinfo->args [n].reg = ppc_sp; /* in the caller*/
481                                 stack_size += 4;
482                         }
483                         n++;
484                         break;
485                 case MONO_TYPE_R8:
486                         cinfo->args [n].size = 8;
487                         /* It was 7, now it is 8 in LinuxPPC */
488                         if (fr <= PPC_LAST_FPARG_REG) {
489                                 cinfo->args [n].regtype = RegTypeFP;
490                                 cinfo->args [n].reg = fr;
491                                 fr ++;
492                                 FP_ALSO_IN_REG (gr += 2);
493                                 ALWAYS_ON_STACK (stack_size += 8);
494                         } else {
495                                 cinfo->args [n].offset = PPC_STACK_PARAM_OFFSET + stack_size;
496                                 cinfo->args [n].regtype = RegTypeBase;
497                                 cinfo->args [n].reg = ppc_sp; /* in the caller*/
498                                 stack_size += 8;
499                         }
500                         n++;
501                         break;
502                 default:
503                         g_error ("Can't trampoline 0x%x", sig->params [i]->type);
504                 }
505         }
506
507         {
508                 simpletype = sig->ret->type;
509 enum_retvalue:
510                 switch (simpletype) {
511                 case MONO_TYPE_BOOLEAN:
512                 case MONO_TYPE_I1:
513                 case MONO_TYPE_U1:
514                 case MONO_TYPE_I2:
515                 case MONO_TYPE_U2:
516                 case MONO_TYPE_CHAR:
517                 case MONO_TYPE_I4:
518                 case MONO_TYPE_U4:
519                 case MONO_TYPE_I:
520                 case MONO_TYPE_U:
521                 case MONO_TYPE_PTR:
522                 case MONO_TYPE_FNPTR:
523                 case MONO_TYPE_CLASS:
524                 case MONO_TYPE_OBJECT:
525                 case MONO_TYPE_SZARRAY:
526                 case MONO_TYPE_ARRAY:
527                 case MONO_TYPE_STRING:
528                         cinfo->ret.reg = ppc_r3;
529                         break;
530                 case MONO_TYPE_U8:
531                 case MONO_TYPE_I8:
532                         cinfo->ret.reg = ppc_r3;
533                         break;
534                 case MONO_TYPE_R4:
535                 case MONO_TYPE_R8:
536                         cinfo->ret.reg = ppc_f1;
537                         cinfo->ret.regtype = RegTypeFP;
538                         break;
539                 case MONO_TYPE_VALUETYPE:
540                         if (sig->ret->data.klass->enumtype) {
541                                 simpletype = sig->ret->data.klass->enum_basetype->type;
542                                 goto enum_retvalue;
543                         }
544                         break;
545                 case MONO_TYPE_TYPEDBYREF:
546                 case MONO_TYPE_VOID:
547                         break;
548                 default:
549                         g_error ("Can't handle as return value 0x%x", sig->ret->type);
550                 }
551         }
552
553         /* align stack size to 16 */
554         DEBUG (printf ("      stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
555         stack_size = (stack_size + 15) & ~15;
556
557         cinfo->stack_usage = stack_size;
558         return cinfo;
559 }
560
561
562 /*
563  * Set var information according to the calling convention. ppc version.
564  * The locals var stuff should most likely be split in another method.
565  */
566 void
567 mono_arch_allocate_vars (MonoCompile *m)
568 {
569         MonoMethodSignature *sig;
570         MonoMethodHeader *header;
571         MonoInst *inst;
572         int i, offset, size, align, curinst;
573         int frame_reg = ppc_sp;
574
575         /* allow room for the vararg method args: void* and long/double */
576         if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
577                 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
578         /* this is bug #60332: remove when #59509 is fixed, so no weird vararg 
579          * call convs needs to be handled this way.
580          */
581         if (m->flags & MONO_CFG_HAS_VARARGS)
582                 m->param_area = MAX (m->param_area, sizeof (gpointer)*8);
583
584         header = ((MonoMethodNormal *)m->method)->header;
585
586         /* 
587          * We use the frame register also for any method that has
588          * exception clauses. This way, when the handlers are called,
589          * the code will reference local variables using the frame reg instead of
590          * the stack pointer: if we had to restore the stack pointer, we'd
591          * corrupt the method frames that are already on the stack (since
592          * filters get called before stack unwinding happens) when the filter
593          * code would call any method (this also applies to finally etc.).
594          */ 
595         if ((m->flags & MONO_CFG_HAS_ALLOCA) || header->num_clauses)
596                 frame_reg = ppc_r31;
597         m->frame_reg = frame_reg;
598         if (frame_reg != ppc_sp) {
599                 m->used_int_regs |= 1 << frame_reg;
600         }
601
602         sig = m->method->signature;
603         
604         offset = 0;
605         curinst = 0;
606         if (MONO_TYPE_ISSTRUCT (sig->ret)) {
607                 m->ret->opcode = OP_REGVAR;
608                 m->ret->inst_c0 = ppc_r3;
609         } else {
610                 /* FIXME: handle long and FP values */
611                 switch (sig->ret->type) {
612                 case MONO_TYPE_VOID:
613                         break;
614                 default:
615                         m->ret->opcode = OP_REGVAR;
616                         m->ret->inst_c0 = ppc_r3;
617                         break;
618                 }
619         }
620         /* local vars are at a positive offset from the stack pointer */
621         /* 
622          * also note that if the function uses alloca, we use ppc_r31
623          * to point at the local variables.
624          */
625         offset = PPC_MINIMAL_STACK_SIZE; /* linkage area */
626         /* align the offset to 16 bytes: not sure this is needed here  */
627         //offset += 16 - 1;
628         //offset &= ~(16 - 1);
629
630         /* add parameter area size for called functions */
631         offset += m->param_area;
632         offset += 16 - 1;
633         offset &= ~(16 - 1);
634
635         /* allow room to save the return value */
636         if (mono_jit_trace_calls != NULL && mono_trace_eval (m->method))
637                 offset += 8;
638
639         /* the MonoLMF structure is stored just below the stack pointer */
640
641 #if 0
642         /* this stuff should not be needed on ppc and the new jit,
643          * because a call on ppc to the handlers doesn't change the 
644          * stack pointer and the jist doesn't manipulate the stack pointer
645          * for operations involving valuetypes.
646          */
647         /* reserve space to store the esp */
648         offset += sizeof (gpointer);
649
650         /* this is a global constant */
651         mono_exc_esp_offset = offset;
652 #endif
653
654         if (MONO_TYPE_ISSTRUCT (sig->ret)) {
655                 inst = m->ret;
656                 offset += sizeof(gpointer) - 1;
657                 offset &= ~(sizeof(gpointer) - 1);
658                 inst->inst_offset = offset;
659                 inst->opcode = OP_REGOFFSET;
660                 inst->inst_basereg = frame_reg;
661                 offset += sizeof(gpointer);
662         }
663         curinst = m->locals_start;
664         for (i = curinst; i < m->num_varinfo; ++i) {
665                 inst = m->varinfo [i];
666                 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR)
667                         continue;
668
669                 /* inst->unused indicates native sized value types, this is used by the
670                 * pinvoke wrappers when they call functions returning structure */
671                 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
672                         size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
673                 else
674                         size = mono_type_size (inst->inst_vtype, &align);
675
676                 offset += align - 1;
677                 offset &= ~(align - 1);
678                 inst->inst_offset = offset;
679                 inst->opcode = OP_REGOFFSET;
680                 inst->inst_basereg = frame_reg;
681                 offset += size;
682                 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
683         }
684
685         curinst = 0;
686         if (sig->hasthis) {
687                 inst = m->varinfo [curinst];
688                 if (inst->opcode != OP_REGVAR) {
689                         inst->opcode = OP_REGOFFSET;
690                         inst->inst_basereg = frame_reg;
691                         offset += sizeof (gpointer) - 1;
692                         offset &= ~(sizeof (gpointer) - 1);
693                         inst->inst_offset = offset;
694                         offset += sizeof (gpointer);
695                 }
696                 curinst++;
697         }
698
699         for (i = 0; i < sig->param_count; ++i) {
700                 inst = m->varinfo [curinst];
701                 if (inst->opcode != OP_REGVAR) {
702                         inst->opcode = OP_REGOFFSET;
703                         inst->inst_basereg = frame_reg;
704                         size = mono_type_size (sig->params [i], &align);
705                         offset += align - 1;
706                         offset &= ~(align - 1);
707                         inst->inst_offset = offset;
708                         offset += size;
709                 }
710                 curinst++;
711         }
712
713         /* align the offset to 16 bytes */
714         offset += 16 - 1;
715         offset &= ~(16 - 1);
716
717         /* change sign? */
718         m->stack_offset = offset;
719
720 }
721
722 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
723  * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info 
724  */
725
726 /* 
727  * take the arguments and generate the arch-specific
728  * instructions to properly call the function in call.
729  * This includes pushing, moving arguments to the right register
730  * etc.
731  * Issue: who does the spilling if needed, and when?
732  */
733 MonoCallInst*
734 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
735         MonoInst *arg, *in;
736         MonoMethodSignature *sig;
737         int i, n;
738         CallInfo *cinfo;
739         ArgInfo *ainfo;
740
741         sig = call->signature;
742         n = sig->param_count + sig->hasthis;
743         
744         cinfo = calculate_sizes (sig, sig->pinvoke);
745         if (cinfo->struct_ret)
746                 call->used_iregs |= 1 << cinfo->struct_ret;
747
748         for (i = 0; i < n; ++i) {
749                 ainfo = cinfo->args + i;
750                 if (is_virtual && i == 0) {
751                         /* the argument will be attached to the call instrucion */
752                         in = call->args [i];
753                         call->used_iregs |= 1 << ainfo->reg;
754                 } else {
755                         MONO_INST_NEW (cfg, arg, OP_OUTARG);
756                         in = call->args [i];
757                         arg->cil_code = in->cil_code;
758                         arg->inst_left = in;
759                         arg->type = in->type;
760                         /* prepend, we'll need to reverse them later */
761                         arg->next = call->out_args;
762                         call->out_args = arg;
763                         if (ainfo->regtype == RegTypeGeneral) {
764                                 arg->unused = ainfo->reg;
765                                 call->used_iregs |= 1 << ainfo->reg;
766                                 if (arg->type == STACK_I8)
767                                         call->used_iregs |= 1 << (ainfo->reg + 1);
768                         } else if (ainfo->regtype == RegTypeStructByAddr) {
769                                 /* FIXME: where si the data allocated? */
770                                 arg->unused = ainfo->reg;
771                                 call->used_iregs |= 1 << ainfo->reg;
772                         } else if (ainfo->regtype == RegTypeStructByVal) {
773                                 int cur_reg;
774                                 /* mark the used regs */
775                                 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
776                                         call->used_iregs |= 1 << (ainfo->reg + cur_reg);
777                                 }
778                                 arg->opcode = OP_OUTARG_VT;
779                                 arg->unused = ainfo->reg | (ainfo->size << 8) | (ainfo->vtsize << 16);
780                                 arg->inst_imm = ainfo->offset;
781                         } else if (ainfo->regtype == RegTypeBase) {
782                                 arg->opcode = OP_OUTARG;
783                                 arg->unused = ainfo->reg | (ainfo->size << 8);
784                                 arg->inst_imm = ainfo->offset;
785                         } else if (ainfo->regtype == RegTypeFP) {
786                                 arg->opcode = OP_OUTARG_R8;
787                                 arg->unused = ainfo->reg;
788                                 call->used_fregs |= 1 << ainfo->reg;
789                                 if (ainfo->size == 4) {
790                                         arg->opcode = OP_OUTARG_R8;
791                                         /* we reduce the precision */
792                                         /*MonoInst *conv;
793                                         MONO_INST_NEW (cfg, conv, OP_FCONV_TO_R4);
794                                         conv->inst_left = arg->inst_left;
795                                         arg->inst_left = conv;*/
796                                 }
797                         } else {
798                                 g_assert_not_reached ();
799                         }
800                 }
801         }
802         /*
803          * Reverse the call->out_args list.
804          */
805         {
806                 MonoInst *prev = NULL, *list = call->out_args, *next;
807                 while (list) {
808                         next = list->next;
809                         list->next = prev;
810                         prev = list;
811                         list = next;
812                 }
813                 call->out_args = prev;
814         }
815         call->stack_usage = cinfo->stack_usage;
816         cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
817         cfg->flags |= MONO_CFG_HAS_CALLS;
818         /* 
819          * should set more info in call, such as the stack space
820          * used by the args that needs to be added back to esp
821          */
822
823         g_free (cinfo);
824         return call;
825 }
826
827 /*
828  * Allow tracing to work with this interface (with an optional argument)
829  */
830
831 /*
832  * This may be needed on some archs or for debugging support.
833  */
834 void
835 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
836 {
837         /* no stack room needed now (may be needed for FASTCALL-trace support) */
838         *stack = 0;
839         /* split prolog-epilog requirements? */
840         *code = 50; /* max bytes needed: check this number */
841 }
842
843 void*
844 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
845 {
846         guchar *code = p;
847
848         ppc_load (code, ppc_r3, cfg->method);
849         ppc_li (code, ppc_r4, 0); /* NULL ebp for now */
850         ppc_load (code, ppc_r0, func);
851         ppc_mtlr (code, ppc_r0);
852         ppc_blrl (code);
853         return code;
854 }
855
856 enum {
857         SAVE_NONE,
858         SAVE_STRUCT,
859         SAVE_ONE,
860         SAVE_TWO,
861         SAVE_FP
862 };
863
864 void*
865 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
866 {
867         guchar *code = p;
868         int save_mode = SAVE_NONE;
869         MonoMethod *method = cfg->method;
870         int rtype = method->signature->ret->type;
871         int save_offset = PPC_STACK_PARAM_OFFSET + cfg->param_area;
872         save_offset += 15;
873         save_offset &= ~15;
874         
875 handle_enum:
876         switch (rtype) {
877         case MONO_TYPE_VOID:
878                 /* special case string .ctor icall */
879                 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
880                         save_mode = SAVE_ONE;
881                 else
882                         save_mode = SAVE_NONE;
883                 break;
884         case MONO_TYPE_I8:
885         case MONO_TYPE_U8:
886                 save_mode = SAVE_TWO;
887                 break;
888         case MONO_TYPE_R4:
889         case MONO_TYPE_R8:
890                 save_mode = SAVE_FP;
891                 break;
892         case MONO_TYPE_VALUETYPE:
893                 if (method->signature->ret->data.klass->enumtype) {
894                         rtype = method->signature->ret->data.klass->enum_basetype->type;
895                         goto handle_enum;
896                 }
897                 save_mode = SAVE_STRUCT;
898                 break;
899         default:
900                 save_mode = SAVE_ONE;
901                 break;
902         }
903
904         switch (save_mode) {
905         case SAVE_TWO:
906                 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
907                 ppc_stw (code, ppc_r4, save_offset + 4, cfg->frame_reg);
908                 if (enable_arguments) {
909                         ppc_mr (code, ppc_r5, ppc_r4);
910                         ppc_mr (code, ppc_r4, ppc_r3);
911                 }
912                 break;
913         case SAVE_ONE:
914                 ppc_stw (code, ppc_r3, save_offset, cfg->frame_reg);
915                 if (enable_arguments) {
916                         ppc_mr (code, ppc_r4, ppc_r3);
917                 }
918                 break;
919         case SAVE_FP:
920                 ppc_stfd (code, ppc_f1, save_offset, cfg->frame_reg);
921                 if (enable_arguments) {
922                         /* FIXME: what reg?  */
923                         ppc_fmr (code, ppc_f3, ppc_f1);
924                         ppc_lwz (code, ppc_r4, save_offset, cfg->frame_reg);
925                         ppc_lwz (code, ppc_r5, save_offset + 4, cfg->frame_reg);
926                 }
927                 break;
928         case SAVE_STRUCT:
929                 if (enable_arguments) {
930                         /* FIXME: get the actual address  */
931                         ppc_mr (code, ppc_r4, ppc_r3);
932                 }
933                 break;
934         case SAVE_NONE:
935         default:
936                 break;
937         }
938
939         ppc_load (code, ppc_r3, cfg->method);
940         ppc_load (code, ppc_r0, func);
941         ppc_mtlr (code, ppc_r0);
942         ppc_blrl (code);
943
944         switch (save_mode) {
945         case SAVE_TWO:
946                 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
947                 ppc_lwz (code, ppc_r4, save_offset + 4, cfg->frame_reg);
948                 break;
949         case SAVE_ONE:
950                 ppc_lwz (code, ppc_r3, save_offset, cfg->frame_reg);
951                 break;
952         case SAVE_FP:
953                 ppc_lfd (code, ppc_f1, save_offset, cfg->frame_reg);
954                 break;
955         case SAVE_NONE:
956         default:
957                 break;
958         }
959
960         return code;
961 }
962 /*
963  * Conditional branches have a small offset, so if it is likely overflowed,
964  * we do a branch to the end of the method (uncond branches have much larger
965  * offsets) where we perform the conditional and jump back unconditionally.
966  * It's slightly slower, since we add two uncond branches, but it's very simple
967  * with the current patch implementation and such large methods are likely not
968  * going to be perf critical anyway.
969  */
970 typedef struct {
971         MonoBasicBlock *bb;
972         void *ip;
973         guint16 b0_cond;
974         guint16 b1_cond;
975 } MonoOvfJump;
976
977 #define EMIT_COND_BRANCH_FLAGS(ins,b0,b1) \
978 if (ins->flags & MONO_INST_BRLABEL) { \
979         if (0 && ins->inst_i0->inst_c0) { \
980                 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff);  \
981         } else { \
982                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
983                 ppc_bc (code, (b0), (b1), 0);   \
984         } \
985 } else { \
986         if (0 && ins->inst_true_bb->native_offset) { \
987                 ppc_bc (code, (b0), (b1), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
988         } else { \
989                 int br_disp = ins->inst_true_bb->max_offset - offset;   \
990                 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
991                         MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump));    \
992                         ovfj->bb = ins->inst_true_bb;   \
993                         ovfj->ip = NULL;        \
994                         ovfj->b0_cond = (b0);   \
995                         ovfj->b1_cond = (b1);   \
996                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB_OVF, ovfj); \
997                         ppc_b (code, 0);        \
998                 } else {        \
999                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1000                         ppc_bc (code, (b0), (b1), 0);   \
1001                 }       \
1002         } \
1003 }
1004
1005 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_b0_table [(cond)], branch_b1_table [(cond)])
1006
1007 /* emit an exception if condition is fail
1008  *
1009  * We assign the extra code used to throw the implicit exceptions
1010  * to cfg->bb_exit as far as the big branch handling is concerned
1011  */
1012 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(b0,b1,exc_name)            \
1013         do {                                                        \
1014                 int br_disp = cfg->bb_exit->max_offset - offset;        \
1015                 if (!ppc_is_imm16 (br_disp + 1024) || ! ppc_is_imm16 (ppc_is_imm16 (br_disp - 1024))) { \
1016                         MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump));    \
1017                         ovfj->bb = NULL;        \
1018                         ovfj->ip = code;        \
1019                         ovfj->b0_cond = (b0);   \
1020                         ovfj->b1_cond = (b1);   \
1021                         /* FIXME: test this code */     \
1022                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj); \
1023                         ppc_b (code, 0);        \
1024                         cfg->bb_exit->max_offset += 24; \
1025                 } else {        \
1026                         mono_add_patch_info (cfg, code - cfg->native_code,   \
1027                                     MONO_PATCH_INFO_EXC, exc_name);  \
1028                         ppc_bc (code, (b0), (b1), 0);   \
1029                 }       \
1030         } while (0); 
1031
1032 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_b0_table [(cond)], branch_b1_table [(cond)], (exc_name))
1033
1034 static void
1035 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1036 {
1037         MonoInst *ins, *last_ins = NULL;
1038         ins = bb->code;
1039
1040         while (ins) {
1041
1042                 switch (ins->opcode) {
1043                 case OP_MUL_IMM: 
1044                         /* remove unnecessary multiplication with 1 */
1045                         if (ins->inst_imm == 1) {
1046                                 if (ins->dreg != ins->sreg1) {
1047                                         ins->opcode = OP_MOVE;
1048                                 } else {
1049                                         last_ins->next = ins->next;                             
1050                                         ins = ins->next;                                
1051                                         continue;
1052                                 }
1053                         } else {
1054                                 int power2 = mono_is_power_of_two (ins->inst_imm);
1055                                 if (power2 > 0) {
1056                                         ins->opcode = OP_SHL_IMM;
1057                                         ins->inst_imm = power2;
1058                                 }
1059                         }
1060                         break;
1061                 case OP_LOAD_MEMBASE:
1062                 case OP_LOADI4_MEMBASE:
1063                         /* 
1064                          * OP_STORE_MEMBASE_REG reg, offset(basereg) 
1065                          * OP_LOAD_MEMBASE offset(basereg), reg
1066                          */
1067                         if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG 
1068                                          || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1069                             ins->inst_basereg == last_ins->inst_destbasereg &&
1070                             ins->inst_offset == last_ins->inst_offset) {
1071                                 if (ins->dreg == last_ins->sreg1) {
1072                                         last_ins->next = ins->next;                             
1073                                         ins = ins->next;                                
1074                                         continue;
1075                                 } else {
1076                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1077                                         ins->opcode = OP_MOVE;
1078                                         ins->sreg1 = last_ins->sreg1;
1079                                 }
1080
1081                         /* 
1082                          * Note: reg1 must be different from the basereg in the second load
1083                          * OP_LOAD_MEMBASE offset(basereg), reg1
1084                          * OP_LOAD_MEMBASE offset(basereg), reg2
1085                          * -->
1086                          * OP_LOAD_MEMBASE offset(basereg), reg1
1087                          * OP_MOVE reg1, reg2
1088                          */
1089                         } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1090                                            || last_ins->opcode == OP_LOAD_MEMBASE) &&
1091                               ins->inst_basereg != last_ins->dreg &&
1092                               ins->inst_basereg == last_ins->inst_basereg &&
1093                               ins->inst_offset == last_ins->inst_offset) {
1094
1095                                 if (ins->dreg == last_ins->dreg) {
1096                                         last_ins->next = ins->next;                             
1097                                         ins = ins->next;                                
1098                                         continue;
1099                                 } else {
1100                                         ins->opcode = OP_MOVE;
1101                                         ins->sreg1 = last_ins->dreg;
1102                                 }
1103
1104                                 //g_assert_not_reached ();
1105
1106 #if 0
1107                         /* 
1108                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
1109                          * OP_LOAD_MEMBASE offset(basereg), reg
1110                          * -->
1111                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
1112                          * OP_ICONST reg, imm
1113                          */
1114                         } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1115                                                 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1116                                    ins->inst_basereg == last_ins->inst_destbasereg &&
1117                                    ins->inst_offset == last_ins->inst_offset) {
1118                                 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1119                                 ins->opcode = OP_ICONST;
1120                                 ins->inst_c0 = last_ins->inst_imm;
1121                                 g_assert_not_reached (); // check this rule
1122 #endif
1123                         }
1124                         break;
1125                 case OP_LOADU1_MEMBASE:
1126                 case OP_LOADI1_MEMBASE:
1127                         if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1128                                         ins->inst_basereg == last_ins->inst_destbasereg &&
1129                                         ins->inst_offset == last_ins->inst_offset) {
1130                                 if (ins->dreg == last_ins->sreg1) {
1131                                         last_ins->next = ins->next;                             
1132                                         ins = ins->next;                                
1133                                         continue;
1134                                 } else {
1135                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1136                                         ins->opcode = OP_MOVE;
1137                                         ins->sreg1 = last_ins->sreg1;
1138                                 }
1139                         }
1140                         break;
1141                 case OP_LOADU2_MEMBASE:
1142                 case OP_LOADI2_MEMBASE:
1143                         if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1144                                         ins->inst_basereg == last_ins->inst_destbasereg &&
1145                                         ins->inst_offset == last_ins->inst_offset) {
1146                                 if (ins->dreg == last_ins->sreg1) {
1147                                         last_ins->next = ins->next;                             
1148                                         ins = ins->next;                                
1149                                         continue;
1150                                 } else {
1151                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1152                                         ins->opcode = OP_MOVE;
1153                                         ins->sreg1 = last_ins->sreg1;
1154                                 }
1155                         }
1156                         break;
1157                 case CEE_CONV_I4:
1158                 case CEE_CONV_U4:
1159                 case OP_MOVE:
1160                 case OP_SETREG:
1161                         ins->opcode = OP_MOVE;
1162                         /* 
1163                          * OP_MOVE reg, reg 
1164                          */
1165                         if (ins->dreg == ins->sreg1) {
1166                                 if (last_ins)
1167                                         last_ins->next = ins->next;                             
1168                                 ins = ins->next;
1169                                 continue;
1170                         }
1171                         /* 
1172                          * OP_MOVE sreg, dreg 
1173                          * OP_MOVE dreg, sreg
1174                          */
1175                         if (last_ins && last_ins->opcode == OP_MOVE &&
1176                             ins->sreg1 == last_ins->dreg &&
1177                             ins->dreg == last_ins->sreg1) {
1178                                 last_ins->next = ins->next;                             
1179                                 ins = ins->next;                                
1180                                 continue;
1181                         }
1182                         break;
1183                 }
1184                 last_ins = ins;
1185                 ins = ins->next;
1186         }
1187         bb->last_ins = last_ins;
1188 }
1189
1190 /* 
1191  * the branch_b0_table should maintain the order of these
1192  * opcodes.
1193 case CEE_BEQ:
1194 case CEE_BGE:
1195 case CEE_BGT:
1196 case CEE_BLE:
1197 case CEE_BLT:
1198 case CEE_BNE_UN:
1199 case CEE_BGE_UN:
1200 case CEE_BGT_UN:
1201 case CEE_BLE_UN:
1202 case CEE_BLT_UN:
1203  */
1204 static const guchar 
1205 branch_b0_table [] = {
1206         PPC_BR_TRUE, 
1207         PPC_BR_FALSE, 
1208         PPC_BR_TRUE, 
1209         PPC_BR_FALSE, 
1210         PPC_BR_TRUE, 
1211         
1212         PPC_BR_FALSE, 
1213         PPC_BR_FALSE, 
1214         PPC_BR_TRUE, 
1215         PPC_BR_FALSE,
1216         PPC_BR_TRUE
1217 };
1218
1219 static const guchar 
1220 branch_b1_table [] = {
1221         PPC_BR_EQ, 
1222         PPC_BR_LT, 
1223         PPC_BR_GT, 
1224         PPC_BR_GT,
1225         PPC_BR_LT, 
1226         
1227         PPC_BR_EQ, 
1228         PPC_BR_LT, 
1229         PPC_BR_GT, 
1230         PPC_BR_GT,
1231         PPC_BR_LT 
1232 };
1233
1234 /*
1235  * returns the offset used by spillvar. It allocates a new
1236  * spill variable if necessary. 
1237  */
1238 static int
1239 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
1240 {
1241         MonoSpillInfo **si, *info;
1242         int i = 0;
1243
1244         si = &cfg->spill_info; 
1245         
1246         while (i <= spillvar) {
1247
1248                 if (!*si) {
1249                         *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1250                         info->next = NULL;
1251                         info->offset = cfg->stack_offset;
1252                         cfg->stack_offset += sizeof (gpointer);
1253                 }
1254
1255                 if (i == spillvar)
1256                         return (*si)->offset;
1257
1258                 i++;
1259                 si = &(*si)->next;
1260         }
1261
1262         g_assert_not_reached ();
1263         return 0;
1264 }
1265
1266 static int
1267 mono_spillvar_offset_float (MonoCompile *cfg, int spillvar)
1268 {
1269         MonoSpillInfo **si, *info;
1270         int i = 0;
1271
1272         si = &cfg->spill_info_float; 
1273         
1274         while (i <= spillvar) {
1275
1276                 if (!*si) {
1277                         *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
1278                         info->next = NULL;
1279                         cfg->stack_offset += 7;
1280                         cfg->stack_offset &= ~7;
1281                         info->offset = cfg->stack_offset;
1282                         cfg->stack_offset += sizeof (double);
1283                 }
1284
1285                 if (i == spillvar)
1286                         return (*si)->offset;
1287
1288                 i++;
1289                 si = &(*si)->next;
1290         }
1291
1292         g_assert_not_reached ();
1293         return 0;
1294 }
1295
1296 #undef DEBUG
1297 #define DEBUG(a) if (cfg->verbose_level > 1) a
1298 //#define DEBUG(a)
1299 /* use ppc_r3-ppc_10,ppc_r12 as temp registers, f1-f13 for FP registers */
1300 #define PPC_CALLER_REGS ((0xff<<3) | (1<<12) | USE_EXTRA_TEMPS)
1301 #define PPC_CALLER_FREGS (0x3ffe)
1302
1303 #define reg_is_freeable(r) (PPC_CALLER_REGS & 1 << (r))
1304 #define freg_is_freeable(r) ((r) >= 1 && (r) <= 13)
1305
1306 typedef struct {
1307         int born_in;
1308         int killed_in;
1309         int last_use;
1310         int prev_use;
1311 } RegTrack;
1312
1313 static const char*const * ins_spec = ppcg4;
1314
1315 static void
1316 print_ins (int i, MonoInst *ins)
1317 {
1318         const char *spec = ins_spec [ins->opcode];
1319         g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1320         if (spec [MONO_INST_DEST]) {
1321                 if (ins->dreg >= MONO_MAX_IREGS)
1322                         g_print (" R%d <-", ins->dreg);
1323                 else
1324                         g_print (" %s <-", mono_arch_regname (ins->dreg));
1325         }
1326         if (spec [MONO_INST_SRC1]) {
1327                 if (ins->sreg1 >= MONO_MAX_IREGS)
1328                         g_print (" R%d", ins->sreg1);
1329                 else
1330                         g_print (" %s", mono_arch_regname (ins->sreg1));
1331         }
1332         if (spec [MONO_INST_SRC2]) {
1333                 if (ins->sreg2 >= MONO_MAX_IREGS)
1334                         g_print (" R%d", ins->sreg2);
1335                 else
1336                         g_print (" %s", mono_arch_regname (ins->sreg2));
1337         }
1338         if (spec [MONO_INST_CLOB])
1339                 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1340         g_print ("\n");
1341 }
1342
1343 static void
1344 print_regtrack (RegTrack *t, int num)
1345 {
1346         int i;
1347         char buf [32];
1348         const char *r;
1349         
1350         for (i = 0; i < num; ++i) {
1351                 if (!t [i].born_in)
1352                         continue;
1353                 if (i >= MONO_MAX_IREGS) {
1354                         g_snprintf (buf, sizeof(buf), "R%d", i);
1355                         r = buf;
1356                 } else
1357                         r = mono_arch_regname (i);
1358                 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1359         }
1360 }
1361
1362 typedef struct InstList InstList;
1363
1364 struct InstList {
1365         InstList *prev;
1366         InstList *next;
1367         MonoInst *data;
1368 };
1369
1370 static inline InstList*
1371 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1372 {
1373         InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1374         item->data = data;
1375         item->prev = NULL;
1376         item->next = list;
1377         if (list)
1378                 list->prev = item;
1379         return item;
1380 }
1381
1382 /*
1383  * Force the spilling of the variable in the symbolic register 'reg'.
1384  */
1385 static int
1386 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1387 {
1388         MonoInst *load;
1389         int i, sel, spill;
1390         
1391         sel = cfg->rs->iassign [reg];
1392         /*i = cfg->rs->isymbolic [sel];
1393         g_assert (i == reg);*/
1394         i = reg;
1395         spill = ++cfg->spill_count;
1396         cfg->rs->iassign [i] = -spill - 1;
1397         mono_regstate_free_int (cfg->rs, sel);
1398         /* we need to create a spill var and insert a load to sel after the current instruction */
1399         MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1400         load->dreg = sel;
1401         load->inst_basereg = cfg->frame_reg;
1402         load->inst_offset = mono_spillvar_offset (cfg, spill);
1403         if (item->prev) {
1404                 while (ins->next != item->prev->data)
1405                         ins = ins->next;
1406         }
1407         load->next = ins->next;
1408         ins->next = load;
1409         DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1410         i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1411         g_assert (i == sel);
1412
1413         return sel;
1414 }
1415
1416 static int
1417 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1418 {
1419         MonoInst *load;
1420         int i, sel, spill;
1421
1422         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));
1423         /* exclude the registers in the current instruction */
1424         if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1425                 if (ins->sreg1 >= MONO_MAX_IREGS)
1426                         regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1427                 else
1428                         regmask &= ~ (1 << ins->sreg1);
1429                 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1430         }
1431         if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1432                 if (ins->sreg2 >= MONO_MAX_IREGS)
1433                         regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1434                 else
1435                         regmask &= ~ (1 << ins->sreg2);
1436                 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1437         }
1438         if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1439                 regmask &= ~ (1 << ins->dreg);
1440                 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1441         }
1442
1443         DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1444         g_assert (regmask); /* need at least a register we can free */
1445         sel = -1;
1446         /* we should track prev_use and spill the register that's farther */
1447         for (i = 0; i < MONO_MAX_IREGS; ++i) {
1448                 if (regmask & (1 << i)) {
1449                         sel = i;
1450                         DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1451                         break;
1452                 }
1453         }
1454         i = cfg->rs->isymbolic [sel];
1455         spill = ++cfg->spill_count;
1456         cfg->rs->iassign [i] = -spill - 1;
1457         mono_regstate_free_int (cfg->rs, sel);
1458         /* we need to create a spill var and insert a load to sel after the current instruction */
1459         MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1460         load->dreg = sel;
1461         load->inst_basereg = cfg->frame_reg;
1462         load->inst_offset = mono_spillvar_offset (cfg, spill);
1463         if (item->prev) {
1464                 while (ins->next != item->prev->data)
1465                         ins = ins->next;
1466         }
1467         load->next = ins->next;
1468         ins->next = load;
1469         DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1470         i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1471         g_assert (i == sel);
1472         
1473         return sel;
1474 }
1475
1476 static int
1477 get_float_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1478 {
1479         MonoInst *load;
1480         int i, sel, spill;
1481
1482         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));
1483         /* exclude the registers in the current instruction */
1484         if (reg != ins->sreg1 && (freg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg1] >= 0))) {
1485                 if (ins->sreg1 >= MONO_MAX_FREGS)
1486                         regmask &= ~ (1 << cfg->rs->fassign [ins->sreg1]);
1487                 else
1488                         regmask &= ~ (1 << ins->sreg1);
1489                 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1490         }
1491         if (reg != ins->sreg2 && (freg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_FREGS && cfg->rs->fassign [ins->sreg2] >= 0))) {
1492                 if (ins->sreg2 >= MONO_MAX_FREGS)
1493                         regmask &= ~ (1 << cfg->rs->fassign [ins->sreg2]);
1494                 else
1495                         regmask &= ~ (1 << ins->sreg2);
1496                 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1497         }
1498         if (reg != ins->dreg && freg_is_freeable (ins->dreg)) {
1499                 regmask &= ~ (1 << ins->dreg);
1500                 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1501         }
1502
1503         DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1504         g_assert (regmask); /* need at least a register we can free */
1505         sel = -1;
1506         /* we should track prev_use and spill the register that's farther */
1507         for (i = 0; i < MONO_MAX_FREGS; ++i) {
1508                 if (regmask & (1 << i)) {
1509                         sel = i;
1510                         DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->fassign [sel]));
1511                         break;
1512                 }
1513         }
1514         i = cfg->rs->fsymbolic [sel];
1515         spill = ++cfg->spill_count;
1516         cfg->rs->fassign [i] = -spill - 1;
1517         mono_regstate_free_float(cfg->rs, sel);
1518         /* we need to create a spill var and insert a load to sel after the current instruction */
1519         MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
1520         load->dreg = sel;
1521         load->inst_basereg = cfg->frame_reg;
1522         load->inst_offset = mono_spillvar_offset_float (cfg, spill);
1523         if (item->prev) {
1524                 while (ins->next != item->prev->data)
1525                         ins = ins->next;
1526         }
1527         load->next = ins->next;
1528         ins->next = load;
1529         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)));
1530         i = mono_regstate_alloc_float (cfg->rs, 1 << sel);
1531         g_assert (i == sel);
1532         
1533         return sel;
1534 }
1535
1536 static MonoInst*
1537 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1538 {
1539         MonoInst *copy;
1540         MONO_INST_NEW (cfg, copy, OP_MOVE);
1541         copy->dreg = dest;
1542         copy->sreg1 = src;
1543         if (ins) {
1544                 copy->next = ins->next;
1545                 ins->next = copy;
1546         }
1547         DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1548         return copy;
1549 }
1550
1551 static MonoInst*
1552 create_copy_ins_float (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1553 {
1554         MonoInst *copy;
1555         MONO_INST_NEW (cfg, copy, OP_FMOVE);
1556         copy->dreg = dest;
1557         copy->sreg1 = src;
1558         if (ins) {
1559                 copy->next = ins->next;
1560                 ins->next = copy;
1561         }
1562         DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1563         return copy;
1564 }
1565
1566 static MonoInst*
1567 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1568 {
1569         MonoInst *store;
1570         MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1571         store->sreg1 = reg;
1572         store->inst_destbasereg = cfg->frame_reg;
1573         store->inst_offset = mono_spillvar_offset (cfg, spill);
1574         if (ins) {
1575                 store->next = ins->next;
1576                 ins->next = store;
1577         }
1578         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)));
1579         return store;
1580 }
1581
1582 static MonoInst*
1583 create_spilled_store_float (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1584 {
1585         MonoInst *store;
1586         MONO_INST_NEW (cfg, store, OP_STORER8_MEMBASE_REG);
1587         store->sreg1 = reg;
1588         store->inst_destbasereg = cfg->frame_reg;
1589         store->inst_offset = mono_spillvar_offset_float (cfg, spill);
1590         if (ins) {
1591                 store->next = ins->next;
1592                 ins->next = store;
1593         }
1594         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)));
1595         return store;
1596 }
1597
1598 static void
1599 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1600 {
1601         MonoInst *prev;
1602         g_assert (item->next);
1603         prev = item->next->data;
1604
1605         while (prev->next != ins)
1606                 prev = prev->next;
1607         to_insert->next = ins;
1608         prev->next = to_insert;
1609         /* 
1610          * needed otherwise in the next instruction we can add an ins to the 
1611          * end and that would get past this instruction.
1612          */
1613         item->data = to_insert; 
1614 }
1615
1616 static int
1617 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1618 {
1619         int val = cfg->rs->iassign [sym_reg];
1620         if (val < 0) {
1621                 int spill = 0;
1622                 if (val < -1) {
1623                         /* the register gets spilled after this inst */
1624                         spill = -val -1;
1625                 }
1626                 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1627                 if (val < 0)
1628                         val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1629                 cfg->rs->iassign [sym_reg] = val;
1630                 /* add option to store before the instruction for src registers */
1631                 if (spill)
1632                         create_spilled_store (cfg, spill, val, sym_reg, ins);
1633         }
1634         cfg->rs->isymbolic [val] = sym_reg;
1635         return val;
1636 }
1637
1638 /*
1639  * Local register allocation.
1640  * We first scan the list of instructions and we save the liveness info of
1641  * each register (when the register is first used, when it's value is set etc.).
1642  * We also reverse the list of instructions (in the InstList list) because assigning
1643  * registers backwards allows for more tricks to be used.
1644  */
1645 void
1646 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1647 {
1648         MonoInst *ins;
1649         MonoRegState *rs = cfg->rs;
1650         int i, val;
1651         RegTrack *reginfo, *reginfof;
1652         RegTrack *reginfo1, *reginfo2, *reginfod;
1653         InstList *tmp, *reversed = NULL;
1654         const char *spec;
1655         guint32 src1_mask, src2_mask, dest_mask;
1656         guint32 cur_iregs, cur_fregs;
1657
1658         if (!bb->code)
1659                 return;
1660         rs->next_vireg = bb->max_ireg;
1661         rs->next_vfreg = bb->max_freg;
1662         mono_regstate_assign (rs);
1663         reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1664         reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1665         rs->ifree_mask = PPC_CALLER_REGS;
1666         rs->ffree_mask = PPC_CALLER_FREGS;
1667
1668         ins = bb->code;
1669         i = 1;
1670         DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1671         /* forward pass on the instructions to collect register liveness info */
1672         while (ins) {
1673                 spec = ins_spec [ins->opcode];
1674                 DEBUG (print_ins (i, ins));
1675                 /*if (spec [MONO_INST_CLOB] == 'c') {
1676                         MonoCallInst * call = (MonoCallInst*)ins;
1677                         int j;
1678                 }*/
1679                 if (spec [MONO_INST_SRC1]) {
1680                         if (spec [MONO_INST_SRC1] == 'f')
1681                                 reginfo1 = reginfof;
1682                         else
1683                                 reginfo1 = reginfo;
1684                         reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1685                         reginfo1 [ins->sreg1].last_use = i;
1686                 } else {
1687                         ins->sreg1 = -1;
1688                 }
1689                 if (spec [MONO_INST_SRC2]) {
1690                         if (spec [MONO_INST_SRC2] == 'f')
1691                                 reginfo2 = reginfof;
1692                         else
1693                                 reginfo2 = reginfo;
1694                         reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1695                         reginfo2 [ins->sreg2].last_use = i;
1696                 } else {
1697                         ins->sreg2 = -1;
1698                 }
1699                 if (spec [MONO_INST_DEST]) {
1700                         if (spec [MONO_INST_DEST] == 'f')
1701                                 reginfod = reginfof;
1702                         else
1703                                 reginfod = reginfo;
1704                         if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1705                                 reginfod [ins->dreg].killed_in = i;
1706                         reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1707                         reginfod [ins->dreg].last_use = i;
1708                         if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1709                                 reginfod [ins->dreg].born_in = i;
1710                         if (spec [MONO_INST_DEST] == 'l') {
1711                                 /* result in eax:edx, the virtual register is allocated sequentially */
1712                                 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1713                                 reginfod [ins->dreg + 1].last_use = i;
1714                                 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1715                                         reginfod [ins->dreg + 1].born_in = i;
1716                         }
1717                 } else {
1718                         ins->dreg = -1;
1719                 }
1720                 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1721                 ++i;
1722                 ins = ins->next;
1723         }
1724
1725         cur_iregs = PPC_CALLER_REGS;
1726         cur_fregs = PPC_CALLER_FREGS;
1727
1728         DEBUG (print_regtrack (reginfo, rs->next_vireg));
1729         DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1730         tmp = reversed;
1731         while (tmp) {
1732                 int prev_dreg, prev_sreg1, prev_sreg2;
1733                 --i;
1734                 ins = tmp->data;
1735                 spec = ins_spec [ins->opcode];
1736                 DEBUG (g_print ("processing:"));
1737                 DEBUG (print_ins (i, ins));
1738                 /* make the register available for allocation: FIXME add fp reg */
1739                 if (ins->opcode == OP_SETREG || ins->opcode == OP_SETREGIMM) {
1740                         cur_iregs |= 1 << ins->dreg;
1741                         DEBUG (g_print ("adding %d to cur_iregs\n", ins->dreg));
1742                 } else if (ins->opcode == OP_SETFREG) {
1743                         cur_fregs |= 1 << ins->dreg;
1744                         DEBUG (g_print ("adding %d to cur_fregs\n", ins->dreg));
1745                 } else if (spec [MONO_INST_CLOB] == 'c') {
1746                         MonoCallInst *cinst = (MonoCallInst*)ins;
1747                         DEBUG (g_print ("excluding regs 0x%x from cur_iregs (0x%x)\n", cinst->used_iregs, cur_iregs));
1748                         DEBUG (g_print ("excluding fpregs 0x%x from cur_fregs (0x%x)\n", cinst->used_fregs, cur_fregs));
1749                         cur_iregs &= ~cinst->used_iregs;
1750                         cur_fregs &= ~cinst->used_fregs;
1751                         DEBUG (g_print ("available cur_iregs: 0x%x\n", cur_iregs));
1752                         DEBUG (g_print ("available cur_fregs: 0x%x\n", cur_fregs));
1753                         /* registers used by the calling convention are excluded from 
1754                          * allocation: they will be selectively enabled when they are 
1755                          * assigned by the special SETREG opcodes.
1756                          */
1757                 }
1758                 dest_mask = src1_mask = src2_mask = cur_iregs;
1759                 /* update for use with FP regs... */
1760                 if (spec [MONO_INST_DEST] == 'f') {
1761                         dest_mask = cur_fregs;
1762                         if (ins->dreg >= MONO_MAX_FREGS) {
1763                                 val = rs->fassign [ins->dreg];
1764                                 prev_dreg = ins->dreg;
1765                                 if (val < 0) {
1766                                         int spill = 0;
1767                                         if (val < -1) {
1768                                                 /* the register gets spilled after this inst */
1769                                                 spill = -val -1;
1770                                         }
1771                                         val = mono_regstate_alloc_float (rs, dest_mask);
1772                                         if (val < 0)
1773                                                 val = get_float_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1774                                         rs->fassign [ins->dreg] = val;
1775                                         if (spill)
1776                                                 create_spilled_store_float (cfg, spill, val, prev_dreg, ins);
1777                                 }
1778                                 DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1779                                 rs->fsymbolic [val] = prev_dreg;
1780                                 ins->dreg = val;
1781                                 if (spec [MONO_INST_CLOB] == 'c' && ins->dreg != ppc_f1) {
1782                                         /* this instruction only outputs to ppc_f1, need to copy */
1783                                         create_copy_ins_float (cfg, ins->dreg, ppc_f1, ins);
1784                                 }
1785                         } else {
1786                                 prev_dreg = -1;
1787                         }
1788                         if (freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i || !(cur_fregs & (1 << ins->dreg)))) {
1789                                 DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1790                                 mono_regstate_free_float (rs, ins->dreg);
1791                         }
1792                 } else if (ins->dreg >= MONO_MAX_IREGS) {
1793                         val = rs->iassign [ins->dreg];
1794                         prev_dreg = ins->dreg;
1795                         if (val < 0) {
1796                                 int spill = 0;
1797                                 if (val < -1) {
1798                                         /* the register gets spilled after this inst */
1799                                         spill = -val -1;
1800                                 }
1801                                 val = mono_regstate_alloc_int (rs, dest_mask);
1802                                 if (val < 0)
1803                                         val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1804                                 rs->iassign [ins->dreg] = val;
1805                                 if (spill)
1806                                         create_spilled_store (cfg, spill, val, prev_dreg, ins);
1807                         }
1808                         DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1809                         rs->isymbolic [val] = prev_dreg;
1810                         ins->dreg = val;
1811                         if (spec [MONO_INST_DEST] == 'l') {
1812                                 int hreg = prev_dreg + 1;
1813                                 val = rs->iassign [hreg];
1814                                 if (val < 0) {
1815                                         int spill = 0;
1816                                         if (val < -1) {
1817                                                 /* the register gets spilled after this inst */
1818                                                 spill = -val -1;
1819                                         }
1820                                         val = mono_regstate_alloc_int (rs, dest_mask);
1821                                         if (val < 0)
1822                                                 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1823                                         rs->iassign [hreg] = val;
1824                                         if (spill)
1825                                                 create_spilled_store (cfg, spill, val, hreg, ins);
1826                                 }
1827                                 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1828                                 rs->isymbolic [val] = hreg;
1829                                 /* FIXME:? ins->dreg = val; */
1830                                 if (ins->dreg == ppc_r4) {
1831                                         if (val != ppc_r3)
1832                                                 create_copy_ins (cfg, val, ppc_r3, ins);
1833                                 } else if (ins->dreg == ppc_r3) {
1834                                         if (val == ppc_r4) {
1835                                                 /* swap */
1836                                                 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1837                                                 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1838                                                 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1839                                         } else {
1840                                                 /* two forced copies */
1841                                                 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1842                                                 create_copy_ins (cfg, val, ppc_r3, ins);
1843                                         }
1844                                 } else {
1845                                         if (val == ppc_r3) {
1846                                                 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1847                                         } else {
1848                                                 /* two forced copies */
1849                                                 create_copy_ins (cfg, val, ppc_r3, ins);
1850                                                 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1851                                         }
1852                                 }
1853                                 if (reg_is_freeable (val) && hreg >= 0 && (reginfo [hreg].born_in >= i && !(cur_iregs & (1 << val)))) {
1854                                         DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1855                                         mono_regstate_free_int (rs, val);
1856                                 }
1857                         } else if (spec [MONO_INST_DEST] == 'a' && ins->dreg != ppc_r3 && spec [MONO_INST_CLOB] != 'd') {
1858                                 /* this instruction only outputs to ppc_r3, need to copy */
1859                                 create_copy_ins (cfg, ins->dreg, ppc_r3, ins);
1860                         }
1861                 } else {
1862                         prev_dreg = -1;
1863                 }
1864                 if (spec [MONO_INST_DEST] == 'f' && freg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfof [prev_dreg].born_in >= i)) {
1865                         DEBUG (g_print ("\tfreeable float %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfof [prev_dreg].born_in));
1866                         mono_regstate_free_float (rs, ins->dreg);
1867                 } else if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && (reginfo [prev_dreg].born_in >= i)) {
1868                         DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1869                         mono_regstate_free_int (rs, ins->dreg);
1870                 }
1871                 if (spec [MONO_INST_SRC1] == 'f') {
1872                         src1_mask = cur_fregs;
1873                         if (ins->sreg1 >= MONO_MAX_FREGS) {
1874                                 val = rs->fassign [ins->sreg1];
1875                                 prev_sreg1 = ins->sreg1;
1876                                 if (val < 0) {
1877                                         int spill = 0;
1878                                         if (val < -1) {
1879                                                 /* the register gets spilled after this inst */
1880                                                 spill = -val -1;
1881                                         }
1882                                         //g_assert (val == -1); /* source cannot be spilled */
1883                                         val = mono_regstate_alloc_float (rs, src1_mask);
1884                                         if (val < 0)
1885                                                 val = get_float_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1886                                         rs->fassign [ins->sreg1] = val;
1887                                         DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1888                                         if (spill) {
1889                                                 MonoInst *store = create_spilled_store_float (cfg, spill, val, prev_sreg1, NULL);
1890                                                 insert_before_ins (ins, tmp, store);
1891                                         }
1892                                 }
1893                                 rs->fsymbolic [val] = prev_sreg1;
1894                                 ins->sreg1 = val;
1895                         } else {
1896                                 prev_sreg1 = -1;
1897                         }
1898                 } else if (ins->sreg1 >= MONO_MAX_IREGS) {
1899                         val = rs->iassign [ins->sreg1];
1900                         prev_sreg1 = ins->sreg1;
1901                         if (val < 0) {
1902                                 int spill = 0;
1903                                 if (val < -1) {
1904                                         /* the register gets spilled after this inst */
1905                                         spill = -val -1;
1906                                 }
1907                                 if (0 && ins->opcode == OP_MOVE) {
1908                                         /* 
1909                                          * small optimization: the dest register is already allocated
1910                                          * but the src one is not: we can simply assign the same register
1911                                          * here and peephole will get rid of the instruction later.
1912                                          * This optimization may interfere with the clobbering handling:
1913                                          * it removes a mov operation that will be added again to handle clobbering.
1914                                          * There are also some other issues that should with make testjit.
1915                                          */
1916                                         mono_regstate_alloc_int (rs, 1 << ins->dreg);
1917                                         val = rs->iassign [ins->sreg1] = ins->dreg;
1918                                         //g_assert (val >= 0);
1919                                         DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1920                                 } else {
1921                                         //g_assert (val == -1); /* source cannot be spilled */
1922                                         val = mono_regstate_alloc_int (rs, src1_mask);
1923                                         if (val < 0)
1924                                                 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1925                                         rs->iassign [ins->sreg1] = val;
1926                                         DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1927                                 }
1928                                 if (spill) {
1929                                         MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1930                                         insert_before_ins (ins, tmp, store);
1931                                 }
1932                         }
1933                         rs->isymbolic [val] = prev_sreg1;
1934                         ins->sreg1 = val;
1935                 } else {
1936                         prev_sreg1 = -1;
1937                 }
1938                 if (spec [MONO_INST_SRC2] == 'f') {
1939                         src2_mask = cur_fregs;
1940                         if (ins->sreg2 >= MONO_MAX_FREGS) {
1941                                 val = rs->fassign [ins->sreg2];
1942                                 prev_sreg2 = ins->sreg2;
1943                                 if (val < 0) {
1944                                         int spill = 0;
1945                                         if (val < -1) {
1946                                                 /* the register gets spilled after this inst */
1947                                                 spill = -val -1;
1948                                         }
1949                                         val = mono_regstate_alloc_float (rs, src2_mask);
1950                                         if (val < 0)
1951                                                 val = get_float_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1952                                         rs->fassign [ins->sreg2] = val;
1953                                         DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1954                                         if (spill)
1955                                                 create_spilled_store_float (cfg, spill, val, prev_sreg2, ins);
1956                                 }
1957                                 rs->fsymbolic [val] = prev_sreg2;
1958                                 ins->sreg2 = val;
1959                         } else {
1960                                 prev_sreg2 = -1;
1961                         }
1962                 } else if (ins->sreg2 >= MONO_MAX_IREGS) {
1963                         val = rs->iassign [ins->sreg2];
1964                         prev_sreg2 = ins->sreg2;
1965                         if (val < 0) {
1966                                 int spill = 0;
1967                                 if (val < -1) {
1968                                         /* the register gets spilled after this inst */
1969                                         spill = -val -1;
1970                                 }
1971                                 val = mono_regstate_alloc_int (rs, src2_mask);
1972                                 if (val < 0)
1973                                         val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1974                                 rs->iassign [ins->sreg2] = val;
1975                                 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1976                                 if (spill)
1977                                         create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1978                         }
1979                         rs->isymbolic [val] = prev_sreg2;
1980                         ins->sreg2 = val;
1981                 } else {
1982                         prev_sreg2 = -1;
1983                 }
1984
1985                 if (spec [MONO_INST_CLOB] == 'c') {
1986                         int j, s;
1987                         guint32 clob_mask = PPC_CALLER_REGS;
1988                         for (j = 0; j < MONO_MAX_IREGS; ++j) {
1989                                 s = 1 << j;
1990                                 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1991                                         //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1992                                 }
1993                         }
1994                 }
1995                 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1996                         DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1997                         mono_regstate_free_int (rs, ins->sreg1);
1998                 }
1999                 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2000                         DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2001                         mono_regstate_free_int (rs, ins->sreg2);
2002                 }*/
2003                 
2004                 //DEBUG (print_ins (i, ins));
2005                 tmp = tmp->next;
2006         }
2007 }
2008
2009 static guchar*
2010 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2011 {
2012         /* sreg is a float, dreg is an integer reg. ppc_f0 is used a scratch */
2013         ppc_fctiwz (code, ppc_f0, sreg);
2014         ppc_stfd (code, ppc_f0, -8, ppc_sp);
2015         ppc_lwz (code, dreg, -4, ppc_sp);
2016         if (!is_signed) {
2017                 if (size == 1)
2018                         ppc_andid (code, dreg, dreg, 0xff);
2019                 else if (size == 2)
2020                         ppc_andid (code, dreg, dreg, 0xffff);
2021         } else {
2022                 if (size == 1)
2023                         ppc_extsb (code, dreg, dreg);
2024                 else if (size == 2)
2025                         ppc_extsh (code, dreg, dreg);
2026         }
2027         return code;
2028 }
2029
2030 static unsigned char*
2031 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
2032 {
2033 #if 0
2034         int sreg = tree->sreg1;
2035         x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
2036         if (tree->flags & MONO_INST_INIT) {
2037                 int offset = 0;
2038                 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
2039                         x86_push_reg (code, X86_EAX);
2040                         offset += 4;
2041                 }
2042                 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
2043                         x86_push_reg (code, X86_ECX);
2044                         offset += 4;
2045                 }
2046                 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
2047                         x86_push_reg (code, X86_EDI);
2048                         offset += 4;
2049                 }
2050                 
2051                 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
2052                 if (sreg != X86_ECX)
2053                         x86_mov_reg_reg (code, X86_ECX, sreg, 4);
2054                 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
2055                                 
2056                 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
2057                 x86_cld (code);
2058                 x86_prefix (code, X86_REP_PREFIX);
2059                 x86_stosl (code);
2060                 
2061                 if (tree->dreg != X86_EDI && sreg != X86_EDI)
2062                         x86_pop_reg (code, X86_EDI);
2063                 if (tree->dreg != X86_ECX && sreg != X86_ECX)
2064                         x86_pop_reg (code, X86_ECX);
2065                 if (tree->dreg != X86_EAX && sreg != X86_EAX)
2066                         x86_pop_reg (code, X86_EAX);
2067         }
2068 #endif
2069         return code;
2070 }
2071
2072 typedef struct {
2073         guchar *code;
2074         guchar *target;
2075         int absolute;
2076         int found;
2077 } PatchData;
2078
2079 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
2080
2081 static int
2082 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
2083         PatchData *pdata = (PatchData*)user_data;
2084         guchar *code = data;
2085         guint32 *thunks = data;
2086         guint32 *endthunks = (guint32*)(code + bsize);
2087         guint32 load [2];
2088         guchar *templ;
2089         int i, count = 0;
2090         int difflow, diffhigh;
2091
2092         /* always ensure a call from pdata->code can reach to the thunks without further thunks */
2093         difflow = (char*)pdata->code - (char*)thunks;
2094         diffhigh = (char*)pdata->code - (char*)endthunks;
2095         if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
2096                 return 0;
2097
2098         templ = (guchar*)load;
2099         ppc_lis (templ, ppc_r0, (guint32)(pdata->target) >> 16);
2100         ppc_ori (templ, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2101
2102         //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
2103         if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
2104                 while (thunks < endthunks) {
2105                         //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
2106                         if ((thunks [0] == load [0]) && (thunks [1] == load [1])) {
2107                                 ppc_patch (pdata->code, (guchar*)thunks);
2108                                 mono_arch_flush_icache (pdata->code, 4);
2109                                 pdata->found = 1;
2110                                 return 1;
2111                         } else if ((thunks [0] == 0) && (thunks [1] == 0)) {
2112                                 /* found a free slot instead: emit thunk */
2113                                 code = (guchar*)thunks;
2114                                 ppc_lis (code, ppc_r0, (guint32)(pdata->target) >> 16);
2115                                 ppc_ori (code, ppc_r0, ppc_r0, (guint32)(pdata->target) & 0xffff);
2116                                 ppc_mtctr (code, ppc_r0);
2117                                 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2118                                 mono_arch_flush_icache ((guchar*)thunks, 16);
2119
2120                                 ppc_patch (pdata->code, (guchar*)thunks);
2121                                 mono_arch_flush_icache (pdata->code, 4);
2122                                 pdata->found = 1;
2123                                 return 1;
2124                         }
2125                         /* skip 16 bytes, the size of the thunk */
2126                         thunks += 4;
2127                         count++;
2128                 }
2129                 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
2130         }
2131         return 0;
2132 }
2133
2134 static void
2135 handle_thunk (int absolute, guchar *code, guchar *target) {
2136         MonoDomain *domain = mono_domain_get ();
2137         PatchData pdata;
2138
2139         pdata.code = code;
2140         pdata.target = target;
2141         pdata.absolute = absolute;
2142         pdata.found = 0;
2143
2144         mono_domain_lock (domain);
2145         mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2146
2147         if (!pdata.found) {
2148                 /* this uses the first available slot */
2149                 pdata.found = 2;
2150                 mono_code_manager_foreach (domain->code_mp, search_thunk_slot, &pdata);
2151         }
2152         mono_domain_unlock (domain);
2153
2154         if (pdata.found != 1)
2155                 g_print ("thunk failed for %p from %p\n", target, code);
2156         g_assert (pdata.found == 1);
2157 }
2158
2159 void
2160 ppc_patch (guchar *code, guchar *target)
2161 {
2162         guint32 ins = *(guint32*)code;
2163         guint32 prim = ins >> 26;
2164         guint32 ovf;
2165
2166         //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2167         if (prim == 18) {
2168                 // prefer relative branches, they are more position independent (e.g. for AOT compilation).
2169                 gint diff = target - code;
2170                 if (diff >= 0){
2171                         if (diff <= 33554431){
2172                                 ins = (18 << 26) | (diff) | (ins & 1);
2173                                 *(guint32*)code = ins;
2174                                 return;
2175                         }
2176                 } else {
2177                         /* diff between 0 and -33554432 */
2178                         if (diff >= -33554432){
2179                                 ins = (18 << 26) | (diff & ~0xfc000000) | (ins & 1);
2180                                 *(guint32*)code = ins;
2181                                 return;
2182                         }
2183                 }
2184                 
2185                 if ((glong)target >= 0){
2186                         if ((glong)target <= 33554431){
2187                                 ins = (18 << 26) | ((guint32) target) | (ins & 1) | 2;
2188                                 *(guint32*)code = ins;
2189                                 return;
2190                         }
2191                 } else {
2192                         if ((glong)target >= -33554432){
2193                                 ins = (18 << 26) | (((guint32)target) & ~0xfc000000) | (ins & 1) | 2;
2194                                 *(guint32*)code = ins;
2195                                 return;
2196                         }
2197                 }
2198
2199                 handle_thunk (TRUE, code, target);
2200                 return;
2201
2202                 g_assert_not_reached ();
2203         }
2204         
2205         
2206         if (prim == 16) {
2207                 // absolute address
2208                 if (ins & 2) {
2209                         guint32 li = (guint32)target;
2210                         ins = (ins & 0xffff0000) | (ins & 3);
2211                         ovf  = li & 0xffff0000;
2212                         if (ovf != 0 && ovf != 0xffff0000)
2213                                 g_assert_not_reached ();
2214                         li &= 0xffff;
2215                         ins |= li;
2216                         // FIXME: assert the top bits of li are 0
2217                 } else {
2218                         gint diff = target - code;
2219                         ins = (ins & 0xffff0000) | (ins & 3);
2220                         ovf  = diff & 0xffff0000;
2221                         if (ovf != 0 && ovf != 0xffff0000)
2222                                 g_assert_not_reached ();
2223                         diff &= 0xffff;
2224                         ins |= diff;
2225                 }
2226                 *(guint32*)code = ins;
2227         } else {
2228                 g_assert_not_reached ();
2229         }
2230 //      g_print ("patched with 0x%08x\n", ins);
2231 }
2232
2233 void
2234 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2235 {
2236         MonoInst *ins;
2237         MonoCallInst *call;
2238         guint offset;
2239         guint8 *code = cfg->native_code + cfg->code_len;
2240         MonoInst *last_ins = NULL;
2241         guint last_offset = 0;
2242         int max_len, cpos;
2243
2244         if (cfg->opt & MONO_OPT_PEEPHOLE)
2245                 peephole_pass (cfg, bb);
2246
2247         /* we don't align basic blocks of loops on ppc */
2248
2249         if (cfg->verbose_level > 2)
2250                 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2251
2252         cpos = bb->max_offset;
2253
2254         if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2255                 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
2256                 //g_assert (!mono_compile_aot);
2257                 //cpos += 6;
2258                 //if (bb->cil_code)
2259                 //      cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
2260                 /* this is not thread save, but good enough */
2261                 /* fixme: howto handle overflows? */
2262                 //x86_inc_mem (code, &cov->data [bb->dfn].count); 
2263         }
2264
2265         ins = bb->code;
2266         while (ins) {
2267                 offset = code - cfg->native_code;
2268
2269                 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2270
2271                 if (offset > (cfg->code_size - max_len - 16)) {
2272                         cfg->code_size *= 2;
2273                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2274                         code = cfg->native_code + offset;
2275                 }
2276         //      if (ins->cil_code)
2277         //              g_print ("cil code\n");
2278                 mono_debug_record_line_number (cfg, ins, offset);
2279
2280                 switch (ins->opcode) {
2281                 case OP_BIGMUL:
2282                         ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2283                         ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
2284                         break;
2285                 case OP_BIGMUL_UN:
2286                         ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
2287                         ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
2288                         break;
2289                 case OP_STOREI1_MEMBASE_IMM:
2290                         ppc_li (code, ppc_r0, ins->inst_imm);
2291                         if (ppc_is_imm16 (ins->inst_offset)) {
2292                                 ppc_stb (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2293                         } else {
2294                                 ppc_load (code, ppc_r11, ins->inst_offset);
2295                                 ppc_stbx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2296                         }
2297                         break;
2298                 case OP_STOREI2_MEMBASE_IMM:
2299                         ppc_li (code, ppc_r0, ins->inst_imm);
2300                         if (ppc_is_imm16 (ins->inst_offset)) {
2301                                 ppc_sth (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2302                         } else {
2303                                 ppc_load (code, ppc_r11, ins->inst_offset);
2304                                 ppc_sthx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2305                         }
2306                         break;
2307                 case OP_STORE_MEMBASE_IMM:
2308                 case OP_STOREI4_MEMBASE_IMM:
2309                         ppc_load (code, ppc_r0, ins->inst_imm);
2310                         if (ppc_is_imm16 (ins->inst_offset)) {
2311                                 ppc_stw (code, ppc_r0, ins->inst_offset, ins->inst_destbasereg);
2312                         } else {
2313                                 ppc_load (code, ppc_r11, ins->inst_offset);
2314                                 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_destbasereg);
2315                         }
2316                         break;
2317                 case OP_STOREI1_MEMBASE_REG:
2318                         if (ppc_is_imm16 (ins->inst_offset)) {
2319                                 ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2320                         } else {
2321                                 ppc_load (code, ppc_r11, ins->inst_offset);
2322                                 ppc_stbx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2323                         }
2324                         break;
2325                 case OP_STOREI2_MEMBASE_REG:
2326                         if (ppc_is_imm16 (ins->inst_offset)) {
2327                                 ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2328                         } else {
2329                                 ppc_load (code, ppc_r11, ins->inst_offset);
2330                                 ppc_sthx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2331                         }
2332                         break;
2333                 case OP_STORE_MEMBASE_REG:
2334                 case OP_STOREI4_MEMBASE_REG:
2335                         if (ppc_is_imm16 (ins->inst_offset)) {
2336                                 ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2337                         } else {
2338                                 ppc_load (code, ppc_r11, ins->inst_offset);
2339                                 ppc_stwx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2340                         }
2341                         break;
2342                 case CEE_LDIND_I:
2343                 case CEE_LDIND_I4:
2344                 case CEE_LDIND_U4:
2345                         g_assert_not_reached ();
2346                         //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
2347                         break;
2348                 case OP_LOADU4_MEM:
2349                         g_assert_not_reached ();
2350                         //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
2351                         //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
2352                         break;
2353                 case OP_LOAD_MEMBASE:
2354                 case OP_LOADI4_MEMBASE:
2355                 case OP_LOADU4_MEMBASE:
2356                         if (ppc_is_imm16 (ins->inst_offset)) {
2357                                 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2358                         } else {
2359                                 ppc_load (code, ppc_r11, ins->inst_offset);
2360                                 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2361                         }
2362                         break;
2363                 case OP_LOADI1_MEMBASE:
2364                 case OP_LOADU1_MEMBASE:
2365                         if (ppc_is_imm16 (ins->inst_offset)) {
2366                                 ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2367                         } else {
2368                                 ppc_load (code, ppc_r11, ins->inst_offset);
2369                                 ppc_lbzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2370                         }
2371                         if (ins->opcode == OP_LOADI1_MEMBASE)
2372                                 ppc_extsb (code, ins->dreg, ins->dreg);
2373                         break;
2374                 case OP_LOADU2_MEMBASE:
2375                         if (ppc_is_imm16 (ins->inst_offset)) {
2376                                 ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2377                         } else {
2378                                 ppc_load (code, ppc_r11, ins->inst_offset);
2379                                 ppc_lhzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2380                         }
2381                         break;
2382                 case OP_LOADI2_MEMBASE:
2383                         if (ppc_is_imm16 (ins->inst_offset)) {
2384                                 ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
2385                         } else {
2386                                 ppc_load (code, ppc_r11, ins->inst_offset);
2387                                 ppc_lhax (code, ins->dreg, ppc_r11, ins->inst_basereg);
2388                         }
2389                         break;
2390                 case CEE_CONV_I1:
2391                         ppc_extsb (code, ins->dreg, ins->sreg1);
2392                         break;
2393                 case CEE_CONV_I2:
2394                         ppc_extsh (code, ins->dreg, ins->sreg1);
2395                         break;
2396                 case CEE_CONV_U1:
2397                         ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
2398                         break;
2399                 case CEE_CONV_U2:
2400                         ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
2401                         break;
2402                 case OP_COMPARE:
2403                         if (ins->next && 
2404                                         ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2405                                         (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2406                                         (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN)))
2407                                 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
2408                         else
2409                                 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2410                         break;
2411                 case OP_COMPARE_IMM:
2412                         if (ins->next && 
2413                                         ((ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN) ||
2414                                         (ins->next->opcode >= OP_COND_EXC_NE_UN && ins->next->opcode <= OP_COND_EXC_LT_UN) ||
2415                                         (ins->next->opcode == OP_CLT_UN || ins->next->opcode == OP_CGT_UN))) {
2416                                 if (ppc_is_uimm16 (ins->inst_imm)) {
2417                                         ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2418                                 } else {
2419                                         ppc_load (code, ppc_r11, ins->inst_imm);
2420                                         ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2421                                 }
2422                         } else {
2423                                 if (ppc_is_imm16 (ins->inst_imm)) {
2424                                         ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2425                                 } else {
2426                                         ppc_load (code, ppc_r11, ins->inst_imm);
2427                                         ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2428                                 }
2429                         }
2430                         break;
2431                 case OP_X86_TEST_NULL:
2432                         ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2433                         break;
2434                 case CEE_BREAK:
2435                         ppc_break (code);
2436                         break;
2437                 case OP_ADDCC:
2438                         ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2439                         break;
2440                 case CEE_ADD:
2441                         ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2442                         break;
2443                 case OP_ADC:
2444                         ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2445                         break;
2446                 case OP_ADDCC_IMM:
2447                         if (ppc_is_imm16 (ins->inst_imm)) {
2448                                 ppc_addic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2449                         } else {
2450                                 ppc_load (code, ppc_r11, ins->inst_imm);
2451                                 ppc_addc (code, ins->dreg, ins->sreg1, ppc_r11);
2452                         }
2453                         break;
2454                 case OP_ADD_IMM:
2455                         if (ppc_is_imm16 (ins->inst_imm)) {
2456                                 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2457                         } else {
2458                                 ppc_load (code, ppc_r11, ins->inst_imm);
2459                                 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2460                         }
2461                         break;
2462                 case OP_ADC_IMM:
2463                         ppc_load (code, ppc_r11, ins->inst_imm);
2464                         ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2465                         break;
2466                 case CEE_ADD_OVF:
2467                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2468                          */
2469                         ppc_addo (code, ins->dreg, ins->sreg1, ins->sreg2);
2470                         ppc_mfspr (code, ppc_r0, ppc_xer);
2471                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2472                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2473                         break;
2474                 case CEE_ADD_OVF_UN:
2475                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2476                          */
2477                         ppc_addco (code, ins->dreg, ins->sreg1, ins->sreg2);
2478                         ppc_mfspr (code, ppc_r0, ppc_xer);
2479                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2480                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2481                         break;
2482                 case CEE_SUB_OVF:
2483                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2484                          */
2485                         ppc_subfo (code, ins->dreg, ins->sreg2, ins->sreg1);
2486                         ppc_mfspr (code, ppc_r0, ppc_xer);
2487                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2488                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2489                         break;
2490                 case CEE_SUB_OVF_UN:
2491                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2492                          */
2493                         ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2494                         ppc_mfspr (code, ppc_r0, ppc_xer);
2495                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2496                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2497                         break;
2498                 case OP_ADD_OVF_CARRY:
2499                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2500                          */
2501                         ppc_addeo (code, ins->dreg, ins->sreg1, ins->sreg2);
2502                         ppc_mfspr (code, ppc_r0, ppc_xer);
2503                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2504                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2505                         break;
2506                 case OP_ADD_OVF_UN_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<<13));
2512                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2513                         break;
2514                 case OP_SUB_OVF_CARRY:
2515                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2516                          */
2517                         ppc_subfeo (code, ins->dreg, ins->sreg2, ins->sreg1);
2518                         ppc_mfspr (code, ppc_r0, ppc_xer);
2519                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2520                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2521                         break;
2522                 case OP_SUB_OVF_UN_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<<13));
2528                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2529                         break;
2530                 case OP_SUBCC:
2531                         ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2532                         break;
2533                 case OP_SUBCC_IMM:
2534                         ppc_load (code, ppc_r11, ins->inst_imm);
2535                         ppc_subfc (code, ins->dreg, ppc_r11, ins->sreg1);
2536                         break;
2537                 case CEE_SUB:
2538                         ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2539                         break;
2540                 case OP_SBB:
2541                         ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2542                         break;
2543                 case OP_SUB_IMM:
2544                         // we add the negated value
2545                         if (ppc_is_imm16 (-ins->inst_imm))
2546                                 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2547                         else {
2548                                 ppc_load (code, ppc_r11, ins->inst_imm);
2549                                 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2550                         }
2551                         break;
2552                 case OP_SBB_IMM:
2553                         ppc_load (code, ppc_r11, ins->inst_imm);
2554                         ppc_subfe (code, ins->dreg, ppc_r11, ins->sreg1);
2555                         break;
2556                 case OP_PPC_SUBFIC:
2557                         g_assert (ppc_is_imm16 (ins->inst_imm));
2558                         ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2559                         break;
2560                 case OP_PPC_SUBFZE:
2561                         ppc_subfze (code, ins->dreg, ins->sreg1);
2562                         break;
2563                 case CEE_AND:
2564                         /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2565                         ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2566                         break;
2567                 case OP_AND_IMM:
2568                         if (!(ins->inst_imm & 0xffff0000)) {
2569                                 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2570                         } else if (!(ins->inst_imm & 0xffff)) {
2571                                 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2572                         } else {
2573                                 ppc_load (code, ppc_r11, ins->inst_imm);
2574                                 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2575                         }
2576                         break;
2577                 case CEE_DIV:
2578                          /* XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2579                          */
2580                         ppc_divwod (code, ins->dreg, ins->sreg1, ins->sreg2);
2581                         ppc_mfspr (code, ppc_r0, ppc_xer);
2582                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2583                         /* FIXME: use OverflowException for 0x80000000/-1 */
2584                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2585                         break;
2586                 case CEE_DIV_UN:
2587                         ppc_divwuod (code, ins->dreg, ins->sreg1, ins->sreg2);
2588                         ppc_mfspr (code, ppc_r0, ppc_xer);
2589                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2590                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2591                         break;
2592                 case OP_DIV_IMM:
2593                         g_assert_not_reached ();
2594 #if 0
2595                         ppc_load (code, ppc_r11, ins->inst_imm);
2596                         ppc_divwod (code, ins->dreg, ins->sreg1, ppc_r11);
2597                         ppc_mfspr (code, ppc_r0, ppc_xer);
2598                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2599                         /* FIXME: use OverflowException for 0x80000000/-1 */
2600                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2601                         break;
2602 #endif
2603                 case CEE_REM:
2604                         ppc_divwod (code, ppc_r11, ins->sreg1, ins->sreg2);
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                         ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2610                         ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2611                         break;
2612                 case CEE_REM_UN:
2613                         ppc_divwuod (code, ppc_r11, ins->sreg1, ins->sreg2);
2614                         ppc_mfspr (code, ppc_r0, ppc_xer);
2615                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
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 OP_REM_IMM:
2621                         g_assert_not_reached ();
2622                 case CEE_OR:
2623                         ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2624                         break;
2625                 case OP_OR_IMM:
2626                         if (!(ins->inst_imm & 0xffff0000)) {
2627                                 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2628                         } else if (!(ins->inst_imm & 0xffff)) {
2629                                 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2630                         } else {
2631                                 ppc_load (code, ppc_r11, ins->inst_imm);
2632                                 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2633                         }
2634                         break;
2635                 case CEE_XOR:
2636                         ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2637                         break;
2638                 case OP_XOR_IMM:
2639                         if (!(ins->inst_imm & 0xffff0000)) {
2640                                 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2641                         } else if (!(ins->inst_imm & 0xffff)) {
2642                                 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2643                         } else {
2644                                 ppc_load (code, ppc_r11, ins->inst_imm);
2645                                 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2646                         }
2647                         break;
2648                 case CEE_SHL:
2649                         ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2650                         break;
2651                 case OP_SHL_IMM:
2652                         ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2653                         //ppc_load (code, ppc_r11, ins->inst_imm);
2654                         //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2655                         break;
2656                 case CEE_SHR:
2657                         ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2658                         break;
2659                 case OP_SHR_IMM:
2660                         // there is also ppc_srawi
2661                         //ppc_load (code, ppc_r11, ins->inst_imm);
2662                         //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2663                         ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2664                         break;
2665                 case OP_SHR_UN_IMM:
2666                         /*ppc_load (code, ppc_r11, ins->inst_imm);
2667                         ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);*/
2668                         ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0x1f)), (ins->inst_imm & 0x1f), 31);
2669                         break;
2670                 case CEE_SHR_UN:
2671                         ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2672                         break;
2673                 case CEE_NOT:
2674                         ppc_not (code, ins->dreg, ins->sreg1);
2675                         break;
2676                 case CEE_NEG:
2677                         ppc_neg (code, ins->dreg, ins->sreg1);
2678                         break;
2679                 case CEE_MUL:
2680                         ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2681                         break;
2682                 case OP_MUL_IMM:
2683                         ppc_load (code, ppc_r11, ins->inst_imm);
2684                         ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2685                         break;
2686                 case CEE_MUL_OVF:
2687                         /* we annot use mcrxr, since it's not implemented on some processors 
2688                          * XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2689                          */
2690                         ppc_mullwo (code, ins->dreg, ins->sreg1, ins->sreg2);
2691                         ppc_mfspr (code, ppc_r0, ppc_xer);
2692                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2693                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2694                         break;
2695                 case CEE_MUL_OVF_UN:
2696                         /* we first multiply to get the high word and compare to 0
2697                          * to set the flags, then the result is discarded and then 
2698                          * we multiply to get the lower * bits result
2699                          */
2700                         ppc_mulhwu (code, ppc_r0, ins->sreg1, ins->sreg2);
2701                         ppc_cmpi (code, 0, 0, ppc_r0, 0);
2702                         EMIT_COND_SYSTEM_EXCEPTION (CEE_BNE_UN - CEE_BEQ, "OverflowException");
2703                         ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2704                         break;
2705                 case OP_ICONST:
2706                 case OP_SETREGIMM:
2707                         ppc_load (code, ins->dreg, ins->inst_c0);
2708                         break;
2709                 case OP_AOTCONST:
2710                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2711                         ppc_lis (code, ins->dreg, 0);
2712                         ppc_ori (code, ins->dreg, ins->dreg, 0);
2713                         break;
2714                 case CEE_CONV_I4:
2715                 case CEE_CONV_U4:
2716                 case OP_MOVE:
2717                 case OP_SETREG:
2718                         ppc_mr (code, ins->dreg, ins->sreg1);
2719                         break;
2720                 case OP_SETLRET: {
2721                         int saved = ins->sreg1;
2722                         if (ins->sreg1 == ppc_r3) {
2723                                 ppc_mr (code, ppc_r0, ins->sreg1);
2724                                 saved = ppc_r0;
2725                         }
2726                         if (ins->sreg2 != ppc_r3)
2727                                 ppc_mr (code, ppc_r3, ins->sreg2);
2728                         if (saved != ppc_r4)
2729                                 ppc_mr (code, ppc_r4, saved);
2730                         break;
2731                 }
2732                 case OP_SETFREG:
2733                 case OP_FMOVE:
2734                         ppc_fmr (code, ins->dreg, ins->sreg1);
2735                         break;
2736                 case OP_FCONV_TO_R4:
2737                         ppc_frsp (code, ins->dreg, ins->sreg1);
2738                         break;
2739                 case CEE_JMP: {
2740                         int i, pos = 0;
2741                         
2742                         /*
2743                          * Keep in sync with mono_arch_emit_epilog
2744                          */
2745                         g_assert (!cfg->method->save_lmf);
2746                         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2747                                 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
2748                                         ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
2749                                 } else {
2750                                         ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
2751                                         ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
2752                                 }
2753                                 ppc_mtlr (code, ppc_r0);
2754                         }
2755                         if (ppc_is_imm16 (cfg->stack_usage)) {
2756                                 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2757                         } else {
2758                                 ppc_load (code, ppc_r11, cfg->stack_usage);
2759                                 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
2760                         }
2761                         if (!cfg->method->save_lmf) {
2762                                 /*for (i = 31; i >= 14; --i) {
2763                                         if (cfg->used_float_regs & (1 << i)) {
2764                                                 pos += sizeof (double);
2765                                                 ppc_lfd (code, i, -pos, cfg->frame_reg);
2766                                         }
2767                                 }*/
2768                                 for (i = 31; i >= 13; --i) {
2769                                         if (cfg->used_int_regs & (1 << i)) {
2770                                                 pos += sizeof (gulong);
2771                                                 ppc_lwz (code, i, -pos, cfg->frame_reg);
2772                                         }
2773                                 }
2774                         } else {
2775                                 /* FIXME restore from MonoLMF: though this can't happen yet */
2776                         }
2777                         mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2778                         ppc_b (code, 0);
2779                         break;
2780                 }
2781                 case OP_CHECK_THIS:
2782                         /* ensure ins->sreg1 is not NULL */
2783                         ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2784                         break;
2785                 case OP_ARGLIST:
2786                         /* FIXME: implement */
2787                         break;
2788                 case OP_FCALL:
2789                 case OP_LCALL:
2790                 case OP_VCALL:
2791                 case OP_VOIDCALL:
2792                 case CEE_CALL:
2793                         call = (MonoCallInst*)ins;
2794                         if (ins->flags & MONO_INST_HAS_METHOD)
2795                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2796                         else
2797                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2798                         ppc_bl (code, 0);
2799                         break;
2800                 case OP_FCALL_REG:
2801                 case OP_LCALL_REG:
2802                 case OP_VCALL_REG:
2803                 case OP_VOIDCALL_REG:
2804                 case OP_CALL_REG:
2805                         ppc_mtlr (code, ins->sreg1);
2806                         ppc_blrl (code);
2807                         break;
2808                 case OP_FCALL_MEMBASE:
2809                 case OP_LCALL_MEMBASE:
2810                 case OP_VCALL_MEMBASE:
2811                 case OP_VOIDCALL_MEMBASE:
2812                 case OP_CALL_MEMBASE:
2813                         ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2814                         ppc_mtlr (code, ppc_r0);
2815                         ppc_blrl (code);
2816                         break;
2817                 case OP_OUTARG:
2818                         g_assert_not_reached ();
2819                         break;
2820                 case OP_LOCALLOC: {
2821                         /* keep alignment */
2822                         int alloca_waste = PPC_STACK_PARAM_OFFSET + cfg->param_area + 31;
2823                         int area_offset = alloca_waste;
2824                         area_offset &= ~31;
2825                         ppc_addi (code, ppc_r11, ins->sreg1, alloca_waste);
2826                         ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 0, 27);
2827                         ppc_lwz (code, ppc_r0, 0, ppc_sp);
2828                         ppc_neg (code, ppc_r11, ppc_r11);
2829                         ppc_stwux (code, ppc_r0, ppc_sp, ppc_r11);
2830                         ppc_addi (code, ins->dreg, ppc_sp, area_offset);
2831                         break;
2832                 }
2833                 case CEE_RET:
2834                         ppc_blr (code);
2835                         break;
2836                 case CEE_THROW: {
2837                         //ppc_break (code);
2838                         ppc_mr (code, ppc_r3, ins->sreg1);
2839                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
2840                                              (gpointer)"mono_arch_throw_exception");
2841                         ppc_bl (code, 0);
2842                         break;
2843                 }
2844                 case OP_START_HANDLER:
2845                         ppc_mflr (code, ppc_r0);
2846                         if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2847                                 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2848                         } else {
2849                                 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2850                                 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_left->inst_basereg);
2851                         }
2852                         break;
2853                 case OP_ENDFILTER:
2854                         if (ins->sreg1 != ppc_r3)
2855                                 ppc_mr (code, ppc_r3, ins->sreg1);
2856                         if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2857                                 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2858                         } else {
2859                                 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2860                                 ppc_lwzx (code, ppc_r0, ins->inst_left->inst_basereg, ppc_r11);
2861                         }
2862                         ppc_mtlr (code, ppc_r0);
2863                         ppc_blr (code);
2864                         break;
2865                 case CEE_ENDFINALLY:
2866                         ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2867                         ppc_mtlr (code, ppc_r0);
2868                         ppc_blr (code);
2869                         break;
2870                 case OP_CALL_HANDLER: 
2871                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2872                         ppc_bl (code, 0);
2873                         break;
2874                 case OP_LABEL:
2875                         ins->inst_c0 = code - cfg->native_code;
2876                         break;
2877                 case CEE_BR:
2878                         //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2879                         //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2880                         //break;
2881                         if (ins->flags & MONO_INST_BRLABEL) {
2882                                 /*if (ins->inst_i0->inst_c0) {
2883                                         ppc_b (code, 0);
2884                                         //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2885                                 } else*/ {
2886                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2887                                         ppc_b (code, 0);
2888                                 }
2889                         } else {
2890                                 /*if (ins->inst_target_bb->native_offset) {
2891                                         ppc_b (code, 0);
2892                                         //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset); 
2893                                 } else*/ {
2894                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2895                                         ppc_b (code, 0);
2896                                 } 
2897                         }
2898                         break;
2899                 case OP_BR_REG:
2900                         ppc_mtctr (code, ins->sreg1);
2901                         ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2902                         break;
2903                 case OP_CEQ:
2904                         ppc_li (code, ins->dreg, 0);
2905                         ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2906                         ppc_li (code, ins->dreg, 1);
2907                         break;
2908                 case OP_CLT:
2909                 case OP_CLT_UN:
2910                         ppc_li (code, ins->dreg, 1);
2911                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2912                         ppc_li (code, ins->dreg, 0);
2913                         break;
2914                 case OP_CGT:
2915                 case OP_CGT_UN:
2916                         ppc_li (code, ins->dreg, 1);
2917                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2918                         ppc_li (code, ins->dreg, 0);
2919                         break;
2920                 case OP_COND_EXC_EQ:
2921                 case OP_COND_EXC_NE_UN:
2922                 case OP_COND_EXC_LT:
2923                 case OP_COND_EXC_LT_UN:
2924                 case OP_COND_EXC_GT:
2925                 case OP_COND_EXC_GT_UN:
2926                 case OP_COND_EXC_GE:
2927                 case OP_COND_EXC_GE_UN:
2928                 case OP_COND_EXC_LE:
2929                 case OP_COND_EXC_LE_UN:
2930                         EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2931                         break;
2932                 case OP_COND_EXC_C:
2933                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2934                          */
2935                         /*ppc_mfspr (code, ppc_r0, ppc_xer);
2936                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2937                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2938                         break;*/
2939                 case OP_COND_EXC_OV:
2940                         /*ppc_mcrxr (code, 0);
2941                         EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2942                         break;*/
2943                 case OP_COND_EXC_NC:
2944                 case OP_COND_EXC_NO:
2945                         g_assert_not_reached ();
2946                         break;
2947                 case CEE_BEQ:
2948                 case CEE_BNE_UN:
2949                 case CEE_BLT:
2950                 case CEE_BLT_UN:
2951                 case CEE_BGT:
2952                 case CEE_BGT_UN:
2953                 case CEE_BGE:
2954                 case CEE_BGE_UN:
2955                 case CEE_BLE:
2956                 case CEE_BLE_UN:
2957                         EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2958                         break;
2959
2960                 /* floating point opcodes */
2961                 case OP_R8CONST:
2962                         ppc_load (code, ppc_r11, ins->inst_p0);
2963                         ppc_lfd (code, ins->dreg, 0, ppc_r11);
2964                         break;
2965                 case OP_R4CONST:
2966                         ppc_load (code, ppc_r11, ins->inst_p0);
2967                         ppc_lfs (code, ins->dreg, 0, ppc_r11);
2968                         break;
2969                 case OP_STORER8_MEMBASE_REG:
2970                         if (ppc_is_imm16 (ins->inst_offset)) {
2971                                 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2972                         } else {
2973                                 ppc_load (code, ppc_r11, ins->inst_offset);
2974                                 ppc_stfdx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2975                         }
2976                         break;
2977                 case OP_LOADR8_MEMBASE:
2978                         if (ppc_is_imm16 (ins->inst_offset)) {
2979                                 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2980                         } else {
2981                                 ppc_load (code, ppc_r11, ins->inst_offset);
2982                                 ppc_lfdx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2983                         }
2984                         break;
2985                 case OP_STORER4_MEMBASE_REG:
2986                         if (ppc_is_imm16 (ins->inst_offset)) {
2987                                 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2988                         } else {
2989                                 ppc_load (code, ppc_r11, ins->inst_offset);
2990                                 ppc_stfsx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2991                         }
2992                         break;
2993                 case OP_LOADR4_MEMBASE:
2994                         if (ppc_is_imm16 (ins->inst_offset)) {
2995                                 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2996                         } else {
2997                                 ppc_load (code, ppc_r11, ins->inst_offset);
2998                                 ppc_lfsx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2999                         }
3000                         break;
3001                 case CEE_CONV_R_UN: {
3002                         static const guint64 adjust_val = 0x4330000000000000ULL;
3003                         ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3004                         ppc_stw (code, ppc_r0, -8, ppc_sp);
3005                         ppc_stw (code, ins->sreg1, -4, ppc_sp);
3006                         ppc_load (code, ppc_r11, &adjust_val);
3007                         ppc_lfd (code, ins->dreg, -8, ppc_sp);
3008                         ppc_lfd (code, ppc_f0, 0, ppc_r11);
3009                         ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3010                         break;
3011                 }
3012                 case CEE_CONV_R4: /* FIXME: change precision */
3013                 case CEE_CONV_R8: {
3014                         static const guint64 adjust_val = 0x4330000080000000ULL;
3015                         // addis is special for ppc_r0
3016                         ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3017                         ppc_stw (code, ppc_r0, -8, ppc_sp);
3018                         ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
3019                         ppc_stw (code, ppc_r11, -4, ppc_sp);
3020                         ppc_lfd (code, ins->dreg, -8, ppc_sp);
3021                         ppc_load (code, ppc_r11, &adjust_val);
3022                         ppc_lfd (code, ppc_f0, 0, ppc_r11);
3023                         ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3024                         break;
3025                 }
3026                 case OP_X86_FP_LOAD_I8:
3027                         g_assert_not_reached ();
3028                         /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
3029                         break;
3030                 case OP_X86_FP_LOAD_I4:
3031                         g_assert_not_reached ();
3032                         /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
3033                         break;
3034                 case OP_FCONV_TO_I1:
3035                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
3036                         break;
3037                 case OP_FCONV_TO_U1:
3038                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
3039                         break;
3040                 case OP_FCONV_TO_I2:
3041                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
3042                         break;
3043                 case OP_FCONV_TO_U2:
3044                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
3045                         break;
3046                 case OP_FCONV_TO_I4:
3047                 case OP_FCONV_TO_I:
3048                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
3049                         break;
3050                 case OP_FCONV_TO_U4:
3051                 case OP_FCONV_TO_U:
3052                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
3053                         break;
3054                 case OP_FCONV_TO_I8:
3055                 case OP_FCONV_TO_U8:
3056                         g_assert_not_reached ();
3057                         /* Implemented as helper calls */
3058                         break;
3059                 case OP_LCONV_TO_R_UN:
3060                         g_assert_not_reached ();
3061                         /* Implemented as helper calls */
3062                         break;
3063                 case OP_LCONV_TO_OVF_I: {
3064                         ppc_mr (code, ins->dreg, ins->sreg1);
3065                         /* FIXME: emit exception if needed */
3066                         break;
3067                 }
3068                 case OP_SQRT:
3069                         ppc_fsqrtd (code, ins->dreg, ins->sreg1);
3070                         break;
3071                 case OP_FADD:
3072                         ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
3073                         break;
3074                 case OP_FSUB:
3075                         ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
3076                         break;          
3077                 case OP_FMUL:
3078                         ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
3079                         break;          
3080                 case OP_FDIV:
3081                         ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
3082                         break;          
3083                 case OP_FNEG:
3084                         ppc_fneg (code, ins->dreg, ins->sreg1);
3085                         break;          
3086                 case OP_FREM:
3087                         /* emulated */
3088                         g_assert_not_reached ();
3089                         break;
3090                 case OP_FCOMPARE:
3091                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3092                         break;
3093                 case OP_FCEQ:
3094                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3095                         ppc_li (code, ins->dreg, 0);
3096                         ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
3097                         ppc_li (code, ins->dreg, 1);
3098                         break;
3099                 case OP_FCLT:
3100                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3101                         ppc_li (code, ins->dreg, 1);
3102                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3103                         ppc_li (code, ins->dreg, 0);
3104                         break;
3105                 case OP_FCLT_UN:
3106                         ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3107                         ppc_li (code, ins->dreg, 1);
3108                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3109                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3110                         ppc_li (code, ins->dreg, 0);
3111                         break;
3112                 case OP_FCGT:
3113                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3114                         ppc_li (code, ins->dreg, 1);
3115                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3116                         ppc_li (code, ins->dreg, 0);
3117                         break;
3118                 case OP_FCGT_UN:
3119                         ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3120                         ppc_li (code, ins->dreg, 1);
3121                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3122                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3123                         ppc_li (code, ins->dreg, 0);
3124                         break;
3125                 case OP_FBEQ:
3126                         EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
3127                         break;
3128                 case OP_FBNE_UN:
3129                         EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
3130                         break;
3131                 case OP_FBLT:
3132                         EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
3133                         break;
3134                 case OP_FBLT_UN:
3135                         EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3136                         EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
3137                         break;
3138                 case OP_FBGT:
3139                         EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
3140                         break;
3141                 case OP_FBGT_UN:
3142                         EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3143                         EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
3144                         break;
3145                 case OP_FBGE:
3146                         EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
3147                         break;
3148                 case OP_FBGE_UN:
3149                         EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
3150                         break;
3151                 case OP_FBLE:
3152                         EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
3153                         break;
3154                 case OP_FBLE_UN:
3155                         EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
3156                         break;
3157                 case CEE_CKFINITE: {
3158                         ppc_stfd (code, ins->sreg1, -8, ppc_sp);
3159                         ppc_lwz (code, ppc_r11, -8, ppc_sp);
3160                         ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
3161                         ppc_addis (code, ppc_r11, ppc_r11, -32752);
3162                         ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
3163                         EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");
3164                         break;
3165                 }
3166                 default:
3167                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3168                         g_assert_not_reached ();
3169                 }
3170
3171                 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
3172                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3173                                    mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3174                         g_assert_not_reached ();
3175                 }
3176                
3177                 cpos += max_len;
3178
3179                 last_ins = ins;
3180                 last_offset = offset;
3181                 
3182                 ins = ins->next;
3183         }
3184
3185         cfg->code_len = code - cfg->native_code;
3186 }
3187
3188 void
3189 mono_arch_register_lowlevel_calls (void)
3190 {
3191 }
3192
3193 #define patch_lis_ori(ip,val) do {\
3194                 guint16 *__lis_ori = (guint16*)(ip);    \
3195                 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff;      \
3196                 __lis_ori [3] = ((guint32)(val)) & 0xffff;      \
3197         } while (0)
3198
3199 void
3200 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3201 {
3202         MonoJumpInfo *patch_info;
3203
3204         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3205                 unsigned char *ip = patch_info->ip.i + code;
3206                 const unsigned char *target;
3207
3208                 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3209
3210                 switch (patch_info->type) {
3211                 case MONO_PATCH_INFO_IP:
3212                         patch_lis_ori (ip, ip);
3213                         continue;
3214                 case MONO_PATCH_INFO_METHOD_REL:
3215                         g_assert_not_reached ();
3216                         *((gpointer *)(ip)) = code + patch_info->data.offset;
3217                         continue;
3218                 case MONO_PATCH_INFO_SWITCH: {
3219                         gpointer *table = (gpointer *)patch_info->data.target;
3220                         int i;
3221
3222                         // FIXME: inspect code to get the register
3223                         ppc_load (ip, ppc_r11, patch_info->data.target);
3224                         //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3225
3226                         for (i = 0; i < patch_info->table_size; i++) {
3227                                 table [i] = (int)patch_info->data.table [i] + code;
3228                         }
3229                         /* we put into the table the absolute address, no need for ppc_patch in this case */
3230                         continue;
3231                 }
3232                 case MONO_PATCH_INFO_METHODCONST:
3233                 case MONO_PATCH_INFO_CLASS:
3234                 case MONO_PATCH_INFO_IMAGE:
3235                 case MONO_PATCH_INFO_FIELD:
3236                 case MONO_PATCH_INFO_VTABLE:
3237                 case MONO_PATCH_INFO_IID:
3238                 case MONO_PATCH_INFO_SFLDA:
3239                 case MONO_PATCH_INFO_LDSTR:
3240                 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3241                 case MONO_PATCH_INFO_LDTOKEN:
3242                         /* from OP_AOTCONST : lis + ori */
3243                         patch_lis_ori (ip, target);
3244                         continue;
3245                 case MONO_PATCH_INFO_R4:
3246                 case MONO_PATCH_INFO_R8:
3247                         g_assert_not_reached ();
3248                         *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3249                         continue;
3250                 case MONO_PATCH_INFO_EXC_NAME:
3251                         g_assert_not_reached ();
3252                         *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3253                         continue;
3254                 case MONO_PATCH_INFO_BB_OVF:
3255                 case MONO_PATCH_INFO_EXC_OVF:
3256                         /* everything is dealt with at epilog output time */
3257                         continue;
3258                 default:
3259                         break;
3260                 }
3261                 ppc_patch (ip, target);
3262         }
3263 }
3264
3265 int
3266 mono_arch_max_epilog_size (MonoCompile *cfg)
3267 {
3268         int max_epilog_size = 16 + 20*4;
3269         MonoJumpInfo *patch_info;
3270         
3271         if (cfg->method->save_lmf)
3272                 max_epilog_size += 128;
3273         
3274         if (mono_jit_trace_calls != NULL)
3275                 max_epilog_size += 50;
3276
3277         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3278                 max_epilog_size += 50;
3279
3280         /* count the number of exception infos */
3281      
3282         /* 
3283          * make sure we have enough space for exceptions
3284          * 24 is the simulated call to throw_exception_by_name
3285          */
3286         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3287                 if (patch_info->type == MONO_PATCH_INFO_EXC)
3288                         max_epilog_size += 24;
3289                 else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
3290                         max_epilog_size += 12;
3291                 else if (patch_info->type == MONO_PATCH_INFO_EXC_OVF)
3292                         max_epilog_size += 12;
3293         }
3294
3295         return max_epilog_size;
3296 }
3297
3298 /*
3299  * Stack frame layout:
3300  * 
3301  *   ------------------- sp
3302  *      MonoLMF structure or saved registers
3303  *   -------------------
3304  *      spilled regs
3305  *   -------------------
3306  *      locals
3307  *   -------------------
3308  *      optional 8 bytes for tracing
3309  *   -------------------
3310  *      param area             size is cfg->param_area
3311  *   -------------------
3312  *      linkage area           size is PPC_STACK_PARAM_OFFSET
3313  *   ------------------- sp
3314  *      red zone
3315  */
3316 guint8 *
3317 mono_arch_emit_prolog (MonoCompile *cfg)
3318 {
3319         MonoMethod *method = cfg->method;
3320         MonoBasicBlock *bb;
3321         MonoMethodSignature *sig;
3322         MonoInst *inst;
3323         int alloc_size, pos, max_offset, i;
3324         guint8 *code;
3325         CallInfo *cinfo;
3326         int tracing = 0;
3327         int lmf_offset = 0;
3328
3329         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3330                 tracing = 1;
3331
3332         cfg->code_size = 256;
3333         code = cfg->native_code = g_malloc (cfg->code_size);
3334
3335         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3336                 ppc_mflr (code, ppc_r0);
3337                 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3338         }
3339         cfg->used_int_regs |= USE_EXTRA_TEMPS;
3340
3341         alloc_size = cfg->stack_offset;
3342         pos = 0;
3343
3344         if (!method->save_lmf) {
3345                 /*for (i = 31; i >= 14; --i) {
3346                         if (cfg->used_float_regs & (1 << i)) {
3347                                 pos += sizeof (gdouble);
3348                                 ppc_stfd (code, i, -pos, ppc_sp);
3349                         }
3350                 }*/
3351                 for (i = 31; i >= 13; --i) {
3352                         if (cfg->used_int_regs & (1 << i)) {
3353                                 pos += sizeof (gulong);
3354                                 ppc_stw (code, i, -pos, ppc_sp);
3355                         }
3356                 }
3357         } else {
3358                 int ofs;
3359                 pos += sizeof (MonoLMF);
3360                 lmf_offset = pos;
3361                 ofs = -pos + G_STRUCT_OFFSET(MonoLMF, iregs);
3362                 ppc_stmw (code, ppc_r13, ppc_r1, ofs);
3363                 for (i = 14; i < 32; i++) {
3364                         ppc_stfd (code, i, (-pos + G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble))), ppc_r1);
3365                 }
3366         }
3367         alloc_size += pos;
3368         // align to PPC_STACK_ALIGNMENT bytes
3369         if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
3370                 alloc_size += PPC_STACK_ALIGNMENT - 1;
3371                 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
3372         }
3373
3374         cfg->stack_usage = alloc_size;
3375         g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
3376         if (alloc_size) {
3377                 if (ppc_is_imm16 (-alloc_size)) {
3378                         ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3379                 } else {
3380                         ppc_load (code, ppc_r11, -alloc_size);
3381                         ppc_stwux (code, ppc_sp, ppc_sp, ppc_r11);
3382                 }
3383         }
3384         if (cfg->frame_reg != ppc_sp)
3385                 ppc_mr (code, cfg->frame_reg, ppc_sp);
3386
3387         /* compute max_offset in order to use short forward jumps
3388          * we always do it on ppc because the immediate displacement
3389          * for jumps is too small 
3390          */
3391         max_offset = 0;
3392         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3393                 MonoInst *ins = bb->code;
3394                 bb->max_offset = max_offset;
3395
3396                 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3397                         max_offset += 6; 
3398
3399                 while (ins) {
3400                         max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3401                         ins = ins->next;
3402                 }
3403         }
3404
3405         /* load arguments allocated to register from the stack */
3406         sig = method->signature;
3407         pos = 0;
3408
3409         cinfo = calculate_sizes (sig, sig->pinvoke);
3410
3411         if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3412                 ArgInfo *ainfo = &cinfo->ret;
3413                 inst = cfg->ret;
3414                 if (ppc_is_imm16 (inst->inst_offset)) {
3415                         ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3416                 } else {
3417                         ppc_load (code, ppc_r11, inst->inst_offset);
3418                         ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3419                 }
3420         }
3421         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3422                 ArgInfo *ainfo = cinfo->args + i;
3423                 inst = cfg->varinfo [pos];
3424                 
3425                 if (cfg->verbose_level > 2)
3426                         g_print ("Saving argument %d (type: %d)\n", i, ainfo->regtype);
3427                 if (inst->opcode == OP_REGVAR) {
3428                         if (ainfo->regtype == RegTypeGeneral)
3429                                 ppc_mr (code, inst->dreg, ainfo->reg);
3430                         else if (ainfo->regtype == RegTypeFP)
3431                                 ppc_fmr (code, inst->dreg, ainfo->reg);
3432                         else if (ainfo->regtype == RegTypeBase) {
3433                                 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3434                                 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3435                         } else
3436                                 g_assert_not_reached ();
3437
3438                         if (cfg->verbose_level > 2)
3439                                 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3440                 } else {
3441                         /* the argument should be put on the stack: FIXME handle size != word  */
3442                         if (ainfo->regtype == RegTypeGeneral) {
3443                                 switch (ainfo->size) {
3444                                 case 1:
3445                                         if (ppc_is_imm16 (inst->inst_offset)) {
3446                                                 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3447                                         } else {
3448                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3449                                                 ppc_stbx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3450                                         }
3451                                         break;
3452                                 case 2:
3453                                         if (ppc_is_imm16 (inst->inst_offset)) {
3454                                                 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3455                                         } else {
3456                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3457                                                 ppc_sthx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3458                                         }
3459                                         break;
3460                                 case 8:
3461                                         if (ppc_is_imm16 (inst->inst_offset + 4)) {
3462                                                 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3463                                                 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3464                                         } else {
3465                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3466                                                 ppc_add (code, ppc_r11, ppc_r11, inst->inst_basereg);
3467                                                 ppc_stw (code, ainfo->reg, 0, ppc_r11);
3468                                                 ppc_stw (code, ainfo->reg + 1, 4, ppc_r11);
3469                                         }
3470                                         break;
3471                                 default:
3472                                         if (ppc_is_imm16 (inst->inst_offset)) {
3473                                                 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3474                                         } else {
3475                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3476                                                 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3477                                         }
3478                                         break;
3479                                 }
3480                         } else if (ainfo->regtype == RegTypeBase) {
3481                                 /* load the previous stack pointer in r11 */
3482                                 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3483                                 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3484                                 switch (ainfo->size) {
3485                                 case 1:
3486                                         if (ppc_is_imm16 (inst->inst_offset)) {
3487                                                 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3488                                         } else {
3489                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3490                                                 ppc_stbx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3491                                         }
3492                                         break;
3493                                 case 2:
3494                                         if (ppc_is_imm16 (inst->inst_offset)) {
3495                                                 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3496                                         } else {
3497                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3498                                                 ppc_sthx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3499                                         }
3500                                         break;
3501                                 case 8:
3502                                         if (ppc_is_imm16 (inst->inst_offset + 4)) {
3503                                                 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3504                                                 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3505                                                 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3506                                         } else {
3507                                                 /* FIXME */
3508                                                 g_assert_not_reached ();
3509                                         }
3510                                         break;
3511                                 default:
3512                                         if (ppc_is_imm16 (inst->inst_offset)) {
3513                                                 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3514                                         } else {
3515                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3516                                                 ppc_stwx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3517                                         }
3518                                         break;
3519                                 }
3520                         } else if (ainfo->regtype == RegTypeFP) {
3521                                 g_assert (ppc_is_imm16 (inst->inst_offset));
3522                                 if (ainfo->size == 8)
3523                                         ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3524                                 else if (ainfo->size == 4)
3525                                         ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3526                                 else
3527                                         g_assert_not_reached ();
3528                         } else if (ainfo->regtype == RegTypeStructByVal) {
3529                                 int doffset = inst->inst_offset;
3530                                 int soffset = 0;
3531                                 int cur_reg;
3532                                 g_assert (ppc_is_imm16 (inst->inst_offset));
3533                                 g_assert (ppc_is_imm16 (inst->inst_offset + ainfo->size * sizeof (gpointer)));
3534                                 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3535                                         ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3536                                         soffset += sizeof (gpointer);
3537                                         doffset += sizeof (gpointer);
3538                                 }
3539                                 if (ainfo->vtsize) {
3540                                         /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3541                                         ppc_lwz (code, ppc_r11, 0, ppc_sp);
3542                                         /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3543                                         code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3544                                 }
3545                         } else if (ainfo->regtype == RegTypeStructByAddr) {
3546                                 g_assert (ppc_is_imm16 (inst->inst_offset));
3547                                 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3548                                 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3549                         } else
3550                                 g_assert_not_reached ();
3551                 }
3552                 pos++;
3553         }
3554
3555         if (method->save_lmf) {
3556
3557                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3558                                      (gpointer)"mono_get_lmf_addr");
3559                 ppc_bl (code, 0);
3560                 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3561                 /* lmf_offset is the offset from the previous stack pointer,
3562                  * alloc_size is the total stack space allocated, so the offset
3563                  * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
3564                  * The pointer to the struct is put in ppc_r11 (new_lmf).
3565                  * The callee-saved registers are already in the MonoLMF structure
3566                  */
3567                 ppc_addi (code, ppc_r11, ppc_sp, alloc_size - lmf_offset);
3568                 /* ppc_r3 is the result from mono_get_lmf_addr () */
3569                 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3570                 /* new_lmf->previous_lmf = *lmf_addr */
3571                 ppc_lwz (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3572                 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3573                 /* *(lmf_addr) = r11 */
3574                 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3575                 /* save method info */
3576                 ppc_load (code, ppc_r0, method);
3577                 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3578                 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3579                 /* save the current IP */
3580                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3581                 ppc_load (code, ppc_r0, 0x01010101);
3582                 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3583         }
3584
3585         if (tracing)
3586                 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3587
3588         cfg->code_len = code - cfg->native_code;
3589         g_free (cinfo);
3590
3591         return code;
3592 }
3593
3594 void
3595 mono_arch_emit_epilog (MonoCompile *cfg)
3596 {
3597         MonoJumpInfo *patch_info;
3598         MonoMethod *method = cfg->method;
3599         int pos, i;
3600         guint8 *code;
3601
3602         /*
3603          * Keep in sync with CEE_JMP
3604          */
3605         code = cfg->native_code + cfg->code_len;
3606
3607         if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3608                 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3609         }
3610         pos = 0;
3611
3612         if (method->save_lmf) {
3613                 int lmf_offset;
3614                 pos +=  sizeof (MonoLMF);
3615                 lmf_offset = pos;
3616                 /* save the frame reg in r8 */
3617                 ppc_mr (code, ppc_r8, cfg->frame_reg);
3618                 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage - lmf_offset);
3619                 /* r5 = previous_lmf */
3620                 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3621                 /* r6 = lmf_addr */
3622                 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3623                 /* *(lmf_addr) = previous_lmf */
3624                 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3625                 /* FIXME: speedup: there is no actual need to restore the registers if
3626                  * we didn't actually change them (idea from Zoltan).
3627                  */
3628                 /* restore iregs */
3629                 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3630                 /* restore fregs */
3631                 /*for (i = 14; i < 32; i++) {
3632                         ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3633                 }*/
3634                 g_assert (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET));
3635                 /* use the saved copy of the frame reg in r8 */
3636                 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3637                         ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, ppc_r8);
3638                         ppc_mtlr (code, ppc_r0);
3639                 }
3640                 ppc_addic (code, ppc_sp, ppc_r8, cfg->stack_usage);
3641         } else {
3642                 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3643                         if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
3644                                 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3645                         } else {
3646                                 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
3647                                 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
3648                         }
3649                         ppc_mtlr (code, ppc_r0);
3650                 }
3651                 if (ppc_is_imm16 (cfg->stack_usage)) {
3652                         ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3653                 } else {
3654                         ppc_load (code, ppc_r11, cfg->stack_usage);
3655                         ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
3656                 }
3657
3658                 /*for (i = 31; i >= 14; --i) {
3659                         if (cfg->used_float_regs & (1 << i)) {
3660                                 pos += sizeof (double);
3661                                 ppc_lfd (code, i, -pos, ppc_sp);
3662                         }
3663                 }*/
3664                 for (i = 31; i >= 13; --i) {
3665                         if (cfg->used_int_regs & (1 << i)) {
3666                                 pos += sizeof (gulong);
3667                                 ppc_lwz (code, i, -pos, ppc_sp);
3668                         }
3669                 }
3670         }
3671         ppc_blr (code);
3672
3673         /* add code to raise exceptions */
3674         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3675                 switch (patch_info->type) {
3676                 case MONO_PATCH_INFO_BB_OVF: {
3677                         MonoOvfJump *ovfj = patch_info->data.target;
3678                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
3679                         /* patch the initial jump */
3680                         ppc_patch (ip, code);
3681                         ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3682                         ppc_b (code, 0);
3683                         ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3684                         /* jump back to the true target */
3685                         ppc_b (code, 0);
3686                         ip = ovfj->bb->native_offset + cfg->native_code;
3687                         ppc_patch (code - 4, ip);
3688                         break;
3689                 }
3690                 case MONO_PATCH_INFO_EXC_OVF: {
3691                         MonoOvfJump *ovfj = patch_info->data.target;
3692                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
3693                         /* patch the initial jump */
3694                         ppc_patch (ip, code);
3695                         ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3696                         ppc_b (code, 0);
3697                         ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3698                         /* jump back to the true target */
3699                         ppc_b (code, 0);
3700                         ip = (char*)ovfj->ip + 4;
3701                         ppc_patch (code - 4, ip);
3702                         break;
3703                 }
3704                 case MONO_PATCH_INFO_EXC: {
3705                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
3706                         ppc_patch (ip, code);
3707                         /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3708                         ppc_load (code, ppc_r3, patch_info->data.target);
3709                         /* simulate a call from ip */
3710                         ppc_load (code, ppc_r0, ip + 4);
3711                         ppc_mtlr (code, ppc_r0);
3712                         patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3713                         patch_info->data.name = "mono_arch_throw_exception_by_name";
3714                         patch_info->ip.i = code - cfg->native_code;
3715                         ppc_b (code, 0);
3716                         break;
3717                 }
3718                 default:
3719                         /* do nothing */
3720                         break;
3721                 }
3722         }
3723
3724         cfg->code_len = code - cfg->native_code;
3725
3726         g_assert (cfg->code_len < cfg->code_size);
3727
3728 }
3729
3730 void
3731 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3732 {
3733 }
3734
3735 void
3736 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3737 {
3738 }
3739
3740 void
3741 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3742 {
3743         int this_dreg = ppc_r3;
3744         
3745         if (vt_reg != -1)
3746                 this_dreg = ppc_r4;
3747
3748         /* add the this argument */
3749         if (this_reg != -1) {
3750                 MonoInst *this;
3751                 MONO_INST_NEW (cfg, this, OP_SETREG);
3752                 this->type = this_type;
3753                 this->sreg1 = this_reg;
3754                 this->dreg = this_dreg;
3755                 mono_bblock_add_inst (cfg->cbb, this);
3756         }
3757
3758         if (vt_reg != -1) {
3759                 MonoInst *vtarg;
3760                 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3761                 vtarg->type = STACK_MP;
3762                 vtarg->sreg1 = vt_reg;
3763                 vtarg->dreg = ppc_r3;
3764                 mono_bblock_add_inst (cfg->cbb, vtarg);
3765         }
3766 }
3767
3768 gint
3769 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3770 {
3771         /* optional instruction, need to detect it
3772         if (cmethod->klass == mono_defaults.math_class) {
3773                 if (strcmp (cmethod->name, "Sqrt") == 0)
3774                         return OP_SQRT;
3775         }*/
3776         return -1;
3777 }
3778
3779
3780 gboolean
3781 mono_arch_print_tree (MonoInst *tree, int arity)
3782 {
3783         return 0;
3784 }