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