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