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