2008-10-21 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / mini.c
1 /*
2  * mini.c: The new Mono code generator.
3  *
4  * Author:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *
8  * (C) 2002 Ximian, Inc.
9  */
10
11 #include <config.h>
12 #include <signal.h>
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <math.h>
17 #ifdef HAVE_SYS_TIME_H
18 #include <sys/time.h>
19 #endif
20
21 #ifdef PLATFORM_MACOSX
22 #include <mach/mach.h>
23 #include <mach/mach_error.h>
24 #include <mach/exception.h>
25 #include <mach/task.h>
26 #include <pthread.h>
27 #endif
28
29 #ifdef HAVE_VALGRIND_MEMCHECK_H
30 #include <valgrind/memcheck.h>
31 #endif
32
33 #include <mono/metadata/assembly.h>
34 #include <mono/metadata/loader.h>
35 #include <mono/metadata/tabledefs.h>
36 #include <mono/metadata/class.h>
37 #include <mono/metadata/object.h>
38 #include <mono/metadata/exception.h>
39 #include <mono/metadata/opcodes.h>
40 #include <mono/metadata/mono-endian.h>
41 #include <mono/metadata/tokentype.h>
42 #include <mono/metadata/tabledefs.h>
43 #include <mono/metadata/threads.h>
44 #include <mono/metadata/marshal.h>
45 #include <mono/metadata/socket-io.h>
46 #include <mono/metadata/appdomain.h>
47 #include <mono/metadata/debug-helpers.h>
48 #include <mono/io-layer/io-layer.h>
49 #include "mono/metadata/profiler.h"
50 #include <mono/metadata/profiler-private.h>
51 #include <mono/metadata/mono-config.h>
52 #include <mono/metadata/environment.h>
53 #include <mono/metadata/mono-debug.h>
54 #include <mono/metadata/monitor.h>
55 #include <mono/metadata/gc-internal.h>
56 #include <mono/metadata/security-manager.h>
57 #include <mono/metadata/threads-types.h>
58 #include <mono/metadata/security-core-clr.h>
59 #include <mono/metadata/verify.h>
60 #include <mono/metadata/verify-internals.h>
61 #include <mono/metadata/mempool-internals.h>
62 #include <mono/metadata/attach.h>
63 #include <mono/utils/mono-math.h>
64 #include <mono/utils/mono-compiler.h>
65 #include <mono/utils/mono-counters.h>
66 #include <mono/utils/mono-logger.h>
67 #include <mono/utils/mono-mmap.h>
68 #include <mono/utils/dtrace.h>
69
70 #include "mini.h"
71 #include <string.h>
72 #include <ctype.h>
73 #include "inssel.h"
74 #include "trace.h"
75
76 #include "jit-icalls.h"
77
78 #include "aliasing.h"
79
80 #include "debug-mini.h"
81
82 #define BRANCH_COST 100
83 #define INLINE_LENGTH_LIMIT 20
84 #define INLINE_FAILURE do {\
85                 if ((cfg->method != method) && (method->wrapper_type == MONO_WRAPPER_NONE))\
86                         goto inline_failure;\
87         } while (0)
88 #define CHECK_CFG_EXCEPTION do {\
89                 if (cfg->exception_type != MONO_EXCEPTION_NONE)\
90                         goto exception_exit;\
91         } while (0)
92 #define METHOD_ACCESS_FAILURE do {      \
93                 char *method_fname = mono_method_full_name (method, TRUE);      \
94                 char *cil_method_fname = mono_method_full_name (cil_method, TRUE);      \
95                 cfg->exception_type = MONO_EXCEPTION_METHOD_ACCESS;     \
96                 cfg->exception_message = g_strdup_printf ("Method `%s' is inaccessible from method `%s'\n", cil_method_fname, method_fname);    \
97                 g_free (method_fname);  \
98                 g_free (cil_method_fname);      \
99                 goto exception_exit;    \
100         } while (0)
101 #define FIELD_ACCESS_FAILURE do {       \
102                 char *method_fname = mono_method_full_name (method, TRUE);      \
103                 char *field_fname = mono_field_full_name (field);       \
104                 cfg->exception_type = MONO_EXCEPTION_FIELD_ACCESS;      \
105                 cfg->exception_message = g_strdup_printf ("Field `%s' is inaccessible from method `%s'\n", field_fname, method_fname);  \
106                 g_free (method_fname);  \
107                 g_free (field_fname);   \
108                 goto exception_exit;    \
109         } while (0)
110 #define GENERIC_SHARING_FAILURE(opcode) do {            \
111                 if (cfg->generic_sharing_context) {     \
112             if (cfg->verbose_level > 1) \
113                             printf ("sharing failed for method %s.%s.%s/%d opcode %s line %d\n", method->klass->name_space, method->klass->name, method->name, method->signature->param_count, mono_opcode_name ((opcode)), __LINE__); \
114                         cfg->exception_type = MONO_EXCEPTION_GENERIC_SHARING_FAILED;    \
115                         goto exception_exit;    \
116                 }                       \
117         } while (0)
118 #define GET_RGCTX(rgctx, context_used) do {                                             \
119                 MonoInst *this = NULL;                                  \
120                 g_assert (context_used);                                \
121                 if (!(method->flags & METHOD_ATTRIBUTE_STATIC) &&       \
122                                 !((context_used) & MONO_GENERIC_CONTEXT_USED_METHOD) && \
123                                 !method->klass->valuetype)              \
124                         NEW_ARGLOAD (cfg, this, 0);                     \
125                 (rgctx) = get_runtime_generic_context (cfg, method, (context_used), this, ip); \
126         } while (0)
127
128
129 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
130
131 static void setup_stat_profiler (void);
132 gboolean  mono_arch_print_tree(MonoInst *tree, int arity);
133 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
134 static gpointer mono_jit_compile_method (MonoMethod *method);
135 inline static int mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip);
136
137 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, 
138                           const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier);
139
140 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
141
142 int mono_method_to_ir2 (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
143                    MonoInst *return_var, GList *dont_inline, MonoInst **inline_args, 
144                    guint inline_offset, gboolean is_virtual_call);
145
146 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
147                    int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args, 
148                    guint inline_offset, gboolean is_virtual_call);
149
150 #ifdef MONO_ARCH_SOFT_FLOAT
151 static void
152 handle_store_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, MonoInst *val, const unsigned char *ip);
153 #endif
154
155 /* helper methods signature */
156 /* FIXME: Make these static again */
157 MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
158 MonoMethodSignature *helper_sig_domain_get = NULL;
159 MonoMethodSignature *helper_sig_generic_class_init_trampoline = NULL;
160 MonoMethodSignature *helper_sig_rgctx_lazy_fetch_trampoline = NULL;
161
162 static guint32 default_opt = 0;
163 static gboolean default_opt_set = FALSE;
164
165 guint32 mono_jit_tls_id = -1;
166
167 #ifdef HAVE_KW_THREAD
168 static __thread gpointer mono_jit_tls MONO_TLS_FAST;
169 #endif
170
171 MonoTraceSpec *mono_jit_trace_calls = NULL;
172 gboolean mono_break_on_exc = FALSE;
173 gboolean mono_compile_aot = FALSE;
174 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
175 gboolean mono_aot_only = FALSE;
176 /* Whenever to use IMT */
177 #ifdef MONO_ARCH_HAVE_IMT
178 gboolean mono_use_imt = TRUE;
179 #else
180 gboolean mono_use_imt = FALSE;
181 #endif
182 MonoMethodDesc *mono_inject_async_exc_method = NULL;
183 int mono_inject_async_exc_pos;
184 MonoMethodDesc *mono_break_at_bb_method = NULL;
185 int mono_break_at_bb_bb_num;
186 gboolean mono_do_x86_stack_align = TRUE;
187
188 static int mini_verbose = 0;
189
190 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
191 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
192 static CRITICAL_SECTION jit_mutex;
193
194 static MonoCodeManager *global_codeman = NULL;
195
196 /* FIXME: Make this static again */
197 GHashTable *jit_icall_name_hash = NULL;
198
199 static MonoDebugOptions debug_options;
200
201 #ifdef VALGRIND_JIT_REGISTER_MAP
202 static int valgrind_register = 0;
203 #endif
204
205 /*
206  * Table written to by the debugger with a 1-based index into the
207  * mono_breakpoint_info table, which contains changes made to
208  * the JIT instructions by the debugger.
209  */
210 gssize
211 mono_breakpoint_info_index [MONO_BREAKPOINT_ARRAY_SIZE];
212
213 /* Whenever to check for pending exceptions in managed-to-native wrappers */
214 gboolean check_for_pending_exc = TRUE;
215
216 /* Whenever to disable passing/returning small valuetypes in registers for managed methods */
217 gboolean disable_vtypes_in_regs = FALSE;
218
219 gboolean mono_dont_free_global_codeman;
220
221 #ifdef DISABLE_JIT
222 /* Define this here, since many files reference it */
223 const guint8 mono_burg_arity [MBMAX_OPCODES] = {
224 };
225 #endif
226
227 gboolean
228 mono_running_on_valgrind (void)
229 {
230 #ifdef HAVE_VALGRIND_MEMCHECK_H
231                 if (RUNNING_ON_VALGRIND){
232 #ifdef VALGRIND_JIT_REGISTER_MAP
233                         valgrind_register = TRUE;
234 #endif
235                         return TRUE;
236                 } else
237                         return FALSE;
238 #else
239                 return FALSE;
240 #endif
241 }
242
243 typedef struct {
244         void *ip;
245         MonoMethod *method;
246 } FindTrampUserData;
247
248 static void
249 find_tramp (gpointer key, gpointer value, gpointer user_data)
250 {
251         FindTrampUserData *ud = (FindTrampUserData*)user_data;
252
253         if (value == ud->ip)
254                 ud->method = (MonoMethod*)key;
255 }
256
257 /* debug function */
258 G_GNUC_UNUSED static char*
259 get_method_from_ip (void *ip)
260 {
261         MonoJitInfo *ji;
262         char *method;
263         char *res;
264         MonoDomain *domain = mono_domain_get ();
265         MonoDebugSourceLocation *location;
266         FindTrampUserData user_data;
267         
268         ji = mono_jit_info_table_find (domain, ip);
269         if (!ji) {
270                 user_data.ip = ip;
271                 user_data.method = NULL;
272                 mono_domain_lock (domain);
273                 g_hash_table_foreach (domain_jit_info (domain)->jit_trampoline_hash, find_tramp, &user_data);
274                 mono_domain_unlock (domain);
275                 if (user_data.method) {
276                         char *mname = mono_method_full_name (user_data.method, TRUE);
277                         res = g_strdup_printf ("<%p - JIT trampoline for %s>", ip, mname);
278                         g_free (mname);
279                         return res;
280                 }
281                 else
282                         return NULL;
283         }
284         method = mono_method_full_name (ji->method, TRUE);
285         /* FIXME: unused ? */
286         location = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
287
288         res = g_strdup_printf (" %s + 0x%x (%p %p) [%p - %s]", method, (int)((char*)ip - (char*)ji->code_start), ji->code_start, (char*)ji->code_start + ji->code_size, domain, domain->friendly_name);
289
290         mono_debug_free_source_location (location);
291         g_free (method);
292
293         return res;
294 }
295
296 /** 
297  * mono_pmip:
298  * @ip: an instruction pointer address
299  *
300  * This method is used from a debugger to get the name of the
301  * method at address @ip.   This routine is typically invoked from
302  * a debugger like this:
303  *
304  * (gdb) print mono_pmip ($pc)
305  *
306  * Returns: the name of the method at address @ip.
307  */
308 G_GNUC_UNUSED char *
309 mono_pmip (void *ip)
310 {
311         return get_method_from_ip (ip);
312 }
313
314 /** 
315  * mono_print_method_from_ip
316  * @ip: an instruction pointer address
317  *
318  * This method is used from a debugger to get the name of the
319  * method at address @ip.
320  *
321  * This prints the name of the method at address @ip in the standard
322  * output.  Unlike mono_pmip which returns a string, this routine
323  * prints the value on the standard output. 
324  */
325 void
326 mono_print_method_from_ip (void *ip)
327 {
328         MonoJitInfo *ji;
329         char *method;
330         MonoDebugSourceLocation *source;
331         MonoDomain *domain = mono_domain_get ();
332         FindTrampUserData user_data;
333         
334         ji = mono_jit_info_table_find (domain, ip);
335         if (!ji) {
336                 user_data.ip = ip;
337                 user_data.method = NULL;
338                 mono_domain_lock (domain);
339                 g_hash_table_foreach (domain_jit_info (domain)->jit_trampoline_hash, find_tramp, &user_data);
340                 mono_domain_unlock (domain);
341                 if (user_data.method) {
342                         char *mname = mono_method_full_name (user_data.method, TRUE);
343                         printf ("IP %p is a JIT trampoline for %s\n", ip, mname);
344                         g_free (mname);
345                 }
346                 else
347                         g_print ("No method at %p\n", ip);
348                 return;
349         }
350         method = mono_method_full_name (ji->method, TRUE);
351         source = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
352
353         g_print ("IP %p at offset 0x%x of method %s (%p %p)[domain %p - %s]\n", ip, (int)((char*)ip - (char*)ji->code_start), method, ji->code_start, (char*)ji->code_start + ji->code_size, domain, domain->friendly_name);
354
355         if (source)
356                 g_print ("%s:%d\n", source->source_file, source->row);
357
358         mono_debug_free_source_location (source);
359         g_free (method);
360 }
361         
362 /* 
363  * mono_method_same_domain:
364  *
365  * Determine whenever two compiled methods are in the same domain, thus
366  * the address of the callee can be embedded in the caller.
367  */
368 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
369 {
370         if (!caller || !callee)
371                 return FALSE;
372
373         /*
374          * If the call was made from domain-neutral to domain-specific 
375          * code, we can't patch the call site.
376          */
377         if (caller->domain_neutral && !callee->domain_neutral)
378                 return FALSE;
379
380         if ((caller->method->klass == mono_defaults.appdomain_class) &&
381                 (strstr (caller->method->name, "InvokeInDomain"))) {
382                  /* The InvokeInDomain methods change the current appdomain */
383                 return FALSE;
384         }
385
386         return TRUE;
387 }
388
389 /*
390  * mono_global_codeman_reserve:
391  *
392  *  Allocate code memory from the global code manager.
393  */
394 void *mono_global_codeman_reserve (int size)
395 {
396         void *ptr;
397
398         if (mono_aot_only)
399                 g_error ("Attempting to allocate from the global code manager while running with --aot-only.\n");
400
401         if (!global_codeman) {
402                 /* This can happen during startup */
403                 global_codeman = mono_code_manager_new ();
404                 return mono_code_manager_reserve (global_codeman, size);
405         }
406         else {
407                 mono_jit_lock ();
408                 ptr = mono_code_manager_reserve (global_codeman, size);
409                 mono_jit_unlock ();
410                 return ptr;
411         }
412 }
413
414 MonoJumpInfoToken *
415 mono_jump_info_token_new2 (MonoMemPool *mp, MonoImage *image, guint32 token, MonoGenericContext *context)
416 {
417         MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
418         res->image = image;
419         res->token = token;
420         res->has_context = context != NULL;
421         if (context)
422                 memcpy (&res->context, context, sizeof (MonoGenericContext));
423
424         return res;
425 }
426
427 MonoJumpInfoToken *
428 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
429 {
430         return mono_jump_info_token_new2 (mp, image, token, NULL);
431 }
432
433 #define MONO_INIT_VARINFO(vi,id) do { \
434         (vi)->range.first_use.pos.bid = 0xffff; \
435         (vi)->reg = -1; \
436         (vi)->idx = (id); \
437 } while (0)
438
439 //#define UNVERIFIED do { G_BREAKPOINT (); goto unverified; } while (0)
440 #define UNVERIFIED do { if (debug_options.break_on_unverified) G_BREAKPOINT (); else goto unverified; } while (0)
441
442 /*
443  * Basic blocks have two numeric identifiers:
444  * dfn: Depth First Number
445  * block_num: unique ID assigned at bblock creation
446  */
447 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
448 #define ADD_BBLOCK(cfg,b) do {  \
449                 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b);   \
450                 (b)->block_num = cfg->num_bblocks++;    \
451                 (b)->real_offset = real_offset; \
452         } while (0)
453
454 #define GET_BBLOCK(cfg,tblock,ip) do {  \
455                 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
456                 if (!(tblock)) {        \
457                         if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
458                         (tblock) = NEW_BBLOCK (cfg);    \
459                         (tblock)->cil_code = (ip);      \
460                         ADD_BBLOCK (cfg, (tblock));     \
461                 } \
462         } while (0)
463
464 #define CHECK_BBLOCK(target,ip,tblock) do {     \
465                 if ((target) < (ip) && !(tblock)->code) {       \
466                         bb_recheck = g_list_prepend (bb_recheck, (tblock));     \
467                         if (cfg->verbose_level > 2) g_print ("queued block %d for check at IL%04x from IL%04x\n", (tblock)->block_num, (int)((target) - header->code), (int)((ip) - header->code));     \
468                 }       \
469         } while (0)
470
471 #define NEW_ICONST(cfg,dest,val) do {   \
472                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
473                 (dest)->opcode = OP_ICONST;     \
474                 (dest)->inst_c0 = (val);        \
475                 (dest)->type = STACK_I4;        \
476         } while (0)
477
478 #define NEW_PCONST(cfg,dest,val) do {   \
479                 MONO_INST_NEW ((cfg), (dest), OP_PCONST);       \
480                 (dest)->inst_p0 = (val);        \
481                 (dest)->type = STACK_PTR;       \
482         } while (0)
483
484
485 #ifdef MONO_ARCH_NEED_GOT_VAR
486
487 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do {   \
488                 MONO_INST_NEW ((cfg), (dest), OP_PATCH_INFO);   \
489                 (dest)->inst_left = (gpointer)(el1);    \
490                 (dest)->inst_right = (gpointer)(el2);   \
491         } while (0)
492
493 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {                     \
494                 MONO_INST_NEW ((cfg), (dest), OP_NOP); \
495                 (dest)->opcode = cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST; \
496                 if (cfg->compile_aot) {                                 \
497                         MonoInst *group, *got_var, *got_loc;            \
498                         got_loc = mono_get_got_var (cfg);               \
499                         NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
500                         NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
501                         (dest)->inst_p0 = got_var;                      \
502                         (dest)->inst_p1 = group;                        \
503                 } else {                                                \
504                         (dest)->inst_p0 = (cons);                       \
505                         (dest)->inst_i1 = (gpointer)(patch_type);       \
506                 }                                                       \
507                 (dest)->type = STACK_PTR;                               \
508         } while (0)
509
510 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
511                 MonoInst *group, *got_var, *got_loc;                    \
512                 MONO_INST_NEW ((cfg), (dest), OP_GOT_ENTRY); \
513                 got_loc = mono_get_got_var (cfg);                       \
514                 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0);        \
515                 NEW_PATCH_INFO ((cfg), group, NULL, patch_type);        \
516                 group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
517                 (dest)->inst_p0 = got_var;                              \
518                 (dest)->inst_p1 = group;                                \
519                 (dest)->type = (stack_type);                    \
520         (dest)->klass = (stack_class);          \
521         } while (0)
522
523 #else
524
525 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {    \
526                 MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
527                 (dest)->opcode = cfg->compile_aot ? OP_AOTCONST : OP_PCONST;    \
528                 (dest)->inst_p0 = (cons);       \
529                 (dest)->inst_i1 = (gpointer)(patch_type); \
530                 (dest)->type = STACK_PTR;       \
531     } while (0)
532
533 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
534                 MONO_INST_NEW ((cfg), (dest), OP_AOTCONST);     \
535                 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token));  \
536                 (dest)->inst_p1 = (gpointer)(patch_type); \
537                 (dest)->type = (stack_type);    \
538         (dest)->klass = (stack_class);          \
539     } while (0)
540
541 #endif
542
543 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
544
545 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
546
547 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
548
549 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
550
551 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
552
553 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
554
555 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
556
557 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_TYPE_FROM_HANDLE, (image), (token), STACK_OBJ, mono_defaults.monotype_class)
558
559 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
560
561 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
562                 if (cfg->compile_aot) { \
563                         NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ, NULL); \
564                 } else { \
565                         NEW_PCONST (cfg, args [0], (entry).blob); \
566                 } \
567         } while (0)
568
569 #define NEW_DOMAINCONST(cfg,dest) do { \
570                 if (cfg->opt & MONO_OPT_SHARED) { \
571                         /* avoid depending on undefined C behavior in sequence points */ \
572                         MonoInst* __domain_var = mono_get_domainvar (cfg); \
573                         NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
574                 } else { \
575                         NEW_PCONST (cfg, dest, (cfg)->domain); \
576                 } \
577         } while (0)
578
579 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
580
581 #define NEW_ARGLOAD(cfg,dest,num) do {  \
582                 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
583                 MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
584                 (dest)->ssa_op = MONO_SSA_LOAD; \
585                 (dest)->inst_i0 = arg_array [(num)];    \
586                 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
587                 type_to_eval_stack_type ((cfg), param_types [(num)], (dest));   \
588                 (dest)->klass = (dest)->inst_i0->klass; \
589         }} while (0)
590
591 #define NEW_LOCLOAD(cfg,dest,num) do {  \
592                 MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
593                 (dest)->ssa_op = MONO_SSA_LOAD; \
594                 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)];       \
595                 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
596                 type_to_eval_stack_type ((cfg), header->locals [(num)], (dest));        \
597                 (dest)->klass = (dest)->inst_i0->klass; \
598         } while (0)
599
600 #define NEW_LOCLOADA(cfg,dest,num) do { \
601                 MONO_INST_NEW ((cfg), (dest), OP_LDADDR);       \
602                 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN;        \
603                 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)];       \
604                 (dest)->inst_i0->flags |= MONO_INST_INDIRECT;   \
605                 (dest)->type = STACK_MP;        \
606                 (dest)->klass = (dest)->inst_i0->klass; \
607         if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
608            (cfg)->disable_ssa = TRUE; \
609         } while (0)
610
611 #define NEW_RETLOADA(cfg,dest) do {     \
612         if (cfg->vret_addr) { \
613                     MONO_INST_NEW ((cfg), (dest), OP_NOP);      \
614                     (dest)->ssa_op = MONO_SSA_LOAD;     \
615                     (dest)->inst_i0 = cfg->vret_addr; \
616                     (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
617             (dest)->type = STACK_MP; \
618                     (dest)->klass = (dest)->inst_i0->klass;     \
619         } else { \
620                         MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
621                     (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN;    \
622                     (dest)->inst_i0 = (cfg)->ret;       \
623                     (dest)->inst_i0->flags |= MONO_INST_INDIRECT;       \
624                     (dest)->opcode = cfg->ret_var_is_local ? OP_LDADDR : CEE_LDIND_I;   \
625                     (dest)->type = STACK_MP;    \
626                     (dest)->klass = (dest)->inst_i0->klass;     \
627             (cfg)->disable_ssa = TRUE; \
628         } \
629         } while (0)
630
631 #define NEW_ARGLOADA(cfg,dest,num) do { \
632                 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
633                 MONO_INST_NEW ((cfg), (dest), OP_LDADDR);       \
634                 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN;        \
635                 (dest)->inst_i0 = arg_array [(num)];    \
636                 (dest)->inst_i0->flags |= MONO_INST_INDIRECT;   \
637                 (dest)->type = STACK_MP;        \
638                 (dest)->klass = (dest)->inst_i0->klass; \
639                 (cfg)->disable_ssa = TRUE; \
640         } while (0)
641
642 #define NEW_TEMPLOAD(cfg,dest,num) do { \
643                 MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
644                 (dest)->ssa_op = MONO_SSA_LOAD; \
645                 (dest)->inst_i0 = (cfg)->varinfo [(num)];       \
646                 (dest)->opcode = mini_type_to_ldind ((cfg), (dest)->inst_i0->inst_vtype); \
647                 type_to_eval_stack_type ((cfg), (dest)->inst_i0->inst_vtype, (dest));   \
648                 (dest)->klass = (dest)->inst_i0->klass; \
649         } while (0)
650
651 #define NEW_TEMPLOADA(cfg,dest,num) do {        \
652                 MONO_INST_NEW ((cfg), (dest), OP_LDADDR);       \
653                 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN;        \
654                 (dest)->inst_i0 = (cfg)->varinfo [(num)];       \
655                 (dest)->inst_i0->flags |= MONO_INST_INDIRECT;   \
656                 (dest)->type = STACK_MP;        \
657                 (dest)->klass = (dest)->inst_i0->klass; \
658         if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
659            (cfg)->disable_ssa = TRUE; \
660         } while (0)
661
662 #define NEW_INDLOAD(cfg,dest,addr,vtype) do {   \
663                 MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
664                 (dest)->inst_left = addr;       \
665                 (dest)->opcode = mini_type_to_ldind ((cfg), vtype);     \
666                 type_to_eval_stack_type ((cfg), vtype, (dest)); \
667                 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/     \
668         } while (0)
669
670 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do {    \
671                 MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
672                 (dest)->inst_i0 = addr; \
673                 (dest)->opcode = mini_type_to_stind ((cfg), vtype);     \
674                 (dest)->inst_i1 = (value);      \
675                 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/     \
676         } while (0)
677
678 #define NEW_TEMPSTORE(cfg,dest,num,inst) do {   \
679                 MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
680                 (dest)->ssa_op = MONO_SSA_STORE;        \
681                 (dest)->inst_i0 = (cfg)->varinfo [(num)];       \
682                 (dest)->opcode = mini_type_to_stind ((cfg), (dest)->inst_i0->inst_vtype); \
683                 (dest)->inst_i1 = (inst);       \
684                 (dest)->klass = (dest)->inst_i0->klass; \
685         } while (0)
686
687 #define NEW_LOCSTORE(cfg,dest,num,inst) do {    \
688                 MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
689                 (dest)->opcode = mini_type_to_stind ((cfg), header->locals [(num)]); \
690                 (dest)->ssa_op = MONO_SSA_STORE;        \
691                 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)];       \
692                 (dest)->inst_i1 = (inst);       \
693                 (dest)->klass = (dest)->inst_i0->klass; \
694         } while (0)
695
696 #define NEW_ARGSTORE(cfg,dest,num,inst) do {    \
697                 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
698                 MONO_INST_NEW ((cfg), (dest), OP_NOP);  \
699                 (dest)->opcode = mini_type_to_stind ((cfg), param_types [(num)]); \
700                 (dest)->ssa_op = MONO_SSA_STORE;        \
701                 (dest)->inst_i0 = arg_array [(num)];    \
702                 (dest)->inst_i1 = (inst);       \
703                 (dest)->klass = (dest)->inst_i0->klass; \
704         } while (0)
705
706 #define NEW_MEMCPY(cfg,dest,dst,src,memcpy_size,memcpy_align) do { \
707                 MONO_INST_NEW (cfg, dest, OP_MEMCPY); \
708         (dest)->inst_left = (dst); \
709                 (dest)->inst_right = (src); \
710         (dest)->backend.memcpy_args = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoMemcpyArgs)); \
711                 (dest)->backend.memcpy_args->size = (memcpy_size); \
712                 (dest)->backend.memcpy_args->align = (memcpy_align); \
713     } while (0)
714
715 #define NEW_MEMSET(cfg,dest,dst,imm,memcpy_size,memcpy_align) do { \
716                 MONO_INST_NEW (cfg, dest, OP_MEMSET); \
717         (dest)->inst_left = (dst); \
718                 (dest)->inst_imm = (imm); \
719         (dest)->backend.memcpy_args = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoMemcpyArgs)); \
720                 (dest)->backend.memcpy_args->size = (memcpy_size); \
721                 (dest)->backend.memcpy_args->align = (memcpy_align); \
722     } while (0)
723
724 #define NEW_DUMMY_USE(cfg,dest,load) do { \
725                 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_USE);    \
726                 (dest)->inst_left = (load); \
727     } while (0)
728
729 #define NEW_DUMMY_STORE(cfg,dest,num) do { \
730                 MONO_INST_NEW ((cfg), (dest), OP_DUMMY_STORE);  \
731                 (dest)->inst_i0 = (cfg)->varinfo [(num)];       \
732                 (dest)->klass = (dest)->inst_i0->klass; \
733         } while (0)
734
735 #define ADD_BINOP(op) do {      \
736                 MONO_INST_NEW (cfg, ins, (op)); \
737                 sp -= 2;        \
738                 ins->inst_i0 = sp [0];  \
739                 ins->inst_i1 = sp [1];  \
740                 *sp++ = ins;    \
741                 type_from_op (ins);     \
742                 CHECK_TYPE (ins);       \
743                 /* Have to insert a widening op */               \
744                 /* FIXME: Need to add many more cases */ \
745                 if (ins->inst_i0->type == STACK_PTR && ins->inst_i1->type == STACK_I4) { \
746                         MonoInst *widen;  \
747                         MONO_INST_NEW (cfg, widen, CEE_CONV_I); \
748             widen->inst_i0 = ins->inst_i1; \
749                         ins->inst_i1 = widen; \
750                 } \
751         } while (0)
752
753 #define ADD_UNOP(op) do {       \
754                 MONO_INST_NEW (cfg, ins, (op)); \
755                 sp--;   \
756                 ins->inst_i0 = sp [0];  \
757                 *sp++ = ins;    \
758                 type_from_op (ins);     \
759                 CHECK_TYPE (ins);       \
760         } while (0)
761
762 #define ADD_BINCOND(next_block) do {    \
763                 MonoInst *cmp;  \
764                 sp -= 2;                \
765                 MONO_INST_NEW(cfg, cmp, OP_COMPARE);    \
766                 cmp->inst_i0 = sp [0];  \
767                 cmp->inst_i1 = sp [1];  \
768                 cmp->cil_code = ins->cil_code;  \
769                 type_from_op (cmp);     \
770                 CHECK_TYPE (cmp);       \
771                 ins->inst_i0 = cmp;     \
772                 MONO_ADD_INS (bblock, ins);     \
773                 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
774                 GET_BBLOCK (cfg, tblock, target);               \
775                 link_bblock (cfg, bblock, tblock);      \
776                 ins->inst_true_bb = tblock;     \
777                 CHECK_BBLOCK (target, ip, tblock);      \
778                 if ((next_block)) {     \
779                         link_bblock (cfg, bblock, (next_block));        \
780                         ins->inst_false_bb = (next_block);      \
781                         start_new_bblock = 1;   \
782                 } else {        \
783                         GET_BBLOCK (cfg, tblock, ip);           \
784                         link_bblock (cfg, bblock, tblock);      \
785                         ins->inst_false_bb = tblock;    \
786                         start_new_bblock = 2;   \
787                 }       \
788         } while (0)
789
790 /* FIXME: handle float, long ... */
791 #define ADD_UNCOND(istrue) do { \
792                 MonoInst *cmp;  \
793                 sp--;           \
794                 MONO_INST_NEW(cfg, cmp, OP_COMPARE);    \
795                 cmp->inst_i0 = sp [0];  \
796                 switch (cmp->inst_i0->type) { \
797                 case STACK_I8: \
798                         cmp->inst_i1 = zero_int64; break; \
799                 case STACK_R8: \
800                         cmp->inst_i1 = zero_r8; break; \
801                 case STACK_PTR: \
802                 case STACK_MP: \
803                         cmp->inst_i1 = zero_ptr; break; \
804                 case STACK_OBJ: \
805                         cmp->inst_i1 = zero_obj; break; \
806                 default: \
807                         cmp->inst_i1 = zero_int32;  \
808                 }  \
809                 cmp->cil_code = ins->cil_code;  \
810                 type_from_op (cmp);     \
811                 CHECK_TYPE (cmp);       \
812                 ins->inst_i0 = cmp;     \
813                 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ;    \
814                 MONO_ADD_INS (bblock, ins);     \
815                 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
816                 GET_BBLOCK (cfg, tblock, target);               \
817                 link_bblock (cfg, bblock, tblock);      \
818                 ins->inst_true_bb = tblock;     \
819                 CHECK_BBLOCK (target, ip, tblock);      \
820                 GET_BBLOCK (cfg, tblock, ip);           \
821                 link_bblock (cfg, bblock, tblock);      \
822                 ins->inst_false_bb = tblock;    \
823                 start_new_bblock = 2;   \
824         } while (0)
825
826 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
827                 MONO_INST_NEW ((cfg), (dest), CEE_LDELEMA);     \
828                 (dest)->inst_left = (sp) [0];   \
829                 (dest)->inst_right = (sp) [1];  \
830                 (dest)->type = STACK_MP;        \
831                 (dest)->klass = (k);    \
832                 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
833         } while (0)
834
835 #define NEW_GROUP(cfg,dest,el1,el2) do {        \
836                 MONO_INST_NEW ((cfg), (dest), OP_GROUP);        \
837                 (dest)->inst_left = (el1);      \
838                 (dest)->inst_right = (el2);     \
839         } while (0)
840
841 #if 0
842 static gint
843 compare_bblock (gconstpointer a, gconstpointer b)
844 {
845         const MonoBasicBlock *b1 = a;
846         const MonoBasicBlock *b2 = b;
847
848         return b2->cil_code - b1->cil_code;
849 }
850 #endif
851
852 /* *
853  * link_bblock: Links two basic blocks
854  *
855  * links two basic blocks in the control flow graph, the 'from'
856  * argument is the starting block and the 'to' argument is the block
857  * the control flow ends to after 'from'.
858  */
859 static void
860 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
861 {
862         MonoBasicBlock **newa;
863         int i, found;
864
865 #if 0
866         if (from->cil_code) {
867                 if (to->cil_code)
868                         g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
869                 else
870                         g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
871         } else {
872                 if (to->cil_code)
873                         g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
874                 else
875                         g_print ("edge from entry to exit\n");
876         }
877 #endif
878         found = FALSE;
879         for (i = 0; i < from->out_count; ++i) {
880                 if (to == from->out_bb [i]) {
881                         found = TRUE;
882                         break;
883                 }
884         }
885         if (!found) {
886                 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
887                 for (i = 0; i < from->out_count; ++i) {
888                         newa [i] = from->out_bb [i];
889                 }
890                 newa [i] = to;
891                 from->out_count++;
892                 from->out_bb = newa;
893         }
894
895         found = FALSE;
896         for (i = 0; i < to->in_count; ++i) {
897                 if (from == to->in_bb [i]) {
898                         found = TRUE;
899                         break;
900                 }
901         }
902         if (!found) {
903                 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
904                 for (i = 0; i < to->in_count; ++i) {
905                         newa [i] = to->in_bb [i];
906                 }
907                 newa [i] = from;
908                 to->in_count++;
909                 to->in_bb = newa;
910         }
911 }
912
913 /**
914  * mono_unlink_bblock:
915  *
916  *   Unlink two basic blocks.
917  */
918 void
919 mono_unlink_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
920 {
921         int i, pos;
922         gboolean found;
923
924         found = FALSE;
925         for (i = 0; i < from->out_count; ++i) {
926                 if (to == from->out_bb [i]) {
927                         found = TRUE;
928                         break;
929                 }
930         }
931         if (found) {
932                 pos = 0;
933                 for (i = 0; i < from->out_count; ++i) {
934                         if (from->out_bb [i] != to)
935                                 from->out_bb [pos ++] = from->out_bb [i];
936                 }
937                 g_assert (pos == from->out_count - 1);
938                 from->out_count--;
939         }
940
941         found = FALSE;
942         for (i = 0; i < to->in_count; ++i) {
943                 if (from == to->in_bb [i]) {
944                         found = TRUE;
945                         break;
946                 }
947         }
948         if (found) {
949                 pos = 0;
950                 for (i = 0; i < to->in_count; ++i) {
951                         if (to->in_bb [i] != from)
952                                 to->in_bb [pos ++] = to->in_bb [i];
953                 }
954                 g_assert (pos == to->in_count - 1);
955                 to->in_count--;
956         }
957 }
958
959 /*
960  * mono_bblocks_linked:
961  *
962  *   Return whenever BB1 and BB2 are linked in the CFG.
963  */
964 gboolean
965 mono_bblocks_linked (MonoBasicBlock *bb1, MonoBasicBlock *bb2)
966 {
967         int i;
968
969         for (i = 0; i < bb1->out_count; ++i) {
970                 if (bb1->out_bb [i] == bb2)
971                         return TRUE;
972         }
973
974         return FALSE;
975 }
976
977 /**
978  * mono_find_block_region:
979  *
980  *   We mark each basic block with a region ID. We use that to avoid BB
981  *   optimizations when blocks are in different regions.
982  *
983  * Returns:
984  *   A region token that encodes where this region is, and information
985  *   about the clause owner for this block.
986  *
987  *   The region encodes the try/catch/filter clause that owns this block
988  *   as well as the type.  -1 is a special value that represents a block
989  *   that is in none of try/catch/filter.
990  */
991 static int
992 mono_find_block_region (MonoCompile *cfg, int offset)
993 {
994         MonoMethod *method = cfg->method;
995         MonoMethodHeader *header = mono_method_get_header (method);
996         MonoExceptionClause *clause;
997         int i;
998
999         /* first search for handlers and filters */
1000         for (i = 0; i < header->num_clauses; ++i) {
1001                 clause = &header->clauses [i];
1002                 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
1003                     (offset < (clause->handler_offset)))
1004                         return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
1005                            
1006                 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
1007                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
1008                                 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
1009                         else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
1010                                 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
1011                         else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
1012                                 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
1013                 }
1014         }
1015
1016         /* search the try blocks */
1017         for (i = 0; i < header->num_clauses; ++i) {
1018                 clause = &header->clauses [i];
1019                 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1020                         return ((i + 1) << 8) | clause->flags;
1021         }
1022
1023         return -1;
1024 }
1025
1026 static GList*
1027 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
1028 {
1029         MonoMethod *method = cfg->method;
1030         MonoMethodHeader *header = mono_method_get_header (method);
1031         MonoExceptionClause *clause;
1032         MonoBasicBlock *handler;
1033         int i;
1034         GList *res = NULL;
1035
1036         for (i = 0; i < header->num_clauses; ++i) {
1037                 clause = &header->clauses [i];
1038                 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) && 
1039                     (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
1040                         if (clause->flags == type) {
1041                                 handler = cfg->cil_offset_to_bb [clause->handler_offset];
1042                                 g_assert (handler);
1043                                 res = g_list_append (res, handler);
1044                         }
1045                 }
1046         }
1047         return res;
1048 }
1049
1050 MonoInst *
1051 mono_find_spvar_for_region (MonoCompile *cfg, int region)
1052 {
1053         return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
1054 }
1055
1056 static void
1057 mono_create_spvar_for_region (MonoCompile *cfg, int region)
1058 {
1059         MonoInst *var;
1060
1061         var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
1062         if (var)
1063                 return;
1064
1065         var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1066         /* prevent it from being register allocated */
1067         var->flags |= MONO_INST_INDIRECT;
1068
1069         g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
1070 }
1071
1072 static MonoInst *
1073 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
1074 {
1075         return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
1076 }
1077
1078 static MonoInst*
1079 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
1080 {
1081         MonoInst *var;
1082
1083         var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
1084         if (var)
1085                 return var;
1086
1087         var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
1088         /* prevent it from being register allocated */
1089         var->flags |= MONO_INST_INDIRECT;
1090
1091         g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
1092
1093         return var;
1094 }
1095
1096 static void
1097 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
1098 {
1099         int i;
1100
1101         array [*dfn] = start;
1102         /* g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num); */
1103         for (i = 0; i < start->out_count; ++i) {
1104                 if (start->out_bb [i]->dfn)
1105                         continue;
1106                 (*dfn)++;
1107                 start->out_bb [i]->dfn = *dfn;
1108                 start->out_bb [i]->df_parent = start;
1109                 array [*dfn] = start->out_bb [i];
1110                 df_visit (start->out_bb [i], dfn, array);
1111         }
1112 }
1113
1114 static MonoBasicBlock*
1115 find_previous (MonoBasicBlock **bblocks, guint32 n_bblocks, MonoBasicBlock *start, const guchar *code)
1116 {
1117         MonoBasicBlock *best = start;
1118         int i;
1119
1120         for (i = 0; i < n_bblocks; ++i) {
1121                 if (bblocks [i]) {
1122                         MonoBasicBlock *bb = bblocks [i];
1123
1124                         if (bb->cil_code && bb->cil_code < code && bb->cil_code > best->cil_code)
1125                                 best = bb;
1126                 }
1127         }
1128
1129         return best;
1130 }
1131
1132 static void
1133 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
1134         int i, j;
1135         MonoInst *inst;
1136         MonoBasicBlock *bb;
1137
1138         if (second->code)
1139                 return;
1140         
1141         /* 
1142          * FIXME: take into account all the details:
1143          * second may have been the target of more than one bblock
1144          */
1145         second->out_count = first->out_count;
1146         second->out_bb = first->out_bb;
1147
1148         for (i = 0; i < first->out_count; ++i) {
1149                 bb = first->out_bb [i];
1150                 for (j = 0; j < bb->in_count; ++j) {
1151                         if (bb->in_bb [j] == first)
1152                                 bb->in_bb [j] = second;
1153                 }
1154         }
1155
1156         first->out_count = 0;
1157         first->out_bb = NULL;
1158         link_bblock (cfg, first, second);
1159
1160         second->last_ins = first->last_ins;
1161
1162         /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
1163         MONO_BB_FOR_EACH_INS (first, inst) {
1164                 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
1165                 g_print ("found %p: %s", inst->next->cil_code, code);
1166                 g_free (code);*/
1167                 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
1168                         second->code = inst->next;
1169                         inst->next = NULL;
1170                         first->last_ins = inst;
1171                         second->next_bb = first->next_bb;
1172                         first->next_bb = second;
1173                         return;
1174                 }
1175         }
1176         if (!second->code) {
1177                 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
1178                 //G_BREAKPOINT ();
1179         }
1180 }
1181
1182 guint32
1183 mono_reverse_branch_op (guint32 opcode)
1184 {
1185         static const int reverse_map [] = {
1186                 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
1187                 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
1188         };
1189         static const int reverse_fmap [] = {
1190                 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
1191                 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
1192         };
1193         static const int reverse_lmap [] = {
1194                 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
1195                 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
1196         };
1197         static const int reverse_imap [] = {
1198                 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
1199                 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
1200         };
1201                                 
1202         if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
1203                 opcode = reverse_map [opcode - CEE_BEQ];
1204         } else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
1205                 opcode = reverse_fmap [opcode - OP_FBEQ];
1206         } else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
1207                 opcode = reverse_lmap [opcode - OP_LBEQ];
1208         } else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
1209                 opcode = reverse_imap [opcode - OP_IBEQ];
1210         } else
1211                 g_assert_not_reached ();
1212
1213         return opcode;
1214 }
1215
1216 guint
1217 mono_type_to_store_membase (MonoCompile *cfg, MonoType *type)
1218 {
1219         if (type->byref)
1220                 return OP_STORE_MEMBASE_REG;
1221
1222 handle_enum:
1223         switch (type->type) {
1224         case MONO_TYPE_I1:
1225         case MONO_TYPE_U1:
1226         case MONO_TYPE_BOOLEAN:
1227                 return OP_STOREI1_MEMBASE_REG;
1228         case MONO_TYPE_I2:
1229         case MONO_TYPE_U2:
1230         case MONO_TYPE_CHAR:
1231                 return OP_STOREI2_MEMBASE_REG;
1232         case MONO_TYPE_I4:
1233         case MONO_TYPE_U4:
1234                 return OP_STOREI4_MEMBASE_REG;
1235         case MONO_TYPE_I:
1236         case MONO_TYPE_U:
1237         case MONO_TYPE_PTR:
1238         case MONO_TYPE_FNPTR:
1239                 return OP_STORE_MEMBASE_REG;
1240         case MONO_TYPE_CLASS:
1241         case MONO_TYPE_STRING:
1242         case MONO_TYPE_OBJECT:
1243         case MONO_TYPE_SZARRAY:
1244         case MONO_TYPE_ARRAY:    
1245                 return OP_STORE_MEMBASE_REG;
1246         case MONO_TYPE_I8:
1247         case MONO_TYPE_U8:
1248                 return OP_STOREI8_MEMBASE_REG;
1249         case MONO_TYPE_R4:
1250                 return OP_STORER4_MEMBASE_REG;
1251         case MONO_TYPE_R8:
1252                 return OP_STORER8_MEMBASE_REG;
1253         case MONO_TYPE_VALUETYPE:
1254                 if (type->data.klass->enumtype) {
1255                         type = type->data.klass->enum_basetype;
1256                         goto handle_enum;
1257                 }
1258                 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
1259                         return OP_STOREX_MEMBASE;
1260                 return OP_STOREV_MEMBASE;
1261         case MONO_TYPE_TYPEDBYREF:
1262                 return OP_STOREV_MEMBASE;
1263         case MONO_TYPE_GENERICINST:
1264                 type = &type->data.generic_class->container_class->byval_arg;
1265                 goto handle_enum;
1266         case MONO_TYPE_VAR:
1267         case MONO_TYPE_MVAR:
1268                 /* FIXME: all the arguments must be references for now,
1269                  * later look inside cfg and see if the arg num is
1270                  * really a reference
1271                  */
1272                 g_assert (cfg->generic_sharing_context);
1273                 return OP_STORE_MEMBASE_REG;
1274         default:
1275                 g_error ("unknown type 0x%02x in type_to_store_membase", type->type);
1276         }
1277         return -1;
1278 }
1279
1280 guint
1281 mono_type_to_load_membase (MonoCompile *cfg, MonoType *type)
1282 {
1283         if (type->byref)
1284                 return OP_LOAD_MEMBASE;
1285
1286         switch (mono_type_get_underlying_type (type)->type) {
1287         case MONO_TYPE_I1:
1288                 return OP_LOADI1_MEMBASE;
1289         case MONO_TYPE_U1:
1290         case MONO_TYPE_BOOLEAN:
1291                 return OP_LOADU1_MEMBASE;
1292         case MONO_TYPE_I2:
1293                 return OP_LOADI2_MEMBASE;
1294         case MONO_TYPE_U2:
1295         case MONO_TYPE_CHAR:
1296                 return OP_LOADU2_MEMBASE;
1297         case MONO_TYPE_I4:
1298                 return OP_LOADI4_MEMBASE;
1299         case MONO_TYPE_U4:
1300                 return OP_LOADU4_MEMBASE;
1301         case MONO_TYPE_I:
1302         case MONO_TYPE_U:
1303         case MONO_TYPE_PTR:
1304         case MONO_TYPE_FNPTR:
1305                 return OP_LOAD_MEMBASE;
1306         case MONO_TYPE_CLASS:
1307         case MONO_TYPE_STRING:
1308         case MONO_TYPE_OBJECT:
1309         case MONO_TYPE_SZARRAY:
1310         case MONO_TYPE_ARRAY:    
1311                 return OP_LOAD_MEMBASE;
1312         case MONO_TYPE_I8:
1313         case MONO_TYPE_U8:
1314                 return OP_LOADI8_MEMBASE;
1315         case MONO_TYPE_R4:
1316                 return OP_LOADR4_MEMBASE;
1317         case MONO_TYPE_R8:
1318                 return OP_LOADR8_MEMBASE;
1319         case MONO_TYPE_VALUETYPE:
1320                 if (MONO_CLASS_IS_SIMD (cfg, mono_class_from_mono_type (type)))
1321                         return OP_LOADX_MEMBASE;
1322         case MONO_TYPE_TYPEDBYREF:
1323                 return OP_LOADV_MEMBASE;
1324         case MONO_TYPE_GENERICINST:
1325                 if (mono_type_generic_inst_is_valuetype (type))
1326                         return OP_LOADV_MEMBASE;
1327                 else
1328                         return OP_LOAD_MEMBASE;
1329                 break;
1330         case MONO_TYPE_VAR:
1331         case MONO_TYPE_MVAR:
1332                 /* FIXME: all the arguments must be references for now,
1333                  * later look inside cfg and see if the arg num is
1334                  * really a reference
1335                  */
1336                 g_assert (cfg->generic_sharing_context);
1337                 return OP_LOAD_MEMBASE;
1338         default:
1339                 g_error ("unknown type 0x%02x in type_to_load_membase", type->type);
1340         }
1341         return -1;
1342 }
1343
1344 #ifdef MONO_ARCH_SOFT_FLOAT
1345 static int
1346 condbr_to_fp_br (int opcode)
1347 {
1348         switch (opcode) {
1349         case CEE_BEQ: return OP_FBEQ;
1350         case CEE_BGE: return OP_FBGE;
1351         case CEE_BGT: return OP_FBGT;
1352         case CEE_BLE: return OP_FBLE;
1353         case CEE_BLT: return OP_FBLT;
1354         case CEE_BNE_UN: return OP_FBNE_UN;
1355         case CEE_BGE_UN: return OP_FBGE_UN;
1356         case CEE_BGT_UN: return OP_FBGT_UN;
1357         case CEE_BLE_UN: return OP_FBLE_UN;
1358         case CEE_BLT_UN: return OP_FBLT_UN;
1359         }
1360         g_assert_not_reached ();
1361         return 0;
1362 }
1363 #endif
1364
1365 /*
1366  * The following tables are used to quickly validate the IL code in type_from_op ().
1367  */
1368 static const char
1369 bin_num_table [STACK_MAX] [STACK_MAX] = {
1370         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1371         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
1372         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1373         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
1374         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8,  STACK_INV, STACK_INV, STACK_INV},
1375         {STACK_INV, STACK_MP,  STACK_INV, STACK_MP,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1376         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1377         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1378 };
1379
1380 static const char 
1381 neg_table [] = {
1382         STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1383 };
1384
1385 /* reduce the size of this table */
1386 static const char
1387 bin_int_table [STACK_MAX] [STACK_MAX] = {
1388         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1389         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1390         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1391         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1392         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1393         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1394         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1395         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1396 };
1397
1398 static const char
1399 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1400 /*      Inv i  L  p  F  &  O  vt */
1401         {0},
1402         {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
1403         {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
1404         {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
1405         {0, 0, 0, 0, 1, 0, 0, 0}, /* F, R8 */
1406         {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
1407         {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
1408         {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
1409 };
1410
1411 /* reduce the size of this table */
1412 static const char
1413 shift_table [STACK_MAX] [STACK_MAX] = {
1414         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1415         {STACK_INV, STACK_I4,  STACK_INV, STACK_I4,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1416         {STACK_INV, STACK_I8,  STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1417         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1418         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1419         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1420         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1421         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1422 };
1423
1424 /*
1425  * Tables to map from the non-specific opcode to the matching
1426  * type-specific opcode.
1427  */
1428 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1429 static const guint16
1430 binops_op_map [STACK_MAX] = {
1431         0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1432 };
1433
1434 /* handles from CEE_NEG to CEE_CONV_U8 */
1435 static const guint16
1436 unops_op_map [STACK_MAX] = {
1437         0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1438 };
1439
1440 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1441 static const guint16
1442 ovfops_op_map [STACK_MAX] = {
1443         0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2
1444 };
1445
1446 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1447 static const guint16
1448 ovf2ops_op_map [STACK_MAX] = {
1449         0, 0, OP_LCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_FCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN, OP_PCONV_TO_OVF_I1_UN-CEE_CONV_OVF_I1_UN
1450 };
1451
1452 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1453 static const guint16
1454 ovf3ops_op_map [STACK_MAX] = {
1455         0, 0, OP_LCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_FCONV_TO_OVF_I1-CEE_CONV_OVF_I1, OP_PCONV_TO_OVF_I1-CEE_CONV_OVF_I1
1456 };
1457
1458 /* handles from CEE_CEQ to CEE_CLT_UN */
1459 static const guint16
1460 ceqops_op_map [STACK_MAX] = {
1461         0, 0, OP_LCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_FCEQ-OP_CEQ, OP_LCEQ-OP_CEQ
1462 };
1463
1464 /*
1465  * Sets ins->type (the type on the eval stack) according to the
1466  * type of the opcode and the arguments to it.
1467  * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1468  *
1469  * FIXME: this function sets ins->type unconditionally in some cases, but
1470  * it should set it to invalid for some types (a conv.x on an object)
1471  */
1472 static void
1473 type_from_op (MonoInst *ins) {
1474         switch (ins->opcode) {
1475         /* binops */
1476         case CEE_ADD:
1477         case CEE_SUB:
1478         case CEE_MUL:
1479         case CEE_DIV:
1480         case CEE_REM:
1481                 /* FIXME: check unverifiable args for STACK_MP */
1482                 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1483                 ins->opcode += binops_op_map [ins->type];
1484                 return;
1485         case CEE_DIV_UN:
1486         case CEE_REM_UN:
1487         case CEE_AND:
1488         case CEE_OR:
1489         case CEE_XOR:
1490                 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1491                 ins->opcode += binops_op_map [ins->type];
1492                 return;
1493         case CEE_SHL:
1494         case CEE_SHR:
1495         case CEE_SHR_UN:
1496                 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1497                 ins->opcode += binops_op_map [ins->type];
1498                 return;
1499         case OP_COMPARE:
1500         case OP_LCOMPARE:
1501                 /* FIXME: handle some specifics with ins->next->type */
1502                 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1503                 if ((ins->inst_i0->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((ins->inst_i0->type == STACK_PTR) || (ins->inst_i0->type == STACK_OBJ) || (ins->inst_i0->type == STACK_MP))))
1504                         ins->opcode = OP_LCOMPARE;
1505                 return;
1506         case OP_CEQ:
1507                 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1508                 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1509                 return;
1510                 
1511         case OP_CGT:
1512         case OP_CGT_UN:
1513         case OP_CLT:
1514         case OP_CLT_UN:
1515                 ins->type = (bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] & 1) ? STACK_I4: STACK_INV;
1516                 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1517                 return;
1518         /* unops */
1519         case CEE_NEG:
1520                 ins->type = neg_table [ins->inst_i0->type];
1521                 ins->opcode += unops_op_map [ins->type];
1522                 return;
1523         case CEE_NOT:
1524                 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1525                         ins->type = ins->inst_i0->type;
1526                 else
1527                         ins->type = STACK_INV;
1528                 ins->opcode += unops_op_map [ins->type];
1529                 return;
1530         case CEE_CONV_I1:
1531         case CEE_CONV_I2:
1532         case CEE_CONV_I4:
1533         case CEE_CONV_U4:
1534                 ins->type = STACK_I4;
1535                 ins->opcode += unops_op_map [ins->inst_i0->type];
1536                 return;
1537         case CEE_CONV_R_UN:
1538                 ins->type = STACK_R8;
1539                 switch (ins->inst_i0->type) {
1540                 case STACK_I4:
1541                 case STACK_PTR:
1542                         break;
1543                 case STACK_I8:
1544                         ins->opcode = OP_LCONV_TO_R_UN; 
1545                         break;
1546                 }
1547                 return;
1548         case CEE_CONV_OVF_I1:
1549         case CEE_CONV_OVF_U1:
1550         case CEE_CONV_OVF_I2:
1551         case CEE_CONV_OVF_U2:
1552         case CEE_CONV_OVF_I4:
1553         case CEE_CONV_OVF_U4:
1554                 ins->type = STACK_I4;
1555                 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1556                 return;
1557         case CEE_CONV_OVF_I_UN:
1558         case CEE_CONV_OVF_U_UN:
1559                 ins->type = STACK_PTR;
1560                 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1561                 return;
1562         case CEE_CONV_OVF_I1_UN:
1563         case CEE_CONV_OVF_I2_UN:
1564         case CEE_CONV_OVF_I4_UN:
1565         case CEE_CONV_OVF_U1_UN:
1566         case CEE_CONV_OVF_U2_UN:
1567         case CEE_CONV_OVF_U4_UN:
1568                 ins->type = STACK_I4;
1569                 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1570                 return;
1571         case CEE_CONV_U:
1572                 ins->type = STACK_PTR;
1573                 switch (ins->inst_i0->type) {
1574                 case STACK_I4:
1575                         break;
1576                 case STACK_PTR:
1577                 case STACK_MP:
1578 #if SIZEOF_VOID_P == 8
1579                         ins->opcode = OP_LCONV_TO_U;
1580 #endif
1581                         break;
1582                 case STACK_I8:
1583                         ins->opcode = OP_LCONV_TO_U;
1584                         break;
1585                 case STACK_R8:
1586                         ins->opcode = OP_FCONV_TO_U;
1587                         break;
1588                 }
1589                 return;
1590         case CEE_CONV_I8:
1591         case CEE_CONV_U8:
1592                 ins->type = STACK_I8;
1593                 ins->opcode += unops_op_map [ins->inst_i0->type];
1594                 return;
1595         case CEE_CONV_OVF_I8:
1596         case CEE_CONV_OVF_U8:
1597                 ins->type = STACK_I8;
1598                 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1599                 return;
1600         case CEE_CONV_OVF_U8_UN:
1601         case CEE_CONV_OVF_I8_UN:
1602                 ins->type = STACK_I8;
1603                 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1604                 return;
1605         case CEE_CONV_R4:
1606         case CEE_CONV_R8:
1607                 ins->type = STACK_R8;
1608                 ins->opcode += unops_op_map [ins->inst_i0->type];
1609                 return;
1610         case OP_CKFINITE:
1611                 ins->type = STACK_R8;           
1612                 return;
1613         case CEE_CONV_U2:
1614         case CEE_CONV_U1:
1615                 ins->type = STACK_I4;
1616                 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1617                 break;
1618         case CEE_CONV_I:
1619         case CEE_CONV_OVF_I:
1620         case CEE_CONV_OVF_U:
1621                 ins->type = STACK_PTR;
1622                 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1623                 return;
1624         case CEE_ADD_OVF:
1625         case CEE_ADD_OVF_UN:
1626         case CEE_MUL_OVF:
1627         case CEE_MUL_OVF_UN:
1628         case CEE_SUB_OVF:
1629         case CEE_SUB_OVF_UN:
1630                 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1631                 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1632                 if (ins->type == STACK_R8)
1633                         ins->type = STACK_INV;
1634                 return;
1635         default:
1636                 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1637                 break;
1638         }
1639 }
1640
1641 static const char 
1642 ldind_type [] = {
1643         STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1644 };
1645
1646 /* map ldelem.x to the matching ldind.x opcode */
1647 static const guchar
1648 ldelem_to_ldind [] = {
1649         CEE_LDIND_I1,
1650         CEE_LDIND_U1,
1651         CEE_LDIND_I2,
1652         CEE_LDIND_U2,
1653         CEE_LDIND_I4,
1654         CEE_LDIND_U4,
1655         CEE_LDIND_I8,
1656         CEE_LDIND_I,
1657         CEE_LDIND_R4,
1658         CEE_LDIND_R8,
1659         CEE_LDIND_REF
1660 };
1661
1662 /* map stelem.x to the matching stind.x opcode */
1663 static const guchar
1664 stelem_to_stind [] = {
1665         CEE_STIND_I,
1666         CEE_STIND_I1,
1667         CEE_STIND_I2,
1668         CEE_STIND_I4,
1669         CEE_STIND_I8,
1670         CEE_STIND_R4,
1671         CEE_STIND_R8,
1672         CEE_STIND_REF
1673 };
1674
1675
1676 #ifdef MONO_ARCH_SOFT_FLOAT
1677 static void
1678 handle_store_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, MonoInst *val, const unsigned char *ip)
1679 {
1680         MonoInst *iargs [2];
1681         iargs [0] = val;
1682         iargs [1] = ptr;
1683
1684         mono_emit_jit_icall (cfg, bblock, mono_fstore_r4, iargs, ip);
1685 }
1686
1687 static int
1688 handle_load_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, const unsigned char *ip)
1689 {
1690         MonoInst *iargs [1];
1691         iargs [0] = ptr;
1692
1693         return mono_emit_jit_icall (cfg, bblock, mono_fload_r4, iargs, ip);
1694 }
1695
1696 #define LDLOC_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1697                 if (header->locals [(idx)]->type == MONO_TYPE_R4 && !header->locals [(idx)]->byref) {   \
1698                         int temp;       \
1699                         NEW_LOCLOADA (cfg, (ins), (idx));       \
1700                         temp = handle_load_float (cfg, bblock, (ins), (ip));    \
1701                         NEW_TEMPLOAD (cfg, (ins), temp);        \
1702                 }       \
1703         } while (0)
1704 #define STLOC_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1705                 if (header->locals [(idx)]->type == MONO_TYPE_R4 && !header->locals [(idx)]->byref) {   \
1706                         NEW_LOCLOADA (cfg, (ins), (idx));       \
1707                         handle_store_float (cfg, bblock, (ins), *sp, (ip));     \
1708                         MONO_INST_NEW (cfg, (ins), OP_NOP);     \
1709                 }       \
1710         } while (0)
1711 #define LDARG_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1712                 if (param_types [(idx)]->type == MONO_TYPE_R4 && !param_types [(idx)]->byref) { \
1713                         int temp;       \
1714                         NEW_ARGLOADA (cfg, (ins), (idx));       \
1715                         temp = handle_load_float (cfg, bblock, (ins), (ip));    \
1716                         NEW_TEMPLOAD (cfg, (ins), temp);        \
1717                 }       \
1718         } while (0)
1719 #define STARG_SOFT_FLOAT(cfg,ins,idx,ip) do {\
1720                 if (param_types [(idx)]->type == MONO_TYPE_R4 && !param_types [(idx)]->byref) { \
1721                         NEW_ARGLOADA (cfg, (ins), (idx));       \
1722                         handle_store_float (cfg, bblock, (ins), *sp, (ip));     \
1723                         MONO_INST_NEW (cfg, (ins), OP_NOP);     \
1724                 }       \
1725         } while (0)
1726
1727 #define NEW_TEMPLOAD_SOFT_FLOAT(cfg,bblock,ins,num,ip) do {             \
1728         if ((ins)->opcode == CEE_LDIND_R4) {                                            \
1729             int idx = (num);                                                                            \
1730             int temp;                                                                                   \
1731             NEW_TEMPLOADA (cfg, (ins), (idx));                                                  \
1732                 temp = handle_load_float (cfg, (bblock), (ins), ip);            \
1733                 NEW_TEMPLOAD (cfg, (ins), (temp));                                                      \
1734         }                                                                                                                               \
1735         } while (0)
1736
1737 #define NEW_TEMPSTORE_SOFT_FLOAT(cfg,bblock,ins,num,val,ip) do {                \
1738         if ((ins)->opcode == CEE_STIND_R4) {                                                            \
1739             int idx = (num);                                                                            \
1740                 NEW_TEMPLOADA (cfg, (ins), (idx)); \
1741                 handle_store_float ((cfg), (bblock), (ins), (val), (ip));       \
1742         } \
1743         } while (0)
1744
1745 #else
1746
1747 #define LDLOC_SOFT_FLOAT(cfg,ins,idx,ip)
1748 #define STLOC_SOFT_FLOAT(cfg,ins,idx,ip)
1749 #define LDARG_SOFT_FLOAT(cfg,ins,idx,ip)
1750 #define STARG_SOFT_FLOAT(cfg,ins,idx,ip)
1751 #define NEW_TEMPLOAD_SOFT_FLOAT(cfg,bblock,ins,num,ip)
1752 #define NEW_TEMPSTORE_SOFT_FLOAT(cfg,bblock,ins,num,val,ip)
1753 #endif
1754
1755 #if 0
1756
1757 static const char
1758 param_table [STACK_MAX] [STACK_MAX] = {
1759         {0},
1760 };
1761
1762 static int
1763 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig)
1764 {
1765         int i;
1766
1767         if (sig->hasthis) {
1768                 switch (args->type) {
1769                 case STACK_I4:
1770                 case STACK_I8:
1771                 case STACK_R8:
1772                 case STACK_VTYPE:
1773                 case STACK_INV:
1774                         return 0;
1775                 }
1776                 args++;
1777         }
1778         for (i = 0; i < sig->param_count; ++i) {
1779                 switch (args [i].type) {
1780                 case STACK_INV:
1781                         return 0;
1782                 case STACK_MP:
1783                         if (!sig->params [i]->byref)
1784                                 return 0;
1785                         continue;
1786                 case STACK_OBJ:
1787                         if (sig->params [i]->byref)
1788                                 return 0;
1789                         switch (sig->params [i]->type) {
1790                         case MONO_TYPE_CLASS:
1791                         case MONO_TYPE_STRING:
1792                         case MONO_TYPE_OBJECT:
1793                         case MONO_TYPE_SZARRAY:
1794                         case MONO_TYPE_ARRAY:
1795                                 break;
1796                         default:
1797                                 return 0;
1798                         }
1799                         continue;
1800                 case STACK_R8:
1801                         if (sig->params [i]->byref)
1802                                 return 0;
1803                         if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1804                                 return 0;
1805                         continue;
1806                 case STACK_PTR:
1807                 case STACK_I4:
1808                 case STACK_I8:
1809                 case STACK_VTYPE:
1810                         break;
1811                 }
1812                 /*if (!param_table [args [i].type] [sig->params [i]->type])
1813                         return 0;*/
1814         }
1815         return 1;
1816 }
1817 #endif
1818
1819 static guint
1820 mini_type_to_ldind (MonoCompile* cfg, MonoType *type)
1821 {
1822         if (cfg->generic_sharing_context && !type->byref) {
1823                 /* FIXME: all the arguments must be references for now,
1824                  * later look inside cfg and see if the arg num is
1825                  * really a reference
1826                  */
1827                 if (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)
1828                         return CEE_LDIND_REF;
1829         }
1830         return mono_type_to_ldind (type);
1831 }
1832
1833 guint
1834 mini_type_to_stind (MonoCompile* cfg, MonoType *type)
1835 {
1836         if (cfg->generic_sharing_context && !type->byref) {
1837                 /* FIXME: all the arguments must be references for now,
1838                  * later look inside cfg and see if the arg num is
1839                  * really a reference
1840                  */
1841                 if (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)
1842                         return CEE_STIND_REF;
1843         }
1844         return mono_type_to_stind (type);
1845 }
1846
1847 int
1848 mono_op_imm_to_op (int opcode)
1849 {
1850         switch (opcode) {
1851         case OP_ADD_IMM:
1852 #if SIZEOF_VOID_P == 4
1853                 return OP_IADD;
1854 #else
1855                 return OP_LADD;
1856 #endif
1857         case OP_IADD_IMM:
1858                 return OP_IADD;
1859         case OP_LADD_IMM:
1860                 return OP_LADD;
1861         case OP_ISUB_IMM:
1862                 return OP_ISUB;
1863         case OP_LSUB_IMM:
1864                 return OP_LSUB;
1865         case OP_IMUL_IMM:
1866                 return OP_IMUL;
1867         case OP_AND_IMM:
1868 #if SIZEOF_VOID_P == 4
1869                 return OP_IAND;
1870 #else
1871                 return OP_LAND;
1872 #endif
1873         case OP_IAND_IMM:
1874                 return OP_IAND;
1875         case OP_LAND_IMM:
1876                 return OP_LAND;
1877         case OP_IOR_IMM:
1878                 return OP_IOR;
1879         case OP_LOR_IMM:
1880                 return OP_LOR;
1881         case OP_IXOR_IMM:
1882                 return OP_IXOR;
1883         case OP_LXOR_IMM:
1884                 return OP_LXOR;
1885         case OP_ISHL_IMM:
1886                 return OP_ISHL;
1887         case OP_LSHL_IMM:
1888                 return OP_LSHL;
1889         case OP_ISHR_IMM:
1890                 return OP_ISHR;
1891         case OP_LSHR_IMM:
1892                 return OP_LSHR;
1893         case OP_ISHR_UN_IMM:
1894                 return OP_ISHR_UN;
1895         case OP_LSHR_UN_IMM:
1896                 return OP_LSHR_UN;
1897         case OP_IDIV_IMM:
1898                 return OP_IDIV;
1899         case OP_IDIV_UN_IMM:
1900                 return OP_IDIV_UN;
1901         case OP_IREM_UN_IMM:
1902                 return OP_IREM_UN;
1903         case OP_IREM_IMM:
1904                 return OP_IREM;
1905         case OP_DIV_IMM:
1906 #if SIZEOF_VOID_P == 4
1907                 return OP_IDIV;
1908 #else
1909                 return OP_LDIV;
1910 #endif
1911         case OP_REM_IMM:
1912 #if SIZEOF_VOID_P == 4
1913                 return OP_IREM;
1914 #else
1915                 return OP_LREM;
1916 #endif
1917         case OP_ADDCC_IMM:
1918                 return OP_ADDCC;
1919         case OP_ADC_IMM:
1920                 return OP_ADC;
1921         case OP_SUBCC_IMM:
1922                 return OP_SUBCC;
1923         case OP_SBB_IMM:
1924                 return OP_SBB;
1925         case OP_IADC_IMM:
1926                 return OP_IADC;
1927         case OP_ISBB_IMM:
1928                 return OP_ISBB;
1929         case OP_COMPARE_IMM:
1930                 return OP_COMPARE;
1931         case OP_ICOMPARE_IMM:
1932                 return OP_ICOMPARE;
1933         case OP_LOCALLOC_IMM:
1934                 return OP_LOCALLOC;
1935         default:
1936                 printf ("%s\n", mono_inst_name (opcode));
1937                 g_assert_not_reached ();
1938                 return -1;
1939         }
1940 }
1941
1942 /*
1943  * mono_decompose_op_imm:
1944  *
1945  *   Replace the OP_.._IMM INS with its non IMM variant.
1946  */
1947 void
1948 mono_decompose_op_imm (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins)
1949 {
1950         MonoInst *temp;
1951
1952         MONO_INST_NEW (cfg, temp, OP_ICONST);
1953         temp->inst_c0 = ins->inst_imm;
1954         temp->dreg = mono_regstate_next_int (cfg->rs);
1955         mono_bblock_insert_before_ins (bb, ins, temp);
1956         ins->opcode = mono_op_imm_to_op (ins->opcode);
1957         if (ins->opcode == OP_LOCALLOC)
1958                 ins->sreg1 = temp->dreg;
1959         else
1960                 ins->sreg2 = temp->dreg;
1961
1962         bb->max_vreg = MAX (bb->max_vreg, cfg->rs->next_vreg);
1963 }
1964
1965 /*
1966  * When we need a pointer to the current domain many times in a method, we
1967  * call mono_domain_get() once and we store the result in a local variable.
1968  * This function returns the variable that represents the MonoDomain*.
1969  */
1970 inline static MonoInst *
1971 mono_get_domainvar (MonoCompile *cfg)
1972 {
1973         if (!cfg->domainvar)
1974                 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1975         return cfg->domainvar;
1976 }
1977
1978 /*
1979  * The got_var contains the address of the Global Offset Table when AOT 
1980  * compiling.
1981  */
1982 inline static MonoInst *
1983 mono_get_got_var (MonoCompile *cfg)
1984 {
1985 #ifdef MONO_ARCH_NEED_GOT_VAR
1986         if (!cfg->compile_aot)
1987                 return NULL;
1988         if (!cfg->got_var) {
1989                 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1990         }
1991         return cfg->got_var;
1992 #else
1993         return NULL;
1994 #endif
1995 }
1996
1997 static MonoInst *
1998 mono_get_vtable_var (MonoCompile *cfg)
1999 {
2000         g_assert (cfg->generic_sharing_context);
2001
2002         if (!cfg->rgctx_var) {
2003                 cfg->rgctx_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2004                 /* force the var to be stack allocated */
2005                 cfg->rgctx_var->flags |= MONO_INST_INDIRECT;
2006         }
2007
2008         return cfg->rgctx_var;
2009 }
2010
2011 static void
2012 set_vreg_to_inst (MonoCompile *cfg, int vreg, MonoInst *inst)
2013 {
2014         if (vreg >= cfg->vreg_to_inst_len) {
2015                 MonoInst **tmp = cfg->vreg_to_inst;
2016                 int size = cfg->vreg_to_inst_len;
2017
2018                 while (vreg >= cfg->vreg_to_inst_len)
2019                         cfg->vreg_to_inst_len = cfg->vreg_to_inst_len ? cfg->vreg_to_inst_len * 2 : 32;
2020                 cfg->vreg_to_inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * cfg->vreg_to_inst_len);
2021                 if (size)
2022                         memcpy (cfg->vreg_to_inst, tmp, size * sizeof (MonoInst*));
2023         }
2024         cfg->vreg_to_inst [vreg] = inst;
2025 }
2026
2027 #define mono_type_is_long(type) (!(type)->byref && ((mono_type_get_underlying_type (type)->type == MONO_TYPE_I8) || (mono_type_get_underlying_type (type)->type == MONO_TYPE_U8)))
2028 #define mono_type_is_float(type) (!(type)->byref && (((type)->type == MONO_TYPE_R8) || ((type)->type == MONO_TYPE_R4)))
2029
2030 MonoInst*
2031 mono_compile_create_var_for_vreg (MonoCompile *cfg, MonoType *type, int opcode, int vreg)
2032 {
2033         MonoInst *inst;
2034         int num = cfg->num_varinfo;
2035         gboolean regpair;
2036
2037         if ((num + 1) >= cfg->varinfo_count) {
2038                 int orig_count = cfg->varinfo_count;
2039                 cfg->varinfo_count = cfg->varinfo_count ? (cfg->varinfo_count * 2) : 64;
2040                 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
2041                 cfg->vars = (MonoMethodVar *)g_realloc (cfg->vars, sizeof (MonoMethodVar) * cfg->varinfo_count);
2042                 memset (&cfg->vars [orig_count], 0, (cfg->varinfo_count - orig_count) * sizeof (MonoMethodVar));
2043         }
2044
2045         mono_jit_stats.allocate_var++;
2046
2047         MONO_INST_NEW (cfg, inst, opcode);
2048         inst->inst_c0 = num;
2049         inst->inst_vtype = type;
2050         inst->klass = mono_class_from_mono_type (type);
2051         type_to_eval_stack_type (cfg, type, inst);
2052         /* if set to 1 the variable is native */
2053         inst->backend.is_pinvoke = 0;
2054         inst->dreg = vreg;
2055
2056         cfg->varinfo [num] = inst;
2057
2058         MONO_INIT_VARINFO (&cfg->vars [num], num);
2059
2060         if (vreg != -1)
2061                 set_vreg_to_inst (cfg, vreg, inst);
2062
2063 #if SIZEOF_VOID_P == 4
2064 #ifdef MONO_ARCH_SOFT_FLOAT
2065         regpair = mono_type_is_long (type) || mono_type_is_float (type);
2066 #else
2067         regpair = mono_type_is_long (type);
2068 #endif
2069 #else
2070         regpair = FALSE;
2071 #endif
2072
2073         if (regpair) {
2074                 MonoInst *tree;
2075
2076                 /* 
2077                  * These two cannot be allocated using create_var_for_vreg since that would
2078                  * put it into the cfg->varinfo array, confusing many parts of the JIT.
2079                  */
2080
2081                 /* 
2082                  * Set flags to VOLATILE so SSA skips it.
2083                  */
2084
2085                 if (cfg->verbose_level >= 4) {
2086                         printf ("  Create LVAR R%d (R%d, R%d)\n", inst->dreg, inst->dreg + 1, inst->dreg + 2);
2087                 }
2088
2089 #ifdef MONO_ARCH_SOFT_FLOAT
2090                 if (cfg->opt & MONO_OPT_SSA) {
2091                         if (mono_type_is_float (type))
2092                                 inst->flags = MONO_INST_VOLATILE;
2093                 }
2094 #endif
2095
2096                 /* Allocate a dummy MonoInst for the first vreg */
2097                 MONO_INST_NEW (cfg, tree, OP_LOCAL);
2098                 tree->dreg = inst->dreg + 1;
2099                 if (cfg->opt & MONO_OPT_SSA)
2100                         tree->flags = MONO_INST_VOLATILE;
2101                 tree->inst_c0 = num;
2102                 tree->type = STACK_I4;
2103                 tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
2104                 tree->klass = mono_class_from_mono_type (tree->inst_vtype);
2105
2106                 set_vreg_to_inst (cfg, inst->dreg + 1, tree);
2107
2108                 /* Allocate a dummy MonoInst for the second vreg */
2109                 MONO_INST_NEW (cfg, tree, OP_LOCAL);
2110                 tree->dreg = inst->dreg + 2;
2111                 if (cfg->opt & MONO_OPT_SSA)
2112                         tree->flags = MONO_INST_VOLATILE;
2113                 tree->inst_c0 = num;
2114                 tree->type = STACK_I4;
2115                 tree->inst_vtype = &mono_defaults.int32_class->byval_arg;
2116                 tree->klass = mono_class_from_mono_type (tree->inst_vtype);
2117
2118                 set_vreg_to_inst (cfg, inst->dreg + 2, tree);
2119         }
2120
2121         cfg->num_varinfo++;
2122         if (cfg->verbose_level > 2)
2123                 g_print ("created temp %d (R%d) of type %s\n", num, vreg, mono_type_get_name (type));
2124         return inst;
2125 }
2126
2127 MonoInst*
2128 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
2129 {
2130         int dreg;
2131
2132         if (mono_type_is_long (type))
2133                 dreg = mono_alloc_dreg (cfg, STACK_I8);
2134 #ifdef MONO_ARCH_SOFT_FLOAT
2135         else if (mono_type_is_float (type))
2136                 dreg = mono_alloc_dreg (cfg, STACK_R8);
2137 #endif
2138         else
2139                 /* All the others are unified */
2140                 dreg = mono_alloc_preg (cfg);
2141
2142         return mono_compile_create_var_for_vreg (cfg, type, opcode, dreg);
2143 }
2144
2145 /*
2146  * Transform a MonoInst into a load from the variable of index var_index.
2147  */
2148 void
2149 mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index) {
2150         memset (dest, 0, sizeof (MonoInst));
2151         dest->ssa_op = MONO_SSA_LOAD;
2152         dest->inst_i0 = cfg->varinfo [var_index];
2153         dest->opcode = mini_type_to_ldind (cfg, dest->inst_i0->inst_vtype);
2154         type_to_eval_stack_type (cfg, dest->inst_i0->inst_vtype, dest);
2155         dest->klass = dest->inst_i0->klass;
2156 }
2157
2158 /*
2159  * Create a MonoInst that is a load from the variable of index var_index.
2160  */
2161 MonoInst*
2162 mono_compile_create_var_load (MonoCompile *cfg, gssize var_index) {
2163         MonoInst *dest;
2164         NEW_TEMPLOAD (cfg,dest,var_index);
2165         return dest;
2166 }
2167
2168 /*
2169  * Create a MonoInst that is a store of the given value into the variable of index var_index.
2170  */
2171 MonoInst*
2172 mono_compile_create_var_store (MonoCompile *cfg, gssize var_index, MonoInst *value) {
2173         MonoInst *dest;
2174         NEW_TEMPSTORE (cfg, dest, var_index, value);
2175         return dest;
2176 }
2177
2178 static MonoType*
2179 type_from_stack_type (MonoInst *ins) {
2180         switch (ins->type) {
2181         case STACK_I4: return &mono_defaults.int32_class->byval_arg;
2182         case STACK_I8: return &mono_defaults.int64_class->byval_arg;
2183         case STACK_PTR: return &mono_defaults.int_class->byval_arg;
2184         case STACK_R8: return &mono_defaults.double_class->byval_arg;
2185         case STACK_MP:
2186                 /* 
2187                  * this if used to be commented without any specific reason, but
2188                  * it breaks #80235 when commented
2189                  */
2190                 if (ins->klass)
2191                         return &ins->klass->this_arg;
2192                 else
2193                         return &mono_defaults.object_class->this_arg;
2194         case STACK_OBJ:
2195                 /* ins->klass may not be set for ldnull.
2196                  * Also, if we have a boxed valuetype, we want an object lass,
2197                  * not the valuetype class
2198                  */
2199                 if (ins->klass && !ins->klass->valuetype)
2200                         return &ins->klass->byval_arg;
2201                 return &mono_defaults.object_class->byval_arg;
2202         case STACK_VTYPE: return &ins->klass->byval_arg;
2203         default:
2204                 g_error ("stack type %d to montype not handled\n", ins->type);
2205         }
2206         return NULL;
2207 }
2208
2209 MonoType*
2210 mono_type_from_stack_type (MonoInst *ins) {
2211         return type_from_stack_type (ins);
2212 }
2213
2214 static MonoClass*
2215 array_access_to_klass (int opcode, MonoInst *array_obj)
2216 {
2217         switch (opcode) {
2218         case CEE_LDELEM_U1:
2219                 return mono_defaults.byte_class;
2220         case CEE_LDELEM_U2:
2221                 return mono_defaults.uint16_class;
2222         case CEE_LDELEM_I:
2223         case CEE_STELEM_I:
2224                 return mono_defaults.int_class;
2225         case CEE_LDELEM_I1:
2226         case CEE_STELEM_I1:
2227                 return mono_defaults.sbyte_class;
2228         case CEE_LDELEM_I2:
2229         case CEE_STELEM_I2:
2230                 return mono_defaults.int16_class;
2231         case CEE_LDELEM_I4:
2232         case CEE_STELEM_I4:
2233                 return mono_defaults.int32_class;
2234         case CEE_LDELEM_U4:
2235                 return mono_defaults.uint32_class;
2236         case CEE_LDELEM_I8:
2237         case CEE_STELEM_I8:
2238                 return mono_defaults.int64_class;
2239         case CEE_LDELEM_R4:
2240         case CEE_STELEM_R4:
2241                 return mono_defaults.single_class;
2242         case CEE_LDELEM_R8:
2243         case CEE_STELEM_R8:
2244                 return mono_defaults.double_class;
2245         case CEE_LDELEM_REF:
2246         case CEE_STELEM_REF: {
2247                 MonoClass *klass = array_obj->klass;
2248                 /* FIXME: add assert */
2249                 if (klass && klass->rank)
2250                         return klass->element_class;
2251                 return mono_defaults.object_class;
2252         }
2253         default:
2254                 g_assert_not_reached ();
2255         }
2256         return NULL;
2257 }
2258
2259 /*
2260  * mono_add_ins_to_end:
2261  *
2262  *   Same as MONO_ADD_INS, but add INST before any branches at the end of BB.
2263  */
2264 void
2265 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
2266 {
2267         int opcode;
2268
2269         if (!bb->code) {
2270                 MONO_ADD_INS (bb, inst);
2271                 return;
2272         }
2273
2274         switch (bb->last_ins->opcode) {
2275         case OP_BR:
2276         case OP_BR_REG:
2277         case CEE_BEQ:
2278         case CEE_BGE:
2279         case CEE_BGT:
2280         case CEE_BLE:
2281         case CEE_BLT:
2282         case CEE_BNE_UN:
2283         case CEE_BGE_UN:
2284         case CEE_BGT_UN:
2285         case CEE_BLE_UN:
2286         case CEE_BLT_UN:
2287         case OP_SWITCH:
2288                 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2289                 break;
2290         default:
2291                 if (MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
2292                         /* Need to insert the ins before the compare */
2293                         if (bb->code == bb->last_ins) {
2294                                 mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2295                                 return;
2296                         }
2297
2298                         if (bb->code->next == bb->last_ins) {
2299                                 /* Only two instructions */
2300                                 opcode = bb->code->opcode;
2301
2302                                 if ((opcode == OP_COMPARE) || (opcode == OP_COMPARE_IMM) || (opcode == OP_ICOMPARE) || (opcode == OP_ICOMPARE_IMM) || (opcode == OP_FCOMPARE) || (opcode == OP_LCOMPARE) || (opcode == OP_LCOMPARE_IMM)) {
2303                                         /* NEW IR */
2304                                         mono_bblock_insert_before_ins (bb, bb->code, inst);
2305                                 } else {
2306                                         mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2307                                 }
2308                         } else {
2309                                 opcode = bb->last_ins->prev->opcode;
2310
2311                                 if ((opcode == OP_COMPARE) || (opcode == OP_COMPARE_IMM) || (opcode == OP_ICOMPARE) || (opcode == OP_ICOMPARE_IMM) || (opcode == OP_FCOMPARE) || (opcode == OP_LCOMPARE) || (opcode == OP_LCOMPARE_IMM)) {
2312                                         /* NEW IR */
2313                                         mono_bblock_insert_before_ins (bb, bb->last_ins->prev, inst);
2314                                 } else {
2315                                         mono_bblock_insert_before_ins (bb, bb->last_ins, inst);
2316                                 }                                       
2317                         }
2318                 }
2319                 else
2320                         MONO_ADD_INS (bb, inst);
2321                 break;
2322         }
2323 }
2324
2325 /**
2326  * mono_replace_ins:
2327  *
2328  *   Replace INS with its decomposition which is stored in a series of bblocks starting
2329  * at FIRST_BB and ending at LAST_BB. On enter, PREV points to the predecessor of INS. 
2330  * On return, it will be set to the last ins of the decomposition.
2331  */
2332 void
2333 mono_replace_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb)
2334 {
2335         MonoInst *next = ins->next;
2336
2337         if (next && next->opcode == OP_NOP) {
2338                 /* Avoid NOPs following branches */
2339                 ins->next = next->next;
2340                 next = next->next;
2341         }
2342
2343         if (first_bb == last_bb) {
2344                 /* 
2345                  * Only one replacement bb, merge the code into
2346                  * the current bb.
2347                  */
2348
2349                 /* Delete links between the first_bb and its successors */
2350                 while (first_bb->out_count)
2351                         mono_unlink_bblock (cfg, first_bb, first_bb->out_bb [0]);
2352
2353                 /* Head */
2354                 if (*prev) {
2355                         (*prev)->next = first_bb->code;
2356                         first_bb->code->prev = (*prev);
2357                 } else {
2358                         bb->code = first_bb->code;
2359                 }
2360
2361                 /* Tail */
2362                 last_bb->last_ins->next = next;
2363                 if (next)
2364                         next->prev = last_bb->last_ins;
2365                 else
2366                         bb->last_ins = last_bb->last_ins;
2367                 *prev = last_bb->last_ins;
2368         } else {
2369                 int i, count;
2370                 MonoBasicBlock **tmp_bblocks, *tmp;
2371                 MonoInst *last;
2372
2373                 /* Multiple BBs */
2374
2375                 /* Set region */
2376                 for (tmp = first_bb; tmp; tmp = tmp->next_bb)
2377                         tmp->region = bb->region;
2378
2379                 /* Split the original bb */
2380                 if (ins->next)
2381                         ins->next->prev = NULL;
2382                 ins->next = NULL;
2383                 bb->last_ins = ins;
2384
2385                 /* Merge the second part of the original bb into the last bb */
2386                 if (last_bb->last_ins) {
2387                         last_bb->last_ins->next = next;
2388                         if (next)
2389                                 next->prev = last_bb->last_ins;
2390                 } else {
2391                         last_bb->code = next;
2392                 }
2393
2394                 if (next) {
2395                         for (last = next; last->next != NULL; last = last->next)
2396                                 ;
2397                         last_bb->last_ins = last;
2398                 }
2399
2400                 for (i = 0; i < bb->out_count; ++i)
2401                         link_bblock (cfg, last_bb, bb->out_bb [i]);
2402
2403                 /* Merge the first (dummy) bb to the original bb */
2404                 if (*prev) {
2405                         (*prev)->next = first_bb->code;
2406                         first_bb->code->prev = (*prev);
2407                 } else {
2408                         bb->code = first_bb->code;
2409                 }
2410                 bb->last_ins = first_bb->last_ins;
2411
2412                 /* Delete the links between the original bb and its successors */
2413                 tmp_bblocks = bb->out_bb;
2414                 count = bb->out_count;
2415                 for (i = 0; i < count; ++i)
2416                         mono_unlink_bblock (cfg, bb, tmp_bblocks [i]);
2417
2418                 /* Add links between the original bb and the first_bb's successors */
2419                 for (i = 0; i < first_bb->out_count; ++i) {
2420                         MonoBasicBlock *out_bb = first_bb->out_bb [i];
2421
2422                         link_bblock (cfg, bb, out_bb);
2423                 }
2424                 /* Delete links between the first_bb and its successors */
2425                 for (i = 0; i < bb->out_count; ++i) {
2426                         MonoBasicBlock *out_bb = bb->out_bb [i];
2427
2428                         mono_unlink_bblock (cfg, first_bb, out_bb);
2429                 }
2430                 last_bb->next_bb = bb->next_bb;
2431                 bb->next_bb = first_bb->next_bb;
2432
2433                 *prev = NULL;
2434         }
2435 }
2436
2437 void
2438 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
2439 {
2440         MonoInst *inst, *load;
2441
2442         NEW_TEMPLOAD (cfg, load, src);
2443
2444         NEW_TEMPSTORE (cfg, inst, dest, load);
2445         /* FIXME: handle CEE_STIND_R4 */
2446         if (inst->opcode == CEE_STOBJ) {
2447                 NEW_TEMPLOADA (cfg, inst, dest);
2448                 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE, FALSE);
2449         } else {
2450                 inst->cil_code = NULL;
2451                 mono_add_ins_to_end (bb, inst);
2452         }
2453 }
2454
2455 /*
2456  * This function is called to handle items that are left on the evaluation stack
2457  * at basic block boundaries. What happens is that we save the values to local variables
2458  * and we reload them later when first entering the target basic block (with the
2459  * handle_loaded_temps () function).
2460  * It is also used to handle items on the stack in store opcodes, since it is
2461  * possible that the variable to be stored into is already on the stack, in
2462  * which case its old value should be used.
2463  * A single joint point will use the same variables (stored in the array bb->out_stack or
2464  * bb->in_stack, if the basic block is before or after the joint point).
2465  * If the stack merge fails at a join point, cfg->unverifiable is set.
2466  */
2467 static void
2468 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count)
2469 {
2470         int i, bindex;
2471         MonoBasicBlock *outb;
2472         MonoInst *inst, **locals;
2473         gboolean found;
2474
2475         if (!count)
2476                 return;
2477         if (cfg->verbose_level > 3)
2478                 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
2479
2480         if (!bb->out_scount) {
2481                 bb->out_scount = count;
2482                 //g_print ("bblock %d has out:", bb->block_num);
2483                 found = FALSE;
2484                 for (i = 0; i < bb->out_count; ++i) {
2485                         outb = bb->out_bb [i];
2486                         /* exception handlers are linked, but they should not be considered for stack args */
2487                         if (outb->flags & BB_EXCEPTION_HANDLER)
2488                                 continue;
2489                         //g_print (" %d", outb->block_num);
2490                         if (outb->in_stack) {
2491                                 found = TRUE;
2492                                 bb->out_stack = outb->in_stack;
2493                                 break;
2494                         }
2495                 }
2496                 //g_print ("\n");
2497                 if (!found) {
2498                         bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
2499                         for (i = 0; i < count; ++i) {
2500                                 /* 
2501                                  * try to reuse temps already allocated for this purpouse, if they occupy the same
2502                                  * stack slot and if they are of the same type.
2503                                  * This won't cause conflicts since if 'local' is used to 
2504                                  * store one of the values in the in_stack of a bblock, then
2505                                  * the same variable will be used for the same outgoing stack 
2506                                  * slot as well. 
2507                                  * This doesn't work when inlining methods, since the bblocks
2508                                  * in the inlined methods do not inherit their in_stack from
2509                                  * the bblock they are inlined to. See bug #58863 for an
2510                                  * example.
2511                                  * This hack is disabled since it also prevents proper tracking of types.
2512                                  */
2513 #if 1
2514                                 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
2515 #else
2516                                 if (cfg->inlined_method)
2517                                         bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
2518                                 else
2519                                         bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
2520 #endif
2521                         }
2522                 }
2523         }
2524
2525         for (i = 0; i < bb->out_count; ++i) {
2526                 outb = bb->out_bb [i];
2527                 /* exception handlers are linked, but they should not be considered for stack args */
2528                 if (outb->flags & BB_EXCEPTION_HANDLER)
2529                         continue;
2530                 if (outb->in_scount) {
2531                         if (outb->in_scount != bb->out_scount) {
2532                                 cfg->unverifiable = TRUE;
2533                                 return;
2534                         }
2535                         continue; /* check they are the same locals */
2536                 }
2537                 outb->in_scount = count;
2538                 outb->in_stack = bb->out_stack;
2539         }
2540
2541         locals = bb->out_stack;
2542         for (i = 0; i < count; ++i) {
2543                 /* add store ops at the end of the bb, before the branch */
2544                 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
2545                 if (inst->opcode == CEE_STOBJ) {
2546                         NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
2547                         handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE, FALSE);
2548                 } else {
2549                         inst->cil_code = sp [i]->cil_code;
2550                         mono_add_ins_to_end (bb, inst);
2551                 }
2552                 if (cfg->verbose_level > 3)
2553                         g_print ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
2554         }
2555
2556         /*
2557          * It is possible that the out bblocks already have in_stack assigned, and
2558          * the in_stacks differ. In this case, we will store to all the different 
2559          * in_stacks.
2560          */
2561
2562         found = TRUE;
2563         bindex = 0;
2564         while (found) {
2565                 /* Find a bblock which has a different in_stack */
2566                 found = FALSE;
2567                 while (bindex < bb->out_count) {
2568                         outb = bb->out_bb [bindex];
2569                         /* exception handlers are linked, but they should not be considered for stack args */
2570                         if (outb->flags & BB_EXCEPTION_HANDLER) {
2571                                 bindex++;
2572                                 continue;
2573                         }
2574                         if (outb->in_stack != locals) {
2575                                 /* 
2576                                  * Instead of storing sp [i] to locals [i], we need to store
2577                                  * locals [i] to <new locals>[i], since the sp [i] tree can't
2578                                  * be shared between trees.
2579                                  */
2580                                 for (i = 0; i < count; ++i)
2581                                         mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
2582                                 locals = outb->in_stack;
2583                                 found = TRUE;
2584                                 break;
2585                         }
2586                         bindex ++;
2587                 }
2588         }
2589 }
2590
2591 static int
2592 ret_type_to_call_opcode (MonoType *type, int calli, int virt, MonoGenericSharingContext *gsctx)
2593 {
2594         if (type->byref)
2595                 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2596
2597 handle_enum:
2598         type = mini_get_basic_type_from_generic (gsctx, type);
2599         switch (type->type) {
2600         case MONO_TYPE_VOID:
2601                 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
2602         case MONO_TYPE_I1:
2603         case MONO_TYPE_U1:
2604         case MONO_TYPE_BOOLEAN:
2605         case MONO_TYPE_I2:
2606         case MONO_TYPE_U2:
2607         case MONO_TYPE_CHAR:
2608         case MONO_TYPE_I4:
2609         case MONO_TYPE_U4:
2610                 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2611         case MONO_TYPE_I:
2612         case MONO_TYPE_U:
2613         case MONO_TYPE_PTR:
2614         case MONO_TYPE_FNPTR:
2615                 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2616         case MONO_TYPE_CLASS:
2617         case MONO_TYPE_STRING:
2618         case MONO_TYPE_OBJECT:
2619         case MONO_TYPE_SZARRAY:
2620         case MONO_TYPE_ARRAY:    
2621                 return calli? OP_CALL_REG: virt? OP_CALLVIRT: OP_CALL;
2622         case MONO_TYPE_I8:
2623         case MONO_TYPE_U8:
2624                 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
2625         case MONO_TYPE_R4:
2626         case MONO_TYPE_R8:
2627                 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
2628         case MONO_TYPE_VALUETYPE:
2629                 if (type->data.klass->enumtype) {
2630                         type = type->data.klass->enum_basetype;
2631                         goto handle_enum;
2632                 } else
2633                         return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2634         case MONO_TYPE_TYPEDBYREF:
2635                 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2636         case MONO_TYPE_GENERICINST:
2637                 type = &type->data.generic_class->container_class->byval_arg;
2638                 goto handle_enum;
2639         default:
2640                 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2641         }
2642         return -1;
2643 }
2644
2645 void
2646 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
2647 {
2648         MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
2649         MonoJumpInfoBBTable *table;
2650
2651         table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
2652         table->table = bbs;
2653         table->table_size = num_blocks;
2654         
2655         ji->ip.label = label;
2656         ji->type = MONO_PATCH_INFO_SWITCH;
2657         ji->data.table = table;
2658         ji->next = cfg->patch_info;
2659         cfg->patch_info = ji;
2660 }
2661
2662 static void
2663 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
2664 {
2665         if (cfg->compile_aot) {
2666                 MonoJumpInfoToken *jump_info_token = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
2667                 jump_info_token->image = image;
2668                 jump_info_token->token = token;
2669                 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
2670         }
2671 }
2672
2673 /*
2674  * When we add a tree of instructions, we need to ensure the instructions currently
2675  * on the stack are executed before (like, if we load a value from a local).
2676  * We ensure this by saving the currently loaded values to temps and rewriting the
2677  * instructions to load the values.
2678  * This is not done for opcodes that terminate a basic block (because it's handled already
2679  * by handle_stack_args ()) and for opcodes that can't change values, like POP.
2680  */
2681 static void
2682 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
2683 {
2684         MonoInst *load, *store, *temp, *ins;
2685
2686         while (stack < sp) {
2687                 ins = *stack;
2688                 /* handle also other constants */
2689                 if ((ins->opcode != OP_ICONST) &&
2690                     /* temps never get written to again, so we can safely avoid duplicating them */
2691                     !(ins->ssa_op == MONO_SSA_LOAD && ins->inst_i0->opcode == OP_LOCAL && ins->inst_i0->flags & MONO_INST_IS_TEMP)) {
2692                         temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2693                         temp->flags |= MONO_INST_IS_TEMP;
2694                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2695                         store->cil_code = ins->cil_code;
2696                         if (store->opcode == CEE_STOBJ) {
2697                                 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2698                                 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE, FALSE);
2699                         } else
2700                                 MONO_ADD_INS (bblock, store);
2701                         NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2702                         load->cil_code = ins->cil_code;
2703                         *stack = load;
2704                 }
2705                 stack++;
2706         }
2707 }
2708
2709 /*
2710  * target_type_is_incompatible:
2711  * @cfg: MonoCompile context
2712  *
2713  * Check that the item @arg on the evaluation stack can be stored
2714  * in the target type (can be a local, or field, etc).
2715  * The cfg arg can be used to check if we need verification or just
2716  * validity checks.
2717  *
2718  * Returns: non-0 value if arg can't be stored on a target.
2719  */
2720 static int
2721 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2722 {
2723         MonoType *simple_type;
2724         MonoClass *klass;
2725
2726         if (target->byref) {
2727                 /* FIXME: check that the pointed to types match */
2728                 if (arg->type == STACK_MP)
2729                         return arg->klass != mono_class_from_mono_type (target);
2730                 if (arg->type == STACK_PTR)
2731                         return 0;
2732                 return 1;
2733         }
2734         simple_type = mono_type_get_underlying_type (target);
2735         switch (simple_type->type) {
2736         case MONO_TYPE_VOID:
2737                 return 1;
2738         case MONO_TYPE_I1:
2739         case MONO_TYPE_U1:
2740         case MONO_TYPE_BOOLEAN:
2741         case MONO_TYPE_I2:
2742         case MONO_TYPE_U2:
2743         case MONO_TYPE_CHAR:
2744         case MONO_TYPE_I4:
2745         case MONO_TYPE_U4:
2746                 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2747                         return 1;
2748                 return 0;
2749         case MONO_TYPE_PTR:
2750                 /* STACK_MP is needed when setting pinned locals */
2751                 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2752                         return 1;
2753                 return 0;
2754         case MONO_TYPE_I:
2755         case MONO_TYPE_U:
2756         case MONO_TYPE_FNPTR:
2757                 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2758                         return 1;
2759                 return 0;
2760         case MONO_TYPE_OBJECT:
2761                 if (arg->type != STACK_OBJ)
2762                         return 1;
2763                 return 0;
2764         case MONO_TYPE_STRING:
2765                 if (arg->type != STACK_OBJ)
2766                         return 1;
2767                 /* ldnull has arg->klass unset */
2768                 /*if (arg->klass && arg->klass != mono_defaults.string_class) {
2769                         G_BREAKPOINT ();
2770                         return 1;
2771                 }*/
2772                 return 0;
2773         case MONO_TYPE_CLASS:
2774         case MONO_TYPE_SZARRAY:
2775         case MONO_TYPE_ARRAY:    
2776                 if (arg->type != STACK_OBJ)
2777                         return 1;
2778                 /* FIXME: check type compatibility */
2779                 return 0;
2780         case MONO_TYPE_I8:
2781         case MONO_TYPE_U8:
2782                 if (arg->type != STACK_I8)
2783                         return 1;
2784                 return 0;
2785         case MONO_TYPE_R4:
2786         case MONO_TYPE_R8:
2787                 if (arg->type != STACK_R8)
2788                         return 1;
2789                 return 0;
2790         case MONO_TYPE_VALUETYPE:
2791                 if (arg->type != STACK_VTYPE)
2792                         return 1;
2793                 klass = mono_class_from_mono_type (simple_type);
2794                 if (klass != arg->klass)
2795                         return 1;
2796                 return 0;
2797         case MONO_TYPE_TYPEDBYREF:
2798                 if (arg->type != STACK_VTYPE)
2799                         return 1;
2800                 klass = mono_class_from_mono_type (simple_type);
2801                 if (klass != arg->klass)
2802                         return 1;
2803                 return 0;
2804         case MONO_TYPE_GENERICINST:
2805                 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2806                         klass = mono_class_from_mono_type (simple_type);
2807                         if (klass->enumtype)
2808                                 return target_type_is_incompatible (cfg, klass->enum_basetype, arg);
2809                         if (arg->type != STACK_VTYPE)
2810                                 return 1;
2811                         if (klass != arg->klass)
2812                                 return 1;
2813                         return 0;
2814                 } else {
2815                         if (arg->type != STACK_OBJ)
2816                                 return 1;
2817                         /* FIXME: check type compatibility */
2818                         return 0;
2819                 }
2820         case MONO_TYPE_VAR:
2821         case MONO_TYPE_MVAR:
2822                 /* FIXME: all the arguments must be references for now,
2823                  * later look inside cfg and see if the arg num is
2824                  * really a reference
2825                  */
2826                 g_assert (cfg->generic_sharing_context);
2827                 if (arg->type != STACK_OBJ)
2828                         return 1;
2829                 return 0;
2830         default:
2831                 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2832         }
2833         return 1;
2834 }
2835
2836 /*
2837  * Prepare arguments for passing to a function call.
2838  * Return a non-zero value if the arguments can't be passed to the given
2839  * signature.
2840  * The type checks are not yet complete and some conversions may need
2841  * casts on 32 or 64 bit architectures.
2842  *
2843  * FIXME: implement this using target_type_is_incompatible ()
2844  */
2845 static int
2846 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2847 {
2848         MonoType *simple_type;
2849         int i;
2850
2851         if (sig->hasthis) {
2852                 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2853                         return 1;
2854                 args++;
2855         }
2856         for (i = 0; i < sig->param_count; ++i) {
2857                 if (sig->params [i]->byref) {
2858                         if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2859                                 return 1;
2860                         continue;
2861                 }
2862                 simple_type = sig->params [i];
2863                 simple_type = mini_get_basic_type_from_generic (cfg->generic_sharing_context, simple_type);
2864 handle_enum:
2865                 switch (simple_type->type) {
2866                 case MONO_TYPE_VOID:
2867                         return 1;
2868                         continue;
2869                 case MONO_TYPE_I1:
2870                 case MONO_TYPE_U1:
2871                 case MONO_TYPE_BOOLEAN:
2872                 case MONO_TYPE_I2:
2873                 case MONO_TYPE_U2:
2874                 case MONO_TYPE_CHAR:
2875                 case MONO_TYPE_I4:
2876                 case MONO_TYPE_U4:
2877                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2878                                 return 1;
2879                         continue;
2880                 case MONO_TYPE_I:
2881                 case MONO_TYPE_U:
2882                 case MONO_TYPE_PTR:
2883                 case MONO_TYPE_FNPTR:
2884                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2885                                 return 1;
2886                         continue;
2887                 case MONO_TYPE_CLASS:
2888                 case MONO_TYPE_STRING:
2889                 case MONO_TYPE_OBJECT:
2890                 case MONO_TYPE_SZARRAY:
2891                 case MONO_TYPE_ARRAY:    
2892                         if (args [i]->type != STACK_OBJ)
2893                                 return 1;
2894                         continue;
2895                 case MONO_TYPE_I8:
2896                 case MONO_TYPE_U8:
2897                         if (args [i]->type != STACK_I8)
2898                                 return 1;
2899                         continue;
2900                 case MONO_TYPE_R4:
2901                 case MONO_TYPE_R8:
2902                         if (args [i]->type != STACK_R8)
2903                                 return 1;
2904                         continue;
2905                 case MONO_TYPE_VALUETYPE:
2906                         if (simple_type->data.klass->enumtype) {
2907                                 simple_type = simple_type->data.klass->enum_basetype;
2908                                 goto handle_enum;
2909                         }
2910                         if (args [i]->type != STACK_VTYPE)
2911                                 return 1;
2912                         continue;
2913                 case MONO_TYPE_TYPEDBYREF:
2914                         if (args [i]->type != STACK_VTYPE)
2915                                 return 1;
2916                         continue;
2917                 case MONO_TYPE_GENERICINST:
2918                         simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2919                         goto handle_enum;
2920
2921                 default:
2922                         g_error ("unknown type 0x%02x in check_call_signature",
2923                                  simple_type->type);
2924                 }
2925         }
2926         return 0;
2927 }
2928
2929 inline static int
2930 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object, 
2931                  const guint8 *ip, gboolean to_end)
2932 {
2933         MonoInst *temp, *store, *ins = (MonoInst*)call;
2934         MonoType *ret = sig->ret;
2935
2936         if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
2937                 if (ret_object) {
2938                         call->inst.type = STACK_OBJ;
2939                         call->inst.opcode = OP_CALL;
2940                         temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
2941                 } else {
2942                         type_to_eval_stack_type (cfg, ret, ins);
2943                         temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
2944                 }
2945                 
2946                 temp->flags |= MONO_INST_IS_TEMP;
2947
2948                 if (MONO_TYPE_ISSTRUCT (ret)) {
2949                         MonoInst *loada, *dummy_store;
2950
2951                         /* 
2952                          * Emit a dummy store to the local holding the result so the
2953                          * liveness info remains correct.
2954                          */
2955                         NEW_DUMMY_STORE (cfg, dummy_store, temp->inst_c0);
2956                         if (to_end)
2957                                 mono_add_ins_to_end (bblock, dummy_store);
2958                         else
2959                                 MONO_ADD_INS (bblock, dummy_store);
2960
2961                         /* we use this to allocate native sized structs */
2962                         temp->backend.is_pinvoke = sig->pinvoke;
2963
2964                         NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
2965                         if (call->inst.opcode == OP_VCALL || call->inst.opcode == OP_VCALL_RGCTX)
2966                                 ins->inst_left = loada;
2967                         else
2968                                 ins->inst_right = loada; /* a virtual or indirect call */
2969
2970                         if (to_end)
2971                                 mono_add_ins_to_end (bblock, ins);
2972                         else
2973                                 MONO_ADD_INS (bblock, ins);
2974                 } else {
2975                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2976                         store->cil_code = ip;
2977                         
2978 #ifdef MONO_ARCH_SOFT_FLOAT
2979                         if (store->opcode == CEE_STIND_R4) {
2980                                 /*FIXME implement proper support for to_end*/
2981                                 g_assert (!to_end);
2982                                 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2983                                 handle_store_float (cfg, bblock, store, ins, ip);
2984                         } else
2985 #endif
2986                         if (to_end)
2987                                 mono_add_ins_to_end (bblock, store);
2988                         else
2989                                 MONO_ADD_INS (bblock, store);
2990                 }
2991                 return temp->inst_c0;
2992         } else {
2993                 if (to_end)
2994                         mono_add_ins_to_end (bblock, ins);
2995                 else
2996                         MONO_ADD_INS (bblock, ins);
2997                 return -1;
2998         }
2999 }
3000
3001 inline static MonoCallInst *
3002 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, 
3003                      MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
3004 {
3005         MonoCallInst *call;
3006         MonoInst *arg;
3007
3008         MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual, cfg->generic_sharing_context));
3009
3010 #ifdef MONO_ARCH_SOFT_FLOAT
3011         /* we need to convert the r4 value to an int value */
3012         {
3013                 int i;
3014                 for (i = 0; i < sig->param_count; ++i) {
3015                         if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4) {
3016                                 MonoInst *iargs [1];
3017                                 int temp;
3018                                 iargs [0] = args [i + sig->hasthis];
3019
3020                                 temp = mono_emit_jit_icall (cfg, bblock, mono_fload_r4_arg, iargs, ip);
3021                                 NEW_TEMPLOAD (cfg, arg, temp);
3022                                 args [i + sig->hasthis] = arg;
3023                         }
3024                 }
3025         }
3026 #endif
3027
3028         call->inst.cil_code = ip;
3029         call->args = args;
3030         call->signature = sig;
3031         call = mono_arch_call_opcode (cfg, bblock, call, virtual);
3032         type_to_eval_stack_type (cfg, sig->ret, &call->inst);
3033
3034         for (arg = call->out_args; arg;) {
3035                 MonoInst *narg = arg->next;
3036                 arg->next = NULL;
3037                 if (!arg->cil_code)
3038                         arg->cil_code = ip;
3039                 if (to_end)
3040                         mono_add_ins_to_end (bblock, arg);
3041                 else
3042                         MONO_ADD_INS (bblock, arg);
3043                 arg = narg;
3044         }
3045         return call;
3046 }
3047
3048 inline static MonoCallInst*
3049 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, 
3050                  MonoInst **args, MonoInst *addr, const guint8 *ip)
3051 {
3052         MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
3053
3054         call->inst.inst_i0 = addr;
3055
3056         return call;
3057 }
3058
3059 inline static MonoCallInst*
3060 mono_emit_rgctx_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3061         MonoInst **args, MonoInst *addr, MonoInst *rgctx_arg, const guint8 *ip)
3062 {
3063         MonoCallInst *call = mono_emit_calli (cfg, bblock, sig, args, addr, ip);
3064
3065         if (rgctx_arg) {
3066                 switch (call->inst.opcode) {
3067                 case OP_CALL_REG: call->inst.opcode = OP_CALL_REG_RGCTX; break;
3068                 case OP_VOIDCALL_REG: call->inst.opcode = OP_VOIDCALL_REG_RGCTX; break;
3069                 case OP_FCALL_REG: call->inst.opcode = OP_FCALL_REG_RGCTX; break;
3070                 case OP_LCALL_REG: call->inst.opcode = OP_LCALL_REG_RGCTX; break;
3071                 case OP_VCALL_REG: {
3072                         MonoInst *group;
3073
3074                         NEW_GROUP (cfg, group, call->inst.inst_left, NULL);
3075                         call->inst.inst_left = group;
3076                         call->inst.opcode = OP_VCALL_REG_RGCTX;
3077                         break;
3078                 }
3079                 default: g_assert_not_reached ();
3080                 }
3081
3082                 if (call->inst.opcode != OP_VCALL_REG_RGCTX) {
3083                         g_assert (!call->inst.inst_right);
3084                         call->inst.inst_right = rgctx_arg;
3085                 } else {
3086                         g_assert (!call->inst.inst_left->inst_right);
3087                         call->inst.inst_left->inst_right = rgctx_arg;
3088                 }
3089         }
3090
3091         return call;
3092 }
3093
3094 inline static int
3095 mono_emit_calli_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, 
3096                                                  MonoInst **args, MonoInst *addr, const guint8 *ip)
3097 {
3098         MonoCallInst *call = mono_emit_calli (cfg, bblock, sig, args, addr, ip);
3099
3100         return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
3101 }
3102
3103 static int
3104 mono_emit_rgctx_calli_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
3105         MonoInst **args, MonoInst *addr, MonoInst *rgctx_arg, const guint8 *ip)
3106 {
3107         MonoCallInst *call = mono_emit_rgctx_calli (cfg, bblock, sig, args, addr, rgctx_arg, ip);
3108
3109         return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
3110 }
3111
3112 static MonoCallInst*
3113 mono_emit_method_call_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
3114                        MonoInst **args, const guint8 *ip, MonoInst *this, gboolean to_end)
3115 {
3116         gboolean virtual = this != NULL;
3117         MonoCallInst *call;
3118
3119         call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, to_end);
3120
3121         if (this && sig->hasthis && 
3122             (method->klass->marshalbyref || method->klass == mono_defaults.object_class) && 
3123             !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
3124                 call->method = mono_marshal_get_remoting_invoke_with_check (method);
3125         } else {
3126                 call->method = method;
3127         }
3128         call->inst.flags |= MONO_INST_HAS_METHOD;
3129         call->inst.inst_left = this;
3130
3131         if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
3132                 /* Needed by the code generated in inssel.brg */
3133                 mono_get_got_var (cfg);
3134
3135         return call;
3136 }
3137
3138 static MonoCallInst*
3139 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
3140                        MonoInst **args, const guint8 *ip, MonoInst *this)
3141 {
3142         return mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
3143 }
3144
3145 inline static int
3146 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,  
3147                        MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
3148 {
3149         MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
3150
3151         return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
3152 }
3153
3154 inline static int
3155 mono_emit_method_call_spilled_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,  
3156                        MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this,
3157                        gboolean ret_object, gboolean to_end)
3158 {
3159         MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, signature, args, ip, this, to_end);
3160
3161         return mono_spill_call (cfg, bblock, call, signature, ret_object, ip, to_end);
3162 }
3163
3164 inline static int
3165 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
3166                        MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
3167 {
3168         MonoCallInst *call;
3169
3170         g_assert (sig);
3171
3172         call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
3173         call->fptr = func;
3174
3175         return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
3176 }
3177
3178 inline static int
3179 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
3180 {
3181         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
3182         
3183         if (!info) {
3184                 g_warning ("unregistered JIT ICall");
3185                 g_assert_not_reached ();
3186         }
3187
3188         return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
3189 }
3190
3191 static MonoCallInst*
3192 mono_emit_rgctx_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
3193                 MonoInst **args, MonoInst *rgctx_arg, MonoInst *imt_arg, const guint8 *ip, MonoInst *this)
3194 {
3195         MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
3196
3197         g_assert (!(rgctx_arg && imt_arg));
3198
3199         if (rgctx_arg) {
3200                 switch (call->inst.opcode) {
3201                 case OP_CALL: call->inst.opcode = OP_CALL_RGCTX; break;
3202                 case OP_VOIDCALL: call->inst.opcode = OP_VOIDCALL_RGCTX; break;
3203                 case OP_FCALL: call->inst.opcode = OP_FCALL_RGCTX; break;
3204                 case OP_LCALL: call->inst.opcode = OP_LCALL_RGCTX; break;
3205                 case OP_VCALL: call->inst.opcode = OP_VCALL_RGCTX; break;
3206                 default: g_assert_not_reached ();
3207                 }
3208
3209                 if (call->inst.opcode != OP_VCALL_RGCTX) {
3210                         g_assert (!call->inst.inst_left);
3211                         call->inst.inst_left = rgctx_arg;
3212                 } else {
3213                         g_assert (!call->inst.inst_right);
3214                         call->inst.inst_right = rgctx_arg;
3215                 }
3216         } else if (imt_arg) {
3217                 switch (call->inst.opcode) {
3218                 case OP_CALLVIRT: call->inst.opcode = OP_CALLVIRT_IMT; break;
3219                 case OP_VOIDCALLVIRT: call->inst.opcode = OP_VOIDCALLVIRT_IMT; break;
3220                 case OP_FCALLVIRT: call->inst.opcode = OP_FCALLVIRT_IMT; break;
3221                 case OP_LCALLVIRT: call->inst.opcode = OP_LCALLVIRT_IMT; break;
3222                 case OP_VCALLVIRT: {
3223                         MonoInst *group;
3224
3225                         NEW_GROUP (cfg, group, call->inst.inst_left, NULL);
3226                         call->inst.inst_left = group;
3227                         call->inst.opcode = OP_VCALLVIRT_IMT;
3228                         break;
3229                 }
3230                 default: g_assert_not_reached ();
3231                 }
3232
3233                 if (call->inst.opcode != OP_VCALLVIRT_IMT) {
3234                         g_assert (!call->inst.inst_right);
3235                         call->inst.inst_right = imt_arg;
3236                 } else {
3237                         g_assert (!call->inst.inst_left->inst_right);
3238                         call->inst.inst_left->inst_right = imt_arg;
3239                 }
3240         }
3241
3242         return call;
3243 }
3244
3245 inline static int
3246 mono_emit_rgctx_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,  
3247                 MonoMethodSignature *signature, MonoInst **args, MonoInst *rgctx_arg, MonoInst *imt_arg,
3248                 const guint8 *ip, MonoInst *this)
3249 {
3250         MonoCallInst *call = mono_emit_rgctx_method_call (cfg, bblock, method, signature, args, rgctx_arg, imt_arg, ip, this);
3251
3252         return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
3253 }
3254
3255 static void
3256 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
3257 {
3258         MonoInst *ins, *temp = NULL, *store, *load, *begin;
3259         MonoInst *last_arg = NULL;
3260         int nargs;
3261         MonoCallInst *call;
3262
3263         //g_print ("emulating: ");
3264         //mono_print_tree_nl (tree);
3265         MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE, cfg->generic_sharing_context));
3266         ins = (MonoInst*)call;
3267         
3268         call->inst.cil_code = tree->cil_code;
3269         call->args = iargs;
3270         call->signature = info->sig;
3271
3272         call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
3273
3274         if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3275                 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
3276                 temp->flags |= MONO_INST_IS_TEMP;
3277                 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3278                 /* FIXME: handle CEE_STIND_R4 */
3279                 store->cil_code = tree->cil_code;
3280         } else {
3281                 store = ins;
3282         }
3283
3284         nargs = info->sig->param_count + info->sig->hasthis;
3285
3286         for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
3287
3288         if (nargs)
3289                 last_arg->next = store;
3290
3291         if (nargs)
3292                 begin = call->out_args;
3293         else
3294                 begin = store;
3295
3296         if (cfg->prev_ins) {
3297                 /* 
3298                  * This assumes that that in a tree, emulate_opcode is called for a
3299                  * node before it is called for its children. dec_foreach needs to
3300                  * take this into account.
3301                  */
3302                 store->next = cfg->prev_ins->next;
3303                 cfg->prev_ins->next = begin;
3304         } else {
3305                 store->next = cfg->cbb->code;
3306                 cfg->cbb->code = begin;
3307         }
3308
3309         call->fptr = mono_icall_get_wrapper (info);
3310
3311         if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
3312                 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3313                 *tree = *load;
3314         }
3315 }
3316
3317 /*
3318  * This entry point could be used later for arbitrary method
3319  * redirection.
3320  */
3321 inline static int
3322 mini_redirect_call (int *temp, MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,  
3323                        MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
3324 {
3325
3326         if (method->klass == mono_defaults.string_class) {
3327                 /* managed string allocation support */
3328                 if (strcmp (method->name, "InternalAllocateStr") == 0) {
3329                         MonoInst *iargs [2];
3330                         MonoVTable *vtable = mono_class_vtable (cfg->domain, method->klass);
3331                         MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, FALSE);
3332                         if (!managed_alloc)
3333                                 return FALSE;
3334                         NEW_VTABLECONST (cfg, iargs [0], vtable);
3335                         iargs [1] = args [0];
3336                         *temp = mono_emit_method_call_spilled (cfg, bblock, managed_alloc, mono_method_signature (managed_alloc), iargs, ip, this);
3337                         return TRUE;
3338                 }
3339         }
3340         return FALSE;
3341 }
3342
3343 static MonoMethodSignature *
3344 mono_get_array_new_va_signature (int arity)
3345 {
3346         static GHashTable *sighash = NULL;
3347         MonoMethodSignature *res;
3348         int i;
3349
3350         mono_jit_lock ();
3351         if (!sighash) {
3352                 sighash = g_hash_table_new (NULL, NULL);
3353         }
3354         else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
3355                 mono_jit_unlock ();
3356                 return res;
3357         }
3358
3359         res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
3360
3361         res->pinvoke = 1;
3362 #ifdef MONO_ARCH_VARARG_ICALLS
3363         /* Only set this only some archs since not all backends can handle varargs+pinvoke */
3364         res->call_convention = MONO_CALL_VARARG;
3365 #endif
3366
3367 #ifdef PLATFORM_WIN32
3368         res->call_convention = MONO_CALL_C;
3369 #endif
3370
3371         res->params [0] = &mono_defaults.int_class->byval_arg;  
3372         for (i = 0; i < arity; i++)
3373                 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
3374
3375         res->ret = &mono_defaults.object_class->byval_arg;
3376
3377         g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
3378         mono_jit_unlock ();
3379
3380         return res;
3381 }
3382
3383 MonoJitICallInfo *
3384 mono_get_array_new_va_icall (int rank)
3385 {
3386         MonoMethodSignature *esig;
3387         char icall_name [256];
3388         char *name;
3389         MonoJitICallInfo *info;
3390
3391         /* Need to register the icall so it gets an icall wrapper */
3392         sprintf (icall_name, "ves_array_new_va_%d", rank);
3393
3394         mono_jit_lock ();
3395         info = mono_find_jit_icall_by_name (icall_name);
3396         if (info == NULL) {
3397                 esig = mono_get_array_new_va_signature (rank);
3398                 name = g_strdup (icall_name);
3399                 info = mono_register_jit_icall (mono_array_new_va, name, esig, FALSE);
3400
3401                 g_hash_table_insert (jit_icall_name_hash, name, name);
3402         }
3403         mono_jit_unlock ();
3404
3405         return info;
3406 }
3407
3408 static MonoMethod*
3409 get_memcpy_method (void)
3410 {
3411         static MonoMethod *memcpy_method = NULL;
3412         if (!memcpy_method) {
3413                 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
3414                 if (!memcpy_method)
3415                         g_error ("Old corlib found. Install a new one");
3416         }
3417         return memcpy_method;
3418 }
3419
3420 static void
3421 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier) {
3422         MonoInst *iargs [3];
3423         int n;
3424         guint32 align = 0;
3425         MonoMethod *memcpy_method;
3426
3427         g_assert (klass);
3428         /*
3429          * This check breaks with spilled vars... need to handle it during verification anyway.
3430          * g_assert (klass && klass == src->klass && klass == dest->klass);
3431          */
3432
3433         if (native)
3434                 n = mono_class_native_size (klass, &align);
3435         else
3436                 n = mono_class_value_size (klass, &align);
3437
3438 #if HAVE_WRITE_BARRIERS
3439         /* if native is true there should be no references in the struct */
3440         if (write_barrier && klass->has_references && !native) {
3441                 iargs [0] = dest;
3442                 iargs [1] = src;
3443                 NEW_PCONST (cfg, iargs [2], klass);
3444
3445                 mono_emit_jit_icall (cfg, bblock, mono_value_copy, iargs, ip);
3446                 return;
3447         }
3448 #endif
3449
3450         /* FIXME: add write barrier handling */
3451         if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
3452                 MonoInst *inst;
3453                 if (dest->opcode == OP_LDADDR) {
3454                         /* Keep liveness info correct */
3455                         NEW_DUMMY_STORE (cfg, inst, dest->inst_i0->inst_c0);
3456                         MONO_ADD_INS (bblock, inst);
3457                 }
3458                 NEW_MEMCPY (cfg, inst, dest, src, n, align);
3459                 MONO_ADD_INS (bblock, inst);
3460                 return;
3461         }
3462         iargs [0] = dest;
3463         iargs [1] = src;
3464         NEW_ICONST (cfg, iargs [2], n);
3465
3466         memcpy_method = get_memcpy_method ();
3467         mono_emit_method_call_spilled_full (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL, FALSE, to_end);
3468 }
3469
3470 static MonoMethod*
3471 get_memset_method (void)
3472 {
3473         static MonoMethod *memset_method = NULL;
3474         if (!memset_method) {
3475                 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
3476                 if (!memset_method)
3477                         g_error ("Old corlib found. Install a new one");
3478         }
3479         return memset_method;
3480 }
3481
3482 static void
3483 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
3484 {
3485         MonoInst *iargs [3];
3486         MonoInst *ins, *zero_int32;
3487         int n;
3488         guint32 align;
3489         MonoMethod *memset_method;
3490
3491         NEW_ICONST (cfg, zero_int32, 0);
3492
3493         mono_class_init (klass);
3494         n = mono_class_value_size (klass, &align);
3495         MONO_INST_NEW (cfg, ins, 0);
3496         ins->cil_code = ip;
3497         ins->inst_left = dest;
3498         ins->inst_right = zero_int32;
3499         if (n == 1) {
3500                 ins->opcode = CEE_STIND_I1;
3501                 MONO_ADD_INS (bblock, ins);
3502         } else if ((n == 2) && (align >= 2)) {
3503                 ins->opcode = CEE_STIND_I2;
3504                 MONO_ADD_INS (bblock, ins);
3505         } else if ((n == 2) && (align >= 4)) {
3506                 ins->opcode = CEE_STIND_I4;
3507                 MONO_ADD_INS (bblock, ins);
3508         } else if (n <= sizeof (gpointer) * 5) {
3509                 NEW_MEMSET (cfg, ins, dest, 0, n, align);
3510                 MONO_ADD_INS (bblock, ins);
3511         } else {
3512                 memset_method = get_memset_method ();
3513                 handle_loaded_temps (cfg, bblock, stack_start, sp);
3514                 iargs [0] = dest;
3515                 NEW_ICONST (cfg, iargs [1], 0);
3516                 NEW_ICONST (cfg, iargs [2], n);
3517                 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
3518         }
3519 }
3520
3521 static int
3522 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, gboolean for_box, const guchar *ip)
3523 {
3524         MonoInst *iargs [2];
3525         void *alloc_ftn;
3526
3527         if (cfg->opt & MONO_OPT_SHARED) {
3528                 NEW_DOMAINCONST (cfg, iargs [0]);
3529                 NEW_CLASSCONST (cfg, iargs [1], klass);
3530
3531                 alloc_ftn = mono_object_new;
3532         } else if (cfg->compile_aot && bblock->out_of_line && klass->type_token && klass->image == mono_defaults.corlib) {
3533                 /* This happens often in argument checking code, eg. throw new FooException... */
3534                 /* Avoid relocations by calling a helper function specialized to mscorlib */
3535                 NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
3536                 return mono_emit_jit_icall (cfg, bblock, mono_helper_newobj_mscorlib, iargs, ip);
3537         } else {
3538                 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3539                 MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, for_box);
3540                 gboolean pass_lw;
3541
3542                 if (managed_alloc) {
3543                         NEW_VTABLECONST (cfg, iargs [0], vtable);
3544                         return mono_emit_method_call_spilled (cfg, bblock, managed_alloc, mono_method_signature (managed_alloc), iargs, ip, NULL);
3545                 }
3546                 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
3547                 if (pass_lw) {
3548                         guint32 lw = vtable->klass->instance_size;
3549                         lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
3550                         NEW_ICONST (cfg, iargs [0], lw);
3551                         NEW_VTABLECONST (cfg, iargs [1], vtable);
3552                 }
3553                 else
3554                         NEW_VTABLECONST (cfg, iargs [0], vtable);
3555         }
3556
3557         return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
3558 }
3559
3560 static int
3561 handle_alloc_from_inst (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, MonoInst *data_inst,
3562                 gboolean for_box, const guchar *ip)
3563 {
3564         MonoInst *iargs [2];
3565         MonoMethod *managed_alloc = NULL;
3566         void *alloc_ftn;
3567         /*
3568           FIXME: we cannot get managed_alloc here because we can't get
3569           the class's vtable (because it's not a closed class)
3570
3571         MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3572         MonoMethod *managed_alloc = mono_gc_get_managed_allocator (vtable, for_box);
3573         */
3574
3575         if (cfg->opt & MONO_OPT_SHARED) {
3576                 NEW_DOMAINCONST (cfg, iargs [0]);
3577                 iargs [1] = data_inst;
3578                 alloc_ftn = mono_object_new;
3579         } else {
3580                 g_assert (!cfg->compile_aot);
3581
3582                 if (managed_alloc) {
3583                         iargs [0] = data_inst;
3584                         return mono_emit_method_call_spilled (cfg, bblock, managed_alloc,
3585                                 mono_method_signature (managed_alloc), iargs, ip, NULL);
3586                 }
3587
3588                 iargs [0] = data_inst;
3589                 alloc_ftn = mono_object_new_specific;
3590         }
3591
3592         return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
3593 }
3594
3595 static MonoInst*
3596 handle_box_copy (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass, int temp)
3597 {
3598         MonoInst *dest, *vtoffset, *add, *vstore;
3599
3600         NEW_TEMPLOAD (cfg, dest, temp);
3601         NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3602         MONO_INST_NEW (cfg, add, OP_PADD);
3603         add->inst_left = dest;
3604         add->inst_right = vtoffset;
3605         add->cil_code = ip;
3606         add->klass = klass;
3607         MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3608         vstore->opcode = mini_type_to_stind (cfg, &klass->byval_arg);
3609         vstore->cil_code = ip;
3610         vstore->inst_left = add;
3611         vstore->inst_right = val;
3612
3613 #ifdef MONO_ARCH_SOFT_FLOAT
3614         if (vstore->opcode == CEE_STIND_R4) {
3615                 handle_store_float (cfg, bblock, add, val, ip);
3616         } else
3617 #endif
3618         if (vstore->opcode == CEE_STOBJ) {
3619                 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE, TRUE);
3620         } else
3621                 MONO_ADD_INS (bblock, vstore);
3622
3623         NEW_TEMPLOAD (cfg, dest, temp);
3624         return dest;
3625 }
3626
3627 static MonoInst *
3628 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
3629 {
3630         MonoInst *dest;
3631         int temp;
3632
3633         if (mono_class_is_nullable (klass)) {
3634                 MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
3635                 temp = mono_emit_method_call_spilled (cfg, bblock, method, mono_method_signature (method), &val, ip, NULL);
3636                 NEW_TEMPLOAD (cfg, dest, temp);
3637                 return dest;
3638         }
3639
3640         temp = handle_alloc (cfg, bblock, klass, TRUE, ip);
3641
3642         return handle_box_copy (cfg, bblock, val, ip, klass, temp);
3643 }
3644
3645 static MonoInst *
3646 handle_box_from_inst (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip,
3647                 MonoClass *klass, MonoInst *data_inst)
3648 {
3649         int temp;
3650
3651         g_assert (!mono_class_is_nullable (klass));
3652
3653         temp = handle_alloc_from_inst (cfg, bblock, klass, data_inst, TRUE, ip);
3654
3655         return handle_box_copy (cfg, bblock, val, ip, klass, temp);
3656 }
3657
3658 static MonoInst*
3659 handle_delegate_ctor (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, MonoInst *target, MonoMethod *method, unsigned char *ip)
3660 {
3661         gpointer *trampoline;
3662         MonoInst *obj, *ins, *store, *offset_ins, *method_ins, *tramp_ins;
3663         int temp;
3664
3665         temp = handle_alloc (cfg, bblock, klass, FALSE, ip);
3666
3667         /* Inline the contents of mono_delegate_ctor */
3668
3669         /* Set target field */
3670         /* Optimize away setting of NULL target */
3671         if (!(target->opcode == OP_PCONST && target->inst_p0 == 0)) {
3672                 NEW_TEMPLOAD (cfg, obj, temp);
3673                 NEW_ICONST (cfg, offset_ins, G_STRUCT_OFFSET (MonoDelegate, target));
3674                 MONO_INST_NEW (cfg, ins, OP_PADD);
3675                 ins->inst_left = obj;
3676                 ins->inst_right = offset_ins;
3677
3678                 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3679                 store->inst_left = ins;
3680                 store->inst_right = target;
3681                 mono_bblock_add_inst (bblock, store);
3682         }
3683
3684         /* Set method field */
3685         NEW_TEMPLOAD (cfg, obj, temp);
3686         NEW_ICONST (cfg, offset_ins, G_STRUCT_OFFSET (MonoDelegate, method));
3687         MONO_INST_NEW (cfg, ins, OP_PADD);
3688         ins->inst_left = obj;
3689         ins->inst_right = offset_ins;
3690
3691         NEW_METHODCONST (cfg, method_ins, method);
3692
3693         MONO_INST_NEW (cfg, store, CEE_STIND_I);
3694         store->inst_left = ins;
3695         store->inst_right = method_ins;
3696         mono_bblock_add_inst (bblock, store);
3697
3698         /* Set invoke_impl field */
3699         NEW_TEMPLOAD (cfg, obj, temp);
3700         NEW_ICONST (cfg, offset_ins, G_STRUCT_OFFSET (MonoDelegate, invoke_impl));
3701         MONO_INST_NEW (cfg, ins, OP_PADD);
3702         ins->inst_left = obj;
3703         ins->inst_right = offset_ins;
3704
3705         trampoline = mono_create_delegate_trampoline (klass);
3706         NEW_AOTCONST (cfg, tramp_ins, MONO_PATCH_INFO_ABS, trampoline);
3707
3708         MONO_INST_NEW (cfg, store, CEE_STIND_I);
3709         store->inst_left = ins;
3710         store->inst_right = tramp_ins;
3711         mono_bblock_add_inst (bblock, store);
3712
3713         /* All the checks which are in mono_delegate_ctor () are done by the delegate trampoline */
3714
3715         NEW_TEMPLOAD (cfg, obj, temp);
3716
3717         return obj;
3718 }
3719
3720 static int
3721 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
3722 {
3723         MonoJitICallInfo *info;
3724
3725         info = mono_get_array_new_va_icall (rank);
3726
3727         cfg->flags |= MONO_CFG_HAS_VARARGS;
3728
3729         /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
3730         return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
3731 }
3732
3733 static void
3734 mono_emit_load_got_addr (MonoCompile *cfg)
3735 {
3736         MonoInst *load, *store, *dummy_use;
3737         MonoInst *get_got;
3738
3739         if (!cfg->got_var || cfg->got_var_allocated)
3740                 return;
3741
3742         MONO_INST_NEW (cfg, get_got, OP_LOAD_GOTADDR);
3743         NEW_TEMPSTORE (cfg, store, cfg->got_var->inst_c0, get_got);
3744
3745         /* Add it to the start of the first bblock */
3746         if (cfg->bb_entry->code) {
3747                 store->next = cfg->bb_entry->code;
3748                 cfg->bb_entry->code = store;
3749         }
3750         else
3751                 MONO_ADD_INS (cfg->bb_entry, store);
3752
3753         cfg->got_var_allocated = TRUE;
3754
3755         /* 
3756          * Add a dummy use to keep the got_var alive, since real uses might
3757          * only be generated in the decompose or instruction selection phases.
3758          * Add it to end_bblock, so the variable's lifetime covers the whole
3759          * method.
3760          */
3761         NEW_TEMPLOAD (cfg, load, cfg->got_var->inst_c0);
3762         NEW_DUMMY_USE (cfg, dummy_use, load);
3763         MONO_ADD_INS (cfg->bb_exit, dummy_use);
3764 }
3765
3766 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
3767
3768 gboolean
3769 mini_class_is_system_array (MonoClass *klass)
3770 {
3771         if (klass->parent == mono_defaults.array_class)
3772                 return TRUE;
3773         else
3774                 return FALSE;
3775 }
3776
3777 static gboolean
3778 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
3779 {
3780         MonoMethodHeader *header = mono_method_get_header (method);
3781         MonoMethodSignature *signature = mono_method_signature (method);
3782         MonoVTable *vtable;
3783         int i;
3784
3785         if (cfg->generic_sharing_context)
3786                 return FALSE;
3787
3788         if (method->inline_failure)
3789                 return FALSE;
3790
3791 #ifdef MONO_ARCH_HAVE_LMF_OPS
3792         if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3793                  (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
3794             !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
3795                 return TRUE;
3796 #endif
3797
3798         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3799             (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3800             (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
3801             (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
3802             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3803             (method->klass->marshalbyref) ||
3804             !header || header->num_clauses ||
3805             /* fixme: why cant we inline valuetype returns? */
3806             MONO_TYPE_ISSTRUCT (signature->ret))
3807                 return FALSE;
3808
3809 #ifdef MONO_ARCH_SOFT_FLOAT
3810         /* this complicates things, fix later */
3811         if (signature->ret->type == MONO_TYPE_R4)
3812                 return FALSE;
3813 #endif
3814         /* its not worth to inline methods with valuetype arguments?? */
3815         for (i = 0; i < signature->param_count; i++) {
3816                 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
3817                         return FALSE;
3818                 }
3819 #ifdef MONO_ARCH_SOFT_FLOAT
3820                 /* this complicates things, fix later */
3821                 if (!signature->params [i]->byref && signature->params [i]->type == MONO_TYPE_R4)
3822                         return FALSE;
3823 #endif
3824         }
3825
3826         /* also consider num_locals? */
3827         /* Do the size check early to avoid creating vtables */
3828         if (getenv ("MONO_INLINELIMIT")) {
3829                 if (header->code_size >= atoi (getenv ("MONO_INLINELIMIT"))) {
3830                         return FALSE;
3831                 }
3832         } else if (header->code_size >= INLINE_LENGTH_LIMIT)
3833                 return FALSE;
3834
3835         /*
3836          * if we can initialize the class of the method right away, we do,
3837          * otherwise we don't allow inlining if the class needs initialization,
3838          * since it would mean inserting a call to mono_runtime_class_init()
3839          * inside the inlined code
3840          */
3841         if (!(cfg->opt & MONO_OPT_SHARED)) {
3842                 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
3843                         if (cfg->run_cctors && method->klass->has_cctor) {
3844                                 if (!method->klass->runtime_info)
3845                                         /* No vtable created yet */
3846                                         return FALSE;
3847                                 vtable = mono_class_vtable (cfg->domain, method->klass);
3848                                 if (!vtable)
3849                                         return FALSE;
3850                                 /* This makes so that inline cannot trigger */
3851                                 /* .cctors: too many apps depend on them */
3852                                 /* running with a specific order... */
3853                                 if (! vtable->initialized)
3854                                         return FALSE;
3855                                 mono_runtime_class_init (vtable);
3856                         }
3857                 } else if (mono_class_needs_cctor_run (method->klass, NULL)) {
3858                         if (!method->klass->runtime_info)
3859                                 /* No vtable created yet */
3860                                 return FALSE;
3861                         vtable = mono_class_vtable (cfg->domain, method->klass);
3862                         if (!vtable)
3863                                 return FALSE;
3864                         if (!vtable->initialized)
3865                                 return FALSE;
3866                 }
3867         } else {
3868                 /* 
3869                  * If we're compiling for shared code
3870                  * the cctor will need to be run at aot method load time, for example,
3871                  * or at the end of the compilation of the inlining method.
3872                  */
3873                 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
3874                         return FALSE;
3875         }
3876         //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
3877
3878         /*
3879          * CAS - do not inline methods with declarative security
3880          * Note: this has to be before any possible return TRUE;
3881          */
3882         if (mono_method_has_declsec (method))
3883                 return FALSE;
3884
3885         return TRUE;
3886 }
3887
3888 static gboolean
3889 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoVTable *vtable)
3890 {
3891         if (vtable->initialized && !cfg->compile_aot)
3892                 return FALSE;
3893
3894         if (vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
3895                 return FALSE;
3896
3897         if (!mono_class_needs_cctor_run (vtable->klass, method))
3898                 return FALSE;
3899
3900         if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (vtable->klass == method->klass))
3901                 /* The initialization is already done before the method is called */
3902                 return FALSE;
3903
3904         return TRUE;
3905 }
3906
3907 static MonoInst*
3908 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
3909 {
3910         int temp, rank;
3911         MonoInst *addr;
3912         MonoMethod *addr_method;
3913         int element_size;
3914
3915         rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
3916
3917         if (rank == 1) {
3918                 MONO_INST_NEW (cfg, addr, CEE_LDELEMA);
3919                 addr->inst_left = sp [0];
3920                 addr->inst_right = sp [1];
3921                 addr->type = STACK_MP;
3922                 addr->klass = cmethod->klass->element_class;
3923                 return addr;
3924         }
3925
3926         if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
3927 #if defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(MONO_ARCH_NO_EMULATE_MUL)
3928                 /* OP_LDELEMA2D depends on OP_LMUL */
3929 #else
3930                 MonoInst *indexes;
3931                 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
3932                 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
3933                 addr->inst_left = sp [0];
3934                 addr->inst_right = indexes;
3935                 addr->type = STACK_MP;
3936                 addr->klass = cmethod->klass->element_class;
3937                 return addr;
3938 #endif
3939         }
3940
3941         element_size = mono_class_array_element_size (cmethod->klass->element_class);
3942         addr_method = mono_marshal_get_array_address (rank, element_size);
3943         temp = mono_emit_method_call_spilled (cfg, bblock, addr_method, addr_method->signature, sp, ip, NULL);
3944         NEW_TEMPLOAD (cfg, addr, temp);
3945         return addr;
3946
3947 }
3948
3949 static MonoJitICallInfo **emul_opcode_map = NULL;
3950
3951 MonoJitICallInfo *
3952 mono_find_jit_opcode_emulation (int opcode)
3953 {
3954         g_assert (opcode >= 0 && opcode <= OP_LAST);
3955         if  (emul_opcode_map)
3956                 return emul_opcode_map [opcode];
3957         else
3958                 return NULL;
3959 }
3960
3961 static MonoInst*
3962 mini_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3963 {
3964         MonoInst *ins = NULL;
3965         
3966         static MonoClass *runtime_helpers_class = NULL;
3967         if (! runtime_helpers_class)
3968                 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
3969                         "System.Runtime.CompilerServices", "RuntimeHelpers");
3970
3971         if (cmethod->klass == mono_defaults.string_class) {
3972                 if (strcmp (cmethod->name, "get_Chars") == 0) {
3973                         MONO_INST_NEW (cfg, ins, OP_GETCHR);
3974                         ins->inst_i0 = args [0];
3975                         ins->inst_i1 = args [1];
3976                         return ins;
3977                 } else if (strcmp (cmethod->name, "get_Length") == 0) {
3978                         MONO_INST_NEW (cfg, ins, OP_STRLEN);
3979                         ins->inst_i0 = args [0];
3980                         return ins;
3981                 } else if (strcmp (cmethod->name, "InternalSetChar") == 0) {
3982                         MonoInst *get_addr;
3983                         MONO_INST_NEW (cfg, get_addr, OP_STR_CHAR_ADDR);
3984                         get_addr->inst_i0 = args [0];
3985                         get_addr->inst_i1 = args [1];
3986                         MONO_INST_NEW (cfg, ins, CEE_STIND_I2);
3987                         ins->inst_i0 = get_addr;
3988                         ins->inst_i1 = args [2];
3989                         return ins;
3990                 } else 
3991                         return NULL;
3992         } else if (cmethod->klass == mono_defaults.object_class) {
3993                 if (strcmp (cmethod->name, "GetType") == 0) {
3994                         MONO_INST_NEW (cfg, ins, OP_GETTYPE);
3995                         ins->inst_i0 = args [0];
3996                         return ins;
3997                 /* The OP_GETHASHCODE rule depends on OP_MUL */
3998 #if !defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(HAVE_MOVING_COLLECTOR)
3999                 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0) {
4000                         MONO_INST_NEW (cfg, ins, OP_GETHASHCODE);
4001                         ins->inst_i0 = args [0];
4002                         return ins;
4003 #endif
4004                 } else if (strcmp (cmethod->name, ".ctor") == 0) {
4005                         MONO_INST_NEW (cfg, ins, OP_NOP);
4006                         return ins;
4007                 } else
4008                         return NULL;
4009         } else if (cmethod->klass == mono_defaults.array_class) {
4010                 if (cmethod->name [0] != 'g')
4011                         return NULL;
4012
4013                 if (strcmp (cmethod->name, "get_Rank") == 0) {
4014                         MONO_INST_NEW (cfg, ins, OP_ARRAY_RANK);
4015                         ins->inst_i0 = args [0];
4016                         return ins;
4017                 } else if (strcmp (cmethod->name, "get_Length") == 0) {
4018                         MONO_INST_NEW (cfg, ins, CEE_LDLEN);
4019                         ins->inst_i0 = args [0];
4020                         return ins;
4021                 } else
4022                         return NULL;
4023         } else if (cmethod->klass == runtime_helpers_class) {
4024                 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
4025                         NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
4026                         return ins;
4027                 } else
4028                         return NULL;
4029         } else if (cmethod->klass == mono_defaults.thread_class) {
4030                 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
4031                         return ins;
4032                 if (strcmp (cmethod->name, "MemoryBarrier") == 0) {
4033                         MONO_INST_NEW (cfg, ins, OP_MEMORY_BARRIER);
4034                         return ins;
4035                 }
4036                 if (strcmp (cmethod->name, "SpinWait_nop") == 0) {
4037                         MONO_INST_NEW (cfg, ins, OP_RELAXED_NOP);
4038                         return ins;
4039                 }
4040         } else if (mini_class_is_system_array (cmethod->klass) &&
4041                         strcmp (cmethod->name, "GetGenericValueImpl") == 0) {
4042                 MonoInst *sp [2];
4043                 MonoInst *ldelem, *store, *load;
4044                 MonoClass *eklass = mono_class_from_mono_type (fsig->params [1]);
4045                 int n;
4046                 n = mini_type_to_stind (cfg, &eklass->byval_arg);
4047                 if (n == CEE_STOBJ)
4048                         return NULL;
4049                 sp [0] = args [0];
4050                 sp [1] = args [1];
4051                 NEW_LDELEMA (cfg, ldelem, sp, eklass);
4052                 ldelem->flags |= MONO_INST_NORANGECHECK;
4053                 MONO_INST_NEW (cfg, store, n);
4054                 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, &eklass->byval_arg));
4055                 type_to_eval_stack_type (cfg, &eklass->byval_arg, load);
4056                 load->inst_left = ldelem;
4057                 store->inst_left = args [2];
4058                 store->inst_right = load;
4059                 return store;
4060         } else if (cmethod->klass == mono_defaults.math_class) {
4061                 /* 
4062                  * There is general branches code for Min/Max, but it does not work for 
4063                  * all inputs:
4064                  * http://everything2.com/?node_id=1051618
4065                  */
4066         } else if (cmethod->klass->image == mono_defaults.corlib &&
4067                            (strcmp (cmethod->klass->name_space, "System.Threading") == 0) &&
4068                            (strcmp (cmethod->klass->name, "Interlocked") == 0)) {
4069                 ins = NULL;
4070
4071 #if SIZEOF_VOID_P == 8
4072                 if (strcmp (cmethod->name, "Read") == 0 && (fsig->params [0]->type == MONO_TYPE_I8)) {
4073                         /* 64 bit reads are already atomic */
4074                         MONO_INST_NEW (cfg, ins, CEE_LDIND_I8);
4075                         ins->inst_i0 = args [0];
4076                 }
4077 #endif
4078
4079 #ifdef MONO_ARCH_HAVE_ATOMIC_ADD
4080                 if (strcmp (cmethod->name, "Increment") == 0) {
4081                         MonoInst *ins_iconst;
4082                         guint32 opcode;
4083
4084                         if (fsig->params [0]->type == MONO_TYPE_I4)
4085                                 opcode = OP_ATOMIC_ADD_NEW_I4;
4086                         else if (fsig->params [0]->type == MONO_TYPE_I8)
4087                                 opcode = OP_ATOMIC_ADD_NEW_I8;
4088                         else
4089                                 g_assert_not_reached ();
4090
4091 #if SIZEOF_VOID_P == 4
4092                         if (opcode == OP_ATOMIC_ADD_NEW_I8)
4093                                 return NULL;
4094 #endif
4095
4096                         MONO_INST_NEW (cfg, ins, opcode);
4097                         MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4098                         ins_iconst->inst_c0 = 1;
4099
4100                         ins->inst_i0 = args [0];
4101                         ins->inst_i1 = ins_iconst;
4102                 } else if (strcmp (cmethod->name, "Decrement") == 0) {
4103                         MonoInst *ins_iconst;
4104                         guint32 opcode;
4105
4106                         if (fsig->params [0]->type == MONO_TYPE_I4)
4107                                 opcode = OP_ATOMIC_ADD_NEW_I4;
4108                         else if (fsig->params [0]->type == MONO_TYPE_I8)
4109                                 opcode = OP_ATOMIC_ADD_NEW_I8;
4110                         else
4111                                 g_assert_not_reached ();
4112
4113 #if SIZEOF_VOID_P == 4
4114                         if (opcode == OP_ATOMIC_ADD_NEW_I8)
4115                                 return NULL;
4116 #endif
4117
4118                         MONO_INST_NEW (cfg, ins, opcode);
4119                         MONO_INST_NEW (cfg, ins_iconst, OP_ICONST);
4120                         ins_iconst->inst_c0 = -1;
4121
4122                         ins->inst_i0 = args [0];
4123                         ins->inst_i1 = ins_iconst;
4124                 } else if (strcmp (cmethod->name, "Add") == 0) {
4125                         guint32 opcode;
4126
4127                         if (fsig->params [0]->type == MONO_TYPE_I4)
4128                                 opcode = OP_ATOMIC_ADD_NEW_I4;
4129                         else if (fsig->params [0]->type == MONO_TYPE_I8)
4130                                 opcode = OP_ATOMIC_ADD_NEW_I8;
4131                         else
4132                                 g_assert_not_reached ();
4133
4134 #if SIZEOF_VOID_P == 4
4135                         if (opcode == OP_ATOMIC_ADD_NEW_I8)
4136                                 return NULL;
4137 #endif
4138                         
4139                         MONO_INST_NEW (cfg, ins, opcode);
4140
4141                         ins->inst_i0 = args [0];
4142                         ins->inst_i1 = args [1];
4143                 }
4144 #endif /* MONO_ARCH_HAVE_ATOMIC_ADD */
4145
4146 #ifdef MONO_ARCH_HAVE_ATOMIC_EXCHANGE
4147                 if (strcmp (cmethod->name, "Exchange") == 0) {
4148                         guint32 opcode;
4149
4150                         if (fsig->params [0]->type == MONO_TYPE_I4)
4151                                 opcode = OP_ATOMIC_EXCHANGE_I4;
4152 #if SIZEOF_VOID_P == 8
4153                         else if ((fsig->params [0]->type == MONO_TYPE_I8) ||
4154                                          (fsig->params [0]->type == MONO_TYPE_I) ||
4155                                          (fsig->params [0]->type == MONO_TYPE_OBJECT))
4156                                 opcode = OP_ATOMIC_EXCHANGE_I8;
4157 #else
4158                         else if ((fsig->params [0]->type == MONO_TYPE_I) ||
4159                                          (fsig->params [0]->type == MONO_TYPE_OBJECT))
4160                                 opcode = OP_ATOMIC_EXCHANGE_I4;
4161 #endif
4162                         else
4163                                 return NULL;
4164
4165 #if SIZEOF_VOID_P == 4
4166                         if (opcode == OP_ATOMIC_EXCHANGE_I8)
4167                                 return NULL;
4168 #endif
4169
4170                         MONO_INST_NEW (cfg, ins, opcode);
4171
4172                         ins->inst_i0 = args [0];
4173                         ins->inst_i1 = args [1];
4174                 }
4175 #endif /* MONO_ARCH_HAVE_ATOMIC_EXCHANGE */
4176
4177 #ifdef MONO_ARCH_HAVE_ATOMIC_CAS_IMM
4178                 /* 
4179                  * Can't implement CompareExchange methods this way since they have
4180                  * three arguments. We can implement one of the common cases, where the new
4181                  * value is a constant.
4182                  */
4183                 if ((strcmp (cmethod->name, "CompareExchange") == 0)) {
4184                         if (fsig->params [1]->type == MONO_TYPE_I4 && args [2]->opcode == OP_ICONST) {
4185                                 MONO_INST_NEW (cfg, ins, OP_ATOMIC_CAS_IMM_I4);
4186                                 ins->inst_i0 = args [0];
4187                                 ins->inst_i1 = args [1];
4188                                 ins->backend.data = GINT_TO_POINTER (args [2]->inst_c0);
4189                         }
4190                         /* The I8 case is hard to detect, since the arg might be a conv.i8 (iconst) tree */
4191                 }
4192 #endif /* MONO_ARCH_HAVE_ATOMIC_CAS_IMM */
4193
4194                 if (ins)
4195                         return ins;
4196         } else if (cmethod->klass->image == mono_defaults.corlib) {
4197                 if (cmethod->name [0] == 'B' && strcmp (cmethod->name, "Break") == 0
4198                                 && strcmp (cmethod->klass->name, "Debugger") == 0) {
4199                         MONO_INST_NEW (cfg, ins, OP_BREAK);
4200                         return ins;
4201                 }
4202                 if (cmethod->name [0] == 'g' && strcmp (cmethod->name, "get_IsRunningOnWindows") == 0
4203                                 && strcmp (cmethod->klass->name, "Environment") == 0) {
4204 #ifdef PLATFORM_WIN32
4205                         NEW_ICONST (cfg, ins, 1);
4206 #else
4207                         NEW_ICONST (cfg, ins, 0);
4208 #endif
4209                         return ins;
4210                 }
4211         }
4212
4213         return mono_arch_get_inst_for_method (cfg, cmethod, fsig, args);
4214 }
4215
4216 static void
4217 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
4218 {
4219         MonoInst *store, *temp;
4220         int i;
4221
4222         g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
4223
4224         if (!sig->hasthis && sig->param_count == 0) 
4225                 return;
4226
4227         if (sig->hasthis) {
4228                 if (sp [0]->opcode == OP_ICONST) {
4229                         *args++ = sp [0];
4230                 } else {
4231                         temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
4232                         *args++ = temp;
4233                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
4234                         /* FIXME: handle CEE_STIND_R4 */
4235                         store->cil_code = sp [0]->cil_code;
4236                         MONO_ADD_INS (bblock, store);
4237                 }
4238                 sp++;
4239         }
4240
4241         for (i = 0; i < sig->param_count; ++i) {
4242                 if (sp [0]->opcode == OP_ICONST) {
4243                         *args++ = sp [0];
4244                 } else {
4245                         temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
4246                         *args++ = temp;
4247                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
4248                         store->cil_code = sp [0]->cil_code;
4249                         /* FIXME: handle CEE_STIND_R4 */
4250                         if (store->opcode == CEE_STOBJ) {
4251                                 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
4252                                 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE, FALSE);
4253 #ifdef MONO_ARCH_SOFT_FLOAT
4254                         } else if (store->opcode == CEE_STIND_R4) {
4255                                 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
4256                                 handle_store_float (cfg, bblock, store, *sp, sp [0]->cil_code);
4257 #endif
4258                         } else {
4259                                 MONO_ADD_INS (bblock, store);
4260                         } 
4261                 }
4262                 sp++;
4263         }
4264 }
4265 #define MONO_INLINE_CALLED_LIMITED_METHODS 0
4266 #define MONO_INLINE_CALLER_LIMITED_METHODS 0
4267
4268 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
4269 static char*
4270 mono_inline_called_method_name_limit = NULL;
4271 static gboolean check_inline_called_method_name_limit (MonoMethod *called_method) {
4272         char *called_method_name = mono_method_full_name (called_method, TRUE);
4273         int strncmp_result;
4274         
4275         if (mono_inline_called_method_name_limit == NULL) {
4276                 char *limit_string = getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
4277                 if (limit_string != NULL) {
4278                         mono_inline_called_method_name_limit = limit_string;
4279                 } else {
4280                         mono_inline_called_method_name_limit = (char *) "";
4281                 }
4282         }
4283         
4284         strncmp_result = strncmp (called_method_name, mono_inline_called_method_name_limit, strlen (mono_inline_called_method_name_limit));
4285         g_free (called_method_name);
4286         
4287         //return (strncmp_result <= 0);
4288         return (strncmp_result == 0);
4289 }
4290 #endif
4291
4292 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
4293 static char*
4294 mono_inline_caller_method_name_limit = NULL;
4295 static gboolean check_inline_caller_method_name_limit (MonoMethod *caller_method) {
4296         char *caller_method_name = mono_method_full_name (caller_method, TRUE);
4297         int strncmp_result;
4298         
4299         if (mono_inline_caller_method_name_limit == NULL) {
4300                 char *limit_string = getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
4301                 if (limit_string != NULL) {
4302                         mono_inline_caller_method_name_limit = limit_string;
4303                 } else {
4304                         mono_inline_caller_method_name_limit = (char *) "";
4305                 }
4306         }
4307         
4308         strncmp_result = strncmp (caller_method_name, mono_inline_caller_method_name_limit, strlen (mono_inline_caller_method_name_limit));
4309         g_free (caller_method_name);
4310         
4311         //return (strncmp_result <= 0);
4312         return (strncmp_result == 0);
4313 }
4314 #endif
4315
4316 static int
4317 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
4318                 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
4319 {
4320         MonoInst *ins, *rvar = NULL;
4321         MonoMethodHeader *cheader;
4322         MonoBasicBlock *ebblock, *sbblock;
4323         int i, costs, new_locals_offset;
4324         MonoMethod *prev_inlined_method;
4325         MonoBasicBlock **prev_cil_offset_to_bb;
4326         unsigned char* prev_cil_start;
4327         guint32 prev_cil_offset_to_bb_len;
4328
4329         g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
4330
4331 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
4332         if ((! inline_allways) && ! check_inline_called_method_name_limit (cmethod))
4333                 return 0;
4334 #endif
4335 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
4336         if ((! inline_allways) && ! check_inline_caller_method_name_limit (cfg->method))
4337                 return 0;
4338 #endif
4339
4340         if (bblock->out_of_line && !inline_allways)
4341                 return 0;
4342
4343         if (cfg->verbose_level > 2)
4344                 g_print ("INLINE START %p %s -> %s\n", cmethod,  mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
4345
4346         if (!cmethod->inline_info) {
4347                 mono_jit_stats.inlineable_methods++;
4348                 cmethod->inline_info = 1;
4349         }
4350         /* allocate space to store the return value */
4351         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
4352                 rvar =  mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
4353         }
4354
4355         /* allocate local variables */
4356         cheader = mono_method_get_header (cmethod);
4357         new_locals_offset = cfg->num_varinfo;
4358         for (i = 0; i < cheader->num_locals; ++i)
4359                 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
4360
4361         /* allocate starte and end blocks */
4362         sbblock = NEW_BBLOCK (cfg);
4363         sbblock->block_num = cfg->num_bblocks++;
4364         sbblock->real_offset = real_offset;
4365
4366         ebblock = NEW_BBLOCK (cfg);
4367         ebblock->block_num = cfg->num_bblocks++;
4368         ebblock->real_offset = real_offset;
4369
4370         prev_inlined_method = cfg->inlined_method;
4371         cfg->inlined_method = cmethod;
4372         prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
4373         prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
4374         prev_cil_start = cfg->cil_start;
4375
4376         costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
4377
4378         cfg->inlined_method = prev_inlined_method;
4379         cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
4380         cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
4381         cfg->cil_start = prev_cil_start;
4382
4383         if ((costs >= 0 && costs < 60) || inline_allways) {
4384                 if (cfg->verbose_level > 2)
4385                         g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
4386                 
4387                 mono_jit_stats.inlined_methods++;
4388
4389                 /* always add some code to avoid block split failures */
4390                 MONO_INST_NEW (cfg, ins, OP_NOP);
4391                 MONO_ADD_INS (bblock, ins);
4392                 ins->cil_code = ip;
4393
4394                 bblock->next_bb = sbblock;
4395                 link_bblock (cfg, bblock, sbblock);
4396
4397                 if (rvar) {
4398                         NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
4399                         NEW_TEMPLOAD_SOFT_FLOAT (cfg, ebblock, ins, rvar->inst_c0, ip);
4400                         *sp++ = ins;
4401                 }
4402                 *last_b = ebblock;
4403                 return costs + 1;
4404         } else {
4405                 if (cfg->verbose_level > 2)
4406                         g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
4407                 cfg->exception_type = MONO_EXCEPTION_NONE;
4408                 mono_loader_clear_error ();
4409                 cmethod->inline_failure = TRUE;
4410         }
4411         return 0;
4412 }
4413
4414 /*
4415  * Some of these comments may well be out-of-date.
4416  * Design decisions: we do a single pass over the IL code (and we do bblock 
4417  * splitting/merging in the few cases when it's required: a back jump to an IL
4418  * address that was not already seen as bblock starting point).
4419  * Code is validated as we go (full verification is still better left to metadata/verify.c).
4420  * Complex operations are decomposed in simpler ones right away. We need to let the 
4421  * arch-specific code peek and poke inside this process somehow (except when the 
4422  * optimizations can take advantage of the full semantic info of coarse opcodes).
4423  * All the opcodes of the form opcode.s are 'normalized' to opcode.
4424  * MonoInst->opcode initially is the IL opcode or some simplification of that 
4425  * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific 
4426  * opcode with value bigger than OP_LAST.
4427  * At this point the IR can be handed over to an interpreter, a dumb code generator
4428  * or to the optimizing code generator that will translate it to SSA form.
4429  *
4430  * Profiling directed optimizations.
4431  * We may compile by default with few or no optimizations and instrument the code
4432  * or the user may indicate what methods to optimize the most either in a config file
4433  * or through repeated runs where the compiler applies offline the optimizations to 
4434  * each method and then decides if it was worth it.
4435  *
4436  */
4437
4438 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
4439 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
4440 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
4441 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
4442 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
4443 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
4444 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
4445 #define CHECK_TYPELOAD(klass) if (!(klass) || (klass)->exception_type) {cfg->exception_ptr = klass; goto load_error;}
4446
4447 /* offset from br.s -> br like opcodes */
4448 #define BIG_BRANCH_OFFSET 13
4449
4450 static inline gboolean
4451 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
4452 {
4453         MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
4454         
4455         return b == NULL || b == bb;
4456 }
4457
4458 static int
4459 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
4460 {
4461         unsigned char *ip = start;
4462         unsigned char *target;
4463         int i;
4464         guint cli_addr;
4465         MonoBasicBlock *bblock;
4466         const MonoOpcode *opcode;
4467
4468         while (ip < end) {
4469                 cli_addr = ip - start;
4470                 i = mono_opcode_value ((const guint8 **)&ip, end);
4471                 if (i < 0)
4472                         UNVERIFIED;
4473                 opcode = &mono_opcodes [i];
4474                 switch (opcode->argument) {
4475                 case MonoInlineNone:
4476                         ip++; 
4477                         break;
4478                 case MonoInlineString:
4479                 case MonoInlineType:
4480                 case MonoInlineField:
4481                 case MonoInlineMethod:
4482                 case MonoInlineTok:
4483                 case MonoInlineSig:
4484                 case MonoShortInlineR:
4485                 case MonoInlineI:
4486                         ip += 5;
4487                         break;
4488                 case MonoInlineVar:
4489                         ip += 3;
4490                         break;
4491                 case MonoShortInlineVar:
4492                 case MonoShortInlineI:
4493                         ip += 2;
4494                         break;
4495                 case MonoShortInlineBrTarget:
4496                         target = start + cli_addr + 2 + (signed char)ip [1];
4497                         GET_BBLOCK (cfg, bblock, target);
4498                         ip += 2;
4499                         if (ip < end)
4500                                 GET_BBLOCK (cfg, bblock, ip);
4501                         break;
4502                 case MonoInlineBrTarget:
4503                         target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
4504                         GET_BBLOCK (cfg, bblock, target);
4505                         ip += 5;
4506                         if (ip < end)
4507                                 GET_BBLOCK (cfg, bblock, ip);
4508                         break;
4509                 case MonoInlineSwitch: {
4510                         guint32 n = read32 (ip + 1);
4511                         guint32 j;
4512                         ip += 5;
4513                         cli_addr += 5 + 4 * n;
4514                         target = start + cli_addr;
4515                         GET_BBLOCK (cfg, bblock, target);
4516                         
4517                         for (j = 0; j < n; ++j) {
4518                                 target = start + cli_addr + (gint32)read32 (ip);
4519                                 GET_BBLOCK (cfg, bblock, target);
4520                                 ip += 4;
4521                         }
4522                         break;
4523                 }
4524                 case MonoInlineR:
4525                 case MonoInlineI8:
4526                         ip += 9;
4527                         break;
4528                 default:
4529                         g_assert_not_reached ();
4530                 }
4531
4532                 if (i == CEE_THROW) {
4533                         unsigned char *bb_start = ip - 1;
4534                         
4535                         /* Find the start of the bblock containing the throw */
4536                         bblock = NULL;
4537                         while ((bb_start >= start) && !bblock) {
4538                                 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
4539                                 bb_start --;
4540                         }
4541                         if (bblock)
4542                                 bblock->out_of_line = 1;
4543                 }
4544         }
4545         return 0;
4546 unverified:
4547         *pos = ip;
4548         return 1;
4549 }
4550
4551 static MonoInst*
4552 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins, const guint8* ip_next)
4553 {
4554         MonoInst *store, *temp, *load;
4555         
4556         if (ip_in_bb (cfg, bblock, ip_next) &&
4557                 (CODE_IS_STLOC (ip_next) || *ip_next == CEE_RET))
4558                         return ins;
4559         
4560         temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4561         temp->flags |= MONO_INST_IS_TEMP;
4562         NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4563         /* FIXME: handle CEE_STIND_R4 */
4564         store->cil_code = ins->cil_code;
4565         MONO_ADD_INS (bblock, store);
4566         NEW_TEMPLOAD (cfg, load, temp->inst_c0);
4567         load->cil_code = ins->cil_code;
4568         return load;
4569 }
4570
4571 static inline MonoMethod *
4572 mini_get_method_allow_open (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
4573 {
4574         MonoMethod *method;
4575
4576         if (m->wrapper_type != MONO_WRAPPER_NONE)
4577                 return mono_method_get_wrapper_data (m, token);
4578
4579         method = mono_get_method_full (m->klass->image, token, klass, context);
4580
4581         return method;
4582 }
4583
4584 static inline MonoMethod *
4585 mini_get_method (MonoCompile *cfg, MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
4586 {
4587         MonoMethod *method = mini_get_method_allow_open (m, token, klass, context);
4588
4589         if (method && cfg && !cfg->generic_sharing_context && mono_class_is_open_constructed_type (&method->klass->byval_arg))
4590                 return NULL;
4591
4592         return method;
4593 }
4594
4595 static inline MonoClass*
4596 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
4597 {
4598         MonoClass *klass;
4599
4600         if (method->wrapper_type != MONO_WRAPPER_NONE)
4601                 klass = mono_method_get_wrapper_data (method, token);
4602         else
4603                 klass = mono_class_get_full (method->klass->image, token, context);
4604         if (klass)
4605                 mono_class_init (klass);
4606         return klass;
4607 }
4608
4609 /*
4610  * Returns TRUE if the JIT should abort inlining because "callee"
4611  * is influenced by security attributes.
4612  */
4613 static
4614 gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
4615 {
4616         guint32 result;
4617         
4618         if ((cfg->method != caller) && mono_method_has_declsec (callee)) {
4619                 return TRUE;
4620         }
4621         
4622         result = mono_declsec_linkdemand (cfg->domain, caller, callee);
4623         if (result == MONO_JIT_SECURITY_OK)
4624                 return FALSE;
4625
4626         if (result == MONO_JIT_LINKDEMAND_ECMA) {
4627                 /* Generate code to throw a SecurityException before the actual call/link */
4628                 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4629                 MonoInst *args [2];
4630
4631                 NEW_ICONST (cfg, args [0], 4);
4632                 NEW_METHODCONST (cfg, args [1], caller);
4633                 mono_emit_method_call_spilled (cfg, bblock, secman->linkdemandsecurityexception, mono_method_signature (secman->linkdemandsecurityexception), args, ip, NULL);
4634         } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
4635                  /* don't hide previous results */
4636                 cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
4637                 cfg->exception_data = result;
4638                 return TRUE;
4639         }
4640         
4641         return FALSE;
4642 }
4643
4644 static MonoMethod*
4645 method_access_exception (void)
4646 {
4647         static MonoMethod *method = NULL;
4648
4649         if (!method) {
4650                 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4651                 method = mono_class_get_method_from_name (secman->securitymanager,
4652                                                           "MethodAccessException", 2);
4653         }
4654         g_assert (method);
4655         return method;
4656 }
4657
4658 static void
4659 emit_throw_method_access_exception (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
4660                                     MonoBasicBlock *bblock, unsigned char *ip)
4661 {
4662         MonoMethod *thrower = method_access_exception ();
4663         MonoInst *args [2];
4664
4665         NEW_METHODCONST (cfg, args [0], caller);
4666         NEW_METHODCONST (cfg, args [1], callee);
4667         mono_emit_method_call_spilled (cfg, bblock, thrower,
4668                 mono_method_signature (thrower), args, ip, NULL);
4669 }
4670
4671 static MonoMethod*
4672 verification_exception (void)
4673 {
4674         static MonoMethod *method = NULL;
4675
4676         if (!method) {
4677                 MonoSecurityManager *secman = mono_security_manager_get_methods ();
4678                 method = mono_class_get_method_from_name (secman->securitymanager,
4679                                                           "VerificationException", 0);
4680         }
4681         g_assert (method);
4682         return method;
4683 }
4684
4685 static void
4686 emit_throw_verification_exception (MonoCompile *cfg, MonoBasicBlock *bblock, unsigned char *ip)
4687 {
4688         MonoMethod *thrower = verification_exception ();
4689
4690         mono_emit_method_call_spilled (cfg, bblock, thrower,
4691                 mono_method_signature (thrower),
4692                 NULL, ip, NULL);
4693 }
4694
4695 static void
4696 ensure_method_is_allowed_to_call_method (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee,
4697                                          MonoBasicBlock *bblock, unsigned char *ip)
4698 {
4699         MonoSecurityCoreCLRLevel caller_level = mono_security_core_clr_method_level (caller, TRUE);
4700         MonoSecurityCoreCLRLevel callee_level = mono_security_core_clr_method_level (callee, TRUE);
4701         gboolean is_safe = TRUE;
4702
4703         if (!(caller_level >= callee_level ||
4704                         caller_level == MONO_SECURITY_CORE_CLR_SAFE_CRITICAL ||
4705                         callee_level == MONO_SECURITY_CORE_CLR_SAFE_CRITICAL)) {
4706                 is_safe = FALSE;
4707         }
4708
4709         if (!is_safe)
4710                 emit_throw_method_access_exception (cfg, caller, callee, bblock, ip);
4711 }
4712
4713 static gboolean
4714 method_is_safe (MonoMethod *method)
4715 {
4716         /*
4717         if (strcmp (method->name, "unsafeMethod") == 0)
4718                 return FALSE;
4719         */
4720         return TRUE;
4721 }
4722
4723 /*
4724  * Check that the IL instructions at ip are the array initialization
4725  * sequence and return the pointer to the data and the size.
4726  */
4727 static const char*
4728 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoInst *newarr, int *out_size)
4729 {
4730         /*
4731          * newarr[System.Int32]
4732          * dup
4733          * ldtoken field valuetype ...
4734          * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
4735          */
4736         if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
4737                 MonoClass *klass = newarr->inst_newa_class;
4738                 guint32 field_token = read32 (ip + 2);
4739                 guint32 field_index = field_token & 0xffffff;
4740                 guint32 token = read32 (ip + 7);
4741                 guint32 rva;
4742                 const char *data_ptr;
4743                 int size = 0;
4744                 MonoMethod *cmethod;
4745                 MonoClass *dummy_class;
4746                 MonoClassField *field = mono_field_from_token (method->klass->image, field_token, &dummy_class, NULL);
4747                 int dummy_align;
4748
4749                 if (!field)
4750                         return NULL;
4751
4752                 if (newarr->inst_newa_len->opcode != OP_ICONST)
4753                         return NULL;
4754                 cmethod = mini_get_method (NULL, method, token, NULL, NULL);
4755                 if (!cmethod)
4756                         return NULL;
4757                 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
4758                         return NULL;
4759                 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
4760                 case MONO_TYPE_BOOLEAN:
4761                 case MONO_TYPE_I1:
4762                 case MONO_TYPE_U1:
4763                         size = 1; break;
4764                 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
4765 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
4766                 case MONO_TYPE_CHAR:
4767                 case MONO_TYPE_I2:
4768                 case MONO_TYPE_U2:
4769                         size = 2; break;
4770                 case MONO_TYPE_I4:
4771                 case MONO_TYPE_U4:
4772                 case MONO_TYPE_R4:
4773                         size = 4; break;
4774                 case MONO_TYPE_R8:
4775 #ifdef ARM_FPU_FPA
4776                         return NULL; /* stupid ARM FP swapped format */
4777 #endif
4778                 case MONO_TYPE_I8:
4779                 case MONO_TYPE_U8:
4780                         size = 8; break;
4781 #endif
4782                 default:
4783                         return NULL;
4784                 }
4785                 size *= newarr->inst_newa_len->inst_c0;
4786                 if (size > mono_type_size (field->type, &dummy_align))
4787                     return NULL;
4788                 *out_size = size;
4789                 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
4790                 field_index = read32 (ip + 2) & 0xffffff;
4791                 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
4792                 data_ptr = mono_image_rva_map (method->klass->image, rva);
4793                 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
4794                 /* for aot code we do the lookup on load */
4795                 if (aot && data_ptr)
4796                         return GUINT_TO_POINTER (rva);
4797                 return data_ptr;
4798         }
4799         return NULL;
4800 }
4801
4802 static void
4803 set_exception_type_from_invalid_il (MonoCompile *cfg, MonoMethod *method, unsigned char *ip)
4804 {
4805         char *method_fname = mono_method_full_name (method, TRUE);
4806         char *method_code;
4807
4808         if (mono_method_get_header (method)->code_size == 0)
4809                 method_code = g_strdup ("method body is empty.");
4810         else
4811                 method_code = mono_disasm_code_one (NULL, method, ip, NULL);
4812         cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
4813         cfg->exception_message = g_strdup_printf ("Invalid IL code in %s: %s\n", method_fname, method_code);
4814         g_free (method_fname);
4815         g_free (method_code);
4816 }
4817
4818 static void
4819 set_exception_object (MonoCompile *cfg, MonoException *exception)
4820 {
4821         cfg->exception_type = MONO_EXCEPTION_OBJECT_SUPPLIED;
4822         MONO_GC_REGISTER_ROOT (cfg->exception_ptr);
4823         cfg->exception_ptr = exception;
4824 }
4825
4826 static MonoInst*
4827 get_runtime_generic_context (MonoCompile *cfg, MonoMethod *method, int context_used, MonoInst *this, unsigned char *ip)
4828 {
4829         g_assert (cfg->generic_sharing_context);
4830
4831         if (method->klass->valuetype)
4832                 g_assert (!this);
4833
4834         if (context_used & MONO_GENERIC_CONTEXT_USED_METHOD) {
4835                 MonoInst *mrgctx_loc, *mrgctx_var;
4836
4837                 g_assert (!this);
4838                 g_assert (method->is_inflated && mono_method_get_context (method)->method_inst);
4839
4840                 mrgctx_loc = mono_get_vtable_var (cfg);
4841                 NEW_TEMPLOAD (cfg, mrgctx_var, mrgctx_loc->inst_c0);
4842
4843                 return mrgctx_var;
4844         } else if ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype) {
4845                 MonoInst *vtable_loc, *vtable_var;
4846
4847                 g_assert (!this);
4848
4849                 vtable_loc = mono_get_vtable_var (cfg);
4850                 NEW_TEMPLOAD (cfg, vtable_var, vtable_loc->inst_c0);
4851
4852                 if (method->is_inflated && mono_method_get_context (method)->method_inst) {
4853                         MonoInst *mrgctx_var = vtable_var;
4854
4855                         g_assert (G_STRUCT_OFFSET (MonoMethodRuntimeGenericContext, class_vtable) == 0);
4856
4857                         MONO_INST_NEW (cfg, vtable_var, CEE_LDIND_I);
4858                         vtable_var->cil_code = ip;
4859                         vtable_var->inst_left = mrgctx_var;
4860                         vtable_var->type = STACK_PTR;
4861                 }
4862
4863                 return vtable_var;
4864         } else {
4865                 MonoInst *vtable;
4866
4867                 g_assert (this);
4868
4869                 MONO_INST_NEW (cfg, vtable, CEE_LDIND_I);
4870                 vtable->inst_left = this;
4871                 vtable->type = STACK_PTR;
4872
4873                 return vtable;
4874         }
4875 }
4876
4877 static MonoInst*
4878 get_runtime_generic_context_other_table_ptr (MonoCompile *cfg, MonoBasicBlock *bblock,
4879         MonoInst *rgc_ptr, guint32 slot, const unsigned char *ip)
4880 {
4881         MonoMethodSignature *sig = helper_sig_rgctx_lazy_fetch_trampoline;
4882         guint8 *tramp = mono_create_rgctx_lazy_fetch_trampoline (slot);
4883         int temp;
4884         MonoInst *field;
4885
4886         temp = mono_emit_native_call (cfg, bblock, tramp, sig, &rgc_ptr, ip, FALSE, FALSE);
4887
4888         NEW_TEMPLOAD (cfg, field, temp);
4889
4890         return field;
4891 }
4892
4893 static MonoInst*
4894 get_runtime_generic_context_ptr (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4895         MonoClass *klass, MonoGenericContext *generic_context, MonoInst *rgctx, int rgctx_type, unsigned char *ip)
4896 {
4897         guint32 slot = mono_method_lookup_or_register_other_info (method,
4898                 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, &klass->byval_arg, rgctx_type, generic_context);
4899
4900         return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4901 }
4902
4903 static MonoInst*
4904 get_runtime_generic_context_method (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4905         MonoMethod *cmethod, MonoGenericContext *generic_context, MonoInst *rgctx, int rgctx_type, const unsigned char *ip)
4906 {
4907         guint32 slot = mono_method_lookup_or_register_other_info (method,
4908                 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, cmethod, rgctx_type, generic_context);
4909
4910         return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4911 }
4912
4913 static MonoInst*
4914 get_runtime_generic_context_field (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4915         MonoClassField *field, MonoGenericContext *generic_context, MonoInst *rgctx, int rgctx_type,
4916         const unsigned char *ip)
4917 {
4918         guint32 slot = mono_method_lookup_or_register_other_info (method,
4919                 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, field, rgctx_type, generic_context);
4920
4921         return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4922 }
4923
4924 static MonoInst*
4925 get_runtime_generic_context_method_rgctx (MonoCompile *cfg, MonoMethod *method, int context_used, MonoBasicBlock *bblock,
4926         MonoMethod *rgctx_method, MonoGenericContext *generic_context, MonoInst *rgctx, const unsigned char *ip)
4927 {
4928         guint32 slot = mono_method_lookup_or_register_other_info (method,
4929                 context_used & MONO_GENERIC_CONTEXT_USED_METHOD, rgctx_method,
4930                 MONO_RGCTX_INFO_METHOD_RGCTX, generic_context);
4931
4932         return get_runtime_generic_context_other_table_ptr (cfg, bblock, rgctx, slot, ip);
4933 }
4934
4935 static gboolean
4936 generic_class_is_reference_type (MonoCompile *cfg, MonoClass *klass)
4937 {
4938         MonoType *type;
4939
4940         if (cfg->generic_sharing_context)
4941                 type = mini_get_basic_type_from_generic (cfg->generic_sharing_context, &klass->byval_arg);
4942         else
4943                 type = &klass->byval_arg;
4944         return MONO_TYPE_IS_REFERENCE (type);
4945 }
4946
4947 /**
4948  * Handles unbox of a Nullable<T>, returning a temp variable where the
4949  * result is stored.  If a rgctx is passed, then shared generic code
4950  * is generated.
4951  */
4952 static int
4953 handle_unbox_nullable (MonoCompile* cfg, MonoMethod *caller_method, int context_used, MonoBasicBlock* bblock,
4954         MonoInst* val, const guchar *ip, MonoClass* klass, MonoGenericContext *generic_context, MonoInst *rgctx)
4955 {
4956         MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
4957         MonoMethodSignature *signature = mono_method_signature (method);
4958
4959         if (rgctx) {
4960                 /* FIXME: What if the class is shared?  We might not
4961                    have to get the address of the method from the
4962                    RGCTX. */
4963                 MonoInst *addr = get_runtime_generic_context_method (cfg, caller_method, context_used, bblock, method,
4964                         generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
4965
4966                 return mono_emit_rgctx_calli_spilled (cfg, bblock, signature, &val, addr, NULL, ip);
4967         } else {
4968                 return mono_emit_method_call_spilled (cfg, bblock, method, signature, &val, ip, NULL);
4969         }
4970 }
4971
4972 static MonoInst*
4973 handle_box_nullable_from_inst (MonoCompile *cfg, MonoMethod *caller_method, int context_used, MonoBasicBlock *bblock,
4974         MonoInst *val, const guchar *ip, MonoClass *klass, MonoGenericContext *generic_context, MonoInst *rgctx)
4975 {
4976         MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
4977         MonoInst *dest, *method_addr;
4978         int temp;
4979
4980         g_assert (mono_class_is_nullable (klass));
4981
4982         /* FIXME: What if the class is shared?  We might not have to
4983            get the method address from the RGCTX. */
4984         method_addr = get_runtime_generic_context_method (cfg, caller_method, context_used, bblock, method,
4985                         generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
4986         temp = mono_emit_rgctx_calli_spilled (cfg, bblock, mono_method_signature (method), &val,
4987                         method_addr, NULL, ip);
4988         NEW_TEMPLOAD (cfg, dest, temp);
4989         return dest;
4990 }
4991
4992 static int
4993 emit_castclass (MonoClass *klass, guint32 token, int context_used, gboolean inst_is_castclass, MonoCompile *cfg,
4994                 MonoMethod *method, MonoInst **arg_array, MonoType **param_types, GList *dont_inline,
4995                 unsigned char *end, MonoMethodHeader *header, MonoGenericContext *generic_context,
4996                 MonoBasicBlock **_bblock, unsigned char **_ip, MonoInst ***_sp, int *_inline_costs, guint *_real_offset)
4997 {
4998         MonoBasicBlock *bblock = *_bblock;
4999         unsigned char *ip = *_ip;
5000         MonoInst **sp = *_sp;
5001         int inline_costs = *_inline_costs;
5002         guint real_offset = *_real_offset;
5003         int return_value = 0;
5004
5005         if (context_used) {
5006                 MonoInst *rgctx, *args [2];
5007                 int temp;
5008
5009                 g_assert (!method->klass->valuetype);
5010
5011                 /* obj */
5012                 args [0] = *sp;
5013
5014                 /* klass */
5015                 GET_RGCTX (rgctx, context_used);
5016                 args [1] = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
5017                                 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
5018
5019                 temp = mono_emit_jit_icall (cfg, bblock, mono_object_castclass, args, ip);
5020                 NEW_TEMPLOAD (cfg, *sp, temp);
5021
5022                 sp++;
5023                 ip += 5;
5024                 inline_costs += 2;
5025         } else if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5026
5027                 MonoMethod *mono_castclass;
5028                 MonoInst *iargs [1];
5029                 MonoBasicBlock *ebblock;
5030                 int costs;
5031                 int temp;
5032
5033                 mono_castclass = mono_marshal_get_castclass (klass);
5034                 iargs [0] = sp [0];
5035
5036                 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock,
5037                                 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5038
5039                 g_assert (costs > 0);
5040
5041                 ip += 5;
5042                 real_offset += 5;
5043
5044                 GET_BBLOCK (cfg, bblock, ip);
5045                 ebblock->next_bb = bblock;
5046                 link_bblock (cfg, ebblock, bblock);
5047
5048                 temp = iargs [0]->inst_i0->inst_c0;
5049                 NEW_TEMPLOAD (cfg, *sp, temp);
5050
5051                 sp++;
5052                 bblock = ebblock;
5053                 inline_costs += costs;
5054         } else {
5055                 MonoInst *ins;
5056
5057                 /* Needed by the code generated in inssel.brg */
5058                 mono_get_got_var (cfg);
5059
5060                 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
5061                 ins->type = STACK_OBJ;
5062                 ins->inst_left = *sp;
5063                 ins->klass = klass;
5064                 ins->inst_newa_class = klass;
5065                 if (inst_is_castclass)
5066                         ins->backend.record_cast_details = debug_options.better_cast_details;
5067                 if (inst_is_castclass)
5068                         *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5069                 else
5070                         *sp++ = ins;
5071                 ip += 5;
5072         }
5073
5074 do_return:
5075         *_bblock = bblock;
5076         *_ip = ip;
5077         *_sp = sp;
5078         *_inline_costs = inline_costs;
5079         *_real_offset = real_offset;
5080         return return_value;
5081 unverified:
5082         return_value = -1;
5083         goto do_return;
5084 }
5085
5086 static int
5087 emit_unbox (MonoClass *klass, guint32 token, int context_used,
5088                 MonoCompile *cfg, MonoMethod *method, MonoInst **arg_array, MonoType **param_types, GList *dont_inline,
5089                 unsigned char *end, MonoMethodHeader *header, MonoGenericContext *generic_context,
5090                 MonoBasicBlock **_bblock, unsigned char **_ip, MonoInst ***_sp, int *_inline_costs, guint *_real_offset)
5091 {
5092         MonoBasicBlock *bblock = *_bblock;
5093         unsigned char *ip = *_ip;
5094         MonoInst **sp = *_sp;
5095         int inline_costs = *_inline_costs;
5096         guint real_offset = *_real_offset;
5097         int return_value = 0;
5098
5099         MonoInst *add, *vtoffset, *ins;
5100
5101         /* Needed by the code generated in inssel.brg */
5102         mono_get_got_var (cfg);
5103
5104         if (context_used) {
5105                 MonoInst *rgctx, *element_class;
5106
5107                 /* This assertion is from the unboxcast insn */
5108                 g_assert (klass->rank == 0);
5109
5110                 GET_RGCTX (rgctx, context_used);
5111                 element_class = get_runtime_generic_context_ptr (cfg, method, context_used, bblock,
5112                                 klass->element_class, generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
5113
5114                 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST_REG);
5115                 ins->type = STACK_OBJ;
5116                 ins->inst_left = *sp;
5117                 ins->inst_right = element_class;
5118                 ins->klass = klass;
5119                 ins->cil_code = ip;
5120         } else {
5121                 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5122                 ins->type = STACK_OBJ;
5123                 ins->inst_left = *sp;
5124                 ins->klass = klass;
5125                 ins->inst_newa_class = klass;
5126                 ins->cil_code = ip;
5127         }
5128
5129         MONO_INST_NEW (cfg, add, OP_PADD);
5130         NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5131         add->inst_left = ins;
5132         add->inst_right = vtoffset;
5133         add->type = STACK_MP;
5134         add->klass = klass;
5135         *sp = add;
5136
5137         *_bblock = bblock;
5138         *_ip = ip;
5139         *_sp = sp;
5140         *_inline_costs = inline_costs;
5141         *_real_offset = real_offset;
5142         return return_value;
5143 }
5144
5145 gboolean
5146 mini_assembly_can_skip_verification (MonoDomain *domain, MonoMethod *method)
5147 {
5148         MonoAssembly *assembly = method->klass->image->assembly;
5149         if (method->wrapper_type != MONO_WRAPPER_NONE)
5150                 return FALSE;
5151         if (assembly->in_gac || assembly->image == mono_defaults.corlib)
5152                 return FALSE;
5153         if (mono_security_get_mode () != MONO_SECURITY_MODE_NONE)
5154                 return FALSE;
5155         return mono_assembly_has_skip_verification (assembly);
5156 }
5157
5158 /*
5159  * mini_method_verify:
5160  * 
5161  * Verify the method using the new verfier.
5162  * 
5163  * Returns true if the method is invalid. 
5164  */
5165 gboolean
5166 mini_method_verify (MonoCompile *cfg, MonoMethod *method)
5167 {
5168         GSList *tmp, *res;
5169         gboolean is_fulltrust;
5170         MonoLoaderError *error;
5171
5172         if (method->verification_success)
5173                 return FALSE;
5174
5175         is_fulltrust = mono_verifier_is_method_full_trust (method);
5176
5177         if (!mono_verifier_is_enabled_for_method (method))
5178                 return FALSE;
5179
5180         res = mono_method_verify_with_current_settings (method, cfg->skip_visibility);
5181
5182         if ((error = mono_loader_get_last_error ())) {
5183                 cfg->exception_type = error->exception_type;
5184                 if (res)
5185                         mono_free_verify_list (res);
5186                 return TRUE;
5187         }
5188
5189         if (res) { 
5190                 for (tmp = res; tmp; tmp = tmp->next) {
5191                         MonoVerifyInfoExtended *info = (MonoVerifyInfoExtended *)tmp->data;
5192                         if (info->info.status == MONO_VERIFY_ERROR) {
5193                                 cfg->exception_type = info->exception_type;
5194                                 cfg->exception_message = g_strdup (info->info.message);
5195                                 mono_free_verify_list (res);
5196                                 return TRUE;
5197                         }
5198                         if (info->info.status == MONO_VERIFY_NOT_VERIFIABLE && !is_fulltrust) {
5199                                 cfg->exception_type = info->exception_type;
5200                                 cfg->exception_message = g_strdup (info->info.message);
5201                                 mono_free_verify_list (res);
5202                                 return TRUE;
5203                         }
5204                 }
5205                 mono_free_verify_list (res);
5206         }
5207         method->verification_success = 1;
5208         return FALSE;
5209 }
5210
5211 /*
5212  * mono_method_to_ir: translates IL into basic blocks containing trees
5213  */
5214 static int
5215 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
5216                    int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args, 
5217                    guint inline_offset, gboolean is_virtual_call)
5218 {
5219         MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
5220         MonoInst *ins, **sp, **stack_start;
5221         MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
5222         MonoMethod *cmethod, *method_definition;
5223         MonoInst **arg_array;
5224         MonoMethodHeader *header;
5225         MonoImage *image;
5226         guint32 token, ins_flag;
5227         MonoClass *klass;
5228         MonoClass *constrained_call = NULL;
5229         unsigned char *ip, *end, *target, *err_pos;
5230         static double r8_0 = 0.0;
5231         MonoMethodSignature *sig;
5232         MonoGenericContext *generic_context = NULL;
5233         MonoGenericContainer *generic_container = NULL;
5234         MonoType **param_types;
5235         GList *bb_recheck = NULL, *tmp;
5236         int i, n, start_new_bblock, ialign;
5237         int num_calls = 0, inline_costs = 0;
5238         int breakpoint_id = 0;
5239         guint32 align;
5240         guint real_offset, num_args;
5241         MonoBoolean security, pinvoke;
5242         MonoSecurityManager* secman = NULL;
5243         MonoDeclSecurityActions actions;
5244         GSList *class_inits = NULL;
5245         gboolean dont_verify, dont_verify_stloc, readonly = FALSE;
5246         int context_used;
5247
5248         /* serialization and xdomain stuff may need access to private fields and methods */
5249         dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
5250         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
5251         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
5252         dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
5253         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
5254         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
5255
5256         /* turn off visibility checks for smcs */
5257         dont_verify |= mono_security_get_mode () == MONO_SECURITY_MODE_SMCS_HACK;
5258
5259         /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
5260         dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
5261         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
5262         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
5263
5264         image = method->klass->image;
5265         header = mono_method_get_header (method);
5266         generic_container = mono_method_get_generic_container (method);
5267         sig = mono_method_signature (method);
5268         num_args = sig->hasthis + sig->param_count;
5269         ip = (unsigned char*)header->code;
5270         cfg->cil_start = ip;
5271         end = ip + header->code_size;
5272         mono_jit_stats.cil_code_size += header->code_size;
5273
5274         method_definition = method;
5275         while (method_definition->is_inflated) {
5276                 MonoMethodInflated *imethod = (MonoMethodInflated *) method_definition;
5277                 method_definition = imethod->declaring;
5278         }
5279
5280         /* SkipVerification is not allowed if core-clr is enabled */
5281         if (!dont_verify && mini_assembly_can_skip_verification (cfg->domain, method)) {
5282                 dont_verify = TRUE;
5283                 dont_verify_stloc = TRUE;
5284         }
5285
5286         if (!dont_verify && mini_method_verify (cfg, method_definition))
5287                 goto exception_exit;
5288
5289         if (mono_debug_using_mono_debugger ())
5290                 cfg->keep_cil_nops = TRUE;
5291
5292         if (sig->is_inflated)
5293                 generic_context = mono_method_get_context (method);
5294         else if (generic_container)
5295                 generic_context = &generic_container->context;
5296
5297         if (!cfg->generic_sharing_context)
5298                 g_assert (!sig->has_type_parameters);
5299
5300         if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
5301                 g_assert (method->is_inflated);
5302                 g_assert (mono_method_get_context (method)->method_inst);
5303         }
5304         if (method->is_inflated && mono_method_get_context (method)->method_inst)
5305                 g_assert (sig->generic_param_count);
5306
5307         if (cfg->method == method)
5308                 real_offset = 0;
5309         else
5310                 real_offset = inline_offset;
5311
5312         cfg->cil_offset_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
5313         cfg->cil_offset_to_bb_len = header->code_size;
5314
5315         if (cfg->verbose_level > 2)
5316                 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
5317
5318         dont_inline = g_list_prepend (dont_inline, method);
5319         if (cfg->method == method) {
5320
5321                 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
5322                         cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
5323
5324                 /* ENTRY BLOCK */
5325                 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
5326                 start_bblock->cil_code = NULL;
5327                 start_bblock->cil_length = 0;
5328                 start_bblock->block_num = cfg->num_bblocks++;
5329
5330                 /* EXIT BLOCK */
5331                 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
5332                 end_bblock->cil_code = NULL;
5333                 end_bblock->cil_length = 0;
5334                 end_bblock->block_num = cfg->num_bblocks++;
5335                 g_assert (cfg->num_bblocks == 2);
5336
5337                 arg_array = alloca (sizeof (MonoInst *) * num_args);
5338                 for (i = num_args - 1; i >= 0; i--)
5339                         arg_array [i] = cfg->varinfo [i];
5340
5341                 if (header->num_clauses) {
5342                         cfg->spvars = g_hash_table_new (NULL, NULL);
5343                         cfg->exvars = g_hash_table_new (NULL, NULL);
5344                 }
5345                 /* handle exception clauses */
5346                 for (i = 0; i < header->num_clauses; ++i) {
5347                         MonoBasicBlock *try_bb;
5348                         MonoExceptionClause *clause = &header->clauses [i];
5349
5350                         GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
5351                         try_bb->real_offset = clause->try_offset;
5352                         GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
5353                         tblock->real_offset = clause->handler_offset;
5354                         tblock->flags |= BB_EXCEPTION_HANDLER;
5355
5356                         link_bblock (cfg, try_bb, tblock);
5357
5358                         if (*(ip + clause->handler_offset) == CEE_POP)
5359                                 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
5360
5361                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
5362                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
5363                             clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
5364                                 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
5365                                 MONO_ADD_INS (tblock, ins);
5366
5367                                 /* todo: is a fault block unsafe to optimize? */
5368                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
5369                                         tblock->flags |= BB_EXCEPTION_UNSAFE;
5370                         }
5371
5372
5373                         /*g_print ("clause try IL_%04x to IL_%04x handler %d at IL_%04x to IL_%04x\n", clause->try_offset, clause->try_offset + clause->try_len, clause->flags, clause->handler_offset, clause->handler_offset + clause->handler_len);
5374                           while (p < end) {
5375                           g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
5376                           }*/
5377                         /* catch and filter blocks get the exception object on the stack */
5378                         if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
5379                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5380                                 MonoInst *load, *dummy_use;
5381
5382                                 /* mostly like handle_stack_args (), but just sets the input args */
5383                                 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
5384                                 tblock->in_scount = 1;
5385                                 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
5386                                 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
5387
5388                                 /* 
5389                                  * Add a dummy use for the exvar so its liveness info will be
5390                                  * correct.
5391                                  */
5392                                 NEW_TEMPLOAD (cfg, load, tblock->in_stack [0]->inst_c0);
5393                                 NEW_DUMMY_USE (cfg, dummy_use, load);
5394                                 MONO_ADD_INS (tblock, dummy_use);
5395                                 
5396                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5397                                         GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
5398                                         tblock->real_offset = clause->data.filter_offset;
5399                                         tblock->in_scount = 1;
5400                                         tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
5401                                         /* The filter block shares the exvar with the handler block */
5402                                         tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
5403                                         MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
5404                                         MONO_ADD_INS (tblock, ins);
5405                                 }
5406                         }
5407
5408                         if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER &&
5409                                         clause->data.catch_class &&
5410                                         cfg->generic_sharing_context &&
5411                                         mono_class_check_context_used (clause->data.catch_class)) {
5412                                 if (mono_method_get_context (method)->method_inst)
5413                                         GENERIC_SHARING_FAILURE (CEE_NOP);
5414
5415                                 /*
5416                                  * In shared generic code with catch
5417                                  * clauses containing type variables
5418                                  * the exception handling code has to
5419                                  * be able to get to the rgctx.
5420                                  * Therefore we have to make sure that
5421                                  * the vtable/mrgctx argument (for
5422                                  * static or generic methods) or the
5423                                  * "this" argument (for non-static
5424                                  * methods) are live.
5425                                  */
5426                                 if ((method->flags & METHOD_ATTRIBUTE_STATIC) ||
5427                                                 mini_method_get_context (method)->method_inst ||
5428                                                 method->klass->valuetype) {
5429                                         mono_get_vtable_var (cfg);
5430                                 } else {
5431                                         MonoInst *this, *dummy_use;
5432                                         MonoType *this_type;
5433
5434                                         if (method->klass->valuetype)
5435                                                 this_type = &method->klass->this_arg;
5436                                         else
5437                                                 this_type = &method->klass->byval_arg;
5438
5439                                         if (arg_array [0]->opcode == OP_ICONST) {
5440                                                 this = arg_array [0];
5441                                         } else {
5442                                                 this = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));
5443                                                 this->ssa_op = MONO_SSA_LOAD;
5444                                                 this->inst_i0 = arg_array [0];
5445                                                 this->opcode = mini_type_to_ldind ((cfg), this->inst_i0->inst_vtype);
5446                                                 type_to_eval_stack_type ((cfg), this_type, this);
5447                                                 this->klass = this->inst_i0->klass;
5448                                         }
5449
5450                                         NEW_DUMMY_USE (cfg, dummy_use, this);
5451                                         MONO_ADD_INS (tblock, dummy_use);
5452                                 }
5453                         }
5454                 }
5455         } else {
5456                 arg_array = alloca (sizeof (MonoInst *) * num_args);
5457                 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
5458         }
5459
5460         /* FIRST CODE BLOCK */
5461         bblock = NEW_BBLOCK (cfg);
5462         bblock->cil_code = ip;
5463
5464         ADD_BBLOCK (cfg, bblock);
5465
5466         if (cfg->method == method) {
5467                 breakpoint_id = mono_debugger_method_has_breakpoint (method);
5468                 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
5469                         MONO_INST_NEW (cfg, ins, OP_BREAK);
5470                         MONO_ADD_INS (bblock, ins);
5471                 }
5472         }
5473
5474         if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
5475                 secman = mono_security_manager_get_methods ();
5476
5477         security = (secman && mono_method_has_declsec (method));
5478         /* at this point having security doesn't mean we have any code to generate */
5479         if (security && (cfg->method == method)) {
5480                 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
5481                  * And we do not want to enter the next section (with allocation) if we
5482                  * have nothing to generate */
5483                 security = mono_declsec_get_demands (method, &actions);
5484         }
5485
5486         /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
5487         pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
5488         if (pinvoke) {
5489                 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
5490                 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5491                         MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
5492
5493                         /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
5494                         if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
5495                                 pinvoke = FALSE;
5496                         }
5497                         if (custom)
5498                                 mono_custom_attrs_free (custom);
5499
5500                         if (pinvoke) {
5501                                 custom = mono_custom_attrs_from_class (wrapped->klass);
5502                                 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
5503                                         pinvoke = FALSE;
5504                                 }
5505                                 if (custom)
5506                                         mono_custom_attrs_free (custom);
5507                         }
5508                 } else {
5509                         /* not a P/Invoke after all */
5510                         pinvoke = FALSE;
5511                 }
5512         }
5513         
5514         if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || cfg->compile_aot || security || pinvoke) {
5515                 /* we use a separate basic block for the initialization code */
5516                 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
5517                 init_localsbb->real_offset = real_offset;
5518                 start_bblock->next_bb = init_localsbb;
5519                 init_localsbb->next_bb = bblock;
5520                 link_bblock (cfg, start_bblock, init_localsbb);
5521                 link_bblock (cfg, init_localsbb, bblock);
5522                 init_localsbb->block_num = cfg->num_bblocks++;
5523         } else {
5524                 start_bblock->next_bb = bblock;
5525                 link_bblock (cfg, start_bblock, bblock);
5526         }
5527
5528         /* at this point we know, if security is TRUE, that some code needs to be generated */
5529         if (security && (cfg->method == method)) {
5530                 MonoInst *args [2];
5531
5532                 mono_jit_stats.cas_demand_generation++;
5533
5534                 if (actions.demand.blob) {
5535                         /* Add code for SecurityAction.Demand */
5536                         NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
5537                         NEW_ICONST (cfg, args [1], actions.demand.size);
5538                         /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
5539                         mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
5540                 }
5541                 if (actions.noncasdemand.blob) {
5542                         /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
5543                         /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
5544                         NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
5545                         NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
5546                         /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
5547                         mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
5548                 }
5549                 if (actions.demandchoice.blob) {
5550                         /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
5551                         NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
5552                         NEW_ICONST (cfg, args [1], actions.demandchoice.size);
5553                         /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
5554                         mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandchoice, mono_method_signature (secman->demandchoice), args, ip, NULL);
5555                 }
5556         }
5557
5558         /* we must Demand SecurityPermission.Unmanaged before p/invoking */
5559         if (pinvoke) {
5560                 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandunmanaged, mono_method_signature (secman->demandunmanaged), NULL, ip, NULL);
5561         }
5562
5563         if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
5564                 if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) {
5565                         MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
5566                         if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5567                                 if (!(method->klass && method->klass->image &&
5568                                                 mono_security_core_clr_is_platform_image (method->klass->image))) {
5569                                         emit_throw_method_access_exception (cfg, method, wrapped, bblock, ip);
5570                                 }
5571                         }
5572                 }
5573                 if (!method_is_safe (method))
5574                         emit_throw_verification_exception (cfg, bblock, ip);
5575         }
5576
5577         if (header->code_size == 0)
5578                 UNVERIFIED;
5579
5580         if (get_basic_blocks (cfg, header, real_offset, ip, end, &err_pos)) {
5581                 ip = err_pos;
5582                 UNVERIFIED;
5583         }
5584
5585         if (cfg->method == method)
5586                 mono_debug_init_method (cfg, bblock, breakpoint_id);
5587
5588         param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
5589         if (sig->hasthis)
5590                 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
5591         for (n = 0; n < sig->param_count; ++n)
5592                 param_types [n + sig->hasthis] = sig->params [n];
5593         for (n = 0; n < header->num_locals; ++n) {
5594                 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
5595                         UNVERIFIED;
5596         }
5597         class_inits = NULL;
5598
5599         /* do this somewhere outside - not here */
5600         NEW_ICONST (cfg, zero_int32, 0);
5601         NEW_ICONST (cfg, zero_int64, 0);
5602         zero_int64->type = STACK_I8;
5603         NEW_PCONST (cfg, zero_ptr, 0);
5604         NEW_PCONST (cfg, zero_obj, 0);
5605         zero_obj->type = STACK_OBJ;
5606
5607         MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
5608         zero_r8->type = STACK_R8;
5609         zero_r8->inst_p0 = &r8_0;
5610
5611         /* add a check for this != NULL to inlined methods */
5612         if (is_virtual_call) {
5613                 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
5614                 NEW_ARGLOAD (cfg, ins->inst_left, 0);
5615                 ins->cil_code = ip;
5616                 MONO_ADD_INS (bblock, ins);
5617         }
5618
5619         /* we use a spare stack slot in SWITCH and NEWOBJ and others */
5620         stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
5621
5622         ins_flag = 0;
5623         start_new_bblock = 0;
5624         while (ip < end) {
5625
5626                 if (cfg->method == method)
5627                         real_offset = ip - header->code;
5628                 else
5629                         real_offset = inline_offset;
5630                 cfg->ip = ip;
5631
5632                 context_used = 0;
5633
5634                 if (start_new_bblock) {
5635                         bblock->cil_length = ip - bblock->cil_code;
5636                         if (start_new_bblock == 2) {
5637                                 g_assert (ip == tblock->cil_code);
5638                         } else {
5639                                 GET_BBLOCK (cfg, tblock, ip);
5640                         }
5641                         bblock->next_bb = tblock;
5642                         bblock = tblock;
5643                         start_new_bblock = 0;
5644                         for (i = 0; i < bblock->in_scount; ++i) {
5645                                 if (cfg->verbose_level > 3)
5646                                         g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);                                           
5647                                 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
5648                                 *sp++ = ins;
5649                         }
5650                         g_slist_free (class_inits);
5651                         class_inits = NULL;
5652                 } else {
5653                         if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != bblock)) {
5654                                 link_bblock (cfg, bblock, tblock);
5655                                 if (sp != stack_start) {
5656                                         handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
5657                                         sp = stack_start;
5658                                         CHECK_UNVERIFIABLE (cfg);
5659                                 }
5660                                 bblock->next_bb = tblock;
5661                                 bblock = tblock;
5662                                 for (i = 0; i < bblock->in_scount; ++i) {
5663                                         if (cfg->verbose_level > 3)
5664                                                 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);                                           
5665                                         NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
5666                                         *sp++ = ins;
5667                                 }
5668                                 g_slist_free (class_inits);
5669                                 class_inits = NULL;
5670                         }
5671                 }
5672
5673                 bblock->real_offset = real_offset;
5674
5675                 if ((cfg->method == method) && cfg->coverage_info) {
5676                         MonoInst *store, *one;
5677                         guint32 cil_offset = ip - header->code;
5678                         cfg->coverage_info->data [cil_offset].cil_code = ip;
5679
5680                         /* TODO: Use an increment here */
5681                         NEW_ICONST (cfg, one, 1);
5682                         one->cil_code = ip;
5683
5684                         NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
5685                         ins->cil_code = ip;
5686
5687                         MONO_INST_NEW (cfg, store, CEE_STIND_I);
5688                         store->inst_left = ins;
5689                         store->inst_right = one;
5690
5691                         MONO_ADD_INS (bblock, store);
5692                 }
5693
5694                 if (cfg->verbose_level > 3)
5695                         g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
5696
5697                 switch (*ip) {
5698                 case CEE_NOP:
5699                         if (cfg->keep_cil_nops)
5700                                 MONO_INST_NEW (cfg, ins, OP_HARD_NOP);
5701                         else
5702                                 MONO_INST_NEW (cfg, ins, OP_NOP);
5703                         ip++;
5704                         MONO_ADD_INS (bblock, ins);
5705                         break;
5706                 case CEE_BREAK:
5707                         MONO_INST_NEW (cfg, ins, OP_BREAK);
5708                         ip++;
5709                         MONO_ADD_INS (bblock, ins);
5710                         break;
5711                 case CEE_LDARG_0:
5712                 case CEE_LDARG_1:
5713                 case CEE_LDARG_2:
5714                 case CEE_LDARG_3:
5715                         CHECK_STACK_OVF (1);
5716                         n = (*ip)-CEE_LDARG_0;
5717                         CHECK_ARG (n);
5718                         NEW_ARGLOAD (cfg, ins, n);
5719                         LDARG_SOFT_FLOAT (cfg, ins, n, ip);
5720                         ip++;
5721                         *sp++ = ins;
5722                         break;
5723                 case CEE_LDLOC_0:
5724                 case CEE_LDLOC_1:
5725                 case CEE_LDLOC_2:
5726                 case CEE_LDLOC_3:
5727                         CHECK_STACK_OVF (1);
5728                         n = (*ip)-CEE_LDLOC_0;
5729                         CHECK_LOCAL (n);
5730                         NEW_LOCLOAD (cfg, ins, n);
5731                         LDLOC_SOFT_FLOAT (cfg, ins, n, ip);
5732                         ip++;
5733                         *sp++ = ins;
5734                         break;
5735                 case CEE_STLOC_0:
5736                 case CEE_STLOC_1:
5737                 case CEE_STLOC_2:
5738                 case CEE_STLOC_3:
5739                         CHECK_STACK (1);
5740                         n = (*ip)-CEE_STLOC_0;
5741                         CHECK_LOCAL (n);
5742                         --sp;
5743                         handle_loaded_temps (cfg, bblock, stack_start, sp);
5744                         NEW_LOCSTORE (cfg, ins, n, *sp);
5745                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
5746                                 UNVERIFIED;
5747                         STLOC_SOFT_FLOAT (cfg, ins, n, ip);
5748                         if (ins->opcode == CEE_STOBJ) {
5749                                 NEW_LOCLOADA (cfg, ins, n);
5750                                 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5751                         } else
5752                                 MONO_ADD_INS (bblock, ins);
5753                         ++ip;
5754                         inline_costs += 1;
5755                         break;
5756                 case CEE_LDARG_S:
5757                         CHECK_OPSIZE (2);
5758                         CHECK_STACK_OVF (1);
5759                         CHECK_ARG (ip [1]);
5760                         NEW_ARGLOAD (cfg, ins, ip [1]);
5761                         LDARG_SOFT_FLOAT (cfg, ins, ip [1], ip);
5762                         *sp++ = ins;
5763                         ip += 2;
5764                         break;
5765                 case CEE_LDARGA_S:
5766                         CHECK_OPSIZE (2);
5767                         CHECK_STACK_OVF (1);
5768                         CHECK_ARG (ip [1]);
5769                         NEW_ARGLOADA (cfg, ins, ip [1]);
5770                         *sp++ = ins;
5771                         ip += 2;
5772                         break;
5773                 case CEE_STARG_S:
5774                         CHECK_OPSIZE (2);
5775                         CHECK_STACK (1);
5776                         --sp;
5777                         CHECK_ARG (ip [1]);
5778                         NEW_ARGSTORE (cfg, ins, ip [1], *sp);
5779                         handle_loaded_temps (cfg, bblock, stack_start, sp);
5780                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
5781                                 UNVERIFIED;
5782                         STARG_SOFT_FLOAT (cfg, ins, ip [1], ip);
5783                         if (ins->opcode == CEE_STOBJ) {
5784                                 NEW_ARGLOADA (cfg, ins, ip [1]);
5785                                 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5786                         } else
5787                                 MONO_ADD_INS (bblock, ins);
5788                         ip += 2;
5789                         break;
5790                 case CEE_LDLOC_S:
5791                         CHECK_OPSIZE (2);
5792                         CHECK_STACK_OVF (1);
5793                         CHECK_LOCAL (ip [1]);
5794                         NEW_LOCLOAD (cfg, ins, ip [1]);
5795                         LDLOC_SOFT_FLOAT (cfg, ins, ip [1], ip);
5796                         *sp++ = ins;
5797                         ip += 2;
5798                         break;
5799                 case CEE_LDLOCA_S:
5800                         CHECK_OPSIZE (2);
5801                         CHECK_STACK_OVF (1);
5802                         CHECK_LOCAL (ip [1]);
5803                         NEW_LOCLOADA (cfg, ins, ip [1]);
5804                         *sp++ = ins;
5805                         ip += 2;
5806                         break;
5807                 case CEE_STLOC_S:
5808                         CHECK_OPSIZE (2);
5809                         CHECK_STACK (1);
5810                         --sp;
5811                         handle_loaded_temps (cfg, bblock, stack_start, sp);
5812                         CHECK_LOCAL (ip [1]);
5813                         NEW_LOCSTORE (cfg, ins, ip [1], *sp);
5814                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
5815                                 UNVERIFIED;
5816                         STLOC_SOFT_FLOAT (cfg, ins, ip [1], ip);
5817                         if (ins->opcode == CEE_STOBJ) {
5818                                 NEW_LOCLOADA (cfg, ins, ip [1]);
5819                                 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5820                         } else
5821                                 MONO_ADD_INS (bblock, ins);
5822                         ip += 2;
5823                         inline_costs += 1;
5824                         break;
5825                 case CEE_LDNULL:
5826                         CHECK_STACK_OVF (1);
5827                         NEW_PCONST (cfg, ins, NULL);
5828                         ins->type = STACK_OBJ;
5829                         ++ip;
5830                         *sp++ = ins;
5831                         break;
5832                 case CEE_LDC_I4_M1:
5833                         CHECK_STACK_OVF (1);
5834                         NEW_ICONST (cfg, ins, -1);
5835                         ++ip;
5836                         *sp++ = ins;
5837                         break;
5838                 case CEE_LDC_I4_0:
5839                 case CEE_LDC_I4_1:
5840                 case CEE_LDC_I4_2:
5841                 case CEE_LDC_I4_3:
5842                 case CEE_LDC_I4_4:
5843                 case CEE_LDC_I4_5:
5844                 case CEE_LDC_I4_6:
5845                 case CEE_LDC_I4_7:
5846                 case CEE_LDC_I4_8:
5847                         CHECK_STACK_OVF (1);
5848                         NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
5849                         ++ip;
5850                         *sp++ = ins;
5851                         break;
5852                 case CEE_LDC_I4_S:
5853                         CHECK_OPSIZE (2);
5854                         CHECK_STACK_OVF (1);
5855                         ++ip;
5856                         NEW_ICONST (cfg, ins, *((signed char*)ip));
5857                         ++ip;
5858                         *sp++ = ins;
5859                         break;
5860                 case CEE_LDC_I4:
5861                         CHECK_OPSIZE (5);
5862                         CHECK_STACK_OVF (1);
5863                         NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
5864                         ip += 5;
5865                         *sp++ = ins;
5866                         break;
5867                 case CEE_LDC_I8:
5868                         CHECK_OPSIZE (9);
5869                         CHECK_STACK_OVF (1);
5870                         MONO_INST_NEW (cfg, ins, OP_I8CONST);
5871                         ins->type = STACK_I8;
5872                         ++ip;
5873                         ins->inst_l = (gint64)read64 (ip);
5874                         ip += 8;
5875                         *sp++ = ins;
5876                         break;
5877                 case CEE_LDC_R4: {
5878                         float *f;
5879                         /* we should really allocate this only late in the compilation process */
5880                         mono_domain_lock (cfg->domain);
5881                         f = mono_domain_alloc (cfg->domain, sizeof (float));
5882                         mono_domain_unlock (cfg->domain);
5883                         CHECK_OPSIZE (5);
5884                         CHECK_STACK_OVF (1);
5885                         MONO_INST_NEW (cfg, ins, OP_R4CONST);
5886                         ins->type = STACK_R8;
5887                         ++ip;
5888                         readr4 (ip, f);
5889                         ins->inst_p0 = f;
5890
5891                         ip += 4;
5892                         *sp++ = ins;                    
5893                         break;
5894                 }
5895                 case CEE_LDC_R8: {
5896                         double *d;
5897                         mono_domain_lock (cfg->domain);
5898                         d = mono_domain_alloc (cfg->domain, sizeof (double));
5899                         mono_domain_unlock (cfg->domain);
5900                         CHECK_OPSIZE (9);
5901                         CHECK_STACK_OVF (1);
5902                         MONO_INST_NEW (cfg, ins, OP_R8CONST);
5903                         ins->type = STACK_R8;
5904                         ++ip;
5905                         readr8 (ip, d);
5906                         ins->inst_p0 = d;
5907
5908                         ip += 8;
5909                         *sp++ = ins;                    
5910                         break;
5911                 }
5912                 case CEE_DUP: {
5913                         MonoInst *temp, *store;
5914                         CHECK_STACK (1);
5915                         CHECK_STACK_OVF (1);
5916                         sp--;
5917                         ins = *sp;
5918                 
5919                         /* 
5920                          * small optimization: if the loaded value was from a local already,
5921                          * just load it twice.
5922                          */
5923                         if (ins->ssa_op == MONO_SSA_LOAD && 
5924                             (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
5925                                 sp++;
5926                                 MONO_INST_NEW (cfg, temp, 0);
5927                                 *temp = *ins;
5928                                 *sp++ = temp;
5929                         } else {
5930                                 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
5931                                 temp->flags |= MONO_INST_IS_TEMP;
5932                                 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
5933                                 /* FIXME: handle CEE_STIND_R4 */
5934                                 if (store->opcode == CEE_STOBJ) {
5935                                         NEW_TEMPLOADA (cfg, store, temp->inst_c0);
5936                                         handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE, FALSE);
5937                                 } else {
5938                                         MONO_ADD_INS (bblock, store);
5939                                 }
5940                                 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
5941                                 *sp++ = ins;
5942                                 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
5943                                 *sp++ = ins;
5944                         }
5945                         ++ip;
5946                         inline_costs += 2;
5947                         break;
5948                 }
5949                 case CEE_POP:
5950                         CHECK_STACK (1);
5951                         MONO_INST_NEW (cfg, ins, CEE_POP);
5952                         MONO_ADD_INS (bblock, ins);
5953                         ip++;
5954                         --sp;
5955                         ins->inst_i0 = *sp;
5956                         break;
5957                 case CEE_JMP:
5958                         CHECK_OPSIZE (5);
5959                         if (stack_start != sp)
5960                                 UNVERIFIED;
5961                         MONO_INST_NEW (cfg, ins, OP_JMP);
5962                         token = read32 (ip + 1);
5963                         /* FIXME: check the signature matches */
5964                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
5965
5966                         if (!cmethod)
5967                                 goto load_error;
5968
5969                         if (cfg->generic_sharing_context && mono_method_check_context_used (cmethod))
5970                                 GENERIC_SHARING_FAILURE (CEE_JMP);
5971
5972                         if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
5973                                 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
5974                                         INLINE_FAILURE;
5975                                 CHECK_CFG_EXCEPTION;
5976                         }
5977
5978                         ins->inst_p0 = cmethod;
5979                         MONO_ADD_INS (bblock, ins);
5980                         ip += 5;
5981                         start_new_bblock = 1;
5982                         break;
5983                 case CEE_CALLI:
5984                 case CEE_CALL:
5985                 case CEE_CALLVIRT: {
5986                         MonoInst *addr = NULL;
5987                         MonoMethodSignature *fsig = NULL;
5988                         int temp, array_rank = 0;
5989                         int virtual = *ip == CEE_CALLVIRT;
5990                         gboolean no_spill;
5991                         gboolean pass_imt_from_rgctx = FALSE;
5992                         MonoInst *imt_arg = NULL;
5993                         gboolean pass_vtable = FALSE;
5994                         gboolean pass_mrgctx = FALSE;
5995                         MonoInst *vtable_arg = NULL;
5996                         gboolean check_this = FALSE;
5997
5998                         CHECK_OPSIZE (5);
5999                         token = read32 (ip + 1);
6000
6001                         if (*ip == CEE_CALLI) {
6002                                 cmethod = NULL;
6003                                 CHECK_STACK (1);
6004                                 --sp;
6005                                 addr = *sp;
6006                                 if (method->wrapper_type != MONO_WRAPPER_NONE)
6007                                         fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
6008                                 else
6009                                         fsig = mono_metadata_parse_signature (image, token);
6010
6011                                 n = fsig->param_count + fsig->hasthis;
6012                         } else {
6013                                 MonoMethod *cil_method;
6014                                 
6015                                 if (method->wrapper_type != MONO_WRAPPER_NONE) {
6016                                         cmethod =  (MonoMethod *)mono_method_get_wrapper_data (method, token);
6017                                         cil_method = cmethod;
6018                                 } else if (constrained_call) {
6019                                         cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context, &cil_method);
6020                                         cil_method = cmethod;
6021                                 } else {
6022                                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
6023                                         cil_method = cmethod;
6024                                 }
6025
6026                                 if (!cmethod)
6027                                         goto load_error;
6028                                 if (!dont_verify && !cfg->skip_visibility) {
6029                                         MonoMethod *target_method = cil_method;
6030                                         if (method->is_inflated) {
6031                                                 target_method = mini_get_method_allow_open (method, token, NULL, &(mono_method_get_generic_container (method_definition)->context));
6032                                         }
6033                                         if (!mono_method_can_access_method (method_definition, target_method) &&
6034                                                 !mono_method_can_access_method (method, cil_method))
6035                                                 METHOD_ACCESS_FAILURE;
6036                                 }
6037
6038                                 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
6039                                         ensure_method_is_allowed_to_call_method (cfg, method, cil_method, bblock, ip);
6040
6041                                 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
6042                                         /* MS.NET seems to silently convert this to a callvirt */
6043                                         virtual = 1;
6044
6045                                 if (!cmethod->klass->inited){
6046                                         if (!mono_class_init (cmethod->klass))
6047                                                 goto load_error;
6048                                 }
6049
6050                                 if (mono_method_signature (cmethod)->pinvoke) {
6051                                         MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod, check_for_pending_exc, FALSE);
6052                                         fsig = mono_method_signature (wrapper);
6053                                 } else if (constrained_call) {
6054                                         fsig = mono_method_signature (cmethod);
6055                                 } else {
6056                                         fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
6057                                 }
6058
6059                                 mono_save_token_info (cfg, image, token, cmethod);
6060
6061                                 n = fsig->param_count + fsig->hasthis;
6062
6063                                 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
6064                                         if (check_linkdemand (cfg, method, cmethod, bblock, ip))
6065                                                 INLINE_FAILURE;
6066                                         CHECK_CFG_EXCEPTION;
6067                                 }
6068
6069                                 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
6070                                     mini_class_is_system_array (cmethod->klass)) {
6071                                         array_rank = cmethod->klass->rank;
6072                                 }
6073
6074                                 if (cmethod->string_ctor)
6075                                         g_assert_not_reached ();
6076
6077                         }
6078
6079                         if (!cfg->generic_sharing_context && cmethod && cmethod->klass->generic_container)
6080                                 UNVERIFIED;
6081
6082                         if (!cfg->generic_sharing_context && cmethod)
6083                                 g_assert (!mono_method_check_context_used (cmethod));
6084
6085                         CHECK_STACK (n);
6086
6087                         //g_assert (!virtual || fsig->hasthis);
6088
6089                         sp -= n;
6090
6091                         if (constrained_call) {
6092                                 /*
6093                                  * We have the `constrained.' prefix opcode.
6094                                  */
6095                                 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
6096                                         MonoInst *load;
6097                                         /*
6098                                          * The type parameter is instantiated as a valuetype,
6099                                          * but that type doesn't override the method we're
6100                                          * calling, so we need to box `this'.
6101                                          * sp [0] is a pointer to the data: we need the value
6102                                          * in handle_box (), so load it here.
6103                                          */
6104                                         MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, &constrained_call->byval_arg));
6105                                         type_to_eval_stack_type (cfg, &constrained_call->byval_arg, load);
6106                                         load->inst_left = sp [0];
6107                                         sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
6108                                 } else if (!constrained_call->valuetype) {
6109                                         MonoInst *ins;
6110
6111                                         /*
6112                                          * The type parameter is instantiated as a reference
6113                                          * type.  We have a managed pointer on the stack, so
6114                                          * we need to dereference it here.
6115                                          */
6116
6117                                         MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
6118                                         ins->inst_i0 = sp [0];
6119                                         ins->type = STACK_OBJ;
6120                                         ins->klass = mono_class_from_mono_type (&constrained_call->byval_arg);
6121                                         sp [0] = ins;
6122                                 } else if (cmethod->klass->valuetype)
6123                                         virtual = 0;
6124                                 constrained_call = NULL;
6125                         }
6126
6127                         if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
6128                                 UNVERIFIED;
6129
6130                         if (cmethod && ((cmethod->flags & METHOD_ATTRIBUTE_STATIC) || cmethod->klass->valuetype) &&
6131                                         (cmethod->klass->generic_class || cmethod->klass->generic_container)) {
6132                                 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
6133                                 MonoGenericContext *context = mini_class_get_context (cmethod->klass);
6134                                 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
6135
6136                                 /*
6137                                  * Pass vtable iff target method might
6138                                  * be shared, which means that sharing
6139                                  * is enabled for its class and its
6140                                  * context is sharable (and it's not a
6141                                  * generic method).
6142                                  */
6143                                 if (sharing_enabled && context_sharable &&
6144                                         !(mini_method_get_context (cmethod) && mini_method_get_context (cmethod)->method_inst))
6145                                         pass_vtable = TRUE;
6146                         }
6147
6148                         if (cmethod && mini_method_get_context (cmethod) &&
6149                                         mini_method_get_context (cmethod)->method_inst) {
6150                                 gboolean sharing_enabled = mono_class_generic_sharing_enabled (cmethod->klass);
6151                                 MonoGenericContext *context = mini_method_get_context (cmethod);
6152                                 gboolean context_sharable = mono_generic_context_is_sharable (context, TRUE);
6153
6154                                 g_assert (!pass_vtable);
6155
6156                                 if (sharing_enabled && context_sharable)
6157                                         pass_mrgctx = TRUE;
6158                         }
6159
6160                         if (cfg->generic_sharing_context && cmethod) {
6161                                 MonoGenericContext *cmethod_context = mono_method_get_context (cmethod);
6162
6163                                 context_used = mono_method_check_context_used (cmethod);
6164
6165                                 if (context_used && (cmethod->klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
6166                                         /* Generic method interface
6167                                            calls are resolved via a
6168                                            helper function and don't
6169                                            need an imt. */
6170                                         if (!cmethod_context || !cmethod_context->method_inst)
6171                                                 pass_imt_from_rgctx = TRUE;
6172                                 }
6173
6174                                 /*
6175                                  * If a shared method calls another
6176                                  * shared method then the caller must
6177                                  * have a generic sharing context
6178                                  * because the magic trampoline
6179                                  * requires it.  FIXME: We shouldn't
6180                                  * have to force the vtable/mrgctx
6181                                  * variable here.  Instead there
6182                                  * should be a flag in the cfg to
6183                                  * request a generic sharing context.
6184                                  */
6185                                 if (context_used && ((method->flags & METHOD_ATTRIBUTE_STATIC) || method->klass->valuetype))
6186                                         mono_get_vtable_var (cfg);
6187                         }
6188
6189                         if (pass_vtable) {
6190                                 if (context_used) {
6191                                         MonoInst *rgctx;
6192
6193                                         GET_RGCTX (rgctx, context_used);
6194                                         vtable_arg = get_runtime_generic_context_ptr (cfg, method, context_used,
6195                                                 bblock, cmethod->klass, generic_context, rgctx, MONO_RGCTX_INFO_VTABLE, ip);
6196                                 } else {
6197                                         MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
6198                                         
6199                                         CHECK_TYPELOAD (cmethod->klass);
6200                                         NEW_VTABLECONST (cfg, vtable_arg, vtable);
6201                                 }
6202                         }
6203
6204                         if (pass_mrgctx) {
6205                                 g_assert (!vtable_arg);
6206
6207                                 if (context_used) {
6208                                         MonoInst *rgctx;
6209
6210                                         GET_RGCTX (rgctx, context_used);
6211                                         vtable_arg = get_runtime_generic_context_method_rgctx (cfg, method,
6212                                                 context_used, bblock, cmethod, generic_context, rgctx, ip);
6213                                 } else {
6214                                         MonoMethodRuntimeGenericContext *mrgctx;
6215
6216                                         mrgctx = mono_method_lookup_rgctx (mono_class_vtable (cfg->domain, cmethod->klass),
6217                                                 mini_method_get_context (cmethod)->method_inst);
6218
6219                                         cfg->disable_aot = TRUE;
6220                                         NEW_PCONST (cfg, vtable_arg, mrgctx);
6221                                 }
6222
6223                                 if (!(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
6224                                                 (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) {
6225                                         if (virtual)
6226                                                 check_this = TRUE;
6227                                         virtual = 0;
6228                                 }
6229                         }
6230
6231                         if (pass_imt_from_rgctx) {
6232                                 MonoInst *rgctx;
6233
6234                                 g_assert (!pass_vtable);
6235                                 g_assert (cmethod);
6236
6237                                 GET_RGCTX (rgctx, context_used);
6238                                 imt_arg = get_runtime_generic_context_method (cfg, method, context_used, bblock, cmethod,
6239                                                 generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
6240                         }
6241
6242                         if (check_this) {
6243                                 MonoInst *check;
6244
6245                                 MONO_INST_NEW (cfg, check, OP_CHECK_THIS_PASSTHROUGH);
6246                                 check->cil_code = ip;
6247                                 check->inst_left = sp [0];
6248                                 check->type = sp [0]->type;
6249                                 sp [0] = check;
6250                         }
6251
6252                         if (cmethod && virtual && 
6253                             (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) && 
6254                             !((cmethod->flags & METHOD_ATTRIBUTE_FINAL) && 
6255                               cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
6256                             mono_method_signature (cmethod)->generic_param_count) {
6257                                 MonoInst *this_temp, *this_arg_temp, *store;
6258                                 MonoInst *iargs [4];
6259
6260                                 g_assert (mono_method_signature (cmethod)->is_inflated);
6261                                 /* Prevent inlining of methods that contain indirect calls */
6262                                 INLINE_FAILURE;
6263
6264                                 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
6265                                 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
6266                                 MONO_ADD_INS (bblock, store);
6267
6268                                 /* FIXME: This should be a managed pointer */
6269                                 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
6270
6271                                 /* Because of the PCONST below */
6272                                 cfg->disable_aot = TRUE;
6273                                 NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
6274                                 if (context_used) {
6275                                         MonoInst *rgctx;
6276
6277                                         GET_RGCTX (rgctx, context_used);
6278                                         iargs [1] = get_runtime_generic_context_method (cfg, method, context_used,
6279                                                         bblock, cmethod,
6280                                                         generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
6281                                         NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
6282                                         temp = mono_emit_jit_icall (cfg, bblock,
6283                                                 mono_helper_compile_generic_method, iargs, ip);
6284                                 } else {
6285                                         NEW_METHODCONST (cfg, iargs [1], cmethod);
6286                                         NEW_TEMPLOADA (cfg, iargs [2], this_arg_temp->inst_c0);
6287                                         temp = mono_emit_jit_icall (cfg, bblock, mono_helper_compile_generic_method,
6288                                                 iargs, ip);
6289                                 }
6290
6291                                 NEW_TEMPLOAD (cfg, addr, temp);
6292                                 NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
6293
6294                                 if ((temp = mono_emit_calli_spilled (cfg, bblock, fsig, sp, addr, ip)) != -1) {
6295                                         NEW_TEMPLOAD (cfg, *sp, temp);
6296                                         sp++;
6297                                 }
6298
6299                                 ip += 5;
6300                                 ins_flag = 0;
6301                                 break;
6302                         }
6303
6304                         /* FIXME: runtime generic context pointer for jumps? */
6305                         /* FIXME: handle this for generic sharing eventually */
6306                         if ((ins_flag & MONO_INST_TAILCALL) && !cfg->generic_sharing_context && !vtable_arg && cmethod && (*ip == CEE_CALL) &&
6307                                  (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
6308                                 int i;
6309
6310                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
6311                                 INLINE_FAILURE;
6312                                 /* FIXME: This assumes the two methods has the same number and type of arguments */
6313                                 /*
6314                                  * We implement tail calls by storing the actual arguments into the 
6315                                  * argument variables, then emitting a OP_JMP. Since the actual arguments
6316                                  * can refer to the arg variables, we have to spill them.
6317                                  */
6318                                 handle_loaded_temps (cfg, bblock, sp, sp + n);
6319                                 for (i = 0; i < n; ++i) {
6320                                         /* Prevent argument from being register allocated */
6321                                         arg_array [i]->flags |= MONO_INST_VOLATILE;
6322
6323                                         /* Check if argument is the same */
6324                                         /* 
6325                                          * FIXME: This loses liveness info, so it can only be done if the
6326                                          * argument is not register allocated.
6327                                          */
6328                                         NEW_ARGLOAD (cfg, ins, i);
6329                                         if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
6330                                                 continue;
6331
6332                                         NEW_ARGSTORE (cfg, ins, i, sp [i]);
6333                                         /* FIXME: handle CEE_STIND_R4 */
6334                                         if (ins->opcode == CEE_STOBJ) {
6335                                                 NEW_ARGLOADA (cfg, ins, i);
6336                                                 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE, FALSE);
6337                                         }
6338                                         else
6339                                                 MONO_ADD_INS (bblock, ins);
6340                                 }
6341                                 MONO_INST_NEW (cfg, ins, OP_JMP);
6342                                 ins->inst_p0 = cmethod;
6343                                 ins->inst_p1 = arg_array [0];
6344                                 MONO_ADD_INS (bblock, ins);
6345                                 link_bblock (cfg, bblock, end_bblock);                  
6346                                 start_new_bblock = 1;
6347                                 /* skip CEE_RET as well */
6348                                 ip += 6;
6349                                 ins_flag = 0;
6350                                 break;
6351                         }
6352                         if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_inst_for_method (cfg, cmethod, fsig, sp))) {
6353                                 if (MONO_TYPE_IS_VOID (fsig->ret)) {
6354                                         MONO_ADD_INS (bblock, ins);
6355                                 } else {
6356                                         type_to_eval_stack_type (cfg, fsig->ret, ins);
6357                                         *sp = ins;
6358                                         sp++;
6359                                 }
6360
6361                                 ip += 5;
6362                                 ins_flag = 0;
6363                                 break;
6364                         }
6365
6366                         handle_loaded_temps (cfg, bblock, stack_start, sp);
6367
6368                         if ((cfg->opt & MONO_OPT_INLINE) && cmethod && //!check_this &&
6369                             (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) && 
6370                             mono_method_check_inlining (cfg, cmethod) &&
6371                                  !g_list_find (dont_inline, cmethod)) {
6372                                 int costs;
6373                                 MonoBasicBlock *ebblock;
6374                                 gboolean allways = FALSE;
6375
6376                                 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6377                                         (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6378                                         /* Prevent inlining of methods that call wrappers */
6379                                         INLINE_FAILURE;
6380                                         cmethod = mono_marshal_get_native_wrapper (cmethod, check_for_pending_exc, FALSE);
6381                                         allways = TRUE;
6382                                 }
6383
6384                                 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
6385                                         ip += 5;
6386                                         real_offset += 5;
6387
6388                                         GET_BBLOCK (cfg, bblock, ip);
6389                                         ebblock->next_bb = bblock;
6390                                         link_bblock (cfg, ebblock, bblock);
6391
6392                                         if (!MONO_TYPE_IS_VOID (fsig->ret))
6393                                                 sp++;
6394
6395                                         /* indicates start of a new block, and triggers a load of all 
6396                                            stack arguments at bb boundarie */
6397                                         bblock = ebblock;
6398
6399                                         inline_costs += costs;
6400                                         ins_flag = 0;
6401                                         break;
6402                                 }
6403                         }
6404                         
6405                         inline_costs += 10 * num_calls++;
6406
6407                         /* tail recursion elimination */
6408                         if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET &&
6409                                         !vtable_arg) {
6410                                 gboolean has_vtargs = FALSE;
6411                                 int i;
6412                                 
6413                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
6414                                 INLINE_FAILURE;
6415                                 /* keep it simple */
6416                                 for (i =  fsig->param_count - 1; i >= 0; i--) {
6417                                         if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i])) 
6418                                                 has_vtargs = TRUE;
6419                                 }
6420
6421                                 if (!has_vtargs) {
6422                                         for (i = 0; i < n; ++i) {
6423                                                 /* FIXME: handle CEE_STIND_R4 */
6424                                                 NEW_ARGSTORE (cfg, ins, i, sp [i]);
6425                                                 MONO_ADD_INS (bblock, ins);
6426                                         }
6427                                         MONO_INST_NEW (cfg, ins, OP_BR);
6428                                         MONO_ADD_INS (bblock, ins);
6429                                         tblock = start_bblock->out_bb [0];
6430                                         link_bblock (cfg, bblock, tblock);
6431                                         ins->inst_target_bb = tblock;
6432                                         start_new_bblock = 1;
6433
6434                                         /* skip the CEE_RET, too */
6435                                         if (ip_in_bb (cfg, bblock, ip + 5))
6436                                                 ip += 6;
6437                                         else
6438                                                 ip += 5;
6439                                         ins_flag = 0;
6440                                         break;
6441                                 }
6442                         }
6443
6444                         if (ip_in_bb (cfg, bblock, ip + 5) 
6445                                 && (!MONO_TYPE_ISSTRUCT (fsig->ret))
6446                                 && (!MONO_TYPE_IS_VOID (fsig->ret) || (cmethod && cmethod->string_ctor))
6447                                 && (CODE_IS_STLOC (ip + 5) || ip [5] == CEE_POP || ip [5] == CEE_RET))
6448                                 /* No need to spill */
6449                                 no_spill = TRUE;
6450                         else
6451                                 no_spill = FALSE;
6452
6453                         if (context_used && !imt_arg &&
6454                                         (!mono_method_is_generic_sharable_impl (cmethod, TRUE) ||
6455                                                 !mono_class_generic_sharing_enabled (cmethod->klass)) &&
6456                                         (!virtual || cmethod->flags & METHOD_ATTRIBUTE_FINAL ||
6457                                                 !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL))) {
6458                                 MonoInst *rgctx;
6459
6460                                 INLINE_FAILURE;
6461
6462                                 g_assert (cfg->generic_sharing_context && cmethod);
6463                                 g_assert (!addr);
6464
6465                                 /*
6466                                  * We are compiling a call to
6467                                  * non-shared generic code from shared
6468                                  * code, which means that we have to
6469                                  * look up the method in the rgctx and
6470                                  * do an indirect call.
6471                                  */
6472                                 GET_RGCTX (rgctx, context_used);
6473                                 addr = get_runtime_generic_context_method (cfg, method, context_used, bblock, cmethod,
6474                                                 generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
6475
6476                         }
6477
6478                         if (addr) {
6479                                 g_assert (!imt_arg);
6480
6481                                 if (*ip == CEE_CALL) {
6482                                         g_assert (context_used);
6483                                 } else if (*ip == CEE_CALLI) {
6484                                         g_assert (!vtable_arg);
6485                                 } else {
6486                                         g_assert (cmethod->flags & METHOD_ATTRIBUTE_FINAL ||
6487                                                         !(cmethod->flags & METHOD_ATTRIBUTE_FINAL));
6488                                 }
6489
6490                                 /* Prevent inlining of methods with indirect calls */
6491                                 INLINE_FAILURE;
6492                                 if (no_spill) {
6493                                         ins = (MonoInst*)mono_emit_rgctx_calli (cfg, bblock, fsig, sp, addr, vtable_arg, ip);
6494                                         *sp++ = ins;                                    
6495                                 } else {
6496                                         temp = mono_emit_rgctx_calli_spilled (cfg, bblock, fsig, sp, addr, vtable_arg, ip);
6497                                         if (temp != -1) {
6498                                                 NEW_TEMPLOAD (cfg, *sp, temp);
6499                                                 NEW_TEMPLOAD_SOFT_FLOAT (cfg, bblock, *sp, temp, ip);
6500                                                 sp++;
6501                                         }
6502                                 }                       
6503                         } else if (array_rank) {
6504                                 MonoInst *addr;
6505
6506                                 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */ 
6507                                         if (sp [fsig->param_count]->type == STACK_OBJ) {
6508                                                 MonoInst *iargs [2];
6509                                                 MonoInst *array, *to_store, *store;
6510
6511                                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
6512                                                 
6513                                                 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
6514                                                 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
6515                                                 MONO_ADD_INS (bblock, store);
6516                                                 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
6517
6518                                                 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
6519                                                 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
6520                                                 /* FIXME: handle CEE_STIND_R4 */
6521                                                 MONO_ADD_INS (bblock, store);
6522                                                 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
6523
6524                                                 /*
6525                                                  * We first save the args for the call so that the args are copied to the stack
6526                                                  * and a new instruction tree for them is created. If we don't do this,
6527                                                  * the same MonoInst is added to two different trees and this is not 
6528                                                  * allowed by burg.
6529                                                  */
6530                                                 mono_emit_jit_icall (cfg, bblock, mono_helper_stelem_ref_check, iargs, ip);
6531
6532                                                 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
6533                                                 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
6534                                         }
6535
6536                                         addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
6537                                         NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
6538                                         /* FIXME: handle CEE_STIND_R4 */
6539                                         if (ins->opcode == CEE_STOBJ) {
6540                                                 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE, TRUE);
6541                                         } else {
6542                                                 MONO_ADD_INS (bblock, ins);
6543                                         }
6544
6545                                 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
6546                                         addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
6547                                         NEW_INDLOAD (cfg, ins, addr, fsig->ret);
6548
6549                                         *sp++ = ins;
6550                                 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
6551                                         if (!cmethod->klass->element_class->valuetype && !readonly) {
6552                                                 MonoInst* check;
6553                                                 //* Needed by the code generated in inssel.brg * /
6554                                                 mono_get_got_var (cfg);
6555
6556                                                 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
6557                                                 check->klass = cmethod->klass;
6558                                                 check->inst_left = sp [0];
6559                                                 check->type = STACK_OBJ;
6560                                                 sp [0] = check;
6561                                         }
6562
6563                                         readonly = FALSE;
6564                                         addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
6565                                         *sp++ = addr;
6566                                 } else {
6567                                         g_assert_not_reached ();
6568                                 }
6569
6570                         } else {
6571                                 /* Prevent inlining of methods which call other methods */
6572                                 INLINE_FAILURE;
6573                                 if (mini_redirect_call (&temp, cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) {
6574                                         if (temp != -1) {
6575                                                 NEW_TEMPLOAD (cfg, *sp, temp);
6576                                                 sp++;
6577                                         }
6578                                 } else if (no_spill) {
6579                                         ins = (MonoInst*)mono_emit_rgctx_method_call (cfg, bblock, cmethod, fsig, sp,
6580                                                         vtable_arg, imt_arg, ip, virtual ? sp [0] : NULL);
6581                                         *sp++ = ins;
6582                                 } else {
6583                                         if ((temp = mono_emit_rgctx_method_call_spilled (cfg, bblock, cmethod, fsig, sp,
6584                                                         vtable_arg, imt_arg, ip, virtual ? sp [0] : NULL)) != -1) {
6585                                                 MonoInst *load;
6586                                                 NEW_TEMPLOAD (cfg, load, temp);
6587
6588 #ifdef MONO_ARCH_SOFT_FLOAT
6589                                                 if (load->opcode == CEE_LDIND_R4) {
6590                                                         NEW_TEMPLOADA (cfg, load, temp);
6591                                                         temp = handle_load_float (cfg, bblock, load, ip);
6592                                                         NEW_TEMPLOAD (cfg, load, temp);
6593                                                 }
6594 #endif
6595                                                 *sp++ = load;
6596                                         }
6597                                 }
6598                         }
6599
6600                         ip += 5;
6601                         ins_flag = 0;
6602                         break;
6603                 }
6604                 case CEE_RET:
6605                         if (cfg->method != method) {
6606                                 /* return from inlined method */
6607                                 if (return_var) {
6608                                         MonoInst *store;
6609                                         CHECK_STACK (1);
6610                                         --sp;
6611                                         //g_assert (returnvar != -1);
6612                                         NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
6613                                         store->cil_code = sp [0]->cil_code;
6614                                         if (store->opcode == CEE_STOBJ) {
6615                                                 g_assert_not_reached ();
6616                                                 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
6617                                                 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
6618                                                 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE, FALSE);
6619 #ifdef MONO_ARCH_SOFT_FLOAT
6620                                         } else if (store->opcode == CEE_STIND_R4) {
6621                                                 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
6622                                                 handle_store_float (cfg, bblock, store, *sp, sp [0]->cil_code);
6623 #endif
6624                                         } else
6625                                                 MONO_ADD_INS (bblock, store);
6626                                 } 
6627                         } else {
6628                                 if (cfg->ret) {
6629                                         g_assert (!return_var);
6630                                         CHECK_STACK (1);
6631                                         --sp;
6632                                         MONO_INST_NEW (cfg, ins, OP_NOP);
6633                                         ins->opcode = mini_type_to_stind (cfg, mono_method_signature (method)->ret);
6634                                         if (ins->opcode == CEE_STOBJ) {
6635                                                 NEW_RETLOADA (cfg, ins);
6636                                                 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
6637                                                 handle_stobj (cfg, bblock, ins, *sp, ip, cfg->ret->klass, FALSE, FALSE, FALSE);
6638                                         } else {
6639                                                 ins->opcode = OP_SETRET;
6640                                                 ins->inst_i0 = *sp;;
6641                                                 ins->inst_i1 = NULL;
6642                                                 MONO_ADD_INS (bblock, ins);
6643                                         }
6644                                 }
6645                         }
6646                         if (sp != stack_start)
6647                                 UNVERIFIED;
6648                         MONO_INST_NEW (cfg, ins, OP_BR);
6649                         ip++;
6650                         ins->inst_target_bb = end_bblock;
6651                         MONO_ADD_INS (bblock, ins);
6652                         link_bblock (cfg, bblock, end_bblock);
6653                         start_new_bblock = 1;
6654                         break;
6655                 case CEE_BR_S:
6656                         CHECK_OPSIZE (2);
6657                         MONO_INST_NEW (cfg, ins, OP_BR);
6658                         ip++;
6659                         MONO_ADD_INS (bblock, ins);
6660                         target = ip + 1 + (signed char)(*ip);
6661                         ++ip;
6662                         GET_BBLOCK (cfg, tblock, target);
6663                         link_bblock (cfg, bblock, tblock);
6664                         CHECK_BBLOCK (target, ip, tblock);
6665                         ins->inst_target_bb = tblock;
6666                         if (sp != stack_start) {
6667                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6668                                 sp = stack_start;
6669                                 CHECK_UNVERIFIABLE (cfg);
6670                         }
6671                         start_new_bblock = 1;
6672                         inline_costs += BRANCH_COST;
6673                         break;
6674                 case CEE_BRFALSE_S:
6675                 case CEE_BRTRUE_S:
6676                         CHECK_OPSIZE (2);
6677                         CHECK_STACK (1);
6678                         if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
6679                                 UNVERIFIED;
6680                         MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
6681                         ip++;
6682                         target = ip + 1 + *(signed char*)ip;
6683                         ip++;
6684                         ADD_UNCOND (ins->opcode == CEE_BRTRUE);
6685                         if (sp != stack_start) {
6686                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6687                                 sp = stack_start;
6688                                 CHECK_UNVERIFIABLE (cfg);
6689                         }
6690                         inline_costs += BRANCH_COST;
6691                         break;
6692                 case CEE_BEQ_S:
6693                 case CEE_BGE_S:
6694                 case CEE_BGT_S:
6695                 case CEE_BLE_S:
6696                 case CEE_BLT_S:
6697                 case CEE_BNE_UN_S:
6698                 case CEE_BGE_UN_S:
6699                 case CEE_BGT_UN_S:
6700                 case CEE_BLE_UN_S:
6701                 case CEE_BLT_UN_S:
6702                         CHECK_OPSIZE (2);
6703                         CHECK_STACK (2);
6704                         MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
6705                         ip++;
6706                         target = ip + 1 + *(signed char*)ip;
6707                         ip++;
6708 #ifdef MONO_ARCH_SOFT_FLOAT
6709                         if (sp [-1]->type == STACK_R8 || sp [-2]->type == STACK_R8) {
6710                                 ins->opcode = condbr_to_fp_br (ins->opcode);
6711                                 sp -= 2;
6712                                 ins->inst_left = sp [0];
6713                                 ins->inst_right = sp [1];
6714                                 ins->type = STACK_I4;
6715                                 *sp++ = emit_tree (cfg, bblock, ins, ins->cil_code);
6716                                 MONO_INST_NEW (cfg, ins, CEE_BRTRUE);
6717                                 ADD_UNCOND (TRUE);
6718                         } else {
6719                                 ADD_BINCOND (NULL);
6720                         }
6721 #else
6722                         ADD_BINCOND (NULL);
6723 #endif
6724                         if (sp != stack_start) {
6725                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6726                                 sp = stack_start;
6727                                 CHECK_UNVERIFIABLE (cfg);
6728                         }
6729                         inline_costs += BRANCH_COST;
6730                         break;
6731                 case CEE_BR:
6732                         CHECK_OPSIZE (5);
6733                         MONO_INST_NEW (cfg, ins, OP_BR);
6734                         ip++;
6735                         MONO_ADD_INS (bblock, ins);
6736                         target = ip + 4 + (gint32)read32(ip);
6737                         ip += 4;
6738                         GET_BBLOCK (cfg, tblock, target);
6739                         link_bblock (cfg, bblock, tblock);
6740                         CHECK_BBLOCK (target, ip, tblock);
6741                         ins->inst_target_bb = tblock;
6742                         if (sp != stack_start) {
6743                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6744                                 sp = stack_start;
6745                                 CHECK_UNVERIFIABLE (cfg);
6746                         }
6747                         start_new_bblock = 1;
6748                         inline_costs += BRANCH_COST;
6749                         break;
6750                 case CEE_BRFALSE:
6751                 case CEE_BRTRUE:
6752                         CHECK_OPSIZE (5);
6753                         CHECK_STACK (1);
6754                         if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
6755                                 UNVERIFIED;
6756                         MONO_INST_NEW (cfg, ins, *ip);
6757                         ip++;
6758                         target = ip + 4 + (gint32)read32(ip);
6759                         ip += 4;
6760                         ADD_UNCOND(ins->opcode == CEE_BRTRUE);
6761                         if (sp != stack_start) {
6762                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6763                                 sp = stack_start;
6764                                 CHECK_UNVERIFIABLE (cfg);
6765                         }
6766                         inline_costs += BRANCH_COST;
6767                         break;
6768                 case CEE_BEQ:
6769                 case CEE_BGE:
6770                 case CEE_BGT:
6771                 case CEE_BLE:
6772                 case CEE_BLT:
6773                 case CEE_BNE_UN:
6774                 case CEE_BGE_UN:
6775                 case CEE_BGT_UN:
6776                 case CEE_BLE_UN:
6777                 case CEE_BLT_UN:
6778                         CHECK_OPSIZE (5);
6779                         CHECK_STACK (2);
6780                         MONO_INST_NEW (cfg, ins, *ip);
6781                         ip++;
6782                         target = ip + 4 + (gint32)read32(ip);
6783                         ip += 4;
6784 #ifdef MONO_ARCH_SOFT_FLOAT
6785                         if (sp [-1]->type == STACK_R8 || sp [-2]->type == STACK_R8) {
6786                                 ins->opcode = condbr_to_fp_br (ins->opcode);
6787                                 sp -= 2;
6788                                 ins->inst_left = sp [0];
6789                                 ins->inst_right = sp [1];
6790                                 ins->type = STACK_I4;
6791                                 *sp++ = emit_tree (cfg, bblock, ins, ins->cil_code);
6792                                 MONO_INST_NEW (cfg, ins, CEE_BRTRUE);
6793                                 ADD_UNCOND (TRUE);
6794                         } else {
6795                                 ADD_BINCOND (NULL);
6796                         }
6797 #else
6798                         ADD_BINCOND (NULL);
6799 #endif
6800                         if (sp != stack_start) {
6801                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6802                                 sp = stack_start;
6803                                 CHECK_UNVERIFIABLE (cfg);
6804                         }
6805                         inline_costs += BRANCH_COST;
6806                         break;
6807                 case CEE_SWITCH:
6808                         CHECK_OPSIZE (5);
6809                         CHECK_STACK (1);
6810                         n = read32 (ip + 1);
6811                         MONO_INST_NEW (cfg, ins, OP_SWITCH);
6812                         --sp;
6813                         ins->inst_left = *sp;
6814                         if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR)) 
6815                                 UNVERIFIED;
6816                         ip += 5;
6817                         CHECK_OPSIZE (n * sizeof (guint32));
6818                         target = ip + n * sizeof (guint32);
6819                         MONO_ADD_INS (bblock, ins);
6820                         GET_BBLOCK (cfg, tblock, target);
6821                         link_bblock (cfg, bblock, tblock);
6822                         ins->klass = GUINT_TO_POINTER (n);
6823                         ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
6824                         ins->inst_many_bb [n] = tblock;
6825
6826                         for (i = 0; i < n; ++i) {
6827                                 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
6828                                 link_bblock (cfg, bblock, tblock);
6829                                 ins->inst_many_bb [i] = tblock;
6830                                 ip += 4;
6831                         }
6832                         if (sp != stack_start) {
6833                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6834                                 sp = stack_start;
6835                                 CHECK_UNVERIFIABLE (cfg);
6836                         }
6837                         /* Needed by the code generated in inssel.brg */
6838                         mono_get_got_var (cfg);
6839                         inline_costs += (BRANCH_COST * 2);
6840                         break;
6841                 case CEE_LDIND_I1:
6842                 case CEE_LDIND_U1:
6843                 case CEE_LDIND_I2:
6844                 case CEE_LDIND_U2:
6845                 case CEE_LDIND_I4:
6846                 case CEE_LDIND_U4:
6847                 case CEE_LDIND_I8:
6848                 case CEE_LDIND_I:
6849                 case CEE_LDIND_R4:
6850                 case CEE_LDIND_R8:
6851                 case CEE_LDIND_REF:
6852                         CHECK_STACK (1);
6853                         MONO_INST_NEW (cfg, ins, *ip);
6854                         --sp;
6855                         ins->inst_i0 = *sp;
6856                         *sp++ = ins;
6857                         ins->type = ldind_type [*ip - CEE_LDIND_I1];
6858                         ins->flags |= ins_flag;
6859                         ins_flag = 0;
6860                         if (ins->type == STACK_OBJ)
6861                                 ins->klass = mono_defaults.object_class;
6862 #ifdef MONO_ARCH_SOFT_FLOAT
6863                         if (*ip == CEE_LDIND_R4) {
6864                                 int temp;
6865                                 --sp;
6866                                 temp = handle_load_float (cfg, bblock, ins->inst_i0, ip);
6867                                 NEW_TEMPLOAD (cfg, *sp, temp);
6868                                 sp++;
6869                         }
6870 #endif
6871                         ++ip;
6872                         break;
6873                 case CEE_STIND_REF:
6874                 case CEE_STIND_I1:
6875                 case CEE_STIND_I2:
6876                 case CEE_STIND_I4:
6877                 case CEE_STIND_I8:
6878                 case CEE_STIND_R4:
6879                 case CEE_STIND_R8:
6880                         CHECK_STACK (2);
6881 #ifdef MONO_ARCH_SOFT_FLOAT
6882                         if (*ip == CEE_STIND_R4) {
6883                                 sp -= 2;
6884                                 handle_store_float (cfg, bblock, sp [0], sp [1], ip);
6885                                 ip++;
6886                                 break;
6887                         }
6888 #endif
6889 #if HAVE_WRITE_BARRIERS
6890                         if (*ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER && !((sp [-1]->opcode == OP_PCONST) && (sp [-1]->inst_p0 == 0))) {
6891                                 /* insert call to write barrier */
6892                                 MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
6893                                 sp -= 2;
6894                                 mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), sp, ip, NULL);
6895                                 ip++;
6896                                 break;
6897                         }
6898 #endif
6899                         MONO_INST_NEW (cfg, ins, *ip);
6900                         ip++;
6901                         sp -= 2;
6902                         handle_loaded_temps (cfg, bblock, stack_start, sp);
6903                         MONO_ADD_INS (bblock, ins);
6904                         ins->inst_i0 = sp [0];
6905                         ins->inst_i1 = sp [1];
6906                         ins->flags |= ins_flag;
6907                         ins_flag = 0;
6908                         inline_costs += 1;
6909                         break;
6910                 case CEE_MUL:
6911                         CHECK_STACK (2);
6912                         ADD_BINOP (*ip);
6913
6914 #ifdef MONO_ARCH_NO_EMULATE_MUL_IMM
6915                         /* FIXME: This breaks with ssapre (mono -O=ssapre loader.exe) */
6916                         if ((ins->inst_right->opcode == OP_ICONST) && !(cfg->opt & MONO_OPT_SSAPRE)) {
6917                                 switch (ins->opcode) {
6918                                 case CEE_MUL:
6919                                         ins->opcode = OP_IMUL_IMM;
6920                                         ins->inst_imm = ins->inst_right->inst_c0;
6921                                         break;
6922                                 case OP_LMUL:
6923                                         ins->opcode = OP_LMUL_IMM;
6924                                         ins->inst_imm = ins->inst_right->inst_c0;
6925                                         break;
6926                                 default:
6927                                         g_assert_not_reached ();
6928                                 }
6929                         }
6930 #endif
6931
6932                         if (mono_find_jit_opcode_emulation (ins->opcode)) {
6933                                 --sp;
6934                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
6935                         }
6936                         ip++;
6937                         break;
6938                 case CEE_ADD:
6939                 case CEE_SUB:
6940                 case CEE_DIV:
6941                 case CEE_DIV_UN:
6942                 case CEE_REM:
6943                 case CEE_REM_UN:
6944                 case CEE_AND:
6945                 case CEE_OR:
6946                 case CEE_XOR:
6947                 case CEE_SHL:
6948                 case CEE_SHR:
6949                 case CEE_SHR_UN:
6950                         CHECK_STACK (2);
6951                         ADD_BINOP (*ip);
6952                         /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
6953                          * later apply the speedup to the left shift as well
6954                          * See BUG# 57957.
6955                          */
6956                         if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8) 
6957                                         && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
6958                                 ins->opcode = OP_LSHR_UN_32;
6959                                 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
6960                                 ip++;
6961                                 break;
6962                         }
6963                         if (mono_find_jit_opcode_emulation (ins->opcode)) {
6964                                 --sp;
6965                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
6966                         }
6967                         ip++;
6968                         break;
6969                 case CEE_NEG:
6970                 case CEE_NOT:
6971                 case CEE_CONV_I1:
6972                 case CEE_CONV_I2:
6973                 case CEE_CONV_I4:
6974                 case CEE_CONV_R4:
6975                 case CEE_CONV_R8:
6976                 case CEE_CONV_U4:
6977                 case CEE_CONV_I8:
6978                 case CEE_CONV_U8:
6979                 case CEE_CONV_OVF_I8:
6980                 case CEE_CONV_OVF_U8:
6981                 case CEE_CONV_R_UN:
6982                         CHECK_STACK (1);
6983                         ADD_UNOP (*ip);
6984
6985 #ifdef MONO_ARCH_SOFT_FLOAT
6986                         /*
6987                          * Its rather hard to emit the soft float code during the decompose
6988                          * pass, so avoid it in some specific cases.
6989                          */
6990                         if (ins->opcode == OP_LCONV_TO_R4) {
6991                                 MonoInst *conv;
6992
6993                                 ins->opcode = OP_LCONV_TO_R8;
6994                                 ins->type = STACK_R8;
6995
6996                                 --sp;
6997                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
6998
6999                                 MONO_INST_NEW (cfg, conv, CEE_CONV_R4);
7000                                 conv->inst_left = sp [-1];
7001                                 conv->type = STACK_R8;
7002                                 sp [-1] = ins;
7003
7004                                 ip++;
7005                                 break;
7006                         }
7007 #endif
7008
7009                         if (mono_find_jit_opcode_emulation (ins->opcode)) {
7010                                 --sp;
7011                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
7012                         }
7013                         ip++;                   
7014                         break;
7015                 case CEE_CONV_OVF_I4:
7016                 case CEE_CONV_OVF_I1:
7017                 case CEE_CONV_OVF_I2:
7018                 case CEE_CONV_OVF_I:
7019                 case CEE_CONV_OVF_U:
7020                         CHECK_STACK (1);
7021
7022                         if (sp [-1]->type == STACK_R8) {
7023                                 ADD_UNOP (CEE_CONV_OVF_I8);
7024                                 ADD_UNOP (*ip);
7025                         } else {
7026                                 ADD_UNOP (*ip);
7027                         }
7028
7029                         ip++;
7030                         break;
7031                 case CEE_CONV_OVF_U1:
7032                 case CEE_CONV_OVF_U2:
7033                 case CEE_CONV_OVF_U4:
7034                         CHECK_STACK (1);
7035
7036                         if (sp [-1]->type == STACK_R8) {
7037                                 ADD_UNOP (CEE_CONV_OVF_U8);
7038                                 ADD_UNOP (*ip);
7039                         } else {
7040                                 ADD_UNOP (*ip);
7041                         }
7042
7043                         ip++;
7044                         break;
7045                 case CEE_CONV_OVF_I1_UN:
7046                 case CEE_CONV_OVF_I2_UN:
7047                 case CEE_CONV_OVF_I4_UN:
7048                 case CEE_CONV_OVF_I8_UN:
7049                 case CEE_CONV_OVF_U1_UN:
7050                 case CEE_CONV_OVF_U2_UN:
7051                 case CEE_CONV_OVF_U4_UN:
7052                 case CEE_CONV_OVF_U8_UN:
7053                 case CEE_CONV_OVF_I_UN:
7054                 case CEE_CONV_OVF_U_UN:
7055                         CHECK_STACK (1);
7056                         ADD_UNOP (*ip);
7057                         ip++;
7058                         break;
7059                 case CEE_CPOBJ:
7060                         CHECK_OPSIZE (5);
7061                         CHECK_STACK (2);
7062                         token = read32 (ip + 1);
7063                         klass = mini_get_class (method, token, generic_context);
7064                         CHECK_TYPELOAD (klass);
7065                         sp -= 2;
7066                         if (generic_class_is_reference_type (cfg, klass)) {
7067                                 MonoInst *store, *load;
7068                                 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
7069                                 load->inst_i0 = sp [1];
7070                                 load->type = STACK_OBJ;
7071                                 load->klass = klass;
7072                                 load->flags |= ins_flag;
7073                                 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
7074                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
7075                                 MONO_ADD_INS (bblock, store);
7076                                 store->inst_i0 = sp [0];
7077                                 store->inst_i1 = load;
7078                                 store->flags |= ins_flag;
7079                         } else {
7080                                 guint32 align;
7081
7082                                 n = mono_class_value_size (klass, &align);
7083                                 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
7084                                         MonoInst *copy;
7085                                         NEW_MEMCPY (cfg, copy, sp [0], sp [1], n, align);
7086                                         MONO_ADD_INS (bblock, copy);
7087                                 } else {
7088                                         MonoMethod *memcpy_method = get_memcpy_method ();
7089                                         MonoInst *iargs [3];
7090                                         iargs [0] = sp [0];
7091                                         iargs [1] = sp [1];
7092                                         NEW_ICONST (cfg, iargs [2], n);
7093
7094                                         mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7095                                 }
7096                         }
7097                         ins_flag = 0;
7098                         ip += 5;
7099                         break;
7100                 case CEE_LDOBJ: {
7101                         MonoInst *iargs [3];
7102                         int loc_index = -1;
7103                         int stloc_len = 0;
7104                         guint32 align;
7105
7106                         CHECK_OPSIZE (5);
7107                         CHECK_STACK (1);
7108                         --sp;
7109                         token = read32 (ip + 1);
7110                         klass = mini_get_class (method, token, generic_context);
7111                         CHECK_TYPELOAD (klass);
7112                         if (generic_class_is_reference_type (cfg, klass)) {
7113                                 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
7114                                 ins->inst_i0 = sp [0];
7115                                 ins->type = STACK_OBJ;
7116                                 ins->klass = klass;
7117                                 ins->flags |= ins_flag;
7118                                 ins_flag = 0;
7119                                 *sp++ = ins;
7120                                 ip += 5;
7121                                 break;
7122                         }
7123
7124                         /* Optimize the common ldobj+stloc combination */
7125                         switch (ip [5]) {
7126                         case CEE_STLOC_S:
7127                                 loc_index = ip [6];
7128                                 stloc_len = 2;
7129                                 break;
7130                         case CEE_STLOC_0:
7131                         case CEE_STLOC_1:
7132                         case CEE_STLOC_2:
7133                         case CEE_STLOC_3:
7134                                 loc_index = ip [5] - CEE_STLOC_0;
7135                                 stloc_len = 1;
7136                                 break;
7137                         default:
7138                                 break;
7139                         }
7140
7141                         if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
7142                                 CHECK_LOCAL (loc_index);
7143                                 NEW_LOCSTORE (cfg, ins, loc_index, *sp);
7144
7145                                 /* FIXME: handle CEE_STIND_R4 */
7146                                 if (ins->opcode == CEE_STOBJ) {
7147                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
7148                                         g_assert (ins->opcode == CEE_STOBJ);
7149                                         NEW_LOCLOADA (cfg, ins, loc_index);
7150                                         handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
7151                                         ip += 5;
7152                                         ip += stloc_len;
7153                                         break;
7154                                 }
7155                         }
7156
7157                         n = mono_class_value_size (klass, &align);
7158                         ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
7159                         NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
7160                         if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
7161                                 MonoInst *copy;
7162                                 NEW_MEMCPY (cfg, copy, iargs [0], *sp, n, align);
7163                                 MONO_ADD_INS (bblock, copy);
7164                         } else {
7165                                 MonoMethod *memcpy_method = get_memcpy_method ();
7166                                 iargs [1] = *sp;
7167                                 NEW_ICONST (cfg, iargs [2], n);
7168
7169                                 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7170                         }
7171                         NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
7172                         ++sp;
7173                         ip += 5;
7174                         ins_flag = 0;
7175                         inline_costs += 1;
7176                         break;
7177                 }
7178                 case CEE_LDSTR:
7179                         CHECK_STACK_OVF (1);
7180                         CHECK_OPSIZE (5);
7181                         n = read32 (ip + 1);
7182
7183                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
7184                                 /* FIXME: moving GC */
7185                                 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
7186                                 ins->type = STACK_OBJ;
7187                                 ins->klass = mono_defaults.string_class;
7188                                 *sp = ins;
7189                         }
7190                         else if (method->wrapper_type != MONO_WRAPPER_NONE) {
7191                                 int temp;
7192                                 MonoInst *iargs [1];
7193
7194                                 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));                          
7195                                 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
7196                                 NEW_TEMPLOAD (cfg, *sp, temp);
7197
7198                         } else {
7199
7200                                 if (cfg->opt & MONO_OPT_SHARED) {
7201                                         int temp;
7202                                         MonoInst *iargs [3];
7203                                         MonoInst* domain_var;
7204                                         
7205                                         if (cfg->compile_aot) {
7206                                                 /* FIXME: bug when inlining methods from different assemblies (n is a token valid just in one). */
7207                                                 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
7208                                         }
7209                                         /* avoid depending on undefined C behavior in sequence points */
7210                                         domain_var = mono_get_domainvar (cfg);
7211                                         NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
7212                                         NEW_IMAGECONST (cfg, iargs [1], image);
7213                                         NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
7214                                         temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
7215                                         NEW_TEMPLOAD (cfg, *sp, temp);
7216                                         mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
7217                                 } else {
7218                                         if (bblock->out_of_line) {
7219                                                 MonoInst *iargs [2];
7220                                                 int temp;
7221
7222                                                 if (cfg->method->klass->image == mono_defaults.corlib) {
7223                                                         /* 
7224                                                          * Avoid relocations and save some code size by using a 
7225                                                          * version of helper_ldstr specialized to mscorlib.
7226                                                          */
7227                                                         NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
7228                                                         temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr_mscorlib, iargs, ip);
7229                                                 } else {
7230                                                         /* Avoid creating the string object */
7231                                                         NEW_IMAGECONST (cfg, iargs [0], image);
7232                                                         NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
7233                                                         temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr, iargs, ip);
7234                                                 }
7235                                                 NEW_TEMPLOAD (cfg, *sp, temp);
7236                                         } 
7237                                         else
7238                                         if (cfg->compile_aot) {
7239                                                 NEW_LDSTRCONST (cfg, ins, image, n);
7240                                                 *sp = ins;
7241                                         } 
7242                                         else {
7243                                                 NEW_PCONST (cfg, ins, NULL);
7244                                                 ins->type = STACK_OBJ;
7245                                                 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
7246                                                 ins->klass = mono_defaults.string_class;
7247                                                 *sp = ins;
7248                                         }
7249                                 }
7250                         }
7251
7252                         sp++;
7253                         ip += 5;
7254                         break;
7255                 case CEE_NEWOBJ: {
7256                         MonoInst *iargs [2];
7257                         MonoMethodSignature *fsig;
7258                         MonoInst this_ins;
7259                         int temp;
7260                         MonoInst *vtable_arg = NULL;
7261
7262                         CHECK_OPSIZE (5);
7263                         token = read32 (ip + 1);
7264                         cmethod = mini_get_method (cfg, method, token, NULL, generic_context);
7265                         if (!cmethod)
7266                                 goto load_error;
7267                         fsig = mono_method_get_signature (cmethod, image, token);
7268
7269                         mono_save_token_info (cfg, image, token, cmethod);
7270
7271                         if (!mono_class_init (cmethod->klass))
7272                                 goto load_error;
7273
7274                         if (cfg->generic_sharing_context)
7275                                 context_used = mono_method_check_context_used (cmethod);
7276
7277                         if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
7278                                 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
7279                                         INLINE_FAILURE;
7280                                 CHECK_CFG_EXCEPTION;
7281                         } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
7282                                 ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
7283                         }
7284
7285                         if (cmethod->klass->valuetype && mono_class_generic_sharing_enabled (cmethod->klass) &&
7286                                         mono_method_is_generic_sharable_impl (cmethod, TRUE)) {
7287                                 if (cmethod->is_inflated && mono_method_get_context (cmethod)->method_inst) {
7288                                         if (context_used) {
7289                                                 MonoInst *rgctx;
7290
7291                                                 GET_RGCTX (rgctx, context_used);
7292                                                 vtable_arg = get_runtime_generic_context_method_rgctx (cfg, method,
7293                                                         context_used, bblock, cmethod, generic_context, rgctx, ip);
7294                                         } else {
7295                                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7296                                                 MonoMethodRuntimeGenericContext *mrgctx;
7297
7298                                                 mrgctx = mono_method_lookup_rgctx (vtable,
7299                                                         mini_method_get_context (cmethod)->method_inst);
7300
7301                                                 NEW_PCONST (cfg, vtable_arg, mrgctx);
7302                                         }
7303                                 } else {
7304                                         if (context_used) {
7305                                                 MonoInst *rgctx;
7306
7307                                                 GET_RGCTX (rgctx, context_used);
7308                                                 vtable_arg = get_runtime_generic_context_ptr (cfg, method, context_used,
7309                                                         bblock, cmethod->klass, generic_context,
7310                                                         rgctx, MONO_RGCTX_INFO_VTABLE, ip);
7311                                         } else {
7312                                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7313
7314                                                 CHECK_TYPELOAD (cmethod->klass);
7315                                                 NEW_VTABLECONST (cfg, vtable_arg, vtable);
7316                                         }
7317                                 }
7318                         }
7319
7320                         n = fsig->param_count;
7321                         CHECK_STACK (n);
7322  
7323                         /* 
7324                          * Generate smaller code for the common newobj <exception> instruction in
7325                          * argument checking code.
7326                          */
7327                         if (bblock->out_of_line && cmethod->klass->image == mono_defaults.corlib && n <= 2 && 
7328                                 ((n < 1) || (!fsig->params [0]->byref && fsig->params [0]->type == MONO_TYPE_STRING)) && 
7329                                 ((n < 2) || (!fsig->params [1]->byref && fsig->params [1]->type == MONO_TYPE_STRING))) {
7330                                 MonoInst *iargs [3];
7331                                 int temp;
7332
7333                                 g_assert (!vtable_arg);
7334
7335                                 sp -= n;
7336
7337                                 NEW_ICONST (cfg, iargs [0], cmethod->klass->type_token);
7338                                 switch (n) {
7339                                 case 0:
7340                                         temp = mono_emit_jit_icall (cfg, bblock, mono_create_corlib_exception_0, iargs, ip);
7341                                         break;
7342                                 case 1:
7343                                         iargs [1] = sp [0];
7344                                         temp = mono_emit_jit_icall (cfg, bblock, mono_create_corlib_exception_1, iargs, ip);
7345                                         break;
7346                                 case 2:
7347                                         iargs [1] = sp [0];
7348                                         iargs [2] = sp [1];
7349                                         temp = mono_emit_jit_icall (cfg, bblock, mono_create_corlib_exception_2, iargs, ip);
7350                                         break;
7351                                 default:
7352                                         g_assert_not_reached ();
7353                                 }
7354                                 NEW_TEMPLOAD (cfg, ins, temp);
7355                                 *sp ++ = ins;
7356
7357                                 ip += 5;
7358                                 inline_costs += 5;
7359                                 break;
7360                         }
7361
7362                         /* move the args to allow room for 'this' in the first position */
7363                         while (n--) {
7364                                 --sp;
7365                                 sp [1] = sp [0];
7366                         }
7367
7368                         /* check_call_signature () requires sp[0] to be set */
7369                         this_ins.type = STACK_OBJ;
7370                         sp [0] = &this_ins;
7371                         if (check_call_signature (cfg, fsig, sp))
7372                                 UNVERIFIED;
7373
7374                         handle_loaded_temps (cfg, bblock, stack_start, sp);
7375
7376                         if (mini_class_is_system_array (cmethod->klass)) {
7377                                 g_assert (!context_used);
7378                                 g_assert (!vtable_arg);
7379
7380                                 NEW_METHODCONST (cfg, *sp, cmethod);
7381
7382                                 if (fsig->param_count == 2)
7383                                         /* Avoid varargs in the common case */
7384                                         temp = mono_emit_jit_icall (cfg, bblock, mono_array_new_2, sp, ip);
7385                                 else
7386                                         temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
7387                         } else if (cmethod->string_ctor) {
7388                                 g_assert (!context_used);
7389                                 g_assert (!vtable_arg);
7390
7391                                 /* we simply pass a null pointer */
7392                                 NEW_PCONST (cfg, *sp, NULL); 
7393                                 /* now call the string ctor */
7394                                 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
7395                         } else {
7396                                 MonoInst* callvirt_this_arg = NULL;
7397                                 
7398                                 if (cmethod->klass->valuetype) {
7399                                         iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
7400                                         temp = iargs [0]->inst_c0;
7401
7402                                         NEW_TEMPLOADA (cfg, *sp, temp);
7403
7404                                         handle_initobj (cfg, bblock, *sp, NULL, cmethod->klass, stack_start, sp);
7405
7406                                         NEW_TEMPLOADA (cfg, *sp, temp);
7407
7408                                         /* 
7409                                          * The code generated by mini_emit_virtual_call () expects
7410                                          * iargs [0] to be a boxed instance, but luckily the vcall
7411                                          * will be transformed into a normal call there.
7412                                          */
7413                                 } else if (context_used) {
7414                                         MonoInst *rgctx, *data;
7415                                         int rgctx_info;
7416
7417                                         GET_RGCTX (rgctx, context_used);
7418                                         if (cfg->opt & MONO_OPT_SHARED)
7419                                                 rgctx_info = MONO_RGCTX_INFO_KLASS;
7420                                         else
7421                                                 rgctx_info = MONO_RGCTX_INFO_VTABLE;
7422                                         data = get_runtime_generic_context_ptr (cfg, method, context_used, bblock,
7423                                                 cmethod->klass, generic_context, rgctx, rgctx_info, ip);
7424
7425                                         temp = handle_alloc_from_inst (cfg, bblock, cmethod->klass, data, FALSE, ip);
7426                                         NEW_TEMPLOAD (cfg, *sp, temp);
7427                                 } else {
7428                                         MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
7429
7430                                         CHECK_TYPELOAD (cmethod->klass);
7431                                         if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
7432                                                 guint8 *tramp = mono_create_class_init_trampoline (vtable);
7433                                                 mono_emit_native_call (cfg, bblock, tramp, 
7434                                                                                            helper_sig_class_init_trampoline,
7435                                                                                            NULL, ip, FALSE, FALSE);
7436                                                 if (cfg->verbose_level > 2)
7437                                                         g_print ("class %s.%s needs init call for ctor\n", cmethod->klass->name_space, cmethod->klass->name);
7438                                                 class_inits = g_slist_prepend (class_inits, vtable);
7439                                         }
7440                                         temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
7441                                         NEW_TEMPLOAD (cfg, *sp, temp);
7442                                 }
7443
7444                                 /* Avoid virtual calls to ctors if possible */
7445                                 if (cmethod->klass->marshalbyref)
7446                                         callvirt_this_arg = sp [0];
7447                                 
7448                                 if ((cfg->opt & MONO_OPT_INLINE) && cmethod && !context_used && !vtable_arg &&
7449                                     mono_method_check_inlining (cfg, cmethod) &&
7450                                     !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
7451                                     !g_list_find (dont_inline, cmethod)) {
7452                                         int costs;
7453                                         MonoBasicBlock *ebblock;
7454                                         if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
7455
7456                                                 ip += 5;
7457                                                 real_offset += 5;
7458                                                 
7459                                                 GET_BBLOCK (cfg, bblock, ip);
7460                                                 ebblock->next_bb = bblock;
7461                                                 link_bblock (cfg, ebblock, bblock);
7462
7463                                                 NEW_TEMPLOAD (cfg, *sp, temp);
7464                                                 sp++;
7465
7466                                                 /* indicates start of a new block, and triggers a load 
7467                                                    of all stack arguments at bb boundarie */
7468                                                 bblock = ebblock;
7469
7470                                                 inline_costs += costs;
7471                                                 break;
7472                                                 
7473                                         } else {
7474                                                 /* Prevent inlining of methods which call other methods */
7475                                                 INLINE_FAILURE;
7476                                                 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
7477                                         }
7478                                 } else if (context_used &&
7479                                                 (!mono_method_is_generic_sharable_impl (cmethod, TRUE) ||
7480                                                         !mono_class_generic_sharing_enabled (cmethod->klass))) {
7481                                         MonoInst *rgctx, *cmethod_addr;
7482
7483                                         g_assert (!callvirt_this_arg);
7484
7485                                         GET_RGCTX (rgctx, context_used);
7486                                         cmethod_addr = get_runtime_generic_context_method (cfg, method, context_used,
7487                                                         bblock, cmethod,
7488                                                         generic_context, rgctx, MONO_RGCTX_INFO_GENERIC_METHOD_CODE, ip);
7489
7490                                         mono_emit_rgctx_calli_spilled (cfg, bblock, fsig, sp, cmethod_addr, vtable_arg, ip);
7491                                 } else {
7492                                         /* Prevent inlining of methods which call other methods */
7493                                         INLINE_FAILURE;
7494                                         /* now call the actual ctor */
7495                                         mono_emit_rgctx_method_call_spilled (cfg, bblock, cmethod, fsig, sp,
7496                                                 vtable_arg, NULL, ip, callvirt_this_arg);
7497                                 }
7498                         }
7499
7500                         NEW_TEMPLOAD (cfg, *sp, temp);
7501                         sp++;
7502                         
7503                         ip += 5;
7504                         inline_costs += 5;
7505                         break;
7506                 }
7507                 case CEE_ISINST:
7508                         CHECK_STACK (1);
7509                         --sp;
7510                         CHECK_OPSIZE (5);
7511                         token = read32 (ip + 1);
7512                         klass = mini_get_class (method, token, generic_context);
7513                         CHECK_TYPELOAD (klass);
7514                         if (sp [0]->type != STACK_OBJ)
7515                                 UNVERIFIED;
7516
7517                         if (cfg->generic_sharing_context)
7518                                 context_used = mono_class_check_context_used (klass);
7519
7520                         /* Needed by the code generated in inssel.brg */
7521                         if (!context_used)
7522                                 mono_get_got_var (cfg);
7523
7524                         if (context_used) {
7525                                 MonoInst *rgctx, *args [2];
7526                                 int temp;
7527
7528                                 /* obj */
7529                                 args [0] = *sp;
7530
7531                                 /* klass */
7532                                 GET_RGCTX (rgctx, context_used);
7533                                 args [1] = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
7534                                         generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
7535
7536                                 temp = mono_emit_jit_icall (cfg, bblock, mono_object_isinst, args, ip);
7537                                 NEW_TEMPLOAD (cfg, *sp, temp);
7538
7539                                 sp++;
7540                                 ip += 5;
7541                                 inline_costs += 2;
7542                         } else if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
7543                         
7544                                 MonoMethod *mono_isinst;
7545                                 MonoInst *iargs [1];
7546                                 MonoBasicBlock *ebblock;
7547                                 int costs;
7548                                 int temp;
7549                                 
7550                                 mono_isinst = mono_marshal_get_isinst (klass); 
7551                                 iargs [0] = sp [0];
7552                                 
7553                                 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), bblock, 
7554                                                        iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
7555                         
7556                                 g_assert (costs > 0);
7557                                 
7558                                 ip += 5;
7559                                 real_offset += 5;
7560                         
7561                                 GET_BBLOCK (cfg, bblock, ip);
7562                                 ebblock->next_bb = bblock;
7563                                 link_bblock (cfg, ebblock, bblock);
7564
7565                                 temp = iargs [0]->inst_i0->inst_c0;
7566                                 NEW_TEMPLOAD (cfg, *sp, temp);
7567                                 
7568                                 sp++;
7569                                 bblock = ebblock;
7570                                 inline_costs += costs;
7571                         } else {
7572                                 MONO_INST_NEW (cfg, ins, *ip);
7573                                 ins->type = STACK_OBJ;
7574                                 ins->inst_left = *sp;
7575                                 ins->inst_newa_class = klass;
7576                                 ins->klass = klass;
7577                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
7578                                 ip += 5;
7579                         }
7580                         break;
7581                 case CEE_UNBOX_ANY: {
7582                         MonoInst *iargs [3];
7583                         guint32 align;
7584
7585                         CHECK_STACK (1);
7586                         --sp;
7587                         CHECK_OPSIZE (5);
7588                         token = read32 (ip + 1);
7589                         klass = mini_get_class (method, token, generic_context);
7590                         CHECK_TYPELOAD (klass);
7591
7592                         if (cfg->generic_sharing_context)
7593                                 context_used = mono_class_check_context_used (klass);
7594
7595                         if (generic_class_is_reference_type (cfg, klass)) {
7596                                 switch (emit_castclass (klass, token, context_used, FALSE,
7597                                                 cfg, method, arg_array, param_types, dont_inline, end, header,
7598                                                 generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7599                                 case 0: break;
7600                                 case -1: goto unverified;
7601                                 case -2: goto exception_exit;
7602                                 default: g_assert_not_reached ();
7603                                 }
7604                                 break;
7605                         }
7606
7607                         if (mono_class_is_nullable (klass)) {
7608                                 int v;
7609                                 MonoInst *rgctx = NULL;
7610
7611                                 if (context_used)
7612                                         GET_RGCTX (rgctx, context_used);
7613
7614                                 v = handle_unbox_nullable (cfg, method, context_used, bblock, *sp, ip, klass,
7615                                         generic_context, rgctx);
7616                                 NEW_TEMPLOAD (cfg, *sp, v);
7617                                 sp ++;
7618                                 ip += 5;
7619                                 break;
7620                         }
7621
7622                         switch (emit_unbox (klass, token, context_used,
7623                                         cfg, method, arg_array, param_types, dont_inline, end, header,
7624                                         generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7625                         case 0: break;
7626                         case -1: goto unverified;
7627                         case -2: goto exception_exit;
7628                         default: g_assert_not_reached ();
7629                         }
7630                         ip += 5;
7631                         /* LDOBJ impl */
7632                         n = mono_class_value_size (klass, &align);
7633                         ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
7634                         NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
7635                         if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
7636                                 MonoInst *copy;
7637                                 NEW_MEMCPY (cfg, copy, iargs [0], *sp, n, align);
7638                                 MONO_ADD_INS (bblock, copy);
7639                         } else {
7640                                 MonoMethod *memcpy_method = get_memcpy_method ();
7641                                 iargs [1] = *sp;
7642                                 NEW_ICONST (cfg, iargs [2], n);
7643
7644                                 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7645                         }
7646                         NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
7647                         ++sp;
7648                         inline_costs += 2;
7649                         break;
7650                 }
7651                 case CEE_UNBOX:
7652                         CHECK_STACK (1);
7653                         --sp;
7654                         CHECK_OPSIZE (5);
7655                         token = read32 (ip + 1);
7656                         klass = mini_get_class (method, token, generic_context);
7657                         CHECK_TYPELOAD (klass);
7658
7659                         if (cfg->generic_sharing_context)
7660                                 context_used = mono_class_check_context_used (klass);
7661
7662                         if (mono_class_is_nullable (klass)) {
7663                                 int v;
7664                                 MonoInst *rgctx = NULL;
7665
7666                                 if (context_used)
7667                                         GET_RGCTX (rgctx, context_used);
7668                                 v = handle_unbox_nullable (cfg, method, context_used, bblock, *sp, ip, klass,
7669                                         generic_context, rgctx);
7670                                 NEW_TEMPLOAD (cfg, *sp, v);
7671                                 sp ++;
7672                                 ip += 5;
7673                                 break;
7674                         }
7675
7676                         switch (emit_unbox (klass, token, context_used,
7677                                         cfg, method, arg_array, param_types, dont_inline, end, header,
7678                                         generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7679                         case 0: break;
7680                         case -1: goto unverified;
7681                         case -2: goto exception_exit;
7682                         default: g_assert_not_reached ();
7683                         }
7684
7685                         sp++;
7686                         ip += 5;
7687                         inline_costs += 2;
7688                         break;
7689                 case CEE_CASTCLASS:
7690                         CHECK_STACK (1);
7691                         --sp;
7692                         CHECK_OPSIZE (5);
7693                         token = read32 (ip + 1);
7694                         klass = mini_get_class (method, token, generic_context);
7695                         CHECK_TYPELOAD (klass);
7696                         if (sp [0]->type != STACK_OBJ)
7697                                 UNVERIFIED;
7698
7699                         if (cfg->generic_sharing_context)
7700                                 context_used = mono_class_check_context_used (klass);
7701
7702                         switch (emit_castclass (klass, token, context_used, TRUE,
7703                                         cfg, method, arg_array, param_types, dont_inline, end, header,
7704                                         generic_context, &bblock, &ip, &sp, &inline_costs, &real_offset)) {
7705                         case 0: break;
7706                         case -1: goto unverified;
7707                         case -2: goto exception_exit;
7708                         default: g_assert_not_reached ();
7709                         }
7710                         break;
7711                 case CEE_THROW:
7712                         CHECK_STACK (1);
7713                         MONO_INST_NEW (cfg, ins, OP_THROW);
7714                         --sp;
7715                         ins->inst_left = *sp;
7716                         ip++;
7717                         bblock->out_of_line = TRUE;
7718                         MONO_ADD_INS (bblock, ins);
7719                         MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
7720                         ins->cil_code = ip - 1;
7721                         MONO_ADD_INS (bblock, ins);
7722                         sp = stack_start;
7723                         
7724                         link_bblock (cfg, bblock, end_bblock);
7725                         start_new_bblock = 1;
7726                         break;
7727                 case CEE_LDFLD:
7728                 case CEE_LDFLDA:
7729                 case CEE_STFLD: {
7730                         MonoInst *offset_ins;
7731                         MonoClassField *field;
7732                         MonoBasicBlock *ebblock;
7733                         int costs;
7734                         guint foffset;
7735
7736                         if (*ip == CEE_STFLD) {
7737                                 CHECK_STACK (2);
7738                                 sp -= 2;
7739                         } else {
7740                                 CHECK_STACK (1);
7741                                 --sp;
7742                         }
7743                         if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
7744                                 UNVERIFIED;
7745                         if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
7746                                 UNVERIFIED;
7747                         CHECK_OPSIZE (5);
7748                         token = read32 (ip + 1);
7749                         if (method->wrapper_type != MONO_WRAPPER_NONE) {
7750                                 field = mono_method_get_wrapper_data (method, token);
7751                                 klass = field->parent;
7752                         } else {
7753                                 field = mono_field_from_token (image, token, &klass, generic_context);
7754                         }
7755                         if (!field)
7756                                 goto load_error;
7757                         mono_class_init (klass);
7758                         if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
7759                                 FIELD_ACCESS_FAILURE;
7760
7761                         foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
7762                         /* FIXME: mark instructions for use in SSA */
7763                         if (*ip == CEE_STFLD) {
7764                                 if (target_type_is_incompatible (cfg, field->type, sp [1]))
7765                                         UNVERIFIED;
7766                                 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
7767                                         MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type); 
7768                                         MonoInst *iargs [5];
7769
7770                                         iargs [0] = sp [0];
7771                                         NEW_CLASSCONST (cfg, iargs [1], klass);
7772                                         NEW_FIELDCONST (cfg, iargs [2], field);
7773                                         NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : 
7774                                                     field->offset);
7775                                         iargs [4] = sp [1];
7776
7777                                         if (cfg->opt & MONO_OPT_INLINE) {
7778                                                 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), bblock, 
7779                                                                 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
7780                                                 g_assert (costs > 0);
7781                                                       
7782                                                 ip += 5;
7783                                                 real_offset += 5;
7784
7785                                                 GET_BBLOCK (cfg, bblock, ip);
7786                                                 ebblock->next_bb = bblock;
7787                                                 link_bblock (cfg, ebblock, bblock);
7788
7789                                                 /* indicates start of a new block, and triggers a load 
7790                                                    of all stack arguments at bb boundarie */
7791                                                 bblock = ebblock;
7792
7793                                                 inline_costs += costs;
7794                                                 break;
7795                                         } else {
7796                                                 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
7797                                         }
7798 #if HAVE_WRITE_BARRIERS
7799                                 } else if (mini_type_to_stind (cfg, field->type) == CEE_STIND_REF && !(sp [1]->opcode == OP_PCONST && sp [1]->inst_c0 == 0)) {
7800                                         /* insert call to write barrier */
7801                                         MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
7802                                         MonoInst *iargs [2];
7803                                         NEW_ICONST (cfg, offset_ins, foffset);
7804                                         MONO_INST_NEW (cfg, ins, OP_PADD);
7805                                         ins->inst_left = *sp;
7806                                         ins->inst_right = offset_ins;
7807                                         ins->type = STACK_MP;
7808                                         ins->klass = mono_defaults.object_class;
7809                                         iargs [0] = ins;
7810                                         iargs [1] = sp [1];
7811                                         mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), iargs, ip, NULL);
7812 #endif
7813 #ifdef MONO_ARCH_SOFT_FLOAT
7814                                 } else if (mini_type_to_stind (cfg, field->type) == CEE_STIND_R4) {
7815                                         NEW_ICONST (cfg, offset_ins, foffset);
7816                                         MONO_INST_NEW (cfg, ins, OP_PADD);
7817                                         ins->inst_left = *sp;
7818                                         ins->inst_right = offset_ins;
7819                                         ins->type = STACK_MP;
7820                                         ins->klass = mono_defaults.object_class;
7821                                         handle_store_float (cfg, bblock, ins, sp [1], ip);
7822 #endif
7823                                 } else {
7824                                         MonoInst *store;
7825                                         NEW_ICONST (cfg, offset_ins, foffset);
7826                                         MONO_INST_NEW (cfg, ins, OP_PADD);
7827                                         ins->inst_left = *sp;
7828                                         ins->inst_right = offset_ins;
7829                                         ins->type = STACK_MP;
7830
7831                                         MONO_INST_NEW (cfg, store, mini_type_to_stind (cfg, field->type));
7832                                         store->inst_left = ins;
7833                                         store->inst_right = sp [1];
7834                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
7835                                         store->flags |= ins_flag;
7836                                         ins_flag = 0;
7837                                         if (store->opcode == CEE_STOBJ) {
7838                                                 handle_stobj (cfg, bblock, ins, sp [1], ip, 
7839                                                               mono_class_from_mono_type (field->type), FALSE, FALSE, TRUE);
7840                                         } else
7841                                                 MONO_ADD_INS (bblock, store);
7842                                 }
7843                         } else {
7844                                 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
7845                                         MonoMethod *wrapper = (*ip == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type); 
7846                                         MonoInst *iargs [4];
7847                                         int temp;
7848                                         
7849                                         iargs [0] = sp [0];
7850                                         NEW_CLASSCONST (cfg, iargs [1], klass);
7851                                         NEW_FIELDCONST (cfg, iargs [2], field);
7852                                         NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
7853                                         if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (wrapper)->ret)) {
7854                                                 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), bblock, 
7855                                                                 iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
7856                                                 g_assert (costs > 0);
7857                                                       
7858                                                 ip += 5;
7859                                                 real_offset += 5;
7860
7861                                                 GET_BBLOCK (cfg, bblock, ip);
7862                                                 ebblock->next_bb = bblock;
7863                                                 link_bblock (cfg, ebblock, bblock);
7864
7865                                                 temp = iargs [0]->inst_i0->inst_c0;
7866
7867                                                 NEW_TEMPLOAD (cfg, *sp, temp);
7868                                                 sp++;
7869
7870                                                 /* indicates start of a new block, and triggers a load of
7871                                                    all stack arguments at bb boundarie */
7872                                                 bblock = ebblock;
7873                                                 
7874                                                 inline_costs += costs;
7875                                                 break;
7876                                         } else {
7877                                                 temp = mono_emit_method_call_spilled (cfg, bblock, wrapper, mono_method_signature (wrapper), iargs, ip, NULL);
7878                                                 NEW_TEMPLOAD (cfg, *sp, temp);
7879                                                 NEW_TEMPLOAD_SOFT_FLOAT (cfg, bblock, *sp, temp, ip);
7880                                                 sp++;
7881                                         }
7882                                 } else {
7883                                         NEW_ICONST (cfg, offset_ins, foffset);
7884                                         MONO_INST_NEW (cfg, ins, OP_PADD);
7885                                         ins->inst_left = *sp;
7886                                         ins->inst_right = offset_ins;
7887                                         ins->type = STACK_MP;
7888
7889                                         if (*ip == CEE_LDFLDA) {
7890                                                 ins->klass = mono_class_from_mono_type (field->type);
7891                                                 *sp++ = ins;
7892                                         } else {
7893                                                 MonoInst *load;
7894                                                 MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, field->type));
7895                                                 type_to_eval_stack_type (cfg, field->type, load);
7896                                                 load->inst_left = ins;
7897                                                 load->flags |= ins_flag;
7898                                                 ins_flag = 0;
7899 #ifdef MONO_ARCH_SOFT_FLOAT
7900                                                 if (mini_type_to_ldind (cfg, field->type) == CEE_LDIND_R4) {
7901                                                         int temp;
7902                                                         temp = handle_load_float (cfg, bblock, ins, ip);
7903                                                         NEW_TEMPLOAD (cfg, *sp, temp);
7904                                                         sp++;
7905                                                 } else
7906 #endif
7907                                                 *sp++ = load;
7908                                         }
7909                                 }
7910                         }
7911                         ip += 5;
7912                         break;
7913                 }
7914                 case CEE_LDSFLD:
7915                 case CEE_LDSFLDA:
7916                 case CEE_STSFLD: {
7917                         MonoClassField *field;
7918                         gboolean is_special_static;
7919                         gpointer addr = NULL;
7920
7921                         CHECK_OPSIZE (5);
7922                         token = read32 (ip + 1);
7923                         if (method->wrapper_type != MONO_WRAPPER_NONE) {
7924                                 field = mono_method_get_wrapper_data (method, token);
7925                                 klass = field->parent;
7926                         }
7927                         else
7928                                 field = mono_field_from_token (image, token, &klass, generic_context);
7929                         if (!field)
7930                                 goto load_error;
7931                         mono_class_init (klass);
7932                         if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
7933                                 FIELD_ACCESS_FAILURE;
7934
7935                         /*
7936                          * We can only support shared generic static
7937                          * field access on architectures where the
7938                          * trampoline code has been extended to handle
7939                          * the generic class init.
7940                          */
7941 #ifndef MONO_ARCH_VTABLE_REG
7942                         GENERIC_SHARING_FAILURE (*ip);
7943 #endif
7944
7945                         if (cfg->generic_sharing_context)
7946                                 context_used = mono_class_check_context_used (klass);
7947
7948                         g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
7949
7950                         if ((*ip) == CEE_STSFLD)
7951                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
7952
7953                         is_special_static = mono_class_field_is_special_static (field);
7954
7955                         if ((cfg->opt & MONO_OPT_SHARED) ||
7956                                         (cfg->compile_aot && is_special_static) ||
7957                                         (context_used && is_special_static)) {
7958                                 int temp;
7959                                 MonoInst *iargs [2];
7960
7961                                 g_assert (field->parent);
7962                                 if ((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) {
7963                                         MonoInst *domain_var;
7964                                         /* avoid depending on undefined C behavior in sequence points */
7965                                         domain_var = mono_get_domainvar (cfg);
7966                                         NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
7967                                 } else {
7968                                         NEW_DOMAINCONST (cfg, iargs [0]);
7969                                 }
7970                                 if (context_used) {
7971                                         MonoInst *rgctx;
7972
7973                                         GET_RGCTX (rgctx, context_used);
7974                                         iargs [1] = get_runtime_generic_context_field (cfg, method, context_used,
7975                                                         bblock, field,
7976                                                         generic_context, rgctx, MONO_RGCTX_INFO_CLASS_FIELD, ip);
7977                                 } else {
7978                                         NEW_FIELDCONST (cfg, iargs [1], field);
7979                                 }
7980                                 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
7981                                 NEW_TEMPLOAD (cfg, ins, temp);
7982                         } else if (context_used) {
7983                                 MonoInst *rgctx, *static_data;
7984
7985                                 /*
7986                                 g_print ("sharing static field access in %s.%s.%s - depth %d offset %d\n",
7987                                         method->klass->name_space, method->klass->name, method->name,
7988                                         depth, field->offset);
7989                                 */
7990
7991                                 if (mono_class_needs_cctor_run (klass, method)) {
7992                                         MonoMethodSignature *sig = helper_sig_generic_class_init_trampoline;
7993                                         MonoCallInst *call;
7994                                         MonoInst *vtable, *rgctx;
7995
7996                                         GET_RGCTX (rgctx, context_used);
7997                                         vtable = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
7998                                                         generic_context, rgctx, MONO_RGCTX_INFO_VTABLE, ip);
7999
8000                                         call = mono_emit_call_args (cfg, bblock, sig, NULL, FALSE, FALSE, ip, FALSE);
8001                                         call->inst.opcode = OP_TRAMPCALL_VTABLE;
8002                                         call->fptr = mono_create_generic_class_init_trampoline ();
8003
8004                                         call->inst.inst_left = vtable;
8005
8006                                         mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
8007                                 }
8008
8009                                 /*
8010                                  * The pointer we're computing here is
8011                                  *
8012                                  *   super_info.static_data + field->offset
8013                                  */
8014                                 GET_RGCTX (rgctx, context_used);
8015                                 static_data = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8016                                         generic_context, rgctx, MONO_RGCTX_INFO_STATIC_DATA, ip);
8017
8018                                 if (field->offset == 0) {
8019                                         ins = static_data;
8020                                 } else {
8021                                         MonoInst *field_offset;
8022
8023                                         NEW_ICONST (cfg, field_offset, field->offset);
8024
8025                                         MONO_INST_NEW (cfg, ins, OP_PADD);
8026                                         ins->inst_left = static_data;
8027                                         ins->inst_right = field_offset;
8028                                         ins->type = STACK_PTR;
8029                                         ins->klass = klass;
8030                                 }
8031                         } else {
8032                                 MonoVTable *vtable;
8033
8034                                 vtable = mono_class_vtable (cfg->domain, klass);
8035                                 CHECK_TYPELOAD (klass);
8036                                 if (!is_special_static) {
8037                                         if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
8038                                                 guint8 *tramp = mono_create_class_init_trampoline (vtable);
8039                                                 mono_emit_native_call (cfg, bblock, tramp, 
8040                                                                                            helper_sig_class_init_trampoline,
8041                                                                                            NULL, ip, FALSE, FALSE);
8042                                                 if (cfg->verbose_level > 2)
8043                                                         g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
8044                                                 class_inits = g_slist_prepend (class_inits, vtable);
8045                                         } else {
8046                                                 if (cfg->run_cctors) {
8047                                                         MonoException *ex;
8048                                                         /* This makes so that inline cannot trigger */
8049                                                         /* .cctors: too many apps depend on them */
8050                                                         /* running with a specific order... */
8051                                                         if (! vtable->initialized)
8052                                                                 INLINE_FAILURE;
8053                                                         ex = mono_runtime_class_init_full (vtable, FALSE);
8054                                                         if (ex) {
8055                                                                 set_exception_object (cfg, ex);
8056                                                                 goto exception_exit;
8057                                                         }                                       
8058                                                 }
8059                                         }
8060                                         addr = (char*)vtable->data + field->offset;
8061
8062                                         if (cfg->compile_aot)
8063                                                 NEW_SFLDACONST (cfg, ins, field);
8064                                         else
8065                                                 NEW_PCONST (cfg, ins, addr);
8066                                 } else {
8067                                         int temp;
8068                                         MonoInst *iargs [1];
8069
8070                                         /* The special_static_fields
8071                                          * field is init'd in
8072                                          * mono_class_vtable, so it
8073                                          * needs to be called here.
8074                                          */
8075                                         if (!(cfg->opt & MONO_OPT_SHARED)) {
8076                                                 mono_class_vtable (cfg->domain, klass);
8077                                                 CHECK_TYPELOAD (klass);
8078                                         }
8079                                         mono_domain_lock (cfg->domain);
8080                                         if (cfg->domain->special_static_fields)
8081                                                 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
8082                                         mono_domain_unlock (cfg->domain);
8083
8084                                         /* 
8085                                          * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr)) 
8086                                          * This could be later optimized to do just a couple of
8087                                          * memory dereferences with constant offsets.
8088                                          */
8089                                         NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
8090                                         temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
8091                                         NEW_TEMPLOAD (cfg, ins, temp);
8092                                 }
8093                         }
8094
8095                         /* FIXME: mark instructions for use in SSA */
8096                         if (*ip == CEE_LDSFLDA) {
8097                                 ins->klass = mono_class_from_mono_type (field->type);
8098                                 *sp++ = ins;
8099                         } else if (*ip == CEE_STSFLD) {
8100                                 MonoInst *store;
8101                                 CHECK_STACK (1);
8102                                 sp--;
8103                                 MONO_INST_NEW (cfg, store, mini_type_to_stind (cfg, field->type));
8104                                 store->inst_left = ins;
8105                                 store->inst_right = sp [0];
8106                                 store->flags |= ins_flag;
8107                                 ins_flag = 0;
8108
8109 #ifdef MONO_ARCH_SOFT_FLOAT
8110                                 if (store->opcode == CEE_STIND_R4)
8111                                         handle_store_float (cfg, bblock, ins, sp [0], ip);
8112                                 else
8113 #endif
8114                                 if (store->opcode == CEE_STOBJ) {
8115                                         handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE, FALSE);
8116                                 } else
8117                                         MONO_ADD_INS (bblock, store);
8118                         } else {
8119                                 gboolean is_const = FALSE;
8120                                 MonoVTable *vtable = NULL;
8121
8122                                 if (!context_used)
8123                                         vtable = mono_class_vtable (cfg->domain, klass);
8124
8125                                 CHECK_TYPELOAD (klass);
8126                                 if (!context_used && !((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && 
8127                                     vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
8128                                         gpointer addr = (char*)vtable->data + field->offset;
8129                                         int ro_type = field->type->type;
8130                                         if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
8131                                                 ro_type = field->type->data.klass->enum_basetype->type;
8132                                         }
8133                                         /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
8134                                         is_const = TRUE;
8135                                         switch (ro_type) {
8136                                         case MONO_TYPE_BOOLEAN:
8137                                         case MONO_TYPE_U1:
8138                                                 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
8139                                                 sp++;
8140                                                 break;
8141                                         case MONO_TYPE_I1:
8142                                                 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
8143                                                 sp++;
8144                                                 break;                                          
8145                                         case MONO_TYPE_CHAR:
8146                                         case MONO_TYPE_U2:
8147                                                 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
8148                                                 sp++;
8149                                                 break;
8150                                         case MONO_TYPE_I2:
8151                                                 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
8152                                                 sp++;
8153                                                 break;
8154                                                 break;
8155                                         case MONO_TYPE_I4:
8156                                                 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
8157                                                 sp++;
8158                                                 break;                                          
8159                                         case MONO_TYPE_U4:
8160                                                 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
8161                                                 sp++;
8162                                                 break;
8163 #ifndef HAVE_MOVING_COLLECTOR
8164                                         case MONO_TYPE_I:
8165                                         case MONO_TYPE_U:
8166                                         case MONO_TYPE_STRING:
8167                                         case MONO_TYPE_OBJECT:
8168                                         case MONO_TYPE_CLASS:
8169                                         case MONO_TYPE_SZARRAY:
8170                                         case MONO_TYPE_PTR:
8171                                         case MONO_TYPE_FNPTR:
8172                                         case MONO_TYPE_ARRAY:
8173                                                 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
8174                                                 type_to_eval_stack_type (cfg, field->type, *sp);
8175                                                 sp++;
8176                                                 break;
8177 #endif
8178                                         case MONO_TYPE_I8:
8179                                         case MONO_TYPE_U8:
8180                                                 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
8181                                                 sp [0]->type = STACK_I8;
8182                                                 sp [0]->inst_l = *((gint64 *)addr);
8183                                                 sp++;
8184                                                 break;
8185                                         case MONO_TYPE_R4:
8186                                         case MONO_TYPE_R8:
8187                                         case MONO_TYPE_VALUETYPE:
8188                                         default:
8189                                                 is_const = FALSE;
8190                                                 break;
8191                                         }
8192                                 }
8193
8194                                 if (!is_const) {
8195                                         MonoInst *load;
8196                                         CHECK_STACK_OVF (1);
8197                                         MONO_INST_NEW (cfg, load, mini_type_to_ldind (cfg, field->type));
8198                                         type_to_eval_stack_type (cfg, field->type, load);
8199                                         load->inst_left = ins;
8200                                         load->flags |= ins_flag;
8201 #ifdef MONO_ARCH_SOFT_FLOAT
8202                                         if (load->opcode == CEE_LDIND_R4) {
8203                                                 int temp;
8204                                                 temp = handle_load_float (cfg, bblock, ins, ip);
8205                                                 NEW_TEMPLOAD (cfg, load, temp);
8206                                         }
8207 #endif
8208                                         *sp++ = load;
8209                                         ins_flag = 0;
8210                                 }
8211                         }
8212                         ip += 5;
8213                         break;
8214                 }
8215                 case CEE_STOBJ:
8216                         CHECK_STACK (2);
8217                         sp -= 2;
8218                         CHECK_OPSIZE (5);
8219                         token = read32 (ip + 1);
8220                         klass = mini_get_class (method, token, generic_context);
8221                         CHECK_TYPELOAD (klass);
8222                         n = mini_type_to_stind (cfg, &klass->byval_arg);
8223                         /* FIXME: handle CEE_STIND_R4 */
8224                         if (n == CEE_STOBJ) {
8225                                 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE, TRUE);
8226                         } else {
8227                                 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
8228                                 MonoInst *store;
8229                                 MONO_INST_NEW (cfg, store, n);
8230                                 store->inst_left = sp [0];
8231                                 store->inst_right = sp [1];
8232                                 store->flags |= ins_flag;
8233                                 MONO_ADD_INS (bblock, store);
8234                         }
8235                         ins_flag = 0;
8236                         ip += 5;
8237                         inline_costs += 1;
8238                         break;
8239                 case CEE_BOX: {
8240                         MonoInst *val;
8241
8242                         CHECK_STACK (1);
8243                         --sp;
8244                         val = *sp;
8245                         CHECK_OPSIZE (5);
8246                         token = read32 (ip + 1);
8247                         klass = mini_get_class (method, token, generic_context);
8248                         CHECK_TYPELOAD (klass);
8249
8250                         if (cfg->generic_sharing_context)
8251                                 context_used =  mono_class_check_context_used (klass);
8252
8253                         if (generic_class_is_reference_type (cfg, klass)) {
8254                                 *sp++ = val;
8255                                 ip += 5;
8256                                 break;
8257                         }
8258                         if (klass == mono_defaults.void_class)
8259                                 UNVERIFIED;
8260                         if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
8261                                 UNVERIFIED;
8262                         /* frequent check in generic code: box (struct), brtrue */
8263                         if (!mono_class_is_nullable (klass) &&
8264                             ip + 5 < end && ip_in_bb (cfg, bblock, ip + 5) && (ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S)) {
8265                                 /*g_print ("box-brtrue opt at 0x%04x in %s\n", real_offset, method->name);*/
8266                                 MONO_INST_NEW (cfg, ins, CEE_POP);
8267                                 MONO_ADD_INS (bblock, ins);
8268                                 ins->inst_i0 = *sp;
8269                                 ip += 5;
8270                                 cfg->ip = ip;
8271                                 MONO_INST_NEW (cfg, ins, OP_BR);
8272                                 MONO_ADD_INS (bblock, ins);
8273                                 if (*ip == CEE_BRTRUE_S) {
8274                                         CHECK_OPSIZE (2);
8275                                         ip++;
8276                                         target = ip + 1 + (signed char)(*ip);
8277                                         ip++;
8278                                 } else {
8279                                         CHECK_OPSIZE (5);
8280                                         ip++;
8281                                         target = ip + 4 + (gint)(read32 (ip));
8282                                         ip += 4;
8283                                 }
8284                                 GET_BBLOCK (cfg, tblock, target);
8285                                 link_bblock (cfg, bblock, tblock);
8286                                 CHECK_BBLOCK (target, ip, tblock);
8287                                 ins->inst_target_bb = tblock;
8288                                 GET_BBLOCK (cfg, tblock, ip);
8289                                 link_bblock (cfg, bblock, tblock);
8290                                 if (sp != stack_start) {
8291                                         handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
8292                                         sp = stack_start;
8293                                         CHECK_UNVERIFIABLE (cfg);
8294                                 }
8295                                 start_new_bblock = 1;
8296                                 break;
8297                         }
8298                         if (context_used) {
8299                                 MonoInst *rgctx;
8300
8301                                 if (mono_class_is_nullable (klass)) {
8302                                         GET_RGCTX (rgctx, context_used);
8303                                         *sp++ = handle_box_nullable_from_inst (cfg, method, context_used, bblock, val,
8304                                                         ip, klass, generic_context, rgctx);
8305                                 } else {
8306                                         MonoInst *data;
8307                                         int rgctx_info;
8308
8309                                         GET_RGCTX (rgctx, context_used);
8310                                         if (cfg->opt & MONO_OPT_SHARED)
8311                                                 rgctx_info = MONO_RGCTX_INFO_KLASS;
8312                                         else
8313                                                 rgctx_info = MONO_RGCTX_INFO_VTABLE;
8314                                         data = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8315                                                         generic_context, rgctx, rgctx_info, ip);
8316
8317                                         *sp++ = handle_box_from_inst (cfg, bblock, val, ip, klass, data);
8318                                 }
8319                         } else {
8320                                 *sp++ = handle_box (cfg, bblock, val, ip, klass);
8321                         }
8322                         ip += 5;
8323                         inline_costs += 1;
8324                         break;
8325                 }
8326                 case CEE_NEWARR:
8327                         CHECK_STACK (1);
8328                         --sp;
8329
8330                         CHECK_OPSIZE (5);
8331                         token = read32 (ip + 1);
8332
8333                         /* allocate the domainvar - becaus this is used in decompose_foreach */
8334                         if (cfg->opt & MONO_OPT_SHARED) {
8335                                 mono_get_domainvar (cfg);
8336                                 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
8337                                 cfg->domainvar->flags |= MONO_INST_VOLATILE;
8338                         }
8339
8340                         /* Ditto */
8341                         mono_get_got_var (cfg);
8342
8343                         klass = mini_get_class (method, token, generic_context);
8344                         CHECK_TYPELOAD (klass);
8345
8346                         if (cfg->generic_sharing_context)
8347                                 context_used = mono_class_check_context_used (klass);
8348
8349                         if (context_used) {
8350                                 MonoInst *rgctx, *args [3];
8351                                 int temp;
8352
8353                                 /* domain */
8354                                 /* FIXME: what about domain-neutral code? */
8355                                 NEW_DOMAINCONST (cfg, args [0]);
8356
8357                                 /* klass */
8358                                 GET_RGCTX (rgctx, context_used);
8359                                 args [1] = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8360                                         generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
8361
8362                                 /* array len */
8363                                 args [2] = *sp;
8364
8365                                 temp = mono_emit_jit_icall (cfg, bblock, mono_array_new, args, ip);
8366                                 NEW_TEMPLOAD (cfg, ins, temp);
8367                         } else {
8368                                 MONO_INST_NEW (cfg, ins, *ip);
8369                                 ins->inst_newa_class = klass;
8370                                 ins->inst_newa_len = *sp;
8371                                 ins->type = STACK_OBJ;
8372                                 ins->klass = mono_array_class_get (klass, 1);
8373                         }
8374
8375                         ip += 5;
8376                         *sp++ = ins;
8377                         /* 
8378                          * we store the object so calls to create the array are not interleaved
8379                          * with the arguments of other calls.
8380                          */
8381                         if (1) {
8382                                 MonoInst *store, *temp, *load;
8383                                 const char *data_ptr;
8384                                 int data_size = 0;
8385                                 --sp;
8386                                 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
8387                                 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8388                                 store->cil_code = ins->cil_code;
8389                                 MONO_ADD_INS (bblock, store);
8390                                 /* 
8391                                  * we inline/optimize the initialization sequence if possible.
8392                                  * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
8393                                  * for small sizes open code the memcpy
8394                                  * ensure the rva field is big enough
8395                                  */
8396                                 if ((cfg->opt & MONO_OPT_INTRINS) && ip + 6 < end && ip_in_bb (cfg, bblock, ip + 6) && (data_ptr = initialize_array_data (method, cfg->compile_aot, ip, ins, &data_size))) {
8397                                         MonoMethod *memcpy_method = get_memcpy_method ();
8398                                         MonoInst *data_offset, *add;
8399                                         MonoInst *iargs [3];
8400                                         NEW_ICONST (cfg, iargs [2], data_size);
8401                                         NEW_TEMPLOAD (cfg, load, temp->inst_c0);
8402                                         load->cil_code = ins->cil_code;
8403                                         NEW_ICONST (cfg, data_offset, G_STRUCT_OFFSET (MonoArray, vector));
8404                                         MONO_INST_NEW (cfg, add, OP_PADD);
8405                                         add->inst_left = load;
8406                                         add->inst_right = data_offset;
8407                                         iargs [0] = add;
8408                                         if (cfg->compile_aot) {
8409                                                 NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(data_ptr), STACK_PTR, NULL);
8410                                         } else {
8411                                                 NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
8412                                         }
8413                                         mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
8414                                         ip += 11;
8415                                 }
8416                                 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
8417                                 load->cil_code = ins->cil_code;
8418                                 *sp++ = load;
8419                         }
8420                         inline_costs += 1;
8421                         break;
8422                 case CEE_LDLEN:
8423                         CHECK_STACK (1);
8424                         --sp;
8425                         if (sp [0]->type != STACK_OBJ)
8426                                 UNVERIFIED;
8427                         MONO_INST_NEW (cfg, ins, *ip);
8428                         ip++;
8429                         ins->inst_left = *sp;
8430                         ins->type = STACK_PTR;
8431                         *sp++ = ins;
8432                         break;
8433                 case CEE_LDELEMA:
8434                         CHECK_STACK (2);
8435                         sp -= 2;
8436                         CHECK_OPSIZE (5);
8437                         if (sp [0]->type != STACK_OBJ)
8438                                 UNVERIFIED;
8439
8440                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
8441                         CHECK_TYPELOAD (klass);
8442                         /* we need to make sure that this array is exactly the type it needs
8443                          * to be for correctness. the wrappers are lax with their usage
8444                          * so we need to ignore them here
8445                          */
8446                         if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE && !readonly) {
8447                                 MonoInst* check;
8448
8449                                 /* Needed by the code generated in inssel.brg */
8450                                 mono_get_got_var (cfg);
8451
8452                                 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
8453                                 check->klass = mono_array_class_get (klass, 1);
8454                                 check->inst_left = sp [0];
8455                                 check->type = STACK_OBJ;
8456                                 sp [0] = check;
8457                         }
8458                         
8459                         readonly = FALSE;
8460                         mono_class_init (klass);
8461                         NEW_LDELEMA (cfg, ins, sp, klass);
8462                         *sp++ = ins;
8463                         ip += 5;
8464                         break;
8465                 case CEE_LDELEM_ANY: {
8466                         MonoInst *load;
8467                         CHECK_STACK (2);
8468                         sp -= 2;
8469                         if (sp [0]->type != STACK_OBJ)
8470                                 UNVERIFIED;
8471                         CHECK_OPSIZE (5);
8472                         token = read32 (ip + 1);
8473                         klass = mini_get_class (method, token, generic_context);
8474                         CHECK_TYPELOAD (klass);
8475                         mono_class_init (klass);
8476                         NEW_LDELEMA (cfg, load, sp, klass);
8477                         MONO_INST_NEW (cfg, ins, mini_type_to_ldind (cfg, &klass->byval_arg));
8478                         ins->inst_left = load;
8479                         *sp++ = ins;
8480                         type_to_eval_stack_type (cfg, &klass->byval_arg, ins);
8481                         ip += 5;
8482                         break;
8483                 }
8484                 case CEE_LDELEM_I1:
8485                 case CEE_LDELEM_U1:
8486                 case CEE_LDELEM_I2:
8487                 case CEE_LDELEM_U2:
8488                 case CEE_LDELEM_I4:
8489                 case CEE_LDELEM_U4:
8490                 case CEE_LDELEM_I8:
8491                 case CEE_LDELEM_I:
8492                 case CEE_LDELEM_R4:
8493                 case CEE_LDELEM_R8:
8494                 case CEE_LDELEM_REF: {
8495                         MonoInst *load;
8496                         /*
8497                          * translate to:
8498                          * ldind.x (ldelema (array, index))
8499                          * ldelema does the bounds check
8500                          */
8501                         CHECK_STACK (2);
8502                         sp -= 2;
8503                         if (sp [0]->type != STACK_OBJ)
8504                                 UNVERIFIED;
8505                         klass = array_access_to_klass (*ip, sp [0]);
8506                         NEW_LDELEMA (cfg, load, sp, klass);
8507 #ifdef MONO_ARCH_SOFT_FLOAT
8508                         if (*ip == CEE_LDELEM_R4) {
8509                                 int temp;
8510                                 temp = handle_load_float (cfg, bblock, load, ip);
8511                                 NEW_TEMPLOAD (cfg, *sp, temp);
8512                                 sp++;
8513                                 ++ip;
8514                                 break;
8515                         }
8516 #endif
8517                         MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
8518                         ins->inst_left = load;
8519                         *sp++ = ins;
8520                         ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
8521                         ins->klass = klass;
8522                         ++ip;
8523                         break;
8524                 }
8525                 case CEE_STELEM_I:
8526                 case CEE_STELEM_I1:
8527                 case CEE_STELEM_I2:
8528                 case CEE_STELEM_I4:
8529                 case CEE_STELEM_I8:
8530                 case CEE_STELEM_R4:
8531                 case CEE_STELEM_R8: {
8532                         MonoInst *load;
8533                         /*
8534                          * translate to:
8535                          * stind.x (ldelema (array, index), val)
8536                          * ldelema does the bounds check
8537                          */
8538                         CHECK_STACK (3);
8539                         sp -= 3;
8540                         if (sp [0]->type != STACK_OBJ)
8541                                 UNVERIFIED;
8542                         klass = array_access_to_klass (*ip, sp [0]);
8543                         NEW_LDELEMA (cfg, load, sp, klass);
8544 #ifdef MONO_ARCH_SOFT_FLOAT
8545                         if (*ip == CEE_STELEM_R4) {
8546                                 handle_store_float (cfg, bblock, load, sp [2], ip);
8547                                 ip++;
8548                                 break;
8549                         }
8550 #endif
8551                         MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
8552                         ins->inst_left = load;
8553                         ins->inst_right = sp [2];
8554                         ++ip;
8555                         handle_loaded_temps (cfg, bblock, stack_start, sp);
8556                         MONO_ADD_INS (bblock, ins);
8557                         inline_costs += 1;
8558                         break;
8559                 }
8560                 case CEE_STELEM_ANY: {
8561                         MonoInst *load;
8562                         /*
8563                          * translate to:
8564                          * stind.x (ldelema (array, index), val)
8565                          * ldelema does the bounds check
8566                          */
8567                         CHECK_STACK (3);
8568                         sp -= 3;
8569                         if (sp [0]->type != STACK_OBJ)
8570                                 UNVERIFIED;
8571                         CHECK_OPSIZE (5);
8572                         token = read32 (ip + 1);
8573                         klass = mini_get_class (method, token, generic_context);
8574                         CHECK_TYPELOAD (klass);
8575                         mono_class_init (klass);
8576                         if (generic_class_is_reference_type (cfg, klass)) {
8577                                 /* storing a NULL doesn't need any of the complex checks in stelemref */
8578                                 if (sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL) {
8579                                         MonoInst *load;
8580                                         NEW_LDELEMA (cfg, load, sp, mono_defaults.object_class);
8581                                         MONO_INST_NEW (cfg, ins, CEE_STIND_REF);
8582                                         ins->inst_left = load;
8583                                         ins->inst_right = sp [2];
8584                                         MONO_ADD_INS (bblock, ins);
8585                                 } else {
8586                                         MonoMethod* helper = mono_marshal_get_stelemref ();
8587                                         MonoInst *iargs [3];
8588                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
8589
8590                                         iargs [2] = sp [2];
8591                                         iargs [1] = sp [1];
8592                                         iargs [0] = sp [0];
8593
8594                                         mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
8595                                 }
8596                         } else {
8597                                 NEW_LDELEMA (cfg, load, sp, klass);
8598
8599                                 n = mini_type_to_stind (cfg, &klass->byval_arg);
8600                                 /* FIXME: CEE_STIND_R4 */
8601                                 if (n == CEE_STOBJ)
8602                                         handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE, TRUE);
8603                                 else {
8604                                         MONO_INST_NEW (cfg, ins, n);
8605                                         ins->inst_left = load;
8606                                         ins->inst_right = sp [2];
8607                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
8608                                         MONO_ADD_INS (bblock, ins);
8609                                 }
8610                         }
8611                         ip += 5;
8612                         inline_costs += 1;
8613                         break;
8614                 }
8615                 case CEE_STELEM_REF: {
8616                         MonoInst *iargs [3];
8617                         MonoMethod* helper = mono_marshal_get_stelemref ();
8618
8619                         CHECK_STACK (3);
8620                         sp -= 3;
8621                         if (sp [0]->type != STACK_OBJ)
8622                                 UNVERIFIED;
8623                         if (sp [2]->type != STACK_OBJ)
8624                                 UNVERIFIED;
8625
8626                         handle_loaded_temps (cfg, bblock, stack_start, sp);
8627
8628                         /* storing a NULL doesn't need any of the complex checks in stelemref */
8629                         if (sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL) {
8630                                 MonoInst *load;
8631                                 NEW_LDELEMA (cfg, load, sp, mono_defaults.object_class);
8632                                 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
8633                                 ins->inst_left = load;
8634                                 ins->inst_right = sp [2];
8635                                 MONO_ADD_INS (bblock, ins);
8636                         } else {
8637                                 iargs [2] = sp [2];
8638                                 iargs [1] = sp [1];
8639                                 iargs [0] = sp [0];
8640                         
8641                                 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
8642                                 inline_costs += 1;
8643                         }
8644
8645                         ++ip;
8646                         break;
8647                 }
8648                 case CEE_CKFINITE: {
8649                         MonoInst *store, *temp;
8650                         CHECK_STACK (1);
8651
8652                         /* this instr. can throw exceptions as side effect,
8653                          * so we cant eliminate dead code which contains CKFINITE opdodes.
8654                          * Spilling to memory makes sure that we always perform
8655                          * this check */
8656
8657                         
8658                         MONO_INST_NEW (cfg, ins, OP_CKFINITE);
8659                         ins->inst_left = sp [-1];
8660                         temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
8661
8662                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
8663                         MONO_ADD_INS (bblock, store);
8664
8665                         NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
8666                        
8667                         ++ip;
8668                         break;
8669                 }
8670                 case CEE_REFANYVAL:
8671                         CHECK_STACK (1);
8672                         --sp;
8673                         CHECK_OPSIZE (5);
8674                         token = read32 (ip + 1);
8675                         klass = mono_class_get_full (image, token, generic_context);
8676                         CHECK_TYPELOAD (klass);
8677                         mono_class_init (klass);
8678
8679                         /* Needed by the code generated in inssel.brg */
8680                         mono_get_got_var (cfg);
8681
8682                         if (cfg->generic_sharing_context) {
8683                                 context_used = mono_class_check_context_used (klass);
8684                                 if (context_used && cfg->compile_aot)
8685                                         GENERIC_SHARING_FAILURE (*ip);
8686                         }
8687
8688                         if (context_used) {
8689                                 MonoInst *rgctx;
8690
8691                                 MONO_INST_NEW (cfg, ins, OP_REFANYVAL_REG);
8692                                 ins->type = STACK_MP;
8693                                 ins->inst_left = *sp;
8694                                 ins->klass = klass;
8695
8696                                 GET_RGCTX (rgctx, context_used);
8697                                 ins->inst_right = get_runtime_generic_context_ptr (cfg, method, context_used,
8698                                                 bblock, klass, generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
8699                         } else {
8700                                 MONO_INST_NEW (cfg, ins, *ip);
8701                                 ins->type = STACK_MP;
8702                                 ins->inst_left = *sp;
8703                                 ins->klass = klass;
8704                                 ins->inst_newa_class = klass;
8705                         }
8706                         ip += 5;
8707                         *sp++ = ins;
8708                         break;
8709                 case CEE_MKREFANY: {
8710                         MonoInst *loc;
8711
8712                         CHECK_STACK (1);
8713                         --sp;
8714                         CHECK_OPSIZE (5);
8715                         token = read32 (ip + 1);
8716                         klass = mono_class_get_full (image, token, generic_context);
8717                         CHECK_TYPELOAD (klass);
8718                         mono_class_init (klass);
8719
8720                         if (cfg->generic_sharing_context) {
8721                                 context_used = mono_class_check_context_used (klass);
8722                                 if (context_used && cfg->compile_aot)
8723                                         GENERIC_SHARING_FAILURE (CEE_MKREFANY);
8724                         }
8725
8726                         loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
8727                         if (context_used) {
8728                                 MonoInst *rgctx, *klass_type, *klass_klass, *loc_load;
8729
8730                                 GET_RGCTX (rgctx, context_used);
8731                                 klass_klass = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8732                                                 generic_context, rgctx, MONO_RGCTX_INFO_KLASS, ip);
8733                                 GET_RGCTX (rgctx, context_used);
8734                                 klass_type = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, klass,
8735                                                 generic_context, rgctx, MONO_RGCTX_INFO_TYPE, ip);
8736
8737                                 NEW_TEMPLOADA (cfg, loc_load, loc->inst_c0);
8738
8739                                 MONO_INST_NEW (cfg, ins, OP_MKREFANY_REGS);
8740                                 NEW_GROUP (cfg, ins->inst_left, klass_type, klass_klass);
8741                                 NEW_GROUP (cfg, ins->inst_right, *sp, loc_load);
8742                         } else {
8743                                 MonoInst *klassconst;
8744
8745                                 NEW_PCONST (cfg, klassconst, klass);
8746
8747                                 MONO_INST_NEW (cfg, ins, *ip);
8748                                 NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
8749                                 NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
8750                         }
8751
8752                         MONO_ADD_INS (bblock, ins);
8753
8754                         NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
8755                         ++sp;
8756                         ip += 5;
8757                         break;
8758                 }
8759                 case CEE_LDTOKEN: {
8760                         gpointer handle;
8761                         MonoClass *handle_class;
8762
8763                         CHECK_STACK_OVF (1);
8764
8765                         CHECK_OPSIZE (5);
8766                         n = read32 (ip + 1);
8767
8768                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
8769                                 handle = mono_method_get_wrapper_data (method, n);
8770                                 handle_class = mono_method_get_wrapper_data (method, n + 1);
8771                                 if (handle_class == mono_defaults.typehandle_class)
8772                                         handle = &((MonoClass*)handle)->byval_arg;
8773                         }
8774                         else {
8775                                 handle = mono_ldtoken (image, n, &handle_class, generic_context);
8776                         }
8777                         if (!handle)
8778                                 goto load_error;
8779                         mono_class_init (handle_class);
8780
8781                         if (cfg->generic_sharing_context) {
8782                                 if (handle_class == mono_defaults.typehandle_class) {
8783                                         /* If we get a MONO_TYPE_CLASS
8784                                            then we need to provide the
8785                                            open type, not an
8786                                            instantiation of it. */
8787                                         if (mono_type_get_type (handle) == MONO_TYPE_CLASS)
8788                                                 context_used = 0;
8789                                         else
8790                                                 context_used = mono_class_check_context_used (mono_class_from_mono_type (handle));
8791                                 } else if (handle_class == mono_defaults.fieldhandle_class)
8792                                         context_used = mono_class_check_context_used (((MonoClassField*)handle)->parent);
8793                                 else if (handle_class == mono_defaults.methodhandle_class)
8794                                         context_used = mono_method_check_context_used (handle);
8795                                 else
8796                                         g_assert_not_reached ();
8797                         }
8798
8799                         if (cfg->opt & MONO_OPT_SHARED) {
8800                                 int temp;
8801                                 MonoInst *res, *store, *addr, *vtvar, *iargs [3];
8802                                 int method_context_used;
8803
8804                                 if (cfg->generic_sharing_context)
8805                                         method_context_used = mono_method_check_context_used (method);
8806                                 else
8807                                         method_context_used = 0;
8808
8809                                 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL); 
8810
8811                                 NEW_IMAGECONST (cfg, iargs [0], image);
8812                                 NEW_ICONST (cfg, iargs [1], n);
8813                                 if (method_context_used) {
8814                                         MonoInst *rgctx;
8815
8816                                         GET_RGCTX (rgctx, method_context_used);
8817                                         iargs [2] = get_runtime_generic_context_method (cfg, method, method_context_used,
8818                                                         bblock, method,
8819                                                         generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
8820                                         temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper_generic_shared,
8821                                                         iargs, ip);
8822                                 } else {
8823                                         NEW_PCONST (cfg, iargs [2], generic_context);
8824                                         temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
8825                                 }
8826                                 NEW_TEMPLOAD (cfg, res, temp);
8827                                 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
8828                                 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
8829                                 MONO_ADD_INS (bblock, store);
8830                                 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
8831                         } else {
8832                                 if ((ip + 10 < end) && ip_in_bb (cfg, bblock, ip + 5) &&
8833                                         handle_class == mono_defaults.typehandle_class &&
8834                                         ((ip [5] == CEE_CALL) || (ip [5] == CEE_CALLVIRT)) && 
8835                                         (cmethod = mini_get_method (cfg, method, read32 (ip + 6), NULL, generic_context)) &&
8836                                         (cmethod->klass == mono_defaults.monotype_class->parent) &&
8837                                         (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
8838                                         MonoClass *tclass = mono_class_from_mono_type (handle);
8839                                         mono_class_init (tclass);
8840                                         if (context_used) {
8841                                                 MonoInst *rgctx;
8842
8843                                                 g_assert (!cfg->compile_aot);
8844
8845                                                 GET_RGCTX (rgctx, context_used);
8846                                                 ins = get_runtime_generic_context_ptr (cfg, method, context_used, bblock, tclass,
8847                                                         generic_context, rgctx, MONO_RGCTX_INFO_REFLECTION_TYPE, ip);
8848                                         } else if (cfg->compile_aot) {
8849                                                 /*
8850                                                  * FIXME: We would have to include the context into the
8851                                                  * aot constant too (tests/generic-array-type.2.exe).
8852                                                  */
8853                                                 if (generic_context)
8854                                                         cfg->disable_aot = TRUE;
8855                                                 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
8856                                         } else {
8857                                                 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
8858                                         }
8859                                         ins->type = STACK_OBJ;
8860                                         ins->klass = cmethod->klass;
8861                                         ip += 5;
8862                                 } else {
8863                                         MonoInst *store, *addr, *vtvar;
8864
8865                                         if (context_used) {
8866                                                         MonoInst *rgctx;
8867
8868                                                 g_assert (!cfg->compile_aot);
8869
8870                                                 GET_RGCTX (rgctx, context_used);
8871                                                 if (handle_class == mono_defaults.typehandle_class) {
8872                                                         ins = get_runtime_generic_context_ptr (cfg, method,
8873                                                                         context_used, bblock,
8874                                                                         mono_class_from_mono_type (handle), generic_context,
8875                                                                         rgctx, MONO_RGCTX_INFO_TYPE, ip);
8876                                                 } else if (handle_class == mono_defaults.methodhandle_class) {
8877                                                         ins = get_runtime_generic_context_method (cfg, method,
8878                                                                         context_used, bblock, handle, generic_context,
8879                                                                         rgctx, MONO_RGCTX_INFO_METHOD, ip);
8880                                                 } else if (handle_class == mono_defaults.fieldhandle_class) {
8881                                                         ins = get_runtime_generic_context_field (cfg, method,
8882                                                                         context_used, bblock, handle, generic_context,
8883                                                                         rgctx, MONO_RGCTX_INFO_CLASS_FIELD, ip);
8884                                                 } else {
8885                                                         g_assert_not_reached ();
8886                                                 }
8887                                         }
8888                                         else if (cfg->compile_aot) {
8889                                                 NEW_LDTOKENCONST (cfg, ins, image, n);
8890                                         } else {
8891                                                 NEW_PCONST (cfg, ins, handle);
8892                                         }
8893                                         vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
8894                                         NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
8895                                         NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
8896                                         MONO_ADD_INS (bblock, store);
8897                                         NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
8898                                 }
8899                         }
8900
8901                         *sp++ = ins;
8902                         ip += 5;
8903                         break;
8904                 }
8905                 case CEE_CONV_U2:
8906                 case CEE_CONV_U1:
8907                 case CEE_CONV_I:
8908                         CHECK_STACK (1);
8909                         ADD_UNOP (*ip);
8910                         ip++;
8911                         break;
8912                 case CEE_ADD_OVF:
8913                 case CEE_ADD_OVF_UN:
8914                 case CEE_MUL_OVF:
8915                 case CEE_MUL_OVF_UN:
8916                 case CEE_SUB_OVF:
8917                 case CEE_SUB_OVF_UN:
8918                         CHECK_STACK (2);
8919                         ADD_BINOP (*ip);
8920                         if (mono_find_jit_opcode_emulation (ins->opcode)) {
8921                                 --sp;
8922                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
8923                         }
8924                         ip++;
8925                         break;
8926                 case CEE_ENDFINALLY:
8927                         MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
8928                         MONO_ADD_INS (bblock, ins);
8929                         ip++;
8930                         start_new_bblock = 1;
8931
8932                         /*
8933                          * Control will leave the method so empty the stack, otherwise
8934                          * the next basic block will start with a nonempty stack.
8935                          */
8936                         while (sp != stack_start) {
8937                                 MONO_INST_NEW (cfg, ins, CEE_POP);
8938                                 sp--;
8939                                 ins->inst_i0 = *sp;
8940                                 MONO_ADD_INS (bblock, ins);
8941                         }
8942                         break;
8943                 case CEE_LEAVE:
8944                 case CEE_LEAVE_S: {
8945                         GList *handlers;
8946
8947                         if (*ip == CEE_LEAVE) {
8948                                 CHECK_OPSIZE (5);
8949                                 target = ip + 5 + (gint32)read32(ip + 1);
8950                         } else {
8951                                 CHECK_OPSIZE (2);
8952                                 target = ip + 2 + (signed char)(ip [1]);
8953                         }
8954
8955                         /* empty the stack */
8956                         while (sp != stack_start) {
8957                                 MONO_INST_NEW (cfg, ins, CEE_POP);
8958                                 sp--;
8959                                 ins->inst_i0 = *sp;
8960                                 MONO_ADD_INS (bblock, ins);
8961                         }
8962
8963                         /* 
8964                          * If this leave statement is in a catch block, check for a
8965                          * pending exception, and rethrow it if necessary.
8966                          */
8967                         for (i = 0; i < header->num_clauses; ++i) {
8968                                 MonoExceptionClause *clause = &header->clauses [i];
8969
8970                                 /* 
8971                                  * Use <= in the final comparison to handle clauses with multiple
8972                                  * leave statements, like in bug #78024.
8973                                  * The ordering of the exception clauses guarantees that we find the
8974                                  * innermost clause.
8975                                  */
8976                                 if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) && (ip - header->code + ((*ip == CEE_LEAVE) ? 5 : 2)) <= (clause->handler_offset + clause->handler_len)) {
8977                                         int temp;
8978                                         MonoInst *load;
8979
8980                                         NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
8981
8982                                         temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_undeniable_exception, NULL, ip);
8983                                         NEW_TEMPLOAD (cfg, *sp, temp);
8984                                 
8985                                         MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
8986                                         ins->inst_left = *sp;
8987                                         ins->inst_right = load;
8988                                         MONO_ADD_INS (bblock, ins);
8989                                 }
8990                         }
8991
8992                         if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
8993                                 GList *tmp;
8994                                 for (tmp = handlers; tmp; tmp = tmp->next) {
8995                                         tblock = tmp->data;
8996                                         link_bblock (cfg, bblock, tblock);
8997                                         MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
8998                                         ins->inst_target_bb = tblock;
8999                                         MONO_ADD_INS (bblock, ins);
9000                                 }
9001                                 g_list_free (handlers);
9002                         } 
9003
9004                         MONO_INST_NEW (cfg, ins, OP_BR);
9005                         MONO_ADD_INS (bblock, ins);
9006                         GET_BBLOCK (cfg, tblock, target);
9007                         link_bblock (cfg, bblock, tblock);
9008                         CHECK_BBLOCK (target, ip, tblock);
9009                         ins->inst_target_bb = tblock;
9010                         start_new_bblock = 1;
9011
9012                         if (*ip == CEE_LEAVE)
9013                                 ip += 5;
9014                         else
9015                                 ip += 2;
9016
9017                         break;
9018                 }
9019                 case CEE_STIND_I:
9020                         CHECK_STACK (2);
9021                         MONO_INST_NEW (cfg, ins, *ip);
9022                         sp -= 2;
9023                         handle_loaded_temps (cfg, bblock, stack_start, sp);
9024                         MONO_ADD_INS (bblock, ins);
9025                         ip++;
9026                         ins->inst_i0 = sp [0];
9027                         ins->inst_i1 = sp [1];
9028                         inline_costs += 1;
9029                         break;
9030                 case CEE_CONV_U:
9031                         CHECK_STACK (1);
9032                         ADD_UNOP (*ip);
9033                         ip++;
9034                         break;
9035                 /* trampoline mono specific opcodes */
9036                 case MONO_CUSTOM_PREFIX: {
9037
9038                         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
9039
9040                         CHECK_OPSIZE (2);
9041                         switch (ip [1]) {
9042
9043                         case CEE_MONO_ICALL: {
9044                                 int temp;
9045                                 gpointer func;
9046                                 MonoJitICallInfo *info;
9047
9048                                 token = read32 (ip + 2);
9049                                 func = mono_method_get_wrapper_data (method, token);
9050                                 info = mono_find_jit_icall_by_addr (func);
9051                                 if (info == NULL){
9052                                         g_error ("An attempt has been made to perform an icall to address %p, "
9053                                                  "but the address has not been registered as an icall\n", info);
9054                                         g_assert_not_reached ();
9055                                 }
9056
9057                                 CHECK_STACK (info->sig->param_count);
9058                                 sp -= info->sig->param_count;
9059
9060                                 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
9061                                 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
9062                                         NEW_TEMPLOAD (cfg, *sp, temp);
9063                                         sp++;
9064                                 }
9065
9066                                 ip += 6;
9067                                 inline_costs += 10 * num_calls++;
9068
9069                                 break;
9070                         }
9071                         case CEE_MONO_LDPTR: {
9072                                 gpointer ptr;
9073
9074                                 CHECK_STACK_OVF (1);
9075                                 CHECK_OPSIZE (6);
9076                                 token = read32 (ip + 2);
9077
9078                                 ptr = mono_method_get_wrapper_data (method, token);
9079                                 if (cfg->compile_aot && (cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || cfg->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE)) {
9080                                         MonoMethod *wrapped = mono_marshal_method_from_wrapper (cfg->method);
9081
9082                                         if (wrapped && ptr != NULL && mono_lookup_internal_call (wrapped) == ptr) {
9083                                                 NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, wrapped);
9084                                                 *sp++ = ins;
9085                                                 ip += 6;
9086                                                 break;
9087                                         }
9088
9089                                         if ((method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
9090                                                 MonoJitICallInfo *callinfo;
9091                                                 const char *icall_name;
9092
9093                                                 icall_name = method->name + strlen ("__icall_wrapper_");
9094                                                 g_assert (icall_name);
9095                                                 callinfo = mono_find_jit_icall_by_name (icall_name);
9096                                                 g_assert (callinfo);
9097
9098                                                 if (ptr == callinfo->func) {
9099                                                         /* Will be transformed into an AOTCONST later */
9100                                                         NEW_PCONST (cfg, ins, ptr);
9101                                                         *sp++ = ins;
9102                                                         ip += 6;
9103                                                         break;
9104                                                 }
9105                                         }
9106                                 }
9107                                 /* FIXME: Generalize this */
9108                                 if (cfg->compile_aot && ptr == mono_thread_interruption_request_flag ()) {
9109                                         NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG, NULL);
9110                                         *sp++ = ins;
9111                                         ip += 6;
9112                                         break;
9113                                 }
9114                                 NEW_PCONST (cfg, ins, ptr);
9115                                 *sp++ = ins;
9116                                 ip += 6;
9117                                 inline_costs += 10 * num_calls++;
9118                                 /* Can't embed random pointers into AOT code */
9119                                 cfg->disable_aot = 1;
9120                                 break;
9121                         }
9122                         case CEE_MONO_ICALL_ADDR: {
9123                                 MonoMethod *cmethod;
9124
9125                                 CHECK_STACK_OVF (1);
9126                                 CHECK_OPSIZE (6);
9127                                 token = read32 (ip + 2);
9128
9129                                 cmethod = mono_method_get_wrapper_data (method, token);
9130
9131                                 g_assert (cfg->compile_aot);
9132
9133                                 NEW_AOTCONST (cfg, ins, MONO_PATCH_INFO_ICALL_ADDR, cmethod);
9134                                 *sp++ = ins;
9135                                 ip += 6;
9136                                 break;
9137                         }
9138                         case CEE_MONO_VTADDR:
9139                                 CHECK_STACK (1);
9140                                 --sp;
9141                                 MONO_INST_NEW (cfg, ins, OP_VTADDR);
9142                                 ins->type = STACK_MP;
9143                                 ins->inst_left = *sp;
9144                                 *sp++ = ins;
9145                                 ip += 2;
9146                                 break;
9147                         case CEE_MONO_NEWOBJ: {
9148                                 MonoInst *iargs [2];
9149                                 int temp;
9150                                 CHECK_STACK_OVF (1);
9151                                 CHECK_OPSIZE (6);
9152                                 token = read32 (ip + 2);
9153                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
9154                                 mono_class_init (klass);
9155                                 NEW_DOMAINCONST (cfg, iargs [0]);
9156                                 NEW_CLASSCONST (cfg, iargs [1], klass);
9157                                 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
9158                                 NEW_TEMPLOAD (cfg, *sp, temp);
9159                                 sp++;
9160                                 ip += 6;
9161                                 inline_costs += 10 * num_calls++;
9162                                 break;
9163                         }
9164                         case CEE_MONO_OBJADDR:
9165                                 CHECK_STACK (1);
9166                                 --sp;
9167                                 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
9168                                 ins->type = STACK_MP;
9169                                 ins->inst_left = *sp;
9170                                 *sp++ = ins;
9171                                 ip += 2;
9172                                 break;
9173                         case CEE_MONO_LDNATIVEOBJ:
9174                                 CHECK_STACK (1);
9175                                 CHECK_OPSIZE (6);
9176                                 token = read32 (ip + 2);
9177                                 klass = mono_method_get_wrapper_data (method, token);
9178                                 g_assert (klass->valuetype);
9179                                 mono_class_init (klass);
9180                                 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
9181                                 sp [-1] = ins;
9182                                 ip += 6;
9183                                 break;
9184                         case CEE_MONO_RETOBJ:
9185                                 g_assert (cfg->ret);
9186                                 g_assert (mono_method_signature (method)->pinvoke); 
9187                                 CHECK_STACK (1);
9188                                 --sp;
9189                                 
9190                                 CHECK_OPSIZE (6);
9191                                 token = read32 (ip + 2);    
9192                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
9193
9194                                 NEW_RETLOADA (cfg, ins);
9195                                 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE, FALSE);
9196                                 
9197                                 if (sp != stack_start)
9198                                         UNVERIFIED;
9199                                 
9200                                 MONO_INST_NEW (cfg, ins, OP_BR);
9201                                 ins->inst_target_bb = end_bblock;
9202                                 MONO_ADD_INS (bblock, ins);
9203                                 link_bblock (cfg, bblock, end_bblock);
9204                                 start_new_bblock = 1;
9205                                 ip += 6;
9206                                 break;
9207                         case CEE_MONO_CISINST:
9208                         case CEE_MONO_CCASTCLASS: {
9209                                 int token;
9210                                 CHECK_STACK (1);
9211                                 --sp;
9212                                 CHECK_OPSIZE (6);
9213                                 token = read32 (ip + 2);
9214                                 /* Needed by the code generated in inssel.brg */
9215                                 mono_get_got_var (cfg);
9216
9217 #ifdef __i386__
9218                                 /* 
9219                                  * The code generated for CCASTCLASS has too much register pressure
9220                                  * (obj+vtable+ibitmap_byte_reg+iid_reg), leading to the usual
9221                                  * branches-inside-bblocks problem.
9222                                  */
9223                                 cfg->disable_aot = TRUE;
9224 #endif
9225                 
9226                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
9227                                 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
9228                                 ins->type = STACK_I4;
9229                                 ins->inst_left = *sp;
9230                                 ins->inst_newa_class = klass;
9231                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 6);
9232                                 ip += 6;
9233                                 break;
9234                         }
9235                         case CEE_MONO_SAVE_LMF:
9236                         case CEE_MONO_RESTORE_LMF:
9237 #ifdef MONO_ARCH_HAVE_LMF_OPS
9238                                 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
9239                                 MONO_ADD_INS (bblock, ins);
9240                                 cfg->need_lmf_area = TRUE;
9241 #endif
9242                                 ip += 2;
9243                                 break;
9244                         case CEE_MONO_CLASSCONST:
9245                                 CHECK_STACK_OVF (1);
9246                                 CHECK_OPSIZE (6);
9247                                 token = read32 (ip + 2);
9248                                 NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
9249                                 *sp++ = ins;
9250                                 ip += 6;
9251                                 inline_costs += 10 * num_calls++;
9252                                 break;
9253                         case CEE_MONO_NOT_TAKEN:
9254                                 bblock->out_of_line = TRUE;
9255                                 ip += 2;
9256                                 break;
9257                         case CEE_MONO_TLS:
9258                                 CHECK_STACK_OVF (1);
9259                                 CHECK_OPSIZE (6);
9260                                 MONO_INST_NEW (cfg, ins, OP_TLS_GET);
9261                                 ins->inst_offset = (gint32)read32 (ip + 2);
9262                                 ins->type = STACK_PTR;
9263                                 *sp++ = ins;
9264                                 ip += 6;
9265                                 break;
9266                         default:
9267                                 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
9268                                 break;
9269                         }
9270                         break;
9271                 }
9272                 case CEE_PREFIX1: {
9273                         CHECK_OPSIZE (2);
9274                         switch (ip [1]) {
9275                         case CEE_ARGLIST: {
9276                                 /* somewhat similar to LDTOKEN */
9277                                 MonoInst *addr, *vtvar;
9278                                 CHECK_STACK_OVF (1);
9279                                 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL); 
9280
9281                                 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
9282                                 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
9283                                 ins->inst_left = addr;
9284                                 MONO_ADD_INS (bblock, ins);
9285                                 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
9286                                 *sp++ = ins;
9287                                 ip += 2;
9288                                 break;
9289                         }
9290                         case CEE_CEQ:
9291                         case CEE_CGT:
9292                         case CEE_CGT_UN:
9293                         case CEE_CLT:
9294                         case CEE_CLT_UN: {
9295                                 MonoInst *cmp;
9296                                 CHECK_STACK (2);
9297                                 /*
9298                                  * The following transforms:
9299                                  *    CEE_CEQ    into OP_CEQ
9300                                  *    CEE_CGT    into OP_CGT
9301                                  *    CEE_CGT_UN into OP_CGT_UN
9302                                  *    CEE_CLT    into OP_CLT
9303                                  *    CEE_CLT_UN into OP_CLT_UN
9304                                  */
9305                                 MONO_INST_NEW (cfg, cmp, (OP_CEQ - CEE_CEQ) + ip [1]);
9306                                 
9307                                 MONO_INST_NEW (cfg, ins, cmp->opcode);
9308                                 sp -= 2;
9309                                 cmp->inst_i0 = sp [0];
9310                                 cmp->inst_i1 = sp [1];
9311                                 type_from_op (cmp);
9312                                 CHECK_TYPE (cmp);
9313                                 ins->type = STACK_I4;
9314                                 ins->inst_i0 = cmp;
9315 #if MONO_ARCH_SOFT_FLOAT
9316                                 if (sp [0]->type == STACK_R8) {
9317                                         cmp->type = STACK_I4;
9318                                         *sp++ = emit_tree (cfg, bblock, cmp, ip + 2);
9319                                         ip += 2;
9320                                         break;
9321                                 }
9322 #endif
9323                                 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
9324                                         cmp->opcode = OP_LCOMPARE;
9325                                 else
9326                                         cmp->opcode = OP_COMPARE;
9327                                 *sp++ = ins;
9328                                 /* spill it to reduce the expression complexity
9329                                  * and workaround bug 54209 
9330                                  */
9331                                 if (cmp->inst_left->type == STACK_I8) {
9332                                         --sp;
9333                                         *sp++ = emit_tree (cfg, bblock, ins, ip + 2);
9334                                 }
9335                                 ip += 2;
9336                                 break;
9337                         }
9338                         case CEE_LDFTN: {
9339                                 MonoInst *argconst;
9340                                 MonoMethod *cil_method, *ctor_method;
9341                                 int temp;
9342                                 gboolean needs_static_rgctx_invoke;
9343
9344                                 CHECK_STACK_OVF (1);
9345                                 CHECK_OPSIZE (6);
9346                                 n = read32 (ip + 2);
9347                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
9348                                 if (!cmethod)
9349                                         goto load_error;
9350                                 mono_class_init (cmethod->klass);
9351
9352                                 if (cfg->generic_sharing_context)
9353                                         context_used = mono_method_check_context_used (cmethod);
9354
9355                                 needs_static_rgctx_invoke = mono_method_needs_static_rgctx_invoke (cmethod, TRUE);
9356
9357                                 cil_method = cmethod;
9358                                 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cmethod))
9359                                         METHOD_ACCESS_FAILURE;
9360                                 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
9361                                         if (check_linkdemand (cfg, method, cmethod, bblock, ip))
9362                                                 INLINE_FAILURE;
9363                                         CHECK_CFG_EXCEPTION;
9364                                 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
9365                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
9366                                 }
9367
9368                                 /* 
9369                                  * Optimize the common case of ldftn+delegate creation
9370                                  */
9371 #if defined(MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE) && !defined(HAVE_WRITE_BARRIERS)
9372                                 /* FIXME: SGEN support */
9373                                 /* FIXME: handle shared static generic methods */
9374                                 /* FIXME: handle this in shared code */
9375                                 if (!needs_static_rgctx_invoke && !context_used && (sp > stack_start) && (ip + 6 + 5 < end) && ip_in_bb (cfg, bblock, ip + 6) && (ip [6] == CEE_NEWOBJ) && (ctor_method = mini_get_method (cfg, method, read32 (ip + 7), NULL, generic_context)) && (ctor_method->klass->parent == mono_defaults.multicastdelegate_class)) {
9376                                         MonoInst *target_ins;
9377
9378                                         ip += 6;
9379                                         if (cfg->verbose_level > 3)
9380                                                 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
9381                                         target_ins = sp [-1];
9382                                         sp --;
9383                                         *sp = handle_delegate_ctor (cfg, bblock, ctor_method->klass, target_ins, cmethod, ip);
9384                                         ip += 5;                                        
9385                                         sp ++;
9386                                         break;
9387                                 }
9388 #endif
9389
9390                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
9391
9392                                 if (context_used) {
9393                                         MonoInst *rgctx;
9394
9395                                         if (needs_static_rgctx_invoke)
9396                                                 cmethod = mono_marshal_get_static_rgctx_invoke (cmethod);
9397
9398                                         GET_RGCTX (rgctx, context_used);
9399                                         argconst = get_runtime_generic_context_method (cfg, method, context_used,
9400                                                         bblock, cmethod,
9401                                                         generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
9402                                 } else if (needs_static_rgctx_invoke) {
9403                                         NEW_METHODCONST (cfg, argconst, mono_marshal_get_static_rgctx_invoke (cmethod));
9404                                 } else {
9405                                         NEW_METHODCONST (cfg, argconst, cmethod);
9406                                 }
9407                                 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
9408                                 NEW_TEMPLOAD (cfg, *sp, temp);
9409                                 sp ++;
9410                                 
9411                                 ip += 6;
9412                                 inline_costs += 10 * num_calls++;
9413                                 break;
9414                         }
9415                         case CEE_LDVIRTFTN: {
9416                                 MonoInst *args [2];
9417                                 int temp;
9418
9419                                 CHECK_STACK (1);
9420                                 CHECK_OPSIZE (6);
9421                                 n = read32 (ip + 2);
9422                                 cmethod = mini_get_method (cfg, method, n, NULL, generic_context);
9423                                 if (!cmethod)
9424                                         goto load_error;
9425                                 mono_class_init (cmethod->klass);
9426
9427                                 if (cfg->generic_sharing_context)
9428                                         context_used = mono_method_check_context_used (cmethod);
9429
9430                                 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
9431                                         if (check_linkdemand (cfg, method, cmethod, bblock, ip))
9432                                                 INLINE_FAILURE;
9433                                         CHECK_CFG_EXCEPTION;
9434                                 } else if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR) {
9435                                         ensure_method_is_allowed_to_call_method (cfg, method, cmethod, bblock, ip);
9436                                 }
9437
9438                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
9439
9440                                 --sp;
9441                                 args [0] = *sp;
9442                                 if (context_used) {
9443                                         MonoInst *rgctx;
9444
9445                                         GET_RGCTX (rgctx, context_used);
9446                                         args [1] = get_runtime_generic_context_method (cfg, method, context_used,
9447                                                         bblock, cmethod,
9448                                                         generic_context, rgctx, MONO_RGCTX_INFO_METHOD, ip);
9449                                         temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn_gshared, args, ip);
9450                                 } else {
9451                                         NEW_METHODCONST (cfg, args [1], cmethod);
9452                                         temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
9453                                 }
9454                                 NEW_TEMPLOAD (cfg, *sp, temp);
9455                                 sp ++;
9456
9457                                 ip += 6;
9458                                 inline_costs += 10 * num_calls++;
9459                                 break;
9460                         }
9461                         case CEE_LDARG:
9462                                 CHECK_STACK_OVF (1);
9463                                 CHECK_OPSIZE (4);
9464                                 n = read16 (ip + 2);
9465                                 CHECK_ARG (n);
9466                                 NEW_ARGLOAD (cfg, ins, n);
9467                                 LDARG_SOFT_FLOAT (cfg, ins, n, ip);
9468                                 *sp++ = ins;
9469                                 ip += 4;
9470                                 break;
9471                         case CEE_LDARGA:
9472                                 CHECK_STACK_OVF (1);
9473                                 CHECK_OPSIZE (4);
9474                                 n = read16 (ip + 2);
9475                                 CHECK_ARG (n);
9476                                 NEW_ARGLOADA (cfg, ins, n);
9477                                 *sp++ = ins;
9478                                 ip += 4;
9479                                 break;
9480                         case CEE_STARG:
9481                                 CHECK_STACK (1);
9482                                 --sp;
9483                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
9484                                 CHECK_OPSIZE (4);
9485                                 n = read16 (ip + 2);
9486                                 CHECK_ARG (n);
9487                                 NEW_ARGSTORE (cfg, ins, n, *sp);
9488                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
9489                                         UNVERIFIED;
9490                                 STARG_SOFT_FLOAT (cfg, ins, n, ip);
9491                                 if (ins->opcode == CEE_STOBJ) {
9492                                         NEW_ARGLOADA (cfg, ins, n);
9493                                         handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
9494                                 } else
9495                                         MONO_ADD_INS (bblock, ins);
9496                                 ip += 4;
9497                                 break;
9498                         case CEE_LDLOC:
9499                                 CHECK_STACK_OVF (1);
9500                                 CHECK_OPSIZE (4);
9501                                 n = read16 (ip + 2);
9502                                 CHECK_LOCAL (n);
9503                                 NEW_LOCLOAD (cfg, ins, n);
9504                                 LDLOC_SOFT_FLOAT (cfg, ins, n, ip);
9505                                 *sp++ = ins;
9506                                 ip += 4;
9507                                 break;
9508                         case CEE_LDLOCA:
9509                                 CHECK_STACK_OVF (1);
9510                                 CHECK_OPSIZE (4);
9511                                 n = read16 (ip + 2);
9512                                 CHECK_LOCAL (n);
9513                                 NEW_LOCLOADA (cfg, ins, n);
9514                                 *sp++ = ins;
9515                                 ip += 4;
9516                                 break;
9517                         case CEE_STLOC:
9518                                 CHECK_STACK (1);
9519                                 --sp;
9520                                 CHECK_OPSIZE (4);
9521                                 n = read16 (ip + 2);
9522                                 CHECK_LOCAL (n);
9523                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
9524                                 NEW_LOCSTORE (cfg, ins, n, *sp);
9525                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
9526                                         UNVERIFIED;
9527                                 STLOC_SOFT_FLOAT (cfg, ins, n, ip);
9528                                 if (ins->opcode == CEE_STOBJ) {
9529                                         NEW_LOCLOADA (cfg, ins, n);
9530                                         handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
9531                                 } else
9532                                         MONO_ADD_INS (bblock, ins);
9533                                 ip += 4;
9534                                 inline_costs += 1;
9535                                 break;
9536                         case CEE_LOCALLOC:
9537                                 CHECK_STACK (1);
9538                                 --sp;
9539                                 if (sp != stack_start) 
9540                                         UNVERIFIED;
9541                                 if (cfg->method != method) 
9542                                         /* 
9543                                          * Inlining this into a loop in a parent could lead to 
9544                                          * stack overflows which is different behavior than the
9545                                          * non-inlined case, thus disable inlining in this case.
9546                                          */
9547                                         goto inline_failure;
9548                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
9549                                 ins->inst_left = *sp;
9550                                 ins->type = STACK_PTR;
9551
9552                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
9553                                 if (header->init_locals)
9554                                         ins->flags |= MONO_INST_INIT;
9555
9556                                 *sp++ = ins;
9557                                 ip += 2;
9558                                 /* FIXME: set init flag if locals init is set in this method */
9559                                 break;
9560                         case CEE_ENDFILTER: {
9561                                 MonoExceptionClause *clause, *nearest;
9562                                 int cc, nearest_num;
9563
9564                                 CHECK_STACK (1);
9565                                 --sp;
9566                                 if ((sp != stack_start) || (sp [0]->type != STACK_I4)) 
9567                                         UNVERIFIED;
9568                                 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
9569                                 ins->inst_left = *sp;
9570                                 MONO_ADD_INS (bblock, ins);
9571                                 start_new_bblock = 1;
9572                                 ip += 2;
9573
9574                                 nearest = NULL;
9575                                 nearest_num = 0;
9576                                 for (cc = 0; cc < header->num_clauses; ++cc) {
9577                                         clause = &header->clauses [cc];
9578                                         if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
9579                                                 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
9580                                             (!nearest || (clause->data.filter_offset < nearest->data.filter_offset))) {
9581                                                 nearest = clause;
9582                                                 nearest_num = cc;
9583                                         }
9584                                 }
9585                                 g_assert (nearest);
9586                                 if ((ip - header->code) != nearest->handler_offset)
9587                                         UNVERIFIED;
9588
9589                                 break;
9590                         }
9591                         case CEE_UNALIGNED_:
9592                                 ins_flag |= MONO_INST_UNALIGNED;
9593                                 /* FIXME: record alignment? we can assume 1 for now */
9594                                 CHECK_OPSIZE (3);
9595                                 ip += 3;
9596                                 break;
9597                         case CEE_VOLATILE_:
9598                                 ins_flag |= MONO_INST_VOLATILE;
9599                                 ip += 2;
9600                                 break;
9601                         case CEE_TAIL_:
9602                                 ins_flag   |= MONO_INST_TAILCALL;
9603                                 cfg->flags |= MONO_CFG_HAS_TAIL;
9604                                 /* Can't inline tail calls at this time */
9605                                 inline_costs += 100000;
9606                                 ip += 2;
9607                                 break;
9608                         case CEE_INITOBJ:
9609                                 CHECK_STACK (1);
9610                                 --sp;
9611                                 CHECK_OPSIZE (6);
9612                                 token = read32 (ip + 2);
9613                                 klass = mini_get_class (method, token, generic_context);
9614                                 CHECK_TYPELOAD (klass);
9615
9616                                 if (generic_class_is_reference_type (cfg, klass)) {
9617                                         MonoInst *store, *load;
9618                                         NEW_PCONST (cfg, load, NULL);
9619                                         load->type = STACK_OBJ;
9620                                         load->klass = klass;
9621                                         MONO_INST_NEW (cfg, store, CEE_STIND_REF);
9622                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
9623                                         MONO_ADD_INS (bblock, store);
9624                                         store->inst_i0 = sp [0];
9625                                         store->inst_i1 = load;
9626                                 } else {
9627                                         handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
9628                                 }
9629                                 ip += 6;
9630                                 inline_costs += 1;
9631                                 break;
9632                         case CEE_CONSTRAINED_:
9633                                 /* FIXME: implement */
9634                                 CHECK_OPSIZE (6);
9635                                 token = read32 (ip + 2);
9636                                 constrained_call = mono_class_get_full (image, token, generic_context);
9637                                 CHECK_TYPELOAD (constrained_call);
9638                                 ip += 6;
9639                                 break;
9640                         case CEE_CPBLK:
9641                         case CEE_INITBLK: {
9642                                 MonoInst *iargs [3];
9643                                 CHECK_STACK (3);
9644                                 sp -= 3;
9645                                 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
9646                                         MonoInst *copy;
9647                                         NEW_MEMCPY (cfg, copy, sp [0], sp [1], n, 0);
9648                                         MONO_ADD_INS (bblock, copy);
9649                                         ip += 2;
9650                                         break;
9651                                 }
9652                                 iargs [0] = sp [0];
9653                                 iargs [1] = sp [1];
9654                                 iargs [2] = sp [2];
9655                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
9656                                 if (ip [1] == CEE_CPBLK) {
9657                                         MonoMethod *memcpy_method = get_memcpy_method ();
9658                                         mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
9659                                 } else {
9660                                         MonoMethod *memset_method = get_memset_method ();
9661                                         mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
9662                                 }
9663                                 ip += 2;
9664                                 inline_costs += 1;
9665                                 break;
9666                         }
9667                         case CEE_NO_:
9668                                 CHECK_OPSIZE (3);
9669                                 if (ip [2] & 0x1)
9670                                         ins_flag |= MONO_INST_NOTYPECHECK;
9671                                 if (ip [2] & 0x2)
9672                                         ins_flag |= MONO_INST_NORANGECHECK;
9673                                 /* we ignore the no-nullcheck for now since we
9674                                  * really do it explicitly only when doing callvirt->call
9675                                  */
9676                                 ip += 3;
9677                                 break;
9678                         case CEE_RETHROW: {
9679                                 MonoInst *load;
9680                                 int handler_offset = -1;
9681
9682                                 for (i = 0; i < header->num_clauses; ++i) {
9683                                         MonoExceptionClause *clause = &header->clauses [i];
9684                                         if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
9685                                                 handler_offset = clause->handler_offset;
9686                                                 break;
9687                                         }
9688                                 }
9689
9690                                 bblock->flags |= BB_EXCEPTION_UNSAFE;
9691
9692                                 g_assert (handler_offset != -1);
9693
9694                                 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
9695                                 MONO_INST_NEW (cfg, ins, OP_RETHROW);
9696                                 ins->inst_left = load;
9697                                 MONO_ADD_INS (bblock, ins);
9698                                 sp = stack_start;
9699                                 link_bblock (cfg, bblock, end_bblock);
9700                                 start_new_bblock = 1;
9701                                 ip += 2;
9702                                 break;
9703                         }
9704                         case CEE_SIZEOF:
9705                                 CHECK_STACK_OVF (1);
9706                                 CHECK_OPSIZE (6);
9707                                 token = read32 (ip + 2);
9708                                 /* FIXXME: handle generics. */
9709                                 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
9710                                         MonoType *type = mono_type_create_from_typespec (image, token);
9711                                         token = mono_type_size (type, &ialign);
9712                                 } else {
9713                                         MonoClass *klass = mono_class_get_full (image, token, generic_context);
9714                                         CHECK_TYPELOAD (klass);
9715                                         mono_class_init (klass);
9716                                         token = mono_class_value_size (klass, &align);
9717                                 }
9718                                 NEW_ICONST (cfg, ins, token);
9719                                 *sp++= ins;
9720                                 ip += 6;
9721                                 break;
9722                         case CEE_REFANYTYPE:
9723                                 CHECK_STACK (1);
9724                                 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
9725                                 --sp;
9726                                 ins->type = STACK_MP;
9727                                 ins->inst_left = *sp;
9728                                 ins->type = STACK_VTYPE;
9729                                 ins->klass = mono_defaults.typehandle_class;
9730                                 ip += 2;
9731                                 *sp++ = ins;
9732                                 break;
9733                         case CEE_READONLY_:
9734                                 readonly = TRUE;
9735                                 ip += 2;
9736                                 break;
9737                         default:
9738                                 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
9739                         }
9740                         break;
9741                 }
9742                 default:
9743                         g_error ("opcode 0x%02x not handled", *ip);
9744                 }
9745         }
9746         if (start_new_bblock != 1)
9747                 UNVERIFIED;
9748
9749         bblock->cil_length = ip - bblock->cil_code;
9750         bblock->next_bb = end_bblock;
9751
9752         if (cfg->method == method && cfg->domainvar) {
9753                 MonoInst *store;
9754                 MonoInst *get_domain;
9755                 
9756                 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
9757                         MonoCallInst *call;
9758                         
9759                         MONO_INST_NEW_CALL (cfg, call, OP_CALL);
9760                         call->signature = helper_sig_domain_get;
9761                         call->inst.type = STACK_PTR;
9762                         call->fptr = mono_domain_get;
9763                         get_domain = (MonoInst*)call;
9764                 }
9765                 
9766                 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
9767                 MONO_ADD_INS (init_localsbb, store);
9768         }
9769
9770         if (cfg->method == method && cfg->got_var)
9771                 mono_emit_load_got_addr (cfg);
9772
9773         if (header->init_locals) {
9774                 MonoInst *store;
9775                 cfg->ip = header->code;
9776                 for (i = 0; i < header->num_locals; ++i) {
9777                         MonoType *ptype = header->locals [i];
9778                         int t = ptype->type;
9779                         if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
9780                                 t = ptype->data.klass->enum_basetype->type;
9781                         if (ptype->byref) {
9782                                 NEW_PCONST (cfg, ins, NULL);
9783                                 NEW_LOCSTORE (cfg, store, i, ins);
9784                                 MONO_ADD_INS (init_localsbb, store);
9785                         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
9786                                 NEW_ICONST (cfg, ins, 0);
9787                                 NEW_LOCSTORE (cfg, store, i, ins);
9788                                 MONO_ADD_INS (init_localsbb, store);
9789                         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
9790                                 MONO_INST_NEW (cfg, ins, OP_I8CONST);
9791                                 ins->type = STACK_I8;
9792                                 ins->inst_l = 0;
9793                                 NEW_LOCSTORE (cfg, store, i, ins);
9794                                 MONO_ADD_INS (init_localsbb, store);
9795                         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
9796 #ifdef MONO_ARCH_SOFT_FLOAT
9797                                 /* FIXME: handle init of R4 */
9798 #else
9799                                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
9800                                 ins->type = STACK_R8;
9801                                 ins->inst_p0 = (void*)&r8_0;
9802                                 NEW_LOCSTORE (cfg, store, i, ins);
9803                                 MONO_ADD_INS (init_localsbb, store);
9804 #endif
9805                         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
9806                                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (ptype))) {
9807                                 NEW_LOCLOADA (cfg, ins, i);
9808                                 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
9809                         } else {
9810                                 NEW_PCONST (cfg, ins, NULL);
9811                                 NEW_LOCSTORE (cfg, store, i, ins);
9812                                 MONO_ADD_INS (init_localsbb, store);
9813                         }
9814                 }
9815         }
9816
9817         cfg->ip = NULL;
9818
9819         /* resolve backward branches in the middle of an existing basic block */
9820         for (tmp = bb_recheck; tmp; tmp = tmp->next) {
9821                 bblock = tmp->data;
9822                 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
9823                 tblock = find_previous (cfg->cil_offset_to_bb, header->code_size, start_bblock, bblock->cil_code);
9824                 if (tblock != start_bblock) {
9825                         int l;
9826                         split_bblock (cfg, tblock, bblock);
9827                         l = bblock->cil_code - header->code;
9828                         bblock->cil_length = tblock->cil_length - l;
9829                         tblock->cil_length = l;
9830                 } else {
9831                         g_print ("recheck failed.\n");
9832                 }
9833         }
9834
9835         if (cfg->method == method) {
9836                 MonoBasicBlock *bb;
9837                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9838                         bb->region = mono_find_block_region (cfg, bb->real_offset);
9839                         if (cfg->spvars)
9840                                 mono_create_spvar_for_region (cfg, bb->region);
9841                         if (cfg->verbose_level > 2)
9842                                 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
9843                 }
9844         }
9845
9846         g_slist_free (class_inits);
9847         dont_inline = g_list_remove (dont_inline, method);
9848
9849         if (inline_costs < 0) {
9850                 char *mname;
9851
9852                 /* Method is too large */
9853                 mname = mono_method_full_name (method, TRUE);
9854                 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
9855                 cfg->exception_message = g_strdup_printf ("Method %s is too complex.", mname);
9856                 g_free (mname);
9857                 return -1;
9858         }
9859
9860         return inline_costs;
9861
9862  exception_exit:
9863         g_assert (cfg->exception_type != MONO_EXCEPTION_NONE);
9864         g_slist_free (class_inits);
9865         dont_inline = g_list_remove (dont_inline, method);
9866         return -1;
9867
9868  inline_failure:
9869         g_slist_free (class_inits);
9870         dont_inline = g_list_remove (dont_inline, method);
9871         return -1;
9872
9873  load_error:
9874         g_slist_free (class_inits);
9875         dont_inline = g_list_remove (dont_inline, method);
9876         cfg->exception_type = MONO_EXCEPTION_TYPE_LOAD;
9877         return -1;
9878
9879  unverified:
9880         g_slist_free (class_inits);
9881         dont_inline = g_list_remove (dont_inline, method);
9882         set_exception_type_from_invalid_il (cfg, method, ip);
9883         return -1;
9884 }
9885
9886 void
9887 mono_print_tree (MonoInst *tree) {
9888         int arity;
9889
9890         if (!tree)
9891                 return;
9892
9893         arity = mono_burg_arity [tree->opcode];
9894
9895         printf (" %s%s", arity?"(":"",  mono_inst_name (tree->opcode));
9896
9897         switch (tree->opcode) {
9898         case OP_ICONST:
9899                 printf ("[%d]", (int)tree->inst_c0);
9900                 break;
9901         case OP_I8CONST:
9902                 printf ("[%lld]", (long long)tree->inst_l);
9903                 break;
9904         case OP_R8CONST:
9905                 printf ("[%f]", *(double*)tree->inst_p0);
9906                 break;
9907         case OP_R4CONST:
9908                 printf ("[%f]", *(float*)tree->inst_p0);
9909                 break;
9910         case OP_ARG:
9911         case OP_LOCAL:
9912                 printf ("[%d]", (int)tree->inst_c0);
9913                 break;
9914         case OP_REGOFFSET:
9915                 if (tree->inst_offset < 0)
9916                         printf ("[-0x%x(%s)]", (int)(-tree->inst_offset), mono_arch_regname (tree->inst_basereg));
9917                 else
9918                         printf ("[0x%x(%s)]", (int)(tree->inst_offset), mono_arch_regname (tree->inst_basereg));
9919                 break;
9920         case OP_REGVAR:
9921                 printf ("[%s]", mono_arch_regname (tree->dreg));
9922                 break;
9923         case CEE_NEWARR:
9924                 printf ("[%s]",  tree->inst_newa_class->name);
9925                 mono_print_tree (tree->inst_newa_len);
9926                 break;
9927         case OP_CALL:
9928         case OP_CALLVIRT:
9929         case OP_FCALL:
9930         case OP_FCALLVIRT:
9931         case OP_LCALL:
9932         case OP_LCALLVIRT:
9933         case OP_VCALL:
9934         case OP_VCALLVIRT:
9935         case OP_VOIDCALL:
9936         case OP_VOIDCALLVIRT:
9937         case OP_TRAMPCALL_VTABLE: {
9938                 MonoCallInst *call = (MonoCallInst*)tree;
9939                 if (call->method)
9940                         printf ("[%s]", call->method->name);
9941                 else if (call->fptr) {
9942                         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
9943                         if (info)
9944                                 printf ("[%s]", info->name);
9945                 }
9946                 break;
9947         }
9948         case OP_PHI: {
9949                 int i;
9950                 printf ("[%d (", (int)tree->inst_c0);
9951                 for (i = 0; i < tree->inst_phi_args [0]; i++) {
9952                         if (i)
9953                                 printf (", ");
9954                         printf ("%d", tree->inst_phi_args [i + 1]);
9955                 }
9956                 printf (")]");
9957                 break;
9958         }
9959         case OP_RENAME:
9960         case OP_RETARG:
9961         case OP_NOP:
9962         case OP_JMP:
9963         case OP_BREAK:
9964                 break;
9965         case OP_LOAD_MEMBASE:
9966         case OP_LOADI4_MEMBASE:
9967         case OP_LOADU4_MEMBASE:
9968         case OP_LOADU1_MEMBASE:
9969         case OP_LOADI1_MEMBASE:
9970         case OP_LOADU2_MEMBASE:
9971         case OP_LOADI2_MEMBASE:
9972                 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), (int)tree->inst_offset);
9973                 break;
9974         case OP_BR:
9975         case OP_CALL_HANDLER:
9976                 printf ("[B%d]", tree->inst_target_bb->block_num);
9977                 break;
9978         case OP_SWITCH:
9979         case CEE_ISINST:
9980         case CEE_CASTCLASS:
9981         case OP_OUTARG:
9982         case OP_CALL_REG:
9983         case OP_FCALL_REG:
9984         case OP_LCALL_REG:
9985         case OP_VCALL_REG:
9986         case OP_VOIDCALL_REG:
9987                 mono_print_tree (tree->inst_left);
9988                 break;
9989         case CEE_BNE_UN:
9990         case CEE_BEQ:
9991         case CEE_BLT:
9992         case CEE_BLT_UN:
9993         case CEE_BGT:
9994         case CEE_BGT_UN:
9995         case CEE_BGE:
9996         case CEE_BGE_UN:
9997         case CEE_BLE:
9998         case CEE_BLE_UN:
9999                 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
10000                 mono_print_tree (tree->inst_left);
10001                 break;
10002         default:
10003                 if (!mono_arch_print_tree(tree, arity)) {
10004                         if (arity) {
10005                                 mono_print_tree (tree->inst_left);
10006                                 if (arity > 1)
10007                                         mono_print_tree (tree->inst_right);
10008                         }
10009                 }
10010                 break;
10011         }
10012
10013         if (arity)
10014                 printf (")");
10015 }
10016
10017 void
10018 mono_print_tree_nl (MonoInst *tree)
10019 {
10020         mono_print_tree (tree);
10021         printf ("\n");
10022 }
10023
10024 static void
10025 create_helper_signature (void)
10026 {
10027         helper_sig_domain_get = mono_create_icall_signature ("ptr");
10028         helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
10029         helper_sig_generic_class_init_trampoline = mono_create_icall_signature ("void");
10030         helper_sig_rgctx_lazy_fetch_trampoline = mono_create_icall_signature ("ptr ptr");
10031 }
10032
10033 static gconstpointer
10034 mono_icall_get_wrapper_full (MonoJitICallInfo* callinfo, gboolean do_compile)
10035 {
10036         char *name;
10037         MonoMethod *wrapper;
10038         gconstpointer trampoline;
10039         MonoDomain *domain = mono_get_root_domain ();
10040         
10041         if (callinfo->wrapper) {
10042                 return callinfo->wrapper;
10043         }
10044
10045         if (callinfo->trampoline)
10046                 return callinfo->trampoline;
10047
10048         /* 
10049          * We use the lock on the root domain instead of the JIT lock to protect 
10050          * callinfo->trampoline, since we do a lot of stuff inside the critical section.
10051          */
10052         mono_domain_lock (domain);
10053
10054         if (callinfo->trampoline) {
10055                 mono_domain_unlock (domain);
10056                 return callinfo->trampoline;
10057         }
10058
10059         name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
10060         wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
10061         g_free (name);
10062
10063         if (do_compile)
10064                 trampoline = mono_compile_method (wrapper);
10065         else
10066                 trampoline = mono_create_ftnptr (domain, mono_create_jit_trampoline_in_domain (domain, wrapper));
10067         mono_register_jit_icall_wrapper (callinfo, trampoline);
10068
10069         callinfo->trampoline = trampoline;
10070
10071         mono_domain_unlock (domain);
10072         
10073         return callinfo->trampoline;
10074 }
10075
10076 gconstpointer
10077 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
10078 {
10079         return mono_icall_get_wrapper_full (callinfo, FALSE);
10080 }
10081
10082 static void
10083 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
10084 {
10085         if (!domain_jit_info (domain)->dynamic_code_hash)
10086                 domain_jit_info (domain)->dynamic_code_hash = g_hash_table_new (NULL, NULL);
10087         g_hash_table_insert (domain_jit_info (domain)->dynamic_code_hash, method, ji);
10088 }
10089
10090 static MonoJitDynamicMethodInfo*
10091 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
10092 {
10093         MonoJitDynamicMethodInfo *res;
10094
10095         if (domain_jit_info (domain)->dynamic_code_hash)
10096                 res = g_hash_table_lookup (domain_jit_info (domain)->dynamic_code_hash, method);
10097         else
10098                 res = NULL;
10099         return res;
10100 }
10101
10102 typedef struct {
10103         MonoClass *vtype;
10104         GList *active, *inactive;
10105         GSList *slots;
10106 } StackSlotInfo;
10107
10108 static gint 
10109 compare_by_interval_start_pos_func (gconstpointer a, gconstpointer b)
10110 {
10111         MonoMethodVar *v1 = (MonoMethodVar*)a;
10112         MonoMethodVar *v2 = (MonoMethodVar*)b;
10113
10114         if (v1 == v2)
10115                 return 0;
10116         else if (v1->interval->range && v2->interval->range)
10117                 return v1->interval->range->from - v2->interval->range->from;
10118         else if (v1->interval->range)
10119                 return -1;
10120         else
10121                 return 1;
10122 }
10123
10124 #ifndef DISABLE_JIT
10125
10126 #if 0
10127 #define LSCAN_DEBUG(a) do { a; } while (0)
10128 #else
10129 #define LSCAN_DEBUG(a)
10130 #endif
10131
10132 static gint32*
10133 mono_allocate_stack_slots_full2 (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
10134 {
10135         int i, slot, offset, size;
10136         guint32 align;
10137         MonoMethodVar *vmv;
10138         MonoInst *inst;
10139         gint32 *offsets;
10140         GList *vars = NULL, *l, *unhandled;
10141         StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
10142         MonoType *t;
10143         int nvtypes;
10144
10145         LSCAN_DEBUG (printf ("Allocate Stack Slots 2 for %s:\n", mono_method_full_name (cfg->method, TRUE)));
10146
10147         scalar_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * MONO_TYPE_PINNED);
10148         vtype_stack_slots = NULL;
10149         nvtypes = 0;
10150
10151         offsets = mono_mempool_alloc (cfg->mempool, sizeof (gint32) * cfg->num_varinfo);
10152         for (i = 0; i < cfg->num_varinfo; ++i)
10153                 offsets [i] = -1;
10154
10155         for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
10156                 inst = cfg->varinfo [i];
10157                 vmv = MONO_VARINFO (cfg, i);
10158
10159                 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
10160                         continue;
10161
10162                 vars = g_list_prepend (vars, vmv);
10163         }
10164
10165         vars = g_list_sort (g_list_copy (vars), compare_by_interval_start_pos_func);
10166
10167         /* Sanity check */
10168         /*
10169         i = 0;
10170         for (unhandled = vars; unhandled; unhandled = unhandled->next) {
10171                 MonoMethodVar *current = unhandled->data;
10172
10173                 if (current->interval->range) {
10174                         g_assert (current->interval->range->from >= i);
10175                         i = current->interval->range->from;
10176                 }
10177         }
10178         */
10179
10180         offset = 0;
10181         *stack_align = 0;
10182         for (unhandled = vars; unhandled; unhandled = unhandled->next) {
10183                 MonoMethodVar *current = unhandled->data;
10184
10185                 vmv = current;
10186                 inst = cfg->varinfo [vmv->idx];
10187
10188                 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
10189                 * pinvoke wrappers when they call functions returning structures */
10190                 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
10191                         size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
10192                 else {
10193                         int ialign;
10194
10195                         size = mono_type_size (inst->inst_vtype, &ialign);
10196                         align = ialign;
10197                 }
10198
10199                 t = mono_type_get_underlying_type (inst->inst_vtype);
10200                 switch (t->type) {
10201                 case MONO_TYPE_GENERICINST:
10202                         if (!mono_type_generic_inst_is_valuetype (t)) {
10203                                 slot_info = &scalar_stack_slots [t->type];
10204                                 break;
10205                         }
10206                         /* Fall through */
10207                 case MONO_TYPE_VALUETYPE:
10208                         if (!vtype_stack_slots)
10209                                 vtype_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * 256);
10210                         for (i = 0; i < nvtypes; ++i)
10211                                 if (t->data.klass == vtype_stack_slots [i].vtype)
10212                                         break;
10213                         if (i < nvtypes)
10214                                 slot_info = &vtype_stack_slots [i];
10215                         else {
10216                                 g_assert (nvtypes < 256);
10217                                 vtype_stack_slots [nvtypes].vtype = t->data.klass;
10218                                 slot_info = &vtype_stack_slots [nvtypes];
10219                                 nvtypes ++;
10220                         }
10221                         break;
10222                 case MONO_TYPE_CLASS:
10223                 case MONO_TYPE_OBJECT:
10224                 case MONO_TYPE_ARRAY:
10225                 case MONO_TYPE_SZARRAY:
10226                 case MONO_TYPE_STRING:
10227                 case MONO_TYPE_PTR:
10228                 case MONO_TYPE_I:
10229                 case MONO_TYPE_U:
10230 #if SIZEOF_VOID_P == 4
10231                 case MONO_TYPE_I4:
10232 #else
10233                 case MONO_TYPE_I8:
10234                         /* Share non-float stack slots of the same size */
10235                         slot_info = &scalar_stack_slots [MONO_TYPE_CLASS];
10236                         break;
10237 #endif
10238                 default:
10239                         slot_info = &scalar_stack_slots [t->type];
10240                 }
10241
10242                 slot = 0xffffff;
10243                 if (cfg->comp_done & MONO_COMP_LIVENESS) {
10244                         int pos;
10245                         gboolean changed;
10246
10247                         //printf ("START  %2d %08x %08x\n",  vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
10248
10249                         if (!current->interval->range) {
10250                                 if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
10251                                         pos = ~0;
10252                                 else {
10253                                         /* Dead */
10254                                         inst->flags |= MONO_INST_IS_DEAD;
10255                                         continue;
10256                                 }
10257                         }
10258                         else
10259                                 pos = current->interval->range->from;
10260
10261                         LSCAN_DEBUG (printf ("process R%d ", inst->dreg));
10262                         if (current->interval->range)
10263                                 LSCAN_DEBUG (mono_linterval_print (current->interval));
10264                         LSCAN_DEBUG (printf ("\n"));
10265
10266                         /* Check for intervals in active which expired or inactive */
10267                         changed = TRUE;
10268                         /* FIXME: Optimize this */
10269                         while (changed) {
10270                                 changed = FALSE;
10271                                 for (l = slot_info->active; l != NULL; l = l->next) {
10272                                         MonoMethodVar *v = (MonoMethodVar*)l->data;
10273
10274                                         if (v->interval->last_range->to < pos) {
10275                                                 slot_info->active = g_list_delete_link (slot_info->active, l);
10276                                                 slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
10277                                                 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg->varinfo [v->idx]->dreg, offsets [v->idx]));
10278                                                 changed = TRUE;
10279                                                 break;
10280                                         }
10281                                         else if (!mono_linterval_covers (v->interval, pos)) {
10282                                                 slot_info->inactive = g_list_append (slot_info->inactive, v);
10283                                                 slot_info->active = g_list_delete_link (slot_info->active, l);
10284                                                 LSCAN_DEBUG (printf ("Interval R%d became inactive\n", cfg->varinfo [v->idx]->dreg));
10285                                                 changed = TRUE;
10286                                                 break;
10287                                         }
10288                                 }
10289                         }
10290
10291                         /* Check for intervals in inactive which expired or active */
10292                         changed = TRUE;
10293                         /* FIXME: Optimize this */
10294                         while (changed) {
10295                                 changed = FALSE;
10296                                 for (l = slot_info->inactive; l != NULL; l = l->next) {
10297                                         MonoMethodVar *v = (MonoMethodVar*)l->data;
10298
10299                                         if (v->interval->last_range->to < pos) {
10300                                                 slot_info->inactive = g_list_delete_link (slot_info->inactive, l);
10301                                                 // FIXME: Enabling this seems to cause impossible to debug crashes
10302                                                 //slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [v->idx]));
10303                                                 LSCAN_DEBUG (printf ("Interval R%d has expired, adding 0x%x to slots\n", cfg->varinfo [v->idx]->dreg, offsets [v->idx]));
10304                                                 changed = TRUE;
10305                                                 break;
10306                                         }
10307                                         else if (mono_linterval_covers (v->interval, pos)) {
10308                                                 slot_info->active = g_list_append (slot_info->active, v);
10309                                                 slot_info->inactive = g_list_delete_link (slot_info->inactive, l);
10310                                                 LSCAN_DEBUG (printf ("\tInterval R%d became active\n", cfg->varinfo [v->idx]->dreg));
10311                                                 changed = TRUE;
10312                                                 break;
10313                                         }
10314                                 }
10315                         }
10316
10317                         /* 
10318                          * This also handles the case when the variable is used in an
10319                          * exception region, as liveness info is not computed there.
10320                          */
10321                         /* 
10322                          * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
10323                          * opcodes.
10324                          */
10325                         if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
10326                                 if (slot_info->slots) {
10327                                         slot = GPOINTER_TO_INT (slot_info->slots->data);
10328
10329                                         slot_info->slots = slot_info->slots->next;
10330                                 }
10331
10332                                 /* FIXME: We might want to consider the inactive intervals as well if slot_info->slots is empty */
10333
10334                                 slot_info->active = mono_varlist_insert_sorted (cfg, slot_info->active, vmv, TRUE);
10335                         }
10336                 }
10337
10338 #if 0
10339                 {
10340                         static int count = 0;
10341                         count ++;
10342
10343                         if (count == atoi (getenv ("COUNT3")))
10344                                 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
10345                         if (count > atoi (getenv ("COUNT3")))
10346                                 slot = 0xffffff;
10347                         else {
10348                                 mono_print_tree_nl (inst);
10349                                 }
10350                 }
10351 #endif
10352
10353                 LSCAN_DEBUG (printf ("R%d %s -> 0x%x\n", inst->dreg, mono_type_full_name (t), slot));
10354
10355                 if (slot == 0xffffff) {
10356                         /*
10357                          * Allways allocate valuetypes to sizeof (gpointer) to allow more
10358                          * efficient copying (and to work around the fact that OP_MEMCPY
10359                          * and OP_MEMSET ignores alignment).
10360                          */
10361                         if (MONO_TYPE_ISSTRUCT (t))
10362                                 align = sizeof (gpointer);
10363
10364                         if (backward) {
10365                                 offset += size;
10366                                 offset += align - 1;
10367                                 offset &= ~(align - 1);
10368                                 slot = offset;
10369                         }
10370                         else {
10371                                 offset += align - 1;
10372                                 offset &= ~(align - 1);
10373                                 slot = offset;
10374                                 offset += size;
10375                         }
10376
10377                         if (*stack_align == 0)
10378                                 *stack_align = align;
10379                 }
10380
10381                 offsets [vmv->idx] = slot;
10382         }
10383         g_list_free (vars);
10384         for (i = 0; i < MONO_TYPE_PINNED; ++i) {
10385                 if (scalar_stack_slots [i].active)
10386                         g_list_free (scalar_stack_slots [i].active);
10387         }
10388         for (i = 0; i < nvtypes; ++i) {
10389                 if (vtype_stack_slots [i].active)
10390                         g_list_free (vtype_stack_slots [i].active);
10391         }
10392
10393         mono_jit_stats.locals_stack_size += offset;
10394
10395         *stack_size = offset;
10396         return offsets;
10397 }
10398
10399 /*
10400  *  mono_allocate_stack_slots_full:
10401  *
10402  *  Allocate stack slots for all non register allocated variables using a
10403  * linear scan algorithm.
10404  * Returns: an array of stack offsets.
10405  * STACK_SIZE is set to the amount of stack space needed.
10406  * STACK_ALIGN is set to the alignment needed by the locals area.
10407  */
10408 gint32*
10409 mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
10410 {
10411         int i, slot, offset, size;
10412         guint32 align;
10413         MonoMethodVar *vmv;
10414         MonoInst *inst;
10415         gint32 *offsets;
10416         GList *vars = NULL, *l;
10417         StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
10418         MonoType *t;
10419         int nvtypes;
10420
10421         if ((cfg->num_varinfo > 0) && MONO_VARINFO (cfg, 0)->interval)
10422                 return mono_allocate_stack_slots_full2 (cfg, backward, stack_size, stack_align);
10423
10424         scalar_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * MONO_TYPE_PINNED);
10425         vtype_stack_slots = NULL;
10426         nvtypes = 0;
10427
10428         offsets = mono_mempool_alloc (cfg->mempool, sizeof (gint32) * cfg->num_varinfo);
10429         for (i = 0; i < cfg->num_varinfo; ++i)
10430                 offsets [i] = -1;
10431
10432         for (i = cfg->locals_start; i < cfg->num_varinfo; i++) {
10433                 inst = cfg->varinfo [i];
10434                 vmv = MONO_VARINFO (cfg, i);
10435
10436                 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
10437                         continue;
10438
10439                 vars = g_list_prepend (vars, vmv);
10440         }
10441
10442         vars = mono_varlist_sort (cfg, vars, 0);
10443         offset = 0;
10444         *stack_align = sizeof (gpointer);
10445         for (l = vars; l; l = l->next) {
10446                 vmv = l->data;
10447                 inst = cfg->varinfo [vmv->idx];
10448
10449                 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
10450                 * pinvoke wrappers when they call functions returning structures */
10451                 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
10452                         size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
10453                 else {
10454                         int ialign;
10455
10456                         size = mono_type_size (inst->inst_vtype, &ialign);
10457                         align = ialign;
10458                 }
10459
10460                 t = mono_type_get_underlying_type (inst->inst_vtype);
10461                 if (t->byref) {
10462                         slot_info = &scalar_stack_slots [MONO_TYPE_I];
10463                 } else {
10464                         switch (t->type) {
10465                         case MONO_TYPE_GENERICINST:
10466                                 if (!mono_type_generic_inst_is_valuetype (t)) {
10467                                         slot_info = &scalar_stack_slots [t->type];
10468                                         break;
10469                                 }
10470                                 /* Fall through */
10471                         case MONO_TYPE_VALUETYPE:
10472                                 if (!vtype_stack_slots)
10473                                         vtype_stack_slots = mono_mempool_alloc0 (cfg->mempool, sizeof (StackSlotInfo) * 256);
10474                                 for (i = 0; i < nvtypes; ++i)
10475                                         if (t->data.klass == vtype_stack_slots [i].vtype)
10476                                                 break;
10477                                 if (i < nvtypes)
10478                                         slot_info = &vtype_stack_slots [i];
10479                                 else {
10480                                         g_assert (nvtypes < 256);
10481                                         vtype_stack_slots [nvtypes].vtype = t->data.klass;
10482                                         slot_info = &vtype_stack_slots [nvtypes];
10483                                         nvtypes ++;
10484                                 }
10485                                 break;
10486                         case MONO_TYPE_CLASS:
10487                         case MONO_TYPE_OBJECT:
10488                         case MONO_TYPE_ARRAY:
10489                         case MONO_TYPE_SZARRAY:
10490                         case MONO_TYPE_STRING:
10491                         case MONO_TYPE_PTR:
10492                         case MONO_TYPE_I:
10493                         case MONO_TYPE_U:
10494 #if SIZEOF_VOID_P == 4
10495                         case MONO_TYPE_I4:
10496 #else
10497                         case MONO_TYPE_I8:
10498 #endif
10499                                 /* Share non-float stack slots of the same size */
10500                                 slot_info = &scalar_stack_slots [MONO_TYPE_CLASS];
10501                                 break;
10502                         default:
10503                                 slot_info = &scalar_stack_slots [t->type];
10504                         }
10505                 }
10506
10507                 slot = 0xffffff;
10508                 if (cfg->comp_done & MONO_COMP_LIVENESS) {
10509                         //printf ("START  %2d %08x %08x\n",  vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
10510                         
10511                         /* expire old intervals in active */
10512                         while (slot_info->active) {
10513                                 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
10514
10515                                 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
10516                                         break;
10517
10518                                 //printf ("EXPIR  %2d %08x %08x C%d R%d\n", amv->idx, amv->range.first_use.abs_pos, amv->range.last_use.abs_pos, amv->spill_costs, amv->reg);
10519
10520                                 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
10521                                 slot_info->slots = g_slist_prepend_mempool (cfg->mempool, slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
10522                         }
10523
10524                         /* 
10525                          * This also handles the case when the variable is used in an
10526                          * exception region, as liveness info is not computed there.
10527                          */
10528                         /* 
10529                          * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
10530                          * opcodes.
10531                          */
10532                         if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
10533                                 if (slot_info->slots) {
10534                                         slot = GPOINTER_TO_INT (slot_info->slots->data);
10535
10536                                         slot_info->slots = slot_info->slots->next;
10537                                 }
10538
10539                                 slot_info->active = mono_varlist_insert_sorted (cfg, slot_info->active, vmv, TRUE);
10540                         }
10541                 }
10542
10543                 {
10544                         static int count = 0;
10545                         count ++;
10546
10547                         /*
10548                         if (count == atoi (getenv ("COUNT")))
10549                                 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
10550                         if (count > atoi (getenv ("COUNT")))
10551                                 slot = 0xffffff;
10552                         else {
10553                                 mono_print_tree_nl (inst);
10554                                 }
10555                         */
10556                 }
10557
10558                 if (cfg->disable_reuse_stack_slots)
10559                         slot = 0xffffff;
10560
10561                 if (slot == 0xffffff) {
10562                         /*
10563                          * Allways allocate valuetypes to sizeof (gpointer) to allow more
10564                          * efficient copying (and to work around the fact that OP_MEMCPY
10565                          * and OP_MEMSET ignores alignment).
10566                          */
10567                         if (MONO_TYPE_ISSTRUCT (t))
10568                                 align = sizeof (gpointer);
10569
10570                         if (backward) {
10571                                 offset += size;
10572                                 offset += align - 1;
10573                                 offset &= ~(align - 1);
10574                                 slot = offset;
10575                         }
10576                         else {
10577                                 offset += align - 1;
10578                                 offset &= ~(align - 1);
10579                                 slot = offset;
10580                                 offset += size;
10581                         }
10582
10583                         *stack_align = MAX (*stack_align, align);
10584                 }
10585
10586                 offsets [vmv->idx] = slot;
10587         }
10588         g_list_free (vars);
10589         for (i = 0; i < MONO_TYPE_PINNED; ++i) {
10590                 if (scalar_stack_slots [i].active)
10591                         g_list_free (scalar_stack_slots [i].active);
10592         }
10593         for (i = 0; i < nvtypes; ++i) {
10594                 if (vtype_stack_slots [i].active)
10595                         g_list_free (vtype_stack_slots [i].active);
10596         }
10597
10598         mono_jit_stats.locals_stack_size += offset;
10599
10600         *stack_size = offset;
10601         return offsets;
10602 }
10603
10604 gint32*
10605 mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_align)
10606 {
10607         return mono_allocate_stack_slots_full (m, TRUE, stack_size, stack_align);
10608 }
10609
10610 #else
10611
10612 gint32*
10613 mono_allocate_stack_slots_full (MonoCompile *cfg, gboolean backward, guint32 *stack_size, guint32 *stack_align)
10614 {
10615         g_assert_not_reached ();
10616         return NULL;
10617 }
10618
10619 #endif /* DISABLE_JIT */
10620
10621 void
10622 mono_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, gboolean no_throw)
10623 {
10624         MonoJitICallInfo *info;
10625         MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
10626
10627         if (!emul_opcode_map)
10628                 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
10629
10630         g_assert (!sig->hasthis);
10631         g_assert (sig->param_count < 3);
10632
10633         info = mono_register_jit_icall (func, name, sig, no_throw);
10634
10635         emul_opcode_map [opcode] = info;
10636 }
10637
10638 static void
10639 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
10640 {
10641         MonoMethodSignature *sig;
10642
10643         if (sigstr)
10644                 sig = mono_create_icall_signature (sigstr);
10645         else
10646                 sig = NULL;
10647
10648         mono_register_jit_icall (func, name, sig, save);
10649 }
10650
10651 static void
10652 decompose_foreach (MonoInst *tree, gpointer data) 
10653 {
10654         static MonoJitICallInfo *newarr_info = NULL;
10655         static MonoJitICallInfo *newarr_specific_info = NULL;
10656         MonoJitICallInfo *info;
10657         int i;
10658
10659         switch (tree->opcode) {
10660         case CEE_NEWARR: {
10661                 MonoCompile *cfg = data;
10662                 MonoInst *iargs [3];
10663
10664                 if (!newarr_info) {
10665                         newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
10666                         g_assert (newarr_info);
10667                         newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
10668                         g_assert (newarr_specific_info);
10669                 }
10670
10671                 if (cfg->opt & MONO_OPT_SHARED) {
10672                         NEW_DOMAINCONST (cfg, iargs [0]);
10673                         NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
10674                         iargs [2] = tree->inst_newa_len;
10675
10676                         info = newarr_info;
10677                 }
10678                 else {
10679                         MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
10680
10681                         g_assert (vtable);
10682                         NEW_VTABLECONST (cfg, iargs [0], vtable);
10683                         iargs [1] = tree->inst_newa_len;
10684
10685                         info = newarr_specific_info;
10686                 }
10687
10688                 mono_emulate_opcode (cfg, tree, iargs, info);
10689
10690                 /* Need to decompose arguments after the the opcode is decomposed */
10691                 for (i = 0; i < info->sig->param_count; ++i)
10692                         dec_foreach (iargs [i], cfg);
10693                 break;
10694         }
10695 #ifdef MONO_ARCH_SOFT_FLOAT
10696         case OP_FBEQ:
10697         case OP_FBGE:
10698         case OP_FBGT:
10699         case OP_FBLE:
10700         case OP_FBLT:
10701         case OP_FBNE_UN:
10702         case OP_FBGE_UN:
10703         case OP_FBGT_UN:
10704         case OP_FBLE_UN:
10705         case OP_FBLT_UN: {
10706                 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
10707                         MonoCompile *cfg = data;
10708                         MonoInst *iargs [2];
10709                 
10710                         iargs [0] = tree->inst_i0;
10711                         iargs [1] = tree->inst_i1;
10712                 
10713                         mono_emulate_opcode (cfg, tree, iargs, info);
10714
10715                         dec_foreach (iargs [0], cfg);
10716                         dec_foreach (iargs [1], cfg);
10717                         break;
10718                 } else {
10719                         g_assert_not_reached ();
10720                 }
10721                 break;
10722         }
10723         case OP_FCEQ:
10724         case OP_FCGT:
10725         case OP_FCGT_UN:
10726         case OP_FCLT:
10727         case OP_FCLT_UN: {
10728                 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
10729                         MonoCompile *cfg = data;
10730                         MonoInst *iargs [2];
10731
10732                         /* the args are in the compare opcode ... */
10733                         iargs [0] = tree->inst_i0;
10734                         iargs [1] = tree->inst_i1;
10735                 
10736                         mono_emulate_opcode (cfg, tree, iargs, info);
10737
10738                         dec_foreach (iargs [0], cfg);
10739                         dec_foreach (iargs [1], cfg);
10740                         break;
10741                 } else {
10742                         g_assert_not_reached ();
10743                 }
10744                 break;
10745         }
10746 #endif
10747
10748         default:
10749                 break;
10750         }
10751 }
10752
10753 void
10754 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
10755
10756         switch (mono_burg_arity [tree->opcode]) {
10757         case 0: break;
10758         case 1: 
10759                 mono_inst_foreach (tree->inst_left, func, data);
10760                 break;
10761         case 2: 
10762                 mono_inst_foreach (tree->inst_left, func, data);
10763                 mono_inst_foreach (tree->inst_right, func, data);
10764                 break;
10765         default:
10766                 g_assert_not_reached ();
10767         }
10768         func (tree, data);
10769 }
10770
10771 G_GNUC_UNUSED
10772 static void
10773 mono_print_bb_code (MonoBasicBlock *bb)
10774 {
10775         MonoInst *c;
10776
10777         MONO_BB_FOR_EACH_INS (bb, c) {
10778                 mono_print_tree (c);
10779                 g_print ("\n");
10780         }
10781 }
10782
10783 static void
10784 print_dfn (MonoCompile *cfg) {
10785         int i, j;
10786         char *code;
10787         MonoBasicBlock *bb;
10788         MonoInst *c;
10789
10790         g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
10791
10792         for (i = 0; i < cfg->num_bblocks; ++i) {
10793                 bb = cfg->bblocks [i];
10794                 /*if (bb->cil_code) {
10795                         char* code1, *code2;
10796                         code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
10797                         if (bb->last_ins->cil_code)
10798                                 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
10799                         else
10800                                 code2 = g_strdup ("");
10801
10802                         code1 [strlen (code1) - 1] = 0;
10803                         code = g_strdup_printf ("%s -> %s", code1, code2);
10804                         g_free (code1);
10805                         g_free (code2);
10806                 } else*/
10807                         code = g_strdup ("\n");
10808                 g_print ("\nBB%d (%d) (len: %d): %s", bb->block_num, i, bb->cil_length, code);
10809                 MONO_BB_FOR_EACH_INS (bb, c) {
10810                         if (cfg->new_ir) {
10811                                 mono_print_ins_index (-1, c);
10812                         } else {
10813                                 mono_print_tree (c);
10814                                 g_print ("\n");
10815                         }
10816                 }
10817
10818                 g_print ("\tprev:");
10819                 for (j = 0; j < bb->in_count; ++j) {
10820                         g_print (" BB%d", bb->in_bb [j]->block_num);
10821                 }
10822                 g_print ("\t\tsucc:");
10823                 for (j = 0; j < bb->out_count; ++j) {
10824                         g_print (" BB%d", bb->out_bb [j]->block_num);
10825                 }
10826                 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
10827
10828                 if (bb->idom)
10829                         g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
10830
10831                 if (bb->dominators)
10832                         mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
10833                 if (bb->dfrontier)
10834                         mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
10835                 g_free (code);
10836         }
10837
10838         g_print ("\n");
10839 }
10840
10841 void
10842 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
10843 {
10844         MONO_ADD_INS (bb, inst);
10845 }
10846
10847 void
10848 mono_bblock_insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert)
10849 {
10850         if (ins == NULL) {
10851                 ins = bb->code;
10852                 bb->code = ins_to_insert;
10853
10854                 /* Link with next */
10855                 ins_to_insert->next = ins;
10856                 if (ins)
10857                         ins->prev = ins_to_insert;
10858
10859                 if (bb->last_ins == NULL)
10860                         bb->last_ins = ins_to_insert;
10861         } else {
10862                 /* Link with next */
10863                 ins_to_insert->next = ins->next;
10864                 if (ins->next)
10865                         ins->next->prev = ins_to_insert;
10866
10867                 /* Link with previous */
10868                 ins->next = ins_to_insert;
10869                 ins_to_insert->prev = ins;
10870
10871                 if (bb->last_ins == ins)
10872                         bb->last_ins = ins_to_insert;
10873         }
10874 }
10875
10876 void
10877 mono_bblock_insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst *ins_to_insert)
10878 {
10879         if (ins == NULL) {
10880                 NOT_IMPLEMENTED;
10881                 ins = bb->code;
10882                 bb->code = ins_to_insert;
10883                 ins_to_insert->next = ins;
10884                 if (bb->last_ins == NULL)
10885                         bb->last_ins = ins_to_insert;
10886         } else {
10887                 /* Link with previous */
10888                 if (ins->prev)
10889                         ins->prev->next = ins_to_insert;
10890                 ins_to_insert->prev = ins->prev;
10891
10892                 /* Link with next */
10893                 ins->prev = ins_to_insert;
10894                 ins_to_insert->next = ins;
10895
10896                 if (bb->code == ins)
10897                         bb->code = ins_to_insert;
10898         }
10899 }
10900
10901 /*
10902  * mono_verify_bblock:
10903  *
10904  *   Verify that the next and prev pointers are consistent inside the instructions in BB.
10905  */
10906 void
10907 mono_verify_bblock (MonoBasicBlock *bb)
10908 {
10909         MonoInst *ins, *prev;
10910
10911         prev = NULL;
10912         for (ins = bb->code; ins; ins = ins->next) {
10913                 g_assert (ins->prev == prev);
10914                 prev = ins;
10915         }
10916         if (bb->last_ins)
10917                 g_assert (!bb->last_ins->next);
10918 }
10919
10920 /*
10921  * mono_verify_cfg:
10922  *
10923  *   Perform consistency checks on the JIT data structures and the IR
10924  */
10925 void
10926 mono_verify_cfg (MonoCompile *cfg)
10927 {
10928         MonoBasicBlock *bb;
10929
10930         for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
10931                 mono_verify_bblock (bb);
10932 }
10933
10934 void
10935 mono_destroy_compile (MonoCompile *cfg)
10936 {
10937         //mono_mempool_stats (cfg->mempool);
10938         mono_free_loop_info (cfg);
10939         if (cfg->rs)
10940                 mono_regstate_free (cfg->rs);
10941         if (cfg->spvars)
10942                 g_hash_table_destroy (cfg->spvars);
10943         if (cfg->exvars)
10944                 g_hash_table_destroy (cfg->exvars);
10945         mono_mempool_destroy (cfg->mempool);
10946         g_list_free (cfg->ldstr_list);
10947         g_hash_table_destroy (cfg->token_info_hash);
10948         if (cfg->abs_patches)
10949                 g_hash_table_destroy (cfg->abs_patches);
10950
10951         g_free (cfg->varinfo);
10952         g_free (cfg->vars);
10953         g_free (cfg->exception_message);
10954         g_free (cfg);
10955 }
10956
10957 #ifdef HAVE_KW_THREAD
10958 static __thread gpointer mono_lmf_addr MONO_TLS_FAST;
10959 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
10960 /* 
10961  * When this is defined, the current lmf is stored in this tls variable instead of in 
10962  * jit_tls->lmf.
10963  */
10964 static __thread gpointer mono_lmf MONO_TLS_FAST;
10965 #endif
10966 #endif
10967
10968 guint32
10969 mono_get_jit_tls_key (void)
10970 {
10971         return mono_jit_tls_id;
10972 }
10973
10974 gint32
10975 mono_get_jit_tls_offset (void)
10976 {
10977 #ifdef HAVE_KW_THREAD
10978         int offset;
10979         MONO_THREAD_VAR_OFFSET (mono_jit_tls, offset);
10980         return offset;
10981 #else
10982         return -1;
10983 #endif
10984 }
10985
10986 gint32
10987 mono_get_lmf_tls_offset (void)
10988 {
10989 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
10990         int offset;
10991         MONO_THREAD_VAR_OFFSET(mono_lmf,offset);
10992         return offset;
10993 #else
10994         return -1;
10995 #endif
10996 }
10997
10998 gint32
10999 mono_get_lmf_addr_tls_offset (void)
11000 {
11001         int offset;
11002         MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
11003         return offset;
11004 }
11005
11006 MonoLMF *
11007 mono_get_lmf (void)
11008 {
11009 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
11010         return mono_lmf;
11011 #else
11012         MonoJitTlsData *jit_tls;
11013
11014         if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
11015                 return jit_tls->lmf;
11016
11017         g_assert_not_reached ();
11018         return NULL;
11019 #endif
11020 }
11021
11022 MonoLMF **
11023 mono_get_lmf_addr (void)
11024 {
11025 #ifdef HAVE_KW_THREAD
11026         return mono_lmf_addr;
11027 #else
11028         MonoJitTlsData *jit_tls;
11029
11030         if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
11031                 return &jit_tls->lmf;
11032
11033         g_assert_not_reached ();
11034         return NULL;
11035 #endif
11036 }
11037
11038 /* Called by native->managed wrappers */
11039 void
11040 mono_jit_thread_attach (MonoDomain *domain)
11041 {
11042 #ifdef HAVE_KW_THREAD
11043         if (!mono_lmf_addr) {
11044                 mono_thread_attach (domain);
11045         }
11046 #else
11047         if (!TlsGetValue (mono_jit_tls_id))
11048                 mono_thread_attach (domain);
11049 #endif
11050         if (mono_domain_get () != domain)
11051                 mono_domain_set (domain, TRUE);
11052 }       
11053
11054 /**
11055  * mono_thread_abort:
11056  * @obj: exception object
11057  *
11058  * abort the thread, print exception information and stack trace
11059  */
11060 static void
11061 mono_thread_abort (MonoObject *obj)
11062 {
11063         /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
11064         
11065         /* handle_remove should be eventually called for this thread, too
11066         g_free (jit_tls);*/
11067
11068         if ((mono_runtime_unhandled_exception_policy_get () == MONO_UNHANLED_POLICY_LEGACY) ||
11069                         (obj->vtable->klass == mono_defaults.threadabortexception_class)) {
11070                 mono_thread_exit ();
11071         } else {
11072                 exit (mono_environment_exitcode_get ());
11073         }
11074 }
11075
11076 static void*
11077 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
11078 {
11079         MonoJitTlsData *jit_tls;
11080         MonoLMF *lmf;
11081
11082         jit_tls = TlsGetValue (mono_jit_tls_id);
11083         if (jit_tls)
11084                 return jit_tls;
11085
11086         jit_tls = g_new0 (MonoJitTlsData, 1);
11087
11088         TlsSetValue (mono_jit_tls_id, jit_tls);
11089
11090 #ifdef HAVE_KW_THREAD
11091         mono_jit_tls = jit_tls;
11092 #endif
11093
11094         jit_tls->abort_func = abort_func;
11095         jit_tls->end_of_stack = stack_start;
11096
11097         lmf = g_new0 (MonoLMF, 1);
11098 #ifdef MONO_ARCH_INIT_TOP_LMF_ENTRY
11099         MONO_ARCH_INIT_TOP_LMF_ENTRY (lmf);
11100 #else
11101         lmf->ebp = -1;
11102 #endif
11103
11104         jit_tls->first_lmf = lmf;
11105
11106 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
11107         /* jit_tls->lmf is unused */
11108         mono_lmf = lmf;
11109         mono_lmf_addr = &mono_lmf;
11110 #else
11111 #if defined(HAVE_KW_THREAD)
11112         mono_lmf_addr = &jit_tls->lmf;  
11113 #endif
11114
11115         jit_tls->lmf = lmf;
11116 #endif
11117
11118         mono_arch_setup_jit_tls_data (jit_tls);
11119         mono_setup_altstack (jit_tls);
11120
11121         return jit_tls;
11122 }
11123
11124 static void
11125 mono_thread_start_cb (gsize tid, gpointer stack_start, gpointer func)
11126 {
11127         MonoThread *thread;
11128         void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
11129         thread = mono_thread_current ();
11130         mono_debugger_thread_created (tid, thread, jit_tls);
11131         if (thread)
11132                 thread->jit_data = jit_tls;
11133 }
11134
11135 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
11136
11137 static void
11138 mono_thread_abort_dummy (MonoObject *obj)
11139 {
11140   if (mono_thread_attach_aborted_cb)
11141     mono_thread_attach_aborted_cb (obj);
11142   else
11143     mono_thread_abort (obj);
11144 }
11145
11146 static void
11147 mono_thread_attach_cb (gsize tid, gpointer stack_start)
11148 {
11149         MonoThread *thread;
11150         void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
11151         thread = mono_thread_current ();
11152         mono_debugger_thread_created (tid, thread, (MonoJitTlsData *) jit_tls);
11153         if (thread)
11154                 thread->jit_data = jit_tls;
11155         if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
11156                 setup_stat_profiler ();
11157 }
11158
11159 static void
11160 mini_thread_cleanup (MonoThread *thread)
11161 {
11162         MonoJitTlsData *jit_tls = thread->jit_data;
11163
11164         if (jit_tls) {
11165                 mono_debugger_thread_cleanup (jit_tls);
11166                 mono_arch_free_jit_tls_data (jit_tls);
11167
11168                 mono_free_altstack (jit_tls);
11169                 g_free (jit_tls->first_lmf);
11170                 g_free (jit_tls);
11171                 thread->jit_data = NULL;
11172                 TlsSetValue (mono_jit_tls_id, NULL);
11173         }
11174 }
11175
11176 static MonoInst*
11177 mono_create_tls_get (MonoCompile *cfg, int offset)
11178 {
11179 #ifdef MONO_ARCH_HAVE_TLS_GET
11180         MonoInst* ins;
11181         
11182         if (offset == -1)
11183                 return NULL;
11184         
11185         MONO_INST_NEW (cfg, ins, OP_TLS_GET);
11186         ins->dreg = cfg->new_ir ? mono_alloc_preg (cfg) : mono_regstate_next_int (cfg->rs);
11187         ins->inst_offset = offset;
11188         return ins;
11189 #else
11190         return NULL;
11191 #endif
11192 }
11193
11194 MonoInst*
11195 mono_get_jit_tls_intrinsic (MonoCompile *cfg)
11196 {
11197         return mono_create_tls_get (cfg, mono_get_jit_tls_offset ());
11198 }
11199
11200 void
11201 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
11202 {
11203         MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
11204
11205         ji->ip.i = ip;
11206         ji->type = type;
11207         ji->data.target = target;
11208         ji->next = cfg->patch_info;
11209
11210         cfg->patch_info = ji;
11211 }
11212
11213 MonoJumpInfo *
11214 mono_patch_info_list_prepend (MonoJumpInfo *list, int ip, MonoJumpInfoType type, gconstpointer target)
11215 {
11216         MonoJumpInfo *ji = g_new0 (MonoJumpInfo, 1);
11217
11218         ji->ip.i = ip;
11219         ji->type = type;
11220         ji->data.target = target;
11221         ji->next = list;
11222
11223         return ji;
11224 }
11225
11226 void
11227 mono_remove_patch_info (MonoCompile *cfg, int ip)
11228 {
11229         MonoJumpInfo **ji = &cfg->patch_info;
11230
11231         while (*ji) {
11232                 if ((*ji)->ip.i == ip)
11233                         *ji = (*ji)->next;
11234                 else
11235                         ji = &((*ji)->next);
11236         }
11237 }
11238
11239 /**
11240  * mono_patch_info_dup_mp:
11241  *
11242  * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
11243  */
11244 MonoJumpInfo*
11245 mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
11246 {
11247         MonoJumpInfo *res = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
11248         memcpy (res, patch_info, sizeof (MonoJumpInfo));
11249
11250         switch (patch_info->type) {
11251         case MONO_PATCH_INFO_RVA:
11252         case MONO_PATCH_INFO_LDSTR:
11253         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
11254         case MONO_PATCH_INFO_LDTOKEN:
11255         case MONO_PATCH_INFO_DECLSEC:
11256                 res->data.token = mono_mempool_alloc (mp, sizeof (MonoJumpInfoToken));
11257                 memcpy (res->data.token, patch_info->data.token, sizeof (MonoJumpInfoToken));
11258                 break;
11259         case MONO_PATCH_INFO_SWITCH:
11260                 res->data.table = mono_mempool_alloc (mp, sizeof (MonoJumpInfoBBTable));
11261                 memcpy (res->data.table, patch_info->data.table, sizeof (MonoJumpInfoBBTable));
11262                 break;
11263         case MONO_PATCH_INFO_RGCTX_FETCH:
11264                 res->data.rgctx_entry = mono_mempool_alloc (mp, sizeof (MonoJumpInfoRgctxEntry));
11265                 memcpy (res->data.rgctx_entry, patch_info->data.rgctx_entry, sizeof (MonoJumpInfoRgctxEntry));
11266                 res->data.rgctx_entry->data = mono_patch_info_dup_mp (mp, res->data.rgctx_entry->data);
11267                 break;
11268         default:
11269                 break;
11270         }
11271
11272         return res;
11273 }
11274
11275 guint
11276 mono_patch_info_hash (gconstpointer data)
11277 {
11278         const MonoJumpInfo *ji = (MonoJumpInfo*)data;
11279
11280         switch (ji->type) {
11281         case MONO_PATCH_INFO_RVA:
11282         case MONO_PATCH_INFO_LDSTR:
11283         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
11284         case MONO_PATCH_INFO_LDTOKEN:
11285         case MONO_PATCH_INFO_DECLSEC:
11286                 return (ji->type << 8) | ji->data.token->token;
11287         case MONO_PATCH_INFO_VTABLE:
11288         case MONO_PATCH_INFO_CLASS:
11289         case MONO_PATCH_INFO_IID:
11290         case MONO_PATCH_INFO_ADJUSTED_IID:
11291         case MONO_PATCH_INFO_CLASS_INIT:
11292         case MONO_PATCH_INFO_METHODCONST:
11293         case MONO_PATCH_INFO_METHOD:
11294         case MONO_PATCH_INFO_METHOD_JUMP:
11295         case MONO_PATCH_INFO_IMAGE:
11296         case MONO_PATCH_INFO_INTERNAL_METHOD:
11297         case MONO_PATCH_INFO_JIT_ICALL_ADDR:
11298         case MONO_PATCH_INFO_FIELD:
11299         case MONO_PATCH_INFO_SFLDA:
11300                 return (ji->type << 8) | (gssize)ji->data.target;
11301         default:
11302                 return (ji->type << 8);
11303         }
11304 }
11305
11306 /* 
11307  * mono_patch_info_equal:
11308  * 
11309  * This might fail to recognize equivalent patches, i.e. floats, so its only
11310  * usable in those cases where this is not a problem, i.e. sharing GOT slots
11311  * in AOT.
11312  */
11313 gint
11314 mono_patch_info_equal (gconstpointer ka, gconstpointer kb)
11315 {
11316         const MonoJumpInfo *ji1 = (MonoJumpInfo*)ka;
11317         const MonoJumpInfo *ji2 = (MonoJumpInfo*)kb;
11318
11319         if (ji1->type != ji2->type)
11320                 return 0;
11321
11322         switch (ji1->type) {
11323         case MONO_PATCH_INFO_RVA:
11324         case MONO_PATCH_INFO_LDSTR:
11325         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
11326         case MONO_PATCH_INFO_LDTOKEN:
11327         case MONO_PATCH_INFO_DECLSEC:
11328                 if ((ji1->data.token->image != ji2->data.token->image) ||
11329                         (ji1->data.token->token != ji2->data.token->token) || 
11330                         (ji1->data.token->has_context != ji2->data.token->has_context) ||
11331                         (ji1->data.token->context.class_inst != ji2->data.token->context.class_inst) ||
11332                         (ji1->data.token->context.method_inst != ji2->data.token->context.method_inst))
11333                         return 0;
11334                 break;
11335         default:
11336                 if (ji1->data.target != ji2->data.target)
11337                         return 0;
11338                 break;
11339         }
11340
11341         return 1;
11342 }
11343
11344 gpointer
11345 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
11346 {
11347         unsigned char *ip = patch_info->ip.i + code;
11348         gconstpointer target = NULL;
11349
11350         switch (patch_info->type) {
11351         case MONO_PATCH_INFO_BB:
11352                 g_assert (patch_info->data.bb->native_offset);
11353                 target = patch_info->data.bb->native_offset + code;
11354                 break;
11355         case MONO_PATCH_INFO_ABS:
11356                 target = patch_info->data.target;
11357                 break;
11358         case MONO_PATCH_INFO_LABEL:
11359                 target = patch_info->data.inst->inst_c0 + code;
11360                 break;
11361         case MONO_PATCH_INFO_IP:
11362                 target = ip;
11363                 break;
11364         case MONO_PATCH_INFO_METHOD_REL:
11365                 target = code + patch_info->data.offset;
11366                 break;
11367         case MONO_PATCH_INFO_INTERNAL_METHOD: {
11368                 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
11369                 if (!mi) {
11370                         g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
11371                         g_assert_not_reached ();
11372                 }
11373                 target = mono_icall_get_wrapper (mi);
11374                 break;
11375         }
11376         case MONO_PATCH_INFO_METHOD_JUMP:
11377                 target = mono_create_jump_trampoline (domain, patch_info->data.method, FALSE);
11378                 break;
11379         case MONO_PATCH_INFO_METHOD:
11380                 if (patch_info->data.method == method) {
11381                         target = code;
11382                 } else {
11383                         /* get the trampoline to the method from the domain */
11384                         if (method && method->wrapper_type == MONO_WRAPPER_STATIC_RGCTX_INVOKE) {
11385                                 target = mono_create_jit_trampoline_in_domain (mono_domain_get (),
11386                                         patch_info->data.method);
11387                         } else {
11388                                 target = mono_create_jit_trampoline (patch_info->data.method);
11389                         }
11390                 }
11391                 break;
11392         case MONO_PATCH_INFO_SWITCH: {
11393                 gpointer *jump_table;
11394                 int i;
11395
11396                 if (method && method->dynamic) {
11397                         jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
11398                 } else {
11399                         mono_domain_lock (domain);
11400                         if (mono_aot_only)
11401                                 jump_table = mono_domain_alloc (domain, sizeof (gpointer) * patch_info->data.table->table_size);
11402                         else
11403                                 jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
11404                         mono_domain_unlock (domain);
11405                 }
11406
11407                 for (i = 0; i < patch_info->data.table->table_size; i++)
11408                         jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
11409                 target = jump_table;
11410                 break;
11411         }
11412         case MONO_PATCH_INFO_METHODCONST:
11413         case MONO_PATCH_INFO_CLASS:
11414         case MONO_PATCH_INFO_IMAGE:
11415         case MONO_PATCH_INFO_FIELD:
11416                 target = patch_info->data.target;
11417                 break;
11418         case MONO_PATCH_INFO_IID:
11419                 mono_class_init (patch_info->data.klass);
11420                 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
11421                 break;
11422         case MONO_PATCH_INFO_ADJUSTED_IID:
11423                 mono_class_init (patch_info->data.klass);
11424                 target = GINT_TO_POINTER ((int)(-((patch_info->data.klass->interface_id + 1) * SIZEOF_VOID_P)));
11425                 break;
11426         case MONO_PATCH_INFO_VTABLE:
11427                 target = mono_class_vtable (domain, patch_info->data.klass);
11428                 g_assert (target);
11429                 break;
11430         case MONO_PATCH_INFO_CLASS_INIT: {
11431                 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.klass);
11432
11433                 g_assert (vtable);
11434                 target = mono_create_class_init_trampoline (vtable);
11435                 break;
11436         }
11437         case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
11438                 target = mono_create_delegate_trampoline (patch_info->data.klass);
11439                 break;
11440         case MONO_PATCH_INFO_SFLDA: {
11441                 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
11442
11443                 g_assert (vtable);
11444                 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
11445                         /* Done by the generated code */
11446                         ;
11447                 else {
11448                         if (run_cctors)
11449                                 mono_runtime_class_init (vtable);
11450                 }
11451                 target = (char*)vtable->data + patch_info->data.field->offset;
11452                 break;
11453         }
11454         case MONO_PATCH_INFO_RVA:
11455                 target = mono_image_rva_map (patch_info->data.token->image, patch_info->data.token->token);
11456                 break;
11457         case MONO_PATCH_INFO_R4:
11458         case MONO_PATCH_INFO_R8:
11459                 target = patch_info->data.target;
11460                 break;
11461         case MONO_PATCH_INFO_EXC_NAME:
11462                 target = patch_info->data.name;
11463                 break;
11464         case MONO_PATCH_INFO_LDSTR:
11465                 target =
11466                         mono_ldstr (domain, patch_info->data.token->image, 
11467                                                 mono_metadata_token_index (patch_info->data.token->token));
11468                 break;
11469         case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
11470                 gpointer handle;
11471                 MonoClass *handle_class;
11472
11473                 handle = mono_ldtoken (patch_info->data.token->image, 
11474                                                            patch_info->data.token->token, &handle_class, patch_info->data.token->has_context ? &patch_info->data.token->context : NULL);
11475                 mono_class_init (handle_class);
11476                 mono_class_init (mono_class_from_mono_type (handle));
11477
11478                 target =
11479                         mono_type_get_object (domain, handle);
11480                 break;
11481         }
11482         case MONO_PATCH_INFO_LDTOKEN: {
11483                 gpointer handle;
11484                 MonoClass *handle_class;
11485                 
11486                 handle = mono_ldtoken (patch_info->data.token->image,
11487                                        patch_info->data.token->token, &handle_class, NULL);
11488                 mono_class_init (handle_class);
11489                 
11490                 target = handle;
11491                 break;
11492         }
11493         case MONO_PATCH_INFO_DECLSEC:
11494                 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
11495                 break;
11496         case MONO_PATCH_INFO_ICALL_ADDR:
11497                 target = mono_lookup_internal_call (patch_info->data.method);
11498                 /* run_cctors == 0 -> AOT */
11499                 if (!target && run_cctors)
11500                         g_error ("Unregistered icall '%s'\n", mono_method_full_name (patch_info->data.method, TRUE));
11501                 break;
11502         case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
11503                 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
11504                 if (!mi) {
11505                         g_warning ("unknown MONO_PATCH_INFO_JIT_ICALL_ADDR %s", patch_info->data.name);
11506                         g_assert_not_reached ();
11507                 }
11508                 target = mi->func;
11509                 break;
11510         }
11511         case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
11512                 target = mono_thread_interruption_request_flag ();
11513                 break;
11514         case MONO_PATCH_INFO_METHOD_RGCTX:
11515                 target = mono_method_lookup_rgctx (mono_class_vtable (domain, patch_info->data.method->klass), mini_method_get_context (patch_info->data.method)->method_inst);
11516                 break;
11517         case MONO_PATCH_INFO_BB_OVF:
11518         case MONO_PATCH_INFO_EXC_OVF:
11519         case MONO_PATCH_INFO_GOT_OFFSET:
11520         case MONO_PATCH_INFO_NONE:
11521                 break;
11522         case MONO_PATCH_INFO_RGCTX_FETCH: {
11523                 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
11524                 guint32 slot = -1;
11525
11526                 switch (entry->data->type) {
11527                 case MONO_PATCH_INFO_CLASS:
11528                         slot = mono_method_lookup_or_register_other_info (entry->method, entry->in_mrgctx, &entry->data->data.klass->byval_arg, entry->info_type, mono_method_get_context (entry->method));
11529                         break;
11530                 case MONO_PATCH_INFO_METHOD:
11531                 case MONO_PATCH_INFO_METHODCONST:
11532                         slot = mono_method_lookup_or_register_other_info (entry->method, entry->in_mrgctx, entry->data->data.method, entry->info_type, mono_method_get_context (entry->method));
11533                         break;
11534                 case MONO_PATCH_INFO_FIELD:
11535                         slot = mono_method_lookup_or_register_other_info (entry->method, entry->in_mrgctx, entry->data->data.field, entry->info_type, mono_method_get_context (entry->method));
11536                         break;
11537                 default:
11538                         g_assert_not_reached ();
11539                         break;
11540                 }
11541
11542                 target = mono_create_rgctx_lazy_fetch_trampoline (slot);
11543                 break;
11544         }
11545         case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
11546                 target = mono_create_generic_class_init_trampoline ();
11547                 break;
11548         default:
11549                 g_assert_not_reached ();
11550         }
11551
11552         return (gpointer)target;
11553 }
11554
11555 static void
11556 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
11557         MonoJitICallInfo *info;
11558
11559         decompose_foreach (tree, cfg);
11560
11561         switch (mono_burg_arity [tree->opcode]) {
11562         case 0: break;
11563         case 1: 
11564                 dec_foreach (tree->inst_left, cfg);
11565
11566                 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
11567                         MonoInst *iargs [2];
11568                 
11569                         iargs [0] = tree->inst_left;
11570
11571                         mono_emulate_opcode (cfg, tree, iargs, info);
11572                         return;
11573                 }
11574
11575                 break;
11576         case 2:
11577 #ifdef MONO_ARCH_BIGMUL_INTRINS
11578                 if (tree->opcode == OP_LMUL
11579                                 && (cfg->opt & MONO_OPT_INTRINS)
11580                                 && (tree->inst_left->opcode == CEE_CONV_I8 
11581                                         || tree->inst_left->opcode == CEE_CONV_U8)
11582                                 && tree->inst_left->inst_left->type == STACK_I4
11583                                 && (tree->inst_right->opcode == CEE_CONV_I8 
11584                                         || tree->inst_right->opcode == CEE_CONV_U8)
11585                                 && tree->inst_right->inst_left->type == STACK_I4
11586                                 && tree->inst_left->opcode == tree->inst_right->opcode) {
11587                         tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
11588                         tree->inst_left = tree->inst_left->inst_left;
11589                         tree->inst_right = tree->inst_right->inst_left;
11590                         dec_foreach (tree, cfg);
11591                 } else 
11592 #endif
11593                         if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
11594                         MonoInst *iargs [2];
11595                 
11596                         iargs [0] = tree->inst_i0;
11597                         iargs [1] = tree->inst_i1;
11598                 
11599                         mono_emulate_opcode (cfg, tree, iargs, info);
11600
11601                         dec_foreach (iargs [0], cfg);
11602                         dec_foreach (iargs [1], cfg);
11603                         return;
11604                 } else {
11605                         dec_foreach (tree->inst_left, cfg);
11606                         dec_foreach (tree->inst_right, cfg);
11607                 }
11608                 break;
11609         default:
11610                 g_assert_not_reached ();
11611         }
11612 }
11613
11614 static void
11615 decompose_pass (MonoCompile *cfg) {
11616         MonoBasicBlock *bb;
11617
11618         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11619                 MonoInst *tree;
11620                 cfg->cbb = bb;
11621                 cfg->prev_ins = NULL;
11622                 MONO_BB_FOR_EACH_INS (cfg->cbb, tree) {
11623                         dec_foreach (tree, cfg);
11624                         cfg->prev_ins = tree;
11625                 }
11626         }
11627 }
11628
11629 static void
11630 mono_compile_create_vars (MonoCompile *cfg)
11631 {
11632         MonoMethodSignature *sig;
11633         MonoMethodHeader *header;
11634         int i;
11635
11636         header = mono_method_get_header (cfg->method);
11637
11638         sig = mono_method_signature (cfg->method);
11639         
11640         if (!MONO_TYPE_IS_VOID (sig->ret)) {
11641                 if (cfg->new_ir) {
11642                         cfg->ret = mono_compile_create_var (cfg, sig->ret, OP_ARG);
11643                         /* Inhibit optimizations */
11644                         cfg->ret->flags |= MONO_INST_VOLATILE;
11645                 } else {
11646                         cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
11647                         cfg->ret->opcode = OP_RETARG;
11648                         cfg->ret->inst_vtype = sig->ret;
11649                         cfg->ret->klass = mono_class_from_mono_type (sig->ret);
11650                 }
11651         }
11652         if (cfg->verbose_level > 2)
11653                 g_print ("creating vars\n");
11654
11655         cfg->args = mono_mempool_alloc0 (cfg->mempool, (sig->param_count + sig->hasthis) * sizeof (MonoInst*));
11656
11657         if (sig->hasthis)
11658                 cfg->args [0] = mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
11659
11660         for (i = 0; i < sig->param_count; ++i) {
11661                 cfg->args [i + sig->hasthis] = mono_compile_create_var (cfg, sig->params [i], OP_ARG);
11662                 if (sig->params [i]->byref) {
11663                         if (!cfg->new_ir) cfg->disable_ssa = TRUE;
11664                 }
11665         }
11666
11667         if (cfg->new_ir && cfg->verbose_level > 2) {
11668                 if (cfg->ret) {
11669                         printf ("\treturn : ");
11670                         mono_print_ins (cfg->ret);
11671                 }
11672
11673                 if (sig->hasthis) {
11674                         printf ("\tthis: ");
11675                         mono_print_ins (cfg->args [0]);
11676                 }
11677
11678                 for (i = 0; i < sig->param_count; ++i) {
11679                         printf ("\targ [%d]: ", i);
11680                         mono_print_ins (cfg->args [i + sig->hasthis]);
11681                 }
11682         }
11683
11684         cfg->locals_start = cfg->num_varinfo;
11685         cfg->locals = mono_mempool_alloc0 (cfg->mempool, header->num_locals * sizeof (MonoInst*));
11686
11687         if (cfg->verbose_level > 2)
11688                 g_print ("creating locals\n");
11689
11690         for (i = 0; i < header->num_locals; ++i)
11691                 cfg->locals [i] = mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
11692
11693         if (cfg->verbose_level > 2)
11694                 g_print ("locals done\n");
11695
11696         mono_arch_create_vars (cfg);
11697 }
11698
11699 void
11700 mono_print_code (MonoCompile *cfg, const char* msg)
11701 {
11702         MonoBasicBlock *bb;
11703         
11704         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11705                 MonoInst *tree = bb->code;      
11706
11707                 if (cfg->new_ir) {
11708                         mono_print_bb (bb, msg);
11709                 } else {
11710                         if (!tree)
11711                                 continue;
11712                         
11713                         g_print ("%s CODE BLOCK %d (nesting %d):\n", msg, bb->block_num, bb->nesting);
11714
11715                         MONO_BB_FOR_EACH_INS (bb, tree) {
11716                                 mono_print_tree (tree);
11717                                 g_print ("\n");
11718                         }
11719                 }
11720         }
11721 }
11722
11723 #ifndef DISABLE_JIT
11724
11725 extern const char * const mono_burg_rule_string [];
11726
11727 static void
11728 emit_state (MonoCompile *cfg, MBState *state, int goal)
11729 {
11730         MBState *kids [10];
11731         int ern = mono_burg_rule (state, goal);
11732         const guint16 *nts = mono_burg_nts_data + mono_burg_nts [ern];
11733
11734         //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
11735         switch (goal) {
11736         case MB_NTERM_reg:
11737                 //if (state->reg2)
11738                 //      state->reg1 = state->reg2; /* chain rule */
11739                 //else
11740 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
11741                 if (!state->reg1)
11742 #endif
11743                         state->reg1 = mono_regstate_next_int (cfg->rs);
11744                 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
11745                 break;
11746         case MB_NTERM_lreg:
11747                 state->reg1 = mono_regstate_next_int (cfg->rs);
11748                 state->reg2 = mono_regstate_next_int (cfg->rs);
11749                 break;
11750         case MB_NTERM_freg:
11751 #ifdef MONO_ARCH_SOFT_FLOAT
11752                 state->reg1 = mono_regstate_next_int (cfg->rs);
11753                 state->reg2 = mono_regstate_next_int (cfg->rs);
11754 #else
11755                 state->reg1 = mono_regstate_next_float (cfg->rs);
11756 #endif
11757                 break;
11758         default:
11759 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
11760                 /*
11761                  * Enabling this might cause bugs to surface in the local register
11762                  * allocators on some architectures like x86.
11763                  */
11764                 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
11765                         /* Do not optimize away reg-reg moves */
11766                         if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
11767                                 state->right->reg1 = state->left->tree->dreg;
11768                         }
11769                 }
11770 #endif
11771
11772                 /* do nothing */
11773                 break;
11774         }
11775         if (nts [0]) {
11776                 mono_burg_kids (state, ern, kids);
11777
11778                 emit_state (cfg, kids [0], nts [0]);
11779                 if (nts [1]) {
11780                         emit_state (cfg, kids [1], nts [1]);
11781                         if (nts [2]) {
11782                                 emit_state (cfg, kids [2], nts [2]);
11783                                 if (nts [3]) {
11784                                         g_assert (!nts [4]);
11785                                         emit_state (cfg, kids [3], nts [3]);
11786                                 }
11787                         }
11788                 }
11789         }
11790
11791 //      g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
11792         mono_burg_emit (ern, state, state->tree, cfg);
11793 }
11794
11795 #define DEBUG_SELECTION
11796
11797 static void 
11798 mini_select_instructions (MonoCompile *cfg)
11799 {
11800         MonoBasicBlock *bb;
11801         
11802         cfg->state_pool = mono_mempool_new ();
11803         cfg->rs = mono_regstate_new ();
11804
11805         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11806                 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
11807                     bb->last_ins->inst_false_bb && bb->next_bb != bb->last_ins->inst_false_bb) {
11808
11809                         /* we are careful when inverting, since bugs like #59580
11810                          * could show up when dealing with NaNs.
11811                          */
11812                         if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
11813                                 MonoBasicBlock *tmp =  bb->last_ins->inst_true_bb;
11814                                 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
11815                                 bb->last_ins->inst_false_bb = tmp;
11816
11817                                 bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
11818                         } else {                        
11819                                 MonoInst *ins;
11820
11821                                 MONO_INST_NEW (cfg, ins, OP_BR);
11822                                 ins->inst_target_bb = bb->last_ins->inst_false_bb;
11823                                 MONO_ADD_INS (bb, ins);
11824                         }
11825                 }
11826         }
11827
11828 #ifdef DEBUG_SELECTION
11829         if (cfg->verbose_level >= 4) {
11830                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11831                         MonoInst *tree;
11832                         g_print ("DUMP BLOCK %d:\n", bb->block_num);
11833                         MONO_BB_FOR_EACH_INS (bb, tree) {
11834                                 mono_print_tree (tree);
11835                                 g_print ("\n");
11836                         }
11837                 }
11838         }
11839 #endif
11840
11841         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11842                 MonoInst *tree = bb->code, *next;       
11843                 MBState *mbstate;
11844
11845                 if (!tree)
11846                         continue;
11847                 bb->code = NULL;
11848                 bb->last_ins = NULL;
11849                 
11850                 cfg->cbb = bb;
11851                 mono_regstate_reset (cfg->rs);
11852
11853 #ifdef DEBUG_SELECTION
11854                 if (cfg->verbose_level >= 3)
11855                         g_print ("LABEL BLOCK %d:\n", bb->block_num);
11856 #endif
11857                 for (; tree; tree = next) {
11858                         next = tree->next;
11859 #ifdef DEBUG_SELECTION
11860                         if (cfg->verbose_level >= 3) {
11861                                 mono_print_tree (tree);
11862                                 g_print ("\n");
11863                         }
11864 #endif
11865
11866                         cfg->ip = tree->cil_code;
11867                         if (!(mbstate = mono_burg_label (tree, cfg))) {
11868                                 g_warning ("unable to label tree %p", tree);
11869                                 mono_print_tree (tree);
11870                                 g_print ("\n");                         
11871                                 g_assert_not_reached ();
11872                         }
11873                         emit_state (cfg, mbstate, MB_NTERM_stmt);
11874                 }
11875                 bb->max_vreg = cfg->rs->next_vreg;
11876
11877                 if (bb->last_ins)
11878                         bb->last_ins->next = NULL;
11879
11880                 mono_mempool_empty (cfg->state_pool); 
11881         }
11882         mono_mempool_destroy (cfg->state_pool); 
11883
11884         cfg->ip = NULL;
11885 }
11886
11887 /*
11888  * mono_normalize_opcodes:
11889  *
11890  *   Replace CEE_ and OP_ opcodes with the corresponding OP_I or OP_L opcodes.
11891  */
11892
11893 static gint16 *remap_table;
11894
11895 #if SIZEOF_VOID_P == 8
11896 #define REMAP_OPCODE(opcode) OP_L ## opcode
11897 #else
11898 #define REMAP_OPCODE(opcode) OP_I ## opcode
11899 #endif
11900
11901 static G_GNUC_UNUSED void
11902 mono_normalize_opcodes (MonoCompile *cfg, MonoBasicBlock *bb)
11903 {
11904         MonoInst *ins;
11905
11906         if (!remap_table) {
11907                 remap_table = g_new0 (gint16, OP_LAST);
11908
11909 #if SIZEOF_VOID_P == 8
11910                 remap_table [CEE_CONV_U8] = OP_ZEXT_I4;
11911                 remap_table [CEE_CONV_U] = OP_ZEXT_I4;
11912                 remap_table [CEE_CONV_I8] = OP_SEXT_I4;
11913                 remap_table [CEE_CONV_I] = OP_SEXT_I4;
11914                 remap_table [CEE_CONV_OVF_U4] = OP_LCONV_TO_OVF_U4;
11915                 remap_table [CEE_CONV_OVF_I4_UN] = OP_LCONV_TO_OVF_I4_UN;
11916 #else
11917 #endif
11918                 remap_table [CEE_CONV_R4] = OP_ICONV_TO_R4;
11919                 remap_table [CEE_CONV_R8] = OP_ICONV_TO_R8;
11920                 remap_table [CEE_CONV_I4] = OP_MOVE;
11921                 remap_table [CEE_CONV_U4] = OP_MOVE;
11922                 remap_table [CEE_CONV_I1] = REMAP_OPCODE (CONV_TO_I1);
11923                 remap_table [CEE_CONV_I2] = REMAP_OPCODE (CONV_TO_I2);
11924                 remap_table [CEE_CONV_U1] = REMAP_OPCODE (CONV_TO_U1);
11925                 remap_table [CEE_CONV_U2] = REMAP_OPCODE (CONV_TO_U2);
11926                 remap_table [CEE_CONV_R_UN] = REMAP_OPCODE (CONV_TO_R_UN);
11927                 remap_table [CEE_ADD] = REMAP_OPCODE (ADD);
11928                 remap_table [CEE_SUB] = REMAP_OPCODE (SUB);
11929                 remap_table [CEE_MUL] = REMAP_OPCODE (MUL);
11930                 remap_table [CEE_DIV] = REMAP_OPCODE (DIV);
11931                 remap_table [CEE_REM] = REMAP_OPCODE (REM);
11932                 remap_table [CEE_DIV_UN] = REMAP_OPCODE (DIV_UN);
11933                 remap_table [CEE_REM_UN] = REMAP_OPCODE (REM_UN);
11934                 remap_table [CEE_AND] = REMAP_OPCODE (AND);
11935                 remap_table [CEE_OR] = REMAP_OPCODE (OR);
11936                 remap_table [CEE_XOR] = REMAP_OPCODE (XOR);
11937                 remap_table [CEE_SHL] = REMAP_OPCODE (SHL);
11938                 remap_table [CEE_SHR] = REMAP_OPCODE (SHR);
11939                 remap_table [CEE_SHR_UN] = REMAP_OPCODE (SHR_UN);
11940                 remap_table [CEE_NOT] = REMAP_OPCODE (NOT);
11941                 remap_table [CEE_NEG] = REMAP_OPCODE (NEG);
11942                 remap_table [CEE_CALL] = OP_CALL;
11943                 remap_table [CEE_BEQ] = REMAP_OPCODE (BEQ);
11944                 remap_table [CEE_BNE_UN] = REMAP_OPCODE (BNE_UN);
11945                 remap_table [CEE_BLT] = REMAP_OPCODE (BLT);
11946                 remap_table [CEE_BLT_UN] = REMAP_OPCODE (BLT_UN);
11947                 remap_table [CEE_BGT] = REMAP_OPCODE (BGT);
11948                 remap_table [CEE_BGT_UN] = REMAP_OPCODE (BGT_UN);
11949                 remap_table [CEE_BGE] = REMAP_OPCODE (BGE);
11950                 remap_table [CEE_BGE_UN] = REMAP_OPCODE (BGE_UN);
11951                 remap_table [CEE_BLE] = REMAP_OPCODE (BLE);
11952                 remap_table [CEE_BLE_UN] = REMAP_OPCODE (BLE_UN);
11953                 remap_table [CEE_ADD_OVF] = REMAP_OPCODE (ADD_OVF);
11954                 remap_table [CEE_ADD_OVF_UN] = REMAP_OPCODE (ADD_OVF_UN);
11955                 remap_table [CEE_SUB_OVF] = REMAP_OPCODE (SUB_OVF);
11956                 remap_table [CEE_SUB_OVF_UN] = REMAP_OPCODE (SUB_OVF_UN);
11957                 remap_table [CEE_MUL_OVF] = REMAP_OPCODE (MUL_OVF);
11958                 remap_table [CEE_MUL_OVF_UN] = REMAP_OPCODE (MUL_OVF_UN);
11959         }
11960
11961         MONO_BB_FOR_EACH_INS (bb, ins) {
11962                 int remapped = remap_table [ins->opcode];
11963                 if (remapped)
11964                         ins->opcode = remapped;
11965         }
11966 }
11967
11968 void
11969 mono_codegen (MonoCompile *cfg)
11970 {
11971         MonoJumpInfo *patch_info;
11972         MonoBasicBlock *bb;
11973         int i, max_epilog_size;
11974         guint8 *code;
11975
11976         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
11977                 cfg->spill_count = 0;
11978                 /* we reuse dfn here */
11979                 /* bb->dfn = bb_count++; */
11980 #ifdef MONO_ARCH_ENABLE_NORMALIZE_OPCODES
11981                 if (!cfg->new_ir)
11982                         mono_normalize_opcodes (cfg, bb);
11983 #endif
11984
11985                 mono_arch_lowering_pass (cfg, bb);
11986
11987                 if (cfg->opt & MONO_OPT_PEEPHOLE)
11988                         mono_arch_peephole_pass_1 (cfg, bb);
11989
11990                 if (!cfg->globalra)
11991                         mono_local_regalloc (cfg, bb);
11992
11993                 if (cfg->opt & MONO_OPT_PEEPHOLE)
11994                         mono_arch_peephole_pass_2 (cfg, bb);
11995         }
11996
11997         if (cfg->prof_options & MONO_PROFILE_COVERAGE)
11998                 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
11999
12000         code = mono_arch_emit_prolog (cfg);
12001
12002         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
12003                 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
12004
12005         cfg->code_len = code - cfg->native_code;
12006         cfg->prolog_end = cfg->code_len;
12007
12008         mono_debug_open_method (cfg);
12009
12010         /* emit code all basic blocks */
12011         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12012                 bb->native_offset = cfg->code_len;
12013                 //if ((bb == cfg->bb_entry) || !(bb->region == -1 && !bb->dfn))
12014                         mono_arch_output_basic_block (cfg, bb);
12015
12016                 if (bb == cfg->bb_exit) {
12017                         cfg->epilog_begin = cfg->code_len;
12018
12019                         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
12020                                 code = cfg->native_code + cfg->code_len;
12021                                 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
12022                                 cfg->code_len = code - cfg->native_code;
12023                                 g_assert (cfg->code_len < cfg->code_size);
12024                         }
12025
12026                         mono_arch_emit_epilog (cfg);
12027                 }
12028         }
12029
12030         mono_arch_emit_exceptions (cfg);
12031
12032         max_epilog_size = 0;
12033
12034         code = cfg->native_code + cfg->code_len;
12035
12036         /* we always allocate code in cfg->domain->code_mp to increase locality */
12037         cfg->code_size = cfg->code_len + max_epilog_size;
12038         /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
12039
12040         if (cfg->method->dynamic) {
12041                 guint unwindlen = 0;
12042 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
12043                 unwindlen = mono_arch_unwindinfo_get_size (cfg->arch.unwindinfo);
12044 #endif
12045                 /* Allocate the code into a separate memory pool so it can be freed */
12046                 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
12047                 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
12048                 mono_domain_lock (cfg->domain);
12049                 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
12050                 mono_domain_unlock (cfg->domain);
12051
12052                 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size + unwindlen);
12053         } else {
12054                 guint unwindlen = 0;
12055 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
12056                 unwindlen = mono_arch_unwindinfo_get_size (cfg->arch.unwindinfo);
12057 #endif
12058                 mono_domain_lock (cfg->domain);
12059                 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size + unwindlen);
12060                 mono_domain_unlock (cfg->domain);
12061         }
12062
12063         memcpy (code, cfg->native_code, cfg->code_len);
12064         g_free (cfg->native_code);
12065         cfg->native_code = code;
12066         code = cfg->native_code + cfg->code_len;
12067   
12068         /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
12069         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
12070                 switch (patch_info->type) {
12071                 case MONO_PATCH_INFO_ABS: {
12072                         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
12073
12074                         /*
12075                          * Change patches of type MONO_PATCH_INFO_ABS into patches describing the 
12076                          * absolute address.
12077                          */
12078                         if (info) {
12079                                 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
12080                                 // FIXME: CLEAN UP THIS MESS.
12081                                 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && 
12082                                         strstr (cfg->method->name, info->name)) {
12083                                         /*
12084                                          * This is an icall wrapper, and this is a call to the
12085                                          * wrapped function.
12086                                          */
12087                                         if (cfg->compile_aot) {
12088                                                 patch_info->type = MONO_PATCH_INFO_JIT_ICALL_ADDR;
12089                                                 patch_info->data.name = info->name;
12090                                         }
12091                                 } else {
12092                                         /* for these array methods we currently register the same function pointer
12093                                          * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
12094                                          * will return the incorrect one depending on the order they are registered.
12095                                          * See tests/test-arr.cs
12096                                          */
12097                                         if (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL) {
12098                                                 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
12099                                                 patch_info->data.name = info->name;
12100                                         }
12101                                 }
12102                         }
12103                         
12104                         if (patch_info->type == MONO_PATCH_INFO_ABS && !cfg->new_ir) {
12105                                 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
12106                                 if (vtable) {
12107                                         patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
12108                                         patch_info->data.klass = vtable->klass;
12109                                 }
12110                         }
12111
12112                         if (patch_info->type == MONO_PATCH_INFO_ABS) {
12113                                 MonoClass *klass = mono_find_delegate_trampoline_by_addr (patch_info->data.target);
12114                                 if (klass) {
12115                                         patch_info->type = MONO_PATCH_INFO_DELEGATE_TRAMPOLINE;
12116                                         patch_info->data.klass = klass;
12117                                 }
12118                         }
12119
12120                         if (patch_info->type == MONO_PATCH_INFO_ABS) {
12121                                 if (cfg->abs_patches) {
12122                                         MonoJumpInfo *abs_ji = g_hash_table_lookup (cfg->abs_patches, patch_info->data.target);
12123                                         if (abs_ji) {
12124                                                 patch_info->type = abs_ji->type;
12125                                                 patch_info->data.target = abs_ji->data.target;
12126                                         }
12127                                 }
12128                         }
12129
12130                         break;
12131                 }
12132                 case MONO_PATCH_INFO_SWITCH: {
12133                         gpointer *table;
12134                         if (cfg->method->dynamic) {
12135                                 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
12136                         } else {
12137                                 mono_domain_lock (cfg->domain);
12138                                 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
12139                                 mono_domain_unlock (cfg->domain);
12140                         }
12141
12142                         if (!cfg->compile_aot && !cfg->new_ir)
12143                                 /* In the aot case, the patch already points to the correct location */
12144                                 patch_info->ip.i = patch_info->ip.label->inst_c0;
12145                         for (i = 0; i < patch_info->data.table->table_size; i++) {
12146                                 /* Might be NULL if the switch is eliminated */
12147                                 if (patch_info->data.table->table [i]) {
12148                                         g_assert (patch_info->data.table->table [i]->native_offset);
12149                                         table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
12150                                 } else {
12151                                         table [i] = NULL;
12152                                 }
12153                         }
12154                         patch_info->data.table->table = (MonoBasicBlock**)table;
12155                         break;
12156                 }
12157                 case MONO_PATCH_INFO_METHOD_JUMP: {
12158                         GSList *list;
12159                         MonoDomain *domain = cfg->domain;
12160                         unsigned char *ip = cfg->native_code + patch_info->ip.i;
12161
12162                         mono_domain_lock (domain);
12163                         if (!domain_jit_info (domain)->jump_target_hash)
12164                                 domain_jit_info (domain)->jump_target_hash = g_hash_table_new (NULL, NULL);
12165                         list = g_hash_table_lookup (domain_jit_info (domain)->jump_target_hash, patch_info->data.method);
12166                         list = g_slist_prepend (list, ip);
12167                         g_hash_table_insert (domain_jit_info (domain)->jump_target_hash, patch_info->data.method, list);
12168                         mono_domain_unlock (domain);
12169                         break;
12170                 }
12171                 default:
12172                         /* do nothing */
12173                         break;
12174                 }
12175         }
12176
12177 #ifdef VALGRIND_JIT_REGISTER_MAP
12178 if (valgrind_register){
12179                 char* nm = mono_method_full_name (cfg->method, TRUE);
12180                 VALGRIND_JIT_REGISTER_MAP (nm, cfg->native_code, cfg->native_code + cfg->code_len);
12181                 g_free (nm);
12182         }
12183 #endif
12184  
12185         if (cfg->verbose_level > 0) {
12186                 char* nm = mono_method_full_name (cfg->method, TRUE);
12187                 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n", 
12188                                  nm, 
12189                                  cfg->native_code, cfg->native_code + cfg->code_len, cfg->code_len, cfg->domain->friendly_name);
12190                 g_free (nm);
12191         }
12192
12193         {
12194                 gboolean is_generic = FALSE;
12195
12196                 if (cfg->method->is_inflated || mono_method_get_generic_container (cfg->method) ||
12197                                 cfg->method->klass->generic_container || cfg->method->klass->generic_class) {
12198                         is_generic = TRUE;
12199                 }
12200
12201                 if (cfg->generic_sharing_context)
12202                         g_assert (is_generic);
12203         }
12204
12205 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
12206         mono_arch_save_unwind_info (cfg);
12207 #endif
12208         
12209         mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
12210
12211         if (cfg->method->dynamic) {
12212                 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
12213         } else {
12214                 mono_domain_lock (cfg->domain);
12215                 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
12216                 mono_domain_unlock (cfg->domain);
12217         }
12218         
12219         mono_arch_flush_icache (cfg->native_code, cfg->code_len);
12220
12221         mono_debug_close_method (cfg);
12222 #ifdef MONO_ARCH_HAVE_UNWIND_TABLE
12223         mono_arch_unwindinfo_install_unwind_info (&cfg->arch.unwindinfo, cfg->native_code, cfg->code_len);
12224 #endif
12225 }
12226
12227 static MonoGenericInst*
12228 get_object_generic_inst (int type_argc)
12229 {
12230         MonoType **type_argv;
12231         int i;
12232
12233         type_argv = alloca (sizeof (MonoType*) * type_argc);
12234
12235         for (i = 0; i < type_argc; ++i)
12236                 type_argv [i] = &mono_defaults.object_class->byval_arg;
12237
12238         return mono_metadata_get_generic_inst (type_argc, type_argv);
12239 }
12240
12241 /*
12242  * mini_method_compile:
12243  * @method: the method to compile
12244  * @opts: the optimization flags to use
12245  * @domain: the domain where the method will be compiled in
12246  * @run_cctors: whether we should run type ctors if possible
12247  * @compile_aot: whether this is an AOT compilation
12248  * @parts: debug flag
12249  *
12250  * Returns: a MonoCompile* pointer. Caller must check the exception_type
12251  * field in the returned struct to see if compilation succeded.
12252  */
12253 MonoCompile*
12254 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
12255 {
12256         MonoMethodHeader *header;
12257         guint8 *ip;
12258         MonoCompile *cfg;
12259         MonoJitInfo *jinfo;
12260         int dfn, i, code_size_ratio;
12261         gboolean deadce_has_run = FALSE;
12262         gboolean try_generic_shared;
12263         MonoMethod *method_to_compile, *method_to_register;
12264         int generic_info_size;
12265
12266         mono_jit_stats.methods_compiled++;
12267         if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
12268                 mono_profiler_method_jit (method);
12269         if (MONO_PROBE_METHOD_COMPILE_BEGIN_ENABLED ())
12270                 MONO_PROBE_METHOD_COMPILE_BEGIN (method);
12271  
12272         if (compile_aot)
12273                 /* We are passed the original generic method definition */
12274                 try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
12275                         (opts & MONO_OPT_GSHARED) && (method->is_generic || method->klass->generic_container);
12276         else
12277                 try_generic_shared = mono_class_generic_sharing_enabled (method->klass) &&
12278                         (opts & MONO_OPT_GSHARED) && mono_method_is_generic_sharable_impl (method, FALSE);
12279
12280         if (opts & MONO_OPT_GSHARED) {
12281                 if (try_generic_shared)
12282                         mono_stats.generics_sharable_methods++;
12283                 else if (mono_method_is_generic_impl (method))
12284                         mono_stats.generics_unsharable_methods++;
12285         }
12286
12287  restart_compile:
12288         if (try_generic_shared) {
12289                 MonoMethod *declaring_method;
12290                 MonoGenericContext *shared_context;
12291
12292                 if (compile_aot) {
12293                         declaring_method = method;
12294                 } else {
12295                         declaring_method = mono_method_get_declaring_generic_method (method);
12296                         if (method->klass->generic_class)
12297                                 g_assert (method->klass->generic_class->container_class == declaring_method->klass);
12298                         else
12299                                 g_assert (method->klass == declaring_method->klass);
12300                 }
12301
12302                 if (declaring_method->is_generic)
12303                         shared_context = &(mono_method_get_generic_container (declaring_method)->context);
12304                 else
12305                         shared_context = &declaring_method->klass->generic_container->context;
12306
12307                 method_to_compile = mono_class_inflate_generic_method (declaring_method, shared_context);
12308                 g_assert (method_to_compile);
12309         } else {
12310                 method_to_compile = method;
12311         }
12312
12313         cfg = g_new0 (MonoCompile, 1);
12314         cfg->method = method_to_compile;
12315         cfg->mempool = mono_mempool_new ();
12316         cfg->opt = opts;
12317         cfg->prof_options = mono_profiler_get_events ();
12318         cfg->run_cctors = run_cctors;
12319         cfg->domain = domain;
12320         cfg->verbose_level = mini_verbose;
12321         cfg->compile_aot = compile_aot;
12322         cfg->skip_visibility = method->skip_visibility;
12323         cfg->orig_method = method;
12324         if (try_generic_shared)
12325                 cfg->generic_sharing_context = (MonoGenericSharingContext*)&cfg->generic_sharing_context;
12326         cfg->token_info_hash = g_hash_table_new (NULL, NULL);
12327
12328         if (cfg->compile_aot && !try_generic_shared && (method->is_generic || method->klass->generic_container)) {
12329                 cfg->exception_type = MONO_EXCEPTION_GENERIC_SHARING_FAILED;
12330                 return cfg;
12331         }
12332
12333         /* The debugger has no liveness information, so avoid sharing registers/stack slots */
12334         if (mono_debug_using_mono_debugger () || debug_options.mdb_optimizations) {
12335                 cfg->disable_reuse_registers = TRUE;
12336                 cfg->disable_reuse_stack_slots = TRUE;
12337                 /* 
12338                  * This decreases the change the debugger will read registers/stack slots which are
12339                  * not yet initialized.
12340                  */
12341                 cfg->disable_initlocals_opt = TRUE;
12342
12343                 /* Temporarily disable this when running in the debugger until we have support
12344                  * for this in the debugger. */
12345                 cfg->disable_omit_fp = TRUE;
12346
12347                 /* The debugger needs all locals to be on the stack or in a global register */
12348                 cfg->disable_vreg_to_lvreg = TRUE;
12349
12350                 // cfg->opt |= MONO_OPT_SHARED;
12351                 cfg->opt &= ~MONO_OPT_INLINE;
12352                 cfg->opt &= ~MONO_OPT_COPYPROP;
12353                 cfg->opt &= ~MONO_OPT_CONSPROP;
12354                 cfg->opt &= ~MONO_OPT_GSHARED;
12355         }
12356
12357         header = mono_method_get_header (method_to_compile);
12358         if (!header) {
12359                 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
12360                 cfg->exception_message = g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name);
12361                 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12362                         MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
12363                 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
12364                         mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
12365                 return cfg;
12366         }
12367
12368         if (getenv ("MONO_VERBOSE_METHOD")) {
12369                 if (strcmp (cfg->method->name, getenv ("MONO_VERBOSE_METHOD")) == 0)
12370                         cfg->verbose_level = 4;
12371         }
12372
12373         ip = (guint8 *)header->code;
12374
12375         cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX * header->max_stack);
12376
12377         if (cfg->verbose_level > 2) {
12378                 if (cfg->generic_sharing_context)
12379                         g_print ("converting shared method %s\n", mono_method_full_name (method, TRUE));
12380                 else
12381                         g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
12382         }
12383
12384         if (cfg->opt & (MONO_OPT_ABCREM | MONO_OPT_SSAPRE))
12385                 cfg->opt |= MONO_OPT_SSA;
12386
12387         {
12388                 static int count = 0;
12389
12390                 count ++;
12391
12392                 if (getenv ("MONO_COUNT")) {
12393                         if (count == atoi (getenv ("MONO_COUNT"))) {
12394                                 printf ("LAST: %s\n", mono_method_full_name (method, TRUE));
12395                                 //cfg->verbose_level = 5;
12396                         }
12397                         if (count <= atoi (getenv ("MONO_COUNT")))
12398                                 cfg->new_ir = TRUE;
12399
12400                         /*
12401                          * Passing/returning vtypes in registers in managed methods is an ABI change 
12402                          * from the old JIT.
12403                          */
12404                         disable_vtypes_in_regs = TRUE;
12405                 }
12406                 else
12407                         cfg->new_ir = TRUE;
12408         }
12409
12410         /* 
12411         if ((cfg->method->klass->image != mono_defaults.corlib) || (strstr (cfg->method->klass->name, "StackOverflowException") && strstr (cfg->method->name, ".ctor")) || (strstr (cfg->method->klass->name, "OutOfMemoryException") && strstr (cfg->method->name, ".ctor")))
12412                 cfg->globalra = TRUE;
12413         */
12414
12415         //cfg->globalra = TRUE;
12416
12417         //if (!strcmp (cfg->method->klass->name, "Tests") && !cfg->method->wrapper_type)
12418         //      cfg->globalra = TRUE;
12419
12420         {
12421                 static int count = 0;
12422                 count ++;
12423
12424                 if (getenv ("COUNT2")) {
12425                         if (count == atoi (getenv ("COUNT2")))
12426                                 printf ("LAST: %s\n", mono_method_full_name (cfg->method, TRUE));
12427                         if (count > atoi (getenv ("COUNT2")))
12428                                 cfg->globalra = FALSE;
12429                 }
12430         }
12431
12432         if (header->clauses)
12433                 cfg->globalra = FALSE;
12434
12435         if (cfg->method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED)
12436                 /* The code in the prolog clobbers caller saved registers */
12437                 cfg->globalra = FALSE;
12438
12439         // FIXME: Disable globalra in case of tracing/profiling
12440
12441         if (cfg->method->save_lmf)
12442                 /* The LMF saving code might clobber caller saved registers */
12443                 cfg->globalra = FALSE;
12444
12445         // FIXME:
12446         if (!strcmp (cfg->method->name, "CompareInternal"))
12447                 cfg->globalra = FALSE;
12448
12449         /*
12450         if (strstr (cfg->method->name, "LoadData"))
12451                 cfg->new_ir = FALSE;
12452         */
12453
12454         if (cfg->new_ir) {
12455                 cfg->rs = mono_regstate_new ();
12456                 cfg->next_vreg = cfg->rs->next_vreg;
12457         }
12458
12459         /* FIXME: Fix SSA to handle branches inside bblocks */
12460         if (cfg->opt & MONO_OPT_SSA)
12461                 cfg->enable_extended_bblocks = FALSE;
12462
12463         /*
12464          * FIXME: This confuses liveness analysis because variables which are assigned after
12465          * a branch inside a bblock become part of the kill set, even though the assignment
12466          * might not get executed. This causes the optimize_initlocals pass to delete some
12467          * assignments which are needed.
12468          * Also, the mono_if_conversion pass needs to be modified to recognize the code
12469          * created by this.
12470          */
12471         //cfg->enable_extended_bblocks = TRUE;
12472
12473         /*
12474          * create MonoInst* which represents arguments and local variables
12475          */
12476         mono_compile_create_vars (cfg);
12477
12478         if (cfg->new_ir) {
12479                 /* SSAPRE is not supported on linear IR */
12480                 cfg->opt &= ~MONO_OPT_SSAPRE;
12481
12482                 i = mono_method_to_ir2 (cfg, method_to_compile, NULL, NULL, NULL, NULL, NULL, 0, FALSE);
12483         }
12484         else {
12485                 i = mono_method_to_ir (cfg, method_to_compile, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE);
12486         }
12487
12488         if (i < 0) {
12489                 if (try_generic_shared && cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
12490                         if (compile_aot) {
12491                                 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12492                                         MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
12493                                 return cfg;
12494                         }
12495                         mono_destroy_compile (cfg);
12496                         try_generic_shared = FALSE;
12497                         goto restart_compile;
12498                 }
12499                 g_assert (cfg->exception_type != MONO_EXCEPTION_GENERIC_SHARING_FAILED);
12500
12501                 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12502                         MONO_PROBE_METHOD_COMPILE_END (method, FALSE);
12503                 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
12504                         mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
12505                 /* cfg contains the details of the failure, so let the caller cleanup */
12506                 return cfg;
12507         }
12508
12509         mono_jit_stats.basic_blocks += cfg->num_bblocks;
12510         mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
12511
12512         /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
12513
12514         if (cfg->new_ir) {
12515                 mono_decompose_long_opts (cfg);
12516
12517                 /* Should be done before branch opts */
12518                 if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
12519                         mono_local_cprop2 (cfg);
12520         }
12521
12522         if (cfg->opt & MONO_OPT_BRANCH)
12523                 mono_optimize_branches (cfg);
12524
12525         if (cfg->new_ir) {
12526                 /* This must be done _before_ global reg alloc and _after_ decompose */
12527                 mono_handle_global_vregs (cfg);
12528                 mono_local_deadce (cfg);
12529                 mono_if_conversion (cfg);
12530         }
12531
12532         if ((cfg->opt & MONO_OPT_SSAPRE) || cfg->globalra)
12533                 mono_remove_critical_edges (cfg);
12534
12535         /* Depth-first ordering on basic blocks */
12536         cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
12537
12538         dfn = 0;
12539         df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
12540         if (cfg->num_bblocks != dfn + 1) {
12541                 MonoBasicBlock *bb;
12542
12543                 cfg->num_bblocks = dfn + 1;
12544
12545                 /* remove unreachable code, because the code in them may be 
12546                  * inconsistent  (access to dead variables for example) */
12547                 for (bb = cfg->bb_entry; bb;) {
12548                         MonoBasicBlock *bbn = bb->next_bb;
12549
12550                         /* 
12551                          * FIXME: Can't use the second case in methods with clauses, since the 
12552                          * bblocks inside the clauses are not processed during dfn computation.
12553                          */
12554                         if (((header->clauses && (bbn && bbn->region == -1 && bbn->in_count == 0)) ||
12555                                  (!header->clauses && (bbn && bbn->region == -1 && !bbn->dfn))) &&
12556                                 bbn != cfg->bb_exit) {
12557                                 if (cfg->verbose_level > 1)
12558                                         g_print ("found unreachable code in BB%d\n", bbn->block_num);
12559                                 /* There may exist unreachable branches to this bb */
12560                                 bb->next_bb = bbn->next_bb;
12561                                 mono_nullify_basic_block (bbn);                 
12562                         } else {
12563                                 bb = bb->next_bb;
12564                         }
12565                 }
12566         }
12567
12568         if (((cfg->num_varinfo > 2000) || (cfg->num_bblocks > 1000)) && !cfg->compile_aot) {
12569                 /* 
12570                  * we disable some optimizations if there are too many variables
12571                  * because JIT time may become too expensive. The actual number needs 
12572                  * to be tweaked and eventually the non-linear algorithms should be fixed.
12573                  */
12574                 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
12575                 cfg->disable_ssa = TRUE;
12576         }
12577
12578         if (cfg->opt & MONO_OPT_LOOP) {
12579                 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
12580                 mono_compute_natural_loops (cfg);
12581         }
12582
12583         /* after method_to_ir */
12584         if (parts == 1) {
12585                 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12586                         MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
12587                 return cfg;
12588         }
12589
12590 //#define DEBUGSSA "logic_run"
12591 #define DEBUGSSA_CLASS "Tests"
12592 #ifdef DEBUGSSA
12593
12594         if (!header->num_clauses && !cfg->disable_ssa) {
12595                 mono_local_cprop (cfg);
12596
12597 #ifndef DISABLE_SSA
12598                 if (cfg->new_ir)
12599                         mono_ssa_compute2 (cfg);
12600                 else
12601                         mono_ssa_compute (cfg);
12602 #endif
12603         }
12604 #else 
12605         if (cfg->opt & MONO_OPT_SSA) {
12606                 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
12607 #ifndef DISABLE_SSA
12608                         if (!cfg->new_ir)
12609                                 mono_local_cprop (cfg);
12610                         if (cfg->new_ir)
12611                                 mono_ssa_compute2 (cfg);
12612                         else
12613                                 mono_ssa_compute (cfg);
12614 #endif
12615
12616                         if (cfg->verbose_level >= 2) {
12617                                 print_dfn (cfg);
12618                         }
12619                 }
12620         }
12621 #endif
12622
12623         /* after SSA translation */
12624         if (parts == 2) {
12625                 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12626                         MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
12627                 return cfg;
12628         }
12629
12630         if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
12631                 if (cfg->comp_done & MONO_COMP_SSA) {
12632 #ifndef DISABLE_SSA
12633                         if (cfg->new_ir)
12634                                 mono_ssa_cprop2 (cfg);
12635                         else
12636                                 mono_ssa_cprop (cfg);
12637 #endif
12638                 } else {
12639                         if (!cfg->new_ir)
12640                                 mono_local_cprop (cfg);
12641                 }
12642         }
12643
12644 #ifndef DISABLE_SSA
12645         if (cfg->comp_done & MONO_COMP_SSA) {                   
12646                 //mono_ssa_strength_reduction (cfg);
12647
12648                 if (cfg->opt & MONO_OPT_SSAPRE) {
12649                         mono_perform_ssapre (cfg);
12650                         //mono_local_cprop (cfg);
12651                 }
12652
12653                 if (cfg->opt & MONO_OPT_DEADCE) {
12654                         if (cfg->new_ir)
12655                                 mono_ssa_deadce2 (cfg);
12656                         else
12657                                 mono_ssa_deadce (cfg);
12658                         deadce_has_run = TRUE;
12659                 }
12660
12661                 if (cfg->new_ir) {
12662                         if ((cfg->flags & (MONO_CFG_HAS_LDELEMA|MONO_CFG_HAS_CHECK_THIS)) && (cfg->opt & MONO_OPT_ABCREM))
12663                                 mono_perform_abc_removal2 (cfg);
12664                 } else {
12665                         if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
12666                                 mono_perform_abc_removal (cfg);
12667                 }
12668
12669                 if (cfg->new_ir) {
12670                         mono_ssa_remove2 (cfg);
12671                         mono_local_cprop2 (cfg);
12672                         mono_handle_global_vregs (cfg);
12673                         mono_local_deadce (cfg);
12674                 }
12675                 else
12676                         mono_ssa_remove (cfg);
12677
12678                 if (cfg->opt & MONO_OPT_BRANCH) {
12679                         MonoBasicBlock *bb;
12680
12681                         mono_optimize_branches (cfg);
12682
12683                         /* Have to recompute cfg->bblocks and bb->dfn */
12684                         if (cfg->globalra) {
12685                                 mono_remove_critical_edges (cfg);
12686
12687                                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12688                                         bb->dfn = 0;
12689
12690                                 /* Depth-first ordering on basic blocks */
12691                                 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
12692
12693                                 dfn = 0;
12694                                 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
12695                                 cfg->num_bblocks = dfn + 1;
12696                         }
12697                 }
12698         }
12699 #endif
12700
12701         /* after SSA removal */
12702         if (parts == 3) {
12703                 if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
12704                         MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
12705                 return cfg;
12706         }
12707
12708         if (cfg->new_ir) {
12709 #ifdef MONO_ARCH_SOFT_FLOAT
12710                 mono_decompose_soft_float (cfg);
12711 #endif
12712                 mono_decompose_vtype_opts (cfg);
12713                 if (cfg->flags & MONO_CFG_HAS_ARRAY_ACCESS)
12714                         mono_decompose_array_access_opts (cfg);
12715         }
12716
12717         if (!cfg->new_ir) {
12718                 if (cfg->verbose_level > 4)
12719                         mono_print_code (cfg, "BEFORE DECOMPOSE");
12720
12721                 decompose_pass (cfg);
12722         }
12723
12724         if (cfg->got_var) {
12725                 GList *regs;
12726
12727                 g_assert (cfg->got_var_allocated);
12728
12729                 /* 
12730                  * Allways allocate the GOT var to a register, because keeping it
12731                  * in memory will increase the number of live temporaries in some
12732                  * code created by inssel.brg, leading to the well known spills+
12733                  * branches problem. Testcase: mcs crash in 
12734                  * System.MonoCustomAttrs:GetCustomAttributes.
12735                  */
12736                 regs = mono_arch_get_global_int_regs (cfg);
12737                 g_assert (regs);
12738                 cfg->got_var->opcode = OP_REGVAR;
12739                 cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
12740                 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
12741                 
12742                 g_list_free (regs);
12743         }
12744
12745         /* todo: remove code when we have verified that the liveness for try/catch blocks
12746          * works perfectly 
12747          */
12748         /* 
12749          * Currently, this can't be commented out since exception blocks are not
12750          * processed during liveness analysis.
12751          */
12752         mono_liveness_handle_exception_clauses (cfg);
12753
12754         if (cfg->globalra) {
12755                 MonoBasicBlock *bb;
12756
12757                 /* Have to do this before regalloc since it can create vregs */
12758                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb)
12759                         mono_arch_lowering_pass (cfg, bb);
12760
12761                 mono_global_regalloc (cfg);
12762         }
12763
12764         if ((cfg->opt & MONO_OPT_LINEARS) && !cfg->globalra) {
12765                 GList *vars, *regs;
12766                 
12767                 /* For now, compute aliasing info only if needed for deadce... */
12768                 if (!cfg->new_ir && (cfg->opt & MONO_OPT_DEADCE) && (! deadce_has_run) && (header->num_clauses == 0)) {
12769                         cfg->aliasing_info = mono_build_aliasing_information (cfg);
12770                 }
12771
12772                 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
12773                 cfg->comp_done &= ~MONO_COMP_LIVENESS;
12774                 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
12775                         mono_analyze_liveness (cfg);
12776
12777                 if (cfg->aliasing_info != NULL) {
12778                         mono_aliasing_deadce (cfg->aliasing_info);
12779                         deadce_has_run = TRUE;
12780                 }
12781                 
12782                 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
12783                         regs = mono_arch_get_global_int_regs (cfg);
12784                         if (cfg->got_var)
12785                                 regs = g_list_delete_link (regs, regs);
12786                         mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
12787                 }
12788                 
12789                 if (cfg->aliasing_info != NULL) {
12790                         mono_destroy_aliasing_information (cfg->aliasing_info);
12791                         cfg->aliasing_info = NULL;
12792                 }
12793         }
12794
12795         //mono_print_code (cfg);
12796
12797     //print_dfn (cfg);
12798         
12799         /* variables are allocated after decompose, since decompose could create temps */
12800         if (!cfg->globalra)
12801                 mono_arch_allocate_vars (cfg);
12802
12803         if (!cfg->new_ir && cfg->opt & MONO_OPT_CFOLD)
12804                 mono_constant_fold (cfg);
12805
12806         if (cfg->new_ir) {
12807                 MonoBasicBlock *bb;
12808                 gboolean need_local_opts;
12809
12810                 if (!cfg->globalra) {
12811                         mono_spill_global_vars (cfg, &need_local_opts);
12812
12813                         if (need_local_opts || cfg->compile_aot) {
12814                                 /* To optimize code created by spill_global_vars */
12815                                 mono_local_cprop2 (cfg);
12816                                 mono_local_deadce (cfg);
12817                         }
12818                 }
12819
12820                 /* Add branches between non-consecutive bblocks */
12821                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12822                         if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
12823                                 bb->last_ins->inst_false_bb && bb->next_bb != bb->last_ins->inst_false_bb) {
12824                                 /* we are careful when inverting, since bugs like #59580
12825                                  * could show up when dealing with NaNs.
12826                                  */
12827                                 if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
12828                                         MonoBasicBlock *tmp =  bb->last_ins->inst_true_bb;
12829                                         bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
12830                                         bb->last_ins->inst_false_bb = tmp;
12831
12832                                         bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
12833                                 } else {                        
12834                                         MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
12835                                         inst->opcode = OP_BR;
12836                                         inst->inst_target_bb = bb->last_ins->inst_false_bb;
12837                                         mono_bblock_add_inst (bb, inst);
12838                                 }
12839                         }
12840                 }
12841
12842                 if (cfg->verbose_level >= 4 && !cfg->globalra) {
12843                         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12844                                 MonoInst *tree = bb->code;      
12845                                 g_print ("DUMP BLOCK %d:\n", bb->block_num);
12846                                 if (!tree)
12847                                         continue;
12848                                 for (; tree; tree = tree->next) {
12849                                         mono_print_ins_index (-1, tree);
12850                                 }
12851                         }
12852                 }
12853
12854                 /* FIXME: */
12855                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
12856                         bb->max_vreg = cfg->next_vreg;
12857                 }
12858         }
12859         else
12860                 mini_select_instructions (cfg);
12861
12862         mono_codegen (cfg);
12863         if (cfg->verbose_level >= 2) {
12864                 char *id =  mono_method_full_name (cfg->method, FALSE);
12865                 mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
12866                 g_free (id);
12867         }
12868
12869         if (cfg->generic_sharing_context)
12870                 generic_info_size = sizeof (MonoGenericJitInfo);
12871         else
12872                 generic_info_size = 0;
12873
12874         if (cfg->method->dynamic) {
12875                 jinfo = g_malloc0 (sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)) +
12876                                 generic_info_size);
12877         } else {
12878                 /* we access cfg->domain->mp */
12879                 mono_domain_lock (cfg->domain);
12880                 jinfo = mono_domain_alloc0 (cfg->domain, sizeof (MonoJitInfo) +
12881                                 (header->num_clauses * sizeof (MonoJitExceptionInfo)) +
12882                                 generic_info_size);
12883                 mono_domain_unlock (cfg->domain);
12884         }
12885
12886         if (cfg->generic_sharing_context) {
12887                 MonoGenericContext object_context;
12888
12889                 g_assert (!method_to_compile->klass->generic_class);
12890                 if (method_to_compile->klass->generic_container) {
12891                         int type_argc = method_to_compile->klass->generic_container->type_argc;
12892
12893                         object_context.class_inst = get_object_generic_inst (type_argc);
12894                 } else {
12895                         object_context.class_inst = NULL;
12896                 }
12897
12898                 if (mini_method_get_context (method_to_compile)->method_inst) {
12899                         int type_argc = mini_method_get_context (method_to_compile)->method_inst->type_argc;
12900
12901                         object_context.method_inst = get_object_generic_inst (type_argc);
12902                 } else {
12903                         object_context.method_inst = NULL;
12904                 }
12905
12906                 g_assert (object_context.class_inst || object_context.method_inst);
12907
12908                 method_to_register = mono_class_inflate_generic_method (method_to_compile, &object_context);
12909         } else {
12910                 g_assert (method == method_to_compile);
12911                 method_to_register = method;
12912         }
12913
12914         jinfo->method = method_to_register;
12915         jinfo->code_start = cfg->native_code;
12916         jinfo->code_size = cfg->code_len;
12917         jinfo->used_regs = cfg->used_int_regs;
12918         jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
12919         jinfo->cas_inited = FALSE; /* initialization delayed at the first stalk walk using this method */
12920         jinfo->num_clauses = header->num_clauses;
12921
12922         if (cfg->generic_sharing_context) {
12923                 MonoInst *inst;
12924                 MonoGenericJitInfo *gi;
12925
12926                 jinfo->has_generic_jit_info = 1;
12927
12928                 gi = mono_jit_info_get_generic_jit_info (jinfo);
12929                 g_assert (gi);
12930
12931                 gi->generic_sharing_context = cfg->generic_sharing_context;
12932
12933                 /*
12934                  * Non-generic static methods only get a "this" info
12935                  * if they use the rgctx variable (which they are
12936                  * forced to if they have any open catch clauses).
12937                  */
12938                 if (cfg->rgctx_var ||
12939                                 (!(method_to_compile->flags & METHOD_ATTRIBUTE_STATIC) &&
12940                                 !mini_method_get_context (method_to_compile)->method_inst &&
12941                                 !method_to_compile->klass->valuetype)) {
12942                         gi->has_this = 1;
12943
12944                         if ((method_to_compile->flags & METHOD_ATTRIBUTE_STATIC) ||
12945                                         mini_method_get_context (method_to_compile)->method_inst ||
12946                                         method_to_compile->klass->valuetype) {
12947                                 inst = cfg->rgctx_var;
12948                                 g_assert (inst->opcode == OP_REGOFFSET);
12949                         } else {
12950                                 inst = cfg->args [0];
12951                         }
12952
12953                         if (inst->opcode == OP_REGVAR) {
12954                                 gi->this_in_reg = 1;
12955                                 gi->this_reg = inst->dreg;
12956
12957                                 //g_print ("this in reg %d\n", inst->dreg);
12958                         } else {
12959                                 g_assert (inst->opcode == OP_REGOFFSET);
12960 #ifdef __i386__
12961                                 g_assert (inst->inst_basereg == X86_EBP);
12962 #elif defined(__x86_64__)
12963                                 g_assert (inst->inst_basereg == X86_EBP || inst->inst_basereg == X86_ESP);
12964 #endif
12965                                 g_assert (inst->inst_offset >= G_MININT32 && inst->inst_offset <= G_MAXINT32);
12966
12967                                 gi->this_in_reg = 0;
12968                                 gi->this_reg = inst->inst_basereg;
12969                                 gi->this_offset = inst->inst_offset;
12970
12971                                 //g_print ("this at offset %d from reg %d\n", gi->this_offset, gi->this_reg);
12972                         }
12973                 } else {
12974                         gi->has_this = 0;
12975                 }
12976         }
12977
12978         if (header->num_clauses) {
12979                 int i;
12980
12981                 for (i = 0; i < header->num_clauses; i++) {
12982                         MonoExceptionClause *ec = &header->clauses [i];
12983                         MonoJitExceptionInfo *ei = &jinfo->clauses [i];
12984                         MonoBasicBlock *tblock;
12985                         MonoInst *exvar;
12986
12987                         ei->flags = ec->flags;
12988
12989                         exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
12990                         ei->exvar_offset = exvar ? exvar->inst_offset : 0;
12991
12992                         if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
12993                                 tblock = cfg->cil_offset_to_bb [ec->data.filter_offset];
12994                                 g_assert (tblock);
12995                                 ei->data.filter = cfg->native_code + tblock->native_offset;
12996                         } else {
12997                                 ei->data.catch_class = ec->data.catch_class;
12998                         }
12999
13000                         tblock = cfg->cil_offset_to_bb [ec->try_offset];
13001                         g_assert (tblock);
13002                         ei->try_start = cfg->native_code + tblock->native_offset;
13003                         g_assert (tblock->native_offset);
13004                         tblock = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
13005                         g_assert (tblock);
13006                         ei->try_end = cfg->native_code + tblock->native_offset;
13007                         g_assert (tblock->native_offset);
13008                         tblock = cfg->cil_offset_to_bb [ec->handler_offset];
13009                         g_assert (tblock);
13010                         ei->handler_start = cfg->native_code + tblock->native_offset;
13011                 }
13012         }
13013
13014         cfg->jit_info = jinfo;
13015 #if defined(__arm__)
13016         mono_arch_fixup_jinfo (cfg);
13017 #endif
13018
13019         if (!cfg->compile_aot) {
13020                 mono_domain_lock (cfg->domain);
13021                 mono_jit_info_table_add (cfg->domain, jinfo);
13022
13023                 if (cfg->method->dynamic)
13024                         mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
13025                 mono_domain_unlock (cfg->domain);
13026         }
13027
13028         /* collect statistics */
13029         mono_perfcounters->jit_methods++;
13030         mono_perfcounters->jit_bytes += header->code_size;
13031         mono_jit_stats.allocated_code_size += cfg->code_len;
13032         code_size_ratio = cfg->code_len;
13033         if (code_size_ratio > mono_jit_stats.biggest_method_size && mono_jit_stats.enabled) {
13034                 mono_jit_stats.biggest_method_size = code_size_ratio;
13035                 g_free (mono_jit_stats.biggest_method);
13036                 mono_jit_stats.biggest_method = g_strdup_printf ("%s::%s)", method->klass->name, method->name);
13037         }
13038         code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
13039         if (code_size_ratio > mono_jit_stats.max_code_size_ratio && mono_jit_stats.enabled) {
13040                 mono_jit_stats.max_code_size_ratio = code_size_ratio;
13041                 g_free (mono_jit_stats.max_ratio_method);
13042                 mono_jit_stats.max_ratio_method = g_strdup_printf ("%s::%s)", method->klass->name, method->name);
13043         }
13044         mono_jit_stats.native_code_size += cfg->code_len;
13045
13046         if (MONO_PROBE_METHOD_COMPILE_END_ENABLED ())
13047                 MONO_PROBE_METHOD_COMPILE_END (method, TRUE);
13048         if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
13049                 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
13050
13051         return cfg;
13052 }
13053
13054 #else
13055
13056 MonoCompile*
13057 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
13058 {
13059         g_assert_not_reached ();
13060         return NULL;
13061 }
13062
13063 #endif /* DISABLE_JIT */
13064
13065 static MonoJitInfo*
13066 lookup_generic_method (MonoDomain *domain, MonoMethod *method)
13067 {
13068         MonoMethod *open_method;
13069
13070         if (!mono_method_is_generic_sharable_impl (method, FALSE))
13071                 return NULL;
13072
13073         open_method = mono_method_get_declaring_generic_method (method);
13074
13075         return mono_domain_lookup_shared_generic (domain, open_method);
13076 }
13077
13078 /*
13079  * LOCKING: Assumes domain->jit_code_hash_lock is held.
13080  */
13081 static MonoJitInfo*
13082 lookup_method_inner (MonoDomain *domain, MonoMethod *method)
13083 {
13084         MonoJitInfo *ji = mono_internal_hash_table_lookup (&domain->jit_code_hash, method);
13085
13086         if (ji)
13087                 return ji;
13088
13089         return lookup_generic_method (domain, method);
13090 }
13091
13092 static MonoJitInfo*
13093 lookup_method (MonoDomain *domain, MonoMethod *method)
13094 {
13095         MonoJitInfo *info;
13096
13097         mono_domain_jit_code_hash_lock (domain);
13098         info = lookup_method_inner (domain, method);
13099         mono_domain_jit_code_hash_unlock (domain);
13100
13101         return info;
13102 }
13103
13104 static gpointer
13105 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, int opt)
13106 {
13107         MonoCompile *cfg;
13108         gpointer code = NULL;
13109         MonoJitInfo *info;
13110         MonoVTable *vtable;
13111
13112 #ifdef MONO_USE_AOT_COMPILER
13113         if ((opt & MONO_OPT_AOT) && !(mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)) {
13114                 MonoDomain *domain = mono_domain_get ();
13115
13116                 mono_class_init (method->klass);
13117
13118                 mono_domain_lock (domain);
13119                 if ((code = mono_aot_get_method (domain, method))) {
13120                         mono_domain_unlock (domain);
13121                         vtable = mono_class_vtable (domain, method->klass);
13122                         g_assert (vtable);
13123                         mono_runtime_class_init (vtable);
13124                         return code;
13125                 }
13126
13127                 mono_domain_unlock (domain);
13128         }
13129 #endif
13130
13131         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
13132             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
13133                 MonoMethod *nm;
13134                 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
13135
13136                 if (!piinfo->addr) {
13137                         if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
13138                                 piinfo->addr = mono_lookup_internal_call (method);
13139                         else if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)
13140 #ifdef PLATFORM_WIN32
13141                                 g_warning ("Method '%s' in assembly '%s' contains native code that cannot be executed by Mono in modules loaded from byte arrays. The assembly was probably created using C++/CLI.\n", mono_method_full_name (method, TRUE), method->klass->image->name);
13142 #else
13143                                 g_warning ("Method '%s' in assembly '%s' contains native code that cannot be executed by Mono on this platform. The assembly was probably created using C++/CLI.\n", mono_method_full_name (method, TRUE), method->klass->image->name);
13144 #endif
13145                         else
13146                                 mono_lookup_pinvoke_call (method, NULL, NULL);
13147                 }
13148                 nm = mono_marshal_get_native_wrapper (method, check_for_pending_exc, FALSE);
13149                 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13150
13151                 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE) 
13152                 //mono_debug_add_wrapper (method, nm);
13153         } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
13154                 const char *name = method->name;
13155                 MonoMethod *nm;
13156
13157                 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
13158                         if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
13159                                 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
13160                                 g_assert (mi);
13161                                 /*
13162                                  * We need to make sure this wrapper
13163                                  * is compiled because it might end up
13164                                  * in an (M)RGCTX if generic sharing
13165                                  * is enabled, and would be called
13166                                  * indirectly.  If it were a
13167                                  * trampoline we'd try to patch that
13168                                  * indirect call, which is not
13169                                  * possible.
13170                                  */
13171                                 return mono_get_addr_from_ftnptr ((gpointer)mono_icall_get_wrapper_full (mi, TRUE));
13172                         } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
13173 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
13174                                 return mono_create_delegate_trampoline (method->klass);
13175 #else
13176                                 nm = mono_marshal_get_delegate_invoke (method, NULL);
13177                                 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13178 #endif
13179                         } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
13180                                 nm = mono_marshal_get_delegate_begin_invoke (method);
13181                                 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13182                         } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
13183                                 nm = mono_marshal_get_delegate_end_invoke (method);
13184                                 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
13185                         }
13186                 }
13187                 return NULL;
13188         }
13189
13190         if (mono_aot_only)
13191                 g_error ("Attempting to JIT compile method '%s' while running with --aot-only.\n", mono_method_full_name (method, TRUE));
13192
13193         cfg = mini_method_compile (method, opt, target_domain, TRUE, FALSE, 0);
13194
13195         switch (cfg->exception_type) {
13196         case MONO_EXCEPTION_NONE:
13197                 break;
13198         case MONO_EXCEPTION_TYPE_LOAD:
13199         case MONO_EXCEPTION_MISSING_FIELD:
13200         case MONO_EXCEPTION_MISSING_METHOD:
13201         case MONO_EXCEPTION_FILE_NOT_FOUND:
13202         case MONO_EXCEPTION_BAD_IMAGE: {
13203                 /* Throw a type load exception if needed */
13204                 MonoLoaderError *error = mono_loader_get_last_error ();
13205                 MonoException *ex;
13206
13207                 if (error) {
13208                         ex = mono_loader_error_prepare_exception (error);
13209                 } else {
13210                         if (cfg->exception_ptr) {
13211                                 ex = mono_class_get_exception_for_failure (cfg->exception_ptr);
13212                         } else {
13213                                 if (cfg->exception_type == MONO_EXCEPTION_MISSING_FIELD)
13214                                         ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", cfg->exception_message);
13215                                 else if (cfg->exception_type == MONO_EXCEPTION_MISSING_METHOD)
13216                                         ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", cfg->exception_message);
13217                                 else if (cfg->exception_type == MONO_EXCEPTION_TYPE_LOAD)
13218                                         ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", cfg->exception_message);
13219                                 else if (cfg->exception_type == MONO_EXCEPTION_FILE_NOT_FOUND)
13220                                         ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "FileNotFoundException", cfg->exception_message);
13221                                 else if (cfg->exception_type == MONO_EXCEPTION_BAD_IMAGE)
13222                                         ex = mono_get_exception_bad_image_format (cfg->exception_message);
13223                                 else
13224                                         g_assert_not_reached ();
13225                         }
13226                 }
13227                 mono_destroy_compile (cfg);
13228                 mono_raise_exception (ex);
13229                 break;
13230         }
13231         case MONO_EXCEPTION_INVALID_PROGRAM: {
13232                 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", cfg->exception_message);
13233                 mono_destroy_compile (cfg);
13234                 mono_raise_exception (ex);
13235                 break;
13236         }
13237         case MONO_EXCEPTION_UNVERIFIABLE_IL: {
13238                 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", cfg->exception_message);
13239                 mono_destroy_compile (cfg);
13240                 mono_raise_exception (ex);
13241                 break;
13242         }
13243         case MONO_EXCEPTION_METHOD_ACCESS: {
13244                 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MethodAccessException", cfg->exception_message);
13245                 mono_destroy_compile (cfg);
13246                 mono_raise_exception (ex);
13247                 break;
13248         }
13249         case MONO_EXCEPTION_FIELD_ACCESS: {
13250                 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "FieldAccessException", cfg->exception_message);
13251                 mono_destroy_compile (cfg);
13252                 mono_raise_exception (ex);
13253                 break;
13254         }
13255         /* this can only be set if the security manager is active */
13256         case MONO_EXCEPTION_SECURITY_LINKDEMAND: {
13257                 MonoSecurityManager* secman = mono_security_manager_get_methods ();
13258                 MonoObject *exc = NULL;
13259                 gpointer args [2];
13260
13261                 args [0] = &cfg->exception_data;
13262                 args [1] = &method;
13263                 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
13264
13265                 mono_destroy_compile (cfg);
13266                 cfg = NULL;
13267
13268                 mono_raise_exception ((MonoException*)exc);
13269         }
13270         case MONO_EXCEPTION_OBJECT_SUPPLIED: {
13271                 MonoException *exp = cfg->exception_ptr;
13272                 MONO_GC_UNREGISTER_ROOT (cfg->exception_ptr);
13273                 mono_destroy_compile (cfg);
13274                 mono_raise_exception (exp);
13275                 break;
13276         }
13277         default:
13278                 g_assert_not_reached ();
13279         }
13280
13281         mono_domain_lock (target_domain);
13282
13283         /* Check if some other thread already did the job. In this case, we can
13284        discard the code this thread generated. */
13285
13286         mono_domain_jit_code_hash_lock (target_domain);
13287
13288         info = lookup_method_inner (target_domain, method);
13289         if (info) {
13290                 /* We can't use a domain specific method in another domain */
13291                 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
13292                         code = info->code_start;
13293 //                      printf("Discarding code for method %s\n", method->name);
13294                 }
13295         }
13296         
13297         if (code == NULL) {
13298                 mono_internal_hash_table_insert (&target_domain->jit_code_hash, cfg->jit_info->method, cfg->jit_info);
13299                 mono_domain_jit_code_hash_unlock (target_domain);
13300                 code = cfg->native_code;
13301
13302                 if (cfg->generic_sharing_context && mono_method_is_generic_sharable_impl (method, FALSE)) {
13303                         /* g_print ("inserting method %s.%s.%s\n", method->klass->name_space, method->klass->name, method->name); */
13304                         mono_domain_register_shared_generic (target_domain, 
13305                                 mono_method_get_declaring_generic_method (method), cfg->jit_info);
13306                         mono_stats.generics_shared_methods++;
13307                 }
13308         } else {
13309                 mono_domain_jit_code_hash_unlock (target_domain);
13310         }
13311
13312         mono_destroy_compile (cfg);
13313
13314         if (domain_jit_info (target_domain)->jump_target_hash) {
13315                 MonoJumpInfo patch_info;
13316                 GSList *list, *tmp;
13317                 list = g_hash_table_lookup (domain_jit_info (target_domain)->jump_target_hash, method);
13318                 if (list) {
13319                         patch_info.next = NULL;
13320                         patch_info.ip.i = 0;
13321                         patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
13322                         patch_info.data.method = method;
13323                         g_hash_table_remove (domain_jit_info (target_domain)->jump_target_hash, method);
13324                 }
13325                 for (tmp = list; tmp; tmp = tmp->next)
13326                         mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
13327                 g_slist_free (list);
13328         }
13329
13330         mono_domain_unlock (target_domain);
13331
13332         vtable = mono_class_vtable (target_domain, method->klass);
13333         if (!vtable) {
13334                 MonoException *exc;
13335                 exc = mono_class_get_exception_for_failure (method->klass);
13336                 g_assert (exc);
13337                 mono_raise_exception (exc);
13338         }
13339         mono_runtime_class_init (vtable);
13340         return code;
13341 }
13342
13343 static gpointer
13344 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
13345 {
13346         MonoDomain *target_domain, *domain = mono_domain_get ();
13347         MonoJitInfo *info;
13348         gpointer p;
13349         MonoJitICallInfo *callinfo = NULL;
13350
13351         /*
13352          * ICALL wrappers are handled specially, since there is only one copy of them
13353          * shared by all appdomains.
13354          */
13355         if ((method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
13356                 const char *icall_name;
13357
13358                 icall_name = method->name + strlen ("__icall_wrapper_");
13359                 g_assert (icall_name);
13360                 callinfo = mono_find_jit_icall_by_name (icall_name);
13361                 g_assert (callinfo);
13362
13363                 /* Must be domain neutral since there is only one copy */
13364                 opt |= MONO_OPT_SHARED;
13365         }
13366
13367         if (opt & MONO_OPT_SHARED)
13368                 target_domain = mono_get_root_domain ();
13369         else 
13370                 target_domain = domain;
13371
13372         info = lookup_method (target_domain, method);
13373         if (info) {
13374                 /* We can't use a domain specific method in another domain */
13375                 if (! ((domain != target_domain) && !info->domain_neutral)) {
13376                         MonoVTable *vtable;
13377
13378                         mono_jit_stats.methods_lookups++;
13379                         vtable = mono_class_vtable (domain, method->klass);
13380                         mono_runtime_class_init (vtable);
13381                         return mono_create_ftnptr (target_domain, info->code_start);
13382                 }
13383         }
13384
13385         p = mono_create_ftnptr (target_domain, mono_jit_compile_method_inner (method, target_domain, opt));
13386
13387         if (callinfo) {
13388                 mono_jit_lock ();
13389                 if (!callinfo->wrapper) {
13390                         callinfo->wrapper = p;
13391                         mono_register_jit_icall_wrapper (callinfo, p);
13392                         mono_debug_add_icall_wrapper (method, callinfo);
13393                 }
13394                 mono_jit_unlock ();
13395         }
13396
13397         return p;
13398 }
13399
13400 static gpointer
13401 mono_jit_compile_method (MonoMethod *method)
13402 {
13403         return mono_jit_compile_method_with_opt (method, default_opt);
13404 }
13405
13406 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
13407 static void
13408 invalidated_delegate_trampoline (char *desc)
13409 {
13410         g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
13411                  "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
13412                  desc);
13413 }
13414 #endif
13415
13416 /*
13417  * mono_jit_free_method:
13418  *
13419  *  Free all memory allocated by the JIT for METHOD.
13420  */
13421 static void
13422 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
13423 {
13424         MonoJitDynamicMethodInfo *ji;
13425         gboolean destroy = TRUE;
13426
13427         g_assert (method->dynamic);
13428
13429         mono_domain_lock (domain);
13430         ji = mono_dynamic_code_hash_lookup (domain, method);
13431         mono_domain_unlock (domain);
13432
13433         if (!ji)
13434                 return;
13435         mono_domain_lock (domain);
13436         g_hash_table_remove (domain_jit_info (domain)->dynamic_code_hash, method);
13437         mono_internal_hash_table_remove (&domain->jit_code_hash, method);
13438         g_hash_table_remove (domain_jit_info (domain)->jump_trampoline_hash, method);
13439         mono_domain_unlock (domain);
13440
13441 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
13442         if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
13443                 /*
13444                  * Instead of freeing the code, change it to call an error routine
13445                  * so people can fix their code.
13446                  */
13447                 char *type = mono_type_full_name (&method->klass->byval_arg);
13448                 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
13449
13450                 g_free (type);
13451                 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
13452                 destroy = FALSE;
13453         }
13454 #endif
13455
13456         /* 
13457          * This needs to be done before freeing code_mp, since the code address is the
13458          * key in the table, so if we free the code_mp first, another thread can grab the
13459          * same code address and replace our entry in the table.
13460          */
13461         mono_jit_info_table_remove (domain, ji->ji);
13462
13463         if (destroy)
13464                 mono_code_manager_destroy (ji->code_mp);
13465         g_free (ji);
13466 }
13467
13468 gpointer
13469 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
13470 {
13471         MonoDomain *target_domain;
13472         MonoJitInfo *info;
13473
13474         if (default_opt & MONO_OPT_SHARED)
13475                 target_domain = mono_get_root_domain ();
13476         else 
13477                 target_domain = domain;
13478
13479         info = lookup_method (target_domain, method);
13480         if (info) {
13481                 /* We can't use a domain specific method in another domain */
13482                 if (! ((domain != target_domain) && !info->domain_neutral)) {
13483                         mono_jit_stats.methods_lookups++;
13484                         return info->code_start;
13485                 }
13486         }
13487
13488         return NULL;
13489 }
13490
13491 /**
13492  * mono_jit_runtime_invoke:
13493  * @method: the method to invoke
13494  * @obj: this pointer
13495  * @params: array of parameter values.
13496  * @exc: used to catch exceptions objects
13497  */
13498 static MonoObject*
13499 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
13500 {
13501         MonoMethod *to_compile;
13502         MonoMethod *invoke;
13503         MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
13504         void* compiled_method;
13505         MonoVTable *vtable;
13506
13507         if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
13508                 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
13509                 return NULL;
13510         }
13511
13512         if (mono_method_needs_static_rgctx_invoke (method, FALSE))
13513                 to_compile = mono_marshal_get_static_rgctx_invoke (method);
13514         else
13515                 to_compile = method;
13516
13517         invoke = mono_marshal_get_runtime_invoke (method);
13518         runtime_invoke = mono_jit_compile_method (invoke);
13519         
13520         /* We need this here becuase mono_marshal_get_runtime_invoke can be place 
13521          * the helper method in System.Object and not the target class
13522          */
13523         vtable = mono_class_vtable (mono_domain_get (), method->klass);
13524         g_assert (vtable);
13525         mono_runtime_class_init (vtable);
13526
13527         if (method->klass->rank && (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
13528                 (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE)) {
13529                 /* 
13530                  * Array Get/Set/Address methods. The JIT implements them using inline code 
13531                  * inside the runtime invoke wrappers, so no need to compile them.
13532                  */
13533                 compiled_method = NULL;
13534         } else {
13535                 compiled_method = mono_jit_compile_method (to_compile);
13536         }
13537         return runtime_invoke (obj, params, exc, compiled_method);
13538 }
13539
13540 #ifdef MONO_GET_CONTEXT
13541 #define GET_CONTEXT MONO_GET_CONTEXT
13542 #endif
13543
13544 #ifndef GET_CONTEXT
13545 #ifdef PLATFORM_WIN32
13546 #define GET_CONTEXT \
13547         struct sigcontext *ctx = (struct sigcontext*)_dummy;
13548 #else
13549 #ifdef MONO_ARCH_USE_SIGACTION
13550 #define GET_CONTEXT \
13551     void *ctx = context;
13552 #elif defined(__sparc__)
13553 #define GET_CONTEXT \
13554     void *ctx = sigctx;
13555 #else
13556 #define GET_CONTEXT \
13557         void **_p = (void **)&_dummy; \
13558         struct sigcontext *ctx = (struct sigcontext *)++_p;
13559 #endif
13560 #endif
13561 #endif
13562
13563 #ifdef MONO_ARCH_USE_SIGACTION
13564 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
13565 #elif defined(__sparc__)
13566 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, void *sigctx)
13567 #else
13568 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
13569 #endif
13570
13571 static void
13572 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
13573 {
13574         MonoException *exc = NULL;
13575 #ifndef MONO_ARCH_USE_SIGACTION
13576         void *info = NULL;
13577 #endif
13578         GET_CONTEXT;
13579
13580 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
13581         if (mono_arch_is_int_overflow (ctx, info))
13582                 exc = mono_get_exception_arithmetic ();
13583         else
13584                 exc = mono_get_exception_divide_by_zero ();
13585 #else
13586         exc = mono_get_exception_divide_by_zero ();
13587 #endif
13588         
13589         mono_arch_handle_exception (ctx, exc, FALSE);
13590 }
13591
13592 static void
13593 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
13594 {
13595         MonoException *exc;
13596         GET_CONTEXT;
13597
13598         exc = mono_get_exception_execution_engine ("SIGILL");
13599         
13600         mono_arch_handle_exception (ctx, exc, FALSE);
13601 }
13602
13603 static void
13604 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
13605 {
13606 #ifndef MONO_ARCH_SIGSEGV_ON_ALTSTACK
13607         MonoException *exc = NULL;
13608 #endif
13609         MonoJitInfo *ji;
13610         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
13611
13612         GET_CONTEXT;
13613
13614 #ifdef MONO_ARCH_USE_SIGACTION
13615         if (debug_options.collect_pagefault_stats) {
13616                 if (mono_aot_is_pagefault (info->si_addr)) {
13617                         mono_aot_handle_pagefault (info->si_addr);
13618                         return;
13619                 }
13620         }
13621 #endif
13622
13623         /* The thread might no be registered with the runtime */
13624         if (!mono_domain_get () || !jit_tls)
13625                 mono_handle_native_sigsegv (SIGSEGV, ctx);
13626
13627         ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
13628
13629 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
13630         if (mono_handle_soft_stack_ovf (jit_tls, ji, ctx, (guint8*)info->si_addr))
13631                 return;
13632
13633         /* The hard-guard page has been hit: there is not much we can do anymore
13634          * Print a hopefully clear message and abort.
13635          */
13636         if (jit_tls->stack_size && 
13637                         ABS ((guint8*)info->si_addr - ((guint8*)jit_tls->end_of_stack - jit_tls->stack_size)) < 32768) {
13638                 const char *method;
13639                 /* we don't do much now, but we can warn the user with a useful message */
13640                 fprintf (stderr, "Stack overflow: IP: %p, fault addr: %p\n", mono_arch_ip_from_context (ctx), (gpointer)info->si_addr);
13641                 if (ji && ji->method)
13642                         method = mono_method_full_name (ji->method, TRUE);
13643                 else
13644                         method = "Unmanaged";
13645                 fprintf (stderr, "At %s\n", method);
13646                 _exit (1);
13647         } else {
13648                 mono_arch_handle_altstack_exception (ctx, info->si_addr, FALSE);
13649         }
13650 #else
13651
13652         if (!ji) {
13653                 mono_handle_native_sigsegv (SIGSEGV, ctx);
13654         }
13655                         
13656         mono_arch_handle_exception (ctx, exc, FALSE);
13657 #endif
13658 }
13659
13660 #ifndef PLATFORM_WIN32
13661
13662 static void
13663 SIG_HANDLER_SIGNATURE (sigabrt_signal_handler)
13664 {
13665         MonoJitInfo *ji;
13666         GET_CONTEXT;
13667
13668         ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
13669         if (!ji) {
13670                 mono_handle_native_sigsegv (SIGABRT, ctx);
13671         }
13672 }
13673
13674 static void
13675 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
13676 {
13677         gboolean running_managed;
13678         MonoException *exc;
13679         MonoThread *thread = mono_thread_current ();
13680         void *ji;
13681         
13682         GET_CONTEXT;
13683
13684         if (thread->thread_dump_requested) {
13685                 thread->thread_dump_requested = FALSE;
13686
13687                 mono_print_thread_dump (ctx);
13688         }
13689
13690         /*
13691          * FIXME:
13692          * This is an async signal, so the code below must not call anything which
13693          * is not async safe. That includes the pthread locking functions. If we
13694          * know that we interrupted managed code, then locking is safe.
13695          */
13696         ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
13697         running_managed = ji != NULL;
13698         
13699         exc = mono_thread_request_interruption (running_managed); 
13700         if (!exc) return;
13701
13702         mono_arch_handle_exception (ctx, exc, FALSE);
13703 }
13704
13705 #if defined(__i386__) || defined(__x86_64__)
13706 #define FULL_STAT_PROFILER_BACKTRACE 1
13707 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
13708 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
13709 #if MONO_ARCH_STACK_GROWS_UP
13710 #define IS_BEFORE_ON_STACK <
13711 #define IS_AFTER_ON_STACK >
13712 #else
13713 #define IS_BEFORE_ON_STACK >
13714 #define IS_AFTER_ON_STACK <
13715 #endif
13716 #else
13717 #define FULL_STAT_PROFILER_BACKTRACE 0
13718 #endif
13719
13720 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
13721
13722 static void
13723 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
13724 {
13725         NOT_IMPLEMENTED;
13726 }
13727
13728 #else
13729
13730 static void
13731 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
13732 {
13733         int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
13734         GET_CONTEXT;
13735         
13736         if (call_chain_depth == 0) {
13737                 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
13738         } else {
13739                 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
13740                 int current_frame_index = 1;
13741                 MonoContext mono_context;
13742 #if FULL_STAT_PROFILER_BACKTRACE
13743                 guchar *current_frame;
13744                 guchar *stack_bottom;
13745                 guchar *stack_top;
13746 #else
13747                 MonoDomain *domain;
13748 #endif
13749                 guchar *ips [call_chain_depth + 1];
13750
13751                 mono_arch_sigctx_to_monoctx (ctx, &mono_context);
13752                 ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
13753                 
13754                 if (jit_tls != NULL) {
13755 #if FULL_STAT_PROFILER_BACKTRACE
13756                         stack_bottom = jit_tls->end_of_stack;
13757                         stack_top = MONO_CONTEXT_GET_SP (&mono_context);
13758                         current_frame = MONO_CONTEXT_GET_BP (&mono_context);
13759                         
13760                         while ((current_frame_index <= call_chain_depth) &&
13761                                         (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
13762                                         ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
13763                                 ips [current_frame_index] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
13764                                 current_frame_index ++;
13765                                 stack_top = current_frame;
13766                                 current_frame = CURRENT_FRAME_GET_BASE_POINTER (current_frame);
13767                         }
13768 #else
13769                         domain = mono_domain_get ();
13770                         if (domain != NULL) {
13771                                 MonoLMF *lmf = NULL;
13772                                 MonoJitInfo *ji;
13773                                 MonoJitInfo res;
13774                                 MonoContext new_mono_context;
13775                                 int native_offset;
13776                                 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
13777                                                 &new_mono_context, NULL, &lmf, &native_offset, NULL);
13778                                 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
13779                                         ips [current_frame_index] = MONO_CONTEXT_GET_IP (&new_mono_context);
13780                                         current_frame_index ++;
13781                                         mono_context = new_mono_context;
13782                                         ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
13783                                                         &new_mono_context, NULL, &lmf, &native_offset, NULL);
13784                                 }
13785                         }
13786 #endif
13787                 }
13788                 
13789                 
13790                 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
13791         }
13792 }
13793
13794 #endif
13795
13796 static void
13797 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
13798 {
13799         gboolean res;
13800
13801         GET_CONTEXT;
13802
13803         /* We use this signal to start the attach agent too */
13804         res = mono_attach_start ();
13805         if (res)
13806                 return;
13807
13808         printf ("Full thread dump:\n");
13809
13810         mono_threads_request_thread_dump ();
13811
13812         /*
13813          * print_thread_dump () skips the current thread, since sending a signal
13814          * to it would invoke the signal handler below the sigquit signal handler,
13815          * and signal handlers don't create an lmf, so the stack walk could not
13816          * be performed.
13817          */
13818         mono_print_thread_dump (ctx);
13819 }
13820
13821 static void
13822 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
13823 {
13824         gboolean enabled = mono_trace_is_enabled ();
13825
13826         mono_trace_enable (!enabled);
13827 }
13828
13829 #endif
13830
13831 static void
13832 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
13833 {
13834         MonoException *exc;
13835         GET_CONTEXT;
13836
13837         exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
13838         
13839         mono_arch_handle_exception (ctx, exc, FALSE);
13840 }
13841
13842 #ifdef PLATFORM_MACOSX
13843
13844 /*
13845  * This code disables the CrashReporter of MacOS X by installing
13846  * a dummy Mach exception handler.
13847  */
13848
13849 /*
13850  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
13851  */
13852 extern
13853 boolean_t
13854 exc_server (mach_msg_header_t *request_msg,
13855             mach_msg_header_t *reply_msg);
13856
13857 /*
13858  * The exception message
13859  */
13860 typedef struct {
13861         mach_msg_base_t msg;  /* common mach message header */
13862         char payload [1024];  /* opaque */
13863 } mach_exception_msg_t;
13864
13865 /* The exception port */
13866 static mach_port_t mach_exception_port = VM_MAP_NULL;
13867
13868 /*
13869  * Implicitly called by exc_server. Must be public.
13870  *
13871  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
13872  */
13873 kern_return_t
13874 catch_exception_raise (
13875         mach_port_t exception_port,
13876         mach_port_t thread,
13877         mach_port_t task,
13878         exception_type_t exception,
13879         exception_data_t code,
13880         mach_msg_type_number_t code_count)
13881 {
13882         /* consume the exception */
13883         return KERN_FAILURE;
13884 }
13885
13886 /*
13887  * Exception thread handler.
13888  */
13889 static
13890 void *
13891 mach_exception_thread (void *arg)
13892 {
13893         for (;;) {
13894                 mach_exception_msg_t request;
13895                 mach_exception_msg_t reply;
13896                 mach_msg_return_t result;
13897
13898                 /* receive from "mach_exception_port" */
13899                 result = mach_msg (&request.msg.header,
13900                                    MACH_RCV_MSG | MACH_RCV_LARGE,
13901                                    0,
13902                                    sizeof (request),
13903                                    mach_exception_port,
13904                                    MACH_MSG_TIMEOUT_NONE,
13905                                    MACH_PORT_NULL);
13906
13907                 g_assert (result == MACH_MSG_SUCCESS);
13908
13909                 /* dispatch to catch_exception_raise () */
13910                 exc_server (&request.msg.header, &reply.msg.header);
13911
13912                 /* send back to sender */
13913                 result = mach_msg (&reply.msg.header,
13914                                    MACH_SEND_MSG,
13915                                    reply.msg.header.msgh_size,
13916                                    0,
13917                                    MACH_PORT_NULL,
13918                                    MACH_MSG_TIMEOUT_NONE,
13919                                    MACH_PORT_NULL);
13920
13921                 g_assert (result == MACH_MSG_SUCCESS);
13922         }
13923         return NULL;
13924 }
13925
13926 static void
13927 macosx_register_exception_handler ()
13928 {
13929         mach_port_t task;
13930         pthread_attr_t attr;
13931         pthread_t thread;
13932
13933         if (mach_exception_port != VM_MAP_NULL)
13934                 return;
13935
13936         task = mach_task_self ();
13937
13938         /* create the "mach_exception_port" with send & receive rights */
13939         g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
13940                                       &mach_exception_port) == KERN_SUCCESS);
13941         g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
13942                                           MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
13943
13944         /* create the exception handler thread */
13945         g_assert (!pthread_attr_init (&attr));
13946         g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
13947         g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
13948         pthread_attr_destroy (&attr);
13949
13950         /*
13951          * register "mach_exception_port" as a receiver for the
13952          * EXC_BAD_ACCESS exception
13953          *
13954          * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
13955          */
13956         g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
13957                                             mach_exception_port,
13958                                             EXCEPTION_DEFAULT,
13959                                             MACHINE_THREAD_STATE) == KERN_SUCCESS);
13960 }
13961 #endif
13962
13963 #ifndef PLATFORM_WIN32
13964 static void
13965 add_signal_handler (int signo, gpointer handler)
13966 {
13967         struct sigaction sa;
13968
13969 #ifdef MONO_ARCH_USE_SIGACTION
13970         sa.sa_sigaction = handler;
13971         sigemptyset (&sa.sa_mask);
13972         sa.sa_flags = SA_SIGINFO;
13973 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
13974         if (signo == SIGSEGV)
13975                 sa.sa_flags |= SA_ONSTACK;
13976 #endif
13977 #else
13978         sa.sa_handler = handler;
13979         sigemptyset (&sa.sa_mask);
13980         sa.sa_flags = 0;
13981 #endif
13982         g_assert (sigaction (signo, &sa, NULL) != -1);
13983 }
13984
13985 static void
13986 remove_signal_handler (int signo)
13987 {
13988         struct sigaction sa;
13989
13990         sa.sa_handler = SIG_DFL;
13991         sigemptyset (&sa.sa_mask);
13992         sa.sa_flags = 0;
13993
13994         g_assert (sigaction (signo, &sa, NULL) != -1);
13995 }
13996 #endif
13997
13998 static void
13999 mono_runtime_install_handlers (void)
14000 {
14001 #ifdef PLATFORM_WIN32
14002         win32_seh_init();
14003         win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
14004         win32_seh_set_handler(SIGILL, sigill_signal_handler);
14005         win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
14006         if (debug_options.handle_sigint)
14007                 win32_seh_set_handler(SIGINT, sigint_signal_handler);
14008
14009 #else /* !PLATFORM_WIN32 */
14010
14011         sigset_t signal_set;
14012
14013 #if defined(PLATFORM_MACOSX) && !defined(__arm__)
14014         macosx_register_exception_handler ();
14015 #endif
14016
14017         if (debug_options.handle_sigint)
14018                 add_signal_handler (SIGINT, sigint_signal_handler);
14019
14020         add_signal_handler (SIGFPE, sigfpe_signal_handler);
14021         add_signal_handler (SIGQUIT, sigquit_signal_handler);
14022         add_signal_handler (SIGILL, sigill_signal_handler);
14023         add_signal_handler (SIGBUS, sigsegv_signal_handler);
14024         if (mono_jit_trace_calls != NULL)
14025                 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
14026
14027         add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
14028         /* it seems to have become a common bug for some programs that run as parents
14029          * of many processes to block signal delivery for real time signals.
14030          * We try to detect and work around their breakage here.
14031          */
14032         sigemptyset (&signal_set);
14033         sigaddset (&signal_set, mono_thread_get_abort_signal ());
14034         sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
14035
14036         signal (SIGPIPE, SIG_IGN);
14037
14038         add_signal_handler (SIGABRT, sigabrt_signal_handler);
14039
14040         /* catch SIGSEGV */
14041         add_signal_handler (SIGSEGV, sigsegv_signal_handler);
14042 #endif /* PLATFORM_WIN32 */
14043 }
14044
14045 static void
14046 mono_runtime_cleanup_handlers (void)
14047 {
14048 #ifdef PLATFORM_WIN32
14049         win32_seh_cleanup();
14050 #else
14051         if (debug_options.handle_sigint)
14052                 remove_signal_handler (SIGINT);
14053
14054         remove_signal_handler (SIGFPE);
14055         remove_signal_handler (SIGQUIT);
14056         remove_signal_handler (SIGILL);
14057         remove_signal_handler (SIGBUS);
14058         if (mono_jit_trace_calls != NULL)
14059                 remove_signal_handler (SIGUSR2);
14060
14061         remove_signal_handler (mono_thread_get_abort_signal ());
14062
14063         remove_signal_handler (SIGABRT);
14064
14065         remove_signal_handler (SIGSEGV);
14066 #endif /* PLATFORM_WIN32 */
14067 }
14068
14069
14070 #ifdef HAVE_LINUX_RTC_H
14071 #include <linux/rtc.h>
14072 #include <sys/ioctl.h>
14073 #include <fcntl.h>
14074 static int rtc_fd = -1;
14075
14076 static int
14077 enable_rtc_timer (gboolean enable)
14078 {
14079         int flags;
14080         flags = fcntl (rtc_fd, F_GETFL);
14081         if (flags < 0) {
14082                 perror ("getflags");
14083                 return 0;
14084         }
14085         if (enable)
14086                 flags |= FASYNC;
14087         else
14088                 flags &= ~FASYNC;
14089         if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
14090                 perror ("setflags");
14091                 return 0;
14092         }
14093         return 1;
14094 }
14095 #endif
14096
14097 #ifdef PLATFORM_WIN32
14098 static HANDLE win32_main_thread;
14099 static MMRESULT win32_timer;
14100
14101 static void CALLBACK
14102 win32_time_proc (UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
14103 {
14104         CONTEXT context;
14105
14106         context.ContextFlags = CONTEXT_CONTROL;
14107         if (GetThreadContext (win32_main_thread, &context)) {
14108 #ifdef _WIN64
14109                 mono_profiler_stat_hit ((guchar *) context.Rip, &context);
14110 #else
14111                 mono_profiler_stat_hit ((guchar *) context.Eip, &context);
14112 #endif
14113         }
14114 }
14115 #endif
14116
14117 static void
14118 setup_stat_profiler (void)
14119 {
14120 #ifdef ITIMER_PROF
14121         struct itimerval itval;
14122         static int inited = 0;
14123 #ifdef HAVE_LINUX_RTC_H
14124         const char *rtc_freq;
14125         if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
14126                 int freq = 0;
14127                 inited = 1;
14128                 if (*rtc_freq)
14129                         freq = atoi (rtc_freq);
14130                 if (!freq)
14131                         freq = 1024;
14132                 rtc_fd = open ("/dev/rtc", O_RDONLY);
14133                 if (rtc_fd == -1) {
14134                         perror ("open /dev/rtc");
14135                         return;
14136                 }
14137                 add_signal_handler (SIGPROF, sigprof_signal_handler);
14138                 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
14139                         perror ("set rtc freq");
14140                         return;
14141                 }
14142                 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
14143                         perror ("start rtc");
14144                         return;
14145                 }
14146                 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
14147                         perror ("setsig");
14148                         return;
14149                 }
14150                 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
14151                         perror ("setown");
14152                         return;
14153                 }
14154                 enable_rtc_timer (TRUE);
14155                 return;
14156         }
14157         if (rtc_fd >= 0)
14158                 return;
14159 #endif
14160
14161         itval.it_interval.tv_usec = 999;
14162         itval.it_interval.tv_sec = 0;
14163         itval.it_value = itval.it_interval;
14164         setitimer (ITIMER_PROF, &itval, NULL);
14165         if (inited)
14166                 return;
14167         inited = 1;
14168         add_signal_handler (SIGPROF, sigprof_signal_handler);
14169 #elif defined (PLATFORM_WIN32)
14170         static int inited = 0;
14171         TIMECAPS timecaps;
14172
14173         if (inited)
14174                 return;
14175
14176         inited = 1;
14177         if (timeGetDevCaps (&timecaps, sizeof (timecaps)) != TIMERR_NOERROR)
14178                 return;
14179
14180         if ((win32_main_thread = OpenThread (READ_CONTROL | THREAD_GET_CONTEXT, FALSE, GetCurrentThreadId ())) == NULL)
14181                 return;
14182
14183         if (timeBeginPeriod (1) != TIMERR_NOERROR)
14184                 return;
14185
14186         if ((win32_timer = timeSetEvent (1, 0, win32_time_proc, 0, TIME_PERIODIC)) == 0) {
14187                 timeEndPeriod (1);
14188                 return;
14189         }
14190 #endif
14191 }
14192
14193 /* mono_jit_create_remoting_trampoline:
14194  * @method: pointer to the method info
14195  *
14196  * Creates a trampoline which calls the remoting functions. This
14197  * is used in the vtable of transparent proxies.
14198  * 
14199  * Returns: a pointer to the newly created code 
14200  */
14201 static gpointer
14202 mono_jit_create_remoting_trampoline (MonoDomain *domain, MonoMethod *method, MonoRemotingTarget target)
14203 {
14204         MonoMethod *nm;
14205         guint8 *addr = NULL;
14206
14207         if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && mono_method_signature (method)->generic_param_count) {
14208                 return mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING,
14209                         domain, NULL);
14210         }
14211
14212         if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || 
14213             (mono_method_signature (method)->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
14214                 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
14215                 addr = mono_compile_method (nm);
14216         } else {
14217                 addr = mono_compile_method (method);
14218         }
14219         return mono_get_addr_from_ftnptr (addr);
14220 }
14221
14222 #ifdef MONO_ARCH_HAVE_IMT
14223 static gpointer
14224 mini_get_imt_trampoline (void)
14225 {
14226         static gpointer tramp = NULL;
14227         if (!tramp)
14228                 tramp = mono_create_specific_trampoline (MONO_FAKE_IMT_METHOD, MONO_TRAMPOLINE_JIT, mono_get_root_domain (), NULL);
14229         return tramp;
14230 }
14231 #endif
14232
14233 #ifdef MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
14234 gpointer
14235 mini_get_vtable_trampoline (void)
14236 {
14237         static gpointer tramp = NULL;
14238         if (!tramp)
14239                 tramp = mono_create_specific_trampoline (MONO_FAKE_VTABLE_METHOD, MONO_TRAMPOLINE_JIT, mono_get_root_domain (), NULL);
14240         return tramp;
14241 }
14242 #endif
14243
14244 static void
14245 mini_parse_debug_options (void)
14246 {
14247         char *options = getenv ("MONO_DEBUG");
14248         gchar **args, **ptr;
14249         
14250         if (!options)
14251                 return;
14252
14253         args = g_strsplit (options, ",", -1);
14254
14255         for (ptr = args; ptr && *ptr; ptr++) {
14256                 const char *arg = *ptr;
14257
14258                 if (!strcmp (arg, "handle-sigint"))
14259                         debug_options.handle_sigint = TRUE;
14260                 else if (!strcmp (arg, "keep-delegates"))
14261                         debug_options.keep_delegates = TRUE;
14262                 else if (!strcmp (arg, "collect-pagefault-stats"))
14263                         debug_options.collect_pagefault_stats = TRUE;
14264                 else if (!strcmp (arg, "break-on-unverified"))
14265                         debug_options.break_on_unverified = TRUE;
14266                 else if (!strcmp (arg, "no-gdb-backtrace"))
14267                         debug_options.no_gdb_backtrace = TRUE;
14268                 else if (!strcmp (arg, "dont-free-domains"))
14269                         mono_dont_free_domains = TRUE;
14270                 else {
14271                         fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
14272                         fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'collect-pagefault-stats', 'break-on-unverified', 'no-gdb-backtrace', 'dont-free-domains'\n");
14273                         exit (1);
14274                 }
14275         }
14276
14277         g_strfreev (args);
14278 }
14279
14280 MonoDebugOptions *
14281 mini_get_debug_options (void)
14282 {
14283         return &debug_options;
14284 }
14285  
14286 static void
14287 mini_create_jit_domain_info (MonoDomain *domain)
14288 {
14289         MonoJitDomainInfo *info = g_new0 (MonoJitDomainInfo, 1);
14290
14291         info->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
14292         info->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
14293         info->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
14294         info->delegate_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
14295
14296         domain->runtime_info = info;
14297 }
14298
14299 static void
14300 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
14301 {
14302         g_slist_free (value);
14303 }
14304
14305 static void
14306 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
14307 {
14308         MonoJitDynamicMethodInfo *di = value;
14309         mono_code_manager_destroy (di->code_mp);
14310         g_free (di);
14311 }
14312
14313 static void
14314 mini_free_jit_domain_info (MonoDomain *domain)
14315 {
14316         MonoJitDomainInfo *info = domain_jit_info (domain);
14317
14318         if (info->jump_target_hash) {
14319                 g_hash_table_foreach (info->jump_target_hash, delete_jump_list, NULL);
14320                 g_hash_table_destroy (info->jump_target_hash);
14321         }
14322         if (info->jump_target_got_slot_hash) {
14323                 g_hash_table_foreach (info->jump_target_got_slot_hash, delete_jump_list, NULL);
14324                 g_hash_table_destroy (info->jump_target_got_slot_hash);
14325         }
14326         if (info->dynamic_code_hash) {
14327                 g_hash_table_foreach (info->dynamic_code_hash, dynamic_method_info_free, NULL);
14328                 g_hash_table_destroy (info->dynamic_code_hash);
14329         }
14330         if (info->method_code_hash)
14331                 g_hash_table_destroy (info->method_code_hash);
14332         g_hash_table_destroy (info->class_init_trampoline_hash);
14333         g_hash_table_destroy (info->jump_trampoline_hash);
14334         g_hash_table_destroy (info->jit_trampoline_hash);
14335         g_hash_table_destroy (info->delegate_trampoline_hash);
14336
14337         g_free (domain->runtime_info);
14338         domain->runtime_info = NULL;
14339 }
14340
14341 MonoDomain *
14342 mini_init (const char *filename, const char *runtime_version)
14343 {
14344         MonoDomain *domain;
14345
14346         MONO_PROBE_VES_INIT_BEGIN ();
14347
14348 #ifdef __linux__
14349         if (access ("/proc/self/maps", F_OK) != 0) {
14350                 g_print ("Mono requires /proc to be mounted.\n");
14351                 exit (1);
14352         }
14353 #endif
14354
14355         /* Happens when using the embedding interface */
14356         if (!default_opt_set)
14357                 default_opt = mono_parse_default_optimizations (NULL);
14358
14359         InitializeCriticalSection (&jit_mutex);
14360
14361         if (!global_codeman)
14362                 global_codeman = mono_code_manager_new ();
14363         jit_icall_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
14364
14365         mono_arch_cpu_init ();
14366
14367         mono_arch_init ();
14368
14369         mono_trampolines_init ();
14370
14371         if (!g_thread_supported ())
14372                 g_thread_init (NULL);
14373
14374         if (getenv ("MONO_DEBUG") != NULL)
14375                 mini_parse_debug_options ();
14376
14377         mono_gc_base_init ();
14378
14379         mono_jit_tls_id = TlsAlloc ();
14380         setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
14381
14382 #ifndef DISABLE_JIT
14383         mono_burg_init ();
14384 #endif
14385
14386         if (default_opt & MONO_OPT_AOT)
14387                 mono_aot_init ();
14388
14389         mono_runtime_install_handlers ();
14390         mono_threads_install_cleanup (mini_thread_cleanup);
14391
14392 #ifdef MONO_ARCH_HAVE_NOTIFY_PENDING_EXC
14393         // This is experimental code so provide an env var to switch it off
14394         if (getenv ("MONO_DISABLE_PENDING_EXCEPTIONS")) {
14395                 printf ("MONO_DISABLE_PENDING_EXCEPTIONS env var set.\n");
14396         } else {
14397                 check_for_pending_exc = FALSE;
14398                 mono_threads_install_notify_pending_exc (mono_arch_notify_pending_exc);
14399         }
14400 #endif
14401
14402 #define JIT_TRAMPOLINES_WORK
14403 #ifdef JIT_TRAMPOLINES_WORK
14404         mono_install_compile_method (mono_jit_compile_method);
14405         mono_install_free_method (mono_jit_free_method);
14406         mono_install_trampoline (mono_create_jit_trampoline);
14407         mono_install_jump_trampoline (mono_create_jump_trampoline);
14408         mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
14409         mono_install_delegate_trampoline (mono_create_delegate_trampoline);
14410         mono_install_create_domain_hook (mini_create_jit_domain_info);
14411         mono_install_free_domain_hook (mini_free_jit_domain_info);
14412 #endif
14413 #define JIT_INVOKE_WORKS
14414 #ifdef JIT_INVOKE_WORKS
14415         mono_install_runtime_invoke (mono_jit_runtime_invoke);
14416 #endif
14417         mono_install_stack_walk (mono_jit_walk_stack);
14418         mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
14419         mono_install_get_class_from_name (mono_aot_get_class_from_name);
14420         mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
14421
14422         if (debug_options.collect_pagefault_stats) {
14423                 mono_aot_set_make_unreadable (TRUE);
14424         }
14425
14426         if (runtime_version)
14427                 domain = mono_init_version (filename, runtime_version);
14428         else
14429                 domain = mono_init_from_assembly (filename, filename);
14430
14431         if (mono_aot_only) {
14432                 /* The IMT tables are very dynamic thus they are hard to AOT */
14433                 mono_use_imt = FALSE;
14434                 /* This helps catch code allocation requests */
14435                 mono_code_manager_set_read_only (domain->code_mp);
14436         }
14437
14438 #ifdef MONO_ARCH_HAVE_IMT
14439         if (mono_use_imt) {
14440                 mono_install_imt_thunk_builder (mono_arch_build_imt_thunk);
14441                 mono_install_imt_trampoline (mini_get_imt_trampoline ());
14442 #if MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
14443                 mono_install_vtable_trampoline (mini_get_vtable_trampoline ());
14444 #endif
14445         }
14446 #endif
14447
14448         /* This must come after mono_init () in the aot-only case */
14449         mono_exceptions_init ();
14450         mono_install_handler (mono_get_throw_exception ());
14451
14452         mono_icall_init ();
14453
14454         mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", 
14455                                 ves_icall_get_frame_info);
14456         mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", 
14457                                 ves_icall_get_trace);
14458         mono_add_internal_call ("System.Exception::get_trace", 
14459                                 ves_icall_System_Exception_get_trace);
14460         mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
14461                                 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
14462         mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
14463                                 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
14464         mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", 
14465                                 mono_runtime_install_handlers);
14466
14467
14468         create_helper_signature ();
14469
14470 #define JIT_CALLS_WORK
14471 #ifdef JIT_CALLS_WORK
14472         /* Needs to be called here since register_jit_icall depends on it */
14473         mono_marshal_init ();
14474
14475         mono_arch_register_lowlevel_calls ();
14476         register_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
14477         register_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
14478         register_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
14479         register_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
14480         register_icall (mono_get_lmf_addr, "mono_get_lmf_addr", "ptr", TRUE);
14481         register_icall (mono_jit_thread_attach, "mono_jit_thread_attach", "void", TRUE);
14482         register_icall (mono_domain_get, "mono_domain_get", "ptr", TRUE);
14483
14484         register_icall (mono_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
14485         register_icall (mono_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
14486         register_icall (mono_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE); 
14487 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
14488         register_icall (mono_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception", 
14489                                  "void ptr", TRUE);
14490 #endif
14491         register_icall (mono_thread_get_undeniable_exception, "mono_thread_get_undeniable_exception", "object", FALSE);
14492         register_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", "void", FALSE);
14493         register_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", "void", FALSE);
14494         register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
14495         register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
14496
14497         /* 
14498          * NOTE, NOTE, NOTE, NOTE:
14499          * when adding emulation for some opcodes, remember to also add a dummy
14500          * rule to the burg files, because we need the arity information to be correct.
14501          */
14502 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
14503         mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", "long long long", mono_llmult, TRUE);
14504         mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", "long long long", mono_lldiv, FALSE);
14505         mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", "long long long", mono_lldiv_un, FALSE);
14506         mono_register_opcode_emulation (OP_LREM, "__emul_lrem", "long long long", mono_llrem, FALSE);
14507         mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", "long long long", mono_llrem_un, FALSE);
14508         mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un, FALSE);
14509         mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", "long long long", mono_llmult_ovf, FALSE);
14510 #endif
14511
14512 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
14513         mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", "long long int32", mono_lshl, TRUE);
14514         mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", "long long int32", mono_lshr, TRUE);
14515         mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", "long long int32", mono_lshr_un, TRUE);
14516 #endif
14517
14518 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
14519         mono_register_opcode_emulation (CEE_DIV, "__emul_idiv", "int32 int32 int32", mono_idiv, FALSE);
14520         mono_register_opcode_emulation (CEE_DIV_UN, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un, FALSE);
14521         mono_register_opcode_emulation (CEE_REM, "__emul_irem", "int32 int32 int32", mono_irem, FALSE);
14522         mono_register_opcode_emulation (CEE_REM_UN, "__emul_irem_un", "int32 int32 int32", mono_irem_un, FALSE);
14523         mono_register_opcode_emulation (OP_IDIV, "__emul_op_idiv", "int32 int32 int32", mono_idiv, FALSE);
14524         mono_register_opcode_emulation (OP_IDIV_UN, "__emul_op_idiv_un", "int32 int32 int32", mono_idiv_un, FALSE);
14525         mono_register_opcode_emulation (OP_IREM, "__emul_op_irem", "int32 int32 int32", mono_irem, FALSE);
14526         mono_register_opcode_emulation (OP_IREM_UN, "__emul_op_irem_un", "int32 int32 int32", mono_irem_un, FALSE);
14527 #endif
14528
14529 #ifdef MONO_ARCH_EMULATE_MUL_DIV
14530         mono_register_opcode_emulation (CEE_MUL_OVF, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf, FALSE);
14531         mono_register_opcode_emulation (CEE_MUL_OVF_UN, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, FALSE);
14532         mono_register_opcode_emulation (CEE_MUL, "__emul_imul", "int32 int32 int32", mono_imul, TRUE);
14533         mono_register_opcode_emulation (OP_IMUL, "__emul_op_imul", "int32 int32 int32", mono_imul, TRUE);
14534         mono_register_opcode_emulation (OP_IMUL_OVF, "__emul_op_imul_ovf", "int32 int32 int32", mono_imul_ovf, FALSE);
14535         mono_register_opcode_emulation (OP_IMUL_OVF_UN, "__emul_op_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, FALSE);
14536 #endif
14537 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
14538         mono_register_opcode_emulation (OP_FDIV, "__emul_fdiv", "double double double", mono_fdiv, FALSE);
14539 #endif
14540
14541         mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8, FALSE);
14542         mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4, FALSE);
14543         mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8, FALSE);
14544         mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8, FALSE);
14545
14546 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
14547         mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", "long double", mono_fconv_i8, FALSE);
14548 #endif
14549 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
14550         mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un, FALSE);
14551         mono_register_opcode_emulation (OP_ICONV_TO_R_UN, "__emul_iconv_to_r_un", "double int32", mono_conv_to_r8_un, FALSE);
14552 #endif
14553 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
14554         mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8, FALSE);
14555 #endif
14556 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
14557         mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4, FALSE);
14558 #endif
14559 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
14560         mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, FALSE);
14561 #endif
14562 #ifdef MONO_ARCH_EMULATE_FREM
14563         mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, FALSE);
14564 #endif
14565
14566 #ifdef MONO_ARCH_SOFT_FLOAT
14567         mono_register_opcode_emulation (OP_FSUB, "__emul_fsub", "double double double", mono_fsub, FALSE);
14568         mono_register_opcode_emulation (OP_FADD, "__emul_fadd", "double double double", mono_fadd, FALSE);
14569         mono_register_opcode_emulation (OP_FMUL, "__emul_fmul", "double double double", mono_fmul, FALSE);
14570         mono_register_opcode_emulation (OP_FNEG, "__emul_fneg", "double double", mono_fneg, FALSE);
14571         mono_register_opcode_emulation (CEE_CONV_R8, "__emul_conv_r8", "double int32", mono_conv_to_r8, FALSE);
14572         mono_register_opcode_emulation (OP_ICONV_TO_R8, "__emul_iconv_to_r8", "double int32", mono_conv_to_r8, FALSE);
14573         mono_register_opcode_emulation (CEE_CONV_R4, "__emul_conv_r4", "double int32", mono_conv_to_r4, FALSE);
14574         mono_register_opcode_emulation (OP_ICONV_TO_R4, "__emul_iconv_to_r4", "double int32", mono_conv_to_r4, FALSE);
14575         mono_register_opcode_emulation (OP_FCONV_TO_R4, "__emul_fconv_to_r4", "double double", mono_fconv_r4, FALSE);
14576         mono_register_opcode_emulation (OP_FCONV_TO_I1, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1, FALSE);
14577         mono_register_opcode_emulation (OP_FCONV_TO_I2, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2, FALSE);
14578         mono_register_opcode_emulation (OP_FCONV_TO_I4, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4, FALSE);
14579         mono_register_opcode_emulation (OP_FCONV_TO_U1, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1, FALSE);
14580         mono_register_opcode_emulation (OP_FCONV_TO_U2, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2, FALSE);
14581
14582         mono_register_opcode_emulation (OP_FBEQ, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq, FALSE);
14583         mono_register_opcode_emulation (OP_FBLT, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt, FALSE);
14584         mono_register_opcode_emulation (OP_FBGT, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt, FALSE);
14585         mono_register_opcode_emulation (OP_FBLE, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le, FALSE);
14586         mono_register_opcode_emulation (OP_FBGE, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge, FALSE);
14587         mono_register_opcode_emulation (OP_FBNE_UN, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un, FALSE);
14588         mono_register_opcode_emulation (OP_FBLT_UN, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un, FALSE);
14589         mono_register_opcode_emulation (OP_FBGT_UN, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un, FALSE);
14590         mono_register_opcode_emulation (OP_FBLE_UN, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un, FALSE);
14591         mono_register_opcode_emulation (OP_FBGE_UN, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un, FALSE);
14592
14593         mono_register_opcode_emulation (OP_FCEQ, "__emul_fcmp_ceq", "uint32 double double", mono_fceq, FALSE);
14594         mono_register_opcode_emulation (OP_FCGT, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt, FALSE);
14595         mono_register_opcode_emulation (OP_FCGT_UN, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un, FALSE);
14596         mono_register_opcode_emulation (OP_FCLT, "__emul_fcmp_clt", "uint32 double double", mono_fclt, FALSE);
14597         mono_register_opcode_emulation (OP_FCLT_UN, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un, FALSE);
14598
14599         register_icall (mono_fload_r4, "mono_fload_r4", "double ptr", FALSE);
14600         register_icall (mono_fstore_r4, "mono_fstore_r4", "void double ptr", FALSE);
14601         register_icall (mono_fload_r4_arg, "mono_fload_r4_arg", "uint32 double", FALSE);
14602 #endif
14603
14604 #if SIZEOF_VOID_P == 4
14605         mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, TRUE);
14606 #endif
14607
14608         /* other jit icalls */
14609         register_icall (mono_delegate_ctor, "mono_delegate_ctor", "void object object ptr", FALSE);
14610         register_icall (mono_class_static_field_address , "mono_class_static_field_address", 
14611                                  "ptr ptr ptr", FALSE);
14612         register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
14613         register_icall (mono_ldtoken_wrapper_generic_shared, "mono_ldtoken_wrapper_generic_shared",
14614                 "ptr ptr ptr ptr", FALSE);
14615         register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
14616         register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
14617         register_icall (mono_helper_stelem_ref_check, "helper_stelem_ref_check", "void object object", FALSE);
14618         register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
14619         register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
14620         register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
14621         register_icall (mono_array_new_specific, "mono_array_new_specific", "object ptr int32", FALSE);
14622         register_icall (mono_runtime_class_init, "mono_runtime_class_init", "void ptr", FALSE);
14623         register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
14624         register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
14625         register_icall (mono_ldvirtfn_gshared, "mono_ldvirtfn_gshared", "ptr object ptr", FALSE);
14626         register_icall (mono_helper_compile_generic_method, "compile_generic_method", "ptr object ptr ptr", FALSE);
14627         register_icall (mono_helper_ldstr, "helper_ldstr", "object ptr int", FALSE);
14628         register_icall (mono_helper_ldstr_mscorlib, "helper_ldstr_mscorlib", "object int", FALSE);
14629         register_icall (mono_helper_newobj_mscorlib, "helper_newobj_mscorlib", "object int", FALSE);
14630         register_icall (mono_value_copy, "mono_value_copy", "void ptr ptr ptr", FALSE);
14631         register_icall (mono_object_castclass, "mono_object_castclass", "object object ptr", FALSE);
14632         register_icall (mono_break, "mono_break", NULL, TRUE);
14633         register_icall (mono_create_corlib_exception_0, "mono_create_corlib_exception_0", "object int", TRUE);
14634         register_icall (mono_create_corlib_exception_1, "mono_create_corlib_exception_1", "object int object", TRUE);
14635         register_icall (mono_create_corlib_exception_2, "mono_create_corlib_exception_2", "object int object object", TRUE);
14636         register_icall (mono_array_new_1, "mono_array_new_1", "object ptr int", FALSE);
14637         register_icall (mono_array_new_2, "mono_array_new_2", "object ptr int int", FALSE);
14638 #endif
14639
14640         mono_generic_sharing_init ();
14641
14642 #ifdef MONO_ARCH_SIMD_INTRINSICS
14643         mono_simd_intrinsics_init ();
14644 #endif
14645
14646         if (mono_compile_aot)
14647                 /* 
14648                  * Avoid running managed code when AOT compiling, since the platform
14649                  * might only support aot-only execution.
14650                  */
14651                 mono_runtime_set_no_exec (TRUE);
14652
14653 #define JIT_RUNTIME_WORKS
14654 #ifdef JIT_RUNTIME_WORKS
14655         mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
14656         mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
14657         mono_thread_attach (domain);
14658 #endif
14659
14660         mono_profiler_runtime_initialized ();
14661         
14662         MONO_PROBE_VES_INIT_END ();
14663         
14664         return domain;
14665 }
14666
14667 MonoJitStats mono_jit_stats = {0};
14668
14669 static void 
14670 print_jit_stats (void)
14671 {
14672         if (mono_jit_stats.enabled) {
14673                 g_print ("Mono Jit statistics\n");
14674                 g_print ("Compiled methods:       %ld\n", mono_jit_stats.methods_compiled);
14675                 g_print ("Methods from AOT:       %ld\n", mono_jit_stats.methods_aot);
14676                 g_print ("Methods cache lookup:   %ld\n", mono_jit_stats.methods_lookups);
14677                 g_print ("Method trampolines:     %ld\n", mono_jit_stats.method_trampolines);
14678                 g_print ("Basic blocks:           %ld\n", mono_jit_stats.basic_blocks);
14679                 g_print ("Max basic blocks:       %ld\n", mono_jit_stats.max_basic_blocks);
14680                 g_print ("Allocated vars:         %ld\n", mono_jit_stats.allocate_var);
14681                 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
14682                 g_print ("Native code size:       %ld\n", mono_jit_stats.native_code_size);
14683                 g_print ("Max code size ratio:    %.2f (%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
14684                                  mono_jit_stats.max_ratio_method);
14685                 g_print ("Biggest method:         %ld (%s)\n", mono_jit_stats.biggest_method_size,
14686                                  mono_jit_stats.biggest_method);
14687                 g_print ("Code reallocs:          %ld\n", mono_jit_stats.code_reallocs);
14688                 g_print ("Allocated code size:    %ld\n", mono_jit_stats.allocated_code_size);
14689                 g_print ("Inlineable methods:     %ld\n", mono_jit_stats.inlineable_methods);
14690                 g_print ("Inlined methods:        %ld\n", mono_jit_stats.inlined_methods);
14691                 g_print ("Regvars:                %ld\n", mono_jit_stats.regvars);
14692                 g_print ("Locals stack size:      %ld\n", mono_jit_stats.locals_stack_size);
14693
14694                 g_print ("\nCreated object count:   %ld\n", mono_stats.new_object_count);
14695                 g_print ("Delegates created:      %ld\n", mono_stats.delegate_creations);
14696                 g_print ("Initialized classes:    %ld\n", mono_stats.initialized_class_count);
14697                 g_print ("Used classes:           %ld\n", mono_stats.used_class_count);
14698                 g_print ("Generic vtables:        %ld\n", mono_stats.generic_vtable_count);
14699                 g_print ("Methods:                %ld\n", mono_stats.method_count);
14700                 g_print ("Static data size:       %ld\n", mono_stats.class_static_data_size);
14701                 g_print ("VTable data size:       %ld\n", mono_stats.class_vtable_size);
14702                 g_print ("Mscorlib mempool size:  %d\n", mono_mempool_get_allocated (mono_defaults.corlib->mempool));
14703
14704                 g_print ("\nGeneric instances:      %ld\n", mono_stats.generic_instance_count);
14705                 g_print ("Initialized classes:    %ld\n", mono_stats.generic_class_count);
14706                 g_print ("Inflated methods:       %ld / %ld\n", mono_stats.inflated_method_count_2,
14707                          mono_stats.inflated_method_count);
14708                 g_print ("Inflated types:         %ld\n", mono_stats.inflated_type_count);
14709                 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
14710                 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats.generic_virtual_invocations);
14711
14712                 g_print ("Sharable generic methods: %ld\n", mono_stats.generics_sharable_methods);
14713                 g_print ("Unsharable generic methods: %ld\n", mono_stats.generics_unsharable_methods);
14714                 g_print ("Shared generic methods: %ld\n", mono_stats.generics_shared_methods);
14715
14716                 g_print ("Dynamic code allocs:    %ld\n", mono_stats.dynamic_code_alloc_count);
14717                 g_print ("Dynamic code bytes:     %ld\n", mono_stats.dynamic_code_bytes_count);
14718                 g_print ("Dynamic code frees:     %ld\n", mono_stats.dynamic_code_frees_count);
14719
14720                 g_print ("IMT tables size:        %ld\n", mono_stats.imt_tables_size);
14721                 g_print ("IMT number of tables:   %ld\n", mono_stats.imt_number_of_tables);
14722                 g_print ("IMT number of methods:  %ld\n", mono_stats.imt_number_of_methods);
14723                 g_print ("IMT used slots:         %ld\n", mono_stats.imt_used_slots);
14724                 g_print ("IMT colliding slots:    %ld\n", mono_stats.imt_slots_with_collisions);
14725                 g_print ("IMT max collisions:     %ld\n", mono_stats.imt_max_collisions_in_slot);
14726                 g_print ("IMT methods at max col: %ld\n", mono_stats.imt_method_count_when_max_collisions);
14727                 g_print ("IMT thunks size:        %ld\n", mono_stats.imt_thunks_size);
14728
14729                 g_print ("JIT info table inserts: %ld\n", mono_stats.jit_info_table_insert_count);
14730                 g_print ("JIT info table removes: %ld\n", mono_stats.jit_info_table_remove_count);
14731                 g_print ("JIT info table lookups: %ld\n", mono_stats.jit_info_table_lookup_count);
14732
14733                 g_print ("Hazardous pointers:     %ld\n", mono_stats.hazardous_pointer_count);
14734 #ifdef HAVE_SGEN_GC
14735                 g_print ("Minor GC collections:   %ld\n", mono_stats.minor_gc_count);
14736                 g_print ("Major GC collections:   %ld\n", mono_stats.major_gc_count);
14737                 g_print ("Minor GC time in msecs: %lf\n", (double)mono_stats.minor_gc_time_usecs / 1000.0);
14738                 g_print ("Major GC time in msecs: %lf\n", (double)mono_stats.major_gc_time_usecs / 1000.0);
14739 #endif
14740                 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS) {
14741                         g_print ("\nDecl security check   : %ld\n", mono_jit_stats.cas_declsec_check);
14742                         g_print ("LinkDemand (user)     : %ld\n", mono_jit_stats.cas_linkdemand);
14743                         g_print ("LinkDemand (icall)    : %ld\n", mono_jit_stats.cas_linkdemand_icall);
14744                         g_print ("LinkDemand (pinvoke)  : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
14745                         g_print ("LinkDemand (aptc)     : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
14746                         g_print ("Demand (code gen)     : %ld\n", mono_jit_stats.cas_demand_generation);
14747                 }
14748                 if (debug_options.collect_pagefault_stats) {
14749                         g_print ("AOT pagefaults        : %d\n", mono_aot_get_n_pagefaults ());
14750                 }
14751
14752                 g_free (mono_jit_stats.max_ratio_method);
14753                 mono_jit_stats.max_ratio_method = NULL;
14754                 g_free (mono_jit_stats.biggest_method);
14755                 mono_jit_stats.biggest_method = NULL;
14756         }
14757 }
14758
14759 void
14760 mini_cleanup (MonoDomain *domain)
14761 {
14762 #ifdef HAVE_LINUX_RTC_H
14763         if (rtc_fd >= 0)
14764                 enable_rtc_timer (FALSE);
14765 #endif
14766
14767         /* 
14768          * mono_runtime_cleanup() and mono_domain_finalize () need to
14769          * be called early since they need the execution engine still
14770          * fully working (mono_domain_finalize may invoke managed finalizers
14771          * and mono_runtime_cleanup will wait for other threads to finish).
14772          */
14773         mono_domain_finalize (domain, 2000);
14774
14775         /* This accesses metadata so needs to be called before runtime shutdown */
14776         print_jit_stats ();
14777
14778         mono_runtime_cleanup (domain);
14779
14780         mono_profiler_shutdown ();
14781
14782         mono_icall_cleanup ();
14783
14784         mono_runtime_cleanup_handlers ();
14785
14786         mono_domain_free (domain, TRUE);
14787
14788         mono_debugger_cleanup ();
14789
14790         mono_trampolines_cleanup ();
14791
14792         if (!mono_dont_free_global_codeman)
14793                 mono_code_manager_destroy (global_codeman);
14794         g_hash_table_destroy (jit_icall_name_hash);
14795         g_free (emul_opcode_map);
14796
14797         mono_arch_cleanup ();
14798
14799         mono_cleanup ();
14800
14801         mono_trace_cleanup ();
14802
14803         mono_counters_dump (-1, stdout);
14804
14805         if (mono_inject_async_exc_method)
14806                 mono_method_desc_free (mono_inject_async_exc_method);
14807
14808         TlsFree(mono_jit_tls_id);
14809
14810         DeleteCriticalSection (&jit_mutex);
14811
14812         DeleteCriticalSection (&mono_delegate_section);
14813 }
14814
14815 void
14816 mono_set_defaults (int verbose_level, guint32 opts)
14817 {
14818         mini_verbose = verbose_level;
14819         default_opt = opts;
14820         default_opt_set = TRUE;
14821 }
14822
14823 static void
14824 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
14825 {
14826         GHashTable *assemblies = (GHashTable*)user_data;
14827         MonoImage *image = mono_assembly_get_image (ass);
14828         MonoMethod *method, *invoke;
14829         int i, count = 0;
14830
14831         if (g_hash_table_lookup (assemblies, ass))
14832                 return;
14833
14834         g_hash_table_insert (assemblies, ass, ass);
14835
14836         if (mini_verbose > 0)
14837                 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
14838
14839         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
14840                 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
14841                 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
14842                         continue;
14843
14844                 count++;
14845                 if (mini_verbose > 1) {
14846                         char * desc = mono_method_full_name (method, TRUE);
14847                         g_print ("Compiling %d %s\n", count, desc);
14848                         g_free (desc);
14849                 }
14850                 mono_compile_method (method);
14851                 if (strcmp (method->name, "Finalize") == 0) {
14852                         invoke = mono_marshal_get_runtime_invoke (method);
14853                         mono_compile_method (invoke);
14854                 }
14855                 if (method->klass->marshalbyref && mono_method_signature (method)->hasthis) {
14856                         invoke = mono_marshal_get_remoting_invoke_with_check (method);
14857                         mono_compile_method (invoke);
14858                 }
14859         }
14860
14861         /* Load and precompile referenced assemblies as well */
14862         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
14863                 mono_assembly_load_reference (image, i);
14864                 if (image->references [i])
14865                         mono_precompile_assembly (image->references [i], assemblies);
14866         }
14867 }
14868
14869 void mono_precompile_assemblies ()
14870 {
14871         GHashTable *assemblies = g_hash_table_new (NULL, NULL);
14872
14873         mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
14874
14875         g_hash_table_destroy (assemblies);
14876 }