[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[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, *last_ins = NULL;
3115
3116         MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
3117                 switch (ins->opcode) {
3118                 case OP_MUL_IMM: 
3119                 case OP_IMUL_IMM: 
3120                         /* Already done by an arch-independent pass */
3121                         break;
3122                 case OP_LOAD_MEMBASE:
3123                 case OP_LOADI4_MEMBASE:
3124                         /* 
3125                          * OP_STORE_MEMBASE_REG reg, offset(basereg) 
3126                          * OP_LOAD_MEMBASE offset(basereg), reg
3127                          */
3128                         if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG 
3129                                          || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
3130                             ins->inst_basereg == last_ins->inst_destbasereg &&
3131                             ins->inst_offset == last_ins->inst_offset) {
3132                                 if (ins->dreg == last_ins->sreg1) {
3133                                         MONO_DELETE_INS (bb, ins);
3134                                         continue;
3135                                 } else {
3136                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
3137                                         ins->opcode = OP_MOVE;
3138                                         ins->sreg1 = last_ins->sreg1;
3139                                 }
3140
3141                         /* 
3142                          * Note: reg1 must be different from the basereg in the second load
3143                          * OP_LOAD_MEMBASE offset(basereg), reg1
3144                          * OP_LOAD_MEMBASE offset(basereg), reg2
3145                          * -->
3146                          * OP_LOAD_MEMBASE offset(basereg), reg1
3147                          * OP_MOVE reg1, reg2
3148                          */
3149                         } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
3150                                            || last_ins->opcode == OP_LOAD_MEMBASE) &&
3151                               ins->inst_basereg != last_ins->dreg &&
3152                               ins->inst_basereg == last_ins->inst_basereg &&
3153                               ins->inst_offset == last_ins->inst_offset) {
3154
3155                                 if (ins->dreg == last_ins->dreg) {
3156                                         MONO_DELETE_INS (bb, ins);
3157                                         continue;
3158                                 } else {
3159                                         ins->opcode = OP_MOVE;
3160                                         ins->sreg1 = last_ins->dreg;
3161                                 }
3162
3163                                 //g_assert_not_reached ();
3164
3165 #if 0
3166                         /* 
3167                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
3168                          * OP_LOAD_MEMBASE offset(basereg), reg
3169                          * -->
3170                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
3171                          * OP_ICONST reg, imm
3172                          */
3173                         } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
3174                                                 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
3175                                    ins->inst_basereg == last_ins->inst_destbasereg &&
3176                                    ins->inst_offset == last_ins->inst_offset) {
3177                                 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
3178                                 ins->opcode = OP_ICONST;
3179                                 ins->inst_c0 = last_ins->inst_imm;
3180                                 g_assert_not_reached (); // check this rule
3181 #endif
3182                         }
3183                         break;
3184                 case OP_LOADU1_MEMBASE:
3185                 case OP_LOADI1_MEMBASE:
3186                         if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
3187                                         ins->inst_basereg == last_ins->inst_destbasereg &&
3188                                         ins->inst_offset == last_ins->inst_offset) {
3189                                 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_ICONV_TO_I1 : OP_ICONV_TO_U1;
3190                                 ins->sreg1 = last_ins->sreg1;                           
3191                         }
3192                         break;
3193                 case OP_LOADU2_MEMBASE:
3194                 case OP_LOADI2_MEMBASE:
3195                         if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
3196                                         ins->inst_basereg == last_ins->inst_destbasereg &&
3197                                         ins->inst_offset == last_ins->inst_offset) {
3198                                 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_ICONV_TO_I2 : OP_ICONV_TO_U2;
3199                                 ins->sreg1 = last_ins->sreg1;                           
3200                         }
3201                         break;
3202                 case OP_MOVE:
3203                         ins->opcode = OP_MOVE;
3204                         /* 
3205                          * OP_MOVE reg, reg 
3206                          */
3207                         if (ins->dreg == ins->sreg1) {
3208                                 MONO_DELETE_INS (bb, ins);
3209                                 continue;
3210                         }
3211                         /* 
3212                          * OP_MOVE sreg, dreg 
3213                          * OP_MOVE dreg, sreg
3214                          */
3215                         if (last_ins && last_ins->opcode == OP_MOVE &&
3216                             ins->sreg1 == last_ins->dreg &&
3217                             ins->dreg == last_ins->sreg1) {
3218                                 MONO_DELETE_INS (bb, ins);
3219                                 continue;
3220                         }
3221                         break;
3222                 }
3223                 last_ins = ins;
3224                 ins = ins->next;
3225         }
3226         bb->last_ins = last_ins;
3227 }
3228
3229 /* 
3230  * the branch_cc_table should maintain the order of these
3231  * opcodes.
3232 case CEE_BEQ:
3233 case CEE_BGE:
3234 case CEE_BGT:
3235 case CEE_BLE:
3236 case CEE_BLT:
3237 case CEE_BNE_UN:
3238 case CEE_BGE_UN:
3239 case CEE_BGT_UN:
3240 case CEE_BLE_UN:
3241 case CEE_BLT_UN:
3242  */
3243 static const guchar 
3244 branch_cc_table [] = {
3245         ARMCOND_EQ, 
3246         ARMCOND_GE, 
3247         ARMCOND_GT, 
3248         ARMCOND_LE,
3249         ARMCOND_LT, 
3250         
3251         ARMCOND_NE, 
3252         ARMCOND_HS, 
3253         ARMCOND_HI, 
3254         ARMCOND_LS,
3255         ARMCOND_LO
3256 };
3257
3258 #define ADD_NEW_INS(cfg,dest,op) do {       \
3259                 MONO_INST_NEW ((cfg), (dest), (op)); \
3260         mono_bblock_insert_before_ins (bb, ins, (dest)); \
3261         } while (0)
3262
3263 static int
3264 map_to_reg_reg_op (int op)
3265 {
3266         switch (op) {
3267         case OP_ADD_IMM:
3268                 return OP_IADD;
3269         case OP_SUB_IMM:
3270                 return OP_ISUB;
3271         case OP_AND_IMM:
3272                 return OP_IAND;
3273         case OP_COMPARE_IMM:
3274                 return OP_COMPARE;
3275         case OP_ICOMPARE_IMM:
3276                 return OP_ICOMPARE;
3277         case OP_ADDCC_IMM:
3278                 return OP_ADDCC;
3279         case OP_ADC_IMM:
3280                 return OP_ADC;
3281         case OP_SUBCC_IMM:
3282                 return OP_SUBCC;
3283         case OP_SBB_IMM:
3284                 return OP_SBB;
3285         case OP_OR_IMM:
3286                 return OP_IOR;
3287         case OP_XOR_IMM:
3288                 return OP_IXOR;
3289         case OP_LOAD_MEMBASE:
3290                 return OP_LOAD_MEMINDEX;
3291         case OP_LOADI4_MEMBASE:
3292                 return OP_LOADI4_MEMINDEX;
3293         case OP_LOADU4_MEMBASE:
3294                 return OP_LOADU4_MEMINDEX;
3295         case OP_LOADU1_MEMBASE:
3296                 return OP_LOADU1_MEMINDEX;
3297         case OP_LOADI2_MEMBASE:
3298                 return OP_LOADI2_MEMINDEX;
3299         case OP_LOADU2_MEMBASE:
3300                 return OP_LOADU2_MEMINDEX;
3301         case OP_LOADI1_MEMBASE:
3302                 return OP_LOADI1_MEMINDEX;
3303         case OP_STOREI1_MEMBASE_REG:
3304                 return OP_STOREI1_MEMINDEX;
3305         case OP_STOREI2_MEMBASE_REG:
3306                 return OP_STOREI2_MEMINDEX;
3307         case OP_STOREI4_MEMBASE_REG:
3308                 return OP_STOREI4_MEMINDEX;
3309         case OP_STORE_MEMBASE_REG:
3310                 return OP_STORE_MEMINDEX;
3311         case OP_STORER4_MEMBASE_REG:
3312                 return OP_STORER4_MEMINDEX;
3313         case OP_STORER8_MEMBASE_REG:
3314                 return OP_STORER8_MEMINDEX;
3315         case OP_STORE_MEMBASE_IMM:
3316                 return OP_STORE_MEMBASE_REG;
3317         case OP_STOREI1_MEMBASE_IMM:
3318                 return OP_STOREI1_MEMBASE_REG;
3319         case OP_STOREI2_MEMBASE_IMM:
3320                 return OP_STOREI2_MEMBASE_REG;
3321         case OP_STOREI4_MEMBASE_IMM:
3322                 return OP_STOREI4_MEMBASE_REG;
3323         }
3324         g_assert_not_reached ();
3325 }
3326
3327 /*
3328  * Remove from the instruction list the instructions that can't be
3329  * represented with very simple instructions with no register
3330  * requirements.
3331  */
3332 void
3333 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
3334 {
3335         MonoInst *ins, *temp, *last_ins = NULL;
3336         int rot_amount, imm8, low_imm;
3337
3338         MONO_BB_FOR_EACH_INS (bb, ins) {
3339 loop_start:
3340                 switch (ins->opcode) {
3341                 case OP_ADD_IMM:
3342                 case OP_SUB_IMM:
3343                 case OP_AND_IMM:
3344                 case OP_COMPARE_IMM:
3345                 case OP_ICOMPARE_IMM:
3346                 case OP_ADDCC_IMM:
3347                 case OP_ADC_IMM:
3348                 case OP_SUBCC_IMM:
3349                 case OP_SBB_IMM:
3350                 case OP_OR_IMM:
3351                 case OP_XOR_IMM:
3352                 case OP_IADD_IMM:
3353                 case OP_ISUB_IMM:
3354                 case OP_IAND_IMM:
3355                 case OP_IADC_IMM:
3356                 case OP_ISBB_IMM:
3357                 case OP_IOR_IMM:
3358                 case OP_IXOR_IMM:
3359                         if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
3360                                 ADD_NEW_INS (cfg, temp, OP_ICONST);
3361                                 temp->inst_c0 = ins->inst_imm;
3362                                 temp->dreg = mono_alloc_ireg (cfg);
3363                                 ins->sreg2 = temp->dreg;
3364                                 ins->opcode = mono_op_imm_to_op (ins->opcode);
3365                         }
3366                         if (ins->opcode == OP_SBB || ins->opcode == OP_ISBB || ins->opcode == OP_SUBCC)
3367                                 goto loop_start;
3368                         else
3369                                 break;
3370                 case OP_MUL_IMM:
3371                 case OP_IMUL_IMM:
3372                         if (ins->inst_imm == 1) {
3373                                 ins->opcode = OP_MOVE;
3374                                 break;
3375                         }
3376                         if (ins->inst_imm == 0) {
3377                                 ins->opcode = OP_ICONST;
3378                                 ins->inst_c0 = 0;
3379                                 break;
3380                         }
3381                         imm8 = mono_is_power_of_two (ins->inst_imm);
3382                         if (imm8 > 0) {
3383                                 ins->opcode = OP_SHL_IMM;
3384                                 ins->inst_imm = imm8;
3385                                 break;
3386                         }
3387                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3388                         temp->inst_c0 = ins->inst_imm;
3389                         temp->dreg = mono_alloc_ireg (cfg);
3390                         ins->sreg2 = temp->dreg;
3391                         ins->opcode = OP_IMUL;
3392                         break;
3393                 case OP_SBB:
3394                 case OP_ISBB:
3395                 case OP_SUBCC:
3396                 case OP_ISUBCC:
3397                         if (ins->next  && (ins->next->opcode == OP_COND_EXC_C || ins->next->opcode == OP_COND_EXC_IC))
3398                                 /* ARM sets the C flag to 1 if there was _no_ overflow */
3399                                 ins->next->opcode = OP_COND_EXC_NC;
3400                         break;
3401                 case OP_IDIV_IMM:
3402                 case OP_IDIV_UN_IMM:
3403                 case OP_IREM_IMM:
3404                 case OP_IREM_UN_IMM:
3405                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3406                         temp->inst_c0 = ins->inst_imm;
3407                         temp->dreg = mono_alloc_ireg (cfg);
3408                         ins->sreg2 = temp->dreg;
3409                         ins->opcode = mono_op_imm_to_op (ins->opcode);
3410                         break;
3411                 case OP_LOCALLOC_IMM:
3412                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3413                         temp->inst_c0 = ins->inst_imm;
3414                         temp->dreg = mono_alloc_ireg (cfg);
3415                         ins->sreg1 = temp->dreg;
3416                         ins->opcode = OP_LOCALLOC;
3417                         break;
3418                 case OP_LOAD_MEMBASE:
3419                 case OP_LOADI4_MEMBASE:
3420                 case OP_LOADU4_MEMBASE:
3421                 case OP_LOADU1_MEMBASE:
3422                         /* we can do two things: load the immed in a register
3423                          * and use an indexed load, or see if the immed can be
3424                          * represented as an ad_imm + a load with a smaller offset
3425                          * that fits. We just do the first for now, optimize later.
3426                          */
3427                         if (arm_is_imm12 (ins->inst_offset))
3428                                 break;
3429                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3430                         temp->inst_c0 = ins->inst_offset;
3431                         temp->dreg = mono_alloc_ireg (cfg);
3432                         ins->sreg2 = temp->dreg;
3433                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3434                         break;
3435                 case OP_LOADI2_MEMBASE:
3436                 case OP_LOADU2_MEMBASE:
3437                 case OP_LOADI1_MEMBASE:
3438                         if (arm_is_imm8 (ins->inst_offset))
3439                                 break;
3440                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3441                         temp->inst_c0 = ins->inst_offset;
3442                         temp->dreg = mono_alloc_ireg (cfg);
3443                         ins->sreg2 = temp->dreg;
3444                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3445                         break;
3446                 case OP_LOADR4_MEMBASE:
3447                 case OP_LOADR8_MEMBASE:
3448                         if (arm_is_fpimm8 (ins->inst_offset))
3449                                 break;
3450                         low_imm = ins->inst_offset & 0x1ff;
3451                         if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~0x1ff, &rot_amount)) >= 0) {
3452                                 ADD_NEW_INS (cfg, temp, OP_ADD_IMM);
3453                                 temp->inst_imm = ins->inst_offset & ~0x1ff;
3454                                 temp->sreg1 = ins->inst_basereg;
3455                                 temp->dreg = mono_alloc_ireg (cfg);
3456                                 ins->inst_basereg = temp->dreg;
3457                                 ins->inst_offset = low_imm;
3458                         } else {
3459                                 MonoInst *add_ins;
3460
3461                                 ADD_NEW_INS (cfg, temp, OP_ICONST);
3462                                 temp->inst_c0 = ins->inst_offset;
3463                                 temp->dreg = mono_alloc_ireg (cfg);
3464
3465                                 ADD_NEW_INS (cfg, add_ins, OP_IADD);
3466                                 add_ins->sreg1 = ins->inst_basereg;
3467                                 add_ins->sreg2 = temp->dreg;
3468                                 add_ins->dreg = mono_alloc_ireg (cfg);
3469
3470                                 ins->inst_basereg = add_ins->dreg;
3471                                 ins->inst_offset = 0;
3472                         }
3473                         break;
3474                 case OP_STORE_MEMBASE_REG:
3475                 case OP_STOREI4_MEMBASE_REG:
3476                 case OP_STOREI1_MEMBASE_REG:
3477                         if (arm_is_imm12 (ins->inst_offset))
3478                                 break;
3479                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3480                         temp->inst_c0 = ins->inst_offset;
3481                         temp->dreg = mono_alloc_ireg (cfg);
3482                         ins->sreg2 = temp->dreg;
3483                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3484                         break;
3485                 case OP_STOREI2_MEMBASE_REG:
3486                         if (arm_is_imm8 (ins->inst_offset))
3487                                 break;
3488                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3489                         temp->inst_c0 = ins->inst_offset;
3490                         temp->dreg = mono_alloc_ireg (cfg);
3491                         ins->sreg2 = temp->dreg;
3492                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3493                         break;
3494                 case OP_STORER4_MEMBASE_REG:
3495                 case OP_STORER8_MEMBASE_REG:
3496                         if (arm_is_fpimm8 (ins->inst_offset))
3497                                 break;
3498                         low_imm = ins->inst_offset & 0x1ff;
3499                         if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~ 0x1ff, &rot_amount)) >= 0 && arm_is_fpimm8 (low_imm)) {
3500                                 ADD_NEW_INS (cfg, temp, OP_ADD_IMM);
3501                                 temp->inst_imm = ins->inst_offset & ~0x1ff;
3502                                 temp->sreg1 = ins->inst_destbasereg;
3503                                 temp->dreg = mono_alloc_ireg (cfg);
3504                                 ins->inst_destbasereg = temp->dreg;
3505                                 ins->inst_offset = low_imm;
3506                         } else {
3507                                 MonoInst *add_ins;
3508
3509                                 ADD_NEW_INS (cfg, temp, OP_ICONST);
3510                                 temp->inst_c0 = ins->inst_offset;
3511                                 temp->dreg = mono_alloc_ireg (cfg);
3512
3513                                 ADD_NEW_INS (cfg, add_ins, OP_IADD);
3514                                 add_ins->sreg1 = ins->inst_destbasereg;
3515                                 add_ins->sreg2 = temp->dreg;
3516                                 add_ins->dreg = mono_alloc_ireg (cfg);
3517
3518                                 ins->inst_destbasereg = add_ins->dreg;
3519                                 ins->inst_offset = 0;
3520                         }
3521                         break;
3522                 case OP_STORE_MEMBASE_IMM:
3523                 case OP_STOREI1_MEMBASE_IMM:
3524                 case OP_STOREI2_MEMBASE_IMM:
3525                 case OP_STOREI4_MEMBASE_IMM:
3526                         ADD_NEW_INS (cfg, temp, OP_ICONST);
3527                         temp->inst_c0 = ins->inst_imm;
3528                         temp->dreg = mono_alloc_ireg (cfg);
3529                         ins->sreg1 = temp->dreg;
3530                         ins->opcode = map_to_reg_reg_op (ins->opcode);
3531                         last_ins = temp;
3532                         goto loop_start; /* make it handle the possibly big ins->inst_offset */
3533                 case OP_FCOMPARE:
3534                 case OP_RCOMPARE: {
3535                         gboolean swap = FALSE;
3536                         int reg;
3537
3538                         if (!ins->next) {
3539                                 /* Optimized away */
3540                                 NULLIFY_INS (ins);
3541                                 break;
3542                         }
3543
3544                         /* Some fp compares require swapped operands */
3545                         switch (ins->next->opcode) {
3546                         case OP_FBGT:
3547                                 ins->next->opcode = OP_FBLT;
3548                                 swap = TRUE;
3549                                 break;
3550                         case OP_FBGT_UN:
3551                                 ins->next->opcode = OP_FBLT_UN;
3552                                 swap = TRUE;
3553                                 break;
3554                         case OP_FBLE:
3555                                 ins->next->opcode = OP_FBGE;
3556                                 swap = TRUE;
3557                                 break;
3558                         case OP_FBLE_UN:
3559                                 ins->next->opcode = OP_FBGE_UN;
3560                                 swap = TRUE;
3561                                 break;
3562                         default:
3563                                 break;
3564                         }
3565                         if (swap) {
3566                                 reg = ins->sreg1;
3567                                 ins->sreg1 = ins->sreg2;
3568                                 ins->sreg2 = reg;
3569                         }
3570                         break;
3571                 }
3572                 }
3573
3574                 last_ins = ins;
3575         }
3576         bb->last_ins = last_ins;
3577         bb->max_vreg = cfg->next_vreg;
3578 }
3579
3580 void
3581 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
3582 {
3583         MonoInst *ins;
3584
3585         if (long_ins->opcode == OP_LNEG) {
3586                 ins = long_ins;
3587                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSBS_IMM, ins->dreg + 1, ins->sreg1 + 1, 0);
3588                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
3589                 NULLIFY_INS (ins);
3590         }
3591 }
3592
3593 static guchar*
3594 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3595 {
3596         /* sreg is a float, dreg is an integer reg  */
3597         if (IS_VFP) {
3598                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
3599                 if (is_signed)
3600                         ARM_TOSIZD (code, vfp_scratch1, sreg);
3601                 else
3602                         ARM_TOUIZD (code, vfp_scratch1, sreg);
3603                 ARM_FMRS (code, dreg, vfp_scratch1);
3604                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
3605         }
3606         if (!is_signed) {
3607                 if (size == 1)
3608                         ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
3609                 else if (size == 2) {
3610                         ARM_SHL_IMM (code, dreg, dreg, 16);
3611                         ARM_SHR_IMM (code, dreg, dreg, 16);
3612                 }
3613         } else {
3614                 if (size == 1) {
3615                         ARM_SHL_IMM (code, dreg, dreg, 24);
3616                         ARM_SAR_IMM (code, dreg, dreg, 24);
3617                 } else if (size == 2) {
3618                         ARM_SHL_IMM (code, dreg, dreg, 16);
3619                         ARM_SAR_IMM (code, dreg, dreg, 16);
3620                 }
3621         }
3622         return code;
3623 }
3624
3625 static guchar*
3626 emit_r4_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
3627 {
3628         /* sreg is a float, dreg is an integer reg  */
3629         g_assert (IS_VFP);
3630         code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
3631         if (is_signed)
3632                 ARM_TOSIZS (code, vfp_scratch1, sreg);
3633         else
3634                 ARM_TOUIZS (code, vfp_scratch1, sreg);
3635         ARM_FMRS (code, dreg, vfp_scratch1);
3636         code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
3637
3638         if (!is_signed) {
3639                 if (size == 1)
3640                         ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
3641                 else if (size == 2) {
3642                         ARM_SHL_IMM (code, dreg, dreg, 16);
3643                         ARM_SHR_IMM (code, dreg, dreg, 16);
3644                 }
3645         } else {
3646                 if (size == 1) {
3647                         ARM_SHL_IMM (code, dreg, dreg, 24);
3648                         ARM_SAR_IMM (code, dreg, dreg, 24);
3649                 } else if (size == 2) {
3650                         ARM_SHL_IMM (code, dreg, dreg, 16);
3651                         ARM_SAR_IMM (code, dreg, dreg, 16);
3652                 }
3653         }
3654         return code;
3655 }
3656
3657 #endif /* #ifndef DISABLE_JIT */
3658
3659 typedef struct {
3660         guchar *code;
3661         const guchar *target;
3662         int absolute;
3663         int found;
3664 } PatchData;
3665
3666 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
3667
3668 static int
3669 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
3670         PatchData *pdata = (PatchData*)user_data;
3671         guchar *code = data;
3672         guint32 *thunks = data;
3673         guint32 *endthunks = (guint32*)(code + bsize);
3674         int count = 0;
3675         int difflow, diffhigh;
3676
3677         /* always ensure a call from pdata->code can reach to the thunks without further thunks */
3678         difflow = (char*)pdata->code - (char*)thunks;
3679         diffhigh = (char*)pdata->code - (char*)endthunks;
3680         if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
3681                 return 0;
3682
3683         /*
3684          * The thunk is composed of 3 words:
3685          * load constant from thunks [2] into ARM_IP
3686          * bx to ARM_IP
3687          * address constant
3688          * Note that the LR register is already setup
3689          */
3690         //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
3691         if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
3692                 while (thunks < endthunks) {
3693                         //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
3694                         if (thunks [2] == (guint32)pdata->target) {
3695                                 arm_patch (pdata->code, (guchar*)thunks);
3696                                 mono_arch_flush_icache (pdata->code, 4);
3697                                 pdata->found = 1;
3698                                 return 1;
3699                         } else if ((thunks [0] == 0) && (thunks [1] == 0) && (thunks [2] == 0)) {
3700                                 /* found a free slot instead: emit thunk */
3701                                 /* ARMREG_IP is fine to use since this can't be an IMT call
3702                                  * which is indirect
3703                                  */
3704                                 code = (guchar*)thunks;
3705                                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
3706                                 if (thumb_supported)
3707                                         ARM_BX (code, ARMREG_IP);
3708                                 else
3709                                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3710                                 thunks [2] = (guint32)pdata->target;
3711                                 mono_arch_flush_icache ((guchar*)thunks, 12);
3712
3713                                 arm_patch (pdata->code, (guchar*)thunks);
3714                                 mono_arch_flush_icache (pdata->code, 4);
3715                                 pdata->found = 1;
3716                                 return 1;
3717                         }
3718                         /* skip 12 bytes, the size of the thunk */
3719                         thunks += 3;
3720                         count++;
3721                 }
3722                 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
3723         }
3724         return 0;
3725 }
3726
3727 static void
3728 handle_thunk (MonoDomain *domain, int absolute, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
3729 {
3730         PatchData pdata;
3731
3732         if (!domain)
3733                 domain = mono_domain_get ();
3734
3735         pdata.code = code;
3736         pdata.target = target;
3737         pdata.absolute = absolute;
3738         pdata.found = 0;
3739
3740         if (dyn_code_mp) {
3741                 mono_code_manager_foreach (dyn_code_mp, search_thunk_slot, &pdata);
3742         }
3743
3744         if (pdata.found != 1) {
3745                 mono_domain_lock (domain);
3746                 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
3747
3748                 if (!pdata.found) {
3749                         /* this uses the first available slot */
3750                         pdata.found = 2;
3751                         mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
3752                 }
3753                 mono_domain_unlock (domain);
3754         }
3755
3756         if (pdata.found != 1) {
3757                 GHashTable *hash;
3758                 GHashTableIter iter;
3759                 MonoJitDynamicMethodInfo *ji;
3760
3761                 /*
3762                  * This might be a dynamic method, search its code manager. We can only
3763                  * use the dynamic method containing CODE, since the others might be freed later.
3764                  */
3765                 pdata.found = 0;
3766
3767                 mono_domain_lock (domain);
3768                 hash = domain_jit_info (domain)->dynamic_code_hash;
3769                 if (hash) {
3770                         /* FIXME: Speed this up */
3771                         g_hash_table_iter_init (&iter, hash);
3772                         while (g_hash_table_iter_next (&iter, NULL, (gpointer*)&ji)) {
3773                                 mono_code_manager_foreach (ji->code_mp, search_thunk_slot, &pdata);
3774                                 if (pdata.found == 1)
3775                                         break;
3776                         }
3777                 }
3778                 mono_domain_unlock (domain);
3779         }
3780         if (pdata.found != 1)
3781                 g_print ("thunk failed for %p from %p\n", target, code);
3782         g_assert (pdata.found == 1);
3783 }
3784
3785 static void
3786 arm_patch_general (MonoDomain *domain, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
3787 {
3788         guint32 *code32 = (void*)code;
3789         guint32 ins = *code32;
3790         guint32 prim = (ins >> 25) & 7;
3791         guint32 tval = GPOINTER_TO_UINT (target);
3792
3793         //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
3794         if (prim == 5) { /* 101b */
3795                 /* the diff starts 8 bytes from the branch opcode */
3796                 gint diff = target - code - 8;
3797                 gint tbits;
3798                 gint tmask = 0xffffffff;
3799                 if (tval & 1) { /* entering thumb mode */
3800                         diff = target - 1 - code - 8;
3801                         g_assert (thumb_supported);
3802                         tbits = 0xf << 28; /* bl->blx bit pattern */
3803                         g_assert ((ins & (1 << 24))); /* it must be a bl, not b instruction */
3804                         /* this low bit of the displacement is moved to bit 24 in the instruction encoding */
3805                         if (diff & 2) {
3806                                 tbits |= 1 << 24;
3807                         }
3808                         tmask = ~(1 << 24); /* clear the link bit */
3809                         /*g_print ("blx to thumb: target: %p, code: %p, diff: %d, mask: %x\n", target, code, diff, tmask);*/
3810                 } else {
3811                         tbits = 0;
3812                 }
3813                 if (diff >= 0) {
3814                         if (diff <= 33554431) {
3815                                 diff >>= 2;
3816                                 ins = (ins & 0xff000000) | diff;
3817                                 ins &= tmask;
3818                                 *code32 = ins | tbits;
3819                                 return;
3820                         }
3821                 } else {
3822                         /* diff between 0 and -33554432 */
3823                         if (diff >= -33554432) {
3824                                 diff >>= 2;
3825                                 ins = (ins & 0xff000000) | (diff & ~0xff000000);
3826                                 ins &= tmask;
3827                                 *code32 = ins | tbits;
3828                                 return;
3829                         }
3830                 }
3831                 
3832                 handle_thunk (domain, TRUE, code, target, dyn_code_mp);
3833                 return;
3834         }
3835
3836 #ifdef USE_JUMP_TABLES
3837         {
3838                 gpointer *jte = mono_jumptable_get_entry (code);
3839                 g_assert (jte);
3840                 jte [0] = (gpointer) target;
3841         }
3842 #else
3843         /*
3844          * The alternative call sequences looks like this:
3845          *
3846          *      ldr ip, [pc] // loads the address constant
3847          *      b 1f         // jumps around the constant
3848          *      address constant embedded in the code
3849          *   1f:
3850          *      mov lr, pc
3851          *      mov pc, ip
3852          *
3853          * There are two cases for patching:
3854          * a) at the end of method emission: in this case code points to the start
3855          *    of the call sequence
3856          * b) during runtime patching of the call site: in this case code points
3857          *    to the mov pc, ip instruction
3858          *
3859          * We have to handle also the thunk jump code sequence:
3860          *
3861          *      ldr ip, [pc]
3862          *      mov pc, ip
3863          *      address constant // execution never reaches here
3864          */
3865         if ((ins & 0x0ffffff0) == 0x12fff10) {
3866                 /* Branch and exchange: the address is constructed in a reg 
3867                  * We can patch BX when the code sequence is the following:
3868                  *  ldr     ip, [pc, #0]    ; 0x8
3869                  *  b       0xc
3870                  *  .word code_ptr
3871                  *  mov     lr, pc
3872                  *  bx      ips
3873                  * */
3874                 guint32 ccode [4];
3875                 guint8 *emit = (guint8*)ccode;
3876                 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3877                 ARM_B (emit, 0);
3878                 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3879                 ARM_BX (emit, ARMREG_IP);
3880
3881                 /*patching from magic trampoline*/
3882                 if (ins == ccode [3]) {
3883                         g_assert (code32 [-4] == ccode [0]);
3884                         g_assert (code32 [-3] == ccode [1]);
3885                         g_assert (code32 [-1] == ccode [2]);
3886                         code32 [-2] = (guint32)target;
3887                         return;
3888                 }
3889                 /*patching from JIT*/
3890                 if (ins == ccode [0]) {
3891                         g_assert (code32 [1] == ccode [1]);
3892                         g_assert (code32 [3] == ccode [2]);
3893                         g_assert (code32 [4] == ccode [3]);
3894                         code32 [2] = (guint32)target;
3895                         return;
3896                 }
3897                 g_assert_not_reached ();
3898         } else if ((ins & 0x0ffffff0) == 0x12fff30) {
3899                 /*
3900                  * ldr ip, [pc, #0]
3901                  * b 0xc
3902                  * .word code_ptr
3903                  * blx ip
3904                  */
3905                 guint32 ccode [4];
3906                 guint8 *emit = (guint8*)ccode;
3907                 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3908                 ARM_B (emit, 0);
3909                 ARM_BLX_REG (emit, ARMREG_IP);
3910
3911                 g_assert (code32 [-3] == ccode [0]);
3912                 g_assert (code32 [-2] == ccode [1]);
3913                 g_assert (code32 [0] == ccode [2]);
3914
3915                 code32 [-1] = (guint32)target;
3916         } else {
3917                 guint32 ccode [4];
3918                 guint32 *tmp = ccode;
3919                 guint8 *emit = (guint8*)tmp;
3920                 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3921                 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3922                 ARM_MOV_REG_REG (emit, ARMREG_PC, ARMREG_IP);
3923                 ARM_BX (emit, ARMREG_IP);
3924                 if (ins == ccode [2]) {
3925                         g_assert_not_reached (); // should be -2 ...
3926                         code32 [-1] = (guint32)target;
3927                         return;
3928                 }
3929                 if (ins == ccode [0]) {
3930                         /* handles both thunk jump code and the far call sequence */
3931                         code32 [2] = (guint32)target;
3932                         return;
3933                 }
3934                 g_assert_not_reached ();
3935         }
3936 //      g_print ("patched with 0x%08x\n", ins);
3937 #endif
3938 }
3939
3940 void
3941 arm_patch (guchar *code, const guchar *target)
3942 {
3943         arm_patch_general (NULL, code, target, NULL);
3944 }
3945
3946 /* 
3947  * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
3948  * (with the rotation amount in *rot_amount. rot_amount is already adjusted
3949  * to be used with the emit macros.
3950  * Return -1 otherwise.
3951  */
3952 int
3953 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
3954 {
3955         guint32 res, i;
3956         for (i = 0; i < 31; i+= 2) {
3957                 res = (val << (32 - i)) | (val >> i);
3958                 if (res & ~0xff)
3959                         continue;
3960                 *rot_amount = i? 32 - i: 0;
3961                 return res;
3962         }
3963         return -1;
3964 }
3965
3966 /*
3967  * Emits in code a sequence of instructions that load the value 'val'
3968  * into the dreg register. Uses at most 4 instructions.
3969  */
3970 guint8*
3971 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
3972 {
3973         int imm8, rot_amount;
3974 #if 0
3975         ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3976         /* skip the constant pool */
3977         ARM_B (code, 0);
3978         *(int*)code = val;
3979         code += 4;
3980         return code;
3981 #endif
3982         if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
3983                 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
3984         } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
3985                 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
3986         } else {
3987                 if (v7_supported) {
3988                         ARM_MOVW_REG_IMM (code, dreg, val & 0xffff);
3989                         if (val >> 16)
3990                                 ARM_MOVT_REG_IMM (code, dreg, (val >> 16) & 0xffff);
3991                         return code;
3992                 }
3993                 if (val & 0xFF) {
3994                         ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
3995                         if (val & 0xFF00) {
3996                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3997                         }
3998                         if (val & 0xFF0000) {
3999                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
4000                         }
4001                         if (val & 0xFF000000) {
4002                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
4003                         }
4004                 } else if (val & 0xFF00) {
4005                         ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
4006                         if (val & 0xFF0000) {
4007                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
4008                         }
4009                         if (val & 0xFF000000) {
4010                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
4011                         }
4012                 } else if (val & 0xFF0000) {
4013                         ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
4014                         if (val & 0xFF000000) {
4015                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
4016                         }
4017                 }
4018                 //g_assert_not_reached ();
4019         }
4020         return code;
4021 }
4022
4023 gboolean
4024 mono_arm_thumb_supported (void)
4025 {
4026         return thumb_supported;
4027 }
4028
4029 #ifndef DISABLE_JIT
4030
4031 /*
4032  * emit_load_volatile_arguments:
4033  *
4034  *  Load volatile arguments from the stack to the original input registers.
4035  * Required before a tail call.
4036  */
4037 static guint8*
4038 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
4039 {
4040         MonoMethod *method = cfg->method;
4041         MonoMethodSignature *sig;
4042         MonoInst *inst;
4043         CallInfo *cinfo;
4044         guint32 i, pos;
4045
4046         /* FIXME: Generate intermediate code instead */
4047
4048         sig = mono_method_signature (method);
4049
4050         /* This is the opposite of the code in emit_prolog */
4051
4052         pos = 0;
4053
4054         cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
4055
4056         if (cinfo->vtype_retaddr) {
4057                 ArgInfo *ainfo = &cinfo->ret;
4058                 inst = cfg->vret_addr;
4059                 g_assert (arm_is_imm12 (inst->inst_offset));
4060                 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4061         }
4062         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4063                 ArgInfo *ainfo = cinfo->args + i;
4064                 inst = cfg->args [pos];
4065                 
4066                 if (cfg->verbose_level > 2)
4067                         g_print ("Loading argument %d (type: %d)\n", i, ainfo->storage);
4068                 if (inst->opcode == OP_REGVAR) {
4069                         if (ainfo->storage == RegTypeGeneral)
4070                                 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
4071                         else if (ainfo->storage == RegTypeFP) {
4072                                 g_assert_not_reached ();
4073                         } else if (ainfo->storage == RegTypeBase) {
4074                                 // FIXME:
4075                                 NOT_IMPLEMENTED;
4076                                 /*
4077                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
4078                                         ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
4079                                 } else {
4080                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4081                                         ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
4082                                 }
4083                                 */
4084                         } else
4085                                 g_assert_not_reached ();
4086                 } else {
4087                         if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
4088                                 switch (ainfo->size) {
4089                                 case 1:
4090                                 case 2:
4091                                         // FIXME:
4092                                         NOT_IMPLEMENTED;
4093                                         break;
4094                                 case 8:
4095                                         g_assert (arm_is_imm12 (inst->inst_offset));
4096                                         ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4097                                         g_assert (arm_is_imm12 (inst->inst_offset + 4));
4098                                         ARM_LDR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
4099                                         break;
4100                                 default:
4101                                         if (arm_is_imm12 (inst->inst_offset)) {
4102                                                 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4103                                         } else {
4104                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
4105                                                 ARM_LDR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
4106                                         }
4107                                         break;
4108                                 }
4109                         } else if (ainfo->storage == RegTypeBaseGen) {
4110                                 // FIXME:
4111                                 NOT_IMPLEMENTED;
4112                         } else if (ainfo->storage == RegTypeBase) {
4113                                 /* Nothing to do */
4114                         } else if (ainfo->storage == RegTypeFP) {
4115                                 g_assert_not_reached ();
4116                         } else if (ainfo->storage == RegTypeStructByVal) {
4117                                 int doffset = inst->inst_offset;
4118                                 int soffset = 0;
4119                                 int cur_reg;
4120                                 int size = 0;
4121                                 if (mono_class_from_mono_type (inst->inst_vtype))
4122                                         size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
4123                                 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
4124                                         if (arm_is_imm12 (doffset)) {
4125                                                 ARM_LDR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
4126                                         } else {
4127                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
4128                                                 ARM_LDR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
4129                                         }
4130                                         soffset += sizeof (gpointer);
4131                                         doffset += sizeof (gpointer);
4132                                 }
4133                                 if (ainfo->vtsize)
4134                                         // FIXME:
4135                                         NOT_IMPLEMENTED;
4136                         } else if (ainfo->storage == RegTypeStructByAddr) {
4137                         } else {
4138                                 // FIXME:
4139                                 NOT_IMPLEMENTED;
4140                         }
4141                 }
4142                 pos ++;
4143         }
4144
4145         g_free (cinfo);
4146
4147         return code;
4148 }
4149
4150 void
4151 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
4152 {
4153         MonoInst *ins;
4154         MonoCallInst *call;
4155         guint offset;
4156         guint8 *code = cfg->native_code + cfg->code_len;
4157         MonoInst *last_ins = NULL;
4158         guint last_offset = 0;
4159         int max_len, cpos;
4160         int imm8, rot_amount;
4161
4162         /* we don't align basic blocks of loops on arm */
4163
4164         if (cfg->verbose_level > 2)
4165                 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
4166
4167         cpos = bb->max_offset;
4168
4169         if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
4170                 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
4171                 //g_assert (!mono_compile_aot);
4172                 //cpos += 6;
4173                 //if (bb->cil_code)
4174                 //      cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
4175                 /* this is not thread save, but good enough */
4176                 /* fixme: howto handle overflows? */
4177                 //x86_inc_mem (code, &cov->data [bb->dfn].count); 
4178         }
4179
4180     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) {
4181                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
4182                                                          (gpointer)"mono_break");
4183                 code = emit_call_seq (cfg, code);
4184         }
4185
4186         MONO_BB_FOR_EACH_INS (bb, ins) {
4187                 offset = code - cfg->native_code;
4188
4189                 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
4190
4191                 if (offset > (cfg->code_size - max_len - 16)) {
4192                         cfg->code_size *= 2;
4193                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4194                         code = cfg->native_code + offset;
4195                 }
4196         //      if (ins->cil_code)
4197         //              g_print ("cil code\n");
4198                 mono_debug_record_line_number (cfg, ins, offset);
4199
4200                 switch (ins->opcode) {
4201                 case OP_MEMORY_BARRIER:
4202                         if (v6_supported) {
4203                                 ARM_MOV_REG_IMM8 (code, ARMREG_R0, 0);
4204                                 ARM_MCR (code, 15, 0, ARMREG_R0, 7, 10, 5);
4205                         }
4206                         break;
4207                 case OP_TLS_GET:
4208 #ifdef HAVE_AEABI_READ_TP
4209                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
4210                                                                  (gpointer)"__aeabi_read_tp");
4211                         code = emit_call_seq (cfg, code);
4212
4213                         ARM_LDR_IMM (code, ins->dreg, ARMREG_R0, ins->inst_offset);
4214 #else
4215                         g_assert_not_reached ();
4216 #endif
4217                         break;
4218                 case OP_ATOMIC_EXCHANGE_I4:
4219                 case OP_ATOMIC_CAS_I4:
4220                 case OP_ATOMIC_ADD_I4: {
4221                         int tmpreg;
4222                         guint8 *buf [16];
4223
4224                         g_assert (v7_supported);
4225
4226                         /* Free up a reg */
4227                         if (ins->sreg1 != ARMREG_IP && ins->sreg2 != ARMREG_IP && ins->sreg3 != ARMREG_IP)
4228                                 tmpreg = ARMREG_IP;
4229                         else if (ins->sreg1 != ARMREG_R0 && ins->sreg2 != ARMREG_R0 && ins->sreg3 != ARMREG_R0)
4230                                 tmpreg = ARMREG_R0;
4231                         else if (ins->sreg1 != ARMREG_R1 && ins->sreg2 != ARMREG_R1 && ins->sreg3 != ARMREG_R1)
4232                                 tmpreg = ARMREG_R1;
4233                         else
4234                                 tmpreg = ARMREG_R2;
4235                         g_assert (cfg->arch.atomic_tmp_offset != -1);
4236                         ARM_STR_IMM (code, tmpreg, cfg->frame_reg, cfg->arch.atomic_tmp_offset);
4237
4238                         switch (ins->opcode) {
4239                         case OP_ATOMIC_EXCHANGE_I4:
4240                                 buf [0] = code;
4241                                 ARM_DMB (code, ARM_DMB_SY);
4242                                 ARM_LDREX_REG (code, ARMREG_LR, ins->sreg1);
4243                                 ARM_STREX_REG (code, tmpreg, ins->sreg2, ins->sreg1);
4244                                 ARM_CMP_REG_IMM (code, tmpreg, 0, 0);
4245                                 buf [1] = code;
4246                                 ARM_B_COND (code, ARMCOND_NE, 0);
4247                                 arm_patch (buf [1], buf [0]);
4248                                 break;
4249                         case OP_ATOMIC_CAS_I4:
4250                                 ARM_DMB (code, ARM_DMB_SY);
4251                                 buf [0] = code;
4252                                 ARM_LDREX_REG (code, ARMREG_LR, ins->sreg1);
4253                                 ARM_CMP_REG_REG (code, ARMREG_LR, ins->sreg3);
4254                                 buf [1] = code;
4255                                 ARM_B_COND (code, ARMCOND_NE, 0);
4256                                 ARM_STREX_REG (code, tmpreg, ins->sreg2, ins->sreg1);
4257                                 ARM_CMP_REG_IMM (code, tmpreg, 0, 0);
4258                                 buf [2] = code;
4259                                 ARM_B_COND (code, ARMCOND_NE, 0);
4260                                 arm_patch (buf [2], buf [0]);
4261                                 arm_patch (buf [1], code);
4262                                 break;
4263                         case OP_ATOMIC_ADD_I4:
4264                                 buf [0] = code;
4265                                 ARM_DMB (code, ARM_DMB_SY);
4266                                 ARM_LDREX_REG (code, ARMREG_LR, ins->sreg1);
4267                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->sreg2);
4268                                 ARM_STREX_REG (code, tmpreg, ARMREG_LR, ins->sreg1);
4269                                 ARM_CMP_REG_IMM (code, tmpreg, 0, 0);
4270                                 buf [1] = code;
4271                                 ARM_B_COND (code, ARMCOND_NE, 0);
4272                                 arm_patch (buf [1], buf [0]);
4273                                 break;
4274                         default:
4275                                 g_assert_not_reached ();
4276                         }
4277
4278                         ARM_DMB (code, ARM_DMB_SY);
4279                         if (tmpreg != ins->dreg)
4280                                 ARM_LDR_IMM (code, tmpreg, cfg->frame_reg, cfg->arch.atomic_tmp_offset);
4281                         ARM_MOV_REG_REG (code, ins->dreg, ARMREG_LR);
4282                         break;
4283                 }
4284                 case OP_ATOMIC_LOAD_I1:
4285                 case OP_ATOMIC_LOAD_U1:
4286                 case OP_ATOMIC_LOAD_I2:
4287                 case OP_ATOMIC_LOAD_U2:
4288                 case OP_ATOMIC_LOAD_I4:
4289                 case OP_ATOMIC_LOAD_U4:
4290                 case OP_ATOMIC_LOAD_R4:
4291                 case OP_ATOMIC_LOAD_R8: {
4292                         if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4293                                 ARM_DMB (code, ARM_DMB_SY);
4294
4295                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4296
4297                         switch (ins->opcode) {
4298                         case OP_ATOMIC_LOAD_I1:
4299                                 ARM_LDRSB_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4300                                 break;
4301                         case OP_ATOMIC_LOAD_U1:
4302                                 ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4303                                 break;
4304                         case OP_ATOMIC_LOAD_I2:
4305                                 ARM_LDRSH_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4306                                 break;
4307                         case OP_ATOMIC_LOAD_U2:
4308                                 ARM_LDRH_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4309                                 break;
4310                         case OP_ATOMIC_LOAD_I4:
4311                         case OP_ATOMIC_LOAD_U4:
4312                                 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4313                                 break;
4314                         case OP_ATOMIC_LOAD_R4:
4315                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
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_CVTD (code, vfp_scratch1, ins->sreg1);
4357                                 ARM_FSTS (code, vfp_scratch1, ins->inst_destbasereg, ins->inst_offset);
4358                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
4359                                 break;
4360                         case OP_ATOMIC_STORE_R8:
4361                                 ARM_ADD_REG_REG (code, ARMREG_LR, ins->inst_destbasereg, ARMREG_LR);
4362                                 ARM_FSTD (code, ins->sreg1, ARMREG_LR, 0);
4363                                 break;
4364                         }
4365
4366                         if (ins->backend.memory_barrier_kind == MONO_MEMORY_BARRIER_SEQ)
4367                                 ARM_DMB (code, ARM_DMB_SY);
4368                         break;
4369                 }
4370                 /*case OP_BIGMUL:
4371                         ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
4372                         ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
4373                         break;
4374                 case OP_BIGMUL_UN:
4375                         ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
4376                         ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
4377                         break;*/
4378                 case OP_STOREI1_MEMBASE_IMM:
4379                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
4380                         g_assert (arm_is_imm12 (ins->inst_offset));
4381                         ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
4382                         break;
4383                 case OP_STOREI2_MEMBASE_IMM:
4384                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
4385                         g_assert (arm_is_imm8 (ins->inst_offset));
4386                         ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
4387                         break;
4388                 case OP_STORE_MEMBASE_IMM:
4389                 case OP_STOREI4_MEMBASE_IMM:
4390                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
4391                         g_assert (arm_is_imm12 (ins->inst_offset));
4392                         ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
4393                         break;
4394                 case OP_STOREI1_MEMBASE_REG:
4395                         g_assert (arm_is_imm12 (ins->inst_offset));
4396                         ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4397                         break;
4398                 case OP_STOREI2_MEMBASE_REG:
4399                         g_assert (arm_is_imm8 (ins->inst_offset));
4400                         ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4401                         break;
4402                 case OP_STORE_MEMBASE_REG:
4403                 case OP_STOREI4_MEMBASE_REG:
4404                         /* this case is special, since it happens for spill code after lowering has been called */
4405                         if (arm_is_imm12 (ins->inst_offset)) {
4406                                 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4407                         } else {
4408                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4409                                 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
4410                         }
4411                         break;
4412                 case OP_STOREI1_MEMINDEX:
4413                         ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
4414                         break;
4415                 case OP_STOREI2_MEMINDEX:
4416                         ARM_STRH_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
4417                         break;
4418                 case OP_STORE_MEMINDEX:
4419                 case OP_STOREI4_MEMINDEX:
4420                         ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
4421                         break;
4422                 case OP_LOADU4_MEM:
4423                         g_assert_not_reached ();
4424                         break;
4425                 case OP_LOAD_MEMINDEX:
4426                 case OP_LOADI4_MEMINDEX:
4427                 case OP_LOADU4_MEMINDEX:
4428                         ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4429                         break;
4430                 case OP_LOADI1_MEMINDEX:
4431                         ARM_LDRSB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4432                         break;
4433                 case OP_LOADU1_MEMINDEX:
4434                         ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4435                         break;
4436                 case OP_LOADI2_MEMINDEX:
4437                         ARM_LDRSH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4438                         break;
4439                 case OP_LOADU2_MEMINDEX:
4440                         ARM_LDRH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
4441                         break;
4442                 case OP_LOAD_MEMBASE:
4443                 case OP_LOADI4_MEMBASE:
4444                 case OP_LOADU4_MEMBASE:
4445                         /* this case is special, since it happens for spill code after lowering has been called */
4446                         if (arm_is_imm12 (ins->inst_offset)) {
4447                                 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4448                         } else {
4449                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4450                                 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
4451                         }
4452                         break;
4453                 case OP_LOADI1_MEMBASE:
4454                         g_assert (arm_is_imm8 (ins->inst_offset));
4455                         ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4456                         break;
4457                 case OP_LOADU1_MEMBASE:
4458                         g_assert (arm_is_imm12 (ins->inst_offset));
4459                         ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4460                         break;
4461                 case OP_LOADU2_MEMBASE:
4462                         g_assert (arm_is_imm8 (ins->inst_offset));
4463                         ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4464                         break;
4465                 case OP_LOADI2_MEMBASE:
4466                         g_assert (arm_is_imm8 (ins->inst_offset));
4467                         ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4468                         break;
4469                 case OP_ICONV_TO_I1:
4470                         ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
4471                         ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
4472                         break;
4473                 case OP_ICONV_TO_I2:
4474                         ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
4475                         ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
4476                         break;
4477                 case OP_ICONV_TO_U1:
4478                         ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
4479                         break;
4480                 case OP_ICONV_TO_U2:
4481                         ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
4482                         ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
4483                         break;
4484                 case OP_COMPARE:
4485                 case OP_ICOMPARE:
4486                         ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
4487                         break;
4488                 case OP_COMPARE_IMM:
4489                 case OP_ICOMPARE_IMM:
4490                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4491                         g_assert (imm8 >= 0);
4492                         ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
4493                         break;
4494                 case OP_BREAK:
4495                         /*
4496                          * gdb does not like encountering the hw breakpoint ins in the debugged code. 
4497                          * So instead of emitting a trap, we emit a call a C function and place a 
4498                          * breakpoint there.
4499                          */
4500                         //*(int*)code = 0xef9f0001;
4501                         //code += 4;
4502                         //ARM_DBRK (code);
4503                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
4504                                                                  (gpointer)"mono_break");
4505                         code = emit_call_seq (cfg, code);
4506                         break;
4507                 case OP_RELAXED_NOP:
4508                         ARM_NOP (code);
4509                         break;
4510                 case OP_NOP:
4511                 case OP_DUMMY_USE:
4512                 case OP_DUMMY_STORE:
4513                 case OP_DUMMY_ICONST:
4514                 case OP_DUMMY_R8CONST:
4515                 case OP_NOT_REACHED:
4516                 case OP_NOT_NULL:
4517                         break;
4518                 case OP_IL_SEQ_POINT:
4519                         mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4520                         break;
4521                 case OP_SEQ_POINT: {
4522                         int i;
4523                         MonoInst *info_var = cfg->arch.seq_point_info_var;
4524                         MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
4525                         MonoInst *ss_read_var = cfg->arch.seq_point_read_var;
4526                         MonoInst *ss_method_var = cfg->arch.seq_point_ss_method_var;
4527                         MonoInst *bp_method_var = cfg->arch.seq_point_bp_method_var;
4528                         MonoInst *var;
4529                         int dreg = ARMREG_LR;
4530
4531                         if (cfg->soft_breakpoints) {
4532                                 g_assert (!cfg->compile_aot);
4533                         }
4534
4535                         /*
4536                          * For AOT, we use one got slot per method, which will point to a
4537                          * SeqPointInfo structure, containing all the information required
4538                          * by the code below.
4539                          */
4540                         if (cfg->compile_aot) {
4541                                 g_assert (info_var);
4542                                 g_assert (info_var->opcode == OP_REGOFFSET);
4543                                 g_assert (arm_is_imm12 (info_var->inst_offset));
4544                         }
4545
4546                         if (!cfg->soft_breakpoints) {
4547                                 /*
4548                                  * Read from the single stepping trigger page. This will cause a
4549                                  * SIGSEGV when single stepping is enabled.
4550                                  * We do this _before_ the breakpoint, so single stepping after
4551                                  * a breakpoint is hit will step to the next IL offset.
4552                                  */
4553                                 g_assert (((guint64)(gsize)ss_trigger_page >> 32) == 0);
4554                         }
4555
4556                         if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
4557                                 if (cfg->soft_breakpoints) {
4558                                         /* Load the address of the sequence point trigger variable. */
4559                                         var = ss_read_var;
4560                                         g_assert (var);
4561                                         g_assert (var->opcode == OP_REGOFFSET);
4562                                         g_assert (arm_is_imm12 (var->inst_offset));
4563                                         ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
4564
4565                                         /* Read the value and check whether it is non-zero. */
4566                                         ARM_LDR_IMM (code, dreg, dreg, 0);
4567                                         ARM_CMP_REG_IMM (code, dreg, 0, 0);
4568
4569                                         /* Load the address of the sequence point method. */
4570                                         var = ss_method_var;
4571                                         g_assert (var);
4572                                         g_assert (var->opcode == OP_REGOFFSET);
4573                                         g_assert (arm_is_imm12 (var->inst_offset));
4574                                         ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
4575
4576                                         /* Call it conditionally. */
4577                                         ARM_BLX_REG_COND (code, ARMCOND_NE, dreg);
4578                                 } else {
4579                                         if (cfg->compile_aot) {
4580                                                 /* Load the trigger page addr from the variable initialized in the prolog */
4581                                                 var = ss_trigger_page_var;
4582                                                 g_assert (var);
4583                                                 g_assert (var->opcode == OP_REGOFFSET);
4584                                                 g_assert (arm_is_imm12 (var->inst_offset));
4585                                                 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
4586                                         } else {
4587 #ifdef USE_JUMP_TABLES
4588                                                 gpointer *jte = mono_jumptable_add_entry ();
4589                                                 code = mono_arm_load_jumptable_entry (code, jte, dreg);
4590                                                 jte [0] = ss_trigger_page;
4591 #else
4592                                                 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
4593                                                 ARM_B (code, 0);
4594                                                 *(int*)code = (int)ss_trigger_page;
4595                                                 code += 4;
4596 #endif
4597                                         }
4598                                         ARM_LDR_IMM (code, dreg, dreg, 0);
4599                                 }
4600                         }
4601
4602                         mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
4603
4604                         if (cfg->soft_breakpoints) {
4605                                 /* Load the address of the breakpoint method into ip. */
4606                                 var = bp_method_var;
4607                                 g_assert (var);
4608                                 g_assert (var->opcode == OP_REGOFFSET);
4609                                 g_assert (arm_is_imm12 (var->inst_offset));
4610                                 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
4611
4612                                 /*
4613                                  * A placeholder for a possible breakpoint inserted by
4614                                  * mono_arch_set_breakpoint ().
4615                                  */
4616                                 ARM_NOP (code);
4617                         } else if (cfg->compile_aot) {
4618                                 guint32 offset = code - cfg->native_code;
4619                                 guint32 val;
4620
4621                                 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
4622                                 /* Add the offset */
4623                                 val = ((offset / 4) * sizeof (guint8*)) + MONO_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
4624                                 /* Load the info->bp_addrs [offset], which is either 0 or the address of a trigger page */
4625                                 if (arm_is_imm12 ((int)val)) {
4626                                         ARM_LDR_IMM (code, dreg, dreg, val);
4627                                 } else {
4628                                         ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF), 0);
4629                                         if (val & 0xFF00)
4630                                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
4631                                         if (val & 0xFF0000)
4632                                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
4633                                         g_assert (!(val & 0xFF000000));
4634
4635                                         ARM_LDR_IMM (code, dreg, dreg, 0);
4636                                 }
4637                                 /* What is faster, a branch or a load ? */
4638                                 ARM_CMP_REG_IMM (code, dreg, 0, 0);
4639                                 /* The breakpoint instruction */
4640                                 ARM_LDR_IMM_COND (code, dreg, dreg, 0, ARMCOND_NE);
4641                         } else {
4642                                 /* 
4643                                  * A placeholder for a possible breakpoint inserted by
4644                                  * mono_arch_set_breakpoint ().
4645                                  */
4646                                 for (i = 0; i < 4; ++i)
4647                                         ARM_NOP (code);
4648                         }
4649                         break;
4650                 }
4651                 case OP_ADDCC:
4652                 case OP_IADDCC:
4653                         ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4654                         break;
4655                 case OP_IADD:
4656                         ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4657                         break;
4658                 case OP_ADC:
4659                 case OP_IADC:
4660                         ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4661                         break;
4662                 case OP_ADDCC_IMM:
4663                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4664                         g_assert (imm8 >= 0);
4665                         ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4666                         break;
4667                 case OP_ADD_IMM:
4668                 case OP_IADD_IMM:
4669                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4670                         g_assert (imm8 >= 0);
4671                         ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4672                         break;
4673                 case OP_ADC_IMM:
4674                 case OP_IADC_IMM:
4675                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4676                         g_assert (imm8 >= 0);
4677                         ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4678                         break;
4679                 case OP_IADD_OVF:
4680                         ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4681                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4682                         break;
4683                 case OP_IADD_OVF_UN:
4684                         ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4685                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4686                         break;
4687                 case OP_ISUB_OVF:
4688                         ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4689                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4690                         break;
4691                 case OP_ISUB_OVF_UN:
4692                         ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4693                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
4694                         break;
4695                 case OP_ADD_OVF_CARRY:
4696                         ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4697                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4698                         break;
4699                 case OP_ADD_OVF_UN_CARRY:
4700                         ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4701                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4702                         break;
4703                 case OP_SUB_OVF_CARRY:
4704                         ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4705                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
4706                         break;
4707                 case OP_SUB_OVF_UN_CARRY:
4708                         ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4709                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
4710                         break;
4711                 case OP_SUBCC:
4712                 case OP_ISUBCC:
4713                         ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4714                         break;
4715                 case OP_SUBCC_IMM:
4716                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4717                         g_assert (imm8 >= 0);
4718                         ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4719                         break;
4720                 case OP_ISUB:
4721                         ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4722                         break;
4723                 case OP_SBB:
4724                 case OP_ISBB:
4725                         ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4726                         break;
4727                 case OP_SUB_IMM:
4728                 case OP_ISUB_IMM:
4729                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4730                         g_assert (imm8 >= 0);
4731                         ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4732                         break;
4733                 case OP_SBB_IMM:
4734                 case OP_ISBB_IMM:
4735                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4736                         g_assert (imm8 >= 0);
4737                         ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4738                         break;
4739                 case OP_ARM_RSBS_IMM:
4740                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4741                         g_assert (imm8 >= 0);
4742                         ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4743                         break;
4744                 case OP_ARM_RSC_IMM:
4745                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4746                         g_assert (imm8 >= 0);
4747                         ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4748                         break;
4749                 case OP_IAND:
4750                         ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4751                         break;
4752                 case OP_AND_IMM:
4753                 case OP_IAND_IMM:
4754                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4755                         g_assert (imm8 >= 0);
4756                         ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4757                         break;
4758                 case OP_IDIV:
4759                         g_assert (v7s_supported);
4760                         ARM_SDIV (code, ins->dreg, ins->sreg1, ins->sreg2);
4761                         break;
4762                 case OP_IDIV_UN:
4763                         g_assert (v7s_supported);
4764                         ARM_UDIV (code, ins->dreg, ins->sreg1, ins->sreg2);
4765                         break;
4766                 case OP_IREM:
4767                         g_assert (v7s_supported);
4768                         ARM_SDIV (code, ARMREG_LR, ins->sreg1, ins->sreg2);
4769                         ARM_MLS (code, ins->dreg, ARMREG_LR, ins->sreg2, ins->sreg1);
4770                         break;
4771                 case OP_IREM_UN:
4772                         g_assert (v7s_supported);
4773                         ARM_UDIV (code, ARMREG_LR, ins->sreg1, ins->sreg2);
4774                         ARM_MLS (code, ins->dreg, ARMREG_LR, ins->sreg2, ins->sreg1);
4775                         break;
4776                 case OP_DIV_IMM:
4777                 case OP_REM_IMM:
4778                         g_assert_not_reached ();
4779                 case OP_IOR:
4780                         ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4781                         break;
4782                 case OP_OR_IMM:
4783                 case OP_IOR_IMM:
4784                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4785                         g_assert (imm8 >= 0);
4786                         ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4787                         break;
4788                 case OP_IXOR:
4789                         ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4790                         break;
4791                 case OP_XOR_IMM:
4792                 case OP_IXOR_IMM:
4793                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
4794                         g_assert (imm8 >= 0);
4795                         ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
4796                         break;
4797                 case OP_ISHL:
4798                         ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4799                         break;
4800                 case OP_SHL_IMM:
4801                 case OP_ISHL_IMM:
4802                         if (ins->inst_imm)
4803                                 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
4804                         else if (ins->dreg != ins->sreg1)
4805                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4806                         break;
4807                 case OP_ISHR:
4808                         ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4809                         break;
4810                 case OP_SHR_IMM:
4811                 case OP_ISHR_IMM:
4812                         if (ins->inst_imm)
4813                                 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
4814                         else if (ins->dreg != ins->sreg1)
4815                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4816                         break;
4817                 case OP_SHR_UN_IMM:
4818                 case OP_ISHR_UN_IMM:
4819                         if (ins->inst_imm)
4820                                 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
4821                         else if (ins->dreg != ins->sreg1)
4822                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4823                         break;
4824                 case OP_ISHR_UN:
4825                         ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4826                         break;
4827                 case OP_INOT:
4828                         ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
4829                         break;
4830                 case OP_INEG:
4831                         ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
4832                         break;
4833                 case OP_IMUL:
4834                         if (ins->dreg == ins->sreg2)
4835                                 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4836                         else
4837                                 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
4838                         break;
4839                 case OP_MUL_IMM:
4840                         g_assert_not_reached ();
4841                         break;
4842                 case OP_IMUL_OVF:
4843                         /* FIXME: handle ovf/ sreg2 != dreg */
4844                         ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4845                         /* FIXME: MUL doesn't set the C/O flags on ARM */
4846                         break;
4847                 case OP_IMUL_OVF_UN:
4848                         /* FIXME: handle ovf/ sreg2 != dreg */
4849                         ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4850                         /* FIXME: MUL doesn't set the C/O flags on ARM */
4851                         break;
4852                 case OP_ICONST:
4853                         code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
4854                         break;
4855                 case OP_AOTCONST:
4856                         /* Load the GOT offset */
4857                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4858                         ARM_LDR_IMM (code, ins->dreg, ARMREG_PC, 0);
4859                         ARM_B (code, 0);
4860                         *(gpointer*)code = NULL;
4861                         code += 4;
4862                         /* Load the value from the GOT */
4863                         ARM_LDR_REG_REG (code, ins->dreg, ARMREG_PC, ins->dreg);
4864                         break;
4865                 case OP_OBJC_GET_SELECTOR:
4866                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_OBJC_SELECTOR_REF, ins->inst_p0);
4867                         ARM_LDR_IMM (code, ins->dreg, ARMREG_PC, 0);
4868                         ARM_B (code, 0);
4869                         *(gpointer*)code = NULL;
4870                         code += 4;
4871                         ARM_LDR_REG_REG (code, ins->dreg, ARMREG_PC, ins->dreg);
4872                         break;
4873                 case OP_ICONV_TO_I4:
4874                 case OP_ICONV_TO_U4:
4875                 case OP_MOVE:
4876                         if (ins->dreg != ins->sreg1)
4877                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4878                         break;
4879                 case OP_SETLRET: {
4880                         int saved = ins->sreg2;
4881                         if (ins->sreg2 == ARM_LSW_REG) {
4882                                 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
4883                                 saved = ARMREG_LR;
4884                         }
4885                         if (ins->sreg1 != ARM_LSW_REG)
4886                                 ARM_MOV_REG_REG (code, ARM_LSW_REG, ins->sreg1);
4887                         if (saved != ARM_MSW_REG)
4888                                 ARM_MOV_REG_REG (code, ARM_MSW_REG, saved);
4889                         break;
4890                 }
4891                 case OP_FMOVE:
4892                         if (IS_VFP && ins->dreg != ins->sreg1)
4893                                 ARM_CPYD (code, ins->dreg, ins->sreg1);
4894                         break;
4895                 case OP_MOVE_F_TO_I4:
4896                         code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
4897                         ARM_CVTD (code, vfp_scratch1, ins->sreg1);
4898                         ARM_FMRS (code, ins->dreg, vfp_scratch1);
4899                         code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
4900                         break;
4901                 case OP_MOVE_I4_TO_F:
4902                         ARM_FMSR (code, ins->dreg, ins->sreg1);
4903                         ARM_CVTS (code, ins->dreg, ins->dreg);
4904                         break;
4905                 case OP_FCONV_TO_R4:
4906                         if (IS_VFP) {
4907                                 if (cfg->r4fp) {
4908                                         ARM_CVTD (code, ins->dreg, ins->sreg1);
4909                                 } else {
4910                                         ARM_CVTD (code, ins->dreg, ins->sreg1);
4911                                         ARM_CVTS (code, ins->dreg, ins->dreg);
4912                                 }
4913                         }
4914                         break;
4915                 case OP_JMP:
4916                         /*
4917                          * Keep in sync with mono_arch_emit_epilog
4918                          */
4919                         g_assert (!cfg->method->save_lmf);
4920
4921                         code = emit_load_volatile_arguments (cfg, code);
4922
4923                         code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
4924                         if (iphone_abi) {
4925                                 if (cfg->used_int_regs)
4926                                         ARM_POP (code, cfg->used_int_regs);
4927                                 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
4928                         } else {
4929                                 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_LR));
4930                         }
4931                         mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
4932                         if (cfg->compile_aot) {
4933                                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
4934                                 ARM_B (code, 0);
4935                                 *(gpointer*)code = NULL;
4936                                 code += 4;
4937                                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
4938                         } else {
4939                                 code = mono_arm_patchable_b (code, ARMCOND_AL);
4940                         }
4941                         break;
4942                 case OP_TAILCALL: {
4943                         MonoCallInst *call = (MonoCallInst*)ins;
4944
4945                         /*
4946                          * The stack looks like the following:
4947                          * <caller argument area>
4948                          * <saved regs etc>
4949                          * <rest of frame>
4950                          * <callee argument area>
4951                          * Need to copy the arguments from the callee argument area to
4952                          * the caller argument area, and pop the frame.
4953                          */
4954                         if (call->stack_usage) {
4955                                 int i, prev_sp_offset = 0;
4956
4957                                 /* Compute size of saved registers restored below */
4958                                 if (iphone_abi)
4959                                         prev_sp_offset = 2 * 4;
4960                                 else
4961                                         prev_sp_offset = 1 * 4;
4962                                 for (i = 0; i < 16; ++i) {
4963                                         if (cfg->used_int_regs & (1 << i))
4964                                                 prev_sp_offset += 4;
4965                                 }
4966
4967                                 code = emit_big_add (code, ARMREG_IP, cfg->frame_reg, cfg->stack_usage + prev_sp_offset);
4968
4969                                 /* Copy arguments on the stack to our argument area */
4970                                 for (i = 0; i < call->stack_usage; i += sizeof (mgreg_t)) {
4971                                         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, i);
4972                                         ARM_STR_IMM (code, ARMREG_LR, ARMREG_IP, i);
4973                                 }
4974                         }
4975
4976                         /*
4977                          * Keep in sync with mono_arch_emit_epilog
4978                          */
4979                         g_assert (!cfg->method->save_lmf);
4980
4981                         code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
4982                         if (iphone_abi) {
4983                                 if (cfg->used_int_regs)
4984                                         ARM_POP (code, cfg->used_int_regs);
4985                                 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
4986                         } else {
4987                                 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_LR));
4988                         }
4989
4990                         mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, call->method);
4991                         if (cfg->compile_aot) {
4992                                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
4993                                 ARM_B (code, 0);
4994                                 *(gpointer*)code = NULL;
4995                                 code += 4;
4996                                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
4997                         } else {
4998                                 code = mono_arm_patchable_b (code, ARMCOND_AL);
4999                         }
5000                         break;
5001                 }
5002                 case OP_CHECK_THIS:
5003                         /* ensure ins->sreg1 is not NULL */
5004                         ARM_LDRB_IMM (code, ARMREG_LR, ins->sreg1, 0);
5005                         break;
5006                 case OP_ARGLIST: {
5007                         g_assert (cfg->sig_cookie < 128);
5008                         ARM_LDR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
5009                         ARM_STR_IMM (code, ARMREG_IP, ins->sreg1, 0);
5010                         break;
5011                 }
5012                 case OP_FCALL:
5013                 case OP_RCALL:
5014                 case OP_LCALL:
5015                 case OP_VCALL:
5016                 case OP_VCALL2:
5017                 case OP_VOIDCALL:
5018                 case OP_CALL:
5019                         call = (MonoCallInst*)ins;
5020
5021                         if (IS_HARD_FLOAT)
5022                                 code = emit_float_args (cfg, call, code, &max_len, &offset);
5023
5024                         if (ins->flags & MONO_INST_HAS_METHOD)
5025                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
5026                         else
5027                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
5028                         code = emit_call_seq (cfg, code);
5029                         ins->flags |= MONO_INST_GC_CALLSITE;
5030                         ins->backend.pc_offset = code - cfg->native_code;
5031                         code = emit_move_return_value (cfg, ins, code);
5032                         break;
5033                 case OP_FCALL_REG:
5034                 case OP_RCALL_REG:
5035                 case OP_LCALL_REG:
5036                 case OP_VCALL_REG:
5037                 case OP_VCALL2_REG:
5038                 case OP_VOIDCALL_REG:
5039                 case OP_CALL_REG:
5040                         if (IS_HARD_FLOAT)
5041                                 code = emit_float_args (cfg, (MonoCallInst *)ins, code, &max_len, &offset);
5042
5043                         code = emit_call_reg (code, ins->sreg1);
5044                         ins->flags |= MONO_INST_GC_CALLSITE;
5045                         ins->backend.pc_offset = code - cfg->native_code;
5046                         code = emit_move_return_value (cfg, ins, code);
5047                         break;
5048                 case OP_FCALL_MEMBASE:
5049                 case OP_RCALL_MEMBASE:
5050                 case OP_LCALL_MEMBASE:
5051                 case OP_VCALL_MEMBASE:
5052                 case OP_VCALL2_MEMBASE:
5053                 case OP_VOIDCALL_MEMBASE:
5054                 case OP_CALL_MEMBASE: {
5055                         gboolean imt_arg = FALSE;
5056
5057                         g_assert (ins->sreg1 != ARMREG_LR);
5058                         call = (MonoCallInst*)ins;
5059
5060                         if (IS_HARD_FLOAT)
5061                                 code = emit_float_args (cfg, call, code, &max_len, &offset);
5062
5063                         if (call->dynamic_imt_arg || call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
5064                                 imt_arg = TRUE;
5065                         if (!arm_is_imm12 (ins->inst_offset))
5066                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, ins->inst_offset);
5067 #ifdef USE_JUMP_TABLES
5068 #define LR_BIAS 0
5069 #else
5070 #define LR_BIAS 4
5071 #endif
5072                         if (imt_arg)
5073                                 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_PC, LR_BIAS);
5074                         else
5075                                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
5076 #undef LR_BIAS
5077                         if (!arm_is_imm12 (ins->inst_offset))
5078                                 ARM_LDR_REG_REG (code, ARMREG_PC, ins->sreg1, ARMREG_IP);
5079                         else
5080                                 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
5081                         if (imt_arg) {
5082                                 /* 
5083                                  * We can't embed the method in the code stream in PIC code, or
5084                                  * in gshared code.
5085                                  * Instead, we put it in V5 in code emitted by 
5086                                  * mono_arch_emit_imt_argument (), and embed NULL here to 
5087                                  * signal the IMT thunk that the value is in V5.
5088                                  */
5089 #ifdef USE_JUMP_TABLES
5090                                 /* In case of jumptables we always use value in V5. */
5091 #else
5092
5093                                 if (call->dynamic_imt_arg)
5094                                         *((gpointer*)code) = NULL;
5095                                 else
5096                                         *((gpointer*)code) = (gpointer)call->method;
5097                                 code += 4;
5098 #endif
5099                         }
5100                         ins->flags |= MONO_INST_GC_CALLSITE;
5101                         ins->backend.pc_offset = code - cfg->native_code;
5102                         code = emit_move_return_value (cfg, ins, code);
5103                         break;
5104                 }
5105                 case OP_LOCALLOC: {
5106                         /* round the size to 8 bytes */
5107                         ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
5108                         ARM_BIC_REG_IMM8 (code, ins->dreg, ins->dreg, 7);
5109                         ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
5110                         /* memzero the area: dreg holds the size, sp is the pointer */
5111                         if (ins->flags & MONO_INST_INIT) {
5112                                 guint8 *start_loop, *branch_to_cond;
5113                                 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
5114                                 branch_to_cond = code;
5115                                 ARM_B (code, 0);
5116                                 start_loop = code;
5117                                 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
5118                                 arm_patch (branch_to_cond, code);
5119                                 /* decrement by 4 and set flags */
5120                                 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, sizeof (mgreg_t));
5121                                 ARM_B_COND (code, ARMCOND_GE, 0);
5122                                 arm_patch (code - 4, start_loop);
5123                         }
5124                         ARM_MOV_REG_REG (code, ins->dreg, ARMREG_SP);
5125                         if (cfg->param_area)
5126                                 code = emit_sub_imm (code, ARMREG_SP, ARMREG_SP, ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT));
5127                         break;
5128                 }
5129                 case OP_DYN_CALL: {
5130                         int i;
5131                         MonoInst *var = cfg->dyn_call_var;
5132
5133                         g_assert (var->opcode == OP_REGOFFSET);
5134                         g_assert (arm_is_imm12 (var->inst_offset));
5135
5136                         /* lr = args buffer filled by mono_arch_get_dyn_call_args () */
5137                         ARM_MOV_REG_REG( code, ARMREG_LR, ins->sreg1);
5138                         /* ip = ftn */
5139                         ARM_MOV_REG_REG( code, ARMREG_IP, ins->sreg2);
5140
5141                         /* Save args buffer */
5142                         ARM_STR_IMM (code, ARMREG_LR, var->inst_basereg, var->inst_offset);
5143
5144                         /* Set stack slots using R0 as scratch reg */
5145                         /* MONO_ARCH_DYN_CALL_PARAM_AREA gives the size of stack space available */
5146                         for (i = 0; i < DYN_CALL_STACK_ARGS; ++i) {
5147                                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, (PARAM_REGS + i) * sizeof (mgreg_t));
5148                                 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, i * sizeof (mgreg_t));
5149                         }
5150
5151                         /* Set argument registers */
5152                         for (i = 0; i < PARAM_REGS; ++i)
5153                                 ARM_LDR_IMM (code, i, ARMREG_LR, i * sizeof (mgreg_t));
5154
5155                         /* Make the call */
5156                         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
5157                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
5158
5159                         /* Save result */
5160                         ARM_LDR_IMM (code, ARMREG_IP, var->inst_basereg, var->inst_offset);
5161                         ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, MONO_STRUCT_OFFSET (DynCallArgs, res)); 
5162                         ARM_STR_IMM (code, ARMREG_R1, ARMREG_IP, MONO_STRUCT_OFFSET (DynCallArgs, res2)); 
5163                         break;
5164                 }
5165                 case OP_THROW: {
5166                         if (ins->sreg1 != ARMREG_R0)
5167                                 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
5168                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
5169                                              (gpointer)"mono_arch_throw_exception");
5170                         code = emit_call_seq (cfg, code);
5171                         break;
5172                 }
5173                 case OP_RETHROW: {
5174                         if (ins->sreg1 != ARMREG_R0)
5175                                 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
5176                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
5177                                              (gpointer)"mono_arch_rethrow_exception");
5178                         code = emit_call_seq (cfg, code);
5179                         break;
5180                 }
5181                 case OP_START_HANDLER: {
5182                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5183                         int i, rot_amount;
5184
5185                         /* Reserve a param area, see filter-stack.exe */
5186                         if (cfg->param_area) {
5187                                 if ((i = mono_arm_is_rotated_imm8 (cfg->param_area, &rot_amount)) >= 0) {
5188                                         ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
5189                                 } else {
5190                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->param_area);
5191                                         ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
5192                                 }
5193                         }
5194
5195                         if (arm_is_imm12 (spvar->inst_offset)) {
5196                                 ARM_STR_IMM (code, ARMREG_LR, spvar->inst_basereg, spvar->inst_offset);
5197                         } else {
5198                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
5199                                 ARM_STR_REG_REG (code, ARMREG_LR, spvar->inst_basereg, ARMREG_IP);
5200                         }
5201                         break;
5202                 }
5203                 case OP_ENDFILTER: {
5204                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5205                         int i, rot_amount;
5206
5207                         /* Free the param area */
5208                         if (cfg->param_area) {
5209                                 if ((i = mono_arm_is_rotated_imm8 (cfg->param_area, &rot_amount)) >= 0) {
5210                                         ARM_ADD_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
5211                                 } else {
5212                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->param_area);
5213                                         ARM_ADD_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
5214                                 }
5215                         }
5216
5217                         if (ins->sreg1 != ARMREG_R0)
5218                                 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
5219                         if (arm_is_imm12 (spvar->inst_offset)) {
5220                                 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
5221                         } else {
5222                                 g_assert (ARMREG_IP != spvar->inst_basereg);
5223                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
5224                                 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
5225                         }
5226                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
5227                         break;
5228                 }
5229                 case OP_ENDFINALLY: {
5230                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
5231                         int i, rot_amount;
5232
5233                         /* Free the param area */
5234                         if (cfg->param_area) {
5235                                 if ((i = mono_arm_is_rotated_imm8 (cfg->param_area, &rot_amount)) >= 0) {
5236                                         ARM_ADD_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
5237                                 } else {
5238                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->param_area);
5239                                         ARM_ADD_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
5240                                 }
5241                         }
5242
5243                         if (arm_is_imm12 (spvar->inst_offset)) {
5244                                 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
5245                         } else {
5246                                 g_assert (ARMREG_IP != spvar->inst_basereg);
5247                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
5248                                 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
5249                         }
5250                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
5251                         break;
5252                 }
5253                 case OP_CALL_HANDLER: 
5254                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5255                         code = mono_arm_patchable_bl (code, ARMCOND_AL);
5256                         mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
5257                         break;
5258                 case OP_LABEL:
5259                         ins->inst_c0 = code - cfg->native_code;
5260                         break;
5261                 case OP_BR:
5262                         /*if (ins->inst_target_bb->native_offset) {
5263                                 ARM_B (code, 0);
5264                                 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset); 
5265                         } else*/ {
5266                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
5267                                 code = mono_arm_patchable_b (code, ARMCOND_AL);
5268                         } 
5269                         break;
5270                 case OP_BR_REG:
5271                         ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
5272                         break;
5273                 case OP_SWITCH:
5274                         /* 
5275                          * In the normal case we have:
5276                          *      ldr pc, [pc, ins->sreg1 << 2]
5277                          *      nop
5278                          * If aot, we have:
5279                          *      ldr lr, [pc, ins->sreg1 << 2]
5280                          *      add pc, pc, lr
5281                          * After follows the data.
5282                          * FIXME: add aot support.
5283                          */
5284                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_SWITCH, ins->inst_p0);
5285 #ifdef USE_JUMP_TABLES
5286                         {
5287                                 gpointer *jte = mono_jumptable_add_entries (GPOINTER_TO_INT (ins->klass));
5288                                 code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_IP);
5289                                 ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_IP, ins->sreg1, ARMSHIFT_LSL, 2);
5290                         }
5291 #else
5292
5293                         max_len += 4 * GPOINTER_TO_INT (ins->klass);
5294                         if (offset + max_len > (cfg->code_size - 16)) {
5295                                 cfg->code_size += max_len;
5296                                 cfg->code_size *= 2;
5297                                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5298                                 code = cfg->native_code + offset;
5299                         }
5300                         ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
5301                         ARM_NOP (code);
5302                         code += 4 * GPOINTER_TO_INT (ins->klass);
5303 #endif
5304                         break;
5305                 case OP_CEQ:
5306                 case OP_ICEQ:
5307                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
5308                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
5309                         break;
5310                 case OP_CLT:
5311                 case OP_ICLT:
5312                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5313                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
5314                         break;
5315                 case OP_CLT_UN:
5316                 case OP_ICLT_UN:
5317                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5318                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
5319                         break;
5320                 case OP_CGT:
5321                 case OP_ICGT:
5322                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5323                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
5324                         break;
5325                 case OP_CGT_UN:
5326                 case OP_ICGT_UN:
5327                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5328                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
5329                         break;
5330                 case OP_ICNEQ:
5331                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_NE);
5332                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_EQ);
5333                         break;
5334                 case OP_ICGE:
5335                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5336                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_LT);
5337                         break;
5338                 case OP_ICLE:
5339                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5340                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_GT);
5341                         break;
5342                 case OP_ICGE_UN:
5343                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5344                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_LO);
5345                         break;
5346                 case OP_ICLE_UN:
5347                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5348                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_HI);
5349                         break;
5350                 case OP_COND_EXC_EQ:
5351                 case OP_COND_EXC_NE_UN:
5352                 case OP_COND_EXC_LT:
5353                 case OP_COND_EXC_LT_UN:
5354                 case OP_COND_EXC_GT:
5355                 case OP_COND_EXC_GT_UN:
5356                 case OP_COND_EXC_GE:
5357                 case OP_COND_EXC_GE_UN:
5358                 case OP_COND_EXC_LE:
5359                 case OP_COND_EXC_LE_UN:
5360                         EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
5361                         break;
5362                 case OP_COND_EXC_IEQ:
5363                 case OP_COND_EXC_INE_UN:
5364                 case OP_COND_EXC_ILT:
5365                 case OP_COND_EXC_ILT_UN:
5366                 case OP_COND_EXC_IGT:
5367                 case OP_COND_EXC_IGT_UN:
5368                 case OP_COND_EXC_IGE:
5369                 case OP_COND_EXC_IGE_UN:
5370                 case OP_COND_EXC_ILE:
5371                 case OP_COND_EXC_ILE_UN:
5372                         EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_IEQ, ins->inst_p1);
5373                         break;
5374                 case OP_COND_EXC_C:
5375                 case OP_COND_EXC_IC:
5376                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CS, ins->inst_p1);
5377                         break;
5378                 case OP_COND_EXC_OV:
5379                 case OP_COND_EXC_IOV:
5380                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, ins->inst_p1);
5381                         break;
5382                 case OP_COND_EXC_NC:
5383                 case OP_COND_EXC_INC:
5384                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CC, ins->inst_p1);
5385                         break;
5386                 case OP_COND_EXC_NO:
5387                 case OP_COND_EXC_INO:
5388                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VC, ins->inst_p1);
5389                         break;
5390                 case OP_IBEQ:
5391                 case OP_IBNE_UN:
5392                 case OP_IBLT:
5393                 case OP_IBLT_UN:
5394                 case OP_IBGT:
5395                 case OP_IBGT_UN:
5396                 case OP_IBGE:
5397                 case OP_IBGE_UN:
5398                 case OP_IBLE:
5399                 case OP_IBLE_UN:
5400                         EMIT_COND_BRANCH (ins, ins->opcode - OP_IBEQ);
5401                         break;
5402
5403                 /* floating point opcodes */
5404                 case OP_R8CONST:
5405                         if (cfg->compile_aot) {
5406                                 ARM_FLDD (code, ins->dreg, ARMREG_PC, 0);
5407                                 ARM_B (code, 1);
5408                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
5409                                 code += 4;
5410                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
5411                                 code += 4;
5412                         } else {
5413                                 /* FIXME: we can optimize the imm load by dealing with part of 
5414                                  * the displacement in LDFD (aligning to 512).
5415                                  */
5416                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
5417                                 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
5418                         }
5419                         break;
5420                 case OP_R4CONST:
5421                         if (cfg->compile_aot) {
5422                                 ARM_FLDS (code, ins->dreg, ARMREG_PC, 0);
5423                                 ARM_B (code, 0);
5424                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
5425                                 code += 4;
5426                                 if (!cfg->r4fp)
5427                                         ARM_CVTS (code, ins->dreg, ins->dreg);
5428                         } else {
5429                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
5430                                 ARM_FLDS (code, ins->dreg, ARMREG_LR, 0);
5431                                 if (!cfg->r4fp)
5432                                         ARM_CVTS (code, ins->dreg, ins->dreg);
5433                         }
5434                         break;
5435                 case OP_STORER8_MEMBASE_REG:
5436                         /* This is generated by the local regalloc pass which runs after the lowering pass */
5437                         if (!arm_is_fpimm8 (ins->inst_offset)) {
5438                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5439                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
5440                                 ARM_FSTD (code, ins->sreg1, ARMREG_LR, 0);
5441                         } else {
5442                                 ARM_FSTD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
5443                         }
5444                         break;
5445                 case OP_LOADR8_MEMBASE:
5446                         /* This is generated by the local regalloc pass which runs after the lowering pass */
5447                         if (!arm_is_fpimm8 (ins->inst_offset)) {
5448                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5449                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
5450                                 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
5451                         } else {
5452                                 ARM_FLDD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5453                         }
5454                         break;
5455                 case OP_STORER4_MEMBASE_REG:
5456                         g_assert (arm_is_fpimm8 (ins->inst_offset));
5457                         if (cfg->r4fp) {
5458                                 ARM_FSTS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
5459                         } else {
5460                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5461                                 ARM_CVTD (code, vfp_scratch1, ins->sreg1);
5462                                 ARM_FSTS (code, vfp_scratch1, ins->inst_destbasereg, ins->inst_offset);
5463                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5464                         }
5465                         break;
5466                 case OP_LOADR4_MEMBASE:
5467                         if (cfg->r4fp) {
5468                                 ARM_FLDS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
5469                         } else {
5470                                 g_assert (arm_is_fpimm8 (ins->inst_offset));
5471                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5472                                 ARM_FLDS (code, vfp_scratch1, ins->inst_basereg, ins->inst_offset);
5473                                 ARM_CVTS (code, ins->dreg, vfp_scratch1);
5474                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5475                         }
5476                         break;
5477                 case OP_ICONV_TO_R_UN: {
5478                         g_assert_not_reached ();
5479                         break;
5480                 }
5481                 case OP_ICONV_TO_R4:
5482                         if (cfg->r4fp) {
5483                                 ARM_FMSR (code, ins->dreg, ins->sreg1);
5484                                 ARM_FSITOS (code, ins->dreg, ins->dreg);
5485                         } else {
5486                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5487                                 ARM_FMSR (code, vfp_scratch1, ins->sreg1);
5488                                 ARM_FSITOS (code, vfp_scratch1, vfp_scratch1);
5489                                 ARM_CVTS (code, ins->dreg, vfp_scratch1);
5490                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5491                         }
5492                         break;
5493                 case OP_ICONV_TO_R8:
5494                         code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5495                         ARM_FMSR (code, vfp_scratch1, ins->sreg1);
5496                         ARM_FSITOD (code, ins->dreg, vfp_scratch1);
5497                         code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5498                         break;
5499
5500                 case OP_SETFRET: {
5501                         MonoType *sig_ret = mini_type_get_underlying_type (NULL, mono_method_signature (cfg->method)->ret);
5502                         if (sig_ret->type == MONO_TYPE_R4) {
5503                                 if (cfg->r4fp) {
5504                                         g_assert (!IS_HARD_FLOAT);
5505                                         ARM_FMRS (code, ARMREG_R0, ins->sreg1);
5506                                 } else {
5507                                         ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
5508
5509                                         if (!IS_HARD_FLOAT)
5510                                                 ARM_FMRS (code, ARMREG_R0, ARM_VFP_F0);
5511                                 }
5512                         } else {
5513                                 if (IS_HARD_FLOAT)
5514                                         ARM_CPYD (code, ARM_VFP_D0, ins->sreg1);
5515                                 else
5516                                         ARM_FMRRD (code, ARMREG_R0, ARMREG_R1, ins->sreg1);
5517                         }
5518                         break;
5519                 }
5520                 case OP_FCONV_TO_I1:
5521                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5522                         break;
5523                 case OP_FCONV_TO_U1:
5524                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5525                         break;
5526                 case OP_FCONV_TO_I2:
5527                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5528                         break;
5529                 case OP_FCONV_TO_U2:
5530                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5531                         break;
5532                 case OP_FCONV_TO_I4:
5533                 case OP_FCONV_TO_I:
5534                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5535                         break;
5536                 case OP_FCONV_TO_U4:
5537                 case OP_FCONV_TO_U:
5538                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5539                         break;
5540                 case OP_FCONV_TO_I8:
5541                 case OP_FCONV_TO_U8:
5542                         g_assert_not_reached ();
5543                         /* Implemented as helper calls */
5544                         break;
5545                 case OP_LCONV_TO_R_UN:
5546                         g_assert_not_reached ();
5547                         /* Implemented as helper calls */
5548                         break;
5549                 case OP_LCONV_TO_OVF_I4_2: {
5550                         guint8 *high_bit_not_set, *valid_negative, *invalid_negative, *valid_positive;
5551                         /* 
5552                          * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
5553                          */
5554
5555                         ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
5556                         high_bit_not_set = code;
5557                         ARM_B_COND (code, ARMCOND_GE, 0); /*branch if bit 31 of the lower part is not set*/
5558
5559                         ARM_CMN_REG_IMM8 (code, ins->sreg2, 1); /*This have the same effect as CMP reg, 0xFFFFFFFF */
5560                         valid_negative = code;
5561                         ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0xFFFFFFFF (lower part has bit 31 set) */
5562                         invalid_negative = code;
5563                         ARM_B_COND (code, ARMCOND_AL, 0);
5564                         
5565                         arm_patch (high_bit_not_set, code);
5566
5567                         ARM_CMP_REG_IMM8 (code, ins->sreg2, 0);
5568                         valid_positive = code;
5569                         ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0 (lower part has bit 31 clear)*/
5570
5571                         arm_patch (invalid_negative, code);
5572                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_AL, "OverflowException");
5573
5574                         arm_patch (valid_negative, code);
5575                         arm_patch (valid_positive, code);
5576
5577                         if (ins->dreg != ins->sreg1)
5578                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
5579                         break;
5580                 }
5581                 case OP_FADD:
5582                         ARM_VFP_ADDD (code, ins->dreg, ins->sreg1, ins->sreg2);
5583                         break;
5584                 case OP_FSUB:
5585                         ARM_VFP_SUBD (code, ins->dreg, ins->sreg1, ins->sreg2);
5586                         break;          
5587                 case OP_FMUL:
5588                         ARM_VFP_MULD (code, ins->dreg, ins->sreg1, ins->sreg2);
5589                         break;          
5590                 case OP_FDIV:
5591                         ARM_VFP_DIVD (code, ins->dreg, ins->sreg1, ins->sreg2);
5592                         break;          
5593                 case OP_FNEG:
5594                         ARM_NEGD (code, ins->dreg, ins->sreg1);
5595                         break;
5596                 case OP_FREM:
5597                         /* emulated */
5598                         g_assert_not_reached ();
5599                         break;
5600                 case OP_FCOMPARE:
5601                         if (IS_VFP) {
5602                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5603                                 ARM_FMSTAT (code);
5604                         }
5605                         break;
5606                 case OP_RCOMPARE:
5607                         g_assert (IS_VFP);
5608                         ARM_CMPS (code, ins->sreg1, ins->sreg2);
5609                         ARM_FMSTAT (code);
5610                         break;
5611                 case OP_FCEQ:
5612                         if (IS_VFP) {
5613                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5614                                 ARM_FMSTAT (code);
5615                         }
5616                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
5617                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
5618                         break;
5619                 case OP_FCLT:
5620                         if (IS_VFP) {
5621                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5622                                 ARM_FMSTAT (code);
5623                         }
5624                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5625                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5626                         break;
5627                 case OP_FCLT_UN:
5628                         if (IS_VFP) {
5629                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5630                                 ARM_FMSTAT (code);
5631                         }
5632                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5633                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5634                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
5635                         break;
5636                 case OP_FCGT:
5637                         if (IS_VFP) {
5638                                 ARM_CMPD (code, ins->sreg2, ins->sreg1);
5639                                 ARM_FMSTAT (code);
5640                         }
5641                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5642                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5643                         break;
5644                 case OP_FCGT_UN:
5645                         if (IS_VFP) {
5646                                 ARM_CMPD (code, ins->sreg2, ins->sreg1);
5647                                 ARM_FMSTAT (code);
5648                         }
5649                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5650                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5651                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
5652                         break;
5653                 case OP_FCNEQ:
5654                         if (IS_VFP) {
5655                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5656                                 ARM_FMSTAT (code);
5657                         }
5658                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_NE);
5659                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_EQ);
5660                         break;
5661                 case OP_FCGE:
5662                         if (IS_VFP) {
5663                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
5664                                 ARM_FMSTAT (code);
5665                         }
5666                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5667                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_MI);
5668                         break;
5669                 case OP_FCLE:
5670                         if (IS_VFP) {
5671                                 ARM_CMPD (code, ins->sreg2, ins->sreg1);
5672                                 ARM_FMSTAT (code);
5673                         }
5674                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5675                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_MI);
5676                         break;
5677
5678                 /* ARM FPA flags table:
5679                  * N        Less than               ARMCOND_MI
5680                  * Z        Equal                   ARMCOND_EQ
5681                  * C        Greater Than or Equal   ARMCOND_CS
5682                  * V        Unordered               ARMCOND_VS
5683                  */
5684                 case OP_FBEQ:
5685                         EMIT_COND_BRANCH (ins, OP_IBEQ - OP_IBEQ);
5686                         break;
5687                 case OP_FBNE_UN:
5688                         EMIT_COND_BRANCH (ins, OP_IBNE_UN - OP_IBEQ);
5689                         break;
5690                 case OP_FBLT:
5691                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
5692                         break;
5693                 case OP_FBLT_UN:
5694                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
5695                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
5696                         break;
5697                 case OP_FBGT:
5698                 case OP_FBGT_UN:
5699                 case OP_FBLE:
5700                 case OP_FBLE_UN:
5701                         g_assert_not_reached ();
5702                         break;
5703                 case OP_FBGE:
5704                         if (IS_VFP) {
5705                                 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
5706                         } else {
5707                                 /* FPA requires EQ even thou the docs suggests that just CS is enough */
5708                                 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_EQ);
5709                                 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS);
5710                         }
5711                         break;
5712                 case OP_FBGE_UN:
5713                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
5714                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
5715                         break;
5716
5717                 case OP_CKFINITE: {
5718                         if (IS_VFP) {
5719                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch1);
5720                                 code = mono_arm_emit_vfp_scratch_save (cfg, code, vfp_scratch2);
5721
5722 #ifdef USE_JUMP_TABLES
5723                                 {
5724                                         gpointer *jte = mono_jumptable_add_entries (2);
5725                                         jte [0] = GUINT_TO_POINTER (0xffffffff);
5726                                         jte [1] = GUINT_TO_POINTER (0x7fefffff);
5727                                         code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_IP);
5728                                         ARM_FLDD (code, vfp_scratch1, ARMREG_IP, 0);
5729                                 }
5730 #else
5731                                 ARM_ABSD (code, vfp_scratch2, ins->sreg1);
5732                                 ARM_FLDD (code, vfp_scratch1, ARMREG_PC, 0);
5733                                 ARM_B (code, 1);
5734                                 *(guint32*)code = 0xffffffff;
5735                                 code += 4;
5736                                 *(guint32*)code = 0x7fefffff;
5737                                 code += 4;
5738 #endif
5739                                 ARM_CMPD (code, vfp_scratch2, vfp_scratch1);
5740                                 ARM_FMSTAT (code);
5741                                 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_GT, "ArithmeticException");
5742                                 ARM_CMPD (code, ins->sreg1, ins->sreg1);
5743                                 ARM_FMSTAT (code);
5744                                 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, "ArithmeticException");
5745                                 ARM_CPYD (code, ins->dreg, ins->sreg1);
5746
5747                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch1);
5748                                 code = mono_arm_emit_vfp_scratch_restore (cfg, code, vfp_scratch2);
5749                         }
5750                         break;
5751                 }
5752
5753                 case OP_RCONV_TO_I1:
5754                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
5755                         break;
5756                 case OP_RCONV_TO_U1:
5757                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
5758                         break;
5759                 case OP_RCONV_TO_I2:
5760                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
5761                         break;
5762                 case OP_RCONV_TO_U2:
5763                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
5764                         break;
5765                 case OP_RCONV_TO_I4:
5766                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
5767                         break;
5768                 case OP_RCONV_TO_U4:
5769                         code = emit_r4_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
5770                         break;
5771                 case OP_RCONV_TO_R4:
5772                         g_assert (IS_VFP);
5773                         if (ins->dreg != ins->sreg1)
5774                                 ARM_CPYS (code, ins->dreg, ins->sreg1);
5775                         break;
5776                 case OP_RCONV_TO_R8:
5777                         g_assert (IS_VFP);
5778                         ARM_CVTS (code, ins->dreg, ins->sreg1);
5779                         break;
5780                 case OP_RADD:
5781                         ARM_VFP_ADDS (code, ins->dreg, ins->sreg1, ins->sreg2);
5782                         break;
5783                 case OP_RSUB:
5784                         ARM_VFP_SUBS (code, ins->dreg, ins->sreg1, ins->sreg2);
5785                         break;          
5786                 case OP_RMUL:
5787                         ARM_VFP_MULS (code, ins->dreg, ins->sreg1, ins->sreg2);
5788                         break;          
5789                 case OP_RDIV:
5790                         ARM_VFP_DIVS (code, ins->dreg, ins->sreg1, ins->sreg2);
5791                         break;          
5792                 case OP_RNEG:
5793                         ARM_NEGS (code, ins->dreg, ins->sreg1);
5794                         break;
5795                 case OP_RCEQ:
5796                         if (IS_VFP) {
5797                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5798                                 ARM_FMSTAT (code);
5799                         }
5800                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
5801                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
5802                         break;
5803                 case OP_RCLT:
5804                         if (IS_VFP) {
5805                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5806                                 ARM_FMSTAT (code);
5807                         }
5808                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5809                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5810                         break;
5811                 case OP_RCLT_UN:
5812                         if (IS_VFP) {
5813                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5814                                 ARM_FMSTAT (code);
5815                         }
5816                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5817                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5818                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
5819                         break;
5820                 case OP_RCGT:
5821                         if (IS_VFP) {
5822                                 ARM_CMPS (code, ins->sreg2, ins->sreg1);
5823                                 ARM_FMSTAT (code);
5824                         }
5825                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5826                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5827                         break;
5828                 case OP_RCGT_UN:
5829                         if (IS_VFP) {
5830                                 ARM_CMPS (code, ins->sreg2, ins->sreg1);
5831                                 ARM_FMSTAT (code);
5832                         }
5833                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
5834                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
5835                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
5836                         break;
5837                 case OP_RCNEQ:
5838                         if (IS_VFP) {
5839                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5840                                 ARM_FMSTAT (code);
5841                         }
5842                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_NE);
5843                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_EQ);
5844                         break;
5845                 case OP_RCGE:
5846                         if (IS_VFP) {
5847                                 ARM_CMPS (code, ins->sreg1, ins->sreg2);
5848                                 ARM_FMSTAT (code);
5849                         }
5850                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5851                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_MI);
5852                         break;
5853                 case OP_RCLE:
5854                         if (IS_VFP) {
5855                                 ARM_CMPS (code, ins->sreg2, ins->sreg1);
5856                                 ARM_FMSTAT (code);
5857                         }
5858                         ARM_MOV_REG_IMM8 (code, ins->dreg, 1);
5859                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_MI);
5860                         break;
5861
5862                 case OP_GC_LIVENESS_DEF:
5863                 case OP_GC_LIVENESS_USE:
5864                 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
5865                         ins->backend.pc_offset = code - cfg->native_code;
5866                         break;
5867                 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
5868                         ins->backend.pc_offset = code - cfg->native_code;
5869                         bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
5870                         break;
5871
5872                 default:
5873                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
5874                         g_assert_not_reached ();
5875                 }
5876
5877                 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
5878                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
5879                                    mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
5880                         g_assert_not_reached ();
5881                 }
5882                
5883                 cpos += max_len;
5884
5885                 last_ins = ins;
5886                 last_offset = offset;
5887         }
5888
5889         cfg->code_len = code - cfg->native_code;
5890 }
5891
5892 #endif /* DISABLE_JIT */
5893
5894 #ifdef HAVE_AEABI_READ_TP
5895 void __aeabi_read_tp (void);
5896 #endif
5897
5898 void
5899 mono_arch_register_lowlevel_calls (void)
5900 {
5901         /* The signature doesn't matter */
5902         mono_register_jit_icall (mono_arm_throw_exception, "mono_arm_throw_exception", mono_create_icall_signature ("void"), TRUE);
5903         mono_register_jit_icall (mono_arm_throw_exception_by_token, "mono_arm_throw_exception_by_token", mono_create_icall_signature ("void"), TRUE);
5904
5905 #ifndef MONO_CROSS_COMPILE
5906 #ifdef HAVE_AEABI_READ_TP
5907         mono_register_jit_icall (__aeabi_read_tp, "__aeabi_read_tp", mono_create_icall_signature ("void"), TRUE);
5908 #endif
5909 #endif
5910 }
5911
5912 #define patch_lis_ori(ip,val) do {\
5913                 guint16 *__lis_ori = (guint16*)(ip);    \
5914                 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff;      \
5915                 __lis_ori [3] = ((guint32)(val)) & 0xffff;      \
5916         } while (0)
5917
5918 void
5919 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
5920 {
5921         MonoJumpInfo *patch_info;
5922         gboolean compile_aot = !run_cctors;
5923
5924         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
5925                 unsigned char *ip = patch_info->ip.i + code;
5926                 const unsigned char *target;
5927
5928                 if (patch_info->type == MONO_PATCH_INFO_SWITCH && !compile_aot) {
5929 #ifdef USE_JUMP_TABLES
5930                         gpointer *jt = mono_jumptable_get_entry (ip);
5931 #else
5932                         gpointer *jt = (gpointer*)(ip + 8);
5933 #endif
5934                         int i;
5935                         /* jt is the inlined jump table, 2 instructions after ip
5936                          * In the normal case we store the absolute addresses,
5937                          * otherwise the displacements.
5938                          */
5939                         for (i = 0; i < patch_info->data.table->table_size; i++)
5940                                 jt [i] = code + (int)patch_info->data.table->table [i];
5941                         continue;
5942                 }
5943
5944                 if (compile_aot) {
5945                         switch (patch_info->type) {
5946                         case MONO_PATCH_INFO_BB:
5947                         case MONO_PATCH_INFO_LABEL:
5948                                 break;
5949                         default:
5950                                 /* No need to patch these */
5951                                 continue;
5952                         }
5953                 }
5954
5955                 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
5956
5957                 switch (patch_info->type) {
5958                 case MONO_PATCH_INFO_IP:
5959                         g_assert_not_reached ();
5960                         patch_lis_ori (ip, ip);
5961                         continue;
5962                 case MONO_PATCH_INFO_METHOD_REL:
5963                         g_assert_not_reached ();
5964                         *((gpointer *)(ip)) = code + patch_info->data.offset;
5965                         continue;
5966                 case MONO_PATCH_INFO_METHODCONST:
5967                 case MONO_PATCH_INFO_CLASS:
5968                 case MONO_PATCH_INFO_IMAGE:
5969                 case MONO_PATCH_INFO_FIELD:
5970                 case MONO_PATCH_INFO_VTABLE:
5971                 case MONO_PATCH_INFO_IID:
5972                 case MONO_PATCH_INFO_SFLDA:
5973                 case MONO_PATCH_INFO_LDSTR:
5974                 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
5975                 case MONO_PATCH_INFO_LDTOKEN:
5976                         g_assert_not_reached ();
5977                         /* from OP_AOTCONST : lis + ori */
5978                         patch_lis_ori (ip, target);
5979                         continue;
5980                 case MONO_PATCH_INFO_R4:
5981                 case MONO_PATCH_INFO_R8:
5982                         g_assert_not_reached ();
5983                         *((gconstpointer *)(ip + 2)) = patch_info->data.target;
5984                         continue;
5985                 case MONO_PATCH_INFO_EXC_NAME:
5986                         g_assert_not_reached ();
5987                         *((gconstpointer *)(ip + 1)) = patch_info->data.name;
5988                         continue;
5989                 case MONO_PATCH_INFO_NONE:
5990                 case MONO_PATCH_INFO_BB_OVF:
5991                 case MONO_PATCH_INFO_EXC_OVF:
5992                         /* everything is dealt with at epilog output time */
5993                         continue;
5994                 default:
5995                         break;
5996                 }
5997                 arm_patch_general (domain, ip, target, dyn_code_mp);
5998         }
5999 }
6000
6001 #ifndef DISABLE_JIT
6002
6003 /*
6004  * Stack frame layout:
6005  * 
6006  *   ------------------- fp
6007  *      MonoLMF structure or saved registers
6008  *   -------------------
6009  *      locals
6010  *   -------------------
6011  *      spilled regs
6012  *   -------------------
6013  *      optional 8 bytes for tracing
6014  *   -------------------
6015  *      param area             size is cfg->param_area
6016  *   ------------------- sp
6017  */
6018 guint8 *
6019 mono_arch_emit_prolog (MonoCompile *cfg)
6020 {
6021         MonoMethod *method = cfg->method;
6022         MonoBasicBlock *bb;
6023         MonoMethodSignature *sig;
6024         MonoInst *inst;
6025         int alloc_size, orig_alloc_size, pos, max_offset, i, rot_amount;
6026         guint8 *code;
6027         CallInfo *cinfo;
6028         int tracing = 0;
6029         int lmf_offset = 0;
6030         int prev_sp_offset, reg_offset;
6031
6032         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
6033                 tracing = 1;
6034
6035         sig = mono_method_signature (method);
6036         cfg->code_size = 256 + sig->param_count * 64;
6037         code = cfg->native_code = g_malloc (cfg->code_size);
6038
6039         mono_emit_unwind_op_def_cfa (cfg, code, ARMREG_SP, 0);
6040
6041         alloc_size = cfg->stack_offset;
6042         pos = 0;
6043         prev_sp_offset = 0;
6044
6045         if (iphone_abi) {
6046                 /* 
6047                  * The iphone uses R7 as the frame pointer, and it points at the saved
6048                  * r7+lr:
6049                  *         <lr>
6050                  * r7 ->   <r7>
6051                  *         <rest of frame>
6052                  * We can't use r7 as a frame pointer since it points into the middle of
6053                  * the frame, so we keep using our own frame pointer.
6054                  * FIXME: Optimize this.
6055                  */
6056                 ARM_PUSH (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
6057                 ARM_MOV_REG_REG (code, ARMREG_R7, ARMREG_SP);
6058                 prev_sp_offset += 8; /* r7 and lr */
6059                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
6060                 mono_emit_unwind_op_offset (cfg, code, ARMREG_R7, (- prev_sp_offset) + 0);
6061         }
6062
6063         if (!method->save_lmf) {
6064                 if (iphone_abi) {
6065                         /* No need to push LR again */
6066                         if (cfg->used_int_regs)
6067                                 ARM_PUSH (code, cfg->used_int_regs);
6068                 } else {
6069                         ARM_PUSH (code, cfg->used_int_regs | (1 << ARMREG_LR));
6070                         prev_sp_offset += 4;
6071                 }
6072                 for (i = 0; i < 16; ++i) {
6073                         if (cfg->used_int_regs & (1 << i))
6074                                 prev_sp_offset += 4;
6075                 }
6076                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
6077                 reg_offset = 0;
6078                 for (i = 0; i < 16; ++i) {
6079                         if ((cfg->used_int_regs & (1 << i))) {
6080                                 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
6081                                 mini_gc_set_slot_type_from_cfa (cfg, (- prev_sp_offset) + reg_offset, SLOT_NOREF);
6082                                 reg_offset += 4;
6083                         }
6084                 }
6085                 if (iphone_abi) {
6086                         mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
6087                         mini_gc_set_slot_type_from_cfa (cfg, -4, SLOT_NOREF);
6088                 } else {
6089                         mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
6090                         mini_gc_set_slot_type_from_cfa (cfg, -4, SLOT_NOREF);
6091                 }
6092         } else {
6093                 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
6094                 ARM_PUSH (code, 0x5ff0);
6095                 prev_sp_offset += 4 * 10; /* all but r0-r3, sp and pc */
6096                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
6097                 reg_offset = 0;
6098                 for (i = 0; i < 16; ++i) {
6099                         if ((i > ARMREG_R3) && (i != ARMREG_SP) && (i != ARMREG_PC)) {
6100                                 /* The original r7 is saved at the start */
6101                                 if (!(iphone_abi && i == ARMREG_R7))
6102                                         mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
6103                                 reg_offset += 4;
6104                         }
6105                 }
6106                 g_assert (reg_offset == 4 * 10);
6107                 pos += sizeof (MonoLMF) - (4 * 10);
6108                 lmf_offset = pos;
6109         }
6110         alloc_size += pos;
6111         orig_alloc_size = alloc_size;
6112         // align to MONO_ARCH_FRAME_ALIGNMENT bytes
6113         if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
6114                 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
6115                 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
6116         }
6117
6118         /* the stack used in the pushed regs */
6119         if (prev_sp_offset & 4)
6120                 alloc_size += 4;
6121         cfg->stack_usage = alloc_size;
6122         if (alloc_size) {
6123                 if ((i = mono_arm_is_rotated_imm8 (alloc_size, &rot_amount)) >= 0) {
6124                         ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
6125                 } else {
6126                         code = mono_arm_emit_load_imm (code, ARMREG_IP, alloc_size);
6127                         ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
6128                 }
6129                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset + alloc_size);
6130         }
6131         if (cfg->frame_reg != ARMREG_SP) {
6132                 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
6133                 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
6134         }
6135         //g_print ("prev_sp_offset: %d, alloc_size:%d\n", prev_sp_offset, alloc_size);
6136         prev_sp_offset += alloc_size;
6137
6138         for (i = 0; i < alloc_size - orig_alloc_size; i += 4)
6139                 mini_gc_set_slot_type_from_cfa (cfg, (- prev_sp_offset) + orig_alloc_size + i, SLOT_NOREF);
6140
6141         /* compute max_offset in order to use short forward jumps
6142          * we could skip do it on arm because the immediate displacement
6143          * for jumps is large enough, it may be useful later for constant pools
6144          */
6145         max_offset = 0;
6146         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6147                 MonoInst *ins = bb->code;
6148                 bb->max_offset = max_offset;
6149
6150                 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6151                         max_offset += 6; 
6152
6153                 MONO_BB_FOR_EACH_INS (bb, ins)
6154                         max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
6155         }
6156
6157         /* store runtime generic context */
6158         if (cfg->rgctx_var) {
6159                 MonoInst *ins = cfg->rgctx_var;
6160
6161                 g_assert (ins->opcode == OP_REGOFFSET);
6162
6163                 if (arm_is_imm12 (ins->inst_offset)) {
6164                         ARM_STR_IMM (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ins->inst_offset);
6165                 } else {
6166                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
6167                         ARM_STR_REG_REG (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ARMREG_LR);
6168                 }
6169         }
6170
6171         /* load arguments allocated to register from the stack */
6172         pos = 0;
6173
6174         cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
6175
6176         if (cinfo->vtype_retaddr) {
6177                 ArgInfo *ainfo = &cinfo->ret;
6178                 inst = cfg->vret_addr;
6179                 g_assert (arm_is_imm12 (inst->inst_offset));
6180                 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6181         }
6182
6183         if (sig->call_convention == MONO_CALL_VARARG) {
6184                 ArgInfo *cookie = &cinfo->sig_cookie;
6185
6186                 /* Save the sig cookie address */
6187                 g_assert (cookie->storage == RegTypeBase);
6188
6189                 g_assert (arm_is_imm12 (prev_sp_offset + cookie->offset));
6190                 g_assert (arm_is_imm12 (cfg->sig_cookie));
6191                 ARM_ADD_REG_IMM8 (code, ARMREG_IP, cfg->frame_reg, prev_sp_offset + cookie->offset);
6192                 ARM_STR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
6193         }
6194
6195         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6196                 ArgInfo *ainfo = cinfo->args + i;
6197                 inst = cfg->args [pos];
6198                 
6199                 if (cfg->verbose_level > 2)
6200                         g_print ("Saving argument %d (type: %d)\n", i, ainfo->storage);
6201                 if (inst->opcode == OP_REGVAR) {
6202                         if (ainfo->storage == RegTypeGeneral)
6203                                 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
6204                         else if (ainfo->storage == RegTypeFP) {
6205                                 g_assert_not_reached ();
6206                         } else if (ainfo->storage == RegTypeBase) {
6207                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
6208                                         ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
6209                                 } else {
6210                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
6211                                         ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
6212                                 }
6213                         } else
6214                                 g_assert_not_reached ();
6215
6216                         if (cfg->verbose_level > 2)
6217                                 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
6218                 } else {
6219                         /* the argument should be put on the stack: FIXME handle size != word  */
6220                         if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair || ainfo->storage == RegTypeGSharedVtInReg) {
6221                                 switch (ainfo->size) {
6222                                 case 1:
6223                                         if (arm_is_imm12 (inst->inst_offset))
6224                                                 ARM_STRB_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6225                                         else {
6226                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6227                                                 ARM_STRB_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
6228                                         }
6229                                         break;
6230                                 case 2:
6231                                         if (arm_is_imm8 (inst->inst_offset)) {
6232                                                 ARM_STRH_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6233                                         } else {
6234                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6235                                                 ARM_STRH_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
6236                                         }
6237                                         break;
6238                                 case 8:
6239                                         if (arm_is_imm12 (inst->inst_offset)) {
6240                                                 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6241                                         } else {
6242                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6243                                                 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
6244                                         }
6245                                         if (arm_is_imm12 (inst->inst_offset + 4)) {
6246                                                 ARM_STR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
6247                                         } else {
6248                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
6249                                                 ARM_STR_REG_REG (code, ainfo->reg + 1, inst->inst_basereg, ARMREG_IP);
6250                                         }
6251                                         break;
6252                                 default:
6253                                         if (arm_is_imm12 (inst->inst_offset)) {
6254                                                 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
6255                                         } else {
6256                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6257                                                 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
6258                                         }
6259                                         break;
6260                                 }
6261                         } else if (ainfo->storage == RegTypeBaseGen) {
6262                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
6263                                         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
6264                                 } else {
6265                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
6266                                         ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
6267                                 }
6268                                 if (arm_is_imm12 (inst->inst_offset + 4)) {
6269                                         ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
6270                                         ARM_STR_IMM (code, ARMREG_R3, inst->inst_basereg, inst->inst_offset);
6271                                 } else {
6272                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
6273                                         ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6274                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6275                                         ARM_STR_REG_REG (code, ARMREG_R3, inst->inst_basereg, ARMREG_IP);
6276                                 }
6277                         } else if (ainfo->storage == RegTypeBase || ainfo->storage == RegTypeGSharedVtOnStack) {
6278                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
6279                                         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
6280                                 } else {
6281                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
6282                                         ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
6283                                 }
6284
6285                                 switch (ainfo->size) {
6286                                 case 1:
6287                                         if (arm_is_imm8 (inst->inst_offset)) {
6288                                                 ARM_STRB_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
6289                                         } else {
6290                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6291                                                 ARM_STRB_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6292                                         }
6293                                         break;
6294                                 case 2:
6295                                         if (arm_is_imm8 (inst->inst_offset)) {
6296                                                 ARM_STRH_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
6297                                         } else {
6298                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6299                                                 ARM_STRH_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6300                                         }
6301                                         break;
6302                                 case 8:
6303                                         if (arm_is_imm12 (inst->inst_offset)) {
6304                                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
6305                                         } else {
6306                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6307                                                 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6308                                         }
6309                                         if (arm_is_imm12 (prev_sp_offset + ainfo->offset + 4)) {
6310                                                 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset + 4));
6311                                         } else {
6312                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset + 4);
6313                                                 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
6314                                         }
6315                                         if (arm_is_imm12 (inst->inst_offset + 4)) {
6316                                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
6317                                         } else {
6318                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
6319                                                 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6320                                         }
6321                                         break;
6322                                 default:
6323                                         if (arm_is_imm12 (inst->inst_offset)) {
6324                                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
6325                                         } else {
6326                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6327                                                 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
6328                                         }
6329                                         break;
6330                                 }
6331                         } else if (ainfo->storage == RegTypeFP) {
6332                                 int imm8, rot_amount;
6333
6334                                 if ((imm8 = mono_arm_is_rotated_imm8 (inst->inst_offset, &rot_amount)) == -1) {
6335                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
6336                                         ARM_ADD_REG_REG (code, ARMREG_IP, ARMREG_IP, inst->inst_basereg);
6337                                 } else
6338                                         ARM_ADD_REG_IMM (code, ARMREG_IP, inst->inst_basereg, imm8, rot_amount);
6339
6340                                 if (ainfo->size == 8)
6341                                         ARM_FSTD (code, ainfo->reg, ARMREG_IP, 0);
6342                                 else
6343                                         ARM_FSTS (code, ainfo->reg, ARMREG_IP, 0);
6344                         } else if (ainfo->storage == RegTypeStructByVal) {
6345                                 int doffset = inst->inst_offset;
6346                                 int soffset = 0;
6347                                 int cur_reg;
6348                                 int size = 0;
6349                                 size = mini_type_stack_size_full (cfg->generic_sharing_context, inst->inst_vtype, NULL, sig->pinvoke);
6350                                 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
6351                                         if (arm_is_imm12 (doffset)) {
6352                                                 ARM_STR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
6353                                         } else {
6354                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
6355                                                 ARM_STR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
6356                                         }
6357                                         soffset += sizeof (gpointer);
6358                                         doffset += sizeof (gpointer);
6359                                 }
6360                                 if (ainfo->vtsize) {
6361                                         /* FIXME: handle overrun! with struct sizes not multiple of 4 */
6362                                         //g_print ("emit_memcpy (prev_sp_ofs: %d, ainfo->offset: %d, soffset: %d)\n", prev_sp_offset, ainfo->offset, soffset);
6363                                         code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ARMREG_SP, prev_sp_offset + ainfo->offset);
6364                                 }
6365                         } else if (ainfo->storage == RegTypeStructByAddr) {
6366                                 g_assert_not_reached ();
6367                                 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
6368                                 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
6369                         } else
6370                                 g_assert_not_reached ();
6371                 }
6372                 pos++;
6373         }
6374
6375         if (method->save_lmf)
6376                 code = emit_save_lmf (cfg, code, alloc_size - lmf_offset);
6377
6378         if (tracing)
6379                 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
6380
6381         if (cfg->arch.seq_point_info_var) {
6382                 MonoInst *ins = cfg->arch.seq_point_info_var;
6383
6384                 /* Initialize the variable from a GOT slot */
6385                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
6386 #ifdef USE_JUMP_TABLES
6387                 {
6388                         gpointer *jte = mono_jumptable_add_entry ();
6389                         code = mono_arm_load_jumptable_entry (code, jte, ARMREG_IP);
6390                         ARM_LDR_IMM (code, ARMREG_R0, ARMREG_IP, 0);
6391                 }
6392                 /** XXX: is it correct? */
6393 #else
6394                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
6395                 ARM_B (code, 0);
6396                 *(gpointer*)code = NULL;
6397                 code += 4;
6398 #endif
6399                 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
6400
6401                 g_assert (ins->opcode == OP_REGOFFSET);
6402
6403                 if (arm_is_imm12 (ins->inst_offset)) {
6404                         ARM_STR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
6405                 } else {
6406                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
6407                         ARM_STR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
6408                 }
6409         }
6410
6411         /* Initialize ss_trigger_page_var */
6412         if (!cfg->soft_breakpoints) {
6413                 MonoInst *info_var = cfg->arch.seq_point_info_var;
6414                 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
6415                 int dreg = ARMREG_LR;
6416
6417                 if (info_var) {
6418                         g_assert (info_var->opcode == OP_REGOFFSET);
6419                         g_assert (arm_is_imm12 (info_var->inst_offset));
6420
6421                         ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
6422                         /* Load the trigger page addr */
6423                         ARM_LDR_IMM (code, dreg, dreg, MONO_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page));
6424                         ARM_STR_IMM (code, dreg, ss_trigger_page_var->inst_basereg, ss_trigger_page_var->inst_offset);
6425                 }
6426         }
6427
6428         if (cfg->arch.seq_point_read_var) {
6429                 MonoInst *read_ins = cfg->arch.seq_point_read_var;
6430                 MonoInst *ss_method_ins = cfg->arch.seq_point_ss_method_var;
6431                 MonoInst *bp_method_ins = cfg->arch.seq_point_bp_method_var;
6432 #ifdef USE_JUMP_TABLES
6433                 gpointer *jte;
6434 #endif
6435                 g_assert (read_ins->opcode == OP_REGOFFSET);
6436                 g_assert (arm_is_imm12 (read_ins->inst_offset));
6437                 g_assert (ss_method_ins->opcode == OP_REGOFFSET);
6438                 g_assert (arm_is_imm12 (ss_method_ins->inst_offset));
6439                 g_assert (bp_method_ins->opcode == OP_REGOFFSET);
6440                 g_assert (arm_is_imm12 (bp_method_ins->inst_offset));
6441
6442 #ifdef USE_JUMP_TABLES
6443                 jte = mono_jumptable_add_entries (3);
6444                 jte [0] = (gpointer)&ss_trigger_var;
6445                 jte [1] = single_step_func_wrapper;
6446                 jte [2] = breakpoint_func_wrapper;
6447                 code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_LR);
6448 #else
6449                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
6450                 ARM_B (code, 2);
6451                 *(volatile int **)code = &ss_trigger_var;
6452                 code += 4;
6453                 *(gpointer*)code = single_step_func_wrapper;
6454                 code += 4;
6455                 *(gpointer*)code = breakpoint_func_wrapper;
6456                 code += 4;
6457 #endif
6458
6459                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 0);
6460                 ARM_STR_IMM (code, ARMREG_IP, read_ins->inst_basereg, read_ins->inst_offset);
6461                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 4);
6462                 ARM_STR_IMM (code, ARMREG_IP, ss_method_ins->inst_basereg, ss_method_ins->inst_offset);
6463                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 8);
6464                 ARM_STR_IMM (code, ARMREG_IP, bp_method_ins->inst_basereg, bp_method_ins->inst_offset);
6465         }
6466
6467         cfg->code_len = code - cfg->native_code;
6468         g_assert (cfg->code_len < cfg->code_size);
6469         g_free (cinfo);
6470
6471         return code;
6472 }
6473
6474 void
6475 mono_arch_emit_epilog (MonoCompile *cfg)
6476 {
6477         MonoMethod *method = cfg->method;
6478         int pos, i, rot_amount;
6479         int max_epilog_size = 16 + 20*4;
6480         guint8 *code;
6481         CallInfo *cinfo;
6482
6483         if (cfg->method->save_lmf)
6484                 max_epilog_size += 128;
6485         
6486         if (mono_jit_trace_calls != NULL)
6487                 max_epilog_size += 50;
6488
6489         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6490                 max_epilog_size += 50;
6491
6492         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
6493                 cfg->code_size *= 2;
6494                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
6495                 cfg->stat_code_reallocs++;
6496         }
6497
6498         /*
6499          * Keep in sync with OP_JMP
6500          */
6501         code = cfg->native_code + cfg->code_len;
6502
6503         if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
6504                 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
6505         }
6506         pos = 0;
6507
6508         /* Load returned vtypes into registers if needed */
6509         cinfo = cfg->arch.cinfo;
6510         if (cinfo->ret.storage == RegTypeStructByVal) {
6511                 MonoInst *ins = cfg->ret;
6512
6513                 if (arm_is_imm12 (ins->inst_offset)) {
6514                         ARM_LDR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
6515                 } else {
6516                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
6517                         ARM_LDR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
6518                 }
6519         }
6520
6521         if (method->save_lmf) {
6522                 int lmf_offset, reg, sp_adj, regmask;
6523                 /* all but r0-r3, sp and pc */
6524                 pos += sizeof (MonoLMF) - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
6525                 lmf_offset = pos;
6526
6527                 code = emit_restore_lmf (cfg, code, cfg->stack_usage - lmf_offset);
6528
6529                 /* This points to r4 inside MonoLMF->iregs */
6530                 sp_adj = (sizeof (MonoLMF) - MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
6531                 reg = ARMREG_R4;
6532                 regmask = 0x9ff0; /* restore lr to pc */
6533                 /* Skip caller saved registers not used by the method */
6534                 while (!(cfg->used_int_regs & (1 << reg)) && reg < ARMREG_FP) {
6535                         regmask &= ~(1 << reg);
6536                         sp_adj += 4;
6537                         reg ++;
6538                 }
6539                 if (iphone_abi)
6540                         /* Restored later */
6541                         regmask &= ~(1 << ARMREG_PC);
6542                 /* point sp at the registers to restore: 10 is 14 -4, because we skip r0-r3 */
6543                 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage - lmf_offset + sp_adj);
6544                 /* restore iregs */
6545                 ARM_POP (code, regmask); 
6546                 if (iphone_abi) {
6547                         /* Restore saved r7, restore LR to PC */
6548                         /* Skip lr from the lmf */
6549                         ARM_ADD_REG_IMM (code, ARMREG_SP, ARMREG_SP, sizeof (gpointer), 0);
6550                         ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_PC));
6551                 }
6552         } else {
6553                 if ((i = mono_arm_is_rotated_imm8 (cfg->stack_usage, &rot_amount)) >= 0) {
6554                         ARM_ADD_REG_IMM (code, ARMREG_SP, cfg->frame_reg, i, rot_amount);
6555                 } else {
6556                         code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->stack_usage);
6557                         ARM_ADD_REG_REG (code, ARMREG_SP, cfg->frame_reg, ARMREG_IP);
6558                 }
6559
6560                 if (iphone_abi) {
6561                         /* Restore saved gregs */
6562                         if (cfg->used_int_regs)
6563                                 ARM_POP (code, cfg->used_int_regs);
6564                         /* Restore saved r7, restore LR to PC */
6565                         ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_PC));
6566                 } else {
6567                         ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_PC));
6568                 }
6569         }
6570
6571         cfg->code_len = code - cfg->native_code;
6572
6573         g_assert (cfg->code_len < cfg->code_size);
6574
6575 }
6576
6577 void
6578 mono_arch_emit_exceptions (MonoCompile *cfg)
6579 {
6580         MonoJumpInfo *patch_info;
6581         int i;
6582         guint8 *code;
6583         guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM];
6584         guint8 exc_throw_found [MONO_EXC_INTRINS_NUM];
6585         int max_epilog_size = 50;
6586
6587         for (i = 0; i < MONO_EXC_INTRINS_NUM; i++) {
6588                 exc_throw_pos [i] = NULL;
6589                 exc_throw_found [i] = 0;
6590         }
6591
6592         /* count the number of exception infos */
6593      
6594         /* 
6595          * make sure we have enough space for exceptions
6596          */
6597         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6598                 if (patch_info->type == MONO_PATCH_INFO_EXC) {
6599                         i = mini_exception_id_by_name (patch_info->data.target);
6600                         if (!exc_throw_found [i]) {
6601                                 max_epilog_size += 32;
6602                                 exc_throw_found [i] = TRUE;
6603                         }
6604                 }
6605         }
6606
6607         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
6608                 cfg->code_size *= 2;
6609                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
6610                 cfg->stat_code_reallocs++;
6611         }
6612
6613         code = cfg->native_code + cfg->code_len;
6614
6615         /* add code to raise exceptions */
6616         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6617                 switch (patch_info->type) {
6618                 case MONO_PATCH_INFO_EXC: {
6619                         MonoClass *exc_class;
6620                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
6621
6622                         i = mini_exception_id_by_name (patch_info->data.target);
6623                         if (exc_throw_pos [i]) {
6624                                 arm_patch (ip, exc_throw_pos [i]);
6625                                 patch_info->type = MONO_PATCH_INFO_NONE;
6626                                 break;
6627                         } else {
6628                                 exc_throw_pos [i] = code;
6629                         }
6630                         arm_patch (ip, code);
6631
6632                         exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
6633                         g_assert (exc_class);
6634
6635                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR);
6636 #ifdef USE_JUMP_TABLES
6637                         {
6638                                 gpointer *jte = mono_jumptable_add_entries (2);
6639                                 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6640                                 patch_info->data.name = "mono_arch_throw_corlib_exception";
6641                                 patch_info->ip.i = code - cfg->native_code;
6642                                 code = mono_arm_load_jumptable_entry_addr (code, jte, ARMREG_R0);
6643                                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, 0);
6644                                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
6645                                 ARM_BLX_REG (code, ARMREG_IP);
6646                                 jte [1] = GUINT_TO_POINTER (exc_class->type_token);
6647                         }
6648 #else
6649                         ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
6650                         patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6651                         patch_info->data.name = "mono_arch_throw_corlib_exception";
6652                         patch_info->ip.i = code - cfg->native_code;
6653                         ARM_BL (code, 0);
6654                         *(guint32*)(gpointer)code = exc_class->type_token;
6655                         code += 4;
6656 #endif
6657                         break;
6658                 }
6659                 default:
6660                         /* do nothing */
6661                         break;
6662                 }
6663         }
6664
6665         cfg->code_len = code - cfg->native_code;
6666
6667         g_assert (cfg->code_len < cfg->code_size);
6668
6669 }
6670
6671 #endif /* #ifndef DISABLE_JIT */
6672
6673 void
6674 mono_arch_finish_init (void)
6675 {
6676 }
6677
6678 void
6679 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
6680 {
6681 }
6682
6683 MonoInst*
6684 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
6685 {
6686         /* FIXME: */
6687         return NULL;
6688 }
6689
6690 gboolean
6691 mono_arch_print_tree (MonoInst *tree, int arity)
6692 {
6693         return 0;
6694 }
6695
6696 #ifndef DISABLE_JIT
6697
6698 #endif
6699
6700 guint32
6701 mono_arch_get_patch_offset (guint8 *code)
6702 {
6703         /* OP_AOTCONST */
6704         return 8;
6705 }
6706
6707 void
6708 mono_arch_flush_register_windows (void)
6709 {
6710 }
6711
6712 #ifndef DISABLE_JIT
6713
6714 void
6715 mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
6716 {
6717         int method_reg = mono_alloc_ireg (cfg);
6718 #ifdef USE_JUMP_TABLES
6719         int use_jumptables = TRUE;
6720 #else
6721         int use_jumptables = FALSE;
6722 #endif
6723
6724         if (cfg->compile_aot) {
6725                 MonoInst *ins;
6726
6727                 call->dynamic_imt_arg = TRUE;
6728
6729                 if (imt_arg) {
6730                         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
6731                 } else {
6732                         MONO_INST_NEW (cfg, ins, OP_AOTCONST);
6733                         ins->dreg = method_reg;
6734                         ins->inst_p0 = call->method;
6735                         ins->inst_c1 = MONO_PATCH_INFO_METHODCONST;
6736                         MONO_ADD_INS (cfg->cbb, ins);
6737                 }
6738                 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
6739         } else if (cfg->generic_context || imt_arg || mono_use_llvm || use_jumptables) {
6740                 /* Always pass in a register for simplicity */
6741                 call->dynamic_imt_arg = TRUE;
6742
6743                 cfg->uses_rgctx_reg = TRUE;
6744
6745                 if (imt_arg) {
6746                         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, method_reg, imt_arg->dreg);
6747                 } else {
6748                         MonoInst *ins;
6749
6750                         MONO_INST_NEW (cfg, ins, OP_PCONST);
6751                         ins->inst_p0 = call->method;
6752                         ins->dreg = method_reg;
6753                         MONO_ADD_INS (cfg->cbb, ins);
6754                 }
6755
6756                 mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
6757         }
6758 }
6759
6760 #endif /* DISABLE_JIT */
6761
6762 MonoMethod*
6763 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
6764 {
6765 #ifdef USE_JUMP_TABLES
6766         return (MonoMethod*)regs [ARMREG_V5];
6767 #else
6768         gpointer method;
6769         guint32 *code_ptr = (guint32*)code;
6770         code_ptr -= 2;
6771         method = GUINT_TO_POINTER (code_ptr [1]);
6772
6773         if (mono_use_llvm)
6774                 /* Passed in V5 */
6775                 return (MonoMethod*)regs [ARMREG_V5];
6776
6777         /* The IMT value is stored in the code stream right after the LDC instruction. */
6778         /* This is no longer true for the gsharedvt_in trampoline */
6779         /*
6780         if (!IS_LDR_PC (code_ptr [0])) {
6781                 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]);
6782                 g_assert (IS_LDR_PC (code_ptr [0]));
6783         }
6784         */
6785         if (method == 0)
6786                 /* This is AOTed code, or the gsharedvt trampoline, the IMT method is in V5 */
6787                 return (MonoMethod*)regs [ARMREG_V5];
6788         else
6789                 return (MonoMethod*) method;
6790 #endif
6791 }
6792
6793 MonoVTable*
6794 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
6795 {
6796         return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
6797 }
6798
6799 /* #define ENABLE_WRONG_METHOD_CHECK 1 */
6800 #define BASE_SIZE (6 * 4)
6801 #define BSEARCH_ENTRY_SIZE (4 * 4)
6802 #define CMP_SIZE (3 * 4)
6803 #define BRANCH_SIZE (1 * 4)
6804 #define CALL_SIZE (2 * 4)
6805 #define WMC_SIZE (8 * 4)
6806 #define DISTANCE(A, B) (((gint32)(B)) - ((gint32)(A)))
6807
6808 #ifdef USE_JUMP_TABLES
6809 static void
6810 set_jumptable_element (gpointer *base, guint32 index, gpointer value)
6811 {
6812         g_assert (base [index] == NULL);
6813         base [index] = value;
6814 }
6815 static arminstr_t *
6816 load_element_with_regbase_cond (arminstr_t *code, ARMReg dreg, ARMReg base, guint32 jti, int cond)
6817 {
6818         if (arm_is_imm12 (jti * 4)) {
6819                 ARM_LDR_IMM_COND (code, dreg, base, jti * 4, cond);
6820         } else {
6821                 ARM_MOVW_REG_IMM_COND (code, dreg, (jti * 4) & 0xffff, cond);
6822                 if ((jti * 4) >> 16)
6823                         ARM_MOVT_REG_IMM_COND (code, dreg, ((jti * 4) >> 16) & 0xffff, cond);
6824                 ARM_LDR_REG_REG_SHIFT_COND (code, dreg, base, dreg, ARMSHIFT_LSL, 0, cond);
6825         }
6826         return code;
6827 }
6828 #else
6829 static arminstr_t *
6830 arm_emit_value_and_patch_ldr (arminstr_t *code, arminstr_t *target, guint32 value)
6831 {
6832         guint32 delta = DISTANCE (target, code);
6833         delta -= 8;
6834         g_assert (delta >= 0 && delta <= 0xFFF);
6835         *target = *target | delta;
6836         *code = value;
6837         return code + 1;
6838 }
6839 #endif
6840
6841 #ifdef ENABLE_WRONG_METHOD_CHECK
6842 static void
6843 mini_dump_bad_imt (int input_imt, int compared_imt, int pc)
6844 {
6845         g_print ("BAD IMT comparing %x with expected %x at ip %x", input_imt, compared_imt, pc);
6846         g_assert (0);
6847 }
6848 #endif
6849
6850 gpointer
6851 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
6852         gpointer fail_tramp)
6853 {
6854         int size, i;
6855         arminstr_t *code, *start;
6856 #ifdef USE_JUMP_TABLES
6857         gpointer *jte;
6858 #else
6859         gboolean large_offsets = FALSE;
6860         guint32 **constant_pool_starts;
6861         arminstr_t *vtable_target = NULL;
6862         int extra_space = 0;
6863 #endif
6864 #ifdef ENABLE_WRONG_METHOD_CHECK
6865         char * cond;
6866 #endif
6867
6868         size = BASE_SIZE;
6869 #ifdef USE_JUMP_TABLES
6870         for (i = 0; i < count; ++i) {
6871                 MonoIMTCheckItem *item = imt_entries [i];
6872                 item->chunk_size += 4 * 16;
6873                 if (!item->is_equals)
6874                         imt_entries [item->check_target_idx]->compare_done = TRUE;
6875                 size += item->chunk_size;
6876         }
6877 #else
6878         constant_pool_starts = g_new0 (guint32*, count);
6879
6880         for (i = 0; i < count; ++i) {
6881                 MonoIMTCheckItem *item = imt_entries [i];
6882                 if (item->is_equals) {
6883                         gboolean fail_case = !item->check_target_idx && fail_tramp;
6884
6885                         if (item->has_target_code || !arm_is_imm12 (DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]))) {
6886                                 item->chunk_size += 32;
6887                                 large_offsets = TRUE;
6888                         }
6889
6890                         if (item->check_target_idx || fail_case) {
6891                                 if (!item->compare_done || fail_case)
6892                                         item->chunk_size += CMP_SIZE;
6893                                 item->chunk_size += BRANCH_SIZE;
6894                         } else {
6895 #ifdef ENABLE_WRONG_METHOD_CHECK
6896                                 item->chunk_size += WMC_SIZE;
6897 #endif
6898                         }
6899                         if (fail_case) {
6900                                 item->chunk_size += 16;
6901                                 large_offsets = TRUE;
6902                         }
6903                         item->chunk_size += CALL_SIZE;
6904                 } else {
6905                         item->chunk_size += BSEARCH_ENTRY_SIZE;
6906                         imt_entries [item->check_target_idx]->compare_done = TRUE;
6907                 }
6908                 size += item->chunk_size;
6909         }
6910
6911         if (large_offsets)
6912                 size += 4 * count; /* The ARM_ADD_REG_IMM to pop the stack */
6913 #endif
6914
6915         if (fail_tramp)
6916                 code = mono_method_alloc_generic_virtual_thunk (domain, size);
6917         else
6918                 code = mono_domain_code_reserve (domain, size);
6919         start = code;
6920
6921 #ifdef DEBUG_IMT
6922         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);
6923         for (i = 0; i < count; ++i) {
6924                 MonoIMTCheckItem *item = imt_entries [i];
6925                 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);
6926         }
6927 #endif
6928
6929 #ifdef USE_JUMP_TABLES
6930         ARM_PUSH3 (code, ARMREG_R0, ARMREG_R1, ARMREG_R2);
6931         /* If jumptables we always pass the IMT method in R5 */
6932         ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V5);
6933 #define VTABLE_JTI 0
6934 #define IMT_METHOD_OFFSET 0
6935 #define TARGET_CODE_OFFSET 1
6936 #define JUMP_CODE_OFFSET 2
6937 #define RECORDS_PER_ENTRY 3
6938 #define IMT_METHOD_JTI(idx) (1 + idx * RECORDS_PER_ENTRY + IMT_METHOD_OFFSET)
6939 #define TARGET_CODE_JTI(idx) (1 + idx * RECORDS_PER_ENTRY + TARGET_CODE_OFFSET)
6940 #define JUMP_CODE_JTI(idx) (1 + idx * RECORDS_PER_ENTRY + JUMP_CODE_OFFSET)
6941
6942         jte = mono_jumptable_add_entries (RECORDS_PER_ENTRY * count + 1 /* vtable */);
6943         code = (arminstr_t *) mono_arm_load_jumptable_entry_addr ((guint8 *) code, jte, ARMREG_R2);
6944         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R2, VTABLE_JTI);
6945         set_jumptable_element (jte, VTABLE_JTI, vtable);
6946 #else
6947         if (large_offsets)
6948                 ARM_PUSH4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
6949         else
6950                 ARM_PUSH2 (code, ARMREG_R0, ARMREG_R1);
6951         ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, -4);
6952         vtable_target = code;
6953         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
6954
6955         if (mono_use_llvm) {
6956                 /* LLVM always passes the IMT method in R5 */
6957                 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V5);
6958         } else {
6959                 /* R0 == 0 means we are called from AOT code. In this case, V5 contains the IMT method */
6960                 ARM_CMP_REG_IMM8 (code, ARMREG_R0, 0);
6961                 ARM_MOV_REG_REG_COND (code, ARMREG_R0, ARMREG_V5, ARMCOND_EQ);
6962         }
6963 #endif
6964
6965         for (i = 0; i < count; ++i) {
6966                 MonoIMTCheckItem *item = imt_entries [i];
6967 #ifdef USE_JUMP_TABLES
6968                 guint32 imt_method_jti = 0, target_code_jti = 0;
6969 #else
6970                 arminstr_t *imt_method = NULL, *vtable_offset_ins = NULL, *target_code_ins = NULL;
6971 #endif
6972                 gint32 vtable_offset;
6973
6974                 item->code_target = (guint8*)code;
6975
6976                 if (item->is_equals) {
6977                         gboolean fail_case = !item->check_target_idx && fail_tramp;
6978
6979                         if (item->check_target_idx || fail_case) {
6980                                 if (!item->compare_done || fail_case) {
6981 #ifdef USE_JUMP_TABLES
6982                                         imt_method_jti = IMT_METHOD_JTI (i);
6983                                         code = load_element_with_regbase_cond (code, ARMREG_R1, ARMREG_R2, imt_method_jti, ARMCOND_AL);
6984 #else
6985                                         imt_method = code;
6986                                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
6987 #endif
6988                                         ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
6989                                 }
6990 #ifdef USE_JUMP_TABLES
6991                                 code = load_element_with_regbase_cond (code, ARMREG_R1, ARMREG_R2, JUMP_CODE_JTI (i), ARMCOND_NE);
6992                                 ARM_BX_COND (code, ARMCOND_NE, ARMREG_R1);
6993                                 item->jmp_code = GUINT_TO_POINTER (JUMP_CODE_JTI (i));
6994 #else
6995                                 item->jmp_code = (guint8*)code;
6996                                 ARM_B_COND (code, ARMCOND_NE, 0);
6997 #endif
6998                         } else {
6999                                 /*Enable the commented code to assert on wrong method*/
7000 #ifdef ENABLE_WRONG_METHOD_CHECK
7001 #ifdef USE_JUMP_TABLES
7002                                 imt_method_jti = IMT_METHOD_JTI (i);
7003                                 code = load_element_with_regbase_cond (code, ARMREG_R1, ARMREG_R2, imt_method_jti, ARMCOND_AL);
7004 #else
7005                                 imt_method = code;
7006                                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
7007 #endif
7008                                 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
7009                                 cond = code;
7010                                 ARM_B_COND (code, ARMCOND_EQ, 0);
7011
7012 /* Define this if your system is so bad that gdb is failing. */
7013 #ifdef BROKEN_DEV_ENV
7014                                 ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_PC);
7015                                 ARM_BL (code, 0);
7016                                 arm_patch (code - 1, mini_dump_bad_imt);
7017 #else
7018                                 ARM_DBRK (code);
7019 #endif
7020                                 arm_patch (cond, code);
7021 #endif
7022                         }
7023
7024                         if (item->has_target_code) {
7025                                 /* Load target address */
7026 #ifdef USE_JUMP_TABLES
7027                                 target_code_jti = TARGET_CODE_JTI (i);
7028                                 code = load_element_with_regbase_cond (code, ARMREG_R1, ARMREG_R2, target_code_jti, ARMCOND_AL);
7029                                 /* Restore registers */
7030                                 ARM_POP3 (code, ARMREG_R0, ARMREG_R1, ARMREG_R2);
7031                                 /*  And branch */
7032                                 ARM_BX (code, ARMREG_R1);
7033                                 set_jumptable_element (jte, target_code_jti, item->value.target_code);
7034 #else
7035                                 target_code_ins = code;
7036                                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
7037                                 /* Save it to the fourth slot */
7038                                 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
7039                                 /* Restore registers and branch */
7040                                 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
7041                                 
7042                                 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)item->value.target_code);
7043 #endif
7044                         } else {
7045                                 vtable_offset = DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]);
7046                                 if (!arm_is_imm12 (vtable_offset)) {
7047                                         /* 
7048                                          * We need to branch to a computed address but we don't have
7049                                          * a free register to store it, since IP must contain the 
7050                                          * vtable address. So we push the two values to the stack, and
7051                                          * load them both using LDM.
7052                                          */
7053                                         /* Compute target address */
7054 #ifdef USE_JUMP_TABLES
7055                                         ARM_MOVW_REG_IMM (code, ARMREG_R1, vtable_offset & 0xffff);
7056                                         if (vtable_offset >> 16)
7057                                                 ARM_MOVT_REG_IMM (code, ARMREG_R1, (vtable_offset >> 16) & 0xffff);
7058                                         /* IP had vtable base. */
7059                                         ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_IP, ARMREG_R1);
7060                                         /* Restore registers and branch */
7061                                         ARM_POP3 (code, ARMREG_R0, ARMREG_R1, ARMREG_R2);
7062                                         ARM_BX (code, ARMREG_IP);
7063 #else
7064                                         vtable_offset_ins = code;
7065                                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
7066                                         ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_IP, ARMREG_R1);
7067                                         /* Save it to the fourth slot */
7068                                         ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
7069                                         /* Restore registers and branch */
7070                                         ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
7071                                 
7072                                         code = arm_emit_value_and_patch_ldr (code, vtable_offset_ins, vtable_offset);
7073 #endif
7074                                 } else {
7075 #ifdef USE_JUMP_TABLES
7076                                         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, vtable_offset);
7077                                         ARM_POP3 (code, ARMREG_R0, ARMREG_R1, ARMREG_R2);
7078                                         ARM_BX (code, ARMREG_IP);
7079 #else
7080                                         ARM_POP2 (code, ARMREG_R0, ARMREG_R1);
7081                                         if (large_offsets)
7082                                                 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 2 * sizeof (gpointer));
7083                                         ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, vtable_offset);
7084 #endif
7085                                 }
7086                         }
7087
7088                         if (fail_case) {
7089 #ifdef USE_JUMP_TABLES
7090                                 set_jumptable_element (jte, GPOINTER_TO_UINT (item->jmp_code), code);
7091                                 target_code_jti = TARGET_CODE_JTI (i);
7092                                 /* Load target address */
7093                                 code = load_element_with_regbase_cond (code, ARMREG_R1, ARMREG_R2, target_code_jti, ARMCOND_AL);
7094                                 /* Restore registers */
7095                                 ARM_POP3 (code, ARMREG_R0, ARMREG_R1, ARMREG_R2);
7096                                 /* And branch */
7097                                 ARM_BX (code, ARMREG_R1);
7098                                 set_jumptable_element (jte, target_code_jti, fail_tramp);
7099 #else
7100                                 arm_patch (item->jmp_code, (guchar*)code);
7101
7102                                 target_code_ins = code;
7103                                 /* Load target address */
7104                                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
7105                                 /* Save it to the fourth slot */
7106                                 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
7107                                 /* Restore registers and branch */
7108                                 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
7109                                 
7110                                 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)fail_tramp);
7111 #endif
7112                                 item->jmp_code = NULL;
7113                         }
7114
7115 #ifdef USE_JUMP_TABLES
7116                         if (imt_method_jti)
7117                                 set_jumptable_element (jte, imt_method_jti, item->key);
7118 #else
7119                         if (imt_method)
7120                                 code = arm_emit_value_and_patch_ldr (code, imt_method, (guint32)item->key);
7121
7122                         /*must emit after unconditional branch*/
7123                         if (vtable_target) {
7124                                 code = arm_emit_value_and_patch_ldr (code, vtable_target, (guint32)vtable);
7125                                 item->chunk_size += 4;
7126                                 vtable_target = NULL;
7127                         }
7128
7129                         /*We reserve the space for bsearch IMT values after the first entry with an absolute jump*/
7130                         constant_pool_starts [i] = code;
7131                         if (extra_space) {
7132                                 code += extra_space;
7133                                 extra_space = 0;
7134                         }
7135 #endif
7136                 } else {
7137 #ifdef USE_JUMP_TABLES
7138                         code = load_element_with_regbase_cond (code, ARMREG_R1, ARMREG_R2, IMT_METHOD_JTI (i), ARMCOND_AL);
7139                         ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
7140                         code = load_element_with_regbase_cond (code, ARMREG_R1, ARMREG_R2, JUMP_CODE_JTI (i), ARMCOND_HS);
7141                         ARM_BX_COND (code, ARMCOND_HS, ARMREG_R1);
7142                         item->jmp_code = GUINT_TO_POINTER (JUMP_CODE_JTI (i));
7143 #else
7144                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
7145                         ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
7146
7147                         item->jmp_code = (guint8*)code;
7148                         ARM_B_COND (code, ARMCOND_HS, 0);
7149                         ++extra_space;
7150 #endif
7151                 }
7152         }
7153
7154         for (i = 0; i < count; ++i) {
7155                 MonoIMTCheckItem *item = imt_entries [i];
7156                 if (item->jmp_code) {
7157                         if (item->check_target_idx)
7158 #ifdef USE_JUMP_TABLES
7159                                 set_jumptable_element (jte, GPOINTER_TO_UINT (item->jmp_code), imt_entries [item->check_target_idx]->code_target);
7160 #else
7161                                 arm_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
7162 #endif
7163                 }
7164                 if (i > 0 && item->is_equals) {
7165                         int j;
7166 #ifdef USE_JUMP_TABLES
7167                         for (j = i - 1; j >= 0 && !imt_entries [j]->is_equals; --j)
7168                                 set_jumptable_element (jte, IMT_METHOD_JTI (j), imt_entries [j]->key);
7169 #else
7170                         arminstr_t *space_start = constant_pool_starts [i];
7171                         for (j = i - 1; j >= 0 && !imt_entries [j]->is_equals; --j) {
7172                                 space_start = arm_emit_value_and_patch_ldr (space_start, (arminstr_t*)imt_entries [j]->code_target, (guint32)imt_entries [j]->key);
7173                         }
7174 #endif
7175                 }
7176         }
7177
7178 #ifdef DEBUG_IMT
7179         {
7180                 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
7181                 mono_disassemble_code (NULL, (guint8*)start, size, buff);
7182                 g_free (buff);
7183         }
7184 #endif
7185
7186 #ifndef USE_JUMP_TABLES
7187         g_free (constant_pool_starts);
7188 #endif
7189
7190         mono_arch_flush_icache ((guint8*)start, size);
7191         mono_profiler_code_buffer_new (start, code - start, MONO_PROFILER_CODE_BUFFER_IMT_TRAMPOLINE, NULL);
7192         mono_stats.imt_thunks_size += code - start;
7193
7194         g_assert (DISTANCE (start, code) <= size);
7195         return start;
7196 }
7197
7198 mgreg_t
7199 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
7200 {
7201         return ctx->regs [reg];
7202 }
7203
7204 void
7205 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
7206 {
7207         ctx->regs [reg] = val;
7208 }
7209
7210 /*
7211  * mono_arch_get_trampolines:
7212  *
7213  *   Return a list of MonoTrampInfo structures describing arch specific trampolines
7214  * for AOT.
7215  */
7216 GSList *
7217 mono_arch_get_trampolines (gboolean aot)
7218 {
7219         return mono_arm_get_exception_trampolines (aot);
7220 }
7221
7222 gpointer
7223 mono_arch_install_handler_block_guard (MonoJitInfo *ji, MonoJitExceptionInfo *clause, MonoContext *ctx, gpointer new_value)
7224 {
7225         gpointer *lr_loc;
7226         char *old_value;
7227         char *bp;
7228
7229         /*Load the spvar*/
7230         bp = MONO_CONTEXT_GET_BP (ctx);
7231         lr_loc = (gpointer*)(bp + clause->exvar_offset);
7232
7233         old_value = *lr_loc;
7234         if ((char*)old_value < (char*)ji->code_start || (char*)old_value > ((char*)ji->code_start + ji->code_size))
7235                 return old_value;
7236
7237         *lr_loc = new_value;
7238
7239         return old_value;
7240 }
7241
7242 #if defined(MONO_ARCH_SOFT_DEBUG_SUPPORTED)
7243 /*
7244  * mono_arch_set_breakpoint:
7245  *
7246  *   Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
7247  * The location should contain code emitted by OP_SEQ_POINT.
7248  */
7249 void
7250 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
7251 {
7252         guint8 *code = ip;
7253         guint32 native_offset = ip - (guint8*)ji->code_start;
7254         MonoDebugOptions *opt = mini_get_debug_options ();
7255
7256         if (opt->soft_breakpoints) {
7257                 g_assert (!ji->from_aot);
7258                 code += 4;
7259                 ARM_BLX_REG (code, ARMREG_LR);
7260                 mono_arch_flush_icache (code - 4, 4);
7261         } else if (ji->from_aot) {
7262                 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
7263
7264                 g_assert (native_offset % 4 == 0);
7265                 g_assert (info->bp_addrs [native_offset / 4] == 0);
7266                 info->bp_addrs [native_offset / 4] = bp_trigger_page;
7267         } else {
7268                 int dreg = ARMREG_LR;
7269
7270                 /* Read from another trigger page */
7271 #ifdef USE_JUMP_TABLES
7272                 gpointer *jte = mono_jumptable_add_entry ();
7273                 code = mono_arm_load_jumptable_entry (code, jte, dreg);
7274                 jte [0] = bp_trigger_page;
7275 #else
7276                 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
7277                 ARM_B (code, 0);
7278                 *(int*)code = (int)bp_trigger_page;
7279                 code += 4;
7280 #endif
7281                 ARM_LDR_IMM (code, dreg, dreg, 0);
7282
7283                 mono_arch_flush_icache (code - 16, 16);
7284
7285 #if 0
7286                 /* This is currently implemented by emitting an SWI instruction, which 
7287                  * qemu/linux seems to convert to a SIGILL.
7288                  */
7289                 *(int*)code = (0xef << 24) | 8;
7290                 code += 4;
7291                 mono_arch_flush_icache (code - 4, 4);
7292 #endif
7293         }
7294 }
7295
7296 /*
7297  * mono_arch_clear_breakpoint:
7298  *
7299  *   Clear the breakpoint at IP.
7300  */
7301 void
7302 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
7303 {
7304         MonoDebugOptions *opt = mini_get_debug_options ();
7305         guint8 *code = ip;
7306         int i;
7307
7308         if (opt->soft_breakpoints) {
7309                 g_assert (!ji->from_aot);
7310                 code += 4;
7311                 ARM_NOP (code);
7312                 mono_arch_flush_icache (code - 4, 4);
7313         } else if (ji->from_aot) {
7314                 guint32 native_offset = ip - (guint8*)ji->code_start;
7315                 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
7316
7317                 g_assert (native_offset % 4 == 0);
7318                 g_assert (info->bp_addrs [native_offset / 4] == bp_trigger_page);
7319                 info->bp_addrs [native_offset / 4] = 0;
7320         } else {
7321                 for (i = 0; i < 4; ++i)
7322                         ARM_NOP (code);
7323
7324                 mono_arch_flush_icache (ip, code - ip);
7325         }
7326 }
7327         
7328 /*
7329  * mono_arch_start_single_stepping:
7330  *
7331  *   Start single stepping.
7332  */
7333 void
7334 mono_arch_start_single_stepping (void)
7335 {
7336         if (ss_trigger_page)
7337                 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
7338         else
7339                 ss_trigger_var = 1;
7340 }
7341         
7342 /*
7343  * mono_arch_stop_single_stepping:
7344  *
7345  *   Stop single stepping.
7346  */
7347 void
7348 mono_arch_stop_single_stepping (void)
7349 {
7350         if (ss_trigger_page)
7351                 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
7352         else
7353                 ss_trigger_var = 0;
7354 }
7355
7356 #if __APPLE__
7357 #define DBG_SIGNAL SIGBUS
7358 #else
7359 #define DBG_SIGNAL SIGSEGV
7360 #endif
7361
7362 /*
7363  * mono_arch_is_single_step_event:
7364  *
7365  *   Return whenever the machine state in SIGCTX corresponds to a single
7366  * step event.
7367  */
7368 gboolean
7369 mono_arch_is_single_step_event (void *info, void *sigctx)
7370 {
7371         siginfo_t *sinfo = info;
7372
7373         if (!ss_trigger_page)
7374                 return FALSE;
7375
7376         /* Sometimes the address is off by 4 */
7377         if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
7378                 return TRUE;
7379         else
7380                 return FALSE;
7381 }
7382
7383 /*
7384  * mono_arch_is_breakpoint_event:
7385  *
7386  *   Return whenever the machine state in SIGCTX corresponds to a breakpoint event.
7387  */
7388 gboolean
7389 mono_arch_is_breakpoint_event (void *info, void *sigctx)
7390 {
7391         siginfo_t *sinfo = info;
7392
7393         if (!ss_trigger_page)
7394                 return FALSE;
7395
7396         if (sinfo->si_signo == DBG_SIGNAL) {
7397                 /* Sometimes the address is off by 4 */
7398                 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
7399                         return TRUE;
7400                 else
7401                         return FALSE;
7402         } else {
7403                 return FALSE;
7404         }
7405 }
7406
7407 /*
7408  * mono_arch_skip_breakpoint:
7409  *
7410  *   See mini-amd64.c for docs.
7411  */
7412 void
7413 mono_arch_skip_breakpoint (MonoContext *ctx, MonoJitInfo *ji)
7414 {
7415         MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
7416 }
7417
7418 /*
7419  * mono_arch_skip_single_step:
7420  *
7421  *   See mini-amd64.c for docs.
7422  */
7423 void
7424 mono_arch_skip_single_step (MonoContext *ctx)
7425 {
7426         MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
7427 }
7428
7429 #endif /* MONO_ARCH_SOFT_DEBUG_SUPPORTED */
7430
7431 /*
7432  * mono_arch_get_seq_point_info:
7433  *
7434  *   See mini-amd64.c for docs.
7435  */
7436 gpointer
7437 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
7438 {
7439         SeqPointInfo *info;
7440         MonoJitInfo *ji;
7441
7442         // FIXME: Add a free function
7443
7444         mono_domain_lock (domain);
7445         info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points, 
7446                                                                 code);
7447         mono_domain_unlock (domain);
7448
7449         if (!info) {
7450                 ji = mono_jit_info_table_find (domain, (char*)code);
7451                 g_assert (ji);
7452
7453                 info = g_malloc0 (sizeof (SeqPointInfo) + ji->code_size);
7454
7455                 info->ss_trigger_page = ss_trigger_page;
7456                 info->bp_trigger_page = bp_trigger_page;
7457
7458                 mono_domain_lock (domain);
7459                 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
7460                                                          code, info);
7461                 mono_domain_unlock (domain);
7462         }
7463
7464         return info;
7465 }
7466
7467 void
7468 mono_arch_init_lmf_ext (MonoLMFExt *ext, gpointer prev_lmf)
7469 {
7470         ext->lmf.previous_lmf = prev_lmf;
7471         /* Mark that this is a MonoLMFExt */
7472         ext->lmf.previous_lmf = (gpointer)(((gssize)ext->lmf.previous_lmf) | 2);
7473         ext->lmf.sp = (gssize)ext;
7474 }
7475
7476 /*
7477  * mono_arch_set_target:
7478  *
7479  *   Set the target architecture the JIT backend should generate code for, in the form
7480  * of a GNU target triplet. Only used in AOT mode.
7481  */
7482 void
7483 mono_arch_set_target (char *mtriple)
7484 {
7485         /* The GNU target triple format is not very well documented */
7486         if (strstr (mtriple, "armv7")) {
7487                 v5_supported = TRUE;
7488                 v6_supported = TRUE;
7489                 v7_supported = TRUE;
7490         }
7491         if (strstr (mtriple, "armv6")) {
7492                 v5_supported = TRUE;
7493                 v6_supported = TRUE;
7494         }
7495         if (strstr (mtriple, "armv7s")) {
7496                 v7s_supported = TRUE;
7497         }
7498         if (strstr (mtriple, "thumbv7s")) {
7499                 v5_supported = TRUE;
7500                 v6_supported = TRUE;
7501                 v7_supported = TRUE;
7502                 v7s_supported = TRUE;
7503                 thumb_supported = TRUE;
7504                 thumb2_supported = TRUE;
7505         }
7506         if (strstr (mtriple, "darwin") || strstr (mtriple, "ios")) {
7507                 v5_supported = TRUE;
7508                 v6_supported = TRUE;
7509                 thumb_supported = TRUE;
7510                 iphone_abi = TRUE;
7511         }
7512         if (strstr (mtriple, "gnueabi"))
7513                 eabi_supported = TRUE;
7514 }
7515
7516 gboolean
7517 mono_arch_opcode_supported (int opcode)
7518 {
7519         switch (opcode) {
7520         case OP_ATOMIC_ADD_I4:
7521         case OP_ATOMIC_EXCHANGE_I4:
7522         case OP_ATOMIC_CAS_I4:
7523         case OP_ATOMIC_LOAD_I1:
7524         case OP_ATOMIC_LOAD_I2:
7525         case OP_ATOMIC_LOAD_I4:
7526         case OP_ATOMIC_LOAD_U1:
7527         case OP_ATOMIC_LOAD_U2:
7528         case OP_ATOMIC_LOAD_U4:
7529         case OP_ATOMIC_STORE_I1:
7530         case OP_ATOMIC_STORE_I2:
7531         case OP_ATOMIC_STORE_I4:
7532         case OP_ATOMIC_STORE_U1:
7533         case OP_ATOMIC_STORE_U2:
7534         case OP_ATOMIC_STORE_U4:
7535                 return v7_supported;
7536         case OP_ATOMIC_LOAD_R4:
7537         case OP_ATOMIC_LOAD_R8:
7538         case OP_ATOMIC_STORE_R4:
7539         case OP_ATOMIC_STORE_R8:
7540                 return v7_supported && IS_VFP;
7541         default:
7542                 return FALSE;
7543         }
7544 }
7545
7546 #if defined(ENABLE_GSHAREDVT)
7547
7548 #include "../../../mono-extensions/mono/mini/mini-arm-gsharedvt.c"
7549
7550 #endif /* !MONOTOUCH */