2004-08-23 Zoltan Varga <vargaz@freemail.hu>
[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_ADD_IMM:
2447                         if (ppc_is_imm16 (ins->inst_imm)) {
2448                                 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2449                         } else {
2450                                 ppc_load (code, ppc_r11, ins->inst_imm);
2451                                 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2452                         }
2453                         break;
2454                 case OP_ADC_IMM:
2455                         ppc_load (code, ppc_r11, ins->inst_imm);
2456                         ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2457                         break;
2458                 case CEE_ADD_OVF:
2459                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2460                          */
2461                         ppc_addo (code, ins->dreg, ins->sreg1, ins->sreg2);
2462                         ppc_mfspr (code, ppc_r0, ppc_xer);
2463                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2464                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2465                         break;
2466                 case CEE_ADD_OVF_UN:
2467                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2468                          */
2469                         ppc_addco (code, ins->dreg, ins->sreg1, ins->sreg2);
2470                         ppc_mfspr (code, ppc_r0, ppc_xer);
2471                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2472                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2473                         break;
2474                 case CEE_SUB_OVF:
2475                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2476                          */
2477                         ppc_subfo (code, ins->dreg, ins->sreg2, ins->sreg1);
2478                         ppc_mfspr (code, ppc_r0, ppc_xer);
2479                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2480                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2481                         break;
2482                 case CEE_SUB_OVF_UN:
2483                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2484                          */
2485                         ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2486                         ppc_mfspr (code, ppc_r0, ppc_xer);
2487                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<13));
2488                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2489                         break;
2490                 case OP_ADD_OVF_CARRY:
2491                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2492                          */
2493                         ppc_addeo (code, ins->dreg, ins->sreg1, ins->sreg2);
2494                         ppc_mfspr (code, ppc_r0, ppc_xer);
2495                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2496                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2497                         break;
2498                 case OP_ADD_OVF_UN_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<<13));
2504                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2505                         break;
2506                 case OP_SUB_OVF_CARRY:
2507                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2508                          */
2509                         ppc_subfeo (code, ins->dreg, ins->sreg2, ins->sreg1);
2510                         ppc_mfspr (code, ppc_r0, ppc_xer);
2511                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2512                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2513                         break;
2514                 case OP_SUB_OVF_UN_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<<13));
2520                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
2521                         break;
2522                 case OP_SUBCC:
2523                         ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2524                         break;
2525                 case CEE_SUB:
2526                         ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2527                         break;
2528                 case OP_SBB:
2529                         ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2530                         break;
2531                 case OP_SUB_IMM:
2532                         // we add the negated value
2533                         if (ppc_is_imm16 (-ins->inst_imm))
2534                                 ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2535                         else {
2536                                 ppc_load (code, ppc_r11, ins->inst_imm);
2537                                 ppc_sub (code, ins->dreg, ins->sreg1, ppc_r11);
2538                         }
2539                         break;
2540                 case OP_SBB_IMM:
2541                         ppc_load (code, ppc_r11, ins->inst_imm);
2542                         ppc_subfe (code, ins->dreg, ins->sreg2, ppc_r11);
2543                         break;
2544                 case OP_PPC_SUBFIC:
2545                         g_assert (ppc_is_imm16 (ins->inst_imm));
2546                         ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2547                         break;
2548                 case OP_PPC_SUBFZE:
2549                         ppc_subfze (code, ins->dreg, ins->sreg1);
2550                         break;
2551                 case CEE_AND:
2552                         /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2553                         ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2554                         break;
2555                 case OP_AND_IMM:
2556                         if (!(ins->inst_imm & 0xffff0000)) {
2557                                 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2558                         } else if (!(ins->inst_imm & 0xffff)) {
2559                                 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2560                         } else {
2561                                 ppc_load (code, ppc_r11, ins->inst_imm);
2562                                 ppc_and (code, ins->sreg1, ins->dreg, ppc_r11);
2563                         }
2564                         break;
2565                 case CEE_DIV:
2566                          /* XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2567                          */
2568                         ppc_divwod (code, ins->dreg, ins->sreg1, ins->sreg2);
2569                         ppc_mfspr (code, ppc_r0, ppc_xer);
2570                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2571                         /* FIXME: use OverflowException for 0x80000000/-1 */
2572                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2573                         break;
2574                 case CEE_DIV_UN:
2575                         ppc_divwuod (code, ins->dreg, ins->sreg1, ins->sreg2);
2576                         ppc_mfspr (code, ppc_r0, ppc_xer);
2577                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2578                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2579                         break;
2580                 case OP_DIV_IMM:
2581                         g_assert_not_reached ();
2582 #if 0
2583                         ppc_load (code, ppc_r11, ins->inst_imm);
2584                         ppc_divwod (code, ins->dreg, ins->sreg1, ppc_r11);
2585                         ppc_mfspr (code, ppc_r0, ppc_xer);
2586                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2587                         /* FIXME: use OverflowException for 0x80000000/-1 */
2588                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2589                         break;
2590 #endif
2591                 case CEE_REM:
2592                         ppc_divwod (code, ppc_r11, ins->sreg1, ins->sreg2);
2593                         ppc_mfspr (code, ppc_r0, ppc_xer);
2594                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2595                         /* FIXME: use OverflowException for 0x80000000/-1 */
2596                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2597                         ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2598                         ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2599                         break;
2600                 case CEE_REM_UN:
2601                         ppc_divwuod (code, ppc_r11, ins->sreg1, ins->sreg2);
2602                         ppc_mfspr (code, ppc_r0, ppc_xer);
2603                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2604                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "DivideByZeroException");
2605                         ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2606                         ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2607                         break;
2608                 case OP_REM_IMM:
2609                         g_assert_not_reached ();
2610                 case CEE_OR:
2611                         ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2612                         break;
2613                 case OP_OR_IMM:
2614                         if (!(ins->inst_imm & 0xffff0000)) {
2615                                 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2616                         } else if (!(ins->inst_imm & 0xffff)) {
2617                                 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2618                         } else {
2619                                 ppc_load (code, ppc_r11, ins->inst_imm);
2620                                 ppc_or (code, ins->sreg1, ins->dreg, ppc_r11);
2621                         }
2622                         break;
2623                 case CEE_XOR:
2624                         ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2625                         break;
2626                 case OP_XOR_IMM:
2627                         if (!(ins->inst_imm & 0xffff0000)) {
2628                                 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2629                         } else if (!(ins->inst_imm & 0xffff)) {
2630                                 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2631                         } else {
2632                                 ppc_load (code, ppc_r11, ins->inst_imm);
2633                                 ppc_xor (code, ins->sreg1, ins->dreg, ppc_r11);
2634                         }
2635                         break;
2636                 case CEE_SHL:
2637                         ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2638                         break;
2639                 case OP_SHL_IMM:
2640                         ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f), 0, (31 - (ins->inst_imm & 0x1f)));
2641                         //ppc_load (code, ppc_r11, ins->inst_imm);
2642                         //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2643                         break;
2644                 case CEE_SHR:
2645                         ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2646                         break;
2647                 case OP_SHR_IMM:
2648                         // there is also ppc_srawi
2649                         //ppc_load (code, ppc_r11, ins->inst_imm);
2650                         //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2651                         ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2652                         break;
2653                 case OP_SHR_UN_IMM:
2654                         /*ppc_load (code, ppc_r11, ins->inst_imm);
2655                         ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);*/
2656                         ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0x1f)), (ins->inst_imm & 0x1f), 31);
2657                         break;
2658                 case CEE_SHR_UN:
2659                         ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2660                         break;
2661                 case CEE_NOT:
2662                         ppc_not (code, ins->dreg, ins->sreg1);
2663                         break;
2664                 case CEE_NEG:
2665                         ppc_neg (code, ins->dreg, ins->sreg1);
2666                         break;
2667                 case CEE_MUL:
2668                         ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2669                         break;
2670                 case OP_MUL_IMM:
2671                         ppc_load (code, ppc_r11, ins->inst_imm);
2672                         ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2673                         break;
2674                 case CEE_MUL_OVF:
2675                         /* we annot use mcrxr, since it's not implemented on some processors 
2676                          * XER format: SO, OV, CA, reserved [21 bits], count [8 bits]
2677                          */
2678                         ppc_mullwo (code, ins->dreg, ins->sreg1, ins->sreg2);
2679                         ppc_mfspr (code, ppc_r0, ppc_xer);
2680                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2681                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2682                         break;
2683                 case CEE_MUL_OVF_UN:
2684                         /* we first multiply to get the high word and compare to 0
2685                          * to set the flags, then the result is discarded and then 
2686                          * we multiply to get the lower * bits result
2687                          */
2688                         ppc_mulhwu (code, ppc_r0, ins->sreg1, ins->sreg2);
2689                         ppc_cmpi (code, 0, 0, ppc_r0, 0);
2690                         EMIT_COND_SYSTEM_EXCEPTION (CEE_BNE_UN - CEE_BEQ, "OverflowException");
2691                         ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2692                         break;
2693                 case OP_ICONST:
2694                 case OP_SETREGIMM:
2695                         ppc_load (code, ins->dreg, ins->inst_c0);
2696                         break;
2697                 case OP_AOTCONST:
2698                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2699                         ppc_lis (code, ins->dreg, 0);
2700                         ppc_ori (code, ins->dreg, ins->dreg, 0);
2701                         break;
2702                 case CEE_CONV_I4:
2703                 case CEE_CONV_U4:
2704                 case OP_MOVE:
2705                 case OP_SETREG:
2706                         ppc_mr (code, ins->dreg, ins->sreg1);
2707                         break;
2708                 case OP_SETLRET: {
2709                         int saved = ins->sreg1;
2710                         if (ins->sreg1 == ppc_r3) {
2711                                 ppc_mr (code, ppc_r0, ins->sreg1);
2712                                 saved = ppc_r0;
2713                         }
2714                         if (ins->sreg2 != ppc_r3)
2715                                 ppc_mr (code, ppc_r3, ins->sreg2);
2716                         if (saved != ppc_r4)
2717                                 ppc_mr (code, ppc_r4, saved);
2718                         break;
2719                 }
2720                 case OP_SETFREG:
2721                 case OP_FMOVE:
2722                         ppc_fmr (code, ins->dreg, ins->sreg1);
2723                         break;
2724                 case OP_FCONV_TO_R4:
2725                         ppc_frsp (code, ins->dreg, ins->sreg1);
2726                         break;
2727                 case CEE_JMP: {
2728                         int i, pos = 0;
2729                         
2730                         /*
2731                          * Keep in sync with mono_arch_emit_epilog
2732                          */
2733                         g_assert (!cfg->method->save_lmf);
2734                         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2735                                 if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
2736                                         ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
2737                                 } else {
2738                                         ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
2739                                         ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
2740                                 }
2741                                 ppc_mtlr (code, ppc_r0);
2742                         }
2743                         if (ppc_is_imm16 (cfg->stack_usage)) {
2744                                 ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2745                         } else {
2746                                 ppc_load (code, ppc_r11, cfg->stack_usage);
2747                                 ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
2748                         }
2749                         if (!cfg->method->save_lmf) {
2750                                 /*for (i = 31; i >= 14; --i) {
2751                                         if (cfg->used_float_regs & (1 << i)) {
2752                                                 pos += sizeof (double);
2753                                                 ppc_lfd (code, i, -pos, cfg->frame_reg);
2754                                         }
2755                                 }*/
2756                                 for (i = 31; i >= 13; --i) {
2757                                         if (cfg->used_int_regs & (1 << i)) {
2758                                                 pos += sizeof (gulong);
2759                                                 ppc_lwz (code, i, -pos, cfg->frame_reg);
2760                                         }
2761                                 }
2762                         } else {
2763                                 /* FIXME restore from MonoLMF: though this can't happen yet */
2764                         }
2765                         mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2766                         ppc_b (code, 0);
2767                         break;
2768                 }
2769                 case OP_CHECK_THIS:
2770                         /* ensure ins->sreg1 is not NULL */
2771                         ppc_lwz (code, ppc_r0, 0, ins->sreg1);
2772                         break;
2773                 case OP_ARGLIST:
2774                         /* FIXME: implement */
2775                         break;
2776                 case OP_FCALL:
2777                 case OP_LCALL:
2778                 case OP_VCALL:
2779                 case OP_VOIDCALL:
2780                 case CEE_CALL:
2781                         call = (MonoCallInst*)ins;
2782                         if (ins->flags & MONO_INST_HAS_METHOD)
2783                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2784                         else
2785                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2786                         ppc_bl (code, 0);
2787                         break;
2788                 case OP_FCALL_REG:
2789                 case OP_LCALL_REG:
2790                 case OP_VCALL_REG:
2791                 case OP_VOIDCALL_REG:
2792                 case OP_CALL_REG:
2793                         ppc_mtlr (code, ins->sreg1);
2794                         ppc_blrl (code);
2795                         break;
2796                 case OP_FCALL_MEMBASE:
2797                 case OP_LCALL_MEMBASE:
2798                 case OP_VCALL_MEMBASE:
2799                 case OP_VOIDCALL_MEMBASE:
2800                 case OP_CALL_MEMBASE:
2801                         ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2802                         ppc_mtlr (code, ppc_r0);
2803                         ppc_blrl (code);
2804                         break;
2805                 case OP_OUTARG:
2806                         g_assert_not_reached ();
2807                         break;
2808                 case OP_LOCALLOC: {
2809                         /* keep alignment */
2810                         int alloca_waste = PPC_STACK_PARAM_OFFSET + cfg->param_area + 31;
2811                         int area_offset = alloca_waste;
2812                         area_offset &= ~31;
2813                         ppc_addi (code, ppc_r11, ins->sreg1, alloca_waste + 31);
2814                         ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 0, 27);
2815                         ppc_lwz (code, ppc_r0, 0, ppc_sp);
2816                         ppc_neg (code, ppc_r11, ppc_r11);
2817                         ppc_stwux (code, ppc_r0, ppc_sp, ppc_r11);
2818                         ppc_addi (code, ins->dreg, ppc_sp, area_offset);
2819                         break;
2820                 }
2821                 case CEE_RET:
2822                         ppc_blr (code);
2823                         break;
2824                 case CEE_THROW: {
2825                         //ppc_break (code);
2826                         ppc_mr (code, ppc_r3, ins->sreg1);
2827                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
2828                                              (gpointer)"mono_arch_throw_exception");
2829                         ppc_bl (code, 0);
2830                         break;
2831                 }
2832                 case OP_START_HANDLER:
2833                         ppc_mflr (code, ppc_r0);
2834                         if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2835                                 ppc_stw (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2836                         } else {
2837                                 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2838                                 ppc_stwx (code, ppc_r0, ppc_r11, ins->inst_left->inst_basereg);
2839                         }
2840                         break;
2841                 case OP_ENDFILTER:
2842                         if (ins->sreg1 != ppc_r3)
2843                                 ppc_mr (code, ppc_r3, ins->sreg1);
2844                         if (ppc_is_imm16 (ins->inst_left->inst_offset)) {
2845                                 ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2846                         } else {
2847                                 ppc_load (code, ppc_r11, ins->inst_left->inst_offset);
2848                                 ppc_lwzx (code, ppc_r0, ins->inst_left->inst_basereg, ppc_r11);
2849                         }
2850                         ppc_mtlr (code, ppc_r0);
2851                         ppc_blr (code);
2852                         break;
2853                 case CEE_ENDFINALLY:
2854                         ppc_lwz (code, ppc_r0, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
2855                         ppc_mtlr (code, ppc_r0);
2856                         ppc_blr (code);
2857                         break;
2858                 case OP_CALL_HANDLER: 
2859                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2860                         ppc_bl (code, 0);
2861                         break;
2862                 case OP_LABEL:
2863                         ins->inst_c0 = code - cfg->native_code;
2864                         break;
2865                 case CEE_BR:
2866                         //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2867                         //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2868                         //break;
2869                         if (ins->flags & MONO_INST_BRLABEL) {
2870                                 /*if (ins->inst_i0->inst_c0) {
2871                                         ppc_b (code, 0);
2872                                         //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2873                                 } else*/ {
2874                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2875                                         ppc_b (code, 0);
2876                                 }
2877                         } else {
2878                                 /*if (ins->inst_target_bb->native_offset) {
2879                                         ppc_b (code, 0);
2880                                         //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset); 
2881                                 } else*/ {
2882                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2883                                         ppc_b (code, 0);
2884                                 } 
2885                         }
2886                         break;
2887                 case OP_BR_REG:
2888                         ppc_mtctr (code, ins->sreg1);
2889                         ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2890                         break;
2891                 case OP_CEQ:
2892                         ppc_li (code, ins->dreg, 0);
2893                         ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2894                         ppc_li (code, ins->dreg, 1);
2895                         break;
2896                 case OP_CLT:
2897                 case OP_CLT_UN:
2898                         ppc_li (code, ins->dreg, 1);
2899                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2900                         ppc_li (code, ins->dreg, 0);
2901                         break;
2902                 case OP_CGT:
2903                 case OP_CGT_UN:
2904                         ppc_li (code, ins->dreg, 1);
2905                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
2906                         ppc_li (code, ins->dreg, 0);
2907                         break;
2908                 case OP_COND_EXC_EQ:
2909                 case OP_COND_EXC_NE_UN:
2910                 case OP_COND_EXC_LT:
2911                 case OP_COND_EXC_LT_UN:
2912                 case OP_COND_EXC_GT:
2913                 case OP_COND_EXC_GT_UN:
2914                 case OP_COND_EXC_GE:
2915                 case OP_COND_EXC_GE_UN:
2916                 case OP_COND_EXC_LE:
2917                 case OP_COND_EXC_LE_UN:
2918                         EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
2919                         break;
2920                 case OP_COND_EXC_C:
2921                         /* check XER [0-3] (SO, OV, CA): we can't use mcrxr
2922                          */
2923                         /*ppc_mfspr (code, ppc_r0, ppc_xer);
2924                         ppc_andisd (code, ppc_r0, ppc_r0, (1<<14));
2925                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
2926                         break;*/
2927                 case OP_COND_EXC_OV:
2928                         /*ppc_mcrxr (code, 0);
2929                         EMIT_COND_SYSTEM_EXCEPTION (CEE_BGT - CEE_BEQ, ins->inst_p1);
2930                         break;*/
2931                 case OP_COND_EXC_NC:
2932                 case OP_COND_EXC_NO:
2933                         g_assert_not_reached ();
2934                         break;
2935                 case CEE_BEQ:
2936                 case CEE_BNE_UN:
2937                 case CEE_BLT:
2938                 case CEE_BLT_UN:
2939                 case CEE_BGT:
2940                 case CEE_BGT_UN:
2941                 case CEE_BGE:
2942                 case CEE_BGE_UN:
2943                 case CEE_BLE:
2944                 case CEE_BLE_UN:
2945                         EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2946                         break;
2947
2948                 /* floating point opcodes */
2949                 case OP_R8CONST:
2950                         ppc_load (code, ppc_r11, ins->inst_p0);
2951                         ppc_lfd (code, ins->dreg, 0, ppc_r11);
2952                         break;
2953                 case OP_R4CONST:
2954                         ppc_load (code, ppc_r11, ins->inst_p0);
2955                         ppc_lfs (code, ins->dreg, 0, ppc_r11);
2956                         break;
2957                 case OP_STORER8_MEMBASE_REG:
2958                         if (ppc_is_imm16 (ins->inst_offset)) {
2959                                 ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2960                         } else {
2961                                 ppc_load (code, ppc_r11, ins->inst_offset);
2962                                 ppc_stfdx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2963                         }
2964                         break;
2965                 case OP_LOADR8_MEMBASE:
2966                         if (ppc_is_imm16 (ins->inst_offset)) {
2967                                 ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2968                         } else {
2969                                 ppc_load (code, ppc_r11, ins->inst_offset);
2970                                 ppc_lfdx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2971                         }
2972                         break;
2973                 case OP_STORER4_MEMBASE_REG:
2974                         if (ppc_is_imm16 (ins->inst_offset)) {
2975                                 ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2976                         } else {
2977                                 ppc_load (code, ppc_r11, ins->inst_offset);
2978                                 ppc_stfsx (code, ins->sreg1, ppc_r11, ins->inst_destbasereg);
2979                         }
2980                         break;
2981                 case OP_LOADR4_MEMBASE:
2982                         if (ppc_is_imm16 (ins->inst_offset)) {
2983                                 ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2984                         } else {
2985                                 ppc_load (code, ppc_r11, ins->inst_offset);
2986                                 ppc_lfsx (code, ins->dreg, ppc_r11, ins->inst_basereg);
2987                         }
2988                         break;
2989                 case CEE_CONV_R_UN: {
2990                         static const guint64 adjust_val = 0x4330000000000000ULL;
2991                         ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
2992                         ppc_stw (code, ppc_r0, -8, ppc_sp);
2993                         ppc_stw (code, ins->sreg1, -4, ppc_sp);
2994                         ppc_load (code, ppc_r11, &adjust_val);
2995                         ppc_lfd (code, ins->dreg, -8, ppc_sp);
2996                         ppc_lfd (code, ppc_f0, 0, ppc_r11);
2997                         ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
2998                         break;
2999                 }
3000                 case CEE_CONV_R4: /* FIXME: change precision */
3001                 case CEE_CONV_R8: {
3002                         static const guint64 adjust_val = 0x4330000080000000ULL;
3003                         // addis is special for ppc_r0
3004                         ppc_addis (code, ppc_r0, ppc_r0, 0x4330);
3005                         ppc_stw (code, ppc_r0, -8, ppc_sp);
3006                         ppc_xoris (code, ins->sreg1, ppc_r11, 0x8000);
3007                         ppc_stw (code, ppc_r11, -4, ppc_sp);
3008                         ppc_lfd (code, ins->dreg, -8, ppc_sp);
3009                         ppc_load (code, ppc_r11, &adjust_val);
3010                         ppc_lfd (code, ppc_f0, 0, ppc_r11);
3011                         ppc_fsub (code, ins->dreg, ins->dreg, ppc_f0);
3012                         break;
3013                 }
3014                 case OP_X86_FP_LOAD_I8:
3015                         g_assert_not_reached ();
3016                         /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);*/
3017                         break;
3018                 case OP_X86_FP_LOAD_I4:
3019                         g_assert_not_reached ();
3020                         /*x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);*/
3021                         break;
3022                 case OP_FCONV_TO_I1:
3023                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
3024                         break;
3025                 case OP_FCONV_TO_U1:
3026                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
3027                         break;
3028                 case OP_FCONV_TO_I2:
3029                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
3030                         break;
3031                 case OP_FCONV_TO_U2:
3032                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
3033                         break;
3034                 case OP_FCONV_TO_I4:
3035                 case OP_FCONV_TO_I:
3036                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
3037                         break;
3038                 case OP_FCONV_TO_U4:
3039                 case OP_FCONV_TO_U:
3040                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
3041                         break;
3042                 case OP_FCONV_TO_I8:
3043                 case OP_FCONV_TO_U8:
3044                         g_assert_not_reached ();
3045                         /* Implemented as helper calls */
3046                         break;
3047                 case OP_LCONV_TO_R_UN:
3048                         g_assert_not_reached ();
3049                         /* Implemented as helper calls */
3050                         break;
3051                 case OP_LCONV_TO_OVF_I: {
3052                         ppc_mr (code, ins->dreg, ins->sreg1);
3053                         /* FIXME: emit exception if needed */
3054                         break;
3055                 }
3056                 case OP_SQRT:
3057                         ppc_fsqrtd (code, ins->dreg, ins->sreg1);
3058                         break;
3059                 case OP_FADD:
3060                         ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
3061                         break;
3062                 case OP_FSUB:
3063                         ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
3064                         break;          
3065                 case OP_FMUL:
3066                         ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
3067                         break;          
3068                 case OP_FDIV:
3069                         ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
3070                         break;          
3071                 case OP_FNEG:
3072                         ppc_fneg (code, ins->dreg, ins->sreg1);
3073                         break;          
3074                 case OP_FREM:
3075                         /* emulated */
3076                         g_assert_not_reached ();
3077                         break;
3078                 case OP_FCOMPARE:
3079                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3080                         break;
3081                 case OP_FCEQ:
3082                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3083                         ppc_li (code, ins->dreg, 0);
3084                         ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
3085                         ppc_li (code, ins->dreg, 1);
3086                         break;
3087                 case OP_FCLT:
3088                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3089                         ppc_li (code, ins->dreg, 1);
3090                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3091                         ppc_li (code, ins->dreg, 0);
3092                         break;
3093                 case OP_FCLT_UN:
3094                         ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3095                         ppc_li (code, ins->dreg, 1);
3096                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3097                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
3098                         ppc_li (code, ins->dreg, 0);
3099                         break;
3100                 case OP_FCGT:
3101                         ppc_fcmpo (code, 0, ins->sreg1, ins->sreg2);
3102                         ppc_li (code, ins->dreg, 1);
3103                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3104                         ppc_li (code, ins->dreg, 0);
3105                         break;
3106                 case OP_FCGT_UN:
3107                         ppc_fcmpu (code, 0, ins->sreg1, ins->sreg2);
3108                         ppc_li (code, ins->dreg, 1);
3109                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_SO, 3);
3110                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_GT, 2);
3111                         ppc_li (code, ins->dreg, 0);
3112                         break;
3113                 case OP_FBEQ:
3114                         EMIT_COND_BRANCH (ins, CEE_BEQ - CEE_BEQ);
3115                         break;
3116                 case OP_FBNE_UN:
3117                         EMIT_COND_BRANCH (ins, CEE_BNE_UN - CEE_BEQ);
3118                         break;
3119                 case OP_FBLT:
3120                         EMIT_COND_BRANCH (ins, CEE_BLT - CEE_BEQ);
3121                         break;
3122                 case OP_FBLT_UN:
3123                         EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3124                         EMIT_COND_BRANCH (ins, CEE_BLT_UN - CEE_BEQ);
3125                         break;
3126                 case OP_FBGT:
3127                         EMIT_COND_BRANCH (ins, CEE_BGT - CEE_BEQ);
3128                         break;
3129                 case OP_FBGT_UN:
3130                         EMIT_COND_BRANCH_FLAGS (ins, PPC_BR_TRUE, PPC_BR_SO);
3131                         EMIT_COND_BRANCH (ins, CEE_BGT_UN - CEE_BEQ);
3132                         break;
3133                 case OP_FBGE:
3134                         EMIT_COND_BRANCH (ins, CEE_BGE - CEE_BEQ);
3135                         break;
3136                 case OP_FBGE_UN:
3137                         EMIT_COND_BRANCH (ins, CEE_BGE_UN - CEE_BEQ);
3138                         break;
3139                 case OP_FBLE:
3140                         EMIT_COND_BRANCH (ins, CEE_BLE - CEE_BEQ);
3141                         break;
3142                 case OP_FBLE_UN:
3143                         EMIT_COND_BRANCH (ins, CEE_BLE_UN - CEE_BEQ);
3144                         break;
3145                 case CEE_CKFINITE: {
3146                         ppc_stfd (code, ins->sreg1, -8, ppc_sp);
3147                         ppc_lwz (code, ppc_r11, -8, ppc_sp);
3148                         ppc_rlwinm (code, ppc_r11, ppc_r11, 0, 1, 31);
3149                         ppc_addis (code, ppc_r11, ppc_r11, -32752);
3150                         ppc_rlwinmd (code, ppc_r11, ppc_r11, 1, 31, 31);
3151                         EMIT_COND_SYSTEM_EXCEPTION (CEE_BEQ - CEE_BEQ, "ArithmeticException");
3152                         break;
3153                 }
3154                 default:
3155                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3156                         g_assert_not_reached ();
3157                 }
3158
3159                 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
3160                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3161                                    mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
3162                         g_assert_not_reached ();
3163                 }
3164                
3165                 cpos += max_len;
3166
3167                 last_ins = ins;
3168                 last_offset = offset;
3169                 
3170                 ins = ins->next;
3171         }
3172
3173         cfg->code_len = code - cfg->native_code;
3174 }
3175
3176 void
3177 mono_arch_register_lowlevel_calls (void)
3178 {
3179 }
3180
3181 #define patch_lis_ori(ip,val) do {\
3182                 guint16 *__lis_ori = (guint16*)(ip);    \
3183                 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff;      \
3184                 __lis_ori [3] = ((guint32)(val)) & 0xffff;      \
3185         } while (0)
3186
3187 void
3188 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3189 {
3190         MonoJumpInfo *patch_info;
3191
3192         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3193                 unsigned char *ip = patch_info->ip.i + code;
3194                 const unsigned char *target;
3195
3196                 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3197
3198                 switch (patch_info->type) {
3199                 case MONO_PATCH_INFO_IP:
3200                         patch_lis_ori (ip, ip);
3201                         continue;
3202                 case MONO_PATCH_INFO_METHOD_REL:
3203                         g_assert_not_reached ();
3204                         *((gpointer *)(ip)) = code + patch_info->data.offset;
3205                         continue;
3206                 case MONO_PATCH_INFO_SWITCH: {
3207                         gpointer *table = (gpointer *)patch_info->data.target;
3208                         int i;
3209
3210                         // FIXME: inspect code to get the register
3211                         ppc_load (ip, ppc_r11, patch_info->data.target);
3212                         //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
3213
3214                         for (i = 0; i < patch_info->table_size; i++) {
3215                                 table [i] = (int)patch_info->data.table [i] + code;
3216                         }
3217                         /* we put into the table the absolute address, no need for ppc_patch in this case */
3218                         continue;
3219                 }
3220                 case MONO_PATCH_INFO_METHODCONST:
3221                 case MONO_PATCH_INFO_CLASS:
3222                 case MONO_PATCH_INFO_IMAGE:
3223                 case MONO_PATCH_INFO_FIELD:
3224                 case MONO_PATCH_INFO_VTABLE:
3225                 case MONO_PATCH_INFO_IID:
3226                 case MONO_PATCH_INFO_SFLDA:
3227                 case MONO_PATCH_INFO_LDSTR:
3228                 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3229                 case MONO_PATCH_INFO_LDTOKEN:
3230                         /* from OP_AOTCONST : lis + ori */
3231                         patch_lis_ori (ip, target);
3232                         continue;
3233                 case MONO_PATCH_INFO_R4:
3234                 case MONO_PATCH_INFO_R8:
3235                         g_assert_not_reached ();
3236                         *((gconstpointer *)(ip + 2)) = patch_info->data.target;
3237                         continue;
3238                 case MONO_PATCH_INFO_EXC_NAME:
3239                         g_assert_not_reached ();
3240                         *((gconstpointer *)(ip + 1)) = patch_info->data.name;
3241                         continue;
3242                 case MONO_PATCH_INFO_BB_OVF:
3243                 case MONO_PATCH_INFO_EXC_OVF:
3244                         /* everything is dealt with at epilog output time */
3245                         continue;
3246                 default:
3247                         break;
3248                 }
3249                 ppc_patch (ip, target);
3250         }
3251 }
3252
3253 int
3254 mono_arch_max_epilog_size (MonoCompile *cfg)
3255 {
3256         int max_epilog_size = 16 + 20*4;
3257         MonoJumpInfo *patch_info;
3258         
3259         if (cfg->method->save_lmf)
3260                 max_epilog_size += 128;
3261         
3262         if (mono_jit_trace_calls != NULL)
3263                 max_epilog_size += 50;
3264
3265         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
3266                 max_epilog_size += 50;
3267
3268         /* count the number of exception infos */
3269      
3270         /* 
3271          * make sure we have enough space for exceptions
3272          * 24 is the simulated call to throw_exception_by_name
3273          */
3274         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3275                 if (patch_info->type == MONO_PATCH_INFO_EXC)
3276                         max_epilog_size += 24;
3277                 else if (patch_info->type == MONO_PATCH_INFO_BB_OVF)
3278                         max_epilog_size += 12;
3279                 else if (patch_info->type == MONO_PATCH_INFO_EXC_OVF)
3280                         max_epilog_size += 12;
3281         }
3282
3283         return max_epilog_size;
3284 }
3285
3286 /*
3287  * Stack frame layout:
3288  * 
3289  *   ------------------- sp
3290  *      MonoLMF structure or saved registers
3291  *   -------------------
3292  *      spilled regs
3293  *   -------------------
3294  *      locals
3295  *   -------------------
3296  *      optional 8 bytes for tracing
3297  *   -------------------
3298  *      param area             size is cfg->param_area
3299  *   -------------------
3300  *      linkage area           size is PPC_STACK_PARAM_OFFSET
3301  *   ------------------- sp
3302  *      red zone
3303  */
3304 guint8 *
3305 mono_arch_emit_prolog (MonoCompile *cfg)
3306 {
3307         MonoMethod *method = cfg->method;
3308         MonoBasicBlock *bb;
3309         MonoMethodSignature *sig;
3310         MonoInst *inst;
3311         int alloc_size, pos, max_offset, i;
3312         guint8 *code;
3313         CallInfo *cinfo;
3314         int tracing = 0;
3315         int lmf_offset = 0;
3316
3317         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
3318                 tracing = 1;
3319
3320         cfg->code_size = 256;
3321         code = cfg->native_code = g_malloc (cfg->code_size);
3322
3323         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3324                 ppc_mflr (code, ppc_r0);
3325                 ppc_stw (code, ppc_r0, PPC_RET_ADDR_OFFSET, ppc_sp);
3326         }
3327         cfg->used_int_regs |= USE_EXTRA_TEMPS;
3328
3329         alloc_size = cfg->stack_offset;
3330         pos = 0;
3331
3332         if (!method->save_lmf) {
3333                 /*for (i = 31; i >= 14; --i) {
3334                         if (cfg->used_float_regs & (1 << i)) {
3335                                 pos += sizeof (gdouble);
3336                                 ppc_stfd (code, i, -pos, ppc_sp);
3337                         }
3338                 }*/
3339                 for (i = 31; i >= 13; --i) {
3340                         if (cfg->used_int_regs & (1 << i)) {
3341                                 pos += sizeof (gulong);
3342                                 ppc_stw (code, i, -pos, ppc_sp);
3343                         }
3344                 }
3345         } else {
3346                 int ofs;
3347                 pos += sizeof (MonoLMF);
3348                 lmf_offset = pos;
3349                 ofs = -pos + G_STRUCT_OFFSET(MonoLMF, iregs);
3350                 ppc_stmw (code, ppc_r13, ppc_r1, ofs);
3351                 for (i = 14; i < 32; i++) {
3352                         ppc_stfd (code, i, (-pos + G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble))), ppc_r1);
3353                 }
3354         }
3355         alloc_size += pos;
3356         // align to PPC_STACK_ALIGNMENT bytes
3357         if (alloc_size & (PPC_STACK_ALIGNMENT - 1)) {
3358                 alloc_size += PPC_STACK_ALIGNMENT - 1;
3359                 alloc_size &= ~(PPC_STACK_ALIGNMENT - 1);
3360         }
3361
3362         cfg->stack_usage = alloc_size;
3363         g_assert ((alloc_size & (PPC_STACK_ALIGNMENT-1)) == 0);
3364         if (alloc_size) {
3365                 if (ppc_is_imm16 (-alloc_size)) {
3366                         ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
3367                 } else {
3368                         ppc_load (code, ppc_r11, -alloc_size);
3369                         ppc_stwux (code, ppc_sp, ppc_sp, ppc_r11);
3370                 }
3371         }
3372         if (cfg->frame_reg != ppc_sp)
3373                 ppc_mr (code, cfg->frame_reg, ppc_sp);
3374
3375         /* compute max_offset in order to use short forward jumps
3376          * we always do it on ppc because the immediate displacement
3377          * for jumps is too small 
3378          */
3379         max_offset = 0;
3380         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
3381                 MonoInst *ins = bb->code;
3382                 bb->max_offset = max_offset;
3383
3384                 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
3385                         max_offset += 6; 
3386
3387                 while (ins) {
3388                         max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
3389                         ins = ins->next;
3390                 }
3391         }
3392
3393         /* load arguments allocated to register from the stack */
3394         sig = method->signature;
3395         pos = 0;
3396
3397         cinfo = calculate_sizes (sig, sig->pinvoke);
3398
3399         if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3400                 ArgInfo *ainfo = &cinfo->ret;
3401                 inst = cfg->ret;
3402                 if (ppc_is_imm16 (inst->inst_offset)) {
3403                         ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3404                 } else {
3405                         ppc_load (code, ppc_r11, inst->inst_offset);
3406                         ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3407                 }
3408         }
3409         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3410                 ArgInfo *ainfo = cinfo->args + i;
3411                 inst = cfg->varinfo [pos];
3412                 
3413                 if (cfg->verbose_level > 2)
3414                         g_print ("Saving argument %d (type: %d)\n", i, ainfo->regtype);
3415                 if (inst->opcode == OP_REGVAR) {
3416                         if (ainfo->regtype == RegTypeGeneral)
3417                                 ppc_mr (code, inst->dreg, ainfo->reg);
3418                         else if (ainfo->regtype == RegTypeFP)
3419                                 ppc_fmr (code, inst->dreg, ainfo->reg);
3420                         else if (ainfo->regtype == RegTypeBase) {
3421                                 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3422                                 ppc_lwz (code, inst->dreg, ainfo->offset, ppc_r11);
3423                         } else
3424                                 g_assert_not_reached ();
3425
3426                         if (cfg->verbose_level > 2)
3427                                 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
3428                 } else {
3429                         /* the argument should be put on the stack: FIXME handle size != word  */
3430                         if (ainfo->regtype == RegTypeGeneral) {
3431                                 switch (ainfo->size) {
3432                                 case 1:
3433                                         if (ppc_is_imm16 (inst->inst_offset)) {
3434                                                 ppc_stb (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3435                                         } else {
3436                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3437                                                 ppc_stbx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3438                                         }
3439                                         break;
3440                                 case 2:
3441                                         if (ppc_is_imm16 (inst->inst_offset)) {
3442                                                 ppc_sth (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3443                                         } else {
3444                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3445                                                 ppc_sthx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3446                                         }
3447                                         break;
3448                                 case 8:
3449                                         if (ppc_is_imm16 (inst->inst_offset + 4)) {
3450                                                 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3451                                                 ppc_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
3452                                         } else {
3453                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3454                                                 ppc_add (code, ppc_r11, ppc_r11, inst->inst_basereg);
3455                                                 ppc_stw (code, ainfo->reg, 0, ppc_r11);
3456                                                 ppc_stw (code, ainfo->reg + 1, 4, ppc_r11);
3457                                         }
3458                                         break;
3459                                 default:
3460                                         if (ppc_is_imm16 (inst->inst_offset)) {
3461                                                 ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3462                                         } else {
3463                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3464                                                 ppc_stwx (code, ainfo->reg, ppc_r11, inst->inst_basereg);
3465                                         }
3466                                         break;
3467                                 }
3468                         } else if (ainfo->regtype == RegTypeBase) {
3469                                 /* load the previous stack pointer in r11 */
3470                                 ppc_lwz (code, ppc_r11, 0, ppc_sp);
3471                                 ppc_lwz (code, ppc_r0, ainfo->offset, ppc_r11);
3472                                 switch (ainfo->size) {
3473                                 case 1:
3474                                         if (ppc_is_imm16 (inst->inst_offset)) {
3475                                                 ppc_stb (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3476                                         } else {
3477                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3478                                                 ppc_stbx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3479                                         }
3480                                         break;
3481                                 case 2:
3482                                         if (ppc_is_imm16 (inst->inst_offset)) {
3483                                                 ppc_sth (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3484                                         } else {
3485                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3486                                                 ppc_sthx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3487                                         }
3488                                         break;
3489                                 case 8:
3490                                         if (ppc_is_imm16 (inst->inst_offset + 4)) {
3491                                                 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3492                                                 ppc_lwz (code, ppc_r0, ainfo->offset + 4, ppc_r11);
3493                                                 ppc_stw (code, ppc_r0, inst->inst_offset + 4, inst->inst_basereg);
3494                                         } else {
3495                                                 /* FIXME */
3496                                                 g_assert_not_reached ();
3497                                         }
3498                                         break;
3499                                 default:
3500                                         if (ppc_is_imm16 (inst->inst_offset)) {
3501                                                 ppc_stw (code, ppc_r0, inst->inst_offset, inst->inst_basereg);
3502                                         } else {
3503                                                 ppc_load (code, ppc_r11, inst->inst_offset);
3504                                                 ppc_stwx (code, ppc_r0, ppc_r11, inst->inst_basereg);
3505                                         }
3506                                         break;
3507                                 }
3508                         } else if (ainfo->regtype == RegTypeFP) {
3509                                 g_assert (ppc_is_imm16 (inst->inst_offset));
3510                                 if (ainfo->size == 8)
3511                                         ppc_stfd (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3512                                 else if (ainfo->size == 4)
3513                                         ppc_stfs (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
3514                                 else
3515                                         g_assert_not_reached ();
3516                         } else if (ainfo->regtype == RegTypeStructByVal) {
3517                                 int doffset = inst->inst_offset;
3518                                 int soffset = 0;
3519                                 int cur_reg;
3520                                 g_assert (ppc_is_imm16 (inst->inst_offset));
3521                                 g_assert (ppc_is_imm16 (inst->inst_offset + ainfo->size * sizeof (gpointer)));
3522                                 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3523                                         ppc_stw (code, ainfo->reg + cur_reg, doffset, inst->inst_basereg);
3524                                         soffset += sizeof (gpointer);
3525                                         doffset += sizeof (gpointer);
3526                                 }
3527                                 if (ainfo->vtsize) {
3528                                         /* load the previous stack pointer in r11 (r0 gets overwritten by the memcpy) */
3529                                         ppc_lwz (code, ppc_r11, 0, ppc_sp);
3530                                         /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3531                                         code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ppc_r11, ainfo->offset + soffset);
3532                                 }
3533                         } else if (ainfo->regtype == RegTypeStructByAddr) {
3534                                 g_assert (ppc_is_imm16 (inst->inst_offset));
3535                                 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
3536                                 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
3537                         } else
3538                                 g_assert_not_reached ();
3539                 }
3540                 pos++;
3541         }
3542
3543         if (method->save_lmf) {
3544
3545                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3546                                      (gpointer)"mono_get_lmf_addr");
3547                 ppc_bl (code, 0);
3548                 /* we build the MonoLMF structure on the stack - see mini-ppc.h */
3549                 /* lmf_offset is the offset from the previous stack pointer,
3550                  * alloc_size is the total stack space allocated, so the offset
3551                  * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
3552                  * The pointer to the struct is put in ppc_r11 (new_lmf).
3553                  * The callee-saved registers are already in the MonoLMF structure
3554                  */
3555                 ppc_addi (code, ppc_r11, ppc_sp, alloc_size - lmf_offset);
3556                 /* ppc_r3 is the result from mono_get_lmf_addr () */
3557                 ppc_stw (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3558                 /* new_lmf->previous_lmf = *lmf_addr */
3559                 ppc_lwz (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3560                 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3561                 /* *(lmf_addr) = r11 */
3562                 ppc_stw (code, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
3563                 /* save method info */
3564                 ppc_load (code, ppc_r0, method);
3565                 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
3566                 ppc_stw (code, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
3567                 /* save the current IP */
3568                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
3569                 ppc_load (code, ppc_r0, 0x01010101);
3570                 ppc_stw (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
3571         }
3572
3573         if (tracing)
3574                 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
3575
3576         cfg->code_len = code - cfg->native_code;
3577         g_free (cinfo);
3578
3579         return code;
3580 }
3581
3582 void
3583 mono_arch_emit_epilog (MonoCompile *cfg)
3584 {
3585         MonoJumpInfo *patch_info;
3586         MonoMethod *method = cfg->method;
3587         int pos, i;
3588         guint8 *code;
3589
3590         /*
3591          * Keep in sync with CEE_JMP
3592          */
3593         code = cfg->native_code + cfg->code_len;
3594
3595         if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
3596                 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
3597         }
3598         pos = 0;
3599
3600         if (method->save_lmf) {
3601                 int lmf_offset;
3602                 pos +=  sizeof (MonoLMF);
3603                 lmf_offset = pos;
3604                 /* save the frame reg in r8 */
3605                 ppc_mr (code, ppc_r8, cfg->frame_reg);
3606                 ppc_addi (code, ppc_r11, cfg->frame_reg, cfg->stack_usage - lmf_offset);
3607                 /* r5 = previous_lmf */
3608                 ppc_lwz (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
3609                 /* r6 = lmf_addr */
3610                 ppc_lwz (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
3611                 /* *(lmf_addr) = previous_lmf */
3612                 ppc_stw (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
3613                 /* FIXME: speedup: there is no actual need to restore the registers if
3614                  * we didn't actually change them (idea from Zoltan).
3615                  */
3616                 /* restore iregs */
3617                 ppc_lmw (code, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
3618                 /* restore fregs */
3619                 /*for (i = 14; i < 32; i++) {
3620                         ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
3621                 }*/
3622                 g_assert (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET));
3623                 /* use the saved copy of the frame reg in r8 */
3624                 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3625                         ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, ppc_r8);
3626                         ppc_mtlr (code, ppc_r0);
3627                 }
3628                 ppc_addic (code, ppc_sp, ppc_r8, cfg->stack_usage);
3629         } else {
3630                 if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
3631                         if (ppc_is_imm16 (cfg->stack_usage + PPC_RET_ADDR_OFFSET)) {
3632                                 ppc_lwz (code, ppc_r0, cfg->stack_usage + PPC_RET_ADDR_OFFSET, cfg->frame_reg);
3633                         } else {
3634                                 ppc_load (code, ppc_r11, cfg->stack_usage + PPC_RET_ADDR_OFFSET);
3635                                 ppc_lwzx (code, ppc_r0, cfg->frame_reg, ppc_r11);
3636                         }
3637                         ppc_mtlr (code, ppc_r0);
3638                 }
3639                 if (ppc_is_imm16 (cfg->stack_usage)) {
3640                         ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
3641                 } else {
3642                         ppc_load (code, ppc_r11, cfg->stack_usage);
3643                         ppc_add (code, ppc_sp, cfg->frame_reg, ppc_r11);
3644                 }
3645
3646                 /*for (i = 31; i >= 14; --i) {
3647                         if (cfg->used_float_regs & (1 << i)) {
3648                                 pos += sizeof (double);
3649                                 ppc_lfd (code, i, -pos, ppc_sp);
3650                         }
3651                 }*/
3652                 for (i = 31; i >= 13; --i) {
3653                         if (cfg->used_int_regs & (1 << i)) {
3654                                 pos += sizeof (gulong);
3655                                 ppc_lwz (code, i, -pos, ppc_sp);
3656                         }
3657                 }
3658         }
3659         ppc_blr (code);
3660
3661         /* add code to raise exceptions */
3662         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3663                 switch (patch_info->type) {
3664                 case MONO_PATCH_INFO_BB_OVF: {
3665                         MonoOvfJump *ovfj = patch_info->data.target;
3666                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
3667                         /* patch the initial jump */
3668                         ppc_patch (ip, code);
3669                         ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3670                         ppc_b (code, 0);
3671                         ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3672                         /* jump back to the true target */
3673                         ppc_b (code, 0);
3674                         ip = ovfj->bb->native_offset + cfg->native_code;
3675                         ppc_patch (code - 4, ip);
3676                         break;
3677                 }
3678                 case MONO_PATCH_INFO_EXC_OVF: {
3679                         MonoOvfJump *ovfj = patch_info->data.target;
3680                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
3681                         /* patch the initial jump */
3682                         ppc_patch (ip, code);
3683                         ppc_bc (code, ovfj->b0_cond, ovfj->b1_cond, 2);
3684                         ppc_b (code, 0);
3685                         ppc_patch (code - 4, ip + 4); /* jump back after the initiali branch */
3686                         /* jump back to the true target */
3687                         ppc_b (code, 0);
3688                         ip = (char*)ovfj->ip + 4;
3689                         ppc_patch (code - 4, ip);
3690                         break;
3691                 }
3692                 case MONO_PATCH_INFO_EXC: {
3693                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
3694                         ppc_patch (ip, code);
3695                         /*mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC_NAME, patch_info->data.target);*/
3696                         ppc_load (code, ppc_r3, patch_info->data.target);
3697                         /* simulate a call from ip */
3698                         ppc_load (code, ppc_r0, ip + 4);
3699                         ppc_mtlr (code, ppc_r0);
3700                         patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
3701                         patch_info->data.name = "mono_arch_throw_exception_by_name";
3702                         patch_info->ip.i = code - cfg->native_code;
3703                         ppc_b (code, 0);
3704                         break;
3705                 }
3706                 default:
3707                         /* do nothing */
3708                         break;
3709                 }
3710         }
3711
3712         cfg->code_len = code - cfg->native_code;
3713
3714         g_assert (cfg->code_len < cfg->code_size);
3715
3716 }
3717
3718 void
3719 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
3720 {
3721 }
3722
3723 void
3724 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
3725 {
3726 }
3727
3728 void
3729 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
3730 {
3731         int this_dreg = ppc_r3;
3732         
3733         if (vt_reg != -1)
3734                 this_dreg = ppc_r4;
3735
3736         /* add the this argument */
3737         if (this_reg != -1) {
3738                 MonoInst *this;
3739                 MONO_INST_NEW (cfg, this, OP_SETREG);
3740                 this->type = this_type;
3741                 this->sreg1 = this_reg;
3742                 this->dreg = this_dreg;
3743                 mono_bblock_add_inst (cfg->cbb, this);
3744         }
3745
3746         if (vt_reg != -1) {
3747                 MonoInst *vtarg;
3748                 MONO_INST_NEW (cfg, vtarg, OP_SETREG);
3749                 vtarg->type = STACK_MP;
3750                 vtarg->sreg1 = vt_reg;
3751                 vtarg->dreg = ppc_r3;
3752                 mono_bblock_add_inst (cfg->cbb, vtarg);
3753         }
3754 }
3755
3756 gint
3757 mono_arch_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3758 {
3759         /* optional instruction, need to detect it
3760         if (cmethod->klass == mono_defaults.math_class) {
3761                 if (strcmp (cmethod->name, "Sqrt") == 0)
3762                         return OP_SQRT;
3763         }*/
3764         return -1;
3765 }
3766
3767
3768 gboolean
3769 mono_arch_print_tree (MonoInst *tree, int arity)
3770 {
3771         return 0;
3772 }
3773
3774 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
3775 {
3776         return NULL;
3777 }
3778
3779 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
3780 {
3781         return NULL;
3782 }