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