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