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