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