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