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