Implement precise stack marking for ARM.
[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/appdomain.h>
16 #include <mono/metadata/debug-helpers.h>
17 #include <mono/utils/mono-mmap.h>
18
19 #include "mini-arm.h"
20 #include "cpu-arm.h"
21 #include "trace.h"
22 #include "ir-emit.h"
23 #include "debugger-agent.h"
24 #include "mini-gc.h"
25 #include "mono/arch/arm/arm-fpa-codegen.h"
26 #include "mono/arch/arm/arm-vfp-codegen.h"
27
28 #if defined(__ARM_EABI__) && defined(__linux__) && !defined(PLATFORM_ANDROID)
29 #define HAVE_AEABI_READ_TP 1
30 #endif
31
32 #ifdef ARM_FPU_FPA
33 #define IS_FPA 1
34 #else
35 #define IS_FPA 0
36 #endif
37
38 #ifdef ARM_FPU_VFP
39 #define IS_VFP 1
40 #else
41 #define IS_VFP 0
42 #endif
43
44 #ifdef MONO_ARCH_SOFT_FLOAT
45 #define IS_SOFT_FLOAT 1
46 #else
47 #define IS_SOFT_FLOAT 0
48 #endif
49
50 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
51
52 static gint lmf_tls_offset = -1;
53 static gint lmf_addr_tls_offset = -1;
54
55 /* This mutex protects architecture specific caches */
56 #define mono_mini_arch_lock() EnterCriticalSection (&mini_arch_mutex)
57 #define mono_mini_arch_unlock() LeaveCriticalSection (&mini_arch_mutex)
58 static CRITICAL_SECTION mini_arch_mutex;
59
60 static int v5_supported = 0;
61 static int v6_supported = 0;
62 static int v7_supported = 0;
63 static int thumb_supported = 0;
64 /*
65  * Whenever to use the ARM EABI
66  */
67 static int eabi_supported = 0;
68
69 /*
70  * Whenever we are on arm/darwin aka the iphone.
71  */
72 static int darwin = 0;
73 /* 
74  * Whenever to use the iphone ABI extensions:
75  * http://developer.apple.com/library/ios/documentation/Xcode/Conceptual/iPhoneOSABIReference/index.html
76  * Basically, r7 is used as a frame pointer and it should point to the saved r7 + lr.
77  * This is required for debugging/profiling tools to work, but it has some overhead so it should
78  * only be turned on in debug builds.
79  */
80 static int iphone_abi = 0;
81
82 /*
83  * The FPU we are generating code for. This is NOT runtime configurable right now,
84  * since some things like MONO_ARCH_CALLEE_FREGS still depend on defines.
85  */
86 static MonoArmFPU arm_fpu;
87
88 static int i8_align;
89
90 static volatile int ss_trigger_var = 0;
91
92 static gpointer single_step_func_wrapper;
93 static gpointer breakpoint_func_wrapper;
94
95 /*
96  * The code generated for sequence points reads from this location, which is
97  * made read-only when single stepping is enabled.
98  */
99 static gpointer ss_trigger_page;
100
101 /* Enabled breakpoints read from this trigger page */
102 static gpointer bp_trigger_page;
103
104 /* Structure used by the sequence points in AOTed code */
105 typedef struct {
106         gpointer ss_trigger_page;
107         gpointer bp_trigger_page;
108         guint8* bp_addrs [MONO_ZERO_LEN_ARRAY];
109 } SeqPointInfo;
110
111 /*
112  * TODO:
113  * floating point support: on ARM it is a mess, there are at least 3
114  * different setups, each of which binary incompat with the other.
115  * 1) FPA: old and ugly, but unfortunately what current distros use
116  *    the double binary format has the two words swapped. 8 double registers.
117  *    Implemented usually by kernel emulation.
118  * 2) softfloat: the compiler emulates all the fp ops. Usually uses the
119  *    ugly swapped double format (I guess a softfloat-vfp exists, too, though).
120  * 3) VFP: the new and actually sensible and useful FP support. Implemented
121  *    in HW or kernel-emulated, requires new tools. I think this is what symbian uses.
122  *
123  * The plan is to write the FPA support first. softfloat can be tested in a chroot.
124  */
125 int mono_exc_esp_offset = 0;
126
127 #define arm_is_imm12(v) ((v) > -4096 && (v) < 4096)
128 #define arm_is_imm8(v) ((v) > -256 && (v) < 256)
129 #define arm_is_fpimm8(v) ((v) >= -1020 && (v) <= 1020)
130
131 #define LDR_MASK ((0xf << ARMCOND_SHIFT) | (3 << 26) | (1 << 22) | (1 << 20) | (15 << 12))
132 #define LDR_PC_VAL ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 26) | (0 << 22) | (1 << 20) | (15 << 12))
133 #define IS_LDR_PC(val) (((val) & LDR_MASK) == LDR_PC_VAL)
134
135 #define ADD_LR_PC_4 ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 25) | (1 << 23) | (ARMREG_PC << 16) | (ARMREG_LR << 12) | 4)
136 #define MOV_LR_PC ((ARMCOND_AL << ARMCOND_SHIFT) | (1 << 24) | (0xa << 20) |  (ARMREG_LR << 12) | ARMREG_PC)
137 #define DEBUG_IMT 0
138  
139 /* A variant of ARM_LDR_IMM which can handle large offsets */
140 #define ARM_LDR_IMM_GENERAL(code, dreg, basereg, offset, scratch_reg) do { \
141         if (arm_is_imm12 ((offset))) { \
142                 ARM_LDR_IMM (code, (dreg), (basereg), (offset));        \
143         } else {                                                                                                \
144                 g_assert ((scratch_reg) != (basereg));                                     \
145                 code = mono_arm_emit_load_imm (code, (scratch_reg), (offset));  \
146                 ARM_LDR_REG_REG (code, (dreg), (basereg), (scratch_reg));               \
147         }                                                                                                                                       \
148         } while (0)
149
150 #define ARM_STR_IMM_GENERAL(code, dreg, basereg, offset, scratch_reg) do {      \
151         if (arm_is_imm12 ((offset))) { \
152                 ARM_STR_IMM (code, (dreg), (basereg), (offset));        \
153         } else {                                                                                                \
154                 g_assert ((scratch_reg) != (basereg));                                     \
155                 code = mono_arm_emit_load_imm (code, (scratch_reg), (offset));  \
156                 ARM_STR_REG_REG (code, (dreg), (basereg), (scratch_reg));               \
157         }                                                                                                                                       \
158         } while (0)
159
160 static void mono_arch_compute_omit_fp (MonoCompile *cfg);
161
162 const char*
163 mono_arch_regname (int reg)
164 {
165         static const char * rnames[] = {
166                 "arm_r0", "arm_r1", "arm_r2", "arm_r3", "arm_v1",
167                 "arm_v2", "arm_v3", "arm_v4", "arm_v5", "arm_v6",
168                 "arm_v7", "arm_fp", "arm_ip", "arm_sp", "arm_lr",
169                 "arm_pc"
170         };
171         if (reg >= 0 && reg < 16)
172                 return rnames [reg];
173         return "unknown";
174 }
175
176 const char*
177 mono_arch_fregname (int reg)
178 {
179         static const char * rnames[] = {
180                 "arm_f0", "arm_f1", "arm_f2", "arm_f3", "arm_f4",
181                 "arm_f5", "arm_f6", "arm_f7", "arm_f8", "arm_f9",
182                 "arm_f10", "arm_f11", "arm_f12", "arm_f13", "arm_f14",
183                 "arm_f15", "arm_f16", "arm_f17", "arm_f18", "arm_f19",
184                 "arm_f20", "arm_f21", "arm_f22", "arm_f23", "arm_f24",
185                 "arm_f25", "arm_f26", "arm_f27", "arm_f28", "arm_f29",
186                 "arm_f30", "arm_f31"
187         };
188         if (reg >= 0 && reg < 32)
189                 return rnames [reg];
190         return "unknown";
191 }
192
193 #ifndef DISABLE_JIT
194
195 static guint8*
196 emit_big_add (guint8 *code, int dreg, int sreg, int imm)
197 {
198         int imm8, rot_amount;
199         if ((imm8 = mono_arm_is_rotated_imm8 (imm, &rot_amount)) >= 0) {
200                 ARM_ADD_REG_IMM (code, dreg, sreg, imm8, rot_amount);
201                 return code;
202         }
203         g_assert (dreg != sreg);
204         code = mono_arm_emit_load_imm (code, dreg, imm);
205         ARM_ADD_REG_REG (code, dreg, dreg, sreg);
206         return code;
207 }
208
209 static guint8*
210 emit_memcpy (guint8 *code, int size, int dreg, int doffset, int sreg, int soffset)
211 {
212         /* we can use r0-r3, since this is called only for incoming args on the stack */
213         if (size > sizeof (gpointer) * 4) {
214                 guint8 *start_loop;
215                 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
216                 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
217                 start_loop = code = mono_arm_emit_load_imm (code, ARMREG_R2, size);
218                 ARM_LDR_IMM (code, ARMREG_R3, ARMREG_R0, 0);
219                 ARM_STR_IMM (code, ARMREG_R3, ARMREG_R1, 0);
220                 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, 4);
221                 ARM_ADD_REG_IMM8 (code, ARMREG_R1, ARMREG_R1, 4);
222                 ARM_SUBS_REG_IMM8 (code, ARMREG_R2, ARMREG_R2, 4);
223                 ARM_B_COND (code, ARMCOND_NE, 0);
224                 arm_patch (code - 4, start_loop);
225                 return code;
226         }
227         if (arm_is_imm12 (doffset) && arm_is_imm12 (doffset + size) &&
228                         arm_is_imm12 (soffset) && arm_is_imm12 (soffset + size)) {
229                 while (size >= 4) {
230                         ARM_LDR_IMM (code, ARMREG_LR, sreg, soffset);
231                         ARM_STR_IMM (code, ARMREG_LR, dreg, doffset);
232                         doffset += 4;
233                         soffset += 4;
234                         size -= 4;
235                 }
236         } else if (size) {
237                 code = emit_big_add (code, ARMREG_R0, sreg, soffset);
238                 code = emit_big_add (code, ARMREG_R1, dreg, doffset);
239                 doffset = soffset = 0;
240                 while (size >= 4) {
241                         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_R0, soffset);
242                         ARM_STR_IMM (code, ARMREG_LR, ARMREG_R1, doffset);
243                         doffset += 4;
244                         soffset += 4;
245                         size -= 4;
246                 }
247         }
248         g_assert (size == 0);
249         return code;
250 }
251
252 static guint8*
253 emit_call_reg (guint8 *code, int reg)
254 {
255         if (v5_supported) {
256                 ARM_BLX_REG (code, reg);
257         } else {
258                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
259                 if (thumb_supported)
260                         ARM_BX (code, reg);
261                 else
262                         ARM_MOV_REG_REG (code, ARMREG_PC, reg);
263         }
264         return code;
265 }
266
267 static guint8*
268 emit_call_seq (MonoCompile *cfg, guint8 *code)
269 {
270         if (cfg->method->dynamic) {
271                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
272                 ARM_B (code, 0);
273                 *(gpointer*)code = NULL;
274                 code += 4;
275                 code = emit_call_reg (code, ARMREG_IP);
276         } else {
277                 ARM_BL (code, 0);
278         }
279         return code;
280 }
281
282 static guint8*
283 emit_move_return_value (MonoCompile *cfg, MonoInst *ins, guint8 *code)
284 {
285         switch (ins->opcode) {
286         case OP_FCALL:
287         case OP_FCALL_REG:
288         case OP_FCALL_MEMBASE:
289                 if (IS_FPA) {
290                         if (ins->dreg != ARM_FPA_F0)
291                                 ARM_FPA_MVFD (code, ins->dreg, ARM_FPA_F0);
292                 } else if (IS_VFP) {
293                         if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
294                                 ARM_FMSR (code, ins->dreg, ARMREG_R0);
295                                 ARM_CVTS (code, ins->dreg, ins->dreg);
296                         } else {
297                                 ARM_FMDRR (code, ARMREG_R0, ARMREG_R1, ins->dreg);
298                         }
299                 }
300                 break;
301         }
302
303         return code;
304 }
305
306 /*
307  * emit_save_lmf:
308  *
309  *   Emit code to push an LMF structure on the LMF stack.
310  * On arm, this is intermixed with the initialization of other fields of the structure.
311  */
312 static guint8*
313 emit_save_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
314 {
315         gboolean get_lmf_fast = FALSE;
316         int i;
317
318 #ifdef HAVE_AEABI_READ_TP
319         gint32 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
320
321         if (lmf_addr_tls_offset != -1) {
322                 get_lmf_fast = TRUE;
323
324                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
325                                                          (gpointer)"__aeabi_read_tp");
326                 code = emit_call_seq (cfg, code);
327
328                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, lmf_addr_tls_offset);
329                 get_lmf_fast = TRUE;
330         }
331 #endif
332         if (!get_lmf_fast) {
333                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
334                                                                  (gpointer)"mono_get_lmf_addr");
335                 code = emit_call_seq (cfg, code);
336         }
337         /* we build the MonoLMF structure on the stack - see mini-arm.h */
338         /* lmf_offset is the offset from the previous stack pointer,
339          * alloc_size is the total stack space allocated, so the offset
340          * of MonoLMF from the current stack ptr is alloc_size - lmf_offset.
341          * The pointer to the struct is put in r1 (new_lmf).
342          * ip is used as scratch
343          * The callee-saved registers are already in the MonoLMF structure
344          */
345         code = emit_big_add (code, ARMREG_R1, ARMREG_SP, lmf_offset);
346         /* r0 is the result from mono_get_lmf_addr () */
347         ARM_STR_IMM (code, ARMREG_R0, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, lmf_addr));
348         /* new_lmf->previous_lmf = *lmf_addr */
349         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
350         ARM_STR_IMM (code, ARMREG_IP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
351         /* *(lmf_addr) = r1 */
352         ARM_STR_IMM (code, ARMREG_R1, ARMREG_R0, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
353         /* Skip method (only needed for trampoline LMF frames) */
354         ARM_STR_IMM (code, ARMREG_SP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, esp));
355         /* save the current IP */
356         ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
357         ARM_STR_IMM (code, ARMREG_IP, ARMREG_R1, G_STRUCT_OFFSET (MonoLMF, eip));
358
359         for (i = 0; i < sizeof (MonoLMF); i += sizeof (mgreg_t))
360                 mini_gc_set_slot_type_from_fp (cfg, lmf_offset + i, SLOT_NOREF);
361
362         return code;
363 }
364
365 /*
366  * emit_save_lmf:
367  *
368  *   Emit code to pop an LMF structure from the LMF stack.
369  */
370 static guint8*
371 emit_restore_lmf (MonoCompile *cfg, guint8 *code, gint32 lmf_offset)
372 {
373         int basereg, offset;
374
375         if (lmf_offset < 32) {
376                 basereg = cfg->frame_reg;
377                 offset = lmf_offset;
378         } else {
379                 basereg = ARMREG_R2;
380                 offset = 0;
381                 code = emit_big_add (code, ARMREG_R2, cfg->frame_reg, lmf_offset);
382         }
383
384         /* ip = previous_lmf */
385         ARM_LDR_IMM (code, ARMREG_IP, basereg, offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf));
386         /* lr = lmf_addr */
387         ARM_LDR_IMM (code, ARMREG_LR, basereg, offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr));
388         /* *(lmf_addr) = previous_lmf */
389         ARM_STR_IMM (code, ARMREG_IP, ARMREG_LR, G_STRUCT_OFFSET (MonoLMF, previous_lmf));
390
391         return code;
392 }
393
394 #endif /* #ifndef DISABLE_JIT */
395
396 /*
397  * mono_arch_get_argument_info:
398  * @csig:  a method signature
399  * @param_count: the number of parameters to consider
400  * @arg_info: an array to store the result infos
401  *
402  * Gathers information on parameters such as size, alignment and
403  * padding. arg_info should be large enought to hold param_count + 1 entries. 
404  *
405  * Returns the size of the activation frame.
406  */
407 int
408 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
409 {
410         int k, frame_size = 0;
411         guint32 size, align, pad;
412         int offset = 8;
413
414         if (MONO_TYPE_ISSTRUCT (csig->ret)) { 
415                 frame_size += sizeof (gpointer);
416                 offset += 4;
417         }
418
419         arg_info [0].offset = offset;
420
421         if (csig->hasthis) {
422                 frame_size += sizeof (gpointer);
423                 offset += 4;
424         }
425
426         arg_info [0].size = frame_size;
427
428         for (k = 0; k < param_count; k++) {
429                 size = mini_type_stack_size_full (NULL, csig->params [k], &align, csig->pinvoke);
430
431                 /* ignore alignment for now */
432                 align = 1;
433
434                 frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1); 
435                 arg_info [k].pad = pad;
436                 frame_size += size;
437                 arg_info [k + 1].pad = 0;
438                 arg_info [k + 1].size = size;
439                 offset += pad;
440                 arg_info [k + 1].offset = offset;
441                 offset += size;
442         }
443
444         align = MONO_ARCH_FRAME_ALIGNMENT;
445         frame_size += pad = (align - (frame_size & (align - 1))) & (align - 1);
446         arg_info [k].pad = pad;
447
448         return frame_size;
449 }
450
451 #define MAX_ARCH_DELEGATE_PARAMS 3
452
453 static gpointer
454 get_delegate_invoke_impl (gboolean has_target, gboolean param_count, guint32 *code_size)
455 {
456         guint8 *code, *start;
457
458         if (has_target) {
459                 start = code = mono_global_codeman_reserve (12);
460
461                 /* Replace the this argument with the target */
462                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
463                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, target));
464                 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
465
466                 g_assert ((code - start) <= 12);
467
468                 mono_arch_flush_icache (start, 12);
469         } else {
470                 int size, i;
471
472                 size = 8 + param_count * 4;
473                 start = code = mono_global_codeman_reserve (size);
474
475                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_R0, G_STRUCT_OFFSET (MonoDelegate, method_ptr));
476                 /* slide down the arguments */
477                 for (i = 0; i < param_count; ++i) {
478                         ARM_MOV_REG_REG (code, (ARMREG_R0 + i), (ARMREG_R0 + i + 1));
479                 }
480                 ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
481
482                 g_assert ((code - start) <= size);
483
484                 mono_arch_flush_icache (start, size);
485         }
486
487         if (code_size)
488                 *code_size = code - start;
489
490         return start;
491 }
492
493 /*
494  * mono_arch_get_delegate_invoke_impls:
495  *
496  *   Return a list of MonoAotTrampInfo structures for the delegate invoke impl
497  * trampolines.
498  */
499 GSList*
500 mono_arch_get_delegate_invoke_impls (void)
501 {
502         GSList *res = NULL;
503         guint8 *code;
504         guint32 code_len;
505         int i;
506
507         code = get_delegate_invoke_impl (TRUE, 0, &code_len);
508         res = g_slist_prepend (res, mono_tramp_info_create (g_strdup ("delegate_invoke_impl_has_target"), code, code_len, NULL, NULL));
509
510         for (i = 0; i <= MAX_ARCH_DELEGATE_PARAMS; ++i) {
511                 code = get_delegate_invoke_impl (FALSE, i, &code_len);
512                 res = g_slist_prepend (res, mono_tramp_info_create (g_strdup_printf ("delegate_invoke_impl_target_%d", i), code, code_len, NULL, NULL));
513         }
514
515         return res;
516 }
517
518 gpointer
519 mono_arch_get_delegate_invoke_impl (MonoMethodSignature *sig, gboolean has_target)
520 {
521         guint8 *code, *start;
522
523         /* FIXME: Support more cases */
524         if (MONO_TYPE_ISSTRUCT (sig->ret))
525                 return NULL;
526
527         if (has_target) {
528                 static guint8* cached = NULL;
529                 mono_mini_arch_lock ();
530                 if (cached) {
531                         mono_mini_arch_unlock ();
532                         return cached;
533                 }
534
535                 if (mono_aot_only)
536                         start = mono_aot_get_trampoline ("delegate_invoke_impl_has_target");
537                 else
538                         start = get_delegate_invoke_impl (TRUE, 0, NULL);
539                 cached = start;
540                 mono_mini_arch_unlock ();
541                 return cached;
542         } else {
543                 static guint8* cache [MAX_ARCH_DELEGATE_PARAMS + 1] = {NULL};
544                 int i;
545
546                 if (sig->param_count > MAX_ARCH_DELEGATE_PARAMS)
547                         return NULL;
548                 for (i = 0; i < sig->param_count; ++i)
549                         if (!mono_is_regsize_var (sig->params [i]))
550                                 return NULL;
551
552                 mono_mini_arch_lock ();
553                 code = cache [sig->param_count];
554                 if (code) {
555                         mono_mini_arch_unlock ();
556                         return code;
557                 }
558
559                 if (mono_aot_only) {
560                         char *name = g_strdup_printf ("delegate_invoke_impl_target_%d", sig->param_count);
561                         start = mono_aot_get_trampoline (name);
562                         g_free (name);
563                 } else {
564                         start = get_delegate_invoke_impl (FALSE, sig->param_count, NULL);
565                 }
566                 cache [sig->param_count] = start;
567                 mono_mini_arch_unlock ();
568                 return start;
569         }
570
571         return NULL;
572 }
573
574 gpointer
575 mono_arch_get_this_arg_from_call (mgreg_t *regs, guint8 *code)
576 {
577         return (gpointer)regs [ARMREG_R0];
578 }
579
580 /*
581  * Initialize the cpu to execute managed code.
582  */
583 void
584 mono_arch_cpu_init (void)
585 {
586 #if defined(__ARM_EABI__)
587         eabi_supported = TRUE;
588 #endif
589 #if defined(__APPLE__) && defined(MONO_CROSS_COMPILE)
590                 i8_align = 4;
591 #else
592                 i8_align = __alignof__ (gint64);
593 #endif
594 }
595
596 static gpointer
597 create_function_wrapper (gpointer function)
598 {
599         guint8 *start, *code;
600
601         start = code = mono_global_codeman_reserve (96);
602
603         /*
604          * Construct the MonoContext structure on the stack.
605          */
606
607         ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, sizeof (MonoContext));
608
609         /* save ip, lr and pc into their correspodings ctx.regs slots. */
610         ARM_STR_IMM (code, ARMREG_IP, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_IP);
611         ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
612         ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
613
614         /* save r0..r10 and fp */
615         ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, regs));
616         ARM_STM (code, ARMREG_IP, 0x0fff);
617
618         /* now we can update fp. */
619         ARM_MOV_REG_REG (code, ARMREG_FP, ARMREG_SP);
620
621         /* make ctx.esp hold the actual value of sp at the beginning of this method. */
622         ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_FP, sizeof (MonoContext));
623         ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, 4 * ARMREG_SP);
624         ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, esp));
625
626         /* make ctx.eip hold the address of the call. */
627         ARM_SUB_REG_IMM8 (code, ARMREG_LR, ARMREG_LR, 4);
628         ARM_STR_IMM (code, ARMREG_LR, ARMREG_SP, G_STRUCT_OFFSET (MonoContext, eip));
629
630         /* r0 now points to the MonoContext */
631         ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_FP);
632
633         /* call */
634         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
635         ARM_B (code, 0);
636         *(gpointer*)code = function;
637         code += 4;
638         ARM_BLX_REG (code, ARMREG_IP);
639
640         /* we're back; save ctx.eip and ctx.esp into the corresponding regs slots. */
641         ARM_LDR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, eip));
642         ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_LR);
643         ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_PC);
644         ARM_LDR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, esp));
645         ARM_STR_IMM (code, ARMREG_R0, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, regs) + 4 * ARMREG_SP);
646
647         /* make ip point to the regs array, then restore everything, including pc. */
648         ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_FP, G_STRUCT_OFFSET (MonoContext, regs));
649         ARM_LDM (code, ARMREG_IP, 0xffff);
650
651         mono_arch_flush_icache (start, code - start);
652
653         return start;
654 }
655
656 /*
657  * Initialize architecture specific code.
658  */
659 void
660 mono_arch_init (void)
661 {
662         InitializeCriticalSection (&mini_arch_mutex);
663
664         if (mini_get_debug_options ()->soft_breakpoints) {
665                 single_step_func_wrapper = create_function_wrapper (debugger_agent_single_step_from_context);
666                 breakpoint_func_wrapper = create_function_wrapper (debugger_agent_breakpoint_from_context);
667         } else {
668                 ss_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
669                 bp_trigger_page = mono_valloc (NULL, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_32BIT);
670                 mono_mprotect (bp_trigger_page, mono_pagesize (), 0);
671         }
672
673         mono_aot_register_jit_icall ("mono_arm_throw_exception", mono_arm_throw_exception);
674         mono_aot_register_jit_icall ("mono_arm_throw_exception_by_token", mono_arm_throw_exception_by_token);
675         mono_aot_register_jit_icall ("mono_arm_resume_unwind", mono_arm_resume_unwind);
676
677 #ifdef ARM_FPU_FPA
678         arm_fpu = MONO_ARM_FPU_FPA;
679 #elif defined(ARM_FPU_VFP)
680         arm_fpu = MONO_ARM_FPU_VFP;
681 #else
682         arm_fpu = MONO_ARM_FPU_NONE;
683 #endif
684 }
685
686 /*
687  * Cleanup architecture specific code.
688  */
689 void
690 mono_arch_cleanup (void)
691 {
692 }
693
694 /*
695  * This function returns the optimizations supported on this cpu.
696  */
697 guint32
698 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
699 {
700         guint32 opts = 0;
701         const char *cpu_arch = getenv ("MONO_CPU_ARCH");
702         if (cpu_arch != NULL) {
703                 thumb_supported = strstr (cpu_arch, "thumb") != NULL;
704                 if (strncmp (cpu_arch, "armv", 4) == 0) {
705                         v5_supported = cpu_arch [4] >= '5';
706                         v6_supported = cpu_arch [4] >= '6';
707                         v7_supported = cpu_arch [4] >= '7';
708                 }
709         } else {
710 #if __APPLE__
711         thumb_supported = TRUE;
712         v5_supported = TRUE;
713         darwin = TRUE;
714         iphone_abi = TRUE;
715 #else
716         char buf [512];
717         char *line;
718         FILE *file = fopen ("/proc/cpuinfo", "r");
719         if (file) {
720                 while ((line = fgets (buf, 512, file))) {
721                         if (strncmp (line, "Processor", 9) == 0) {
722                                 char *ver = strstr (line, "(v");
723                                 if (ver && (ver [2] == '5' || ver [2] == '6' || ver [2] == '7'))
724                                         v5_supported = TRUE;
725                                 if (ver && (ver [2] == '6' || ver [2] == '7'))
726                                         v6_supported = TRUE;
727                                 if (ver && (ver [2] == '7'))
728                                         v7_supported = TRUE;
729                                 continue;
730                         }
731                         if (strncmp (line, "Features", 8) == 0) {
732                                 char *th = strstr (line, "thumb");
733                                 if (th) {
734                                         thumb_supported = TRUE;
735                                         if (v5_supported)
736                                                 break;
737                                 }
738                                 continue;
739                         }
740                 }
741                 fclose (file);
742                 /*printf ("features: v5: %d, thumb: %d\n", v5_supported, thumb_supported);*/
743         }
744 #endif
745         }
746
747         /* no arm-specific optimizations yet */
748         *exclude_mask = 0;
749         return opts;
750 }
751
752 #ifndef DISABLE_JIT
753
754 static gboolean
755 is_regsize_var (MonoType *t) {
756         if (t->byref)
757                 return TRUE;
758         t = mini_type_get_underlying_type (NULL, t);
759         switch (t->type) {
760         case MONO_TYPE_I4:
761         case MONO_TYPE_U4:
762         case MONO_TYPE_I:
763         case MONO_TYPE_U:
764         case MONO_TYPE_PTR:
765         case MONO_TYPE_FNPTR:
766                 return TRUE;
767         case MONO_TYPE_OBJECT:
768         case MONO_TYPE_STRING:
769         case MONO_TYPE_CLASS:
770         case MONO_TYPE_SZARRAY:
771         case MONO_TYPE_ARRAY:
772                 return TRUE;
773         case MONO_TYPE_GENERICINST:
774                 if (!mono_type_generic_inst_is_valuetype (t))
775                         return TRUE;
776                 return FALSE;
777         case MONO_TYPE_VALUETYPE:
778                 return FALSE;
779         }
780         return FALSE;
781 }
782
783 GList *
784 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
785 {
786         GList *vars = NULL;
787         int i;
788
789         for (i = 0; i < cfg->num_varinfo; i++) {
790                 MonoInst *ins = cfg->varinfo [i];
791                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
792
793                 /* unused vars */
794                 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
795                         continue;
796
797                 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
798                         continue;
799
800                 /* we can only allocate 32 bit values */
801                 if (is_regsize_var (ins->inst_vtype)) {
802                         g_assert (MONO_VARINFO (cfg, i)->reg == -1);
803                         g_assert (i == vmv->idx);
804                         vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
805                 }
806         }
807
808         return vars;
809 }
810
811 #define USE_EXTRA_TEMPS 0
812
813 GList *
814 mono_arch_get_global_int_regs (MonoCompile *cfg)
815 {
816         GList *regs = NULL;
817
818         mono_arch_compute_omit_fp (cfg);
819
820         /* 
821          * FIXME: Interface calls might go through a static rgctx trampoline which
822          * sets V5, but it doesn't save it, so we need to save it ourselves, and
823          * avoid using it.
824          */
825         if (cfg->flags & MONO_CFG_HAS_CALLS)
826                 cfg->uses_rgctx_reg = TRUE;
827
828         if (cfg->arch.omit_fp)
829                 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_FP));
830         regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V1));
831         regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V2));
832         regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V3));
833         if (darwin)
834                 /* V4=R7 is used as a frame pointer, but V7=R10 is preserved */
835                 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));
836         else
837                 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V4));
838         if (!(cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg)))
839                 /* V5 is reserved for passing the vtable/rgctx/IMT method */
840                 regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V5));
841         /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V6));*/
842         /*regs = g_list_prepend (regs, GUINT_TO_POINTER (ARMREG_V7));*/
843
844         return regs;
845 }
846
847 /*
848  * mono_arch_regalloc_cost:
849  *
850  *  Return the cost, in number of memory references, of the action of 
851  * allocating the variable VMV into a register during global register
852  * allocation.
853  */
854 guint32
855 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
856 {
857         /* FIXME: */
858         return 2;
859 }
860
861 #endif /* #ifndef DISABLE_JIT */
862
863 #ifndef __GNUC_PREREQ
864 #define __GNUC_PREREQ(maj, min) (0)
865 #endif
866
867 void
868 mono_arch_flush_icache (guint8 *code, gint size)
869 {
870 #if __APPLE__
871         sys_icache_invalidate (code, size);
872 #elif __GNUC_PREREQ(4, 1)
873         __clear_cache (code, code + size);
874 #elif defined(PLATFORM_ANDROID)
875         const int syscall = 0xf0002;
876         __asm __volatile (
877                 "mov     r0, %0\n"                      
878                 "mov     r1, %1\n"
879                 "mov     r7, %2\n"
880                 "mov     r2, #0x0\n"
881                 "svc     0x00000000\n"
882                 :
883                 :       "r" (code), "r" (code + size), "r" (syscall)
884                 :       "r0", "r1", "r7", "r2"
885                 );
886 #else
887         __asm __volatile ("mov r0, %0\n"
888                         "mov r1, %1\n"
889                         "mov r2, %2\n"
890                         "swi 0x9f0002       @ sys_cacheflush"
891                         : /* no outputs */
892                         : "r" (code), "r" (code + size), "r" (0)
893                         : "r0", "r1", "r3" );
894 #endif
895 }
896
897 typedef enum {
898         RegTypeNone,
899         RegTypeGeneral,
900         RegTypeIRegPair,
901         RegTypeBase,
902         RegTypeBaseGen,
903         RegTypeFP,
904         RegTypeStructByVal,
905         RegTypeStructByAddr
906 } ArgStorage;
907
908 typedef struct {
909         gint32  offset;
910         guint16 vtsize; /* in param area */
911         guint8  reg;
912         ArgStorage  storage;
913         gint32  struct_size;
914         guint8  size    : 4; /* 1, 2, 4, 8, or regs used by RegTypeStructByVal */
915 } ArgInfo;
916
917 typedef struct {
918         int nargs;
919         guint32 stack_usage;
920         gboolean vtype_retaddr;
921         /* The index of the vret arg in the argument list */
922         int vret_arg_index;
923         ArgInfo ret;
924         ArgInfo sig_cookie;
925         ArgInfo args [1];
926 } CallInfo;
927
928 #define DEBUG(a)
929
930 #ifndef __GNUC__
931 /*#define __alignof__(a) sizeof(a)*/
932 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
933 #endif
934
935 #define PARAM_REGS 4
936
937 static void inline
938 add_general (guint *gr, guint *stack_size, ArgInfo *ainfo, gboolean simple)
939 {
940         if (simple) {
941                 if (*gr > ARMREG_R3) {
942                         ainfo->offset = *stack_size;
943                         ainfo->reg = ARMREG_SP; /* in the caller */
944                         ainfo->storage = RegTypeBase;
945                         *stack_size += 4;
946                 } else {
947                         ainfo->storage = RegTypeGeneral;
948                         ainfo->reg = *gr;
949                 }
950         } else {
951                 gboolean split;
952
953                 if (eabi_supported)
954                         split = i8_align == 4;
955                 else
956                         split = TRUE;
957                 
958                 if (*gr == ARMREG_R3 && split) {
959                         /* first word in r3 and the second on the stack */
960                         ainfo->offset = *stack_size;
961                         ainfo->reg = ARMREG_SP; /* in the caller */
962                         ainfo->storage = RegTypeBaseGen;
963                         *stack_size += 4;
964                 } else if (*gr >= ARMREG_R3) {
965                         if (eabi_supported) {
966                                 /* darwin aligns longs to 4 byte only */
967                                 if (i8_align == 8) {
968                                         *stack_size += 7;
969                                         *stack_size &= ~7;
970                                 }
971                         }
972                         ainfo->offset = *stack_size;
973                         ainfo->reg = ARMREG_SP; /* in the caller */
974                         ainfo->storage = RegTypeBase;
975                         *stack_size += 8;
976                 } else {
977                         if (eabi_supported) {
978                                 if (i8_align == 8 && ((*gr) & 1))
979                                         (*gr) ++;
980                         }
981                         ainfo->storage = RegTypeIRegPair;
982                         ainfo->reg = *gr;
983                 }
984                 (*gr) ++;
985         }
986         (*gr) ++;
987 }
988
989 static CallInfo*
990 get_call_info (MonoGenericSharingContext *gsctx, MonoMemPool *mp, MonoMethodSignature *sig)
991 {
992         guint i, gr, pstart;
993         int n = sig->hasthis + sig->param_count;
994         MonoType *simpletype;
995         guint32 stack_size = 0;
996         CallInfo *cinfo;
997         gboolean is_pinvoke = sig->pinvoke;
998
999         if (mp)
1000                 cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1001         else
1002                 cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
1003
1004         cinfo->nargs = n;
1005         gr = ARMREG_R0;
1006
1007         /* FIXME: handle returning a struct */
1008         if (MONO_TYPE_ISSTRUCT (sig->ret)) {
1009                 guint32 align;
1010
1011                 if (is_pinvoke && mono_class_native_size (mono_class_from_mono_type (sig->ret), &align) <= sizeof (gpointer)) {
1012                         cinfo->ret.storage = RegTypeStructByVal;
1013                 } else {
1014                         cinfo->vtype_retaddr = TRUE;
1015                 }
1016         }
1017
1018         pstart = 0;
1019         n = 0;
1020         /*
1021          * To simplify get_this_arg_reg () and LLVM integration, emit the vret arg after
1022          * the first argument, allowing 'this' to be always passed in the first arg reg.
1023          * Also do this if the first argument is a reference type, since virtual calls
1024          * are sometimes made using calli without sig->hasthis set, like in the delegate
1025          * invoke wrappers.
1026          */
1027         if (cinfo->vtype_retaddr && !is_pinvoke && (sig->hasthis || (sig->param_count > 0 && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (gsctx, sig->params [0]))))) {
1028                 if (sig->hasthis) {
1029                         add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
1030                 } else {
1031                         add_general (&gr, &stack_size, &cinfo->args [sig->hasthis + 0], TRUE);
1032                         pstart = 1;
1033                 }
1034                 n ++;
1035                 add_general (&gr, &stack_size, &cinfo->ret, TRUE);
1036                 cinfo->vret_arg_index = 1;
1037         } else {
1038                 /* this */
1039                 if (sig->hasthis) {
1040                         add_general (&gr, &stack_size, cinfo->args + 0, TRUE);
1041                         n ++;
1042                 }
1043
1044                 if (cinfo->vtype_retaddr)
1045                         add_general (&gr, &stack_size, &cinfo->ret, TRUE);
1046         }
1047
1048         DEBUG(printf("params: %d\n", sig->param_count));
1049         for (i = pstart; i < sig->param_count; ++i) {
1050                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1051                         /* Prevent implicit arguments and sig_cookie from
1052                            being passed in registers */
1053                         gr = ARMREG_R3 + 1;
1054                         /* Emit the signature cookie just before the implicit arguments */
1055                         add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
1056                 }
1057                 DEBUG(printf("param %d: ", i));
1058                 if (sig->params [i]->byref) {
1059                         DEBUG(printf("byref\n"));
1060                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1061                         n++;
1062                         continue;
1063                 }
1064                 simpletype = mini_type_get_underlying_type (NULL, sig->params [i]);
1065                 switch (simpletype->type) {
1066                 case MONO_TYPE_BOOLEAN:
1067                 case MONO_TYPE_I1:
1068                 case MONO_TYPE_U1:
1069                         cinfo->args [n].size = 1;
1070                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1071                         n++;
1072                         break;
1073                 case MONO_TYPE_CHAR:
1074                 case MONO_TYPE_I2:
1075                 case MONO_TYPE_U2:
1076                         cinfo->args [n].size = 2;
1077                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1078                         n++;
1079                         break;
1080                 case MONO_TYPE_I4:
1081                 case MONO_TYPE_U4:
1082                         cinfo->args [n].size = 4;
1083                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1084                         n++;
1085                         break;
1086                 case MONO_TYPE_I:
1087                 case MONO_TYPE_U:
1088                 case MONO_TYPE_PTR:
1089                 case MONO_TYPE_FNPTR:
1090                 case MONO_TYPE_CLASS:
1091                 case MONO_TYPE_OBJECT:
1092                 case MONO_TYPE_STRING:
1093                 case MONO_TYPE_SZARRAY:
1094                 case MONO_TYPE_ARRAY:
1095                 case MONO_TYPE_R4:
1096                         cinfo->args [n].size = sizeof (gpointer);
1097                         add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1098                         n++;
1099                         break;
1100                 case MONO_TYPE_GENERICINST:
1101                         if (!mono_type_generic_inst_is_valuetype (simpletype)) {
1102                                 cinfo->args [n].size = sizeof (gpointer);
1103                                 add_general (&gr, &stack_size, cinfo->args + n, TRUE);
1104                                 n++;
1105                                 break;
1106                         }
1107                         /* Fall through */
1108                 case MONO_TYPE_TYPEDBYREF:
1109                 case MONO_TYPE_VALUETYPE: {
1110                         gint size;
1111                         int align_size;
1112                         int nwords;
1113                         guint32 align;
1114
1115                         if (simpletype->type == MONO_TYPE_TYPEDBYREF) {
1116                                 size = sizeof (MonoTypedRef);
1117                                 align = sizeof (gpointer);
1118                         } else {
1119                                 MonoClass *klass = mono_class_from_mono_type (sig->params [i]);
1120                                 if (is_pinvoke)
1121                                         size = mono_class_native_size (klass, &align);
1122                                 else
1123                                         size = mono_class_value_size (klass, &align);
1124                         }
1125                         DEBUG(printf ("load %d bytes struct\n",
1126                                       mono_class_native_size (sig->params [i]->data.klass, NULL)));
1127                         align_size = size;
1128                         nwords = 0;
1129                         align_size += (sizeof (gpointer) - 1);
1130                         align_size &= ~(sizeof (gpointer) - 1);
1131                         nwords = (align_size + sizeof (gpointer) -1 ) / sizeof (gpointer);
1132                         cinfo->args [n].storage = RegTypeStructByVal;
1133                         cinfo->args [n].struct_size = size;
1134                         /* FIXME: align stack_size if needed */
1135                         if (eabi_supported) {
1136                                 if (align >= 8 && (gr & 1))
1137                                         gr ++;
1138                         }
1139                         if (gr > ARMREG_R3) {
1140                                 cinfo->args [n].size = 0;
1141                                 cinfo->args [n].vtsize = nwords;
1142                         } else {
1143                                 int rest = ARMREG_R3 - gr + 1;
1144                                 int n_in_regs = rest >= nwords? nwords: rest;
1145
1146                                 cinfo->args [n].size = n_in_regs;
1147                                 cinfo->args [n].vtsize = nwords - n_in_regs;
1148                                 cinfo->args [n].reg = gr;
1149                                 gr += n_in_regs;
1150                                 nwords -= n_in_regs;
1151                         }
1152                         cinfo->args [n].offset = stack_size;
1153                         /*g_print ("offset for arg %d at %d\n", n, stack_size);*/
1154                         stack_size += nwords * sizeof (gpointer);
1155                         n++;
1156                         break;
1157                 }
1158                 case MONO_TYPE_U8:
1159                 case MONO_TYPE_I8:
1160                 case MONO_TYPE_R8:
1161                         cinfo->args [n].size = 8;
1162                         add_general (&gr, &stack_size, cinfo->args + n, FALSE);
1163                         n++;
1164                         break;
1165                 default:
1166                         g_error ("Can't trampoline 0x%x", sig->params [i]->type);
1167                 }
1168         }
1169
1170         /* Handle the case where there are no implicit arguments */
1171         if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1172                 /* Prevent implicit arguments and sig_cookie from
1173                    being passed in registers */
1174                 gr = ARMREG_R3 + 1;
1175                 /* Emit the signature cookie just before the implicit arguments */
1176                 add_general (&gr, &stack_size, &cinfo->sig_cookie, TRUE);
1177         }
1178
1179         {
1180                 simpletype = mini_type_get_underlying_type (NULL, sig->ret);
1181                 switch (simpletype->type) {
1182                 case MONO_TYPE_BOOLEAN:
1183                 case MONO_TYPE_I1:
1184                 case MONO_TYPE_U1:
1185                 case MONO_TYPE_I2:
1186                 case MONO_TYPE_U2:
1187                 case MONO_TYPE_CHAR:
1188                 case MONO_TYPE_I4:
1189                 case MONO_TYPE_U4:
1190                 case MONO_TYPE_I:
1191                 case MONO_TYPE_U:
1192                 case MONO_TYPE_PTR:
1193                 case MONO_TYPE_FNPTR:
1194                 case MONO_TYPE_CLASS:
1195                 case MONO_TYPE_OBJECT:
1196                 case MONO_TYPE_SZARRAY:
1197                 case MONO_TYPE_ARRAY:
1198                 case MONO_TYPE_STRING:
1199                         cinfo->ret.storage = RegTypeGeneral;
1200                         cinfo->ret.reg = ARMREG_R0;
1201                         break;
1202                 case MONO_TYPE_U8:
1203                 case MONO_TYPE_I8:
1204                         cinfo->ret.storage = RegTypeIRegPair;
1205                         cinfo->ret.reg = ARMREG_R0;
1206                         break;
1207                 case MONO_TYPE_R4:
1208                 case MONO_TYPE_R8:
1209                         cinfo->ret.storage = RegTypeFP;
1210                         cinfo->ret.reg = ARMREG_R0;
1211                         /* FIXME: cinfo->ret.reg = ???;
1212                         cinfo->ret.storage = RegTypeFP;*/
1213                         break;
1214                 case MONO_TYPE_GENERICINST:
1215                         if (!mono_type_generic_inst_is_valuetype (simpletype)) {
1216                                 cinfo->ret.storage = RegTypeGeneral;
1217                                 cinfo->ret.reg = ARMREG_R0;
1218                                 break;
1219                         }
1220                         /* Fall through */
1221                 case MONO_TYPE_VALUETYPE:
1222                 case MONO_TYPE_TYPEDBYREF:
1223                         if (cinfo->ret.storage != RegTypeStructByVal)
1224                                 cinfo->ret.storage = RegTypeStructByAddr;
1225                         break;
1226                 case MONO_TYPE_VOID:
1227                         break;
1228                 default:
1229                         g_error ("Can't handle as return value 0x%x", sig->ret->type);
1230                 }
1231         }
1232
1233         /* align stack size to 8 */
1234         DEBUG (printf ("      stack size: %d (%d)\n", (stack_size + 15) & ~15, stack_size));
1235         stack_size = (stack_size + 7) & ~7;
1236
1237         cinfo->stack_usage = stack_size;
1238         return cinfo;
1239 }
1240
1241 #ifndef DISABLE_JIT
1242
1243 G_GNUC_UNUSED static void
1244 break_count (void)
1245 {
1246 }
1247
1248 G_GNUC_UNUSED static gboolean
1249 debug_count (void)
1250 {
1251         static int count = 0;
1252         count ++;
1253
1254         if (!getenv ("COUNT"))
1255                 return TRUE;
1256
1257         if (count == atoi (getenv ("COUNT"))) {
1258                 break_count ();
1259         }
1260
1261         if (count > atoi (getenv ("COUNT"))) {
1262                 return FALSE;
1263         }
1264
1265         return TRUE;
1266 }
1267
1268 static gboolean
1269 debug_omit_fp (void)
1270 {
1271 #if 0
1272         return debug_count ();
1273 #else
1274         return TRUE;
1275 #endif
1276 }
1277
1278 /**
1279  * mono_arch_compute_omit_fp:
1280  *
1281  *   Determine whenever the frame pointer can be eliminated.
1282  */
1283 static void
1284 mono_arch_compute_omit_fp (MonoCompile *cfg)
1285 {
1286         MonoMethodSignature *sig;
1287         MonoMethodHeader *header;
1288         int i, locals_size;
1289         CallInfo *cinfo;
1290
1291         if (cfg->arch.omit_fp_computed)
1292                 return;
1293
1294         header = cfg->header;
1295
1296         sig = mono_method_signature (cfg->method);
1297
1298         if (!cfg->arch.cinfo)
1299                 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1300         cinfo = cfg->arch.cinfo;
1301
1302         /*
1303          * FIXME: Remove some of the restrictions.
1304          */
1305         cfg->arch.omit_fp = TRUE;
1306         cfg->arch.omit_fp_computed = TRUE;
1307
1308         if (cfg->disable_omit_fp)
1309                 cfg->arch.omit_fp = FALSE;
1310         if (!debug_omit_fp ())
1311                 cfg->arch.omit_fp = FALSE;
1312         /*
1313         if (cfg->method->save_lmf)
1314                 cfg->arch.omit_fp = FALSE;
1315         */
1316         if (cfg->flags & MONO_CFG_HAS_ALLOCA)
1317                 cfg->arch.omit_fp = FALSE;
1318         if (header->num_clauses)
1319                 cfg->arch.omit_fp = FALSE;
1320         if (cfg->param_area)
1321                 cfg->arch.omit_fp = FALSE;
1322         if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG))
1323                 cfg->arch.omit_fp = FALSE;
1324         if ((mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method)) ||
1325                 (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE))
1326                 cfg->arch.omit_fp = FALSE;
1327         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1328                 ArgInfo *ainfo = &cinfo->args [i];
1329
1330                 if (ainfo->storage == RegTypeBase || ainfo->storage == RegTypeBaseGen || ainfo->storage == RegTypeStructByVal) {
1331                         /* 
1332                          * The stack offset can only be determined when the frame
1333                          * size is known.
1334                          */
1335                         cfg->arch.omit_fp = FALSE;
1336                 }
1337         }
1338
1339         locals_size = 0;
1340         for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
1341                 MonoInst *ins = cfg->varinfo [i];
1342                 int ialign;
1343
1344                 locals_size += mono_type_size (ins->inst_vtype, &ialign);
1345         }
1346 }
1347
1348 /*
1349  * Set var information according to the calling convention. arm version.
1350  * The locals var stuff should most likely be split in another method.
1351  */
1352 void
1353 mono_arch_allocate_vars (MonoCompile *cfg)
1354 {
1355         MonoMethodSignature *sig;
1356         MonoMethodHeader *header;
1357         MonoInst *ins;
1358         int i, offset, size, align, curinst;
1359         CallInfo *cinfo;
1360         guint32 ualign;
1361
1362         sig = mono_method_signature (cfg->method);
1363
1364         if (!cfg->arch.cinfo)
1365                 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1366         cinfo = cfg->arch.cinfo;
1367
1368         mono_arch_compute_omit_fp (cfg);
1369
1370         if (cfg->arch.omit_fp)
1371                 cfg->frame_reg = ARMREG_SP;
1372         else
1373                 cfg->frame_reg = ARMREG_FP;
1374
1375         cfg->flags |= MONO_CFG_HAS_SPILLUP;
1376
1377         /* allow room for the vararg method args: void* and long/double */
1378         if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1379                 cfg->param_area = MAX (cfg->param_area, sizeof (gpointer)*8);
1380
1381         header = cfg->header;
1382
1383         /* See mono_arch_get_global_int_regs () */
1384         if (cfg->flags & MONO_CFG_HAS_CALLS)
1385                 cfg->uses_rgctx_reg = TRUE;
1386
1387         if (cfg->frame_reg != ARMREG_SP)
1388                 cfg->used_int_regs |= 1 << cfg->frame_reg;
1389
1390         if (cfg->compile_aot || cfg->uses_rgctx_reg || COMPILE_LLVM (cfg))
1391                 /* V5 is reserved for passing the vtable/rgctx/IMT method */
1392                 cfg->used_int_regs |= (1 << ARMREG_V5);
1393
1394         offset = 0;
1395         curinst = 0;
1396         if (!MONO_TYPE_ISSTRUCT (sig->ret)) {
1397                 switch (mini_type_get_underlying_type (NULL, sig->ret)->type) {
1398                 case MONO_TYPE_VOID:
1399                         break;
1400                 default:
1401                         cfg->ret->opcode = OP_REGVAR;
1402                         cfg->ret->inst_c0 = ARMREG_R0;
1403                         break;
1404                 }
1405         }
1406         /* local vars are at a positive offset from the stack pointer */
1407         /* 
1408          * also note that if the function uses alloca, we use FP
1409          * to point at the local variables.
1410          */
1411         offset = 0; /* linkage area */
1412         /* align the offset to 16 bytes: not sure this is needed here  */
1413         //offset += 8 - 1;
1414         //offset &= ~(8 - 1);
1415
1416         /* add parameter area size for called functions */
1417         offset += cfg->param_area;
1418         offset += 8 - 1;
1419         offset &= ~(8 - 1);
1420         if (cfg->flags & MONO_CFG_HAS_FPOUT)
1421                 offset += 8;
1422
1423         /* allow room to save the return value */
1424         if (mono_jit_trace_calls != NULL && mono_trace_eval (cfg->method))
1425                 offset += 8;
1426
1427         /* the MonoLMF structure is stored just below the stack pointer */
1428         if (MONO_TYPE_ISSTRUCT (sig->ret)) {
1429                 if (cinfo->ret.storage == RegTypeStructByVal) {
1430                         cfg->ret->opcode = OP_REGOFFSET;
1431                         cfg->ret->inst_basereg = cfg->frame_reg;
1432                         offset += sizeof (gpointer) - 1;
1433                         offset &= ~(sizeof (gpointer) - 1);
1434                         cfg->ret->inst_offset = - offset;
1435                 } else {
1436                         ins = cfg->vret_addr;
1437                         offset += sizeof(gpointer) - 1;
1438                         offset &= ~(sizeof(gpointer) - 1);
1439                         ins->inst_offset = offset;
1440                         ins->opcode = OP_REGOFFSET;
1441                         ins->inst_basereg = cfg->frame_reg;
1442                         if (G_UNLIKELY (cfg->verbose_level > 1)) {
1443                                 printf ("vret_addr =");
1444                                 mono_print_ins (cfg->vret_addr);
1445                         }
1446                 }
1447                 offset += sizeof(gpointer);
1448         }
1449
1450         /* Allocate these first so they have a small offset, OP_SEQ_POINT depends on this */
1451         if (cfg->arch.seq_point_info_var) {
1452                 MonoInst *ins;
1453
1454                 ins = cfg->arch.seq_point_info_var;
1455
1456                 size = 4;
1457                 align = 4;
1458                 offset += align - 1;
1459                 offset &= ~(align - 1);
1460                 ins->opcode = OP_REGOFFSET;
1461                 ins->inst_basereg = cfg->frame_reg;
1462                 ins->inst_offset = offset;
1463                 offset += size;
1464
1465                 ins = cfg->arch.ss_trigger_page_var;
1466                 size = 4;
1467                 align = 4;
1468                 offset += align - 1;
1469                 offset &= ~(align - 1);
1470                 ins->opcode = OP_REGOFFSET;
1471                 ins->inst_basereg = cfg->frame_reg;
1472                 ins->inst_offset = offset;
1473                 offset += size;
1474         }
1475
1476         if (cfg->arch.seq_point_read_var) {
1477                 MonoInst *ins;
1478
1479                 ins = cfg->arch.seq_point_read_var;
1480
1481                 size = 4;
1482                 align = 4;
1483                 offset += align - 1;
1484                 offset &= ~(align - 1);
1485                 ins->opcode = OP_REGOFFSET;
1486                 ins->inst_basereg = cfg->frame_reg;
1487                 ins->inst_offset = offset;
1488                 offset += size;
1489
1490                 ins = cfg->arch.seq_point_ss_method_var;
1491                 size = 4;
1492                 align = 4;
1493                 offset += align - 1;
1494                 offset &= ~(align - 1);
1495                 ins->opcode = OP_REGOFFSET;
1496                 ins->inst_basereg = cfg->frame_reg;
1497                 ins->inst_offset = offset;
1498                 offset += size;
1499
1500                 ins = cfg->arch.seq_point_bp_method_var;
1501                 size = 4;
1502                 align = 4;
1503                 offset += align - 1;
1504                 offset &= ~(align - 1);
1505                 ins->opcode = OP_REGOFFSET;
1506                 ins->inst_basereg = cfg->frame_reg;
1507                 ins->inst_offset = offset;
1508                 offset += size;
1509         }
1510
1511         cfg->locals_min_stack_offset = offset;
1512
1513         curinst = cfg->locals_start;
1514         for (i = curinst; i < cfg->num_varinfo; ++i) {
1515                 ins = cfg->varinfo [i];
1516                 if ((ins->flags & MONO_INST_IS_DEAD) || ins->opcode == OP_REGVAR || ins->opcode == OP_REGOFFSET)
1517                         continue;
1518
1519                 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
1520                 * pinvoke wrappers when they call functions returning structure */
1521                 if (ins->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (ins->inst_vtype) && ins->inst_vtype->type != MONO_TYPE_TYPEDBYREF) {
1522                         size = mono_class_native_size (mono_class_from_mono_type (ins->inst_vtype), &ualign);
1523                         align = ualign;
1524                 }
1525                 else
1526                         size = mono_type_size (ins->inst_vtype, &align);
1527
1528                 /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1529                  * since it loads/stores misaligned words, which don't do the right thing.
1530                  */
1531                 if (align < 4 && size >= 4)
1532                         align = 4;
1533                 if (ALIGN_TO (offset, align) > ALIGN_TO (offset, 4))
1534                         mini_gc_set_slot_type_from_fp (cfg, ALIGN_TO (offset, 4), SLOT_NOREF);
1535                 offset += align - 1;
1536                 offset &= ~(align - 1);
1537                 ins->opcode = OP_REGOFFSET;
1538                 ins->inst_offset = offset;
1539                 ins->inst_basereg = cfg->frame_reg;
1540                 offset += size;
1541                 //g_print ("allocating local %d to %d\n", i, inst->inst_offset);
1542         }
1543
1544         cfg->locals_max_stack_offset = offset;
1545
1546         curinst = 0;
1547         if (sig->hasthis) {
1548                 ins = cfg->args [curinst];
1549                 if (ins->opcode != OP_REGVAR) {
1550                         ins->opcode = OP_REGOFFSET;
1551                         ins->inst_basereg = cfg->frame_reg;
1552                         offset += sizeof (gpointer) - 1;
1553                         offset &= ~(sizeof (gpointer) - 1);
1554                         ins->inst_offset = offset;
1555                         offset += sizeof (gpointer);
1556                 }
1557                 curinst++;
1558         }
1559
1560         if (sig->call_convention == MONO_CALL_VARARG) {
1561                 size = 4;
1562                 align = 4;
1563
1564                 /* Allocate a local slot to hold the sig cookie address */
1565                 offset += align - 1;
1566                 offset &= ~(align - 1);
1567                 cfg->sig_cookie = offset;
1568                 offset += size;
1569         }                       
1570
1571         for (i = 0; i < sig->param_count; ++i) {
1572                 ins = cfg->args [curinst];
1573
1574                 if (ins->opcode != OP_REGVAR) {
1575                         ins->opcode = OP_REGOFFSET;
1576                         ins->inst_basereg = cfg->frame_reg;
1577                         size = mini_type_stack_size_full (NULL, sig->params [i], &ualign, sig->pinvoke);
1578                         align = ualign;
1579                         /* FIXME: if a structure is misaligned, our memcpy doesn't work,
1580                          * since it loads/stores misaligned words, which don't do the right thing.
1581                          */
1582                         if (align < 4 && size >= 4)
1583                                 align = 4;
1584                         /* The code in the prolog () stores words when storing vtypes received in a register */
1585                         if (MONO_TYPE_ISSTRUCT (sig->params [i]))
1586                                 align = 4;
1587                         if (ALIGN_TO (offset, align) > ALIGN_TO (offset, 4))
1588                                 mini_gc_set_slot_type_from_fp (cfg, ALIGN_TO (offset, 4), SLOT_NOREF);
1589                         offset += align - 1;
1590                         offset &= ~(align - 1);
1591                         ins->inst_offset = offset;
1592                         offset += size;
1593                 }
1594                 curinst++;
1595         }
1596
1597         /* align the offset to 8 bytes */
1598         if (ALIGN_TO (offset, 8) > ALIGN_TO (offset, 4))
1599                 mini_gc_set_slot_type_from_fp (cfg, ALIGN_TO (offset, 4), SLOT_NOREF);
1600         offset += 8 - 1;
1601         offset &= ~(8 - 1);
1602
1603         /* change sign? */
1604         cfg->stack_offset = offset;
1605 }
1606
1607 void
1608 mono_arch_create_vars (MonoCompile *cfg)
1609 {
1610         MonoMethodSignature *sig;
1611         CallInfo *cinfo;
1612
1613         sig = mono_method_signature (cfg->method);
1614
1615         if (!cfg->arch.cinfo)
1616                 cfg->arch.cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1617         cinfo = cfg->arch.cinfo;
1618
1619         if (cinfo->ret.storage == RegTypeStructByVal)
1620                 cfg->ret_var_is_local = TRUE;
1621
1622         if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
1623                 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1624                 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1625                         printf ("vret_addr = ");
1626                         mono_print_ins (cfg->vret_addr);
1627                 }
1628         }
1629
1630         if (cfg->gen_seq_points) {
1631                 if (cfg->soft_breakpoints) {
1632                         MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1633                         ins->flags |= MONO_INST_VOLATILE;
1634                         cfg->arch.seq_point_read_var = ins;
1635
1636                         ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1637                         ins->flags |= MONO_INST_VOLATILE;
1638                         cfg->arch.seq_point_ss_method_var = ins;
1639
1640                         ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1641                         ins->flags |= MONO_INST_VOLATILE;
1642                         cfg->arch.seq_point_bp_method_var = ins;
1643
1644                         g_assert (!cfg->compile_aot);
1645                 } else if (cfg->compile_aot) {
1646                         MonoInst *ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1647                         ins->flags |= MONO_INST_VOLATILE;
1648                         cfg->arch.seq_point_info_var = ins;
1649
1650                         /* Allocate a separate variable for this to save 1 load per seq point */
1651                         ins = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1652                         ins->flags |= MONO_INST_VOLATILE;
1653                         cfg->arch.ss_trigger_page_var = ins;
1654                 }
1655         }
1656 }
1657
1658 static void
1659 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1660 {
1661         MonoMethodSignature *tmp_sig;
1662         MonoInst *sig_arg;
1663
1664         if (call->tail_call)
1665                 NOT_IMPLEMENTED;
1666
1667         /* FIXME: Add support for signature tokens to AOT */
1668         cfg->disable_aot = TRUE;
1669
1670         g_assert (cinfo->sig_cookie.storage == RegTypeBase);
1671                         
1672         /*
1673          * mono_ArgIterator_Setup assumes the signature cookie is 
1674          * passed first and all the arguments which were before it are
1675          * passed on the stack after the signature. So compensate by 
1676          * passing a different signature.
1677          */
1678         tmp_sig = mono_metadata_signature_dup (call->signature);
1679         tmp_sig->param_count -= call->signature->sentinelpos;
1680         tmp_sig->sentinelpos = 0;
1681         memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1682
1683         MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1684         sig_arg->dreg = mono_alloc_ireg (cfg);
1685         sig_arg->inst_p0 = tmp_sig;
1686         MONO_ADD_INS (cfg->cbb, sig_arg);
1687
1688         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, cinfo->sig_cookie.offset, sig_arg->dreg);
1689 }
1690
1691 #ifdef ENABLE_LLVM
1692 LLVMCallInfo*
1693 mono_arch_get_llvm_call_info (MonoCompile *cfg, MonoMethodSignature *sig)
1694 {
1695         int i, n;
1696         CallInfo *cinfo;
1697         ArgInfo *ainfo;
1698         LLVMCallInfo *linfo;
1699
1700         n = sig->param_count + sig->hasthis;
1701
1702         cinfo = get_call_info (cfg->generic_sharing_context, cfg->mempool, sig);
1703
1704         linfo = mono_mempool_alloc0 (cfg->mempool, sizeof (LLVMCallInfo) + (sizeof (LLVMArgInfo) * n));
1705
1706         /*
1707          * LLVM always uses the native ABI while we use our own ABI, the
1708          * only difference is the handling of vtypes:
1709          * - we only pass/receive them in registers in some cases, and only 
1710          *   in 1 or 2 integer registers.
1711          */
1712         if (cinfo->vtype_retaddr) {
1713                 /* Vtype returned using a hidden argument */
1714                 linfo->ret.storage = LLVMArgVtypeRetAddr;
1715                 linfo->vret_arg_index = cinfo->vret_arg_index;
1716         } else if (cinfo->ret.storage != RegTypeGeneral && cinfo->ret.storage != RegTypeNone && cinfo->ret.storage != RegTypeFP && cinfo->ret.storage != RegTypeIRegPair) {
1717                 cfg->exception_message = g_strdup ("unknown ret conv");
1718                 cfg->disable_llvm = TRUE;
1719                 return linfo;
1720         }
1721
1722         for (i = 0; i < n; ++i) {
1723                 ainfo = cinfo->args + i;
1724
1725                 linfo->args [i].storage = LLVMArgNone;
1726
1727                 switch (ainfo->storage) {
1728                 case RegTypeGeneral:
1729                 case RegTypeIRegPair:
1730                 case RegTypeBase:
1731                         linfo->args [i].storage = LLVMArgInIReg;
1732                         break;
1733                 case RegTypeStructByVal:
1734                         // FIXME: Passing entirely on the stack or split reg/stack
1735                         if (ainfo->vtsize == 0 && ainfo->size <= 2) {
1736                                 linfo->args [i].storage = LLVMArgVtypeInReg;
1737                                 linfo->args [i].pair_storage [0] = LLVMArgInIReg;
1738                                 if (ainfo->size == 2)
1739                                         linfo->args [i].pair_storage [1] = LLVMArgInIReg;
1740                                 else
1741                                         linfo->args [i].pair_storage [1] = LLVMArgNone;
1742                         } else {
1743                                 cfg->exception_message = g_strdup_printf ("vtype-by-val on stack");
1744                                 cfg->disable_llvm = TRUE;
1745                         }
1746                         break;
1747                 default:
1748                         cfg->exception_message = g_strdup_printf ("ainfo->storage (%d)", ainfo->storage);
1749                         cfg->disable_llvm = TRUE;
1750                         break;
1751                 }
1752         }
1753
1754         return linfo;
1755 }
1756 #endif
1757
1758 void
1759 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1760 {
1761         MonoInst *in, *ins;
1762         MonoMethodSignature *sig;
1763         int i, n;
1764         CallInfo *cinfo;
1765
1766         sig = call->signature;
1767         n = sig->param_count + sig->hasthis;
1768         
1769         cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
1770
1771         for (i = 0; i < n; ++i) {
1772                 ArgInfo *ainfo = cinfo->args + i;
1773                 MonoType *t;
1774
1775                 if (i >= sig->hasthis)
1776                         t = sig->params [i - sig->hasthis];
1777                 else
1778                         t = &mono_defaults.int_class->byval_arg;
1779                 t = mini_type_get_underlying_type (NULL, t);
1780
1781                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1782                         /* Emit the signature cookie just before the implicit arguments */
1783                         emit_sig_cookie (cfg, call, cinfo);
1784                 }
1785
1786                 in = call->args [i];
1787
1788                 switch (ainfo->storage) {
1789                 case RegTypeGeneral:
1790                 case RegTypeIRegPair:
1791                         if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1792                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
1793                                 ins->dreg = mono_alloc_ireg (cfg);
1794                                 ins->sreg1 = in->dreg + 1;
1795                                 MONO_ADD_INS (cfg->cbb, ins);
1796                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1797
1798                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
1799                                 ins->dreg = mono_alloc_ireg (cfg);
1800                                 ins->sreg1 = in->dreg + 2;
1801                                 MONO_ADD_INS (cfg->cbb, ins);
1802                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1803                         } else if (!t->byref && ((t->type == MONO_TYPE_R8) || (t->type == MONO_TYPE_R4))) {
1804                                 if (ainfo->size == 4) {
1805                                         if (IS_SOFT_FLOAT) {
1806                                                 /* mono_emit_call_args () have already done the r8->r4 conversion */
1807                                                 /* The converted value is in an int vreg */
1808                                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
1809                                                 ins->dreg = mono_alloc_ireg (cfg);
1810                                                 ins->sreg1 = in->dreg;
1811                                                 MONO_ADD_INS (cfg->cbb, ins);
1812                                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1813                                         } else {
1814                                                 int creg;
1815
1816                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1817                                                 creg = mono_alloc_ireg (cfg);
1818                                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1819                                                 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1820                                         }
1821                                 } else {
1822                                         if (IS_SOFT_FLOAT) {
1823                                                 MONO_INST_NEW (cfg, ins, OP_FGETLOW32);
1824                                                 ins->dreg = mono_alloc_ireg (cfg);
1825                                                 ins->sreg1 = in->dreg;
1826                                                 MONO_ADD_INS (cfg->cbb, ins);
1827                                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1828
1829                                                 MONO_INST_NEW (cfg, ins, OP_FGETHIGH32);
1830                                                 ins->dreg = mono_alloc_ireg (cfg);
1831                                                 ins->sreg1 = in->dreg;
1832                                                 MONO_ADD_INS (cfg->cbb, ins);
1833                                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg + 1, FALSE);
1834                                         } else {
1835                                                 int creg;
1836
1837                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1838                                                 creg = mono_alloc_ireg (cfg);
1839                                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1840                                                 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg, FALSE);
1841                                                 creg = mono_alloc_ireg (cfg);
1842                                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8 + 4));
1843                                                 mono_call_inst_add_outarg_reg (cfg, call, creg, ainfo->reg + 1, FALSE);
1844                                         }
1845                                 }
1846                                 cfg->flags |= MONO_CFG_HAS_FPOUT;
1847                         } else {
1848                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
1849                                 ins->dreg = mono_alloc_ireg (cfg);
1850                                 ins->sreg1 = in->dreg;
1851                                 MONO_ADD_INS (cfg->cbb, ins);
1852
1853                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ainfo->reg, FALSE);
1854                         }
1855                         break;
1856                 case RegTypeStructByAddr:
1857                         NOT_IMPLEMENTED;
1858 #if 0
1859                         /* FIXME: where si the data allocated? */
1860                         arg->backend.reg3 = ainfo->reg;
1861                         call->used_iregs |= 1 << ainfo->reg;
1862                         g_assert_not_reached ();
1863 #endif
1864                         break;
1865                 case RegTypeStructByVal:
1866                         MONO_INST_NEW (cfg, ins, OP_OUTARG_VT);
1867                         ins->opcode = OP_OUTARG_VT;
1868                         ins->sreg1 = in->dreg;
1869                         ins->klass = in->klass;
1870                         ins->inst_p0 = call;
1871                         ins->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1872                         memcpy (ins->inst_p1, ainfo, sizeof (ArgInfo));
1873                         mono_call_inst_add_outarg_vt (cfg, call, ins);
1874                         MONO_ADD_INS (cfg->cbb, ins);
1875                         break;
1876                 case RegTypeBase:
1877                         if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1878                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1879                         } else if (!t->byref && ((t->type == MONO_TYPE_R4) || (t->type == MONO_TYPE_R8))) {
1880                                 if (t->type == MONO_TYPE_R8) {
1881                                         MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1882                                 } else {
1883                                         if (IS_SOFT_FLOAT)
1884                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1885                                         else
1886                                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1887                                 }
1888                         } else {
1889                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, in->dreg);
1890                         }
1891                         break;
1892                 case RegTypeBaseGen:
1893                         if (!t->byref && ((t->type == MONO_TYPE_I8) || (t->type == MONO_TYPE_U8))) {
1894                                 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);
1895                                 MONO_INST_NEW (cfg, ins, OP_MOVE);
1896                                 ins->dreg = mono_alloc_ireg (cfg);
1897                                 ins->sreg1 = G_BYTE_ORDER == G_BIG_ENDIAN ? in->dreg + 2 : in->dreg + 1;
1898                                 MONO_ADD_INS (cfg->cbb, ins);
1899                                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, ARMREG_R3, FALSE);
1900                         } else if (!t->byref && (t->type == MONO_TYPE_R8)) {
1901                                 int creg;
1902
1903                                 /* This should work for soft-float as well */
1904
1905                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, ARMREG_SP, (cfg->param_area - 8), in->dreg);
1906                                 creg = mono_alloc_ireg (cfg);
1907                                 mono_call_inst_add_outarg_reg (cfg, call, creg, ARMREG_R3, FALSE);
1908                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 8));
1909                                 creg = mono_alloc_ireg (cfg);
1910                                 MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOAD_MEMBASE, creg, ARMREG_SP, (cfg->param_area - 4));
1911                                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORE_MEMBASE_REG, ARMREG_SP, ainfo->offset, creg);
1912                                 cfg->flags |= MONO_CFG_HAS_FPOUT;
1913                         } else {
1914                                 g_assert_not_reached ();
1915                         }
1916                         break;
1917                 case RegTypeFP: {
1918                         /* FIXME: */
1919                         NOT_IMPLEMENTED;
1920 #if 0
1921                         arg->backend.reg3 = ainfo->reg;
1922                         /* FP args are passed in int regs */
1923                         call->used_iregs |= 1 << ainfo->reg;
1924                         if (ainfo->size == 8) {
1925                                 arg->opcode = OP_OUTARG_R8;
1926                                 call->used_iregs |= 1 << (ainfo->reg + 1);
1927                         } else {
1928                                 arg->opcode = OP_OUTARG_R4;
1929                         }
1930 #endif
1931                         cfg->flags |= MONO_CFG_HAS_FPOUT;
1932                         break;
1933                 }
1934                 default:
1935                         g_assert_not_reached ();
1936                 }
1937         }
1938
1939         /* Handle the case where there are no implicit arguments */
1940         if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos))
1941                 emit_sig_cookie (cfg, call, cinfo);
1942
1943         if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1944                 MonoInst *vtarg;
1945
1946                 if (cinfo->ret.storage == RegTypeStructByVal) {
1947                         /* The JIT will transform this into a normal call */
1948                         call->vret_in_reg = TRUE;
1949                 } else {
1950                         MONO_INST_NEW (cfg, vtarg, OP_MOVE);
1951                         vtarg->sreg1 = call->vret_var->dreg;
1952                         vtarg->dreg = mono_alloc_preg (cfg);
1953                         MONO_ADD_INS (cfg->cbb, vtarg);
1954
1955                         mono_call_inst_add_outarg_reg (cfg, call, vtarg->dreg, cinfo->ret.reg, FALSE);
1956                 }
1957         }
1958
1959         call->stack_usage = cinfo->stack_usage;
1960
1961         g_free (cinfo);
1962 }
1963
1964 void
1965 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1966 {
1967         MonoCallInst *call = (MonoCallInst*)ins->inst_p0;
1968         ArgInfo *ainfo = ins->inst_p1;
1969         int ovf_size = ainfo->vtsize;
1970         int doffset = ainfo->offset;
1971         int struct_size = ainfo->struct_size;
1972         int i, soffset, dreg, tmpreg;
1973
1974         soffset = 0;
1975         for (i = 0; i < ainfo->size; ++i) {
1976                 dreg = mono_alloc_ireg (cfg);
1977                 switch (struct_size) {
1978                 case 1:
1979                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
1980                         break;
1981                 case 2:
1982                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU2_MEMBASE, dreg, src->dreg, soffset);
1983                         break;
1984                 case 3:
1985                         tmpreg = mono_alloc_ireg (cfg);
1986                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, dreg, src->dreg, soffset);
1987                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 1);
1988                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 8);
1989                         MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
1990                         MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADU1_MEMBASE, tmpreg, src->dreg, soffset + 2);
1991                         MONO_EMIT_NEW_BIALU_IMM (cfg, OP_SHL_IMM, tmpreg, tmpreg, 16);
1992                         MONO_EMIT_NEW_BIALU (cfg, OP_IOR, dreg, dreg, tmpreg);
1993                         break;
1994                 default:
1995                         MONO_EMIT_NEW_LOAD_MEMBASE (cfg, dreg, src->dreg, soffset);
1996                         break;
1997                 }
1998                 mono_call_inst_add_outarg_reg (cfg, call, dreg, ainfo->reg + i, FALSE);
1999                 soffset += sizeof (gpointer);
2000                 struct_size -= sizeof (gpointer);
2001         }
2002         //g_print ("vt size: %d at R%d + %d\n", doffset, vt->inst_basereg, vt->inst_offset);
2003         if (ovf_size != 0)
2004                 mini_emit_memcpy (cfg, ARMREG_SP, doffset, src->dreg, soffset, MIN (ovf_size * sizeof (gpointer), struct_size), struct_size < 4 ? 1 : 4);
2005 }
2006
2007 void
2008 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
2009 {
2010         MonoType *ret = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret);
2011
2012         if (!ret->byref) {
2013                 if (ret->type == MONO_TYPE_I8 || ret->type == MONO_TYPE_U8) {
2014                         MonoInst *ins;
2015
2016                         if (COMPILE_LLVM (cfg)) {
2017                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2018                         } else {
2019                                 MONO_INST_NEW (cfg, ins, OP_SETLRET);
2020                                 ins->sreg1 = val->dreg + 1;
2021                                 ins->sreg2 = val->dreg + 2;
2022                                 MONO_ADD_INS (cfg->cbb, ins);
2023                         }
2024                         return;
2025                 }
2026                 switch (arm_fpu) {
2027                 case MONO_ARM_FPU_NONE:
2028                         if (ret->type == MONO_TYPE_R8) {
2029                                 MonoInst *ins;
2030
2031                                 MONO_INST_NEW (cfg, ins, OP_SETFRET);
2032                                 ins->dreg = cfg->ret->dreg;
2033                                 ins->sreg1 = val->dreg;
2034                                 MONO_ADD_INS (cfg->cbb, ins);
2035                                 return;
2036                         }
2037                         if (ret->type == MONO_TYPE_R4) {
2038                                 /* Already converted to an int in method_to_ir () */
2039                                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2040                                 return;
2041                         }
2042                         break;
2043                 case MONO_ARM_FPU_VFP:
2044                         if (ret->type == MONO_TYPE_R8 || ret->type == MONO_TYPE_R4) {
2045                                 MonoInst *ins;
2046
2047                                 MONO_INST_NEW (cfg, ins, OP_SETFRET);
2048                                 ins->dreg = cfg->ret->dreg;
2049                                 ins->sreg1 = val->dreg;
2050                                 MONO_ADD_INS (cfg->cbb, ins);
2051                                 return;
2052                         }
2053                         break;
2054                 case MONO_ARM_FPU_FPA:
2055                         if (ret->type == MONO_TYPE_R4 || ret->type == MONO_TYPE_R8) {
2056                                 MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
2057                                 return;
2058                         }
2059                         break;
2060                 default:
2061                         g_assert_not_reached ();
2062                 }
2063         }
2064
2065         MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
2066 }
2067
2068 #endif /* #ifndef DISABLE_JIT */
2069
2070 gboolean 
2071 mono_arch_is_inst_imm (gint64 imm)
2072 {
2073         return TRUE;
2074 }
2075
2076 #define DYN_CALL_STACK_ARGS 6
2077
2078 typedef struct {
2079         MonoMethodSignature *sig;
2080         CallInfo *cinfo;
2081 } ArchDynCallInfo;
2082
2083 typedef struct {
2084         mgreg_t regs [PARAM_REGS + DYN_CALL_STACK_ARGS];
2085         mgreg_t res, res2;
2086         guint8 *ret;
2087 } DynCallArgs;
2088
2089 static gboolean
2090 dyn_call_supported (CallInfo *cinfo, MonoMethodSignature *sig)
2091 {
2092         int i;
2093
2094         if (sig->hasthis + sig->param_count > PARAM_REGS + DYN_CALL_STACK_ARGS)
2095                 return FALSE;
2096
2097         switch (cinfo->ret.storage) {
2098         case RegTypeNone:
2099         case RegTypeGeneral:
2100         case RegTypeIRegPair:
2101         case RegTypeStructByAddr:
2102                 break;
2103         case RegTypeFP:
2104                 if (IS_FPA)
2105                         return FALSE;
2106                 else if (IS_VFP)
2107                         break;
2108                 else
2109                         return FALSE;
2110         default:
2111                 return FALSE;
2112         }
2113
2114         for (i = 0; i < cinfo->nargs; ++i) {
2115                 switch (cinfo->args [i].storage) {
2116                 case RegTypeGeneral:
2117                         break;
2118                 case RegTypeIRegPair:
2119                         break;
2120                 case RegTypeBase:
2121                         if (cinfo->args [i].offset >= (DYN_CALL_STACK_ARGS * sizeof (gpointer)))
2122                                 return FALSE;
2123                         break;
2124                 case RegTypeStructByVal:
2125                         if (cinfo->args [i].reg + cinfo->args [i].vtsize >= PARAM_REGS + DYN_CALL_STACK_ARGS)
2126                                 return FALSE;
2127                         break;
2128                 default:
2129                         return FALSE;
2130                 }
2131         }
2132
2133         // FIXME: Can't use cinfo only as it doesn't contain info about I8/float */
2134         for (i = 0; i < sig->param_count; ++i) {
2135                 MonoType *t = sig->params [i];
2136
2137                 if (t->byref)
2138                         continue;
2139
2140                 switch (t->type) {
2141                 case MONO_TYPE_R4:
2142                 case MONO_TYPE_R8:
2143                         if (IS_SOFT_FLOAT)
2144                                 return FALSE;
2145                         else
2146                                 break;
2147                         /*
2148                 case MONO_TYPE_I8:
2149                 case MONO_TYPE_U8:
2150                         return FALSE;
2151                         */
2152                 default:
2153                         break;
2154                 }
2155         }
2156
2157         return TRUE;
2158 }
2159
2160 MonoDynCallInfo*
2161 mono_arch_dyn_call_prepare (MonoMethodSignature *sig)
2162 {
2163         ArchDynCallInfo *info;
2164         CallInfo *cinfo;
2165
2166         cinfo = get_call_info (NULL, NULL, sig);
2167
2168         if (!dyn_call_supported (cinfo, sig)) {
2169                 g_free (cinfo);
2170                 return NULL;
2171         }
2172
2173         info = g_new0 (ArchDynCallInfo, 1);
2174         // FIXME: Preprocess the info to speed up start_dyn_call ()
2175         info->sig = sig;
2176         info->cinfo = cinfo;
2177         
2178         return (MonoDynCallInfo*)info;
2179 }
2180
2181 void
2182 mono_arch_dyn_call_free (MonoDynCallInfo *info)
2183 {
2184         ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2185
2186         g_free (ainfo->cinfo);
2187         g_free (ainfo);
2188 }
2189
2190 void
2191 mono_arch_start_dyn_call (MonoDynCallInfo *info, gpointer **args, guint8 *ret, guint8 *buf, int buf_len)
2192 {
2193         ArchDynCallInfo *dinfo = (ArchDynCallInfo*)info;
2194         DynCallArgs *p = (DynCallArgs*)buf;
2195         int arg_index, greg, i, j, pindex;
2196         MonoMethodSignature *sig = dinfo->sig;
2197
2198         g_assert (buf_len >= sizeof (DynCallArgs));
2199
2200         p->res = 0;
2201         p->ret = ret;
2202
2203         arg_index = 0;
2204         greg = 0;
2205         pindex = 0;
2206
2207         if (sig->hasthis || dinfo->cinfo->vret_arg_index == 1) {
2208                 p->regs [greg ++] = (mgreg_t)*(args [arg_index ++]);
2209                 if (!sig->hasthis)
2210                         pindex = 1;
2211         }
2212
2213         if (dinfo->cinfo->vtype_retaddr)
2214                 p->regs [greg ++] = (mgreg_t)ret;
2215
2216         for (i = pindex; i < sig->param_count; i++) {
2217                 MonoType *t = mono_type_get_underlying_type (sig->params [i]);
2218                 gpointer *arg = args [arg_index ++];
2219                 ArgInfo *ainfo = &dinfo->cinfo->args [i + sig->hasthis];
2220                 int slot = -1;
2221
2222                 if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair || ainfo->storage == RegTypeStructByVal)
2223                         slot = ainfo->reg;
2224                 else if (ainfo->storage == RegTypeBase)
2225                         slot = PARAM_REGS + (ainfo->offset / 4);
2226                 else
2227                         g_assert_not_reached ();
2228
2229                 if (t->byref) {
2230                         p->regs [slot] = (mgreg_t)*arg;
2231                         continue;
2232                 }
2233
2234                 switch (t->type) {
2235                 case MONO_TYPE_STRING:
2236                 case MONO_TYPE_CLASS:  
2237                 case MONO_TYPE_ARRAY:
2238                 case MONO_TYPE_SZARRAY:
2239                 case MONO_TYPE_OBJECT:
2240                 case MONO_TYPE_PTR:
2241                 case MONO_TYPE_I:
2242                 case MONO_TYPE_U:
2243                         p->regs [slot] = (mgreg_t)*arg;
2244                         break;
2245                 case MONO_TYPE_BOOLEAN:
2246                 case MONO_TYPE_U1:
2247                         p->regs [slot] = *(guint8*)arg;
2248                         break;
2249                 case MONO_TYPE_I1:
2250                         p->regs [slot] = *(gint8*)arg;
2251                         break;
2252                 case MONO_TYPE_I2:
2253                         p->regs [slot] = *(gint16*)arg;
2254                         break;
2255                 case MONO_TYPE_U2:
2256                 case MONO_TYPE_CHAR:
2257                         p->regs [slot] = *(guint16*)arg;
2258                         break;
2259                 case MONO_TYPE_I4:
2260                         p->regs [slot] = *(gint32*)arg;
2261                         break;
2262                 case MONO_TYPE_U4:
2263                         p->regs [slot] = *(guint32*)arg;
2264                         break;
2265                 case MONO_TYPE_I8:
2266                 case MONO_TYPE_U8:
2267                         p->regs [slot ++] = (mgreg_t)arg [0];
2268                         p->regs [slot] = (mgreg_t)arg [1];
2269                         break;
2270                 case MONO_TYPE_R4:
2271                         p->regs [slot] = *(mgreg_t*)arg;
2272                         break;
2273                 case MONO_TYPE_R8:
2274                         p->regs [slot ++] = (mgreg_t)arg [0];
2275                         p->regs [slot] = (mgreg_t)arg [1];
2276                         break;
2277                 case MONO_TYPE_GENERICINST:
2278                         if (MONO_TYPE_IS_REFERENCE (t)) {
2279                                 p->regs [slot] = (mgreg_t)*arg;
2280                                 break;
2281                         } else {
2282                                 /* Fall though */
2283                         }
2284                 case MONO_TYPE_VALUETYPE:
2285                         g_assert (ainfo->storage == RegTypeStructByVal);
2286
2287                         if (ainfo->size == 0)
2288                                 slot = PARAM_REGS + (ainfo->offset / 4);
2289                         else
2290                                 slot = ainfo->reg;
2291
2292                         for (j = 0; j < ainfo->size + ainfo->vtsize; ++j)
2293                                 p->regs [slot ++] = ((mgreg_t*)arg) [j];
2294                         break;
2295                 default:
2296                         g_assert_not_reached ();
2297                 }
2298         }
2299 }
2300
2301 void
2302 mono_arch_finish_dyn_call (MonoDynCallInfo *info, guint8 *buf)
2303 {
2304         ArchDynCallInfo *ainfo = (ArchDynCallInfo*)info;
2305         MonoMethodSignature *sig = ((ArchDynCallInfo*)info)->sig;
2306         guint8 *ret = ((DynCallArgs*)buf)->ret;
2307         mgreg_t res = ((DynCallArgs*)buf)->res;
2308         mgreg_t res2 = ((DynCallArgs*)buf)->res2;
2309
2310         switch (mono_type_get_underlying_type (sig->ret)->type) {
2311         case MONO_TYPE_VOID:
2312                 *(gpointer*)ret = NULL;
2313                 break;
2314         case MONO_TYPE_STRING:
2315         case MONO_TYPE_CLASS:  
2316         case MONO_TYPE_ARRAY:
2317         case MONO_TYPE_SZARRAY:
2318         case MONO_TYPE_OBJECT:
2319         case MONO_TYPE_I:
2320         case MONO_TYPE_U:
2321         case MONO_TYPE_PTR:
2322                 *(gpointer*)ret = (gpointer)res;
2323                 break;
2324         case MONO_TYPE_I1:
2325                 *(gint8*)ret = res;
2326                 break;
2327         case MONO_TYPE_U1:
2328         case MONO_TYPE_BOOLEAN:
2329                 *(guint8*)ret = res;
2330                 break;
2331         case MONO_TYPE_I2:
2332                 *(gint16*)ret = res;
2333                 break;
2334         case MONO_TYPE_U2:
2335         case MONO_TYPE_CHAR:
2336                 *(guint16*)ret = res;
2337                 break;
2338         case MONO_TYPE_I4:
2339                 *(gint32*)ret = res;
2340                 break;
2341         case MONO_TYPE_U4:
2342                 *(guint32*)ret = res;
2343                 break;
2344         case MONO_TYPE_I8:
2345         case MONO_TYPE_U8:
2346                 /* This handles endianness as well */
2347                 ((gint32*)ret) [0] = res;
2348                 ((gint32*)ret) [1] = res2;
2349                 break;
2350         case MONO_TYPE_GENERICINST:
2351                 if (MONO_TYPE_IS_REFERENCE (sig->ret)) {
2352                         *(gpointer*)ret = (gpointer)res;
2353                         break;
2354                 } else {
2355                         /* Fall though */
2356                 }
2357         case MONO_TYPE_VALUETYPE:
2358                 g_assert (ainfo->cinfo->vtype_retaddr);
2359                 /* Nothing to do */
2360                 break;
2361         case MONO_TYPE_R4:
2362                 g_assert (IS_VFP);
2363                 *(float*)ret = *(float*)&res;
2364                 break;
2365         case MONO_TYPE_R8: {
2366                 mgreg_t regs [2];
2367
2368                 g_assert (IS_VFP);
2369                 regs [0] = res;
2370                 regs [1] = res2;
2371
2372                 *(double*)ret = *(double*)&regs;
2373                 break;
2374         }
2375         default:
2376                 g_assert_not_reached ();
2377         }
2378 }
2379
2380 #ifndef DISABLE_JIT
2381
2382 /*
2383  * Allow tracing to work with this interface (with an optional argument)
2384  */
2385
2386 void*
2387 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
2388 {
2389         guchar *code = p;
2390
2391         code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2392         ARM_MOV_REG_IMM8 (code, ARMREG_R1, 0); /* NULL ebp for now */
2393         code = mono_arm_emit_load_imm (code, ARMREG_R2, (guint32)func);
2394         code = emit_call_reg (code, ARMREG_R2);
2395         return code;
2396 }
2397
2398 enum {
2399         SAVE_NONE,
2400         SAVE_STRUCT,
2401         SAVE_ONE,
2402         SAVE_TWO,
2403         SAVE_FP
2404 };
2405
2406 void*
2407 mono_arch_instrument_epilog_full (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments, gboolean preserve_argument_registers)
2408 {
2409         guchar *code = p;
2410         int save_mode = SAVE_NONE;
2411         int offset;
2412         MonoMethod *method = cfg->method;
2413         int rtype = mini_type_get_underlying_type (cfg->generic_sharing_context, mono_method_signature (method)->ret)->type;
2414         int save_offset = cfg->param_area;
2415         save_offset += 7;
2416         save_offset &= ~7;
2417         
2418         offset = code - cfg->native_code;
2419         /* we need about 16 instructions */
2420         if (offset > (cfg->code_size - 16 * 4)) {
2421                 cfg->code_size *= 2;
2422                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2423                 code = cfg->native_code + offset;
2424         }
2425         switch (rtype) {
2426         case MONO_TYPE_VOID:
2427                 /* special case string .ctor icall */
2428                 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
2429                         save_mode = SAVE_ONE;
2430                 else
2431                         save_mode = SAVE_NONE;
2432                 break;
2433         case MONO_TYPE_I8:
2434         case MONO_TYPE_U8:
2435                 save_mode = SAVE_TWO;
2436                 break;
2437         case MONO_TYPE_R4:
2438         case MONO_TYPE_R8:
2439                 save_mode = SAVE_FP;
2440                 break;
2441         case MONO_TYPE_VALUETYPE:
2442                 save_mode = SAVE_STRUCT;
2443                 break;
2444         default:
2445                 save_mode = SAVE_ONE;
2446                 break;
2447         }
2448
2449         switch (save_mode) {
2450         case SAVE_TWO:
2451                 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2452                 ARM_STR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2453                 if (enable_arguments) {
2454                         ARM_MOV_REG_REG (code, ARMREG_R2, ARMREG_R1);
2455                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2456                 }
2457                 break;
2458         case SAVE_ONE:
2459                 ARM_STR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2460                 if (enable_arguments) {
2461                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2462                 }
2463                 break;
2464         case SAVE_FP:
2465                 /* FIXME: what reg?  */
2466                 if (enable_arguments) {
2467                         /* FIXME: what reg?  */
2468                 }
2469                 break;
2470         case SAVE_STRUCT:
2471                 if (enable_arguments) {
2472                         /* FIXME: get the actual address  */
2473                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_R0);
2474                 }
2475                 break;
2476         case SAVE_NONE:
2477         default:
2478                 break;
2479         }
2480
2481         code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->method);
2482         code = mono_arm_emit_load_imm (code, ARMREG_IP, (guint32)func);
2483         code = emit_call_reg (code, ARMREG_IP);
2484
2485         switch (save_mode) {
2486         case SAVE_TWO:
2487                 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2488                 ARM_LDR_IMM (code, ARMREG_R1, cfg->frame_reg, save_offset + 4);
2489                 break;
2490         case SAVE_ONE:
2491                 ARM_LDR_IMM (code, ARMREG_R0, cfg->frame_reg, save_offset);
2492                 break;
2493         case SAVE_FP:
2494                 /* FIXME */
2495                 break;
2496         case SAVE_NONE:
2497         default:
2498                 break;
2499         }
2500
2501         return code;
2502 }
2503
2504 /*
2505  * The immediate field for cond branches is big enough for all reasonable methods
2506  */
2507 #define EMIT_COND_BRANCH_FLAGS(ins,condcode) \
2508 if (0 && ins->inst_true_bb->native_offset) { \
2509         ARM_B_COND (code, (condcode), (code - cfg->native_code + ins->inst_true_bb->native_offset) & 0xffffff); \
2510 } else { \
2511         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
2512         ARM_B_COND (code, (condcode), 0);       \
2513 }
2514
2515 #define EMIT_COND_BRANCH(ins,cond) EMIT_COND_BRANCH_FLAGS(ins, branch_cc_table [(cond)])
2516
2517 /* emit an exception if condition is fail
2518  *
2519  * We assign the extra code used to throw the implicit exceptions
2520  * to cfg->bb_exit as far as the big branch handling is concerned
2521  */
2522 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(condcode,exc_name)            \
2523         do {                                                        \
2524                 mono_add_patch_info (cfg, code - cfg->native_code,   \
2525                                     MONO_PATCH_INFO_EXC, exc_name);  \
2526                 ARM_BL_COND (code, (condcode), 0);      \
2527         } while (0); 
2528
2529 #define EMIT_COND_SYSTEM_EXCEPTION(cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(branch_cc_table [(cond)], (exc_name))
2530
2531 void
2532 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
2533 {
2534 }
2535
2536 void
2537 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
2538 {
2539         MonoInst *ins, *n, *last_ins = NULL;
2540
2541         MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
2542                 switch (ins->opcode) {
2543                 case OP_MUL_IMM: 
2544                 case OP_IMUL_IMM: 
2545                         /* Already done by an arch-independent pass */
2546                         break;
2547                 case OP_LOAD_MEMBASE:
2548                 case OP_LOADI4_MEMBASE:
2549                         /* 
2550                          * OP_STORE_MEMBASE_REG reg, offset(basereg) 
2551                          * OP_LOAD_MEMBASE offset(basereg), reg
2552                          */
2553                         if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG 
2554                                          || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
2555                             ins->inst_basereg == last_ins->inst_destbasereg &&
2556                             ins->inst_offset == last_ins->inst_offset) {
2557                                 if (ins->dreg == last_ins->sreg1) {
2558                                         MONO_DELETE_INS (bb, ins);
2559                                         continue;
2560                                 } else {
2561                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2562                                         ins->opcode = OP_MOVE;
2563                                         ins->sreg1 = last_ins->sreg1;
2564                                 }
2565
2566                         /* 
2567                          * Note: reg1 must be different from the basereg in the second load
2568                          * OP_LOAD_MEMBASE offset(basereg), reg1
2569                          * OP_LOAD_MEMBASE offset(basereg), reg2
2570                          * -->
2571                          * OP_LOAD_MEMBASE offset(basereg), reg1
2572                          * OP_MOVE reg1, reg2
2573                          */
2574                         } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2575                                            || last_ins->opcode == OP_LOAD_MEMBASE) &&
2576                               ins->inst_basereg != last_ins->dreg &&
2577                               ins->inst_basereg == last_ins->inst_basereg &&
2578                               ins->inst_offset == last_ins->inst_offset) {
2579
2580                                 if (ins->dreg == last_ins->dreg) {
2581                                         MONO_DELETE_INS (bb, ins);
2582                                         continue;
2583                                 } else {
2584                                         ins->opcode = OP_MOVE;
2585                                         ins->sreg1 = last_ins->dreg;
2586                                 }
2587
2588                                 //g_assert_not_reached ();
2589
2590 #if 0
2591                         /* 
2592                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
2593                          * OP_LOAD_MEMBASE offset(basereg), reg
2594                          * -->
2595                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
2596                          * OP_ICONST reg, imm
2597                          */
2598                         } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2599                                                 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2600                                    ins->inst_basereg == last_ins->inst_destbasereg &&
2601                                    ins->inst_offset == last_ins->inst_offset) {
2602                                 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
2603                                 ins->opcode = OP_ICONST;
2604                                 ins->inst_c0 = last_ins->inst_imm;
2605                                 g_assert_not_reached (); // check this rule
2606 #endif
2607                         }
2608                         break;
2609                 case OP_LOADU1_MEMBASE:
2610                 case OP_LOADI1_MEMBASE:
2611                         if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2612                                         ins->inst_basereg == last_ins->inst_destbasereg &&
2613                                         ins->inst_offset == last_ins->inst_offset) {
2614                                 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_ICONV_TO_I1 : OP_ICONV_TO_U1;
2615                                 ins->sreg1 = last_ins->sreg1;                           
2616                         }
2617                         break;
2618                 case OP_LOADU2_MEMBASE:
2619                 case OP_LOADI2_MEMBASE:
2620                         if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2621                                         ins->inst_basereg == last_ins->inst_destbasereg &&
2622                                         ins->inst_offset == last_ins->inst_offset) {
2623                                 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_ICONV_TO_I2 : OP_ICONV_TO_U2;
2624                                 ins->sreg1 = last_ins->sreg1;                           
2625                         }
2626                         break;
2627                 case OP_MOVE:
2628                         ins->opcode = OP_MOVE;
2629                         /* 
2630                          * OP_MOVE reg, reg 
2631                          */
2632                         if (ins->dreg == ins->sreg1) {
2633                                 MONO_DELETE_INS (bb, ins);
2634                                 continue;
2635                         }
2636                         /* 
2637                          * OP_MOVE sreg, dreg 
2638                          * OP_MOVE dreg, sreg
2639                          */
2640                         if (last_ins && last_ins->opcode == OP_MOVE &&
2641                             ins->sreg1 == last_ins->dreg &&
2642                             ins->dreg == last_ins->sreg1) {
2643                                 MONO_DELETE_INS (bb, ins);
2644                                 continue;
2645                         }
2646                         break;
2647                 }
2648                 last_ins = ins;
2649                 ins = ins->next;
2650         }
2651         bb->last_ins = last_ins;
2652 }
2653
2654 /* 
2655  * the branch_cc_table should maintain the order of these
2656  * opcodes.
2657 case CEE_BEQ:
2658 case CEE_BGE:
2659 case CEE_BGT:
2660 case CEE_BLE:
2661 case CEE_BLT:
2662 case CEE_BNE_UN:
2663 case CEE_BGE_UN:
2664 case CEE_BGT_UN:
2665 case CEE_BLE_UN:
2666 case CEE_BLT_UN:
2667  */
2668 static const guchar 
2669 branch_cc_table [] = {
2670         ARMCOND_EQ, 
2671         ARMCOND_GE, 
2672         ARMCOND_GT, 
2673         ARMCOND_LE,
2674         ARMCOND_LT, 
2675         
2676         ARMCOND_NE, 
2677         ARMCOND_HS, 
2678         ARMCOND_HI, 
2679         ARMCOND_LS,
2680         ARMCOND_LO
2681 };
2682
2683 #define NEW_INS(cfg,dest,op) do {       \
2684                 MONO_INST_NEW ((cfg), (dest), (op)); \
2685         mono_bblock_insert_before_ins (bb, ins, (dest)); \
2686         } while (0)
2687
2688 static int
2689 map_to_reg_reg_op (int op)
2690 {
2691         switch (op) {
2692         case OP_ADD_IMM:
2693                 return OP_IADD;
2694         case OP_SUB_IMM:
2695                 return OP_ISUB;
2696         case OP_AND_IMM:
2697                 return OP_IAND;
2698         case OP_COMPARE_IMM:
2699                 return OP_COMPARE;
2700         case OP_ICOMPARE_IMM:
2701                 return OP_ICOMPARE;
2702         case OP_ADDCC_IMM:
2703                 return OP_ADDCC;
2704         case OP_ADC_IMM:
2705                 return OP_ADC;
2706         case OP_SUBCC_IMM:
2707                 return OP_SUBCC;
2708         case OP_SBB_IMM:
2709                 return OP_SBB;
2710         case OP_OR_IMM:
2711                 return OP_IOR;
2712         case OP_XOR_IMM:
2713                 return OP_IXOR;
2714         case OP_LOAD_MEMBASE:
2715                 return OP_LOAD_MEMINDEX;
2716         case OP_LOADI4_MEMBASE:
2717                 return OP_LOADI4_MEMINDEX;
2718         case OP_LOADU4_MEMBASE:
2719                 return OP_LOADU4_MEMINDEX;
2720         case OP_LOADU1_MEMBASE:
2721                 return OP_LOADU1_MEMINDEX;
2722         case OP_LOADI2_MEMBASE:
2723                 return OP_LOADI2_MEMINDEX;
2724         case OP_LOADU2_MEMBASE:
2725                 return OP_LOADU2_MEMINDEX;
2726         case OP_LOADI1_MEMBASE:
2727                 return OP_LOADI1_MEMINDEX;
2728         case OP_STOREI1_MEMBASE_REG:
2729                 return OP_STOREI1_MEMINDEX;
2730         case OP_STOREI2_MEMBASE_REG:
2731                 return OP_STOREI2_MEMINDEX;
2732         case OP_STOREI4_MEMBASE_REG:
2733                 return OP_STOREI4_MEMINDEX;
2734         case OP_STORE_MEMBASE_REG:
2735                 return OP_STORE_MEMINDEX;
2736         case OP_STORER4_MEMBASE_REG:
2737                 return OP_STORER4_MEMINDEX;
2738         case OP_STORER8_MEMBASE_REG:
2739                 return OP_STORER8_MEMINDEX;
2740         case OP_STORE_MEMBASE_IMM:
2741                 return OP_STORE_MEMBASE_REG;
2742         case OP_STOREI1_MEMBASE_IMM:
2743                 return OP_STOREI1_MEMBASE_REG;
2744         case OP_STOREI2_MEMBASE_IMM:
2745                 return OP_STOREI2_MEMBASE_REG;
2746         case OP_STOREI4_MEMBASE_IMM:
2747                 return OP_STOREI4_MEMBASE_REG;
2748         }
2749         g_assert_not_reached ();
2750 }
2751
2752 /*
2753  * Remove from the instruction list the instructions that can't be
2754  * represented with very simple instructions with no register
2755  * requirements.
2756  */
2757 void
2758 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2759 {
2760         MonoInst *ins, *temp, *last_ins = NULL;
2761         int rot_amount, imm8, low_imm;
2762
2763         MONO_BB_FOR_EACH_INS (bb, ins) {
2764 loop_start:
2765                 switch (ins->opcode) {
2766                 case OP_ADD_IMM:
2767                 case OP_SUB_IMM:
2768                 case OP_AND_IMM:
2769                 case OP_COMPARE_IMM:
2770                 case OP_ICOMPARE_IMM:
2771                 case OP_ADDCC_IMM:
2772                 case OP_ADC_IMM:
2773                 case OP_SUBCC_IMM:
2774                 case OP_SBB_IMM:
2775                 case OP_OR_IMM:
2776                 case OP_XOR_IMM:
2777                 case OP_IADD_IMM:
2778                 case OP_ISUB_IMM:
2779                 case OP_IAND_IMM:
2780                 case OP_IADC_IMM:
2781                 case OP_ISBB_IMM:
2782                 case OP_IOR_IMM:
2783                 case OP_IXOR_IMM:
2784                         if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount)) < 0) {
2785                                 NEW_INS (cfg, temp, OP_ICONST);
2786                                 temp->inst_c0 = ins->inst_imm;
2787                                 temp->dreg = mono_alloc_ireg (cfg);
2788                                 ins->sreg2 = temp->dreg;
2789                                 ins->opcode = mono_op_imm_to_op (ins->opcode);
2790                         }
2791                         if (ins->opcode == OP_SBB || ins->opcode == OP_ISBB || ins->opcode == OP_SUBCC)
2792                                 goto loop_start;
2793                         else
2794                                 break;
2795                 case OP_MUL_IMM:
2796                 case OP_IMUL_IMM:
2797                         if (ins->inst_imm == 1) {
2798                                 ins->opcode = OP_MOVE;
2799                                 break;
2800                         }
2801                         if (ins->inst_imm == 0) {
2802                                 ins->opcode = OP_ICONST;
2803                                 ins->inst_c0 = 0;
2804                                 break;
2805                         }
2806                         imm8 = mono_is_power_of_two (ins->inst_imm);
2807                         if (imm8 > 0) {
2808                                 ins->opcode = OP_SHL_IMM;
2809                                 ins->inst_imm = imm8;
2810                                 break;
2811                         }
2812                         NEW_INS (cfg, temp, OP_ICONST);
2813                         temp->inst_c0 = ins->inst_imm;
2814                         temp->dreg = mono_alloc_ireg (cfg);
2815                         ins->sreg2 = temp->dreg;
2816                         ins->opcode = OP_IMUL;
2817                         break;
2818                 case OP_SBB:
2819                 case OP_ISBB:
2820                 case OP_SUBCC:
2821                 case OP_ISUBCC:
2822                         if (ins->next  && (ins->next->opcode == OP_COND_EXC_C || ins->next->opcode == OP_COND_EXC_IC))
2823                                 /* ARM sets the C flag to 1 if there was _no_ overflow */
2824                                 ins->next->opcode = OP_COND_EXC_NC;
2825                         break;
2826                 case OP_LOCALLOC_IMM:
2827                         NEW_INS (cfg, temp, OP_ICONST);
2828                         temp->inst_c0 = ins->inst_imm;
2829                         temp->dreg = mono_alloc_ireg (cfg);
2830                         ins->sreg1 = temp->dreg;
2831                         ins->opcode = OP_LOCALLOC;
2832                         break;
2833                 case OP_LOAD_MEMBASE:
2834                 case OP_LOADI4_MEMBASE:
2835                 case OP_LOADU4_MEMBASE:
2836                 case OP_LOADU1_MEMBASE:
2837                         /* we can do two things: load the immed in a register
2838                          * and use an indexed load, or see if the immed can be
2839                          * represented as an ad_imm + a load with a smaller offset
2840                          * that fits. We just do the first for now, optimize later.
2841                          */
2842                         if (arm_is_imm12 (ins->inst_offset))
2843                                 break;
2844                         NEW_INS (cfg, temp, OP_ICONST);
2845                         temp->inst_c0 = ins->inst_offset;
2846                         temp->dreg = mono_alloc_ireg (cfg);
2847                         ins->sreg2 = temp->dreg;
2848                         ins->opcode = map_to_reg_reg_op (ins->opcode);
2849                         break;
2850                 case OP_LOADI2_MEMBASE:
2851                 case OP_LOADU2_MEMBASE:
2852                 case OP_LOADI1_MEMBASE:
2853                         if (arm_is_imm8 (ins->inst_offset))
2854                                 break;
2855                         NEW_INS (cfg, temp, OP_ICONST);
2856                         temp->inst_c0 = ins->inst_offset;
2857                         temp->dreg = mono_alloc_ireg (cfg);
2858                         ins->sreg2 = temp->dreg;
2859                         ins->opcode = map_to_reg_reg_op (ins->opcode);
2860                         break;
2861                 case OP_LOADR4_MEMBASE:
2862                 case OP_LOADR8_MEMBASE:
2863                         if (arm_is_fpimm8 (ins->inst_offset))
2864                                 break;
2865                         low_imm = ins->inst_offset & 0x1ff;
2866                         if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~0x1ff, &rot_amount)) >= 0) {
2867                                 NEW_INS (cfg, temp, OP_ADD_IMM);
2868                                 temp->inst_imm = ins->inst_offset & ~0x1ff;
2869                                 temp->sreg1 = ins->inst_basereg;
2870                                 temp->dreg = mono_alloc_ireg (cfg);
2871                                 ins->inst_basereg = temp->dreg;
2872                                 ins->inst_offset = low_imm;
2873                                 break;
2874                         }
2875                         /* VFP/FPA doesn't have indexed load instructions */
2876                         g_assert_not_reached ();
2877                         break;
2878                 case OP_STORE_MEMBASE_REG:
2879                 case OP_STOREI4_MEMBASE_REG:
2880                 case OP_STOREI1_MEMBASE_REG:
2881                         if (arm_is_imm12 (ins->inst_offset))
2882                                 break;
2883                         NEW_INS (cfg, temp, OP_ICONST);
2884                         temp->inst_c0 = ins->inst_offset;
2885                         temp->dreg = mono_alloc_ireg (cfg);
2886                         ins->sreg2 = temp->dreg;
2887                         ins->opcode = map_to_reg_reg_op (ins->opcode);
2888                         break;
2889                 case OP_STOREI2_MEMBASE_REG:
2890                         if (arm_is_imm8 (ins->inst_offset))
2891                                 break;
2892                         NEW_INS (cfg, temp, OP_ICONST);
2893                         temp->inst_c0 = ins->inst_offset;
2894                         temp->dreg = mono_alloc_ireg (cfg);
2895                         ins->sreg2 = temp->dreg;
2896                         ins->opcode = map_to_reg_reg_op (ins->opcode);
2897                         break;
2898                 case OP_STORER4_MEMBASE_REG:
2899                 case OP_STORER8_MEMBASE_REG:
2900                         if (arm_is_fpimm8 (ins->inst_offset))
2901                                 break;
2902                         low_imm = ins->inst_offset & 0x1ff;
2903                         if ((imm8 = mono_arm_is_rotated_imm8 (ins->inst_offset & ~ 0x1ff, &rot_amount)) >= 0 && arm_is_fpimm8 (low_imm)) {
2904                                 NEW_INS (cfg, temp, OP_ADD_IMM);
2905                                 temp->inst_imm = ins->inst_offset & ~0x1ff;
2906                                 temp->sreg1 = ins->inst_destbasereg;
2907                                 temp->dreg = mono_alloc_ireg (cfg);
2908                                 ins->inst_destbasereg = temp->dreg;
2909                                 ins->inst_offset = low_imm;
2910                                 break;
2911                         }
2912                         /*g_print ("fail with: %d (%d, %d)\n", ins->inst_offset, ins->inst_offset & ~0x1ff, low_imm);*/
2913                         /* VFP/FPA doesn't have indexed store instructions */
2914                         g_assert_not_reached ();
2915                         break;
2916                 case OP_STORE_MEMBASE_IMM:
2917                 case OP_STOREI1_MEMBASE_IMM:
2918                 case OP_STOREI2_MEMBASE_IMM:
2919                 case OP_STOREI4_MEMBASE_IMM:
2920                         NEW_INS (cfg, temp, OP_ICONST);
2921                         temp->inst_c0 = ins->inst_imm;
2922                         temp->dreg = mono_alloc_ireg (cfg);
2923                         ins->sreg1 = temp->dreg;
2924                         ins->opcode = map_to_reg_reg_op (ins->opcode);
2925                         last_ins = temp;
2926                         goto loop_start; /* make it handle the possibly big ins->inst_offset */
2927                 case OP_FCOMPARE: {
2928                         gboolean swap = FALSE;
2929                         int reg;
2930
2931                         if (!ins->next) {
2932                                 /* Optimized away */
2933                                 NULLIFY_INS (ins);
2934                                 break;
2935                         }
2936
2937                         /* Some fp compares require swapped operands */
2938                         switch (ins->next->opcode) {
2939                         case OP_FBGT:
2940                                 ins->next->opcode = OP_FBLT;
2941                                 swap = TRUE;
2942                                 break;
2943                         case OP_FBGT_UN:
2944                                 ins->next->opcode = OP_FBLT_UN;
2945                                 swap = TRUE;
2946                                 break;
2947                         case OP_FBLE:
2948                                 ins->next->opcode = OP_FBGE;
2949                                 swap = TRUE;
2950                                 break;
2951                         case OP_FBLE_UN:
2952                                 ins->next->opcode = OP_FBGE_UN;
2953                                 swap = TRUE;
2954                                 break;
2955                         default:
2956                                 break;
2957                         }
2958                         if (swap) {
2959                                 reg = ins->sreg1;
2960                                 ins->sreg1 = ins->sreg2;
2961                                 ins->sreg2 = reg;
2962                         }
2963                         break;
2964                 }
2965                 }
2966
2967                 last_ins = ins;
2968         }
2969         bb->last_ins = last_ins;
2970         bb->max_vreg = cfg->next_vreg;
2971 }
2972
2973 void
2974 mono_arch_decompose_long_opts (MonoCompile *cfg, MonoInst *long_ins)
2975 {
2976         MonoInst *ins;
2977
2978         if (long_ins->opcode == OP_LNEG) {
2979                 ins = long_ins;
2980                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSBS_IMM, ins->dreg + 1, ins->sreg1 + 1, 0);
2981                 MONO_EMIT_NEW_BIALU_IMM (cfg, OP_ARM_RSC_IMM, ins->dreg + 2, ins->sreg1 + 2, 0);
2982                 NULLIFY_INS (ins);
2983         }
2984 }
2985
2986 static guchar*
2987 emit_float_to_int (MonoCompile *cfg, guchar *code, int dreg, int sreg, int size, gboolean is_signed)
2988 {
2989         /* sreg is a float, dreg is an integer reg  */
2990         if (IS_FPA)
2991                 ARM_FPA_FIXZ (code, dreg, sreg);
2992         else if (IS_VFP) {
2993                 if (is_signed)
2994                         ARM_TOSIZD (code, ARM_VFP_F0, sreg);
2995                 else
2996                         ARM_TOUIZD (code, ARM_VFP_F0, sreg);
2997                 ARM_FMRS (code, dreg, ARM_VFP_F0);
2998         }
2999         if (!is_signed) {
3000                 if (size == 1)
3001                         ARM_AND_REG_IMM8 (code, dreg, dreg, 0xff);
3002                 else if (size == 2) {
3003                         ARM_SHL_IMM (code, dreg, dreg, 16);
3004                         ARM_SHR_IMM (code, dreg, dreg, 16);
3005                 }
3006         } else {
3007                 if (size == 1) {
3008                         ARM_SHL_IMM (code, dreg, dreg, 24);
3009                         ARM_SAR_IMM (code, dreg, dreg, 24);
3010                 } else if (size == 2) {
3011                         ARM_SHL_IMM (code, dreg, dreg, 16);
3012                         ARM_SAR_IMM (code, dreg, dreg, 16);
3013                 }
3014         }
3015         return code;
3016 }
3017
3018 #endif /* #ifndef DISABLE_JIT */
3019
3020 typedef struct {
3021         guchar *code;
3022         const guchar *target;
3023         int absolute;
3024         int found;
3025 } PatchData;
3026
3027 #define is_call_imm(diff) ((gint)(diff) >= -33554432 && (gint)(diff) <= 33554431)
3028
3029 static int
3030 search_thunk_slot (void *data, int csize, int bsize, void *user_data) {
3031         PatchData *pdata = (PatchData*)user_data;
3032         guchar *code = data;
3033         guint32 *thunks = data;
3034         guint32 *endthunks = (guint32*)(code + bsize);
3035         int count = 0;
3036         int difflow, diffhigh;
3037
3038         /* always ensure a call from pdata->code can reach to the thunks without further thunks */
3039         difflow = (char*)pdata->code - (char*)thunks;
3040         diffhigh = (char*)pdata->code - (char*)endthunks;
3041         if (!((is_call_imm (thunks) && is_call_imm (endthunks)) || (is_call_imm (difflow) && is_call_imm (diffhigh))))
3042                 return 0;
3043
3044         /*
3045          * The thunk is composed of 3 words:
3046          * load constant from thunks [2] into ARM_IP
3047          * bx to ARM_IP
3048          * address constant
3049          * Note that the LR register is already setup
3050          */
3051         //g_print ("thunk nentries: %d\n", ((char*)endthunks - (char*)thunks)/16);
3052         if ((pdata->found == 2) || (pdata->code >= code && pdata->code <= code + csize)) {
3053                 while (thunks < endthunks) {
3054                         //g_print ("looking for target: %p at %p (%08x-%08x)\n", pdata->target, thunks, thunks [0], thunks [1]);
3055                         if (thunks [2] == (guint32)pdata->target) {
3056                                 arm_patch (pdata->code, (guchar*)thunks);
3057                                 mono_arch_flush_icache (pdata->code, 4);
3058                                 pdata->found = 1;
3059                                 return 1;
3060                         } else if ((thunks [0] == 0) && (thunks [1] == 0) && (thunks [2] == 0)) {
3061                                 /* found a free slot instead: emit thunk */
3062                                 /* ARMREG_IP is fine to use since this can't be an IMT call
3063                                  * which is indirect
3064                                  */
3065                                 code = (guchar*)thunks;
3066                                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
3067                                 if (thumb_supported)
3068                                         ARM_BX (code, ARMREG_IP);
3069                                 else
3070                                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
3071                                 thunks [2] = (guint32)pdata->target;
3072                                 mono_arch_flush_icache ((guchar*)thunks, 12);
3073
3074                                 arm_patch (pdata->code, (guchar*)thunks);
3075                                 mono_arch_flush_icache (pdata->code, 4);
3076                                 pdata->found = 1;
3077                                 return 1;
3078                         }
3079                         /* skip 12 bytes, the size of the thunk */
3080                         thunks += 3;
3081                         count++;
3082                 }
3083                 //g_print ("failed thunk lookup for %p from %p at %p (%d entries)\n", pdata->target, pdata->code, data, count);
3084         }
3085         return 0;
3086 }
3087
3088 static void
3089 handle_thunk (MonoDomain *domain, int absolute, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
3090 {
3091         PatchData pdata;
3092
3093         if (!domain)
3094                 domain = mono_domain_get ();
3095
3096         pdata.code = code;
3097         pdata.target = target;
3098         pdata.absolute = absolute;
3099         pdata.found = 0;
3100
3101         if (dyn_code_mp) {
3102                 mono_code_manager_foreach (dyn_code_mp, search_thunk_slot, &pdata);
3103         }
3104
3105         if (pdata.found != 1) {
3106                 mono_domain_lock (domain);
3107                 mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
3108
3109                 if (!pdata.found) {
3110                         /* this uses the first available slot */
3111                         pdata.found = 2;
3112                         mono_domain_code_foreach (domain, search_thunk_slot, &pdata);
3113                 }
3114                 mono_domain_unlock (domain);
3115         }
3116
3117         if (pdata.found != 1) {
3118                 GHashTable *hash;
3119                 GHashTableIter iter;
3120                 MonoJitDynamicMethodInfo *ji;
3121
3122                 /*
3123                  * This might be a dynamic method, search its code manager. We can only
3124                  * use the dynamic method containing CODE, since the others might be freed later.
3125                  */
3126                 pdata.found = 0;
3127
3128                 mono_domain_lock (domain);
3129                 hash = domain_jit_info (domain)->dynamic_code_hash;
3130                 if (hash) {
3131                         /* FIXME: Speed this up */
3132                         g_hash_table_iter_init (&iter, hash);
3133                         while (g_hash_table_iter_next (&iter, NULL, (gpointer*)&ji)) {
3134                                 mono_code_manager_foreach (ji->code_mp, search_thunk_slot, &pdata);
3135                                 if (pdata.found == 1)
3136                                         break;
3137                         }
3138                 }
3139                 mono_domain_unlock (domain);
3140         }
3141         if (pdata.found != 1)
3142                 g_print ("thunk failed for %p from %p\n", target, code);
3143         g_assert (pdata.found == 1);
3144 }
3145
3146 static void
3147 arm_patch_general (MonoDomain *domain, guchar *code, const guchar *target, MonoCodeManager *dyn_code_mp)
3148 {
3149         guint32 *code32 = (void*)code;
3150         guint32 ins = *code32;
3151         guint32 prim = (ins >> 25) & 7;
3152         guint32 tval = GPOINTER_TO_UINT (target);
3153
3154         //g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
3155         if (prim == 5) { /* 101b */
3156                 /* the diff starts 8 bytes from the branch opcode */
3157                 gint diff = target - code - 8;
3158                 gint tbits;
3159                 gint tmask = 0xffffffff;
3160                 if (tval & 1) { /* entering thumb mode */
3161                         diff = target - 1 - code - 8;
3162                         g_assert (thumb_supported);
3163                         tbits = 0xf << 28; /* bl->blx bit pattern */
3164                         g_assert ((ins & (1 << 24))); /* it must be a bl, not b instruction */
3165                         /* this low bit of the displacement is moved to bit 24 in the instruction encoding */
3166                         if (diff & 2) {
3167                                 tbits |= 1 << 24;
3168                         }
3169                         tmask = ~(1 << 24); /* clear the link bit */
3170                         /*g_print ("blx to thumb: target: %p, code: %p, diff: %d, mask: %x\n", target, code, diff, tmask);*/
3171                 } else {
3172                         tbits = 0;
3173                 }
3174                 if (diff >= 0) {
3175                         if (diff <= 33554431) {
3176                                 diff >>= 2;
3177                                 ins = (ins & 0xff000000) | diff;
3178                                 ins &= tmask;
3179                                 *code32 = ins | tbits;
3180                                 return;
3181                         }
3182                 } else {
3183                         /* diff between 0 and -33554432 */
3184                         if (diff >= -33554432) {
3185                                 diff >>= 2;
3186                                 ins = (ins & 0xff000000) | (diff & ~0xff000000);
3187                                 ins &= tmask;
3188                                 *code32 = ins | tbits;
3189                                 return;
3190                         }
3191                 }
3192                 
3193                 handle_thunk (domain, TRUE, code, target, dyn_code_mp);
3194                 return;
3195         }
3196
3197         /*
3198          * The alternative call sequences looks like this:
3199          *
3200          *      ldr ip, [pc] // loads the address constant
3201          *      b 1f         // jumps around the constant
3202          *      address constant embedded in the code
3203          *   1f:
3204          *      mov lr, pc
3205          *      mov pc, ip
3206          *
3207          * There are two cases for patching:
3208          * a) at the end of method emission: in this case code points to the start
3209          *    of the call sequence
3210          * b) during runtime patching of the call site: in this case code points
3211          *    to the mov pc, ip instruction
3212          *
3213          * We have to handle also the thunk jump code sequence:
3214          *
3215          *      ldr ip, [pc]
3216          *      mov pc, ip
3217          *      address constant // execution never reaches here
3218          */
3219         if ((ins & 0x0ffffff0) == 0x12fff10) {
3220                 /* Branch and exchange: the address is constructed in a reg 
3221                  * We can patch BX when the code sequence is the following:
3222                  *  ldr     ip, [pc, #0]    ; 0x8
3223                  *  b       0xc
3224                  *  .word code_ptr
3225                  *  mov     lr, pc
3226                  *  bx      ips
3227                  * */
3228                 guint32 ccode [4];
3229                 guint8 *emit = (guint8*)ccode;
3230                 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3231                 ARM_B (emit, 0);
3232                 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3233                 ARM_BX (emit, ARMREG_IP);
3234
3235                 /*patching from magic trampoline*/
3236                 if (ins == ccode [3]) {
3237                         g_assert (code32 [-4] == ccode [0]);
3238                         g_assert (code32 [-3] == ccode [1]);
3239                         g_assert (code32 [-1] == ccode [2]);
3240                         code32 [-2] = (guint32)target;
3241                         return;
3242                 }
3243                 /*patching from JIT*/
3244                 if (ins == ccode [0]) {
3245                         g_assert (code32 [1] == ccode [1]);
3246                         g_assert (code32 [3] == ccode [2]);
3247                         g_assert (code32 [4] == ccode [3]);
3248                         code32 [2] = (guint32)target;
3249                         return;
3250                 }
3251                 g_assert_not_reached ();
3252         } else if ((ins & 0x0ffffff0) == 0x12fff30) {
3253                 /*
3254                  * ldr ip, [pc, #0]
3255                  * b 0xc
3256                  * .word code_ptr
3257                  * blx ip
3258                  */
3259                 guint32 ccode [4];
3260                 guint8 *emit = (guint8*)ccode;
3261                 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3262                 ARM_B (emit, 0);
3263                 ARM_BLX_REG (emit, ARMREG_IP);
3264
3265                 g_assert (code32 [-3] == ccode [0]);
3266                 g_assert (code32 [-2] == ccode [1]);
3267                 g_assert (code32 [0] == ccode [2]);
3268
3269                 code32 [-1] = (guint32)target;
3270         } else {
3271                 guint32 ccode [4];
3272                 guint32 *tmp = ccode;
3273                 guint8 *emit = (guint8*)tmp;
3274                 ARM_LDR_IMM (emit, ARMREG_IP, ARMREG_PC, 0);
3275                 ARM_MOV_REG_REG (emit, ARMREG_LR, ARMREG_PC);
3276                 ARM_MOV_REG_REG (emit, ARMREG_PC, ARMREG_IP);
3277                 ARM_BX (emit, ARMREG_IP);
3278                 if (ins == ccode [2]) {
3279                         g_assert_not_reached (); // should be -2 ...
3280                         code32 [-1] = (guint32)target;
3281                         return;
3282                 }
3283                 if (ins == ccode [0]) {
3284                         /* handles both thunk jump code and the far call sequence */
3285                         code32 [2] = (guint32)target;
3286                         return;
3287                 }
3288                 g_assert_not_reached ();
3289         }
3290 //      g_print ("patched with 0x%08x\n", ins);
3291 }
3292
3293 void
3294 arm_patch (guchar *code, const guchar *target)
3295 {
3296         arm_patch_general (NULL, code, target, NULL);
3297 }
3298
3299 /* 
3300  * Return the >= 0 uimm8 value if val can be represented with a byte + rotation
3301  * (with the rotation amount in *rot_amount. rot_amount is already adjusted
3302  * to be used with the emit macros.
3303  * Return -1 otherwise.
3304  */
3305 int
3306 mono_arm_is_rotated_imm8 (guint32 val, gint *rot_amount)
3307 {
3308         guint32 res, i;
3309         for (i = 0; i < 31; i+= 2) {
3310                 res = (val << (32 - i)) | (val >> i);
3311                 if (res & ~0xff)
3312                         continue;
3313                 *rot_amount = i? 32 - i: 0;
3314                 return res;
3315         }
3316         return -1;
3317 }
3318
3319 /*
3320  * Emits in code a sequence of instructions that load the value 'val'
3321  * into the dreg register. Uses at most 4 instructions.
3322  */
3323 guint8*
3324 mono_arm_emit_load_imm (guint8 *code, int dreg, guint32 val)
3325 {
3326         int imm8, rot_amount;
3327 #if 0
3328         ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3329         /* skip the constant pool */
3330         ARM_B (code, 0);
3331         *(int*)code = val;
3332         code += 4;
3333         return code;
3334 #endif
3335         if ((imm8 = mono_arm_is_rotated_imm8 (val, &rot_amount)) >= 0) {
3336                 ARM_MOV_REG_IMM (code, dreg, imm8, rot_amount);
3337         } else if ((imm8 = mono_arm_is_rotated_imm8 (~val, &rot_amount)) >= 0) {
3338                 ARM_MVN_REG_IMM (code, dreg, imm8, rot_amount);
3339         } else {
3340                 if (v7_supported) {
3341                         ARM_MOVW_REG_IMM (code, dreg, val & 0xffff);
3342                         if (val >> 16)
3343                                 ARM_MOVT_REG_IMM (code, dreg, (val >> 16) & 0xffff);
3344                         return code;
3345                 }
3346                 if (val & 0xFF) {
3347                         ARM_MOV_REG_IMM8 (code, dreg, (val & 0xFF));
3348                         if (val & 0xFF00) {
3349                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3350                         }
3351                         if (val & 0xFF0000) {
3352                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3353                         }
3354                         if (val & 0xFF000000) {
3355                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3356                         }
3357                 } else if (val & 0xFF00) {
3358                         ARM_MOV_REG_IMM (code, dreg, (val & 0xFF00) >> 8, 24);
3359                         if (val & 0xFF0000) {
3360                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3361                         }
3362                         if (val & 0xFF000000) {
3363                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3364                         }
3365                 } else if (val & 0xFF0000) {
3366                         ARM_MOV_REG_IMM (code, dreg, (val & 0xFF0000) >> 16, 16);
3367                         if (val & 0xFF000000) {
3368                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF000000) >> 24, 8);
3369                         }
3370                 }
3371                 //g_assert_not_reached ();
3372         }
3373         return code;
3374 }
3375
3376 gboolean
3377 mono_arm_thumb_supported (void)
3378 {
3379         return thumb_supported;
3380 }
3381
3382 #ifndef DISABLE_JIT
3383
3384 /*
3385  * emit_load_volatile_arguments:
3386  *
3387  *  Load volatile arguments from the stack to the original input registers.
3388  * Required before a tail call.
3389  */
3390 static guint8*
3391 emit_load_volatile_arguments (MonoCompile *cfg, guint8 *code)
3392 {
3393         MonoMethod *method = cfg->method;
3394         MonoMethodSignature *sig;
3395         MonoInst *inst;
3396         CallInfo *cinfo;
3397         guint32 i, pos;
3398
3399         /* FIXME: Generate intermediate code instead */
3400
3401         sig = mono_method_signature (method);
3402
3403         /* This is the opposite of the code in emit_prolog */
3404
3405         pos = 0;
3406
3407         cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
3408
3409         if (MONO_TYPE_ISSTRUCT (sig->ret)) {
3410                 ArgInfo *ainfo = &cinfo->ret;
3411                 inst = cfg->vret_addr;
3412                 g_assert (arm_is_imm12 (inst->inst_offset));
3413                 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3414         }
3415         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3416                 ArgInfo *ainfo = cinfo->args + i;
3417                 inst = cfg->args [pos];
3418                 
3419                 if (cfg->verbose_level > 2)
3420                         g_print ("Loading argument %d (type: %d)\n", i, ainfo->storage);
3421                 if (inst->opcode == OP_REGVAR) {
3422                         if (ainfo->storage == RegTypeGeneral)
3423                                 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
3424                         else if (ainfo->storage == RegTypeFP) {
3425                                 g_assert_not_reached ();
3426                         } else if (ainfo->storage == RegTypeBase) {
3427                                 // FIXME:
3428                                 NOT_IMPLEMENTED;
3429                                 /*
3430                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
3431                                         ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
3432                                 } else {
3433                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
3434                                         ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
3435                                 }
3436                                 */
3437                         } else
3438                                 g_assert_not_reached ();
3439                 } else {
3440                         if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
3441                                 switch (ainfo->size) {
3442                                 case 1:
3443                                 case 2:
3444                                         // FIXME:
3445                                         NOT_IMPLEMENTED;
3446                                         break;
3447                                 case 8:
3448                                         g_assert (arm_is_imm12 (inst->inst_offset));
3449                                         ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3450                                         g_assert (arm_is_imm12 (inst->inst_offset + 4));
3451                                         ARM_LDR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
3452                                         break;
3453                                 default:
3454                                         if (arm_is_imm12 (inst->inst_offset)) {
3455                                                 ARM_LDR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
3456                                         } else {
3457                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
3458                                                 ARM_LDR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
3459                                         }
3460                                         break;
3461                                 }
3462                         } else if (ainfo->storage == RegTypeBaseGen) {
3463                                 // FIXME:
3464                                 NOT_IMPLEMENTED;
3465                         } else if (ainfo->storage == RegTypeBase) {
3466                                 /* Nothing to do */
3467                         } else if (ainfo->storage == RegTypeFP) {
3468                                 g_assert_not_reached ();
3469                         } else if (ainfo->storage == RegTypeStructByVal) {
3470                                 int doffset = inst->inst_offset;
3471                                 int soffset = 0;
3472                                 int cur_reg;
3473                                 int size = 0;
3474                                 if (mono_class_from_mono_type (inst->inst_vtype))
3475                                         size = mono_class_native_size (mono_class_from_mono_type (inst->inst_vtype), NULL);
3476                                 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
3477                                         if (arm_is_imm12 (doffset)) {
3478                                                 ARM_LDR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
3479                                         } else {
3480                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
3481                                                 ARM_LDR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
3482                                         }
3483                                         soffset += sizeof (gpointer);
3484                                         doffset += sizeof (gpointer);
3485                                 }
3486                                 if (ainfo->vtsize)
3487                                         // FIXME:
3488                                         NOT_IMPLEMENTED;
3489                         } else if (ainfo->storage == RegTypeStructByAddr) {
3490                         } else {
3491                                 // FIXME:
3492                                 NOT_IMPLEMENTED;
3493                         }
3494                 }
3495                 pos ++;
3496         }
3497
3498         g_free (cinfo);
3499
3500         return code;
3501 }
3502
3503 void
3504 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
3505 {
3506         MonoInst *ins;
3507         MonoCallInst *call;
3508         guint offset;
3509         guint8 *code = cfg->native_code + cfg->code_len;
3510         MonoInst *last_ins = NULL;
3511         guint last_offset = 0;
3512         int max_len, cpos;
3513         int imm8, rot_amount;
3514
3515         /* we don't align basic blocks of loops on arm */
3516
3517         if (cfg->verbose_level > 2)
3518                 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
3519
3520         cpos = bb->max_offset;
3521
3522         if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
3523                 //MonoCoverageInfo *cov = mono_get_coverage_info (cfg->method);
3524                 //g_assert (!mono_compile_aot);
3525                 //cpos += 6;
3526                 //if (bb->cil_code)
3527                 //      cov->data [bb->dfn].iloffset = bb->cil_code - cfg->cil_code;
3528                 /* this is not thread save, but good enough */
3529                 /* fixme: howto handle overflows? */
3530                 //x86_inc_mem (code, &cov->data [bb->dfn].count); 
3531         }
3532
3533     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) {
3534                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3535                                                          (gpointer)"mono_break");
3536                 code = emit_call_seq (cfg, code);
3537         }
3538
3539         MONO_BB_FOR_EACH_INS (bb, ins) {
3540                 offset = code - cfg->native_code;
3541
3542                 max_len = ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
3543
3544                 if (offset > (cfg->code_size - max_len - 16)) {
3545                         cfg->code_size *= 2;
3546                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
3547                         code = cfg->native_code + offset;
3548                 }
3549         //      if (ins->cil_code)
3550         //              g_print ("cil code\n");
3551                 mono_debug_record_line_number (cfg, ins, offset);
3552
3553                 switch (ins->opcode) {
3554                 case OP_MEMORY_BARRIER:
3555                         if (v6_supported) {
3556                                 ARM_MOV_REG_IMM8 (code, ARMREG_R0, 0);
3557                                 ARM_MCR (code, 15, 0, ARMREG_R0, 7, 10, 5);
3558                         }
3559                         break;
3560                 case OP_TLS_GET:
3561 #ifdef HAVE_AEABI_READ_TP
3562                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3563                                                                  (gpointer)"__aeabi_read_tp");
3564                         code = emit_call_seq (cfg, code);
3565
3566                         ARM_LDR_IMM (code, ins->dreg, ARMREG_R0, ins->inst_offset);
3567 #else
3568                         g_assert_not_reached ();
3569 #endif
3570                         break;
3571                 /*case OP_BIGMUL:
3572                         ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3573                         ppc_mulhw (code, ppc_r3, ins->sreg1, ins->sreg2);
3574                         break;
3575                 case OP_BIGMUL_UN:
3576                         ppc_mullw (code, ppc_r4, ins->sreg1, ins->sreg2);
3577                         ppc_mulhwu (code, ppc_r3, ins->sreg1, ins->sreg2);
3578                         break;*/
3579                 case OP_STOREI1_MEMBASE_IMM:
3580                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFF);
3581                         g_assert (arm_is_imm12 (ins->inst_offset));
3582                         ARM_STRB_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3583                         break;
3584                 case OP_STOREI2_MEMBASE_IMM:
3585                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm & 0xFFFF);
3586                         g_assert (arm_is_imm8 (ins->inst_offset));
3587                         ARM_STRH_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3588                         break;
3589                 case OP_STORE_MEMBASE_IMM:
3590                 case OP_STOREI4_MEMBASE_IMM:
3591                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_imm);
3592                         g_assert (arm_is_imm12 (ins->inst_offset));
3593                         ARM_STR_IMM (code, ARMREG_LR, ins->inst_destbasereg, ins->inst_offset);
3594                         break;
3595                 case OP_STOREI1_MEMBASE_REG:
3596                         g_assert (arm_is_imm12 (ins->inst_offset));
3597                         ARM_STRB_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3598                         break;
3599                 case OP_STOREI2_MEMBASE_REG:
3600                         g_assert (arm_is_imm8 (ins->inst_offset));
3601                         ARM_STRH_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3602                         break;
3603                 case OP_STORE_MEMBASE_REG:
3604                 case OP_STOREI4_MEMBASE_REG:
3605                         /* this case is special, since it happens for spill code after lowering has been called */
3606                         if (arm_is_imm12 (ins->inst_offset)) {
3607                                 ARM_STR_IMM (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3608                         } else {
3609                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3610                                 ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ARMREG_LR);
3611                         }
3612                         break;
3613                 case OP_STOREI1_MEMINDEX:
3614                         ARM_STRB_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3615                         break;
3616                 case OP_STOREI2_MEMINDEX:
3617                         ARM_STRH_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3618                         break;
3619                 case OP_STORE_MEMINDEX:
3620                 case OP_STOREI4_MEMINDEX:
3621                         ARM_STR_REG_REG (code, ins->sreg1, ins->inst_destbasereg, ins->sreg2);
3622                         break;
3623                 case OP_LOADU4_MEM:
3624                         g_assert_not_reached ();
3625                         break;
3626                 case OP_LOAD_MEMINDEX:
3627                 case OP_LOADI4_MEMINDEX:
3628                 case OP_LOADU4_MEMINDEX:
3629                         ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3630                         break;
3631                 case OP_LOADI1_MEMINDEX:
3632                         ARM_LDRSB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3633                         break;
3634                 case OP_LOADU1_MEMINDEX:
3635                         ARM_LDRB_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3636                         break;
3637                 case OP_LOADI2_MEMINDEX:
3638                         ARM_LDRSH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3639                         break;
3640                 case OP_LOADU2_MEMINDEX:
3641                         ARM_LDRH_REG_REG (code, ins->dreg, ins->inst_basereg, ins->sreg2);
3642                         break;
3643                 case OP_LOAD_MEMBASE:
3644                 case OP_LOADI4_MEMBASE:
3645                 case OP_LOADU4_MEMBASE:
3646                         /* this case is special, since it happens for spill code after lowering has been called */
3647                         if (arm_is_imm12 (ins->inst_offset)) {
3648                                 ARM_LDR_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3649                         } else {
3650                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
3651                                 ARM_LDR_REG_REG (code, ins->dreg, ins->inst_basereg, ARMREG_LR);
3652                         }
3653                         break;
3654                 case OP_LOADI1_MEMBASE:
3655                         g_assert (arm_is_imm8 (ins->inst_offset));
3656                         ARM_LDRSB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3657                         break;
3658                 case OP_LOADU1_MEMBASE:
3659                         g_assert (arm_is_imm12 (ins->inst_offset));
3660                         ARM_LDRB_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3661                         break;
3662                 case OP_LOADU2_MEMBASE:
3663                         g_assert (arm_is_imm8 (ins->inst_offset));
3664                         ARM_LDRH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3665                         break;
3666                 case OP_LOADI2_MEMBASE:
3667                         g_assert (arm_is_imm8 (ins->inst_offset));
3668                         ARM_LDRSH_IMM (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
3669                         break;
3670                 case OP_ICONV_TO_I1:
3671                         ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 24);
3672                         ARM_SAR_IMM (code, ins->dreg, ins->dreg, 24);
3673                         break;
3674                 case OP_ICONV_TO_I2:
3675                         ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3676                         ARM_SAR_IMM (code, ins->dreg, ins->dreg, 16);
3677                         break;
3678                 case OP_ICONV_TO_U1:
3679                         ARM_AND_REG_IMM8 (code, ins->dreg, ins->sreg1, 0xff);
3680                         break;
3681                 case OP_ICONV_TO_U2:
3682                         ARM_SHL_IMM (code, ins->dreg, ins->sreg1, 16);
3683                         ARM_SHR_IMM (code, ins->dreg, ins->dreg, 16);
3684                         break;
3685                 case OP_COMPARE:
3686                 case OP_ICOMPARE:
3687                         ARM_CMP_REG_REG (code, ins->sreg1, ins->sreg2);
3688                         break;
3689                 case OP_COMPARE_IMM:
3690                 case OP_ICOMPARE_IMM:
3691                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3692                         g_assert (imm8 >= 0);
3693                         ARM_CMP_REG_IMM (code, ins->sreg1, imm8, rot_amount);
3694                         break;
3695                 case OP_BREAK:
3696                         /*
3697                          * gdb does not like encountering the hw breakpoint ins in the debugged code. 
3698                          * So instead of emitting a trap, we emit a call a C function and place a 
3699                          * breakpoint there.
3700                          */
3701                         //*(int*)code = 0xef9f0001;
3702                         //code += 4;
3703                         //ARM_DBRK (code);
3704                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3705                                                                  (gpointer)"mono_break");
3706                         code = emit_call_seq (cfg, code);
3707                         break;
3708                 case OP_RELAXED_NOP:
3709                         ARM_NOP (code);
3710                         break;
3711                 case OP_NOP:
3712                 case OP_DUMMY_USE:
3713                 case OP_DUMMY_STORE:
3714                 case OP_NOT_REACHED:
3715                 case OP_NOT_NULL:
3716                         break;
3717                 case OP_SEQ_POINT: {
3718                         int i;
3719                         MonoInst *info_var = cfg->arch.seq_point_info_var;
3720                         MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
3721                         MonoInst *ss_read_var = cfg->arch.seq_point_read_var;
3722                         MonoInst *ss_method_var = cfg->arch.seq_point_ss_method_var;
3723                         MonoInst *bp_method_var = cfg->arch.seq_point_bp_method_var;
3724                         MonoInst *var;
3725                         int dreg = ARMREG_LR;
3726
3727                         if (cfg->soft_breakpoints) {
3728                                 g_assert (!cfg->compile_aot);
3729                         }
3730
3731                         /*
3732                          * For AOT, we use one got slot per method, which will point to a
3733                          * SeqPointInfo structure, containing all the information required
3734                          * by the code below.
3735                          */
3736                         if (cfg->compile_aot) {
3737                                 g_assert (info_var);
3738                                 g_assert (info_var->opcode == OP_REGOFFSET);
3739                                 g_assert (arm_is_imm12 (info_var->inst_offset));
3740                         }
3741
3742                         if (!cfg->soft_breakpoints) {
3743                                 /*
3744                                  * Read from the single stepping trigger page. This will cause a
3745                                  * SIGSEGV when single stepping is enabled.
3746                                  * We do this _before_ the breakpoint, so single stepping after
3747                                  * a breakpoint is hit will step to the next IL offset.
3748                                  */
3749                                 g_assert (((guint64)(gsize)ss_trigger_page >> 32) == 0);
3750                         }
3751
3752                         if (ins->flags & MONO_INST_SINGLE_STEP_LOC) {
3753                                 if (cfg->soft_breakpoints) {
3754                                         /* Load the address of the sequence point trigger variable. */
3755                                         var = ss_read_var;
3756                                         g_assert (var);
3757                                         g_assert (var->opcode == OP_REGOFFSET);
3758                                         g_assert (arm_is_imm12 (var->inst_offset));
3759                                         ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3760
3761                                         /* Read the value and check whether it is non-zero. */
3762                                         ARM_LDR_IMM (code, dreg, dreg, 0);
3763                                         ARM_CMP_REG_IMM (code, dreg, 0, 0);
3764
3765                                         /* Load the address of the sequence point method. */
3766                                         var = ss_method_var;
3767                                         g_assert (var);
3768                                         g_assert (var->opcode == OP_REGOFFSET);
3769                                         g_assert (arm_is_imm12 (var->inst_offset));
3770                                         ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3771
3772                                         /* Call it conditionally. */
3773                                         ARM_BLX_REG_COND (code, ARMCOND_NE, dreg);
3774                                 } else {
3775                                         if (cfg->compile_aot) {
3776                                                 /* Load the trigger page addr from the variable initialized in the prolog */
3777                                                 var = ss_trigger_page_var;
3778                                                 g_assert (var);
3779                                                 g_assert (var->opcode == OP_REGOFFSET);
3780                                                 g_assert (arm_is_imm12 (var->inst_offset));
3781                                                 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3782                                         } else {
3783                                                 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
3784                                                 ARM_B (code, 0);
3785                                                 *(int*)code = (int)ss_trigger_page;
3786                                                 code += 4;
3787                                         }
3788                                         ARM_LDR_IMM (code, dreg, dreg, 0);
3789                                 }
3790                         }
3791
3792                         mono_add_seq_point (cfg, bb, ins, code - cfg->native_code);
3793
3794                         if (cfg->soft_breakpoints) {
3795                                 /* Load the address of the breakpoint method into ip. */
3796                                 var = bp_method_var;
3797                                 g_assert (var);
3798                                 g_assert (var->opcode == OP_REGOFFSET);
3799                                 g_assert (arm_is_imm12 (var->inst_offset));
3800                                 ARM_LDR_IMM (code, dreg, var->inst_basereg, var->inst_offset);
3801
3802                                 /*
3803                                  * A placeholder for a possible breakpoint inserted by
3804                                  * mono_arch_set_breakpoint ().
3805                                  */
3806                                 ARM_NOP (code);
3807                         } else if (cfg->compile_aot) {
3808                                 guint32 offset = code - cfg->native_code;
3809                                 guint32 val;
3810
3811                                 ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
3812                                 /* Add the offset */
3813                                 val = ((offset / 4) * sizeof (guint8*)) + G_STRUCT_OFFSET (SeqPointInfo, bp_addrs);
3814                                 ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF), 0);
3815                                 /* 
3816                                  * Have to emit nops to keep the difference between the offset
3817                                  * stored in seq_points and breakpoint instruction constant,
3818                                  * mono_arch_get_ip_for_breakpoint () depends on this.
3819                                  */
3820                                 if (val & 0xFF00)
3821                                         ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF00) >> 8, 24);
3822                                 else
3823                                         ARM_NOP (code);
3824                                 if (val & 0xFF0000)
3825                                         ARM_ADD_REG_IMM (code, dreg, dreg, (val & 0xFF0000) >> 16, 16);
3826                                 else
3827                                         ARM_NOP (code);
3828                                 g_assert (!(val & 0xFF000000));
3829                                 /* Load the info->bp_addrs [offset], which is either 0 or the address of a trigger page */
3830                                 ARM_LDR_IMM (code, dreg, dreg, 0);
3831
3832                                 /* What is faster, a branch or a load ? */
3833                                 ARM_CMP_REG_IMM (code, dreg, 0, 0);
3834                                 /* The breakpoint instruction */
3835                                 ARM_LDR_IMM_COND (code, dreg, dreg, 0, ARMCOND_NE);
3836                         } else {
3837                                 /* 
3838                                  * A placeholder for a possible breakpoint inserted by
3839                                  * mono_arch_set_breakpoint ().
3840                                  */
3841                                 for (i = 0; i < 4; ++i)
3842                                         ARM_NOP (code);
3843                         }
3844                         break;
3845                 }
3846                 case OP_ADDCC:
3847                 case OP_IADDCC:
3848                         ARM_ADDS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3849                         break;
3850                 case OP_IADD:
3851                         ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3852                         break;
3853                 case OP_ADC:
3854                 case OP_IADC:
3855                         ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3856                         break;
3857                 case OP_ADDCC_IMM:
3858                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3859                         g_assert (imm8 >= 0);
3860                         ARM_ADDS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3861                         break;
3862                 case OP_ADD_IMM:
3863                 case OP_IADD_IMM:
3864                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3865                         g_assert (imm8 >= 0);
3866                         ARM_ADD_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3867                         break;
3868                 case OP_ADC_IMM:
3869                 case OP_IADC_IMM:
3870                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3871                         g_assert (imm8 >= 0);
3872                         ARM_ADCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3873                         break;
3874                 case OP_IADD_OVF:
3875                         ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3876                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3877                         break;
3878                 case OP_IADD_OVF_UN:
3879                         ARM_ADD_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3880                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3881                         break;
3882                 case OP_ISUB_OVF:
3883                         ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3884                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3885                         break;
3886                 case OP_ISUB_OVF_UN:
3887                         ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3888                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3889                         break;
3890                 case OP_ADD_OVF_CARRY:
3891                         ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3892                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3893                         break;
3894                 case OP_ADD_OVF_UN_CARRY:
3895                         ARM_ADCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3896                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3897                         break;
3898                 case OP_SUB_OVF_CARRY:
3899                         ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3900                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_FALSE, PPC_BR_EQ, "OverflowException");
3901                         break;
3902                 case OP_SUB_OVF_UN_CARRY:
3903                         ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3904                         //EMIT_COND_SYSTEM_EXCEPTION_FLAGS (PPC_BR_TRUE, PPC_BR_EQ, "OverflowException");
3905                         break;
3906                 case OP_SUBCC:
3907                 case OP_ISUBCC:
3908                         ARM_SUBS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3909                         break;
3910                 case OP_SUBCC_IMM:
3911                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3912                         g_assert (imm8 >= 0);
3913                         ARM_SUBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3914                         break;
3915                 case OP_ISUB:
3916                         ARM_SUB_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3917                         break;
3918                 case OP_SBB:
3919                 case OP_ISBB:
3920                         ARM_SBCS_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3921                         break;
3922                 case OP_SUB_IMM:
3923                 case OP_ISUB_IMM:
3924                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3925                         g_assert (imm8 >= 0);
3926                         ARM_SUB_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3927                         break;
3928                 case OP_SBB_IMM:
3929                 case OP_ISBB_IMM:
3930                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3931                         g_assert (imm8 >= 0);
3932                         ARM_SBCS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3933                         break;
3934                 case OP_ARM_RSBS_IMM:
3935                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3936                         g_assert (imm8 >= 0);
3937                         ARM_RSBS_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3938                         break;
3939                 case OP_ARM_RSC_IMM:
3940                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3941                         g_assert (imm8 >= 0);
3942                         ARM_RSC_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3943                         break;
3944                 case OP_IAND:
3945                         ARM_AND_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3946                         break;
3947                 case OP_AND_IMM:
3948                 case OP_IAND_IMM:
3949                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3950                         g_assert (imm8 >= 0);
3951                         ARM_AND_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3952                         break;
3953                 case OP_IDIV:
3954                 case OP_IDIV_UN:
3955                 case OP_DIV_IMM:
3956                 case OP_IREM:
3957                 case OP_IREM_UN:
3958                 case OP_REM_IMM:
3959                         /* crappy ARM arch doesn't have a DIV instruction */
3960                         g_assert_not_reached ();
3961                 case OP_IOR:
3962                         ARM_ORR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3963                         break;
3964                 case OP_OR_IMM:
3965                 case OP_IOR_IMM:
3966                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3967                         g_assert (imm8 >= 0);
3968                         ARM_ORR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3969                         break;
3970                 case OP_IXOR:
3971                         ARM_EOR_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3972                         break;
3973                 case OP_XOR_IMM:
3974                 case OP_IXOR_IMM:
3975                         imm8 = mono_arm_is_rotated_imm8 (ins->inst_imm, &rot_amount);
3976                         g_assert (imm8 >= 0);
3977                         ARM_EOR_REG_IMM (code, ins->dreg, ins->sreg1, imm8, rot_amount);
3978                         break;
3979                 case OP_ISHL:
3980                         ARM_SHL_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3981                         break;
3982                 case OP_SHL_IMM:
3983                 case OP_ISHL_IMM:
3984                         if (ins->inst_imm)
3985                                 ARM_SHL_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3986                         else if (ins->dreg != ins->sreg1)
3987                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3988                         break;
3989                 case OP_ISHR:
3990                         ARM_SAR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
3991                         break;
3992                 case OP_SHR_IMM:
3993                 case OP_ISHR_IMM:
3994                         if (ins->inst_imm)
3995                                 ARM_SAR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
3996                         else if (ins->dreg != ins->sreg1)
3997                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
3998                         break;
3999                 case OP_SHR_UN_IMM:
4000                 case OP_ISHR_UN_IMM:
4001                         if (ins->inst_imm)
4002                                 ARM_SHR_IMM (code, ins->dreg, ins->sreg1, (ins->inst_imm & 0x1f));
4003                         else if (ins->dreg != ins->sreg1)
4004                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4005                         break;
4006                 case OP_ISHR_UN:
4007                         ARM_SHR_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4008                         break;
4009                 case OP_INOT:
4010                         ARM_MVN_REG_REG (code, ins->dreg, ins->sreg1);
4011                         break;
4012                 case OP_INEG:
4013                         ARM_RSB_REG_IMM8 (code, ins->dreg, ins->sreg1, 0);
4014                         break;
4015                 case OP_IMUL:
4016                         if (ins->dreg == ins->sreg2)
4017                                 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4018                         else
4019                                 ARM_MUL_REG_REG (code, ins->dreg, ins->sreg2, ins->sreg1);
4020                         break;
4021                 case OP_MUL_IMM:
4022                         g_assert_not_reached ();
4023                         break;
4024                 case OP_IMUL_OVF:
4025                         /* FIXME: handle ovf/ sreg2 != dreg */
4026                         ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4027                         /* FIXME: MUL doesn't set the C/O flags on ARM */
4028                         break;
4029                 case OP_IMUL_OVF_UN:
4030                         /* FIXME: handle ovf/ sreg2 != dreg */
4031                         ARM_MUL_REG_REG (code, ins->dreg, ins->sreg1, ins->sreg2);
4032                         /* FIXME: MUL doesn't set the C/O flags on ARM */
4033                         break;
4034                 case OP_ICONST:
4035                         code = mono_arm_emit_load_imm (code, ins->dreg, ins->inst_c0);
4036                         break;
4037                 case OP_AOTCONST:
4038                         /* Load the GOT offset */
4039                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
4040                         ARM_LDR_IMM (code, ins->dreg, ARMREG_PC, 0);
4041                         ARM_B (code, 0);
4042                         *(gpointer*)code = NULL;
4043                         code += 4;
4044                         /* Load the value from the GOT */
4045                         ARM_LDR_REG_REG (code, ins->dreg, ARMREG_PC, ins->dreg);
4046                         break;
4047                 case OP_ICONV_TO_I4:
4048                 case OP_ICONV_TO_U4:
4049                 case OP_MOVE:
4050                         if (ins->dreg != ins->sreg1)
4051                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4052                         break;
4053                 case OP_SETLRET: {
4054                         int saved = ins->sreg2;
4055                         if (ins->sreg2 == ARM_LSW_REG) {
4056                                 ARM_MOV_REG_REG (code, ARMREG_LR, ins->sreg2);
4057                                 saved = ARMREG_LR;
4058                         }
4059                         if (ins->sreg1 != ARM_LSW_REG)
4060                                 ARM_MOV_REG_REG (code, ARM_LSW_REG, ins->sreg1);
4061                         if (saved != ARM_MSW_REG)
4062                                 ARM_MOV_REG_REG (code, ARM_MSW_REG, saved);
4063                         break;
4064                 }
4065                 case OP_FMOVE:
4066                         if (IS_FPA)
4067                                 ARM_FPA_MVFD (code, ins->dreg, ins->sreg1);
4068                         else if (IS_VFP)
4069                                 ARM_CPYD (code, ins->dreg, ins->sreg1);
4070                         break;
4071                 case OP_FCONV_TO_R4:
4072                         if (IS_FPA)
4073                                 ARM_FPA_MVFS (code, ins->dreg, ins->sreg1);
4074                         else if (IS_VFP) {
4075                                 ARM_CVTD (code, ins->dreg, ins->sreg1);
4076                                 ARM_CVTS (code, ins->dreg, ins->dreg);
4077                         }
4078                         break;
4079                 case OP_JMP:
4080                         /*
4081                          * Keep in sync with mono_arch_emit_epilog
4082                          */
4083                         g_assert (!cfg->method->save_lmf);
4084
4085                         code = emit_load_volatile_arguments (cfg, code);
4086
4087                         code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage);
4088                         if (iphone_abi) {
4089                                 if (cfg->used_int_regs)
4090                                         ARM_POP (code, cfg->used_int_regs);
4091                                 ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
4092                         } else {
4093                                 ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_LR));
4094                         }
4095                         mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
4096                         if (cfg->compile_aot) {
4097                                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
4098                                 ARM_B (code, 0);
4099                                 *(gpointer*)code = NULL;
4100                                 code += 4;
4101                                 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
4102                         } else {
4103                                 ARM_B (code, 0);
4104                         }
4105                         break;
4106                 case OP_CHECK_THIS:
4107                         /* ensure ins->sreg1 is not NULL */
4108                         ARM_LDRB_IMM (code, ARMREG_LR, ins->sreg1, 0);
4109                         break;
4110                 case OP_ARGLIST: {
4111                         g_assert (cfg->sig_cookie < 128);
4112                         ARM_LDR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
4113                         ARM_STR_IMM (code, ARMREG_IP, ins->sreg1, 0);
4114                         break;
4115                 }
4116                 case OP_FCALL:
4117                 case OP_LCALL:
4118                 case OP_VCALL:
4119                 case OP_VCALL2:
4120                 case OP_VOIDCALL:
4121                 case OP_CALL:
4122                         call = (MonoCallInst*)ins;
4123                         if (ins->flags & MONO_INST_HAS_METHOD)
4124                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
4125                         else
4126                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
4127                         code = emit_call_seq (cfg, code);
4128                         ins->flags |= MONO_INST_GC_CALLSITE;
4129                         ins->backend.pc_offset = code - cfg->native_code;
4130                         code = emit_move_return_value (cfg, ins, code);
4131                         break;
4132                 case OP_FCALL_REG:
4133                 case OP_LCALL_REG:
4134                 case OP_VCALL_REG:
4135                 case OP_VCALL2_REG:
4136                 case OP_VOIDCALL_REG:
4137                 case OP_CALL_REG:
4138                         code = emit_call_reg (code, ins->sreg1);
4139                         ins->flags |= MONO_INST_GC_CALLSITE;
4140                         ins->backend.pc_offset = code - cfg->native_code;
4141                         code = emit_move_return_value (cfg, ins, code);
4142                         break;
4143                 case OP_FCALL_MEMBASE:
4144                 case OP_LCALL_MEMBASE:
4145                 case OP_VCALL_MEMBASE:
4146                 case OP_VCALL2_MEMBASE:
4147                 case OP_VOIDCALL_MEMBASE:
4148                 case OP_CALL_MEMBASE:
4149                         g_assert (arm_is_imm12 (ins->inst_offset));
4150                         g_assert (ins->sreg1 != ARMREG_LR);
4151                         call = (MonoCallInst*)ins;
4152                         if (call->dynamic_imt_arg || call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
4153                                 ARM_ADD_REG_IMM8 (code, ARMREG_LR, ARMREG_PC, 4);
4154                                 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
4155                                 /* 
4156                                  * We can't embed the method in the code stream in PIC code, or
4157                                  * in gshared code.
4158                                  * Instead, we put it in V5 in code emitted by 
4159                                  * mono_arch_emit_imt_argument (), and embed NULL here to 
4160                                  * signal the IMT thunk that the value is in V5.
4161                                  */
4162                                 if (call->dynamic_imt_arg)
4163                                         *((gpointer*)code) = NULL;
4164                                 else
4165                                         *((gpointer*)code) = (gpointer)call->method;
4166                                 code += 4;
4167                         } else {
4168                                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
4169                                 ARM_LDR_IMM (code, ARMREG_PC, ins->sreg1, ins->inst_offset);
4170                         }
4171                         ins->flags |= MONO_INST_GC_CALLSITE;
4172                         ins->backend.pc_offset = code - cfg->native_code;
4173                         code = emit_move_return_value (cfg, ins, code);
4174                         break;
4175                 case OP_LOCALLOC: {
4176                         /* keep alignment */
4177                         int alloca_waste = cfg->param_area;
4178                         alloca_waste += 7;
4179                         alloca_waste &= ~7;
4180                         /* round the size to 8 bytes */
4181                         ARM_ADD_REG_IMM8 (code, ins->dreg, ins->sreg1, 7);
4182                         ARM_BIC_REG_IMM8 (code, ins->dreg, ins->dreg, 7);
4183                         if (alloca_waste)
4184                                 ARM_ADD_REG_IMM8 (code, ins->dreg, ins->dreg, alloca_waste);
4185                         ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ins->dreg);
4186                         /* memzero the area: dreg holds the size, sp is the pointer */
4187                         if (ins->flags & MONO_INST_INIT) {
4188                                 guint8 *start_loop, *branch_to_cond;
4189                                 ARM_MOV_REG_IMM8 (code, ARMREG_LR, 0);
4190                                 branch_to_cond = code;
4191                                 ARM_B (code, 0);
4192                                 start_loop = code;
4193                                 ARM_STR_REG_REG (code, ARMREG_LR, ARMREG_SP, ins->dreg);
4194                                 arm_patch (branch_to_cond, code);
4195                                 /* decrement by 4 and set flags */
4196                                 ARM_SUBS_REG_IMM8 (code, ins->dreg, ins->dreg, sizeof (mgreg_t));
4197                                 ARM_B_COND (code, ARMCOND_GE, 0);
4198                                 arm_patch (code - 4, start_loop);
4199                         }
4200                         ARM_ADD_REG_IMM8 (code, ins->dreg, ARMREG_SP, alloca_waste);
4201                         break;
4202                 }
4203                 case OP_DYN_CALL: {
4204                         int i;
4205                         MonoInst *var = cfg->dyn_call_var;
4206
4207                         g_assert (var->opcode == OP_REGOFFSET);
4208                         g_assert (arm_is_imm12 (var->inst_offset));
4209
4210                         /* lr = args buffer filled by mono_arch_get_dyn_call_args () */
4211                         ARM_MOV_REG_REG( code, ARMREG_LR, ins->sreg1);
4212                         /* ip = ftn */
4213                         ARM_MOV_REG_REG( code, ARMREG_IP, ins->sreg2);
4214
4215                         /* Save args buffer */
4216                         ARM_STR_IMM (code, ARMREG_LR, var->inst_basereg, var->inst_offset);
4217
4218                         /* Set stack slots using R0 as scratch reg */
4219                         /* MONO_ARCH_DYN_CALL_PARAM_AREA gives the size of stack space available */
4220                         for (i = 0; i < DYN_CALL_STACK_ARGS; ++i) {
4221                                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, (PARAM_REGS + i) * sizeof (mgreg_t));
4222                                 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, i * sizeof (mgreg_t));
4223                         }
4224
4225                         /* Set argument registers */
4226                         for (i = 0; i < PARAM_REGS; ++i)
4227                                 ARM_LDR_IMM (code, i, ARMREG_LR, i * sizeof (mgreg_t));
4228
4229                         /* Make the call */
4230                         ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
4231                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
4232
4233                         /* Save result */
4234                         ARM_LDR_IMM (code, ARMREG_IP, var->inst_basereg, var->inst_offset);
4235                         ARM_STR_IMM (code, ARMREG_R0, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res)); 
4236                         ARM_STR_IMM (code, ARMREG_R1, ARMREG_IP, G_STRUCT_OFFSET (DynCallArgs, res2)); 
4237                         break;
4238                 }
4239                 case OP_THROW: {
4240                         if (ins->sreg1 != ARMREG_R0)
4241                                 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
4242                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
4243                                              (gpointer)"mono_arch_throw_exception");
4244                         code = emit_call_seq (cfg, code);
4245                         break;
4246                 }
4247                 case OP_RETHROW: {
4248                         if (ins->sreg1 != ARMREG_R0)
4249                                 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
4250                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
4251                                              (gpointer)"mono_arch_rethrow_exception");
4252                         code = emit_call_seq (cfg, code);
4253                         break;
4254                 }
4255                 case OP_START_HANDLER: {
4256                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4257
4258                         if (arm_is_imm12 (spvar->inst_offset)) {
4259                                 ARM_STR_IMM (code, ARMREG_LR, spvar->inst_basereg, spvar->inst_offset);
4260                         } else {
4261                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
4262                                 ARM_STR_REG_REG (code, ARMREG_LR, spvar->inst_basereg, ARMREG_IP);
4263                         }
4264                         break;
4265                 }
4266                 case OP_ENDFILTER: {
4267                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4268
4269                         if (ins->sreg1 != ARMREG_R0)
4270                                 ARM_MOV_REG_REG (code, ARMREG_R0, ins->sreg1);
4271                         if (arm_is_imm12 (spvar->inst_offset)) {
4272                                 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
4273                         } else {
4274                                 g_assert (ARMREG_IP != spvar->inst_basereg);
4275                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
4276                                 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
4277                         }
4278                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
4279                         break;
4280                 }
4281                 case OP_ENDFINALLY: {
4282                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
4283
4284                         if (arm_is_imm12 (spvar->inst_offset)) {
4285                                 ARM_LDR_IMM (code, ARMREG_IP, spvar->inst_basereg, spvar->inst_offset);
4286                         } else {
4287                                 g_assert (ARMREG_IP != spvar->inst_basereg);
4288                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, spvar->inst_offset);
4289                                 ARM_LDR_REG_REG (code, ARMREG_IP, spvar->inst_basereg, ARMREG_IP);
4290                         }
4291                         ARM_MOV_REG_REG (code, ARMREG_PC, ARMREG_IP);
4292                         break;
4293                 }
4294                 case OP_CALL_HANDLER: 
4295                         mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4296                         ARM_BL (code, 0);
4297                         mono_cfg_add_try_hole (cfg, ins->inst_eh_block, code, bb);
4298                         break;
4299                 case OP_LABEL:
4300                         ins->inst_c0 = code - cfg->native_code;
4301                         break;
4302                 case OP_BR:
4303                         /*if (ins->inst_target_bb->native_offset) {
4304                                 ARM_B (code, 0);
4305                                 //x86_jump_code (code, cfg->native_code + ins->inst_target_bb->native_offset); 
4306                         } else*/ {
4307                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
4308                                 ARM_B (code, 0);
4309                         } 
4310                         break;
4311                 case OP_BR_REG:
4312                         ARM_MOV_REG_REG (code, ARMREG_PC, ins->sreg1);
4313                         break;
4314                 case OP_SWITCH:
4315                         /* 
4316                          * In the normal case we have:
4317                          *      ldr pc, [pc, ins->sreg1 << 2]
4318                          *      nop
4319                          * If aot, we have:
4320                          *      ldr lr, [pc, ins->sreg1 << 2]
4321                          *      add pc, pc, lr
4322                          * After follows the data.
4323                          * FIXME: add aot support.
4324                          */
4325                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_SWITCH, ins->inst_p0);
4326                         max_len += 4 * GPOINTER_TO_INT (ins->klass);
4327                         if (offset + max_len > (cfg->code_size - 16)) {
4328                                 cfg->code_size += max_len;
4329                                 cfg->code_size *= 2;
4330                                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4331                                 code = cfg->native_code + offset;
4332                         }
4333                         ARM_LDR_REG_REG_SHIFT (code, ARMREG_PC, ARMREG_PC, ins->sreg1, ARMSHIFT_LSL, 2);
4334                         ARM_NOP (code);
4335                         code += 4 * GPOINTER_TO_INT (ins->klass);
4336                         break;
4337                 case OP_CEQ:
4338                 case OP_ICEQ:
4339                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
4340                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
4341                         break;
4342                 case OP_CLT:
4343                 case OP_ICLT:
4344                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4345                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LT);
4346                         break;
4347                 case OP_CLT_UN:
4348                 case OP_ICLT_UN:
4349                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4350                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_LO);
4351                         break;
4352                 case OP_CGT:
4353                 case OP_ICGT:
4354                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4355                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_GT);
4356                         break;
4357                 case OP_CGT_UN:
4358                 case OP_ICGT_UN:
4359                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4360                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_HI);
4361                         break;
4362                 case OP_COND_EXC_EQ:
4363                 case OP_COND_EXC_NE_UN:
4364                 case OP_COND_EXC_LT:
4365                 case OP_COND_EXC_LT_UN:
4366                 case OP_COND_EXC_GT:
4367                 case OP_COND_EXC_GT_UN:
4368                 case OP_COND_EXC_GE:
4369                 case OP_COND_EXC_GE_UN:
4370                 case OP_COND_EXC_LE:
4371                 case OP_COND_EXC_LE_UN:
4372                         EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_EQ, ins->inst_p1);
4373                         break;
4374                 case OP_COND_EXC_IEQ:
4375                 case OP_COND_EXC_INE_UN:
4376                 case OP_COND_EXC_ILT:
4377                 case OP_COND_EXC_ILT_UN:
4378                 case OP_COND_EXC_IGT:
4379                 case OP_COND_EXC_IGT_UN:
4380                 case OP_COND_EXC_IGE:
4381                 case OP_COND_EXC_IGE_UN:
4382                 case OP_COND_EXC_ILE:
4383                 case OP_COND_EXC_ILE_UN:
4384                         EMIT_COND_SYSTEM_EXCEPTION (ins->opcode - OP_COND_EXC_IEQ, ins->inst_p1);
4385                         break;
4386                 case OP_COND_EXC_C:
4387                 case OP_COND_EXC_IC:
4388                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CS, ins->inst_p1);
4389                         break;
4390                 case OP_COND_EXC_OV:
4391                 case OP_COND_EXC_IOV:
4392                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, ins->inst_p1);
4393                         break;
4394                 case OP_COND_EXC_NC:
4395                 case OP_COND_EXC_INC:
4396                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_CC, ins->inst_p1);
4397                         break;
4398                 case OP_COND_EXC_NO:
4399                 case OP_COND_EXC_INO:
4400                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VC, ins->inst_p1);
4401                         break;
4402                 case OP_IBEQ:
4403                 case OP_IBNE_UN:
4404                 case OP_IBLT:
4405                 case OP_IBLT_UN:
4406                 case OP_IBGT:
4407                 case OP_IBGT_UN:
4408                 case OP_IBGE:
4409                 case OP_IBGE_UN:
4410                 case OP_IBLE:
4411                 case OP_IBLE_UN:
4412                         EMIT_COND_BRANCH (ins, ins->opcode - OP_IBEQ);
4413                         break;
4414
4415                 /* floating point opcodes */
4416 #ifdef ARM_FPU_FPA
4417                 case OP_R8CONST:
4418                         if (cfg->compile_aot) {
4419                                 ARM_FPA_LDFD (code, ins->dreg, ARMREG_PC, 0);
4420                                 ARM_B (code, 1);
4421                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4422                                 code += 4;
4423                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
4424                                 code += 4;
4425                         } else {
4426                                 /* FIXME: we can optimize the imm load by dealing with part of 
4427                                  * the displacement in LDFD (aligning to 512).
4428                                  */
4429                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4430                                 ARM_FPA_LDFD (code, ins->dreg, ARMREG_LR, 0);
4431                         }
4432                         break;
4433                 case OP_R4CONST:
4434                         if (cfg->compile_aot) {
4435                                 ARM_FPA_LDFS (code, ins->dreg, ARMREG_PC, 0);
4436                                 ARM_B (code, 0);
4437                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4438                                 code += 4;
4439                         } else {
4440                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4441                                 ARM_FPA_LDFS (code, ins->dreg, ARMREG_LR, 0);
4442                         }
4443                         break;
4444                 case OP_STORER8_MEMBASE_REG:
4445                         /* This is generated by the local regalloc pass which runs after the lowering pass */
4446                         if (!arm_is_fpimm8 (ins->inst_offset)) {
4447                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4448                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
4449                                 ARM_FPA_STFD (code, ins->sreg1, ARMREG_LR, 0);
4450                         } else {
4451                                 ARM_FPA_STFD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4452                         }
4453                         break;
4454                 case OP_LOADR8_MEMBASE:
4455                         /* This is generated by the local regalloc pass which runs after the lowering pass */
4456                         if (!arm_is_fpimm8 (ins->inst_offset)) {
4457                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4458                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
4459                                 ARM_FPA_LDFD (code, ins->dreg, ARMREG_LR, 0);
4460                         } else {
4461                                 ARM_FPA_LDFD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4462                         }
4463                         break;
4464                 case OP_STORER4_MEMBASE_REG:
4465                         g_assert (arm_is_fpimm8 (ins->inst_offset));
4466                         ARM_FPA_STFS (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4467                         break;
4468                 case OP_LOADR4_MEMBASE:
4469                         g_assert (arm_is_fpimm8 (ins->inst_offset));
4470                         ARM_FPA_LDFS (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4471                         break;
4472                 case OP_ICONV_TO_R_UN: {
4473                         int tmpreg;
4474                         tmpreg = ins->dreg == 0? 1: 0;
4475                         ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
4476                         ARM_FPA_FLTD (code, ins->dreg, ins->sreg1);
4477                         ARM_B_COND (code, ARMCOND_GE, 8);
4478                         /* save the temp register */
4479                         ARM_SUB_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
4480                         ARM_FPA_STFD (code, tmpreg, ARMREG_SP, 0);
4481                         ARM_FPA_LDFD (code, tmpreg, ARMREG_PC, 12);
4482                         ARM_FPA_ADFD (code, ins->dreg, ins->dreg, tmpreg);
4483                         ARM_FPA_LDFD (code, tmpreg, ARMREG_SP, 0);
4484                         ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 8);
4485                         /* skip the constant pool */
4486                         ARM_B (code, 8);
4487                         code += 4;
4488                         *(int*)code = 0x41f00000;
4489                         code += 4;
4490                         *(int*)code = 0;
4491                         code += 4;
4492                         /* FIXME: adjust:
4493                          * ldfltd  ftemp, [pc, #8] 0x41f00000 0x00000000
4494                          * adfltd  fdest, fdest, ftemp
4495                          */
4496                         break;
4497                 }
4498                 case OP_ICONV_TO_R4:
4499                         ARM_FPA_FLTS (code, ins->dreg, ins->sreg1);
4500                         break;
4501                 case OP_ICONV_TO_R8:
4502                         ARM_FPA_FLTD (code, ins->dreg, ins->sreg1);
4503                         break;
4504
4505 #elif defined(ARM_FPU_VFP)
4506
4507                 case OP_R8CONST:
4508                         if (cfg->compile_aot) {
4509                                 ARM_FLDD (code, ins->dreg, ARMREG_PC, 0);
4510                                 ARM_B (code, 1);
4511                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4512                                 code += 4;
4513                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[1];
4514                                 code += 4;
4515                         } else {
4516                                 /* FIXME: we can optimize the imm load by dealing with part of 
4517                                  * the displacement in LDFD (aligning to 512).
4518                                  */
4519                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4520                                 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4521                         }
4522                         break;
4523                 case OP_R4CONST:
4524                         if (cfg->compile_aot) {
4525                                 ARM_FLDS (code, ins->dreg, ARMREG_PC, 0);
4526                                 ARM_B (code, 0);
4527                                 *(guint32*)code = ((guint32*)(ins->inst_p0))[0];
4528                                 code += 4;
4529                                 ARM_CVTS (code, ins->dreg, ins->dreg);
4530                         } else {
4531                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, (guint32)ins->inst_p0);
4532                                 ARM_FLDS (code, ins->dreg, ARMREG_LR, 0);
4533                                 ARM_CVTS (code, ins->dreg, ins->dreg);
4534                         }
4535                         break;
4536                 case OP_STORER8_MEMBASE_REG:
4537                         /* This is generated by the local regalloc pass which runs after the lowering pass */
4538                         if (!arm_is_fpimm8 (ins->inst_offset)) {
4539                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4540                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_destbasereg);
4541                                 ARM_FSTD (code, ins->sreg1, ARMREG_LR, 0);
4542                         } else {
4543                                 ARM_FSTD (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
4544                         }
4545                         break;
4546                 case OP_LOADR8_MEMBASE:
4547                         /* This is generated by the local regalloc pass which runs after the lowering pass */
4548                         if (!arm_is_fpimm8 (ins->inst_offset)) {
4549                                 code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
4550                                 ARM_ADD_REG_REG (code, ARMREG_LR, ARMREG_LR, ins->inst_basereg);
4551                                 ARM_FLDD (code, ins->dreg, ARMREG_LR, 0);
4552                         } else {
4553                                 ARM_FLDD (code, ins->dreg, ins->inst_basereg, ins->inst_offset);
4554                         }
4555                         break;
4556                 case OP_STORER4_MEMBASE_REG:
4557                         g_assert (arm_is_fpimm8 (ins->inst_offset));
4558                         ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4559                         ARM_FSTS (code, ARM_VFP_F0, ins->inst_destbasereg, ins->inst_offset);
4560                         break;
4561                 case OP_LOADR4_MEMBASE:
4562                         g_assert (arm_is_fpimm8 (ins->inst_offset));
4563                         ARM_FLDS (code, ARM_VFP_F0, ins->inst_basereg, ins->inst_offset);
4564                         ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4565                         break;
4566                 case OP_ICONV_TO_R_UN: {
4567                         g_assert_not_reached ();
4568                         break;
4569                 }
4570                 case OP_ICONV_TO_R4:
4571                         ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4572                         ARM_FSITOS (code, ARM_VFP_F0, ARM_VFP_F0);
4573                         ARM_CVTS (code, ins->dreg, ARM_VFP_F0);
4574                         break;
4575                 case OP_ICONV_TO_R8:
4576                         ARM_FMSR (code, ARM_VFP_F0, ins->sreg1);
4577                         ARM_FSITOD (code, ins->dreg, ARM_VFP_F0);
4578                         break;
4579
4580                 case OP_SETFRET:
4581                         if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4) {
4582                                 ARM_CVTD (code, ARM_VFP_F0, ins->sreg1);
4583                                 ARM_FMRS (code, ARMREG_R0, ARM_VFP_F0);
4584                         } else {
4585                                 ARM_FMRRD (code, ARMREG_R0, ARMREG_R1, ins->sreg1);
4586                         }
4587                         break;
4588
4589 #endif
4590
4591                 case OP_FCONV_TO_I1:
4592                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
4593                         break;
4594                 case OP_FCONV_TO_U1:
4595                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
4596                         break;
4597                 case OP_FCONV_TO_I2:
4598                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
4599                         break;
4600                 case OP_FCONV_TO_U2:
4601                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
4602                         break;
4603                 case OP_FCONV_TO_I4:
4604                 case OP_FCONV_TO_I:
4605                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
4606                         break;
4607                 case OP_FCONV_TO_U4:
4608                 case OP_FCONV_TO_U:
4609                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
4610                         break;
4611                 case OP_FCONV_TO_I8:
4612                 case OP_FCONV_TO_U8:
4613                         g_assert_not_reached ();
4614                         /* Implemented as helper calls */
4615                         break;
4616                 case OP_LCONV_TO_R_UN:
4617                         g_assert_not_reached ();
4618                         /* Implemented as helper calls */
4619                         break;
4620                 case OP_LCONV_TO_OVF_I4_2: {
4621                         guint8 *high_bit_not_set, *valid_negative, *invalid_negative, *valid_positive;
4622                         /* 
4623                          * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
4624                          */
4625
4626                         ARM_CMP_REG_IMM8 (code, ins->sreg1, 0);
4627                         high_bit_not_set = code;
4628                         ARM_B_COND (code, ARMCOND_GE, 0); /*branch if bit 31 of the lower part is not set*/
4629
4630                         ARM_CMN_REG_IMM8 (code, ins->sreg2, 1); /*This have the same effect as CMP reg, 0xFFFFFFFF */
4631                         valid_negative = code;
4632                         ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0xFFFFFFFF (lower part has bit 31 set) */
4633                         invalid_negative = code;
4634                         ARM_B_COND (code, ARMCOND_AL, 0);
4635                         
4636                         arm_patch (high_bit_not_set, code);
4637
4638                         ARM_CMP_REG_IMM8 (code, ins->sreg2, 0);
4639                         valid_positive = code;
4640                         ARM_B_COND (code, ARMCOND_EQ, 0); /*branch if upper part == 0 (lower part has bit 31 clear)*/
4641
4642                         arm_patch (invalid_negative, code);
4643                         EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_AL, "OverflowException");
4644
4645                         arm_patch (valid_negative, code);
4646                         arm_patch (valid_positive, code);
4647
4648                         if (ins->dreg != ins->sreg1)
4649                                 ARM_MOV_REG_REG (code, ins->dreg, ins->sreg1);
4650                         break;
4651                 }
4652 #ifdef ARM_FPU_FPA
4653                 case OP_FADD:
4654                         ARM_FPA_ADFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4655                         break;
4656                 case OP_FSUB:
4657                         ARM_FPA_SUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4658                         break;          
4659                 case OP_FMUL:
4660                         ARM_FPA_MUFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4661                         break;          
4662                 case OP_FDIV:
4663                         ARM_FPA_DVFD (code, ins->dreg, ins->sreg1, ins->sreg2);
4664                         break;          
4665                 case OP_FNEG:
4666                         ARM_FPA_MNFD (code, ins->dreg, ins->sreg1);
4667                         break;
4668 #elif defined(ARM_FPU_VFP)
4669                 case OP_FADD:
4670                         ARM_VFP_ADDD (code, ins->dreg, ins->sreg1, ins->sreg2);
4671                         break;
4672                 case OP_FSUB:
4673                         ARM_VFP_SUBD (code, ins->dreg, ins->sreg1, ins->sreg2);
4674                         break;          
4675                 case OP_FMUL:
4676                         ARM_VFP_MULD (code, ins->dreg, ins->sreg1, ins->sreg2);
4677                         break;          
4678                 case OP_FDIV:
4679                         ARM_VFP_DIVD (code, ins->dreg, ins->sreg1, ins->sreg2);
4680                         break;          
4681                 case OP_FNEG:
4682                         ARM_NEGD (code, ins->dreg, ins->sreg1);
4683                         break;
4684 #endif
4685                 case OP_FREM:
4686                         /* emulated */
4687                         g_assert_not_reached ();
4688                         break;
4689                 case OP_FCOMPARE:
4690                         if (IS_FPA) {
4691                                 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4692                         } else if (IS_VFP) {
4693                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4694                                 ARM_FMSTAT (code);
4695                         }
4696                         break;
4697                 case OP_FCEQ:
4698                         if (IS_FPA) {
4699                                 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4700                         } else if (IS_VFP) {
4701                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4702                                 ARM_FMSTAT (code);
4703                         }
4704                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 0, ARMCOND_NE);
4705                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_EQ);
4706                         break;
4707                 case OP_FCLT:
4708                         if (IS_FPA) {
4709                                 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4710                         } else {
4711                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4712                                 ARM_FMSTAT (code);
4713                         }
4714                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4715                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4716                         break;
4717                 case OP_FCLT_UN:
4718                         if (IS_FPA) {
4719                                 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg1, ins->sreg2);
4720                         } else if (IS_VFP) {
4721                                 ARM_CMPD (code, ins->sreg1, ins->sreg2);
4722                                 ARM_FMSTAT (code);
4723                         }
4724                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4725                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4726                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4727                         break;
4728                 case OP_FCGT:
4729                         /* swapped */
4730                         if (IS_FPA) {
4731                                 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4732                         } else if (IS_VFP) {
4733                                 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4734                                 ARM_FMSTAT (code);
4735                         }
4736                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4737                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4738                         break;
4739                 case OP_FCGT_UN:
4740                         /* swapped */
4741                         if (IS_FPA) {
4742                                 ARM_FPA_FCMP (code, ARM_FPA_CMF, ins->sreg2, ins->sreg1);
4743                         } else if (IS_VFP) {
4744                                 ARM_CMPD (code, ins->sreg2, ins->sreg1);
4745                                 ARM_FMSTAT (code);
4746                         }
4747                         ARM_MOV_REG_IMM8 (code, ins->dreg, 0);
4748                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_MI);
4749                         ARM_MOV_REG_IMM8_COND (code, ins->dreg, 1, ARMCOND_VS);
4750                         break;
4751                 /* ARM FPA flags table:
4752                  * N        Less than               ARMCOND_MI
4753                  * Z        Equal                   ARMCOND_EQ
4754                  * C        Greater Than or Equal   ARMCOND_CS
4755                  * V        Unordered               ARMCOND_VS
4756                  */
4757                 case OP_FBEQ:
4758                         EMIT_COND_BRANCH (ins, OP_IBEQ - OP_IBEQ);
4759                         break;
4760                 case OP_FBNE_UN:
4761                         EMIT_COND_BRANCH (ins, OP_IBNE_UN - OP_IBEQ);
4762                         break;
4763                 case OP_FBLT:
4764                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4765                         break;
4766                 case OP_FBLT_UN:
4767                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4768                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_MI); /* N set */
4769                         break;
4770                 case OP_FBGT:
4771                 case OP_FBGT_UN:
4772                 case OP_FBLE:
4773                 case OP_FBLE_UN:
4774                         g_assert_not_reached ();
4775                         break;
4776                 case OP_FBGE:
4777                         if (IS_VFP) {
4778                                 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4779                         } else {
4780                                 /* FPA requires EQ even thou the docs suggests that just CS is enough */
4781                                 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_EQ);
4782                                 EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_CS);
4783                         }
4784                         break;
4785                 case OP_FBGE_UN:
4786                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_VS); /* V set */
4787                         EMIT_COND_BRANCH_FLAGS (ins, ARMCOND_GE);
4788                         break;
4789
4790                 case OP_CKFINITE: {
4791                         if (IS_FPA) {
4792                                 if (ins->dreg != ins->sreg1)
4793                                         ARM_FPA_MVFD (code, ins->dreg, ins->sreg1);
4794                         } else if (IS_VFP) {
4795                                 ARM_ABSD (code, ARM_VFP_D1, ins->sreg1);
4796                                 ARM_FLDD (code, ARM_VFP_D0, ARMREG_PC, 0);
4797                                 ARM_B (code, 1);
4798                                 *(guint32*)code = 0xffffffff;
4799                                 code += 4;
4800                                 *(guint32*)code = 0x7fefffff;
4801                                 code += 4;
4802                                 ARM_CMPD (code, ARM_VFP_D1, ARM_VFP_D0);
4803                                 ARM_FMSTAT (code);
4804                                 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_GT, "ArithmeticException");
4805                                 ARM_CMPD (code, ins->sreg1, ins->sreg1);
4806                                 ARM_FMSTAT (code);
4807                                 EMIT_COND_SYSTEM_EXCEPTION_FLAGS (ARMCOND_VS, "ArithmeticException");
4808                                 ARM_CPYD (code, ins->dreg, ins->sreg1);
4809                         }
4810                         break;
4811                 }
4812
4813                 case OP_GC_LIVENESS_DEF:
4814                 case OP_GC_LIVENESS_USE:
4815                 case OP_GC_PARAM_SLOT_LIVENESS_DEF:
4816                         ins->backend.pc_offset = code - cfg->native_code;
4817                         break;
4818                 case OP_GC_SPILL_SLOT_LIVENESS_DEF:
4819                         ins->backend.pc_offset = code - cfg->native_code;
4820                         bb->spill_slot_defs = g_slist_prepend_mempool (cfg->mempool, bb->spill_slot_defs, ins);
4821                         break;
4822
4823                 default:
4824                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
4825                         g_assert_not_reached ();
4826                 }
4827
4828                 if ((cfg->opt & MONO_OPT_BRANCH) && ((code - cfg->native_code - offset) > max_len)) {
4829                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
4830                                    mono_inst_name (ins->opcode), max_len, code - cfg->native_code - offset);
4831                         g_assert_not_reached ();
4832                 }
4833                
4834                 cpos += max_len;
4835
4836                 last_ins = ins;
4837                 last_offset = offset;
4838         }
4839
4840         cfg->code_len = code - cfg->native_code;
4841 }
4842
4843 #endif /* DISABLE_JIT */
4844
4845 #ifdef HAVE_AEABI_READ_TP
4846 void __aeabi_read_tp (void);
4847 #endif
4848
4849 void
4850 mono_arch_register_lowlevel_calls (void)
4851 {
4852         /* The signature doesn't matter */
4853         mono_register_jit_icall (mono_arm_throw_exception, "mono_arm_throw_exception", mono_create_icall_signature ("void"), TRUE);
4854         mono_register_jit_icall (mono_arm_throw_exception_by_token, "mono_arm_throw_exception_by_token", mono_create_icall_signature ("void"), TRUE);
4855
4856 #ifndef MONO_CROSS_COMPILE
4857 #ifdef HAVE_AEABI_READ_TP
4858         mono_register_jit_icall (__aeabi_read_tp, "__aeabi_read_tp", mono_create_icall_signature ("void"), TRUE);
4859 #endif
4860 #endif
4861 }
4862
4863 #define patch_lis_ori(ip,val) do {\
4864                 guint16 *__lis_ori = (guint16*)(ip);    \
4865                 __lis_ori [1] = (((guint32)(val)) >> 16) & 0xffff;      \
4866                 __lis_ori [3] = ((guint32)(val)) & 0xffff;      \
4867         } while (0)
4868
4869 void
4870 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, MonoCodeManager *dyn_code_mp, gboolean run_cctors)
4871 {
4872         MonoJumpInfo *patch_info;
4873         gboolean compile_aot = !run_cctors;
4874
4875         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
4876                 unsigned char *ip = patch_info->ip.i + code;
4877                 const unsigned char *target;
4878
4879                 if (patch_info->type == MONO_PATCH_INFO_SWITCH && !compile_aot) {
4880                         gpointer *jt = (gpointer*)(ip + 8);
4881                         int i;
4882                         /* jt is the inlined jump table, 2 instructions after ip
4883                          * In the normal case we store the absolute addresses,
4884                          * otherwise the displacements.
4885                          */
4886                         for (i = 0; i < patch_info->data.table->table_size; i++)
4887                                 jt [i] = code + (int)patch_info->data.table->table [i];
4888                         continue;
4889                 }
4890                 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
4891
4892                 if (compile_aot) {
4893                         switch (patch_info->type) {
4894                         case MONO_PATCH_INFO_BB:
4895                         case MONO_PATCH_INFO_LABEL:
4896                                 break;
4897                         default:
4898                                 /* No need to patch these */
4899                                 continue;
4900                         }
4901                 }
4902
4903                 switch (patch_info->type) {
4904                 case MONO_PATCH_INFO_IP:
4905                         g_assert_not_reached ();
4906                         patch_lis_ori (ip, ip);
4907                         continue;
4908                 case MONO_PATCH_INFO_METHOD_REL:
4909                         g_assert_not_reached ();
4910                         *((gpointer *)(ip)) = code + patch_info->data.offset;
4911                         continue;
4912                 case MONO_PATCH_INFO_METHODCONST:
4913                 case MONO_PATCH_INFO_CLASS:
4914                 case MONO_PATCH_INFO_IMAGE:
4915                 case MONO_PATCH_INFO_FIELD:
4916                 case MONO_PATCH_INFO_VTABLE:
4917                 case MONO_PATCH_INFO_IID:
4918                 case MONO_PATCH_INFO_SFLDA:
4919                 case MONO_PATCH_INFO_LDSTR:
4920                 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
4921                 case MONO_PATCH_INFO_LDTOKEN:
4922                         g_assert_not_reached ();
4923                         /* from OP_AOTCONST : lis + ori */
4924                         patch_lis_ori (ip, target);
4925                         continue;
4926                 case MONO_PATCH_INFO_R4:
4927                 case MONO_PATCH_INFO_R8:
4928                         g_assert_not_reached ();
4929                         *((gconstpointer *)(ip + 2)) = patch_info->data.target;
4930                         continue;
4931                 case MONO_PATCH_INFO_EXC_NAME:
4932                         g_assert_not_reached ();
4933                         *((gconstpointer *)(ip + 1)) = patch_info->data.name;
4934                         continue;
4935                 case MONO_PATCH_INFO_NONE:
4936                 case MONO_PATCH_INFO_BB_OVF:
4937                 case MONO_PATCH_INFO_EXC_OVF:
4938                         /* everything is dealt with at epilog output time */
4939                         continue;
4940                 default:
4941                         break;
4942                 }
4943                 arm_patch_general (domain, ip, target, dyn_code_mp);
4944         }
4945 }
4946
4947 #ifndef DISABLE_JIT
4948
4949 /*
4950  * Stack frame layout:
4951  * 
4952  *   ------------------- fp
4953  *      MonoLMF structure or saved registers
4954  *   -------------------
4955  *      locals
4956  *   -------------------
4957  *      spilled regs
4958  *   -------------------
4959  *      optional 8 bytes for tracing
4960  *   -------------------
4961  *      param area             size is cfg->param_area
4962  *   ------------------- sp
4963  */
4964 guint8 *
4965 mono_arch_emit_prolog (MonoCompile *cfg)
4966 {
4967         MonoMethod *method = cfg->method;
4968         MonoBasicBlock *bb;
4969         MonoMethodSignature *sig;
4970         MonoInst *inst;
4971         int alloc_size, orig_alloc_size, pos, max_offset, i, rot_amount;
4972         guint8 *code;
4973         CallInfo *cinfo;
4974         int tracing = 0;
4975         int lmf_offset = 0;
4976         int prev_sp_offset, reg_offset;
4977
4978         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4979                 tracing = 1;
4980
4981         sig = mono_method_signature (method);
4982         cfg->code_size = 256 + sig->param_count * 64;
4983         code = cfg->native_code = g_malloc (cfg->code_size);
4984
4985         mono_emit_unwind_op_def_cfa (cfg, code, ARMREG_SP, 0);
4986
4987         alloc_size = cfg->stack_offset;
4988         pos = 0;
4989         prev_sp_offset = 0;
4990
4991         if (!method->save_lmf) {
4992                 if (iphone_abi) {
4993                         /* 
4994                          * The iphone uses R7 as the frame pointer, and it points at the saved
4995                          * r7+lr:
4996                          *         <lr>
4997                          * r7 ->   <r7>
4998                          *         <rest of frame>
4999                          * We can't use r7 as a frame pointer since it points into the middle of
5000                          * the frame, so we keep using our own frame pointer.
5001                          * FIXME: Optimize this.
5002                          */
5003                         g_assert (darwin);
5004                         ARM_PUSH (code, (1 << ARMREG_R7) | (1 << ARMREG_LR));
5005                         ARM_MOV_REG_REG (code, ARMREG_R7, ARMREG_SP);
5006                         prev_sp_offset += 8; /* r7 and lr */
5007                         mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
5008                         mono_emit_unwind_op_offset (cfg, code, ARMREG_R7, (- prev_sp_offset) + 0);
5009
5010                         /* No need to push LR again */
5011                         if (cfg->used_int_regs)
5012                                 ARM_PUSH (code, cfg->used_int_regs);
5013                 } else {
5014                         ARM_PUSH (code, cfg->used_int_regs | (1 << ARMREG_LR));
5015                         prev_sp_offset += 4;
5016                 }
5017                 for (i = 0; i < 16; ++i) {
5018                         if (cfg->used_int_regs & (1 << i))
5019                                 prev_sp_offset += 4;
5020                 }
5021                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
5022                 reg_offset = 0;
5023                 for (i = 0; i < 16; ++i) {
5024                         if ((cfg->used_int_regs & (1 << i))) {
5025                                 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
5026                                 mini_gc_set_slot_type_from_cfa (cfg, (- prev_sp_offset) + reg_offset, SLOT_NOREF);
5027                                 reg_offset += 4;
5028                         }
5029                 }
5030                 if (iphone_abi) {
5031                         mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
5032                         mini_gc_set_slot_type_from_cfa (cfg, -4, SLOT_NOREF);
5033                 } else {
5034                         mono_emit_unwind_op_offset (cfg, code, ARMREG_LR, -4);
5035                         mini_gc_set_slot_type_from_cfa (cfg, -4, SLOT_NOREF);
5036                 }
5037         } else {
5038                 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_SP);
5039                 ARM_PUSH (code, 0x5ff0);
5040                 prev_sp_offset += 4 * 10; /* all but r0-r3, sp and pc */
5041                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset);
5042                 reg_offset = 0;
5043                 for (i = 0; i < 16; ++i) {
5044                         if ((i > ARMREG_R3) && (i != ARMREG_SP) && (i != ARMREG_PC)) {
5045                                 mono_emit_unwind_op_offset (cfg, code, i, (- prev_sp_offset) + reg_offset);
5046                                 reg_offset += 4;
5047                         }
5048                 }
5049                 pos += sizeof (MonoLMF) - prev_sp_offset;
5050                 lmf_offset = pos;
5051         }
5052         alloc_size += pos;
5053         orig_alloc_size = alloc_size;
5054         // align to MONO_ARCH_FRAME_ALIGNMENT bytes
5055         if (alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) {
5056                 alloc_size += MONO_ARCH_FRAME_ALIGNMENT - 1;
5057                 alloc_size &= ~(MONO_ARCH_FRAME_ALIGNMENT - 1);
5058         }
5059
5060         /* the stack used in the pushed regs */
5061         if (prev_sp_offset & 4)
5062                 alloc_size += 4;
5063         cfg->stack_usage = alloc_size;
5064         if (alloc_size) {
5065                 if ((i = mono_arm_is_rotated_imm8 (alloc_size, &rot_amount)) >= 0) {
5066                         ARM_SUB_REG_IMM (code, ARMREG_SP, ARMREG_SP, i, rot_amount);
5067                 } else {
5068                         code = mono_arm_emit_load_imm (code, ARMREG_IP, alloc_size);
5069                         ARM_SUB_REG_REG (code, ARMREG_SP, ARMREG_SP, ARMREG_IP);
5070                 }
5071                 mono_emit_unwind_op_def_cfa_offset (cfg, code, prev_sp_offset + alloc_size);
5072         }
5073         if (cfg->frame_reg != ARMREG_SP) {
5074                 ARM_MOV_REG_REG (code, cfg->frame_reg, ARMREG_SP);
5075                 mono_emit_unwind_op_def_cfa_reg (cfg, code, cfg->frame_reg);
5076         }
5077         //g_print ("prev_sp_offset: %d, alloc_size:%d\n", prev_sp_offset, alloc_size);
5078         prev_sp_offset += alloc_size;
5079
5080         for (i = 0; i < alloc_size - orig_alloc_size; i += 4)
5081                 mini_gc_set_slot_type_from_cfa (cfg, (- prev_sp_offset) + orig_alloc_size + i, SLOT_NOREF);
5082
5083         /* compute max_offset in order to use short forward jumps
5084          * we could skip do it on arm because the immediate displacement
5085          * for jumps is large enough, it may be useful later for constant pools
5086          */
5087         max_offset = 0;
5088         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5089                 MonoInst *ins = bb->code;
5090                 bb->max_offset = max_offset;
5091
5092                 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
5093                         max_offset += 6; 
5094
5095                 MONO_BB_FOR_EACH_INS (bb, ins)
5096                         max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
5097         }
5098
5099         /* store runtime generic context */
5100         if (cfg->rgctx_var) {
5101                 MonoInst *ins = cfg->rgctx_var;
5102
5103                 g_assert (ins->opcode == OP_REGOFFSET);
5104
5105                 if (arm_is_imm12 (ins->inst_offset)) {
5106                         ARM_STR_IMM (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ins->inst_offset);
5107                 } else {
5108                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5109                         ARM_STR_REG_REG (code, MONO_ARCH_RGCTX_REG, ins->inst_basereg, ARMREG_LR);
5110                 }
5111         }
5112
5113         /* load arguments allocated to register from the stack */
5114         pos = 0;
5115
5116         cinfo = get_call_info (cfg->generic_sharing_context, NULL, sig);
5117
5118         if (MONO_TYPE_ISSTRUCT (sig->ret) && cinfo->ret.storage != RegTypeStructByVal) {
5119                 ArgInfo *ainfo = &cinfo->ret;
5120                 inst = cfg->vret_addr;
5121                 g_assert (arm_is_imm12 (inst->inst_offset));
5122                 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5123         }
5124
5125         if (sig->call_convention == MONO_CALL_VARARG) {
5126                 ArgInfo *cookie = &cinfo->sig_cookie;
5127
5128                 /* Save the sig cookie address */
5129                 g_assert (cookie->storage == RegTypeBase);
5130
5131                 g_assert (arm_is_imm12 (prev_sp_offset + cookie->offset));
5132                 g_assert (arm_is_imm12 (cfg->sig_cookie));
5133                 ARM_ADD_REG_IMM8 (code, ARMREG_IP, cfg->frame_reg, prev_sp_offset + cookie->offset);
5134                 ARM_STR_IMM (code, ARMREG_IP, cfg->frame_reg, cfg->sig_cookie);
5135         }
5136
5137         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5138                 ArgInfo *ainfo = cinfo->args + i;
5139                 inst = cfg->args [pos];
5140                 
5141                 if (cfg->verbose_level > 2)
5142                         g_print ("Saving argument %d (type: %d)\n", i, ainfo->storage);
5143                 if (inst->opcode == OP_REGVAR) {
5144                         if (ainfo->storage == RegTypeGeneral)
5145                                 ARM_MOV_REG_REG (code, inst->dreg, ainfo->reg);
5146                         else if (ainfo->storage == RegTypeFP) {
5147                                 g_assert_not_reached ();
5148                         } else if (ainfo->storage == RegTypeBase) {
5149                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
5150                                         ARM_LDR_IMM (code, inst->dreg, ARMREG_SP, (prev_sp_offset + ainfo->offset));
5151                                 } else {
5152                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5153                                         ARM_LDR_REG_REG (code, inst->dreg, ARMREG_SP, ARMREG_IP);
5154                                 }
5155                         } else
5156                                 g_assert_not_reached ();
5157
5158                         if (cfg->verbose_level > 2)
5159                                 g_print ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg));
5160                 } else {
5161                         /* the argument should be put on the stack: FIXME handle size != word  */
5162                         if (ainfo->storage == RegTypeGeneral || ainfo->storage == RegTypeIRegPair) {
5163                                 switch (ainfo->size) {
5164                                 case 1:
5165                                         if (arm_is_imm12 (inst->inst_offset))
5166                                                 ARM_STRB_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5167                                         else {
5168                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5169                                                 ARM_STRB_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
5170                                         }
5171                                         break;
5172                                 case 2:
5173                                         if (arm_is_imm8 (inst->inst_offset)) {
5174                                                 ARM_STRH_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5175                                         } else {
5176                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5177                                                 ARM_STRH_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
5178                                         }
5179                                         break;
5180                                 case 8:
5181                                         g_assert (arm_is_imm12 (inst->inst_offset));
5182                                         ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5183                                         g_assert (arm_is_imm12 (inst->inst_offset + 4));
5184                                         ARM_STR_IMM (code, ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
5185                                         break;
5186                                 default:
5187                                         if (arm_is_imm12 (inst->inst_offset)) {
5188                                                 ARM_STR_IMM (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
5189                                         } else {
5190                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5191                                                 ARM_STR_REG_REG (code, ainfo->reg, inst->inst_basereg, ARMREG_IP);
5192                                         }
5193                                         break;
5194                                 }
5195                         } else if (ainfo->storage == RegTypeBaseGen) {
5196                                 g_assert (arm_is_imm12 (prev_sp_offset + ainfo->offset));
5197                                 g_assert (arm_is_imm12 (inst->inst_offset));
5198                                 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
5199                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
5200                                 ARM_STR_IMM (code, ARMREG_R3, inst->inst_basereg, inst->inst_offset);
5201                         } else if (ainfo->storage == RegTypeBase) {
5202                                 if (arm_is_imm12 (prev_sp_offset + ainfo->offset)) {
5203                                         ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset));
5204                                 } else {
5205                                         code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset);
5206                                         ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
5207                                 }
5208
5209                                 switch (ainfo->size) {
5210                                 case 1:
5211                                         if (arm_is_imm8 (inst->inst_offset)) {
5212                                                 ARM_STRB_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
5213                                         } else {
5214                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5215                                                 ARM_STRB_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5216                                         }
5217                                         break;
5218                                 case 2:
5219                                         if (arm_is_imm8 (inst->inst_offset)) {
5220                                                 ARM_STRH_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
5221                                         } else {
5222                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5223                                                 ARM_STRH_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5224                                         }
5225                                         break;
5226                                 case 8:
5227                                         if (arm_is_imm12 (inst->inst_offset)) {
5228                                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
5229                                         } else {
5230                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5231                                                 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5232                                         }
5233                                         if (arm_is_imm12 (prev_sp_offset + ainfo->offset + 4)) {
5234                                                 ARM_LDR_IMM (code, ARMREG_LR, ARMREG_SP, (prev_sp_offset + ainfo->offset + 4));
5235                                         } else {
5236                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, prev_sp_offset + ainfo->offset + 4);
5237                                                 ARM_LDR_REG_REG (code, ARMREG_LR, ARMREG_SP, ARMREG_IP);
5238                                         }
5239                                         if (arm_is_imm12 (inst->inst_offset + 4)) {
5240                                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset + 4);
5241                                         } else {
5242                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset + 4);
5243                                                 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5244                                         }
5245                                         break;
5246                                 default:
5247                                         if (arm_is_imm12 (inst->inst_offset)) {
5248                                                 ARM_STR_IMM (code, ARMREG_LR, inst->inst_basereg, inst->inst_offset);
5249                                         } else {
5250                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, inst->inst_offset);
5251                                                 ARM_STR_REG_REG (code, ARMREG_LR, inst->inst_basereg, ARMREG_IP);
5252                                         }
5253                                         break;
5254                                 }
5255                         } else if (ainfo->storage == RegTypeFP) {
5256                                 g_assert_not_reached ();
5257                         } else if (ainfo->storage == RegTypeStructByVal) {
5258                                 int doffset = inst->inst_offset;
5259                                 int soffset = 0;
5260                                 int cur_reg;
5261                                 int size = 0;
5262                                 size = mini_type_stack_size_full (cfg->generic_sharing_context, inst->inst_vtype, NULL, sig->pinvoke);
5263                                 for (cur_reg = 0; cur_reg < ainfo->size; ++cur_reg) {
5264                                         if (arm_is_imm12 (doffset)) {
5265                                                 ARM_STR_IMM (code, ainfo->reg + cur_reg, inst->inst_basereg, doffset);
5266                                         } else {
5267                                                 code = mono_arm_emit_load_imm (code, ARMREG_IP, doffset);
5268                                                 ARM_STR_REG_REG (code, ainfo->reg + cur_reg, inst->inst_basereg, ARMREG_IP);
5269                                         }
5270                                         soffset += sizeof (gpointer);
5271                                         doffset += sizeof (gpointer);
5272                                 }
5273                                 if (ainfo->vtsize) {
5274                                         /* FIXME: handle overrun! with struct sizes not multiple of 4 */
5275                                         //g_print ("emit_memcpy (prev_sp_ofs: %d, ainfo->offset: %d, soffset: %d)\n", prev_sp_offset, ainfo->offset, soffset);
5276                                         code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, doffset, ARMREG_SP, prev_sp_offset + ainfo->offset);
5277                                 }
5278                         } else if (ainfo->storage == RegTypeStructByAddr) {
5279                                 g_assert_not_reached ();
5280                                 /* FIXME: handle overrun! with struct sizes not multiple of 4 */
5281                                 code = emit_memcpy (code, ainfo->vtsize * sizeof (gpointer), inst->inst_basereg, inst->inst_offset, ainfo->reg, 0);
5282                         } else
5283                                 g_assert_not_reached ();
5284                 }
5285                 pos++;
5286         }
5287
5288         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
5289                 if (cfg->compile_aot)
5290                         /* AOT code is only used in the root domain */
5291                         code = mono_arm_emit_load_imm (code, ARMREG_R0, 0);
5292                 else
5293                         code = mono_arm_emit_load_imm (code, ARMREG_R0, (guint32)cfg->domain);
5294                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
5295                              (gpointer)"mono_jit_thread_attach");
5296                 code = emit_call_seq (cfg, code);
5297         }
5298
5299         if (method->save_lmf)
5300                 code = emit_save_lmf (cfg, code, alloc_size - lmf_offset);
5301
5302         if (tracing)
5303                 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
5304
5305         if (cfg->arch.seq_point_info_var) {
5306                 MonoInst *ins = cfg->arch.seq_point_info_var;
5307
5308                 /* Initialize the variable from a GOT slot */
5309                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_SEQ_POINT_INFO, cfg->method);
5310                 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
5311                 ARM_B (code, 0);
5312                 *(gpointer*)code = NULL;
5313                 code += 4;
5314                 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
5315
5316                 g_assert (ins->opcode == OP_REGOFFSET);
5317
5318                 if (arm_is_imm12 (ins->inst_offset)) {
5319                         ARM_STR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
5320                 } else {
5321                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5322                         ARM_STR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
5323                 }
5324         }
5325
5326         /* Initialize ss_trigger_page_var */
5327         if (!cfg->soft_breakpoints) {
5328                 MonoInst *info_var = cfg->arch.seq_point_info_var;
5329                 MonoInst *ss_trigger_page_var = cfg->arch.ss_trigger_page_var;
5330                 int dreg = ARMREG_LR;
5331
5332                 if (info_var) {
5333                         g_assert (info_var->opcode == OP_REGOFFSET);
5334                         g_assert (arm_is_imm12 (info_var->inst_offset));
5335
5336                         ARM_LDR_IMM (code, dreg, info_var->inst_basereg, info_var->inst_offset);
5337                         /* Load the trigger page addr */
5338                         ARM_LDR_IMM (code, dreg, dreg, G_STRUCT_OFFSET (SeqPointInfo, ss_trigger_page));
5339                         ARM_STR_IMM (code, dreg, ss_trigger_page_var->inst_basereg, ss_trigger_page_var->inst_offset);
5340                 }
5341         }
5342
5343         if (cfg->arch.seq_point_read_var) {
5344                 MonoInst *read_ins = cfg->arch.seq_point_read_var;
5345                 MonoInst *ss_method_ins = cfg->arch.seq_point_ss_method_var;
5346                 MonoInst *bp_method_ins = cfg->arch.seq_point_bp_method_var;
5347
5348                 g_assert (read_ins->opcode == OP_REGOFFSET);
5349                 g_assert (arm_is_imm12 (read_ins->inst_offset));
5350                 g_assert (ss_method_ins->opcode == OP_REGOFFSET);
5351                 g_assert (arm_is_imm12 (ss_method_ins->inst_offset));
5352                 g_assert (bp_method_ins->opcode == OP_REGOFFSET);
5353                 g_assert (arm_is_imm12 (bp_method_ins->inst_offset));
5354
5355                 ARM_MOV_REG_REG (code, ARMREG_LR, ARMREG_PC);
5356                 ARM_B (code, 2);
5357                 *(volatile int **)code = &ss_trigger_var;
5358                 code += 4;
5359                 *(gpointer*)code = single_step_func_wrapper;
5360                 code += 4;
5361                 *(gpointer*)code = breakpoint_func_wrapper;
5362                 code += 4;
5363
5364                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 0);
5365                 ARM_STR_IMM (code, ARMREG_IP, read_ins->inst_basereg, read_ins->inst_offset);
5366                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 4);
5367                 ARM_STR_IMM (code, ARMREG_IP, ss_method_ins->inst_basereg, ss_method_ins->inst_offset);
5368                 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_LR, 8);
5369                 ARM_STR_IMM (code, ARMREG_IP, bp_method_ins->inst_basereg, bp_method_ins->inst_offset);
5370         }
5371
5372         cfg->code_len = code - cfg->native_code;
5373         g_assert (cfg->code_len < cfg->code_size);
5374         g_free (cinfo);
5375
5376         return code;
5377 }
5378
5379 void
5380 mono_arch_emit_epilog (MonoCompile *cfg)
5381 {
5382         MonoMethod *method = cfg->method;
5383         int pos, i, rot_amount;
5384         int max_epilog_size = 16 + 20*4;
5385         guint8 *code;
5386         CallInfo *cinfo;
5387
5388         if (cfg->method->save_lmf)
5389                 max_epilog_size += 128;
5390         
5391         if (mono_jit_trace_calls != NULL)
5392                 max_epilog_size += 50;
5393
5394         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
5395                 max_epilog_size += 50;
5396
5397         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5398                 cfg->code_size *= 2;
5399                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5400                 cfg->stat_code_reallocs++;
5401         }
5402
5403         /*
5404          * Keep in sync with OP_JMP
5405          */
5406         code = cfg->native_code + cfg->code_len;
5407
5408         if (mono_jit_trace_calls != NULL && mono_trace_eval (method)) {
5409                 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
5410         }
5411         pos = 0;
5412
5413         /* Load returned vtypes into registers if needed */
5414         cinfo = cfg->arch.cinfo;
5415         if (cinfo->ret.storage == RegTypeStructByVal) {
5416                 MonoInst *ins = cfg->ret;
5417
5418                 if (arm_is_imm12 (ins->inst_offset)) {
5419                         ARM_LDR_IMM (code, ARMREG_R0, ins->inst_basereg, ins->inst_offset);
5420                 } else {
5421                         code = mono_arm_emit_load_imm (code, ARMREG_LR, ins->inst_offset);
5422                         ARM_LDR_REG_REG (code, ARMREG_R0, ins->inst_basereg, ARMREG_LR);
5423                 }
5424         }
5425
5426         if (method->save_lmf) {
5427                 int lmf_offset, reg, sp_adj, regmask;
5428                 /* all but r0-r3, sp and pc */
5429                 pos += sizeof (MonoLMF) - (MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
5430                 lmf_offset = pos;
5431
5432                 code = emit_restore_lmf (cfg, code, cfg->stack_usage - lmf_offset);
5433
5434                 /* This points to r4 inside MonoLMF->iregs */
5435                 sp_adj = (sizeof (MonoLMF) - MONO_ARM_NUM_SAVED_REGS * sizeof (mgreg_t));
5436                 reg = ARMREG_R4;
5437                 regmask = 0x9ff0; /* restore lr to pc */
5438                 /* Skip caller saved registers not used by the method */
5439                 while (!(cfg->used_int_regs & (1 << reg)) && reg < ARMREG_FP) {
5440                         regmask &= ~(1 << reg);
5441                         sp_adj += 4;
5442                         reg ++;
5443                 }
5444                 /* point sp at the registers to restore: 10 is 14 -4, because we skip r0-r3 */
5445                 code = emit_big_add (code, ARMREG_SP, cfg->frame_reg, cfg->stack_usage - lmf_offset + sp_adj);
5446                 /* restore iregs */
5447                 ARM_POP (code, regmask); 
5448         } else {
5449                 if ((i = mono_arm_is_rotated_imm8 (cfg->stack_usage, &rot_amount)) >= 0) {
5450                         ARM_ADD_REG_IMM (code, ARMREG_SP, cfg->frame_reg, i, rot_amount);
5451                 } else {
5452                         code = mono_arm_emit_load_imm (code, ARMREG_IP, cfg->stack_usage);
5453                         ARM_ADD_REG_REG (code, ARMREG_SP, cfg->frame_reg, ARMREG_IP);
5454                 }
5455
5456                 if (iphone_abi) {
5457                         /* Restore saved gregs */
5458                         if (cfg->used_int_regs)
5459                                 ARM_POP (code, cfg->used_int_regs);
5460                         /* Restore saved r7, restore LR to PC */
5461                         ARM_POP (code, (1 << ARMREG_R7) | (1 << ARMREG_PC));
5462                 } else {
5463                         ARM_POP (code, cfg->used_int_regs | (1 << ARMREG_PC));
5464                 }
5465         }
5466
5467         cfg->code_len = code - cfg->native_code;
5468
5469         g_assert (cfg->code_len < cfg->code_size);
5470
5471 }
5472
5473 /* remove once throw_exception_by_name is eliminated */
5474 static int
5475 exception_id_by_name (const char *name)
5476 {
5477         if (strcmp (name, "IndexOutOfRangeException") == 0)
5478                 return MONO_EXC_INDEX_OUT_OF_RANGE;
5479         if (strcmp (name, "OverflowException") == 0)
5480                 return MONO_EXC_OVERFLOW;
5481         if (strcmp (name, "ArithmeticException") == 0)
5482                 return MONO_EXC_ARITHMETIC;
5483         if (strcmp (name, "DivideByZeroException") == 0)
5484                 return MONO_EXC_DIVIDE_BY_ZERO;
5485         if (strcmp (name, "InvalidCastException") == 0)
5486                 return MONO_EXC_INVALID_CAST;
5487         if (strcmp (name, "NullReferenceException") == 0)
5488                 return MONO_EXC_NULL_REF;
5489         if (strcmp (name, "ArrayTypeMismatchException") == 0)
5490                 return MONO_EXC_ARRAY_TYPE_MISMATCH;
5491         if (strcmp (name, "ArgumentException") == 0)
5492                 return MONO_EXC_ARGUMENT;
5493         g_error ("Unknown intrinsic exception %s\n", name);
5494         return -1;
5495 }
5496
5497 void
5498 mono_arch_emit_exceptions (MonoCompile *cfg)
5499 {
5500         MonoJumpInfo *patch_info;
5501         int i;
5502         guint8 *code;
5503         guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM];
5504         guint8 exc_throw_found [MONO_EXC_INTRINS_NUM];
5505         int max_epilog_size = 50;
5506
5507         for (i = 0; i < MONO_EXC_INTRINS_NUM; i++) {
5508                 exc_throw_pos [i] = NULL;
5509                 exc_throw_found [i] = 0;
5510         }
5511
5512         /* count the number of exception infos */
5513      
5514         /* 
5515          * make sure we have enough space for exceptions
5516          */
5517         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5518                 if (patch_info->type == MONO_PATCH_INFO_EXC) {
5519                         i = exception_id_by_name (patch_info->data.target);
5520                         if (!exc_throw_found [i]) {
5521                                 max_epilog_size += 32;
5522                                 exc_throw_found [i] = TRUE;
5523                         }
5524                 }
5525         }
5526
5527         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
5528                 cfg->code_size *= 2;
5529                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
5530                 cfg->stat_code_reallocs++;
5531         }
5532
5533         code = cfg->native_code + cfg->code_len;
5534
5535         /* add code to raise exceptions */
5536         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5537                 switch (patch_info->type) {
5538                 case MONO_PATCH_INFO_EXC: {
5539                         MonoClass *exc_class;
5540                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
5541
5542                         i = exception_id_by_name (patch_info->data.target);
5543                         if (exc_throw_pos [i]) {
5544                                 arm_patch (ip, exc_throw_pos [i]);
5545                                 patch_info->type = MONO_PATCH_INFO_NONE;
5546                                 break;
5547                         } else {
5548                                 exc_throw_pos [i] = code;
5549                         }
5550                         arm_patch (ip, code);
5551
5552                         exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
5553                         g_assert (exc_class);
5554
5555                         ARM_MOV_REG_REG (code, ARMREG_R1, ARMREG_LR);
5556                         ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
5557                         patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5558                         patch_info->data.name = "mono_arch_throw_corlib_exception";
5559                         patch_info->ip.i = code - cfg->native_code;
5560                         ARM_BL (code, 0);
5561                         *(guint32*)(gpointer)code = exc_class->type_token;
5562                         code += 4;
5563                         break;
5564                 }
5565                 default:
5566                         /* do nothing */
5567                         break;
5568                 }
5569         }
5570
5571         cfg->code_len = code - cfg->native_code;
5572
5573         g_assert (cfg->code_len < cfg->code_size);
5574
5575 }
5576
5577 #endif /* #ifndef DISABLE_JIT */
5578
5579 static gboolean tls_offset_inited = FALSE;
5580
5581 void
5582 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
5583 {
5584         if (!tls_offset_inited) {
5585                 tls_offset_inited = TRUE;
5586
5587                 lmf_tls_offset = mono_get_lmf_tls_offset ();
5588                 lmf_addr_tls_offset = mono_get_lmf_addr_tls_offset ();
5589         }
5590 }
5591
5592 void
5593 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
5594 {
5595 }
5596
5597 MonoInst*
5598 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
5599 {
5600         /* FIXME: */
5601         return NULL;
5602 }
5603
5604 gboolean
5605 mono_arch_print_tree (MonoInst *tree, int arity)
5606 {
5607         return 0;
5608 }
5609
5610 MonoInst*
5611 mono_arch_get_domain_intrinsic (MonoCompile* cfg)
5612 {
5613         return mono_get_domain_intrinsic (cfg);
5614 }
5615
5616 guint32
5617 mono_arch_get_patch_offset (guint8 *code)
5618 {
5619         /* OP_AOTCONST */
5620         return 8;
5621 }
5622
5623 void
5624 mono_arch_flush_register_windows (void)
5625 {
5626 }
5627
5628 #ifdef MONO_ARCH_HAVE_IMT
5629
5630 #ifndef DISABLE_JIT
5631
5632 void
5633 mono_arch_emit_imt_argument (MonoCompile *cfg, MonoCallInst *call, MonoInst *imt_arg)
5634 {
5635         if (cfg->compile_aot) {
5636                 int method_reg = mono_alloc_ireg (cfg);
5637                 MonoInst *ins;
5638
5639                 call->dynamic_imt_arg = TRUE;
5640
5641                 if (imt_arg) {
5642                         mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5643                 } else {
5644                         MONO_INST_NEW (cfg, ins, OP_AOTCONST);
5645                         ins->dreg = method_reg;
5646                         ins->inst_p0 = call->method;
5647                         ins->inst_c1 = MONO_PATCH_INFO_METHODCONST;
5648                         MONO_ADD_INS (cfg->cbb, ins);
5649
5650                         mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5651                 }
5652         } else if (cfg->generic_context || imt_arg || mono_use_llvm) {
5653
5654                 /* Always pass in a register for simplicity */
5655                 call->dynamic_imt_arg = TRUE;
5656
5657                 cfg->uses_rgctx_reg = TRUE;
5658
5659                 if (imt_arg) {
5660                         mono_call_inst_add_outarg_reg (cfg, call, imt_arg->dreg, ARMREG_V5, FALSE);
5661                 } else {
5662                         MonoInst *ins;
5663                         int method_reg = mono_alloc_preg (cfg);
5664
5665                         MONO_INST_NEW (cfg, ins, OP_PCONST);
5666                         ins->inst_p0 = call->method;
5667                         ins->dreg = method_reg;
5668                         MONO_ADD_INS (cfg->cbb, ins);
5669
5670                         mono_call_inst_add_outarg_reg (cfg, call, method_reg, ARMREG_V5, FALSE);
5671                 }
5672         }
5673 }
5674
5675 #endif /* DISABLE_JIT */
5676
5677 MonoMethod*
5678 mono_arch_find_imt_method (mgreg_t *regs, guint8 *code)
5679 {
5680         guint32 *code_ptr = (guint32*)code;
5681         code_ptr -= 2;
5682
5683         if (mono_use_llvm)
5684                 /* Passed in V5 */
5685                 return (MonoMethod*)regs [ARMREG_V5];
5686
5687         /* The IMT value is stored in the code stream right after the LDC instruction. */
5688         if (!IS_LDR_PC (code_ptr [0])) {
5689                 g_warning ("invalid code stream, instruction before IMT value is not a LDC in %s() (code %p value 0: 0x%x -1: 0x%x -2: 0x%x)", __FUNCTION__, code, code_ptr [2], code_ptr [1], code_ptr [0]);
5690                 g_assert (IS_LDR_PC (code_ptr [0]));
5691         }
5692         if (code_ptr [1] == 0)
5693                 /* This is AOTed code, the IMT method is in V5 */
5694                 return (MonoMethod*)regs [ARMREG_V5];
5695         else
5696                 return (MonoMethod*) code_ptr [1];
5697 }
5698
5699 MonoVTable*
5700 mono_arch_find_static_call_vtable (mgreg_t *regs, guint8 *code)
5701 {
5702         return (MonoVTable*) regs [MONO_ARCH_RGCTX_REG];
5703 }
5704
5705 #define ENABLE_WRONG_METHOD_CHECK 0
5706 #define BASE_SIZE (6 * 4)
5707 #define BSEARCH_ENTRY_SIZE (4 * 4)
5708 #define CMP_SIZE (3 * 4)
5709 #define BRANCH_SIZE (1 * 4)
5710 #define CALL_SIZE (2 * 4)
5711 #define WMC_SIZE (5 * 4)
5712 #define DISTANCE(A, B) (((gint32)(B)) - ((gint32)(A)))
5713
5714 static arminstr_t *
5715 arm_emit_value_and_patch_ldr (arminstr_t *code, arminstr_t *target, guint32 value)
5716 {
5717         guint32 delta = DISTANCE (target, code);
5718         delta -= 8;
5719         g_assert (delta >= 0 && delta <= 0xFFF);
5720         *target = *target | delta;
5721         *code = value;
5722         return code + 1;
5723 }
5724
5725 gpointer
5726 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
5727         gpointer fail_tramp)
5728 {
5729         int size, i, extra_space = 0;
5730         arminstr_t *code, *start, *vtable_target = NULL;
5731         gboolean large_offsets = FALSE;
5732         guint32 **constant_pool_starts;
5733
5734         size = BASE_SIZE;
5735         constant_pool_starts = g_new0 (guint32*, count);
5736
5737         for (i = 0; i < count; ++i) {
5738                 MonoIMTCheckItem *item = imt_entries [i];
5739                 if (item->is_equals) {
5740                         gboolean fail_case = !item->check_target_idx && fail_tramp;
5741
5742                         if (item->has_target_code || !arm_is_imm12 (DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]))) {
5743                                 item->chunk_size += 32;
5744                                 large_offsets = TRUE;
5745                         }
5746
5747                         if (item->check_target_idx || fail_case) {
5748                                 if (!item->compare_done || fail_case)
5749                                         item->chunk_size += CMP_SIZE;
5750                                 item->chunk_size += BRANCH_SIZE;
5751                         } else {
5752 #if ENABLE_WRONG_METHOD_CHECK
5753                                 item->chunk_size += WMC_SIZE;
5754 #endif
5755                         }
5756                         if (fail_case) {
5757                                 item->chunk_size += 16;
5758                                 large_offsets = TRUE;
5759                         }
5760                         item->chunk_size += CALL_SIZE;
5761                 } else {
5762                         item->chunk_size += BSEARCH_ENTRY_SIZE;
5763                         imt_entries [item->check_target_idx]->compare_done = TRUE;
5764                 }
5765                 size += item->chunk_size;
5766         }
5767
5768         if (large_offsets)
5769                 size += 4 * count; /* The ARM_ADD_REG_IMM to pop the stack */
5770
5771         if (fail_tramp)
5772                 code = mono_method_alloc_generic_virtual_thunk (domain, size);
5773         else
5774                 code = mono_domain_code_reserve (domain, size);
5775         start = code;
5776
5777 #if DEBUG_IMT
5778         printf ("building IMT thunk for class %s %s entries %d code size %d code at %p end %p vtable %p\n", vtable->klass->name_space, vtable->klass->name, count, size, start, ((guint8*)start) + size, vtable);
5779         for (i = 0; i < count; ++i) {
5780                 MonoIMTCheckItem *item = imt_entries [i];
5781                 printf ("method %d (%p) %s vtable slot %p is_equals %d chunk size %d\n", i, item->key, item->key->name, &vtable->vtable [item->value.vtable_slot], item->is_equals, item->chunk_size);
5782         }
5783 #endif
5784
5785         if (large_offsets)
5786                 ARM_PUSH4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5787         else
5788                 ARM_PUSH2 (code, ARMREG_R0, ARMREG_R1);
5789         ARM_LDR_IMM (code, ARMREG_R0, ARMREG_LR, -4);
5790         vtable_target = code;
5791         ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
5792
5793         if (mono_use_llvm) {
5794                 /* LLVM always passes the IMT method in R5 */
5795                 ARM_MOV_REG_REG (code, ARMREG_R0, ARMREG_V5);
5796         } else {
5797                 /* R0 == 0 means we are called from AOT code. In this case, V5 contains the IMT method */
5798                 ARM_CMP_REG_IMM8 (code, ARMREG_R0, 0);
5799                 ARM_MOV_REG_REG_COND (code, ARMREG_R0, ARMREG_V5, ARMCOND_EQ);
5800         }
5801
5802         for (i = 0; i < count; ++i) {
5803                 MonoIMTCheckItem *item = imt_entries [i];
5804                 arminstr_t *imt_method = NULL, *vtable_offset_ins = NULL, *target_code_ins = NULL;
5805                 gint32 vtable_offset;
5806
5807                 item->code_target = (guint8*)code;
5808
5809                 if (item->is_equals) {
5810                         gboolean fail_case = !item->check_target_idx && fail_tramp;
5811
5812                         if (item->check_target_idx || fail_case) {
5813                                 if (!item->compare_done || fail_case) {
5814                                         imt_method = code;
5815                                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5816                                         ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5817                                 }
5818                                 item->jmp_code = (guint8*)code;
5819                                 ARM_B_COND (code, ARMCOND_NE, 0);
5820                         } else {
5821                                 /*Enable the commented code to assert on wrong method*/
5822 #if ENABLE_WRONG_METHOD_CHECK
5823                                 imt_method = code;
5824                                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5825                                 ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5826                                 ARM_B_COND (code, ARMCOND_NE, 1);
5827
5828                                 ARM_DBRK (code);
5829 #endif
5830                         }
5831
5832                         if (item->has_target_code) {
5833                                 target_code_ins = code;
5834                                 /* Load target address */
5835                                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5836                                 /* Save it to the fourth slot */
5837                                 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5838                                 /* Restore registers and branch */
5839                                 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5840                                 
5841                                 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)item->value.target_code);
5842                         } else {
5843                                 vtable_offset = DISTANCE (vtable, &vtable->vtable[item->value.vtable_slot]);
5844                                 if (!arm_is_imm12 (vtable_offset)) {
5845                                         /* 
5846                                          * We need to branch to a computed address but we don't have
5847                                          * a free register to store it, since IP must contain the 
5848                                          * vtable address. So we push the two values to the stack, and
5849                                          * load them both using LDM.
5850                                          */
5851                                         /* Compute target address */
5852                                         vtable_offset_ins = code;
5853                                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5854                                         ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_IP, ARMREG_R1);
5855                                         /* Save it to the fourth slot */
5856                                         ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5857                                         /* Restore registers and branch */
5858                                         ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5859                                 
5860                                         code = arm_emit_value_and_patch_ldr (code, vtable_offset_ins, vtable_offset);
5861                                 } else {
5862                                         ARM_POP2 (code, ARMREG_R0, ARMREG_R1);
5863                                         if (large_offsets)
5864                                                 ARM_ADD_REG_IMM8 (code, ARMREG_SP, ARMREG_SP, 2 * sizeof (gpointer));
5865                                         ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, vtable_offset);
5866                                 }
5867                         }
5868
5869                         if (fail_case) {
5870                                 arm_patch (item->jmp_code, (guchar*)code);
5871
5872                                 target_code_ins = code;
5873                                 /* Load target address */
5874                                 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5875                                 /* Save it to the fourth slot */
5876                                 ARM_STR_IMM (code, ARMREG_R1, ARMREG_SP, 3 * sizeof (gpointer));
5877                                 /* Restore registers and branch */
5878                                 ARM_POP4 (code, ARMREG_R0, ARMREG_R1, ARMREG_IP, ARMREG_PC);
5879                                 
5880                                 code = arm_emit_value_and_patch_ldr (code, target_code_ins, (gsize)fail_tramp);
5881                                 item->jmp_code = NULL;
5882                         }
5883
5884                         if (imt_method)
5885                                 code = arm_emit_value_and_patch_ldr (code, imt_method, (guint32)item->key);
5886
5887                         /*must emit after unconditional branch*/
5888                         if (vtable_target) {
5889                                 code = arm_emit_value_and_patch_ldr (code, vtable_target, (guint32)vtable);
5890                                 item->chunk_size += 4;
5891                                 vtable_target = NULL;
5892                         }
5893
5894                         /*We reserve the space for bsearch IMT values after the first entry with an absolute jump*/
5895                         constant_pool_starts [i] = code;
5896                         if (extra_space) {
5897                                 code += extra_space;
5898                                 extra_space = 0;
5899                         }
5900                 } else {
5901                         ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 0);
5902                         ARM_CMP_REG_REG (code, ARMREG_R0, ARMREG_R1);
5903
5904                         item->jmp_code = (guint8*)code;
5905                         ARM_B_COND (code, ARMCOND_GE, 0);
5906                         ++extra_space;
5907                 }
5908         }
5909
5910         for (i = 0; i < count; ++i) {
5911                 MonoIMTCheckItem *item = imt_entries [i];
5912                 if (item->jmp_code) {
5913                         if (item->check_target_idx)
5914                                 arm_patch (item->jmp_code, imt_entries [item->check_target_idx]->code_target);
5915                 }
5916                 if (i > 0 && item->is_equals) {
5917                         int j;
5918                         arminstr_t *space_start = constant_pool_starts [i];
5919                         for (j = i - 1; j >= 0 && !imt_entries [j]->is_equals; --j) {
5920                                 space_start = arm_emit_value_and_patch_ldr (space_start, (arminstr_t*)imt_entries [j]->code_target, (guint32)imt_entries [j]->key);
5921                         }
5922                 }
5923         }
5924
5925 #if DEBUG_IMT
5926         {
5927                 char *buff = g_strdup_printf ("thunk_for_class_%s_%s_entries_%d", vtable->klass->name_space, vtable->klass->name, count);
5928                 mono_disassemble_code (NULL, (guint8*)start, size, buff);
5929                 g_free (buff);
5930         }
5931 #endif
5932
5933         g_free (constant_pool_starts);
5934
5935         mono_arch_flush_icache ((guint8*)start, size);
5936         mono_stats.imt_thunks_size += code - start;
5937
5938         g_assert (DISTANCE (start, code) <= size);
5939         return start;
5940 }
5941
5942 #endif
5943
5944 mgreg_t
5945 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
5946 {
5947         if (reg == ARMREG_SP)
5948                 return ctx->esp;
5949         else
5950                 return ctx->regs [reg];
5951 }
5952
5953 void
5954 mono_arch_context_set_int_reg (MonoContext *ctx, int reg, mgreg_t val)
5955 {
5956         if (reg == ARMREG_SP)
5957                 ctx->esp = val;
5958         else
5959                 ctx->regs [reg] = val;
5960 }
5961
5962 /*
5963  * mono_arch_get_trampolines:
5964  *
5965  *   Return a list of MonoTrampInfo structures describing arch specific trampolines
5966  * for AOT.
5967  */
5968 GSList *
5969 mono_arch_get_trampolines (gboolean aot)
5970 {
5971         return mono_arm_get_exception_trampolines (aot);
5972 }
5973
5974 /*
5975  * mono_arch_set_breakpoint:
5976  *
5977  *   Set a breakpoint at the native code corresponding to JI at NATIVE_OFFSET.
5978  * The location should contain code emitted by OP_SEQ_POINT.
5979  */
5980 void
5981 mono_arch_set_breakpoint (MonoJitInfo *ji, guint8 *ip)
5982 {
5983         guint8 *code = ip;
5984         guint32 native_offset = ip - (guint8*)ji->code_start;
5985         MonoDebugOptions *opt = mini_get_debug_options ();
5986
5987         if (opt->soft_breakpoints) {
5988                 g_assert (!ji->from_aot);
5989                 code += 4;
5990                 ARM_BLX_REG (code, ARMREG_LR);
5991                 mono_arch_flush_icache (code - 4, 4);
5992         } else if (ji->from_aot) {
5993                 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
5994
5995                 g_assert (native_offset % 4 == 0);
5996                 g_assert (info->bp_addrs [native_offset / 4] == 0);
5997                 info->bp_addrs [native_offset / 4] = bp_trigger_page;
5998         } else {
5999                 int dreg = ARMREG_LR;
6000
6001                 /* Read from another trigger page */
6002                 ARM_LDR_IMM (code, dreg, ARMREG_PC, 0);
6003                 ARM_B (code, 0);
6004                 *(int*)code = (int)bp_trigger_page;
6005                 code += 4;
6006                 ARM_LDR_IMM (code, dreg, dreg, 0);
6007
6008                 mono_arch_flush_icache (code - 16, 16);
6009
6010 #if 0
6011                 /* This is currently implemented by emitting an SWI instruction, which 
6012                  * qemu/linux seems to convert to a SIGILL.
6013                  */
6014                 *(int*)code = (0xef << 24) | 8;
6015                 code += 4;
6016                 mono_arch_flush_icache (code - 4, 4);
6017 #endif
6018         }
6019 }
6020
6021 /*
6022  * mono_arch_clear_breakpoint:
6023  *
6024  *   Clear the breakpoint at IP.
6025  */
6026 void
6027 mono_arch_clear_breakpoint (MonoJitInfo *ji, guint8 *ip)
6028 {
6029         MonoDebugOptions *opt = mini_get_debug_options ();
6030         guint8 *code = ip;
6031         int i;
6032
6033         if (opt->soft_breakpoints) {
6034                 g_assert (!ji->from_aot);
6035                 code += 4;
6036                 ARM_NOP (code);
6037                 mono_arch_flush_icache (code - 4, 4);
6038         } else if (ji->from_aot) {
6039                 guint32 native_offset = ip - (guint8*)ji->code_start;
6040                 SeqPointInfo *info = mono_arch_get_seq_point_info (mono_domain_get (), ji->code_start);
6041
6042                 g_assert (native_offset % 4 == 0);
6043                 g_assert (info->bp_addrs [native_offset / 4] == bp_trigger_page);
6044                 info->bp_addrs [native_offset / 4] = 0;
6045         } else {
6046                 for (i = 0; i < 4; ++i)
6047                         ARM_NOP (code);
6048
6049                 mono_arch_flush_icache (ip, code - ip);
6050         }
6051 }
6052         
6053 /*
6054  * mono_arch_start_single_stepping:
6055  *
6056  *   Start single stepping.
6057  */
6058 void
6059 mono_arch_start_single_stepping (void)
6060 {
6061         if (ss_trigger_page)
6062                 mono_mprotect (ss_trigger_page, mono_pagesize (), 0);
6063         else
6064                 ss_trigger_var = 1;
6065 }
6066         
6067 /*
6068  * mono_arch_stop_single_stepping:
6069  *
6070  *   Stop single stepping.
6071  */
6072 void
6073 mono_arch_stop_single_stepping (void)
6074 {
6075         if (ss_trigger_page)
6076                 mono_mprotect (ss_trigger_page, mono_pagesize (), MONO_MMAP_READ);
6077         else
6078                 ss_trigger_var = 0;
6079 }
6080
6081 #if __APPLE__
6082 #define DBG_SIGNAL SIGBUS
6083 #else
6084 #define DBG_SIGNAL SIGSEGV
6085 #endif
6086
6087 /*
6088  * mono_arch_is_single_step_event:
6089  *
6090  *   Return whenever the machine state in SIGCTX corresponds to a single
6091  * step event.
6092  */
6093 gboolean
6094 mono_arch_is_single_step_event (void *info, void *sigctx)
6095 {
6096         siginfo_t *sinfo = info;
6097
6098         if (!ss_trigger_page)
6099                 return FALSE;
6100
6101         /* Sometimes the address is off by 4 */
6102         if (sinfo->si_addr >= ss_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)ss_trigger_page + 128)
6103                 return TRUE;
6104         else
6105                 return FALSE;
6106 }
6107
6108 /*
6109  * mono_arch_is_breakpoint_event:
6110  *
6111  *   Return whenever the machine state in SIGCTX corresponds to a breakpoint event.
6112  */
6113 gboolean
6114 mono_arch_is_breakpoint_event (void *info, void *sigctx)
6115 {
6116         siginfo_t *sinfo = info;
6117
6118         if (!ss_trigger_page)
6119                 return FALSE;
6120
6121         if (sinfo->si_signo == DBG_SIGNAL) {
6122                 /* Sometimes the address is off by 4 */
6123                 if (sinfo->si_addr >= bp_trigger_page && (guint8*)sinfo->si_addr <= (guint8*)bp_trigger_page + 128)
6124                         return TRUE;
6125                 else
6126                         return FALSE;
6127         } else {
6128                 return FALSE;
6129         }
6130 }
6131
6132 guint8*
6133 mono_arch_get_ip_for_breakpoint (MonoJitInfo *ji, MonoContext *ctx)
6134 {
6135         guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
6136
6137         if (ji->from_aot)
6138                 ip -= 6 * 4;
6139         else if (mini_get_debug_options ()->soft_breakpoints)
6140                 ip -= 4;
6141         else
6142                 ip -= 12;
6143
6144         return ip;
6145 }
6146
6147 guint8*
6148 mono_arch_get_ip_for_single_step (MonoJitInfo *ji, MonoContext *ctx)
6149 {
6150         guint8 *ip = MONO_CONTEXT_GET_IP (ctx);
6151
6152         ip += 4;
6153
6154         return ip;
6155 }
6156
6157 /*
6158  * mono_arch_skip_breakpoint:
6159  *
6160  *   See mini-amd64.c for docs.
6161  */
6162 void
6163 mono_arch_skip_breakpoint (MonoContext *ctx)
6164 {
6165         MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
6166 }
6167
6168 /*
6169  * mono_arch_skip_single_step:
6170  *
6171  *   See mini-amd64.c for docs.
6172  */
6173 void
6174 mono_arch_skip_single_step (MonoContext *ctx)
6175 {
6176         MONO_CONTEXT_SET_IP (ctx, (guint8*)MONO_CONTEXT_GET_IP (ctx) + 4);
6177 }
6178
6179 /*
6180  * mono_arch_get_seq_point_info:
6181  *
6182  *   See mini-amd64.c for docs.
6183  */
6184 gpointer
6185 mono_arch_get_seq_point_info (MonoDomain *domain, guint8 *code)
6186 {
6187         SeqPointInfo *info;
6188         MonoJitInfo *ji;
6189
6190         // FIXME: Add a free function
6191
6192         mono_domain_lock (domain);
6193         info = g_hash_table_lookup (domain_jit_info (domain)->arch_seq_points, 
6194                                                                 code);
6195         mono_domain_unlock (domain);
6196
6197         if (!info) {
6198                 ji = mono_jit_info_table_find (domain, (char*)code);
6199                 g_assert (ji);
6200
6201                 info = g_malloc0 (sizeof (SeqPointInfo) + ji->code_size);
6202
6203                 info->ss_trigger_page = ss_trigger_page;
6204                 info->bp_trigger_page = bp_trigger_page;
6205
6206                 mono_domain_lock (domain);
6207                 g_hash_table_insert (domain_jit_info (domain)->arch_seq_points,
6208                                                          code, info);
6209                 mono_domain_unlock (domain);
6210         }
6211
6212         return info;
6213 }
6214
6215 /*
6216  * mono_arch_set_target:
6217  *
6218  *   Set the target architecture the JIT backend should generate code for, in the form
6219  * of a GNU target triplet. Only used in AOT mode.
6220  */
6221 void
6222 mono_arch_set_target (char *mtriple)
6223 {
6224         /* The GNU target triple format is not very well documented */
6225         if (strstr (mtriple, "armv7")) {
6226                 v6_supported = TRUE;
6227                 v7_supported = TRUE;
6228         }
6229         if (strstr (mtriple, "armv6")) {
6230                 v6_supported = TRUE;
6231         }
6232         if (strstr (mtriple, "darwin")) {
6233                 v5_supported = TRUE;
6234                 thumb_supported = TRUE;
6235                 darwin = TRUE;
6236                 iphone_abi = TRUE;
6237         }
6238         if (strstr (mtriple, "gnueabi"))
6239                 eabi_supported = TRUE;
6240 }