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