Merge pull request #4570 from lateralusX/jlorenss/visual_studio_msbuild_fix
[mono.git] / mono / mini / mini-arm.c
1 /**
2  * \file
3  * ARM backend for the Mono code generator
4  *
5  * Authors:
6  *   Paolo Molaro (lupus@ximian.com)
7  *   Dietmar Maurer (dietmar@ximian.com)
8  *
9  * (C) 2003 Ximian, Inc.
10  * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
11  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13  */
14 #include "mini.h"
15 #include <string.h>
16
17 #include <mono/metadata/abi-details.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/profiler-private.h>
20 #include <mono/metadata/debug-helpers.h>
21 #include <mono/utils/mono-mmap.h>
22 #include <mono/utils/mono-hwcap.h>
23 #include <mono/utils/mono-memory-model.h>
24 #include <mono/utils/mono-threads-coop.h>
25
26 #include "mini-arm.h"
27 #include "cpu-arm.h"
28 #include "trace.h"
29 #include "ir-emit.h"
30 #include "debugger-agent.h"
31 #include "mini-gc.h"
32 #include "mono/arch/arm/arm-vfp-codegen.h"
33
34 /* Sanity check: This makes no sense */
35 #if defined(ARM_FPU_NONE) && (defined(ARM_FPU_VFP) || defined(ARM_FPU_VFP_HARD))
36 #error "ARM_FPU_NONE is defined while one of ARM_FPU_VFP/ARM_FPU_VFP_HARD is defined"
37 #endif
38
39 /*
40  * IS_SOFT_FLOAT: Is full software floating point used?
41  * IS_HARD_FLOAT: Is full hardware floating point used?
42  * IS_VFP: Is hardware floating point with software ABI used?
43  *
44  * These are not necessarily constants, e.g. IS_SOFT_FLOAT and
45  * IS_VFP may delegate to mono_arch_is_soft_float ().
46  */
47
48 #if defined(ARM_FPU_VFP_HARD)
49 #define IS_SOFT_FLOAT (FALSE)
50 #define IS_HARD_FLOAT (TRUE)
51 #define IS_VFP (TRUE)
52 #elif defined(ARM_FPU_NONE)
53 #define IS_SOFT_FLOAT (mono_arch_is_soft_float ())
54 #define IS_HARD_FLOAT (FALSE)
55 #define IS_VFP (!mono_arch_is_soft_float ())
56 #else
57 #define IS_SOFT_FLOAT (FALSE)
58 #define IS_HARD_FLOAT (FALSE)
59 #define IS_VFP (TRUE)
60 #endif
61
62 #define THUNK_SIZE (3 * 4)
63
64 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
65
66 #if __APPLE__
67 void sys_icache_invalidate (void *start, size_t len);
68 #endif
69
70 /* This mutex protects architecture specific caches */
71 #define mono_mini_arch_lock() mono_os_mutex_lock (&mini_arch_mutex)
72 #define mono_mini_arch_unlock() mono_os_mutex_unlock (&mini_arch_mutex)
73 static mono_mutex_t mini_arch_mutex;
74
75 static gboolean v5_supported = FALSE;
76 static gboolean v6_supported = FALSE;
77 static gboolean v7_supported = FALSE;
78 static gboolean v7s_supported = FALSE;
79 static gboolean v7k_supported = FALSE;
80 static gboolean thumb_supported = FALSE;
81 static gboolean thumb2_supported = FALSE;
82 /*
83  * Whenever to use the ARM EABI
84  */
85 static gboolean eabi_supported = FALSE;
86
87 /* 
88  * Whenever to use the iphone ABI extensions:
89  * http://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iPhoneOSABIReference/index.html
90  * Basically, r7 is used as a frame pointer and it should point to the saved r7 + lr.
91  * This is required for debugging/profiling tools to work, but it has some overhead so it should
92  * only be turned on in debug builds.
93  */
94 static gboolean iphone_abi = FALSE;
95
96 /*
97  * The FPU we are generating code for. This is NOT runtime configurable right now,
98  * since some things like MONO_ARCH_CALLEE_FREGS still depend on defines.
99  */
100 static MonoArmFPU arm_fpu;
101
102 #if defined(ARM_FPU_VFP_HARD)
103 /*
104  * On armhf, d0-d7 are used for argument passing and d8-d15
105  * must be preserved across calls, which leaves us no room
106  * for scratch registers. So we use d14-d15 but back up their
107  * previous contents to a stack slot before using them - see
108  * mono_arm_emit_vfp_scratch_save/_restore ().
109  */
110 static int vfp_scratch1 = ARM_VFP_D14;
111 static int vfp_scratch2 = ARM_VFP_D15;
112 #else
113 /*
114  * On armel, d0-d7 do not need to be preserved, so we can
115  * freely make use of them as scratch registers.
116  */
117 static int vfp_scratch1 = ARM_VFP_D0;
118 static int vfp_scratch2 = ARM_VFP_D1;
119 #endif
120
121 static int i8_align;
122
123 static gpointer single_step_tramp, breakpoint_tramp;
124 static gpointer get_tls_tramp;
125
126 /*
127  * The code generated for sequence points reads from this location, which is
128  * made read-only when single stepping is enabled.
129  */
130 static gpointer ss_trigger_page;
131
132 /* Enabled breakpoints read from this trigger page */
133 static gpointer bp_trigger_page;
134
135 /*
136  * TODO:
137  * floating point support: on ARM it is a mess, there are at least 3
138  * different setups, each of which binary incompat with the other.
139  * 1) FPA: old and ugly, but unfortunately what current distros use
140  *    the double binary format has the two words swapped. 8 double registers.
141  *    Implemented usually by kernel emulation.
142  * 2) softfloat: the compiler emulates all the fp ops. Usually uses the
143  *    ugly swapped double format (I guess a softfloat-vfp exists, too, though).
144  * 3) VFP: the new and actually sensible and useful FP support. Implemented
145  *    in HW or kernel-emulated, requires new tools. I think this is what symbian uses.
146  *
147  * We do not care about FPA. We will support soft float and VFP.
148  */
149 int mono_exc_esp_offset = 0;
150
151 #define arm_is_imm12(v) ((v) > -4096 && (v) < 4096)
152 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
153 #define arm_is_fpimm8(v) ((v) >= -1020 && (v) <= 1020)
154
155 #define LDR_MASK ((0xf << ARMCOND_SHIFT) | (3 << 26) | (1 << 22) | (1 << 20) | (15 << 12))
156 #define LDR_PC_VAL ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 26) | (0 << 22) | (1 << 20) | (15 << 12))
157 #define IS_LDR_PC(val) (((val) & LDR_MASK) == LDR_PC_VAL)
158
159 //#define DEBUG_IMT 0
160
161 #ifndef DISABLE_JIT
162 static void mono_arch_compute_omit_fp (MonoCompile *cfg);
163 #endif
164
165 static guint8*
166 emit_aotconst (MonoCompile *cfg, guint8 *code, int dreg, int patch_type, gpointer data);
167
168 const char*
169 mono_arch_regname (int reg)
170 {
171         static const char * rnames[] = {
172                 "arm_r0", "arm_r1", "arm_r2", "arm_r3", "arm_v1",
173                 "arm_v2", "arm_v3", "arm_v4", "arm_v5", "arm_v6",
174                 "arm_v7", "arm_fp", "arm_ip", "arm_sp", "arm_lr",
175                 "arm_pc"
176         };
177         if (reg >= 0 && reg < 16)
178                 return rnames [reg];
179         return "unknown";
180 }
181
182 const char*
183 mono_arch_fregname (int reg)
184 {
185         static const char * rnames[] = {
186                 "arm_f0", "arm_f1", "arm_f2", "arm_f3", "arm_f4",
187                 "arm_f5", "arm_f6", "arm_f7", "arm_f8", "arm_f9",
188                 "arm_f10", "arm_f11", "arm_f12", "arm_f13", "arm_f14",
189                 "arm_f15", "arm_f16", "arm_f17", "arm_f18", "arm_f19",
190                 "arm_f20", "arm_f21", "arm_f22", "arm_f23", "arm_f24",
191                 "arm_f25", "arm_f26", "arm_f27", "arm_f28", "arm_f29",
192                 "arm_f30", "arm_f31"
193         };
194         if (reg >= 0 && reg < 32)
195                 return rnames [reg];
196         return "unknown";
197 }
198
199
200 #ifndef DISABLE_JIT
201 static guint8*
202 emit_big_add (guint8 *code, int dreg, int sreg, int imm)
203 {
204         int imm8, rot_amount;
205         if ((imm8 = mono_arm_is_rotated_imm8 (imm, &rot_amount)) >= 0) {
206                 ARM_ADD_REG_IMM (code, dreg, sreg, imm8, rot_amount);
207                 return code;
208         }
209         if (dreg == sreg) {
210                 code = mono_arm_emit_load_imm (code, ARMREG_IP, imm);
211                 ARM_ADD_REG_REG (code, dreg, sreg, ARMREG_IP);
212         } else {
213                 code = mono_arm_emit_load_imm (code, dreg, imm);
214                 ARM_ADD_REG_REG (code, dreg, dreg, sreg);
215         }
216         return code;
217 }
218
219 /* If dreg == sreg, this clobbers IP */
220 static guint8*
221 emit_sub_imm (guint8 *code, int dreg, int sreg, int imm)
222 {
223         int imm8, rot_amount;
224         if ((imm8 = mono_arm_is_rotated_imm8 (imm, &rot_amount)) >= 0) {
225                 ARM_SUB_REG_IMM (code, dreg, sreg, imm8, rot_amount);
226                 return code;
227         }
228         if (dreg == sreg) {
229                 code = mono_arm_emit_load_imm (code, ARMREG_IP, imm);
230                 ARM_SUB_REG_REG (code, dreg, sreg, ARMREG_IP);
231         } else {
232                 code = mono_arm_emit_load_imm (code, dreg, imm);
233                 ARM_SUB_REG_REG (code, dreg, dreg, sreg);
234         }
235         return code;
236 }
237
238 static guint8*
239 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
240 {
241         /* we can use r0-r3, since this is called only for incoming args on the stack */
242         if (size > sizeof (gpointer) * 4) {
243                 guint8 *start_loop;
244                 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
245                 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
246                 start_loop = code = mono_arm_emit_load_imm (code, ARMREG_R2, size);
247                 ARM_LDR_IMM (code, ARMREG_R3, ARMREG_R0, 0);
248                 ARM_STR_IMM (code, ARMREG_R3, ARMREG_R1, 0);
249                 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, 4);
250                 ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, 4);
251                 ARM_SUBS_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, 4);
252                 ARM_B_COND (code, ARMCOND_NE, 0);
253                 arm_patch (code - 4, start_loop);
254                 return code;
255         }
256         if (arm_is_imm12 (doffset) && arm_is_imm12 (doffset + size) &&
257                         arm_is_imm12 (soffset) && arm_is_imm12 (soffset + size)) {
258                 while (size >= 4) {
259                         ARM_LDR_IMM (code, ARMREG_LR, sreg, soffset);
260                         ARM_STR_IMM (code, ARMREG_LR, dreg, doffset);
261                         doffset += 4;
262                         soffset += 4;
263                         size -= 4;
264                 }
265         } else if (size) {
266                 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
267                 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
268                 doffset = soffset = 0;
269                 while (size >= 4) {
270                         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R0, soffset);
271                         ARM_STR_IMM (code, ARMREG_LR, ARMREG_R1, doffset);
272                         doffset += 4;
273                         soffset += 4;
274                         size -= 4;
275                 }
276         }
277         g_assert (size == 0);
278         return code;
279 }
280
281 static guint8*
282 emit_call_reg (guint8 *code, int reg)
283 {
284         if (v5_supported) {
285                 ARM_BLX_REG (code, reg);
286         } else {
287                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
288                 if (thumb_supported)
289                         ARM_BX (code, reg);
290                 else
291                         ARM_MOV_REG_REG (code, ARMREG_PC, reg);
292         }
293         return code;
294 }
295
296 static guint8*
297 emit_call_seq (MonoCompile *cfg, guint8 *code)
298 {
299         if (cfg->method->dynamic) {
300                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
301                 ARM_B (code, 0);
302                 *(gpointer*)code = NULL;
303                 code += 4;
304                 code = emit_call_reg (code, ARMREG_IP);
305         } else {
306                 ARM_BL (code, 0);
307         }
308         cfg->thunk_area += THUNK_SIZE;
309         return code;
310 }
311
312 guint8*
313 mono_arm_patchable_b (guint8 *code, int cond)
314 {
315         ARM_B_COND (code, cond, 0);
316         return code;
317 }
318
319 guint8*
320 mono_arm_patchable_bl (guint8 *code, int cond)
321 {
322         ARM_BL_COND (code, cond, 0);
323         return code;
324 }
325
326 #if defined(__ARM_EABI__) && defined(__linux__) && !defined(PLATFORM_ANDROID) && !defined(__native_client__)
327 #define HAVE_AEABI_READ_TP 1
328 #endif
329
330 #ifdef HAVE_AEABI_READ_TP
331 gpointer __aeabi_read_tp (void);
332 #endif
333
334 gboolean
335 mono_arch_have_fast_tls (void)
336 {
337 #ifdef HAVE_AEABI_READ_TP
338         static gboolean have_fast_tls = FALSE;
339         static gboolean inited = FALSE;
340         gpointer tp1, tp2;
341
342         if (mini_get_debug_options ()->use_fallback_tls)
343                 return FALSE;
344
345         if (inited)
346                 return have_fast_tls;
347
348         tp1 = __aeabi_read_tp ();
349         asm volatile("mrc p15, 0, %0, c13, c0, 3" : "=r" (tp2));
350
351         have_fast_tls = tp1 && tp1 == tp2;
352         inited = TRUE;
353         return have_fast_tls;
354 #else
355         return FALSE;
356 #endif
357 }
358
359 static guint8*
360 emit_tls_get (guint8 *code, int dreg, int tls_offset)
361 {
362         ARM_MRC (code, 15, 0, dreg, 13, 0, 3);
363         ARM_LDR_IMM (code, dreg, dreg, tls_offset);
364         return code;
365 }
366
367 static guint8*
368 emit_tls_set (guint8 *code, int sreg, int tls_offset)
369 {
370         int tp_reg = (sreg != ARMREG_R0) ? ARMREG_R0 : ARMREG_R1;
371         ARM_MRC (code, 15, 0, tp_reg, 13, 0, 3);
372         ARM_STR_IMM (code, sreg, tp_reg, tls_offset);
373         return code;
374 }
375
376 /*
377  * emit_save_lmf:
378  *
379  *   Emit code to push an LMF structure on the LMF stack.
380  * On arm, this is intermixed with the initialization of other fields of the structure.
381  */
382 static guint8*
383 emit_save_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
384 {
385         int i;
386
387         if (mono_arch_have_fast_tls () && mono_tls_get_tls_offset (TLS_KEY_LMF_ADDR) != -1) {
388                 code = emit_tls_get (code, ARMREG_R0, mono_tls_get_tls_offset (TLS_KEY_LMF_ADDR));
389         } else {
390                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
391                                                          (gpointer)"mono_tls_get_lmf_addr");
392                 code = emit_call_seq (cfg, code);
393         }
394         /* we build the MonoLMF structure on the stack - see mini-arm.h */
395         /* lmf_offset is the offset from the previous stack pointer,
396          * alloc_size is the total stack space allocated, so the offset
397          * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
398          * The pointer to the struct is put in r1 (new_lmf).
399          * ip is used as scratch
400          * The callee-saved registers are already in the MonoLMF structure
401          */
402         code = emit_big_add (code, ARMREG_R1, ARMREG_SP, lmf_offset);
403         /* r0 is the result from mono_get_lmf_addr () */
404         ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
405         /* new_lmf->previous_lmf = *lmf_addr */
406         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
407         ARM_STR_IMM (code, ARMREG_IP, ARMREG_R1, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
408         /* *(lmf_addr) = r1 */
409         ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
410         /* Skip method (only needed for trampoline LMF frames) */
411         ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, MONO_STRUCT_OFFSET (MonoLMF, sp));
412         ARM_STR_IMM (code, ARMREG_FP, ARMREG_R1, MONO_STRUCT_OFFSET (MonoLMF, fp));
413         /* save the current IP */
414         ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
415         ARM_STR_IMM (code, ARMREG_IP, ARMREG_R1, MONO_STRUCT_OFFSET (MonoLMF, ip));
416
417         for (i = 0; i < sizeof (MonoLMF); i += sizeof (mgreg_t))
418                 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + i, SLOT_NOREF);
419
420         return code;
421 }
422
423 typedef struct {
424         gint32 vreg;
425         gint32 hreg;
426 } FloatArgData;
427
428 static guint8 *
429 emit_float_args (MonoCompile *cfg, MonoCallInst *inst, guint8 *code, int *max_len, guint *offset)
430 {
431         GSList *list;
432
433         for (list = inst->float_args; list; list = list->next) {
434                 FloatArgData *fad = list->data;
435                 MonoInst *var = get_vreg_to_inst (cfg, fad->vreg);
436                 gboolean imm = arm_is_fpimm8 (var->inst_offset);
437
438                 /* 4+1 insns for emit_big_add () and 1 for FLDS. */
439                 if (!imm)
440                         *max_len += 20 + 4;
441
442                 *max_len += 4;
443
444                 if (*offset + *max_len > cfg->code_size) {
445                         cfg->code_size += *max_len;
446                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
447
448                         code = cfg->native_code + *offset;
449                 }
450
451                 if (!imm) {
452                         code = emit_big_add (code, ARMREG_LR, var->inst_basereg, var->inst_offset);
453                         ARM_FLDS (code, fad->hreg, ARMREG_LR, 0);
454                 } else
455                         ARM_FLDS (code, fad->hreg, var->inst_basereg, var->inst_offset);
456
457                 *offset = code - cfg->native_code;
458         }
459
460         return code;
461 }
462
463 static guint8 *
464 mono_arm_emit_vfp_scratch_save (MonoCompile *cfg, guint8 *code, int reg)
465 {
466         MonoInst *inst;
467
468         g_assert (reg == vfp_scratch1 || reg == vfp_scratch2);
469
470         inst = (MonoInst *) cfg->arch.vfp_scratch_slots [reg == vfp_scratch1 ? 0 : 1];
471
472         if (IS_HARD_FLOAT) {
473                 if (!arm_is_fpimm8 (inst->inst_offset)) {
474                         code = emit_big_add (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
475                         ARM_FSTD (code, reg, ARMREG_LR, 0);
476                 } else
477                         ARM_FSTD (code, reg, inst->inst_basereg, inst->inst_offset);
478         }
479
480         return code;
481 }
482
483 static guint8 *
484 mono_arm_emit_vfp_scratch_restore (MonoCompile *cfg, guint8 *code, int reg)
485 {
486         MonoInst *inst;
487
488         g_assert (reg == vfp_scratch1 || reg == vfp_scratch2);
489
490         inst = (MonoInst *) cfg->arch.vfp_scratch_slots [reg == vfp_scratch1 ? 0 : 1];
491
492         if (IS_HARD_FLOAT) {
493                 if (!arm_is_fpimm8 (inst->inst_offset)) {
494                         code = emit_big_add (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
495                         ARM_FLDD (code, reg, ARMREG_LR, 0);
496                 } else
497                         ARM_FLDD (code, reg, inst->inst_basereg, inst->inst_offset);
498         }
499
500         return code;
501 }
502
503 /*
504  * emit_restore_lmf:
505  *
506  *   Emit code to pop an LMF structure from the LMF stack.
507  */
508 static guint8*
509 emit_restore_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
510 {
511         int basereg, offset;
512
513         if (lmf_offset < 32) {
514                 basereg = cfg->frame_reg;
515                 offset = lmf_offset;
516         } else {
517                 basereg = ARMREG_R2;
518                 offset = 0;
519                 code = emit_big_add (code, ARMREG_R2, cfg->frame_reg, lmf_offset);
520         }
521
522         /* ip = previous_lmf */
523         ARM_LDR_IMM (code, ARMREG_IP, basereg, offset + MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
524         /* lr = lmf_addr */
525         ARM_LDR_IMM (code, ARMREG_LR, basereg, offset + MONO_STRUCT_OFFSET (MonoLMF, lmf_addr));
526         /* *(lmf_addr) = previous_lmf */
527         ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, MONO_STRUCT_OFFSET (MonoLMF, previous_lmf));
528
529         return code;
530 }
531
532 #endif /* #ifndef DISABLE_JIT */
533
534 /*
535  * mono_arch_get_argument_info:
536  * @csig:  a method signature
537  * @param_count: the number of parameters to consider
538  * @arg_info: an array to store the result infos
539  *
540  * Gathers information on parameters such as size, alignment and
541  * padding. arg_info should be large enought to hold param_count + 1 entries. 
542  *
543  * Returns the size of the activation frame.
544  */
545 int
546 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
547 {
548         int k, frame_size = 0;
549         guint32 size, align, pad;
550         int offset = 8;
551         MonoType *t;
552
553         t = mini_get_underlying_type (csig->ret);
554         if (MONO_TYPE_ISSTRUCT (t)) {
555                 frame_size += sizeof (gpointer);
556                 offset += 4;
557         }
558
559         arg_info [0].offset = offset;
560
561         if (csig->hasthis) {
562                 frame_size += sizeof (gpointer);
563                 offset += 4;
564         }
565
566         arg_info [0].size = frame_size;
567
568         for (k = 0; k < param_count; k++) {
569                 size = mini_type_stack_size_full (csig->params [k], &align, csig->pinvoke);
570
571                 /* ignore alignment for now */
572                 align = 1;
573
574                 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1); 
575                 arg_info [k].pad = pad;
576                 frame_size += size;
577                 arg_info [k + 1].pad = 0;
578                 arg_info [k + 1].size = size;
579                 offset += pad;
580                 arg_info [k + 1].offset = offset;
581                 offset += size;
582         }
583
584         align = MONO_ARCH_FRAME_ALIGNMENT;
585         frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
586         arg_info [k].pad = pad;
587
588         return frame_size;
589 }
590
591 #define MAX_ARCH_DELEGATE_PARAMS 3
592
593 static gpointer
594 get_delegate_invoke_impl (MonoTrampInfo **info, gboolean has_target, gboolean param_count)
595 {
596         guint8 *code, *start;
597         GSList *unwind_ops = mono_arch_get_cie_program ();
598
599         if (has_target) {
600                 start = code = mono_global_codeman_reserve (12);
601
602                 /* Replace the this argument with the target */
603                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
604                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, MONO_STRUCT_OFFSET (MonoDelegate, target));
605                 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
606
607                 g_assert ((code - start) <= 12);
608
609                 mono_arch_flush_icache (start, 12);
610         } else {
611                 int size, i;
612
613                 size = 8 + param_count * 4;
614                 start = code = mono_global_codeman_reserve (size);
615
616                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, MONO_STRUCT_OFFSET (MonoDelegate, method_ptr));
617                 /* slide down the arguments */
618                 for (i = 0; i < param_count; ++i) {
619                         ARM_MOV_REG_REG (code, (ARMREG_R0 + i), (ARMREG_R0 + i + 1));
620                 }
621                 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
622
623                 g_assert ((code - start) <= size);
624
625                 mono_arch_flush_icache (start, size);
626         }
627
628         if (has_target) {
629                  *info = mono_tramp_info_create ("delegate_invoke_impl_has_target", start, code - start, NULL, unwind_ops);
630         } else {
631                  char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", param_count);
632                  *info = mono_tramp_info_create (name, start, code - start, NULL, unwind_ops);
633                  g_free (name);
634         }
635
636         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_DELEGATE_INVOKE, NULL);
637
638         return start;
639 }
640
641 /*
642  * mono_arch_get_delegate_invoke_impls:
643  *
644  *   Return a list of MonoAotTrampInfo structures for the delegate invoke impl
645  * trampolines.
646  */
647 GSList*
648 mono_arch_get_delegate_invoke_impls (void)
649 {
650         GSList *res = NULL;
651         MonoTrampInfo *info;
652         int i;
653
654         get_delegate_invoke_impl (&info, TRUE, 0);
655         res = g_slist_prepend (res, info);
656
657         for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
658                 get_delegate_invoke_impl (&info, FALSE, i);
659                 res = g_slist_prepend (res, info);
660         }
661
662         return res;
663 }
664
665 gpointer
666 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
667 {
668         guint8 *code, *start;
669         MonoType *sig_ret;
670
671         /* FIXME: Support more cases */
672         sig_ret = mini_get_underlying_type (sig->ret);
673         if (MONO_TYPE_ISSTRUCT (sig_ret))
674                 return NULL;
675
676         if (has_target) {
677                 static guint8* cached = NULL;
678                 mono_mini_arch_lock ();
679                 if (cached) {
680                         mono_mini_arch_unlock ();
681                         return cached;
682                 }
683
684                 if (mono_aot_only) {
685                         start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
686                 } else {
687                         MonoTrampInfo *info;
688                         start = get_delegate_invoke_impl (&info, TRUE, 0);
689                         mono_tramp_info_register (info, NULL);
690                 }
691                 cached = start;
692                 mono_mini_arch_unlock ();
693                 return cached;
694         } else {
695                 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
696                 int i;
697
698                 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
699                         return NULL;
700                 for (i = 0; i < sig->param_count; ++i)
701                         if (!mono_is_regsize_var (sig->params [i]))
702                                 return NULL;
703
704                 mono_mini_arch_lock ();
705                 code = cache [sig->param_count];
706                 if (code) {
707                         mono_mini_arch_unlock ();
708                         return code;
709                 }
710
711                 if (mono_aot_only) {
712                         char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
713                         start = mono_aot_get_trampoline (name);
714                         g_free (name);
715                 } else {
716                         MonoTrampInfo *info;
717                         start = get_delegate_invoke_impl (&info, FALSE, sig->param_count);
718                         mono_tramp_info_register (info, NULL);
719                 }
720                 cache [sig->param_count] = start;
721                 mono_mini_arch_unlock ();
722                 return start;
723         }
724
725         return NULL;
726 }
727
728 gpointer
729 mono_arch_get_delegate_virtual_invoke_impl (MonoMethodSignature *sig, MonoMethod *method, int offset, gboolean load_imt_reg)
730 {
731         return NULL;
732 }
733
734 gpointer
735 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
736 {
737         return (gpointer)regs [ARMREG_R0];
738 }
739
740 /*
741  * Initialize the cpu to execute managed code.
742  */
743 void
744 mono_arch_cpu_init (void)
745 {
746         i8_align = MONO_ABI_ALIGNOF (gint64);
747 #ifdef MONO_CROSS_COMPILE
748         /* Need to set the alignment of i8 since it can different on the target */
749 #ifdef TARGET_ANDROID
750         /* linux gnueabi */
751         mono_type_set_alignment (MONO_TYPE_I8, i8_align);
752 #endif
753 #endif
754 }
755
756 /*
757  * Initialize architecture specific code.
758  */
759 void
760 mono_arch_init (void)
761 {
762         const char *cpu_arch;
763
764 #ifdef TARGET_WATCHOS
765         mini_get_debug_options ()->soft_breakpoints = TRUE;
766 #endif
767
768         mono_os_mutex_init_recursive (&mini_arch_mutex);
769         if (mini_get_debug_options ()->soft_breakpoints) {
770                 if (!mono_aot_only)
771                         breakpoint_tramp = mini_get_breakpoint_trampoline ();
772         } else {
773                 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT, MONO_MEM_ACCOUNT_OTHER);
774                 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT, MONO_MEM_ACCOUNT_OTHER);
775                 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
776         }
777
778         mono_aot_register_jit_icall ("mono_arm_throw_exception", mono_arm_throw_exception);
779         mono_aot_register_jit_icall ("mono_arm_throw_exception_by_token", mono_arm_throw_exception_by_token);
780         mono_aot_register_jit_icall ("mono_arm_resume_unwind", mono_arm_resume_unwind);
781 #if defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
782         mono_aot_register_jit_icall ("mono_arm_start_gsharedvt_call", mono_arm_start_gsharedvt_call);
783 #endif
784         mono_aot_register_jit_icall ("mono_arm_unaligned_stack", mono_arm_unaligned_stack);
785         mono_aot_register_jit_icall ("mono_arm_handler_block_trampoline_helper", mono_arm_handler_block_trampoline_helper);
786 #if defined(__ARM_EABI__)
787         eabi_supported = TRUE;
788 #endif
789
790 #if defined(ARM_FPU_VFP_HARD)
791         arm_fpu = MONO_ARM_FPU_VFP_HARD;
792 #else
793         arm_fpu = MONO_ARM_FPU_VFP;
794
795 #if defined(ARM_FPU_NONE) && !defined(TARGET_IOS)
796         /*
797          * If we're compiling with a soft float fallback and it
798          * turns out that no VFP unit is available, we need to
799          * switch to soft float. We don't do this for iOS, since
800          * iOS devices always have a VFP unit.
801          */
802         if (!mono_hwcap_arm_has_vfp)
803                 arm_fpu = MONO_ARM_FPU_NONE;
804
805         /*
806          * This environment variable can be useful in testing
807          * environments to make sure the soft float fallback
808          * works. Most ARM devices have VFP units these days, so
809          * normally soft float code would not be exercised much.
810          */
811         const char *soft = g_getenv ("MONO_ARM_FORCE_SOFT_FLOAT");
812
813         if (soft && !strncmp (soft, "1", 1))
814                 arm_fpu = MONO_ARM_FPU_NONE;
815 #endif
816 #endif
817
818         v5_supported = mono_hwcap_arm_is_v5;
819         v6_supported = mono_hwcap_arm_is_v6;
820         v7_supported = mono_hwcap_arm_is_v7;
821
822         /*
823          * On weird devices, the hwcap code may fail to detect
824          * the ARM version. In that case, we can at least safely
825          * assume the version the runtime was compiled for.
826          */
827 #ifdef HAVE_ARMV5
828         v5_supported = TRUE;
829 #endif
830 #ifdef HAVE_ARMV6
831         v6_supported = TRUE;
832 #endif
833 #ifdef HAVE_ARMV7
834         v7_supported = TRUE;
835 #endif
836
837 #if defined(TARGET_IOS)
838         /* iOS is special-cased here because we don't yet
839            have a way to properly detect CPU features on it. */
840         thumb_supported = TRUE;
841         iphone_abi = TRUE;
842 #else
843         thumb_supported = mono_hwcap_arm_has_thumb;
844         thumb2_supported = mono_hwcap_arm_has_thumb2;
845 #endif
846
847         /* Format: armv(5|6|7[s])[-thumb[2]] */
848         cpu_arch = g_getenv ("MONO_CPU_ARCH");
849
850         /* Do this here so it overrides any detection. */
851         if (cpu_arch) {
852                 if (strncmp (cpu_arch, "armv", 4) == 0) {
853                         v5_supported = cpu_arch [4] >= '5';
854                         v6_supported = cpu_arch [4] >= '6';
855                         v7_supported = cpu_arch [4] >= '7';
856                         v7s_supported = strncmp (cpu_arch, "armv7s", 6) == 0;
857                         v7k_supported = strncmp (cpu_arch, "armv7k", 6) == 0;
858                 }
859
860                 thumb_supported = strstr (cpu_arch, "thumb") != NULL;
861                 thumb2_supported = strstr (cpu_arch, "thumb2") != NULL;
862         }
863 }
864
865 /*
866  * Cleanup architecture specific code.
867  */
868 void
869 mono_arch_cleanup (void)
870 {
871 }
872
873 /*
874  * This function returns the optimizations supported on this cpu.
875  */
876 guint32
877 mono_arch_cpu_optimizations (guint32 *exclude_mask)
878 {
879         /* no arm-specific optimizations yet */
880         *exclude_mask = 0;
881         return 0;
882 }
883
884 /*
885  * This function test for all SIMD functions supported.
886  *
887  * Returns a bitmask corresponding to all supported versions.
888  *
889  */
890 guint32
891 mono_arch_cpu_enumerate_simd_versions (void)
892 {
893         /* SIMD is currently unimplemented */
894         return 0;
895 }
896
897 gboolean
898 mono_arm_is_hard_float (void)
899 {
900         return arm_fpu == MONO_ARM_FPU_VFP_HARD;
901 }
902
903 #ifndef DISABLE_JIT
904
905 gboolean
906 mono_arch_opcode_needs_emulation (MonoCompile *cfg, int opcode)
907 {
908         if (v7s_supported || v7k_supported) {
909                 switch (opcode) {
910                 case OP_IDIV:
911                 case OP_IREM:
912                 case OP_IDIV_UN:
913                 case OP_IREM_UN:
914                         return FALSE;
915                 default:
916                         break;
917                 }
918         }
919         return TRUE;
920 }
921
922 #ifdef MONO_ARCH_SOFT_FLOAT_FALLBACK
923 gboolean
924 mono_arch_is_soft_float (void)
925 {
926         return arm_fpu == MONO_ARM_FPU_NONE;
927 }
928 #endif
929
930 static gboolean
931 is_regsize_var (MonoType *t)
932 {
933         if (t->byref)
934                 return TRUE;
935         t = mini_get_underlying_type (t);
936         switch (t->type) {
937         case MONO_TYPE_I4:
938         case MONO_TYPE_U4:
939         case MONO_TYPE_I:
940         case MONO_TYPE_U:
941         case MONO_TYPE_PTR:
942         case MONO_TYPE_FNPTR:
943                 return TRUE;
944         case MONO_TYPE_OBJECT:
945         case MONO_TYPE_STRING:
946         case MONO_TYPE_CLASS:
947         case MONO_TYPE_SZARRAY:
948         case MONO_TYPE_ARRAY:
949                 return TRUE;
950         case MONO_TYPE_GENERICINST:
951                 if (!mono_type_generic_inst_is_valuetype (t))
952                         return TRUE;
953                 return FALSE;
954         case MONO_TYPE_VALUETYPE:
955                 return FALSE;
956         }
957         return FALSE;
958 }
959
960 GList *
961 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
962 {
963         GList *vars = NULL;
964         int i;
965
966         for (i = 0; i < cfg->num_varinfo; i++) {
967                 MonoInst *ins = cfg->varinfo [i];
968                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
969
970                 /* unused vars */
971                 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
972                         continue;
973
974                 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
975                         continue;
976
977                 /* we can only allocate 32 bit values */
978                 if (is_regsize_var (ins->inst_vtype)) {
979                         g_assert (MONO_VARINFO (cfg, i)->reg == -1);
980                         g_assert (i == vmv->idx);
981                         vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
982                 }
983         }
984
985         return vars;
986 }
987
988 GList *
989 mono_arch_get_global_int_regs (MonoCompile *cfg)
990 {
991         GList *regs = NULL;
992
993         mono_arch_compute_omit_fp (cfg);
994
995         /* 
996          * FIXME: Interface calls might go through a static rgctx trampoline which
997          * sets V5, but it doesn't save it, so we need to save it ourselves, and
998          * avoid using it.
999          */
1000         if (cfg->flags & MONO_CFG_HAS_CALLS)
1001                 cfg->uses_rgctx_reg = TRUE;
1002
1003         if (cfg->arch.omit_fp)
1004                 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_FP));
1005         regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V1));
1006         regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V2));
1007         regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V3));
1008         if (iphone_abi)
1009                 /* V4=R7 is used as a frame pointer, but V7=R10 is preserved */
1010                 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));
1011         else
1012                 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V4));
1013         if (!(cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg)))
1014                 /* V5 is reserved for passing the vtable/rgctx/IMT method */
1015                 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V5));
1016         /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V6));*/
1017         /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));*/
1018
1019         return regs;
1020 }
1021
1022 /*
1023  * mono_arch_regalloc_cost:
1024  *
1025  *  Return the cost, in number of memory references, of the action of 
1026  * allocating the variable VMV into a register during global register
1027  * allocation.
1028  */
1029 guint32
1030 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
1031 {
1032         /* FIXME: */
1033         return 2;
1034 }
1035
1036 #endif /* #ifndef DISABLE_JIT */
1037
1038 void
1039 mono_arch_flush_icache (guint8 *code, gint size)
1040 {
1041 #if defined(MONO_CROSS_COMPILE)
1042 #elif __APPLE__
1043         sys_icache_invalidate (code, size);
1044 #else
1045     __builtin___clear_cache (code, code + size);
1046 #endif
1047 }
1048
1049 #define DEBUG(a)
1050
1051 static void inline
1052 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
1053 {
1054         if (simple) {
1055                 if (*gr > ARMREG_R3) {
1056                         ainfo->size = 4;
1057                         ainfo->offset = *stack_size;
1058                         ainfo->reg = ARMREG_SP; /* in the caller */
1059                         ainfo->storage = RegTypeBase;
1060                         *stack_size += 4;
1061                 } else {
1062                         ainfo->storage = RegTypeGeneral;
1063                         ainfo->reg = *gr;
1064                 }
1065         } else {
1066                 gboolean split;
1067
1068                 if (eabi_supported)
1069                         split = i8_align == 4;
1070                 else
1071                         split = TRUE;
1072
1073                 ainfo->size = 8;
1074                 if (*gr == ARMREG_R3 && split) {
1075                         /* first word in r3 and the second on the stack */
1076                         ainfo->offset = *stack_size;
1077                         ainfo->reg = ARMREG_SP; /* in the caller */
1078                         ainfo->storage = RegTypeBaseGen;
1079                         *stack_size += 4;
1080                 } else if (*gr >= ARMREG_R3) {
1081                         if (eabi_supported) {
1082                                 /* darwin aligns longs to 4 byte only */
1083                                 if (i8_align == 8) {
1084                                         *stack_size += 7;
1085                                         *stack_size &= ~7;
1086                                 }
1087                         }
1088                         ainfo->offset = *stack_size;
1089                         ainfo->reg = ARMREG_SP; /* in the caller */
1090                         ainfo->storage = RegTypeBase;
1091                         *stack_size += 8;
1092                 } else {
1093                         if (eabi_supported) {
1094                                 if (i8_align == 8 && ((*gr) & 1))
1095                                         (*gr) ++;
1096                         }
1097                         ainfo->storage = RegTypeIRegPair;
1098                         ainfo->reg = *gr;
1099                 }
1100                 (*gr) ++;
1101         }
1102         (*gr) ++;
1103 }
1104
1105 static void inline
1106 add_float (guint *fpr, guint *stack_size, ArgInfo *ainfo, gboolean is_double, gint *float_spare)
1107 {
1108         /*
1109          * If we're calling a function like this:
1110          *
1111          * void foo(float a, double b, float c)
1112          *
1113          * We pass a in s0 and b in d1. That leaves us
1114          * with s1 being unused. The armhf ABI recognizes
1115          * this and requires register assignment to then
1116          * use that for the next single-precision arg,
1117          * i.e. c in this example. So float_spare either
1118          * tells us which reg to use for the next single-
1119          * precision arg, or it's -1, meaning use *fpr.
1120          *
1121          * Note that even though most of the JIT speaks
1122          * double-precision, fpr represents single-
1123          * precision registers.
1124          *
1125          * See parts 5.5 and 6.1.2 of the AAPCS for how
1126          * this all works.
1127          */
1128
1129         if (*fpr < ARM_VFP_F16 || (!is_double && *float_spare >= 0)) {
1130                 ainfo->storage = RegTypeFP;
1131
1132                 if (is_double) {
1133                         /*
1134                          * If we're passing a double-precision value
1135                          * and *fpr is odd (e.g. it's s1, s3, ...)
1136                          * we need to use the next even register. So
1137                          * we mark the current *fpr as a spare that
1138                          * can be used for the next single-precision
1139                          * value.
1140                          */
1141                         if (*fpr % 2) {
1142                                 *float_spare = *fpr;
1143                                 (*fpr)++;
1144                         }
1145
1146                         /*
1147                          * At this point, we have an even register
1148                          * so we assign that and move along.
1149                          */
1150                         ainfo->reg = *fpr;
1151                         *fpr += 2;
1152                 } else if (*float_spare >= 0) {
1153                         /*
1154                          * We're passing a single-precision value
1155                          * and it looks like a spare single-
1156                          * precision register is available. Let's
1157                          * use it.
1158                          */
1159
1160                         ainfo->reg = *float_spare;
1161                         *float_spare = -1;
1162                 } else {
1163                         /*
1164                          * If we hit this branch, we're passing a
1165                          * single-precision value and we can simply
1166                          * use the next available register.
1167                          */
1168
1169                         ainfo->reg = *fpr;
1170                         (*fpr)++;
1171                 }
1172         } else {
1173                 /*
1174                  * We've exhausted available floating point
1175                  * regs, so pass the rest on the stack.
1176                  */
1177
1178                 if (is_double) {
1179                         *stack_size += 7;
1180                         *stack_size &= ~7;
1181                 }
1182
1183                 ainfo->offset = *stack_size;
1184                 ainfo->reg = ARMREG_SP;
1185                 ainfo->storage = RegTypeBase;
1186
1187                 *stack_size += 8;
1188         }
1189 }
1190
1191 static gboolean
1192 is_hfa (MonoType *t, int *out_nfields, int *out_esize)
1193 {
1194         MonoClass *klass;
1195         gpointer iter;
1196         MonoClassField *field;
1197         MonoType *ftype, *prev_ftype = NULL;
1198         int nfields = 0;
1199
1200         klass = mono_class_from_mono_type (t);
1201         iter = NULL;
1202         while ((field = mono_class_get_fields (klass, &iter))) {
1203                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1204                         continue;
1205                 ftype = mono_field_get_type (field);
1206                 ftype = mini_get_underlying_type (ftype);
1207
1208                 if (MONO_TYPE_ISSTRUCT (ftype)) {
1209                         int nested_nfields, nested_esize;
1210
1211                         if (!is_hfa (ftype, &nested_nfields, &nested_esize))
1212                                 return FALSE;
1213                         if (nested_esize == 4)
1214                                 ftype = &mono_defaults.single_class->byval_arg;
1215                         else
1216                                 ftype = &mono_defaults.double_class->byval_arg;
1217                         if (prev_ftype && prev_ftype->type != ftype->type)
1218                                 return FALSE;
1219                         prev_ftype = ftype;
1220                         nfields += nested_nfields;
1221                 } else {
1222                         if (!(!ftype->byref && (ftype->type == MONO_TYPE_R4 || ftype->type == MONO_TYPE_R8)))
1223                                 return FALSE;
1224                         if (prev_ftype && prev_ftype->type != ftype->type)
1225                                 return FALSE;
1226                         prev_ftype = ftype;
1227                         nfields ++;
1228                 }
1229         }
1230         if (nfields == 0 || nfields > 4)
1231                 return FALSE;
1232         *out_nfields = nfields;
1233         *out_esize = prev_ftype->type == MONO_TYPE_R4 ? 4 : 8;
1234         return TRUE;
1235 }
1236
1237 static CallInfo*
1238 get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
1239 {
1240         guint i, gr, fpr, pstart;
1241         gint float_spare;
1242         int n = sig->hasthis + sig->param_count;
1243         int nfields, esize;
1244         guint32 align;
1245         MonoType *t;
1246         guint32 stack_size = 0;
1247         CallInfo *cinfo;
1248         gboolean is_pinvoke = sig->pinvoke;
1249         gboolean vtype_retaddr = FALSE;
1250
1251         if (mp)
1252                 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1253         else
1254                 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1255
1256         cinfo->nargs = n;
1257         gr = ARMREG_R0;
1258         fpr = ARM_VFP_F0;
1259         float_spare = -1;
1260
1261         t = mini_get_underlying_type (sig->ret);
1262         switch (t->type) {
1263         case MONO_TYPE_I1:
1264         case MONO_TYPE_U1:
1265         case MONO_TYPE_I2:
1266         case MONO_TYPE_U2:
1267         case MONO_TYPE_I4:
1268         case MONO_TYPE_U4:
1269         case MONO_TYPE_I:
1270         case MONO_TYPE_U:
1271         case MONO_TYPE_PTR:
1272         case MONO_TYPE_FNPTR:
1273         case MONO_TYPE_CLASS:
1274         case MONO_TYPE_OBJECT:
1275         case MONO_TYPE_SZARRAY:
1276         case MONO_TYPE_ARRAY:
1277         case MONO_TYPE_STRING:
1278                 cinfo->ret.storage = RegTypeGeneral;
1279                 cinfo->ret.reg = ARMREG_R0;
1280                 break;
1281         case MONO_TYPE_U8:
1282         case MONO_TYPE_I8:
1283                 cinfo->ret.storage = RegTypeIRegPair;
1284                 cinfo->ret.reg = ARMREG_R0;
1285                 break;
1286         case MONO_TYPE_R4:
1287         case MONO_TYPE_R8:
1288                 cinfo->ret.storage = RegTypeFP;
1289
1290                 if (t->type == MONO_TYPE_R4)
1291                         cinfo->ret.size = 4;
1292                 else
1293                         cinfo->ret.size = 8;
1294
1295                 if (IS_HARD_FLOAT) {
1296                         cinfo->ret.reg = ARM_VFP_F0;
1297                 } else {
1298                         cinfo->ret.reg = ARMREG_R0;
1299                 }
1300                 break;
1301         case MONO_TYPE_GENERICINST:
1302                 if (!mono_type_generic_inst_is_valuetype (t)) {
1303                         cinfo->ret.storage = RegTypeGeneral;
1304                         cinfo->ret.reg = ARMREG_R0;
1305                         break;
1306                 }
1307                 if (mini_is_gsharedvt_variable_type (t)) {
1308                         cinfo->ret.storage = RegTypeStructByAddr;
1309                         break;
1310                 }
1311                 /* Fall through */
1312         case MONO_TYPE_VALUETYPE:
1313         case MONO_TYPE_TYPEDBYREF:
1314                 if (IS_HARD_FLOAT && sig->pinvoke && is_hfa (t, &nfields, &esize)) {
1315                         cinfo->ret.storage = RegTypeHFA;
1316                         cinfo->ret.reg = 0;
1317                         cinfo->ret.nregs = nfields;
1318                         cinfo->ret.esize = esize;
1319                 } else {
1320                         if (is_pinvoke) {
1321                                 int native_size = mono_class_native_size (mono_class_from_mono_type (t), &align);
1322                                 int max_size;
1323
1324 #ifdef TARGET_WATCHOS
1325                                 max_size = 16;
1326 #else
1327                                 max_size = 4;
1328 #endif
1329                                 if (native_size <= max_size) {
1330                                         cinfo->ret.storage = RegTypeStructByVal;
1331                                         cinfo->ret.struct_size = native_size;
1332                                         cinfo->ret.nregs = ALIGN_TO (native_size, 4) / 4;
1333                                 } else {
1334                                         cinfo->ret.storage = RegTypeStructByAddr;
1335                                 }
1336                         } else {
1337                                 cinfo->ret.storage = RegTypeStructByAddr;
1338                         }
1339                 }
1340                 break;
1341         case MONO_TYPE_VAR:
1342         case MONO_TYPE_MVAR:
1343                 g_assert (mini_is_gsharedvt_type (t));
1344                 cinfo->ret.storage = RegTypeStructByAddr;
1345                 break;
1346         case MONO_TYPE_VOID:
1347                 break;
1348         default:
1349                 g_error ("Can't handle as return value 0x%x", sig->ret->type);
1350         }
1351
1352         vtype_retaddr = cinfo->ret.storage == RegTypeStructByAddr;
1353
1354         pstart = 0;
1355         n = 0;
1356         /*
1357          * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1358          * the first argument, allowing 'this' to be always passed in the first arg reg.
1359          * Also do this if the first argument is a reference type, since virtual calls
1360          * are sometimes made using calli without sig->hasthis set, like in the delegate
1361          * invoke wrappers.
1362          */
1363         if (vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_get_underlying_type (sig->params [0]))))) {
1364                 if (sig->hasthis) {
1365                         add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
1366                 } else {
1367                         add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0], TRUE);
1368                         pstart = 1;
1369                 }
1370                 n ++;
1371                 cinfo->ret.reg = gr;
1372                 gr ++;
1373                 cinfo->vret_arg_index = 1;
1374         } else {
1375                 /* this */
1376                 if (sig->hasthis) {
1377                         add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
1378                         n ++;
1379                 }
1380                 if (vtype_retaddr) {
1381                         cinfo->ret.reg = gr;
1382                         gr ++;
1383                 }
1384         }
1385
1386         DEBUG(g_print("params: %d\n", sig->param_count));
1387         for (i = pstart; i < sig->param_count; ++i) {
1388                 ArgInfo *ainfo = &cinfo->args [n];
1389
1390                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1391                         /* Prevent implicit arguments and sig_cookie from
1392                            being passed in registers */
1393                         gr = ARMREG_R3 + 1;
1394                         fpr = ARM_VFP_F16;
1395                         /* Emit the signature cookie just before the implicit arguments */
1396                         add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
1397                 }
1398                 DEBUG(g_print("param %d: ", i));
1399                 if (sig->params [i]->byref) {
1400                         DEBUG(g_print("byref\n"));
1401                         add_general (&gr, &stack_size, ainfo, TRUE);
1402                         n++;
1403                         continue;
1404                 }
1405                 t = mini_get_underlying_type (sig->params [i]);
1406                 switch (t->type) {
1407                 case MONO_TYPE_I1:
1408                 case MONO_TYPE_U1:
1409                         cinfo->args [n].size = 1;
1410                         add_general (&gr, &stack_size, ainfo, TRUE);
1411                         break;
1412                 case MONO_TYPE_I2:
1413                 case MONO_TYPE_U2:
1414                         cinfo->args [n].size = 2;
1415                         add_general (&gr, &stack_size, ainfo, TRUE);
1416                         break;
1417                 case MONO_TYPE_I4:
1418                 case MONO_TYPE_U4:
1419                         cinfo->args [n].size = 4;
1420                         add_general (&gr, &stack_size, ainfo, TRUE);
1421                         break;
1422                 case MONO_TYPE_I:
1423                 case MONO_TYPE_U:
1424                 case MONO_TYPE_PTR:
1425                 case MONO_TYPE_FNPTR:
1426                 case MONO_TYPE_CLASS:
1427                 case MONO_TYPE_OBJECT:
1428                 case MONO_TYPE_STRING:
1429                 case MONO_TYPE_SZARRAY:
1430                 case MONO_TYPE_ARRAY:
1431                         cinfo->args [n].size = sizeof (gpointer);
1432                         add_general (&gr, &stack_size, ainfo, TRUE);
1433                         break;
1434                 case MONO_TYPE_GENERICINST:
1435                         if (!mono_type_generic_inst_is_valuetype (t)) {
1436                                 cinfo->args [n].size = sizeof (gpointer);
1437                                 add_general (&gr, &stack_size, ainfo, TRUE);
1438                                 break;
1439                         }
1440                         if (mini_is_gsharedvt_variable_type (t)) {
1441                                 /* gsharedvt arguments are passed by ref */
1442                                 g_assert (mini_is_gsharedvt_type (t));
1443                                 add_general (&gr, &stack_size, ainfo, TRUE);
1444                                 switch (ainfo->storage) {
1445                                 case RegTypeGeneral:
1446                                         ainfo->storage = RegTypeGSharedVtInReg;
1447                                         break;
1448                                 case RegTypeBase:
1449                                         ainfo->storage = RegTypeGSharedVtOnStack;
1450                                         break;
1451                                 default:
1452                                         g_assert_not_reached ();
1453                                 }
1454                                 break;
1455                         }
1456                         /* Fall through */
1457                 case MONO_TYPE_TYPEDBYREF:
1458                 case MONO_TYPE_VALUETYPE: {
1459                         gint size;
1460                         int align_size;
1461                         int nwords, nfields, esize;
1462                         guint32 align;
1463
1464                         if (IS_HARD_FLOAT && sig->pinvoke && is_hfa (t, &nfields, &esize)) {
1465                                 if (fpr + nfields < ARM_VFP_F16) {
1466                                         ainfo->storage = RegTypeHFA;
1467                                         ainfo->reg = fpr;
1468                                         ainfo->nregs = nfields;
1469                                         ainfo->esize = esize;
1470                                         if (esize == 4)
1471                                                 fpr += nfields;
1472                                         else
1473                                                 fpr += nfields * 2;
1474                                         break;
1475                                 } else {
1476                                         fpr = ARM_VFP_F16;
1477                                 }
1478                         }
1479
1480                         if (t->type == MONO_TYPE_TYPEDBYREF) {
1481                                 size = sizeof (MonoTypedRef);
1482                                 align = sizeof (gpointer);
1483                         } else {
1484                                 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
1485                                 if (is_pinvoke)
1486                                         size = mono_class_native_size (klass, &align);
1487                                 else
1488                                         size = mini_type_stack_size_full (t, &align, FALSE);
1489                         }
1490                         DEBUG(g_print ("load %d bytes struct\n", size));
1491
1492 #ifdef TARGET_WATCHOS
1493                         /* Watchos pass large structures by ref */
1494                         /* We only do this for pinvoke to make gsharedvt/dyncall simpler */
1495                         if (sig->pinvoke && size > 16) {
1496                                 add_general (&gr, &stack_size, ainfo, TRUE);
1497                                 switch (ainfo->storage) {
1498                                 case RegTypeGeneral:
1499                                         ainfo->storage = RegTypeStructByAddr;
1500                                         break;
1501                                 case RegTypeBase:
1502                                         ainfo->storage = RegTypeStructByAddrOnStack;
1503                                         break;
1504                                 default:
1505                                         g_assert_not_reached ();
1506                                         break;
1507                                 }
1508                                 break;
1509                         }
1510 #endif
1511
1512                         align_size = size;
1513                         nwords = 0;
1514                         align_size += (sizeof (gpointer) - 1);
1515                         align_size &= ~(sizeof (gpointer) - 1);
1516                         nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
1517                         ainfo->storage = RegTypeStructByVal;
1518                         ainfo->struct_size = size;
1519                         ainfo->align = align;
1520                         /* FIXME: align stack_size if needed */
1521                         if (eabi_supported) {
1522                                 if (align >= 8 && (gr & 1))
1523                                         gr ++;
1524                         }
1525                         if (gr > ARMREG_R3) {
1526                                 ainfo->size = 0;
1527                                 ainfo->vtsize = nwords;
1528                         } else {
1529                                 int rest = ARMREG_R3 - gr + 1;
1530                                 int n_in_regs = rest >= nwords? nwords: rest;
1531
1532                                 ainfo->size = n_in_regs;
1533                                 ainfo->vtsize = nwords - n_in_regs;
1534                                 ainfo->reg = gr;
1535                                 gr += n_in_regs;
1536                                 nwords -= n_in_regs;
1537                         }
1538                         if (sig->call_convention == MONO_CALL_VARARG)
1539                                 /* This matches the alignment in mono_ArgIterator_IntGetNextArg () */
1540                                 stack_size = ALIGN_TO (stack_size, align);
1541                         ainfo->offset = stack_size;
1542                         /*g_print ("offset for arg %d at %d\n", n, stack_size);*/
1543                         stack_size += nwords * sizeof (gpointer);
1544                         break;
1545                 }
1546                 case MONO_TYPE_U8:
1547                 case MONO_TYPE_I8:
1548                         ainfo->size = 8;
1549                         add_general (&gr, &stack_size, ainfo, FALSE);
1550                         break;
1551                 case MONO_TYPE_R4:
1552                         ainfo->size = 4;
1553
1554                         if (IS_HARD_FLOAT)
1555                                 add_float (&fpr, &stack_size, ainfo, FALSE, &float_spare);
1556                         else
1557                                 add_general (&gr, &stack_size, ainfo, TRUE);
1558                         break;
1559                 case MONO_TYPE_R8:
1560                         ainfo->size = 8;
1561
1562                         if (IS_HARD_FLOAT)
1563                                 add_float (&fpr, &stack_size, ainfo, TRUE, &float_spare);
1564                         else
1565                                 add_general (&gr, &stack_size, ainfo, FALSE);
1566                         break;
1567                 case MONO_TYPE_VAR:
1568                 case MONO_TYPE_MVAR:
1569                         /* gsharedvt arguments are passed by ref */
1570                         g_assert (mini_is_gsharedvt_type (t));
1571                         add_general (&gr, &stack_size, ainfo, TRUE);
1572                         switch (ainfo->storage) {
1573                         case RegTypeGeneral:
1574                                 ainfo->storage = RegTypeGSharedVtInReg;
1575                                 break;
1576                         case RegTypeBase:
1577                                 ainfo->storage = RegTypeGSharedVtOnStack;
1578                                 break;
1579                         default:
1580                                 g_assert_not_reached ();
1581                         }
1582                         break;
1583                 default:
1584                         g_error ("Can't handle 0x%x", sig->params [i]->type);
1585                 }
1586                 n ++;
1587         }
1588
1589         /* Handle the case where there are no implicit arguments */
1590         if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1591                 /* Prevent implicit arguments and sig_cookie from
1592                    being passed in registers */
1593                 gr = ARMREG_R3 + 1;
1594                 fpr = ARM_VFP_F16;
1595                 /* Emit the signature cookie just before the implicit arguments */
1596                 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
1597         }
1598
1599         /* align stack size to 8 */
1600         DEBUG (g_print ("      stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
1601         stack_size = (stack_size + 7) & ~7;
1602
1603         cinfo->stack_usage = stack_size;
1604         return cinfo;
1605 }
1606
1607
1608 gboolean
1609 mono_arch_tail_call_supported (MonoCompile *cfg, MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig)
1610 {
1611         MonoType *callee_ret;
1612         CallInfo *c1, *c2;
1613         gboolean res;
1614
1615         c1 = get_call_info (NULL, caller_sig);
1616         c2 = get_call_info (NULL, callee_sig);
1617
1618         /*
1619          * Tail calls with more callee stack usage than the caller cannot be supported, since
1620          * the extra stack space would be left on the stack after the tail call.
1621          */
1622         res = c1->stack_usage >= c2->stack_usage;
1623         callee_ret = mini_get_underlying_type (callee_sig->ret);
1624         if (callee_ret && MONO_TYPE_ISSTRUCT (callee_ret) && c2->ret.storage != RegTypeStructByVal)
1625                 /* An address on the callee's stack is passed as the first argument */
1626                 res = FALSE;
1627
1628         if (c2->stack_usage > 16 * 4)
1629                 res = FALSE;
1630
1631         g_free (c1);
1632         g_free (c2);
1633
1634         return res;
1635 }
1636
1637 #ifndef DISABLE_JIT
1638
1639 static gboolean
1640 debug_omit_fp (void)
1641 {
1642 #if 0
1643         return mono_debug_count ();
1644 #else
1645         return TRUE;
1646 #endif
1647 }
1648
1649 /**
1650  * mono_arch_compute_omit_fp:
1651  * Determine whether the frame pointer can be eliminated.
1652  */
1653 static void
1654 mono_arch_compute_omit_fp (MonoCompile *cfg)
1655 {
1656         MonoMethodSignature *sig;
1657         MonoMethodHeader *header;
1658         int i, locals_size;
1659         CallInfo *cinfo;
1660
1661         if (cfg->arch.omit_fp_computed)
1662                 return;
1663
1664         header = cfg->header;
1665
1666         sig = mono_method_signature (cfg->method);
1667
1668         if (!cfg->arch.cinfo)
1669                 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1670         cinfo = cfg->arch.cinfo;
1671
1672         /*
1673          * FIXME: Remove some of the restrictions.
1674          */
1675         cfg->arch.omit_fp = TRUE;
1676         cfg->arch.omit_fp_computed = TRUE;
1677
1678         if (cfg->disable_omit_fp)
1679                 cfg->arch.omit_fp = FALSE;
1680         if (!debug_omit_fp ())
1681                 cfg->arch.omit_fp = FALSE;
1682         /*
1683         if (cfg->method->save_lmf)
1684                 cfg->arch.omit_fp = FALSE;
1685         */
1686         if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1687                 cfg->arch.omit_fp = FALSE;
1688         if (header->num_clauses)
1689                 cfg->arch.omit_fp = FALSE;
1690         if (cfg->param_area)
1691                 cfg->arch.omit_fp = FALSE;
1692         if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1693                 cfg->arch.omit_fp = FALSE;
1694         if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1695                 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1696                 cfg->arch.omit_fp = FALSE;
1697         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1698                 ArgInfo *ainfo = &cinfo->args [i];
1699
1700                 if (ainfo->storage == RegTypeBase || ainfo->storage == RegTypeBaseGen || ainfo->storage == RegTypeStructByVal) {
1701                         /* 
1702                          * The stack offset can only be determined when the frame
1703                          * size is known.
1704                          */
1705                         cfg->arch.omit_fp = FALSE;
1706                 }
1707         }
1708
1709         locals_size = 0;
1710         for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1711                 MonoInst *ins = cfg->varinfo [i];
1712                 int ialign;
1713
1714                 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1715         }
1716 }
1717
1718 /*
1719  * Set var information according to the calling convention. arm version.
1720  * The locals var stuff should most likely be split in another method.
1721  */
1722 void
1723 mono_arch_allocate_vars (MonoCompile *cfg)
1724 {
1725         MonoMethodSignature *sig;
1726         MonoMethodHeader *header;
1727         MonoInst *ins;
1728         MonoType *sig_ret;
1729         int i, offset, size, align, curinst;
1730         CallInfo *cinfo;
1731         ArgInfo *ainfo;
1732         guint32 ualign;
1733
1734         sig = mono_method_signature (cfg->method);
1735
1736         if (!cfg->arch.cinfo)
1737                 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
1738         cinfo = cfg->arch.cinfo;
1739         sig_ret = mini_get_underlying_type (sig->ret);
1740
1741         mono_arch_compute_omit_fp (cfg);
1742
1743         if (cfg->arch.omit_fp)
1744                 cfg->frame_reg = ARMREG_SP;
1745         else
1746                 cfg->frame_reg = ARMREG_FP;
1747
1748         cfg->flags |= MONO_CFG_HAS_SPILLUP;
1749
1750         /* allow room for the vararg method args: void* and long/double */
1751         if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1752                 cfg->param_area = MAX (cfg->param_area, sizeof (gpointer)*8);
1753
1754         header = cfg->header;
1755
1756         /* See mono_arch_get_global_int_regs () */
1757         if (cfg->flags & MONO_CFG_HAS_CALLS)
1758                 cfg->uses_rgctx_reg = TRUE;
1759
1760         if (cfg->frame_reg != ARMREG_SP)
1761                 cfg->used_int_regs |= 1 << cfg->frame_reg;
1762
1763         if (cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg))
1764                 /* V5 is reserved for passing the vtable/rgctx/IMT method */
1765                 cfg->used_int_regs |= (1 << MONO_ARCH_IMT_REG);
1766
1767         offset = 0;
1768         curinst = 0;
1769         if (!MONO_TYPE_ISSTRUCT (sig_ret) && cinfo->ret.storage != RegTypeStructByAddr) {
1770                 if (sig_ret->type != MONO_TYPE_VOID) {
1771                         cfg->ret->opcode = OP_REGVAR;
1772                         cfg->ret->inst_c0 = ARMREG_R0;
1773                 }
1774         }
1775         /* local vars are at a positive offset from the stack pointer */
1776         /* 
1777          * also note that if the function uses alloca, we use FP
1778          * to point at the local variables.
1779          */
1780         offset = 0; /* linkage area */
1781         /* align the offset to 16 bytes: not sure this is needed here  */
1782         //offset += 8 - 1;
1783         //offset &= ~(8 - 1);
1784
1785         /* add parameter area size for called functions */
1786         offset += cfg->param_area;
1787         offset += 8 - 1;
1788         offset &= ~(8 - 1);
1789         if (cfg->flags & MONO_CFG_HAS_FPOUT)
1790                 offset += 8;
1791
1792         /* allow room to save the return value */
1793         if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1794                 offset += 8;
1795
1796         switch (cinfo->ret.storage) {
1797         case RegTypeStructByVal:
1798         case RegTypeHFA:
1799                 /* Allocate a local to hold the result, the epilog will copy it to the correct place */
1800                 offset = ALIGN_TO (offset, 8);
1801                 cfg->ret->opcode = OP_REGOFFSET;
1802                 cfg->ret->inst_basereg = cfg->frame_reg;
1803                 cfg->ret->inst_offset = offset;
1804                 if (cinfo->ret.storage == RegTypeStructByVal)
1805                         offset += cinfo->ret.nregs * sizeof (gpointer);
1806                 else
1807                         offset += 32;
1808                 break;
1809         case RegTypeStructByAddr:
1810                 ins = cfg->vret_addr;
1811                 offset += sizeof(gpointer) - 1;
1812                 offset &= ~(sizeof(gpointer) - 1);
1813                 ins->inst_offset = offset;
1814                 ins->opcode = OP_REGOFFSET;
1815                 ins->inst_basereg = cfg->frame_reg;
1816                 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1817                         g_print ("vret_addr =");
1818                         mono_print_ins (cfg->vret_addr);
1819                 }
1820                 offset += sizeof(gpointer);
1821                 break;
1822         default:
1823                 break;
1824         }
1825
1826         /* Allocate these first so they have a small offset, OP_SEQ_POINT depends on this */
1827         if (cfg->arch.seq_point_info_var) {
1828                 MonoInst *ins;
1829
1830                 ins = cfg->arch.seq_point_info_var;
1831
1832                 size = 4;
1833                 align = 4;
1834                 offset += align - 1;
1835                 offset &= ~(align - 1);
1836                 ins->opcode = OP_REGOFFSET;
1837                 ins->inst_basereg = cfg->frame_reg;
1838                 ins->inst_offset = offset;
1839                 offset += size;
1840         }
1841         if (cfg->arch.ss_trigger_page_var) {
1842                 MonoInst *ins;
1843
1844                 ins = cfg->arch.ss_trigger_page_var;
1845                 size = 4;
1846                 align = 4;
1847                 offset += align - 1;
1848                 offset &= ~(align - 1);
1849                 ins->opcode = OP_REGOFFSET;
1850                 ins->inst_basereg = cfg->frame_reg;
1851                 ins->inst_offset = offset;
1852                 offset += size;
1853         }
1854
1855         if (cfg->arch.seq_point_ss_method_var) {
1856                 MonoInst *ins;
1857
1858                 ins = cfg->arch.seq_point_ss_method_var;
1859                 size = 4;
1860                 align = 4;
1861                 offset += align - 1;
1862                 offset &= ~(align - 1);
1863                 ins->opcode = OP_REGOFFSET;
1864                 ins->inst_basereg = cfg->frame_reg;
1865                 ins->inst_offset = offset;
1866                 offset += size;
1867         }
1868         if (cfg->arch.seq_point_bp_method_var) {
1869                 MonoInst *ins;
1870
1871                 ins = cfg->arch.seq_point_bp_method_var;
1872                 size = 4;
1873                 align = 4;
1874                 offset += align - 1;
1875                 offset &= ~(align - 1);
1876                 ins->opcode = OP_REGOFFSET;
1877                 ins->inst_basereg = cfg->frame_reg;
1878                 ins->inst_offset = offset;
1879                 offset += size;
1880         }
1881
1882         if (cfg->has_atomic_exchange_i4 || cfg->has_atomic_cas_i4 || cfg->has_atomic_add_i4) {
1883                 /* Allocate a temporary used by the atomic ops */
1884                 size = 4;
1885                 align = 4;
1886
1887                 /* Allocate a local slot to hold the sig cookie address */
1888                 offset += align - 1;
1889                 offset &= ~(align - 1);
1890                 cfg->arch.atomic_tmp_offset = offset;
1891                 offset += size;
1892         } else {
1893                 cfg->arch.atomic_tmp_offset = -1;
1894         }
1895
1896         cfg->locals_min_stack_offset = offset;
1897
1898         curinst = cfg->locals_start;
1899         for (i = curinst; i < cfg->num_varinfo; ++i) {
1900                 MonoType *t;
1901
1902                 ins = cfg->varinfo [i];
1903                 if ((ins->flags & MONO_INST_IS_DEAD) || ins->opcode == OP_REGVAR || ins->opcode == OP_REGOFFSET)
1904                         continue;
1905
1906                 t = ins->inst_vtype;
1907                 if (cfg->gsharedvt && mini_is_gsharedvt_variable_type (t))
1908                         continue;
1909
1910                 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1911                 * pinvoke wrappers when they call functions returning structure */
1912                 if (ins->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (t) && t->type != MONO_TYPE_TYPEDBYREF) {
1913                         size = mono_class_native_size (mono_class_from_mono_type (t), &ualign);
1914                         align = ualign;
1915                 }
1916                 else
1917                         size = mono_type_size (t, &align);
1918
1919                 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1920                  * since it loads/stores misaligned words, which don't do the right thing.
1921                  */
1922                 if (align < 4 && size >= 4)
1923                         align = 4;
1924                 if (ALIGN_TO (offset, align) > ALIGN_TO (offset, 4))
1925                         mini_gc_set_slot_type_from_fp (cfg, ALIGN_TO (offset, 4), SLOT_NOREF);
1926                 offset += align - 1;
1927                 offset &= ~(align - 1);
1928                 ins->opcode = OP_REGOFFSET;
1929                 ins->inst_offset = offset;
1930                 ins->inst_basereg = cfg->frame_reg;
1931                 offset += size;
1932                 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
1933         }
1934
1935         cfg->locals_max_stack_offset = offset;
1936
1937         curinst = 0;
1938         if (sig->hasthis) {
1939                 ins = cfg->args [curinst];
1940                 if (ins->opcode != OP_REGVAR) {
1941                         ins->opcode = OP_REGOFFSET;
1942                         ins->inst_basereg = cfg->frame_reg;
1943                         offset += sizeof (gpointer) - 1;
1944                         offset &= ~(sizeof (gpointer) - 1);
1945                         ins->inst_offset = offset;
1946                         offset += sizeof (gpointer);
1947                 }
1948                 curinst++;
1949         }
1950
1951         if (sig->call_convention == MONO_CALL_VARARG) {
1952                 size = 4;
1953                 align = 4;
1954
1955                 /* Allocate a local slot to hold the sig cookie address */
1956                 offset += align - 1;
1957                 offset &= ~(align - 1);
1958                 cfg->sig_cookie = offset;
1959                 offset += size;
1960         }                       
1961
1962         for (i = 0; i < sig->param_count; ++i) {
1963                 ainfo = cinfo->args + i;
1964
1965                 ins = cfg->args [curinst];
1966
1967                 switch (ainfo->storage) {
1968                 case RegTypeHFA:
1969                         offset = ALIGN_TO (offset, 8);
1970                         ins->opcode = OP_REGOFFSET;
1971                         ins->inst_basereg = cfg->frame_reg;
1972                         /* These arguments are saved to the stack in the prolog */
1973                         ins->inst_offset = offset;
1974                         if (cfg->verbose_level >= 2)
1975                                 g_print ("arg %d allocated to %s+0x%0x.\n", i, mono_arch_regname (ins->inst_basereg), (int)ins->inst_offset);
1976                         // FIXME:
1977                         offset += 32;
1978                         break;
1979                 default:
1980                         break;
1981                 }
1982
1983                 if (ins->opcode != OP_REGVAR) {
1984                         ins->opcode = OP_REGOFFSET;
1985                         ins->inst_basereg = cfg->frame_reg;
1986                         size = mini_type_stack_size_full (sig->params [i], &ualign, sig->pinvoke);
1987                         align = ualign;
1988                         /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1989                          * since it loads/stores misaligned words, which don't do the right thing.
1990                          */
1991                         if (align < 4 && size >= 4)
1992                                 align = 4;
1993                         /* The code in the prolog () stores words when storing vtypes received in a register */
1994                         if (MONO_TYPE_ISSTRUCT (sig->params [i]))
1995                                 align = 4;
1996                         if (ALIGN_TO (offset, align) > ALIGN_TO (offset, 4))
1997                                 mini_gc_set_slot_type_from_fp (cfg, ALIGN_TO (offset, 4), SLOT_NOREF);
1998                         offset += align - 1;
1999                         offset &= ~(align - 1);
2000                         ins->inst_offset = offset;
2001                         offset += size;
2002                 }
2003                 curinst++;
2004         }
2005
2006         /* align the offset to 8 bytes */
2007         if (ALIGN_TO (offset, 8) > ALIGN_TO (offset, 4))
2008                 mini_gc_set_slot_type_from_fp (cfg, ALIGN_TO (offset, 4), SLOT_NOREF);
2009         offset += 8 - 1;
2010         offset &= ~(8 - 1);
2011
2012         /* change sign? */
2013         cfg->stack_offset = offset;
2014 }
2015
2016 void
2017 mono_arch_create_vars (MonoCompile *cfg)
2018 {
2019         MonoMethodSignature *sig;
2020         CallInfo *cinfo;
2021         int i;
2022
2023         sig = mono_method_signature (cfg->method);
2024
2025         if (!cfg->arch.cinfo)
2026                 cfg->arch.cinfo = get_call_info (cfg->mempool, sig);
2027         cinfo = cfg->arch.cinfo;
2028
2029         if (IS_HARD_FLOAT) {
2030                 for (i = 0; i < 2; i++) {
2031                         MonoInst *inst = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
2032                         inst->flags |= MONO_INST_VOLATILE;
2033
2034                         cfg->arch.vfp_scratch_slots [i] = (gpointer) inst;
2035                 }
2036         }
2037
2038         if (cinfo->ret.storage == RegTypeStructByVal)
2039                 cfg->ret_var_is_local = TRUE;
2040
2041         if (cinfo->ret.storage == RegTypeStructByAddr) {
2042                 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
2043                 if (G_UNLIKELY (cfg->verbose_level > 1)) {
2044                         g_print ("vret_addr = ");
2045                         mono_print_ins (cfg->vret_addr);
2046                 }
2047         }
2048
2049         if (cfg->gen_sdb_seq_points) {
2050                 if (cfg->compile_aot) {
2051                         MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2052                         ins->flags |= MONO_INST_VOLATILE;
2053                         cfg->arch.seq_point_info_var = ins;
2054
2055                         if (!cfg->soft_breakpoints) {
2056                                 /* Allocate a separate variable for this to save 1 load per seq point */
2057                                 ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2058                                 ins->flags |= MONO_INST_VOLATILE;
2059                                 cfg->arch.ss_trigger_page_var = ins;
2060                         }
2061                 }
2062                 if (cfg->soft_breakpoints) {
2063                         MonoInst *ins;
2064
2065                         ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2066                         ins->flags |= MONO_INST_VOLATILE;
2067                         cfg->arch.seq_point_ss_method_var = ins;
2068
2069                         ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2070                         ins->flags |= MONO_INST_VOLATILE;
2071                         cfg->arch.seq_point_bp_method_var = ins;
2072                 }
2073         }
2074 }
2075
2076 static void
2077 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
2078 {
2079         MonoMethodSignature *tmp_sig;
2080         int sig_reg;
2081
2082         if (call->tail_call)
2083                 NOT_IMPLEMENTED;
2084
2085         g_assert (cinfo->sig_cookie.storage == RegTypeBase);
2086                         
2087         /*
2088          * mono_ArgIterator_Setup assumes the signature cookie is 
2089          * passed first and all the arguments which were before it are
2090          * passed on the stack after the signature. So compensate by 
2091          * passing a different signature.
2092          */
2093         tmp_sig = mono_metadata_signature_dup (call->signature);
2094         tmp_sig->param_count -= call->signature->sentinelpos;
2095         tmp_sig->sentinelpos = 0;
2096         memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
2097
2098         sig_reg = mono_alloc_ireg (cfg);
2099         MONO_EMIT_NEW_SIGNATURECONST (cfg, sig_reg, tmp_sig);
2100
2101         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, cinfo->sig_cookie.offset, sig_reg);
2102 }
2103
2104 #ifdef ENABLE_LLVM
2105 LLVMCallInfo*
2106 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
2107 {
2108         int i, n;
2109         CallInfo *cinfo;
2110         ArgInfo *ainfo;
2111         LLVMCallInfo *linfo;
2112
2113         n = sig->param_count + sig->hasthis;
2114
2115         cinfo = get_call_info (cfg->mempool, sig);
2116
2117         linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
2118
2119         /*
2120          * LLVM always uses the native ABI while we use our own ABI, the
2121          * only difference is the handling of vtypes:
2122          * - we only pass/receive them in registers in some cases, and only 
2123          *   in 1 or 2 integer registers.
2124          */
2125         switch (cinfo->ret.storage) {
2126         case RegTypeGeneral:
2127         case RegTypeNone:
2128         case RegTypeFP:
2129         case RegTypeIRegPair:
2130                 break;
2131         case RegTypeStructByAddr:
2132                 /* Vtype returned using a hidden argument */
2133                 linfo->ret.storage = LLVMArgVtypeRetAddr;
2134                 linfo->vret_arg_index = cinfo->vret_arg_index;
2135                 break;
2136 #if TARGET_WATCHOS
2137         case RegTypeStructByVal:
2138                 /* LLVM models this by returning an int array */
2139                 linfo->ret.storage = LLVMArgAsIArgs;
2140                 linfo->ret.nslots = cinfo->ret.nregs;
2141                 break;
2142 #endif
2143         case RegTypeHFA:
2144                 linfo->ret.storage = LLVMArgFpStruct;
2145                 linfo->ret.nslots = cinfo->ret.nregs;
2146                 linfo->ret.esize = cinfo->ret.esize;
2147                 break;
2148         default:
2149                 cfg->exception_message = g_strdup_printf ("unknown ret conv (%d)", cinfo->ret.storage);
2150                 cfg->disable_llvm = TRUE;
2151                 return linfo;
2152         }
2153
2154         for (i = 0; i < n; ++i) {
2155                 LLVMArgInfo *lainfo = &linfo->args [i];
2156                 ainfo = cinfo->args + i;
2157
2158                 lainfo->storage = LLVMArgNone;
2159
2160                 switch (ainfo->storage) {
2161                 case RegTypeGeneral:
2162                 case RegTypeIRegPair:
2163                 case RegTypeBase:
2164                 case RegTypeBaseGen:
2165                 case RegTypeFP:
2166                         lainfo->storage = LLVMArgNormal;
2167                         break;
2168                 case RegTypeStructByVal:
2169                         lainfo->storage = LLVMArgAsIArgs;
2170                         if (eabi_supported && ainfo->align == 8) {
2171                                 /* LLVM models this by passing an int64 array */
2172                                 lainfo->nslots = ALIGN_TO (ainfo->struct_size, 8) / 8;
2173                                 lainfo->esize = 8;
2174                         } else {
2175                                 lainfo->nslots = ainfo->struct_size / sizeof (gpointer);
2176                                 lainfo->esize = 4;
2177                         }
2178
2179                         printf ("D: %d\n", ainfo->align);
2180                         break;
2181                 case RegTypeStructByAddr:
2182                 case RegTypeStructByAddrOnStack:
2183                         lainfo->storage = LLVMArgVtypeByRef;
2184                         break;
2185                 case RegTypeHFA: {
2186                         int j;
2187
2188                         lainfo->storage = LLVMArgAsFpArgs;
2189                         lainfo->nslots = ainfo->nregs;
2190                         lainfo->esize = ainfo->esize;
2191                         for (j = 0; j < ainfo->nregs; ++j)
2192                                 lainfo->pair_storage [j] = LLVMArgInFPReg;
2193                         break;
2194                 }
2195                 default:
2196                         cfg->exception_message = g_strdup_printf ("ainfo->storage (%d)", ainfo->storage);
2197                         cfg->disable_llvm = TRUE;
2198                         break;
2199                 }
2200         }
2201
2202         return linfo;
2203 }
2204 #endif
2205
2206 void
2207 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
2208 {
2209         MonoInst *in, *ins;
2210         MonoMethodSignature *sig;
2211         int i, n;
2212         CallInfo *cinfo;
2213
2214         sig = call->signature;
2215         n = sig->param_count + sig->hasthis;
2216         
2217         cinfo = get_call_info (cfg->mempool, sig);
2218
2219         switch (cinfo->ret.storage) {
2220         case RegTypeStructByVal:
2221         case RegTypeHFA:
2222                 if (cinfo->ret.storage == RegTypeStructByVal && cinfo->ret.nregs == 1) {
2223                         /* The JIT will transform this into a normal call */
2224                         call->vret_in_reg = TRUE;
2225                         break;
2226                 }
2227                 if (call->inst.opcode == OP_TAILCALL)
2228                         break;
2229                 /*
2230                  * The vtype is returned in registers, save the return area address in a local, and save the vtype into
2231                  * the location pointed to by it after call in emit_move_return_value ().
2232                  */
2233                 if (!cfg->arch.vret_addr_loc) {
2234                         cfg->arch.vret_addr_loc = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2235                         /* Prevent it from being register allocated or optimized away */
2236                         ((MonoInst*)cfg->arch.vret_addr_loc)->flags |= MONO_INST_VOLATILE;
2237                 }
2238
2239                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, ((MonoInst*)cfg->arch.vret_addr_loc)->dreg, call->vret_var->dreg);
2240                 break;
2241         case RegTypeStructByAddr: {
2242                 MonoInst *vtarg;
2243                 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2244                 vtarg->sreg1 = call->vret_var->dreg;
2245                 vtarg->dreg = mono_alloc_preg (cfg);
2246                 MONO_ADD_INS (cfg->cbb, vtarg);
2247
2248                 mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
2249                 break;
2250         }
2251         default:
2252                 break;
2253         }
2254
2255         for (i = 0; i < n; ++i) {
2256                 ArgInfo *ainfo = cinfo->args + i;
2257                 MonoType *t;
2258
2259                 if (i >= sig->hasthis)
2260                         t = sig->params [i - sig->hasthis];
2261                 else
2262                         t = &mono_defaults.int_class->byval_arg;
2263                 t = mini_get_underlying_type (t);
2264
2265                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
2266                         /* Emit the signature cookie just before the implicit arguments */
2267                         emit_sig_cookie (cfg, call, cinfo);
2268                 }
2269
2270                 in = call->args [i];
2271
2272                 switch (ainfo->storage) {
2273                 case RegTypeGeneral:
2274                 case RegTypeIRegPair:
2275                         if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
2276                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
2277                                 ins->dreg = mono_alloc_ireg (cfg);
2278                                 ins->sreg1 = MONO_LVREG_LS (in->dreg);
2279                                 MONO_ADD_INS (cfg->cbb, ins);
2280                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
2281
2282                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
2283                                 ins->dreg = mono_alloc_ireg (cfg);
2284                                 ins->sreg1 = MONO_LVREG_MS (in->dreg);
2285                                 MONO_ADD_INS (cfg->cbb, ins);
2286                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
2287                         } else if (!t->byref && ((t->type == MONO_TYPE_R8) || (t->type == MONO_TYPE_R4))) {
2288                                 if (ainfo->size == 4) {
2289                                         if (IS_SOFT_FLOAT) {
2290                                                 /* mono_emit_call_args () have already done the r8->r4 conversion */
2291                                                 /* The converted value is in an int vreg */
2292                                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
2293                                                 ins->dreg = mono_alloc_ireg (cfg);
2294                                                 ins->sreg1 = in->dreg;
2295                                                 MONO_ADD_INS (cfg->cbb, ins);
2296                                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
2297                                         } else {
2298                                                 int creg;
2299
2300                                                 cfg->param_area = MAX (cfg->param_area, 8);
2301                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
2302                                                 creg = mono_alloc_ireg (cfg);
2303                                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
2304                                                 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
2305                                         }
2306                                 } else {
2307                                         if (IS_SOFT_FLOAT) {
2308                                                 MONO_INST_NEW (cfg, ins, OP_FGETLOW32);
2309                                                 ins->dreg = mono_alloc_ireg (cfg);
2310                                                 ins->sreg1 = in->dreg;
2311                                                 MONO_ADD_INS (cfg->cbb, ins);
2312                                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
2313
2314                                                 MONO_INST_NEW (cfg, ins, OP_FGETHIGH32);
2315                                                 ins->dreg = mono_alloc_ireg (cfg);
2316                                                 ins->sreg1 = in->dreg;
2317                                                 MONO_ADD_INS (cfg->cbb, ins);
2318                                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
2319                                         } else {
2320                                                 int creg;
2321
2322                                                 cfg->param_area = MAX (cfg->param_area, 8);
2323                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
2324                                                 creg = mono_alloc_ireg (cfg);
2325                                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
2326                                                 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
2327                                                 creg = mono_alloc_ireg (cfg);
2328                                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8 + 4));
2329                                                 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg + 1, FALSE);
2330                                         }
2331                                 }
2332                                 cfg->flags |= MONO_CFG_HAS_FPOUT;
2333                         } else {
2334                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
2335                                 ins->dreg = mono_alloc_ireg (cfg);
2336                                 ins->sreg1 = in->dreg;
2337                                 MONO_ADD_INS (cfg->cbb, ins);
2338
2339                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
2340                         }
2341                         break;
2342                 case RegTypeStructByVal:
2343                 case RegTypeGSharedVtInReg:
2344                 case RegTypeGSharedVtOnStack:
2345                 case RegTypeHFA:
2346                 case RegTypeStructByAddr:
2347                 case RegTypeStructByAddrOnStack:
2348                         MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
2349                         ins->opcode = OP_OUTARG_VT;
2350                         ins->sreg1 = in->dreg;
2351                         ins->klass = in->klass;
2352                         ins->inst_p0 = call;
2353                         ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
2354                         memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
2355                         mono_call_inst_add_outarg_vt (cfg, call, ins);
2356                         MONO_ADD_INS (cfg->cbb, ins);
2357                         break;
2358                 case RegTypeBase:
2359                         if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
2360                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
2361                         } else if (!t->byref && ((t->type == MONO_TYPE_R4) || (t->type == MONO_TYPE_R8))) {
2362                                 if (t->type == MONO_TYPE_R8) {
2363                                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
2364                                 } else {
2365                                         if (IS_SOFT_FLOAT)
2366                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
2367                                         else
2368                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
2369                                 }
2370                         } else {
2371                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
2372                         }
2373                         break;
2374                 case RegTypeBaseGen:
2375                         if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
2376                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, (G_BYTE_ORDER == G_BIG_ENDIAN) ? MONO_LVREG_LS (in->dreg) : MONO_LVREG_MS (in->dreg));
2377                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
2378                                 ins->dreg = mono_alloc_ireg (cfg);
2379                                 ins->sreg1 = G_BYTE_ORDER == G_BIG_ENDIAN ? MONO_LVREG_MS (in->dreg) : MONO_LVREG_LS (in->dreg);
2380                                 MONO_ADD_INS (cfg->cbb, ins);
2381                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ARMREG_R3, FALSE);
2382                         } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
2383                                 int creg;
2384
2385                                 /* This should work for soft-float as well */
2386
2387                                 cfg->param_area = MAX (cfg->param_area, 8);
2388                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
2389                                 creg = mono_alloc_ireg (cfg);
2390                                 mono_call_inst_add_outarg_reg (cfg, call, creg, ARMREG_R3, FALSE);
2391                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
2392                                 creg = mono_alloc_ireg (cfg);
2393                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 4));
2394                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, creg);
2395                                 cfg->flags |= MONO_CFG_HAS_FPOUT;
2396                         } else {
2397                                 g_assert_not_reached ();
2398                         }
2399                         break;
2400                 case RegTypeFP: {
2401                         int fdreg = mono_alloc_freg (cfg);
2402
2403                         if (ainfo->size == 8) {
2404                                 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2405                                 ins->sreg1 = in->dreg;
2406                                 ins->dreg = fdreg;
2407                                 MONO_ADD_INS (cfg->cbb, ins);
2408
2409                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, TRUE);
2410                         } else {
2411                                 FloatArgData *fad;
2412
2413                                 /*
2414                                  * Mono's register allocator doesn't speak single-precision registers that
2415                                  * overlap double-precision registers (i.e. armhf). So we have to work around
2416                                  * the register allocator and load the value from memory manually.
2417                                  *
2418                                  * So we create a variable for the float argument and an instruction to store
2419                                  * the argument into the variable. We then store the list of these arguments
2420                                  * in call->float_args. This list is then used by emit_float_args later to
2421                                  * pass the arguments in the various call opcodes.
2422                                  *
2423                                  * This is not very nice, and we should really try to fix the allocator.
2424                                  */
2425
2426                                 MonoInst *float_arg = mono_compile_create_var (cfg, &mono_defaults.single_class->byval_arg, OP_LOCAL);
2427
2428                                 /* Make sure the instruction isn't seen as pointless and removed.
2429                                  */
2430                                 float_arg->flags |= MONO_INST_VOLATILE;
2431
2432                                 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, float_arg->dreg, in->dreg);
2433
2434                                 /* We use the dreg to look up the instruction later. The hreg is used to
2435                                  * emit the instruction that loads the value into the FP reg.
2436                                  */
2437                                 fad = mono_mempool_alloc0 (cfg->mempool, sizeof (FloatArgData));
2438                                 fad->vreg = float_arg->dreg;
2439                                 fad->hreg = ainfo->reg;
2440
2441                                 call->float_args = g_slist_append_mempool (cfg->mempool, call->float_args, fad);
2442                         }
2443
2444                         call->used_iregs |= 1 << ainfo->reg;
2445                         cfg->flags |= MONO_CFG_HAS_FPOUT;
2446                         break;
2447                 }
2448                 default:
2449                         g_assert_not_reached ();
2450                 }
2451         }
2452
2453         /* Handle the case where there are no implicit arguments */
2454         if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
2455                 emit_sig_cookie (cfg, call, cinfo);
2456
2457         call->call_info = cinfo;
2458         call->stack_usage = cinfo->stack_usage;
2459 }
2460
2461 static void
2462 add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, MonoInst *arg)
2463 {
2464         MonoInst *ins;
2465
2466         switch (storage) {
2467         case RegTypeFP:
2468                 MONO_INST_NEW (cfg, ins, OP_FMOVE);
2469                 ins->dreg = mono_alloc_freg (cfg);
2470                 ins->sreg1 = arg->dreg;
2471                 MONO_ADD_INS (cfg->cbb, ins);
2472                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, reg, TRUE);
2473                 break;
2474         default:
2475                 g_assert_not_reached ();
2476                 break;
2477         }
2478 }
2479
2480 void
2481 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
2482 {
2483         MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
2484         MonoInst *load;
2485         ArgInfo *ainfo = ins->inst_p1;
2486         int ovf_size = ainfo->vtsize;
2487         int doffset = ainfo->offset;
2488         int struct_size = ainfo->struct_size;
2489         int i, soffset, dreg, tmpreg;
2490
2491         switch (ainfo->storage) {
2492         case RegTypeGSharedVtInReg:
2493         case RegTypeStructByAddr:
2494                 /* Pass by addr */
2495                 mono_call_inst_add_outarg_reg (cfg, call, src->dreg, ainfo->reg, FALSE);
2496                 break;
2497         case RegTypeGSharedVtOnStack:
2498         case RegTypeStructByAddrOnStack:
2499                 /* Pass by addr on stack */
2500                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, src->dreg);
2501                 break;
2502         case RegTypeHFA:
2503                 for (i = 0; i < ainfo->nregs; ++i) {
2504                         if (ainfo->esize == 4)
2505                                 MONO_INST_NEW (cfg, load, OP_LOADR4_MEMBASE);
2506                         else
2507                                 MONO_INST_NEW (cfg, load, OP_LOADR8_MEMBASE);
2508                         load->dreg = mono_alloc_freg (cfg);
2509                         load->inst_basereg = src->dreg;
2510                         load->inst_offset = i * ainfo->esize;
2511                         MONO_ADD_INS (cfg->cbb, load);
2512
2513                         if (ainfo->esize == 4) {
2514                                 FloatArgData *fad;
2515
2516                                 /* See RegTypeFP in mono_arch_emit_call () */
2517                                 MonoInst *float_arg = mono_compile_create_var (cfg, &mono_defaults.single_class->byval_arg, OP_LOCAL);
2518                                 float_arg->flags |= MONO_INST_VOLATILE;
2519                                 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, float_arg->dreg, load->dreg);
2520
2521                                 fad = mono_mempool_alloc0 (cfg->mempool, sizeof (FloatArgData));
2522                                 fad->vreg = float_arg->dreg;
2523                                 fad->hreg = ainfo->reg + i;
2524
2525                                 call->float_args = g_slist_append_mempool (cfg->mempool, call->float_args, fad);
2526                         } else {
2527                                 add_outarg_reg (cfg, call, RegTypeFP, ainfo->reg + (i * 2), load);
2528                         }
2529                 }
2530                 break;
2531         default:
2532                 soffset = 0;
2533                 for (i = 0; i < ainfo->size; ++i) {
2534                         dreg = mono_alloc_ireg (cfg);
2535                         switch (struct_size) {
2536                         case 1:
2537                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
2538                                 break;
2539                         case 2:
2540                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, soffset);
2541                                 break;
2542                         case 3:
2543                                 tmpreg = mono_alloc_ireg (cfg);
2544                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
2545                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 1);
2546                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 8);
2547                                 MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
2548                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 2);
2549                                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 16);
2550                                 MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
2551                                 break;
2552                         default:
2553                                 MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, soffset);
2554                                 break;
2555                         }
2556                         mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg + i, FALSE);
2557                         soffset += sizeof (gpointer);
2558                         struct_size -= sizeof (gpointer);
2559                 }
2560                 //g_print ("vt size: %d at R%d + %d\n", doffset, vt->inst_basereg, vt->inst_offset);
2561                 if (ovf_size != 0)
2562                         mini_emit_memcpy (cfg, ARMREG_SP, doffset, src->dreg, soffset, MIN (ovf_size * sizeof (gpointer), struct_size), struct_size < 4 ? 1 : 4);
2563                 break;
2564         }
2565 }
2566
2567 void
2568 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2569 {
2570         MonoType *ret = mini_get_underlying_type (mono_method_signature (method)->ret);
2571
2572         if (!ret->byref) {
2573                 if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
2574                         MonoInst *ins;
2575
2576                         if (COMPILE_LLVM (cfg)) {
2577                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2578                         } else {
2579                                 MONO_INST_NEW (cfg, ins, OP_SETLRET);
2580                                 ins->sreg1 = MONO_LVREG_LS (val->dreg);
2581                                 ins->sreg2 = MONO_LVREG_MS (val->dreg);
2582                                 MONO_ADD_INS (cfg->cbb, ins);
2583                         }
2584                         return;
2585                 }
2586                 switch (arm_fpu) {
2587                 case MONO_ARM_FPU_NONE:
2588                         if (ret->type == MONO_TYPE_R8) {
2589                                 MonoInst *ins;
2590
2591                                 MONO_INST_NEW (cfg, ins, OP_SETFRET);
2592                                 ins->dreg = cfg->ret->dreg;
2593                                 ins->sreg1 = val->dreg;
2594                                 MONO_ADD_INS (cfg->cbb, ins);
2595                                 return;
2596                         }
2597                         if (ret->type == MONO_TYPE_R4) {
2598                                 /* Already converted to an int in method_to_ir () */
2599                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2600                                 return;
2601                         }
2602                         break;
2603                 case MONO_ARM_FPU_VFP:
2604                 case MONO_ARM_FPU_VFP_HARD:
2605                         if (ret->type == MONO_TYPE_R8 || ret->type == MONO_TYPE_R4) {
2606                                 MonoInst *ins;
2607
2608                                 MONO_INST_NEW (cfg, ins, OP_SETFRET);
2609                                 ins->dreg = cfg->ret->dreg;
2610                                 ins->sreg1 = val->dreg;
2611                                 MONO_ADD_INS (cfg->cbb, ins);
2612                                 return;
2613                         }
2614                         break;
2615                 default:
2616                         g_assert_not_reached ();
2617                 }
2618         }
2619
2620         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2621 }
2622
2623 #endif /* #ifndef DISABLE_JIT */
2624
2625 gboolean 
2626 mono_arch_is_inst_imm (gint64 imm)
2627 {
2628         return TRUE;
2629 }
2630
2631 typedef struct {
2632         MonoMethodSignature *sig;
2633         CallInfo *cinfo;
2634         MonoType *rtype;
2635         MonoType **param_types;
2636 } ArchDynCallInfo;
2637
2638 static gboolean
2639 dyn_call_supported (CallInfo *cinfo, MonoMethodSignature *sig)
2640 {
2641         int i;
2642
2643         if (sig->hasthis + sig->param_count > PARAM_REGS + DYN_CALL_STACK_ARGS)
2644                 return FALSE;
2645
2646         switch (cinfo->ret.storage) {
2647         case RegTypeNone:
2648         case RegTypeGeneral:
2649         case RegTypeIRegPair:
2650         case RegTypeStructByAddr:
2651                 break;
2652         case RegTypeFP:
2653                 if (IS_VFP)
2654                         break;
2655                 else
2656                         return FALSE;
2657         default:
2658                 return FALSE;
2659         }
2660
2661         for (i = 0; i < cinfo->nargs; ++i) {
2662                 ArgInfo *ainfo = &cinfo->args [i];
2663                 int last_slot;
2664
2665                 switch (ainfo->storage) {
2666                 case RegTypeGeneral:
2667                 case RegTypeIRegPair:
2668                 case RegTypeBaseGen:
2669                 case RegTypeFP:
2670                         break;
2671                 case RegTypeBase:
2672                         if (ainfo->offset >= (DYN_CALL_STACK_ARGS * sizeof (gpointer)))
2673                                 return FALSE;
2674                         break;
2675                 case RegTypeStructByVal:
2676                         if (ainfo->size == 0)
2677                                 last_slot = PARAM_REGS + (ainfo->offset / 4) + ainfo->vtsize;
2678                         else
2679                                 last_slot = ainfo->reg + ainfo->size + ainfo->vtsize;
2680                         if (last_slot >= PARAM_REGS + DYN_CALL_STACK_ARGS)
2681                                 return FALSE;
2682                         break;
2683                 default:
2684                         return FALSE;
2685                 }
2686         }
2687
2688         // FIXME: Can't use cinfo only as it doesn't contain info about I8/float */
2689         for (i = 0; i < sig->param_count; ++i) {
2690                 MonoType *t = sig->params [i];
2691
2692                 if (t->byref)
2693                         continue;
2694
2695                 t = mini_get_underlying_type (t);
2696
2697                 switch (t->type) {
2698                 case MONO_TYPE_R4:
2699                 case MONO_TYPE_R8:
2700                         if (IS_SOFT_FLOAT)
2701                                 return FALSE;
2702                         else
2703                                 break;
2704                         /*
2705                 case MONO_TYPE_I8:
2706                 case MONO_TYPE_U8:
2707                         return FALSE;
2708                         */
2709                 default:
2710                         break;
2711                 }
2712         }
2713
2714         return TRUE;
2715 }
2716
2717 MonoDynCallInfo*
2718 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2719 {
2720         ArchDynCallInfo *info;
2721         CallInfo *cinfo;
2722         int i;
2723
2724         cinfo = get_call_info (NULL, sig);
2725
2726         if (!dyn_call_supported (cinfo, sig)) {
2727                 g_free (cinfo);
2728                 return NULL;
2729         }
2730
2731         info = g_new0 (ArchDynCallInfo, 1);
2732         // FIXME: Preprocess the info to speed up start_dyn_call ()
2733         info->sig = sig;
2734         info->cinfo = cinfo;
2735         info->rtype = mini_get_underlying_type (sig->ret);
2736         info->param_types = g_new0 (MonoType*, sig->param_count);
2737         for (i = 0; i < sig->param_count; ++i)
2738                 info->param_types [i] = mini_get_underlying_type (sig->params [i]);
2739         
2740         return (MonoDynCallInfo*)info;
2741 }
2742
2743 void
2744 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2745 {
2746         ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2747
2748         g_free (ainfo->cinfo);
2749         g_free (ainfo);
2750 }
2751
2752 void
2753 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2754 {
2755         ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2756         DynCallArgs *p = (DynCallArgs*)buf;
2757         int arg_index, greg, i, j, pindex;
2758         MonoMethodSignature *sig = dinfo->sig;
2759
2760         g_assert (buf_len >= sizeof (DynCallArgs));
2761
2762         p->res = 0;
2763         p->ret = ret;
2764         p->has_fpregs = 0;
2765
2766         arg_index = 0;
2767         greg = 0;
2768         pindex = 0;
2769
2770         if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2771                 p->regs [greg ++] = (mgreg_t)*(args [arg_index ++]);
2772                 if (!sig->hasthis)
2773                         pindex = 1;
2774         }
2775
2776         if (dinfo->cinfo->ret.storage == RegTypeStructByAddr)
2777                 p->regs [greg ++] = (mgreg_t)ret;
2778
2779         for (i = pindex; i < sig->param_count; i++) {
2780                 MonoType *t = dinfo->param_types [i];
2781                 gpointer *arg = args [arg_index ++];
2782                 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2783                 int slot = -1;
2784
2785                 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair || ainfo->storage == RegTypeStructByVal) {
2786                         slot = ainfo->reg;
2787                 } else if (ainfo->storage == RegTypeFP) {
2788                 } else if (ainfo->storage == RegTypeBase) {
2789                         slot = PARAM_REGS + (ainfo->offset / 4);
2790                 } else if (ainfo->storage == RegTypeBaseGen) {
2791                         /* slot + 1 is the first stack slot, so the code below will work */
2792                         slot = 3;
2793                 } else {
2794                         g_assert_not_reached ();
2795                 }
2796
2797                 if (t->byref) {
2798                         p->regs [slot] = (mgreg_t)*arg;
2799                         continue;
2800                 }
2801
2802                 switch (t->type) {
2803                 case MONO_TYPE_STRING:
2804                 case MONO_TYPE_CLASS:  
2805                 case MONO_TYPE_ARRAY:
2806                 case MONO_TYPE_SZARRAY:
2807                 case MONO_TYPE_OBJECT:
2808                 case MONO_TYPE_PTR:
2809                 case MONO_TYPE_I:
2810                 case MONO_TYPE_U:
2811                         p->regs [slot] = (mgreg_t)*arg;
2812                         break;
2813                 case MONO_TYPE_U1:
2814                         p->regs [slot] = *(guint8*)arg;
2815                         break;
2816                 case MONO_TYPE_I1:
2817                         p->regs [slot] = *(gint8*)arg;
2818                         break;
2819                 case MONO_TYPE_I2:
2820                         p->regs [slot] = *(gint16*)arg;
2821                         break;
2822                 case MONO_TYPE_U2:
2823                         p->regs [slot] = *(guint16*)arg;
2824                         break;
2825                 case MONO_TYPE_I4:
2826                         p->regs [slot] = *(gint32*)arg;
2827                         break;
2828                 case MONO_TYPE_U4:
2829                         p->regs [slot] = *(guint32*)arg;
2830                         break;
2831                 case MONO_TYPE_I8:
2832                 case MONO_TYPE_U8:
2833                         p->regs [slot ++] = (mgreg_t)arg [0];
2834                         p->regs [slot] = (mgreg_t)arg [1];
2835                         break;
2836                 case MONO_TYPE_R4:
2837                         if (ainfo->storage == RegTypeFP) {
2838                                 float f = *(float*)arg;
2839                                 p->fpregs [ainfo->reg / 2] = *(double*)&f;
2840                                 p->has_fpregs = 1;
2841                         } else {
2842                                 p->regs [slot] = *(mgreg_t*)arg;
2843                         }
2844                         break;
2845                 case MONO_TYPE_R8:
2846                         if (ainfo->storage == RegTypeFP) {
2847                                 p->fpregs [ainfo->reg / 2] = *(double*)arg;
2848                                 p->has_fpregs = 1;
2849                         } else {
2850                                 p->regs [slot ++] = (mgreg_t)arg [0];
2851                                 p->regs [slot] = (mgreg_t)arg [1];
2852                         }
2853                         break;
2854                 case MONO_TYPE_GENERICINST:
2855                         if (MONO_TYPE_IS_REFERENCE (t)) {
2856                                 p->regs [slot] = (mgreg_t)*arg;
2857                                 break;
2858                         } else {
2859                                 if (t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (t))) {
2860                                         MonoClass *klass = mono_class_from_mono_type (t);
2861                                         guint8 *nullable_buf;
2862                                         int size;
2863
2864                                         size = mono_class_value_size (klass, NULL);
2865                                         nullable_buf = g_alloca (size);
2866                                         g_assert (nullable_buf);
2867
2868                                         /* The argument pointed to by arg is either a boxed vtype or null */
2869                                         mono_nullable_init (nullable_buf, (MonoObject*)arg, klass);
2870
2871                                         arg = (gpointer*)nullable_buf;
2872                                         /* Fall though */
2873                                 } else {
2874                                         /* Fall though */
2875                                 }
2876                         }
2877                 case MONO_TYPE_VALUETYPE:
2878                         g_assert (ainfo->storage == RegTypeStructByVal);
2879
2880                         if (ainfo->size == 0)
2881                                 slot = PARAM_REGS + (ainfo->offset / 4);
2882                         else
2883                                 slot = ainfo->reg;
2884
2885                         for (j = 0; j < ainfo->size + ainfo->vtsize; ++j)
2886                                 p->regs [slot ++] = ((mgreg_t*)arg) [j];
2887                         break;
2888                 default:
2889                         g_assert_not_reached ();
2890                 }
2891         }
2892 }
2893
2894 void
2895 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2896 {
2897         ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2898         DynCallArgs *p = (DynCallArgs*)buf;
2899         MonoType *ptype = ainfo->rtype;
2900         guint8 *ret = p->ret;
2901         mgreg_t res = p->res;
2902         mgreg_t res2 = p->res2;
2903
2904         switch (ptype->type) {
2905         case MONO_TYPE_VOID:
2906                 *(gpointer*)ret = NULL;
2907                 break;
2908         case MONO_TYPE_STRING:
2909         case MONO_TYPE_CLASS:  
2910         case MONO_TYPE_ARRAY:
2911         case MONO_TYPE_SZARRAY:
2912         case MONO_TYPE_OBJECT:
2913         case MONO_TYPE_I:
2914         case MONO_TYPE_U:
2915         case MONO_TYPE_PTR:
2916                 *(gpointer*)ret = (gpointer)res;
2917                 break;
2918         case MONO_TYPE_I1:
2919                 *(gint8*)ret = res;
2920                 break;
2921         case MONO_TYPE_U1:
2922                 *(guint8*)ret = res;
2923                 break;
2924         case MONO_TYPE_I2:
2925                 *(gint16*)ret = res;
2926                 break;
2927         case MONO_TYPE_U2:
2928                 *(guint16*)ret = res;
2929                 break;
2930         case MONO_TYPE_I4:
2931                 *(gint32*)ret = res;
2932                 break;
2933         case MONO_TYPE_U4:
2934                 *(guint32*)ret = res;
2935                 break;
2936         case MONO_TYPE_I8:
2937         case MONO_TYPE_U8:
2938                 /* This handles endianness as well */
2939                 ((gint32*)ret) [0] = res;
2940                 ((gint32*)ret) [1] = res2;
2941                 break;
2942         case MONO_TYPE_GENERICINST:
2943                 if (MONO_TYPE_IS_REFERENCE (ptype)) {
2944                         *(gpointer*)ret = (gpointer)res;
2945                         break;
2946                 } else {
2947                         /* Fall though */
2948                 }
2949         case MONO_TYPE_VALUETYPE:
2950                 g_assert (ainfo->cinfo->ret.storage == RegTypeStructByAddr);
2951                 /* Nothing to do */
2952                 break;
2953         case MONO_TYPE_R4:
2954                 g_assert (IS_VFP);
2955                 if (IS_HARD_FLOAT)
2956                         *(float*)ret = *(float*)&p->fpregs [0];
2957                 else
2958                         *(float*)ret = *(float*)&res;
2959                 break;
2960         case MONO_TYPE_R8: {
2961                 mgreg_t regs [2];
2962
2963                 g_assert (IS_VFP);
2964                 if (IS_HARD_FLOAT) {
2965                         *(double*)ret = p->fpregs [0];
2966                 } else {
2967                         regs [0] = res;
2968                         regs [1] = res2;
2969
2970                         *(double*)ret = *(double*)&regs;
2971                 }
2972                 break;
2973         }
2974         default:
2975                 g_assert_not_reached ();
2976         }
2977 }
2978
2979 #ifndef DISABLE_JIT
2980
2981 /*
2982  * Allow tracing to work with this interface (with an optional argument)
2983  */
2984
2985 void*
2986 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
2987 {
2988         guchar *code = p;
2989
2990         code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2991         ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0); /* NULL ebp for now */
2992         code = mono_arm_emit_load_imm (code, ARMREG_R2, (guint32)func);
2993         code = emit_call_reg (code, ARMREG_R2);
2994         return code;
2995 }
2996
2997 enum {
2998         SAVE_NONE,
2999         SAVE_STRUCT,
3000         SAVE_ONE,
3001         SAVE_TWO,
3002         SAVE_ONE_FP,
3003         SAVE_TWO_FP
3004 };
3005
3006 void*
3007 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
3008 {
3009         guchar *code = p;
3010         int save_mode = SAVE_NONE;
3011         int offset;
3012         MonoMethod *method = cfg->method;
3013         MonoType *ret_type = mini_get_underlying_type (mono_method_signature (method)->ret);
3014         int rtype = ret_type->type;
3015         int save_offset = cfg->param_area;
3016         save_offset += 7;
3017         save_offset &= ~7;
3018         
3019         offset = code - cfg->native_code;
3020         /* we need about 16 instructions */
3021         if (offset > (cfg->code_size - 16 * 4)) {
3022                 cfg->code_size *= 2;
3023                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3024                 code = cfg->native_code + offset;
3025         }
3026         switch (rtype) {
3027         case MONO_TYPE_VOID:
3028                 /* special case string .ctor icall */
3029                 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
3030                         save_mode = SAVE_ONE;
3031                 else
3032                         save_mode = SAVE_NONE;
3033                 break;
3034         case MONO_TYPE_I8:
3035         case MONO_TYPE_U8:
3036                 save_mode = SAVE_TWO;
3037                 break;
3038         case MONO_TYPE_R4:
3039                 if (IS_HARD_FLOAT)
3040                         save_mode = SAVE_ONE_FP;
3041                 else
3042                         save_mode = SAVE_ONE;
3043                 break;
3044         case MONO_TYPE_R8:
3045                 if (IS_HARD_FLOAT)
3046                         save_mode = SAVE_TWO_FP;
3047                 else
3048                         save_mode = SAVE_TWO;
3049                 break;
3050         case MONO_TYPE_GENERICINST:
3051                 if (!mono_type_generic_inst_is_valuetype (ret_type)) {
3052                         save_mode = SAVE_ONE;
3053                         break;
3054                 }
3055                 /* Fall through */
3056         case MONO_TYPE_VALUETYPE:
3057                 save_mode = SAVE_STRUCT;
3058                 break;
3059         default:
3060                 save_mode = SAVE_ONE;
3061                 break;
3062         }
3063
3064         switch (save_mode) {
3065         case SAVE_TWO:
3066                 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
3067                 ARM_STR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
3068                 if (enable_arguments) {
3069                         ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R1);
3070                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
3071                 }
3072                 break;
3073         case SAVE_ONE:
3074                 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
3075                 if (enable_arguments) {
3076                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
3077                 }
3078                 break;
3079         case SAVE_ONE_FP:
3080                 ARM_FSTS (code, ARM_VFP_F0, cfg->frame_reg, save_offset);
3081                 if (enable_arguments) {
3082                         ARM_FMRS (code, ARMREG_R1, ARM_VFP_F0);
3083                 }
3084                 break;
3085         case SAVE_TWO_FP:
3086                 ARM_FSTD (code, ARM_VFP_D0, cfg->frame_reg, save_offset);
3087                 if (enable_arguments) {
3088                         ARM_FMDRR (code, ARMREG_R1, ARMREG_R2, ARM_VFP_D0);
3089                 }
3090                 break;
3091         case SAVE_STRUCT:
3092                 if (enable_arguments) {
3093                         /* FIXME: get the actual address  */
3094                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
3095                 }
3096                 break;
3097         case SAVE_NONE:
3098         default:
3099                 break;
3100         }
3101
3102         code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
3103         code = mono_arm_emit_load_imm (code, ARMREG_IP, (guint32)func);
3104         code = emit_call_reg (code, ARMREG_IP);
3105
3106         switch (save_mode) {
3107         case SAVE_TWO:
3108                 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
3109                 ARM_LDR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
3110                 break;
3111         case SAVE_ONE:
3112                 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
3113                 break;
3114         case SAVE_ONE_FP:
3115                 ARM_FLDS (code, ARM_VFP_F0, cfg->frame_reg, save_offset);
3116                 break;
3117         case SAVE_TWO_FP:
3118                 ARM_FLDD (code, ARM_VFP_D0, cfg->frame_reg, save_offset);
3119                 break;
3120         case SAVE_NONE:
3121         default:
3122                 break;
3123         }
3124
3125         return code;
3126 }
3127
3128 /*
3129  * The immediate field for cond branches is big enough for all reasonable methods
3130  */
3131 #define EMIT_COND_BRANCH_FLAGS(ins,condcode) \
3132 if (0 && ins->inst_true_bb->native_offset) { \
3133         ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffffff); \
3134 } else { \
3135         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
3136         ARM_B_COND (code, (condcode), 0); \
3137 }
3138
3139 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_cc_table [(cond)])
3140
3141 /* emit an exception if condition is fail
3142  *
3143  * We assign the extra code used to throw the implicit exceptions
3144  * to cfg->bb_exit as far as the big branch handling is concerned
3145  */
3146 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(condcode,exc_name)            \
3147         do {                                                        \
3148                 mono_add_patch_info (cfg, code - cfg->native_code,   \
3149                                     MONO_PATCH_INFO_EXC, exc_name); \
3150                 ARM_BL_COND (code, (condcode), 0); \
3151         } while (0); 
3152
3153 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_cc_table [(cond)], (exc_name))
3154
3155 void
3156 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
3157 {
3158 }
3159
3160 void
3161 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
3162 {
3163         MonoInst *ins, *n;
3164
3165         MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3166                 MonoInst *last_ins = mono_inst_prev (ins, FILTER_IL_SEQ_POINT);
3167
3168                 switch (ins->opcode) {
3169                 case OP_MUL_IMM: 
3170                 case OP_IMUL_IMM: 
3171                         /* Already done by an arch-independent pass */
3172                         break;
3173                 case OP_LOAD_MEMBASE:
3174                 case OP_LOADI4_MEMBASE:
3175                         /* 
3176                          * OP_STORE_MEMBASE_REG reg, offset(basereg) 
3177                          * OP_LOAD_MEMBASE offset(basereg), reg
3178                          */
3179                         if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG 
3180                                          || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
3181                             ins->inst_basereg == last_ins->inst_destbasereg &&
3182                             ins->inst_offset == last_ins->inst_offset) {
3183                                 if (ins->dreg == last_ins->sreg1) {
3184                                         MONO_DELETE_INS (bb, ins);
3185                                         continue;
3186                                 } else {
3187                                         //static int c = 0; g_print ("MATCHX %s %d\n", cfg->method->name,c++);
3188                                         ins->opcode = OP_MOVE;
3189                                         ins->sreg1 = last_ins->sreg1;
3190                                 }
3191
3192                         /* 
3193                          * Note: reg1 must be different from the basereg in the second load
3194                          * OP_LOAD_MEMBASE offset(basereg), reg1
3195                          * OP_LOAD_MEMBASE offset(basereg), reg2
3196                          * -->
3197                          * OP_LOAD_MEMBASE offset(basereg), reg1
3198                          * OP_MOVE reg1, reg2
3199                          */
3200                         } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
3201                                            || last_ins->opcode == OP_LOAD_MEMBASE) &&
3202                               ins->inst_basereg != last_ins->dreg &&
3203                               ins->inst_basereg == last_ins->inst_basereg &&
3204                               ins->inst_offset == last_ins->inst_offset) {
3205
3206                                 if (ins->dreg == last_ins->dreg) {
3207                                         MONO_DELETE_INS (bb, ins);
3208                                         continue;
3209                                 } else {
3210                                         ins->opcode = OP_MOVE;
3211                                         ins->sreg1 = last_ins->dreg;
3212                                 }
3213
3214                                 //g_assert_not_reached ();
3215
3216 #if 0
3217                         /* 
3218                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
3219                          * OP_LOAD_MEMBASE offset(basereg), reg
3220                          * -->
3221                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
3222                          * OP_ICONST reg, imm
3223                          */
3224                         } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
3225                                                 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
3226                                    ins->inst_basereg == last_ins->inst_destbasereg &&
3227                                    ins->inst_offset == last_ins->inst_offset) {
3228                                 //static int c = 0; g_print ("MATCHX %s %d\n", cfg->method->name,c++);
3229                                 ins->opcode = OP_ICONST;
3230                                 ins->inst_c0 = last_ins->inst_imm;
3231                                 g_assert_not_reached (); // check this rule
3232 #endif
3233                         }
3234                         break;
3235                 case OP_LOADU1_MEMBASE:
3236                 case OP_LOADI1_MEMBASE:
3237                         if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
3238                                         ins->inst_basereg == last_ins->inst_destbasereg &&
3239                                         ins->inst_offset == last_ins->inst_offset) {
3240                                 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_ICONV_TO_I1 : OP_ICONV_TO_U1;
3241                                 ins->sreg1 = last_ins->sreg1;                           
3242                         }
3243                         break;
3244                 case OP_LOADU2_MEMBASE:
3245                 case OP_LOADI2_MEMBASE:
3246                         if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
3247                                         ins->inst_basereg == last_ins->inst_destbasereg &&
3248                                         ins->inst_offset == last_ins->inst_offset) {
3249                                 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_ICONV_TO_I2 : OP_ICONV_TO_U2;
3250                                 ins->sreg1 = last_ins->sreg1;                           
3251                         }
3252                         break;
3253                 case OP_MOVE:
3254                         ins->opcode = OP_MOVE;
3255                         /* 
3256                          * OP_MOVE reg, reg 
3257                          */
3258                         if (ins->dreg == ins->sreg1) {
3259                                 MONO_DELETE_INS (bb, ins);
3260                                 continue;
3261                         }
3262                         /* 
3263                          * OP_MOVE sreg, dreg 
3264                          * OP_MOVE dreg, sreg
3265                          */
3266                         if (last_ins && last_ins->opcode == OP_MOVE &&
3267                             ins->sreg1 == last_ins->dreg &&
3268                             ins->dreg == last_ins->sreg1) {
3269                                 MONO_DELETE_INS (bb, ins);
3270                                 continue;
3271                         }
3272                         break;
3273                 }
3274         }
3275 }
3276
3277 /* 
3278  * the branch_cc_table should maintain the order of these
3279  * opcodes.
3280 case CEE_BEQ:
3281 case CEE_BGE:
3282 case CEE_BGT:
3283 case CEE_BLE:
3284 case CEE_BLT:
3285 case CEE_BNE_UN:
3286 case CEE_BGE_UN:
3287 case CEE_BGT_UN:
3288 case CEE_BLE_UN:
3289 case CEE_BLT_UN:
3290  */
3291 static const guchar 
3292 branch_cc_table [] = {
3293         ARMCOND_EQ, 
3294         ARMCOND_GE, 
3295         ARMCOND_GT, 
3296         ARMCOND_LE,
3297         ARMCOND_LT, 
3298         
3299         ARMCOND_NE, 
3300         ARMCOND_HS, 
3301         ARMCOND_HI, 
3302         ARMCOND_LS,
3303         ARMCOND_LO
3304 };
3305
3306 #define ADD_NEW_INS(cfg,dest,op) do {       \
3307                 MONO_INST_NEW ((cfg), (dest), (op)); \
3308         mono_bblock_insert_before_ins (bb, ins, (dest)); \
3309         } while (0)
3310
3311 static int
3312 map_to_reg_reg_op (int op)
3313 {
3314         switch (op) {
3315         case OP_ADD_IMM:
3316                 return OP_IADD;
3317         case OP_SUB_IMM:
3318                 return OP_ISUB;
3319         case OP_AND_IMM:
3320                 return OP_IAND;
3321         case OP_COMPARE_IMM:
3322                 return OP_COMPARE;
3323         case OP_ICOMPARE_IMM:
3324                 return OP_ICOMPARE;
3325         case OP_ADDCC_IMM:
3326                 return OP_ADDCC;
3327         case OP_ADC_IMM:
3328                 return OP_ADC;
3329         case OP_SUBCC_IMM:
3330                 return OP_SUBCC;
3331         case OP_SBB_IMM:
3332                 return OP_SBB;
3333         case OP_OR_IMM:
3334                 return OP_IOR;
3335         case OP_XOR_IMM:
3336                 return OP_IXOR;
3337         case OP_LOAD_MEMBASE:
3338                 return OP_LOAD_MEMINDEX;
3339         case OP_LOADI4_MEMBASE:
3340                 return OP_LOADI4_MEMINDEX;
3341         case OP_LOADU4_MEMBASE:
3342                 return OP_LOADU4_MEMINDEX;
3343         case OP_LOADU1_MEMBASE:
3344                 return OP_LOADU1_MEMINDEX;
3345         case OP_LOADI2_MEMBASE:
3346                 return OP_LOADI2_MEMINDEX;
3347         case OP_LOADU2_MEMBASE:
3348                 return OP_LOADU2_MEMINDEX;
3349         case OP_LOADI1_MEMBASE:
3350                 return OP_LOADI1_MEMINDEX;
3351         case OP_STOREI1_MEMBASE_REG:
3352                 return OP_STOREI1_MEMINDEX;
3353         case OP_STOREI2_MEMBASE_REG:
3354                 return OP_STOREI2_MEMINDEX;
3355         case OP_STOREI4_MEMBASE_REG:
3356                 return OP_STOREI4_MEMINDEX;
3357         case OP_STORE_MEMBASE_REG:
3358                 return OP_STORE_MEMINDEX;
3359         case OP_STORER4_MEMBASE_REG:
3360                 return OP_STORER4_MEMINDEX;
3361         case OP_STORER8_MEMBASE_REG:
3362                 return OP_STORER8_MEMINDEX;
3363         case OP_STORE_MEMBASE_IMM:
3364                 return OP_STORE_MEMBASE_REG;
3365         case OP_STOREI1_MEMBASE_IMM:
3366                 return OP_STOREI1_MEMBASE_REG;
3367         case OP_STOREI2_MEMBASE_IMM:
3368                 return OP_STOREI2_MEMBASE_REG;
3369         case OP_STOREI4_MEMBASE_IMM:
3370                 return OP_STOREI4_MEMBASE_REG;
3371         }
3372         g_assert_not_reached ();
3373 }
3374
3375 /*
3376  * Remove from the instruction list the instructions that can't be
3377  * represented with very simple instructions with no register
3378  * requirements.
3379  */
3380 void
3381 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3382 {
3383         MonoInst *ins, *temp, *last_ins = NULL;
3384         int rot_amount, imm8, low_imm;
3385
3386         MONO_BB_FOR_EACH_INS (bb, ins) {
3387 loop_start:
3388                 switch (ins->opcode) {
3389                 case OP_ADD_IMM:
3390                 case OP_SUB_IMM:
3391                 case OP_AND_IMM:
3392                 case OP_COMPARE_IMM:
3393                 case OP_ICOMPARE_IMM:
3394                 case OP_ADDCC_IMM:
3395                 case OP_ADC_IMM:
3396                 case OP_SUBCC_IMM:
3397                 case OP_SBB_IMM:
3398                 case OP_OR_IMM:
3399                 case OP_XOR_IMM:
3400                 case OP_IADD_IMM:
3401                 case OP_ISUB_IMM:
3402                 case OP_IAND_IMM:
3403                 case OP_IADC_IMM:
3404                 case OP_ISBB_IMM:
3405                 case OP_IOR_IMM:
3406                 case OP_IXOR_IMM:
3407                         if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
3408                                 int opcode2 = mono_op_imm_to_op (ins->opcode);
3409                                 ADD_NEW_INS (cfg, temp, OP_ICONST);
3410                                 temp->inst_c0 = ins->inst_imm;
3411                                 temp->dreg = mono_alloc_ireg (cfg);
3412                                 ins->sreg2 = temp->dreg;
3413                                 if (opcode2 == -1)
3414                                         g_error ("mono_op_imm_to_op failed for %s\n", mono_inst_name (ins->opcode));
3415                                 ins->opcode = opcode2;
3416                         }
3417                         if (ins->opcode == OP_SBB || ins->opcode == OP_ISBB || ins->opcode == OP_SUBCC)
3418                                 goto loop_start;
3419                         else
3420                                 break;
3421                 case OP_MUL_IMM:
3422                 case OP_IMUL_IMM:
3423                         if (ins->inst_imm == 1) {
3424                                 ins->opcode = OP_MOVE;
3425                                 break;
3426                         }
3427                         if (ins->inst_imm == 0) {
3428                                 ins->opcode = OP_ICONST;
3429                                 ins->inst_c0 = 0;
3430                                 break;
3431                         }
3432                         imm8 = mono_is_power_of_two (ins->inst_imm);
3433                         if (imm8 > 0) {
3434                                 ins->opcode = OP_SHL_IMM;
3435                                 ins->inst_imm = imm8;
3436                                 break;
3437                         }
3438                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3439                         temp->inst_c0 = ins->inst_imm;
3440                         temp->dreg = mono_alloc_ireg (cfg);
3441                         ins->sreg2 = temp->dreg;
3442                         ins->opcode = OP_IMUL;
3443                         break;
3444                 case OP_SBB:
3445                 case OP_ISBB:
3446                 case OP_SUBCC:
3447                 case OP_ISUBCC:
3448                         if (ins->next  && (ins->next->opcode == OP_COND_EXC_C || ins->next->opcode == OP_COND_EXC_IC))
3449                                 /* ARM sets the C flag to 1 if there was _no_ overflow */
3450                                 ins->next->opcode = OP_COND_EXC_NC;
3451                         break;
3452                 case OP_IDIV_IMM:
3453                 case OP_IDIV_UN_IMM:
3454                 case OP_IREM_IMM:
3455                 case OP_IREM_UN_IMM: {
3456                         int opcode2 = mono_op_imm_to_op (ins->opcode);
3457                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3458                         temp->inst_c0 = ins->inst_imm;
3459                         temp->dreg = mono_alloc_ireg (cfg);
3460                         ins->sreg2 = temp->dreg;
3461                         if (opcode2 == -1)
3462                                 g_error ("mono_op_imm_to_op failed for %s\n", mono_inst_name (ins->opcode));
3463                         ins->opcode = opcode2;
3464                         break;
3465                 }
3466                 case OP_LOCALLOC_IMM:
3467                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3468                         temp->inst_c0 = ins->inst_imm;
3469                         temp->dreg = mono_alloc_ireg (cfg);
3470                         ins->sreg1 = temp->dreg;
3471                         ins->opcode = OP_LOCALLOC;
3472                         break;
3473                 case OP_LOAD_MEMBASE:
3474                 case OP_LOADI4_MEMBASE:
3475                 case OP_LOADU4_MEMBASE:
3476                 case OP_LOADU1_MEMBASE:
3477                         /* we can do two things: load the immed in a register
3478                          * and use an indexed load, or see if the immed can be
3479                          * represented as an ad_imm + a load with a smaller offset
3480                          * that fits. We just do the first for now, optimize later.
3481                          */
3482                         if (arm_is_imm12 (ins->inst_offset))
3483                                 break;
3484                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3485                         temp->inst_c0 = ins->inst_offset;
3486                         temp->dreg = mono_alloc_ireg (cfg);
3487                         ins->sreg2 = temp->dreg;
3488                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3489                         break;
3490                 case OP_LOADI2_MEMBASE:
3491                 case OP_LOADU2_MEMBASE:
3492                 case OP_LOADI1_MEMBASE:
3493                         if (arm_is_imm8 (ins->inst_offset))
3494                                 break;
3495                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3496                         temp->inst_c0 = ins->inst_offset;
3497                         temp->dreg = mono_alloc_ireg (cfg);
3498                         ins->sreg2 = temp->dreg;
3499                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3500                         break;
3501                 case OP_LOADR4_MEMBASE:
3502                 case OP_LOADR8_MEMBASE:
3503                         if (arm_is_fpimm8 (ins->inst_offset))
3504                                 break;
3505                         low_imm = ins->inst_offset & 0x1ff;
3506                         if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~0x1ff, &rot_amount)) >= 0) {
3507                                 ADD_NEW_INS (cfg, temp, OP_ADD_IMM);
3508                                 temp->inst_imm = ins->inst_offset & ~0x1ff;
3509                                 temp->sreg1 = ins->inst_basereg;
3510                                 temp->dreg = mono_alloc_ireg (cfg);
3511                                 ins->inst_basereg = temp->dreg;
3512                                 ins->inst_offset = low_imm;
3513                         } else {
3514                                 MonoInst *add_ins;
3515
3516                                 ADD_NEW_INS (cfg, temp, OP_ICONST);
3517                                 temp->inst_c0 = ins->inst_offset;
3518                                 temp->dreg = mono_alloc_ireg (cfg);
3519
3520                                 ADD_NEW_INS (cfg, add_ins, OP_IADD);
3521                                 add_ins->sreg1 = ins->inst_basereg;
3522                                 add_ins->sreg2 = temp->dreg;
3523                                 add_ins->dreg = mono_alloc_ireg (cfg);
3524
3525                                 ins->inst_basereg = add_ins->dreg;
3526                                 ins->inst_offset = 0;
3527                         }
3528                         break;
3529                 case OP_STORE_MEMBASE_REG:
3530                 case OP_STOREI4_MEMBASE_REG:
3531                 case OP_STOREI1_MEMBASE_REG:
3532                         if (arm_is_imm12 (ins->inst_offset))
3533                                 break;
3534                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3535                         temp->inst_c0 = ins->inst_offset;
3536                         temp->dreg = mono_alloc_ireg (cfg);
3537                         ins->sreg2 = temp->dreg;
3538                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3539                         break;
3540                 case OP_STOREI2_MEMBASE_REG:
3541                         if (arm_is_imm8 (ins->inst_offset))
3542                                 break;
3543                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3544                         temp->inst_c0 = ins->inst_offset;
3545                         temp->dreg = mono_alloc_ireg (cfg);
3546                         ins->sreg2 = temp->dreg;
3547                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3548                         break;
3549                 case OP_STORER4_MEMBASE_REG:
3550                 case OP_STORER8_MEMBASE_REG:
3551                         if (arm_is_fpimm8 (ins->inst_offset))
3552                                 break;
3553                         low_imm = ins->inst_offset & 0x1ff;
3554                         if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~ 0x1ff, &rot_amount)) >= 0 && arm_is_fpimm8 (low_imm)) {
3555                                 ADD_NEW_INS (cfg, temp, OP_ADD_IMM);
3556                                 temp->inst_imm = ins->inst_offset & ~0x1ff;
3557                                 temp->sreg1 = ins->inst_destbasereg;
3558                                 temp->dreg = mono_alloc_ireg (cfg);
3559                                 ins->inst_destbasereg = temp->dreg;
3560                                 ins->inst_offset = low_imm;
3561                         } else {
3562                                 MonoInst *add_ins;
3563
3564                                 ADD_NEW_INS (cfg, temp, OP_ICONST);
3565                                 temp->inst_c0 = ins->inst_offset;
3566                                 temp->dreg = mono_alloc_ireg (cfg);
3567
3568                                 ADD_NEW_INS (cfg, add_ins, OP_IADD);
3569                                 add_ins->sreg1 = ins->inst_destbasereg;
3570                                 add_ins->sreg2 = temp->dreg;
3571                                 add_ins->dreg = mono_alloc_ireg (cfg);
3572
3573                                 ins->inst_destbasereg = add_ins->dreg;
3574                                 ins->inst_offset = 0;
3575                         }
3576                         break;
3577                 case OP_STORE_MEMBASE_IMM:
3578                 case OP_STOREI1_MEMBASE_IMM:
3579                 case OP_STOREI2_MEMBASE_IMM:
3580                 case OP_STOREI4_MEMBASE_IMM:
3581                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3582                         temp->inst_c0 = ins->inst_imm;
3583                         temp->dreg = mono_alloc_ireg (cfg);
3584                         ins->sreg1 = temp->dreg;
3585                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3586                         last_ins = temp;
3587                         goto loop_start; /* make it handle the possibly big ins->inst_offset */
3588                 case OP_FCOMPARE:
3589                 case OP_RCOMPARE: {
3590                         gboolean swap = FALSE;
3591                         int reg;
3592
3593                         if (!ins->next) {
3594                                 /* Optimized away */
3595                                 NULLIFY_INS (ins);
3596                                 break;
3597                         }
3598
3599                         /* Some fp compares require swapped operands */
3600                         switch (ins->next->opcode) {
3601                         case OP_FBGT:
3602                                 ins->next->opcode = OP_FBLT;
3603                                 swap = TRUE;
3604                                 break;
3605                         case OP_FBGT_UN:
3606                                 ins->next->opcode = OP_FBLT_UN;
3607                                 swap = TRUE;
3608                                 break;
3609                         case OP_FBLE:
3610                                 ins->next->opcode = OP_FBGE;
3611                                 swap = TRUE;
3612                                 break;
3613                         case OP_FBLE_UN:
3614                                 ins->next->opcode = OP_FBGE_UN;
3615                                 swap = TRUE;
3616                                 break;
3617                         default:
3618                                 break;
3619                         }
3620                         if (swap) {
3621                                 reg = ins->sreg1;
3622                                 ins->sreg1 = ins->sreg2;
3623                                 ins->sreg2 = reg;
3624                         }
3625                         break;
3626                 }
3627                 }
3628
3629                 last_ins = ins;
3630         }
3631         bb->last_ins = last_ins;
3632         bb->max_vreg = cfg->next_vreg;
3633 }
3634
3635 void
3636 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
3637 {
3638         MonoInst *ins;
3639
3640         if (long_ins->opcode == OP_LNEG) {
3641                 ins = long_ins;
3642                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSBS_IMM, MONO_LVREG_LS (ins->dreg), MONO_LVREG_LS (ins->sreg1), 0);
3643                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSC_IMM, MONO_LVREG_MS (ins->dreg), MONO_LVREG_MS (ins->sreg1), 0);
3644                 NULLIFY_INS (ins);
3645         }
3646 }
3647
3648 static guchar*
3649 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3650 {
3651         /* sreg is a float, dreg is an integer reg  */
3652         if (IS_VFP) {
3653                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
3654                 if (is_signed)
3655                         ARM_TOSIZD (code, vfp_scratch1, sreg);
3656                 else
3657                         ARM_TOUIZD (code, vfp_scratch1, sreg);
3658                 ARM_FMRS (code, dreg, vfp_scratch1);
3659                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
3660         }
3661         if (!is_signed) {
3662                 if (size == 1)
3663                         ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
3664                 else if (size == 2) {
3665                         ARM_SHL_IMM (code, dreg, dreg, 16);
3666                         ARM_SHR_IMM (code, dreg, dreg, 16);
3667                 }
3668         } else {
3669                 if (size == 1) {
3670                         ARM_SHL_IMM (code, dreg, dreg, 24);
3671                         ARM_SAR_IMM (code, dreg, dreg, 24);
3672                 } else if (size == 2) {
3673                         ARM_SHL_IMM (code, dreg, dreg, 16);
3674                         ARM_SAR_IMM (code, dreg, dreg, 16);
3675                 }
3676         }
3677         return code;
3678 }
3679
3680 static guchar*
3681 emit_r4_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3682 {
3683         /* sreg is a float, dreg is an integer reg  */
3684         g_assert (IS_VFP);
3685         code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
3686         if (is_signed)
3687                 ARM_TOSIZS (code, vfp_scratch1, sreg);
3688         else
3689                 ARM_TOUIZS (code, vfp_scratch1, sreg);
3690         ARM_FMRS (code, dreg, vfp_scratch1);
3691         code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
3692
3693         if (!is_signed) {
3694                 if (size == 1)
3695                         ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
3696                 else if (size == 2) {
3697                         ARM_SHL_IMM (code, dreg, dreg, 16);
3698                         ARM_SHR_IMM (code, dreg, dreg, 16);
3699                 }
3700         } else {
3701                 if (size == 1) {
3702                         ARM_SHL_IMM (code, dreg, dreg, 24);
3703                         ARM_SAR_IMM (code, dreg, dreg, 24);
3704                 } else if (size == 2) {
3705                         ARM_SHL_IMM (code, dreg, dreg, 16);
3706                         ARM_SAR_IMM (code, dreg, dreg, 16);
3707                 }
3708         }
3709         return code;
3710 }
3711
3712 #endif /* #ifndef DISABLE_JIT */
3713
3714 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
3715
3716 static void
3717 emit_thunk (guint8 *code, gconstpointer target)
3718 {
3719         guint8 *p = code;
3720
3721         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
3722         if (thumb_supported)
3723                 ARM_BX (code, ARMREG_IP);
3724         else
3725                 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3726         *(guint32*)code = (guint32)target;
3727         code += 4;
3728         mono_arch_flush_icache (p, code - p);
3729 }
3730
3731 static void
3732 handle_thunk (MonoCompile *cfg, MonoDomain *domain, guchar *code, const guchar *target)
3733 {
3734         MonoJitInfo *ji = NULL;
3735         MonoThunkJitInfo *info;
3736         guint8 *thunks, *p;
3737         int thunks_size;
3738         guint8 *orig_target;
3739         guint8 *target_thunk;
3740
3741         if (!domain)
3742                 domain = mono_domain_get ();
3743
3744         if (cfg) {
3745                 /*
3746                  * This can be called multiple times during JITting,
3747                  * save the current position in cfg->arch to avoid
3748                  * doing a O(n^2) search.
3749                  */
3750                 if (!cfg->arch.thunks) {
3751                         cfg->arch.thunks = cfg->thunks;
3752                         cfg->arch.thunks_size = cfg->thunk_area;
3753                 }
3754                 thunks = cfg->arch.thunks;
3755                 thunks_size = cfg->arch.thunks_size;
3756                 if (!thunks_size) {
3757                         g_print ("thunk failed %p->%p, thunk space=%d method %s", code, target, thunks_size, mono_method_full_name (cfg->method, TRUE));
3758                         g_assert_not_reached ();
3759                 }
3760
3761                 g_assert (*(guint32*)thunks == 0);
3762                 emit_thunk (thunks, target);
3763                 arm_patch (code, thunks);
3764
3765                 cfg->arch.thunks += THUNK_SIZE;
3766                 cfg->arch.thunks_size -= THUNK_SIZE;
3767         } else {
3768                 ji = mini_jit_info_table_find (domain, (char*)code, NULL);
3769                 g_assert (ji);
3770                 info = mono_jit_info_get_thunk_info (ji);
3771                 g_assert (info);
3772
3773                 thunks = (guint8*)ji->code_start + info->thunks_offset;
3774                 thunks_size = info->thunks_size;
3775
3776                 orig_target = mono_arch_get_call_target (code + 4);
3777
3778                 mono_mini_arch_lock ();
3779
3780                 target_thunk = NULL;
3781                 if (orig_target >= thunks && orig_target < thunks + thunks_size) {
3782                         /* The call already points to a thunk, because of trampolines etc. */
3783                         target_thunk = orig_target;
3784                 } else {
3785                         for (p = thunks; p < thunks + thunks_size; p += THUNK_SIZE) {
3786                                 if (((guint32*)p) [0] == 0) {
3787                                         /* Free entry */
3788                                         target_thunk = p;
3789                                         break;
3790                                 } else if (((guint32*)p) [2] == (guint32)target) {
3791                                         /* Thunk already points to target */
3792                                         target_thunk = p;
3793                                         break;
3794                                 }
3795                         }
3796                 }
3797
3798                 //g_print ("THUNK: %p %p %p\n", code, target, target_thunk);
3799
3800                 if (!target_thunk) {
3801                         mono_mini_arch_unlock ();
3802                         g_print ("thunk failed %p->%p, thunk space=%d method %s", code, target, thunks_size, cfg ? mono_method_full_name (cfg->method, TRUE) : mono_method_full_name (jinfo_get_method (ji), TRUE));
3803                         g_assert_not_reached ();
3804                 }
3805
3806                 emit_thunk (target_thunk, target);
3807                 arm_patch (code, target_thunk);
3808                 mono_arch_flush_icache (code, 4);
3809
3810                 mono_mini_arch_unlock ();
3811         }
3812 }
3813
3814 static void
3815 arm_patch_general (MonoCompile *cfg, MonoDomain *domain, guchar *code, const guchar *target)
3816 {
3817         guint32 *code32 = (void*)code;
3818         guint32 ins = *code32;
3819         guint32 prim = (ins >> 25) & 7;
3820         guint32 tval = GPOINTER_TO_UINT (target);
3821
3822         //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
3823         if (prim == 5) { /* 101b */
3824                 /* the diff starts 8 bytes from the branch opcode */
3825                 gint diff = target - code - 8;
3826                 gint tbits;
3827                 gint tmask = 0xffffffff;
3828                 if (tval & 1) { /* entering thumb mode */
3829                         diff = target - 1 - code - 8;
3830                         g_assert (thumb_supported);
3831                         tbits = 0xf << 28; /* bl->blx bit pattern */
3832                         g_assert ((ins & (1 << 24))); /* it must be a bl, not b instruction */
3833                         /* this low bit of the displacement is moved to bit 24 in the instruction encoding */
3834                         if (diff & 2) {
3835                                 tbits |= 1 << 24;
3836                         }
3837                         tmask = ~(1 << 24); /* clear the link bit */
3838                         /*g_print ("blx to thumb: target: %p, code: %p, diff: %d, mask: %x\n", target, code, diff, tmask);*/
3839                 } else {
3840                         tbits = 0;
3841                 }
3842                 if (diff >= 0) {
3843                         if (diff <= 33554431) {
3844                                 diff >>= 2;
3845                                 ins = (ins & 0xff000000) | diff;
3846                                 ins &= tmask;
3847                                 *code32 = ins | tbits;
3848                                 return;
3849                         }
3850                 } else {
3851                         /* diff between 0 and -33554432 */
3852                         if (diff >= -33554432) {
3853                                 diff >>= 2;
3854                                 ins = (ins & 0xff000000) | (diff & ~0xff000000);
3855                                 ins &= tmask;
3856                                 *code32 = ins | tbits;
3857                                 return;
3858                         }
3859                 }
3860                 
3861                 handle_thunk (cfg, domain, code, target);
3862                 return;
3863         }
3864
3865         /*
3866          * The alternative call sequences looks like this:
3867          *
3868          *      ldr ip, [pc] // loads the address constant
3869          *      b 1f         // jumps around the constant
3870          *      address constant embedded in the code
3871          *   1f:
3872          *      mov lr, pc
3873          *      mov pc, ip
3874          *
3875          * There are two cases for patching:
3876          * a) at the end of method emission: in this case code points to the start
3877          *    of the call sequence
3878          * b) during runtime patching of the call site: in this case code points
3879          *    to the mov pc, ip instruction
3880          *
3881          * We have to handle also the thunk jump code sequence:
3882          *
3883          *      ldr ip, [pc]
3884          *      mov pc, ip
3885          *      address constant // execution never reaches here
3886          */
3887         if ((ins & 0x0ffffff0) == 0x12fff10) {
3888                 /* Branch and exchange: the address is constructed in a reg 
3889                  * We can patch BX when the code sequence is the following:
3890                  *  ldr     ip, [pc, #0]    ; 0x8
3891                  *  b       0xc
3892                  *  .word code_ptr
3893                  *  mov     lr, pc
3894                  *  bx      ips
3895                  * */
3896                 guint32 ccode [4];
3897                 guint8 *emit = (guint8*)ccode;
3898                 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3899                 ARM_B (emit, 0);
3900                 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3901                 ARM_BX (emit, ARMREG_IP);
3902
3903                 /*patching from magic trampoline*/
3904                 if (ins == ccode [3]) {
3905                         g_assert (code32 [-4] == ccode [0]);
3906                         g_assert (code32 [-3] == ccode [1]);
3907                         g_assert (code32 [-1] == ccode [2]);
3908                         code32 [-2] = (guint32)target;
3909                         return;
3910                 }
3911                 /*patching from JIT*/
3912                 if (ins == ccode [0]) {
3913                         g_assert (code32 [1] == ccode [1]);
3914                         g_assert (code32 [3] == ccode [2]);
3915                         g_assert (code32 [4] == ccode [3]);
3916                         code32 [2] = (guint32)target;
3917                         return;
3918                 }
3919                 g_assert_not_reached ();
3920         } else if ((ins & 0x0ffffff0) == 0x12fff30) {
3921                 /*
3922                  * ldr ip, [pc, #0]
3923                  * b 0xc
3924                  * .word code_ptr
3925                  * blx ip
3926                  */
3927                 guint32 ccode [4];
3928                 guint8 *emit = (guint8*)ccode;
3929                 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3930                 ARM_B (emit, 0);
3931                 ARM_BLX_REG (emit, ARMREG_IP);
3932
3933                 g_assert (code32 [-3] == ccode [0]);
3934                 g_assert (code32 [-2] == ccode [1]);
3935                 g_assert (code32 [0] == ccode [2]);
3936
3937                 code32 [-1] = (guint32)target;
3938         } else {
3939                 guint32 ccode [4];
3940                 guint32 *tmp = ccode;
3941                 guint8 *emit = (guint8*)tmp;
3942                 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3943                 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3944                 ARM_MOV_REG_REG (emit, ARMREG_PC, ARMREG_IP);
3945                 ARM_BX (emit, ARMREG_IP);
3946                 if (ins == ccode [2]) {
3947                         g_assert_not_reached (); // should be -2 ...
3948                         code32 [-1] = (guint32)target;
3949                         return;
3950                 }
3951                 if (ins == ccode [0]) {
3952                         /* handles both thunk jump code and the far call sequence */
3953                         code32 [2] = (guint32)target;
3954                         return;
3955                 }
3956                 g_assert_not_reached ();
3957         }
3958 //      g_print ("patched with 0x%08x\n", ins);
3959 }
3960
3961 void
3962 arm_patch (guchar *code, const guchar *target)
3963 {
3964         arm_patch_general (NULL, NULL, code, target);
3965 }
3966
3967 /* 
3968  * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
3969  * (with the rotation amount in *rot_amount. rot_amount is already adjusted
3970  * to be used with the emit macros.
3971  * Return -1 otherwise.
3972  */
3973 int
3974 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
3975 {
3976         guint32 res, i;
3977         for (i = 0; i < 31; i+= 2) {
3978                 res = (val << (32 - i)) | (val >> i);
3979                 if (res & ~0xff)
3980                         continue;
3981                 *rot_amount = i? 32 - i: 0;
3982                 return res;
3983         }
3984         return -1;
3985 }
3986
3987 /*
3988  * Emits in code a sequence of instructions that load the value 'val'
3989  * into the dreg register. Uses at most 4 instructions.
3990  */
3991 guint8*
3992 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
3993 {
3994         int imm8, rot_amount;
3995 #if 0
3996         ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3997         /* skip the constant pool */
3998         ARM_B (code, 0);
3999         *(int*)code = val;
4000         code += 4;
4001         return code;
4002 #endif
4003         if (mini_get_debug_options()->single_imm_size && v7_supported) {
4004                 ARM_MOVW_REG_IMM (code, dreg, val & 0xffff);
4005                 ARM_MOVT_REG_IMM (code, dreg, (val >> 16) & 0xffff);
4006                 return code;
4007         }
4008
4009         if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
4010                 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
4011         } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
4012                 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
4013         } else {
4014                 if (v7_supported) {
4015                         ARM_MOVW_REG_IMM (code, dreg, val & 0xffff);
4016                         if (val >> 16)
4017                                 ARM_MOVT_REG_IMM (code, dreg, (val >> 16) & 0xffff);
4018                         return code;
4019                 }
4020                 if (val & 0xFF) {
4021                         ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
4022                         if (val & 0xFF00) {
4023                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
4024                         }
4025                         if (val & 0xFF0000) {
4026                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
4027                         }
4028                         if (val & 0xFF000000) {
4029                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
4030                         }
4031                 } else if (val & 0xFF00) {
4032                         ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
4033                         if (val & 0xFF0000) {
4034                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
4035                         }
4036                         if (val & 0xFF000000) {
4037                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
4038                         }
4039                 } else if (val & 0xFF0000) {
4040                         ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
4041                         if (val & 0xFF000000) {
4042                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
4043                         }
4044                 }
4045                 //g_assert_not_reached ();
4046         }
4047         return code;
4048 }
4049
4050 gboolean
4051 mono_arm_thumb_supported (void)
4052 {
4053         return thumb_supported;
4054 }
4055
4056 #ifndef DISABLE_JIT
4057
4058 static guint8*
4059 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
4060 {
4061         CallInfo *cinfo;
4062         MonoCallInst *call;
4063
4064         call = (MonoCallInst*)ins;
4065         cinfo = call->call_info;
4066
4067         switch (cinfo->ret.storage) {
4068         case RegTypeStructByVal:
4069         case RegTypeHFA: {
4070                 MonoInst *loc = cfg->arch.vret_addr_loc;
4071                 int i;
4072
4073                 if (cinfo->ret.storage == RegTypeStructByVal && cinfo->ret.nregs == 1) {
4074                         /* The JIT treats this as a normal call */
4075                         break;
4076                 }
4077
4078                 /* Load the destination address */
4079                 g_assert (loc && loc->opcode == OP_REGOFFSET);
4080
4081                 if (arm_is_imm12 (loc->inst_offset)) {
4082                         ARM_LDR_IMM (code, ARMREG_LR, loc->inst_basereg, loc->inst_offset);
4083                 } else {
4084                         code = mono_arm_emit_load_imm (code, ARMREG_LR, loc->inst_offset);
4085                         ARM_LDR_REG_REG (code, ARMREG_LR, loc->inst_basereg, ARMREG_LR);
4086                 }
4087
4088                 if (cinfo->ret.storage == RegTypeStructByVal) {
4089                         int rsize = cinfo->ret.struct_size;
4090
4091                         for (i = 0; i < cinfo->ret.nregs; ++i) {
4092                                 g_assert (rsize >= 0);
4093                                 switch (rsize) {
4094                                 case 0:
4095                                         break;
4096                                 case 1:
4097                                         ARM_STRB_IMM (code, i, ARMREG_LR, i * 4);
4098                                         break;
4099                                 case 2:
4100                                         ARM_STRH_IMM (code, i, ARMREG_LR, i * 4);
4101                                         break;
4102                                 default:
4103                                         ARM_STR_IMM (code, i, ARMREG_LR, i * 4);
4104                                         break;
4105                                 }
4106                                 rsize -= 4;
4107                         }
4108                 } else {
4109                         for (i = 0; i < cinfo->ret.nregs; ++i) {
4110                                 if (cinfo->ret.esize == 4)
4111                                         ARM_FSTS (code, cinfo->ret.reg + i, ARMREG_LR, i * 4);
4112                                 else
4113                                         ARM_FSTD (code, cinfo->ret.reg + (i * 2), ARMREG_LR, i * 8);
4114                         }
4115                 }
4116                 return code;
4117         }
4118         default:
4119                 break;
4120         }
4121
4122         switch (ins->opcode) {
4123         case OP_FCALL:
4124         case OP_FCALL_REG:
4125         case OP_FCALL_MEMBASE:
4126                 if (IS_VFP) {
4127                         MonoType *sig_ret = mini_get_underlying_type (((MonoCallInst*)ins)->signature->ret);
4128                         if (sig_ret->type == MONO_TYPE_R4) {
4129                                 if (IS_HARD_FLOAT) {
4130                                         ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4131                                 } else {
4132                                         ARM_FMSR (code, ins->dreg, ARMREG_R0);
4133                                         ARM_CVTS (code, ins->dreg, ins->dreg);
4134                                 }
4135                         } else {
4136                                 if (IS_HARD_FLOAT) {
4137                                         ARM_CPYD (code, ins->dreg, ARM_VFP_D0);
4138                                 } else {
4139                                         ARM_FMDRR (code, ARMREG_R0, ARMREG_R1, ins->dreg);
4140                                 }
4141                         }
4142                 }
4143                 break;
4144         case OP_RCALL:
4145         case OP_RCALL_REG:
4146         case OP_RCALL_MEMBASE: {
4147                 MonoType *sig_ret;
4148
4149                 g_assert (IS_VFP);
4150
4151                 sig_ret = mini_get_underlying_type (((MonoCallInst*)ins)->signature->ret);
4152                 g_assert (sig_ret->type == MONO_TYPE_R4);
4153                 if (IS_HARD_FLOAT) {
4154                         ARM_CPYS (code, ins->dreg, ARM_VFP_F0);
4155                 } else {
4156                         ARM_FMSR (code, ins->dreg, ARMREG_R0);
4157                         ARM_CPYS (code, ins->dreg, ins->dreg);
4158                 }
4159                 break;
4160         }
4161         default:
4162                 break;
4163         }
4164
4165         return code;
4166 }
4167
4168 void
4169 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
4170 {
4171         MonoInst *ins;
4172         MonoCallInst *call;
4173         guint offset;
4174         guint8 *code = cfg->native_code + cfg->code_len;
4175         MonoInst *last_ins = NULL;
4176         guint last_offset = 0;
4177         int max_len, cpos;
4178         int imm8, rot_amount;
4179
4180         /* we don't align basic blocks of loops on arm */
4181
4182         if (cfg->verbose_level > 2)
4183                 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
4184
4185         cpos = bb->max_offset;
4186
4187         if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
4188                 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
4189                 //g_assert (!mono_compile_aot);
4190                 //cpos += 6;
4191                 //if (bb->cil_code)
4192                 //      cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
4193                 /* this is not thread save, but good enough */
4194                 /* fixme: howto handle overflows? */
4195                 //x86_inc_mem (code, &cov->data [bb->dfn].count); 
4196         }
4197
4198     if (mono_break_at_bb_method && mono_method_desc_full_match (mono_break_at_bb_method, cfg->method) && bb->block_num == mono_break_at_bb_bb_num) {
4199                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
4200                                                          (gpointer)"mono_break");
4201                 code = emit_call_seq (cfg, code);
4202         }
4203
4204         MONO_BB_FOR_EACH_INS (bb, ins) {
4205                 offset = code - cfg->native_code;
4206
4207                 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4208
4209                 if (offset > (cfg->code_size - max_len - 16)) {
4210                         cfg->code_size *= 2;
4211                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4212                         code = cfg->native_code + offset;
4213                 }
4214         //      if (ins->cil_code)
4215         //              g_print ("cil code\n");
4216                 mono_debug_record_line_number (cfg, ins, offset);
4217
4218                 switch (ins->opcode) {
4219                 case OP_MEMORY_BARRIER:
4220                         if (v6_supported) {
4221                                 ARM_MOV_REG_IMM8 (code, ARMREG_R0, 0);
4222                                 ARM_MCR (code, 15, 0, ARMREG_R0, 7, 10, 5);
4223                         }
4224                         break;
4225                 case OP_TLS_GET:
4226                         code = emit_tls_get (code, ins->dreg, ins->inst_offset);
4227                         break;
4228                 case OP_TLS_SET:
4229                         code = emit_tls_set (code, ins->sreg1, ins->inst_offset);
4230                         break;
4231                 case OP_ATOMIC_EXCHANGE_I4:
4232                 case OP_ATOMIC_CAS_I4:
4233                 case OP_ATOMIC_ADD_I4: {
4234                         int tmpreg;
4235                         guint8 *buf [16];
4236
4237                         g_assert (v7_supported);
4238
4239                         /* Free up a reg */
4240                         if (ins->sreg1 != ARMREG_IP && ins->sreg2 != ARMREG_IP && ins->sreg3 != ARMREG_IP)
4241                                 tmpreg = ARMREG_IP;
4242                         else if (ins->sreg1 != ARMREG_R0 && ins->sreg2 != ARMREG_R0 && ins->sreg3 != ARMREG_R0)
4243                                 tmpreg = ARMREG_R0;
4244                         else if (ins->sreg1 != ARMREG_R1 && ins->sreg2 != ARMREG_R1 && ins->sreg3 != ARMREG_R1)
4245                                 tmpreg = ARMREG_R1;
4246                         else
4247                                 tmpreg = ARMREG_R2;
4248                         g_assert (cfg->arch.atomic_tmp_offset != -1);
4249                         ARM_STR_IMM (code, tmpreg, cfg->frame_reg, cfg->arch.atomic_tmp_offset);
4250
4251                         switch (ins->opcode) {
4252                         case OP_ATOMIC_EXCHANGE_I4:
4253                                 buf [0] = code;
4254                                 ARM_DMB (code, ARM_DMB_SY);
4255                                 ARM_LDREX_REG (code, ARMREG_LR, ins->sreg1);
4256                                 ARM_STREX_REG (code, tmpreg, ins->sreg2, ins->sreg1);
4257                                 ARM_CMP_REG_IMM (code, tmpreg, 0, 0);
4258                                 buf [1] = code;
4259                                 ARM_B_COND (code, ARMCOND_NE, 0);
4260                                 arm_patch (buf [1], buf [0]);
4261                                 break;
4262                         case OP_ATOMIC_CAS_I4:
4263                                 ARM_DMB (code, ARM_DMB_SY);
4264                                 buf [0] = code;
4265                                 ARM_LDREX_REG (code, ARMREG_LR, ins->sreg1);
4266                                 ARM_CMP_REG_REG (code, ARMREG_LR, ins->sreg3);
4267                                 buf [1] = code;
4268                                 ARM_B_COND (code, ARMCOND_NE, 0);
4269                                 ARM_STREX_REG (code, tmpreg, ins->sreg2, ins->sreg1);
4270                                 ARM_CMP_REG_IMM (code, tmpreg, 0, 0);
4271                                 buf [2] = code;
4272                                 ARM_B_COND (code, ARMCOND_NE, 0);
4273                                 arm_patch (buf [2], buf [0]);
4274                                 arm_patch (buf [1], code);
4275                                 break;
4276                         case OP_ATOMIC_ADD_I4:
4277                                 buf [0] = code;
4278                                 ARM_DMB (code, ARM_DMB_SY);
4279                                 ARM_LDREX_REG (code, ARMREG_LR, ins->sreg1);
4280                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->sreg2);
4281                                 ARM_STREX_REG (code, tmpreg, ARMREG_LR, ins->sreg1);
4282                                 ARM_CMP_REG_IMM (code, tmpreg, 0, 0);
4283                                 buf [1] = code;
4284                                 ARM_B_COND (code, ARMCOND_NE, 0);
4285                                 arm_patch (buf [1], buf [0]);
4286                                 break;
4287                         default:
4288                                 g_assert_not_reached ();
4289                         }
4290
4291                         ARM_DMB (code, ARM_DMB_SY);
4292                         if (tmpreg != ins->dreg)
4293                                 ARM_LDR_IMM (code, tmpreg, cfg->frame_reg, cfg->arch.atomic_tmp_offset);
4294                         ARM_MOV_REG_REG (code, ins->dreg, ARMREG_LR);
4295                         break;
4296                 }
4297                 case OP_ATOMIC_LOAD_I1:
4298                 case OP_ATOMIC_LOAD_U1:
4299                 case OP_ATOMIC_LOAD_I2:
4300                 case OP_ATOMIC_LOAD_U2:
4301                 case OP_ATOMIC_LOAD_I4:
4302                 case OP_ATOMIC_LOAD_U4:
4303                 case OP_ATOMIC_LOAD_R4:
4304                 case OP_ATOMIC_LOAD_R8: {
4305                         if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4306                                 ARM_DMB (code, ARM_DMB_SY);
4307
4308                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4309
4310                         switch (ins->opcode) {
4311                         case OP_ATOMIC_LOAD_I1:
4312                                 ARM_LDRSB_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4313                                 break;
4314                         case OP_ATOMIC_LOAD_U1:
4315                                 ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4316                                 break;
4317                         case OP_ATOMIC_LOAD_I2:
4318                                 ARM_LDRSH_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4319                                 break;
4320                         case OP_ATOMIC_LOAD_U2:
4321                                 ARM_LDRH_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4322                                 break;
4323                         case OP_ATOMIC_LOAD_I4:
4324                         case OP_ATOMIC_LOAD_U4:
4325                                 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4326                                 break;
4327                         case OP_ATOMIC_LOAD_R4:
4328                                 if (cfg->r4fp) {
4329                                         ARM_ADD_REG_REG (code, ARMREG_LR, ins->inst_basereg, ARMREG_LR);
4330                                         ARM_FLDS (code, ins->dreg, ARMREG_LR, 0);
4331                                 } else {
4332                                         code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
4333                                         ARM_ADD_REG_REG (code, ARMREG_LR, ins->inst_basereg, ARMREG_LR);
4334                                         ARM_FLDS (code, vfp_scratch1, ARMREG_LR, 0);
4335                                         ARM_CVTS (code, ins->dreg, vfp_scratch1);
4336                                         code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
4337                                 }
4338                                 break;
4339                         case OP_ATOMIC_LOAD_R8:
4340                                 ARM_ADD_REG_REG (code, ARMREG_LR, ins->inst_basereg, ARMREG_LR);
4341                                 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4342                                 break;
4343                         }
4344
4345                         if (ins->backend.memory_barrier_kind != MONO_MEMORY_BARRIER_NONE)
4346                                 ARM_DMB (code, ARM_DMB_SY);
4347                         break;
4348                 }
4349                 case OP_ATOMIC_STORE_I1:
4350                 case OP_ATOMIC_STORE_U1:
4351                 case OP_ATOMIC_STORE_I2:
4352                 case OP_ATOMIC_STORE_U2:
4353                 case OP_ATOMIC_STORE_I4:
4354                 case OP_ATOMIC_STORE_U4:
4355                 case OP_ATOMIC_STORE_R4:
4356                 case OP_ATOMIC_STORE_R8: {
4357                         if (ins->backend.memory_barrier_kind != MONO_MEMORY_BARRIER_NONE)
4358                                 ARM_DMB (code, ARM_DMB_SY);
4359
4360                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4361
4362                         switch (ins->opcode) {
4363                         case OP_ATOMIC_STORE_I1:
4364                         case OP_ATOMIC_STORE_U1:
4365                                 ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
4366                                 break;
4367                         case OP_ATOMIC_STORE_I2:
4368                         case OP_ATOMIC_STORE_U2:
4369                                 ARM_STRH_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
4370                                 break;
4371                         case OP_ATOMIC_STORE_I4:
4372                         case OP_ATOMIC_STORE_U4:
4373                                 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
4374                                 break;
4375                         case OP_ATOMIC_STORE_R4:
4376                                 if (cfg->r4fp) {
4377                                         ARM_ADD_REG_REG (code, ARMREG_LR, ins->inst_destbasereg, ARMREG_LR);
4378                                         ARM_FSTS (code, ins->sreg1, ARMREG_LR, 0);
4379                                 } else {
4380                                         code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
4381                                         ARM_ADD_REG_REG (code, ARMREG_LR, ins->inst_destbasereg, ARMREG_LR);
4382                                         ARM_CVTD (code, vfp_scratch1, ins->sreg1);
4383                                         ARM_FSTS (code, vfp_scratch1, ARMREG_LR, 0);
4384                                         code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
4385                                 }
4386                                 break;
4387                         case OP_ATOMIC_STORE_R8:
4388                                 ARM_ADD_REG_REG (code, ARMREG_LR, ins->inst_destbasereg, ARMREG_LR);
4389                                 ARM_FSTD (code, ins->sreg1, ARMREG_LR, 0);
4390                                 break;
4391                         }
4392
4393                         if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4394                                 ARM_DMB (code, ARM_DMB_SY);
4395                         break;
4396                 }
4397                 case OP_BIGMUL:
4398                         ARM_SMULL_REG_REG (code, ins->backend.reg3, ins->dreg, ins->sreg1, ins->sreg2);
4399                         break;
4400                 case OP_BIGMUL_UN:
4401                         ARM_UMULL_REG_REG (code, ins->backend.reg3, ins->dreg, ins->sreg1, ins->sreg2);
4402                         break;
4403                 case OP_STOREI1_MEMBASE_IMM:
4404                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
4405                         g_assert (arm_is_imm12 (ins->inst_offset));
4406                         ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
4407                         break;
4408                 case OP_STOREI2_MEMBASE_IMM:
4409                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
4410                         g_assert (arm_is_imm8 (ins->inst_offset));
4411                         ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
4412                         break;
4413                 case OP_STORE_MEMBASE_IMM:
4414                 case OP_STOREI4_MEMBASE_IMM:
4415                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
4416                         g_assert (arm_is_imm12 (ins->inst_offset));
4417                         ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
4418                         break;
4419                 case OP_STOREI1_MEMBASE_REG:
4420                         g_assert (arm_is_imm12 (ins->inst_offset));
4421                         ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4422                         break;
4423                 case OP_STOREI2_MEMBASE_REG:
4424                         g_assert (arm_is_imm8 (ins->inst_offset));
4425                         ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4426                         break;
4427                 case OP_STORE_MEMBASE_REG:
4428                 case OP_STOREI4_MEMBASE_REG:
4429                         /* this case is special, since it happens for spill code after lowering has been called */
4430                         if (arm_is_imm12 (ins->inst_offset)) {
4431                                 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4432                         } else {
4433                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4434                                 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
4435                         }
4436                         break;
4437                 case OP_STOREI1_MEMINDEX:
4438                         ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
4439                         break;
4440                 case OP_STOREI2_MEMINDEX:
4441                         ARM_STRH_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
4442                         break;
4443                 case OP_STORE_MEMINDEX:
4444                 case OP_STOREI4_MEMINDEX:
4445                         ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
4446                         break;
4447                 case OP_LOADU4_MEM:
4448                         g_assert_not_reached ();
4449                         break;
4450                 case OP_LOAD_MEMINDEX:
4451                 case OP_LOADI4_MEMINDEX:
4452                 case OP_LOADU4_MEMINDEX:
4453                         ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4454                         break;
4455                 case OP_LOADI1_MEMINDEX:
4456                         ARM_LDRSB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4457                         break;
4458                 case OP_LOADU1_MEMINDEX:
4459                         ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4460                         break;
4461                 case OP_LOADI2_MEMINDEX:
4462                         ARM_LDRSH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4463                         break;
4464                 case OP_LOADU2_MEMINDEX:
4465                         ARM_LDRH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4466                         break;
4467                 case OP_LOAD_MEMBASE:
4468                 case OP_LOADI4_MEMBASE:
4469                 case OP_LOADU4_MEMBASE:
4470                         /* this case is special, since it happens for spill code after lowering has been called */
4471                         if (arm_is_imm12 (ins->inst_offset)) {
4472                                 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4473                         } else {
4474                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4475                                 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4476                         }
4477                         break;
4478                 case OP_LOADI1_MEMBASE:
4479                         g_assert (arm_is_imm8 (ins->inst_offset));
4480                         ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4481                         break;
4482                 case OP_LOADU1_MEMBASE:
4483                         g_assert (arm_is_imm12 (ins->inst_offset));
4484                         ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4485                         break;
4486                 case OP_LOADU2_MEMBASE:
4487                         g_assert (arm_is_imm8 (ins->inst_offset));
4488                         ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4489                         break;
4490                 case OP_LOADI2_MEMBASE:
4491                         g_assert (arm_is_imm8 (ins->inst_offset));
4492                         ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4493                         break;
4494                 case OP_ICONV_TO_I1:
4495                         ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
4496                         ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
4497                         break;
4498                 case OP_ICONV_TO_I2:
4499                         ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
4500                         ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
4501                         break;
4502                 case OP_ICONV_TO_U1:
4503                         ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
4504                         break;
4505                 case OP_ICONV_TO_U2:
4506                         ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
4507                         ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
4508                         break;
4509                 case OP_COMPARE:
4510                 case OP_ICOMPARE:
4511                         ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
4512                         break;
4513                 case OP_COMPARE_IMM:
4514                 case OP_ICOMPARE_IMM:
4515                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4516                         g_assert (imm8 >= 0);
4517                         ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
4518                         break;
4519                 case OP_BREAK:
4520                         /*
4521                          * gdb does not like encountering the hw breakpoint ins in the debugged code. 
4522                          * So instead of emitting a trap, we emit a call a C function and place a 
4523                          * breakpoint there.
4524                          */
4525                         //*(int*)code = 0xef9f0001;
4526                         //code += 4;
4527                         //ARM_DBRK (code);
4528                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
4529                                                                  (gpointer)"mono_break");
4530                         code = emit_call_seq (cfg, code);
4531                         break;
4532                 case OP_RELAXED_NOP:
4533                         ARM_NOP (code);
4534                         break;
4535                 case OP_NOP:
4536                 case OP_DUMMY_USE:
4537                 case OP_DUMMY_STORE:
4538                 case OP_DUMMY_ICONST:
4539                 case OP_DUMMY_R8CONST:
4540                 case OP_NOT_REACHED:
4541                 case OP_NOT_NULL:
4542                         break;
4543                 case OP_IL_SEQ_POINT:
4544                         mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4545                         break;
4546                 case OP_SEQ_POINT: {
4547                         int i;
4548                         MonoInst *info_var = cfg->arch.seq_point_info_var;
4549                         MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
4550                         MonoInst *ss_method_var = cfg->arch.seq_point_ss_method_var;
4551                         MonoInst *bp_method_var = cfg->arch.seq_point_bp_method_var;
4552                         MonoInst *var;
4553                         int dreg = ARMREG_LR;
4554
4555 #if 0
4556                         if (cfg->soft_breakpoints) {
4557                                 g_assert (!cfg->compile_aot);
4558                         }
4559 #endif
4560
4561                         /*
4562                          * For AOT, we use one got slot per method, which will point to a
4563                          * SeqPointInfo structure, containing all the information required
4564                          * by the code below.
4565                          */
4566                         if (cfg->compile_aot) {
4567                                 g_assert (info_var);
4568                                 g_assert (info_var->opcode == OP_REGOFFSET);
4569                                 g_assert (arm_is_imm12 (info_var->inst_offset));
4570                         }
4571
4572                         if (!cfg->soft_breakpoints && !cfg->compile_aot) {
4573                                 /*
4574                                  * Read from the single stepping trigger page. This will cause a
4575                                  * SIGSEGV when single stepping is enabled.
4576                                  * We do this _before_ the breakpoint, so single stepping after
4577                                  * a breakpoint is hit will step to the next IL offset.
4578                                  */
4579                                 g_assert (((guint64)(gsize)ss_trigger_page >> 32) == 0);
4580                         }
4581
4582                         /* Single step check */
4583                         if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4584                                 if (cfg->soft_breakpoints) {
4585                                         /* Load the address of the sequence point method variable. */
4586                                         var = ss_method_var;
4587                                         g_assert (var);
4588                                         g_assert (var->opcode == OP_REGOFFSET);
4589                                         g_assert (arm_is_imm12 (var->inst_offset));
4590                                         ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
4591
4592                                         /* Read the value and check whether it is non-zero. */
4593                                         ARM_LDR_IMM (code, dreg, dreg, 0);
4594                                         ARM_CMP_REG_IMM (code, dreg, 0, 0);
4595                                         /* Call it conditionally. */
4596                                         ARM_BLX_REG_COND (code, ARMCOND_NE, dreg);
4597                                 } else {
4598                                         if (cfg->compile_aot) {
4599                                                 /* Load the trigger page addr from the variable initialized in the prolog */
4600                                                 var = ss_trigger_page_var;
4601                                                 g_assert (var);
4602                                                 g_assert (var->opcode == OP_REGOFFSET);
4603                                                 g_assert (arm_is_imm12 (var->inst_offset));
4604                                                 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
4605                                         } else {
4606                                                 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
4607                                                 ARM_B (code, 0);
4608                                                 *(int*)code = (int)ss_trigger_page;
4609                                                 code += 4;
4610                                         }
4611                                         ARM_LDR_IMM (code, dreg, dreg, 0);
4612                                 }
4613                         }
4614
4615                         mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4616
4617                         /* Breakpoint check */
4618                         if (cfg->compile_aot) {
4619                                 guint32 offset = code - cfg->native_code;
4620                                 guint32 val;
4621
4622                                 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
4623                                 /* Add the offset */
4624                                 val = ((offset / 4) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4625                                 /* Load the info->bp_addrs [offset], which is either 0 or the address of a trigger page */
4626                                 if (arm_is_imm12 ((int)val)) {
4627                                         ARM_LDR_IMM (code, dreg, dreg, val);
4628                                 } else {
4629                                         ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF), 0);
4630                                         if (val & 0xFF00)
4631                                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
4632                                         if (val & 0xFF0000)
4633                                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
4634                                         g_assert (!(val & 0xFF000000));
4635
4636                                         ARM_LDR_IMM (code, dreg, dreg, 0);
4637                                 }
4638                                 /* What is faster, a branch or a load ? */
4639                                 ARM_CMP_REG_IMM (code, dreg, 0, 0);
4640                                 /* The breakpoint instruction */
4641                                 if (cfg->soft_breakpoints)
4642                                         ARM_BLX_REG_COND (code, ARMCOND_NE, dreg);
4643                                 else
4644                                         ARM_LDR_IMM_COND (code, dreg, dreg, 0, ARMCOND_NE);
4645                         } else if (cfg->soft_breakpoints) {
4646                                 /* Load the address of the breakpoint method into ip. */
4647                                 var = bp_method_var;
4648                                 g_assert (var);
4649                                 g_assert (var->opcode == OP_REGOFFSET);
4650                                 g_assert (arm_is_imm12 (var->inst_offset));
4651                                 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
4652
4653                                 /*
4654                                  * A placeholder for a possible breakpoint inserted by
4655                                  * mono_arch_set_breakpoint ().
4656                                  */
4657                                 ARM_NOP (code);
4658                         } else {
4659                                 /* 
4660                                  * A placeholder for a possible breakpoint inserted by
4661                                  * mono_arch_set_breakpoint ().
4662                                  */
4663                                 for (i = 0; i < 4; ++i)
4664                                         ARM_NOP (code);
4665                         }
4666
4667                         /*
4668                          * Add an additional nop so skipping the bp doesn't cause the ip to point
4669                          * to another IL offset.
4670                          */
4671
4672                         ARM_NOP (code);
4673                         break;
4674                 }
4675                 case OP_ADDCC:
4676                 case OP_IADDCC:
4677                         ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4678                         break;
4679                 case OP_IADD:
4680                         ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4681                         break;
4682                 case OP_ADC:
4683                 case OP_IADC:
4684                         ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4685                         break;
4686                 case OP_ADDCC_IMM:
4687                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4688                         g_assert (imm8 >= 0);
4689                         ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4690                         break;
4691                 case OP_ADD_IMM:
4692                 case OP_IADD_IMM:
4693                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4694                         g_assert (imm8 >= 0);
4695                         ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4696                         break;
4697                 case OP_ADC_IMM:
4698                 case OP_IADC_IMM:
4699                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4700                         g_assert (imm8 >= 0);
4701                         ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4702                         break;
4703                 case OP_IADD_OVF:
4704                         ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4705                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4706                         break;
4707                 case OP_IADD_OVF_UN:
4708                         ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4709                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4710                         break;
4711                 case OP_ISUB_OVF:
4712                         ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4713                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4714                         break;
4715                 case OP_ISUB_OVF_UN:
4716                         ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4717                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
4718                         break;
4719                 case OP_ADD_OVF_CARRY:
4720                         ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4721                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4722                         break;
4723                 case OP_ADD_OVF_UN_CARRY:
4724                         ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4725                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4726                         break;
4727                 case OP_SUB_OVF_CARRY:
4728                         ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4729                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4730                         break;
4731                 case OP_SUB_OVF_UN_CARRY:
4732                         ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4733                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
4734                         break;
4735                 case OP_SUBCC:
4736                 case OP_ISUBCC:
4737                         ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4738                         break;
4739                 case OP_SUBCC_IMM:
4740                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4741                         g_assert (imm8 >= 0);
4742                         ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4743                         break;
4744                 case OP_ISUB:
4745                         ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4746                         break;
4747                 case OP_SBB:
4748                 case OP_ISBB:
4749                         ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4750                         break;
4751                 case OP_SUB_IMM:
4752                 case OP_ISUB_IMM:
4753                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4754                         g_assert (imm8 >= 0);
4755                         ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4756                         break;
4757                 case OP_SBB_IMM:
4758                 case OP_ISBB_IMM:
4759                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4760                         g_assert (imm8 >= 0);
4761                         ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4762                         break;
4763                 case OP_ARM_RSBS_IMM:
4764                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4765                         g_assert (imm8 >= 0);
4766                         ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4767                         break;
4768                 case OP_ARM_RSC_IMM:
4769                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4770                         g_assert (imm8 >= 0);
4771                         ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4772                         break;
4773                 case OP_IAND:
4774                         ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4775                         break;
4776                 case OP_AND_IMM:
4777                 case OP_IAND_IMM:
4778                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4779                         g_assert (imm8 >= 0);
4780                         ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4781                         break;
4782                 case OP_IDIV:
4783                         g_assert (v7s_supported || v7k_supported);
4784                         ARM_SDIV (code, ins->dreg, ins->sreg1, ins->sreg2);
4785                         break;
4786                 case OP_IDIV_UN:
4787                         g_assert (v7s_supported || v7k_supported);
4788                         ARM_UDIV (code, ins->dreg, ins->sreg1, ins->sreg2);
4789                         break;
4790                 case OP_IREM:
4791                         g_assert (v7s_supported || v7k_supported);
4792                         ARM_SDIV (code, ARMREG_LR, ins->sreg1, ins->sreg2);
4793                         ARM_MLS (code, ins->dreg, ARMREG_LR, ins->sreg2, ins->sreg1);
4794                         break;
4795                 case OP_IREM_UN:
4796                         g_assert (v7s_supported || v7k_supported);
4797                         ARM_UDIV (code, ARMREG_LR, ins->sreg1, ins->sreg2);
4798                         ARM_MLS (code, ins->dreg, ARMREG_LR, ins->sreg2, ins->sreg1);
4799                         break;
4800                 case OP_DIV_IMM:
4801                 case OP_REM_IMM:
4802                         g_assert_not_reached ();
4803                 case OP_IOR:
4804                         ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4805                         break;
4806                 case OP_OR_IMM:
4807                 case OP_IOR_IMM:
4808                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4809                         g_assert (imm8 >= 0);
4810                         ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4811                         break;
4812                 case OP_IXOR:
4813                         ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4814                         break;
4815                 case OP_XOR_IMM:
4816                 case OP_IXOR_IMM:
4817                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4818                         g_assert (imm8 >= 0);
4819                         ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4820                         break;
4821                 case OP_ISHL:
4822                         ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4823                         break;
4824                 case OP_SHL_IMM:
4825                 case OP_ISHL_IMM:
4826                         if (ins->inst_imm)
4827                                 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
4828                         else if (ins->dreg != ins->sreg1)
4829                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4830                         break;
4831                 case OP_ISHR:
4832                         ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4833                         break;
4834                 case OP_SHR_IMM:
4835                 case OP_ISHR_IMM:
4836                         if (ins->inst_imm)
4837                                 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
4838                         else if (ins->dreg != ins->sreg1)
4839                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4840                         break;
4841                 case OP_SHR_UN_IMM:
4842                 case OP_ISHR_UN_IMM:
4843                         if (ins->inst_imm)
4844                                 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
4845                         else if (ins->dreg != ins->sreg1)
4846                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4847                         break;
4848                 case OP_ISHR_UN:
4849                         ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4850                         break;
4851                 case OP_INOT:
4852                         ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
4853                         break;
4854                 case OP_INEG:
4855                         ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
4856                         break;
4857                 case OP_IMUL:
4858                         if (ins->dreg == ins->sreg2)
4859                                 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4860                         else
4861                                 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
4862                         break;
4863                 case OP_MUL_IMM:
4864                         g_assert_not_reached ();
4865                         break;
4866                 case OP_IMUL_OVF:
4867                         /* FIXME: handle ovf/ sreg2 != dreg */
4868                         ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4869                         /* FIXME: MUL doesn't set the C/O flags on ARM */
4870                         break;
4871                 case OP_IMUL_OVF_UN:
4872                         /* FIXME: handle ovf/ sreg2 != dreg */
4873                         ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4874                         /* FIXME: MUL doesn't set the C/O flags on ARM */
4875                         break;
4876                 case OP_ICONST:
4877                         code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
4878                         break;
4879                 case OP_AOTCONST:
4880                         /* Load the GOT offset */
4881                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4882                         ARM_LDR_IMM (code, ins->dreg, ARMREG_PC, 0);
4883                         ARM_B (code, 0);
4884                         *(gpointer*)code = NULL;
4885                         code += 4;
4886                         /* Load the value from the GOT */
4887                         ARM_LDR_REG_REG (code, ins->dreg, ARMREG_PC, ins->dreg);
4888                         break;
4889                 case OP_OBJC_GET_SELECTOR:
4890                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_OBJC_SELECTOR_REF, ins->inst_p0);
4891                         ARM_LDR_IMM (code, ins->dreg, ARMREG_PC, 0);
4892                         ARM_B (code, 0);
4893                         *(gpointer*)code = NULL;
4894                         code += 4;
4895                         ARM_LDR_REG_REG (code, ins->dreg, ARMREG_PC, ins->dreg);
4896                         break;
4897                 case OP_ICONV_TO_I4:
4898                 case OP_ICONV_TO_U4:
4899                 case OP_MOVE:
4900                         if (ins->dreg != ins->sreg1)
4901                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4902                         break;
4903                 case OP_SETLRET: {
4904                         int saved = ins->sreg2;
4905                         if (ins->sreg2 == ARM_LSW_REG) {
4906                                 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
4907                                 saved = ARMREG_LR;
4908                         }
4909                         if (ins->sreg1 != ARM_LSW_REG)
4910                                 ARM_MOV_REG_REG (code, ARM_LSW_REG, ins->sreg1);
4911                         if (saved != ARM_MSW_REG)
4912                                 ARM_MOV_REG_REG (code, ARM_MSW_REG, saved);
4913                         break;
4914                 }
4915                 case OP_FMOVE:
4916                         if (IS_VFP && ins->dreg != ins->sreg1)
4917                                 ARM_CPYD (code, ins->dreg, ins->sreg1);
4918                         break;
4919                 case OP_RMOVE:
4920                         if (IS_VFP && ins->dreg != ins->sreg1)
4921                                 ARM_CPYS (code, ins->dreg, ins->sreg1);
4922                         break;
4923                 case OP_MOVE_F_TO_I4:
4924                         if (cfg->r4fp) {
4925                                 ARM_FMRS (code, ins->dreg, ins->sreg1);
4926                         } else {
4927                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
4928                                 ARM_CVTD (code, vfp_scratch1, ins->sreg1);
4929                                 ARM_FMRS (code, ins->dreg, vfp_scratch1);
4930                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
4931                         }
4932                         break;
4933                 case OP_MOVE_I4_TO_F:
4934                         if (cfg->r4fp) {
4935                                 ARM_FMSR (code, ins->dreg, ins->sreg1);
4936                         } else {
4937                                 ARM_FMSR (code, ins->dreg, ins->sreg1);
4938                                 ARM_CVTS (code, ins->dreg, ins->dreg);
4939                         }
4940                         break;
4941                 case OP_FCONV_TO_R4:
4942                         if (IS_VFP) {
4943                                 if (cfg->r4fp) {
4944                                         ARM_CVTD (code, ins->dreg, ins->sreg1);
4945                                 } else {
4946                                         ARM_CVTD (code, ins->dreg, ins->sreg1);
4947                                         ARM_CVTS (code, ins->dreg, ins->dreg);
4948                                 }
4949                         }
4950                         break;
4951                 case OP_TAILCALL: {
4952                         MonoCallInst *call = (MonoCallInst*)ins;
4953
4954                         /*
4955                          * The stack looks like the following:
4956                          * <caller argument area>
4957                          * <saved regs etc>
4958                          * <rest of frame>
4959                          * <callee argument area>
4960                          * Need to copy the arguments from the callee argument area to
4961                          * the caller argument area, and pop the frame.
4962                          */
4963                         if (call->stack_usage) {
4964                                 int i, prev_sp_offset = 0;
4965
4966                                 /* Compute size of saved registers restored below */
4967                                 if (iphone_abi)
4968                                         prev_sp_offset = 2 * 4;
4969                                 else
4970                                         prev_sp_offset = 1 * 4;
4971                                 for (i = 0; i < 16; ++i) {
4972                                         if (cfg->used_int_regs & (1 << i))
4973                                                 prev_sp_offset += 4;
4974                                 }
4975
4976                                 code = emit_big_add (code, ARMREG_IP, cfg->frame_reg, cfg->stack_usage + prev_sp_offset);
4977
4978                                 /* Copy arguments on the stack to our argument area */
4979                                 for (i = 0; i < call->stack_usage; i += sizeof (mgreg_t)) {
4980                                         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, i);
4981                                         ARM_STR_IMM (code, ARMREG_LR, ARMREG_IP, i);
4982                                 }
4983                         }
4984
4985                         /*
4986                          * Keep in sync with mono_arch_emit_epilog
4987                          */
4988                         g_assert (!cfg->method->save_lmf);
4989
4990                         code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
4991                         if (iphone_abi) {
4992                                 if (cfg->used_int_regs)
4993                                         ARM_POP (code, cfg->used_int_regs);
4994                                 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
4995                         } else {
4996                                 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_LR));
4997                         }
4998
4999                         mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
5000                         if (cfg->compile_aot) {
5001                                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
5002                                 ARM_B (code, 0);
5003                                 *(gpointer*)code = NULL;
5004                                 code += 4;
5005                                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
5006                         } else {
5007                                 code = mono_arm_patchable_b (code, ARMCOND_AL);
5008                                 cfg->thunk_area += THUNK_SIZE;
5009                         }
5010                         break;
5011                 }
5012                 case OP_CHECK_THIS:
5013                         /* ensure ins->sreg1 is not NULL */
5014                         ARM_LDRB_IMM (code, ARMREG_LR, ins->sreg1, 0);
5015                         break;
5016                 case OP_ARGLIST: {
5017                         g_assert (cfg->sig_cookie < 128);
5018                         ARM_LDR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
5019                         ARM_STR_IMM (code, ARMREG_IP, ins->sreg1, 0);
5020                         break;
5021                 }
5022                 case OP_FCALL:
5023                 case OP_RCALL:
5024                 case OP_LCALL:
5025                 case OP_VCALL:
5026                 case OP_VCALL2:
5027                 case OP_VOIDCALL:
5028                 case OP_CALL:
5029                         call = (MonoCallInst*)ins;
5030
5031                         if (IS_HARD_FLOAT)
5032                                 code = emit_float_args (cfg, call, code, &max_len, &offset);
5033
5034                         if (ins->flags & MONO_INST_HAS_METHOD)
5035                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
5036                         else
5037                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
5038                         code = emit_call_seq (cfg, code);
5039                         ins->flags |= MONO_INST_GC_CALLSITE;
5040                         ins->backend.pc_offset = code - cfg->native_code;
5041                         code = emit_move_return_value (cfg, ins, code);
5042                         break;
5043                 case OP_FCALL_REG:
5044                 case OP_RCALL_REG:
5045                 case OP_LCALL_REG:
5046                 case OP_VCALL_REG:
5047                 case OP_VCALL2_REG:
5048                 case OP_VOIDCALL_REG:
5049                 case OP_CALL_REG:
5050                         if (IS_HARD_FLOAT)
5051                                 code = emit_float_args (cfg, (MonoCallInst *)ins, code, &max_len, &offset);
5052
5053                         code = emit_call_reg (code, ins->sreg1);
5054                         ins->flags |= MONO_INST_GC_CALLSITE;
5055                         ins->backend.pc_offset = code - cfg->native_code;
5056                         code = emit_move_return_value (cfg, ins, code);
5057                         break;
5058                 case OP_FCALL_MEMBASE:
5059                 case OP_RCALL_MEMBASE:
5060                 case OP_LCALL_MEMBASE:
5061                 case OP_VCALL_MEMBASE:
5062                 case OP_VCALL2_MEMBASE:
5063                 case OP_VOIDCALL_MEMBASE:
5064                 case OP_CALL_MEMBASE: {
5065                         g_assert (ins->sreg1 != ARMREG_LR);
5066                         call = (MonoCallInst*)ins;
5067
5068                         if (IS_HARD_FLOAT)
5069                                 code = emit_float_args (cfg, call, code, &max_len, &offset);
5070                         if (!arm_is_imm12 (ins->inst_offset)) {
5071                                 /* sreg1 might be IP */
5072                                 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg1);
5073                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, ins->inst_offset);
5074                                 ARM_ADD_REG_REG (code, ARMREG_IP, ARMREG_IP, ARMREG_LR);
5075                                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
5076                                 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, 0);
5077                         } else {
5078                                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
5079                                 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
5080                         }
5081                         ins->flags |= MONO_INST_GC_CALLSITE;
5082                         ins->backend.pc_offset = code - cfg->native_code;
5083                         code = emit_move_return_value (cfg, ins, code);
5084                         break;
5085                 }
5086                 case OP_GENERIC_CLASS_INIT: {
5087                         int byte_offset;
5088                         guint8 *jump;
5089
5090                         byte_offset = MONO_STRUCT_OFFSET (MonoVTable, initialized);
5091
5092                         g_assert (arm_is_imm8 (byte_offset));
5093                         ARM_LDRSB_IMM (code, ARMREG_IP, ins->sreg1, byte_offset);
5094                         ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
5095                         jump = code;
5096                         ARM_B_COND (code, ARMCOND_NE, 0);
5097
5098                         /* Uninitialized case */
5099                         g_assert (ins->sreg1 == ARMREG_R0);
5100
5101                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD,
5102                                                                  (gpointer)"mono_generic_class_init");
5103                         code = emit_call_seq (cfg, code);
5104
5105                         /* Initialized case */
5106                         arm_patch (jump, code);
5107                         break;
5108                 }
5109                 case OP_LOCALLOC: {
5110                         /* round the size to 8 bytes */
5111                         ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, (MONO_ARCH_FRAME_ALIGNMENT - 1));
5112                         ARM_BIC_REG_IMM8 (code, ins->dreg, ins->dreg, (MONO_ARCH_FRAME_ALIGNMENT - 1));
5113                         ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
5114                         /* memzero the area: dreg holds the size, sp is the pointer */
5115                         if (ins->flags & MONO_INST_INIT) {
5116                                 guint8 *start_loop, *branch_to_cond;
5117                                 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
5118                                 branch_to_cond = code;
5119                                 ARM_B (code, 0);
5120                                 start_loop = code;
5121                                 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
5122                                 arm_patch (branch_to_cond, code);
5123                                 /* decrement by 4 and set flags */
5124                                 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, sizeof (mgreg_t));
5125                                 ARM_B_COND (code, ARMCOND_GE, 0);
5126                                 arm_patch (code - 4, start_loop);
5127                         }
5128                         ARM_MOV_REG_REG (code, ins->dreg, ARMREG_SP);
5129                         if (cfg->param_area)
5130                                 code = emit_sub_imm (code, ARMREG_SP, ARMREG_SP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
5131                         break;
5132                 }
5133                 case OP_DYN_CALL: {
5134                         int i;
5135                         MonoInst *var = cfg->dyn_call_var;
5136                         guint8 *buf [16];
5137
5138                         g_assert (var->opcode == OP_REGOFFSET);
5139                         g_assert (arm_is_imm12 (var->inst_offset));
5140
5141                         /* lr = args buffer filled by mono_arch_get_dyn_call_args () */
5142                         ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg1);
5143                         /* ip = ftn */
5144                         ARM_MOV_REG_REG (code, ARMREG_IP, ins->sreg2);
5145
5146                         /* Save args buffer */
5147                         ARM_STR_IMM (code, ARMREG_LR, var->inst_basereg, var->inst_offset);
5148
5149                         /* Set stack slots using R0 as scratch reg */
5150                         /* MONO_ARCH_DYN_CALL_PARAM_AREA gives the size of stack space available */
5151                         for (i = 0; i < DYN_CALL_STACK_ARGS; ++i) {
5152                                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, (PARAM_REGS + i) * sizeof (mgreg_t));
5153                                 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, i * sizeof (mgreg_t));
5154                         }
5155
5156                         /* Set fp argument registers */
5157                         if (IS_HARD_FLOAT) {
5158                                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, MONO_STRUCT_OFFSET (DynCallArgs, has_fpregs));
5159                                 ARM_CMP_REG_IMM (code, ARMREG_R0, 0, 0);
5160                                 buf [0] = code;
5161                                 ARM_B_COND (code, ARMCOND_EQ, 0);
5162                                 for (i = 0; i < FP_PARAM_REGS; ++i) {
5163                                         int offset = MONO_STRUCT_OFFSET (DynCallArgs, fpregs) + (i * sizeof (double));
5164                                         g_assert (arm_is_fpimm8 (offset));
5165                                         ARM_FLDD (code, i * 2, ARMREG_LR, offset);
5166                                 }
5167                                 arm_patch (buf [0], code);
5168                         }
5169
5170                         /* Set argument registers */
5171                         for (i = 0; i < PARAM_REGS; ++i)
5172                                 ARM_LDR_IMM (code, i, ARMREG_LR, i * sizeof (mgreg_t));
5173
5174                         /* Make the call */
5175                         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
5176                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
5177
5178                         /* Save result */
5179                         ARM_LDR_IMM (code, ARMREG_IP, var->inst_basereg, var->inst_offset);
5180                         ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, MONO_STRUCT_OFFSET (DynCallArgs, res)); 
5181                         ARM_STR_IMM (code, ARMREG_R1, ARMREG_IP, MONO_STRUCT_OFFSET (DynCallArgs, res2));
5182                         if (IS_HARD_FLOAT)
5183                                 ARM_FSTD (code, ARM_VFP_D0, ARMREG_IP, MONO_STRUCT_OFFSET (DynCallArgs, fpregs));
5184                         break;
5185                 }
5186                 case OP_THROW: {
5187                         if (ins->sreg1 != ARMREG_R0)
5188                                 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
5189                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
5190                                              (gpointer)"mono_arch_throw_exception");
5191                         code = emit_call_seq (cfg, code);
5192                         break;
5193                 }
5194                 case OP_RETHROW: {
5195                         if (ins->sreg1 != ARMREG_R0)
5196                                 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
5197                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
5198                                              (gpointer)"mono_arch_rethrow_exception");
5199                         code = emit_call_seq (cfg, code);
5200                         break;
5201                 }
5202                 case OP_START_HANDLER: {
5203                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5204                         int param_area = ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT);
5205                         int i, rot_amount;
5206
5207                         /* Reserve a param area, see filter-stack.exe */
5208                         if (param_area) {
5209                                 if ((i = mono_arm_is_rotated_imm8 (param_area, &rot_amount)) >= 0) {
5210                                         ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
5211                                 } else {
5212                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, param_area);
5213                                         ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
5214                                 }
5215                         }
5216
5217                         if (arm_is_imm12 (spvar->inst_offset)) {
5218                                 ARM_STR_IMM (code, ARMREG_LR, spvar->inst_basereg, spvar->inst_offset);
5219                         } else {
5220                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
5221                                 ARM_STR_REG_REG (code, ARMREG_LR, spvar->inst_basereg, ARMREG_IP);
5222                         }
5223                         break;
5224                 }
5225                 case OP_ENDFILTER: {
5226                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5227                         int param_area = ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT);
5228                         int i, rot_amount;
5229
5230                         /* Free the param area */
5231                         if (param_area) {
5232                                 if ((i = mono_arm_is_rotated_imm8 (param_area, &rot_amount)) >= 0) {
5233                                         ARM_ADD_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
5234                                 } else {
5235                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, param_area);
5236                                         ARM_ADD_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
5237                                 }
5238                         }
5239
5240                         if (ins->sreg1 != ARMREG_R0)
5241                                 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
5242                         if (arm_is_imm12 (spvar->inst_offset)) {
5243                                 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
5244                         } else {
5245                                 g_assert (ARMREG_IP != spvar->inst_basereg);
5246                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
5247                                 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
5248                         }
5249                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
5250                         break;
5251                 }
5252                 case OP_ENDFINALLY: {
5253                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5254                         int param_area = ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT);
5255                         int i, rot_amount;
5256
5257                         /* Free the param area */
5258                         if (param_area) {
5259                                 if ((i = mono_arm_is_rotated_imm8 (param_area, &rot_amount)) >= 0) {
5260                                         ARM_ADD_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
5261                                 } else {
5262                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, param_area);
5263                                         ARM_ADD_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
5264                                 }
5265                         }
5266
5267                         if (arm_is_imm12 (spvar->inst_offset)) {
5268                                 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
5269                         } else {
5270                                 g_assert (ARMREG_IP != spvar->inst_basereg);
5271                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
5272                                 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
5273                         }
5274                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
5275                         break;
5276                 }
5277                 case OP_CALL_HANDLER: 
5278                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5279                         code = mono_arm_patchable_bl (code, ARMCOND_AL);
5280                         cfg->thunk_area += THUNK_SIZE;
5281                         mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
5282                         break;
5283                 case OP_GET_EX_OBJ:
5284                         if (ins->dreg != ARMREG_R0)
5285                                 ARM_MOV_REG_REG (code, ins->dreg, ARMREG_R0);
5286                         break;
5287
5288                 case OP_LABEL:
5289                         ins->inst_c0 = code - cfg->native_code;
5290                         break;
5291                 case OP_BR:
5292                         /*if (ins->inst_target_bb->native_offset) {
5293                                 ARM_B (code, 0);
5294                                 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset); 
5295                         } else*/ {
5296                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5297                                 code = mono_arm_patchable_b (code, ARMCOND_AL);
5298                         } 
5299                         break;
5300                 case OP_BR_REG:
5301                         ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
5302                         break;
5303                 case OP_SWITCH:
5304                         /* 
5305                          * In the normal case we have:
5306                          *      ldr pc, [pc, ins->sreg1 << 2]
5307                          *      nop
5308                          * If aot, we have:
5309                          *      ldr lr, [pc, ins->sreg1 << 2]
5310                          *      add pc, pc, lr
5311                          * After follows the data.
5312                          * FIXME: add aot support.
5313                          */
5314                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_SWITCH, ins->inst_p0);
5315                         max_len += 4 * GPOINTER_TO_INT (ins->klass);
5316                         if (offset + max_len > (cfg->code_size - 16)) {
5317                                 cfg->code_size += max_len;
5318                                 cfg->code_size *= 2;
5319                                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5320                                 code = cfg->native_code + offset;
5321                         }
5322                         ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
5323                         ARM_NOP (code);
5324                         code += 4 * GPOINTER_TO_INT (ins->klass);
5325                         break;
5326                 case OP_CEQ:
5327                 case OP_ICEQ:
5328                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
5329                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
5330                         break;
5331                 case OP_CLT:
5332                 case OP_ICLT:
5333                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5334                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
5335                         break;
5336                 case OP_CLT_UN:
5337                 case OP_ICLT_UN:
5338                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5339                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
5340                         break;
5341                 case OP_CGT:
5342                 case OP_ICGT:
5343                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5344                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
5345                         break;
5346                 case OP_CGT_UN:
5347                 case OP_ICGT_UN:
5348                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5349                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
5350                         break;
5351                 case OP_ICNEQ:
5352                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_NE);
5353                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_EQ);
5354                         break;
5355                 case OP_ICGE:
5356                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5357                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_LT);
5358                         break;
5359                 case OP_ICLE:
5360                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5361                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_GT);
5362                         break;
5363                 case OP_ICGE_UN:
5364                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5365                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_LO);
5366                         break;
5367                 case OP_ICLE_UN:
5368                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5369                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_HI);
5370                         break;
5371                 case OP_COND_EXC_EQ:
5372                 case OP_COND_EXC_NE_UN:
5373                 case OP_COND_EXC_LT:
5374                 case OP_COND_EXC_LT_UN:
5375                 case OP_COND_EXC_GT:
5376                 case OP_COND_EXC_GT_UN:
5377                 case OP_COND_EXC_GE:
5378                 case OP_COND_EXC_GE_UN:
5379                 case OP_COND_EXC_LE:
5380                 case OP_COND_EXC_LE_UN:
5381                         EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
5382                         break;
5383                 case OP_COND_EXC_IEQ:
5384                 case OP_COND_EXC_INE_UN:
5385                 case OP_COND_EXC_ILT:
5386                 case OP_COND_EXC_ILT_UN:
5387                 case OP_COND_EXC_IGT:
5388                 case OP_COND_EXC_IGT_UN:
5389                 case OP_COND_EXC_IGE:
5390                 case OP_COND_EXC_IGE_UN:
5391                 case OP_COND_EXC_ILE:
5392                 case OP_COND_EXC_ILE_UN:
5393                         EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_IEQ, ins->inst_p1);
5394                         break;
5395                 case OP_COND_EXC_C:
5396                 case OP_COND_EXC_IC:
5397                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CS, ins->inst_p1);
5398                         break;
5399                 case OP_COND_EXC_OV:
5400                 case OP_COND_EXC_IOV:
5401                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, ins->inst_p1);
5402                         break;
5403                 case OP_COND_EXC_NC:
5404                 case OP_COND_EXC_INC:
5405                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CC, ins->inst_p1);
5406                         break;
5407                 case OP_COND_EXC_NO:
5408                 case OP_COND_EXC_INO:
5409                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VC, ins->inst_p1);
5410                         break;
5411                 case OP_IBEQ:
5412                 case OP_IBNE_UN:
5413                 case OP_IBLT:
5414                 case OP_IBLT_UN:
5415                 case OP_IBGT:
5416                 case OP_IBGT_UN:
5417                 case OP_IBGE:
5418                 case OP_IBGE_UN:
5419                 case OP_IBLE:
5420                 case OP_IBLE_UN:
5421                         EMIT_COND_BRANCH (ins, ins->opcode - OP_IBEQ);
5422                         break;
5423
5424                 /* floating point opcodes */
5425                 case OP_R8CONST:
5426                         if (cfg->compile_aot) {
5427                                 ARM_FLDD (code, ins->dreg, ARMREG_PC, 0);
5428                                 ARM_B (code, 1);
5429                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
5430                                 code += 4;
5431                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
5432                                 code += 4;
5433                         } else {
5434                                 /* FIXME: we can optimize the imm load by dealing with part of 
5435                                  * the displacement in LDFD (aligning to 512).
5436                                  */
5437                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
5438                                 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
5439                         }
5440                         break;
5441                 case OP_R4CONST:
5442                         if (cfg->compile_aot) {
5443                                 ARM_FLDS (code, ins->dreg, ARMREG_PC, 0);
5444                                 ARM_B (code, 0);
5445                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
5446                                 code += 4;
5447                                 if (!cfg->r4fp)
5448                                         ARM_CVTS (code, ins->dreg, ins->dreg);
5449                         } else {
5450                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
5451                                 ARM_FLDS (code, ins->dreg, ARMREG_LR, 0);
5452                                 if (!cfg->r4fp)
5453                                         ARM_CVTS (code, ins->dreg, ins->dreg);
5454                         }
5455                         break;
5456                 case OP_STORER8_MEMBASE_REG:
5457                         /* This is generated by the local regalloc pass which runs after the lowering pass */
5458                         if (!arm_is_fpimm8 (ins->inst_offset)) {
5459                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5460                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
5461                                 ARM_FSTD (code, ins->sreg1, ARMREG_LR, 0);
5462                         } else {
5463                                 ARM_FSTD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
5464                         }
5465                         break;
5466                 case OP_LOADR8_MEMBASE:
5467                         /* This is generated by the local regalloc pass which runs after the lowering pass */
5468                         if (!arm_is_fpimm8 (ins->inst_offset)) {
5469                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5470                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
5471                                 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
5472                         } else {
5473                                 ARM_FLDD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5474                         }
5475                         break;
5476                 case OP_STORER4_MEMBASE_REG:
5477                         g_assert (arm_is_fpimm8 (ins->inst_offset));
5478                         if (cfg->r4fp) {
5479                                 ARM_FSTS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
5480                         } else {
5481                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5482                                 ARM_CVTD (code, vfp_scratch1, ins->sreg1);
5483                                 ARM_FSTS (code, vfp_scratch1, ins->inst_destbasereg, ins->inst_offset);
5484                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5485                         }
5486                         break;
5487                 case OP_LOADR4_MEMBASE:
5488                         if (cfg->r4fp) {
5489                                 ARM_FLDS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5490                         } else {
5491                                 g_assert (arm_is_fpimm8 (ins->inst_offset));
5492                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5493                                 ARM_FLDS (code, vfp_scratch1, ins->inst_basereg, ins->inst_offset);
5494                                 ARM_CVTS (code, ins->dreg, vfp_scratch1);
5495                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5496                         }
5497                         break;
5498                 case OP_ICONV_TO_R_UN: {
5499                         g_assert_not_reached ();
5500                         break;
5501                 }
5502                 case OP_ICONV_TO_R4:
5503                         if (cfg->r4fp) {
5504                                 ARM_FMSR (code, ins->dreg, ins->sreg1);
5505                                 ARM_FSITOS (code, ins->dreg, ins->dreg);
5506                         } else {
5507                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5508                                 ARM_FMSR (code, vfp_scratch1, ins->sreg1);
5509                                 ARM_FSITOS (code, vfp_scratch1, vfp_scratch1);
5510                                 ARM_CVTS (code, ins->dreg, vfp_scratch1);
5511                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5512                         }
5513                         break;
5514                 case OP_ICONV_TO_R8:
5515                         code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5516                         ARM_FMSR (code, vfp_scratch1, ins->sreg1);
5517                         ARM_FSITOD (code, ins->dreg, vfp_scratch1);
5518                         code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5519                         break;
5520
5521                 case OP_SETFRET: {
5522                         MonoType *sig_ret = mini_get_underlying_type (mono_method_signature (cfg->method)->ret);
5523                         if (sig_ret->type == MONO_TYPE_R4) {
5524                                 if (cfg->r4fp) {
5525                                         if (IS_HARD_FLOAT) {
5526                                                 if (ins->sreg1 != ARM_VFP_D0)
5527                                                         ARM_CPYS (code, ARM_VFP_D0, ins->sreg1);
5528                                         } else {
5529                                                 ARM_FMRS (code, ARMREG_R0, ins->sreg1);
5530                                         }
5531                                 } else {
5532                                         ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
5533
5534                                         if (!IS_HARD_FLOAT)
5535                                                 ARM_FMRS (code, ARMREG_R0, ARM_VFP_F0);
5536                                 }
5537                         } else {
5538                                 if (IS_HARD_FLOAT)
5539                                         ARM_CPYD (code, ARM_VFP_D0, ins->sreg1);
5540                                 else
5541                                         ARM_FMRRD (code, ARMREG_R0, ARMREG_R1, ins->sreg1);
5542                         }
5543                         break;
5544                 }
5545                 case OP_FCONV_TO_I1:
5546                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5547                         break;
5548                 case OP_FCONV_TO_U1:
5549                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5550                         break;
5551                 case OP_FCONV_TO_I2:
5552                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5553                         break;
5554                 case OP_FCONV_TO_U2:
5555                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5556                         break;
5557                 case OP_FCONV_TO_I4:
5558                 case OP_FCONV_TO_I:
5559                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5560                         break;
5561                 case OP_FCONV_TO_U4:
5562                 case OP_FCONV_TO_U:
5563                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5564                         break;
5565                 case OP_FCONV_TO_I8:
5566                 case OP_FCONV_TO_U8:
5567                         g_assert_not_reached ();
5568                         /* Implemented as helper calls */
5569                         break;
5570                 case OP_LCONV_TO_R_UN:
5571                         g_assert_not_reached ();
5572                         /* Implemented as helper calls */
5573                         break;
5574                 case OP_LCONV_TO_OVF_I4_2: {
5575                         guint8 *high_bit_not_set, *valid_negative, *invalid_negative, *valid_positive;
5576                         /* 
5577                          * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
5578                          */
5579
5580                         ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
5581                         high_bit_not_set = code;
5582                         ARM_B_COND (code, ARMCOND_GE, 0); /*branch if bit 31 of the lower part is not set*/
5583
5584                         ARM_CMN_REG_IMM8 (code, ins->sreg2, 1); /*This have the same effect as CMP reg, 0xFFFFFFFF */
5585                         valid_negative = code;
5586                         ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0xFFFFFFFF (lower part has bit 31 set) */
5587                         invalid_negative = code;
5588                         ARM_B_COND (code, ARMCOND_AL, 0);
5589                         
5590                         arm_patch (high_bit_not_set, code);
5591
5592                         ARM_CMP_REG_IMM8 (code, ins->sreg2, 0);
5593                         valid_positive = code;
5594                         ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0 (lower part has bit 31 clear)*/
5595
5596                         arm_patch (invalid_negative, code);
5597                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_AL, "OverflowException");
5598
5599                         arm_patch (valid_negative, code);
5600                         arm_patch (valid_positive, code);
5601
5602                         if (ins->dreg != ins->sreg1)
5603                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
5604                         break;
5605                 }
5606                 case OP_FADD:
5607                         ARM_VFP_ADDD (code, ins->dreg, ins->sreg1, ins->sreg2);
5608                         break;
5609                 case OP_FSUB:
5610                         ARM_VFP_SUBD (code, ins->dreg, ins->sreg1, ins->sreg2);
5611                         break;          
5612                 case OP_FMUL:
5613                         ARM_VFP_MULD (code, ins->dreg, ins->sreg1, ins->sreg2);
5614                         break;          
5615                 case OP_FDIV:
5616                         ARM_VFP_DIVD (code, ins->dreg, ins->sreg1, ins->sreg2);
5617                         break;          
5618                 case OP_FNEG:
5619                         ARM_NEGD (code, ins->dreg, ins->sreg1);
5620                         break;
5621                 case OP_FREM:
5622                         /* emulated */
5623                         g_assert_not_reached ();
5624                         break;
5625                 case OP_FCOMPARE:
5626                         if (IS_VFP) {
5627                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5628                                 ARM_FMSTAT (code);
5629                         }
5630                         break;
5631                 case OP_RCOMPARE:
5632                         g_assert (IS_VFP);
5633                         ARM_CMPS (code, ins->sreg1, ins->sreg2);
5634                         ARM_FMSTAT (code);
5635                         break;
5636                 case OP_FCEQ:
5637                         if (IS_VFP) {
5638                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5639                                 ARM_FMSTAT (code);
5640                         }
5641                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
5642                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
5643                         break;
5644                 case OP_FCLT:
5645                         if (IS_VFP) {
5646                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5647                                 ARM_FMSTAT (code);
5648                         }
5649                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5650                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5651                         break;
5652                 case OP_FCLT_UN:
5653                         if (IS_VFP) {
5654                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5655                                 ARM_FMSTAT (code);
5656                         }
5657                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5658                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5659                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
5660                         break;
5661                 case OP_FCGT:
5662                         if (IS_VFP) {
5663                                 ARM_CMPD (code, ins->sreg2, ins->sreg1);
5664                                 ARM_FMSTAT (code);
5665                         }
5666                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5667                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5668                         break;
5669                 case OP_FCGT_UN:
5670                         if (IS_VFP) {
5671                                 ARM_CMPD (code, ins->sreg2, ins->sreg1);
5672                                 ARM_FMSTAT (code);
5673                         }
5674                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5675                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5676                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
5677                         break;
5678                 case OP_FCNEQ:
5679                         if (IS_VFP) {
5680                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5681                                 ARM_FMSTAT (code);
5682                         }
5683                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_NE);
5684                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_EQ);
5685                         break;
5686                 case OP_FCGE:
5687                         if (IS_VFP) {
5688                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5689                                 ARM_FMSTAT (code);
5690                         }
5691                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5692                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_MI);
5693                         break;
5694                 case OP_FCLE:
5695                         if (IS_VFP) {
5696                                 ARM_CMPD (code, ins->sreg2, ins->sreg1);
5697                                 ARM_FMSTAT (code);
5698                         }
5699                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5700                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_MI);
5701                         break;
5702
5703                 /* ARM FPA flags table:
5704                  * N        Less than               ARMCOND_MI
5705                  * Z        Equal                   ARMCOND_EQ
5706                  * C        Greater Than or Equal   ARMCOND_CS
5707                  * V        Unordered               ARMCOND_VS
5708                  */
5709                 case OP_FBEQ:
5710                         EMIT_COND_BRANCH (ins, OP_IBEQ - OP_IBEQ);
5711                         break;
5712                 case OP_FBNE_UN:
5713                         EMIT_COND_BRANCH (ins, OP_IBNE_UN - OP_IBEQ);
5714                         break;
5715                 case OP_FBLT:
5716                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
5717                         break;
5718                 case OP_FBLT_UN:
5719                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
5720                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
5721                         break;
5722                 case OP_FBGT:
5723                 case OP_FBGT_UN:
5724                 case OP_FBLE:
5725                 case OP_FBLE_UN:
5726                         g_assert_not_reached ();
5727                         break;
5728                 case OP_FBGE:
5729                         if (IS_VFP) {
5730                                 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
5731                         } else {
5732                                 /* FPA requires EQ even thou the docs suggests that just CS is enough */
5733                                 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_EQ);
5734                                 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS);
5735                         }
5736                         break;
5737                 case OP_FBGE_UN:
5738                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
5739                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
5740                         break;
5741
5742                 case OP_CKFINITE: {
5743                         if (IS_VFP) {
5744                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5745                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch2);
5746
5747                                 ARM_ABSD (code, vfp_scratch2, ins->sreg1);
5748                                 ARM_FLDD (code, vfp_scratch1, ARMREG_PC, 0);
5749                                 ARM_B (code, 1);
5750                                 *(guint32*)code = 0xffffffff;
5751                                 code += 4;
5752                                 *(guint32*)code = 0x7fefffff;
5753                                 code += 4;
5754                                 ARM_CMPD (code, vfp_scratch2, vfp_scratch1);
5755                                 ARM_FMSTAT (code);
5756                                 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_GT, "OverflowException");
5757                                 ARM_CMPD (code, ins->sreg1, ins->sreg1);
5758                                 ARM_FMSTAT (code);
5759                                 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, "OverflowException");
5760                                 ARM_CPYD (code, ins->dreg, ins->sreg1);
5761
5762                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5763                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch2);
5764                         }
5765                         break;
5766                 }
5767
5768                 case OP_RCONV_TO_I1:
5769                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5770                         break;
5771                 case OP_RCONV_TO_U1:
5772                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5773                         break;
5774                 case OP_RCONV_TO_I2:
5775                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5776                         break;
5777                 case OP_RCONV_TO_U2:
5778                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5779                         break;
5780                 case OP_RCONV_TO_I4:
5781                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5782                         break;
5783                 case OP_RCONV_TO_U4:
5784                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5785                         break;
5786                 case OP_RCONV_TO_R4:
5787                         g_assert (IS_VFP);
5788                         if (ins->dreg != ins->sreg1)
5789                                 ARM_CPYS (code, ins->dreg, ins->sreg1);
5790                         break;
5791                 case OP_RCONV_TO_R8:
5792                         g_assert (IS_VFP);
5793                         ARM_CVTS (code, ins->dreg, ins->sreg1);
5794                         break;
5795                 case OP_RADD:
5796                         ARM_VFP_ADDS (code, ins->dreg, ins->sreg1, ins->sreg2);
5797                         break;
5798                 case OP_RSUB:
5799                         ARM_VFP_SUBS (code, ins->dreg, ins->sreg1, ins->sreg2);
5800                         break;          
5801                 case OP_RMUL:
5802                         ARM_VFP_MULS (code, ins->dreg, ins->sreg1, ins->sreg2);
5803                         break;          
5804                 case OP_RDIV:
5805                         ARM_VFP_DIVS (code, ins->dreg, ins->sreg1, ins->sreg2);
5806                         break;          
5807                 case OP_RNEG:
5808                         ARM_NEGS (code, ins->dreg, ins->sreg1);
5809                         break;
5810                 case OP_RCEQ:
5811                         if (IS_VFP) {
5812                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5813                                 ARM_FMSTAT (code);
5814                         }
5815                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
5816                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
5817                         break;
5818                 case OP_RCLT:
5819                         if (IS_VFP) {
5820                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5821                                 ARM_FMSTAT (code);
5822                         }
5823                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5824                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5825                         break;
5826                 case OP_RCLT_UN:
5827                         if (IS_VFP) {
5828                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5829                                 ARM_FMSTAT (code);
5830                         }
5831                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5832                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5833                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
5834                         break;
5835                 case OP_RCGT:
5836                         if (IS_VFP) {
5837                                 ARM_CMPS (code, ins->sreg2, ins->sreg1);
5838                                 ARM_FMSTAT (code);
5839                         }
5840                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5841                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5842                         break;
5843                 case OP_RCGT_UN:
5844                         if (IS_VFP) {
5845                                 ARM_CMPS (code, ins->sreg2, ins->sreg1);
5846                                 ARM_FMSTAT (code);
5847                         }
5848                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5849                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5850                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
5851                         break;
5852                 case OP_RCNEQ:
5853                         if (IS_VFP) {
5854                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5855                                 ARM_FMSTAT (code);
5856                         }
5857                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_NE);
5858                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_EQ);
5859                         break;
5860                 case OP_RCGE:
5861                         if (IS_VFP) {
5862                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5863                                 ARM_FMSTAT (code);
5864                         }
5865                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5866                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_MI);
5867                         break;
5868                 case OP_RCLE:
5869                         if (IS_VFP) {
5870                                 ARM_CMPS (code, ins->sreg2, ins->sreg1);
5871                                 ARM_FMSTAT (code);
5872                         }
5873                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5874                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_MI);
5875                         break;
5876
5877                 case OP_GC_LIVENESS_DEF:
5878                 case OP_GC_LIVENESS_USE:
5879                 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
5880                         ins->backend.pc_offset = code - cfg->native_code;
5881                         break;
5882                 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
5883                         ins->backend.pc_offset = code - cfg->native_code;
5884                         bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
5885                         break;
5886                 case OP_GC_SAFE_POINT: {
5887                         guint8 *buf [1];
5888
5889                         g_assert (mono_threads_is_coop_enabled ());
5890
5891                         ARM_LDR_IMM (code, ARMREG_IP, ins->sreg1, 0);
5892                         ARM_CMP_REG_IMM (code, ARMREG_IP, 0, 0);
5893                         buf [0] = code;
5894                         ARM_B_COND (code, ARMCOND_EQ, 0);
5895                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_threads_state_poll");
5896                         code = emit_call_seq (cfg, code);
5897                         arm_patch (buf [0], code);
5898                         break;
5899                 }
5900
5901                 default:
5902                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
5903                         g_assert_not_reached ();
5904                 }
5905
5906                 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
5907                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
5908                                    mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
5909                         g_assert_not_reached ();
5910                 }
5911                
5912                 cpos += max_len;
5913
5914                 last_ins = ins;
5915                 last_offset = offset;
5916         }
5917
5918         cfg->code_len = code - cfg->native_code;
5919 }
5920
5921 #endif /* DISABLE_JIT */
5922
5923 void
5924 mono_arch_register_lowlevel_calls (void)
5925 {
5926         /* The signature doesn't matter */
5927         mono_register_jit_icall (mono_arm_throw_exception, "mono_arm_throw_exception", mono_create_icall_signature ("void"), TRUE);
5928         mono_register_jit_icall (mono_arm_throw_exception_by_token, "mono_arm_throw_exception_by_token", mono_create_icall_signature ("void"), TRUE);
5929         mono_register_jit_icall (mono_arm_unaligned_stack, "mono_arm_unaligned_stack", mono_create_icall_signature ("void"), TRUE);
5930 }
5931
5932 #define patch_lis_ori(ip,val) do {\
5933                 guint16 *__lis_ori = (guint16*)(ip);    \
5934                 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff;      \
5935                 __lis_ori [3] = ((guint32)(val)) & 0xffff;      \
5936         } while (0)
5937
5938 void
5939 mono_arch_patch_code_new (MonoCompile *cfg, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gpointer target)
5940 {
5941         unsigned char *ip = ji->ip.i + code;
5942
5943         if (ji->type == MONO_PATCH_INFO_SWITCH) {
5944         }
5945
5946         switch (ji->type) {
5947         case MONO_PATCH_INFO_SWITCH: {
5948                 gpointer *jt = (gpointer*)(ip + 8);
5949                 int i;
5950                 /* jt is the inlined jump table, 2 instructions after ip
5951                  * In the normal case we store the absolute addresses,
5952                  * otherwise the displacements.
5953                  */
5954                 for (i = 0; i < ji->data.table->table_size; i++)
5955                         jt [i] = code + (int)ji->data.table->table [i];
5956                 break;
5957         }
5958         case MONO_PATCH_INFO_IP:
5959                 g_assert_not_reached ();
5960                 patch_lis_ori (ip, ip);
5961                 break;
5962         case MONO_PATCH_INFO_METHOD_REL:
5963                 g_assert_not_reached ();
5964                 *((gpointer *)(ip)) = target;
5965                 break;
5966         case MONO_PATCH_INFO_METHODCONST:
5967         case MONO_PATCH_INFO_CLASS:
5968         case MONO_PATCH_INFO_IMAGE:
5969         case MONO_PATCH_INFO_FIELD:
5970         case MONO_PATCH_INFO_VTABLE:
5971         case MONO_PATCH_INFO_IID:
5972         case MONO_PATCH_INFO_SFLDA:
5973         case MONO_PATCH_INFO_LDSTR:
5974         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
5975         case MONO_PATCH_INFO_LDTOKEN:
5976                 g_assert_not_reached ();
5977                 /* from OP_AOTCONST : lis + ori */
5978                 patch_lis_ori (ip, target);
5979                 break;
5980         case MONO_PATCH_INFO_R4:
5981         case MONO_PATCH_INFO_R8:
5982                 g_assert_not_reached ();
5983                 *((gconstpointer *)(ip + 2)) = target;
5984                 break;
5985         case MONO_PATCH_INFO_EXC_NAME:
5986                 g_assert_not_reached ();
5987                 *((gconstpointer *)(ip + 1)) = target;
5988                 break;
5989         case MONO_PATCH_INFO_NONE:
5990         case MONO_PATCH_INFO_BB_OVF:
5991         case MONO_PATCH_INFO_EXC_OVF:
5992                 /* everything is dealt with at epilog output time */
5993                 break;
5994         default:
5995                 arm_patch_general (cfg, domain, ip, target);
5996                 break;
5997         }
5998 }
5999
6000 void
6001 mono_arm_unaligned_stack (MonoMethod *method)
6002 {
6003         g_assert_not_reached ();
6004 }
6005
6006 #ifndef DISABLE_JIT
6007
6008 /*
6009  * Stack frame layout:
6010  * 
6011  *   ------------------- fp
6012  *      MonoLMF structure or saved registers
6013  *   -------------------
6014  *      locals
6015  *   -------------------
6016  *      spilled regs
6017  *   -------------------
6018  *      optional 8 bytes for tracing
6019  *   -------------------
6020  *      param area             size is cfg->param_area
6021  *   ------------------- sp
6022  */
6023 guint8 *
6024 mono_arch_emit_prolog (MonoCompile *cfg)
6025 {
6026         MonoMethod *method = cfg->method;
6027         MonoBasicBlock *bb;
6028         MonoMethodSignature *sig;
6029         MonoInst *inst;
6030         int alloc_size, orig_alloc_size, pos, max_offset, i, rot_amount, part;
6031         guint8 *code;
6032         CallInfo *cinfo;
6033         int tracing = 0;
6034         int lmf_offset = 0;
6035         int prev_sp_offset, reg_offset;
6036
6037         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6038                 tracing = 1;
6039
6040         sig = mono_method_signature (method);
6041         cfg->code_size = 256 + sig->param_count * 64;
6042         code = cfg->native_code = g_malloc (cfg->code_size);
6043
6044         mono_emit_unwind_op_def_cfa (cfg, code, ARMREG_SP, 0);
6045
6046         alloc_size = cfg->stack_offset;
6047         pos = 0;
6048         prev_sp_offset = 0;
6049
6050         if (iphone_abi) {
6051                 /* 
6052                  * The iphone uses R7 as the frame pointer, and it points at the saved
6053                  * r7+lr:
6054                  *         <lr>
6055                  * r7 ->   <r7>
6056                  *         <rest of frame>
6057                  * We can't use r7 as a frame pointer since it points into the middle of
6058                  * the frame, so we keep using our own frame pointer.
6059                  * FIXME: Optimize this.
6060                  */
6061                 ARM_PUSH (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
6062                 prev_sp_offset += 8; /* r7 and lr */
6063                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
6064                 mono_emit_unwind_op_offset (cfg, code, ARMREG_R7, (- prev_sp_offset) + 0);
6065                 ARM_MOV_REG_REG (code, ARMREG_R7, ARMREG_SP);
6066         }
6067
6068         if (!method->save_lmf) {
6069                 if (iphone_abi) {
6070                         /* No need to push LR again */
6071                         if (cfg->used_int_regs)
6072                                 ARM_PUSH (code, cfg->used_int_regs);
6073                 } else {
6074                         ARM_PUSH (code, cfg->used_int_regs | (1 << ARMREG_LR));
6075                         prev_sp_offset += 4;
6076                 }
6077                 for (i = 0; i < 16; ++i) {
6078                         if (cfg->used_int_regs & (1 << i))
6079                                 prev_sp_offset += 4;
6080                 }
6081                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
6082                 reg_offset = 0;
6083                 for (i = 0; i < 16; ++i) {
6084                         if ((cfg->used_int_regs & (1 << i))) {
6085                                 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
6086                                 mini_gc_set_slot_type_from_cfa (cfg, (- prev_sp_offset) + reg_offset, SLOT_NOREF);
6087                                 reg_offset += 4;
6088                         }
6089                 }
6090                 mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
6091                 mini_gc_set_slot_type_from_cfa (cfg, -4, SLOT_NOREF);
6092         } else {
6093                 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
6094                 ARM_PUSH (code, 0x5ff0);
6095                 prev_sp_offset += 4 * 10; /* all but r0-r3, sp and pc */
6096                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
6097                 reg_offset = 0;
6098                 for (i = 0; i < 16; ++i) {
6099                         if ((i > ARMREG_R3) && (i != ARMREG_SP) && (i != ARMREG_PC)) {
6100                                 /* The original r7 is saved at the start */
6101                                 if (!(iphone_abi && i == ARMREG_R7))
6102                                         mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
6103                                 reg_offset += 4;
6104                         }
6105                 }
6106                 g_assert (reg_offset == 4 * 10);
6107                 pos += sizeof (MonoLMF) - (4 * 10);
6108                 lmf_offset = pos;
6109         }
6110         alloc_size += pos;
6111         orig_alloc_size = alloc_size;
6112         // align to MONO_ARCH_FRAME_ALIGNMENT bytes
6113         if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
6114                 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
6115                 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
6116         }
6117
6118         /* the stack used in the pushed regs */
6119         alloc_size += ALIGN_TO (prev_sp_offset, MONO_ARCH_FRAME_ALIGNMENT) - prev_sp_offset;
6120         cfg->stack_usage = alloc_size;
6121         if (alloc_size) {
6122                 if ((i = mono_arm_is_rotated_imm8 (alloc_size, &rot_amount)) >= 0) {
6123                         ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
6124                 } else {
6125                         code = mono_arm_emit_load_imm (code, ARMREG_IP, alloc_size);
6126                         ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
6127                 }
6128                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset + alloc_size);
6129         }
6130         if (cfg->frame_reg != ARMREG_SP) {
6131                 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
6132                 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
6133         }
6134         //g_print ("prev_sp_offset: %d, alloc_size:%d\n", prev_sp_offset, alloc_size);
6135         prev_sp_offset += alloc_size;
6136
6137         for (i = 0; i < alloc_size - orig_alloc_size; i += 4)
6138                 mini_gc_set_slot_type_from_cfa (cfg, (- prev_sp_offset) + orig_alloc_size + i, SLOT_NOREF);
6139
6140         /* compute max_offset in order to use short forward jumps
6141          * we could skip do it on arm because the immediate displacement
6142          * for jumps is large enough, it may be useful later for constant pools
6143          */
6144         max_offset = 0;
6145         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6146                 MonoInst *ins = bb->code;
6147                 bb->max_offset = max_offset;
6148
6149                 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6150                         max_offset += 6; 
6151
6152                 MONO_BB_FOR_EACH_INS (bb, ins)
6153                         max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
6154         }
6155
6156         /* stack alignment check */
6157         /*
6158         {
6159                 guint8 *buf [16];
6160                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_SP);
6161                 code = mono_arm_emit_load_imm (code, ARMREG_IP, MONO_ARCH_FRAME_ALIGNMENT -1);
6162                 ARM_AND_REG_REG (code, ARMREG_LR, ARMREG_LR, ARMREG_IP);
6163                 ARM_CMP_REG_IMM (code, ARMREG_LR, 0, 0);
6164                 buf [0] = code;
6165                 ARM_B_COND (code, ARMCOND_EQ, 0);
6166                 if (cfg->compile_aot)
6167                         ARM_MOV_REG_IMM8 (code, ARMREG_R0, 0);
6168                 else
6169                         code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
6170                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, "mono_arm_unaligned_stack");
6171                 code = emit_call_seq (cfg, code);
6172                 arm_patch (buf [0], code);
6173         }
6174         */
6175
6176         /* store runtime generic context */
6177         if (cfg->rgctx_var) {
6178                 MonoInst *ins = cfg->rgctx_var;
6179
6180                 g_assert (ins->opcode == OP_REGOFFSET);
6181
6182                 if (arm_is_imm12 (ins->inst_offset)) {
6183                         ARM_STR_IMM (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ins->inst_offset);
6184                 } else {
6185                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
6186                         ARM_STR_REG_REG (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ARMREG_LR);
6187                 }
6188         }
6189
6190         /* load arguments allocated to register from the stack */
6191         pos = 0;
6192
6193         cinfo = get_call_info (NULL, sig);
6194
6195         if (cinfo->ret.storage == RegTypeStructByAddr) {
6196                 ArgInfo *ainfo = &cinfo->ret;
6197                 inst = cfg->vret_addr;
6198                 g_assert (arm_is_imm12 (inst->inst_offset));
6199                 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6200         }
6201
6202         if (sig->call_convention == MONO_CALL_VARARG) {
6203                 ArgInfo *cookie = &cinfo->sig_cookie;
6204
6205                 /* Save the sig cookie address */
6206                 g_assert (cookie->storage == RegTypeBase);
6207
6208                 g_assert (arm_is_imm12 (prev_sp_offset + cookie->offset));
6209                 g_assert (arm_is_imm12 (cfg->sig_cookie));
6210                 ARM_ADD_REG_IMM8 (code, ARMREG_IP, cfg->frame_reg, prev_sp_offset + cookie->offset);
6211                 ARM_STR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
6212         }
6213
6214         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6215                 ArgInfo *ainfo = cinfo->args + i;
6216                 inst = cfg->args [pos];
6217                 
6218                 if (cfg->verbose_level > 2)
6219                         g_print ("Saving argument %d (type: %d)\n", i, ainfo->storage);
6220
6221                 if (inst->opcode == OP_REGVAR) {
6222                         if (ainfo->storage == RegTypeGeneral)
6223                                 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
6224                         else if (ainfo->storage == RegTypeFP) {
6225                                 g_assert_not_reached ();
6226                         } else if (ainfo->storage == RegTypeBase) {
6227                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
6228                                         ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
6229                                 } else {
6230                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
6231                                         ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
6232                                 }
6233                         } else
6234                                 g_assert_not_reached ();
6235
6236                         if (cfg->verbose_level > 2)
6237                                 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
6238                 } else {
6239                         switch (ainfo->storage) {
6240                         case RegTypeHFA:
6241                                 for (part = 0; part < ainfo->nregs; part ++) {
6242                                         if (ainfo->esize == 4)
6243                                                 ARM_FSTS (code, ainfo->reg + part, inst->inst_basereg, inst->inst_offset + (part * ainfo->esize));
6244                                         else
6245                                                 ARM_FSTD (code, ainfo->reg + (part * 2), inst->inst_basereg, inst->inst_offset + (part * ainfo->esize));
6246                                 }
6247                                 break;
6248                         case RegTypeGeneral:
6249                         case RegTypeIRegPair:
6250                         case RegTypeGSharedVtInReg:
6251                         case RegTypeStructByAddr:
6252                                 switch (ainfo->size) {
6253                                 case 1:
6254                                         if (arm_is_imm12 (inst->inst_offset))
6255                                                 ARM_STRB_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6256                                         else {
6257                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6258                                                 ARM_STRB_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
6259                                         }
6260                                         break;
6261                                 case 2:
6262                                         if (arm_is_imm8 (inst->inst_offset)) {
6263                                                 ARM_STRH_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6264                                         } else {
6265                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6266                                                 ARM_STRH_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
6267                                         }
6268                                         break;
6269                                 case 8:
6270                                         if (arm_is_imm12 (inst->inst_offset)) {
6271                                                 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6272                                         } else {
6273                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6274                                                 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
6275                                         }
6276                                         if (arm_is_imm12 (inst->inst_offset + 4)) {
6277                                                 ARM_STR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
6278                                         } else {
6279                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
6280                                                 ARM_STR_REG_REG (code, ainfo->reg + 1, inst->inst_basereg, ARMREG_IP);
6281                                         }
6282                                         break;
6283                                 default:
6284                                         if (arm_is_imm12 (inst->inst_offset)) {
6285                                                 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6286                                         } else {
6287                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6288                                                 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
6289                                         }
6290                                         break;
6291                                 }
6292                                 break;
6293                         case RegTypeBaseGen:
6294                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
6295                                         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
6296                                 } else {
6297                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
6298                                         ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
6299                                 }
6300                                 if (arm_is_imm12 (inst->inst_offset + 4)) {
6301                                         ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
6302                                         ARM_STR_IMM (code, ARMREG_R3, inst->inst_basereg, inst->inst_offset);
6303                                 } else {
6304                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
6305                                         ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6306                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6307                                         ARM_STR_REG_REG (code, ARMREG_R3, inst->inst_basereg, ARMREG_IP);
6308                                 }
6309                                 break;
6310                         case RegTypeBase:
6311                         case RegTypeGSharedVtOnStack:
6312                         case RegTypeStructByAddrOnStack:
6313                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
6314                                         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
6315                                 } else {
6316                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
6317                                         ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
6318                                 }
6319
6320                                 switch (ainfo->size) {
6321                                 case 1:
6322                                         if (arm_is_imm8 (inst->inst_offset)) {
6323                                                 ARM_STRB_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
6324                                         } else {
6325                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6326                                                 ARM_STRB_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6327                                         }
6328                                         break;
6329                                 case 2:
6330                                         if (arm_is_imm8 (inst->inst_offset)) {
6331                                                 ARM_STRH_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
6332                                         } else {
6333                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6334                                                 ARM_STRH_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6335                                         }
6336                                         break;
6337                                 case 8:
6338                                         if (arm_is_imm12 (inst->inst_offset)) {
6339                                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
6340                                         } else {
6341                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6342                                                 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6343                                         }
6344                                         if (arm_is_imm12 (prev_sp_offset + ainfo->offset + 4)) {
6345                                                 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset + 4));
6346                                         } else {
6347                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset + 4);
6348                                                 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
6349                                         }
6350                                         if (arm_is_imm12 (inst->inst_offset + 4)) {
6351                                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
6352                                         } else {
6353                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
6354                                                 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6355                                         }
6356                                         break;
6357                                 default:
6358                                         if (arm_is_imm12 (inst->inst_offset)) {
6359                                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
6360                                         } else {
6361                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6362                                                 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6363                                         }
6364                                         break;
6365                                 }
6366                                 break;
6367                         case RegTypeFP: {
6368                                 int imm8, rot_amount;
6369
6370                                 if ((imm8 = mono_arm_is_rotated_imm8 (inst->inst_offset, &rot_amount)) == -1) {
6371                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6372                                         ARM_ADD_REG_REG (code, ARMREG_IP, ARMREG_IP, inst->inst_basereg);
6373                                 } else
6374                                         ARM_ADD_REG_IMM (code, ARMREG_IP, inst->inst_basereg, imm8, rot_amount);
6375
6376                                 if (ainfo->size == 8)
6377                                         ARM_FSTD (code, ainfo->reg, ARMREG_IP, 0);
6378                                 else
6379                                         ARM_FSTS (code, ainfo->reg, ARMREG_IP, 0);
6380                                 break;
6381                         }
6382                         case RegTypeStructByVal: {
6383                                 int doffset = inst->inst_offset;
6384                                 int soffset = 0;
6385                                 int cur_reg;
6386                                 int size = 0;
6387                                 size = mini_type_stack_size_full (inst->inst_vtype, NULL, sig->pinvoke);
6388                                 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
6389                                         if (arm_is_imm12 (doffset)) {
6390                                                 ARM_STR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
6391                                         } else {
6392                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
6393                                                 ARM_STR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
6394                                         }
6395                                         soffset += sizeof (gpointer);
6396                                         doffset += sizeof (gpointer);
6397                                 }
6398                                 if (ainfo->vtsize) {
6399                                         /* FIXME: handle overrun! with struct sizes not multiple of 4 */
6400                                         //g_print ("emit_memcpy (prev_sp_ofs: %d, ainfo->offset: %d, soffset: %d)\n", prev_sp_offset, ainfo->offset, soffset);
6401                                         code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ARMREG_SP, prev_sp_offset + ainfo->offset);
6402                                 }
6403                                 break;
6404                         }
6405                         default:
6406                                 g_assert_not_reached ();
6407                                 break;
6408                         }
6409                 }
6410                 pos++;
6411         }
6412
6413         if (method->save_lmf)
6414                 code = emit_save_lmf (cfg, code, alloc_size - lmf_offset);
6415
6416         if (tracing)
6417                 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
6418
6419         if (cfg->arch.seq_point_info_var) {
6420                 MonoInst *ins = cfg->arch.seq_point_info_var;
6421
6422                 /* Initialize the variable from a GOT slot */
6423                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
6424                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
6425                 ARM_B (code, 0);
6426                 *(gpointer*)code = NULL;
6427                 code += 4;
6428                 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
6429
6430                 g_assert (ins->opcode == OP_REGOFFSET);
6431
6432                 if (arm_is_imm12 (ins->inst_offset)) {
6433                         ARM_STR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
6434                 } else {
6435                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
6436                         ARM_STR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
6437                 }
6438         }
6439
6440         /* Initialize ss_trigger_page_var */
6441         if (!cfg->soft_breakpoints) {
6442                 MonoInst *info_var = cfg->arch.seq_point_info_var;
6443                 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
6444                 int dreg = ARMREG_LR;
6445
6446                 if (info_var) {
6447                         g_assert (info_var->opcode == OP_REGOFFSET);
6448                         g_assert (arm_is_imm12 (info_var->inst_offset));
6449
6450                         ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
6451                         /* Load the trigger page addr */
6452                         ARM_LDR_IMM (code, dreg, dreg, MONO_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page));
6453                         ARM_STR_IMM (code, dreg, ss_trigger_page_var->inst_basereg, ss_trigger_page_var->inst_offset);
6454                 }
6455         }
6456
6457         if (cfg->arch.seq_point_ss_method_var) {
6458                 MonoInst *ss_method_ins = cfg->arch.seq_point_ss_method_var;
6459                 MonoInst *bp_method_ins = cfg->arch.seq_point_bp_method_var;
6460
6461                 g_assert (ss_method_ins->opcode == OP_REGOFFSET);
6462                 g_assert (arm_is_imm12 (ss_method_ins->inst_offset));
6463
6464                 if (cfg->compile_aot) {
6465                         MonoInst *info_var = cfg->arch.seq_point_info_var;
6466                         int dreg = ARMREG_LR;
6467
6468                         g_assert (info_var->opcode == OP_REGOFFSET);
6469                         g_assert (arm_is_imm12 (info_var->inst_offset));
6470
6471                         ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
6472                         ARM_LDR_IMM (code, dreg, dreg, MONO_STRUCT_OFFSET (SeqPointInfo, ss_tramp_addr));
6473                         ARM_STR_IMM (code, dreg, ss_method_ins->inst_basereg, ss_method_ins->inst_offset);
6474                 } else {
6475                         g_assert (bp_method_ins->opcode == OP_REGOFFSET);
6476                         g_assert (arm_is_imm12 (bp_method_ins->inst_offset));
6477
6478                         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
6479                         ARM_B (code, 1);
6480                         *(gpointer*)code = &single_step_tramp;
6481                         code += 4;
6482                         *(gpointer*)code = breakpoint_tramp;
6483                         code += 4;
6484
6485                         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 0);
6486                         ARM_STR_IMM (code, ARMREG_IP, ss_method_ins->inst_basereg, ss_method_ins->inst_offset);
6487                         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 4);
6488                         ARM_STR_IMM (code, ARMREG_IP, bp_method_ins->inst_basereg, bp_method_ins->inst_offset);
6489                 }
6490         }
6491
6492         cfg->code_len = code - cfg->native_code;
6493         g_assert (cfg->code_len < cfg->code_size);
6494         g_free (cinfo);
6495
6496         return code;
6497 }
6498
6499 void
6500 mono_arch_emit_epilog (MonoCompile *cfg)
6501 {
6502         MonoMethod *method = cfg->method;
6503         int pos, i, rot_amount;
6504         int max_epilog_size = 16 + 20*4;
6505         guint8 *code;
6506         CallInfo *cinfo;
6507
6508         if (cfg->method->save_lmf)
6509                 max_epilog_size += 128;
6510         
6511         if (mono_jit_trace_calls != NULL)
6512                 max_epilog_size += 50;
6513
6514         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6515                 max_epilog_size += 50;
6516
6517         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
6518                 cfg->code_size *= 2;
6519                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
6520                 cfg->stat_code_reallocs++;
6521         }
6522
6523         /*
6524          * Keep in sync with OP_JMP
6525          */
6526         code = cfg->native_code + cfg->code_len;
6527
6528         /* Save the uwind state which is needed by the out-of-line code */
6529         mono_emit_unwind_op_remember_state (cfg, code);
6530
6531         if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
6532                 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
6533         }
6534         pos = 0;
6535
6536         /* Load returned vtypes into registers if needed */
6537         cinfo = cfg->arch.cinfo;
6538         switch (cinfo->ret.storage) {
6539         case RegTypeStructByVal: {
6540                 MonoInst *ins = cfg->ret;
6541
6542                 if (cinfo->ret.nregs == 1) {
6543                         if (arm_is_imm12 (ins->inst_offset)) {
6544                                 ARM_LDR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
6545                         } else {
6546                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
6547                                 ARM_LDR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
6548                         }
6549                 } else {
6550                         for (i = 0; i < cinfo->ret.nregs; ++i) {
6551                                 int offset = ins->inst_offset + (i * 4);
6552                                 if (arm_is_imm12 (offset)) {
6553                                         ARM_LDR_IMM (code, i, ins->inst_basereg, offset);
6554                                 } else {
6555                                         code = mono_arm_emit_load_imm (code, ARMREG_LR, offset);
6556                                         ARM_LDR_REG_REG (code, i, ins->inst_basereg, ARMREG_LR);
6557                                 }
6558                         }
6559                 }
6560                 break;
6561         }
6562         case RegTypeHFA: {
6563                 MonoInst *ins = cfg->ret;
6564
6565                 for (i = 0; i < cinfo->ret.nregs; ++i) {
6566                         if (cinfo->ret.esize == 4)
6567                                 ARM_FLDS (code, cinfo->ret.reg + i, ins->inst_basereg, ins->inst_offset + (i * cinfo->ret.esize));
6568                         else
6569                                 ARM_FLDD (code, cinfo->ret.reg + (i * 2), ins->inst_basereg, ins->inst_offset + (i * cinfo->ret.esize));
6570                 }
6571                 break;
6572         }
6573         default:
6574                 break;
6575         }
6576
6577         if (method->save_lmf) {
6578                 int lmf_offset, reg, sp_adj, regmask, nused_int_regs = 0;
6579                 /* all but r0-r3, sp and pc */
6580                 pos += sizeof (MonoLMF) - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
6581                 lmf_offset = pos;
6582
6583                 code = emit_restore_lmf (cfg, code, cfg->stack_usage - lmf_offset);
6584
6585                 /* This points to r4 inside MonoLMF->iregs */
6586                 sp_adj = (sizeof (MonoLMF) - MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
6587                 reg = ARMREG_R4;
6588                 regmask = 0x9ff0; /* restore lr to pc */
6589                 /* Skip caller saved registers not used by the method */
6590                 while (!(cfg->used_int_regs & (1 << reg)) && reg < ARMREG_FP) {
6591                         regmask &= ~(1 << reg);
6592                         sp_adj += 4;
6593                         reg ++;
6594                 }
6595                 if (iphone_abi)
6596                         /* Restored later */
6597                         regmask &= ~(1 << ARMREG_PC);
6598                 /* point sp at the registers to restore: 10 is 14 -4, because we skip r0-r3 */
6599                 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage - lmf_offset + sp_adj);
6600                 for (i = 0; i < 16; i++) {
6601                         if (regmask & (1 << i))
6602                                 nused_int_regs ++;
6603                 }
6604                 mono_emit_unwind_op_def_cfa (cfg, code, ARMREG_SP, ((iphone_abi ? 3 : 0) + nused_int_regs) * 4);
6605                 /* restore iregs */
6606                 ARM_POP (code, regmask); 
6607                 if (iphone_abi) {
6608                         for (i = 0; i < 16; i++) {
6609                                 if (regmask & (1 << i))
6610                                         mono_emit_unwind_op_same_value (cfg, code, i);
6611                         }
6612                         /* Restore saved r7, restore LR to PC */
6613                         /* Skip lr from the lmf */
6614                         mono_emit_unwind_op_def_cfa_offset (cfg, code, 3 * 4);
6615                         ARM_ADD_REG_IMM (code, ARMREG_SP, ARMREG_SP, sizeof (gpointer), 0);
6616                         mono_emit_unwind_op_def_cfa_offset (cfg, code, 2 * 4);
6617                         ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_PC));
6618                 }
6619         } else {
6620                 int i, nused_int_regs = 0;
6621
6622                 for (i = 0; i < 16; i++) {
6623                         if (cfg->used_int_regs & (1 << i))
6624                                 nused_int_regs ++;
6625                 }
6626
6627                 if ((i = mono_arm_is_rotated_imm8 (cfg->stack_usage, &rot_amount)) >= 0) {
6628                         ARM_ADD_REG_IMM (code, ARMREG_SP, cfg->frame_reg, i, rot_amount);
6629                 } else {
6630                         code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->stack_usage);
6631                         ARM_ADD_REG_REG (code, ARMREG_SP, cfg->frame_reg, ARMREG_IP);
6632                 }
6633
6634                 if (cfg->frame_reg != ARMREG_SP) {
6635                         mono_emit_unwind_op_def_cfa_reg (cfg, code, ARMREG_SP);
6636                 }
6637
6638                 if (iphone_abi) {
6639                         /* Restore saved gregs */
6640                         if (cfg->used_int_regs) {
6641                                 mono_emit_unwind_op_def_cfa_offset (cfg, code, (2 + nused_int_regs) * 4);
6642                                 ARM_POP (code, cfg->used_int_regs);
6643                                 for (i = 0; i < 16; i++) {
6644                                         if (cfg->used_int_regs & (1 << i))
6645                                                 mono_emit_unwind_op_same_value (cfg, code, i);
6646                                 }
6647                         }
6648                         mono_emit_unwind_op_def_cfa_offset (cfg, code, 2 * 4);
6649                         /* Restore saved r7, restore LR to PC */
6650                         ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_PC));
6651                 } else {
6652                         mono_emit_unwind_op_def_cfa_offset (cfg, code, (nused_int_regs + 1) * 4);
6653                         ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_PC));
6654                 }
6655         }
6656
6657         /* Restore the unwind state to be the same as before the epilog */
6658         mono_emit_unwind_op_restore_state (cfg, code);
6659
6660         cfg->code_len = code - cfg->native_code;
6661
6662         g_assert (cfg->code_len < cfg->code_size);
6663
6664 }
6665
6666 void
6667 mono_arch_emit_exceptions (MonoCompile *cfg)
6668 {
6669         MonoJumpInfo *patch_info;
6670         int i;
6671         guint8 *code;
6672         guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM];
6673         guint8 exc_throw_found [MONO_EXC_INTRINS_NUM];
6674         int max_epilog_size = 50;
6675
6676         for (i = 0; i < MONO_EXC_INTRINS_NUM; i++) {
6677                 exc_throw_pos [i] = NULL;
6678                 exc_throw_found [i] = 0;
6679         }
6680
6681         /* count the number of exception infos */
6682      
6683         /* 
6684          * make sure we have enough space for exceptions
6685          */
6686         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6687                 if (patch_info->type == MONO_PATCH_INFO_EXC) {
6688                         i = mini_exception_id_by_name (patch_info->data.target);
6689                         if (!exc_throw_found [i]) {
6690                                 max_epilog_size += 32;
6691                                 exc_throw_found [i] = TRUE;
6692                         }
6693                 }
6694         }
6695
6696         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
6697                 cfg->code_size *= 2;
6698                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
6699                 cfg->stat_code_reallocs++;
6700         }
6701
6702         code = cfg->native_code + cfg->code_len;
6703
6704         /* add code to raise exceptions */
6705         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6706                 switch (patch_info->type) {
6707                 case MONO_PATCH_INFO_EXC: {
6708                         MonoClass *exc_class;
6709                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
6710
6711                         i = mini_exception_id_by_name (patch_info->data.target);
6712                         if (exc_throw_pos [i]) {
6713                                 arm_patch (ip, exc_throw_pos [i]);
6714                                 patch_info->type = MONO_PATCH_INFO_NONE;
6715                                 break;
6716                         } else {
6717                                 exc_throw_pos [i] = code;
6718                         }
6719                         arm_patch (ip, code);
6720
6721                         exc_class = mono_class_load_from_name (mono_defaults.corlib, "System", patch_info->data.name);
6722
6723                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR);
6724                         ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
6725                         patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6726                         patch_info->data.name = "mono_arch_throw_corlib_exception";
6727                         patch_info->ip.i = code - cfg->native_code;
6728                         ARM_BL (code, 0);
6729                         cfg->thunk_area += THUNK_SIZE;
6730                         *(guint32*)(gpointer)code = exc_class->type_token - MONO_TOKEN_TYPE_DEF;
6731                         code += 4;
6732                         break;
6733                 }
6734                 default:
6735                         /* do nothing */
6736                         break;
6737                 }
6738         }
6739
6740         cfg->code_len = code - cfg->native_code;
6741
6742         g_assert (cfg->code_len < cfg->code_size);
6743
6744 }
6745
6746 #endif /* #ifndef DISABLE_JIT */
6747
6748 void
6749 mono_arch_finish_init (void)
6750 {
6751 }
6752
6753 void
6754 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
6755 {
6756 }
6757
6758 MonoInst*
6759 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6760 {
6761         /* FIXME: */
6762         return NULL;
6763 }
6764
6765 gboolean
6766 mono_arch_print_tree (MonoInst *tree, int arity)
6767 {
6768         return 0;
6769 }
6770
6771 #ifndef DISABLE_JIT
6772
6773 #endif
6774
6775 guint32
6776 mono_arch_get_patch_offset (guint8 *code)
6777 {
6778         /* OP_AOTCONST */
6779         return 8;
6780 }
6781
6782 void
6783 mono_arch_flush_register_windows (void)
6784 {
6785 }
6786
6787 MonoMethod*
6788 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
6789 {
6790         return (MonoMethod*)regs [MONO_ARCH_IMT_REG];
6791 }
6792
6793 MonoVTable*
6794 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
6795 {
6796         return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
6797 }
6798
6799 GSList*
6800 mono_arch_get_cie_program (void)
6801 {
6802         GSList *l = NULL;
6803
6804         mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, ARMREG_SP, 0);
6805
6806         return l;
6807 }
6808
6809 /* #define ENABLE_WRONG_METHOD_CHECK 1 */
6810 #define BASE_SIZE (6 * 4)
6811 #define BSEARCH_ENTRY_SIZE (4 * 4)
6812 #define CMP_SIZE (3 * 4)
6813 #define BRANCH_SIZE (1 * 4)
6814 #define CALL_SIZE (2 * 4)
6815 #define WMC_SIZE (8 * 4)
6816 #define DISTANCE(A, B) (((gint32)(B)) - ((gint32)(A)))
6817
6818 static arminstr_t *
6819 arm_emit_value_and_patch_ldr (arminstr_t *code, arminstr_t *target, guint32 value)
6820 {
6821         guint32 delta = DISTANCE (target, code);
6822         delta -= 8;
6823         g_assert (delta >= 0 && delta <= 0xFFF);
6824         *target = *target | delta;
6825         *code = value;
6826         return code + 1;
6827 }
6828
6829 #ifdef ENABLE_WRONG_METHOD_CHECK
6830 static void
6831 mini_dump_bad_imt (int input_imt, int compared_imt, int pc)
6832 {
6833         g_print ("BAD IMT comparing %x with expected %x at ip %x", input_imt, compared_imt, pc);
6834         g_assert (0);
6835 }
6836 #endif
6837
6838 gpointer
6839 mono_arch_build_imt_trampoline (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
6840         gpointer fail_tramp)
6841 {
6842         int size, i;
6843         arminstr_t *code, *start;
6844         gboolean large_offsets = FALSE;
6845         guint32 **constant_pool_starts;
6846         arminstr_t *vtable_target = NULL;
6847         int extra_space = 0;
6848 #ifdef ENABLE_WRONG_METHOD_CHECK
6849         char * cond;
6850 #endif
6851         GSList *unwind_ops;
6852
6853         size = BASE_SIZE;
6854         constant_pool_starts = g_new0 (guint32*, count);
6855
6856         for (i = 0; i < count; ++i) {
6857                 MonoIMTCheckItem *item = imt_entries [i];
6858                 if (item->is_equals) {
6859                         gboolean fail_case = !item->check_target_idx && fail_tramp;
6860
6861                         if (item->has_target_code || !arm_is_imm12 (DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]))) {
6862                                 item->chunk_size += 32;
6863                                 large_offsets = TRUE;
6864                         }
6865
6866                         if (item->check_target_idx || fail_case) {
6867                                 if (!item->compare_done || fail_case)
6868                                         item->chunk_size += CMP_SIZE;
6869                                 item->chunk_size += BRANCH_SIZE;
6870                         } else {
6871 #ifdef ENABLE_WRONG_METHOD_CHECK
6872                                 item->chunk_size += WMC_SIZE;
6873 #endif
6874                         }
6875                         if (fail_case) {
6876                                 item->chunk_size += 16;
6877                                 large_offsets = TRUE;
6878                         }
6879                         item->chunk_size += CALL_SIZE;
6880                 } else {
6881                         item->chunk_size += BSEARCH_ENTRY_SIZE;
6882                         imt_entries [item->check_target_idx]->compare_done = TRUE;
6883                 }
6884                 size += item->chunk_size;
6885         }
6886
6887         if (large_offsets)
6888                 size += 4 * count; /* The ARM_ADD_REG_IMM to pop the stack */
6889
6890         if (fail_tramp)
6891                 code = mono_method_alloc_generic_virtual_trampoline (domain, size);
6892         else
6893                 code = mono_domain_code_reserve (domain, size);
6894         start = code;
6895
6896         unwind_ops = mono_arch_get_cie_program ();
6897
6898 #ifdef DEBUG_IMT
6899         g_print ("Building IMT trampoline for class %s %s entries %d code size %d code at %p end %p vtable %p fail_tramp %p\n", vtable->klass->name_space, vtable->klass->name, count, size, start, ((guint8*)start) + size, vtable, fail_tramp);
6900         for (i = 0; i < count; ++i) {
6901                 MonoIMTCheckItem *item = imt_entries [i];
6902                 g_print ("method %d (%p) %s vtable slot %p is_equals %d chunk size %d\n", i, item->key, ((MonoMethod*)item->key)->name, &vtable->vtable [item->value.vtable_slot], item->is_equals, item->chunk_size);
6903         }
6904 #endif
6905
6906         if (large_offsets) {
6907                 ARM_PUSH4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
6908                 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, 4 * sizeof (mgreg_t));
6909         } else {
6910                 ARM_PUSH2 (code, ARMREG_R0, ARMREG_R1);
6911                 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, 2 * sizeof (mgreg_t));
6912         }
6913         ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, -4);
6914         vtable_target = code;
6915         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
6916         ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V5);
6917
6918         for (i = 0; i < count; ++i) {
6919                 MonoIMTCheckItem *item = imt_entries [i];
6920                 arminstr_t *imt_method = NULL, *vtable_offset_ins = NULL, *target_code_ins = NULL;
6921                 gint32 vtable_offset;
6922
6923                 item->code_target = (guint8*)code;
6924
6925                 if (item->is_equals) {
6926                         gboolean fail_case = !item->check_target_idx && fail_tramp;
6927
6928                         if (item->check_target_idx || fail_case) {
6929                                 if (!item->compare_done || fail_case) {
6930                                         imt_method = code;
6931                                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
6932                                         ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
6933                                 }
6934                                 item->jmp_code = (guint8*)code;
6935                                 ARM_B_COND (code, ARMCOND_NE, 0);
6936                         } else {
6937                                 /*Enable the commented code to assert on wrong method*/
6938 #ifdef ENABLE_WRONG_METHOD_CHECK
6939                                 imt_method = code;
6940                                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
6941                                 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
6942                                 cond = code;
6943                                 ARM_B_COND (code, ARMCOND_EQ, 0);
6944
6945 /* Define this if your system is so bad that gdb is failing. */
6946 #ifdef BROKEN_DEV_ENV
6947                                 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_PC);
6948                                 ARM_BL (code, 0);
6949                                 arm_patch (code - 1, mini_dump_bad_imt);
6950 #else
6951                                 ARM_DBRK (code);
6952 #endif
6953                                 arm_patch (cond, code);
6954 #endif
6955                         }
6956
6957                         if (item->has_target_code) {
6958                                 /* Load target address */
6959                                 target_code_ins = code;
6960                                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
6961                                 /* Save it to the fourth slot */
6962                                 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
6963                                 /* Restore registers and branch */
6964                                 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
6965                                 
6966                                 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)item->value.target_code);
6967                         } else {
6968                                 vtable_offset = DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]);
6969                                 if (!arm_is_imm12 (vtable_offset)) {
6970                                         /* 
6971                                          * We need to branch to a computed address but we don't have
6972                                          * a free register to store it, since IP must contain the 
6973                                          * vtable address. So we push the two values to the stack, and
6974                                          * load them both using LDM.
6975                                          */
6976                                         /* Compute target address */
6977                                         vtable_offset_ins = code;
6978                                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
6979                                         ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_IP, ARMREG_R1);
6980                                         /* Save it to the fourth slot */
6981                                         ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
6982                                         /* Restore registers and branch */
6983                                         ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
6984                                 
6985                                         code = arm_emit_value_and_patch_ldr (code, vtable_offset_ins, vtable_offset);
6986                                 } else {
6987                                         ARM_POP2 (code, ARMREG_R0, ARMREG_R1);
6988                                         if (large_offsets) {
6989                                                 mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, 2 * sizeof (mgreg_t));
6990                                                 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 2 * sizeof (gpointer));
6991                                         }
6992                                         mono_add_unwind_op_def_cfa_offset (unwind_ops, code, start, 0);
6993                                         ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, vtable_offset);
6994                                 }
6995                         }
6996
6997                         if (fail_case) {
6998                                 arm_patch (item->jmp_code, (guchar*)code);
6999
7000                                 target_code_ins = code;
7001                                 /* Load target address */
7002                                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
7003                                 /* Save it to the fourth slot */
7004                                 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
7005                                 /* Restore registers and branch */
7006                                 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
7007                                 
7008                                 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)fail_tramp);
7009                                 item->jmp_code = NULL;
7010                         }
7011
7012                         if (imt_method)
7013                                 code = arm_emit_value_and_patch_ldr (code, imt_method, (guint32)item->key);
7014
7015                         /*must emit after unconditional branch*/
7016                         if (vtable_target) {
7017                                 code = arm_emit_value_and_patch_ldr (code, vtable_target, (guint32)vtable);
7018                                 item->chunk_size += 4;
7019                                 vtable_target = NULL;
7020                         }
7021
7022                         /*We reserve the space for bsearch IMT values after the first entry with an absolute jump*/
7023                         constant_pool_starts [i] = code;
7024                         if (extra_space) {
7025                                 code += extra_space;
7026                                 extra_space = 0;
7027                         }
7028                 } else {
7029                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
7030                         ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
7031
7032                         item->jmp_code = (guint8*)code;
7033                         ARM_B_COND (code, ARMCOND_HS, 0);
7034                         ++extra_space;
7035                 }
7036         }
7037
7038         for (i = 0; i < count; ++i) {
7039                 MonoIMTCheckItem *item = imt_entries [i];
7040                 if (item->jmp_code) {
7041                         if (item->check_target_idx)
7042                                 arm_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
7043                 }
7044                 if (i > 0 && item->is_equals) {
7045                         int j;
7046                         arminstr_t *space_start = constant_pool_starts [i];
7047                         for (j = i - 1; j >= 0 && !imt_entries [j]->is_equals; --j) {
7048                                 space_start = arm_emit_value_and_patch_ldr (space_start, (arminstr_t*)imt_entries [j]->code_target, (guint32)imt_entries [j]->key);
7049                         }
7050                 }
7051         }
7052
7053 #ifdef DEBUG_IMT
7054         {
7055                 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
7056                 mono_disassemble_code (NULL, (guint8*)start, size, buff);
7057                 g_free (buff);
7058         }
7059 #endif
7060
7061         g_free (constant_pool_starts);
7062
7063         mono_arch_flush_icache ((guint8*)start, size);
7064         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
7065         mono_stats.imt_trampolines_size += code - start;
7066
7067         g_assert (DISTANCE (start, code) <= size);
7068
7069         mono_tramp_info_register (mono_tramp_info_create (NULL, (guint8*)start, DISTANCE (start, code), NULL, unwind_ops), domain);
7070
7071         return start;
7072 }
7073
7074 mgreg_t
7075 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
7076 {
7077         return ctx->regs [reg];
7078 }
7079
7080 void
7081 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
7082 {
7083         ctx->regs [reg] = val;
7084 }
7085
7086 /*
7087  * mono_arch_get_trampolines:
7088  *
7089  *   Return a list of MonoTrampInfo structures describing arch specific trampolines
7090  * for AOT.
7091  */
7092 GSList *
7093 mono_arch_get_trampolines (gboolean aot)
7094 {
7095         return mono_arm_get_exception_trampolines (aot);
7096 }
7097
7098 gpointer
7099 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
7100 {
7101         gpointer *lr_loc;
7102         char *old_value;
7103         char *bp;
7104
7105         /*Load the spvar*/
7106         bp = MONO_CONTEXT_GET_BP (ctx);
7107         lr_loc = (gpointer*)(bp + clause->exvar_offset);
7108
7109         old_value = *lr_loc;
7110         if ((char*)old_value < (char*)ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
7111                 return old_value;
7112
7113         *lr_loc = new_value;
7114
7115         return old_value;
7116 }
7117
7118 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED)
7119 /*
7120  * mono_arch_set_breakpoint:
7121  *
7122  *   Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
7123  * The location should contain code emitted by OP_SEQ_POINT.
7124  */
7125 void
7126 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
7127 {
7128         guint8 *code = ip;
7129         guint32 native_offset = ip - (guint8*)ji->code_start;
7130         MonoDebugOptions *opt = mini_get_debug_options ();
7131
7132         if (ji->from_aot) {
7133                 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
7134
7135                 if (!breakpoint_tramp)
7136                         breakpoint_tramp = mini_get_breakpoint_trampoline ();
7137
7138                 g_assert (native_offset % 4 == 0);
7139                 g_assert (info->bp_addrs [native_offset / 4] == 0);
7140                 info->bp_addrs [native_offset / 4] = opt->soft_breakpoints ? breakpoint_tramp : bp_trigger_page;
7141         } else if (opt->soft_breakpoints) {
7142                 code += 4;
7143                 ARM_BLX_REG (code, ARMREG_LR);
7144                 mono_arch_flush_icache (code - 4, 4);
7145         } else {
7146                 int dreg = ARMREG_LR;
7147
7148                 /* Read from another trigger page */
7149                 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
7150                 ARM_B (code, 0);
7151                 *(int*)code = (int)bp_trigger_page;
7152                 code += 4;
7153                 ARM_LDR_IMM (code, dreg, dreg, 0);
7154
7155                 mono_arch_flush_icache (code - 16, 16);
7156
7157 #if 0
7158                 /* This is currently implemented by emitting an SWI instruction, which 
7159                  * qemu/linux seems to convert to a SIGILL.
7160                  */
7161                 *(int*)code = (0xef << 24) | 8;
7162                 code += 4;
7163                 mono_arch_flush_icache (code - 4, 4);
7164 #endif
7165         }
7166 }
7167
7168 /*
7169  * mono_arch_clear_breakpoint:
7170  *
7171  *   Clear the breakpoint at IP.
7172  */
7173 void
7174 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
7175 {
7176         MonoDebugOptions *opt = mini_get_debug_options ();
7177         guint8 *code = ip;
7178         int i;
7179
7180         if (ji->from_aot) {
7181                 guint32 native_offset = ip - (guint8*)ji->code_start;
7182                 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
7183
7184                 if (!breakpoint_tramp)
7185                         breakpoint_tramp = mini_get_breakpoint_trampoline ();
7186
7187                 g_assert (native_offset % 4 == 0);
7188                 g_assert (info->bp_addrs [native_offset / 4] == (opt->soft_breakpoints ? breakpoint_tramp : bp_trigger_page));
7189                 info->bp_addrs [native_offset / 4] = 0;
7190         } else if (opt->soft_breakpoints) {
7191                 code += 4;
7192                 ARM_NOP (code);
7193                 mono_arch_flush_icache (code - 4, 4);
7194         } else {
7195                 for (i = 0; i < 4; ++i)
7196                         ARM_NOP (code);
7197
7198                 mono_arch_flush_icache (ip, code - ip);
7199         }
7200 }
7201         
7202 /*
7203  * mono_arch_start_single_stepping:
7204  *
7205  *   Start single stepping.
7206  */
7207 void
7208 mono_arch_start_single_stepping (void)
7209 {
7210         if (ss_trigger_page)
7211                 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
7212         else
7213                 single_step_tramp = mini_get_single_step_trampoline ();
7214 }
7215         
7216 /*
7217  * mono_arch_stop_single_stepping:
7218  *
7219  *   Stop single stepping.
7220  */
7221 void
7222 mono_arch_stop_single_stepping (void)
7223 {
7224         if (ss_trigger_page)
7225                 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
7226         else
7227                 single_step_tramp = NULL;
7228 }
7229
7230 #if __APPLE__
7231 #define DBG_SIGNAL SIGBUS
7232 #else
7233 #define DBG_SIGNAL SIGSEGV
7234 #endif
7235
7236 /*
7237  * mono_arch_is_single_step_event:
7238  *
7239  *   Return whenever the machine state in SIGCTX corresponds to a single
7240  * step event.
7241  */
7242 gboolean
7243 mono_arch_is_single_step_event (void *info, void *sigctx)
7244 {
7245         siginfo_t *sinfo = info;
7246
7247         if (!ss_trigger_page)
7248                 return FALSE;
7249
7250         /* Sometimes the address is off by 4 */
7251         if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
7252                 return TRUE;
7253         else
7254                 return FALSE;
7255 }
7256
7257 /*
7258  * mono_arch_is_breakpoint_event:
7259  *
7260  *   Return whenever the machine state in SIGCTX corresponds to a breakpoint event.
7261  */
7262 gboolean
7263 mono_arch_is_breakpoint_event (void *info, void *sigctx)
7264 {
7265         siginfo_t *sinfo = info;
7266
7267         if (!ss_trigger_page)
7268                 return FALSE;
7269
7270         if (sinfo->si_signo == DBG_SIGNAL) {
7271                 /* Sometimes the address is off by 4 */
7272                 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
7273                         return TRUE;
7274                 else
7275                         return FALSE;
7276         } else {
7277                 return FALSE;
7278         }
7279 }
7280
7281 /*
7282  * mono_arch_skip_breakpoint:
7283  *
7284  *   See mini-amd64.c for docs.
7285  */
7286 void
7287 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
7288 {
7289         MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
7290 }
7291
7292 /*
7293  * mono_arch_skip_single_step:
7294  *
7295  *   See mini-amd64.c for docs.
7296  */
7297 void
7298 mono_arch_skip_single_step (MonoContext *ctx)
7299 {
7300         MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
7301 }
7302
7303 #endif /* MONO_ARCH_SOFT_DEBUG_SUPPORTED */
7304
7305 /*
7306  * mono_arch_get_seq_point_info:
7307  *
7308  *   See mini-amd64.c for docs.
7309  */
7310 gpointer
7311 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
7312 {
7313         SeqPointInfo *info;
7314         MonoJitInfo *ji;
7315
7316         // FIXME: Add a free function
7317
7318         mono_domain_lock (domain);
7319         info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points, 
7320                                                                 code);
7321         mono_domain_unlock (domain);
7322
7323         if (!info) {
7324                 ji = mono_jit_info_table_find (domain, (char*)code);
7325                 g_assert (ji);
7326
7327                 info = g_malloc0 (sizeof (SeqPointInfo) + ji->code_size);
7328
7329                 info->ss_trigger_page = ss_trigger_page;
7330                 info->bp_trigger_page = bp_trigger_page;
7331                 info->ss_tramp_addr = &single_step_tramp;
7332
7333                 mono_domain_lock (domain);
7334                 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
7335                                                          code, info);
7336                 mono_domain_unlock (domain);
7337         }
7338
7339         return info;
7340 }
7341
7342 void
7343 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
7344 {
7345         ext->lmf.previous_lmf = prev_lmf;
7346         /* Mark that this is a MonoLMFExt */
7347         ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
7348         ext->lmf.sp = (gssize)ext;
7349 }
7350
7351 /*
7352  * mono_arch_set_target:
7353  *
7354  *   Set the target architecture the JIT backend should generate code for, in the form
7355  * of a GNU target triplet. Only used in AOT mode.
7356  */
7357 void
7358 mono_arch_set_target (char *mtriple)
7359 {
7360         /* The GNU target triple format is not very well documented */
7361         if (strstr (mtriple, "armv7")) {
7362                 v5_supported = TRUE;
7363                 v6_supported = TRUE;
7364                 v7_supported = TRUE;
7365         }
7366         if (strstr (mtriple, "armv6")) {
7367                 v5_supported = TRUE;
7368                 v6_supported = TRUE;
7369         }
7370         if (strstr (mtriple, "armv7s")) {
7371                 v7s_supported = TRUE;
7372         }
7373         if (strstr (mtriple, "armv7k")) {
7374                 v7k_supported = TRUE;
7375         }
7376         if (strstr (mtriple, "thumbv7s")) {
7377                 v5_supported = TRUE;
7378                 v6_supported = TRUE;
7379                 v7_supported = TRUE;
7380                 v7s_supported = TRUE;
7381                 thumb_supported = TRUE;
7382                 thumb2_supported = TRUE;
7383         }
7384         if (strstr (mtriple, "darwin") || strstr (mtriple, "ios")) {
7385                 v5_supported = TRUE;
7386                 v6_supported = TRUE;
7387                 thumb_supported = TRUE;
7388                 iphone_abi = TRUE;
7389         }
7390         if (strstr (mtriple, "gnueabi"))
7391                 eabi_supported = TRUE;
7392 }
7393
7394 gboolean
7395 mono_arch_opcode_supported (int opcode)
7396 {
7397         switch (opcode) {
7398         case OP_ATOMIC_ADD_I4:
7399         case OP_ATOMIC_EXCHANGE_I4:
7400         case OP_ATOMIC_CAS_I4:
7401         case OP_ATOMIC_LOAD_I1:
7402         case OP_ATOMIC_LOAD_I2:
7403         case OP_ATOMIC_LOAD_I4:
7404         case OP_ATOMIC_LOAD_U1:
7405         case OP_ATOMIC_LOAD_U2:
7406         case OP_ATOMIC_LOAD_U4:
7407         case OP_ATOMIC_STORE_I1:
7408         case OP_ATOMIC_STORE_I2:
7409         case OP_ATOMIC_STORE_I4:
7410         case OP_ATOMIC_STORE_U1:
7411         case OP_ATOMIC_STORE_U2:
7412         case OP_ATOMIC_STORE_U4:
7413                 return v7_supported;
7414         case OP_ATOMIC_LOAD_R4:
7415         case OP_ATOMIC_LOAD_R8:
7416         case OP_ATOMIC_STORE_R4:
7417         case OP_ATOMIC_STORE_R8:
7418                 return v7_supported && IS_VFP;
7419         default:
7420                 return FALSE;
7421         }
7422 }
7423
7424 CallInfo*
7425 mono_arch_get_call_info (MonoMemPool *mp, MonoMethodSignature *sig)
7426 {
7427         return get_call_info (mp, sig);
7428 }
7429
7430 gpointer
7431 mono_arch_get_get_tls_tramp (void)
7432 {
7433         return NULL;
7434 }
7435
7436 static guint8*
7437 emit_aotconst (MonoCompile *cfg, guint8 *code, int dreg, int patch_type, gpointer data)
7438 {
7439         /* OP_AOTCONST */
7440         mono_add_patch_info (cfg, code - cfg->native_code, patch_type, data);
7441         ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
7442         ARM_B (code, 0);
7443         *(gpointer*)code = NULL;
7444         code += 4;
7445         /* Load the value from the GOT */
7446         ARM_LDR_REG_REG (code, dreg, ARMREG_PC, dreg);
7447         return code;
7448 }
7449
7450 guint8*
7451 mono_arm_emit_aotconst (gpointer ji_list, guint8 *code, guint8 *buf, int dreg, int patch_type, gconstpointer data)
7452 {
7453         MonoJumpInfo **ji = (MonoJumpInfo**)ji_list;
7454
7455         *ji = mono_patch_info_list_prepend (*ji, code - buf, patch_type, data);
7456         ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
7457         ARM_B (code, 0);
7458         *(gpointer*)code = NULL;
7459         code += 4;
7460         ARM_LDR_REG_REG (code, dreg, ARMREG_PC, dreg);
7461         return code;
7462 }