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