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