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