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