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