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