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