grammar updates
[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
20 int mono_exc_esp_offset = 0;
21
22 const char*
23 mono_arch_regname (int reg) {
24         static const char * rnames[] = {
25                 "ppc_r0", "ppc_sp", "ppc_r2", "ppc_r3", "ppc_r4",
26                 "ppc_r5", "ppc_r6", "ppc_r7", "ppc_r8", "ppc_r9",
27                 "ppc_r10", "ppc_r11", "ppc_r12", "ppc_r13", "ppc_r14",
28                 "ppc_r15", "ppc_r16", "ppc_r17", "ppc_r18", "ppc_r19",
29                 "ppc_r20", "ppc_r21", "ppc_r22", "ppc_r23", "ppc_r24",
30                 "ppc_r25", "ppc_r26", "ppc_r27", "ppc_r28", "ppc_r29",
31                 "ppc_r30", "ppc_r31"
32         };
33         if (reg >= 0 && reg < 32)
34                 return rnames [reg];
35         return "unknown";
36 }
37
38 typedef struct {
39         guint16 size;
40         guint16 offset;
41         guint8  pad;
42 } MonoJitArgumentInfo;
43
44 /*
45  * arch_get_argument_info:
46  * @csig:  a method signature
47  * @param_count: the number of parameters to consider
48  * @arg_info: an array to store the result infos
49  *
50  * Gathers information on parameters such as size, alignment and
51  * padding. arg_info should be large enought to hold param_count + 1 entries. 
52  *
53  * Returns the size of the activation frame.
54  */
55 static int
56 arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
57 {
58         int k, frame_size = 0;
59         int size, align, pad;
60         int offset = 8;
61
62         if (MONO_TYPE_ISSTRUCT (csig->ret)) { 
63                 frame_size += sizeof (gpointer);
64                 offset += 4;
65         }
66
67         arg_info [0].offset = offset;
68
69         if (csig->hasthis) {
70                 frame_size += sizeof (gpointer);
71                 offset += 4;
72         }
73
74         arg_info [0].size = frame_size;
75
76         for (k = 0; k < param_count; k++) {
77                 
78                 if (csig->pinvoke)
79                         size = mono_type_native_stack_size (csig->params [k], &align);
80                 else
81                         size = mono_type_stack_size (csig->params [k], &align);
82
83                 /* ignore alignment for now */
84                 align = 1;
85
86                 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1); 
87                 arg_info [k].pad = pad;
88                 frame_size += size;
89                 arg_info [k + 1].pad = 0;
90                 arg_info [k + 1].size = size;
91                 offset += pad;
92                 arg_info [k + 1].offset = offset;
93                 offset += size;
94         }
95
96         align = MONO_ARCH_FRAME_ALIGNMENT;
97         frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
98         arg_info [k].pad = pad;
99
100         return frame_size;
101 }
102
103 static int indent_level = 0;
104
105 static void indent (int diff) {
106         int v = indent_level;
107         while (v-- > 0) {
108                 printf (". ");
109         }
110         indent_level += diff;
111 }
112
113 static void
114 enter_method (MonoMethod *method, char *ebp)
115 {
116         int i, j;
117         MonoClass *class;
118         MonoObject *o;
119         MonoJitArgumentInfo *arg_info;
120         MonoMethodSignature *sig;
121         char *fname;
122
123         fname = mono_method_full_name (method, TRUE);
124         indent (1);
125         printf ("ENTER: %s(", fname);
126         g_free (fname);
127         
128         if (((int)ebp & (MONO_ARCH_FRAME_ALIGNMENT - 1)) != 0) {
129                 g_error ("unaligned stack detected (%p)", ebp);
130         }
131
132         sig = method->signature;
133
134         arg_info = alloca (sizeof (MonoJitArgumentInfo) * (sig->param_count + 1));
135
136         arch_get_argument_info (sig, sig->param_count, arg_info);
137
138         if (MONO_TYPE_ISSTRUCT (method->signature->ret)) {
139                 g_assert (!method->signature->ret->byref);
140
141                 printf ("VALUERET:%p, ", *((gpointer *)(ebp + 8)));
142         }
143
144         if (method->signature->hasthis) {
145                 gpointer *this = (gpointer *)(ebp + arg_info [0].offset);
146                 if (method->klass->valuetype) {
147                         printf ("value:%p, ", *this);
148                 } else {
149                         o = *((MonoObject **)this);
150
151                         if (o) {
152                                 class = o->vtable->klass;
153
154                                 if (class == mono_defaults.string_class) {
155                                         printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
156                                 } else {
157                                         printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
158                                 }
159                         } else 
160                                 printf ("this:NULL, ");
161                 }
162         }
163
164         for (i = 0; i < method->signature->param_count; ++i) {
165                 gpointer *cpos = (gpointer *)(ebp + arg_info [i + 1].offset);
166                 int size = arg_info [i + 1].size;
167
168                 MonoType *type = method->signature->params [i];
169                 
170                 if (type->byref) {
171                         printf ("[BYREF:%p], ", *cpos); 
172                 } else switch (type->type) {
173                         
174                 case MONO_TYPE_I:
175                 case MONO_TYPE_U:
176                         printf ("%p, ", (gpointer)*((int *)(cpos)));
177                         break;
178                 case MONO_TYPE_BOOLEAN:
179                 case MONO_TYPE_CHAR:
180                 case MONO_TYPE_I1:
181                 case MONO_TYPE_U1:
182                 case MONO_TYPE_I2:
183                 case MONO_TYPE_U2:
184                 case MONO_TYPE_I4:
185                 case MONO_TYPE_U4:
186                         printf ("%d, ", *((int *)(cpos)));
187                         break;
188                 case MONO_TYPE_STRING: {
189                         MonoString *s = *((MonoString **)cpos);
190                         if (s) {
191                                 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
192                                 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
193                         } else 
194                                 printf ("[STRING:null], ");
195                         break;
196                 }
197                 case MONO_TYPE_CLASS:
198                 case MONO_TYPE_OBJECT: {
199                         o = *((MonoObject **)cpos);
200                         if (o) {
201                                 class = o->vtable->klass;
202                     
203                                 if (class == mono_defaults.string_class) {
204                                         printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
205                                 } else if (class == mono_defaults.int32_class) {
206                                         printf ("[INT32:%p:%d], ", o, *(gint32 *)((char *)o + sizeof (MonoObject)));
207                                 } else
208                                         printf ("[%s.%s:%p], ", class->name_space, class->name, o);
209                         } else {
210                                 printf ("%p, ", *((gpointer *)(cpos)));                         
211                         }
212                         break;
213                 }
214                 case MONO_TYPE_PTR:
215                 case MONO_TYPE_FNPTR:
216                 case MONO_TYPE_ARRAY:
217                 case MONO_TYPE_SZARRAY:
218                         printf ("%p, ", *((gpointer *)(cpos)));
219                         break;
220                 case MONO_TYPE_I8:
221                         printf ("%lld, ", *((gint64 *)(cpos)));
222                         break;
223                 case MONO_TYPE_R4:
224                         printf ("%f, ", *((float *)(cpos)));
225                         break;
226                 case MONO_TYPE_R8:
227                         printf ("%f, ", *((double *)(cpos)));
228                         break;
229                 case MONO_TYPE_VALUETYPE: 
230                         printf ("[");
231                         for (j = 0; j < size; j++)
232                                 printf ("%02x,", *((guint8*)cpos +j));
233                         printf ("], ");
234                         break;
235                 default:
236                         printf ("XX, ");
237                 }
238         }
239
240         printf (")\n");
241 }
242
243 static void
244 leave_method (MonoMethod *method, ...)
245 {
246         MonoType *type;
247         char *fname;
248         va_list ap;
249
250         va_start(ap, method);
251
252         fname = mono_method_full_name (method, TRUE);
253         indent (-1);
254         printf ("LEAVE: %s", fname);
255         g_free (fname);
256
257         type = method->signature->ret;
258
259 handle_enum:
260         switch (type->type) {
261         case MONO_TYPE_VOID:
262                 break;
263         case MONO_TYPE_BOOLEAN: {
264                 int eax = va_arg (ap, int);
265                 if (eax)
266                         printf ("TRUE:%d", eax);
267                 else 
268                         printf ("FALSE");
269                         
270                 break;
271         }
272         case MONO_TYPE_CHAR:
273         case MONO_TYPE_I1:
274         case MONO_TYPE_U1:
275         case MONO_TYPE_I2:
276         case MONO_TYPE_U2:
277         case MONO_TYPE_I4:
278         case MONO_TYPE_U4:
279         case MONO_TYPE_I:
280         case MONO_TYPE_U: {
281                 int eax = va_arg (ap, int);
282                 printf ("EAX=%d", eax);
283                 break;
284         }
285         case MONO_TYPE_STRING: {
286                 MonoString *s = va_arg (ap, MonoString *);
287 ;
288                 if (s) {
289                         g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
290                         printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
291                 } else 
292                         printf ("[STRING:null], ");
293                 break;
294         }
295         case MONO_TYPE_CLASS: 
296         case MONO_TYPE_OBJECT: {
297                 MonoObject *o = va_arg (ap, MonoObject *);
298
299                 if (o) {
300                         if (o->vtable->klass == mono_defaults.boolean_class) {
301                                 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));            
302                         } else if  (o->vtable->klass == mono_defaults.int32_class) {
303                                 printf ("[INT32:%p:%d]", o, *((gint32 *)((char *)o + sizeof (MonoObject))));    
304                         } else if  (o->vtable->klass == mono_defaults.int64_class) {
305                                 printf ("[INT64:%p:%lld]", o, *((gint64 *)((char *)o + sizeof (MonoObject))));  
306                         } else
307                                 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
308                 } else
309                         printf ("[OBJECT:%p]", o);
310                
311                 break;
312         }
313         case MONO_TYPE_PTR:
314         case MONO_TYPE_FNPTR:
315         case MONO_TYPE_ARRAY:
316         case MONO_TYPE_SZARRAY: {
317                 gpointer p = va_arg (ap, gpointer);
318                 printf ("result=%p", p);
319                 break;
320         }
321         case MONO_TYPE_I8: {
322                 gint64 l =  va_arg (ap, gint64);
323                 printf ("lresult=%lld", l);
324                 break;
325         }
326         case MONO_TYPE_R8: {
327                 double f = va_arg (ap, double);
328                 printf ("FP=%f\n", f);
329                 break;
330         }
331         case MONO_TYPE_VALUETYPE: 
332                 if (type->data.klass->enumtype) {
333                         type = type->data.klass->enum_basetype;
334                         goto handle_enum;
335                 } else {
336                         guint8 *p = va_arg (ap, gpointer);
337                         int j, size, align;
338                         size = mono_type_size (type, &align);
339                         printf ("[");
340                         for (j = 0; p && j < size; j++)
341                                 printf ("%02x,", p [j]);
342                         printf ("]");
343                 }
344                 break;
345         default:
346                 printf ("(unknown return type %x)", method->signature->ret->type);
347         }
348
349         printf ("\n");
350 }
351
352 /*
353  * Initialize the cpu to execute managed code.
354  */
355 void
356 mono_arch_cpu_init (void)
357 {
358 }
359
360 /*
361  * This function returns the optimizations supported on this cpu.
362  */
363 guint32
364 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
365 {
366         guint32 opts = 0;
367
368         /* no ppc-specific optimizations yet */
369         *exclude_mask = 0;
370         return opts;
371 }
372
373 static gboolean
374 is_regsize_var (MonoType *t) {
375         if (t->byref)
376                 return TRUE;
377         switch (t->type) {
378         case MONO_TYPE_I4:
379         case MONO_TYPE_U4:
380         case MONO_TYPE_I:
381         case MONO_TYPE_U:
382                 return TRUE;
383         case MONO_TYPE_OBJECT:
384         case MONO_TYPE_STRING:
385         case MONO_TYPE_CLASS:
386         case MONO_TYPE_SZARRAY:
387         case MONO_TYPE_ARRAY:
388                 return FALSE;
389         case MONO_TYPE_VALUETYPE:
390                 if (t->data.klass->enumtype)
391                         return is_regsize_var (t->data.klass->enum_basetype);
392                 return FALSE;
393         }
394         return FALSE;
395 }
396
397 GList *
398 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
399 {
400         GList *vars = NULL;
401         int i;
402
403         for (i = 0; i < cfg->num_varinfo; i++) {
404                 MonoInst *ins = cfg->varinfo [i];
405                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
406
407                 /* unused vars */
408                 if (vmv->range.first_use.abs_pos > vmv->range.last_use.abs_pos)
409                         continue;
410
411                 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
412                         continue;
413
414                 /* we can only allocate 32 bit values */
415                 if (is_regsize_var (ins->inst_vtype)) {
416                         g_assert (MONO_VARINFO (cfg, i)->reg == -1);
417                         g_assert (i == vmv->idx);
418                         vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
419                 }
420         }
421
422         return vars;
423 }
424
425 GList *
426 mono_arch_get_global_int_regs (MonoCompile *cfg)
427 {
428         GList *regs = NULL;
429         int i, top = 32;
430         if (cfg->flags & MONO_CFG_HAS_ALLOCA)
431                 top = 31;
432         for (i = 13; i < top; ++i)
433                 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
434
435         return regs;
436 }
437
438 // code from ppc/tramp.c, try to keep in sync
439 #define MIN_CACHE_LINE 8
440
441 void
442 mono_arch_flush_icache (guint8 *code, gint size)
443 {
444         guint i;
445         guint8 *p;
446
447         p = code;
448         for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
449                 asm ("dcbst 0,%0;" : : "r"(p) : "memory");
450         }
451         asm ("sync");
452         p = code;
453         for (i = 0; i < size; i += MIN_CACHE_LINE, p += MIN_CACHE_LINE) {
454                 asm ("icbi 0,%0; sync;" : : "r"(p) : "memory");
455         }
456         asm ("sync");
457         asm ("isync");
458 }
459
460 #define NOT_IMPLEMENTED(x) \
461                 g_error ("FIXME: %s is not yet implemented. (trampoline)", x);
462
463 #define PROLOG_INS 8
464 #define CALL_INS   2
465 #define EPILOG_INS 6
466 #define FLOAT_REGS 8
467 #define GENERAL_REGS 8
468 #ifdef __APPLE__
469 #define MINIMAL_STACK_SIZE 10
470 #define ALWAYS_ON_STACK(s) s
471 #define FP_ALSO_IN_REG(s) s
472 #define RET_ADDR_OFFSET 8
473 #define STACK_PARAM_OFFSET 24
474 #else
475 #define MINIMAL_STACK_SIZE 5
476 #define ALWAYS_ON_STACK(s)
477 #define FP_ALSO_IN_REG(s) s
478 #define ALIGN_DOUBLES
479 #define RET_ADDR_OFFSET 4
480 #define STACK_PARAM_OFFSET 8
481 #endif
482
483 typedef struct {
484         gint16 offset;
485         gint8  reg;
486         gint8  regtype; /* 0 general, 1 basereg, 2 floating point register */
487 } ArgInfo;
488
489 typedef struct {
490         int nargs;
491         guint32 stack_usage;
492         ArgInfo ret;
493         ArgInfo args [1];
494 } CallInfo;
495
496 #define DEBUG(a)
497
498 static void inline
499 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
500 {
501         if (simple) {
502                 if (*gr >= 3 + GENERAL_REGS) {
503                         ainfo->offset = *stack_size;
504                         ainfo->reg = ppc_sp; /* in the caller */
505                         ainfo->regtype = 1;
506                         *stack_size += 4;
507                 } else {
508                         ALWAYS_ON_STACK (*stack_size += 4);
509                         ainfo->reg = *gr;
510                 }
511         } else {
512                 if (*gr >= 3 + GENERAL_REGS - 1) {
513                         ainfo->offset = *stack_size;
514                         ainfo->reg = ppc_sp; /* in the caller */
515                         ainfo->regtype = 1;
516                         *stack_size += 8;
517 #ifdef ALIGN_DOUBLES
518                         *stack_size += (*stack_size % 8);
519 #endif
520                 } else {
521                         ALWAYS_ON_STACK (*stack_size += 8);
522                         ainfo->reg = *gr;
523                 }
524 #ifdef ALIGN_DOUBLES
525                 if ((*gr) & 1)
526                         (*gr) ++;
527 #endif
528                 (*gr) ++;
529         }
530         (*gr) ++;
531 }
532
533 static CallInfo*
534 calculate_sizes (MonoMethodSignature *sig, gboolean is_pinvoke)
535 {
536         guint i, fr, gr;
537         int n = sig->hasthis + sig->param_count;
538         guint32 simpletype;
539         guint32 stack_size = 0;
540         CallInfo *cinfo = g_malloc0 (sizeof (CallInfo) + sizeof (ArgInfo) * n);
541
542         fr = 1;
543         gr = 3;
544
545         /* FIXME: handle returning a struct */
546
547         n = 0;
548         if (sig->hasthis) {
549                 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
550                 n++;
551         }
552         DEBUG(printf("params: %d\n", sig->param_count));
553         for (i = 0; i < sig->param_count; ++i) {
554                 DEBUG(printf("param %d: ", i));
555                 if (sig->params [i]->byref) {
556                         DEBUG(printf("byref\n"));
557                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
558                         n++;
559                         continue;
560                 }
561                 simpletype = sig->params [i]->type;
562         enum_calc_size:
563                 switch (simpletype) {
564                 case MONO_TYPE_BOOLEAN:
565                 case MONO_TYPE_CHAR:
566                 case MONO_TYPE_I1:
567                 case MONO_TYPE_U1:
568                 case MONO_TYPE_I2:
569                 case MONO_TYPE_U2:
570                 case MONO_TYPE_I4:
571                 case MONO_TYPE_U4:
572                 case MONO_TYPE_I:
573                 case MONO_TYPE_U:
574                 case MONO_TYPE_PTR:
575                 case MONO_TYPE_CLASS:
576                 case MONO_TYPE_OBJECT:
577                 case MONO_TYPE_STRING:
578                 case MONO_TYPE_SZARRAY:
579                 case MONO_TYPE_ARRAY:
580                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
581                         n++;
582                         break;
583                 case MONO_TYPE_VALUETYPE: {
584                         gint size;
585                         if (sig->params [i]->data.klass->enumtype) {
586                                 simpletype = sig->params [i]->data.klass->enum_basetype->type;
587                                 goto enum_calc_size;
588                         }
589 #if 0
590                         size = mono_class_value_size (sig->params [i]->data.klass, NULL);
591                         if (size != 4) {
592                                 DEBUG(printf ("copy %d bytes struct on stack\n",
593                                               mono_class_value_size (sig->params [i]->data.klass, NULL)));
594                                 *stack_size += (size + 3) & (~3);
595                                 if (gr > 3 + GENERAL_REGS) {
596                                         *stack_size += 4;
597                                 }
598                         } else {
599                                 DEBUG(printf ("load %d bytes struct\n",
600                                               mono_class_value_size (sig->params [i]->data.klass, NULL)));
601                                 add_general (&gr, stack_size, code_size, TRUE);
602                         }
603 #endif
604                         g_assert_not_reached ();
605                         break;
606                 }
607                 case MONO_TYPE_U8:
608                 case MONO_TYPE_I8:
609                         add_general (&gr, &stack_size, cinfo->args + n, FALSE);
610                         n++;
611                         break;
612                 case MONO_TYPE_R4:
613                         if (fr < 7) {
614                                 fr ++;
615                                 FP_ALSO_IN_REG (gr ++);
616                                 ALWAYS_ON_STACK (stack_size += 4);
617                         } else {
618                                 NOT_IMPLEMENTED ("R4 arg");
619                         }
620                         n++;
621                         break;
622                 case MONO_TYPE_R8:
623                         if (fr < 7) {
624                                 fr ++;
625                                 FP_ALSO_IN_REG (gr += 2);
626                                 ALWAYS_ON_STACK (stack_size += 8);
627                         } else {
628                                 NOT_IMPLEMENTED ("R8 arg");
629                         }
630                         n++;
631                         break;
632                 default:
633                         g_error ("Can't trampoline 0x%x", sig->params [i]->type);
634                 }
635         }
636
637         {
638                 simpletype = sig->ret->type;
639 enum_retvalue:
640                 switch (simpletype) {
641                 case MONO_TYPE_BOOLEAN:
642                 case MONO_TYPE_I1:
643                 case MONO_TYPE_U1:
644                 case MONO_TYPE_I2:
645                 case MONO_TYPE_U2:
646                 case MONO_TYPE_CHAR:
647                 case MONO_TYPE_I4:
648                 case MONO_TYPE_U4:
649                 case MONO_TYPE_I:
650                 case MONO_TYPE_U:
651                 case MONO_TYPE_CLASS:
652                 case MONO_TYPE_OBJECT:
653                 case MONO_TYPE_SZARRAY:
654                 case MONO_TYPE_ARRAY:
655                 case MONO_TYPE_STRING:
656                         cinfo->ret.reg = ppc_r3;
657                         break;
658                 case MONO_TYPE_U8:
659                 case MONO_TYPE_I8:
660                         cinfo->ret.reg = ppc_r3;
661                         break;
662                 case MONO_TYPE_R4:
663                 case MONO_TYPE_R8:
664                         cinfo->ret.reg = ppc_f1;
665                         cinfo->ret.regtype = 2;
666                         break;
667                 case MONO_TYPE_VALUETYPE:
668                         if (sig->ret->data.klass->enumtype) {
669                                 simpletype = sig->ret->data.klass->enum_basetype->type;
670                                 goto enum_retvalue;
671                         }
672                         break;
673                 case MONO_TYPE_VOID:
674                         break;
675                 default:
676                         g_error ("Can't handle as return value 0x%x", sig->ret->type);
677                 }
678         }
679
680         /* align stack size to 16 */
681         DEBUG (printf ("      stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
682         stack_size = (stack_size + 15) & ~15;
683
684         cinfo->stack_usage = stack_size;
685         return cinfo;
686 }
687
688
689 /*
690  * Set var information according to the calling convention. ppc version.
691  * The locals var stuff should most likely be split in another method.
692  */
693 void
694 mono_arch_allocate_vars (MonoCompile *m)
695 {
696         MonoMethodSignature *sig;
697         MonoMethodHeader *header;
698         MonoInst *inst;
699         int i, offset, size, align, curinst;
700         int frame_reg = ppc_sp;
701  
702         if (m->flags & MONO_CFG_HAS_ALLOCA)
703                 frame_reg = ppc_r31;
704         m->frame_reg = frame_reg;
705
706         header = ((MonoMethodNormal *)m->method)->header;
707
708         sig = m->method->signature;
709         
710         offset = 0;
711         curinst = 0;
712         if (MONO_TYPE_ISSTRUCT (sig->ret)) {
713                 m->ret->opcode = OP_REGVAR;
714                 m->ret->inst_c0 = ppc_r3;
715         } else {
716                 /* FIXME: handle long and FP values */
717                 switch (sig->ret->type) {
718                 case MONO_TYPE_VOID:
719                         break;
720                 default:
721                         m->ret->opcode = OP_REGVAR;
722                         m->ret->inst_c0 = ppc_r3;
723                         break;
724                 }
725         }
726         /* local vars are at a positive offset from the stack pointer */
727         /* 
728          * also note that if the function uses alloca, we use ppc_r31
729          * to point at the local variables.
730          */
731         offset = 24; /* linkage area */
732         /* align the offset to 16 bytes: not sure this is needed here  */
733         //offset += 16 - 1;
734         //offset &= ~(16 - 1);
735
736         /* add parameter area size for called functions */
737         offset += m->param_area;
738         offset += 16 - 1;
739         offset &= ~(16 - 1);
740
741         /* FIXME: check how to handle this stuff... reserve space to save LMF and caller saved registers */
742         offset += sizeof (MonoLMF);
743
744 #if 0
745         /* this stuff should not be needed on ppc and the new jit,
746          * because a call on ppc to the handlers doesn't change the 
747          * stack pointer and the jist doesn't manipulate the stack pointer
748          * for operations involving valuetypes.
749          */
750         /* reserve space to store the esp */
751         offset += sizeof (gpointer);
752
753         /* this is a global constant */
754         mono_exc_esp_offset = offset;
755 #endif
756
757         curinst = m->locals_start;
758         for (i = curinst; i < m->num_varinfo; ++i) {
759                 inst = m->varinfo [i];
760                 if (inst->opcode == OP_REGVAR)
761                         continue;
762
763                 /* inst->unused indicates native sized value types, this is used by the
764                 * pinvoke wrappers when they call functions returning structure */
765                 if (inst->unused && MONO_TYPE_ISSTRUCT (inst->inst_vtype))
766                         size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
767                 else
768                         size = mono_type_size (inst->inst_vtype, &align);
769
770                 offset += align - 1;
771                 offset &= ~(align - 1);
772                 inst->inst_offset = offset;
773                 inst->opcode = OP_REGOFFSET;
774                 inst->inst_basereg = frame_reg;
775                 offset += size;
776                 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
777         }
778
779         curinst = 0;
780         if (sig->hasthis) {
781                 inst = m->varinfo [curinst];
782                 if (inst->opcode != OP_REGVAR) {
783                         inst->opcode = OP_REGOFFSET;
784                         inst->inst_basereg = frame_reg;
785                         offset += sizeof (gpointer) - 1;
786                         offset &= ~(sizeof (gpointer) - 1);
787                         inst->inst_offset = offset;
788                         offset += sizeof (gpointer);
789                 }
790                 curinst++;
791         }
792
793         for (i = 0; i < sig->param_count; ++i) {
794                 inst = m->varinfo [curinst];
795                 if (inst->opcode != OP_REGVAR) {
796                         inst->opcode = OP_REGOFFSET;
797                         inst->inst_basereg = frame_reg;
798                         size = mono_type_size (sig->params [i], &align);
799                         offset += align - 1;
800                         offset &= ~(align - 1);
801                         inst->inst_offset = offset;
802                         offset += size;
803                 }
804                 curinst++;
805         }
806
807         /* align the offset to 16 bytes */
808         offset += 16 - 1;
809         offset &= ~(16 - 1);
810
811         /* change sign? */
812         m->stack_offset = offset;
813
814 }
815
816 /* Fixme: we need an alignment solution for enter_method and mono_arch_call_opcode,
817  * currently alignment in mono_arch_call_opcode is computed without arch_get_argument_info 
818  */
819
820 /* 
821  * take the arguments and generate the arch-specific
822  * instructions to properly call the function in call.
823  * This includes pushing, moving arguments to the right register
824  * etc.
825  * Issue: who does the spilling if needed, and when?
826  */
827 MonoCallInst*
828 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
829         MonoInst *arg, *in;
830         MonoMethodSignature *sig;
831         int i, n, type;
832         MonoType *ptype;
833         CallInfo *cinfo;
834         ArgInfo *ainfo;
835
836         sig = call->signature;
837         n = sig->param_count + sig->hasthis;
838         
839         cinfo = calculate_sizes (sig, sig->pinvoke);
840
841         for (i = 0; i < n; ++i) {
842                 ainfo = cinfo->args + i;
843                 if (is_virtual && i == 0) {
844                         /* the argument will be attached to the call instrucion */
845                         in = call->args [i];
846                 } else {
847                         MONO_INST_NEW (cfg, arg, OP_OUTARG);
848                         in = call->args [i];
849                         arg->cil_code = in->cil_code;
850                         arg->inst_left = in;
851                         arg->type = in->type;
852                         /* prepend, we'll need to reverse them later */
853                         arg->next = call->out_args;
854                         call->out_args = arg;
855                         if (ainfo->regtype == 0) {
856                                 arg->unused = ainfo->reg;
857                                 call->used_iregs |= 1 << ainfo->reg;
858                         } else if (ainfo->regtype == 1) {
859                                 g_assert_not_reached ();
860                         } else if (ainfo->regtype == 2) {
861                                 arg->opcode = OP_OUTARG_R8;
862                                 arg->unused = ainfo->reg;
863                                 call->used_fregs |= 1 << ainfo->reg;
864                         } else {
865                                 g_assert_not_reached ();
866                         }
867                 }
868         }
869         /*
870          * Reverse the call->out_args list.
871          */
872         {
873                 MonoInst *prev = NULL, *list = call->out_args, *next;
874                 while (list) {
875                         next = list->next;
876                         list->next = prev;
877                         prev = list;
878                         list = next;
879                 }
880         }
881         call->stack_usage = cinfo->stack_usage;
882         cfg->param_area = MAX (cfg->param_area, cinfo->stack_usage);
883         cfg->flags |= MONO_CFG_HAS_CALLS;
884         /* 
885          * should set more info in call, such as the stack space
886          * used by the args that needs to be added back to esp
887          */
888
889         g_free (cinfo);
890         return call;
891 }
892
893 /*
894  * Allow tracing to work with this interface (with an optional argument)
895  */
896
897 /*
898  * This may be needed on some archs or for debugging support.
899  */
900 void
901 mono_arch_instrument_mem_needs (MonoMethod *method, int *stack, int *code)
902 {
903         /* no stack room needed now (may be needed for FASTCALL-trace support) */
904         *stack = 0;
905         /* split prolog-epilog requirements? */
906         *code = 50; /* max bytes needed: check this number */
907 }
908
909 void*
910 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
911 {
912         guchar *code = p;
913 #if 0
914         /* if some args are passed in registers, we need to save them here */
915         x86_push_reg (code, X86_EBP);
916         x86_push_imm (code, cfg->method);
917         mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
918         x86_call_code (code, 0);
919         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
920 #endif
921         return code;
922 }
923
924 enum {
925         SAVE_NONE,
926         SAVE_STRUCT,
927         SAVE_ONE,
928         SAVE_TWO,
929         SAVE_FP
930 };
931
932 void*
933 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
934 {
935         guchar *code = p;
936         int arg_size = 0, save_mode = SAVE_NONE;
937         MonoMethod *method = cfg->method;
938         int rtype = method->signature->ret->type;
939         
940 handle_enum:
941         switch (rtype) {
942         case MONO_TYPE_VOID:
943                 /* special case string .ctor icall */
944                 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
945                         save_mode = SAVE_ONE;
946                 else
947                         save_mode = SAVE_NONE;
948                 break;
949         case MONO_TYPE_I8:
950         case MONO_TYPE_U8:
951                 save_mode = SAVE_TWO;
952                 break;
953         case MONO_TYPE_R4:
954         case MONO_TYPE_R8:
955                 save_mode = SAVE_FP;
956                 break;
957         case MONO_TYPE_VALUETYPE:
958                 if (method->signature->ret->data.klass->enumtype) {
959                         rtype = method->signature->ret->data.klass->enum_basetype->type;
960                         goto handle_enum;
961                 }
962                 save_mode = SAVE_STRUCT;
963                 break;
964         default:
965                 save_mode = SAVE_ONE;
966                 break;
967         }
968
969         switch (save_mode) {
970         case SAVE_TWO:
971                 //x86_push_reg (code, X86_EDX);
972                 //x86_push_reg (code, X86_EAX);
973                 if (enable_arguments) {
974                         //x86_push_reg (code, X86_EDX);
975                         //x86_push_reg (code, X86_EAX);
976                         arg_size = 8;
977                 }
978                 break;
979         case SAVE_ONE:
980                 //x86_push_reg (code, X86_EAX);
981                 if (enable_arguments) {
982                         //x86_push_reg (code, X86_EAX);
983                         arg_size = 4;
984                 }
985                 break;
986         case SAVE_FP:
987                 //x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
988                 ///x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
989                 if (enable_arguments) {
990                         //x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
991                         //x86_fst_membase (code, X86_ESP, 0, TRUE, TRUE);
992                         arg_size = 8;
993                 }
994                 break;
995         case SAVE_STRUCT:
996                 if (enable_arguments) {
997                         //x86_push_membase (code, X86_EBP, 8);
998                         arg_size = 4;
999                 }
1000                 break;
1001         case SAVE_NONE:
1002         default:
1003                 break;
1004         }
1005
1006         /*x86_push_imm (code, method);
1007         mono_add_patch_info (cfg, code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
1008         x86_call_code (code, 0);
1009         x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
1010         */
1011
1012         switch (save_mode) {
1013         case SAVE_TWO:
1014                 //x86_pop_reg (code, X86_EAX);
1015                 //x86_pop_reg (code, X86_EDX);
1016                 break;
1017         case SAVE_ONE:
1018                 //x86_pop_reg (code, X86_EAX);
1019                 break;
1020         case SAVE_FP:
1021                 //x86_fld_membase (code, X86_ESP, 0, TRUE);
1022                 //x86_alu_reg_imm (code, X86_ADD, X86_ESP, 8);
1023                 break;
1024         case SAVE_NONE:
1025         default:
1026                 break;
1027         }
1028
1029         return code;
1030 }
1031
1032 #define EMIT_COND_BRANCH(ins,cond) \
1033 if (ins->flags & MONO_INST_BRLABEL) { \
1034         if (ins->inst_i0->inst_c0) { \
1035                 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_i0->inst_c0) & 0xffff);      \
1036         } else { \
1037                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1038                 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0);       \
1039         } \
1040 } else { \
1041         if (0 && ins->inst_true_bb->native_offset) { \
1042                 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffff); \
1043         } else { \
1044                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1045                 ppc_bc (code, branch_b0_table [cond], branch_b1_table [cond], 0);       \
1046         } \
1047 }
1048
1049 /* emit an exception if condition is fail */
1050 #define EMIT_COND_SYSTEM_EXCEPTION(cond,signed,exc_name)            \
1051         do {                                                        \
1052                 mono_add_patch_info (cfg, code - cfg->native_code,   \
1053                                     MONO_PATCH_INFO_EXC, exc_name);  \
1054                 x86_branch32 (code, cond, 0, signed);               \
1055         } while (0); 
1056
1057 #define EMIT_FPCOMPARE(code) do { \
1058         x86_fcompp (code); \
1059         x86_fnstsw (code); \
1060         x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4500); \
1061 } while (0); 
1062
1063 static void
1064 peephole_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1065 {
1066         MonoInst *ins, *last_ins = NULL;
1067         ins = bb->code;
1068
1069         while (ins) {
1070
1071                 switch (ins->opcode) {
1072                 case OP_MUL_IMM: 
1073                         /* remove unnecessary multiplication with 1 */
1074                         if (ins->inst_imm == 1) {
1075                                 if (ins->dreg != ins->sreg1) {
1076                                         ins->opcode = OP_MOVE;
1077                                 } else {
1078                                         last_ins->next = ins->next;                             
1079                                         ins = ins->next;                                
1080                                         continue;
1081                                 }
1082                         }
1083                         break;
1084                 case OP_LOAD_MEMBASE:
1085                 case OP_LOADI4_MEMBASE:
1086                         /* 
1087                          * OP_STORE_MEMBASE_REG reg, offset(basereg) 
1088                          * OP_LOAD_MEMBASE offset(basereg), reg
1089                          */
1090                         if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG 
1091                                          || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1092                             ins->inst_basereg == last_ins->inst_destbasereg &&
1093                             ins->inst_offset == last_ins->inst_offset) {
1094                                 if (ins->dreg == last_ins->sreg1) {
1095                                         last_ins->next = ins->next;                             
1096                                         ins = ins->next;                                
1097                                         continue;
1098                                 } else {
1099                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1100                                         ins->opcode = OP_MOVE;
1101                                         ins->sreg1 = last_ins->sreg1;
1102                                 }
1103
1104                         /* 
1105                          * Note: reg1 must be different from the basereg in the second load
1106                          * OP_LOAD_MEMBASE offset(basereg), reg1
1107                          * OP_LOAD_MEMBASE offset(basereg), reg2
1108                          * -->
1109                          * OP_LOAD_MEMBASE offset(basereg), reg1
1110                          * OP_MOVE reg1, reg2
1111                          */
1112                         } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1113                                            || last_ins->opcode == OP_LOAD_MEMBASE) &&
1114                               ins->inst_basereg != last_ins->dreg &&
1115                               ins->inst_basereg == last_ins->inst_basereg &&
1116                               ins->inst_offset == last_ins->inst_offset) {
1117
1118                                 if (ins->dreg == last_ins->dreg) {
1119                                         last_ins->next = ins->next;                             
1120                                         ins = ins->next;                                
1121                                         continue;
1122                                 } else {
1123                                         ins->opcode = OP_MOVE;
1124                                         ins->sreg1 = last_ins->dreg;
1125                                 }
1126
1127                                 //g_assert_not_reached ();
1128
1129 #if 0
1130                         /* 
1131                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
1132                          * OP_LOAD_MEMBASE offset(basereg), reg
1133                          * -->
1134                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
1135                          * OP_ICONST reg, imm
1136                          */
1137                         } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1138                                                 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1139                                    ins->inst_basereg == last_ins->inst_destbasereg &&
1140                                    ins->inst_offset == last_ins->inst_offset) {
1141                                 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1142                                 ins->opcode = OP_ICONST;
1143                                 ins->inst_c0 = last_ins->inst_imm;
1144                                 g_assert_not_reached (); // check this rule
1145 #endif
1146                         }
1147                         break;
1148                 case OP_LOADU1_MEMBASE:
1149                 case OP_LOADI1_MEMBASE:
1150                         if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1151                                         ins->inst_basereg == last_ins->inst_destbasereg &&
1152                                         ins->inst_offset == last_ins->inst_offset) {
1153                                 if (ins->dreg == last_ins->sreg1) {
1154                                         last_ins->next = ins->next;                             
1155                                         ins = ins->next;                                
1156                                         continue;
1157                                 } else {
1158                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1159                                         ins->opcode = OP_MOVE;
1160                                         ins->sreg1 = last_ins->sreg1;
1161                                 }
1162                         }
1163                         break;
1164                 case OP_LOADU2_MEMBASE:
1165                 case OP_LOADI2_MEMBASE:
1166                         if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1167                                         ins->inst_basereg == last_ins->inst_destbasereg &&
1168                                         ins->inst_offset == last_ins->inst_offset) {
1169                                 if (ins->dreg == last_ins->sreg1) {
1170                                         last_ins->next = ins->next;                             
1171                                         ins = ins->next;                                
1172                                         continue;
1173                                 } else {
1174                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1175                                         ins->opcode = OP_MOVE;
1176                                         ins->sreg1 = last_ins->sreg1;
1177                                 }
1178                         }
1179                         break;
1180                 case CEE_CONV_I4:
1181                 case CEE_CONV_U4:
1182                 case OP_MOVE:
1183                         /* 
1184                          * OP_MOVE reg, reg 
1185                          */
1186                         if (ins->dreg == ins->sreg1) {
1187                                 if (last_ins)
1188                                         last_ins->next = ins->next;                             
1189                                 ins = ins->next;
1190                                 continue;
1191                         }
1192                         /* 
1193                          * OP_MOVE sreg, dreg 
1194                          * OP_MOVE dreg, sreg
1195                          */
1196                         if (last_ins && last_ins->opcode == OP_MOVE &&
1197                             ins->sreg1 == last_ins->dreg &&
1198                             ins->dreg == last_ins->sreg1) {
1199                                 last_ins->next = ins->next;                             
1200                                 ins = ins->next;                                
1201                                 continue;
1202                         }
1203                         break;
1204                 }
1205                 last_ins = ins;
1206                 ins = ins->next;
1207         }
1208         bb->last_ins = last_ins;
1209 }
1210
1211 /* 
1212  * the branch_b0_table should maintain the order of these
1213  * opcodes.
1214 case CEE_BEQ:
1215 case CEE_BGE:
1216 case CEE_BGT:
1217 case CEE_BLE:
1218 case CEE_BLT:
1219 case CEE_BNE_UN:
1220 case CEE_BGE_UN:
1221 case CEE_BGT_UN:
1222 case CEE_BLE_UN:
1223 case CEE_BLT_UN:
1224  */
1225 static const guchar 
1226 branch_b0_table [] = {
1227         PPC_BR_TRUE, 
1228         PPC_BR_FALSE, 
1229         PPC_BR_TRUE, 
1230         PPC_BR_FALSE, 
1231         PPC_BR_TRUE, 
1232         
1233         PPC_BR_FALSE, 
1234         PPC_BR_FALSE, 
1235         PPC_BR_TRUE, 
1236         PPC_BR_FALSE,
1237         PPC_BR_TRUE
1238 };
1239
1240 static const guchar 
1241 branch_b1_table [] = {
1242         PPC_BR_EQ, 
1243         PPC_BR_LT, 
1244         PPC_BR_GT, 
1245         PPC_BR_GT,
1246         PPC_BR_LT, 
1247         
1248         PPC_BR_EQ, 
1249         PPC_BR_LT, 
1250         PPC_BR_GT, 
1251         PPC_BR_GT,
1252         PPC_BR_LT 
1253 };
1254
1255 #undef DEBUG
1256 #define DEBUG(a) if (cfg->verbose_level > 1) a
1257 //#define DEBUG(a)
1258 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1259
1260 typedef struct {
1261         int born_in;
1262         int killed_in;
1263         int last_use;
1264         int prev_use;
1265 } RegTrack;
1266
1267 static const char*const * ins_spec = ppcg4;
1268
1269 static void
1270 print_ins (int i, MonoInst *ins)
1271 {
1272         const char *spec = ins_spec [ins->opcode];
1273         g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1274         if (spec [MONO_INST_DEST]) {
1275                 if (ins->dreg >= MONO_MAX_IREGS)
1276                         g_print (" R%d <-", ins->dreg);
1277                 else
1278                         g_print (" %s <-", mono_arch_regname (ins->dreg));
1279         }
1280         if (spec [MONO_INST_SRC1]) {
1281                 if (ins->sreg1 >= MONO_MAX_IREGS)
1282                         g_print (" R%d", ins->sreg1);
1283                 else
1284                         g_print (" %s", mono_arch_regname (ins->sreg1));
1285         }
1286         if (spec [MONO_INST_SRC2]) {
1287                 if (ins->sreg2 >= MONO_MAX_IREGS)
1288                         g_print (" R%d", ins->sreg2);
1289                 else
1290                         g_print (" %s", mono_arch_regname (ins->sreg2));
1291         }
1292         if (spec [MONO_INST_CLOB])
1293                 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1294         g_print ("\n");
1295 }
1296
1297 static void
1298 print_regtrack (RegTrack *t, int num)
1299 {
1300         int i;
1301         char buf [32];
1302         const char *r;
1303         
1304         for (i = 0; i < num; ++i) {
1305                 if (!t [i].born_in)
1306                         continue;
1307                 if (i >= MONO_MAX_IREGS) {
1308                         g_snprintf (buf, sizeof(buf), "R%d", i);
1309                         r = buf;
1310                 } else
1311                         r = mono_arch_regname (i);
1312                 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1313         }
1314 }
1315
1316 typedef struct InstList InstList;
1317
1318 struct InstList {
1319         InstList *prev;
1320         InstList *next;
1321         MonoInst *data;
1322 };
1323
1324 static inline InstList*
1325 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1326 {
1327         InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1328         item->data = data;
1329         item->prev = NULL;
1330         item->next = list;
1331         if (list)
1332                 list->prev = item;
1333         return item;
1334 }
1335
1336 /*
1337  * Force the spilling of the variable in the symbolic register 'reg'.
1338  */
1339 static int
1340 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1341 {
1342         MonoInst *load;
1343         int i, sel, spill;
1344         
1345         sel = cfg->rs->iassign [reg];
1346         /*i = cfg->rs->isymbolic [sel];
1347         g_assert (i == reg);*/
1348         i = reg;
1349         spill = ++cfg->spill_count;
1350         cfg->rs->iassign [i] = -spill - 1;
1351         mono_regstate_free_int (cfg->rs, sel);
1352         /* we need to create a spill var and insert a load to sel after the current instruction */
1353         MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1354         load->dreg = sel;
1355         load->inst_basereg = cfg->frame_reg;
1356         load->inst_offset = mono_spillvar_offset (cfg, spill);
1357         if (item->prev) {
1358                 while (ins->next != item->prev->data)
1359                         ins = ins->next;
1360         }
1361         load->next = ins->next;
1362         ins->next = load;
1363         DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1364         i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1365         g_assert (i == sel);
1366
1367         return sel;
1368 }
1369
1370 static int
1371 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1372 {
1373         MonoInst *load;
1374         int i, sel, spill;
1375
1376         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));
1377         /* exclude the registers in the current instruction */
1378         if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1379                 if (ins->sreg1 >= MONO_MAX_IREGS)
1380                         regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1381                 else
1382                         regmask &= ~ (1 << ins->sreg1);
1383                 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1384         }
1385         if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1386                 if (ins->sreg2 >= MONO_MAX_IREGS)
1387                         regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1388                 else
1389                         regmask &= ~ (1 << ins->sreg2);
1390                 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1391         }
1392         if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1393                 regmask &= ~ (1 << ins->dreg);
1394                 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1395         }
1396
1397         DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1398         g_assert (regmask); /* need at least a register we can free */
1399         sel = -1;
1400         /* we should track prev_use and spill the register that's farther */
1401         for (i = 0; i < MONO_MAX_IREGS; ++i) {
1402                 if (regmask & (1 << i)) {
1403                         sel = i;
1404                         DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1405                         break;
1406                 }
1407         }
1408         i = cfg->rs->isymbolic [sel];
1409         spill = ++cfg->spill_count;
1410         cfg->rs->iassign [i] = -spill - 1;
1411         mono_regstate_free_int (cfg->rs, sel);
1412         /* we need to create a spill var and insert a load to sel after the current instruction */
1413         MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1414         load->dreg = sel;
1415         load->inst_basereg = cfg->frame_reg;
1416         load->inst_offset = mono_spillvar_offset (cfg, spill);
1417         if (item->prev) {
1418                 while (ins->next != item->prev->data)
1419                         ins = ins->next;
1420         }
1421         load->next = ins->next;
1422         ins->next = load;
1423         DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1424         i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1425         g_assert (i == sel);
1426         
1427         return sel;
1428 }
1429
1430 static MonoInst*
1431 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1432 {
1433         MonoInst *copy;
1434         MONO_INST_NEW (cfg, copy, OP_MOVE);
1435         copy->dreg = dest;
1436         copy->sreg1 = src;
1437         if (ins) {
1438                 copy->next = ins->next;
1439                 ins->next = copy;
1440         }
1441         DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1442         return copy;
1443 }
1444
1445 static MonoInst*
1446 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1447 {
1448         MonoInst *store;
1449         MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1450         store->sreg1 = reg;
1451         store->inst_destbasereg = ppc_r1;
1452         store->inst_offset = mono_spillvar_offset (cfg, spill);
1453         if (ins) {
1454                 store->next = ins->next;
1455                 ins->next = store;
1456         }
1457         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)));
1458         return store;
1459 }
1460
1461 static void
1462 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1463 {
1464         MonoInst *prev;
1465         g_assert (item->next);
1466         prev = item->next->data;
1467
1468         while (prev->next != ins)
1469                 prev = prev->next;
1470         to_insert->next = ins;
1471         prev->next = to_insert;
1472         /* 
1473          * needed otherwise in the next instruction we can add an ins to the 
1474          * end and that would get past this instruction.
1475          */
1476         item->data = to_insert; 
1477 }
1478
1479 static int
1480 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1481 {
1482         int val = cfg->rs->iassign [sym_reg];
1483         if (val < 0) {
1484                 int spill = 0;
1485                 if (val < -1) {
1486                         /* the register gets spilled after this inst */
1487                         spill = -val -1;
1488                 }
1489                 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1490                 if (val < 0)
1491                         val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1492                 cfg->rs->iassign [sym_reg] = val;
1493                 /* add option to store before the instruction for src registers */
1494                 if (spill)
1495                         create_spilled_store (cfg, spill, val, sym_reg, ins);
1496         }
1497         cfg->rs->isymbolic [val] = sym_reg;
1498         return val;
1499 }
1500
1501 /* use ppc_r3-ppc_310 as temp registers */
1502 #define PPC_CALLER_REGS (0xf<<3)
1503
1504 /*
1505  * Local register allocation.
1506  * We first scan the list of instructions and we save the liveness info of
1507  * each register (when the register is first used, when it's value is set etc.).
1508  * We also reverse the list of instructions (in the InstList list) because assigning
1509  * registers backwards allows for more tricks to be used.
1510  */
1511 void
1512 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1513 {
1514         MonoInst *ins;
1515         MonoRegState *rs = cfg->rs;
1516         int i, val, fpcount;
1517         RegTrack *reginfo, *reginfof;
1518         RegTrack *reginfo1, *reginfo2, *reginfod;
1519         InstList *tmp, *reversed = NULL;
1520         const char *spec;
1521         guint32 src1_mask, src2_mask, dest_mask;
1522
1523         if (!bb->code)
1524                 return;
1525         rs->next_vireg = bb->max_ireg;
1526         rs->next_vfreg = bb->max_freg;
1527         mono_regstate_assign (rs);
1528         reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1529         reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1530         rs->ifree_mask = PPC_CALLER_REGS;
1531
1532         ins = bb->code;
1533         i = 1;
1534         fpcount = 0; /* FIXME: track fp stack utilization */
1535         DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1536         /* forward pass on the instructions to collect register liveness info */
1537         while (ins) {
1538                 spec = ins_spec [ins->opcode];
1539                 DEBUG (print_ins (i, ins));
1540                 if (spec [MONO_INST_SRC1]) {
1541                         if (spec [MONO_INST_SRC1] == 'f')
1542                                 reginfo1 = reginfof;
1543                         else
1544                                 reginfo1 = reginfo;
1545                         reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1546                         reginfo1 [ins->sreg1].last_use = i;
1547                 } else {
1548                         ins->sreg1 = -1;
1549                 }
1550                 if (spec [MONO_INST_SRC2]) {
1551                         if (spec [MONO_INST_SRC2] == 'f')
1552                                 reginfo2 = reginfof;
1553                         else
1554                                 reginfo2 = reginfo;
1555                         reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1556                         reginfo2 [ins->sreg2].last_use = i;
1557                 } else {
1558                         ins->sreg2 = -1;
1559                 }
1560                 if (spec [MONO_INST_DEST]) {
1561                         if (spec [MONO_INST_DEST] == 'f')
1562                                 reginfod = reginfof;
1563                         else
1564                                 reginfod = reginfo;
1565                         if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1566                                 reginfod [ins->dreg].killed_in = i;
1567                         reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1568                         reginfod [ins->dreg].last_use = i;
1569                         if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1570                                 reginfod [ins->dreg].born_in = i;
1571                         if (spec [MONO_INST_DEST] == 'l') {
1572                                 /* result in eax:edx, the virtual register is allocated sequentially */
1573                                 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1574                                 reginfod [ins->dreg + 1].last_use = i;
1575                                 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1576                                         reginfod [ins->dreg + 1].born_in = i;
1577                         }
1578                 } else {
1579                         ins->dreg = -1;
1580                 }
1581                 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1582                 ++i;
1583                 ins = ins->next;
1584         }
1585
1586         DEBUG (print_regtrack (reginfo, rs->next_vireg));
1587         DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1588         tmp = reversed;
1589         while (tmp) {
1590                 int prev_dreg, prev_sreg1, prev_sreg2;
1591                 dest_mask = src1_mask = src2_mask = PPC_CALLER_REGS;
1592                 --i;
1593                 ins = tmp->data;
1594                 spec = ins_spec [ins->opcode];
1595                 DEBUG (g_print ("processing:"));
1596                 DEBUG (print_ins (i, ins));
1597                 /* update for use with FP regs... */
1598                 if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
1599                         val = rs->iassign [ins->dreg];
1600                         prev_dreg = ins->dreg;
1601                         if (val < 0) {
1602                                 int spill = 0;
1603                                 if (val < -1) {
1604                                         /* the register gets spilled after this inst */
1605                                         spill = -val -1;
1606                                 }
1607                                 val = mono_regstate_alloc_int (rs, dest_mask);
1608                                 if (val < 0)
1609                                         val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1610                                 rs->iassign [ins->dreg] = val;
1611                                 if (spill)
1612                                         create_spilled_store (cfg, spill, val, prev_dreg, ins);
1613                         }
1614                         DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1615                         rs->isymbolic [val] = prev_dreg;
1616                         ins->dreg = val;
1617                         if (spec [MONO_INST_DEST] == 'l') {
1618                                 int hreg = prev_dreg + 1;
1619                                 val = rs->iassign [hreg];
1620                                 if (val < 0) {
1621                                         int spill = 0;
1622                                         if (val < -1) {
1623                                                 /* the register gets spilled after this inst */
1624                                                 spill = -val -1;
1625                                         }
1626                                         val = mono_regstate_alloc_int (rs, dest_mask);
1627                                         if (val < 0)
1628                                                 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1629                                         rs->iassign [hreg] = val;
1630                                         if (spill)
1631                                                 create_spilled_store (cfg, spill, val, hreg, ins);
1632                                 }
1633                                 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1634                                 rs->isymbolic [val] = hreg;
1635                                 /* FIXME:? ins->dreg = val; */
1636                                 if (ins->dreg == ppc_r4) {
1637                                         if (val != ppc_r3)
1638                                                 create_copy_ins (cfg, val, ppc_r3, ins);
1639                                 } else if (ins->dreg == ppc_r3) {
1640                                         if (val == ppc_r4) {
1641                                                 /* swap */
1642                                                 create_copy_ins (cfg, ppc_r0, ppc_r3, ins);
1643                                                 create_copy_ins (cfg, ppc_r3, ppc_r4, ins);
1644                                                 create_copy_ins (cfg, ppc_r4, ppc_r0, ins);
1645                                         } else {
1646                                                 /* two forced copies */
1647                                                 create_copy_ins (cfg, val, ppc_r3, ins);
1648                                                 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1649                                         }
1650                                 } else {
1651                                         if (val == ppc_r3) {
1652                                                 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1653                                         } else {
1654                                                 /* two forced copies */
1655                                                 create_copy_ins (cfg, val, ppc_r3, ins);
1656                                                 create_copy_ins (cfg, ins->dreg, ppc_r4, ins);
1657                                         }
1658                                 }
1659                                 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1660                                         DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1661                                         mono_regstate_free_int (rs, val);
1662                                 }
1663                         }
1664                 } else {
1665                         prev_dreg = -1;
1666                 }
1667                 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1668                         DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1669                         mono_regstate_free_int (rs, ins->dreg);
1670                 }
1671                 if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
1672                         val = rs->iassign [ins->sreg1];
1673                         prev_sreg1 = ins->sreg1;
1674                         if (val < 0) {
1675                                 int spill = 0;
1676                                 if (val < -1) {
1677                                         /* the register gets spilled after this inst */
1678                                         spill = -val -1;
1679                                 }
1680                                 if (0 && ins->opcode == OP_MOVE) {
1681                                         /* 
1682                                          * small optimization: the dest register is already allocated
1683                                          * but the src one is not: we can simply assign the same register
1684                                          * here and peephole will get rid of the instruction later.
1685                                          * This optimization may interfere with the clobbering handling:
1686                                          * it removes a mov operation that will be added again to handle clobbering.
1687                                          * There are also some other issues that should with make testjit.
1688                                          */
1689                                         mono_regstate_alloc_int (rs, 1 << ins->dreg);
1690                                         val = rs->iassign [ins->sreg1] = ins->dreg;
1691                                         //g_assert (val >= 0);
1692                                         DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1693                                 } else {
1694                                         //g_assert (val == -1); /* source cannot be spilled */
1695                                         val = mono_regstate_alloc_int (rs, src1_mask);
1696                                         if (val < 0)
1697                                                 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1698                                         rs->iassign [ins->sreg1] = val;
1699                                         DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1700                                 }
1701                                 if (spill) {
1702                                         MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1703                                         insert_before_ins (ins, tmp, store);
1704                                 }
1705                         }
1706                         rs->isymbolic [val] = prev_sreg1;
1707                         ins->sreg1 = val;
1708                 } else {
1709                         prev_sreg1 = -1;
1710                 }
1711                 if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
1712                         val = rs->iassign [ins->sreg2];
1713                         prev_sreg2 = ins->sreg2;
1714                         if (val < 0) {
1715                                 int spill = 0;
1716                                 if (val < -1) {
1717                                         /* the register gets spilled after this inst */
1718                                         spill = -val -1;
1719                                 }
1720                                 val = mono_regstate_alloc_int (rs, src2_mask);
1721                                 if (val < 0)
1722                                         val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1723                                 rs->iassign [ins->sreg2] = val;
1724                                 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1725                                 if (spill)
1726                                         create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1727                         }
1728                         rs->isymbolic [val] = prev_sreg2;
1729                         ins->sreg2 = val;
1730                 } else {
1731                         prev_sreg2 = -1;
1732                 }
1733
1734                 if (spec [MONO_INST_CLOB] == 'c') {
1735                         int j, s;
1736                         guint32 clob_mask = PPC_CALLER_REGS;
1737                         for (j = 0; j < MONO_MAX_IREGS; ++j) {
1738                                 s = 1 << j;
1739                                 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1740                                         //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1741                                 }
1742                         }
1743                 }
1744                 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1745                         DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1746                         mono_regstate_free_int (rs, ins->sreg1);
1747                 }
1748                 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1749                         DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1750                         mono_regstate_free_int (rs, ins->sreg2);
1751                 }*/
1752                 
1753                 //DEBUG (print_ins (i, ins));
1754                 tmp = tmp->next;
1755         }
1756 }
1757
1758 static guchar*
1759 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1760 {
1761         return code;
1762 }
1763
1764 static unsigned char*
1765 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1766 {
1767 #if 0
1768         int sreg = tree->sreg1;
1769         x86_alu_reg_reg (code, X86_SUB, X86_ESP, tree->sreg1);
1770         if (tree->flags & MONO_INST_INIT) {
1771                 int offset = 0;
1772                 if (tree->dreg != X86_EAX && sreg != X86_EAX) {
1773                         x86_push_reg (code, X86_EAX);
1774                         offset += 4;
1775                 }
1776                 if (tree->dreg != X86_ECX && sreg != X86_ECX) {
1777                         x86_push_reg (code, X86_ECX);
1778                         offset += 4;
1779                 }
1780                 if (tree->dreg != X86_EDI && sreg != X86_EDI) {
1781                         x86_push_reg (code, X86_EDI);
1782                         offset += 4;
1783                 }
1784                 
1785                 x86_shift_reg_imm (code, X86_SHR, sreg, 2);
1786                 if (sreg != X86_ECX)
1787                         x86_mov_reg_reg (code, X86_ECX, sreg, 4);
1788                 x86_alu_reg_reg (code, X86_XOR, X86_EAX, X86_EAX);
1789                                 
1790                 x86_lea_membase (code, X86_EDI, X86_ESP, offset);
1791                 x86_cld (code);
1792                 x86_prefix (code, X86_REP_PREFIX);
1793                 x86_stosl (code);
1794                 
1795                 if (tree->dreg != X86_EDI && sreg != X86_EDI)
1796                         x86_pop_reg (code, X86_EDI);
1797                 if (tree->dreg != X86_ECX && sreg != X86_ECX)
1798                         x86_pop_reg (code, X86_ECX);
1799                 if (tree->dreg != X86_EAX && sreg != X86_EAX)
1800                         x86_pop_reg (code, X86_EAX);
1801         }
1802 #endif
1803         return code;
1804 }
1805
1806 void
1807 ppc_patch (guchar *code, guchar *target)
1808 {
1809         guint32 ins = *(guint32*)code;
1810         guint32 prim = ins >> 26;
1811
1812 //      g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1813         if (prim == 18) {
1814                 // absolute address
1815                 if (ins & 2) {
1816                         guint32 li = (guint32)target;
1817                         ins = prim << 26 | (ins & 3);
1818                         ins |= li;
1819                         // FIXME: assert the top bits of li are 0
1820                 } else {
1821                         gint diff = target - code;
1822                         ins = prim << 26 | (ins & 3);
1823                         diff &= ~3;
1824                         diff &= ~(63 << 26);
1825                         ins |= diff;
1826                 }
1827                 *(guint32*)code = ins;
1828         } else if (prim == 16) {
1829                 // absolute address
1830                 if (ins & 2) {
1831                         guint32 li = (guint32)target;
1832                         ins = (ins & 0xffff0000) | (ins & 3);
1833                         li &= 0xffff;
1834                         ins |= li;
1835                         // FIXME: assert the top bits of li are 0
1836                 } else {
1837                         gint diff = target - code;
1838                         ins = (ins & 0xffff0000) | (ins & 3);
1839                         diff &= 0xffff;
1840                         ins |= diff;
1841                 }
1842                 *(guint32*)code = ins;
1843         } else {
1844                 g_assert_not_reached ();
1845         }
1846 //      g_print ("patched with 0x%08x\n", ins);
1847 }
1848
1849 void
1850 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1851 {
1852         MonoInst *ins;
1853         MonoCallInst *call;
1854         guint offset;
1855         guint8 *code = cfg->native_code + cfg->code_len;
1856         MonoInst *last_ins = NULL;
1857         guint last_offset = 0;
1858         int max_len, cpos;
1859
1860         if (cfg->opt & MONO_OPT_PEEPHOLE)
1861                 peephole_pass (cfg, bb);
1862
1863 #if 0
1864         /* 
1865          * various stratgies to align BBs. Using real loop detection or simply
1866          * aligning every block leads to more consistent benchmark results,
1867          * but usually slows down the code
1868          * we should do the alignment outside this function or we should adjust
1869          * bb->native offset as well or the code is effectively slowed down!
1870          */
1871         /* align all blocks */
1872 //      if ((pad = (cfg->code_len & (align - 1)))) {
1873         /* poor man loop start detection */
1874 //      if (bb->code && bb->in_count && bb->in_bb [0]->cil_code > bb->cil_code && (pad = (cfg->code_len & (align - 1)))) {
1875         /* consider real loop detection and nesting level */
1876 //      if (bb->loop_blocks && bb->nesting < 3 && (pad = (cfg->code_len & (align - 1)))) {
1877         /* consider real loop detection */
1878         if (/*bb->loop_blocks &&*/ (pad = (cfg->code_len & (align - 1)))) {
1879                 pad = align - pad;
1880                 x86_padding (code, pad);
1881                 cfg->code_len += pad;
1882                 bb->native_offset = cfg->code_len;
1883         }
1884 #endif
1885
1886         if (cfg->verbose_level > 2)
1887                 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1888
1889         cpos = bb->max_offset;
1890
1891         if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1892                 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
1893                 //g_assert (!mono_compile_aot);
1894                 //cpos += 6;
1895                 //if (bb->cil_code)
1896                 //      cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
1897                 /* this is not thread save, but good enough */
1898                 /* fixme: howto handle overflows? */
1899                 //x86_inc_mem (code, &cov->data [bb->dfn].count); 
1900         }
1901
1902         ins = bb->code;
1903         while (ins) {
1904                 offset = code - cfg->native_code;
1905
1906                 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1907
1908                 if (offset > (cfg->code_size - max_len - 16)) {
1909                         cfg->code_size *= 2;
1910                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1911                         code = cfg->native_code + offset;
1912                 }
1913         //      if (ins->cil_code)
1914         //              g_print ("cil code\n");
1915
1916                 switch (ins->opcode) {
1917                 case OP_STOREI1_MEMBASE_IMM:
1918                         ppc_li (code, ppc_r11, ins->inst_imm);
1919                         g_assert (ppc_is_imm16 (ins->inst_offset));
1920                         ppc_stb (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
1921                         break;
1922                 case OP_STOREI2_MEMBASE_IMM:
1923                         ppc_li (code, ppc_r11, ins->inst_imm);
1924                         g_assert (ppc_is_imm16 (ins->inst_offset));
1925                         ppc_sth (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
1926                         break;
1927                 case OP_STORE_MEMBASE_IMM:
1928                 case OP_STOREI4_MEMBASE_IMM:
1929                         ppc_load (code, ppc_r11, ins->inst_imm);
1930                         g_assert (ppc_is_imm16 (ins->inst_offset));
1931                         ppc_stw (code, ppc_r11, ins->inst_offset, ins->inst_destbasereg);
1932                         break;
1933                 case OP_STOREI1_MEMBASE_REG:
1934                         g_assert (ppc_is_imm16 (ins->inst_offset));
1935                         ppc_stb (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1936                         break;
1937                 case OP_STOREI2_MEMBASE_REG:
1938                         g_assert (ppc_is_imm16 (ins->inst_offset));
1939                         ppc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1940                         break;
1941                 case OP_STORE_MEMBASE_REG:
1942                 case OP_STOREI4_MEMBASE_REG:
1943                         g_assert (ppc_is_imm16 (ins->inst_offset));
1944                         ppc_stw (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1945                         break;
1946                 case CEE_LDIND_I:
1947                 case CEE_LDIND_I4:
1948                 case CEE_LDIND_U4:
1949                         g_assert_not_reached ();
1950                         //x86_mov_reg_mem (code, ins->dreg, ins->inst_p0, 4);
1951                         break;
1952                 case OP_LOADU4_MEM:
1953                         g_assert_not_reached ();
1954                         //x86_mov_reg_imm (code, ins->dreg, ins->inst_p0);
1955                         //x86_mov_reg_membase (code, ins->dreg, ins->dreg, 0, 4);
1956                         break;
1957                 case OP_LOAD_MEMBASE:
1958                 case OP_LOADI4_MEMBASE:
1959                 case OP_LOADU4_MEMBASE:
1960                         if (ppc_is_imm16 (ins->inst_offset)) {
1961                                 ppc_lwz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
1962                         } else {
1963                                 ppc_load (code, ppc_r11, ins->inst_offset);
1964                                 ppc_lwzx (code, ins->dreg, ppc_r11, ins->inst_basereg);
1965                         }
1966                         break;
1967                 case OP_LOADU1_MEMBASE:
1968                         g_assert (ppc_is_imm16 (ins->inst_offset));
1969                         ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
1970                         break;
1971                 case OP_LOADI1_MEMBASE:
1972                         g_assert (ppc_is_imm16 (ins->inst_offset));
1973                         // FIXME: sign extend
1974                         ppc_lbz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
1975                         break;
1976                 case OP_LOADU2_MEMBASE:
1977                         g_assert (ppc_is_imm16 (ins->inst_offset));
1978                         ppc_lhz (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
1979                         break;
1980                 case OP_LOADI2_MEMBASE:
1981                         g_assert (ppc_is_imm16 (ins->inst_offset));
1982                         ppc_lha (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
1983                         break;
1984                 case CEE_CONV_I1:
1985                         ppc_extsb (code, ins->dreg, ins->sreg1);
1986                         break;
1987                 case CEE_CONV_I2:
1988                         ppc_extsh (code, ins->dreg, ins->sreg1);
1989                         break;
1990                 case CEE_CONV_U1:
1991                         ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 24, 31);
1992                         break;
1993                 case CEE_CONV_U2:
1994                         ppc_rlwinm (code, ins->dreg, ins->sreg1, 0, 16, 31);
1995                         break;
1996                 case OP_COMPARE:
1997                         if (ins->next && ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN)
1998                                 ppc_cmpl (code, 0, 0, ins->sreg1, ins->sreg2);
1999                         else
2000                                 ppc_cmp (code, 0, 0, ins->sreg1, ins->sreg2);
2001                         break;
2002                 case OP_COMPARE_IMM:
2003                         if (ppc_is_imm16 (ins->inst_imm)) {
2004                                 if (ins->next && ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN)
2005                                         ppc_cmpli (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2006                                 else
2007                                         ppc_cmpi (code, 0, 0, ins->sreg1, (ins->inst_imm & 0xffff));
2008                         } else {
2009                                 ppc_load (code, ppc_r11, ins->inst_imm);
2010                                 if (ins->next && ins->next->opcode >= CEE_BNE_UN && ins->next->opcode <= CEE_BLT_UN)
2011                                         ppc_cmp (code, 0, 0, ins->sreg1, ppc_r11);
2012                                 else
2013                                         ppc_cmpl (code, 0, 0, ins->sreg1, ppc_r11);
2014                         }
2015                         break;
2016                 case OP_X86_TEST_NULL:
2017                         ppc_cmpi (code, 0, 0, ins->sreg1, 0);
2018                         break;
2019                 case CEE_BREAK:
2020                         ppc_break (code);
2021                         break;
2022                 case OP_ADDCC:
2023                         ppc_addc (code, ins->dreg, ins->sreg1, ins->sreg2);
2024                         break;
2025                 case CEE_ADD:
2026                         ppc_add (code, ins->dreg, ins->sreg1, ins->sreg2);
2027                         break;
2028                 case OP_ADC:
2029                         ppc_adde (code, ins->dreg, ins->sreg1, ins->sreg2);
2030                         break;
2031                 case OP_ADD_IMM:
2032                         if (ppc_is_imm16 (ins->inst_imm)) {
2033                                 ppc_addi (code, ins->dreg, ins->sreg1, ins->inst_imm);
2034                         } else {
2035                                 ppc_load (code, ppc_r11, ins->inst_imm);
2036                                 ppc_add (code, ins->dreg, ins->sreg1, ppc_r11);
2037                         }
2038                         break;
2039                 case OP_ADC_IMM:
2040                         ppc_load (code, ppc_r11, ins->inst_imm);
2041                         ppc_adde (code, ins->dreg, ins->sreg1, ppc_r11);
2042                         break;
2043                 case OP_SUBCC:
2044                         ppc_subfc (code, ins->dreg, ins->sreg2, ins->sreg1);
2045                         break;
2046                 case CEE_SUB:
2047                         ppc_subf (code, ins->dreg, ins->sreg2, ins->sreg1);
2048                         break;
2049                 case OP_SBB:
2050                         ppc_subfe (code, ins->dreg, ins->sreg2, ins->sreg1);
2051                         break;
2052                 case OP_SUB_IMM:
2053                         // we add the negated value
2054                         g_assert (ppc_is_imm16 (-ins->inst_imm));
2055                         ppc_addi (code, ins->dreg, ins->sreg1, -ins->inst_imm);
2056                         break;
2057                 case OP_SBB_IMM:
2058                         ppc_load (code, ppc_r11, ins->inst_imm);
2059                         ppc_subfe (code, ins->dreg, ins->sreg2, ppc_r11);
2060                         break;
2061                 case OP_PPC_SUBFIC:
2062                         g_assert (ppc_is_imm16 (ins->inst_imm));
2063                         ppc_subfic (code, ins->dreg, ins->sreg1, ins->inst_imm);
2064                         break;
2065                 case OP_PPC_SUBFZE:
2066                         ppc_subfze (code, ins->dreg, ins->sreg1);
2067                         break;
2068                 case CEE_AND:
2069                         /* FIXME: the ppc macros as inconsistent here: put dest as the first arg! */
2070                         ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2071                         break;
2072                 case OP_AND_IMM:
2073                         if (!(ins->inst_imm & 0xffff0000)) {
2074                                 ppc_andid (code, ins->sreg1, ins->dreg, ins->inst_imm);
2075                         } else if (!(ins->inst_imm & 0xffff)) {
2076                                 ppc_andisd (code, ins->sreg1, ins->dreg, ((guint32)ins->inst_imm >> 16));
2077                         } else {
2078                                 ppc_load (code, ppc_r11, ins->inst_imm);
2079                                 ppc_and (code, ins->sreg1, ins->dreg, ins->sreg2);
2080                         }
2081                         break;
2082                 case CEE_DIV:
2083                         ppc_divw (code, ins->dreg, ins->sreg1, ins->sreg2);
2084                         break;
2085                 case CEE_DIV_UN:
2086                         ppc_divwu (code, ins->dreg, ins->sreg1, ins->sreg2);
2087                         break;
2088                 case OP_DIV_IMM:
2089                         ppc_load (code, ppc_r11, ins->inst_imm);
2090                         ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2091                         break;
2092                 case CEE_REM:
2093                         ppc_divw (code, ppc_r11, ins->sreg1, ins->sreg2);
2094                         ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2095                         ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2096                         break;
2097                 case CEE_REM_UN:
2098                         ppc_divwu (code, ppc_r11, ins->sreg1, ins->sreg2);
2099                         ppc_mullw (code, ppc_r11, ppc_r11, ins->sreg2);
2100                         ppc_subf (code, ins->dreg, ppc_r11, ins->sreg1);
2101                         break;
2102                 case OP_REM_IMM:
2103                         ppc_load (code, ppc_r11, ins->inst_imm);
2104                         ppc_divw (code, ins->dreg, ins->sreg1, ppc_r11);
2105                         ppc_mullw (code, ins->dreg, ins->dreg, ppc_r11);
2106                         ppc_subf (code, ins->dreg, ins->dreg, ins->sreg1);
2107                         break;
2108                 case CEE_OR:
2109                         ppc_or (code, ins->dreg, ins->sreg1, ins->sreg2);
2110                         break;
2111                 case OP_OR_IMM:
2112                         if (!(ins->inst_imm & 0xffff0000)) {
2113                                 ppc_ori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2114                         } else if (!(ins->inst_imm & 0xffff)) {
2115                                 ppc_oris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2116                         } else {
2117                                 ppc_load (code, ppc_r11, ins->inst_imm);
2118                                 ppc_or (code, ins->sreg1, ins->dreg, ins->sreg2);
2119                         }
2120                         break;
2121                 case CEE_XOR:
2122                         ppc_xor (code, ins->dreg, ins->sreg1, ins->sreg2);
2123                         break;
2124                 case OP_XOR_IMM:
2125                         if (!(ins->inst_imm & 0xffff0000)) {
2126                                 ppc_xori (code, ins->sreg1, ins->dreg, ins->inst_imm);
2127                         } else if (!(ins->inst_imm & 0xffff)) {
2128                                 ppc_xoris (code, ins->sreg1, ins->dreg, ((guint32)(ins->inst_imm) >> 16));
2129                         } else {
2130                                 ppc_load (code, ppc_r11, ins->inst_imm);
2131                                 ppc_xor (code, ins->sreg1, ins->dreg, ins->sreg2);
2132                         }
2133                         break;
2134                 case CEE_SHL:
2135                         ppc_slw (code, ins->sreg1, ins->dreg, ins->sreg2);
2136                         break;
2137                 case OP_SHL_IMM:
2138                         ppc_rlwinm (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0xf), 0, (31 - (ins->inst_imm & 0xf)));
2139                         //ppc_load (code, ppc_r11, ins->inst_imm);
2140                         //ppc_slw (code, ins->sreg1, ins->dreg, ppc_r11);
2141                         break;
2142                 case CEE_SHR:
2143                         ppc_sraw (code, ins->dreg, ins->sreg1, ins->sreg2);
2144                         break;
2145                 case OP_SHR_IMM:
2146                         // there is also ppc_srawi
2147                         //ppc_load (code, ppc_r11, ins->inst_imm);
2148                         //ppc_sraw (code, ins->dreg, ins->sreg1, ppc_r11);
2149                         ppc_srawi (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
2150                         break;
2151                 case OP_SHR_UN_IMM:
2152                         ppc_load (code, ppc_r11, ins->inst_imm);
2153                         ppc_srw (code, ins->dreg, ins->sreg1, ppc_r11);
2154                         //ppc_rlwinm (code, ins->dreg, ins->sreg1, (32 - (ins->inst_imm & 0xf)), (ins->inst_imm & 0xf), 31);
2155                         break;
2156                 case CEE_SHR_UN:
2157                         ppc_srw (code, ins->dreg, ins->sreg1, ins->sreg2);
2158                         break;
2159                 case CEE_NOT:
2160                         ppc_not (code, ins->dreg, ins->sreg1);
2161                         break;
2162                 case CEE_NEG:
2163                         ppc_neg (code, ins->dreg, ins->sreg1);
2164                         break;
2165                 case CEE_MUL:
2166                         ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2167                         break;
2168                 case OP_MUL_IMM:
2169                         ppc_load (code, ppc_r11, ins->inst_imm);
2170                         ppc_mullw (code, ins->dreg, ins->sreg1, ppc_r11);
2171                         break;
2172                 case CEE_MUL_OVF:
2173                         ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2174                         //g_assert_not_reached ();
2175                         //x86_imul_reg_reg (code, ins->sreg1, ins->sreg2);
2176                         //EMIT_COND_SYSTEM_EXCEPTION (X86_CC_O, FALSE, "OverflowException");
2177                         break;
2178                 case CEE_MUL_OVF_UN:
2179                         ppc_mullw (code, ins->dreg, ins->sreg1, ins->sreg2);
2180                         //FIXME: g_assert_not_reached ();
2181                         break;
2182                 case OP_ICONST:
2183                 case OP_SETREGIMM:
2184                         ppc_load (code, ins->dreg, ins->inst_c0);
2185                         break;
2186                 /*case OP_CLASS:
2187                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_CLASS, (gpointer)ins->inst_c0);
2188                         ppc_load (code, ins->dreg, 0xff00ff00);
2189                         break;
2190                 case OP_IMAGE:
2191                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_IMAGE, (gpointer)ins->inst_c0);
2192                         ppc_load (code, ins->dreg, 0xff00ff00);
2193                         break;*/
2194                 case CEE_CONV_I4:
2195                 case CEE_CONV_U4:
2196                 case OP_MOVE:
2197                 case OP_SETREG:
2198                         ppc_mr (code, ins->dreg, ins->sreg1);
2199                         break;
2200                 case CEE_JMP:
2201                         g_assert_not_reached ();
2202                         break;
2203                 case OP_CHECK_THIS:
2204                         /* ensure ins->sreg1 is not NULL */
2205                         g_assert_not_reached ();
2206                         //x86_alu_membase_imm (code, X86_CMP, ins->sreg1, 0, 0);
2207                         break;
2208                 case OP_FCALL:
2209                 case OP_LCALL:
2210                 case OP_VCALL:
2211                 case OP_VOIDCALL:
2212                 case CEE_CALL:
2213                         call = (MonoCallInst*)ins;
2214                         if (ins->flags & MONO_INST_HAS_METHOD)
2215                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2216                         else
2217                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2218                         ppc_bl (code, 0);
2219                         break;
2220                 case OP_FCALL_REG:
2221                 case OP_LCALL_REG:
2222                 case OP_VCALL_REG:
2223                 case OP_VOIDCALL_REG:
2224                 case OP_CALL_REG:
2225                         ppc_mtlr (code, ins->sreg1);
2226                         ppc_blrl (code);
2227                         break;
2228                 case OP_FCALL_MEMBASE:
2229                 case OP_LCALL_MEMBASE:
2230                 case OP_VCALL_MEMBASE:
2231                 case OP_VOIDCALL_MEMBASE:
2232                 case OP_CALL_MEMBASE:
2233                         ppc_lwz (code, ppc_r0, ins->inst_offset, ins->sreg1);
2234                         ppc_mtlr (code, ppc_r0);
2235                         ppc_blrl (code);
2236                         break;
2237                 case OP_OUTARG:
2238                         g_assert_not_reached ();
2239                         break;
2240                 case OP_LOCALLOC:
2241                         /* keep alignment */
2242 #define MONO_FRAME_ALIGNMENT 32
2243                         ppc_addi (code, ppc_r0, ins->sreg1, MONO_FRAME_ALIGNMENT-1);
2244                         ppc_rlwinm (code, ppc_r0, ppc_r0, 0, 0, 27);
2245                         ppc_lwz (code, ppc_r11, 0, ppc_sp);
2246                         ppc_neg (code, ppc_r0, ppc_r0);
2247                         ppc_stwux (code, ppc_sp, ppc_r0, ppc_sp);
2248                         ppc_mr (code, ins->dreg, ppc_sp);
2249                         g_assert_not_reached ();
2250                         break;
2251                 case CEE_RET:
2252                         ppc_blr (code);
2253                         break;
2254                 case CEE_THROW: {
2255                         ppc_mr (code, ppc_r3, ins->sreg1);
2256                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
2257                                              (gpointer)"throw_exception");
2258                         ppc_bl (code, 0);
2259                         break;
2260                 }
2261                 case OP_ENDFILTER:
2262                         if (ins->sreg1 != ppc_r3)
2263                                 ppc_mr (code, ppc_r3, ins->sreg1);
2264                         ppc_blr (code);
2265                         break;
2266                 case CEE_ENDFINALLY:
2267                         ppc_blr (code);
2268                         break;
2269                 case OP_CALL_HANDLER: 
2270                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2271                         ppc_bl (code, 0);
2272                         break;
2273                 case OP_LABEL:
2274                         ins->inst_c0 = code - cfg->native_code;
2275                         break;
2276                 case CEE_BR:
2277                         //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2278                         //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2279                         //break;
2280                         if (ins->flags & MONO_INST_BRLABEL) {
2281                                 /*if (ins->inst_i0->inst_c0) {
2282                                         ppc_b (code, 0);
2283                                         //x86_jump_code (code, cfg->native_code + ins->inst_i0->inst_c0);
2284                                 } else*/ {
2285                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2286                                         ppc_b (code, 0);
2287                                 }
2288                         } else {
2289                                 /*if (ins->inst_target_bb->native_offset) {
2290                                         ppc_b (code, 0);
2291                                         //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset); 
2292                                 } else*/ {
2293                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2294                                         ppc_b (code, 0);
2295                                 } 
2296                         }
2297                         break;
2298                 case OP_BR_REG:
2299                         ppc_mtctr (code, ins->sreg1);
2300                         ppc_bcctr (code, 20, 0);
2301                         break;
2302                 case OP_CEQ:
2303                         ppc_li (code, ins->dreg, 0);
2304                         ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2305                         ppc_li (code, ins->dreg, 1);
2306                         break;
2307                 case OP_CLT:
2308                 case OP_CLT_UN:
2309                         ppc_li (code, ins->dreg, 1);
2310                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2311                         ppc_li (code, ins->dreg, 0);
2312                         break;
2313                 case OP_CGT:
2314                 case OP_CGT_UN:
2315                         ppc_li (code, ins->dreg, 1);
2316                         ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2317                         ppc_li (code, ins->dreg, 0);
2318                         break;
2319                 case OP_COND_EXC_EQ:
2320                 case OP_COND_EXC_NE_UN:
2321                 case OP_COND_EXC_LT:
2322                 case OP_COND_EXC_LT_UN:
2323                 case OP_COND_EXC_GT:
2324                 case OP_COND_EXC_GT_UN:
2325                 case OP_COND_EXC_GE:
2326                 case OP_COND_EXC_GE_UN:
2327                 case OP_COND_EXC_LE:
2328                 case OP_COND_EXC_LE_UN:
2329                 case OP_COND_EXC_OV:
2330                 case OP_COND_EXC_NO:
2331                 case OP_COND_EXC_C:
2332                 case OP_COND_EXC_NC:
2333                         //EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], 
2334                         //                          (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2335                         break;
2336                 case CEE_BEQ:
2337                 case CEE_BNE_UN:
2338                 case CEE_BLT:
2339                 case CEE_BLT_UN:
2340                 case CEE_BGT:
2341                 case CEE_BGT_UN:
2342                 case CEE_BGE:
2343                 case CEE_BGE_UN:
2344                 case CEE_BLE:
2345                 case CEE_BLE_UN:
2346                         EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2347                         break;
2348
2349                 /* floating point opcodes */
2350                 case OP_R8CONST:
2351                         ppc_load (code, ppc_r11, ins->inst_p0);
2352                         ppc_lfd (code, ins->dreg, 0, ppc_r11);
2353                         break;
2354                 case OP_R4CONST:
2355                         ppc_load (code, ppc_r11, ins->inst_p0);
2356                         ppc_lfs (code, ins->dreg, 0, ppc_r11);
2357                         break;
2358                 case OP_STORER8_MEMBASE_REG:
2359                         ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2360                         break;
2361                 case OP_LOADR8_MEMBASE:
2362                         ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2363                         break;
2364                 case OP_STORER4_MEMBASE_REG:
2365                         ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2366                         break;
2367                 case OP_LOADR4_MEMBASE:
2368                         ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2369                         break;
2370                 case CEE_CONV_R4: /* FIXME: change precision */
2371                 case CEE_CONV_R8:
2372                         g_assert_not_reached ();
2373                         x86_push_reg (code, ins->sreg1);
2374                         x86_fild_membase (code, X86_ESP, 0, FALSE);
2375                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2376                         break;
2377                 case OP_X86_FP_LOAD_I8:
2378                         g_assert_not_reached ();
2379                         x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2380                         break;
2381                 case OP_X86_FP_LOAD_I4:
2382                         g_assert_not_reached ();
2383                         x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2384                         break;
2385                 case OP_FCONV_TO_I1:
2386                         g_assert_not_reached ();
2387                         code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2388                         break;
2389                 case OP_FCONV_TO_U1:
2390                         g_assert_not_reached ();
2391                         code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2392                         break;
2393                 case OP_FCONV_TO_I2:
2394                         g_assert_not_reached ();
2395                         code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2396                         break;
2397                 case OP_FCONV_TO_U2:
2398                         g_assert_not_reached ();
2399                         code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2400                         break;
2401                 case OP_FCONV_TO_I4:
2402                 case OP_FCONV_TO_I:
2403                         g_assert_not_reached ();
2404                         code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2405                         break;
2406                 case OP_FCONV_TO_U4:
2407                 case OP_FCONV_TO_U:
2408                         g_assert_not_reached ();
2409                         code = emit_float_to_int (cfg, code, ins->dreg, 4, FALSE);
2410                         break;
2411                 case OP_FCONV_TO_I8:
2412                 case OP_FCONV_TO_U8:
2413                         g_assert_not_reached ();
2414                         /*x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2415                         x86_fnstcw_membase(code, X86_ESP, 0);
2416                         x86_mov_reg_membase (code, ins->inst_dreg_low, X86_ESP, 0, 2);
2417                         x86_alu_reg_imm (code, X86_OR, ins->inst_dreg_low, 0xc00);
2418                         x86_mov_membase_reg (code, X86_ESP, 2, ins->inst_dreg_low, 2);
2419                         x86_fldcw_membase (code, X86_ESP, 2);
2420                         x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2421                         x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2422                         x86_pop_reg (code, ins->inst_dreg_low);
2423                         x86_pop_reg (code, ins->inst_dreg_high);
2424                         x86_fldcw_membase (code, X86_ESP, 0);
2425                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);*/
2426                         break;
2427                 case OP_LCONV_TO_R_UN: { 
2428 #if 0
2429                         static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2430                         guint8 *br;
2431
2432                         /* load 64bit integer to FP stack */
2433                         x86_push_imm (code, 0);
2434                         x86_push_reg (code, ins->sreg2);
2435                         x86_push_reg (code, ins->sreg1);
2436                         x86_fild_membase (code, X86_ESP, 0, TRUE);
2437                         /* store as 80bit FP value */
2438                         x86_fst80_membase (code, X86_ESP, 0);
2439                         
2440                         /* test if lreg is negative */
2441                         x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2442                         br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2443         
2444                         /* add correction constant mn */
2445                         x86_fld80_mem (code, mn);
2446                         x86_fld80_membase (code, X86_ESP, 0);
2447                         x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2448                         x86_fst80_membase (code, X86_ESP, 0);
2449
2450                         x86_patch (br, code);
2451
2452                         x86_fld80_membase (code, X86_ESP, 0);
2453                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2454 #endif
2455                         g_assert_not_reached ();
2456                         break;
2457                 }
2458                 case OP_LCONV_TO_OVF_I: {
2459 #if 0
2460                         guint8 *br [3], *label [1];
2461
2462                         /* 
2463                          * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2464                          */
2465                         x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2466
2467                         /* If the low word top bit is set, see if we are negative */
2468                         br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2469                         /* We are not negative (no top bit set, check for our top word to be zero */
2470                         x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2471                         br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2472                         label [0] = code;
2473
2474                         /* throw exception */
2475                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2476                         x86_jump32 (code, 0);
2477         
2478                         x86_patch (br [0], code);
2479                         /* our top bit is set, check that top word is 0xfffffff */
2480                         x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2481                 
2482                         x86_patch (br [1], code);
2483                         /* nope, emit exception */
2484                         br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2485                         x86_patch (br [2], label [0]);
2486
2487                         if (ins->dreg != ins->sreg1)
2488                                 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2489 #endif
2490                         g_assert_not_reached ();
2491                         break;
2492                 }
2493                 case OP_FADD:
2494                         ppc_fadd (code, ins->dreg, ins->sreg1, ins->sreg2);
2495                         break;
2496                 case OP_FSUB:
2497                         ppc_fsub (code, ins->dreg, ins->sreg1, ins->sreg2);
2498                         break;          
2499                 case OP_FMUL:
2500                         ppc_fmul (code, ins->dreg, ins->sreg1, ins->sreg2);
2501                         break;          
2502                 case OP_FDIV:
2503                         ppc_fdiv (code, ins->dreg, ins->sreg1, ins->sreg2);
2504                         break;          
2505                 case OP_FNEG:
2506                         ppc_fneg (code, ins->dreg, ins->sreg1);
2507                         break;          
2508                 case OP_FREM:
2509                         g_assert_not_reached ();
2510                         break;
2511                 case OP_FCOMPARE:
2512                         g_assert_not_reached ();
2513                         /* this overwrites EAX */
2514                         EMIT_FPCOMPARE(code);
2515                         break;
2516                 case OP_FCEQ:
2517                         g_assert_not_reached ();
2518                         /*if (ins->dreg != X86_EAX) 
2519                                 x86_push_reg (code, X86_EAX);
2520
2521                         EMIT_FPCOMPARE(code);
2522                         x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2523                         x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2524                         x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2525
2526                         if (ins->dreg != X86_EAX) 
2527                                 x86_pop_reg (code, X86_EAX);*/
2528                         break;
2529                 case OP_FCLT:
2530                 case OP_FCLT_UN:
2531                         g_assert_not_reached ();
2532                         /*if (ins->dreg != X86_EAX) 
2533                                 x86_push_reg (code, X86_EAX);
2534
2535                         EMIT_FPCOMPARE(code);
2536                         x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2537                         x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2538
2539                         if (ins->dreg != X86_EAX) 
2540                                 x86_pop_reg (code, X86_EAX);*/
2541                         break;
2542                 case OP_FCGT:
2543                 case OP_FCGT_UN:
2544                         g_assert_not_reached ();
2545                         /*if (ins->dreg != X86_EAX) 
2546                                 x86_push_reg (code, X86_EAX);
2547
2548                         EMIT_FPCOMPARE(code);
2549                         x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2550                         x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2551                         x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2552
2553                         if (ins->dreg != X86_EAX) 
2554                                 x86_pop_reg (code, X86_EAX);*/
2555                         break;
2556                 case OP_FBEQ:
2557                         g_assert_not_reached ();
2558                         break;
2559                 case OP_FBNE_UN:
2560                         g_assert_not_reached ();
2561                         break;
2562                 case OP_FBLT:
2563                 case OP_FBLT_UN:
2564                         g_assert_not_reached ();
2565                         break;
2566                 case OP_FBGT:
2567                 case OP_FBGT_UN:
2568                         g_assert_not_reached ();
2569                         break;
2570                 case OP_FBGE:
2571                 case OP_FBGE_UN:
2572                         g_assert_not_reached ();
2573                         break;
2574                 case OP_FBLE:
2575                 case OP_FBLE_UN:
2576                         g_assert_not_reached ();
2577                         break;
2578                 case CEE_CKFINITE: {
2579                         g_assert_not_reached ();
2580                         x86_push_reg (code, X86_EAX);
2581                         x86_fxam (code);
2582                         x86_fnstsw (code);
2583                         x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2584                         x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2585                         x86_pop_reg (code, X86_EAX);
2586                         EMIT_COND_SYSTEM_EXCEPTION (X86_CC_EQ, FALSE, "ArithmeticException");
2587                         break;
2588                 }
2589                 default:
2590                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2591                         g_assert_not_reached ();
2592                 }
2593
2594                 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2595                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2596                                    mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2597                         g_assert_not_reached ();
2598                 }
2599                
2600                 cpos += max_len;
2601
2602                 last_ins = ins;
2603                 last_offset = offset;
2604                 
2605                 ins = ins->next;
2606         }
2607
2608         cfg->code_len = code - cfg->native_code;
2609 }
2610
2611 void
2612 mono_arch_register_lowlevel_calls (void)
2613 {
2614         mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2615         mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2616 }
2617
2618 void
2619 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji)
2620 {
2621         MonoJumpInfo *patch_info;
2622
2623         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2624                 unsigned char *ip = patch_info->ip.i + code;
2625                 const unsigned char *target = NULL;
2626
2627                 switch (patch_info->type) {
2628                 case MONO_PATCH_INFO_BB:
2629                         target = patch_info->data.bb->native_offset + code;
2630                         break;
2631                 case MONO_PATCH_INFO_ABS:
2632                         target = patch_info->data.target;
2633                         break;
2634                 case MONO_PATCH_INFO_LABEL:
2635                         target = patch_info->data.inst->inst_c0 + code;
2636                         break;
2637                 case MONO_PATCH_INFO_IP:
2638                         *((gpointer *)(ip)) = ip;
2639                         continue;
2640                 case MONO_PATCH_INFO_INTERNAL_METHOD: {
2641                         MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
2642                         if (!mi) {
2643                                 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
2644                                 g_assert_not_reached ();
2645                         }
2646                         target = mi->wrapper;
2647                         break;
2648                 }
2649                 case MONO_PATCH_INFO_METHOD:
2650                         if (patch_info->data.method == method) {
2651                                 target = code;
2652                         } else {
2653                                 /* get the trampoline to the method from the domain */
2654                                 target = mono_arch_create_jit_trampoline (patch_info->data.method);
2655                         }
2656                         break;
2657                 case MONO_PATCH_INFO_SWITCH: {
2658                         gpointer *table = (gpointer *)patch_info->data.target;
2659                         int i;
2660
2661                         // FIXME: inspect code to get the register
2662                         ppc_load (ip, ppc_r11, patch_info->data.target);
2663                         //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
2664
2665                         for (i = 0; i < patch_info->table_size; i++) {
2666                                 table [i] = (int)patch_info->data.table [i] + code;
2667                         }
2668                         /* we put into the table the absolute address, no need for ppc_patch in this case */
2669                         continue;
2670                 }
2671                 case MONO_PATCH_INFO_METHODCONST:
2672                 case MONO_PATCH_INFO_CLASS:
2673                 case MONO_PATCH_INFO_IMAGE:
2674                 case MONO_PATCH_INFO_FIELD:
2675                         g_assert_not_reached ();
2676                         *((gconstpointer *)(ip + 1)) = patch_info->data.target;
2677                         continue;
2678                 case MONO_PATCH_INFO_R4:
2679                 case MONO_PATCH_INFO_R8:
2680                         g_assert_not_reached ();
2681                         *((gconstpointer *)(ip + 2)) = patch_info->data.target;
2682                         continue;
2683                 default:
2684                         g_assert_not_reached ();
2685                 }
2686                 ppc_patch (ip, target);
2687         }
2688 }
2689
2690 int
2691 mono_arch_max_epilog_size (MonoCompile *cfg)
2692 {
2693         int exc_count = 0, max_epilog_size = 16 + 20*4;
2694         MonoJumpInfo *patch_info;
2695         
2696         if (cfg->method->save_lmf)
2697                 max_epilog_size += 128;
2698         
2699         if (mono_jit_trace_calls != NULL)
2700                 max_epilog_size += 50;
2701
2702         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2703                 max_epilog_size += 50;
2704
2705         /* count the number of exception infos */
2706      
2707         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2708                 if (patch_info->type == MONO_PATCH_INFO_EXC)
2709                         exc_count++;
2710         }
2711
2712         /* 
2713          * make sure we have enough space for exceptions
2714          * 16 is the size of two push_imm instructions and a call
2715          */
2716         max_epilog_size += exc_count*16;
2717
2718         return max_epilog_size;
2719 }
2720
2721 guint8 *
2722 mono_arch_emit_prolog (MonoCompile *cfg)
2723 {
2724         MonoMethod *method = cfg->method;
2725         MonoBasicBlock *bb;
2726         MonoMethodSignature *sig;
2727         MonoInst *inst;
2728         int alloc_size, pos, max_offset, i;
2729         guint8 *code;
2730         CallInfo *cinfo;
2731
2732         cfg->code_size = 256;
2733         code = cfg->native_code = g_malloc (cfg->code_size);
2734
2735         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2736                 ppc_mflr (code, ppc_r0);
2737                 ppc_stw (code, ppc_r0, 8, ppc_sp);
2738         }
2739         if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
2740                 cfg->used_int_regs |= 1 << 31;
2741         }
2742
2743         alloc_size = cfg->stack_offset;
2744         pos = 0;
2745
2746         if (method->save_lmf) {
2747 #if 0
2748                 pos += sizeof (MonoLMF);
2749                 
2750                 /* save the current IP */
2751                 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2752                 x86_push_imm (code, 0);
2753
2754                 /* save all caller saved regs */
2755                 x86_push_reg (code, X86_EBX);
2756                 x86_push_reg (code, X86_EDI);
2757                 x86_push_reg (code, X86_ESI);
2758                 x86_push_reg (code, X86_EBP);
2759
2760                 /* save method info */
2761                 x86_push_imm (code, method);
2762         
2763                 /* get the address of lmf for the current thread */
2764                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
2765                                      (gpointer)"get_lmf_addr");
2766                 x86_call_code (code, 0);
2767
2768                 /* push lmf */
2769                 x86_push_reg (code, X86_EAX); 
2770                 /* push *lfm (previous_lmf) */
2771                 x86_push_membase (code, X86_EAX, 0);
2772                 /* *(lmf) = ESP */
2773                 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
2774 #endif
2775         } else {
2776
2777                 for (i = 13; i < 32; ++i) {
2778                         if (cfg->used_int_regs & (1 << i)) {
2779                                 pos += 4;
2780                                 ppc_stw (code, i, -pos, ppc_sp);
2781                         }
2782                 }
2783         }
2784
2785         alloc_size += pos;
2786         // align to 16 bytes
2787         if (alloc_size & (16 - 1))
2788                 alloc_size += 16 - (alloc_size & (16 - 1));
2789
2790         cfg->stack_usage = alloc_size;
2791         if (alloc_size)
2792                 ppc_stwu (code, ppc_sp, -alloc_size, ppc_sp);
2793         if (cfg->flags & MONO_CFG_HAS_ALLOCA)
2794                 ppc_mr (code, ppc_r31, ppc_sp);
2795
2796         /* compute max_offset in order to use short forward jumps */
2797         max_offset = 0;
2798         if (cfg->opt & MONO_OPT_BRANCH) {
2799                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2800                         MonoInst *ins = bb->code;
2801                         bb->max_offset = max_offset;
2802
2803                         if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2804                                 max_offset += 6; 
2805
2806                         while (ins) {
2807                                 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2808                                 ins = ins->next;
2809                         }
2810                 }
2811         }
2812
2813         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2814                 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
2815
2816         /* load arguments allocated to register from the stack */
2817         sig = method->signature;
2818         pos = 0;
2819
2820         cinfo = calculate_sizes (sig, sig->pinvoke);
2821
2822         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2823                 ArgInfo *ainfo = cinfo->args + i;
2824                 inst = cfg->varinfo [pos];
2825                 
2826                 if (inst->opcode == OP_REGVAR) {
2827                         g_assert (!ainfo->regtype); // fine for now
2828                         ppc_mr (code, inst->dreg, ainfo->reg);
2829                         if (cfg->verbose_level > 2)
2830                                 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
2831                 } else {
2832                         /* the argument should be put on the stack: FIXME handle size != word  */
2833                         ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2834                 }
2835                 pos++;
2836         }
2837
2838         cfg->code_len = code - cfg->native_code;
2839
2840         return code;
2841 }
2842
2843 void
2844 mono_arch_emit_epilog (MonoCompile *cfg)
2845 {
2846         MonoJumpInfo *patch_info;
2847         MonoMethod *method = cfg->method;
2848         int pos, i;
2849         guint8 *code;
2850
2851         code = cfg->native_code + cfg->code_len;
2852
2853         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2854                 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
2855
2856         
2857         pos = 0;
2858         
2859         if (method->save_lmf) {
2860                 pos = -sizeof (MonoLMF);
2861         }
2862
2863         if (method->save_lmf) {
2864 #if 0
2865                 /* ebx = previous_lmf */
2866                 x86_pop_reg (code, X86_EBX);
2867                 /* edi = lmf */
2868                 x86_pop_reg (code, X86_EDI);
2869                 /* *(lmf) = previous_lmf */
2870                 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
2871
2872                 /* discard method info */
2873                 x86_pop_reg (code, X86_ESI);
2874
2875                 /* restore caller saved regs */
2876                 x86_pop_reg (code, X86_EBP);
2877                 x86_pop_reg (code, X86_ESI);
2878                 x86_pop_reg (code, X86_EDI);
2879                 x86_pop_reg (code, X86_EBX);
2880 #endif
2881         }
2882
2883         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2884                 ppc_lwz (code, ppc_r0, cfg->stack_usage + 8, cfg->frame_reg);
2885                 ppc_mtlr (code, ppc_r0);
2886         }
2887         ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2888         for (i = 13; i < 32; ++i) {
2889                 if (cfg->used_int_regs & (1 << i)) {
2890                         pos += 4;
2891                         ppc_lwz (code, i, -pos, cfg->frame_reg);
2892                 }
2893         }
2894         ppc_blr (code);
2895
2896         /* add code to raise exceptions */
2897         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2898                 switch (patch_info->type) {
2899                 case MONO_PATCH_INFO_EXC:
2900                         /*x86_patch (patch_info->ip.i + cfg->native_code, code);
2901                         x86_push_imm (code, patch_info->data.target);
2902                         x86_push_imm (code, patch_info->ip.i + cfg->native_code);
2903                         patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2904                         patch_info->data.name = "throw_exception_by_name";
2905                         patch_info->ip.i = code - cfg->native_code;
2906                         x86_jump_code (code, 0);*/
2907                         break;
2908                 default:
2909                         /* do nothing */
2910                         break;
2911                 }
2912         }
2913
2914         cfg->code_len = code - cfg->native_code;
2915
2916         g_assert (cfg->code_len < cfg->code_size);
2917
2918 }