2009-04-20 Rodrigo Kumpera <rkumpera@novell.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 #include <pthread.h>
18 #include <unistd.h>
19
20 #ifndef __linux__
21 #include <sys/systeminfo.h>
22 #include <thread.h>
23 #endif
24
25 #include <unistd.h>
26 #include <sys/mman.h>
27
28 #include <mono/metadata/appdomain.h>
29 #include <mono/metadata/debug-helpers.h>
30 #include <mono/metadata/tokentype.h>
31 #include <mono/utils/mono-math.h>
32
33 #include "mini-sparc.h"
34 #include "trace.h"
35 #include "cpu-sparc.h"
36 #include "jit-icalls.h"
37 #include "ir-emit.h"
38
39 /*
40  * Sparc V9 means two things:
41  * - the instruction set
42  * - the ABI
43  *
44  * V9 instructions are only usable if the underlying processor is 64 bit. Most Sparc 
45  * processors in use are 64 bit processors. The V9 ABI is only usable if the 
46  * mono executable is a 64 bit executable. So it would make sense to use the 64 bit
47  * instructions without using the 64 bit ABI.
48  */
49
50 /*
51  * Register usage:
52  * - %i0..%i<n> hold the incoming arguments, these are never written by JITted 
53  * code. Unused input registers are used for global register allocation.
54  * - %o0..%o5 and %l7 is used for local register allocation and passing arguments
55  * - %l0..%l6 is used for global register allocation
56  * - %o7 and %g1 is used as scratch registers in opcodes
57  * - all floating point registers are used for local register allocation except %f0. 
58  *   Only double precision registers are used.
59  * In 64 bit mode:
60  * - fp registers %d0..%d30 are used for parameter passing, and %d32..%d62 are
61  *   used for local allocation.
62  */
63
64 /*
65  * Alignment:
66  * - doubles and longs must be stored in dword aligned locations
67  */
68
69 /*
70  * The following things are not implemented or do not work:
71  *  - some fp arithmetic corner cases
72  * The following tests in mono/mini are expected to fail:
73  *  - test_0_simple_double_casts
74  *      This test casts (guint64)-1 to double and then back to guint64 again.
75  *    Under x86, it returns 0, while under sparc it returns -1.
76  *
77  * In addition to this, the runtime requires the trunc function, or its 
78  * solaris counterpart, aintl, to do some double->int conversions. If this 
79  * function is not available, it is emulated somewhat, but the results can be
80  * strange.
81  */
82
83 /*
84  * SPARCV9 FIXME:
85  * - optimize sparc_set according to the memory model
86  * - when non-AOT compiling, compute patch targets immediately so we don't
87  *   have to emit the 6 byte template.
88  * - varags
89  * - struct arguments/returns
90  */
91
92 /*
93  * SPARCV9 ISSUES:
94  * - sparc_call_simple can't be used in a lot of places since the displacement
95  *   might not fit into an imm30.
96  * - g1 can't be used in a lot of places since it is used as a scratch reg in
97  *   sparc_set.
98  * - sparc_f0 can't be used as a scratch register on V9
99  * - the %d34..%d62 fp registers are encoded as: %dx = %f(x - 32 + 1), ie.
100  *   %d36 = %f5.
101  * - ldind.i4/u4 needs to sign extend/clear out upper word -> slows things down
102  * - ins->dreg can't be used as a scatch register in r4 opcodes since it might
103  *   be a double precision register which has no single precision part.
104  * - passing/returning structs is hard to implement, because:
105  *   - the spec is very hard to understand
106  *   - it requires knowledge about the fields of structure, needs to handle
107  *     nested structures etc.
108  */
109
110 /*
111  * Possible optimizations:
112  * - delay slot scheduling
113  * - allocate large constants to registers
114  * - add more mul/div/rem optimizations
115  */
116
117 #ifndef __linux__
118 #define MONO_SPARC_THR_TLS 1
119 #endif
120
121 /*
122  * There was a 64 bit bug in glib-2.2: g_bit_nth_msf (0, -1) would return 32,
123  * causing infinite loops in dominator computation. So glib-2.4 is required.
124  */
125 #ifdef SPARCV9
126 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 4
127 #error "glib 2.4 or later is required for 64 bit mode."
128 #endif
129 #endif
130
131 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
132
133 #define SIGNAL_STACK_SIZE (64 * 1024)
134
135 #define STACK_BIAS MONO_SPARC_STACK_BIAS
136
137 #ifdef SPARCV9
138
139 /* %g1 is used by sparc_set */
140 #define GP_SCRATCH_REG sparc_g4
141 /* %f0 is used for parameter passing */
142 #define FP_SCRATCH_REG sparc_f30
143 #define ARGS_OFFSET (STACK_BIAS + 128)
144
145 #else
146
147 #define FP_SCRATCH_REG sparc_f0
148 #define ARGS_OFFSET 68
149 #define GP_SCRATCH_REG sparc_g1
150
151 #endif
152
153 /* Whenever the CPU supports v9 instructions */
154 static gboolean sparcv9 = FALSE;
155
156 /* Whenever this is a 64bit executable */
157 #if SPARCV9
158 static gboolean v64 = TRUE;
159 #else
160 static gboolean v64 = FALSE;
161 #endif
162
163 static gpointer mono_arch_get_lmf_addr (void);
164
165 const char*
166 mono_arch_regname (int reg) {
167         static const char * rnames[] = {
168                 "sparc_g0", "sparc_g1", "sparc_g2", "sparc_g3", "sparc_g4",
169                 "sparc_g5", "sparc_g6", "sparc_g7", "sparc_o0", "sparc_o1",
170                 "sparc_o2", "sparc_o3", "sparc_o4", "sparc_o5", "sparc_sp",
171                 "sparc_call", "sparc_l0", "sparc_l1", "sparc_l2", "sparc_l3",
172                 "sparc_l4", "sparc_l5", "sparc_l6", "sparc_l7", "sparc_i0",
173                 "sparc_i1", "sparc_i2", "sparc_i3", "sparc_i4", "sparc_i5",
174                 "sparc_fp", "sparc_retadr"
175         };
176         if (reg >= 0 && reg < 32)
177                 return rnames [reg];
178         return "unknown";
179 }
180
181 const char*
182 mono_arch_fregname (int reg) {
183         static const char *rnames [] = {
184                 "sparc_f0", "sparc_f1", "sparc_f2", "sparc_f3", "sparc_f4", 
185                 "sparc_f5", "sparc_f6", "sparc_f7", "sparc_f8", "sparc_f9",
186                 "sparc_f10", "sparc_f11", "sparc_f12", "sparc_f13", "sparc_f14", 
187                 "sparc_f15", "sparc_f16", "sparc_f17", "sparc_f18", "sparc_f19",
188                 "sparc_f20", "sparc_f21", "sparc_f22", "sparc_f23", "sparc_f24", 
189                 "sparc_f25", "sparc_f26", "sparc_f27", "sparc_f28", "sparc_f29",
190                 "sparc_f30", "sparc_f31"
191         };
192
193         if (reg >= 0 && reg < 32)
194                 return rnames [reg];
195         else
196                 return "unknown";
197 }
198
199 /*
200  * Initialize the cpu to execute managed code.
201  */
202 void
203 mono_arch_cpu_init (void)
204 {
205         guint32 dummy;
206         /* make sure sparcv9 is initialized for embedded use */
207         mono_arch_cpu_optimizazions(&dummy);
208 }
209
210 /*
211  * Initialize architecture specific code.
212  */
213 void
214 mono_arch_init (void)
215 {
216 }
217
218 /*
219  * Cleanup architecture specific code.
220  */
221 void
222 mono_arch_cleanup (void)
223 {
224 }
225
226 /*
227  * This function returns the optimizations supported on this cpu.
228  */
229 guint32
230 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
231 {
232         char buf [1024];
233         guint32 opts = 0;
234
235         *exclude_mask = 0;
236
237 #ifndef __linux__
238         if (!sysinfo (SI_ISALIST, buf, 1024))
239                 g_assert_not_reached ();
240 #else
241         /* From glibc.  If the getpagesize is 8192, we're on sparc64, which
242          * (in)directly implies that we're a v9 or better.
243          * Improvements to this are greatly accepted...
244          * Also, we don't differentiate between v7 and v8.  I sense SIGILL
245          * sniffing in my future.  
246          */
247         if (getpagesize() == 8192)
248                 strcpy (buf, "sparcv9");
249         else
250                 strcpy (buf, "sparcv8");
251 #endif
252
253         /* 
254          * On some processors, the cmov instructions are even slower than the
255          * normal ones...
256          */
257         if (strstr (buf, "sparcv9")) {
258                 opts |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
259                 sparcv9 = TRUE;
260         }
261         else
262                 *exclude_mask |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
263
264         return opts;
265 }
266
267 #ifdef __GNUC__
268 #define flushi(addr)    __asm__ __volatile__ ("iflush %0"::"r"(addr):"memory")
269 #else /* assume Sun's compiler */
270 static void flushi(void *addr)
271 {
272     asm("flush %i0");
273 }
274 #endif
275
276 #ifndef __linux__
277 void sync_instruction_memory(caddr_t addr, int len);
278 #endif
279
280 void
281 mono_arch_flush_icache (guint8 *code, gint size)
282 {
283 #ifndef __linux__
284         /* Hopefully this is optimized based on the actual CPU */
285         sync_instruction_memory (code, size);
286 #else
287         gulong start = (gulong) code;
288         gulong end = start + size;
289         gulong align;
290
291         /* Sparcv9 chips only need flushes on 32 byte
292          * cacheline boundaries.
293          *
294          * Sparcv8 needs a flush every 8 bytes.
295          */
296         align = (sparcv9 ? 32 : 8);
297
298         start &= ~(align - 1);
299         end = (end + (align - 1)) & ~(align - 1);
300
301         while (start < end) {
302 #ifdef __GNUC__
303                 __asm__ __volatile__ ("iflush %0"::"r"(start));
304 #else
305                 flushi (start);
306 #endif
307                 start += align;
308         }
309 #endif
310 }
311
312 /*
313  * mono_sparc_flushw:
314  *
315  * Flush all register windows to memory. Every register window is saved to
316  * a 16 word area on the stack pointed to by its %sp register.
317  */
318 void
319 mono_sparc_flushw (void)
320 {
321         static guint32 start [64];
322         static int inited = 0;
323         guint32 *code;
324         static void (*flushw) (void);
325
326         if (!inited) {
327                 code = start;
328
329                 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
330                 sparc_flushw (code);
331                 sparc_ret (code);
332                 sparc_restore_simple (code);
333
334                 g_assert ((code - start) < 64);
335
336                 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
337
338                 flushw = (gpointer)start;
339
340                 inited = 1;
341         }
342
343         flushw ();
344 }
345
346 void
347 mono_arch_flush_register_windows (void)
348 {
349         mono_sparc_flushw ();
350 }
351
352 gboolean 
353 mono_arch_is_inst_imm (gint64 imm)
354 {
355         return sparc_is_imm13 (imm);
356 }
357
358 gboolean 
359 mono_sparc_is_v9 (void) {
360         return sparcv9;
361 }
362
363 gboolean 
364 mono_sparc_is_sparc64 (void) {
365         return v64;
366 }
367
368 typedef enum {
369         ArgInIReg,
370         ArgInIRegPair,
371         ArgInSplitRegStack,
372         ArgInFReg,
373         ArgInFRegPair,
374         ArgOnStack,
375         ArgOnStackPair,
376         ArgInFloatReg,  /* V9 only */
377         ArgInDoubleReg  /* V9 only */
378 } ArgStorage;
379
380 typedef struct {
381         gint16 offset;
382         /* This needs to be offset by %i0 or %o0 depending on caller/callee */
383         gint8  reg;
384         ArgStorage storage;
385         guint32 vt_offset; /* for valuetypes */
386 } ArgInfo;
387
388 typedef struct {
389         int nargs;
390         guint32 stack_usage;
391         guint32 reg_usage;
392         ArgInfo ret;
393         ArgInfo sig_cookie;
394         ArgInfo args [1];
395 } CallInfo;
396
397 #define DEBUG(a)
398
399 /* %o0..%o5 */
400 #define PARAM_REGS 6
401
402 static void inline
403 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean pair)
404 {
405         ainfo->offset = *stack_size;
406
407         if (!pair) {
408                 if (*gr >= PARAM_REGS) {
409                         ainfo->storage = ArgOnStack;
410                 }
411                 else {
412                         ainfo->storage = ArgInIReg;
413                         ainfo->reg = *gr;
414                         (*gr) ++;
415                 }
416
417                 /* Allways reserve stack space for parameters passed in registers */
418                 (*stack_size) += sizeof (gpointer);
419         }
420         else {
421                 if (*gr < PARAM_REGS - 1) {
422                         /* A pair of registers */
423                         ainfo->storage = ArgInIRegPair;
424                         ainfo->reg = *gr;
425                         (*gr) += 2;
426                 }
427                 else if (*gr >= PARAM_REGS) {
428                         /* A pair of stack locations */
429                         ainfo->storage = ArgOnStackPair;
430                 }
431                 else {
432                         ainfo->storage = ArgInSplitRegStack;
433                         ainfo->reg = *gr;
434                         (*gr) ++;
435                 }
436
437                 (*stack_size) += 2 * sizeof (gpointer);
438         }
439 }
440
441 #ifdef SPARCV9
442
443 #define FLOAT_PARAM_REGS 32
444
445 static void inline
446 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean single)
447 {
448         ainfo->offset = *stack_size;
449
450         if (single) {
451                 if (*gr >= FLOAT_PARAM_REGS) {
452                         ainfo->storage = ArgOnStack;
453                 }
454                 else {
455                         /* A single is passed in an even numbered fp register */
456                         ainfo->storage = ArgInFloatReg;
457                         ainfo->reg = *gr + 1;
458                         (*gr) += 2;
459                 }
460         }
461         else {
462                 if (*gr < FLOAT_PARAM_REGS) {
463                         /* A double register */
464                         ainfo->storage = ArgInDoubleReg;
465                         ainfo->reg = *gr;
466                         (*gr) += 2;
467                 }
468                 else {
469                         ainfo->storage = ArgOnStack;
470                 }
471         }
472
473         (*stack_size) += sizeof (gpointer);
474 }
475
476 #endif
477
478 /*
479  * get_call_info:
480  *
481  *  Obtain information about a call according to the calling convention.
482  * For V8, see the "System V ABI, Sparc Processor Supplement" Sparc V8 version 
483  * document for more information.
484  * For V9, see the "Low Level System Information (64-bit psABI)" chapter in
485  * the 'Sparc Compliance Definition 2.4' document.
486  */
487 static CallInfo*
488 get_call_info (MonoCompile *cfg, MonoMethodSignature *sig, gboolean is_pinvoke)
489 {
490         guint32 i, gr, fr;
491         int n = sig->hasthis + sig->param_count;
492         guint32 stack_size = 0;
493         CallInfo *cinfo;
494         MonoType *ret_type;
495         MonoGenericSharingContext *gsctx = cfg ? cfg->generic_sharing_context : NULL;
496
497         cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
498
499         gr = 0;
500         fr = 0;
501
502 #ifdef SPARCV9
503         if (MONO_TYPE_ISSTRUCT ((sig->ret))) {
504                 /* The address of the return value is passed in %o0 */
505                 add_general (&gr, &stack_size, &cinfo->ret, FALSE);
506                 cinfo->ret.reg += sparc_i0;
507         }
508 #endif
509
510         /* this */
511         if (sig->hasthis)
512                 add_general (&gr, &stack_size, cinfo->args + 0, FALSE);
513
514         if ((sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
515                 gr = PARAM_REGS;
516
517                 /* Emit the signature cookie just before the implicit arguments */
518                 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
519         }
520
521         for (i = 0; i < sig->param_count; ++i) {
522                 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
523                 MonoType *ptype;
524
525                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
526                         gr = PARAM_REGS;
527
528                         /* Emit the signature cookie just before the implicit arguments */
529                         add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
530                 }
531
532                 DEBUG(printf("param %d: ", i));
533                 if (sig->params [i]->byref) {
534                         DEBUG(printf("byref\n"));
535                         
536                         add_general (&gr, &stack_size, ainfo, FALSE);
537                         continue;
538                 }
539                 ptype = mono_type_get_underlying_type (sig->params [i]);
540                 ptype = mini_get_basic_type_from_generic (gsctx, ptype);
541                 switch (ptype->type) {
542                 case MONO_TYPE_BOOLEAN:
543                 case MONO_TYPE_I1:
544                 case MONO_TYPE_U1:
545                         add_general (&gr, &stack_size, ainfo, FALSE);
546                         /* the value is in the ls byte */
547                         ainfo->offset += sizeof (gpointer) - 1;
548                         break;
549                 case MONO_TYPE_I2:
550                 case MONO_TYPE_U2:
551                 case MONO_TYPE_CHAR:
552                         add_general (&gr, &stack_size, ainfo, FALSE);
553                         /* the value is in the ls word */
554                         ainfo->offset += sizeof (gpointer) - 2;
555                         break;
556                 case MONO_TYPE_I4:
557                 case MONO_TYPE_U4:
558                         add_general (&gr, &stack_size, ainfo, FALSE);
559                         /* the value is in the ls dword */
560                         ainfo->offset += sizeof (gpointer) - 4;
561                         break;
562                 case MONO_TYPE_I:
563                 case MONO_TYPE_U:
564                 case MONO_TYPE_PTR:
565                 case MONO_TYPE_FNPTR:
566                 case MONO_TYPE_CLASS:
567                 case MONO_TYPE_OBJECT:
568                 case MONO_TYPE_STRING:
569                 case MONO_TYPE_SZARRAY:
570                 case MONO_TYPE_ARRAY:
571                         add_general (&gr, &stack_size, ainfo, FALSE);
572                         break;
573                 case MONO_TYPE_GENERICINST:
574                         if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
575                                 add_general (&gr, &stack_size, ainfo, FALSE);
576                                 break;
577                         }
578                         /* Fall through */
579                 case MONO_TYPE_VALUETYPE:
580 #ifdef SPARCV9
581                         if (sig->pinvoke)
582                                 NOT_IMPLEMENTED;
583 #endif
584                         add_general (&gr, &stack_size, ainfo, FALSE);
585                         break;
586                 case MONO_TYPE_TYPEDBYREF:
587                         add_general (&gr, &stack_size, ainfo, FALSE);
588                         break;
589                 case MONO_TYPE_U8:
590                 case MONO_TYPE_I8:
591 #ifdef SPARCV9
592                         add_general (&gr, &stack_size, ainfo, FALSE);
593 #else
594                         add_general (&gr, &stack_size, ainfo, TRUE);
595 #endif
596                         break;
597                 case MONO_TYPE_R4:
598 #ifdef SPARCV9
599                         add_float (&fr, &stack_size, ainfo, TRUE);
600                         gr ++;
601 #else
602                         /* single precision values are passed in integer registers */
603                         add_general (&gr, &stack_size, ainfo, FALSE);
604 #endif
605                         break;
606                 case MONO_TYPE_R8:
607 #ifdef SPARCV9
608                         add_float (&fr, &stack_size, ainfo, FALSE);
609                         gr ++;
610 #else
611                         /* double precision values are passed in a pair of registers */
612                         add_general (&gr, &stack_size, ainfo, TRUE);
613 #endif
614                         break;
615                 default:
616                         g_assert_not_reached ();
617                 }
618         }
619
620         if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
621                 gr = PARAM_REGS;
622
623                 /* Emit the signature cookie just before the implicit arguments */
624                 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
625         }
626
627         /* return value */
628         ret_type = mono_type_get_underlying_type (sig->ret);
629         ret_type = mini_get_basic_type_from_generic (gsctx, ret_type);
630         switch (ret_type->type) {
631         case MONO_TYPE_BOOLEAN:
632         case MONO_TYPE_I1:
633         case MONO_TYPE_U1:
634         case MONO_TYPE_I2:
635         case MONO_TYPE_U2:
636         case MONO_TYPE_CHAR:
637         case MONO_TYPE_I4:
638         case MONO_TYPE_U4:
639         case MONO_TYPE_I:
640         case MONO_TYPE_U:
641         case MONO_TYPE_PTR:
642         case MONO_TYPE_FNPTR:
643         case MONO_TYPE_CLASS:
644         case MONO_TYPE_OBJECT:
645         case MONO_TYPE_SZARRAY:
646         case MONO_TYPE_ARRAY:
647         case MONO_TYPE_STRING:
648                 cinfo->ret.storage = ArgInIReg;
649                 cinfo->ret.reg = sparc_i0;
650                 if (gr < 1)
651                         gr = 1;
652                 break;
653         case MONO_TYPE_U8:
654         case MONO_TYPE_I8:
655 #ifdef SPARCV9
656                 cinfo->ret.storage = ArgInIReg;
657                 cinfo->ret.reg = sparc_i0;
658                 if (gr < 1)
659                         gr = 1;
660 #else
661                 cinfo->ret.storage = ArgInIRegPair;
662                 cinfo->ret.reg = sparc_i0;
663                 if (gr < 2)
664                         gr = 2;
665 #endif
666                 break;
667         case MONO_TYPE_R4:
668         case MONO_TYPE_R8:
669                 cinfo->ret.storage = ArgInFReg;
670                 cinfo->ret.reg = sparc_f0;
671                 break;
672         case MONO_TYPE_GENERICINST:
673                 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
674                         cinfo->ret.storage = ArgInIReg;
675                         cinfo->ret.reg = sparc_i0;
676                         if (gr < 1)
677                                 gr = 1;
678                         break;
679                 }
680                 /* Fall through */
681         case MONO_TYPE_VALUETYPE:
682                 if (v64) {
683                         if (sig->pinvoke)
684                                 NOT_IMPLEMENTED;
685                         else
686                                 /* Already done */
687                                 ;
688                 }
689                 else
690                         cinfo->ret.storage = ArgOnStack;
691                 break;
692         case MONO_TYPE_TYPEDBYREF:
693                 if (v64) {
694                         if (sig->pinvoke)
695                                 /* Same as a valuetype with size 24 */
696                                 NOT_IMPLEMENTED;
697                         else
698                                 /* Already done */
699                                 ;
700                 }
701                 else
702                         cinfo->ret.storage = ArgOnStack;
703                 break;
704         case MONO_TYPE_VOID:
705                 break;
706         default:
707                 g_error ("Can't handle as return value 0x%x", sig->ret->type);
708         }
709
710         cinfo->stack_usage = stack_size;
711         cinfo->reg_usage = gr;
712         return cinfo;
713 }
714
715 GList *
716 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
717 {
718         GList *vars = NULL;
719         int i;
720
721         /* 
722          * FIXME: If an argument is allocated to a register, then load it from the
723          * stack in the prolog.
724          */
725
726         for (i = 0; i < cfg->num_varinfo; i++) {
727                 MonoInst *ins = cfg->varinfo [i];
728                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
729
730                 /* unused vars */
731                 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
732                         continue;
733
734                 /* FIXME: Make arguments on stack allocateable to registers */
735                 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode == OP_REGVAR) || (ins->opcode == OP_ARG))
736                         continue;
737
738                 if (mono_is_regsize_var (ins->inst_vtype)) {
739                         g_assert (MONO_VARINFO (cfg, i)->reg == -1);
740                         g_assert (i == vmv->idx);
741
742                         vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
743                 }
744         }
745
746         return vars;
747 }
748
749 GList *
750 mono_arch_get_global_int_regs (MonoCompile *cfg)
751 {
752         GList *regs = NULL;
753         int i;
754         MonoMethodSignature *sig;
755         CallInfo *cinfo;
756
757         sig = mono_method_signature (cfg->method);
758
759         cinfo = get_call_info (cfg, sig, FALSE);
760
761         /* Use unused input registers */
762         for (i = cinfo->reg_usage; i < 6; ++i)
763                 regs = g_list_prepend (regs, GUINT_TO_POINTER (sparc_i0 + i));
764
765         /* Use %l0..%l6 as global registers */
766         for (i = sparc_l0; i < sparc_l7; ++i)
767                 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
768
769         g_free (cinfo);
770
771         return regs;
772 }
773
774 /*
775  * mono_arch_regalloc_cost:
776  *
777  *  Return the cost, in number of memory references, of the action of 
778  * allocating the variable VMV into a register during global register
779  * allocation.
780  */
781 guint32
782 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
783 {
784         return 0;
785 }
786
787 /*
788  * Set var information according to the calling convention. sparc version.
789  * The locals var stuff should most likely be split in another method.
790  */
791
792 void
793 mono_arch_allocate_vars (MonoCompile *cfg)
794 {
795         MonoMethodSignature *sig;
796         MonoMethodHeader *header;
797         MonoInst *inst;
798         int i, offset, size, align, curinst;
799         CallInfo *cinfo;
800
801         header = mono_method_get_header (cfg->method);
802
803         sig = mono_method_signature (cfg->method);
804
805         cinfo = get_call_info (cfg, sig, FALSE);
806
807         if (sig->ret->type != MONO_TYPE_VOID) {
808                 switch (cinfo->ret.storage) {
809                 case ArgInIReg:
810                 case ArgInFReg:
811                         cfg->ret->opcode = OP_REGVAR;
812                         cfg->ret->inst_c0 = cinfo->ret.reg;
813                         break;
814                 case ArgInIRegPair:
815                         if (((sig->ret->type == MONO_TYPE_I8) || (sig->ret->type == MONO_TYPE_U8))) {
816                                 MonoInst *low = get_vreg_to_inst (cfg, cfg->ret->dreg + 1);
817                                 MonoInst *high = get_vreg_to_inst (cfg, cfg->ret->dreg + 2);
818
819                                 low->opcode = OP_REGVAR;
820                                 low->dreg = cinfo->ret.reg + 1;
821                                 high->opcode = OP_REGVAR;
822                                 high->dreg = cinfo->ret.reg;
823                         }
824                         cfg->ret->opcode = OP_REGVAR;
825                         cfg->ret->inst_c0 = cinfo->ret.reg;
826                         break;
827                 case ArgOnStack:
828 #ifdef SPARCV9
829                         g_assert_not_reached ();
830 #else
831                         /* valuetypes */
832                         cfg->vret_addr->opcode = OP_REGOFFSET;
833                         cfg->vret_addr->inst_basereg = sparc_fp;
834                         cfg->vret_addr->inst_offset = 64;
835 #endif
836                         break;
837                 default:
838                         NOT_IMPLEMENTED;
839                 }
840                 cfg->ret->dreg = cfg->ret->inst_c0;
841         }
842
843         /*
844          * We use the ABI calling conventions for managed code as well.
845          * Exception: valuetypes are never returned in registers on V9.
846          * FIXME: Use something more optimized.
847          */
848
849         /* Locals are allocated backwards from %fp */
850         cfg->frame_reg = sparc_fp;
851         offset = 0;
852
853         /* 
854          * Reserve a stack slot for holding information used during exception 
855          * handling.
856          */
857         if (header->num_clauses)
858                 offset += sizeof (gpointer) * 2;
859
860         if (cfg->method->save_lmf) {
861                 offset += sizeof (MonoLMF);
862                 cfg->arch.lmf_offset = offset;
863         }
864
865         curinst = cfg->locals_start;
866         for (i = curinst; i < cfg->num_varinfo; ++i) {
867                 inst = cfg->varinfo [i];
868
869                 if ((inst->opcode == OP_REGVAR) || (inst->opcode == OP_REGOFFSET)) {
870                         //g_print ("allocating local %d to %s\n", i, mono_arch_regname (inst->dreg));
871                         continue;
872                 }
873
874                 if (inst->flags & MONO_INST_IS_DEAD)
875                         continue;
876
877                 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
878                 * pinvoke wrappers when they call functions returning structure */
879                 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
880                         size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
881                 else
882                         size = mini_type_stack_size (cfg->generic_sharing_context, inst->inst_vtype, &align);
883
884                 /* 
885                  * This is needed since structures containing doubles must be doubleword 
886          * aligned.
887                  * FIXME: Do this only if needed.
888                  */
889                 if (MONO_TYPE_ISSTRUCT (inst->inst_vtype))
890                         align = 8;
891
892                 /*
893                  * variables are accessed as negative offsets from %fp, so increase
894                  * the offset before assigning it to a variable
895                  */
896                 offset += size;
897
898                 offset += align - 1;
899                 offset &= ~(align - 1);
900                 inst->opcode = OP_REGOFFSET;
901                 inst->inst_basereg = sparc_fp;
902                 inst->inst_offset = STACK_BIAS + -offset;
903
904                 //g_print ("allocating local %d to [%s - %d]\n", i, mono_arch_regname (inst->inst_basereg), - inst->inst_offset);
905         }
906
907         if (sig->call_convention == MONO_CALL_VARARG) {
908                 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
909         }
910
911         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
912                 inst = cfg->args [i];
913                 if (inst->opcode != OP_REGVAR) {
914                         ArgInfo *ainfo = &cinfo->args [i];
915                         gboolean inreg = TRUE;
916                         MonoType *arg_type;
917                         ArgStorage storage;
918
919                         if (sig->hasthis && (i == 0))
920                                 arg_type = &mono_defaults.object_class->byval_arg;
921                         else
922                                 arg_type = sig->params [i - sig->hasthis];
923
924 #ifndef SPARCV9
925                         if (!arg_type->byref && ((arg_type->type == MONO_TYPE_R4) 
926                                                                          || (arg_type->type == MONO_TYPE_R8)))
927                                 /*
928                                  * Since float arguments are passed in integer registers, we need to
929                                  * save them to the stack in the prolog.
930                                  */
931                                 inreg = FALSE;
932 #endif
933
934                         /* FIXME: Allocate volatile arguments to registers */
935                         /* FIXME: This makes the argument holding a vtype address into volatile */
936                         if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
937                                 inreg = FALSE;
938
939                         if (MONO_TYPE_ISSTRUCT (arg_type))
940                                 /* FIXME: this isn't needed */
941                                 inreg = FALSE;
942
943                         inst->opcode = OP_REGOFFSET;
944
945                         if (!inreg)
946                                 storage = ArgOnStack;
947                         else
948                                 storage = ainfo->storage;
949
950                         switch (storage) {
951                         case ArgInIReg:
952                                 inst->opcode = OP_REGVAR;
953                                 inst->dreg = sparc_i0 + ainfo->reg;
954                                 break;
955                         case ArgInIRegPair:
956                                 if (inst->type == STACK_I8) {
957                                         MonoInst *low = get_vreg_to_inst (cfg, inst->dreg + 1);
958                                         MonoInst *high = get_vreg_to_inst (cfg, inst->dreg + 2);
959
960                                         low->opcode = OP_REGVAR;
961                                         low->dreg = sparc_i0 + ainfo->reg + 1;
962                                         high->opcode = OP_REGVAR;
963                                         high->dreg = sparc_i0 + ainfo->reg;
964                                 }
965                                 inst->opcode = OP_REGVAR;
966                                 inst->dreg = sparc_i0 + ainfo->reg;                                     
967                                 break;
968                         case ArgInFloatReg:
969                         case ArgInDoubleReg:
970                                 /* 
971                                  * Since float regs are volatile, we save the arguments to
972                                  * the stack in the prolog.
973                                  * FIXME: Avoid this if the method contains no calls.
974                                  */
975                         case ArgOnStack:
976                         case ArgOnStackPair:
977                         case ArgInSplitRegStack:
978                                 /* Split arguments are saved to the stack in the prolog */
979                                 inst->opcode = OP_REGOFFSET;
980                                 /* in parent frame */
981                                 inst->inst_basereg = sparc_fp;
982                                 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
983
984                                 if (!arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
985                                         /* 
986                                          * It is very hard to load doubles from non-doubleword aligned
987                                          * memory locations. So if the offset is misaligned, we copy the
988                                          * argument to a stack location in the prolog.
989                                          */
990                                         if ((inst->inst_offset - STACK_BIAS) % 8) {
991                                                 inst->inst_basereg = sparc_fp;
992                                                 offset += 8;
993                                                 align = 8;
994                                                 offset += align - 1;
995                                                 offset &= ~(align - 1);
996                                                 inst->inst_offset = STACK_BIAS + -offset;
997
998                                         }
999                                 }
1000                                 break;
1001                         default:
1002                                 NOT_IMPLEMENTED;
1003                         }
1004
1005                         if (MONO_TYPE_ISSTRUCT (arg_type)) {
1006                                 /* Add a level of indirection */
1007                                 /*
1008                                  * It would be easier to add OP_LDIND_I here, but ldind_i instructions
1009                                  * are destructively modified in a lot of places in inssel.brg.
1010                                  */
1011                                 MonoInst *indir;
1012                                 MONO_INST_NEW (cfg, indir, 0);
1013                                 *indir = *inst;
1014                                 inst->opcode = OP_VTARG_ADDR;
1015                                 inst->inst_left = indir;
1016                         }
1017                 }
1018         }
1019
1020         /* Add a properly aligned dword for use by int<->float conversion opcodes */
1021         offset += 8;
1022         offset = ALIGN_TO (offset, 8);
1023         cfg->arch.float_spill_slot_offset = offset;
1024
1025         /* 
1026          * spillvars are stored between the normal locals and the storage reserved
1027          * by the ABI.
1028          */
1029
1030         cfg->stack_offset = offset;
1031
1032         g_free (cinfo);
1033 }
1034
1035 void
1036 mono_arch_create_vars (MonoCompile *cfg)
1037 {
1038         MonoMethodSignature *sig;
1039
1040         sig = mono_method_signature (cfg->method);
1041
1042         if (MONO_TYPE_ISSTRUCT ((sig->ret))) {
1043                 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1044                 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1045                         printf ("vret_addr = ");
1046                         mono_print_ins (cfg->vret_addr);
1047                 }
1048         }
1049 }
1050
1051 static void
1052 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, guint32 sreg)
1053 {
1054         MonoInst *arg;
1055
1056         MONO_INST_NEW (cfg, arg, 0);
1057
1058         arg->sreg1 = sreg;
1059
1060         switch (storage) {
1061         case ArgInIReg:
1062                 arg->opcode = OP_MOVE;
1063                 arg->dreg = mono_alloc_ireg (cfg);
1064
1065                 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, reg, FALSE);
1066                 break;
1067         case ArgInFloatReg:
1068                 arg->opcode = OP_FMOVE;
1069                 arg->dreg = mono_alloc_freg (cfg);
1070
1071                 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, reg, TRUE);
1072                 break;
1073         default:
1074                 g_assert_not_reached ();
1075         }
1076
1077         MONO_ADD_INS (cfg->cbb, arg);
1078 }
1079
1080 static void
1081 add_outarg_load (MonoCompile *cfg, MonoCallInst *call, int opcode, int basereg, int offset, int reg)
1082 {
1083         MonoInst *arg;
1084         int dreg = mono_alloc_ireg (cfg);
1085
1086         EMIT_NEW_LOAD_MEMBASE (cfg, arg, OP_LOAD_MEMBASE, dreg, sparc_sp, offset);
1087         MONO_ADD_INS (cfg->cbb, arg);
1088
1089         mono_call_inst_add_outarg_reg (cfg, call, dreg, reg, FALSE);
1090 }
1091
1092 static void
1093 emit_pass_long (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoInst *in)
1094 {
1095         int offset = ARGS_OFFSET + ainfo->offset;
1096
1097         switch (ainfo->storage) {
1098         case ArgInIRegPair:
1099                 add_outarg_reg (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg + 1, in->dreg + 1);
1100                 add_outarg_reg (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg, in->dreg + 2);
1101                 break;
1102         case ArgOnStackPair:
1103                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, offset, in->dreg + 2);
1104                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, offset + 4, in->dreg + 1);
1105                 break;
1106         case ArgInSplitRegStack:
1107                 add_outarg_reg (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg, in->dreg + 2);
1108                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, offset + 4, in->dreg + 1);
1109                 break;
1110         default:
1111                 g_assert_not_reached ();
1112         }
1113 }
1114
1115 static void
1116 emit_pass_double (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoInst *in)
1117 {
1118         int offset = ARGS_OFFSET + ainfo->offset;
1119
1120         switch (ainfo->storage) {
1121         case ArgInIRegPair:
1122                 /* floating-point <-> integer transfer must go through memory */
1123                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, sparc_sp, offset, in->dreg);
1124
1125                 /* Load into a register pair */
1126                 add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset, sparc_o0 + ainfo->reg);
1127                 add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset + 4, sparc_o0 + ainfo->reg + 1);
1128                 break;
1129         case ArgOnStackPair:
1130                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, sparc_sp, offset, in->dreg);
1131                 break;
1132         case ArgInSplitRegStack:
1133                 /* floating-point <-> integer transfer must go through memory */
1134                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, sparc_sp, offset, in->dreg);
1135                 /* Load most significant word into register */
1136                 add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset, sparc_o0 + ainfo->reg);
1137                 break;
1138         default:
1139                 g_assert_not_reached ();
1140         }
1141 }
1142
1143 static void
1144 emit_pass_float (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoInst *in)
1145 {
1146         int offset = ARGS_OFFSET + ainfo->offset;
1147
1148         switch (ainfo->storage) {
1149         case ArgInIReg:
1150                 /* floating-point <-> integer transfer must go through memory */
1151                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, sparc_sp, offset, in->dreg);
1152                 add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset, sparc_o0 + ainfo->reg);
1153                 break;
1154         case ArgOnStack:
1155                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, sparc_sp, offset, in->dreg);
1156                 break;
1157         default:
1158                 g_assert_not_reached ();
1159         }
1160 }
1161
1162 static void
1163 emit_pass_other (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoType *arg_type, MonoInst *in);
1164
1165 static void
1166 emit_pass_vtype (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo, ArgInfo *ainfo, MonoType *arg_type, MonoInst *in, gboolean pinvoke)
1167 {
1168         MonoInst *arg;
1169         guint32 align, offset, pad, size;
1170
1171         if (arg_type->type == MONO_TYPE_TYPEDBYREF) {
1172                 size = sizeof (MonoTypedRef);
1173                 align = sizeof (gpointer);
1174         }
1175         else if (pinvoke)
1176                 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
1177         else {
1178                 /* 
1179                  * Other backends use mono_type_stack_size (), but that
1180                  * aligns the size to 8, which is larger than the size of
1181                  * the source, leading to reads of invalid memory if the
1182                  * source is at the end of address space.
1183                  */
1184                 size = mono_class_value_size (in->klass, &align);
1185         }
1186
1187         /* The first 6 argument locations are reserved */
1188         if (cinfo->stack_usage < 6 * sizeof (gpointer))
1189                 cinfo->stack_usage = 6 * sizeof (gpointer);
1190
1191         offset = ALIGN_TO ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage, align);
1192         pad = offset - ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage);
1193
1194         cinfo->stack_usage += size;
1195         cinfo->stack_usage += pad;
1196
1197         /* 
1198          * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
1199          * use the normal OUTARG opcodes to pass the address of the location to
1200          * the callee.
1201          */
1202         if (size > 0) {
1203                 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
1204                 arg->sreg1 = in->dreg;
1205                 arg->klass = in->klass;
1206                 arg->backend.size = size;
1207                 arg->inst_p0 = call;
1208                 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1209                 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1210                 ((ArgInfo*)(arg->inst_p1))->offset = STACK_BIAS + offset;
1211                 MONO_ADD_INS (cfg->cbb, arg);
1212
1213                 MONO_INST_NEW (cfg, arg, OP_ADD_IMM);
1214                 arg->dreg = mono_alloc_preg (cfg);
1215                 arg->sreg1 = sparc_sp;
1216                 arg->inst_imm = STACK_BIAS + offset;
1217                 MONO_ADD_INS (cfg->cbb, arg);
1218
1219                 emit_pass_other (cfg, call, ainfo, NULL, arg);
1220         }
1221 }
1222
1223 static void
1224 emit_pass_other (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoType *arg_type, MonoInst *in)
1225 {
1226         int offset = ARGS_OFFSET + ainfo->offset;
1227         int opcode;
1228
1229         switch (ainfo->storage) {
1230         case ArgInIReg:
1231                 add_outarg_reg (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg, in->dreg);
1232                 break;
1233         case ArgOnStack:
1234 #ifdef SPARCV9
1235                 NOT_IMPLEMENTED;
1236 #else
1237                 if (offset & 0x1)
1238                         opcode = OP_STOREI1_MEMBASE_REG;
1239                 else if (offset & 0x2)
1240                         opcode = OP_STOREI2_MEMBASE_REG;
1241                 else
1242                         opcode = OP_STOREI4_MEMBASE_REG;
1243                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, opcode, sparc_sp, offset, in->dreg);
1244 #endif
1245                 break;
1246         default:
1247                 g_assert_not_reached ();
1248         }
1249 }
1250
1251 static void
1252 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1253 {
1254         MonoMethodSignature *tmp_sig;
1255
1256         /*
1257          * mono_ArgIterator_Setup assumes the signature cookie is 
1258          * passed first and all the arguments which were before it are
1259          * passed on the stack after the signature. So compensate by 
1260          * passing a different signature.
1261          */
1262         tmp_sig = mono_metadata_signature_dup (call->signature);
1263         tmp_sig->param_count -= call->signature->sentinelpos;
1264         tmp_sig->sentinelpos = 0;
1265         memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1266
1267         /* FIXME: Add support for signature tokens to AOT */
1268         cfg->disable_aot = TRUE;
1269         /* We allways pass the signature on the stack for simplicity */
1270         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sparc_sp, ARGS_OFFSET + cinfo->sig_cookie.offset, tmp_sig);
1271 }
1272
1273 void
1274 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1275 {
1276         MonoInst *in;
1277         MonoMethodSignature *sig;
1278         int i, n;
1279         CallInfo *cinfo;
1280         ArgInfo *ainfo;
1281         guint32 extra_space = 0;
1282
1283         sig = call->signature;
1284         n = sig->param_count + sig->hasthis;
1285         
1286         cinfo = get_call_info (cfg, sig, sig->pinvoke);
1287
1288         if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1289                 /* Set the 'struct/union return pointer' location on the stack */
1290                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, call->vret_var->dreg);
1291         }
1292
1293         for (i = 0; i < n; ++i) {
1294                 MonoType *arg_type;
1295
1296                 ainfo = cinfo->args + i;
1297
1298                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1299                         /* Emit the signature cookie just before the first implicit argument */
1300                         emit_sig_cookie (cfg, call, cinfo);
1301                 }
1302
1303                 in = call->args [i];
1304
1305                 if (sig->hasthis && (i == 0))
1306                         arg_type = &mono_defaults.object_class->byval_arg;
1307                 else
1308                         arg_type = sig->params [i - sig->hasthis];
1309
1310                 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis])))
1311                         emit_pass_vtype (cfg, call, cinfo, ainfo, arg_type, in, sig->pinvoke);
1312                 else if (!arg_type->byref && ((arg_type->type == MONO_TYPE_I8) || (arg_type->type == MONO_TYPE_U8)))
1313                         emit_pass_long (cfg, call, ainfo, in);
1314                 else if (!arg_type->byref && (arg_type->type == MONO_TYPE_R8))
1315                         emit_pass_double (cfg, call, ainfo, in);
1316                 else if (!arg_type->byref && (arg_type->type == MONO_TYPE_R4))
1317                         emit_pass_float (cfg, call, ainfo, in);
1318                 else
1319                         emit_pass_other (cfg, call, ainfo, arg_type, in);
1320         }
1321
1322         /* Handle the case where there are no implicit arguments */
1323         if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
1324                 emit_sig_cookie (cfg, call, cinfo);
1325         }
1326
1327         call->stack_usage = cinfo->stack_usage + extra_space;
1328
1329         g_free (cinfo);
1330 }
1331
1332 void
1333 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1334 {
1335         ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
1336         int size = ins->backend.size;
1337
1338         mini_emit_memcpy (cfg, sparc_sp, ainfo->offset, src->dreg, 0, size, 0);
1339 }
1340
1341 void
1342 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1343 {
1344         CallInfo *cinfo = get_call_info (cfg, mono_method_signature (method), FALSE);
1345
1346         switch (cinfo->ret.storage) {
1347         case ArgInIReg:
1348                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1349                 break;
1350         case ArgInIRegPair:
1351                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg + 2);
1352                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg + 1, val->dreg + 1);
1353                 break;
1354         case ArgInFReg:
1355                 if (mono_method_signature (method)->ret->type == MONO_TYPE_R4)
1356                         MONO_EMIT_NEW_UNALU (cfg, OP_SETFRET, cfg->ret->dreg, val->dreg);
1357                 else
1358                         MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1359                 break;
1360         default:
1361                 g_assert_not_reached ();
1362         }
1363
1364         g_assert (cinfo);
1365 }
1366
1367 int cond_to_sparc_cond [][3] = {
1368         {sparc_be,   sparc_be,   sparc_fbe},
1369         {sparc_bne,  sparc_bne,  0},
1370         {sparc_ble,  sparc_ble,  sparc_fble},
1371         {sparc_bge,  sparc_bge,  sparc_fbge},
1372         {sparc_bl,   sparc_bl,   sparc_fbl},
1373         {sparc_bg,   sparc_bg,   sparc_fbg},
1374         {sparc_bleu, sparc_bleu, 0},
1375         {sparc_beu,  sparc_beu,  0},
1376         {sparc_blu,  sparc_blu,  sparc_fbl},
1377         {sparc_bgu,  sparc_bgu,  sparc_fbg}
1378 };
1379
1380 /* Map opcode to the sparc condition codes */
1381 static inline SparcCond
1382 opcode_to_sparc_cond (int opcode)
1383 {
1384         CompRelation rel;
1385         CompType t;
1386
1387         switch (opcode) {
1388         case OP_COND_EXC_OV:
1389         case OP_COND_EXC_IOV:
1390                 return sparc_bvs;
1391         case OP_COND_EXC_C:
1392         case OP_COND_EXC_IC:
1393                 return sparc_bcs;
1394         case OP_COND_EXC_NO:
1395         case OP_COND_EXC_NC:
1396                 NOT_IMPLEMENTED;
1397         default:
1398                 rel = mono_opcode_to_cond (opcode);
1399                 t = mono_opcode_to_type (opcode, -1);
1400
1401                 return cond_to_sparc_cond [rel][t];
1402                 break;
1403         }
1404
1405         return -1;
1406 }
1407
1408 #define COMPUTE_DISP(ins) \
1409 if (ins->flags & MONO_INST_BRLABEL) { \
1410         if (ins->inst_i0->inst_c0) \
1411            disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2; \
1412         else { \
1413             disp = 0; \
1414                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1415         } \
1416 } else { \
1417         if (ins->inst_true_bb->native_offset) \
1418            disp = (ins->inst_true_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2; \
1419         else { \
1420             disp = 0; \
1421                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1422         } \
1423 }
1424
1425 #ifdef SPARCV9
1426 #define DEFAULT_ICC sparc_xcc_short
1427 #else
1428 #define DEFAULT_ICC sparc_icc_short
1429 #endif
1430
1431 #ifdef SPARCV9
1432 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) \
1433     do { \
1434         gint32 disp; \
1435         guint32 predict; \
1436         COMPUTE_DISP(ins); \
1437         predict = (disp != 0) ? 1 : 0; \
1438         g_assert (sparc_is_imm19 (disp)); \
1439         sparc_branchp (code, (annul), cond, icc, (predict), disp); \
1440         if (filldelay) sparc_nop (code); \
1441     } while (0)
1442 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_ICC ((ins), (cond), (annul), (filldelay), (sparc_xcc_short))
1443 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) \
1444     do { \
1445         gint32 disp; \
1446         guint32 predict; \
1447         COMPUTE_DISP(ins); \
1448         predict = (disp != 0) ? 1 : 0; \
1449         g_assert (sparc_is_imm19 (disp)); \
1450         sparc_fbranch (code, (annul), cond, disp); \
1451         if (filldelay) sparc_nop (code); \
1452     } while (0)
1453 #else
1454 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) g_assert_not_reached ()
1455 #define EMIT_COND_BRANCH_GENERAL(ins,bop,cond,annul,filldelay) \
1456     do { \
1457         gint32 disp; \
1458         COMPUTE_DISP(ins); \
1459         g_assert (sparc_is_imm22 (disp)); \
1460         sparc_ ## bop (code, (annul), cond, disp); \
1461         if (filldelay) sparc_nop (code); \
1462     } while (0)
1463 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),branch,(cond),annul,filldelay)
1464 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),fbranch,(cond),annul,filldelay)
1465 #endif
1466
1467 #define EMIT_COND_BRANCH_PREDICTED(ins,cond,annul,filldelay) \
1468     do { \
1469             gint32 disp; \
1470         guint32 predict; \
1471         COMPUTE_DISP(ins); \
1472         predict = (disp != 0) ? 1 : 0; \
1473         g_assert (sparc_is_imm19 (disp)); \
1474                 sparc_branchp (code, (annul), (cond), DEFAULT_ICC, (predict), disp); \
1475         if (filldelay) sparc_nop (code); \
1476     } while (0)
1477
1478 #define EMIT_COND_BRANCH_BPR(ins,bop,predict,annul,filldelay) \
1479     do { \
1480             gint32 disp; \
1481         COMPUTE_DISP(ins); \
1482                 g_assert (sparc_is_imm22 (disp)); \
1483                 sparc_ ## bop (code, (annul), (predict), ins->sreg1, disp); \
1484         if (filldelay) sparc_nop (code); \
1485     } while (0)
1486
1487 /* emit an exception if condition is fail */
1488 /*
1489  * We put the exception throwing code out-of-line, at the end of the method
1490  */
1491 #define EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,filldelay,icc) do {     \
1492                 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,   \
1493                                     MONO_PATCH_INFO_EXC, sexc_name);  \
1494         if (sparcv9 && ((icc) != sparc_icc_short)) {          \
1495            sparc_branchp (code, 0, (cond), (icc), 0, 0); \
1496         } \
1497         else { \
1498                         sparc_branch (code, 0, cond, 0);     \
1499         } \
1500         if (filldelay) sparc_nop (code);     \
1501         } while (0); 
1502
1503 #define EMIT_COND_SYSTEM_EXCEPTION(ins,cond,sexc_name) EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,TRUE,DEFAULT_ICC)
1504
1505 #define EMIT_COND_SYSTEM_EXCEPTION_BPR(ins,bop,sexc_name) do { \
1506                 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,   \
1507                                     MONO_PATCH_INFO_EXC, sexc_name);  \
1508                 sparc_ ## bop (code, FALSE, FALSE, ins->sreg1, 0); \
1509         sparc_nop (code);    \
1510 } while (0);
1511
1512 #define EMIT_ALU_IMM(ins,op,setcc) do { \
1513                         if (sparc_is_imm13 ((ins)->inst_imm)) \
1514                                 sparc_ ## op ## _imm (code, (setcc), (ins)->sreg1, ins->inst_imm, (ins)->dreg); \
1515                         else { \
1516                                 sparc_set (code, ins->inst_imm, sparc_o7); \
1517                                 sparc_ ## op (code, (setcc), (ins)->sreg1, sparc_o7, (ins)->dreg); \
1518                         } \
1519 } while (0);
1520
1521 #define EMIT_LOAD_MEMBASE(ins,op) do { \
1522                         if (sparc_is_imm13 (ins->inst_offset)) \
1523                                 sparc_ ## op ## _imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg); \
1524                         else { \
1525                                 sparc_set (code, ins->inst_offset, sparc_o7); \
1526                                 sparc_ ## op (code, ins->inst_basereg, sparc_o7, ins->dreg); \
1527                         } \
1528 } while (0);
1529
1530 /* max len = 5 */
1531 #define EMIT_STORE_MEMBASE_IMM(ins,op) do { \
1532                         guint32 sreg; \
1533                         if (ins->inst_imm == 0) \
1534                                 sreg = sparc_g0; \
1535                         else { \
1536                                 sparc_set (code, ins->inst_imm, sparc_o7); \
1537                                 sreg = sparc_o7; \
1538                         } \
1539                         if (!sparc_is_imm13 (ins->inst_offset)) { \
1540                                 sparc_set (code, ins->inst_offset, GP_SCRATCH_REG); \
1541                                 sparc_ ## op (code, sreg, ins->inst_destbasereg, GP_SCRATCH_REG); \
1542                         } \
1543                         else \
1544                                 sparc_ ## op ## _imm (code, sreg, ins->inst_destbasereg, ins->inst_offset); \
1545                                                                                                                                                                                  } while (0);
1546
1547 #define EMIT_STORE_MEMBASE_REG(ins,op) do { \
1548                         if (!sparc_is_imm13 (ins->inst_offset)) { \
1549                                 sparc_set (code, ins->inst_offset, sparc_o7); \
1550                                 sparc_ ## op (code, ins->sreg1, ins->inst_destbasereg, sparc_o7); \
1551                         } \
1552                                   else \
1553                                 sparc_ ## op ## _imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset); \
1554                                                                                                                                                                                  } while (0);
1555
1556 #define EMIT_CALL() do { \
1557     if (v64) { \
1558         sparc_set_template (code, sparc_o7); \
1559         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7); \
1560     } \
1561     else { \
1562         sparc_call_simple (code, 0); \
1563     } \
1564     sparc_nop (code); \
1565 } while (0);
1566
1567 /*
1568  * A call template is 7 instructions long, so we want to avoid it if possible.
1569  */
1570 static guint32*
1571 emit_call (MonoCompile *cfg, guint32 *code, guint32 patch_type, gconstpointer data)
1572 {
1573         gpointer target;
1574
1575         /* FIXME: This only works if the target method is already compiled */
1576         if (0 && v64 && !cfg->compile_aot) {
1577                 MonoJumpInfo patch_info;
1578
1579                 patch_info.type = patch_type;
1580                 patch_info.data.target = data;
1581
1582                 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &patch_info, FALSE);
1583
1584                 /* FIXME: Add optimizations if the target is close enough */
1585                 sparc_set (code, target, sparc_o7);
1586                 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
1587                 sparc_nop (code);
1588         }
1589         else {
1590                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, patch_type, data);
1591                 EMIT_CALL ();
1592         }
1593         
1594         return code;
1595 }
1596
1597 void
1598 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1599 {
1600 }
1601
1602 void
1603 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
1604 {
1605         MonoInst *ins, *n, *last_ins = NULL;
1606         ins = bb->code;
1607
1608         MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1609                 switch (ins->opcode) {
1610                 case OP_MUL_IMM: 
1611                         /* remove unnecessary multiplication with 1 */
1612                         if (ins->inst_imm == 1) {
1613                                 if (ins->dreg != ins->sreg1) {
1614                                         ins->opcode = OP_MOVE;
1615                                 } else {
1616                                         MONO_DELETE_INS (bb, ins);
1617                                         continue;
1618                                 }
1619                         }
1620                         break;
1621 #ifndef SPARCV9
1622                 case OP_LOAD_MEMBASE:
1623                 case OP_LOADI4_MEMBASE:
1624                         /* 
1625                          * OP_STORE_MEMBASE_REG reg, offset(basereg) 
1626                          * OP_LOAD_MEMBASE offset(basereg), reg
1627                          */
1628                         if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG 
1629                                          || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1630                             ins->inst_basereg == last_ins->inst_destbasereg &&
1631                             ins->inst_offset == last_ins->inst_offset) {
1632                                 if (ins->dreg == last_ins->sreg1) {
1633                                         MONO_DELETE_INS (bb, ins);
1634                                         continue;
1635                                 } else {
1636                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1637                                         ins->opcode = OP_MOVE;
1638                                         ins->sreg1 = last_ins->sreg1;
1639                                 }
1640
1641                         /* 
1642                          * Note: reg1 must be different from the basereg in the second load
1643                          * OP_LOAD_MEMBASE offset(basereg), reg1
1644                          * OP_LOAD_MEMBASE offset(basereg), reg2
1645                          * -->
1646                          * OP_LOAD_MEMBASE offset(basereg), reg1
1647                          * OP_MOVE reg1, reg2
1648                          */
1649                         } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1650                                            || last_ins->opcode == OP_LOAD_MEMBASE) &&
1651                               ins->inst_basereg != last_ins->dreg &&
1652                               ins->inst_basereg == last_ins->inst_basereg &&
1653                               ins->inst_offset == last_ins->inst_offset) {
1654
1655                                 if (ins->dreg == last_ins->dreg) {
1656                                         MONO_DELETE_INS (bb, ins);
1657                                         continue;
1658                                 } else {
1659                                         ins->opcode = OP_MOVE;
1660                                         ins->sreg1 = last_ins->dreg;
1661                                 }
1662
1663                                 //g_assert_not_reached ();
1664
1665 #if 0
1666                         /* 
1667                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
1668                          * OP_LOAD_MEMBASE offset(basereg), reg
1669                          * -->
1670                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
1671                          * OP_ICONST reg, imm
1672                          */
1673                         } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1674                                                 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1675                                    ins->inst_basereg == last_ins->inst_destbasereg &&
1676                                    ins->inst_offset == last_ins->inst_offset) {
1677                                 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1678                                 ins->opcode = OP_ICONST;
1679                                 ins->inst_c0 = last_ins->inst_imm;
1680                                 g_assert_not_reached (); // check this rule
1681 #endif
1682                         }
1683                         break;
1684 #endif
1685                 case OP_LOADI1_MEMBASE:
1686                         if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1687                                         ins->inst_basereg == last_ins->inst_destbasereg &&
1688                                         ins->inst_offset == last_ins->inst_offset) {
1689                                 if (ins->dreg == last_ins->sreg1) {
1690                                         MONO_DELETE_INS (bb, ins);
1691                                         continue;
1692                                 } else {
1693                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1694                                         ins->opcode = OP_MOVE;
1695                                         ins->sreg1 = last_ins->sreg1;
1696                                 }
1697                         }
1698                         break;
1699                 case OP_LOADI2_MEMBASE:
1700                         if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1701                                         ins->inst_basereg == last_ins->inst_destbasereg &&
1702                                         ins->inst_offset == last_ins->inst_offset) {
1703                                 if (ins->dreg == last_ins->sreg1) {
1704                                         MONO_DELETE_INS (bb, ins);
1705                                         continue;
1706                                 } else {
1707                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1708                                         ins->opcode = OP_MOVE;
1709                                         ins->sreg1 = last_ins->sreg1;
1710                                 }
1711                         }
1712                         break;
1713                 case OP_STOREI4_MEMBASE_IMM:
1714                         /* Convert pairs of 0 stores to a dword 0 store */
1715                         /* Used when initializing temporaries */
1716                         /* We know sparc_fp is dword aligned */
1717                         if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM) &&
1718                                 (ins->inst_destbasereg == last_ins->inst_destbasereg) && 
1719                                 (ins->inst_destbasereg == sparc_fp) &&
1720                                 (ins->inst_offset < 0) &&
1721                                 ((ins->inst_offset % 8) == 0) &&
1722                                 ((ins->inst_offset == last_ins->inst_offset - 4)) &&
1723                                 (ins->inst_imm == 0) &&
1724                                 (last_ins->inst_imm == 0)) {
1725                                 if (sparcv9) {
1726                                         last_ins->opcode = OP_STOREI8_MEMBASE_IMM;
1727                                         last_ins->inst_offset = ins->inst_offset;
1728                                         MONO_DELETE_INS (bb, ins);
1729                                         continue;
1730                                 }
1731                         }
1732                         break;
1733                 case OP_IBEQ:
1734                 case OP_IBNE_UN:
1735                 case OP_IBLT:
1736                 case OP_IBGT:
1737                 case OP_IBGE:
1738                 case OP_IBLE:
1739                 case OP_COND_EXC_EQ:
1740                 case OP_COND_EXC_GE:
1741                 case OP_COND_EXC_GT:
1742                 case OP_COND_EXC_LE:
1743                 case OP_COND_EXC_LT:
1744                 case OP_COND_EXC_NE_UN:
1745                         /*
1746                          * Convert compare with zero+branch to BRcc
1747                          */
1748                         /* 
1749                          * This only works in 64 bit mode, since it examines all 64
1750                          * bits of the register.
1751                          * Only do this if the method is small since BPr only has a 16bit
1752                          * displacement.
1753                          */
1754                         if (v64 && (mono_method_get_header (cfg->method)->code_size < 10000) && last_ins && 
1755                                 (last_ins->opcode == OP_COMPARE_IMM) &&
1756                                 (last_ins->inst_imm == 0)) {
1757                                 switch (ins->opcode) {
1758                                 case OP_IBEQ:
1759                                         ins->opcode = OP_SPARC_BRZ;
1760                                         break;
1761                                 case OP_IBNE_UN:
1762                                         ins->opcode = OP_SPARC_BRNZ;
1763                                         break;
1764                                 case OP_IBLT:
1765                                         ins->opcode = OP_SPARC_BRLZ;
1766                                         break;
1767                                 case OP_IBGT:
1768                                         ins->opcode = OP_SPARC_BRGZ;
1769                                         break;
1770                                 case OP_IBGE:
1771                                         ins->opcode = OP_SPARC_BRGEZ;
1772                                         break;
1773                                 case OP_IBLE:
1774                                         ins->opcode = OP_SPARC_BRLEZ;
1775                                         break;
1776                                 case OP_COND_EXC_EQ:
1777                                         ins->opcode = OP_SPARC_COND_EXC_EQZ;
1778                                         break;
1779                                 case OP_COND_EXC_GE:
1780                                         ins->opcode = OP_SPARC_COND_EXC_GEZ;
1781                                         break;
1782                                 case OP_COND_EXC_GT:
1783                                         ins->opcode = OP_SPARC_COND_EXC_GTZ;
1784                                         break;
1785                                 case OP_COND_EXC_LE:
1786                                         ins->opcode = OP_SPARC_COND_EXC_LEZ;
1787                                         break;
1788                                 case OP_COND_EXC_LT:
1789                                         ins->opcode = OP_SPARC_COND_EXC_LTZ;
1790                                         break;
1791                                 case OP_COND_EXC_NE_UN:
1792                                         ins->opcode = OP_SPARC_COND_EXC_NEZ;
1793                                         break;
1794                                 default:
1795                                         g_assert_not_reached ();
1796                                 }
1797                                 ins->sreg1 = last_ins->sreg1;
1798                                 *last_ins = *ins;
1799                                 MONO_DELETE_INS (bb, ins);
1800                                 continue;
1801                         }
1802                         break;
1803                 case OP_MOVE:
1804                         /* 
1805                          * OP_MOVE reg, reg 
1806                          */
1807                         if (ins->dreg == ins->sreg1) {
1808                                 MONO_DELETE_INS (bb, ins);
1809                                 continue;
1810                         }
1811                         /* 
1812                          * OP_MOVE sreg, dreg 
1813                          * OP_MOVE dreg, sreg
1814                          */
1815                         if (last_ins && last_ins->opcode == OP_MOVE &&
1816                             ins->sreg1 == last_ins->dreg &&
1817                             ins->dreg == last_ins->sreg1) {
1818                                 MONO_DELETE_INS (bb, ins);
1819                                 continue;
1820                         }
1821                         break;
1822                 }
1823                 last_ins = ins;
1824                 ins = ins->next;
1825         }
1826         bb->last_ins = last_ins;
1827 }
1828
1829 void
1830 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
1831 {
1832 }
1833
1834 /* FIXME: Strange loads from the stack in basic-float.cs:test_2_rem */
1835
1836 static void
1837 sparc_patch (guint32 *code, const gpointer target)
1838 {
1839         guint32 *c = code;
1840         guint32 ins = *code;
1841         guint32 op = ins >> 30;
1842         guint32 op2 = (ins >> 22) & 0x7;
1843         guint32 rd = (ins >> 25) & 0x1f;
1844         guint8* target8 = (guint8*)target;
1845         gint64 disp = (target8 - (guint8*)code) >> 2;
1846         int reg;
1847
1848 //      g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
1849
1850         if ((op == 0) && (op2 == 2)) {
1851                 if (!sparc_is_imm22 (disp))
1852                         NOT_IMPLEMENTED;
1853                 /* Bicc */
1854                 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1855         }
1856         else if ((op == 0) && (op2 == 1)) {
1857                 if (!sparc_is_imm19 (disp))
1858                         NOT_IMPLEMENTED;
1859                 /* BPcc */
1860                 *code = ((ins >> 19) << 19) | (disp & 0x7ffff);
1861         }
1862         else if ((op == 0) && (op2 == 3)) {
1863                 if (!sparc_is_imm16 (disp))
1864                         NOT_IMPLEMENTED;
1865                 /* BPr */
1866                 *code &= ~(0x180000 | 0x3fff);
1867                 *code |= ((disp << 21) & (0x180000)) | (disp & 0x3fff);
1868         }
1869         else if ((op == 0) && (op2 == 6)) {
1870                 if (!sparc_is_imm22 (disp))
1871                         NOT_IMPLEMENTED;
1872                 /* FBicc */
1873                 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
1874         }
1875         else if ((op == 0) && (op2 == 4)) {
1876                 guint32 ins2 = code [1];
1877
1878                 if (((ins2 >> 30) == 2) && (((ins2 >> 19) & 0x3f) == 2)) {
1879                         /* sethi followed by or */                      
1880                         guint32 *p = code;
1881                         sparc_set (p, target8, rd);
1882                         while (p <= (code + 1))
1883                                 sparc_nop (p);
1884                 }
1885                 else if (ins2 == 0x01000000) {
1886                         /* sethi followed by nop */
1887                         guint32 *p = code;
1888                         sparc_set (p, target8, rd);
1889                         while (p <= (code + 1))
1890                                 sparc_nop (p);
1891                 }
1892                 else if ((sparc_inst_op (ins2) == 3) && (sparc_inst_imm (ins2))) {
1893                         /* sethi followed by load/store */
1894 #ifndef SPARCV9
1895                         guint32 t = (guint32)target8;
1896                         *code &= ~(0x3fffff);
1897                         *code |= (t >> 10);
1898                         *(code + 1) &= ~(0x3ff);
1899                         *(code + 1) |= (t & 0x3ff);
1900 #endif
1901                 }
1902                 else if (v64 && 
1903                                  (sparc_inst_rd (ins) == sparc_g1) &&
1904                                  (sparc_inst_op (c [1]) == 0) && (sparc_inst_op2 (c [1]) == 4) &&
1905                                  (sparc_inst_op (c [2]) == 2) && (sparc_inst_op3 (c [2]) == 2) &&
1906                                  (sparc_inst_op (c [3]) == 2) && (sparc_inst_op3 (c [3]) == 2))
1907                 {
1908                         /* sparc_set */
1909                         guint32 *p = c;
1910                         reg = sparc_inst_rd (c [1]);
1911                         sparc_set (p, target8, reg);
1912                         while (p < (c + 6))
1913                                 sparc_nop (p);
1914                 }
1915                 else if ((sparc_inst_op (ins2) == 2) && (sparc_inst_op3 (ins2) == 0x38) && 
1916                                  (sparc_inst_imm (ins2))) {
1917                         /* sethi followed by jmpl */
1918 #ifndef SPARCV9
1919                         guint32 t = (guint32)target8;
1920                         *code &= ~(0x3fffff);
1921                         *code |= (t >> 10);
1922                         *(code + 1) &= ~(0x3ff);
1923                         *(code + 1) |= (t & 0x3ff);
1924 #endif
1925                 }
1926                 else
1927                         NOT_IMPLEMENTED;
1928         }
1929         else if (op == 01) {
1930                 gint64 disp = (target8 - (guint8*)code) >> 2;
1931
1932                 if (!sparc_is_imm30 (disp))
1933                         NOT_IMPLEMENTED;
1934                 sparc_call_simple (code, target8 - (guint8*)code);
1935         }
1936         else if ((op == 2) && (sparc_inst_op3 (ins) == 0x2) && sparc_inst_imm (ins)) {
1937                 /* mov imm, reg */
1938                 g_assert (sparc_is_imm13 (target8));
1939                 *code &= ~(0x1fff);
1940                 *code |= (guint32)target8;
1941         }
1942         else if ((sparc_inst_op (ins) == 2) && (sparc_inst_op3 (ins) == 0x7)) {
1943                 /* sparc_set case 5. */
1944                 guint32 *p = c;
1945
1946                 g_assert (v64);
1947                 reg = sparc_inst_rd (c [3]);
1948                 sparc_set (p, target, reg);
1949                 while (p < (c + 6))
1950                         sparc_nop (p);
1951         }
1952         else
1953                 NOT_IMPLEMENTED;
1954
1955 //      g_print ("patched with 0x%08x\n", ins);
1956 }
1957
1958 /*
1959  * mono_sparc_emit_save_lmf:
1960  *
1961  *  Emit the code neccesary to push a new entry onto the lmf stack. Used by
1962  * trampolines as well.
1963  */
1964 guint32*
1965 mono_sparc_emit_save_lmf (guint32 *code, guint32 lmf_offset)
1966 {
1967         /* Save lmf_addr */
1968         sparc_sti_imm (code, sparc_o0, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr));
1969         /* Save previous_lmf */
1970         sparc_ldi (code, sparc_o0, sparc_g0, sparc_o7);
1971         sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf));
1972         /* Set new lmf */
1973         sparc_add_imm (code, FALSE, sparc_fp, lmf_offset, sparc_o7);
1974         sparc_sti (code, sparc_o7, sparc_o0, sparc_g0);
1975
1976         return code;
1977 }
1978
1979 guint32*
1980 mono_sparc_emit_restore_lmf (guint32 *code, guint32 lmf_offset)
1981 {
1982         /* Load previous_lmf */
1983         sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sparc_l0);
1984         /* Load lmf_addr */
1985         sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sparc_l1);
1986         /* *(lmf) = previous_lmf */
1987         sparc_sti (code, sparc_l0, sparc_l1, sparc_g0);
1988         return code;
1989 }
1990
1991 static guint32*
1992 emit_save_sp_to_lmf (MonoCompile *cfg, guint32 *code)
1993 {
1994         /*
1995          * Since register windows are saved to the current value of %sp, we need to
1996          * set the sp field in the lmf before the call, not in the prolog.
1997          */
1998         if (cfg->method->save_lmf) {
1999                 gint32 lmf_offset = MONO_SPARC_STACK_BIAS - cfg->arch.lmf_offset;
2000
2001                 /* Save sp */
2002                 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
2003         }
2004
2005         return code;
2006 }
2007
2008 static guint32*
2009 emit_vret_token (MonoGenericSharingContext *gsctx, MonoInst *ins, guint32 *code)
2010 {
2011         MonoCallInst *call = (MonoCallInst*)ins;
2012         guint32 size;
2013
2014         /* 
2015          * The sparc ABI requires that calls to functions which return a structure
2016          * contain an additional unimpl instruction which is checked by the callee.
2017          */
2018         if (call->signature->pinvoke && MONO_TYPE_ISSTRUCT(call->signature->ret)) {
2019                 if (call->signature->ret->type == MONO_TYPE_TYPEDBYREF)
2020                         size = mini_type_stack_size (gsctx, call->signature->ret, NULL);
2021                 else
2022                         size = mono_class_native_size (call->signature->ret->data.klass, NULL);
2023                 sparc_unimp (code, size & 0xfff);
2024         }
2025
2026         return code;
2027 }
2028
2029 static guint32*
2030 emit_move_return_value (MonoInst *ins, guint32 *code)
2031 {
2032         /* Move return value to the target register */
2033         /* FIXME: do more things in the local reg allocator */
2034         switch (ins->opcode) {
2035         case OP_VOIDCALL:
2036         case OP_VOIDCALL_REG:
2037         case OP_VOIDCALL_MEMBASE:
2038                 break;
2039         case OP_CALL:
2040         case OP_CALL_REG:
2041         case OP_CALL_MEMBASE:
2042                 g_assert (ins->dreg == sparc_o0);
2043                 break;
2044         case OP_LCALL:
2045         case OP_LCALL_REG:
2046         case OP_LCALL_MEMBASE:
2047                 /* 
2048                  * ins->dreg is the least significant reg due to the lreg: LCALL rule
2049                  * in inssel-long32.brg.
2050                  */
2051 #ifdef SPARCV9
2052                 sparc_mov_reg_reg (code, sparc_o0, ins->dreg);
2053 #else
2054                 g_assert (ins->dreg == sparc_o1);
2055 #endif
2056                 break;
2057         case OP_FCALL:
2058         case OP_FCALL_REG:
2059         case OP_FCALL_MEMBASE:
2060 #ifdef SPARCV9
2061                 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
2062                         sparc_fmovs (code, sparc_f0, ins->dreg);
2063                         sparc_fstod (code, ins->dreg, ins->dreg);
2064                 }
2065                 else
2066                         sparc_fmovd (code, sparc_f0, ins->dreg);
2067 #else           
2068                 sparc_fmovs (code, sparc_f0, ins->dreg);
2069                 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4)
2070                         sparc_fstod (code, ins->dreg, ins->dreg);
2071                 else
2072                         sparc_fmovs (code, sparc_f1, ins->dreg + 1);
2073 #endif
2074                 break;
2075         case OP_VCALL:
2076         case OP_VCALL_REG:
2077         case OP_VCALL_MEMBASE:
2078         case OP_VCALL2:
2079         case OP_VCALL2_REG:
2080         case OP_VCALL2_MEMBASE:
2081                 break;
2082         default:
2083                 NOT_IMPLEMENTED;
2084         }
2085
2086         return code;
2087 }
2088
2089 /*
2090  * emit_load_volatile_arguments:
2091  *
2092  *  Load volatile arguments from the stack to the original input registers.
2093  * Required before a tail call.
2094  */
2095 static guint32*
2096 emit_load_volatile_arguments (MonoCompile *cfg, guint32 *code)
2097 {
2098         MonoMethod *method = cfg->method;
2099         MonoMethodSignature *sig;
2100         MonoInst *inst;
2101         CallInfo *cinfo;
2102         guint32 i, ireg;
2103
2104         /* FIXME: Generate intermediate code instead */
2105
2106         sig = mono_method_signature (method);
2107
2108         cinfo = get_call_info (cfg, sig, FALSE);
2109         
2110         /* This is the opposite of the code in emit_prolog */
2111
2112         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2113                 ArgInfo *ainfo = cinfo->args + i;
2114                 gint32 stack_offset;
2115                 MonoType *arg_type;
2116
2117                 inst = cfg->args [i];
2118
2119                 if (sig->hasthis && (i == 0))
2120                         arg_type = &mono_defaults.object_class->byval_arg;
2121                 else
2122                         arg_type = sig->params [i - sig->hasthis];
2123
2124                 stack_offset = ainfo->offset + ARGS_OFFSET;
2125                 ireg = sparc_i0 + ainfo->reg;
2126
2127                 if (ainfo->storage == ArgInSplitRegStack) {
2128                         g_assert (inst->opcode == OP_REGOFFSET);
2129
2130                         if (!sparc_is_imm13 (stack_offset))
2131                                 NOT_IMPLEMENTED;
2132                         sparc_st_imm (code, inst->inst_basereg, stack_offset, sparc_i5);
2133                 }
2134
2135                 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
2136                         if (ainfo->storage == ArgInIRegPair) {
2137                                 if (!sparc_is_imm13 (inst->inst_offset + 4))
2138                                         NOT_IMPLEMENTED;
2139                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2140                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2141                         }
2142                         else
2143                                 if (ainfo->storage == ArgInSplitRegStack) {
2144                                         if (stack_offset != inst->inst_offset) {
2145                                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_i5);
2146                                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2147                                                 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2148
2149                                         }
2150                                 }
2151                         else
2152                                 if (ainfo->storage == ArgOnStackPair) {
2153                                         if (stack_offset != inst->inst_offset) {
2154                                                 /* stack_offset is not dword aligned, so we need to make a copy */
2155                                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_o7);
2156                                                 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset);
2157
2158                                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2159                                                 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2160
2161                                         }
2162                                 }
2163                          else
2164                                 g_assert_not_reached ();
2165                 }
2166                 else
2167                         if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
2168                                 /* Argument in register, but need to be saved to stack */
2169                                 if (!sparc_is_imm13 (stack_offset))
2170                                         NOT_IMPLEMENTED;
2171                                 if ((stack_offset - ARGS_OFFSET) & 0x1)
2172                                         /* FIXME: Is this ldsb or ldub ? */
2173                                         sparc_ldsb_imm (code, inst->inst_basereg, stack_offset, ireg);
2174                                 else
2175                                         if ((stack_offset - ARGS_OFFSET) & 0x2)
2176                                                 sparc_ldsh_imm (code, inst->inst_basereg, stack_offset, ireg);
2177                                 else
2178                                         if ((stack_offset - ARGS_OFFSET) & 0x4)
2179                                                 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2180                                         else {
2181                                                 if (v64)
2182                                                         sparc_ldx_imm (code, inst->inst_basereg, stack_offset, ireg);
2183                                                 else
2184                                                         sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2185                                         }
2186                         }
2187                         else if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
2188                                 /* Argument in regpair, but need to be saved to stack */
2189                                 if (!sparc_is_imm13 (inst->inst_offset + 4))
2190                                         NOT_IMPLEMENTED;
2191                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2192                                 sparc_st_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2193                         }
2194                         else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
2195                                 NOT_IMPLEMENTED;
2196                         }
2197                         else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
2198                                 NOT_IMPLEMENTED;
2199                         }
2200
2201                 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
2202                         if (inst->opcode == OP_REGVAR)
2203                                 /* FIXME: Load the argument into memory */
2204                                 NOT_IMPLEMENTED;
2205         }
2206
2207         g_free (cinfo);
2208
2209         return code;
2210 }
2211
2212 /*
2213  * mono_sparc_is_virtual_call:
2214  *
2215  *  Determine whenever the instruction at CODE is a virtual call.
2216  */
2217 gboolean 
2218 mono_sparc_is_virtual_call (guint32 *code)
2219 {
2220         guint32 buf[1];
2221         guint32 *p;
2222
2223         p = buf;
2224
2225         if ((sparc_inst_op (*code) == 0x2) && (sparc_inst_op3 (*code) == 0x38)) {
2226                 /*
2227                  * Register indirect call. If it is a virtual call, then the 
2228                  * instruction in the delay slot is a special kind of nop.
2229                  */
2230
2231                 /* Construct special nop */
2232                 sparc_or_imm (p, FALSE, sparc_g0, 0xca, sparc_g0);
2233                 p --;
2234
2235                 if (code [1] == p [0])
2236                         return TRUE;
2237         }
2238
2239         return FALSE;
2240 }
2241
2242 /*
2243  * mono_arch_get_vcall_slot:
2244  *
2245  *  Determine the vtable slot used by a virtual call.
2246  */
2247 gpointer
2248 mono_arch_get_vcall_slot (guint8 *code8, gpointer *regs, int *displacement)
2249 {
2250         guint32 *code = (guint32*)(gpointer)code8;
2251         guint32 ins = code [0];
2252         guint32 prev_ins = code [-1];
2253
2254         mono_sparc_flushw ();
2255
2256         *displacement = 0;
2257
2258         if (!mono_sparc_is_virtual_call (code))
2259                 return NULL;
2260
2261         if ((sparc_inst_op (ins) == 0x2) && (sparc_inst_op3 (ins) == 0x38)) {
2262                 if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 1) && (sparc_inst_op3 (prev_ins) == 0 || sparc_inst_op3 (prev_ins) == 0xb)) {
2263                         /* ld [r1 + CONST ], r2; call r2 */
2264                         guint32 base = sparc_inst_rs1 (prev_ins);
2265                         gint32 disp = (((gint32)(sparc_inst_imm13 (prev_ins))) << 19) >> 19;
2266                         gpointer base_val;
2267
2268                         g_assert (sparc_inst_rd (prev_ins) == sparc_inst_rs1 (ins));
2269
2270                         g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2271
2272                         base_val = regs [base];
2273
2274                         *displacement = disp;
2275
2276                         return (gpointer)base_val;
2277                 }
2278                 else if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 0) && (sparc_inst_op3 (prev_ins) == 0)) {
2279                         /* set r1, ICONST; ld [r1 + r2], r2; call r2 */
2280                         /* Decode a sparc_set32 */
2281                         guint32 base = sparc_inst_rs1 (prev_ins);
2282                         guint32 disp;
2283                         gpointer base_val;
2284                         guint32 s1 = code [-3];
2285                         guint32 s2 = code [-2];
2286
2287 #ifdef SPARCV9
2288                         NOT_IMPLEMENTED;
2289 #endif
2290
2291                         /* sparc_sethi */
2292                         g_assert (sparc_inst_op (s1) == 0);
2293                         g_assert (sparc_inst_op2 (s1) == 4);
2294
2295                         /* sparc_or_imm */
2296                         g_assert (sparc_inst_op (s2) == 2);
2297                         g_assert (sparc_inst_op3 (s2) == 2);
2298                         g_assert (sparc_inst_i (s2) == 1);
2299                         g_assert (sparc_inst_rs1 (s2) == sparc_inst_rd (s2));
2300                         g_assert (sparc_inst_rd (s1) == sparc_inst_rs1 (s2));
2301
2302                         disp = ((s1 & 0x3fffff) << 10) | sparc_inst_imm13 (s2);
2303
2304                         g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2305
2306                         base_val = regs [base];
2307
2308                         *displacement = disp;
2309
2310                         return (gpointer)base_val;
2311                 } else
2312                         g_assert_not_reached ();
2313         }
2314         else
2315                 g_assert_not_reached ();
2316
2317         return NULL;
2318 }
2319
2320 #define CMP_SIZE 3
2321 #define BR_SMALL_SIZE 2
2322 #define BR_LARGE_SIZE 2
2323 #define JUMP_IMM_SIZE 5
2324 #define ENABLE_WRONG_METHOD_CHECK 0
2325
2326 /*
2327  * LOCKING: called with the domain lock held
2328  */
2329 gpointer
2330 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
2331         gpointer fail_tramp)
2332 {
2333         int i;
2334         int size = 0;
2335         guint32 *code, *start;
2336
2337         g_assert (!fail_tramp);
2338
2339         for (i = 0; i < count; ++i) {
2340                 MonoIMTCheckItem *item = imt_entries [i];
2341                 if (item->is_equals) {
2342                         if (item->check_target_idx) {
2343                                 if (!item->compare_done)
2344                                         item->chunk_size += CMP_SIZE;
2345                                 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
2346                         } else {
2347                                 item->chunk_size += JUMP_IMM_SIZE;
2348 #if ENABLE_WRONG_METHOD_CHECK
2349                                 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
2350 #endif
2351                         }
2352                 } else {
2353                         item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
2354                         imt_entries [item->check_target_idx]->compare_done = TRUE;
2355                 }
2356                 size += item->chunk_size;
2357         }
2358         code = mono_domain_code_reserve (domain, size * 4);
2359         start = code;
2360
2361         for (i = 0; i < count; ++i) {
2362                 MonoIMTCheckItem *item = imt_entries [i];
2363                 item->code_target = (guint8*)code;
2364                 if (item->is_equals) {
2365                         if (item->check_target_idx) {
2366                                 if (!item->compare_done) {
2367                                         sparc_set (code, (guint32)item->key, sparc_g5);
2368                                         sparc_cmp (code, MONO_ARCH_IMT_REG, sparc_g5);
2369                                 }
2370                                 item->jmp_code = (guint8*)code;
2371                                 sparc_branch (code, 0, sparc_bne, 0);
2372                                 sparc_nop (code);
2373                                 sparc_set (code, ((guint32)(&(vtable->vtable [item->value.vtable_slot]))), sparc_g5);
2374                                 sparc_ld (code, sparc_g5, 0, sparc_g5);
2375                                 sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
2376                                 sparc_nop (code);
2377                         } else {
2378                                 /* enable the commented code to assert on wrong method */
2379 #if ENABLE_WRONG_METHOD_CHECK
2380                                 g_assert_not_reached ();
2381 #endif
2382                                 sparc_set (code, ((guint32)(&(vtable->vtable [item->value.vtable_slot]))), sparc_g5);
2383                                 sparc_ld (code, sparc_g5, 0, sparc_g5);
2384                                 sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
2385                                 sparc_nop (code);
2386 #if ENABLE_WRONG_METHOD_CHECK
2387                                 g_assert_not_reached ();
2388 #endif
2389                         }
2390                 } else {
2391                         sparc_set (code, (guint32)item->key, sparc_g5);
2392                         sparc_cmp (code, MONO_ARCH_IMT_REG, sparc_g5);
2393                         item->jmp_code = (guint8*)code;
2394                         sparc_branch (code, 0, sparc_beu, 0);
2395                         sparc_nop (code);
2396                 }
2397         }
2398         /* patch the branches to get to the target items */
2399         for (i = 0; i < count; ++i) {
2400                 MonoIMTCheckItem *item = imt_entries [i];
2401                 if (item->jmp_code) {
2402                         if (item->check_target_idx) {
2403                                 sparc_patch ((guint32*)item->jmp_code, imt_entries [item->check_target_idx]->code_target);
2404                         }
2405                 }
2406         }
2407
2408         mono_arch_flush_icache ((guint8*)start, (code - start) * 4);
2409
2410         mono_stats.imt_thunks_size += (code - start) * 4;
2411         g_assert (code - start <= size);
2412         return start;
2413 }
2414
2415 MonoMethod*
2416 mono_arch_find_imt_method (gpointer *regs, guint8 *code)
2417 {
2418 #ifdef SPARCV9
2419         g_assert_not_reached ();
2420 #endif
2421
2422         return (MonoMethod*)regs [sparc_g1];
2423 }
2424
2425 MonoObject*
2426 mono_arch_find_this_argument (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
2427 {
2428         mono_sparc_flushw ();
2429
2430         return (gpointer)regs [sparc_o0];
2431 }
2432
2433 /*
2434  * Some conventions used in the following code.
2435  * 2) The only scratch registers we have are o7 and g1.  We try to
2436  * stick to o7 when we can, and use g1 when necessary.
2437  */
2438
2439 void
2440 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2441 {
2442         MonoInst *ins;
2443         MonoCallInst *call;
2444         guint offset;
2445         guint32 *code = (guint32*)(cfg->native_code + cfg->code_len);
2446         MonoInst *last_ins = NULL;
2447         int max_len, cpos;
2448         const char *spec;
2449
2450         if (cfg->verbose_level > 2)
2451                 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2452
2453         cpos = bb->max_offset;
2454
2455         if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2456                 NOT_IMPLEMENTED;
2457         }
2458
2459         MONO_BB_FOR_EACH_INS (bb, ins) {
2460                 guint8* code_start;
2461
2462                 offset = (guint8*)code - cfg->native_code;
2463
2464                 spec = ins_get_spec (ins->opcode);
2465
2466                 max_len = ((guint8 *)spec)[MONO_INST_LEN];
2467
2468                 if (offset > (cfg->code_size - max_len - 16)) {
2469                         cfg->code_size *= 2;
2470                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2471                         code = (guint32*)(cfg->native_code + offset);
2472                 }
2473                 code_start = (guint8*)code;
2474                 //      if (ins->cil_code)
2475                 //              g_print ("cil code\n");
2476                 mono_debug_record_line_number (cfg, ins, offset);
2477
2478                 switch (ins->opcode) {
2479                 case OP_STOREI1_MEMBASE_IMM:
2480                         EMIT_STORE_MEMBASE_IMM (ins, stb);
2481                         break;
2482                 case OP_STOREI2_MEMBASE_IMM:
2483                         EMIT_STORE_MEMBASE_IMM (ins, sth);
2484                         break;
2485                 case OP_STORE_MEMBASE_IMM:
2486                         EMIT_STORE_MEMBASE_IMM (ins, sti);
2487                         break;
2488                 case OP_STOREI4_MEMBASE_IMM:
2489                         EMIT_STORE_MEMBASE_IMM (ins, st);
2490                         break;
2491                 case OP_STOREI8_MEMBASE_IMM:
2492 #ifdef SPARCV9
2493                         EMIT_STORE_MEMBASE_IMM (ins, stx);
2494 #else
2495                         /* Only generated by peephole opts */
2496                         g_assert ((ins->inst_offset % 8) == 0);
2497                         g_assert (ins->inst_imm == 0);
2498                         EMIT_STORE_MEMBASE_IMM (ins, stx);
2499 #endif
2500                         break;
2501                 case OP_STOREI1_MEMBASE_REG:
2502                         EMIT_STORE_MEMBASE_REG (ins, stb);
2503                         break;
2504                 case OP_STOREI2_MEMBASE_REG:
2505                         EMIT_STORE_MEMBASE_REG (ins, sth);
2506                         break;
2507                 case OP_STOREI4_MEMBASE_REG:
2508                         EMIT_STORE_MEMBASE_REG (ins, st);
2509                         break;
2510                 case OP_STOREI8_MEMBASE_REG:
2511 #ifdef SPARCV9
2512                         EMIT_STORE_MEMBASE_REG (ins, stx);
2513 #else
2514                         /* Only used by OP_MEMSET */
2515                         EMIT_STORE_MEMBASE_REG (ins, std);
2516 #endif
2517                         break;
2518                 case OP_STORE_MEMBASE_REG:
2519                         EMIT_STORE_MEMBASE_REG (ins, sti);
2520                         break;
2521                 case OP_LOADU4_MEM:
2522                         sparc_set (code, ins->inst_c0, ins->dreg);
2523                         sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
2524                         break;
2525                 case OP_LOADI4_MEMBASE:
2526 #ifdef SPARCV9
2527                         EMIT_LOAD_MEMBASE (ins, ldsw);
2528 #else
2529                         EMIT_LOAD_MEMBASE (ins, ld);
2530 #endif
2531                         break;
2532                 case OP_LOADU4_MEMBASE:
2533                         EMIT_LOAD_MEMBASE (ins, ld);
2534                         break;
2535                 case OP_LOADU1_MEMBASE:
2536                         EMIT_LOAD_MEMBASE (ins, ldub);
2537                         break;
2538                 case OP_LOADI1_MEMBASE:
2539                         EMIT_LOAD_MEMBASE (ins, ldsb);
2540                         break;
2541                 case OP_LOADU2_MEMBASE:
2542                         EMIT_LOAD_MEMBASE (ins, lduh);
2543                         break;
2544                 case OP_LOADI2_MEMBASE:
2545                         EMIT_LOAD_MEMBASE (ins, ldsh);
2546                         break;
2547                 case OP_LOAD_MEMBASE:
2548 #ifdef SPARCV9
2549                                 EMIT_LOAD_MEMBASE (ins, ldx);
2550 #else
2551                                 EMIT_LOAD_MEMBASE (ins, ld);
2552 #endif
2553                         break;
2554 #ifdef SPARCV9
2555                 case OP_LOADI8_MEMBASE:
2556                         EMIT_LOAD_MEMBASE (ins, ldx);
2557                         break;
2558 #endif
2559                 case OP_ICONV_TO_I1:
2560                         sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
2561                         sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
2562                         break;
2563                 case OP_ICONV_TO_I2:
2564                         sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2565                         sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
2566                         break;
2567                 case OP_ICONV_TO_U1:
2568                         sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
2569                         break;
2570                 case OP_ICONV_TO_U2:
2571                         sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2572                         sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
2573                         break;
2574                 case OP_LCONV_TO_OVF_U4:
2575                 case OP_ICONV_TO_OVF_U4:
2576                         /* Only used on V9 */
2577                         sparc_cmp_imm (code, ins->sreg1, 0);
2578                         mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2579                                                                  MONO_PATCH_INFO_EXC, "OverflowException");
2580                         sparc_branchp (code, 0, sparc_bl, sparc_xcc_short, 0, 0);
2581                         /* Delay slot */
2582                         sparc_set (code, 1, sparc_o7);
2583                         sparc_sllx_imm (code, sparc_o7, 32, sparc_o7);
2584                         sparc_cmp (code, ins->sreg1, sparc_o7);
2585                         mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2586                                                                  MONO_PATCH_INFO_EXC, "OverflowException");
2587                         sparc_branchp (code, 0, sparc_bge, sparc_xcc_short, 0, 0);
2588                         sparc_nop (code);
2589                         sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2590                         break;
2591                 case OP_LCONV_TO_OVF_I4_UN:
2592                 case OP_ICONV_TO_OVF_I4_UN:
2593                         /* Only used on V9 */
2594                         NOT_IMPLEMENTED;
2595                         break;
2596                 case OP_COMPARE:
2597                 case OP_LCOMPARE:
2598                 case OP_ICOMPARE:
2599                         sparc_cmp (code, ins->sreg1, ins->sreg2);
2600                         break;
2601                 case OP_COMPARE_IMM:
2602                 case OP_ICOMPARE_IMM:
2603                         if (sparc_is_imm13 (ins->inst_imm))
2604                                 sparc_cmp_imm (code, ins->sreg1, ins->inst_imm);
2605                         else {
2606                                 sparc_set (code, ins->inst_imm, sparc_o7);
2607                                 sparc_cmp (code, ins->sreg1, sparc_o7);
2608                         }
2609                         break;
2610                 case OP_BREAK:
2611                         /*
2612                          * gdb does not like encountering 'ta 1' in the debugged code. So 
2613                          * instead of emitting a trap, we emit a call a C function and place a 
2614                          * breakpoint there.
2615                          */
2616                         //sparc_ta (code, 1);
2617                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, mono_break);
2618                         EMIT_CALL();
2619                         break;
2620                 case OP_ADDCC:
2621                 case OP_IADDCC:
2622                         sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2623                         break;
2624                 case OP_IADD:
2625                         sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2626                         break;
2627                 case OP_ADDCC_IMM:
2628                 case OP_ADD_IMM:
2629                 case OP_IADD_IMM:
2630                         /* according to inssel-long32.brg, this should set cc */
2631                         EMIT_ALU_IMM (ins, add, TRUE);
2632                         break;
2633                 case OP_ADC:
2634                 case OP_IADC:
2635                         /* according to inssel-long32.brg, this should set cc */
2636                         sparc_addx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2637                         break;
2638                 case OP_ADC_IMM:
2639                 case OP_IADC_IMM:
2640                         EMIT_ALU_IMM (ins, addx, TRUE);
2641                         break;
2642                 case OP_SUBCC:
2643                 case OP_ISUBCC:
2644                         sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2645                         break;
2646                 case OP_ISUB:
2647                         sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2648                         break;
2649                 case OP_SUBCC_IMM:
2650                 case OP_SUB_IMM:
2651                 case OP_ISUB_IMM:
2652                         /* according to inssel-long32.brg, this should set cc */
2653                         EMIT_ALU_IMM (ins, sub, TRUE);
2654                         break;
2655                 case OP_SBB:
2656                 case OP_ISBB:
2657                         /* according to inssel-long32.brg, this should set cc */
2658                         sparc_subx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2659                         break;
2660                 case OP_SBB_IMM:
2661                 case OP_ISBB_IMM:
2662                         EMIT_ALU_IMM (ins, subx, TRUE);
2663                         break;
2664                 case OP_IAND:
2665                         sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2666                         break;
2667                 case OP_AND_IMM:
2668                 case OP_IAND_IMM:
2669                         EMIT_ALU_IMM (ins, and, FALSE);
2670                         break;
2671                 case OP_IDIV:
2672                         /* Sign extend sreg1 into %y */
2673                         sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2674                         sparc_wry (code, sparc_o7, sparc_g0);
2675                         sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2676                         EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2677                         break;
2678                 case OP_IDIV_UN:
2679                         sparc_wry (code, sparc_g0, sparc_g0);
2680                         sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2681                         break;
2682                 case OP_DIV_IMM:
2683                 case OP_IDIV_IMM: {
2684                         int i, imm;
2685
2686                         /* Transform division into a shift */
2687                         for (i = 1; i < 30; ++i) {
2688                                 imm = (1 << i);
2689                                 if (ins->inst_imm == imm)
2690                                         break;
2691                         }
2692                         if (i < 30) {
2693                                 if (i == 1) {
2694                                         /* gcc 2.95.3 */
2695                                         sparc_srl_imm (code, ins->sreg1, 31, sparc_o7);
2696                                         sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2697                                         sparc_sra_imm (code, ins->dreg, 1, ins->dreg);
2698                                 }
2699                                 else {
2700                                         /* http://compilers.iecc.com/comparch/article/93-04-079 */
2701                                         sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2702                                         sparc_srl_imm (code, sparc_o7, 32 - i, sparc_o7);
2703                                         sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2704                                         sparc_sra_imm (code, ins->dreg, i, ins->dreg);
2705                                 }
2706                         }
2707                         else {
2708                                 /* Sign extend sreg1 into %y */
2709                                 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2710                                 sparc_wry (code, sparc_o7, sparc_g0);
2711                                 EMIT_ALU_IMM (ins, sdiv, TRUE);
2712                                 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2713                         }
2714                         break;
2715                 }
2716                 case OP_IDIV_UN_IMM:
2717                         sparc_wry (code, sparc_g0, sparc_g0);
2718                         EMIT_ALU_IMM (ins, udiv, FALSE);
2719                         break;
2720                 case OP_IREM:
2721                         /* Sign extend sreg1 into %y */
2722                         sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2723                         sparc_wry (code, sparc_o7, sparc_g0);
2724                         sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, sparc_o7);
2725                         EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2726                         sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2727                         sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2728                         break;
2729                 case OP_IREM_UN:
2730                         sparc_wry (code, sparc_g0, sparc_g0);
2731                         sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2732                         sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2733                         sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2734                         break;
2735                 case OP_REM_IMM:
2736                 case OP_IREM_IMM:
2737                         /* Sign extend sreg1 into %y */
2738                         sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2739                         sparc_wry (code, sparc_o7, sparc_g0);
2740                         if (!sparc_is_imm13 (ins->inst_imm)) {
2741                                 sparc_set (code, ins->inst_imm, GP_SCRATCH_REG);
2742                                 sparc_sdiv (code, TRUE, ins->sreg1, GP_SCRATCH_REG, sparc_o7);
2743                                 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2744                                 sparc_smul (code, FALSE, sparc_o7, GP_SCRATCH_REG, sparc_o7);
2745                         }
2746                         else {
2747                                 sparc_sdiv_imm (code, TRUE, ins->sreg1, ins->inst_imm, sparc_o7);
2748                                 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2749                                 sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
2750                         }
2751                         sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2752                         break;
2753                 case OP_IOR:
2754                         sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2755                         break;
2756                 case OP_OR_IMM:
2757                 case OP_IOR_IMM:
2758                         EMIT_ALU_IMM (ins, or, FALSE);
2759                         break;
2760                 case OP_IXOR:
2761                         sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2762                         break;
2763                 case OP_XOR_IMM:
2764                 case OP_IXOR_IMM:
2765                         EMIT_ALU_IMM (ins, xor, FALSE);
2766                         break;
2767                 case OP_ISHL:
2768                         sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
2769                         break;
2770                 case OP_SHL_IMM:
2771                 case OP_ISHL_IMM:
2772                         if (ins->inst_imm < (1 << 5))
2773                                 sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2774                         else {
2775                                 sparc_set (code, ins->inst_imm, sparc_o7);
2776                                 sparc_sll (code, ins->sreg1, sparc_o7, ins->dreg);
2777                         }
2778                         break;
2779                 case OP_ISHR:
2780                         sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
2781                         break;
2782                 case OP_ISHR_IMM:
2783                 case OP_SHR_IMM:
2784                         if (ins->inst_imm < (1 << 5))
2785                                 sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2786                         else {
2787                                 sparc_set (code, ins->inst_imm, sparc_o7);
2788                                 sparc_sra (code, ins->sreg1, sparc_o7, ins->dreg);
2789                         }
2790                         break;
2791                 case OP_SHR_UN_IMM:
2792                 case OP_ISHR_UN_IMM:
2793                         if (ins->inst_imm < (1 << 5))
2794                                 sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2795                         else {
2796                                 sparc_set (code, ins->inst_imm, sparc_o7);
2797                                 sparc_srl (code, ins->sreg1, sparc_o7, ins->dreg);
2798                         }
2799                         break;
2800                 case OP_ISHR_UN:
2801                         sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
2802                         break;
2803                 case OP_LSHL:
2804                         sparc_sllx (code, ins->sreg1, ins->sreg2, ins->dreg);
2805                         break;
2806                 case OP_LSHL_IMM:
2807                         if (ins->inst_imm < (1 << 6))
2808                                 sparc_sllx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2809                         else {
2810                                 sparc_set (code, ins->inst_imm, sparc_o7);
2811                                 sparc_sllx (code, ins->sreg1, sparc_o7, ins->dreg);
2812                         }
2813                         break;
2814                 case OP_LSHR:
2815                         sparc_srax (code, ins->sreg1, ins->sreg2, ins->dreg);
2816                         break;
2817                 case OP_LSHR_IMM:
2818                         if (ins->inst_imm < (1 << 6))
2819                                 sparc_srax_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2820                         else {
2821                                 sparc_set (code, ins->inst_imm, sparc_o7);
2822                                 sparc_srax (code, ins->sreg1, sparc_o7, ins->dreg);
2823                         }
2824                         break;
2825                 case OP_LSHR_UN:
2826                         sparc_srlx (code, ins->sreg1, ins->sreg2, ins->dreg);
2827                         break;
2828                 case OP_LSHR_UN_IMM:
2829                         if (ins->inst_imm < (1 << 6))
2830                                 sparc_srlx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
2831                         else {
2832                                 sparc_set (code, ins->inst_imm, sparc_o7);
2833                                 sparc_srlx (code, ins->sreg1, sparc_o7, ins->dreg);
2834                         }
2835                         break;
2836                 case OP_INOT:
2837                         /* can't use sparc_not */
2838                         sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
2839                         break;
2840                 case OP_INEG:
2841                         /* can't use sparc_neg */
2842                         sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
2843                         break;
2844                 case OP_IMUL:
2845                         sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2846                         break;
2847                 case OP_IMUL_IMM:
2848                 case OP_MUL_IMM: {
2849                         int i, imm;
2850
2851                         if ((ins->inst_imm == 1) && (ins->sreg1 == ins->dreg))
2852                                 break;
2853
2854                         /* Transform multiplication into a shift */
2855                         for (i = 0; i < 30; ++i) {
2856                                 imm = (1 << i);
2857                                 if (ins->inst_imm == imm)
2858                                         break;
2859                         }
2860                         if (i < 30)
2861                                 sparc_sll_imm (code, ins->sreg1, i, ins->dreg);
2862                         else
2863                                 EMIT_ALU_IMM (ins, smul, FALSE);
2864                         break;
2865                 }
2866                 case OP_IMUL_OVF:
2867                         sparc_smul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2868                         sparc_rdy (code, sparc_g1);
2869                         sparc_sra_imm (code, ins->dreg, 31, sparc_o7);
2870                         sparc_cmp (code, sparc_g1, sparc_o7);
2871                         EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2872                         break;
2873                 case OP_IMUL_OVF_UN:
2874                         sparc_umul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2875                         sparc_rdy (code, sparc_o7);
2876                         sparc_cmp (code, sparc_o7, sparc_g0);
2877                         EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
2878                         break;
2879                 case OP_ICONST:
2880                         sparc_set (code, ins->inst_c0, ins->dreg);
2881                         break;
2882                 case OP_I8CONST:
2883                         sparc_set (code, ins->inst_l, ins->dreg);
2884                         break;
2885                 case OP_AOTCONST:
2886                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2887                         sparc_set_template (code, ins->dreg);
2888                         break;
2889                 case OP_JUMP_TABLE:
2890                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
2891                         sparc_set_template (code, ins->dreg);
2892                         break;
2893                 case OP_ICONV_TO_I4:
2894                 case OP_ICONV_TO_U4:
2895                 case OP_MOVE:
2896                         if (ins->sreg1 != ins->dreg)
2897                                 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2898                         break;
2899                 case OP_FMOVE:
2900 #ifdef SPARCV9
2901                         if (ins->sreg1 != ins->dreg)
2902                                 sparc_fmovd (code, ins->sreg1, ins->dreg);
2903 #else
2904                         sparc_fmovs (code, ins->sreg1, ins->dreg);
2905                         sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
2906 #endif
2907                         break;
2908                 case OP_JMP:
2909                         if (cfg->method->save_lmf)
2910                                 NOT_IMPLEMENTED;
2911
2912                         code = emit_load_volatile_arguments (cfg, code);
2913                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
2914                         sparc_set_template (code, sparc_o7);
2915                         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_g0);
2916                         /* Restore parent frame in delay slot */
2917                         sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
2918                         break;
2919                 case OP_CHECK_THIS:
2920                         /* ensure ins->sreg1 is not NULL */
2921                         /* Might be misaligned in case of vtypes so use a byte load */
2922                         sparc_ldsb_imm (code, ins->sreg1, 0, sparc_g0);
2923                         break;
2924                 case OP_ARGLIST:
2925                         sparc_add_imm (code, FALSE, sparc_fp, cfg->sig_cookie, sparc_o7);
2926                         sparc_sti_imm (code, sparc_o7, ins->sreg1, 0);
2927                         break;
2928                 case OP_FCALL:
2929                 case OP_LCALL:
2930                 case OP_VCALL:
2931                 case OP_VCALL2:
2932                 case OP_VOIDCALL:
2933                 case OP_CALL:
2934                         call = (MonoCallInst*)ins;
2935                         g_assert (!call->virtual);
2936                         code = emit_save_sp_to_lmf (cfg, code);
2937                         if (ins->flags & MONO_INST_HAS_METHOD)
2938                             code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
2939                         else
2940                             code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
2941
2942                         code = emit_vret_token (cfg->generic_sharing_context, ins, code);
2943                         code = emit_move_return_value (ins, code);
2944                         break;
2945                 case OP_FCALL_REG:
2946                 case OP_LCALL_REG:
2947                 case OP_VCALL_REG:
2948                 case OP_VCALL2_REG:
2949                 case OP_VOIDCALL_REG:
2950                 case OP_CALL_REG:
2951                         call = (MonoCallInst*)ins;
2952                         code = emit_save_sp_to_lmf (cfg, code);
2953                         sparc_jmpl (code, ins->sreg1, sparc_g0, sparc_callsite);
2954                         /*
2955                          * We emit a special kind of nop in the delay slot to tell the 
2956                          * trampoline code that this is a virtual call, thus an unbox
2957                          * trampoline might need to be called.
2958                          */
2959                         if (call->virtual)
2960                                 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2961                         else
2962                                 sparc_nop (code);
2963
2964                         code = emit_vret_token (cfg->generic_sharing_context, ins, code);
2965                         code = emit_move_return_value (ins, code);
2966                         break;
2967                 case OP_FCALL_MEMBASE:
2968                 case OP_LCALL_MEMBASE:
2969                 case OP_VCALL_MEMBASE:
2970                 case OP_VCALL2_MEMBASE:
2971                 case OP_VOIDCALL_MEMBASE:
2972                 case OP_CALL_MEMBASE:
2973                         call = (MonoCallInst*)ins;
2974                         code = emit_save_sp_to_lmf (cfg, code);
2975                         if (sparc_is_imm13 (ins->inst_offset)) {
2976                                 sparc_ldi_imm (code, ins->inst_basereg, ins->inst_offset, sparc_o7);
2977                         } else {
2978                                 sparc_set (code, ins->inst_offset, sparc_o7);
2979                                 sparc_ldi (code, ins->inst_basereg, sparc_o7, sparc_o7);
2980                         }
2981                         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
2982                         if (call->virtual)
2983                                 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
2984                         else
2985                                 sparc_nop (code);
2986
2987                         code = emit_vret_token (cfg->generic_sharing_context, ins, code);
2988                         code = emit_move_return_value (ins, code);
2989                         break;
2990                 case OP_SETFRET:
2991                         if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4)
2992                                 sparc_fdtos (code, ins->sreg1, sparc_f0);
2993                         else {
2994 #ifdef SPARCV9
2995                                 sparc_fmovd (code, ins->sreg1, ins->dreg);
2996 #else
2997                                 /* FIXME: Why not use fmovd ? */
2998                                 sparc_fmovs (code, ins->sreg1, ins->dreg);
2999                                 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3000 #endif
3001                         }
3002                         break;
3003                 case OP_LOCALLOC: {
3004                         guint32 size_reg;
3005                         gint32 offset2;
3006
3007 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3008                         /* Perform stack touching */
3009                         NOT_IMPLEMENTED;
3010 #endif
3011
3012                         /* Keep alignment */
3013                         /* Add 4 to compensate for the rounding of localloc_offset */
3014                         sparc_add_imm (code, FALSE, ins->sreg1, 4 + MONO_ARCH_LOCALLOC_ALIGNMENT - 1, ins->dreg);
3015                         sparc_set (code, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1), sparc_o7);
3016                         sparc_and (code, FALSE, ins->dreg, sparc_o7, ins->dreg);
3017
3018                         if ((ins->flags & MONO_INST_INIT) && (ins->sreg1 == ins->dreg)) {
3019 #ifdef SPARCV9
3020                                 size_reg = sparc_g4;
3021 #else
3022                                 size_reg = sparc_g1;
3023 #endif
3024                                 sparc_mov_reg_reg (code, ins->dreg, size_reg);
3025                         }
3026                         else
3027                                 size_reg = ins->sreg1;
3028
3029                         sparc_sub (code, FALSE, sparc_sp, ins->dreg, ins->dreg);
3030                         /* Keep %sp valid at all times */
3031                         sparc_mov_reg_reg (code, ins->dreg, sparc_sp);
3032                         /* Round localloc_offset too so the result is at least 8 aligned */
3033                         offset2 = ALIGN_TO (cfg->arch.localloc_offset, 8);
3034                         g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + offset2));
3035                         sparc_add_imm (code, FALSE, ins->dreg, MONO_SPARC_STACK_BIAS + offset2, ins->dreg);
3036
3037                         if (ins->flags & MONO_INST_INIT) {
3038                                 guint32 *br [3];
3039                                 /* Initialize memory region */
3040                                 sparc_cmp_imm (code, size_reg, 0);
3041                                 br [0] = code;
3042                                 sparc_branch (code, 0, sparc_be, 0);
3043                                 /* delay slot */
3044                                 sparc_set (code, 0, sparc_o7);
3045                                 sparc_sub_imm (code, 0, size_reg, sparcv9 ? 8 : 4, size_reg);
3046                                 /* start of loop */
3047                                 br [1] = code;
3048                                 if (sparcv9)
3049                                         sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
3050                                 else
3051                                         sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
3052                                 sparc_cmp (code, sparc_o7, size_reg);
3053                                 br [2] = code;
3054                                 sparc_branch (code, 0, sparc_bl, 0);
3055                                 sparc_patch (br [2], br [1]);
3056                                 /* delay slot */
3057                                 sparc_add_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
3058                                 sparc_patch (br [0], code);
3059                         }
3060                         break;
3061                 }
3062                 case OP_LOCALLOC_IMM: {
3063                         gint32 offset = ins->inst_imm;
3064                         gint32 offset2;
3065                         
3066 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3067                         /* Perform stack touching */
3068                         NOT_IMPLEMENTED;
3069 #endif
3070
3071                         /* To compensate for the rounding of localloc_offset */
3072                         offset += sizeof (gpointer);
3073                         offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
3074                         if (sparc_is_imm13 (offset))
3075                                 sparc_sub_imm (code, FALSE, sparc_sp, offset, sparc_sp);
3076                         else {
3077                                 sparc_set (code, offset, sparc_o7);
3078                                 sparc_sub (code, FALSE, sparc_sp, sparc_o7, sparc_sp);
3079                         }
3080                         /* Round localloc_offset too so the result is at least 8 aligned */
3081                         offset2 = ALIGN_TO (cfg->arch.localloc_offset, 8);
3082                         g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + offset2));
3083                         sparc_add_imm (code, FALSE, sparc_sp, MONO_SPARC_STACK_BIAS + offset2, ins->dreg);
3084                         if ((ins->flags & MONO_INST_INIT) && (offset > 0)) {
3085                                 guint32 *br [2];
3086                                 int i;
3087
3088                                 if (offset <= 16) {
3089                                         i = 0;
3090                                         while (i < offset) {
3091                                                 if (sparcv9) {
3092                                                         sparc_stx_imm (code, sparc_g0, ins->dreg, i);
3093                                                         i += 8;
3094                                                 }
3095                                                 else {
3096                                                         sparc_st_imm (code, sparc_g0, ins->dreg, i);
3097                                                         i += 4;
3098                                                 }
3099                                         }
3100                                 }
3101                                 else {
3102                                         sparc_set (code, offset, sparc_o7);
3103                                         sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
3104                                         /* beginning of loop */
3105                                         br [0] = code;
3106                                         if (sparcv9)
3107                                                 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
3108                                         else
3109                                                 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
3110                                         sparc_cmp_imm (code, sparc_o7, 0);
3111                                         br [1] = code;
3112                                         sparc_branch (code, 0, sparc_bne, 0);
3113                                         /* delay slot */
3114                                         sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
3115                                         sparc_patch (br [1], br [0]);
3116                                 }
3117                         }
3118                         break;
3119                 }
3120                 case OP_THROW:
3121                         sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3122                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3123                                              (gpointer)"mono_arch_throw_exception");
3124                         EMIT_CALL ();
3125                         break;
3126                 case OP_RETHROW:
3127                         sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3128                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3129                                              (gpointer)"mono_arch_rethrow_exception");
3130                         EMIT_CALL ();
3131                         break;
3132                 case OP_START_HANDLER: {
3133                         /*
3134                          * The START_HANDLER instruction marks the beginning of a handler 
3135                          * block. It is called using a call instruction, so %o7 contains 
3136                          * the return address. Since the handler executes in the same stack
3137              * frame as the method itself, we can't use save/restore to save 
3138                          * the return address. Instead, we save it into a dedicated 
3139                          * variable.
3140                          */
3141                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3142                         if (!sparc_is_imm13 (spvar->inst_offset)) {
3143                                 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3144                                 sparc_sti (code, sparc_o7, spvar->inst_basereg, GP_SCRATCH_REG);
3145                         }
3146                         else
3147                                 sparc_sti_imm (code, sparc_o7, spvar->inst_basereg, spvar->inst_offset);
3148                         break;
3149                 }
3150                 case OP_ENDFILTER: {
3151                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3152                         if (!sparc_is_imm13 (spvar->inst_offset)) {
3153                                 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3154                                 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
3155                         }
3156                         else
3157                                 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
3158                         sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
3159                         /* Delay slot */
3160                         sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3161                         break;
3162                 }
3163                 case OP_ENDFINALLY: {
3164                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3165                         if (!sparc_is_imm13 (spvar->inst_offset)) {
3166                                 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3167                                 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
3168                         }
3169                         else
3170                                 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
3171                         sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
3172                         sparc_nop (code);
3173                         break;
3174                 }
3175                 case OP_CALL_HANDLER: 
3176                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3177                         /* This is a jump inside the method, so call_simple works even on V9 */
3178                         sparc_call_simple (code, 0);
3179                         sparc_nop (code);
3180                         break;
3181                 case OP_LABEL:
3182                         ins->inst_c0 = (guint8*)code - cfg->native_code;
3183                         break;
3184                 case OP_RELAXED_NOP:
3185                 case OP_NOP:
3186                 case OP_DUMMY_USE:
3187                 case OP_DUMMY_STORE:
3188                 case OP_NOT_REACHED:
3189                 case OP_NOT_NULL:
3190                         break;
3191                 case OP_BR:
3192                         //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
3193                         if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
3194                                 break;
3195                         if (ins->flags & MONO_INST_BRLABEL) {
3196                                 if (ins->inst_i0->inst_c0) {
3197                                         gint32 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2;
3198                                         g_assert (sparc_is_imm22 (disp));
3199                                         sparc_branch (code, 1, sparc_ba, disp);
3200                                 } else {
3201                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
3202                                         sparc_branch (code, 1, sparc_ba, 0);
3203                                 }
3204                         } else {
3205                                 if (ins->inst_target_bb->native_offset) {
3206                                         gint32 disp = (ins->inst_target_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2;
3207                                         g_assert (sparc_is_imm22 (disp));
3208                                         sparc_branch (code, 1, sparc_ba, disp);
3209                                 } else {
3210                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3211                                         sparc_branch (code, 1, sparc_ba, 0);
3212                                 } 
3213                         }
3214                         sparc_nop (code);
3215                         break;
3216                 case OP_BR_REG:
3217                         sparc_jmp (code, ins->sreg1, sparc_g0);
3218                         sparc_nop (code);
3219                         break;
3220                 case OP_CEQ:
3221                 case OP_CLT:
3222                 case OP_CLT_UN:
3223                 case OP_CGT:
3224                 case OP_CGT_UN:
3225                         if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3226                                 sparc_clr_reg (code, ins->dreg);
3227                                 sparc_movcc_imm (code, sparc_xcc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3228                         }
3229                         else {
3230                                 sparc_clr_reg (code, ins->dreg);
3231 #ifdef SPARCV9
3232                                 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), DEFAULT_ICC, 0, 2);
3233 #else
3234                                 sparc_branch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3235 #endif
3236                                 /* delay slot */
3237                                 sparc_set (code, 1, ins->dreg);
3238                         }
3239                         break;
3240                 case OP_ICEQ:
3241                 case OP_ICLT:
3242                 case OP_ICLT_UN:
3243                 case OP_ICGT:
3244                 case OP_ICGT_UN:
3245                     if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3246                                 sparc_clr_reg (code, ins->dreg);
3247                                 sparc_movcc_imm (code, sparc_icc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3248                     }
3249                     else {
3250                         sparc_clr_reg (code, ins->dreg);
3251                         sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), sparc_icc_short, 0, 2);
3252                         /* delay slot */
3253                         sparc_set (code, 1, ins->dreg);
3254                     }
3255                     break;
3256                 case OP_COND_EXC_EQ:
3257                 case OP_COND_EXC_NE_UN:
3258                 case OP_COND_EXC_LT:
3259                 case OP_COND_EXC_LT_UN:
3260                 case OP_COND_EXC_GT:
3261                 case OP_COND_EXC_GT_UN:
3262                 case OP_COND_EXC_GE:
3263                 case OP_COND_EXC_GE_UN:
3264                 case OP_COND_EXC_LE:
3265                 case OP_COND_EXC_LE_UN:
3266                 case OP_COND_EXC_OV:
3267                 case OP_COND_EXC_NO:
3268                 case OP_COND_EXC_C:
3269                 case OP_COND_EXC_NC:
3270                 case OP_COND_EXC_IEQ:
3271                 case OP_COND_EXC_INE_UN:
3272                 case OP_COND_EXC_ILT:
3273                 case OP_COND_EXC_ILT_UN:
3274                 case OP_COND_EXC_IGT:
3275                 case OP_COND_EXC_IGT_UN:
3276                 case OP_COND_EXC_IGE:
3277                 case OP_COND_EXC_IGE_UN:
3278                 case OP_COND_EXC_ILE:
3279                 case OP_COND_EXC_ILE_UN:
3280                 case OP_COND_EXC_IOV:
3281                 case OP_COND_EXC_INO:
3282                 case OP_COND_EXC_IC:
3283                 case OP_COND_EXC_INC:
3284 #ifdef SPARCV9
3285                         NOT_IMPLEMENTED;
3286 #else
3287                         EMIT_COND_SYSTEM_EXCEPTION (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1);
3288 #endif
3289                         break;
3290                 case OP_SPARC_COND_EXC_EQZ:
3291                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brz, ins->inst_p1);
3292                         break;
3293                 case OP_SPARC_COND_EXC_GEZ:
3294                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgez, ins->inst_p1);
3295                         break;
3296                 case OP_SPARC_COND_EXC_GTZ:
3297                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgz, ins->inst_p1);
3298                         break;
3299                 case OP_SPARC_COND_EXC_LEZ:
3300                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlez, ins->inst_p1);
3301                         break;
3302                 case OP_SPARC_COND_EXC_LTZ:
3303                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlz, ins->inst_p1);
3304                         break;
3305                 case OP_SPARC_COND_EXC_NEZ:
3306                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brnz, ins->inst_p1);
3307                         break;
3308
3309                 case OP_IBEQ:
3310                 case OP_IBNE_UN:
3311                 case OP_IBLT:
3312                 case OP_IBLT_UN:
3313                 case OP_IBGT:
3314                 case OP_IBGT_UN:
3315                 case OP_IBGE:
3316                 case OP_IBGE_UN:
3317                 case OP_IBLE:
3318                 case OP_IBLE_UN: {
3319                         if (sparcv9)
3320                                 EMIT_COND_BRANCH_PREDICTED (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3321                         else
3322                                 EMIT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3323                         break;
3324                 }
3325
3326                 case OP_SPARC_BRZ:
3327                         EMIT_COND_BRANCH_BPR (ins, brz, 1, 1, 1);
3328                         break;
3329                 case OP_SPARC_BRLEZ:
3330                         EMIT_COND_BRANCH_BPR (ins, brlez, 1, 1, 1);
3331                         break;
3332                 case OP_SPARC_BRLZ:
3333                         EMIT_COND_BRANCH_BPR (ins, brlz, 1, 1, 1);
3334                         break;
3335                 case OP_SPARC_BRNZ:
3336                         EMIT_COND_BRANCH_BPR (ins, brnz, 1, 1, 1);
3337                         break;
3338                 case OP_SPARC_BRGZ:
3339                         EMIT_COND_BRANCH_BPR (ins, brgz, 1, 1, 1);
3340                         break;
3341                 case OP_SPARC_BRGEZ:
3342                         EMIT_COND_BRANCH_BPR (ins, brgez, 1, 1, 1);
3343                         break;
3344
3345                 /* floating point opcodes */
3346                 case OP_R8CONST:
3347                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
3348 #ifdef SPARCV9
3349                         sparc_set_template (code, sparc_o7);
3350 #else
3351                         sparc_sethi (code, 0, sparc_o7);
3352 #endif
3353                         sparc_lddf_imm (code, sparc_o7, 0, ins->dreg);
3354                         break;
3355                 case OP_R4CONST:
3356                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
3357 #ifdef SPARCV9
3358                         sparc_set_template (code, sparc_o7);
3359 #else
3360                         sparc_sethi (code, 0, sparc_o7);
3361 #endif
3362                         sparc_ldf_imm (code, sparc_o7, 0, FP_SCRATCH_REG);
3363
3364                         /* Extend to double */
3365                         sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3366                         break;
3367                 case OP_STORER8_MEMBASE_REG:
3368                         if (!sparc_is_imm13 (ins->inst_offset + 4)) {
3369                                 sparc_set (code, ins->inst_offset, sparc_o7);
3370                                 /* SPARCV9 handles misaligned fp loads/stores */
3371                                 if (!v64 && (ins->inst_offset % 8)) {
3372                                         /* Misaligned */
3373                                         sparc_add (code, FALSE, ins->inst_destbasereg, sparc_o7, sparc_o7);
3374                                         sparc_stf (code, ins->sreg1, sparc_o7, sparc_g0);
3375                                         sparc_stf_imm (code, ins->sreg1 + 1, sparc_o7, 4);
3376                                 } else
3377                                         sparc_stdf (code, ins->sreg1, ins->inst_destbasereg, sparc_o7);
3378                         }
3379                         else {
3380                                 if (!v64 && (ins->inst_offset % 8)) {
3381                                         /* Misaligned */
3382                                         sparc_stf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3383                                         sparc_stf_imm (code, ins->sreg1 + 1, ins->inst_destbasereg, ins->inst_offset + 4);
3384                                 } else
3385                                         sparc_stdf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3386                         }
3387                         break;
3388                 case OP_LOADR8_MEMBASE:
3389                         EMIT_LOAD_MEMBASE (ins, lddf);
3390                         break;
3391                 case OP_STORER4_MEMBASE_REG:
3392                         /* This requires a double->single conversion */
3393                         sparc_fdtos (code, ins->sreg1, FP_SCRATCH_REG);
3394                         if (!sparc_is_imm13 (ins->inst_offset)) {
3395                                 sparc_set (code, ins->inst_offset, sparc_o7);
3396                                 sparc_stf (code, FP_SCRATCH_REG, ins->inst_destbasereg, sparc_o7);
3397                         }
3398                         else
3399                                 sparc_stf_imm (code, FP_SCRATCH_REG, ins->inst_destbasereg, ins->inst_offset);
3400                         break;
3401                 case OP_LOADR4_MEMBASE: {
3402                         /* ldf needs a single precision register */
3403                         int dreg = ins->dreg;
3404                         ins->dreg = FP_SCRATCH_REG;
3405                         EMIT_LOAD_MEMBASE (ins, ldf);
3406                         ins->dreg = dreg;
3407                         /* Extend to double */
3408                         sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3409                         break;
3410                 }
3411                 case OP_ICONV_TO_R4: {
3412                         gint32 offset = cfg->arch.float_spill_slot_offset;
3413 #ifdef SPARCV9
3414                         if (!sparc_is_imm13 (offset)) {
3415                                 sparc_set (code, offset, sparc_o7);
3416                                 sparc_stx (code, ins->sreg1, sparc_sp, offset);
3417                                 sparc_lddf (code, sparc_sp, offset, FP_SCRATCH_REG);
3418                         } else {
3419                                 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3420                                 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3421                         }
3422                         sparc_fxtos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3423 #else
3424                         if (!sparc_is_imm13 (offset)) {
3425                                 sparc_set (code, offset, sparc_o7);
3426                                 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3427                                 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3428                         } else {
3429                                 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3430                                 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3431                         }
3432                         sparc_fitos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3433 #endif
3434                         sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3435                         break;
3436                 }
3437                 case OP_ICONV_TO_R8: {
3438                         gint32 offset = cfg->arch.float_spill_slot_offset;
3439 #ifdef SPARCV9
3440                         if (!sparc_is_imm13 (offset)) {
3441                                 sparc_set (code, offset, sparc_o7);
3442                                 sparc_stx (code, ins->sreg1, sparc_sp, sparc_o7);
3443                                 sparc_lddf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3444                         } else {
3445                                 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3446                                 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3447                         }
3448                         sparc_fxtod (code, FP_SCRATCH_REG, ins->dreg);
3449 #else
3450                         if (!sparc_is_imm13 (offset)) {
3451                                 sparc_set (code, offset, sparc_o7);
3452                                 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3453                                 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3454                         } else {
3455                                 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3456                                 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3457                         }
3458                         sparc_fitod (code, FP_SCRATCH_REG, ins->dreg);
3459 #endif
3460                         break;
3461                 }
3462                 case OP_FCONV_TO_I1:
3463                 case OP_FCONV_TO_U1:
3464                 case OP_FCONV_TO_I2:
3465                 case OP_FCONV_TO_U2:
3466 #ifndef SPARCV9
3467                 case OP_FCONV_TO_I:
3468                 case OP_FCONV_TO_U:
3469 #endif
3470                 case OP_FCONV_TO_I4:
3471                 case OP_FCONV_TO_U4: {
3472                         gint32 offset = cfg->arch.float_spill_slot_offset;
3473                         sparc_fdtoi (code, ins->sreg1, FP_SCRATCH_REG);
3474                         if (!sparc_is_imm13 (offset)) {
3475                                 sparc_set (code, offset, sparc_o7);
3476                                 sparc_stdf (code, FP_SCRATCH_REG, sparc_sp, sparc_o7);
3477                                 sparc_ld (code, sparc_sp, sparc_o7, ins->dreg);
3478                         } else {
3479                                 sparc_stdf_imm (code, FP_SCRATCH_REG, sparc_sp, offset);
3480                                 sparc_ld_imm (code, sparc_sp, offset, ins->dreg);
3481                         }
3482
3483                         switch (ins->opcode) {
3484                         case OP_FCONV_TO_I1:
3485                         case OP_FCONV_TO_U1:
3486                                 sparc_and_imm (code, 0, ins->dreg, 0xff, ins->dreg);
3487                                 break;
3488                         case OP_FCONV_TO_I2:
3489                         case OP_FCONV_TO_U2:
3490                                 sparc_set (code, 0xffff, sparc_o7);
3491                                 sparc_and (code, 0, ins->dreg, sparc_o7, ins->dreg);
3492                                 break;
3493                         default:
3494                                 break;
3495                         }
3496                         break;
3497                 }
3498                 case OP_FCONV_TO_I8:
3499                 case OP_FCONV_TO_U8:
3500                         /* Emulated */
3501                         g_assert_not_reached ();
3502                         break;
3503                 case OP_FCONV_TO_R4:
3504                         /* FIXME: Change precision ? */
3505 #ifdef SPARCV9
3506                         sparc_fmovd (code, ins->sreg1, ins->dreg);
3507 #else
3508                         sparc_fmovs (code, ins->sreg1, ins->dreg);
3509                         sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3510 #endif
3511                         break;
3512                 case OP_LCONV_TO_R_UN: { 
3513                         /* Emulated */
3514                         g_assert_not_reached ();
3515                         break;
3516                 }
3517                 case OP_LCONV_TO_OVF_I:
3518                 case OP_LCONV_TO_OVF_I4_2: {
3519                         guint32 *br [3], *label [1];
3520
3521                         /* 
3522                          * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3523                          */
3524                         sparc_cmp_imm (code, ins->sreg1, 0);
3525                         br [0] = code; 
3526                         sparc_branch (code, 1, sparc_bneg, 0);
3527                         sparc_nop (code);
3528
3529                         /* positive */
3530                         /* ms word must be 0 */
3531                         sparc_cmp_imm (code, ins->sreg2, 0);
3532                         br [1] = code;
3533                         sparc_branch (code, 1, sparc_be, 0);
3534                         sparc_nop (code);
3535
3536                         label [0] = code;
3537
3538                         EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_ba, "OverflowException");
3539
3540                         /* negative */
3541                         sparc_patch (br [0], code);
3542
3543                         /* ms word must 0xfffffff */
3544                         sparc_cmp_imm (code, ins->sreg2, -1);
3545                         br [2] = code;
3546                         sparc_branch (code, 1, sparc_bne, 0);
3547                         sparc_nop (code);
3548                         sparc_patch (br [2], label [0]);
3549
3550                         /* Ok */
3551                         sparc_patch (br [1], code);
3552                         if (ins->sreg1 != ins->dreg)
3553                                 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
3554                         break;
3555                 }
3556                 case OP_FADD:
3557                         sparc_faddd (code, ins->sreg1, ins->sreg2, ins->dreg);
3558                         break;
3559                 case OP_FSUB:
3560                         sparc_fsubd (code, ins->sreg1, ins->sreg2, ins->dreg);
3561                         break;          
3562                 case OP_FMUL:
3563                         sparc_fmuld (code, ins->sreg1, ins->sreg2, ins->dreg);
3564                         break;          
3565                 case OP_FDIV:
3566                         sparc_fdivd (code, ins->sreg1, ins->sreg2, ins->dreg);
3567                         break;          
3568                 case OP_FNEG:
3569 #ifdef SPARCV9
3570                         sparc_fnegd (code, ins->sreg1, ins->dreg);
3571 #else
3572                         /* FIXME: why don't use fnegd ? */
3573                         sparc_fnegs (code, ins->sreg1, ins->dreg);
3574 #endif
3575                         break;          
3576                 case OP_FREM:
3577                         sparc_fdivd (code, ins->sreg1, ins->sreg2, FP_SCRATCH_REG);
3578                         sparc_fmuld (code, ins->sreg2, FP_SCRATCH_REG, FP_SCRATCH_REG);
3579                         sparc_fsubd (code, ins->sreg1, FP_SCRATCH_REG, ins->dreg);
3580                         break;
3581                 case OP_FCOMPARE:
3582                         sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3583                         break;
3584                 case OP_FCEQ:
3585                 case OP_FCLT:
3586                 case OP_FCLT_UN:
3587                 case OP_FCGT:
3588                 case OP_FCGT_UN:
3589                         sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3590                         sparc_clr_reg (code, ins->dreg);
3591                         switch (ins->opcode) {
3592                         case OP_FCLT_UN:
3593                         case OP_FCGT_UN:
3594                                 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 4);
3595                                 /* delay slot */
3596                                 sparc_set (code, 1, ins->dreg);
3597                                 sparc_fbranch (code, 1, sparc_fbu, 2);
3598                                 /* delay slot */
3599                                 sparc_set (code, 1, ins->dreg);
3600                                 break;
3601                         default:
3602                                 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3603                                 /* delay slot */
3604                                 sparc_set (code, 1, ins->dreg);                         
3605                         }
3606                         break;
3607                 case OP_FBEQ:
3608                 case OP_FBLT:
3609                 case OP_FBGT:
3610                         EMIT_FLOAT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3611                         break;
3612                 case OP_FBGE: {
3613                         /* clt.un + brfalse */
3614                         guint32 *p = code;
3615                         sparc_fbranch (code, 1, sparc_fbul, 0);
3616                         /* delay slot */
3617                         sparc_nop (code);
3618                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3619                         sparc_patch (p, (guint8*)code);
3620                         break;
3621                 }
3622                 case OP_FBLE: {
3623                         /* cgt.un + brfalse */
3624                         guint32 *p = code;
3625                         sparc_fbranch (code, 1, sparc_fbug, 0);
3626                         /* delay slot */
3627                         sparc_nop (code);
3628                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3629                         sparc_patch (p, (guint8*)code);
3630                         break;
3631                 }
3632                 case OP_FBNE_UN:
3633                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbne, 1, 1);
3634                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3635                         break;
3636                 case OP_FBLT_UN:
3637                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbl, 1, 1);
3638                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3639                         break;
3640                 case OP_FBGT_UN:
3641                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbg, 1, 1);
3642                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3643                         break;
3644                 case OP_FBGE_UN:
3645                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbge, 1, 1);
3646                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3647                         break;
3648                 case OP_FBLE_UN:
3649                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fble, 1, 1);
3650                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3651                         break;
3652                 case OP_CKFINITE: {
3653                         gint32 offset = cfg->arch.float_spill_slot_offset;
3654                         if (!sparc_is_imm13 (offset)) {
3655                                 sparc_set (code, offset, sparc_o7);
3656                                 sparc_stdf (code, ins->sreg1, sparc_sp, sparc_o7);
3657                                 sparc_lduh (code, sparc_sp, sparc_o7, sparc_o7);
3658                         } else {
3659                                 sparc_stdf_imm (code, ins->sreg1, sparc_sp, offset);
3660                                 sparc_lduh_imm (code, sparc_sp, offset, sparc_o7);
3661                         }
3662                         sparc_srl_imm (code, sparc_o7, 4, sparc_o7);
3663                         sparc_and_imm (code, FALSE, sparc_o7, 2047, sparc_o7);
3664                         sparc_cmp_imm (code, sparc_o7, 2047);
3665                         EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_be, "ArithmeticException");
3666 #ifdef SPARCV9
3667                         sparc_fmovd (code, ins->sreg1, ins->dreg);
3668 #else
3669                         sparc_fmovs (code, ins->sreg1, ins->dreg);
3670                         sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3671 #endif
3672                         break;
3673                 }
3674
3675                 case OP_MEMORY_BARRIER:
3676                         sparc_membar (code, sparc_membar_all);
3677                         break;
3678
3679                 default:
3680 #ifdef __GNUC__
3681                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3682 #else
3683                         g_warning ("%s:%d: unknown opcode %s\n", __FILE__, __LINE__, mono_inst_name (ins->opcode));
3684 #endif
3685                         g_assert_not_reached ();
3686                 }
3687
3688                 if ((((guint8*)code) - code_start) > max_len) {
3689                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3690                                    mono_inst_name (ins->opcode), max_len, ((guint8*)code) - code_start);
3691                         g_assert_not_reached ();
3692                 }
3693                
3694                 cpos += max_len;
3695
3696                 last_ins = ins;
3697         }
3698
3699         cfg->code_len = (guint8*)code - cfg->native_code;
3700 }
3701
3702 void
3703 mono_arch_register_lowlevel_calls (void)
3704 {
3705         mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
3706 }
3707
3708 void
3709 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3710 {
3711         MonoJumpInfo *patch_info;
3712
3713         /* FIXME: Move part of this to arch independent code */
3714         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3715                 unsigned char *ip = patch_info->ip.i + code;
3716                 gpointer target;
3717
3718                 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3719
3720                 switch (patch_info->type) {
3721                 case MONO_PATCH_INFO_NONE:
3722                         continue;
3723                 case MONO_PATCH_INFO_CLASS_INIT: {
3724                         guint32 *ip2 = (guint32*)ip;
3725                         /* Might already been changed to a nop */
3726 #ifdef SPARCV9
3727                         sparc_set_template (ip2, sparc_o7);
3728                         sparc_jmpl (ip2, sparc_o7, sparc_g0, sparc_o7);
3729 #else
3730                         sparc_call_simple (ip2, 0);
3731 #endif
3732                         break;
3733                 }
3734                 case MONO_PATCH_INFO_METHOD_JUMP: {
3735                         guint32 *ip2 = (guint32*)ip;
3736                         /* Might already been patched */
3737                         sparc_set_template (ip2, sparc_o7);
3738                         break;
3739                 }
3740                 default:
3741                         break;
3742                 }
3743                 sparc_patch ((guint32*)ip, target);
3744         }
3745 }
3746
3747 void*
3748 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3749 {
3750         int i;
3751         guint32 *code = (guint32*)p;
3752         MonoMethodSignature *sig = mono_method_signature (cfg->method);
3753         CallInfo *cinfo;
3754
3755         /* Save registers to stack */
3756         for (i = 0; i < 6; ++i)
3757                 sparc_sti_imm (code, sparc_i0 + i, sparc_fp, ARGS_OFFSET + (i * sizeof (gpointer)));
3758
3759         cinfo = get_call_info (cfg, sig, FALSE);
3760
3761         /* Save float regs on V9, since they are caller saved */
3762         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3763                 ArgInfo *ainfo = cinfo->args + i;
3764                 gint32 stack_offset;
3765
3766                 stack_offset = ainfo->offset + ARGS_OFFSET;
3767
3768                 if (ainfo->storage == ArgInFloatReg) {
3769                         if (!sparc_is_imm13 (stack_offset))
3770                                 NOT_IMPLEMENTED;
3771                         sparc_stf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3772                 }
3773                 else if (ainfo->storage == ArgInDoubleReg) {
3774                         /* The offset is guaranteed to be aligned by the ABI rules */
3775                         sparc_stdf_imm (code, ainfo->reg, sparc_fp, stack_offset);
3776                 }
3777         }
3778
3779         sparc_set (code, cfg->method, sparc_o0);
3780         sparc_add_imm (code, FALSE, sparc_fp, MONO_SPARC_STACK_BIAS, sparc_o1);
3781
3782         mono_add_patch_info (cfg, (guint8*)code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
3783         EMIT_CALL ();
3784
3785         /* Restore float regs on V9 */
3786         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3787                 ArgInfo *ainfo = cinfo->args + i;
3788                 gint32 stack_offset;
3789
3790                 stack_offset = ainfo->offset + ARGS_OFFSET;
3791
3792                 if (ainfo->storage == ArgInFloatReg) {
3793                         if (!sparc_is_imm13 (stack_offset))
3794                                 NOT_IMPLEMENTED;
3795                         sparc_ldf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3796                 }
3797                 else if (ainfo->storage == ArgInDoubleReg) {
3798                         /* The offset is guaranteed to be aligned by the ABI rules */
3799                         sparc_lddf_imm (code, sparc_fp, stack_offset, ainfo->reg);
3800                 }
3801         }
3802
3803         g_free (cinfo);
3804
3805         return code;
3806 }
3807
3808 enum {
3809         SAVE_NONE,
3810         SAVE_STRUCT,
3811         SAVE_ONE,
3812         SAVE_TWO,
3813         SAVE_FP
3814 };
3815
3816 void*
3817 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
3818 {
3819         guint32 *code = (guint32*)p;
3820         int save_mode = SAVE_NONE;
3821         MonoMethod *method = cfg->method;
3822
3823         switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
3824         case MONO_TYPE_VOID:
3825                 /* special case string .ctor icall */
3826                 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
3827                         save_mode = SAVE_ONE;
3828                 else
3829                         save_mode = SAVE_NONE;
3830                 break;
3831         case MONO_TYPE_I8:
3832         case MONO_TYPE_U8:
3833 #ifdef SPARCV9
3834                 save_mode = SAVE_ONE;
3835 #else
3836                 save_mode = SAVE_TWO;
3837 #endif
3838                 break;
3839         case MONO_TYPE_R4:
3840         case MONO_TYPE_R8:
3841                 save_mode = SAVE_FP;
3842                 break;
3843         case MONO_TYPE_VALUETYPE:
3844                 save_mode = SAVE_STRUCT;
3845                 break;
3846         default:
3847                 save_mode = SAVE_ONE;
3848                 break;
3849         }
3850
3851         /* Save the result to the stack and also put it into the output registers */
3852
3853         switch (save_mode) {
3854         case SAVE_TWO:
3855                 /* V8 only */
3856                 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
3857                 sparc_st_imm (code, sparc_i0, sparc_fp, 72);
3858                 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3859                 sparc_mov_reg_reg (code, sparc_i1, sparc_o2);
3860                 break;
3861         case SAVE_ONE:
3862                 sparc_sti_imm (code, sparc_i0, sparc_fp, ARGS_OFFSET);
3863                 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3864                 break;
3865         case SAVE_FP:
3866 #ifdef SPARCV9
3867                 sparc_stdf_imm (code, sparc_f0, sparc_fp, ARGS_OFFSET);
3868 #else
3869                 sparc_stdf_imm (code, sparc_f0, sparc_fp, 72);
3870                 sparc_ld_imm (code, sparc_fp, 72, sparc_o1);
3871                 sparc_ld_imm (code, sparc_fp, 72 + 4, sparc_o2);
3872 #endif
3873                 break;
3874         case SAVE_STRUCT:
3875 #ifdef SPARCV9
3876                 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
3877 #else
3878                 sparc_ld_imm (code, sparc_fp, 64, sparc_o1);
3879 #endif
3880                 break;
3881         case SAVE_NONE:
3882         default:
3883                 break;
3884         }
3885
3886         sparc_set (code, cfg->method, sparc_o0);
3887
3888         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, func);
3889         EMIT_CALL ();
3890
3891         /* Restore result */
3892
3893         switch (save_mode) {
3894         case SAVE_TWO:
3895                 sparc_ld_imm (code, sparc_fp, 68, sparc_i0);
3896                 sparc_ld_imm (code, sparc_fp, 72, sparc_i0);
3897                 break;
3898         case SAVE_ONE:
3899                 sparc_ldi_imm (code, sparc_fp, ARGS_OFFSET, sparc_i0);
3900                 break;
3901         case SAVE_FP:
3902                 sparc_lddf_imm (code, sparc_fp, ARGS_OFFSET, sparc_f0);
3903                 break;
3904         case SAVE_NONE:
3905         default:
3906                 break;
3907         }
3908
3909         return code;
3910 }
3911
3912 guint8 *
3913 mono_arch_emit_prolog (MonoCompile *cfg)
3914 {
3915         MonoMethod *method = cfg->method;
3916         MonoMethodSignature *sig;
3917         MonoInst *inst;
3918         guint32 *code;
3919         CallInfo *cinfo;
3920         guint32 i, offset;
3921
3922         cfg->code_size = 256;
3923         cfg->native_code = g_malloc (cfg->code_size);
3924         code = (guint32*)cfg->native_code;
3925
3926         /* FIXME: Generate intermediate code instead */
3927
3928         offset = cfg->stack_offset;
3929         offset += (16 * sizeof (gpointer)); /* register save area */
3930 #ifndef SPARCV9
3931         offset += 4; /* struct/union return pointer */
3932 #endif
3933
3934         /* add parameter area size for called functions */
3935         if (cfg->param_area < (6 * sizeof (gpointer)))
3936                 /* Reserve space for the first 6 arguments even if it is unused */
3937                 offset += 6 * sizeof (gpointer);
3938         else
3939                 offset += cfg->param_area;
3940         
3941         /* align the stack size */
3942         offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
3943
3944         /*
3945          * localloc'd memory is stored between the local variables (whose
3946          * size is given by cfg->stack_offset), and between the space reserved
3947          * by the ABI.
3948          */
3949         cfg->arch.localloc_offset = offset - cfg->stack_offset;
3950
3951         cfg->stack_offset = offset;
3952
3953 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3954                         /* Perform stack touching */
3955                         NOT_IMPLEMENTED;
3956 #endif
3957
3958         if (!sparc_is_imm13 (- cfg->stack_offset)) {
3959                 /* Can't use sparc_o7 here, since we're still in the caller's frame */
3960                 sparc_set (code, (- cfg->stack_offset), GP_SCRATCH_REG);
3961                 sparc_save (code, sparc_sp, GP_SCRATCH_REG, sparc_sp);
3962         }
3963         else
3964                 sparc_save_imm (code, sparc_sp, - cfg->stack_offset, sparc_sp);
3965
3966 /*
3967         if (strstr (cfg->method->name, "foo")) {
3968                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
3969                 sparc_call_simple (code, 0);
3970                 sparc_nop (code);
3971         }
3972 */
3973
3974         sig = mono_method_signature (method);
3975
3976         cinfo = get_call_info (cfg, sig, FALSE);
3977
3978         /* Keep in sync with emit_load_volatile_arguments */
3979         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3980                 ArgInfo *ainfo = cinfo->args + i;
3981                 gint32 stack_offset;
3982                 MonoType *arg_type;
3983                 inst = cfg->args [i];
3984
3985                 if (sig->hasthis && (i == 0))
3986                         arg_type = &mono_defaults.object_class->byval_arg;
3987                 else
3988                         arg_type = sig->params [i - sig->hasthis];
3989
3990                 stack_offset = ainfo->offset + ARGS_OFFSET;
3991
3992                 /* Save the split arguments so they will reside entirely on the stack */
3993                 if (ainfo->storage == ArgInSplitRegStack) {
3994                         /* Save the register to the stack */
3995                         g_assert (inst->opcode == OP_REGOFFSET);
3996                         if (!sparc_is_imm13 (stack_offset))
3997                                 NOT_IMPLEMENTED;
3998                         sparc_st_imm (code, sparc_i5, inst->inst_basereg, stack_offset);
3999                 }
4000
4001                 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
4002                         /* Save the argument to a dword aligned stack location */
4003                         /*
4004                          * stack_offset contains the offset of the argument on the stack.
4005                          * inst->inst_offset contains the dword aligned offset where the value 
4006                          * should be stored.
4007                          */
4008                         if (ainfo->storage == ArgInIRegPair) {
4009                                 if (!sparc_is_imm13 (inst->inst_offset + 4))
4010                                         NOT_IMPLEMENTED;
4011                                 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
4012                                 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
4013                         }
4014                         else
4015                                 if (ainfo->storage == ArgInSplitRegStack) {
4016 #ifdef SPARCV9
4017                                         g_assert_not_reached ();
4018 #endif
4019                                         if (stack_offset != inst->inst_offset) {
4020                                                 /* stack_offset is not dword aligned, so we need to make a copy */
4021                                                 sparc_st_imm (code, sparc_i5, inst->inst_basereg, inst->inst_offset);
4022                                                 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
4023                                                 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
4024                                         }
4025                                 }
4026                         else
4027                                 if (ainfo->storage == ArgOnStackPair) {
4028 #ifdef SPARCV9
4029                                         g_assert_not_reached ();
4030 #endif
4031                                         if (stack_offset != inst->inst_offset) {
4032                                                 /* stack_offset is not dword aligned, so we need to make a copy */
4033                                                 sparc_ld_imm (code, sparc_fp, stack_offset, sparc_o7);
4034                                                 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset);
4035                                                 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
4036                                                 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
4037                                         }
4038                                 }
4039                         else
4040                                 g_assert_not_reached ();
4041                 }
4042                 else
4043                         if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
4044                                 /* Argument in register, but need to be saved to stack */
4045                                 if (!sparc_is_imm13 (stack_offset))
4046                                         NOT_IMPLEMENTED;
4047                                 if ((stack_offset - ARGS_OFFSET) & 0x1)
4048                                         sparc_stb_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4049                                 else
4050                                         if ((stack_offset - ARGS_OFFSET) & 0x2)
4051                                                 sparc_sth_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4052                                 else
4053                                         if ((stack_offset - ARGS_OFFSET) & 0x4)
4054                                                 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);                           
4055                                         else {
4056                                                 if (v64)
4057                                                         sparc_stx_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4058                                                 else
4059                                                         sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4060                                         }
4061                         }
4062                 else
4063                         if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
4064 #ifdef SPARCV9
4065                                 NOT_IMPLEMENTED;
4066 #endif
4067                                 /* Argument in regpair, but need to be saved to stack */
4068                                 if (!sparc_is_imm13 (inst->inst_offset + 4))
4069                                         NOT_IMPLEMENTED;
4070                                 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
4071                                 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);                              
4072                         }
4073                 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
4074                                 if (!sparc_is_imm13 (stack_offset))
4075                                         NOT_IMPLEMENTED;
4076                                 sparc_stf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4077                                 }
4078                         else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
4079                                 /* The offset is guaranteed to be aligned by the ABI rules */
4080                                 sparc_stdf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4081                         }
4082                                         
4083                 if ((ainfo->storage == ArgInFloatReg) && (inst->opcode == OP_REGVAR)) {
4084                         /* Need to move into the a double precision register */
4085                         sparc_fstod (code, ainfo->reg, ainfo->reg - 1);
4086                 }
4087
4088                 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
4089                         if (inst->opcode == OP_REGVAR)
4090                                 /* FIXME: Load the argument into memory */
4091                                 NOT_IMPLEMENTED;
4092         }
4093
4094         g_free (cinfo);
4095
4096         if (cfg->method->save_lmf) {
4097                 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
4098
4099                 /* Save ip */
4100                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
4101                 sparc_set_template (code, sparc_o7);
4102                 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
4103                 /* Save sp */
4104                 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
4105                 /* Save fp */
4106                 sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
4107                 /* Save method */
4108                 /* FIXME: add a relocation for this */
4109                 sparc_set (code, cfg->method, sparc_o7);
4110                 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
4111
4112                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
4113                                                          (gpointer)"mono_arch_get_lmf_addr");           
4114                 EMIT_CALL ();
4115
4116                 code = (guint32*)mono_sparc_emit_save_lmf (code, lmf_offset);
4117         }
4118
4119         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4120                 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
4121
4122         cfg->code_len = (guint8*)code - cfg->native_code;
4123
4124         g_assert (cfg->code_len <= cfg->code_size);
4125
4126         return (guint8*)code;
4127 }
4128
4129 void
4130 mono_arch_emit_epilog (MonoCompile *cfg)
4131 {
4132         MonoMethod *method = cfg->method;
4133         guint32 *code;
4134         int can_fold = 0;
4135         int max_epilog_size = 16 + 20 * 4;
4136         
4137         if (cfg->method->save_lmf)
4138                 max_epilog_size += 128;
4139         
4140         if (mono_jit_trace_calls != NULL)
4141                 max_epilog_size += 50;
4142
4143         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4144                 max_epilog_size += 50;
4145
4146         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4147                 cfg->code_size *= 2;
4148                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4149                 mono_jit_stats.code_reallocs++;
4150         }
4151
4152         code = (guint32*)(cfg->native_code + cfg->code_len);
4153
4154         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4155                 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
4156
4157         if (cfg->method->save_lmf) {
4158                 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
4159
4160                 code = mono_sparc_emit_restore_lmf (code, lmf_offset);
4161         }
4162
4163         /* 
4164          * The V8 ABI requires that calls to functions which return a structure
4165          * return to %i7+12
4166          */
4167         if (!v64 && mono_method_signature (cfg->method)->pinvoke && MONO_TYPE_ISSTRUCT(mono_method_signature (cfg->method)->ret))
4168                 sparc_jmpl_imm (code, sparc_i7, 12, sparc_g0);
4169         else
4170                 sparc_ret (code);
4171
4172         /* Only fold last instruction into the restore if the exit block has an in count of 1
4173            and the previous block hasn't been optimized away since it may have an in count > 1 */
4174         if (cfg->bb_exit->in_count == 1 && cfg->bb_exit->in_bb[0]->native_offset != cfg->bb_exit->native_offset)
4175                 can_fold = 1;
4176
4177         /* 
4178          * FIXME: The last instruction might have a branch pointing into it like in 
4179          * int_ceq sparc_i0 <-
4180          */
4181         can_fold = 0;
4182
4183         /* Try folding last instruction into the restore */
4184         if (can_fold && (sparc_inst_op (code [-2]) == 0x2) && (sparc_inst_op3 (code [-2]) == 0x2) && sparc_inst_imm (code [-2]) && (sparc_inst_rd (code [-2]) == sparc_i0)) {
4185                 /* or reg, imm, %i0 */
4186                 int reg = sparc_inst_rs1 (code [-2]);
4187                 int imm = (((gint32)(sparc_inst_imm13 (code [-2]))) << 19) >> 19;
4188                 code [-2] = code [-1];
4189                 code --;
4190                 sparc_restore_imm (code, reg, imm, sparc_o0);
4191         }
4192         else
4193         if (can_fold && (sparc_inst_op (code [-2]) == 0x2) && (sparc_inst_op3 (code [-2]) == 0x2) && (!sparc_inst_imm (code [-2])) && (sparc_inst_rd (code [-2]) == sparc_i0)) {
4194                 /* or reg, reg, %i0 */
4195                 int reg1 = sparc_inst_rs1 (code [-2]);
4196                 int reg2 = sparc_inst_rs2 (code [-2]);
4197                 code [-2] = code [-1];
4198                 code --;
4199                 sparc_restore (code, reg1, reg2, sparc_o0);
4200         }
4201         else
4202                 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
4203
4204         cfg->code_len = (guint8*)code - cfg->native_code;
4205
4206         g_assert (cfg->code_len < cfg->code_size);
4207
4208 }
4209
4210 void
4211 mono_arch_emit_exceptions (MonoCompile *cfg)
4212 {
4213         MonoJumpInfo *patch_info;
4214         guint32 *code;
4215         int nthrows = 0, i;
4216         int exc_count = 0;
4217         guint32 code_size;
4218         MonoClass *exc_classes [16];
4219         guint8 *exc_throw_start [16], *exc_throw_end [16];
4220
4221         /* Compute needed space */
4222         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4223                 if (patch_info->type == MONO_PATCH_INFO_EXC)
4224                         exc_count++;
4225         }
4226      
4227         /* 
4228          * make sure we have enough space for exceptions
4229          */
4230 #ifdef SPARCV9
4231         code_size = exc_count * (20 * 4);
4232 #else
4233         code_size = exc_count * 24;
4234 #endif
4235
4236         while (cfg->code_len + code_size > (cfg->code_size - 16)) {
4237                 cfg->code_size *= 2;
4238                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4239                 mono_jit_stats.code_reallocs++;
4240         }
4241
4242         code = (guint32*)(cfg->native_code + cfg->code_len);
4243
4244         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4245                 switch (patch_info->type) {
4246                 case MONO_PATCH_INFO_EXC: {
4247                         MonoClass *exc_class;
4248                         guint32 *buf, *buf2;
4249                         guint32 throw_ip, type_idx;
4250                         gint32 disp;
4251
4252                         sparc_patch ((guint32*)(cfg->native_code + patch_info->ip.i), code);
4253
4254                         exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4255                         g_assert (exc_class);
4256                         type_idx = exc_class->type_token - MONO_TOKEN_TYPE_DEF;
4257                         throw_ip = patch_info->ip.i;
4258
4259                         /* Find a throw sequence for the same exception class */
4260                         for (i = 0; i < nthrows; ++i)
4261                                 if (exc_classes [i] == exc_class)
4262                                         break;
4263
4264                         if (i < nthrows) {
4265                                 guint32 throw_offset = (((guint8*)exc_throw_end [i] - cfg->native_code) - throw_ip) >> 2;
4266                                 if (!sparc_is_imm13 (throw_offset))
4267                                         sparc_set32 (code, throw_offset, sparc_o1);
4268
4269                                 disp = (exc_throw_start [i] - (guint8*)code) >> 2;
4270                                 g_assert (sparc_is_imm22 (disp));
4271                                 sparc_branch (code, 0, sparc_ba, disp);
4272                                 if (sparc_is_imm13 (throw_offset))
4273                                         sparc_set32 (code, throw_offset, sparc_o1);
4274                                 else
4275                                         sparc_nop (code);
4276                                 patch_info->type = MONO_PATCH_INFO_NONE;
4277                         }
4278                         else {
4279                                 /* Emit the template for setting o1 */
4280                                 buf = code;
4281                                 if (sparc_is_imm13 (((((guint8*)code - cfg->native_code) - throw_ip) >> 2) - 8))
4282                                         /* Can use a short form */
4283                                         sparc_nop (code);
4284                                 else
4285                                         sparc_set_template (code, sparc_o1);
4286                                 buf2 = code;
4287
4288                                 if (nthrows < 16) {
4289                                         exc_classes [nthrows] = exc_class;
4290                                         exc_throw_start [nthrows] = (guint8*)code;
4291                                 }
4292
4293                                 /*
4294                                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
4295                                 EMIT_CALL();
4296                                 */
4297
4298                                 /* first arg = type token */
4299                                 /* Pass the type index to reduce the size of the sparc_set */
4300                                 if (!sparc_is_imm13 (type_idx))
4301                                         sparc_set32 (code, type_idx, sparc_o0);
4302
4303                                 /* second arg = offset between the throw ip and the current ip */
4304                                 /* On sparc, the saved ip points to the call instruction */
4305                                 disp = (((guint8*)code - cfg->native_code) - throw_ip) >> 2;
4306                                 sparc_set32 (buf, disp, sparc_o1);
4307                                 while (buf < buf2)
4308                                         sparc_nop (buf);
4309
4310                                 if (nthrows < 16) {
4311                                         exc_throw_end [nthrows] = (guint8*)code;
4312                                         nthrows ++;
4313                                 }
4314
4315                                 patch_info->data.name = "mono_arch_throw_corlib_exception";
4316                                 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4317                                 patch_info->ip.i = (guint8*)code - cfg->native_code;
4318
4319                                 EMIT_CALL ();
4320
4321                                 if (sparc_is_imm13 (type_idx)) {
4322                                         /* Put it into the delay slot */
4323                                         code --;
4324                                         buf = code;
4325                                         sparc_set32 (code, type_idx, sparc_o0);
4326                                         g_assert (code - buf == 1);
4327                                 }
4328                         }
4329                         break;
4330                 }
4331                 default:
4332                         /* do nothing */
4333                         break;
4334                 }
4335         }
4336
4337         cfg->code_len = (guint8*)code - cfg->native_code;
4338
4339         g_assert (cfg->code_len < cfg->code_size);
4340
4341 }
4342
4343 gboolean lmf_addr_key_inited = FALSE;
4344
4345 #ifdef MONO_SPARC_THR_TLS
4346 thread_key_t lmf_addr_key;
4347 #else
4348 pthread_key_t lmf_addr_key;
4349 #endif
4350
4351 gpointer
4352 mono_arch_get_lmf_addr (void)
4353 {
4354         /* This is perf critical so we bypass the IO layer */
4355         /* The thr_... functions seem to be somewhat faster */
4356 #ifdef MONO_SPARC_THR_TLS
4357         gpointer res;
4358         thr_getspecific (lmf_addr_key, &res);
4359         return res;
4360 #else
4361         return pthread_getspecific (lmf_addr_key);
4362 #endif
4363 }
4364
4365 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4366
4367 /*
4368  * There seems to be no way to determine stack boundaries under solaris,
4369  * so it's not possible to determine whenever a SIGSEGV is caused by stack
4370  * overflow or not.
4371  */
4372 #error "--with-sigaltstack=yes not supported on solaris"
4373
4374 #endif
4375
4376 void
4377 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4378 {
4379         if (!lmf_addr_key_inited) {
4380                 int res;
4381
4382                 lmf_addr_key_inited = TRUE;
4383
4384 #ifdef MONO_SPARC_THR_TLS
4385                 res = thr_keycreate (&lmf_addr_key, NULL);
4386 #else
4387                 res = pthread_key_create (&lmf_addr_key, NULL);
4388 #endif
4389                 g_assert (res == 0);
4390
4391         }
4392
4393 #ifdef MONO_SPARC_THR_TLS
4394         thr_setspecific (lmf_addr_key, &tls->lmf);
4395 #else
4396         pthread_setspecific (lmf_addr_key, &tls->lmf);
4397 #endif
4398 }
4399
4400 void
4401 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4402 {
4403 }
4404
4405 MonoInst*
4406 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4407 {
4408         MonoInst *ins = NULL;
4409
4410         return ins;
4411 }
4412
4413 /*
4414  * mono_arch_get_argument_info:
4415  * @csig:  a method signature
4416  * @param_count: the number of parameters to consider
4417  * @arg_info: an array to store the result infos
4418  *
4419  * Gathers information on parameters such as size, alignment and
4420  * padding. arg_info should be large enought to hold param_count + 1 entries. 
4421  *
4422  * Returns the size of the activation frame.
4423  */
4424 int
4425 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
4426 {
4427         int k, align;
4428         CallInfo *cinfo;
4429         ArgInfo *ainfo;
4430
4431         cinfo = get_call_info (NULL, csig, FALSE);
4432
4433         if (csig->hasthis) {
4434                 ainfo = &cinfo->args [0];
4435                 arg_info [0].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4436         }
4437
4438         for (k = 0; k < param_count; k++) {
4439                 ainfo = &cinfo->args [k + csig->hasthis];
4440
4441                 arg_info [k + 1].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4442                 arg_info [k + 1].size = mono_type_size (csig->params [k], &align);
4443         }
4444
4445         g_free (cinfo);
4446
4447         return 0;
4448 }
4449
4450 gboolean
4451 mono_arch_print_tree (MonoInst *tree, int arity)
4452 {
4453         return 0;
4454 }
4455
4456 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4457 {
4458         return NULL;
4459 }
4460
4461 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
4462 {
4463         return NULL;
4464 }
4465
4466 gpointer
4467 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
4468 {
4469         /* FIXME: implement */
4470         g_assert_not_reached ();
4471 }