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