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