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