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