2003-09-26 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 #undef DEBUG
1203 #define DEBUG(a) if (cfg->verbose_level > 1) a
1204 //#define DEBUG(a)
1205 #define reg_is_freeable(r) ((r) >= 3 && (r) <= 10)
1206
1207 typedef struct {
1208         int born_in;
1209         int killed_in;
1210         int last_use;
1211         int prev_use;
1212 } RegTrack;
1213
1214 static const char*const * ins_spec = sparc_desc;
1215
1216 static void
1217 print_ins (int i, MonoInst *ins)
1218 {
1219         const char *spec = ins_spec [ins->opcode];
1220         g_print ("\t%-2d %s", i, mono_inst_name (ins->opcode));
1221         if (spec [MONO_INST_DEST]) {
1222                 if (ins->dreg >= MONO_MAX_IREGS)
1223                         g_print (" R%d <-", ins->dreg);
1224                 else
1225                         g_print (" %s <-", mono_arch_regname (ins->dreg));
1226         }
1227         if (spec [MONO_INST_SRC1]) {
1228                 if (ins->sreg1 >= MONO_MAX_IREGS)
1229                         g_print (" R%d", ins->sreg1);
1230                 else
1231                         g_print (" %s", mono_arch_regname (ins->sreg1));
1232         }
1233         if (spec [MONO_INST_SRC2]) {
1234                 if (ins->sreg2 >= MONO_MAX_IREGS)
1235                         g_print (" R%d", ins->sreg2);
1236                 else
1237                         g_print (" %s", mono_arch_regname (ins->sreg2));
1238         }
1239         if (spec [MONO_INST_CLOB])
1240                 g_print (" clobbers: %c", spec [MONO_INST_CLOB]);
1241         g_print ("\n");
1242 }
1243
1244 static void
1245 print_regtrack (RegTrack *t, int num)
1246 {
1247         int i;
1248         char buf [32];
1249         const char *r;
1250         
1251         for (i = 0; i < num; ++i) {
1252                 if (!t [i].born_in)
1253                         continue;
1254                 if (i >= MONO_MAX_IREGS) {
1255                         g_snprintf (buf, sizeof(buf), "R%d", i);
1256                         r = buf;
1257                 } else
1258                         r = mono_arch_regname (i);
1259                 g_print ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].last_use);
1260         }
1261 }
1262
1263 typedef struct InstList InstList;
1264
1265 struct InstList {
1266         InstList *prev;
1267         InstList *next;
1268         MonoInst *data;
1269 };
1270
1271 static inline InstList*
1272 inst_list_prepend (MonoMemPool *pool, InstList *list, MonoInst *data)
1273 {
1274         InstList *item = mono_mempool_alloc (pool, sizeof (InstList));
1275         item->data = data;
1276         item->prev = NULL;
1277         item->next = list;
1278         if (list)
1279                 list->prev = item;
1280         return item;
1281 }
1282
1283 /*
1284  * Force the spilling of the variable in the symbolic register 'reg'.
1285  */
1286 static int
1287 get_register_force_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, int reg)
1288 {
1289         MonoInst *load;
1290         int i, sel, spill;
1291         
1292         sel = cfg->rs->iassign [reg];
1293         /*i = cfg->rs->isymbolic [sel];
1294         g_assert (i == reg);*/
1295         i = reg;
1296         spill = ++cfg->spill_count;
1297         cfg->rs->iassign [i] = -spill - 1;
1298         mono_regstate_free_int (cfg->rs, sel);
1299         /* we need to create a spill var and insert a load to sel after the current instruction */
1300         MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1301         load->dreg = sel;
1302         load->inst_basereg = cfg->frame_reg;
1303         load->inst_offset = mono_spillvar_offset (cfg, spill);
1304         if (item->prev) {
1305                 while (ins->next != item->prev->data)
1306                         ins = ins->next;
1307         }
1308         load->next = ins->next;
1309         ins->next = load;
1310         DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1311         i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1312         g_assert (i == sel);
1313
1314         return sel;
1315 }
1316
1317 static int
1318 get_register_spilling (MonoCompile *cfg, InstList *item, MonoInst *ins, guint32 regmask, int reg)
1319 {
1320         MonoInst *load;
1321         int i, sel, spill;
1322
1323         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));
1324         /* exclude the registers in the current instruction */
1325         if (reg != ins->sreg1 && (reg_is_freeable (ins->sreg1) || (ins->sreg1 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg1] >= 0))) {
1326                 if (ins->sreg1 >= MONO_MAX_IREGS)
1327                         regmask &= ~ (1 << cfg->rs->iassign [ins->sreg1]);
1328                 else
1329                         regmask &= ~ (1 << ins->sreg1);
1330                 DEBUG (g_print ("excluding sreg1 %s\n", mono_arch_regname (ins->sreg1)));
1331         }
1332         if (reg != ins->sreg2 && (reg_is_freeable (ins->sreg2) || (ins->sreg2 >= MONO_MAX_IREGS && cfg->rs->iassign [ins->sreg2] >= 0))) {
1333                 if (ins->sreg2 >= MONO_MAX_IREGS)
1334                         regmask &= ~ (1 << cfg->rs->iassign [ins->sreg2]);
1335                 else
1336                         regmask &= ~ (1 << ins->sreg2);
1337                 DEBUG (g_print ("excluding sreg2 %s %d\n", mono_arch_regname (ins->sreg2), ins->sreg2));
1338         }
1339         if (reg != ins->dreg && reg_is_freeable (ins->dreg)) {
1340                 regmask &= ~ (1 << ins->dreg);
1341                 DEBUG (g_print ("excluding dreg %s\n", mono_arch_regname (ins->dreg)));
1342         }
1343
1344         DEBUG (g_print ("available regmask: 0x%08x\n", regmask));
1345         g_assert (regmask); /* need at least a register we can free */
1346         sel = -1;
1347         /* we should track prev_use and spill the register that's farther */
1348         for (i = 0; i < MONO_MAX_IREGS; ++i) {
1349                 if (regmask & (1 << i)) {
1350                         sel = i;
1351                         DEBUG (g_print ("selected register %s has assignment %d\n", mono_arch_regname (sel), cfg->rs->iassign [sel]));
1352                         break;
1353                 }
1354         }
1355         i = cfg->rs->isymbolic [sel];
1356         spill = ++cfg->spill_count;
1357         cfg->rs->iassign [i] = -spill - 1;
1358         mono_regstate_free_int (cfg->rs, sel);
1359         /* we need to create a spill var and insert a load to sel after the current instruction */
1360         MONO_INST_NEW (cfg, load, OP_LOAD_MEMBASE);
1361         load->dreg = sel;
1362         load->inst_basereg = cfg->frame_reg;
1363         load->inst_offset = mono_spillvar_offset (cfg, spill);
1364         if (item->prev) {
1365                 while (ins->next != item->prev->data)
1366                         ins = ins->next;
1367         }
1368         load->next = ins->next;
1369         ins->next = load;
1370         DEBUG (g_print ("SPILLED LOAD (%d at 0x%08x(%%sp)) R%d (freed %s)\n", spill, load->inst_offset, i, mono_arch_regname (sel)));
1371         i = mono_regstate_alloc_int (cfg->rs, 1 << sel);
1372         g_assert (i == sel);
1373         
1374         return sel;
1375 }
1376
1377 static MonoInst*
1378 create_copy_ins (MonoCompile *cfg, int dest, int src, MonoInst *ins)
1379 {
1380         MonoInst *copy;
1381         MONO_INST_NEW (cfg, copy, OP_MOVE);
1382         copy->dreg = dest;
1383         copy->sreg1 = src;
1384         if (ins) {
1385                 copy->next = ins->next;
1386                 ins->next = copy;
1387         }
1388         DEBUG (g_print ("\tforced copy from %s to %s\n", mono_arch_regname (src), mono_arch_regname (dest)));
1389         return copy;
1390 }
1391
1392 static MonoInst*
1393 create_spilled_store (MonoCompile *cfg, int spill, int reg, int prev_reg, MonoInst *ins)
1394 {
1395         MonoInst *store;
1396         MONO_INST_NEW (cfg, store, OP_STORE_MEMBASE_REG);
1397         store->sreg1 = reg;
1398         store->inst_destbasereg = sparc_sp;
1399         store->inst_offset = mono_spillvar_offset (cfg, spill);
1400         if (ins) {
1401                 store->next = ins->next;
1402                 ins->next = store;
1403         }
1404         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)));
1405         return store;
1406 }
1407
1408 static void
1409 insert_before_ins (MonoInst *ins, InstList *item, MonoInst* to_insert)
1410 {
1411         MonoInst *prev;
1412         g_assert (item->next);
1413         prev = item->next->data;
1414
1415         while (prev->next != ins)
1416                 prev = prev->next;
1417         to_insert->next = ins;
1418         prev->next = to_insert;
1419         /* 
1420          * needed otherwise in the next instruction we can add an ins to the 
1421          * end and that would get past this instruction.
1422          */
1423         item->data = to_insert; 
1424 }
1425
1426 static int
1427 alloc_int_reg (MonoCompile *cfg, InstList *curinst, MonoInst *ins, int sym_reg, guint32 allow_mask)
1428 {
1429         int val = cfg->rs->iassign [sym_reg];
1430         if (val < 0) {
1431                 int spill = 0;
1432                 if (val < -1) {
1433                         /* the register gets spilled after this inst */
1434                         spill = -val -1;
1435                 }
1436                 val = mono_regstate_alloc_int (cfg->rs, allow_mask);
1437                 if (val < 0)
1438                         val = get_register_spilling (cfg, curinst, ins, allow_mask, sym_reg);
1439                 cfg->rs->iassign [sym_reg] = val;
1440                 /* add option to store before the instruction for src registers */
1441                 if (spill)
1442                         create_spilled_store (cfg, spill, val, sym_reg, ins);
1443         }
1444         cfg->rs->isymbolic [val] = sym_reg;
1445         return val;
1446 }
1447
1448 /*
1449  * Local register allocation.
1450  * We first scan the list of instructions and we save the liveness info of
1451  * each register (when the register is first used, when it's value is set etc.).
1452  * We also reverse the list of instructions (in the InstList list) because assigning
1453  * registers backwards allows for more tricks to be used.
1454  */
1455 void
1456 mono_arch_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1457 {
1458         MonoInst *ins;
1459         MonoRegState *rs = cfg->rs;
1460         int i, val, fpcount;
1461         RegTrack *reginfo, *reginfof;
1462         RegTrack *reginfo1, *reginfo2, *reginfod;
1463         InstList *tmp, *reversed = NULL;
1464         const char *spec;
1465         guint32 src1_mask, src2_mask, dest_mask;
1466
1467         if (!bb->code)
1468                 return;
1469         rs->next_vireg = bb->max_ireg;
1470         rs->next_vfreg = bb->max_freg;
1471         mono_regstate_assign (rs);
1472         reginfo = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vireg);
1473         reginfof = mono_mempool_alloc0 (cfg->mempool, sizeof (RegTrack) * rs->next_vfreg);
1474         rs->ifree_mask = 0xdeadbeef; /* FIXME */
1475
1476         ins = bb->code;
1477         i = 1;
1478         fpcount = 0; /* FIXME: track fp stack utilization */
1479         DEBUG (g_print ("LOCAL regalloc: basic block: %d\n", bb->block_num));
1480         /* forward pass on the instructions to collect register liveness info */
1481         while (ins) {
1482                 spec = ins_spec [ins->opcode];
1483                 DEBUG (print_ins (i, ins));
1484                 if (spec [MONO_INST_SRC1]) {
1485                         if (spec [MONO_INST_SRC1] == 'f')
1486                                 reginfo1 = reginfof;
1487                         else
1488                                 reginfo1 = reginfo;
1489                         reginfo1 [ins->sreg1].prev_use = reginfo1 [ins->sreg1].last_use;
1490                         reginfo1 [ins->sreg1].last_use = i;
1491                 } else {
1492                         ins->sreg1 = -1;
1493                 }
1494                 if (spec [MONO_INST_SRC2]) {
1495                         if (spec [MONO_INST_SRC2] == 'f')
1496                                 reginfo2 = reginfof;
1497                         else
1498                                 reginfo2 = reginfo;
1499                         reginfo2 [ins->sreg2].prev_use = reginfo2 [ins->sreg2].last_use;
1500                         reginfo2 [ins->sreg2].last_use = i;
1501                 } else {
1502                         ins->sreg2 = -1;
1503                 }
1504                 if (spec [MONO_INST_DEST]) {
1505                         if (spec [MONO_INST_DEST] == 'f')
1506                                 reginfod = reginfof;
1507                         else
1508                                 reginfod = reginfo;
1509                         if (spec [MONO_INST_DEST] != 'b') /* it's not just a base register */
1510                                 reginfod [ins->dreg].killed_in = i;
1511                         reginfod [ins->dreg].prev_use = reginfod [ins->dreg].last_use;
1512                         reginfod [ins->dreg].last_use = i;
1513                         if (reginfod [ins->dreg].born_in == 0 || reginfod [ins->dreg].born_in > i)
1514                                 reginfod [ins->dreg].born_in = i;
1515                         if (spec [MONO_INST_DEST] == 'l') {
1516                                 /* result in eax:edx, the virtual register is allocated sequentially */
1517                                 reginfod [ins->dreg + 1].prev_use = reginfod [ins->dreg + 1].last_use;
1518                                 reginfod [ins->dreg + 1].last_use = i;
1519                                 if (reginfod [ins->dreg + 1].born_in == 0 || reginfod [ins->dreg + 1].born_in > i)
1520                                         reginfod [ins->dreg + 1].born_in = i;
1521                         }
1522                 } else {
1523                         ins->dreg = -1;
1524                 }
1525                 reversed = inst_list_prepend (cfg->mempool, reversed, ins);
1526                 ++i;
1527                 ins = ins->next;
1528         }
1529
1530         DEBUG (print_regtrack (reginfo, rs->next_vireg));
1531         DEBUG (print_regtrack (reginfof, rs->next_vfreg));
1532         tmp = reversed;
1533         while (tmp) {
1534                 int prev_dreg, prev_sreg1, prev_sreg2;
1535                 dest_mask = src1_mask = src2_mask = 0xdeadbeef; /* FIXME */
1536                 --i;
1537                 ins = tmp->data;
1538                 spec = ins_spec [ins->opcode];
1539                 DEBUG (g_print ("processing:"));
1540                 DEBUG (print_ins (i, ins));
1541                 /* update for use with FP regs... */
1542                 if (spec [MONO_INST_DEST] != 'f' && ins->dreg >= MONO_MAX_IREGS) {
1543                         val = rs->iassign [ins->dreg];
1544                         prev_dreg = ins->dreg;
1545                         if (val < 0) {
1546                                 int spill = 0;
1547                                 if (val < -1) {
1548                                         /* the register gets spilled after this inst */
1549                                         spill = -val -1;
1550                                 }
1551                                 val = mono_regstate_alloc_int (rs, dest_mask);
1552                                 if (val < 0)
1553                                         val = get_register_spilling (cfg, tmp, ins, dest_mask, ins->dreg);
1554                                 rs->iassign [ins->dreg] = val;
1555                                 if (spill)
1556                                         create_spilled_store (cfg, spill, val, prev_dreg, ins);
1557                         }
1558                         DEBUG (g_print ("\tassigned dreg %s to dest R%d\n", mono_arch_regname (val), ins->dreg));
1559                         rs->isymbolic [val] = prev_dreg;
1560                         ins->dreg = val;
1561                         if (spec [MONO_INST_DEST] == 'l') {
1562                                 int hreg = prev_dreg + 1;
1563                                 val = rs->iassign [hreg];
1564                                 if (val < 0) {
1565                                         int spill = 0;
1566                                         if (val < -1) {
1567                                                 /* the register gets spilled after this inst */
1568                                                 spill = -val -1;
1569                                         }
1570                                         val = mono_regstate_alloc_int (rs, dest_mask);
1571                                         if (val < 0)
1572                                                 val = get_register_spilling (cfg, tmp, ins, dest_mask, hreg);
1573                                         rs->iassign [hreg] = val;
1574                                         if (spill)
1575                                                 create_spilled_store (cfg, spill, val, hreg, ins);
1576                                 }
1577                                 DEBUG (g_print ("\tassigned hreg %s to dest R%d\n", mono_arch_regname (val), hreg));
1578                                 rs->isymbolic [val] = hreg;
1579                                 /* FIXME:? ins->dreg = val; */
1580                                 if (ins->dreg == sparc_l1) {
1581                                         if (val != sparc_l0)
1582                                                 create_copy_ins (cfg, val, sparc_l0, ins);
1583                                 } else if (ins->dreg == sparc_l0) {
1584                                         if (val == sparc_l1) {
1585                                                 /* swap */
1586                                                 create_copy_ins (cfg, sparc_l2, sparc_l0, ins);
1587                                                 create_copy_ins (cfg, sparc_l0, sparc_l1, ins);
1588                                                 create_copy_ins (cfg, sparc_l1, sparc_l2, ins);
1589                                         } else {
1590                                                 /* two forced copies */
1591                                                 create_copy_ins (cfg, val, sparc_l0, ins);
1592                                                 create_copy_ins (cfg, ins->dreg, sparc_l1, ins);
1593                                         }
1594                                 } else {
1595                                         if (val == sparc_l0) {
1596                                                 create_copy_ins (cfg, ins->dreg, sparc_l1, ins);
1597                                         } else {
1598                                                 /* two forced copies */
1599                                                 create_copy_ins (cfg, val, sparc_l0, ins);
1600                                                 create_copy_ins (cfg, ins->dreg, sparc_l1, ins);
1601                                         }
1602                                 }
1603                                 if (reg_is_freeable (val) && hreg >= 0 && reginfo [hreg].born_in >= i) {
1604                                         DEBUG (g_print ("\tfreeable %s (R%d)\n", mono_arch_regname (val), hreg));
1605                                         mono_regstate_free_int (rs, val);
1606                                 }
1607                         }
1608                 } else {
1609                         prev_dreg = -1;
1610                 }
1611                 if (spec [MONO_INST_DEST] != 'f' && reg_is_freeable (ins->dreg) && prev_dreg >= 0 && reginfo [prev_dreg].born_in >= i) {
1612                         DEBUG (g_print ("\tfreeable %s (R%d) (born in %d)\n", mono_arch_regname (ins->dreg), prev_dreg, reginfo [prev_dreg].born_in));
1613                         mono_regstate_free_int (rs, ins->dreg);
1614                 }
1615                 if (spec [MONO_INST_SRC1] != 'f' && ins->sreg1 >= MONO_MAX_IREGS) {
1616                         val = rs->iassign [ins->sreg1];
1617                         prev_sreg1 = ins->sreg1;
1618                         if (val < 0) {
1619                                 int spill = 0;
1620                                 if (val < -1) {
1621                                         /* the register gets spilled after this inst */
1622                                         spill = -val -1;
1623                                 }
1624                                 if (0 && ins->opcode == OP_MOVE) {
1625                                         /* 
1626                                          * small optimization: the dest register is already allocated
1627                                          * but the src one is not: we can simply assign the same register
1628                                          * here and peephole will get rid of the instruction later.
1629                                          * This optimization may interfere with the clobbering handling:
1630                                          * it removes a mov operation that will be added again to handle clobbering.
1631                                          * There are also some other issues that should with make testjit.
1632                                          */
1633                                         mono_regstate_alloc_int (rs, 1 << ins->dreg);
1634                                         val = rs->iassign [ins->sreg1] = ins->dreg;
1635                                         //g_assert (val >= 0);
1636                                         DEBUG (g_print ("\tfast assigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1637                                 } else {
1638                                         //g_assert (val == -1); /* source cannot be spilled */
1639                                         val = mono_regstate_alloc_int (rs, src1_mask);
1640                                         if (val < 0)
1641                                                 val = get_register_spilling (cfg, tmp, ins, src1_mask, ins->sreg1);
1642                                         rs->iassign [ins->sreg1] = val;
1643                                         DEBUG (g_print ("\tassigned sreg1 %s to R%d\n", mono_arch_regname (val), ins->sreg1));
1644                                 }
1645                                 if (spill) {
1646                                         MonoInst *store = create_spilled_store (cfg, spill, val, prev_sreg1, NULL);
1647                                         insert_before_ins (ins, tmp, store);
1648                                 }
1649                         }
1650                         rs->isymbolic [val] = prev_sreg1;
1651                         ins->sreg1 = val;
1652                 } else {
1653                         prev_sreg1 = -1;
1654                 }
1655                 if (spec [MONO_INST_SRC2] != 'f' && ins->sreg2 >= MONO_MAX_IREGS) {
1656                         val = rs->iassign [ins->sreg2];
1657                         prev_sreg2 = ins->sreg2;
1658                         if (val < 0) {
1659                                 int spill = 0;
1660                                 if (val < -1) {
1661                                         /* the register gets spilled after this inst */
1662                                         spill = -val -1;
1663                                 }
1664                                 val = mono_regstate_alloc_int (rs, src2_mask);
1665                                 if (val < 0)
1666                                         val = get_register_spilling (cfg, tmp, ins, src2_mask, ins->sreg2);
1667                                 rs->iassign [ins->sreg2] = val;
1668                                 DEBUG (g_print ("\tassigned sreg2 %s to R%d\n", mono_arch_regname (val), ins->sreg2));
1669                                 if (spill)
1670                                         create_spilled_store (cfg, spill, val, prev_sreg2, ins);
1671                         }
1672                         rs->isymbolic [val] = prev_sreg2;
1673                         ins->sreg2 = val;
1674                 } else {
1675                         prev_sreg2 = -1;
1676                 }
1677
1678                 if (spec [MONO_INST_CLOB] == 'c') {
1679                         int j, s;
1680                         guint32 clob_mask = 0xdeadbeef; /* FIXME */
1681                         for (j = 0; j < MONO_MAX_IREGS; ++j) {
1682                                 s = 1 << j;
1683                                 if ((clob_mask & s) && !(rs->ifree_mask & s) && j != ins->sreg1) {
1684                                         //g_warning ("register %s busy at call site\n", mono_arch_regname (j));
1685                                 }
1686                         }
1687                 }
1688                 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
1689                         DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg1)));
1690                         mono_regstate_free_int (rs, ins->sreg1);
1691                 }
1692                 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
1693                         DEBUG (g_print ("freeable %s\n", mono_arch_regname (ins->sreg2)));
1694                         mono_regstate_free_int (rs, ins->sreg2);
1695                 }*/
1696                 
1697                 //DEBUG (print_ins (i, ins));
1698                 tmp = tmp->next;
1699         }
1700 }
1701
1702 static guchar*
1703 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int size, gboolean is_signed)
1704 {
1705         return code;
1706 }
1707
1708 static unsigned char*
1709 mono_emit_stack_alloc (guchar *code, MonoInst* tree)
1710 {
1711         return code;
1712 }
1713
1714 void
1715 sparc_patch (guchar *code, guchar *target)
1716 {
1717         guint32 ins = *(guint32*)code;
1718         guint32 prim = ins >> 26;
1719
1720 //      g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1721         if (prim == 18) {
1722                 // absolute address
1723                 if (ins & 2) {
1724                         guint32 li = (guint32)target;
1725                         ins = prim << 26 | (ins & 3);
1726                         ins |= li;
1727                         // FIXME: assert the top bits of li are 0
1728                 } else {
1729                         gint diff = target - code;
1730                         ins = prim << 26 | (ins & 3);
1731                         diff &= ~3;
1732                         diff &= ~(63 << 26);
1733                         ins |= diff;
1734                 }
1735                 *(guint32*)code = ins;
1736         } else if (prim == 16) {
1737                 // absolute address
1738                 if (ins & 2) {
1739                         guint32 li = (guint32)target;
1740                         ins = (ins & 0xffff0000) | (ins & 3);
1741                         li &= 0xffff;
1742                         ins |= li;
1743                         // FIXME: assert the top bits of li are 0
1744                 } else {
1745                         gint diff = target - code;
1746                         ins = (ins & 0xffff0000) | (ins & 3);
1747                         diff &= 0xffff;
1748                         ins |= diff;
1749                 }
1750                 *(guint32*)code = ins;
1751         } else {
1752                 g_assert_not_reached ();
1753         }
1754 //      g_print ("patched with 0x%08x\n", ins);
1755 }
1756
1757 /*
1758  * Some conventions used in the following code.
1759  * 1) We're assuming a V9 CPU.  We will check for that later.
1760  * In reality, we're mostly sticking with V8 instructions...
1761  * 2) The only scratch registers we have are o7 and g1.  We try to
1762  * stick to o7 when we can, and use g1 when necessary.
1763  */
1764
1765 void
1766 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1767 {
1768         MonoInst *ins;
1769         MonoCallInst *call;
1770         guint offset;
1771         guint8 *code = cfg->native_code + cfg->code_len;
1772         MonoInst *last_ins = NULL;
1773         guint last_offset = 0;
1774         int max_len, cpos;
1775
1776         if (cfg->opt & MONO_OPT_PEEPHOLE)
1777                 peephole_pass (cfg, bb);
1778
1779 #if 0
1780         /* 
1781          * various stratgies to align BBs. Using real loop detection or simply
1782          * aligning every block leads to more consistent benchmark results,
1783          * but usually slows down the code
1784          * we should do the alignment outside this function or we should adjust
1785          * bb->native offset as well or the code is effectively slowed down!
1786          */
1787         /* align all blocks */
1788 //      if ((pad = (cfg->code_len & (align - 1)))) {
1789         /* poor man loop start detection */
1790 //      if (bb->code && bb->in_count && bb->in_bb [0]->cil_code > bb->cil_code && (pad = (cfg->code_len & (align - 1)))) {
1791         /* consider real loop detection and nesting level */
1792 //      if (bb->loop_blocks && bb->nesting < 3 && (pad = (cfg->code_len & (align - 1)))) {
1793         /* consider real loop detection */
1794         if (/*bb->loop_blocks &&*/ (pad = (cfg->code_len & (align - 1)))) {
1795                 pad = align - pad;
1796                 x86_padding (code, pad);
1797                 cfg->code_len += pad;
1798                 bb->native_offset = cfg->code_len;
1799         }
1800 #endif
1801
1802         if (cfg->verbose_level > 2)
1803                 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
1804
1805         cpos = bb->max_offset;
1806
1807         if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1808                 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
1809                 //g_assert (!mono_compile_aot);
1810                 //cpos += 6;
1811                 //if (bb->cil_code)
1812                 //      cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
1813                 /* this is not thread save, but good enough */
1814                 /* fixme: howto handle overflows? */
1815                 //x86_inc_mem (code, &cov->data [bb->dfn].count); 
1816         }
1817
1818         ins = bb->code;
1819         while (ins) {
1820                 offset = code - cfg->native_code;
1821
1822                 max_len = ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
1823
1824                 if (offset > (cfg->code_size - max_len - 16)) {
1825                         cfg->code_size *= 2;
1826                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1827                         code = cfg->native_code + offset;
1828                 }
1829         //      if (ins->cil_code)
1830         //              g_print ("cil code\n");
1831
1832                 switch (ins->opcode) {
1833                 case OP_STOREI1_MEMBASE_IMM:
1834                         sparc_set (code, ins->inst_imm, sparc_o7);
1835 //                      g_assert (ppc_is_imm16 (ins->inst_offset));
1836                         sparc_stb_imm (code, sparc_o7, ins->inst_offset, ins->inst_destbasereg);
1837                         break;
1838                 case OP_STOREI2_MEMBASE_IMM:
1839                         sparc_set (code, ins->inst_imm, sparc_o7);
1840 //                      g_assert (ppc_is_imm16 (ins->inst_offset));
1841                         sparc_sth_imm (code, sparc_o7, ins->inst_offset, ins->inst_destbasereg);
1842                         break;
1843                 case OP_STORE_MEMBASE_IMM:
1844                 case OP_STOREI4_MEMBASE_IMM:
1845                         sparc_set (code, ins->inst_imm, sparc_o7);
1846 //                      g_assert (ppc_is_imm16 (ins->inst_offset));
1847                         sparc_st_imm (code, sparc_o7, ins->inst_offset, ins->inst_destbasereg);
1848                         break;
1849                 case OP_STOREI1_MEMBASE_REG:
1850 //                      g_assert (ppc_is_imm16 (ins->inst_offset));
1851                         sparc_stb_imm (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1852                         break;
1853                 case OP_STOREI2_MEMBASE_REG:
1854 //                      g_assert (ppc_is_imm16 (ins->inst_offset));
1855                         sparc_sth (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1856                         break;
1857                 case OP_STORE_MEMBASE_REG:
1858                 case OP_STOREI4_MEMBASE_REG:
1859 //                      g_assert (ppc_is_imm16 (ins->inst_offset));
1860                         sparc_st (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
1861                         break;
1862                 case CEE_LDIND_I:
1863                 case CEE_LDIND_I4:
1864                 case CEE_LDIND_U4:
1865                         sparc_ld (code, ins->inst_p0, sparc_g0, ins->dreg);
1866                         break;
1867                 /* The cast IS BAD (maybe).  But it needs to be done... */
1868                 case OP_LOADU4_MEM:
1869                         sparc_set (code, (guint)ins->inst_p0, ins->dreg);
1870                         sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
1871                         break;
1872                 case OP_LOAD_MEMBASE:
1873                 case OP_LOADI4_MEMBASE:
1874                 case OP_LOADU4_MEMBASE:
1875                         if (TRUE) { /* FIXME */
1876                                 sparc_ld_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1877                         } else {
1878                                 sparc_ld (code, sparc_l0, 0, ins->inst_offset);
1879                                 sparc_ld (code, ins->dreg, 0, sparc_l0);
1880                         }
1881                         break;
1882                 case OP_LOADU1_MEMBASE:
1883 //                      g_assert (ppc_is_imm16 (ins->inst_offset));
1884                         sparc_ldub_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1885                         break;
1886                 case OP_LOADI1_MEMBASE:
1887                   //    g_assert (ppc_is_imm16 (ins->inst_offset));
1888                         sparc_ldsb_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1889                         break;
1890                 case OP_LOADU2_MEMBASE:
1891                   //    g_assert (ppc_is_imm16 (ins->inst_offset));
1892                         sparc_lduh_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1893                         break;
1894                 case OP_LOADI2_MEMBASE:
1895                         sparc_ldsh_imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg);
1896                         break;
1897                 case CEE_CONV_I1:
1898                         sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
1899                         sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
1900                         break;
1901                 case CEE_CONV_I2:
1902                         sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
1903                         sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
1904                         break;
1905                 /* GCC does this one differently.  Don't ask me WHY. */
1906                 case CEE_CONV_U1:
1907                         sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
1908                         break;
1909                 case CEE_CONV_U2:
1910                         sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
1911                         sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
1912                         break;
1913                 case OP_COMPARE:
1914                         sparc_cmp (code, ins->sreg1, ins->sreg2);
1915                         break;
1916                 case OP_COMPARE_IMM:
1917                         if (TRUE) { /* FIXME */
1918                                 sparc_cmp_imm (code, ins->sreg1, (ins->inst_imm & 0x1fff));
1919                         } else {
1920                                 sparc_set (code, ins->inst_imm, sparc_o7);
1921                                 sparc_cmp (code, ins->sreg1, sparc_o7);
1922                         }
1923                         break;
1924                 case OP_X86_TEST_NULL:
1925                         sparc_cmp_imm (code, ins->sreg1, 0);
1926                         break;
1927                 case CEE_BREAK:
1928                         g_assert_not_reached();
1929                         break;
1930                 case OP_ADDCC:
1931                         sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
1932                         break;
1933                 case CEE_ADD:
1934                         sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1935                         break;
1936                 case OP_ADC:
1937                         sparc_addx (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1938                         break;
1939                 case OP_ADD_IMM:
1940                         if (TRUE) { /* FIXME */
1941                                 sparc_add_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1942                         } else {
1943                                 sparc_ld (code, sparc_l0, 0, ins->inst_imm);
1944                                 sparc_add (code, 0, ins->dreg, ins->sreg1, sparc_l0);
1945                         }
1946                         break;
1947                 case OP_ADC_IMM:
1948                         sparc_addx (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1949                         break;
1950                 case OP_SUBCC:
1951                         sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
1952                         break;
1953                 case CEE_SUB:
1954                         sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1955                         break;
1956                 case OP_SBB:
1957                         sparc_subx (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1958                         break;
1959                 case OP_SUB_IMM:
1960                         // we add the negated value
1961                   //    g_assert (ppc_is_imm16 (-ins->inst_imm));
1962                         sparc_add_imm (code, FALSE, ins->sreg1, -ins->inst_imm, ins->dreg);
1963                         break;
1964                 case OP_SBB_IMM:
1965                         sparc_subx_imm (code, FALSE, ins->sreg2, ins->inst_imm, ins->dreg);
1966                         break;
1967                 case CEE_AND:
1968                         sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1969                         break;
1970                 case OP_AND_IMM:
1971                         sparc_and_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1972                         break;
1973                 case CEE_DIV:
1974                         sparc_sdiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1975                         break;
1976                 case CEE_DIV_UN:
1977                         sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1978                         break;
1979                 case OP_DIV_IMM:
1980                         sparc_sdiv_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
1981                         break;
1982                 case CEE_REM:
1983                         sparc_sdiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
1984                         sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
1985                         sparc_sub (code, FALSE, sparc_o7, ins->sreg1, ins->dreg);
1986                         break;
1987                 case CEE_REM_UN:
1988                         sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
1989                         sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
1990                         sparc_sub (code, FALSE, sparc_o7, ins->sreg1, ins->dreg);
1991                         break;
1992                 case OP_REM_IMM:
1993                         sparc_sdiv_imm (code, FALSE, ins->sreg1, ins->inst_imm, sparc_o7);
1994                         sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
1995                         sparc_sub (code, FALSE, sparc_o7, ins->sreg1, ins->dreg);
1996                         break;
1997                 case CEE_OR:
1998                         sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
1999                         break;
2000                 case OP_OR_IMM:
2001                         sparc_set (code, ins->inst_imm, sparc_o7);
2002                         sparc_or (code, FALSE, ins->sreg2, sparc_o7, ins->dreg);
2003                         break;
2004                 case CEE_XOR:
2005                         sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2006                         break;
2007                 case OP_XOR_IMM:
2008                         sparc_set (code, ins->inst_imm, sparc_o7);
2009                         sparc_xor (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2010                         break;
2011                 case CEE_SHL:
2012                         sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
2013                         break;
2014                 case OP_SHL_IMM:
2015                         sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2016                         break;
2017                 case CEE_SHR:
2018                         sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
2019                         break;
2020                 case OP_SHR_IMM:
2021                         sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2022                         break;
2023                 case OP_SHR_UN_IMM:
2024                         sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2025                         break;
2026                 case CEE_SHR_UN:
2027                         sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
2028                         break;
2029                 case CEE_NOT:
2030                         /* can't use sparc_not */
2031                         sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
2032                         break;
2033                 case CEE_NEG:
2034                         /* can't use sparc_neg */
2035                         sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
2036                         break;
2037                 case CEE_MUL:
2038                         sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2039                         break;
2040                 case OP_MUL_IMM:
2041                         sparc_smul_imm (code, FALSE, ins->sreg1, ins->inst_imm, ins->dreg);
2042                         break;
2043                 case CEE_MUL_OVF:
2044                         /* FIXME: this isn't right, I don't think */
2045                         sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2046                         break;
2047                 case CEE_MUL_OVF_UN:
2048                         /* FIXME: This isn't right either */
2049                         sparc_umul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2050                         break;
2051                 case OP_ICONST:
2052                 case OP_SETREGIMM:
2053                         sparc_set (code, ins->inst_c0, ins->dreg);
2054                         break;
2055                 /*case OP_CLASS:
2056                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_CLASS, (gpointer)ins->inst_c0);
2057                         break;
2058                 case OP_IMAGE:
2059                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_IMAGE, (gpointer)ins->inst_c0);
2060                         break;*/
2061                 case CEE_CONV_I4:
2062                 case CEE_CONV_U4:
2063                 case OP_MOVE:
2064                 case OP_SETREG:
2065                         sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2066                         break;
2067                 case CEE_JMP:
2068                         g_assert_not_reached ();
2069                         /*
2070                          * Copied roughly from x86.  Probably doesn't work
2071                          */
2072
2073                         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2074                                 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
2075                         /* reset offset to make max_len work */
2076                         offset = code - cfg->native_code;
2077
2078                         g_assert (!cfg->method->save_lmf);
2079
2080                         offset = code - cfg->native_code;
2081                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2082                         sparc_jmp_imm (code, sparc_g0, 0);
2083
2084                         break;
2085                 case OP_CHECK_THIS:
2086                         /* ensure ins->sreg1 is not NULL */
2087                         sparc_cmp_imm (code, ins->sreg1, 0);
2088                         break;
2089                 case OP_FCALL:
2090                 case OP_LCALL:
2091                 case OP_VCALL:
2092                 case OP_VOIDCALL:
2093                 case CEE_CALL:
2094                         call = (MonoCallInst*)ins;
2095                         if (ins->flags & MONO_INST_HAS_METHOD)
2096                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
2097                         else
2098                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
2099                         sparc_call_simple (code, 0);
2100                         break;
2101                 case OP_FCALL_REG:
2102                 case OP_LCALL_REG:
2103                 case OP_VCALL_REG:
2104                 case OP_VOIDCALL_REG:
2105                 case OP_CALL_REG:
2106                         call = (MonoCallInst*)ins;
2107                         sparc_call (code, ins->sreg1, sparc_g0);
2108                         /* FIXME: yea, a store in g0 is a GOOD IDEA */
2109                         if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2110                                 sparc_add_imm (code, FALSE, sparc_sp, call->stack_usage, sparc_g0);
2111                         break;
2112                 case OP_FCALL_MEMBASE:
2113                 case OP_LCALL_MEMBASE:
2114                 case OP_VCALL_MEMBASE:
2115                 case OP_VOIDCALL_MEMBASE:
2116                 case OP_CALL_MEMBASE:
2117                         call = (MonoCallInst*)ins;
2118                         sparc_call_imm (code, ins->sreg1, ins->inst_offset);
2119                         /* FIXME: yea, a store in g0 is a GOOD IDEA */
2120                         if (call->stack_usage && (call->signature->call_convention != MONO_CALL_STDCALL))
2121                                 sparc_add_imm (code, FALSE, sparc_sp, call->stack_usage, sparc_g0);
2122                         break;
2123
2124                         break;
2125                 case OP_OUTARG:
2126                         /* FIXME: This can be SO far wrong! */
2127                         sparc_st (code, ins->sreg1, sparc_g0, sparc_sp);
2128                         sparc_sub_imm (code, FALSE, sparc_sp, 4, sparc_sp);
2129                         break;
2130                 case OP_LOCALLOC:
2131                         /* keep alignment */
2132 #define MONO_FRAME_ALIGNMENT 32
2133                         sparc_add_imm (code, 0, ins->sreg1, MONO_FRAME_ALIGNMENT-1, sparc_o7);
2134                         //ppc_rlwinm (code, sparc_l0, sparc_l0, 0, 0, 27);
2135                         //ppc_lwz (code, sparc_l0, 0, ppc_sp);
2136                         // Fix semantics to negate to another reg? FIXME
2137                         //sparc_neg (code, sparc_l0, sparc_l0);
2138                         //ppc_stwux (code, ppc_sp, sparc_l0, ppc_sp);
2139                         //ppc_mr (code, ins->dreg, ppc_sp);
2140                         g_assert_not_reached ();
2141                         break;
2142                 case CEE_RET:
2143                         sparc_ret (code);
2144                         break;
2145                 case CEE_THROW: {
2146                         sparc_mov_reg_reg (code, ins->sreg1, sparc_sp);
2147                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
2148                                              (gpointer)"throw_exception");
2149                         sparc_call_simple (code, 0);
2150                         break;
2151                 }
2152                 case OP_ENDFILTER:
2153                         if (ins->sreg1 != sparc_sp)
2154                                 sparc_mov_reg_reg (code, ins->sreg1, sparc_sp);
2155                         sparc_ret (code);
2156                         break;
2157                 case CEE_ENDFINALLY:
2158                         sparc_ret (code);
2159                         break;
2160                 case OP_CALL_HANDLER: 
2161                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2162                         sparc_call_simple (code, 0);
2163                         break;
2164                 case OP_LABEL:
2165                         ins->inst_c0 = code - cfg->native_code;
2166                         break;
2167                 case CEE_BR:
2168                         //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
2169                         //if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
2170                         //break;
2171                         if (ins->flags & MONO_INST_BRLABEL) {
2172                                 if (ins->inst_i0->inst_c0) {
2173                                         sparc_call_simple (code, 0);
2174                                 } else {
2175                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
2176                                         sparc_call_simple (code, 0);
2177                                 }
2178                         } else {
2179                                 if (ins->inst_target_bb->native_offset) {
2180                                         sparc_call_simple (code, 0);
2181                                 } else {
2182                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
2183                                         sparc_call_simple (code, 0);
2184                                 } 
2185                         }
2186                         break;
2187                 case OP_BR_REG:
2188                         sparc_jmp (code, ins->sreg1, sparc_g0);
2189                         break;
2190                 case OP_CEQ:
2191                         //ppc_li (code, ins->dreg, 0);
2192                         //ppc_bc (code, PPC_BR_FALSE, PPC_BR_EQ, 2);
2193                         //ppc_li (code, ins->dreg, 1);
2194                         break;
2195                 case OP_CLT:
2196                 case OP_CLT_UN:
2197                         //ppc_li (code, ins->dreg, 1);
2198                         //ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2199                         //ppc_li (code, ins->dreg, 0);
2200                         break;
2201                 case OP_CGT:
2202                 case OP_CGT_UN:
2203                         //ppc_li (code, ins->dreg, 1);
2204                         //ppc_bc (code, PPC_BR_TRUE, PPC_BR_LT, 2);
2205                         //ppc_li (code, ins->dreg, 0);
2206                         break;
2207                 case OP_COND_EXC_EQ:
2208                 case OP_COND_EXC_NE_UN:
2209                 case OP_COND_EXC_LT:
2210                 case OP_COND_EXC_LT_UN:
2211                 case OP_COND_EXC_GT:
2212                 case OP_COND_EXC_GT_UN:
2213                 case OP_COND_EXC_GE:
2214                 case OP_COND_EXC_GE_UN:
2215                 case OP_COND_EXC_LE:
2216                 case OP_COND_EXC_LE_UN:
2217                 case OP_COND_EXC_OV:
2218                 case OP_COND_EXC_NO:
2219                 case OP_COND_EXC_C:
2220                 case OP_COND_EXC_NC:
2221                         //EMIT_COND_SYSTEM_EXCEPTION (branch_cc_table [ins->opcode - OP_COND_EXC_EQ], 
2222                         //                          (ins->opcode < OP_COND_EXC_NE_UN), ins->inst_p1);
2223                         break;
2224                 case CEE_BEQ:
2225                 case CEE_BNE_UN:
2226                 case CEE_BLT:
2227                 case CEE_BLT_UN:
2228                 case CEE_BGT:
2229                 case CEE_BGT_UN:
2230                 case CEE_BGE:
2231                 case CEE_BGE_UN:
2232                 case CEE_BLE:
2233                 case CEE_BLE_UN:
2234                         EMIT_COND_BRANCH (ins, ins->opcode - CEE_BEQ);
2235                         break;
2236
2237                 /* floating point opcodes */
2238                 case OP_R8CONST:
2239                         //ppc_load (code, sparc_l0, ins->inst_p0);
2240                         //ppc_lfd (code, ins->dreg, 0, sparc_l0);
2241                         break;
2242                 case OP_R4CONST:
2243                         //ppc_load (code, sparc_l0, ins->inst_p0);
2244                         //ppc_lfs (code, ins->dreg, 0, sparc_l0);
2245                         break;
2246                 case OP_STORER8_MEMBASE_REG:
2247                         //ppc_stfd (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2248                         break;
2249                 case OP_LOADR8_MEMBASE:
2250                         //ppc_lfd (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2251                         break;
2252                 case OP_STORER4_MEMBASE_REG:
2253                         //ppc_stfs (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg);
2254                         break;
2255                 case OP_LOADR4_MEMBASE:
2256                         //ppc_lfs (code, ins->dreg, ins->inst_offset, ins->inst_basereg);
2257                         break;
2258                 case CEE_CONV_R4: /* FIXME: change precision */
2259                 case CEE_CONV_R8:
2260                         g_assert_not_reached ();
2261                         //x86_push_reg (code, ins->sreg1);
2262                         //x86_fild_membase (code, X86_ESP, 0, FALSE);
2263                         //x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
2264                         break;
2265                 case OP_X86_FP_LOAD_I8:
2266                         g_assert_not_reached ();
2267                         //x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, TRUE);
2268                         break;
2269                 case OP_X86_FP_LOAD_I4:
2270                         g_assert_not_reached ();
2271                         //x86_fild_membase (code, ins->inst_basereg, ins->inst_offset, FALSE);
2272                         break;
2273                 case OP_FCONV_TO_I1:
2274                         g_assert_not_reached ();
2275                         code = emit_float_to_int (cfg, code, ins->dreg, 1, TRUE);
2276                         break;
2277                 case OP_FCONV_TO_U1:
2278                         g_assert_not_reached ();
2279                         code = emit_float_to_int (cfg, code, ins->dreg, 1, FALSE);
2280                         break;
2281                 case OP_FCONV_TO_I2:
2282                         g_assert_not_reached ();
2283                         code = emit_float_to_int (cfg, code, ins->dreg, 2, TRUE);
2284                         break;
2285                 case OP_FCONV_TO_U2:
2286                         g_assert_not_reached ();
2287                         code = emit_float_to_int (cfg, code, ins->dreg, 2, FALSE);
2288                         break;
2289                 case OP_FCONV_TO_I4:
2290                 case OP_FCONV_TO_I:
2291                         g_assert_not_reached ();
2292                         code = emit_float_to_int (cfg, code, ins->dreg, 4, TRUE);
2293                         break;
2294                 case OP_FCONV_TO_U4:
2295                 case OP_FCONV_TO_U:
2296                         g_assert_not_reached ();
2297                         code = emit_float_to_int (cfg, code, ins->dreg, 4, FALSE);
2298                         break;
2299                 case OP_FCONV_TO_I8:
2300                 case OP_FCONV_TO_U8:
2301                         g_assert_not_reached ();
2302                         /*x86_alu_reg_imm (code, X86_SUB, X86_ESP, 4);
2303                         x86_fnstcw_membase(code, X86_ESP, 0);
2304                         x86_mov_reg_membase (code, ins->inst_dreg_low, X86_ESP, 0, 2);
2305                         x86_alu_reg_imm (code, X86_OR, ins->inst_dreg_low, 0xc00);
2306                         x86_mov_membase_reg (code, X86_ESP, 2, ins->inst_dreg_low, 2);
2307                         x86_fldcw_membase (code, X86_ESP, 2);
2308                         x86_alu_reg_imm (code, X86_SUB, X86_ESP, 8);
2309                         x86_fist_pop_membase (code, X86_ESP, 0, TRUE);
2310                         x86_pop_reg (code, ins->inst_dreg_low);
2311                         x86_pop_reg (code, ins->inst_dreg_high);
2312                         x86_fldcw_membase (code, X86_ESP, 0);
2313                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);*/
2314                         break;
2315                 case OP_LCONV_TO_R_UN: { 
2316 #if 0
2317                         static guint8 mn[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x40 };
2318                         guint8 *br;
2319
2320                         /* load 64bit integer to FP stack */
2321                         x86_push_imm (code, 0);
2322                         x86_push_reg (code, ins->sreg2);
2323                         x86_push_reg (code, ins->sreg1);
2324                         x86_fild_membase (code, X86_ESP, 0, TRUE);
2325                         /* store as 80bit FP value */
2326                         x86_fst80_membase (code, X86_ESP, 0);
2327                         
2328                         /* test if lreg is negative */
2329                         x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2330                         br = code; x86_branch8 (code, X86_CC_GEZ, 0, TRUE);
2331         
2332                         /* add correction constant mn */
2333                         x86_fld80_mem (code, mn);
2334                         x86_fld80_membase (code, X86_ESP, 0);
2335                         x86_fp_op_reg (code, X86_FADD, 1, TRUE);
2336                         x86_fst80_membase (code, X86_ESP, 0);
2337
2338                         x86_patch (br, code);
2339
2340                         x86_fld80_membase (code, X86_ESP, 0);
2341                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
2342 #endif
2343                         g_assert_not_reached ();
2344                         break;
2345                 }
2346                 case OP_LCONV_TO_OVF_I: {
2347 #if 0
2348                         guint8 *br [3], *label [1];
2349
2350                         /* 
2351                          * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
2352                          */
2353                         x86_test_reg_reg (code, ins->sreg1, ins->sreg1);
2354
2355                         /* If the low word top bit is set, see if we are negative */
2356                         br [0] = code; x86_branch8 (code, X86_CC_LT, 0, TRUE);
2357                         /* We are not negative (no top bit set, check for our top word to be zero */
2358                         x86_test_reg_reg (code, ins->sreg2, ins->sreg2);
2359                         br [1] = code; x86_branch8 (code, X86_CC_EQ, 0, TRUE);
2360                         label [0] = code;
2361
2362                         /* throw exception */
2363                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_EXC, "OverflowException");
2364                         x86_jump32 (code, 0);
2365         
2366                         x86_patch (br [0], code);
2367                         /* our top bit is set, check that top word is 0xfffffff */
2368                         x86_alu_reg_imm (code, X86_CMP, ins->sreg2, 0xffffffff);
2369                 
2370                         x86_patch (br [1], code);
2371                         /* nope, emit exception */
2372                         br [2] = code; x86_branch8 (code, X86_CC_NE, 0, TRUE);
2373                         x86_patch (br [2], label [0]);
2374
2375                         if (ins->dreg != ins->sreg1)
2376                                 x86_mov_reg_reg (code, ins->dreg, ins->sreg1, 4);
2377 #endif
2378                         g_assert_not_reached ();
2379                         break;
2380                 }
2381                 case OP_FADD:
2382                   sparc_fadds( code, ins->sreg1, ins->sreg2, ins->dreg); 
2383                         break;
2384                 case OP_FSUB:
2385                   sparc_fsubs( code, ins->sreg1, ins->sreg2, ins->dreg );
2386                         break;          
2387                 case OP_FMUL:
2388                   sparc_fmuls( code, ins->sreg1, ins->sreg2, ins->dreg );
2389                         break;          
2390                 case OP_FDIV:
2391                   sparc_fdivs( code, ins->sreg1, ins->sreg2, ins->dreg );
2392                         break;          
2393                 case OP_FNEG:
2394                   sparc_fnegs( code, ins->sreg1, ins->sreg2, ins->dreg );
2395                         break;          
2396                 case OP_FREM:
2397                         g_assert_not_reached ();
2398                         break;
2399                 case OP_FCOMPARE:
2400                         sparc_fcmps( code, ins->sreg1, ins->sreg2 );
2401                         /* this overwrites EAX */
2402                         //EMIT_FPCOMPARE(code);
2403                         break;
2404                 case OP_FCEQ:
2405                         g_assert_not_reached();
2406                         /*if (ins->dreg != X86_EAX) 
2407                                 x86_push_reg (code, X86_EAX);
2408
2409                         EMIT_FPCOMPARE(code);
2410                         x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x4000);
2411                         x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2412                         x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2413
2414                         if (ins->dreg != X86_EAX) 
2415                                 x86_pop_reg (code, X86_EAX);*/
2416                         break;
2417                 case OP_FCLT:
2418                 case OP_FCLT_UN:
2419                         g_assert_not_reached ();
2420                         /*if (ins->dreg != X86_EAX) 
2421                                 x86_push_reg (code, X86_EAX);
2422
2423                         EMIT_FPCOMPARE(code);
2424                         x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2425                         x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2426
2427                         if (ins->dreg != X86_EAX) 
2428                                 x86_pop_reg (code, X86_EAX);*/
2429                         break;
2430                 case OP_FCGT:
2431                 case OP_FCGT_UN:
2432                         g_assert_not_reached ();
2433                         /*if (ins->dreg != X86_EAX) 
2434                                 x86_push_reg (code, X86_EAX);
2435
2436                         EMIT_FPCOMPARE(code);
2437                         x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2438                         x86_set_reg (code, X86_CC_EQ, ins->dreg, TRUE);
2439                         x86_widen_reg (code, ins->dreg, ins->dreg, FALSE, FALSE);
2440
2441                         if (ins->dreg != X86_EAX) 
2442                                 x86_pop_reg (code, X86_EAX);*/
2443                         break;
2444                 case OP_FBEQ:
2445                         g_assert_not_reached ();
2446                         break;
2447                 case OP_FBNE_UN:
2448                         g_assert_not_reached ();
2449                         break;
2450                 case OP_FBLT:
2451                 case OP_FBLT_UN:
2452                         g_assert_not_reached ();
2453                         break;
2454                 case OP_FBGT:
2455                 case OP_FBGT_UN:
2456                         g_assert_not_reached ();
2457                         break;
2458                 case OP_FBGE:
2459                 case OP_FBGE_UN:
2460                         g_assert_not_reached ();
2461                         break;
2462                 case OP_FBLE:
2463                 case OP_FBLE_UN:
2464                         g_assert_not_reached ();
2465                         break;
2466                 case CEE_CKFINITE: {
2467                         /* SO FIXME */
2468                         g_assert_not_reached ();
2469                         //x86_push_reg (code, X86_EAX);
2470                         //x86_fxam (code);
2471                         //x86_fnstsw (code);
2472                         //x86_alu_reg_imm (code, X86_AND, X86_EAX, 0x4100);
2473                         //x86_alu_reg_imm (code, X86_CMP, X86_EAX, 0x0100);
2474                         //x86_pop_reg (code, X86_EAX);
2475                         //EMIT_COND_SYSTEM_EXCEPTION (FALSE, FALSE, "ArithmeticException");
2476                         break;
2477                 }
2478                 default:
2479                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2480                         g_assert_not_reached ();
2481                 }
2482
2483                 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
2484                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2485                                    mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
2486                         g_assert_not_reached ();
2487                 }
2488                
2489                 cpos += max_len;
2490
2491                 last_ins = ins;
2492                 last_offset = offset;
2493                 
2494                 ins = ins->next;
2495         }
2496
2497         cfg->code_len = code - cfg->native_code;
2498 }
2499
2500 void
2501 mono_arch_register_lowlevel_calls (void)
2502 {
2503         mono_register_jit_icall (enter_method, "mono_enter_method", NULL, TRUE);
2504         mono_register_jit_icall (leave_method, "mono_leave_method", NULL, TRUE);
2505 }
2506
2507 void
2508 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji)
2509 {
2510         MonoJumpInfo *patch_info;
2511
2512         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2513                 unsigned char *ip = patch_info->ip.i + code;
2514                 const unsigned char *target = NULL;
2515
2516                 switch (patch_info->type) {
2517                 case MONO_PATCH_INFO_BB:
2518                         target = patch_info->data.bb->native_offset + code;
2519                         break;
2520                 case MONO_PATCH_INFO_ABS:
2521                         target = patch_info->data.target;
2522                         break;
2523                 case MONO_PATCH_INFO_LABEL:
2524                         target = patch_info->data.inst->inst_c0 + code;
2525                         break;
2526                 case MONO_PATCH_INFO_IP:
2527                         *((gpointer *)(ip)) = ip;
2528                         continue;
2529                 case MONO_PATCH_INFO_INTERNAL_METHOD: {
2530                         MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
2531                         if (!mi) {
2532                                 g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
2533                                 g_assert_not_reached ();
2534                         }
2535                         target = mi->wrapper;
2536                         break;
2537                 }
2538                 case MONO_PATCH_INFO_METHOD:
2539                         if (patch_info->data.method == method) {
2540                                 target = code;
2541                         } else {
2542                                 /* get the trampoline to the method from the domain */
2543                                 target = mono_arch_create_jit_trampoline (patch_info->data.method);
2544                         }
2545                         break;
2546                 case MONO_PATCH_INFO_SWITCH: {
2547                         gpointer *table = (gpointer *)patch_info->data.target;
2548                         int i;
2549
2550                         // FIXME: inspect code to get the register
2551                         //ppc_load (ip, sparc_l0, patch_info->data.target);
2552                         //*((gconstpointer *)(ip + 2)) = patch_info->data.target;
2553
2554                         for (i = 0; i < patch_info->table_size; i++) {
2555                                 table [i] = (int)patch_info->data.table [i] + code;
2556                         }
2557                         /* we put into the table the absolute address, no need for sparc_patch in this case */
2558                         continue;
2559                 }
2560                 case MONO_PATCH_INFO_METHODCONST:
2561                 case MONO_PATCH_INFO_CLASS:
2562                 case MONO_PATCH_INFO_IMAGE:
2563                 case MONO_PATCH_INFO_FIELD:
2564                         g_assert_not_reached ();
2565                         *((gconstpointer *)(ip + 1)) = patch_info->data.target;
2566                         continue;
2567                 case MONO_PATCH_INFO_R4:
2568                 case MONO_PATCH_INFO_R8:
2569                         g_assert_not_reached ();
2570                         *((gconstpointer *)(ip + 2)) = patch_info->data.target;
2571                         continue;
2572                 default:
2573                         g_assert_not_reached ();
2574                 }
2575                 sparc_patch (ip, target);
2576         }
2577 }
2578
2579 int
2580 mono_arch_max_epilog_size (MonoCompile *cfg)
2581 {
2582         int exc_count = 0, max_epilog_size = 16 + 20*4;
2583         MonoJumpInfo *patch_info;
2584         
2585         if (cfg->method->save_lmf)
2586                 max_epilog_size += 128;
2587         
2588         if (mono_jit_trace_calls)
2589                 max_epilog_size += 50;
2590
2591         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2592                 max_epilog_size += 50;
2593
2594         /* count the number of exception infos */
2595      
2596         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2597                 if (patch_info->type == MONO_PATCH_INFO_EXC)
2598                         exc_count++;
2599         }
2600
2601         /* 
2602          * make sure we have enough space for exceptions
2603          * 16 is the size of two push_imm instructions and a call
2604          */
2605         max_epilog_size += exc_count*16;
2606
2607         return max_epilog_size;
2608 }
2609
2610 guint8 *
2611 mono_arch_emit_prolog (MonoCompile *cfg)
2612 {
2613         MonoMethod *method = cfg->method;
2614         MonoBasicBlock *bb;
2615         MonoMethodSignature *sig;
2616         MonoInst *inst;
2617         int alloc_size, pos, max_offset, i;
2618         guint8 *code;
2619         CallInfo *cinfo;
2620
2621         cfg->code_size = 256;
2622         code = cfg->native_code = g_malloc (cfg->code_size);
2623
2624         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2625                 //ppc_mflr (code, sparc_l0);
2626                 //ppc_stw (code, sparc_l0, 8, ppc_sp);
2627         }
2628         if (cfg->flags & MONO_CFG_HAS_ALLOCA) {
2629                 cfg->used_int_regs |= 1 << 31;
2630         }
2631
2632         alloc_size = cfg->stack_offset;
2633         pos = 0;
2634
2635         if (method->save_lmf) {
2636 #if 0
2637                 pos += sizeof (MonoLMF);
2638                 
2639                 /* save the current IP */
2640                 mono_add_patch_info (cfg, code + 1 - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2641                 x86_push_imm (code, 0);
2642
2643                 /* save all caller saved regs */
2644                 x86_push_reg (code, X86_EBX);
2645                 x86_push_reg (code, X86_EDI);
2646                 x86_push_reg (code, X86_ESI);
2647                 x86_push_reg (code, X86_EBP);
2648
2649                 /* save method info */
2650                 x86_push_imm (code, method);
2651         
2652                 /* get the address of lmf for the current thread */
2653                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
2654                                      (gpointer)"get_lmf_addr");
2655                 x86_call_code (code, 0);
2656
2657                 /* push lmf */
2658                 x86_push_reg (code, X86_EAX); 
2659                 /* push *lfm (previous_lmf) */
2660                 x86_push_membase (code, X86_EAX, 0);
2661                 /* *(lmf) = ESP */
2662                 x86_mov_membase_reg (code, X86_EAX, 0, X86_ESP, 4);
2663 #endif
2664         } else {
2665
2666                 for (i = 13; i < 32; ++i) {
2667                         if (cfg->used_int_regs & (1 << i)) {
2668                                 pos += 4;
2669                                 //ppc_stw (code, i, -pos, ppc_sp);
2670                         }
2671                 }
2672         }
2673
2674         alloc_size += pos;
2675         // align to 16 bytes
2676         if (alloc_size & (16 - 1))
2677                 alloc_size += 16 - (alloc_size & (16 - 1));
2678
2679         cfg->stack_usage = alloc_size;
2680         if (alloc_size)
2681                 //ppc_stwu (code, sparc_sp, -alloc_size, sparc_sp);
2682         if (cfg->flags & MONO_CFG_HAS_ALLOCA)
2683                 //ppc_mr (code, sparc_l7, sparc_sp);
2684
2685         /* compute max_offset in order to use short forward jumps */
2686         max_offset = 0;
2687         if (cfg->opt & MONO_OPT_BRANCH) {
2688                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2689                         MonoInst *ins = bb->code;
2690                         bb->max_offset = max_offset;
2691
2692                         if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2693                                 max_offset += 6; 
2694
2695                         while (ins) {
2696                                 max_offset += ((guint8 *)ins_spec [ins->opcode])[MONO_INST_LEN];
2697                                 ins = ins->next;
2698                         }
2699                 }
2700         }
2701
2702         if (mono_jit_trace_calls)
2703                 code = mono_arch_instrument_prolog (cfg, enter_method, code, TRUE);
2704
2705         /* load arguments allocated to register from the stack */
2706         sig = method->signature;
2707         pos = 0;
2708
2709         cinfo = calculate_sizes (sig, sig->pinvoke);
2710
2711         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2712                 ArgInfo *ainfo = cinfo->args + i;
2713                 inst = cfg->varinfo [pos];
2714                 
2715                 if (inst->opcode == OP_REGVAR) {
2716                         g_assert (!ainfo->regtype); // fine for now
2717                         //ppc_mr (code, inst->dreg, ainfo->reg);
2718                         if (cfg->verbose_level > 2)
2719                                 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
2720                 } else {
2721                         /* the argument should be put on the stack: FIXME handle size != word  */
2722                         //ppc_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2723                 }
2724                 pos++;
2725         }
2726
2727         cfg->code_len = code - cfg->native_code;
2728
2729         return code;
2730 }
2731
2732 void
2733 mono_arch_emit_epilog (MonoCompile *cfg)
2734 {
2735         MonoJumpInfo *patch_info;
2736         MonoMethod *method = cfg->method;
2737         int pos, i;
2738         guint8 *code;
2739
2740         code = cfg->native_code + cfg->code_len;
2741
2742         if (mono_jit_trace_calls)
2743                 code = mono_arch_instrument_epilog (cfg, leave_method, code, TRUE);
2744
2745         
2746         pos = 0;
2747         
2748         if (method->save_lmf) {
2749                 pos = -sizeof (MonoLMF);
2750         }
2751
2752         if (method->save_lmf) {
2753 #if 0
2754                 /* ebx = previous_lmf */
2755                 x86_pop_reg (code, X86_EBX);
2756                 /* edi = lmf */
2757                 x86_pop_reg (code, X86_EDI);
2758                 /* *(lmf) = previous_lmf */
2759                 x86_mov_membase_reg (code, X86_EDI, 0, X86_EBX, 4);
2760
2761                 /* discard method info */
2762                 x86_pop_reg (code, X86_ESI);
2763
2764                 /* restore caller saved regs */
2765                 x86_pop_reg (code, X86_EBP);
2766                 x86_pop_reg (code, X86_ESI);
2767                 x86_pop_reg (code, X86_EDI);
2768                 x86_pop_reg (code, X86_EBX);
2769 #endif
2770         }
2771
2772         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2773                 //ppc_lwz (code, sparc_l0, cfg->stack_usage + 8, cfg->frame_reg);
2774                 //ppc_mtlr (code, sparc_l0);
2775         }
2776         //ppc_addic (code, ppc_sp, cfg->frame_reg, cfg->stack_usage);
2777         for (i = 13; i < 32; ++i) {
2778                 if (cfg->used_int_regs & (1 << i)) {
2779                         pos += 4;
2780                         //ppc_lwz (code, i, -pos, cfg->frame_reg);
2781                 }
2782         }
2783         //ppc_blr (code);
2784
2785         /* add code to raise exceptions */
2786         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2787                 switch (patch_info->type) {
2788                 case MONO_PATCH_INFO_EXC:
2789                         /*x86_patch (patch_info->ip.i + cfg->native_code, code);
2790                         x86_push_imm (code, patch_info->data.target);
2791                         x86_push_imm (code, patch_info->ip.i + cfg->native_code);
2792                         patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2793                         patch_info->data.name = "throw_exception_by_name";
2794                         patch_info->ip.i = code - cfg->native_code;
2795                         x86_jump_code (code, 0);*/
2796                         break;
2797                 default:
2798                         /* do nothing */
2799                         break;
2800                 }
2801         }
2802
2803         cfg->code_len = code - cfg->native_code;
2804
2805         g_assert (cfg->code_len < cfg->code_size);
2806
2807 }