Tue Jul 10 17:33:12 CEST 2007 Paolo Molaro <lupus@ximian.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 PLATFORM_WIN32
30 #define _WIN32_WINNT 0x0500
31 #endif
32
33 #ifdef HAVE_VALGRIND_MEMCHECK_H
34 #include <valgrind/memcheck.h>
35 #endif
36
37 #include <mono/metadata/assembly.h>
38 #include <mono/metadata/loader.h>
39 #include <mono/metadata/cil-coff.h>
40 #include <mono/metadata/tabledefs.h>
41 #include <mono/metadata/class.h>
42 #include <mono/metadata/object.h>
43 #include <mono/metadata/exception.h>
44 #include <mono/metadata/opcodes.h>
45 #include <mono/metadata/mono-endian.h>
46 #include <mono/metadata/tokentype.h>
47 #include <mono/metadata/tabledefs.h>
48 #include <mono/metadata/threads.h>
49 #include <mono/metadata/marshal.h>
50 #include <mono/metadata/socket-io.h>
51 #include <mono/metadata/appdomain.h>
52 #include <mono/metadata/debug-helpers.h>
53 #include <mono/io-layer/io-layer.h>
54 #include "mono/metadata/profiler.h"
55 #include <mono/metadata/profiler-private.h>
56 #include <mono/metadata/mono-config.h>
57 #include <mono/metadata/environment.h>
58 #include <mono/metadata/mono-debug.h>
59 #include <mono/metadata/monitor.h>
60 #include <mono/metadata/security-manager.h>
61 #include <mono/metadata/threads-types.h>
62 #include <mono/metadata/rawbuffer.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/os/gc_wrapper.h>
68
69 #include "mini.h"
70 #include <string.h>
71 #include <ctype.h>
72 #include "inssel.h"
73 #include "trace.h"
74
75 #include "jit-icalls.h"
76
77 #include "aliasing.h"
78
79 #define BRANCH_COST 100
80 #define INLINE_LENGTH_LIMIT 20
81 #define INLINE_FAILURE do {\
82                 if ((cfg->method != method) && (method->wrapper_type == MONO_WRAPPER_NONE))\
83                         goto inline_failure;\
84         } while (0)
85
86 /* 
87  * this is used to determine when some branch optimizations are possible: we exclude FP compares
88  * because they have weird semantics with NaNs.
89  */
90 #define MONO_IS_COND_BRANCH_OP(ins) (((ins)->opcode >= CEE_BEQ && (ins)->opcode <= CEE_BLT_UN) || ((ins)->opcode >= OP_LBEQ && (ins)->opcode <= OP_LBLT_UN) || ((ins)->opcode >= OP_FBEQ && (ins)->opcode <= OP_FBLT_UN) || ((ins)->opcode >= OP_IBEQ && (ins)->opcode <= OP_IBLT_UN))
91 #define MONO_IS_COND_BRANCH_NOFP(ins) (MONO_IS_COND_BRANCH_OP(ins) && (ins)->inst_left->inst_left->type != STACK_R8)
92
93 #define MONO_IS_BRANCH_OP(ins) (MONO_IS_COND_BRANCH_OP(ins) || ((ins)->opcode == OP_BR) || ((ins)->opcode == OP_BR_REG) || ((ins)->opcode == CEE_SWITCH))
94
95 #define MONO_CHECK_THIS(ins) (mono_method_signature (cfg->method)->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
96
97 static void setup_stat_profiler (void);
98 gboolean  mono_arch_print_tree(MonoInst *tree, int arity);
99 static gpointer mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt);
100 static gpointer mono_jit_compile_method (MonoMethod *method);
101 static gpointer mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method);
102 static gpointer mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method);
103 inline static int mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip);
104
105 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, 
106                           const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier);
107
108 static void dec_foreach (MonoInst *tree, MonoCompile *cfg);
109
110 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
111                    int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args, 
112                    guint inline_offset, gboolean is_virtual_call);
113
114 /* helper methods signature */
115 static MonoMethodSignature *helper_sig_class_init_trampoline = NULL;
116 static MonoMethodSignature *helper_sig_domain_get = NULL;
117
118 static guint32 default_opt = 0;
119 static gboolean default_opt_set = FALSE;
120
121 guint32 mono_jit_tls_id = -1;
122 MonoTraceSpec *mono_jit_trace_calls = NULL;
123 gboolean mono_break_on_exc = FALSE;
124 #ifndef DISABLE_AOT
125 gboolean mono_compile_aot = FALSE;
126 #endif
127 gboolean mono_use_security_manager = FALSE;
128
129 static int mini_verbose = 0;
130
131 #define mono_jit_lock() EnterCriticalSection (&jit_mutex)
132 #define mono_jit_unlock() LeaveCriticalSection (&jit_mutex)
133 static CRITICAL_SECTION jit_mutex;
134
135 static GHashTable *class_init_hash_addr = NULL;
136
137 static MonoCodeManager *global_codeman = NULL;
138
139 static GHashTable *jit_icall_name_hash = NULL;
140
141 static MonoDebugOptions debug_options;
142
143 #ifdef VALGRIND_JIT_REGISTER_MAP
144 static int valgrind_register = 0;
145 #endif
146
147 /*
148  * Address of the trampoline code.  This is used by the debugger to check
149  * whether a method is a trampoline.
150  */
151 guint8* mono_trampoline_code [MONO_TRAMPOLINE_NUM];
152
153 gboolean
154 mono_running_on_valgrind (void)
155 {
156 #ifdef HAVE_VALGRIND_MEMCHECK_H
157                 if (RUNNING_ON_VALGRIND){
158 #ifdef VALGRIND_JIT_REGISTER_MAP
159                         valgrind_register = TRUE;
160 #endif
161                         return TRUE;
162                 } else
163                         return FALSE;
164 #else
165                 return FALSE;
166 #endif
167 }
168
169 /*
170  * mono_create_ftnptr:
171  *
172  *   Given a function address, create a function descriptor for it.
173  * This is only needed on IA64.
174  */
175 gpointer
176 mono_create_ftnptr (MonoDomain *domain, gpointer addr)
177 {
178 #ifdef __ia64__
179         gpointer *desc;
180
181         mono_domain_lock (domain);
182         desc = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
183         mono_domain_unlock (domain);
184
185         desc [0] = addr;
186         desc [1] = NULL;
187
188         return desc;
189 #else
190         return addr;
191 #endif
192 }
193
194 typedef struct {
195         void *ip;
196         MonoMethod *method;
197 } FindTrampUserData;
198
199 static void
200 find_tramp (gpointer key, gpointer value, gpointer user_data)
201 {
202         FindTrampUserData *ud = (FindTrampUserData*)user_data;
203
204         if (value == ud->ip)
205                 ud->method = (MonoMethod*)key;
206 }
207
208 /* debug function */
209 G_GNUC_UNUSED static char*
210 get_method_from_ip (void *ip)
211 {
212         MonoJitInfo *ji;
213         char *method;
214         char *res;
215         MonoDomain *domain = mono_domain_get ();
216         MonoDebugSourceLocation *location;
217         FindTrampUserData user_data;
218         
219         ji = mono_jit_info_table_find (domain, ip);
220         if (!ji) {
221                 user_data.ip = ip;
222                 user_data.method = NULL;
223                 mono_domain_lock (domain);
224                 g_hash_table_foreach (domain->jit_trampoline_hash, find_tramp, &user_data);
225                 mono_domain_unlock (domain);
226                 if (user_data.method) {
227                         char *mname = mono_method_full_name (user_data.method, TRUE);
228                         res = g_strdup_printf ("<%p - JIT trampoline for %s>", ip, mname);
229                         g_free (mname);
230                         return res;
231                 }
232                 else
233                         return NULL;
234         }
235         method = mono_method_full_name (ji->method, TRUE);
236         /* FIXME: unused ? */
237         location = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
238
239         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);
240
241         mono_debug_free_source_location (location);
242         g_free (method);
243
244         return res;
245 }
246
247 /** 
248  * mono_pmip:
249  * @ip: an instruction pointer address
250  *
251  * This method is used from a debugger to get the name of the
252  * method at address @ip.   This routine is typically invoked from
253  * a debugger like this:
254  *
255  * (gdb) print mono_pmip ($pc)
256  *
257  * Returns: the name of the method at address @ip.
258  */
259 G_GNUC_UNUSED char *
260 mono_pmip (void *ip)
261 {
262         return get_method_from_ip (ip);
263 }
264
265 /** 
266  * mono_print_method_from_ip
267  * @ip: an instruction pointer address
268  *
269  * This method is used from a debugger to get the name of the
270  * method at address @ip.
271  *
272  * This prints the name of the method at address @ip in the standard
273  * output.  Unlike mono_pmip which returns a string, this routine
274  * prints the value on the standard output. 
275  */
276 void
277 mono_print_method_from_ip (void *ip)
278 {
279         MonoJitInfo *ji;
280         char *method;
281         MonoDebugSourceLocation *source;
282         MonoDomain *domain = mono_domain_get ();
283         FindTrampUserData user_data;
284         
285         ji = mono_jit_info_table_find (domain, ip);
286         if (!ji) {
287                 user_data.ip = ip;
288                 user_data.method = NULL;
289                 mono_domain_lock (domain);
290                 g_hash_table_foreach (domain->jit_trampoline_hash, find_tramp, &user_data);
291                 mono_domain_unlock (domain);
292                 if (user_data.method) {
293                         char *mname = mono_method_full_name (user_data.method, TRUE);
294                         printf ("IP %p is a JIT trampoline for %s\n", ip, mname);
295                         g_free (mname);
296                 }
297                 else
298                         g_print ("No method at %p\n", ip);
299                 return;
300         }
301         method = mono_method_full_name (ji->method, TRUE);
302         source = mono_debug_lookup_source_location (ji->method, (guint32)((guint8*)ip - (guint8*)ji->code_start), domain);
303
304         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);
305
306         if (source)
307                 g_print ("%s:%d\n", source->source_file, source->row);
308
309         mono_debug_free_source_location (source);
310         g_free (method);
311 }
312         
313 /* 
314  * mono_method_same_domain:
315  *
316  * Determine whenever two compiled methods are in the same domain, thus
317  * the address of the callee can be embedded in the caller.
318  */
319 gboolean mono_method_same_domain (MonoJitInfo *caller, MonoJitInfo *callee)
320 {
321         if (!caller || !callee)
322                 return FALSE;
323
324         /*
325          * If the call was made from domain-neutral to domain-specific 
326          * code, we can't patch the call site.
327          */
328         if (caller->domain_neutral && !callee->domain_neutral)
329                 return FALSE;
330
331         if ((caller->method->klass == mono_defaults.appdomain_class) &&
332                 (strstr (caller->method->name, "InvokeInDomain"))) {
333                  /* The InvokeInDomain methods change the current appdomain */
334                 return FALSE;
335         }
336
337         return TRUE;
338 }
339
340 /*
341  * mono_global_codeman_reserve:
342  *
343  *  Allocate code memory from the global code manager.
344  */
345 void *mono_global_codeman_reserve (int size)
346 {
347         void *ptr;
348
349         if (!global_codeman) {
350                 /* This can happen during startup */
351                 global_codeman = mono_code_manager_new ();
352                 return mono_code_manager_reserve (global_codeman, size);
353         }
354         else {
355                 mono_jit_lock ();
356                 ptr = mono_code_manager_reserve (global_codeman, size);
357                 mono_jit_unlock ();
358                 return ptr;
359         }
360 }
361
362 MonoJumpInfoToken *
363 mono_jump_info_token_new (MonoMemPool *mp, MonoImage *image, guint32 token)
364 {
365         MonoJumpInfoToken *res = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoToken));
366         res->image = image;
367         res->token = token;
368
369         return res;
370 }
371
372 #define MONO_INIT_VARINFO(vi,id) do { \
373         (vi)->range.first_use.pos.bid = 0xffff; \
374         (vi)->reg = -1; \
375         (vi)->idx = (id); \
376 } while (0)
377
378 //#define UNVERIFIED do { G_BREAKPOINT (); goto unverified; } while (0)
379 #define UNVERIFIED do { if (debug_options.break_on_unverified) G_BREAKPOINT (); else goto unverified; } while (0)
380
381 /*
382  * Basic blocks have two numeric identifiers:
383  * dfn: Depth First Number
384  * block_num: unique ID assigned at bblock creation
385  */
386 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
387 #define ADD_BBLOCK(cfg,b) do {  \
388                 cfg->cil_offset_to_bb [(b)->cil_code - cfg->cil_start] = (b);   \
389                 (b)->block_num = cfg->num_bblocks++;    \
390                 (b)->real_offset = real_offset; \
391         } while (0)
392
393 #define GET_BBLOCK(cfg,tblock,ip) do {  \
394                 (tblock) = cfg->cil_offset_to_bb [(ip) - cfg->cil_start]; \
395                 if (!(tblock)) {        \
396                         if ((ip) >= end || (ip) < header->code) UNVERIFIED; \
397                         (tblock) = NEW_BBLOCK (cfg);    \
398                         (tblock)->cil_code = (ip);      \
399                         ADD_BBLOCK (cfg, (tblock));     \
400                 } \
401         } while (0)
402
403 #define CHECK_BBLOCK(target,ip,tblock) do {     \
404                 if ((target) < (ip) && !(tblock)->code) {       \
405                         bb_recheck = g_list_prepend (bb_recheck, (tblock));     \
406                         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));     \
407                 }       \
408         } while (0)
409
410 #define NEW_ICONST(cfg,dest,val) do {   \
411                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
412                 (dest)->opcode = OP_ICONST;     \
413                 (dest)->inst_c0 = (val);        \
414                 (dest)->type = STACK_I4;        \
415         } while (0)
416
417 #define NEW_PCONST(cfg,dest,val) do {   \
418                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
419                 (dest)->opcode = OP_PCONST;     \
420                 (dest)->inst_p0 = (val);        \
421                 (dest)->type = STACK_PTR;       \
422         } while (0)
423
424
425 #ifdef MONO_ARCH_NEED_GOT_VAR
426
427 #define NEW_PATCH_INFO(cfg,dest,el1,el2) do {   \
428                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
429                 (dest)->opcode = OP_PATCH_INFO; \
430                 (dest)->inst_left = (gpointer)(el1);    \
431                 (dest)->inst_right = (gpointer)(el2);   \
432         } while (0)
433
434 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {                     \
435                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
436                 (dest)->opcode = cfg->compile_aot ? OP_GOT_ENTRY : OP_PCONST; \
437                 if (cfg->compile_aot) {                                 \
438                         MonoInst *group, *got_var, *got_loc;            \
439                         got_loc = mono_get_got_var (cfg);               \
440                         NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0); \
441                         NEW_PATCH_INFO ((cfg), group, cons, patch_type); \
442                         (dest)->inst_p0 = got_var;                      \
443                         (dest)->inst_p1 = group;                        \
444                 } else {                                                \
445                         (dest)->inst_p0 = (cons);                       \
446                         (dest)->inst_i1 = (gpointer)(patch_type);       \
447                 }                                                       \
448                 (dest)->type = STACK_PTR;                               \
449         } while (0)
450
451 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
452                 MonoInst *group, *got_var, *got_loc;                    \
453                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
454                 (dest)->opcode = OP_GOT_ENTRY;                          \
455                 got_loc = mono_get_got_var (cfg);                       \
456                 NEW_TEMPLOAD ((cfg), got_var, got_loc->inst_c0);        \
457                 NEW_PATCH_INFO ((cfg), group, NULL, patch_type);        \
458                 group->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token)); \
459                 (dest)->inst_p0 = got_var;                              \
460                 (dest)->inst_p1 = group;                                \
461                 (dest)->type = (stack_type);                    \
462         (dest)->klass = (stack_class);          \
463         } while (0)
464
465 #else
466
467 #define NEW_AOTCONST(cfg,dest,patch_type,cons) do {    \
468                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
469                 (dest)->opcode = cfg->compile_aot ? OP_AOTCONST : OP_PCONST;    \
470                 (dest)->inst_p0 = (cons);       \
471                 (dest)->inst_i1 = (gpointer)(patch_type); \
472                 (dest)->type = STACK_PTR;       \
473     } while (0)
474
475 #define NEW_AOTCONST_TOKEN(cfg,dest,patch_type,image,token,stack_type,stack_class) do { \
476                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
477                 (dest)->opcode = OP_AOTCONST;   \
478                 (dest)->inst_p0 = mono_jump_info_token_new ((cfg)->mempool, (image), (token));  \
479                 (dest)->inst_p1 = (gpointer)(patch_type); \
480                 (dest)->type = (stack_type);    \
481         (dest)->klass = (stack_class);          \
482     } while (0)
483
484 #endif
485
486 #define NEW_CLASSCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_CLASS, (val))
487
488 #define NEW_IMAGECONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_IMAGE, (val))
489
490 #define NEW_FIELDCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_FIELD, (val))
491
492 #define NEW_METHODCONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_METHODCONST, (val))
493
494 #define NEW_VTABLECONST(cfg,dest,vtable) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_VTABLE, cfg->compile_aot ? (gpointer)((vtable)->klass) : (vtable))
495
496 #define NEW_SFLDACONST(cfg,dest,val) NEW_AOTCONST ((cfg), (dest), MONO_PATCH_INFO_SFLDA, (val))
497
498 #define NEW_LDSTRCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDSTR, (image), (token), STACK_OBJ, mono_defaults.string_class)
499
500 #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)
501
502 #define NEW_LDTOKENCONST(cfg,dest,image,token) NEW_AOTCONST_TOKEN ((cfg), (dest), MONO_PATCH_INFO_LDTOKEN, (image), (token), STACK_PTR, NULL)
503
504 #define NEW_DECLSECCONST(cfg,dest,image,entry) do { \
505                 if (cfg->compile_aot) { \
506                         NEW_AOTCONST_TOKEN (cfg, dest, MONO_PATCH_INFO_DECLSEC, image, (entry).index, STACK_OBJ, NULL); \
507                 } else { \
508                         NEW_PCONST (cfg, args [0], (entry).blob); \
509                 } \
510         } while (0)
511
512 #define NEW_DOMAINCONST(cfg,dest) do { \
513                 if (cfg->opt & MONO_OPT_SHARED) { \
514                         /* avoid depending on undefined C behavior in sequence points */ \
515                         MonoInst* __domain_var = mono_get_domainvar (cfg); \
516                         NEW_TEMPLOAD (cfg, dest, __domain_var->inst_c0); \
517                 } else { \
518                         NEW_PCONST (cfg, dest, (cfg)->domain); \
519                 } \
520         } while (0)
521
522 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
523
524 #define NEW_ARGLOAD(cfg,dest,num) do {  \
525                 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
526                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
527                 (dest)->ssa_op = MONO_SSA_LOAD; \
528                 (dest)->inst_i0 = arg_array [(num)];    \
529                 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype);      \
530                 type_to_eval_stack_type (param_types [(num)], (dest));  \
531                 (dest)->klass = (dest)->inst_i0->klass; \
532         }} while (0)
533
534 #define NEW_LOCLOAD(cfg,dest,num) do {  \
535                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
536                 (dest)->ssa_op = MONO_SSA_LOAD; \
537                 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)];       \
538                 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype);      \
539                 type_to_eval_stack_type (header->locals [(num)], (dest));       \
540                 (dest)->klass = (dest)->inst_i0->klass; \
541         } while (0)
542
543 #define NEW_LOCLOADA(cfg,dest,num) do { \
544                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
545                 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN;        \
546                 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)];       \
547                 (dest)->inst_i0->flags |= MONO_INST_INDIRECT;   \
548                 (dest)->opcode = OP_LDADDR;     \
549                 (dest)->type = STACK_MP;        \
550                 (dest)->klass = (dest)->inst_i0->klass; \
551         if (!MONO_TYPE_ISSTRUCT (header->locals [(num)])) \
552            (cfg)->disable_ssa = TRUE; \
553         } while (0)
554
555 #define NEW_RETLOADA(cfg,dest) do {     \
556                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
557                 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN;        \
558                 (dest)->inst_i0 = (cfg)->ret;   \
559                 (dest)->inst_i0->flags |= MONO_INST_INDIRECT;   \
560                 (dest)->opcode = cfg->ret_var_is_local ? OP_LDADDR : CEE_LDIND_I;       \
561                 (dest)->type = STACK_MP;        \
562                 (dest)->klass = (dest)->inst_i0->klass; \
563                 (cfg)->disable_ssa = TRUE; \
564         } while (0)
565
566 #define NEW_ARGLOADA(cfg,dest,num) do { \
567                 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
568                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
569                 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN;        \
570                 (dest)->inst_i0 = arg_array [(num)];    \
571                 (dest)->inst_i0->flags |= MONO_INST_INDIRECT;   \
572                 (dest)->opcode = OP_LDADDR;     \
573                 (dest)->type = STACK_MP;        \
574                 (dest)->klass = (dest)->inst_i0->klass; \
575                 (cfg)->disable_ssa = TRUE; \
576         } while (0)
577
578 #define NEW_TEMPLOAD(cfg,dest,num) do { \
579                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
580                 (dest)->ssa_op = MONO_SSA_LOAD; \
581                 (dest)->inst_i0 = (cfg)->varinfo [(num)];       \
582                 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype);      \
583                 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest));  \
584                 (dest)->klass = (dest)->inst_i0->klass; \
585         } while (0)
586
587 #define NEW_TEMPLOADA(cfg,dest,num) do {        \
588                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
589                 (dest)->ssa_op = MONO_SSA_ADDRESS_TAKEN;        \
590                 (dest)->inst_i0 = (cfg)->varinfo [(num)];       \
591                 (dest)->inst_i0->flags |= MONO_INST_INDIRECT;   \
592                 (dest)->opcode = OP_LDADDR;     \
593                 (dest)->type = STACK_MP;        \
594                 (dest)->klass = (dest)->inst_i0->klass; \
595         if (!MONO_TYPE_ISSTRUCT (cfg->varinfo [(num)]->inst_vtype)) \
596            (cfg)->disable_ssa = TRUE; \
597         } while (0)
598
599
600 #define NEW_INDLOAD(cfg,dest,addr,vtype) do {   \
601                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
602                 (dest)->inst_left = addr;       \
603                 (dest)->opcode = mono_type_to_ldind (vtype);    \
604                 type_to_eval_stack_type (vtype, (dest));        \
605                 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/     \
606         } while (0)
607
608 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do {    \
609                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
610                 (dest)->inst_i0 = addr; \
611                 (dest)->opcode = mono_type_to_stind (vtype);    \
612                 (dest)->inst_i1 = (value);      \
613                 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/     \
614         } while (0)
615
616 #define NEW_TEMPSTORE(cfg,dest,num,inst) do {   \
617                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
618                 (dest)->ssa_op = MONO_SSA_STORE;        \
619                 (dest)->inst_i0 = (cfg)->varinfo [(num)];       \
620                 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype);      \
621                 (dest)->inst_i1 = (inst);       \
622                 (dest)->klass = (dest)->inst_i0->klass; \
623         } while (0)
624
625 #define NEW_LOCSTORE(cfg,dest,num,inst) do {    \
626                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
627                 (dest)->opcode = mono_type_to_stind (header->locals [(num)]);   \
628                 (dest)->ssa_op = MONO_SSA_STORE;        \
629                 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)];       \
630                 (dest)->inst_i1 = (inst);       \
631                 (dest)->klass = (dest)->inst_i0->klass; \
632         } while (0)
633
634 #define NEW_ARGSTORE(cfg,dest,num,inst) do {    \
635                 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
636                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
637                 (dest)->opcode = mono_type_to_stind (param_types [(num)]);      \
638                 (dest)->ssa_op = MONO_SSA_STORE;        \
639                 (dest)->inst_i0 = arg_array [(num)];    \
640                 (dest)->inst_i1 = (inst);       \
641                 (dest)->klass = (dest)->inst_i0->klass; \
642         } while (0)
643
644 #define NEW_DUMMY_USE(cfg,dest,load) do { \
645                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
646                 (dest)->opcode = OP_DUMMY_USE; \
647                 (dest)->inst_left = (load); \
648     } while (0)
649
650 #define NEW_DUMMY_STORE(cfg,dest,num) do { \
651                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
652                 (dest)->inst_i0 = (cfg)->varinfo [(num)];       \
653                 (dest)->opcode = OP_DUMMY_STORE; \
654                 (dest)->klass = (dest)->inst_i0->klass; \
655         } while (0)
656
657 #define ADD_BINOP(op) do {      \
658                 MONO_INST_NEW (cfg, ins, (op)); \
659                 ins->cil_code = ip;     \
660                 sp -= 2;        \
661                 ins->inst_i0 = sp [0];  \
662                 ins->inst_i1 = sp [1];  \
663                 *sp++ = ins;    \
664                 type_from_op (ins);     \
665                 CHECK_TYPE (ins);       \
666         } while (0)
667
668 #define ADD_UNOP(op) do {       \
669                 MONO_INST_NEW (cfg, ins, (op)); \
670                 ins->cil_code = ip;     \
671                 sp--;   \
672                 ins->inst_i0 = sp [0];  \
673                 *sp++ = ins;    \
674                 type_from_op (ins);     \
675                 CHECK_TYPE (ins);       \
676         } while (0)
677
678 #define ADD_BINCOND(next_block) do {    \
679                 MonoInst *cmp;  \
680                 sp -= 2;                \
681                 MONO_INST_NEW(cfg, cmp, OP_COMPARE);    \
682                 cmp->inst_i0 = sp [0];  \
683                 cmp->inst_i1 = sp [1];  \
684                 cmp->cil_code = ins->cil_code;  \
685                 type_from_op (cmp);     \
686                 CHECK_TYPE (cmp);       \
687                 ins->inst_i0 = cmp;     \
688                 MONO_ADD_INS (bblock, ins);     \
689                 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
690                 GET_BBLOCK (cfg, tblock, target);               \
691                 link_bblock (cfg, bblock, tblock);      \
692                 ins->inst_true_bb = tblock;     \
693                 CHECK_BBLOCK (target, ip, tblock);      \
694                 if ((next_block)) {     \
695                         link_bblock (cfg, bblock, (next_block));        \
696                         ins->inst_false_bb = (next_block);      \
697                         start_new_bblock = 1;   \
698                 } else {        \
699                         GET_BBLOCK (cfg, tblock, ip);           \
700                         link_bblock (cfg, bblock, tblock);      \
701                         ins->inst_false_bb = tblock;    \
702                         start_new_bblock = 2;   \
703                 }       \
704         } while (0)
705
706 /* FIXME: handle float, long ... */
707 #define ADD_UNCOND(istrue) do { \
708                 MonoInst *cmp;  \
709                 sp--;           \
710                 MONO_INST_NEW(cfg, cmp, OP_COMPARE);    \
711                 cmp->inst_i0 = sp [0];  \
712                 switch (cmp->inst_i0->type) { \
713                 case STACK_I8: \
714                         cmp->inst_i1 = zero_int64; break; \
715                 case STACK_R8: \
716                         cmp->inst_i1 = zero_r8; break; \
717                 case STACK_PTR: \
718                 case STACK_MP: \
719                         cmp->inst_i1 = zero_ptr; break; \
720                 case STACK_OBJ: \
721                         cmp->inst_i1 = zero_obj; break; \
722                 default: \
723                         cmp->inst_i1 = zero_int32;  \
724                 }  \
725                 cmp->cil_code = ins->cil_code;  \
726                 type_from_op (cmp);     \
727                 CHECK_TYPE (cmp);       \
728                 ins->inst_i0 = cmp;     \
729                 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ;    \
730                 MONO_ADD_INS (bblock, ins);     \
731                 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2);      \
732                 GET_BBLOCK (cfg, tblock, target);               \
733                 link_bblock (cfg, bblock, tblock);      \
734                 ins->inst_true_bb = tblock;     \
735                 CHECK_BBLOCK (target, ip, tblock);      \
736                 GET_BBLOCK (cfg, tblock, ip);           \
737                 link_bblock (cfg, bblock, tblock);      \
738                 ins->inst_false_bb = tblock;    \
739                 start_new_bblock = 2;   \
740         } while (0)
741
742 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
743                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
744                 (dest)->opcode = CEE_LDELEMA;   \
745                 (dest)->inst_left = (sp) [0];   \
746                 (dest)->inst_right = (sp) [1];  \
747                 (dest)->type = STACK_MP;        \
748                 (dest)->klass = (k);    \
749                 (cfg)->flags |= MONO_CFG_HAS_LDELEMA; \
750         } while (0)
751
752 #define NEW_GROUP(cfg,dest,el1,el2) do {        \
753                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
754                 (dest)->opcode = OP_GROUP;      \
755                 (dest)->inst_left = (el1);      \
756                 (dest)->inst_right = (el2);     \
757         } while (0)
758
759 #if 0
760 static gint
761 compare_bblock (gconstpointer a, gconstpointer b)
762 {
763         const MonoBasicBlock *b1 = a;
764         const MonoBasicBlock *b2 = b;
765
766         return b2->cil_code - b1->cil_code;
767 }
768 #endif
769
770 /* *
771  * link_bblock: Links two basic blocks
772  *
773  * links two basic blocks in the control flow graph, the 'from'
774  * argument is the starting block and the 'to' argument is the block
775  * the control flow ends to after 'from'.
776  */
777 static void
778 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
779 {
780         MonoBasicBlock **newa;
781         int i, found;
782
783 #if 0
784         if (from->cil_code) {
785                 if (to->cil_code)
786                         g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
787                 else
788                         g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
789         } else {
790                 if (to->cil_code)
791                         g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
792                 else
793                         g_print ("edge from entry to exit\n");
794         }
795 #endif
796         found = FALSE;
797         for (i = 0; i < from->out_count; ++i) {
798                 if (to == from->out_bb [i]) {
799                         found = TRUE;
800                         break;
801                 }
802         }
803         if (!found) {
804                 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
805                 for (i = 0; i < from->out_count; ++i) {
806                         newa [i] = from->out_bb [i];
807                 }
808                 newa [i] = to;
809                 from->out_count++;
810                 from->out_bb = newa;
811         }
812
813         found = FALSE;
814         for (i = 0; i < to->in_count; ++i) {
815                 if (from == to->in_bb [i]) {
816                         found = TRUE;
817                         break;
818                 }
819         }
820         if (!found) {
821                 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
822                 for (i = 0; i < to->in_count; ++i) {
823                         newa [i] = to->in_bb [i];
824                 }
825                 newa [i] = from;
826                 to->in_count++;
827                 to->in_bb = newa;
828         }
829 }
830
831 /**
832  * mono_unlink_bblock:
833  *
834  *   Unlink two basic blocks.
835  */
836 static void
837 mono_unlink_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
838 {
839         int i, pos;
840         gboolean found;
841
842         found = FALSE;
843         for (i = 0; i < from->out_count; ++i) {
844                 if (to == from->out_bb [i]) {
845                         found = TRUE;
846                         break;
847                 }
848         }
849         if (found) {
850                 pos = 0;
851                 for (i = 0; i < from->out_count; ++i) {
852                         if (from->out_bb [i] != to)
853                                 from->out_bb [pos ++] = from->out_bb [i];
854                 }
855                 g_assert (pos == from->out_count - 1);
856                 from->out_count--;
857         }
858
859         found = FALSE;
860         for (i = 0; i < to->in_count; ++i) {
861                 if (from == to->in_bb [i]) {
862                         found = TRUE;
863                         break;
864                 }
865         }
866         if (found) {
867                 pos = 0;
868                 for (i = 0; i < to->in_count; ++i) {
869                         if (to->in_bb [i] != from)
870                                 to->in_bb [pos ++] = to->in_bb [i];
871                 }
872                 g_assert (pos == to->in_count - 1);
873                 to->in_count--;
874         }
875 }
876
877 /**
878  * mono_find_block_region:
879  *
880  *   We mark each basic block with a region ID. We use that to avoid BB
881  *   optimizations when blocks are in different regions.
882  *
883  * Returns:
884  *   A region token that encodes where this region is, and information
885  *   about the clause owner for this block.
886  *
887  *   The region encodes the try/catch/filter clause that owns this block
888  *   as well as the type.  -1 is a special value that represents a block
889  *   that is in none of try/catch/filter.
890  */
891 static int
892 mono_find_block_region (MonoCompile *cfg, int offset)
893 {
894         MonoMethod *method = cfg->method;
895         MonoMethodHeader *header = mono_method_get_header (method);
896         MonoExceptionClause *clause;
897         int i;
898
899         /* first search for handlers and filters */
900         for (i = 0; i < header->num_clauses; ++i) {
901                 clause = &header->clauses [i];
902                 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->data.filter_offset) &&
903                     (offset < (clause->handler_offset)))
904                         return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
905                            
906                 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
907                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
908                                 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
909                         else if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
910                                 return ((i + 1) << 8) | MONO_REGION_FAULT | clause->flags;
911                         else if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE)
912                                 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
913                 }
914         }
915
916         /* search the try blocks */
917         for (i = 0; i < header->num_clauses; ++i) {
918                 clause = &header->clauses [i];
919                 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
920                         return ((i + 1) << 8) | clause->flags;
921         }
922
923         return -1;
924 }
925
926 static GList*
927 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
928 {
929         MonoMethod *method = cfg->method;
930         MonoMethodHeader *header = mono_method_get_header (method);
931         MonoExceptionClause *clause;
932         MonoBasicBlock *handler;
933         int i;
934         GList *res = NULL;
935
936         for (i = 0; i < header->num_clauses; ++i) {
937                 clause = &header->clauses [i];
938                 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) && 
939                     (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
940                         if (clause->flags == type) {
941                                 handler = cfg->cil_offset_to_bb [clause->handler_offset];
942                                 g_assert (handler);
943                                 res = g_list_append (res, handler);
944                         }
945                 }
946         }
947         return res;
948 }
949
950 MonoInst *
951 mono_find_spvar_for_region (MonoCompile *cfg, int region)
952 {
953         return g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
954 }
955
956 static void
957 mono_create_spvar_for_region (MonoCompile *cfg, int region)
958 {
959         MonoInst *var;
960
961         var = g_hash_table_lookup (cfg->spvars, GINT_TO_POINTER (region));
962         if (var)
963                 return;
964
965         var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
966         /* prevent it from being register allocated */
967         var->flags |= MONO_INST_INDIRECT;
968
969         g_hash_table_insert (cfg->spvars, GINT_TO_POINTER (region), var);
970 }
971
972 static MonoInst *
973 mono_find_exvar_for_offset (MonoCompile *cfg, int offset)
974 {
975         return g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
976 }
977
978 static MonoInst*
979 mono_create_exvar_for_offset (MonoCompile *cfg, int offset)
980 {
981         MonoInst *var;
982
983         var = g_hash_table_lookup (cfg->exvars, GINT_TO_POINTER (offset));
984         if (var)
985                 return var;
986
987         var = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
988         /* prevent it from being register allocated */
989         var->flags |= MONO_INST_INDIRECT;
990
991         g_hash_table_insert (cfg->exvars, GINT_TO_POINTER (offset), var);
992
993         return var;
994 }
995
996 static void
997 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
998 {
999         int i;
1000
1001         array [*dfn] = start;
1002         /*g_print ("visit %d at %p (BB%ld)\n", *dfn, start->cil_code, start->block_num);*/
1003         for (i = 0; i < start->out_count; ++i) {
1004                 if (start->out_bb [i]->dfn)
1005                         continue;
1006                 (*dfn)++;
1007                 start->out_bb [i]->dfn = *dfn;
1008                 start->out_bb [i]->df_parent = start;
1009                 array [*dfn] = start->out_bb [i];
1010                 df_visit (start->out_bb [i], dfn, array);
1011         }
1012 }
1013
1014 static MonoBasicBlock*
1015 find_previous (MonoBasicBlock **bblocks, guint32 n_bblocks, MonoBasicBlock *start, const guchar *code)
1016 {
1017         MonoBasicBlock *best = start;
1018         int i;
1019
1020         for (i = 0; i < n_bblocks; ++i) {
1021                 if (bblocks [i]) {
1022                         MonoBasicBlock *bb = bblocks [i];
1023
1024                         if (bb->cil_code && bb->cil_code < code && bb->cil_code > best->cil_code)
1025                                 best = bb;
1026                 }
1027         }
1028
1029         return best;
1030 }
1031
1032 static void
1033 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
1034         int i, j;
1035         MonoInst *inst;
1036         MonoBasicBlock *bb;
1037
1038         if (second->code)
1039                 return;
1040         
1041         /* 
1042          * FIXME: take into account all the details:
1043          * second may have been the target of more than one bblock
1044          */
1045         second->out_count = first->out_count;
1046         second->out_bb = first->out_bb;
1047
1048         for (i = 0; i < first->out_count; ++i) {
1049                 bb = first->out_bb [i];
1050                 for (j = 0; j < bb->in_count; ++j) {
1051                         if (bb->in_bb [j] == first)
1052                                 bb->in_bb [j] = second;
1053                 }
1054         }
1055
1056         first->out_count = 0;
1057         first->out_bb = NULL;
1058         link_bblock (cfg, first, second);
1059
1060         second->last_ins = first->last_ins;
1061
1062         /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
1063         for (inst = first->code; inst && inst->next; inst = inst->next) {
1064                 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
1065                 g_print ("found %p: %s", inst->next->cil_code, code);
1066                 g_free (code);*/
1067                 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
1068                         second->code = inst->next;
1069                         inst->next = NULL;
1070                         first->last_ins = inst;
1071                         second->next_bb = first->next_bb;
1072                         first->next_bb = second;
1073                         return;
1074                 }
1075         }
1076         if (!second->code) {
1077                 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
1078                 //G_BREAKPOINT ();
1079         }
1080 }
1081
1082 static guint32
1083 reverse_branch_op (guint32 opcode)
1084 {
1085         static const int reverse_map [] = {
1086                 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
1087                 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
1088         };
1089         static const int reverse_fmap [] = {
1090                 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
1091                 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
1092         };
1093         static const int reverse_lmap [] = {
1094                 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
1095                 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
1096         };
1097         static const int reverse_imap [] = {
1098                 OP_IBNE_UN, OP_IBLT, OP_IBLE, OP_IBGT, OP_IBGE,
1099                 OP_IBEQ, OP_IBLT_UN, OP_IBLE_UN, OP_IBGT_UN, OP_IBGE_UN
1100         };
1101                                 
1102         if (opcode >= CEE_BEQ && opcode <= CEE_BLT_UN) {
1103                 opcode = reverse_map [opcode - CEE_BEQ];
1104         } else if (opcode >= OP_FBEQ && opcode <= OP_FBLT_UN) {
1105                 opcode = reverse_fmap [opcode - OP_FBEQ];
1106         } else if (opcode >= OP_LBEQ && opcode <= OP_LBLT_UN) {
1107                 opcode = reverse_lmap [opcode - OP_LBEQ];
1108         } else if (opcode >= OP_IBEQ && opcode <= OP_IBLT_UN) {
1109                 opcode = reverse_imap [opcode - OP_IBEQ];
1110         } else
1111                 g_assert_not_reached ();
1112
1113         return opcode;
1114 }
1115
1116 #ifdef MONO_ARCH_SOFT_FLOAT
1117 static int
1118 condbr_to_fp_br (int opcode)
1119 {
1120         switch (opcode) {
1121         case CEE_BEQ: return OP_FBEQ;
1122         case CEE_BGE: return OP_FBGE;
1123         case CEE_BGT: return OP_FBGT;
1124         case CEE_BLE: return OP_FBLE;
1125         case CEE_BLT: return OP_FBLT;
1126         case CEE_BNE_UN: return OP_FBNE_UN;
1127         case CEE_BGE_UN: return OP_FBGE_UN;
1128         case CEE_BGT_UN: return OP_FBGT_UN;
1129         case CEE_BLE_UN: return OP_FBLE_UN;
1130         case CEE_BLT_UN: return OP_FBLT_UN;
1131         }
1132         g_assert_not_reached ();
1133         return 0;
1134 }
1135 #endif
1136
1137 /*
1138  * Returns the type used in the eval stack when @type is loaded.
1139  * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
1140  */
1141 static void
1142 type_to_eval_stack_type (MonoType *type, MonoInst *inst)
1143 {
1144         MonoClass *klass;
1145
1146         inst->klass = klass = mono_class_from_mono_type (type);
1147         if (type->byref) {
1148                 inst->type = STACK_MP;
1149                 return;
1150         }
1151
1152 handle_enum:
1153         switch (type->type) {
1154         case MONO_TYPE_VOID:
1155                 inst->type = STACK_INV;
1156                 return;
1157         case MONO_TYPE_I1:
1158         case MONO_TYPE_U1:
1159         case MONO_TYPE_BOOLEAN:
1160         case MONO_TYPE_I2:
1161         case MONO_TYPE_U2:
1162         case MONO_TYPE_CHAR:
1163         case MONO_TYPE_I4:
1164         case MONO_TYPE_U4:
1165                 inst->type = STACK_I4;
1166                 return;
1167         case MONO_TYPE_I:
1168         case MONO_TYPE_U:
1169         case MONO_TYPE_PTR:
1170         case MONO_TYPE_FNPTR:
1171                 inst->type = STACK_PTR;
1172                 return;
1173         case MONO_TYPE_CLASS:
1174         case MONO_TYPE_STRING:
1175         case MONO_TYPE_OBJECT:
1176         case MONO_TYPE_SZARRAY:
1177         case MONO_TYPE_ARRAY:    
1178                 inst->type = STACK_OBJ;
1179                 return;
1180         case MONO_TYPE_I8:
1181         case MONO_TYPE_U8:
1182                 inst->type = STACK_I8;
1183                 return;
1184         case MONO_TYPE_R4:
1185         case MONO_TYPE_R8:
1186                 inst->type = STACK_R8;
1187                 return;
1188         case MONO_TYPE_VALUETYPE:
1189                 if (type->data.klass->enumtype) {
1190                         type = type->data.klass->enum_basetype;
1191                         goto handle_enum;
1192                 } else {
1193                         inst->klass = klass;
1194                         inst->type = STACK_VTYPE;
1195                         return;
1196                 }
1197         case MONO_TYPE_TYPEDBYREF:
1198                 inst->klass = mono_defaults.typed_reference_class;
1199                 inst->type = STACK_VTYPE;
1200                 return;
1201         case MONO_TYPE_GENERICINST:
1202                 type = &type->data.generic_class->container_class->byval_arg;
1203                 goto handle_enum;
1204         default:
1205                 g_error ("unknown type 0x%02x in eval stack type", type->type);
1206         }
1207 }
1208
1209 /*
1210  * The following tables are used to quickly validate the IL code in type_from_op ().
1211  */
1212 static const char
1213 bin_num_table [STACK_MAX] [STACK_MAX] = {
1214         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1215         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
1216         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1217         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP,  STACK_INV, STACK_INV},
1218         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8,  STACK_INV, STACK_INV, STACK_INV},
1219         {STACK_INV, STACK_MP,  STACK_INV, STACK_MP,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
1220         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1221         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1222 };
1223
1224 static const char 
1225 neg_table [] = {
1226         STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
1227 };
1228
1229 /* reduce the size of this table */
1230 static const char
1231 bin_int_table [STACK_MAX] [STACK_MAX] = {
1232         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1233         {STACK_INV, STACK_I4,  STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1234         {STACK_INV, STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1235         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1236         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1237         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1238         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1239         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1240 };
1241
1242 static const char
1243 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1244 /*      Inv i  L  p  F  &  O  vt */
1245         {0},
1246         {0, 1, 0, 1, 0, 0, 0, 0}, /* i, int32 */
1247         {0, 0, 1, 0, 0, 0, 0, 0}, /* L, int64 */
1248         {0, 1, 0, 1, 0, 2, 4, 0}, /* p, ptr */
1249         {0, 0, 0, 0, 1, 0, 0, 0}, /* F, R8 */
1250         {0, 0, 0, 2, 0, 1, 0, 0}, /* &, managed pointer */
1251         {0, 0, 0, 4, 0, 0, 3, 0}, /* O, reference */
1252         {0, 0, 0, 0, 0, 0, 0, 0}, /* vt value type */
1253 };
1254
1255 /* reduce the size of this table */
1256 static const char
1257 shift_table [STACK_MAX] [STACK_MAX] = {
1258         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1259         {STACK_INV, STACK_I4,  STACK_INV, STACK_I4,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1260         {STACK_INV, STACK_I8,  STACK_INV, STACK_I8,  STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1261         {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1262         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1263         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1264         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1265         {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1266 };
1267
1268 /*
1269  * Tables to map from the non-specific opcode to the matching
1270  * type-specific opcode.
1271  */
1272 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1273 static const guint16
1274 binops_op_map [STACK_MAX] = {
1275         0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, OP_PADD-CEE_ADD
1276 };
1277
1278 /* handles from CEE_NEG to CEE_CONV_U8 */
1279 static const guint16
1280 unops_op_map [STACK_MAX] = {
1281         0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, OP_PNEG-CEE_NEG
1282 };
1283
1284 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1285 static const guint16
1286 ovfops_op_map [STACK_MAX] = {
1287         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
1288 };
1289
1290 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1291 static const guint16
1292 ovf2ops_op_map [STACK_MAX] = {
1293         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
1294 };
1295
1296 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1297 static const guint16
1298 ovf3ops_op_map [STACK_MAX] = {
1299         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
1300 };
1301
1302 /* handles from CEE_CEQ to CEE_CLT_UN */
1303 static const guint16
1304 ceqops_op_map [STACK_MAX] = {
1305         0, 0, OP_LCEQ-OP_CEQ, OP_PCEQ-OP_CEQ, OP_FCEQ-OP_CEQ, OP_LCEQ-OP_CEQ
1306 };
1307
1308 /*
1309  * Sets ins->type (the type on the eval stack) according to the
1310  * type of the opcode and the arguments to it.
1311  * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1312  *
1313  * FIXME: this function sets ins->type unconditionally in some cases, but
1314  * it should set it to invalid for some types (a conv.x on an object)
1315  */
1316 static void
1317 type_from_op (MonoInst *ins) {
1318         switch (ins->opcode) {
1319         /* binops */
1320         case CEE_ADD:
1321         case CEE_SUB:
1322         case CEE_MUL:
1323         case CEE_DIV:
1324         case CEE_REM:
1325                 /* FIXME: check unverifiable args for STACK_MP */
1326                 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1327                 ins->opcode += binops_op_map [ins->type];
1328                 return;
1329         case CEE_DIV_UN:
1330         case CEE_REM_UN:
1331         case CEE_AND:
1332         case CEE_OR:
1333         case CEE_XOR:
1334                 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1335                 ins->opcode += binops_op_map [ins->type];
1336                 return;
1337         case CEE_SHL:
1338         case CEE_SHR:
1339         case CEE_SHR_UN:
1340                 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1341                 ins->opcode += binops_op_map [ins->type];
1342                 return;
1343         case OP_COMPARE:
1344         case OP_LCOMPARE:
1345                 /* FIXME: handle some specifics with ins->next->type */
1346                 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1347                 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))))
1348                         ins->opcode = OP_LCOMPARE;
1349                 return;
1350         case OP_CEQ:
1351                 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1352                 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1353                 return;
1354                 
1355         case OP_CGT:
1356         case OP_CGT_UN:
1357         case OP_CLT:
1358         case OP_CLT_UN:
1359                 ins->type = (bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] & 1) ? STACK_I4: STACK_INV;
1360                 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1361                 return;
1362         /* unops */
1363         case CEE_NEG:
1364                 ins->type = neg_table [ins->inst_i0->type];
1365                 ins->opcode += unops_op_map [ins->type];
1366                 return;
1367         case CEE_NOT:
1368                 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1369                         ins->type = ins->inst_i0->type;
1370                 else
1371                         ins->type = STACK_INV;
1372                 ins->opcode += unops_op_map [ins->type];
1373                 return;
1374         case CEE_CONV_I1:
1375         case CEE_CONV_I2:
1376         case CEE_CONV_I4:
1377         case CEE_CONV_U4:
1378                 ins->type = STACK_I4;
1379                 ins->opcode += unops_op_map [ins->inst_i0->type];
1380                 return;
1381         case CEE_CONV_R_UN:
1382                 ins->type = STACK_R8;
1383                 switch (ins->inst_i0->type) {
1384                 case STACK_I4:
1385                 case STACK_PTR:
1386                         break;
1387                 case STACK_I8:
1388                         ins->opcode = OP_LCONV_TO_R_UN; 
1389                         break;
1390                 }
1391                 return;
1392         case CEE_CONV_OVF_I1:
1393         case CEE_CONV_OVF_U1:
1394         case CEE_CONV_OVF_I2:
1395         case CEE_CONV_OVF_U2:
1396         case CEE_CONV_OVF_I4:
1397         case CEE_CONV_OVF_U4:
1398                 ins->type = STACK_I4;
1399                 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1400                 return;
1401         case CEE_CONV_OVF_I_UN:
1402         case CEE_CONV_OVF_U_UN:
1403                 ins->type = STACK_PTR;
1404                 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1405                 return;
1406         case CEE_CONV_OVF_I1_UN:
1407         case CEE_CONV_OVF_I2_UN:
1408         case CEE_CONV_OVF_I4_UN:
1409         case CEE_CONV_OVF_U1_UN:
1410         case CEE_CONV_OVF_U2_UN:
1411         case CEE_CONV_OVF_U4_UN:
1412                 ins->type = STACK_I4;
1413                 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1414                 return;
1415         case CEE_CONV_U:
1416                 ins->type = STACK_PTR;
1417                 switch (ins->inst_i0->type) {
1418                 case STACK_I4:
1419                         break;
1420                 case STACK_PTR:
1421                 case STACK_MP:
1422 #if SIZEOF_VOID_P == 8
1423                         ins->opcode = OP_LCONV_TO_U;
1424 #endif
1425                         break;
1426                 case STACK_I8:
1427                         ins->opcode = OP_LCONV_TO_U;
1428                         break;
1429                 case STACK_R8:
1430                         ins->opcode = OP_FCONV_TO_U;
1431                         break;
1432                 }
1433                 return;
1434         case CEE_CONV_I8:
1435         case CEE_CONV_U8:
1436                 ins->type = STACK_I8;
1437                 ins->opcode += unops_op_map [ins->inst_i0->type];
1438                 return;
1439         case CEE_CONV_OVF_I8:
1440         case CEE_CONV_OVF_U8:
1441                 ins->type = STACK_I8;
1442                 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1443                 return;
1444         case CEE_CONV_OVF_U8_UN:
1445         case CEE_CONV_OVF_I8_UN:
1446                 ins->type = STACK_I8;
1447                 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1448                 return;
1449         case CEE_CONV_R4:
1450         case CEE_CONV_R8:
1451                 ins->type = STACK_R8;
1452                 ins->opcode += unops_op_map [ins->inst_i0->type];
1453                 return;
1454         case OP_CKFINITE:
1455                 ins->type = STACK_R8;           
1456                 return;
1457         case CEE_CONV_U2:
1458         case CEE_CONV_U1:
1459                 ins->type = STACK_I4;
1460                 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1461                 break;
1462         case CEE_CONV_I:
1463         case CEE_CONV_OVF_I:
1464         case CEE_CONV_OVF_U:
1465                 ins->type = STACK_PTR;
1466                 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1467                 return;
1468         case CEE_ADD_OVF:
1469         case CEE_ADD_OVF_UN:
1470         case CEE_MUL_OVF:
1471         case CEE_MUL_OVF_UN:
1472         case CEE_SUB_OVF:
1473         case CEE_SUB_OVF_UN:
1474                 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1475                 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1476                 return;
1477         default:
1478                 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1479                 break;
1480         }
1481 }
1482
1483 static const char 
1484 ldind_type [] = {
1485         STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_R8, STACK_OBJ
1486 };
1487
1488 /* map ldelem.x to the matching ldind.x opcode */
1489 static const guchar
1490 ldelem_to_ldind [] = {
1491         CEE_LDIND_I1,
1492         CEE_LDIND_U1,
1493         CEE_LDIND_I2,
1494         CEE_LDIND_U2,
1495         CEE_LDIND_I4,
1496         CEE_LDIND_U4,
1497         CEE_LDIND_I8,
1498         CEE_LDIND_I,
1499         CEE_LDIND_R4,
1500         CEE_LDIND_R8,
1501         CEE_LDIND_REF
1502 };
1503
1504 /* map stelem.x to the matching stind.x opcode */
1505 static const guchar
1506 stelem_to_stind [] = {
1507         CEE_STIND_I,
1508         CEE_STIND_I1,
1509         CEE_STIND_I2,
1510         CEE_STIND_I4,
1511         CEE_STIND_I8,
1512         CEE_STIND_R4,
1513         CEE_STIND_R8,
1514         CEE_STIND_REF
1515 };
1516
1517 #if 0
1518
1519 static const char
1520 param_table [STACK_MAX] [STACK_MAX] = {
1521         {0},
1522 };
1523
1524 static int
1525 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1526         int i;
1527
1528         if (sig->hasthis) {
1529                 switch (args->type) {
1530                 case STACK_I4:
1531                 case STACK_I8:
1532                 case STACK_R8:
1533                 case STACK_VTYPE:
1534                 case STACK_INV:
1535                         return 0;
1536                 }
1537                 args++;
1538         }
1539         for (i = 0; i < sig->param_count; ++i) {
1540                 switch (args [i].type) {
1541                 case STACK_INV:
1542                         return 0;
1543                 case STACK_MP:
1544                         if (!sig->params [i]->byref)
1545                                 return 0;
1546                         continue;
1547                 case STACK_OBJ:
1548                         if (sig->params [i]->byref)
1549                                 return 0;
1550                         switch (sig->params [i]->type) {
1551                         case MONO_TYPE_CLASS:
1552                         case MONO_TYPE_STRING:
1553                         case MONO_TYPE_OBJECT:
1554                         case MONO_TYPE_SZARRAY:
1555                         case MONO_TYPE_ARRAY:
1556                                 break;
1557                         default:
1558                                 return 0;
1559                         }
1560                         continue;
1561                 case STACK_R8:
1562                         if (sig->params [i]->byref)
1563                                 return 0;
1564                         if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1565                                 return 0;
1566                         continue;
1567                 case STACK_PTR:
1568                 case STACK_I4:
1569                 case STACK_I8:
1570                 case STACK_VTYPE:
1571                         break;
1572                 }
1573                 /*if (!param_table [args [i].type] [sig->params [i]->type])
1574                         return 0;*/
1575         }
1576         return 1;
1577 }
1578 #endif
1579
1580 /*
1581  * When we need a pointer to the current domain many times in a method, we
1582  * call mono_domain_get() once and we store the result in a local variable.
1583  * This function returns the variable that represents the MonoDomain*.
1584  */
1585 inline static MonoInst *
1586 mono_get_domainvar (MonoCompile *cfg)
1587 {
1588         if (!cfg->domainvar)
1589                 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1590         return cfg->domainvar;
1591 }
1592
1593 /*
1594  * The got_var contains the address of the Global Offset Table when AOT 
1595  * compiling.
1596  */
1597 inline static MonoInst *
1598 mono_get_got_var (MonoCompile *cfg)
1599 {
1600 #ifdef MONO_ARCH_NEED_GOT_VAR
1601         if (!cfg->compile_aot)
1602                 return NULL;
1603         if (!cfg->got_var) {
1604                 cfg->got_var = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1605         }
1606         return cfg->got_var;
1607 #else
1608         return NULL;
1609 #endif
1610 }
1611
1612 MonoInst*
1613 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1614 {
1615         MonoInst *inst;
1616         int num = cfg->num_varinfo;
1617
1618         if ((num + 1) >= cfg->varinfo_count) {
1619                 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1620                 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1621                 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);      
1622         }
1623
1624         /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1625         mono_jit_stats.allocate_var++;
1626
1627         MONO_INST_NEW (cfg, inst, opcode);
1628         inst->inst_c0 = num;
1629         inst->inst_vtype = type;
1630         inst->klass = mono_class_from_mono_type (type);
1631         /* if set to 1 the variable is native */
1632         inst->backend.is_pinvoke = 0;
1633
1634         cfg->varinfo [num] = inst;
1635
1636         cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1637         MONO_INIT_VARINFO (cfg->vars [num], num);
1638
1639         cfg->num_varinfo++;
1640         if (cfg->verbose_level > 2)
1641                 g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1642         return inst;
1643 }
1644
1645 /*
1646  * Transform a MonoInst into a load from the variable of index var_index.
1647  */
1648 void
1649 mono_compile_make_var_load (MonoCompile *cfg, MonoInst *dest, gssize var_index) {
1650         memset (dest, 0, sizeof (MonoInst));
1651         dest->ssa_op = MONO_SSA_LOAD;
1652         dest->inst_i0 = cfg->varinfo [var_index];
1653         dest->opcode = mono_type_to_ldind (dest->inst_i0->inst_vtype);
1654         type_to_eval_stack_type (dest->inst_i0->inst_vtype, dest);
1655         dest->klass = dest->inst_i0->klass;
1656 }
1657
1658 /*
1659  * Create a MonoInst that is a load from the variable of index var_index.
1660  */
1661 MonoInst*
1662 mono_compile_create_var_load (MonoCompile *cfg, gssize var_index) {
1663         MonoInst *dest;
1664         NEW_TEMPLOAD (cfg,dest,var_index);
1665         return dest;
1666 }
1667
1668 /*
1669  * Create a MonoInst that is a store of the given value into the variable of index var_index.
1670  */
1671 MonoInst*
1672 mono_compile_create_var_store (MonoCompile *cfg, gssize var_index, MonoInst *value) {
1673         MonoInst *dest;
1674         NEW_TEMPSTORE (cfg, dest, var_index, value);
1675         return dest;
1676 }
1677
1678 static MonoType*
1679 type_from_stack_type (MonoInst *ins) {
1680         switch (ins->type) {
1681         case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1682         case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1683         case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1684         case STACK_R8: return &mono_defaults.double_class->byval_arg;
1685         case STACK_MP:
1686                 /* 
1687                  * this if used to be commented without any specific reason, but
1688                  * it breaks #80235 when commented
1689                  */
1690                 if (ins->klass)
1691                         return &ins->klass->this_arg;
1692                 else
1693                         return &mono_defaults.object_class->this_arg;
1694         case STACK_OBJ:
1695                 /* ins->klass may not be set for ldnull.
1696                  * Also, if we have a boxed valuetype, we want an object lass,
1697                  * not the valuetype class
1698                  */
1699                 if (ins->klass && !ins->klass->valuetype)
1700                         return &ins->klass->byval_arg;
1701                 return &mono_defaults.object_class->byval_arg;
1702         case STACK_VTYPE: return &ins->klass->byval_arg;
1703         default:
1704                 g_error ("stack type %d to montype not handled\n", ins->type);
1705         }
1706         return NULL;
1707 }
1708
1709 MonoType*
1710 mono_type_from_stack_type (MonoInst *ins) {
1711         return type_from_stack_type (ins);
1712 }
1713
1714 static MonoClass*
1715 array_access_to_klass (int opcode, MonoInst *array_obj)
1716 {
1717         switch (opcode) {
1718         case CEE_LDELEM_U1:
1719                 return mono_defaults.byte_class;
1720         case CEE_LDELEM_U2:
1721                 return mono_defaults.uint16_class;
1722         case CEE_LDELEM_I:
1723         case CEE_STELEM_I:
1724                 return mono_defaults.int_class;
1725         case CEE_LDELEM_I1:
1726         case CEE_STELEM_I1:
1727                 return mono_defaults.sbyte_class;
1728         case CEE_LDELEM_I2:
1729         case CEE_STELEM_I2:
1730                 return mono_defaults.int16_class;
1731         case CEE_LDELEM_I4:
1732         case CEE_STELEM_I4:
1733                 return mono_defaults.int32_class;
1734         case CEE_LDELEM_U4:
1735                 return mono_defaults.uint32_class;
1736         case CEE_LDELEM_I8:
1737         case CEE_STELEM_I8:
1738                 return mono_defaults.int64_class;
1739         case CEE_LDELEM_R4:
1740         case CEE_STELEM_R4:
1741                 return mono_defaults.single_class;
1742         case CEE_LDELEM_R8:
1743         case CEE_STELEM_R8:
1744                 return mono_defaults.double_class;
1745         case CEE_LDELEM_REF:
1746         case CEE_STELEM_REF: {
1747                 MonoClass *klass = array_obj->klass;
1748                 /* FIXME: add assert */
1749                 if (klass && klass->rank)
1750                         return klass->element_class;
1751                 return mono_defaults.object_class;
1752         }
1753         default:
1754                 g_assert_not_reached ();
1755         }
1756         return NULL;
1757 }
1758
1759 void
1760 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1761 {
1762         MonoInst *prev;
1763         if (!bb->code) {
1764                 MONO_ADD_INS (bb, inst);
1765                 return;
1766         }
1767         switch (bb->last_ins->opcode) {
1768         case CEE_BEQ:
1769         case CEE_BGE:
1770         case CEE_BGT:
1771         case CEE_BLE:
1772         case CEE_BLT:
1773         case CEE_BNE_UN:
1774         case CEE_BGE_UN:
1775         case CEE_BGT_UN:
1776         case CEE_BLE_UN:
1777         case CEE_BLT_UN:
1778         case OP_BR:
1779         case CEE_SWITCH:
1780                 prev = bb->code;
1781                 while (prev->next && prev->next != bb->last_ins)
1782                         prev = prev->next;
1783                 if (prev == bb->code) {
1784                         if (bb->last_ins == bb->code) {
1785                                 inst->next = bb->code;
1786                                 bb->code = inst;
1787                         } else {
1788                                 inst->next = prev->next;
1789                                 prev->next = inst;
1790                         }
1791                 } else {
1792                         inst->next = bb->last_ins;
1793                         prev->next = inst;
1794                 }
1795                 break;
1796         //      g_warning ("handle conditional jump in add_ins_to_end ()\n");
1797         default:
1798                 MONO_ADD_INS (bb, inst);
1799                 break;
1800         }
1801 }
1802
1803 void
1804 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1805 {
1806         MonoInst *inst, *load;
1807
1808         NEW_TEMPLOAD (cfg, load, src);
1809
1810         NEW_TEMPSTORE (cfg, inst, dest, load);
1811         if (inst->opcode == CEE_STOBJ) {
1812                 NEW_TEMPLOADA (cfg, inst, dest);
1813                 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE, FALSE);
1814         } else {
1815                 inst->cil_code = NULL;
1816                 mono_add_ins_to_end (bb, inst);
1817         }
1818 }
1819
1820 /*
1821  * merge_stacks:
1822  *
1823  * Merge stack state between two basic blocks according to Ecma 335, Partition III,
1824  * section 1.8.1.1. Store the resulting stack state into stack_2.
1825  * Returns: TRUE, if verification succeeds, FALSE otherwise.
1826  * FIXME: We should store the stack state in a dedicated structure instead of in
1827  * MonoInst's.
1828  */
1829 static gboolean
1830 merge_stacks (MonoCompile *cfg, MonoStackSlot *state_1, MonoStackSlot *state_2, guint32 size)
1831 {
1832         int i;
1833
1834         if (cfg->dont_verify_stack_merge)
1835                 return TRUE;
1836
1837         /* FIXME: Implement all checks from the spec */
1838
1839         for (i = 0; i < size; ++i) {
1840                 MonoStackSlot *slot1 = &state_1 [i];
1841                 MonoStackSlot *slot2 = &state_2 [i];
1842
1843                 if (slot1->type != slot2->type)
1844                         return FALSE;
1845
1846                 switch (slot1->type) {
1847                 case STACK_PTR:
1848                         /* FIXME: Perform merge ? */
1849                         /* klass == NULL means a native int */
1850                         if (slot1->klass && slot2->klass) {
1851                                 if (slot1->klass != slot2->klass)
1852                                         return FALSE;
1853                         }
1854                         break;
1855                 case STACK_MP:
1856                         /* FIXME: Change this to an assert and fix the JIT to allways fill this */
1857                         if (slot1->klass && slot2->klass) {
1858                                 if (slot1->klass != slot2->klass)
1859                                         return FALSE;
1860                         }
1861                         break;
1862                 case STACK_OBJ: {
1863                         MonoClass *klass1 = slot1->klass;
1864                         MonoClass *klass2 = slot2->klass;
1865
1866                         if (!klass1) {
1867                                 /* slot1 is ldnull */
1868                         } else if (!klass2) {
1869                                 /* slot2 is ldnull */
1870                                 slot2->klass = slot1->klass;
1871                         }
1872                         break;
1873                 }
1874                 }
1875         }
1876
1877         return TRUE;
1878 }
1879
1880 /*
1881  * This function is called to handle items that are left on the evaluation stack
1882  * at basic block boundaries. What happens is that we save the values to local variables
1883  * and we reload them later when first entering the target basic block (with the
1884  * handle_loaded_temps () function).
1885  * It is also used to handle items on the stack in store opcodes, since it is
1886  * possible that the variable to be stored into is already on the stack, in
1887  * which case its old value should be used.
1888  * A single joint point will use the same variables (stored in the array bb->out_stack or
1889  * bb->in_stack, if the basic block is before or after the joint point).
1890  * If the stack merge fails at a join point, cfg->unverifiable is set.
1891  */
1892 static int
1893 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1894         int i, bindex;
1895         MonoBasicBlock *outb;
1896         MonoInst *inst, **locals;
1897         MonoStackSlot *stack_state;
1898         gboolean found;
1899
1900         if (!count)
1901                 return 0;
1902         if (cfg->verbose_level > 3)
1903                 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1904
1905         stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot) * count);
1906         for (i = 0; i < count; ++i) {
1907                 stack_state [i].type = sp [i]->type;
1908                 stack_state [i].klass = sp [i]->klass;
1909
1910                 /* Check that instructions other than ldnull have ins->klass set */
1911                 if (!cfg->dont_verify_stack_merge && (sp [i]->type == STACK_OBJ) && !((sp [i]->opcode == OP_PCONST) && sp [i]->inst_c0 == 0))
1912                         g_assert (sp [i]->klass);
1913         }
1914
1915         /* Perform verification and stack state merge */
1916         for (i = 0; i < bb->out_count; ++i) {
1917                 outb = bb->out_bb [i];
1918
1919                 /* exception handlers are linked, but they should not be considered for stack args */
1920                 if (outb->flags & BB_EXCEPTION_HANDLER)
1921                         continue;
1922                 if (outb->stack_state) {
1923                         gboolean verified;
1924
1925                         if (count != outb->in_scount) {
1926                                 cfg->unverifiable = TRUE;
1927                                 return 0;
1928                         }
1929                         verified = merge_stacks (cfg, stack_state, outb->stack_state, count);
1930                         if (!verified) {
1931                                 cfg->unverifiable = TRUE;
1932                                 return 0;
1933                         }
1934
1935                         if (cfg->verbose_level > 3) {
1936                                 int j;
1937
1938                                 for (j = 0; j < count; ++j)
1939                                         printf ("\tStack state of BB%d, slot %d=%d\n", outb->block_num, j, outb->stack_state [j].type);
1940                         }
1941                 } else {
1942                         /* Make a copy of the stack state */
1943                         outb->stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot) * count);
1944                         memcpy (outb->stack_state, stack_state, sizeof (MonoStackSlot) * count);
1945                 }
1946         }
1947
1948         if (!bb->out_scount) {
1949                 bb->out_scount = count;
1950                 //g_print ("bblock %d has out:", bb->block_num);
1951                 found = FALSE;
1952                 for (i = 0; i < bb->out_count; ++i) {
1953                         outb = bb->out_bb [i];
1954                         /* exception handlers are linked, but they should not be considered for stack args */
1955                         if (outb->flags & BB_EXCEPTION_HANDLER)
1956                                 continue;
1957                         //g_print (" %d", outb->block_num);
1958                         if (outb->in_stack) {
1959                                 found = TRUE;
1960                                 bb->out_stack = outb->in_stack;
1961                                 break;
1962                         }
1963                 }
1964                 //g_print ("\n");
1965                 if (!found) {
1966                         bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1967                         for (i = 0; i < count; ++i) {
1968                                 /* 
1969                                  * try to reuse temps already allocated for this purpouse, if they occupy the same
1970                                  * stack slot and if they are of the same type.
1971                                  * This won't cause conflicts since if 'local' is used to 
1972                                  * store one of the values in the in_stack of a bblock, then
1973                                  * the same variable will be used for the same outgoing stack 
1974                                  * slot as well. 
1975                                  * This doesn't work when inlining methods, since the bblocks
1976                                  * in the inlined methods do not inherit their in_stack from
1977                                  * the bblock they are inlined to. See bug #58863 for an
1978                                  * example.
1979                                  * This hack is disabled since it also prevents proper tracking of types.
1980                                  */
1981 #if 1
1982                                 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1983 #else
1984                                 if (cfg->inlined_method)
1985                                         bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1986                                 else
1987                                         bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1988 #endif
1989                         }
1990                 }
1991         }
1992
1993         for (i = 0; i < bb->out_count; ++i) {
1994                 outb = bb->out_bb [i];
1995                 /* exception handlers are linked, but they should not be considered for stack args */
1996                 if (outb->flags & BB_EXCEPTION_HANDLER)
1997                         continue;
1998                 if (outb->in_scount) {
1999                         if (outb->in_scount != bb->out_scount)
2000                                 G_BREAKPOINT ();
2001                         continue; /* check they are the same locals */
2002                 }
2003                 outb->in_scount = count;
2004                 outb->in_stack = bb->out_stack;
2005         }
2006
2007         locals = bb->out_stack;
2008         for (i = 0; i < count; ++i) {
2009                 /* add store ops at the end of the bb, before the branch */
2010                 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
2011                 if (inst->opcode == CEE_STOBJ) {
2012                         NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
2013                         handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE, FALSE);
2014                 } else {
2015                         inst->cil_code = sp [i]->cil_code;
2016                         mono_add_ins_to_end (bb, inst);
2017                 }
2018                 if (cfg->verbose_level > 3)
2019                         g_print ("storing %d to temp %d\n", i, (int)locals [i]->inst_c0);
2020         }
2021
2022         /*
2023          * It is possible that the out bblocks already have in_stack assigned, and
2024          * the in_stacks differ. In this case, we will store to all the different 
2025          * in_stacks.
2026          */
2027
2028         found = TRUE;
2029         bindex = 0;
2030         while (found) {
2031                 /* Find a bblock which has a different in_stack */
2032                 found = FALSE;
2033                 while (bindex < bb->out_count) {
2034                         outb = bb->out_bb [bindex];
2035                         /* exception handlers are linked, but they should not be considered for stack args */
2036                         if (outb->flags & BB_EXCEPTION_HANDLER) {
2037                                 bindex++;
2038                                 continue;
2039                         }
2040                         if (outb->in_stack != locals) {
2041                                 /* 
2042                                  * Instead of storing sp [i] to locals [i], we need to store
2043                                  * locals [i] to <new locals>[i], since the sp [i] tree can't
2044                                  * be shared between trees.
2045                                  */
2046                                 for (i = 0; i < count; ++i)
2047                                         mono_add_varcopy_to_end (cfg, bb, locals [i]->inst_c0, outb->in_stack [i]->inst_c0);
2048                                 locals = outb->in_stack;
2049                                 found = TRUE;
2050                                 break;
2051                         }
2052                         bindex ++;
2053                 }
2054         }
2055         
2056         return 0;
2057 }
2058
2059 static int
2060 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
2061 {
2062         if (type->byref)
2063                 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
2064
2065 handle_enum:
2066         switch (type->type) {
2067         case MONO_TYPE_VOID:
2068                 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
2069         case MONO_TYPE_I1:
2070         case MONO_TYPE_U1:
2071         case MONO_TYPE_BOOLEAN:
2072         case MONO_TYPE_I2:
2073         case MONO_TYPE_U2:
2074         case MONO_TYPE_CHAR:
2075         case MONO_TYPE_I4:
2076         case MONO_TYPE_U4:
2077                 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
2078         case MONO_TYPE_I:
2079         case MONO_TYPE_U:
2080         case MONO_TYPE_PTR:
2081         case MONO_TYPE_FNPTR:
2082                 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
2083         case MONO_TYPE_CLASS:
2084         case MONO_TYPE_STRING:
2085         case MONO_TYPE_OBJECT:
2086         case MONO_TYPE_SZARRAY:
2087         case MONO_TYPE_ARRAY:    
2088                 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
2089         case MONO_TYPE_I8:
2090         case MONO_TYPE_U8:
2091                 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
2092         case MONO_TYPE_R4:
2093         case MONO_TYPE_R8:
2094                 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
2095         case MONO_TYPE_VALUETYPE:
2096                 if (type->data.klass->enumtype) {
2097                         type = type->data.klass->enum_basetype;
2098                         goto handle_enum;
2099                 } else
2100                         return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2101         case MONO_TYPE_TYPEDBYREF:
2102                 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
2103         case MONO_TYPE_GENERICINST:
2104                 type = &type->data.generic_class->container_class->byval_arg;
2105                 goto handle_enum;
2106         default:
2107                 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
2108         }
2109         return -1;
2110 }
2111
2112 void
2113 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
2114 {
2115         MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
2116         MonoJumpInfoBBTable *table;
2117
2118         table = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfoBBTable));
2119         table->table = bbs;
2120         table->table_size = num_blocks;
2121         
2122         ji->ip.label = label;
2123         ji->type = MONO_PATCH_INFO_SWITCH;
2124         ji->data.table = table;
2125         ji->next = cfg->patch_info;
2126         cfg->patch_info = ji;
2127 }
2128
2129 static void
2130 mono_save_token_info (MonoCompile *cfg, MonoImage *image, guint32 token, gpointer key)
2131 {
2132         if (cfg->compile_aot) {
2133                 MonoJumpInfoToken *jump_info_token = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoJumpInfoToken));
2134                 jump_info_token->image = image;
2135                 jump_info_token->token = token;
2136                 g_hash_table_insert (cfg->token_info_hash, key, jump_info_token);
2137         }
2138 }
2139
2140 /*
2141  * When we add a tree of instructions, we need to ensure the instructions currently
2142  * on the stack are executed before (like, if we load a value from a local).
2143  * We ensure this by saving the currently loaded values to temps and rewriting the
2144  * instructions to load the values.
2145  * This is not done for opcodes that terminate a basic block (because it's handled already
2146  * by handle_stack_args ()) and for opcodes that can't change values, like POP.
2147  */
2148 static void
2149 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
2150 {
2151         MonoInst *load, *store, *temp, *ins;
2152
2153         while (stack < sp) {
2154                 ins = *stack;
2155                 /* handle also other constants */
2156                 if ((ins->opcode != OP_ICONST) &&
2157                     /* temps never get written to again, so we can safely avoid duplicating them */
2158                     !(ins->ssa_op == MONO_SSA_LOAD && ins->inst_i0->opcode == OP_LOCAL && ins->inst_i0->flags & MONO_INST_IS_TEMP)) {
2159                         temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2160                         temp->flags |= MONO_INST_IS_TEMP;
2161                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2162                         store->cil_code = ins->cil_code;
2163                         if (store->opcode == CEE_STOBJ) {
2164                                 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2165                                 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE, FALSE);
2166                         } else
2167                                 MONO_ADD_INS (bblock, store);
2168                         NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2169                         load->cil_code = ins->cil_code;
2170                         *stack = load;
2171                 }
2172                 stack++;
2173         }
2174 }
2175
2176 /*
2177  * target_type_is_incompatible:
2178  * @cfg: MonoCompile context
2179  *
2180  * Check that the item @arg on the evaluation stack can be stored
2181  * in the target type (can be a local, or field, etc).
2182  * The cfg arg can be used to check if we need verification or just
2183  * validity checks.
2184  *
2185  * Returns: non-0 value if arg can't be stored on a target.
2186  */
2187 static int
2188 target_type_is_incompatible (MonoCompile *cfg, MonoType *target, MonoInst *arg)
2189 {
2190         MonoType *simple_type;
2191         MonoClass *klass;
2192
2193         if (target->byref) {
2194                 /* FIXME: check that the pointed to types match */
2195                 if (arg->type == STACK_MP)
2196                         return arg->klass != mono_class_from_mono_type (target);
2197                 if (arg->type == STACK_PTR)
2198                         return 0;
2199                 return 1;
2200         }
2201         simple_type = mono_type_get_underlying_type (target);
2202         switch (simple_type->type) {
2203         case MONO_TYPE_VOID:
2204                 return 1;
2205         case MONO_TYPE_I1:
2206         case MONO_TYPE_U1:
2207         case MONO_TYPE_BOOLEAN:
2208         case MONO_TYPE_I2:
2209         case MONO_TYPE_U2:
2210         case MONO_TYPE_CHAR:
2211         case MONO_TYPE_I4:
2212         case MONO_TYPE_U4:
2213                 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2214                         return 1;
2215                 return 0;
2216         case MONO_TYPE_PTR:
2217                 /* STACK_MP is needed when setting pinned locals */
2218                 if (arg->type != STACK_I4 && arg->type != STACK_PTR && arg->type != STACK_MP)
2219                         return 1;
2220                 return 0;
2221         case MONO_TYPE_I:
2222         case MONO_TYPE_U:
2223         case MONO_TYPE_FNPTR:
2224                 if (arg->type != STACK_I4 && arg->type != STACK_PTR)
2225                         return 1;
2226                 return 0;
2227         case MONO_TYPE_OBJECT:
2228                 if (arg->type != STACK_OBJ)
2229                         return 1;
2230                 return 0;
2231         case MONO_TYPE_STRING:
2232                 if (arg->type != STACK_OBJ)
2233                         return 1;
2234                 /* ldnull has arg->klass unset */
2235                 /*if (arg->klass && arg->klass != mono_defaults.string_class) {
2236                         G_BREAKPOINT ();
2237                         return 1;
2238                 }*/
2239                 return 0;
2240         case MONO_TYPE_CLASS:
2241         case MONO_TYPE_SZARRAY:
2242         case MONO_TYPE_ARRAY:    
2243                 if (arg->type != STACK_OBJ)
2244                         return 1;
2245                 /* FIXME: check type compatibility */
2246                 return 0;
2247         case MONO_TYPE_I8:
2248         case MONO_TYPE_U8:
2249                 if (arg->type != STACK_I8)
2250                         return 1;
2251                 return 0;
2252         case MONO_TYPE_R4:
2253         case MONO_TYPE_R8:
2254                 if (arg->type != STACK_R8)
2255                         return 1;
2256                 return 0;
2257         case MONO_TYPE_VALUETYPE:
2258                 if (arg->type != STACK_VTYPE)
2259                         return 1;
2260                 klass = mono_class_from_mono_type (simple_type);
2261                 if (klass != arg->klass)
2262                         return 1;
2263                 return 0;
2264         case MONO_TYPE_TYPEDBYREF:
2265                 if (arg->type != STACK_VTYPE)
2266                         return 1;
2267                 klass = mono_class_from_mono_type (simple_type);
2268                 if (klass != arg->klass)
2269                         return 1;
2270                 return 0;
2271         case MONO_TYPE_GENERICINST:
2272                 if (mono_type_generic_inst_is_valuetype (simple_type)) {
2273                         klass = mono_class_from_mono_type (simple_type);
2274                         if (klass->enumtype)
2275                                 return target_type_is_incompatible (cfg, klass->enum_basetype, arg);
2276                         if (arg->type != STACK_VTYPE)
2277                                 return 1;
2278                         if (klass != arg->klass)
2279                                 return 1;
2280                         return 0;
2281                 } else {
2282                         if (arg->type != STACK_OBJ)
2283                                 return 1;
2284                         /* FIXME: check type compatibility */
2285                         return 0;
2286                 }
2287         default:
2288                 g_error ("unknown type 0x%02x in target_type_is_incompatible", simple_type->type);
2289         }
2290         return 1;
2291 }
2292
2293 /*
2294  * Prepare arguments for passing to a function call.
2295  * Return a non-zero value if the arguments can't be passed to the given
2296  * signature.
2297  * The type checks are not yet complete and some conversions may need
2298  * casts on 32 or 64 bit architectures.
2299  *
2300  * FIXME: implement this using target_type_is_incompatible ()
2301  */
2302 static int
2303 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
2304 {
2305         MonoType *simple_type;
2306         int i;
2307
2308         if (sig->hasthis) {
2309                 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
2310                         return 1;
2311                 args++;
2312         }
2313         for (i = 0; i < sig->param_count; ++i) {
2314                 if (sig->params [i]->byref) {
2315                         if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
2316                                 return 1;
2317                         continue;
2318                 }
2319                 simple_type = sig->params [i];
2320 handle_enum:
2321                 switch (simple_type->type) {
2322                 case MONO_TYPE_VOID:
2323                         return 1;
2324                         continue;
2325                 case MONO_TYPE_I1:
2326                 case MONO_TYPE_U1:
2327                 case MONO_TYPE_BOOLEAN:
2328                 case MONO_TYPE_I2:
2329                 case MONO_TYPE_U2:
2330                 case MONO_TYPE_CHAR:
2331                 case MONO_TYPE_I4:
2332                 case MONO_TYPE_U4:
2333                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
2334                                 return 1;
2335                         continue;
2336                 case MONO_TYPE_I:
2337                 case MONO_TYPE_U:
2338                 case MONO_TYPE_PTR:
2339                 case MONO_TYPE_FNPTR:
2340                         if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
2341                                 return 1;
2342                         continue;
2343                 case MONO_TYPE_CLASS:
2344                 case MONO_TYPE_STRING:
2345                 case MONO_TYPE_OBJECT:
2346                 case MONO_TYPE_SZARRAY:
2347                 case MONO_TYPE_ARRAY:    
2348                         if (args [i]->type != STACK_OBJ)
2349                                 return 1;
2350                         continue;
2351                 case MONO_TYPE_I8:
2352                 case MONO_TYPE_U8:
2353                         if (args [i]->type != STACK_I8)
2354                                 return 1;
2355                         continue;
2356                 case MONO_TYPE_R4:
2357                 case MONO_TYPE_R8:
2358                         if (args [i]->type != STACK_R8)
2359                                 return 1;
2360                         continue;
2361                 case MONO_TYPE_VALUETYPE:
2362                         if (simple_type->data.klass->enumtype) {
2363                                 simple_type = simple_type->data.klass->enum_basetype;
2364                                 goto handle_enum;
2365                         }
2366                         if (args [i]->type != STACK_VTYPE)
2367                                 return 1;
2368                         continue;
2369                 case MONO_TYPE_TYPEDBYREF:
2370                         if (args [i]->type != STACK_VTYPE)
2371                                 return 1;
2372                         continue;
2373                 case MONO_TYPE_GENERICINST:
2374                         simple_type = &simple_type->data.generic_class->container_class->byval_arg;
2375                         goto handle_enum;
2376
2377                 default:
2378                         g_error ("unknown type 0x%02x in check_call_signature",
2379                                  simple_type->type);
2380                 }
2381         }
2382         return 0;
2383 }
2384
2385 inline static int
2386 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object, 
2387                  const guint8 *ip, gboolean to_end)
2388 {
2389         MonoInst *temp, *store, *ins = (MonoInst*)call;
2390         MonoType *ret = sig->ret;
2391
2392         if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
2393                 if (ret_object) {
2394                         call->inst.type = STACK_OBJ;
2395                         call->inst.opcode = CEE_CALL;
2396                         temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
2397                 } else {
2398                         type_to_eval_stack_type (ret, ins);
2399                         temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
2400                 }
2401                 
2402                 temp->flags |= MONO_INST_IS_TEMP;
2403
2404                 if (MONO_TYPE_ISSTRUCT (ret)) {
2405                         MonoInst *loada, *dummy_store;
2406
2407                         /* 
2408                          * Emit a dummy store to the local holding the result so the
2409                          * liveness info remains correct.
2410                          */
2411                         NEW_DUMMY_STORE (cfg, dummy_store, temp->inst_c0);
2412                         if (to_end)
2413                                 mono_add_ins_to_end (bblock, dummy_store);
2414                         else
2415                                 MONO_ADD_INS (bblock, dummy_store);
2416
2417                         /* we use this to allocate native sized structs */
2418                         temp->backend.is_pinvoke = sig->pinvoke;
2419
2420                         NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
2421                         if (call->inst.opcode == OP_VCALL)
2422                                 ins->inst_left = loada;
2423                         else
2424                                 ins->inst_right = loada; /* a virtual or indirect call */
2425
2426                         if (to_end)
2427                                 mono_add_ins_to_end (bblock, ins);
2428                         else
2429                                 MONO_ADD_INS (bblock, ins);
2430                 } else {
2431                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2432                         store->cil_code = ip;
2433                         if (to_end)
2434                                 mono_add_ins_to_end (bblock, store);
2435                         else
2436                                 MONO_ADD_INS (bblock, store);
2437                 }
2438                 return temp->inst_c0;
2439         } else {
2440                 if (to_end)
2441                         mono_add_ins_to_end (bblock, ins);
2442                 else
2443                         MONO_ADD_INS (bblock, ins);
2444                 return -1;
2445         }
2446 }
2447
2448 inline static MonoCallInst *
2449 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, 
2450                      MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
2451 {
2452         MonoCallInst *call;
2453         MonoInst *arg;
2454
2455         MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
2456
2457 #ifdef MONO_ARCH_SOFT_FLOAT
2458         /* we need to convert the r4 value to an int value */
2459         {
2460                 int i;
2461                 for (i = 0; i < sig->param_count; ++i) {
2462                         if (!sig->params [i]->byref && sig->params [i]->type == MONO_TYPE_R4) {
2463                                 MonoInst *iargs [1];
2464                                 int temp;
2465                                 iargs [0] = args [i + sig->hasthis];
2466
2467                                 temp = mono_emit_jit_icall (cfg, bblock, mono_fload_r4_arg, iargs, ip);
2468                                 NEW_TEMPLOAD (cfg, arg, temp);
2469                                 args [i + sig->hasthis] = arg;
2470                         }
2471                 }
2472         }
2473 #endif
2474
2475         call->inst.cil_code = ip;
2476         call->args = args;
2477         call->signature = sig;
2478         call = mono_arch_call_opcode (cfg, bblock, call, virtual);
2479         type_to_eval_stack_type (sig->ret, &call->inst);
2480
2481         for (arg = call->out_args; arg;) {
2482                 MonoInst *narg = arg->next;
2483                 arg->next = NULL;
2484                 if (!arg->cil_code)
2485                         arg->cil_code = ip;
2486                 if (to_end)
2487                         mono_add_ins_to_end (bblock, arg);
2488                 else
2489                         MONO_ADD_INS (bblock, arg);
2490                 arg = narg;
2491         }
2492         return call;
2493 }
2494
2495 inline static int
2496 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, 
2497                  MonoInst **args, MonoInst *addr, const guint8 *ip)
2498 {
2499         MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
2500
2501         call->inst.inst_i0 = addr;
2502
2503         return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
2504 }
2505
2506 static MonoCallInst*
2507 mono_emit_method_call_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2508                        MonoInst **args, const guint8 *ip, MonoInst *this, gboolean to_end)
2509 {
2510         gboolean virtual = this != NULL;
2511         MonoCallInst *call;
2512
2513         call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, to_end);
2514
2515         if (this && sig->hasthis && 
2516             (method->klass->marshalbyref || method->klass == mono_defaults.object_class) && 
2517             !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
2518                 call->method = mono_marshal_get_remoting_invoke_with_check (method);
2519         } else {
2520                 call->method = method;
2521         }
2522         call->inst.flags |= MONO_INST_HAS_METHOD;
2523         call->inst.inst_left = this;
2524
2525         if (call->method->klass->flags & TYPE_ATTRIBUTE_INTERFACE)
2526                 /* Needed by the code generated in inssel.brg */
2527                 mono_get_got_var (cfg);
2528
2529         return call;
2530 }
2531
2532 static MonoCallInst*
2533 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
2534                        MonoInst **args, const guint8 *ip, MonoInst *this)
2535 {
2536         return mono_emit_method_call_full (cfg, bblock, method, sig, args, ip, this, FALSE);
2537 }
2538
2539 inline static int
2540 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,  
2541                        MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
2542 {
2543         MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
2544
2545         return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
2546 }
2547
2548 inline static int
2549 mono_emit_method_call_spilled_full (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,  
2550                        MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this,
2551                        gboolean ret_object, gboolean to_end)
2552 {
2553         MonoCallInst *call = mono_emit_method_call_full (cfg, bblock, method, signature, args, ip, this, to_end);
2554
2555         return mono_spill_call (cfg, bblock, call, signature, ret_object, ip, to_end);
2556 }
2557
2558 inline static int
2559 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
2560                        MonoInst **args, const guint8 *ip, gboolean ret_object, gboolean to_end)
2561 {
2562         MonoCallInst *call;
2563
2564         g_assert (sig);
2565
2566         call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
2567         call->fptr = func;
2568
2569         return mono_spill_call (cfg, bblock, call, sig, ret_object, ip, to_end);
2570 }
2571
2572 inline static int
2573 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
2574 {
2575         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
2576         
2577         if (!info) {
2578                 g_warning ("unregistered JIT ICall");
2579                 g_assert_not_reached ();
2580         }
2581
2582         return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, args, ip, FALSE, FALSE);
2583 }
2584
2585 static void
2586 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
2587 {
2588         MonoInst *ins, *temp = NULL, *store, *load, *begin;
2589         MonoInst *last_arg = NULL;
2590         int nargs;
2591         MonoCallInst *call;
2592
2593         //g_print ("emulating: ");
2594         //mono_print_tree_nl (tree);
2595         MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
2596         ins = (MonoInst*)call;
2597         
2598         call->inst.cil_code = tree->cil_code;
2599         call->args = iargs;
2600         call->signature = info->sig;
2601
2602         call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
2603
2604         if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2605                 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
2606                 temp->flags |= MONO_INST_IS_TEMP;
2607                 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2608                 store->cil_code = tree->cil_code;
2609         } else {
2610                 store = ins;
2611         }
2612
2613         nargs = info->sig->param_count + info->sig->hasthis;
2614
2615         for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
2616
2617         if (nargs)
2618                 last_arg->next = store;
2619
2620         if (nargs)
2621                 begin = call->out_args;
2622         else
2623                 begin = store;
2624
2625         if (cfg->prev_ins) {
2626                 /* 
2627                  * This assumes that that in a tree, emulate_opcode is called for a
2628                  * node before it is called for its children. dec_foreach needs to
2629                  * take this into account.
2630                  */
2631                 store->next = cfg->prev_ins->next;
2632                 cfg->prev_ins->next = begin;
2633         } else {
2634                 store->next = cfg->cbb->code;
2635                 cfg->cbb->code = begin;
2636         }
2637
2638         call->fptr = mono_icall_get_wrapper (info);
2639
2640         if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
2641                 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
2642                 *tree = *load;
2643         }
2644 }
2645
2646 static MonoMethodSignature *
2647 mono_get_array_new_va_signature (int arity)
2648 {
2649         static GHashTable *sighash = NULL;
2650         MonoMethodSignature *res;
2651         int i;
2652
2653         mono_jit_lock ();
2654         if (!sighash) {
2655                 sighash = g_hash_table_new (NULL, NULL);
2656         }
2657         else if ((res = g_hash_table_lookup (sighash, GINT_TO_POINTER (arity)))) {
2658                 mono_jit_unlock ();
2659                 return res;
2660         }
2661
2662         res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2663
2664         res->pinvoke = 1;
2665 #ifdef MONO_ARCH_VARARG_ICALLS
2666         /* Only set this only some archs since not all backends can handle varargs+pinvoke */
2667         res->call_convention = MONO_CALL_VARARG;
2668 #endif
2669
2670 #ifdef PLATFORM_WIN32
2671         res->call_convention = MONO_CALL_C;
2672 #endif
2673
2674         res->params [0] = &mono_defaults.int_class->byval_arg;  
2675         for (i = 0; i < arity; i++)
2676                 res->params [i + 1] = &mono_defaults.int_class->byval_arg;
2677
2678         res->ret = &mono_defaults.int_class->byval_arg;
2679
2680         g_hash_table_insert (sighash, GINT_TO_POINTER (arity), res);
2681         mono_jit_unlock ();
2682
2683         return res;
2684 }
2685
2686 #ifdef MONO_ARCH_SOFT_FLOAT
2687 static void
2688 handle_store_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, MonoInst *val, const unsigned char *ip)
2689 {
2690         MonoInst *iargs [2];
2691         iargs [0] = val;
2692         iargs [1] = ptr;
2693
2694         mono_emit_jit_icall (cfg, bblock, mono_fstore_r4, iargs, ip);
2695 }
2696
2697 static int
2698 handle_load_float (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ptr, const unsigned char *ip)
2699 {
2700         MonoInst *iargs [1];
2701         iargs [0] = ptr;
2702
2703         return mono_emit_jit_icall (cfg, bblock, mono_fload_r4, iargs, ip);
2704 }
2705
2706 #define LDLOC_SOFT_FLOAT(cfg,ins,idx,ip) do {\
2707                 if (header->locals [(idx)]->type == MONO_TYPE_R4 && !header->locals [(idx)]->byref) {   \
2708                         int temp;       \
2709                         NEW_LOCLOADA (cfg, (ins), (idx));       \
2710                         temp = handle_load_float (cfg, bblock, (ins), (ip));    \
2711                         NEW_TEMPLOAD (cfg, (ins), temp);        \
2712                 }       \
2713         } while (0)
2714 #define STLOC_SOFT_FLOAT(cfg,ins,idx,ip) do {\
2715                 if (header->locals [(idx)]->type == MONO_TYPE_R4 && !header->locals [(idx)]->byref) {   \
2716                         int temp;       \
2717                         NEW_LOCLOADA (cfg, (ins), (idx));       \
2718                         handle_store_float (cfg, bblock, (ins), *sp, (ip));     \
2719                         MONO_INST_NEW (cfg, (ins), OP_NOP);     \
2720                 }       \
2721         } while (0)
2722 #define LDARG_SOFT_FLOAT(cfg,ins,idx,ip) do {\
2723                 if (param_types [(idx)]->type == MONO_TYPE_R4 && !param_types [(idx)]->byref) { \
2724                         int temp;       \
2725                         NEW_ARGLOADA (cfg, (ins), (idx));       \
2726                         temp = handle_load_float (cfg, bblock, (ins), (ip));    \
2727                         NEW_TEMPLOAD (cfg, (ins), temp);        \
2728                 }       \
2729         } while (0)
2730 #define STARG_SOFT_FLOAT(cfg,ins,idx,ip) do {\
2731                 if (param_types [(idx)]->type == MONO_TYPE_R4 && !param_types [(idx)]->byref) { \
2732                         int temp;       \
2733                         NEW_ARGLOADA (cfg, (ins), (idx));       \
2734                         handle_store_float (cfg, bblock, (ins), *sp, (ip));     \
2735                         MONO_INST_NEW (cfg, (ins), OP_NOP);     \
2736                 }       \
2737         } while (0)
2738 #else
2739 #define LDLOC_SOFT_FLOAT(cfg,ins,idx,ip)
2740 #define STLOC_SOFT_FLOAT(cfg,ins,idx,ip)
2741 #define LDARG_SOFT_FLOAT(cfg,ins,idx,ip)
2742 #define STARG_SOFT_FLOAT(cfg,ins,idx,ip)
2743 #endif
2744
2745 static MonoMethod*
2746 get_memcpy_method (void)
2747 {
2748         static MonoMethod *memcpy_method = NULL;
2749         if (!memcpy_method) {
2750                 memcpy_method = mono_class_get_method_from_name (mono_defaults.string_class, "memcpy", 3);
2751                 if (!memcpy_method)
2752                         g_error ("Old corlib found. Install a new one");
2753         }
2754         return memcpy_method;
2755 }
2756
2757 static void
2758 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native, gboolean write_barrier) {
2759         MonoInst *iargs [3];
2760         int n;
2761         guint32 align = 0;
2762         MonoMethod *memcpy_method;
2763
2764         g_assert (klass);
2765         /*
2766          * This check breaks with spilled vars... need to handle it during verification anyway.
2767          * g_assert (klass && klass == src->klass && klass == dest->klass);
2768          */
2769
2770         if (native)
2771                 n = mono_class_native_size (klass, &align);
2772         else
2773                 n = mono_class_value_size (klass, &align);
2774
2775 #if HAVE_WRITE_BARRIERS
2776         /* if native is true there should be no references in the struct */
2777         if (write_barrier && klass->has_references && !native) {
2778                 iargs [0] = dest;
2779                 iargs [1] = src;
2780                 NEW_PCONST (cfg, iargs [2], klass);
2781
2782                 mono_emit_jit_icall (cfg, bblock, mono_value_copy, iargs, ip);
2783                 return;
2784         }
2785 #endif
2786
2787         /* FIXME: add write barrier handling */
2788         if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2789                 MonoInst *inst;
2790                 if (dest->opcode == OP_LDADDR) {
2791                         /* Keep liveness info correct */
2792                         NEW_DUMMY_STORE (cfg, inst, dest->inst_i0->inst_c0);
2793                         MONO_ADD_INS (bblock, inst);
2794                 }
2795                 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2796                 inst->inst_left = dest;
2797                 inst->inst_right = src;
2798                 inst->cil_code = ip;
2799                 inst->backend.size = n;
2800                 MONO_ADD_INS (bblock, inst);
2801                 return;
2802         }
2803         iargs [0] = dest;
2804         iargs [1] = src;
2805         NEW_ICONST (cfg, iargs [2], n);
2806
2807         memcpy_method = get_memcpy_method ();
2808         mono_emit_method_call_spilled_full (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL, FALSE, to_end);
2809 }
2810
2811 static MonoMethod*
2812 get_memset_method (void)
2813 {
2814         static MonoMethod *memset_method = NULL;
2815         if (!memset_method) {
2816                 memset_method = mono_class_get_method_from_name (mono_defaults.string_class, "memset", 3);
2817                 if (!memset_method)
2818                         g_error ("Old corlib found. Install a new one");
2819         }
2820         return memset_method;
2821 }
2822
2823 static void
2824 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2825 {
2826         MonoInst *iargs [3];
2827         MonoInst *ins, *zero_int32;
2828         int n;
2829         MonoMethod *memset_method;
2830
2831         NEW_ICONST (cfg, zero_int32, 0);
2832
2833         mono_class_init (klass);
2834         n = mono_class_value_size (klass, NULL);
2835         MONO_INST_NEW (cfg, ins, 0);
2836         ins->cil_code = ip;
2837         ins->inst_left = dest;
2838         ins->inst_right = zero_int32;
2839         switch (n) {
2840         case 1:
2841                 ins->opcode = CEE_STIND_I1;
2842                 MONO_ADD_INS (bblock, ins);
2843                 break;
2844         case 2:
2845                 ins->opcode = CEE_STIND_I2;
2846                 MONO_ADD_INS (bblock, ins);
2847                 break;
2848         case 4:
2849                 ins->opcode = CEE_STIND_I4;
2850                 MONO_ADD_INS (bblock, ins);
2851                 break;
2852         default:
2853                 if (n <= sizeof (gpointer) * 5) {
2854                         ins->opcode = OP_MEMSET;
2855                         ins->inst_imm = 0;
2856                         ins->backend.size = n;
2857                         MONO_ADD_INS (bblock, ins);
2858                         break;
2859                 }
2860                 memset_method = get_memset_method ();
2861                 handle_loaded_temps (cfg, bblock, stack_start, sp);
2862                 iargs [0] = dest;
2863                 NEW_ICONST (cfg, iargs [1], 0);
2864                 NEW_ICONST (cfg, iargs [2], n);
2865                 mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
2866                 break;
2867         }
2868 }
2869
2870 static int
2871 handle_alloc (MonoCompile *cfg, MonoBasicBlock *bblock, MonoClass *klass, gboolean for_box, const guchar *ip)
2872 {
2873         MonoInst *iargs [2];
2874         void *alloc_ftn;
2875
2876         if (cfg->opt & MONO_OPT_SHARED) {
2877                 NEW_DOMAINCONST (cfg, iargs [0]);
2878                 NEW_CLASSCONST (cfg, iargs [1], klass);
2879
2880                 alloc_ftn = mono_object_new;
2881         } else if (cfg->compile_aot && bblock->out_of_line && klass->type_token && klass->image == mono_defaults.corlib) {
2882                 /* This happens often in argument checking code, eg. throw new FooException... */
2883                 /* Avoid relocations by calling a helper function specialized to mscorlib */
2884                 NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (klass->type_token));
2885                 return mono_emit_jit_icall (cfg, bblock, mono_helper_newobj_mscorlib, iargs, ip);
2886         } else {
2887                 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
2888                 gboolean pass_lw;
2889
2890                 alloc_ftn = mono_class_get_allocation_ftn (vtable, for_box, &pass_lw);
2891                 if (pass_lw) {
2892                         guint32 lw = vtable->klass->instance_size;
2893                         lw = ((lw + (sizeof (gpointer) - 1)) & ~(sizeof (gpointer) - 1)) / sizeof (gpointer);
2894                         NEW_ICONST (cfg, iargs [0], lw);
2895                         NEW_VTABLECONST (cfg, iargs [1], vtable);
2896                 }
2897                 else
2898                         NEW_VTABLECONST (cfg, iargs [0], vtable);
2899         }
2900
2901         return mono_emit_jit_icall (cfg, bblock, alloc_ftn, iargs, ip);
2902 }
2903
2904 /**
2905  * Handles unbox of a Nullable<T>, returning a temp variable
2906  * where the result is stored
2907  */
2908 static int
2909 handle_unbox_nullable (MonoCompile* cfg, MonoBasicBlock* bblock, MonoInst* val, const guchar *ip, MonoClass* klass)
2910 {
2911        MonoMethod* method = mono_class_get_method_from_name (klass, "Unbox", 1);
2912        return mono_emit_method_call_spilled (cfg, bblock, method, mono_method_signature (method), &val, ip, NULL);
2913         
2914 }
2915
2916
2917
2918 static MonoInst *
2919 handle_box (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *val, const guchar *ip, MonoClass *klass)
2920 {
2921         MonoInst *dest, *vtoffset, *add, *vstore;
2922         int temp;
2923
2924        if (mono_class_is_nullable (klass)) {
2925                MonoMethod* method = mono_class_get_method_from_name (klass, "Box", 1);
2926                temp = mono_emit_method_call_spilled (cfg, bblock, method, mono_method_signature (method), &val, ip, NULL);
2927                NEW_TEMPLOAD (cfg, dest, temp);
2928                return dest;
2929        }
2930
2931
2932         temp = handle_alloc (cfg, bblock, klass, TRUE, ip);
2933         NEW_TEMPLOAD (cfg, dest, temp);
2934         NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
2935         MONO_INST_NEW (cfg, add, OP_PADD);
2936         add->inst_left = dest;
2937         add->inst_right = vtoffset;
2938         add->cil_code = ip;
2939         add->klass = klass;
2940         MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
2941         vstore->opcode = mono_type_to_stind (&klass->byval_arg);
2942         vstore->cil_code = ip;
2943         vstore->inst_left = add;
2944         vstore->inst_right = val;
2945
2946 #ifdef MONO_ARCH_SOFT_FLOAT
2947         if (vstore->opcode == CEE_STIND_R4) {
2948                 handle_store_float (cfg, bblock, add, val, ip);
2949         } else
2950 #endif
2951         if (vstore->opcode == CEE_STOBJ) {
2952                 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE, TRUE);
2953         } else
2954                 MONO_ADD_INS (bblock, vstore);
2955
2956         NEW_TEMPLOAD (cfg, dest, temp);
2957         return dest;
2958 }
2959
2960 static int
2961 handle_array_new (MonoCompile *cfg, MonoBasicBlock *bblock, int rank, MonoInst **sp, unsigned char *ip)
2962 {
2963         MonoMethodSignature *esig;
2964         char icall_name [256];
2965         char *name;
2966         MonoJitICallInfo *info;
2967
2968         /* Need to register the icall so it gets an icall wrapper */
2969         sprintf (icall_name, "ves_array_new_va_%d", rank);
2970
2971         mono_jit_lock ();
2972         info = mono_find_jit_icall_by_name (icall_name);
2973         if (info == NULL) {
2974                 esig = mono_get_array_new_va_signature (rank);
2975                 name = g_strdup (icall_name);
2976                 info = mono_register_jit_icall (mono_array_new_va, name, esig, FALSE);
2977
2978                 g_hash_table_insert (jit_icall_name_hash, name, name);
2979         }
2980         mono_jit_unlock ();
2981
2982         cfg->flags |= MONO_CFG_HAS_VARARGS;
2983
2984         /* FIXME: This uses info->sig, but it should use the signature of the wrapper */
2985         return mono_emit_native_call (cfg, bblock, mono_icall_get_wrapper (info), info->sig, sp, ip, TRUE, FALSE);
2986 }
2987
2988 static void
2989 mono_emit_load_got_addr (MonoCompile *cfg)
2990 {
2991         MonoInst *load, *store, *dummy_use;
2992         MonoInst *get_got;
2993
2994         if (!cfg->got_var || cfg->got_var_allocated)
2995                 return;
2996
2997         MONO_INST_NEW (cfg, get_got, OP_LOAD_GOTADDR);
2998         NEW_TEMPSTORE (cfg, store, cfg->got_var->inst_c0, get_got);
2999
3000         /* Add it to the start of the first bblock */
3001         if (cfg->bb_entry->code) {
3002                 store->next = cfg->bb_entry->code;
3003                 cfg->bb_entry->code = store;
3004         }
3005         else
3006                 MONO_ADD_INS (cfg->bb_entry, store);
3007
3008         cfg->got_var_allocated = TRUE;
3009
3010         /* 
3011          * Add a dummy use to keep the got_var alive, since real uses might
3012          * only be generated in the decompose or instruction selection phases.
3013          * Add it to end_bblock, so the variable's lifetime covers the whole
3014          * method.
3015          */
3016         NEW_TEMPLOAD (cfg, load, cfg->got_var->inst_c0);
3017         NEW_DUMMY_USE (cfg, dummy_use, load);
3018         MONO_ADD_INS (cfg->bb_exit, dummy_use);
3019 }
3020
3021 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
3022
3023 static gboolean
3024 mini_class_is_system_array (MonoClass *klass)
3025 {
3026         if (klass->parent == mono_defaults.array_class)
3027                 return TRUE;
3028         else
3029                 return FALSE;
3030 }
3031
3032 static gboolean
3033 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
3034 {
3035         MonoMethodHeader *header = mono_method_get_header (method);
3036         MonoMethodSignature *signature = mono_method_signature (method);
3037         MonoVTable *vtable;
3038         int i;
3039
3040 #ifdef MONO_ARCH_HAVE_LMF_OPS
3041         if (((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3042                  (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) &&
3043             !MONO_TYPE_ISSTRUCT (signature->ret) && !mini_class_is_system_array (method->klass))
3044                 return TRUE;
3045 #endif
3046
3047         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3048             (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3049             (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
3050             (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
3051             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3052             (method->klass->marshalbyref) ||
3053             !header || header->num_clauses ||
3054             /* fixme: why cant we inline valuetype returns? */
3055             MONO_TYPE_ISSTRUCT (signature->ret))
3056                 return FALSE;
3057
3058 #ifdef MONO_ARCH_SOFT_FLOAT
3059         /* this complicates things, fix later */
3060         if (signature->ret->type == MONO_TYPE_R4)
3061                 return FALSE;
3062 #endif
3063         /* its not worth to inline methods with valuetype arguments?? */
3064         for (i = 0; i < signature->param_count; i++) {
3065                 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
3066                         return FALSE;
3067                 }
3068 #ifdef MONO_ARCH_SOFT_FLOAT
3069                 /* this complicates things, fix later */
3070                 if (!signature->params [i]->byref && signature->params [i]->type == MONO_TYPE_R4)
3071                         return FALSE;
3072 #endif
3073         }
3074
3075         /*
3076          * if we can initialize the class of the method right away, we do,
3077          * otherwise we don't allow inlining if the class needs initialization,
3078          * since it would mean inserting a call to mono_runtime_class_init()
3079          * inside the inlined code
3080          */
3081         if (!(cfg->opt & MONO_OPT_SHARED)) {
3082                 vtable = mono_class_vtable (cfg->domain, method->klass);
3083                 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) {
3084                         if (cfg->run_cctors) {
3085                                 /* This makes so that inline cannot trigger */
3086                                 /* .cctors: too many apps depend on them */
3087                                 /* running with a specific order... */
3088                                 if (! vtable->initialized)
3089                                         return FALSE;
3090                                 mono_runtime_class_init (vtable);
3091                         }
3092                 }
3093                 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
3094                         return FALSE;
3095         } else {
3096                 /* 
3097                  * If we're compiling for shared code
3098                  * the cctor will need to be run at aot method load time, for example,
3099                  * or at the end of the compilation of the inlining method.
3100                  */
3101                 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
3102                         return FALSE;
3103         }
3104         //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
3105
3106         /*
3107          * CAS - do not inline methods with declarative security
3108          * Note: this has to be before any possible return TRUE;
3109          */
3110         if (mono_method_has_declsec (method))
3111                 return FALSE;
3112
3113         /* also consider num_locals? */
3114         if (getenv ("MONO_INLINELIMIT")) {
3115                 if (header->code_size < atoi (getenv ("MONO_INLINELIMIT"))) {
3116                         return TRUE;
3117                 }
3118         } else if (header->code_size < INLINE_LENGTH_LIMIT)
3119                 return TRUE;
3120
3121         return FALSE;
3122 }
3123
3124 static gboolean
3125 mini_field_access_needs_cctor_run (MonoCompile *cfg, MonoMethod *method, MonoVTable *vtable)
3126 {
3127         if (vtable->initialized && !cfg->compile_aot)
3128                 return FALSE;
3129
3130         if (vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
3131                 return FALSE;
3132
3133         if (!mono_class_needs_cctor_run (vtable->klass, method))
3134                 return FALSE;
3135
3136         if (! (method->flags & METHOD_ATTRIBUTE_STATIC) && (vtable->klass == method->klass))
3137                 /* The initialization is already done before the method is called */
3138                 return FALSE;
3139
3140         return TRUE;
3141 }
3142
3143 static MonoInst*
3144 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
3145 {
3146         int temp, rank;
3147         MonoInst *addr;
3148         MonoMethod *addr_method;
3149         int element_size;
3150
3151         rank = mono_method_signature (cmethod)->param_count - (is_set? 1: 0);
3152
3153         if (rank == 1) {
3154                 MONO_INST_NEW (cfg, addr, CEE_LDELEMA);
3155                 addr->inst_left = sp [0];
3156                 addr->inst_right = sp [1];
3157                 addr->cil_code = ip;
3158                 addr->type = STACK_MP;
3159                 addr->klass = cmethod->klass->element_class;
3160                 return addr;
3161         }
3162
3163         if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
3164 #if defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(MONO_ARCH_NO_EMULATE_MUL)
3165                 /* OP_LDELEMA2D depends on OP_LMUL */
3166 #else
3167                 MonoInst *indexes;
3168                 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
3169                 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
3170                 addr->inst_left = sp [0];
3171                 addr->inst_right = indexes;
3172                 addr->cil_code = ip;
3173                 addr->type = STACK_MP;
3174                 addr->klass = cmethod->klass->element_class;
3175                 return addr;
3176 #endif
3177         }
3178
3179         element_size = mono_class_array_element_size (cmethod->klass->element_class);
3180         addr_method = mono_marshal_get_array_address (rank, element_size);
3181         temp = mono_emit_method_call_spilled (cfg, bblock, addr_method, addr_method->signature, sp, ip, NULL);
3182         NEW_TEMPLOAD (cfg, addr, temp);
3183         return addr;
3184
3185 }
3186
3187 static MonoJitICallInfo **emul_opcode_map = NULL;
3188
3189 MonoJitICallInfo *
3190 mono_find_jit_opcode_emulation (int opcode)
3191 {
3192         g_assert (opcode >= 0 && opcode <= OP_LAST);
3193         if  (emul_opcode_map)
3194                 return emul_opcode_map [opcode];
3195         else
3196                 return NULL;
3197 }
3198
3199 static int
3200 is_unsigned_regsize_type (MonoType *type)
3201 {
3202         switch (type->type) {
3203         case MONO_TYPE_U1:
3204         case MONO_TYPE_U2:
3205         case MONO_TYPE_U4:
3206 #if SIZEOF_VOID_P == 8
3207         /*case MONO_TYPE_U8: this requires different opcodes in inssel.brg */
3208 #endif
3209                 return TRUE;
3210         default:
3211                 return FALSE;
3212         }
3213 }
3214
3215 static MonoInst*
3216 mini_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
3217 {
3218         MonoInst *ins = NULL;
3219         
3220         static MonoClass *runtime_helpers_class = NULL;
3221         if (! runtime_helpers_class)
3222                 runtime_helpers_class = mono_class_from_name (mono_defaults.corlib,
3223                         "System.Runtime.CompilerServices", "RuntimeHelpers");
3224
3225         if (cmethod->klass == mono_defaults.string_class) {
3226                 if (strcmp (cmethod->name, "get_Chars") == 0) {
3227                         MONO_INST_NEW (cfg, ins, OP_GETCHR);
3228                         ins->inst_i0 = args [0];
3229                         ins->inst_i1 = args [1];
3230                         return ins;
3231                 } else if (strcmp (cmethod->name, "get_Length") == 0) {
3232                         MONO_INST_NEW (cfg, ins, OP_STRLEN);
3233                         ins->inst_i0 = args [0];
3234                         return ins;
3235                 } else if (strcmp (cmethod->name, "InternalSetChar") == 0) {
3236                         MonoInst *get_addr;
3237                         MONO_INST_NEW (cfg, get_addr, OP_STR_CHAR_ADDR);
3238                         get_addr->inst_i0 = args [0];
3239                         get_addr->inst_i1 = args [1];
3240                         MONO_INST_NEW (cfg, ins, CEE_STIND_I2);
3241                         ins->inst_i0 = get_addr;
3242                         ins->inst_i1 = args [2];
3243                         return ins;
3244                 } else 
3245                         return NULL;
3246         } else if (cmethod->klass == mono_defaults.object_class) {
3247                 if (strcmp (cmethod->name, "GetType") == 0) {
3248                         MONO_INST_NEW (cfg, ins, OP_GETTYPE);
3249                         ins->inst_i0 = args [0];
3250                         return ins;
3251                 /* The OP_GETHASHCODE rule depends on OP_MUL */
3252 #if !defined(MONO_ARCH_EMULATE_MUL_DIV) && !defined(HAVE_MOVING_COLLECTOR)
3253                 } else if (strcmp (cmethod->name, "InternalGetHashCode") == 0) {
3254                         MONO_INST_NEW (cfg, ins, OP_GETHASHCODE);
3255                         ins->inst_i0 = args [0];
3256                         return ins;
3257 #endif
3258                 } else if (strcmp (cmethod->name, ".ctor") == 0) {
3259                         MONO_INST_NEW (cfg, ins, OP_NOP);
3260                         return ins;
3261                 } else
3262                         return NULL;
3263         } else if (cmethod->klass == mono_defaults.array_class) {
3264                 if (cmethod->name [0] != 'g')
3265                         return NULL;
3266
3267                 if (strcmp (cmethod->name, "get_Rank") == 0) {
3268                         MONO_INST_NEW (cfg, ins, OP_ARRAY_RANK);
3269                         ins->inst_i0 = args [0];
3270                         return ins;
3271                 } else if (strcmp (cmethod->name, "get_Length") == 0) {
3272                         MONO_INST_NEW (cfg, ins, CEE_LDLEN);
3273                         ins->inst_i0 = args [0];
3274                         return ins;
3275                 } else
3276                         return NULL;
3277         } else if (cmethod->klass == runtime_helpers_class) {
3278                 if (strcmp (cmethod->name, "get_OffsetToStringData") == 0) {
3279                         NEW_ICONST (cfg, ins, G_STRUCT_OFFSET (MonoString, chars));
3280                         return ins;
3281                 } else
3282                         return NULL;
3283         } else if (cmethod->klass == mono_defaults.thread_class) {
3284                 if (strcmp (cmethod->name, "get_CurrentThread") == 0 && (ins = mono_arch_get_thread_intrinsic (cfg)))
3285                         return ins;
3286         } else if (mini_class_is_system_array (cmethod->klass) &&
3287                         strcmp (cmethod->name, "GetGenericValueImpl") == 0) {
3288                 MonoInst *sp [2];
3289                 MonoInst *ldelem, *store, *load;
3290                 MonoClass *eklass = mono_class_from_mono_type (fsig->params [1]);
3291                 int n;
3292                 n = mono_type_to_stind (&eklass->byval_arg);
3293                 if (n == CEE_STOBJ)
3294                         return NULL;
3295                 sp [0] = args [0];
3296                 sp [1] = args [1];
3297                 NEW_LDELEMA (cfg, ldelem, sp, eklass);
3298                 ldelem->flags |= MONO_INST_NORANGECHECK;
3299                 MONO_INST_NEW (cfg, store, n);
3300                 n = mono_type_to_ldind (&eklass->byval_arg);
3301                 MONO_INST_NEW (cfg, load, mono_type_to_ldind (&eklass->byval_arg));
3302                 type_to_eval_stack_type (&eklass->byval_arg, load);
3303                 load->inst_left = ldelem;
3304                 store->inst_left = args [2];
3305                 store->inst_right = load;
3306                 return store;
3307         } else if (cmethod->klass == mono_defaults.math_class) {
3308                 if (strcmp (cmethod->name, "Min") == 0) {
3309                         if (is_unsigned_regsize_type (fsig->params [0])) {
3310                                 MONO_INST_NEW (cfg, ins, OP_MIN);
3311                                 ins->inst_i0 = args [0];
3312                                 ins->inst_i1 = args [1];
3313                                 return ins;
3314                         }
3315                 } else if (strcmp (cmethod->name, "Max") == 0) {
3316                         if (is_unsigned_regsize_type (fsig->params [0])) {
3317                                 MONO_INST_NEW (cfg, ins, OP_MAX);
3318                                 ins->inst_i0 = args [0];
3319                                 ins->inst_i1 = args [1];
3320                                 return ins;
3321                         }
3322                 }
3323         } else if (cmethod->klass->image == mono_defaults.corlib) {
3324                 if (cmethod->name [0] == 'B' && strcmp (cmethod->name, "Break") == 0
3325                                 && strcmp (cmethod->klass->name, "Debugger") == 0) {
3326                         MONO_INST_NEW (cfg, ins, OP_BREAK);
3327                         return ins;
3328                 }
3329                 if (cmethod->name [0] == 'g' && strcmp (cmethod->name, "get_IsRunningOnWindows") == 0
3330                                 && strcmp (cmethod->klass->name, "Environment") == 0) {
3331 #ifdef PLATFORM_WIN32
3332                         NEW_ICONST (cfg, ins, 1);
3333 #else
3334                         NEW_ICONST (cfg, ins, 0);
3335 #endif
3336                         return ins;
3337                 }
3338         }
3339
3340         return mono_arch_get_inst_for_method (cfg, cmethod, fsig, args);
3341 }
3342
3343 static void
3344 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
3345 {
3346         MonoInst *store, *temp;
3347         int i;
3348
3349         g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
3350
3351         if (!sig->hasthis && sig->param_count == 0) 
3352                 return;
3353
3354         if (sig->hasthis) {
3355                 if (sp [0]->opcode == OP_ICONST) {
3356                         *args++ = sp [0];
3357                 } else {
3358                         temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
3359                         *args++ = temp;
3360                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
3361                         store->cil_code = sp [0]->cil_code;
3362                         MONO_ADD_INS (bblock, store);
3363                 }
3364                 sp++;
3365         }
3366
3367         for (i = 0; i < sig->param_count; ++i) {
3368                 if (sp [0]->opcode == OP_ICONST) {
3369                         *args++ = sp [0];
3370                 } else {
3371                         temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
3372                         *args++ = temp;
3373                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
3374                         store->cil_code = sp [0]->cil_code;
3375                         if (store->opcode == CEE_STOBJ) {
3376                                 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
3377                                 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE, FALSE);
3378                         } else {
3379                                 MONO_ADD_INS (bblock, store);
3380                         } 
3381                 }
3382                 sp++;
3383         }
3384 }
3385 #define MONO_INLINE_CALLED_LIMITED_METHODS 0
3386 #define MONO_INLINE_CALLER_LIMITED_METHODS 0
3387
3388 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
3389 static char*
3390 mono_inline_called_method_name_limit = NULL;
3391 static gboolean check_inline_called_method_name_limit (MonoMethod *called_method) {
3392         char *called_method_name = mono_method_full_name (called_method, TRUE);
3393         int strncmp_result;
3394         
3395         if (mono_inline_called_method_name_limit == NULL) {
3396                 char *limit_string = getenv ("MONO_INLINE_CALLED_METHOD_NAME_LIMIT");
3397                 if (limit_string != NULL) {
3398                         mono_inline_called_method_name_limit = limit_string;
3399                 } else {
3400                         mono_inline_called_method_name_limit = (char *) "";
3401                 }
3402         }
3403         
3404         strncmp_result = strncmp (called_method_name, mono_inline_called_method_name_limit, strlen (mono_inline_called_method_name_limit));
3405         g_free (called_method_name);
3406         
3407         //return (strncmp_result <= 0);
3408         return (strncmp_result == 0);
3409 }
3410 #endif
3411
3412 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
3413 static char*
3414 mono_inline_caller_method_name_limit = NULL;
3415 static gboolean check_inline_caller_method_name_limit (MonoMethod *caller_method) {
3416         char *caller_method_name = mono_method_full_name (caller_method, TRUE);
3417         int strncmp_result;
3418         
3419         if (mono_inline_caller_method_name_limit == NULL) {
3420                 char *limit_string = getenv ("MONO_INLINE_CALLER_METHOD_NAME_LIMIT");
3421                 if (limit_string != NULL) {
3422                         mono_inline_caller_method_name_limit = limit_string;
3423                 } else {
3424                         mono_inline_caller_method_name_limit = (char *) "";
3425                 }
3426         }
3427         
3428         strncmp_result = strncmp (caller_method_name, mono_inline_caller_method_name_limit, strlen (mono_inline_caller_method_name_limit));
3429         g_free (caller_method_name);
3430         
3431         //return (strncmp_result <= 0);
3432         return (strncmp_result == 0);
3433 }
3434 #endif
3435
3436 static int
3437 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
3438                 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b, gboolean inline_allways)
3439 {
3440         MonoInst *ins, *rvar = NULL;
3441         MonoMethodHeader *cheader;
3442         MonoBasicBlock *ebblock, *sbblock;
3443         int i, costs, new_locals_offset;
3444         MonoMethod *prev_inlined_method;
3445         MonoBasicBlock **prev_cil_offset_to_bb;
3446         unsigned char* prev_cil_start;
3447         guint32 prev_cil_offset_to_bb_len;
3448
3449         g_assert (cfg->exception_type == MONO_EXCEPTION_NONE);
3450
3451 #if (MONO_INLINE_CALLED_LIMITED_METHODS)
3452         if ((! inline_allways) && ! check_inline_called_method_name_limit (cmethod))
3453                 return 0;
3454 #endif
3455 #if (MONO_INLINE_CALLER_LIMITED_METHODS)
3456         if ((! inline_allways) && ! check_inline_caller_method_name_limit (cfg->method))
3457                 return 0;
3458 #endif
3459
3460         if (bblock->out_of_line && !inline_allways)
3461                 return 0;
3462
3463         if (cfg->verbose_level > 2)
3464                 g_print ("INLINE START %p %s -> %s\n", cmethod,  mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
3465
3466         if (!cmethod->inline_info) {
3467                 mono_jit_stats.inlineable_methods++;
3468                 cmethod->inline_info = 1;
3469         }
3470         /* allocate space to store the return value */
3471         if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3472                 rvar =  mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3473         }
3474
3475         /* allocate local variables */
3476         cheader = mono_method_get_header (cmethod);
3477         new_locals_offset = cfg->num_varinfo;
3478         for (i = 0; i < cheader->num_locals; ++i)
3479                 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
3480         
3481         /* allocate starte and end blocks */
3482         sbblock = NEW_BBLOCK (cfg);
3483         sbblock->block_num = cfg->num_bblocks++;
3484         sbblock->real_offset = real_offset;
3485
3486         ebblock = NEW_BBLOCK (cfg);
3487         ebblock->block_num = cfg->num_bblocks++;
3488         ebblock->real_offset = real_offset;
3489
3490         prev_inlined_method = cfg->inlined_method;
3491         cfg->inlined_method = cmethod;
3492         prev_cil_offset_to_bb = cfg->cil_offset_to_bb;
3493         prev_cil_offset_to_bb_len = cfg->cil_offset_to_bb_len;
3494         prev_cil_start = cfg->cil_start;
3495
3496         costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
3497
3498         cfg->inlined_method = prev_inlined_method;
3499         cfg->cil_offset_to_bb = prev_cil_offset_to_bb;
3500         cfg->cil_offset_to_bb_len = prev_cil_offset_to_bb_len;
3501         cfg->cil_start = prev_cil_start;
3502
3503         if ((costs >= 0 && costs < 60) || inline_allways) {
3504                 if (cfg->verbose_level > 2)
3505                         g_print ("INLINE END %s -> %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (cmethod, TRUE));
3506                 
3507                 mono_jit_stats.inlined_methods++;
3508
3509                 /* always add some code to avoid block split failures */
3510                 MONO_INST_NEW (cfg, ins, OP_NOP);
3511                 MONO_ADD_INS (bblock, ins);
3512                 ins->cil_code = ip;
3513
3514                 bblock->next_bb = sbblock;
3515                 link_bblock (cfg, bblock, sbblock);
3516
3517                 if (rvar) {
3518                         NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
3519                         *sp++ = ins;
3520                 }
3521                 *last_b = ebblock;
3522                 return costs + 1;
3523         } else {
3524                 if (cfg->verbose_level > 2)
3525                         g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
3526                 cfg->exception_type = MONO_EXCEPTION_NONE;
3527         }
3528         return 0;
3529 }
3530
3531 /*
3532  * Some of these comments may well be out-of-date.
3533  * Design decisions: we do a single pass over the IL code (and we do bblock 
3534  * splitting/merging in the few cases when it's required: a back jump to an IL
3535  * address that was not already seen as bblock starting point).
3536  * Code is validated as we go (full verification is still better left to metadata/verify.c).
3537  * Complex operations are decomposed in simpler ones right away. We need to let the 
3538  * arch-specific code peek and poke inside this process somehow (except when the 
3539  * optimizations can take advantage of the full semantic info of coarse opcodes).
3540  * All the opcodes of the form opcode.s are 'normalized' to opcode.
3541  * MonoInst->opcode initially is the IL opcode or some simplification of that 
3542  * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific 
3543  * opcode with value bigger than OP_LAST.
3544  * At this point the IR can be handed over to an interpreter, a dumb code generator
3545  * or to the optimizing code generator that will translate it to SSA form.
3546  *
3547  * Profiling directed optimizations.
3548  * We may compile by default with few or no optimizations and instrument the code
3549  * or the user may indicate what methods to optimize the most either in a config file
3550  * or through repeated runs where the compiler applies offline the optimizations to 
3551  * each method and then decides if it was worth it.
3552  *
3553  */
3554
3555 #define CHECK_TYPE(ins) if (!(ins)->type) UNVERIFIED
3556 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) UNVERIFIED
3557 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) UNVERIFIED
3558 #define CHECK_ARG(num) if ((unsigned)(num) >= (unsigned)num_args) UNVERIFIED
3559 #define CHECK_LOCAL(num) if ((unsigned)(num) >= (unsigned)header->num_locals) UNVERIFIED
3560 #define CHECK_OPSIZE(size) if (ip + size > end) UNVERIFIED
3561 #define CHECK_UNVERIFIABLE(cfg) if (cfg->unverifiable) UNVERIFIED
3562 #define CHECK_TYPELOAD(klass) if (!(klass) || (klass)->exception_type) {cfg->exception_ptr = klass; goto load_error;}
3563
3564 /* offset from br.s -> br like opcodes */
3565 #define BIG_BRANCH_OFFSET 13
3566
3567 static inline gboolean
3568 ip_in_bb (MonoCompile *cfg, MonoBasicBlock *bb, const guint8* ip)
3569 {
3570         MonoBasicBlock *b = cfg->cil_offset_to_bb [ip - cfg->cil_start];
3571         
3572         return b == NULL || b == bb;
3573 }
3574
3575 static int
3576 get_basic_blocks (MonoCompile *cfg, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
3577 {
3578         unsigned char *ip = start;
3579         unsigned char *target;
3580         int i;
3581         guint cli_addr;
3582         MonoBasicBlock *bblock;
3583         const MonoOpcode *opcode;
3584
3585         while (ip < end) {
3586                 cli_addr = ip - start;
3587                 i = mono_opcode_value ((const guint8 **)&ip, end);
3588                 if (i < 0)
3589                         UNVERIFIED;
3590                 opcode = &mono_opcodes [i];
3591                 switch (opcode->argument) {
3592                 case MonoInlineNone:
3593                         ip++; 
3594                         break;
3595                 case MonoInlineString:
3596                 case MonoInlineType:
3597                 case MonoInlineField:
3598                 case MonoInlineMethod:
3599                 case MonoInlineTok:
3600                 case MonoInlineSig:
3601                 case MonoShortInlineR:
3602                 case MonoInlineI:
3603                         ip += 5;
3604                         break;
3605                 case MonoInlineVar:
3606                         ip += 3;
3607                         break;
3608                 case MonoShortInlineVar:
3609                 case MonoShortInlineI:
3610                         ip += 2;
3611                         break;
3612                 case MonoShortInlineBrTarget:
3613                         target = start + cli_addr + 2 + (signed char)ip [1];
3614                         GET_BBLOCK (cfg, bblock, target);
3615                         ip += 2;
3616                         if (ip < end)
3617                                 GET_BBLOCK (cfg, bblock, ip);
3618                         break;
3619                 case MonoInlineBrTarget:
3620                         target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
3621                         GET_BBLOCK (cfg, bblock, target);
3622                         ip += 5;
3623                         if (ip < end)
3624                                 GET_BBLOCK (cfg, bblock, ip);
3625                         break;
3626                 case MonoInlineSwitch: {
3627                         guint32 n = read32 (ip + 1);
3628                         guint32 j;
3629                         ip += 5;
3630                         cli_addr += 5 + 4 * n;
3631                         target = start + cli_addr;
3632                         GET_BBLOCK (cfg, bblock, target);
3633                         
3634                         for (j = 0; j < n; ++j) {
3635                                 target = start + cli_addr + (gint32)read32 (ip);
3636                                 GET_BBLOCK (cfg, bblock, target);
3637                                 ip += 4;
3638                         }
3639                         break;
3640                 }
3641                 case MonoInlineR:
3642                 case MonoInlineI8:
3643                         ip += 9;
3644                         break;
3645                 default:
3646                         g_assert_not_reached ();
3647                 }
3648
3649                 if (i == CEE_THROW) {
3650                         unsigned char *bb_start = ip - 1;
3651                         
3652                         /* Find the start of the bblock containing the throw */
3653                         bblock = NULL;
3654                         while ((bb_start >= start) && !bblock) {
3655                                 bblock = cfg->cil_offset_to_bb [(bb_start) - start];
3656                                 bb_start --;
3657                         }
3658                         if (bblock)
3659                                 bblock->out_of_line = 1;
3660                 }
3661         }
3662         return 0;
3663 unverified:
3664         *pos = ip;
3665         return 1;
3666 }
3667
3668 static MonoInst*
3669 emit_tree (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *ins, const guint8* ip_next)
3670 {
3671         MonoInst *store, *temp, *load;
3672         
3673         if (ip_in_bb (cfg, bblock, ip_next) &&
3674                 (CODE_IS_STLOC (ip_next) || *ip_next == CEE_RET))
3675                         return ins;
3676         
3677         temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
3678         temp->flags |= MONO_INST_IS_TEMP;
3679         NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3680         store->cil_code = ins->cil_code;
3681         MONO_ADD_INS (bblock, store);
3682         NEW_TEMPLOAD (cfg, load, temp->inst_c0);
3683         load->cil_code = ins->cil_code;
3684         return load;
3685 }
3686
3687 static inline MonoMethod *
3688 mini_get_method (MonoMethod *m, guint32 token, MonoClass *klass, MonoGenericContext *context)
3689 {
3690         MonoMethod *method;
3691
3692         if (m->wrapper_type != MONO_WRAPPER_NONE)
3693                 return mono_method_get_wrapper_data (m, token);
3694
3695         method = mono_get_method_full (m->klass->image, token, klass, context);
3696
3697         if (method && method->is_inflated)
3698                 method = mono_get_inflated_method (method);
3699
3700         return method;
3701 }
3702
3703 static inline MonoClass*
3704 mini_get_class (MonoMethod *method, guint32 token, MonoGenericContext *context)
3705 {
3706         MonoClass *klass;
3707
3708         if (method->wrapper_type != MONO_WRAPPER_NONE)
3709                 klass = mono_method_get_wrapper_data (method, token);
3710         else
3711                 klass = mono_class_get_full (method->klass->image, token, context);
3712         if (klass)
3713                 mono_class_init (klass);
3714         return klass;
3715 }
3716
3717 /*
3718  * Returns TRUE if the JIT should abort inlining because "callee"
3719  * is influenced by security attributes.
3720  */
3721 static
3722 gboolean check_linkdemand (MonoCompile *cfg, MonoMethod *caller, MonoMethod *callee, MonoBasicBlock *bblock, unsigned char *ip)
3723 {
3724         guint32 result;
3725         
3726         if ((cfg->method != caller) && mono_method_has_declsec (callee)) {
3727                 return TRUE;
3728         }
3729         
3730         result = mono_declsec_linkdemand (cfg->domain, caller, callee);
3731         if (result == MONO_JIT_SECURITY_OK)
3732                 return FALSE;
3733
3734         if (result == MONO_JIT_LINKDEMAND_ECMA) {
3735                 /* Generate code to throw a SecurityException before the actual call/link */
3736                 MonoAssembly *assembly = mono_image_get_assembly (caller->klass->image);
3737                 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (cfg->domain, assembly);
3738                 MonoReflectionMethod *refmet = mono_method_get_object (cfg->domain, caller, NULL);
3739                 MonoSecurityManager *secman = mono_security_manager_get_methods ();
3740                 MonoInst *args [3];
3741
3742                 NEW_ICONST (cfg, args [0], 4);
3743                 NEW_PCONST (cfg, args [1], refass);
3744                 NEW_PCONST (cfg, args [2], refmet);
3745                 mono_emit_method_call_spilled (cfg, bblock, secman->linkdemandsecurityexception, mono_method_signature (secman->linkdemandsecurityexception), args, ip, NULL);
3746         } else if (cfg->exception_type == MONO_EXCEPTION_NONE) {
3747                  /* don't hide previous results */
3748                 cfg->exception_type = MONO_EXCEPTION_SECURITY_LINKDEMAND;
3749                 cfg->exception_data = result;
3750         }
3751         
3752         return FALSE;
3753 }
3754
3755 /*
3756  * Check that the IL instructions at ip are the array initialization
3757  * sequence and return the pointer to the data and the size.
3758  */
3759 static const char*
3760 initialize_array_data (MonoMethod *method, gboolean aot, unsigned char *ip, MonoInst *newarr, int *out_size)
3761 {
3762         /*
3763          * newarr[System.Int32]
3764          * dup
3765          * ldtoken field valuetype ...
3766          * call void class [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, valuetype [mscorlib]System.RuntimeFieldHandle)
3767          */
3768         if (ip [0] == CEE_DUP && ip [1] == CEE_LDTOKEN && ip [5] == 0x4 && ip [6] == CEE_CALL) {
3769                 MonoClass *klass = newarr->inst_newa_class;
3770                 guint32 token = read32 (ip + 7);
3771                 guint32 rva, field_index;
3772                 const char *data_ptr;
3773                 int size = 0;
3774                 MonoMethod *cmethod;
3775
3776                 if (newarr->inst_newa_len->opcode != OP_ICONST)
3777                         return NULL;
3778                 cmethod = mini_get_method (method, token, NULL, NULL);
3779                 if (!cmethod)
3780                         return NULL;
3781                 if (strcmp (cmethod->name, "InitializeArray") || strcmp (cmethod->klass->name, "RuntimeHelpers") || cmethod->klass->image != mono_defaults.corlib)
3782                         return NULL;
3783                 switch (mono_type_get_underlying_type (&klass->byval_arg)->type) {
3784                 case MONO_TYPE_BOOLEAN:
3785                 case MONO_TYPE_I1:
3786                 case MONO_TYPE_U1:
3787                         size = 1; break;
3788                 /* we need to swap on big endian, so punt. Should we handle R4 and R8 as well? */
3789 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
3790                 case MONO_TYPE_CHAR:
3791                 case MONO_TYPE_I2:
3792                 case MONO_TYPE_U2:
3793                         size = 2; break;
3794                 case MONO_TYPE_I4:
3795                 case MONO_TYPE_U4:
3796                 case MONO_TYPE_R4:
3797                         size = 4; break;
3798                 case MONO_TYPE_R8:
3799 #ifdef ARM_FPU_FPA
3800                         return NULL; /* stupid ARM FP swapped format */
3801 #endif
3802                 case MONO_TYPE_I8:
3803                 case MONO_TYPE_U8:
3804                         size = 8; break;
3805 #endif
3806                 default:
3807                         return NULL;
3808                 }
3809                 size *= newarr->inst_newa_len->inst_c0;
3810                 *out_size = size;
3811                 /*g_print ("optimized in %s: size: %d, numelems: %d\n", method->name, size, newarr->inst_newa_len->inst_c0);*/
3812                 field_index = read32 (ip + 2) & 0xffffff;
3813                 mono_metadata_field_info (method->klass->image, field_index - 1, NULL, &rva, NULL);
3814                 data_ptr = mono_image_rva_map (method->klass->image, rva);
3815                 /*g_print ("field: 0x%08x, rva: %d, rva_ptr: %p\n", read32 (ip + 2), rva, data_ptr);*/
3816                 /* for aot code we do the lookup on load */
3817                 if (aot && data_ptr)
3818                         return GUINT_TO_POINTER (rva);
3819                 return data_ptr;
3820         }
3821         return NULL;
3822 }
3823
3824 /*
3825  * mono_method_to_ir: translates IL into basic blocks containing trees
3826  */
3827 static int
3828 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock, 
3829                    int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args, 
3830                    guint inline_offset, gboolean is_virtual_call)
3831 {
3832         MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
3833         MonoInst *ins, **sp, **stack_start;
3834         MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
3835         MonoMethod *cmethod;
3836         MonoInst **arg_array;
3837         MonoMethodHeader *header;
3838         MonoImage *image;
3839         guint32 token, ins_flag;
3840         MonoClass *klass;
3841         MonoClass *constrained_call = NULL;
3842         unsigned char *ip, *end, *target, *err_pos;
3843         static double r8_0 = 0.0;
3844         MonoMethodSignature *sig;
3845         MonoGenericContext *generic_context = NULL;
3846         MonoGenericContainer *generic_container = NULL;
3847         MonoType **param_types;
3848         GList *bb_recheck = NULL, *tmp;
3849         int i, n, start_new_bblock, ialign;
3850         int num_calls = 0, inline_costs = 0;
3851         int breakpoint_id = 0;
3852         guint32 align;
3853         guint real_offset, num_args;
3854         MonoBoolean security, pinvoke;
3855         MonoSecurityManager* secman = NULL;
3856         MonoDeclSecurityActions actions;
3857         GSList *class_inits = NULL;
3858         gboolean dont_verify, dont_verify_stloc;
3859
3860         /* serialization and xdomain stuff may need access to private fields and methods */
3861         dont_verify = method->klass->image->assembly->corlib_internal? TRUE: FALSE;
3862         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE;
3863         dont_verify |= method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH;
3864         dont_verify |= method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE; /* bug #77896 */
3865         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP;
3866         dont_verify |= method->wrapper_type == MONO_WRAPPER_COMINTEROP_INVOKE;
3867
3868         /* still some type unsafety issues in marshal wrappers... (unknown is PtrToStructure) */
3869         dont_verify_stloc = method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE;
3870         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_UNKNOWN;
3871         dont_verify_stloc |= method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED;
3872
3873         /* Not turned on yet */
3874         cfg->dont_verify_stack_merge = TRUE;
3875
3876         image = method->klass->image;
3877         header = mono_method_get_header (method);
3878         generic_container = method->generic_container;
3879         sig = mono_method_signature (method);
3880         num_args = sig->hasthis + sig->param_count;
3881         ip = (unsigned char*)header->code;
3882         cfg->cil_start = ip;
3883         end = ip + header->code_size;
3884         mono_jit_stats.cil_code_size += header->code_size;
3885
3886         if (sig->is_inflated)
3887                 generic_context = mono_method_get_context (method);
3888         else if (generic_container)
3889                 generic_context = &generic_container->context;
3890
3891         g_assert (!sig->has_type_parameters);
3892
3893         if (cfg->method == method)
3894                 real_offset = 0;
3895         else
3896                 real_offset = inline_offset;
3897
3898         cfg->cil_offset_to_bb = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoBasicBlock*) * header->code_size);
3899         cfg->cil_offset_to_bb_len = header->code_size;
3900
3901         if (cfg->verbose_level > 2)
3902                 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
3903
3904         dont_inline = g_list_prepend (dont_inline, method);
3905         if (cfg->method == method) {
3906
3907                 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
3908                         cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
3909
3910                 /* ENTRY BLOCK */
3911                 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
3912                 start_bblock->cil_code = NULL;
3913                 start_bblock->cil_length = 0;
3914                 start_bblock->block_num = cfg->num_bblocks++;
3915
3916                 /* EXIT BLOCK */
3917                 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
3918                 end_bblock->cil_code = NULL;
3919                 end_bblock->cil_length = 0;
3920                 end_bblock->block_num = cfg->num_bblocks++;
3921                 g_assert (cfg->num_bblocks == 2);
3922
3923                 arg_array = alloca (sizeof (MonoInst *) * num_args);
3924                 for (i = num_args - 1; i >= 0; i--)
3925                         arg_array [i] = cfg->varinfo [i];
3926
3927                 if (header->num_clauses) {
3928                         cfg->spvars = g_hash_table_new (NULL, NULL);
3929                         cfg->exvars = g_hash_table_new (NULL, NULL);
3930                 }
3931                 /* handle exception clauses */
3932                 for (i = 0; i < header->num_clauses; ++i) {
3933                         MonoBasicBlock *try_bb;
3934                         MonoExceptionClause *clause = &header->clauses [i];
3935                         GET_BBLOCK (cfg, try_bb, ip + clause->try_offset);
3936                         try_bb->real_offset = clause->try_offset;
3937                         GET_BBLOCK (cfg, tblock, ip + clause->handler_offset);
3938                         tblock->real_offset = clause->handler_offset;
3939                         tblock->flags |= BB_EXCEPTION_HANDLER;
3940
3941                         link_bblock (cfg, try_bb, tblock);
3942
3943                         if (*(ip + clause->handler_offset) == CEE_POP)
3944                                 tblock->flags |= BB_EXCEPTION_DEAD_OBJ;
3945
3946                         if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
3947                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER ||
3948                             clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) {
3949                                 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3950                                 MONO_ADD_INS (tblock, ins);
3951
3952                                 /* todo: is a fault block unsafe to optimize? */
3953                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT)
3954                                         tblock->flags |= BB_EXCEPTION_UNSAFE;
3955                         }
3956
3957
3958                         /*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);
3959                           while (p < end) {
3960                           g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
3961                           }*/
3962                         /* catch and filter blocks get the exception object on the stack */
3963                         if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
3964                             clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3965                                 MonoInst *load, *dummy_use;
3966
3967                                 /* mostly like handle_stack_args (), but just sets the input args */
3968                                 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
3969                                 tblock->in_scount = 1;
3970                                 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3971                                 tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3972                                 tblock->stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot));
3973                                 tblock->stack_state [0].type = STACK_OBJ;
3974                                 /* FIXME? */
3975                                 tblock->stack_state [0].klass = mono_defaults.object_class;
3976
3977                                 /* 
3978                                  * Add a dummy use for the exvar so its liveness info will be
3979                                  * correct.
3980                                  */
3981                                 NEW_TEMPLOAD (cfg, load, tblock->in_stack [0]->inst_c0);
3982                                 NEW_DUMMY_USE (cfg, dummy_use, load);
3983                                 MONO_ADD_INS (tblock, dummy_use);
3984                                 
3985                                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3986                                         GET_BBLOCK (cfg, tblock, ip + clause->data.filter_offset);
3987                                         tblock->real_offset = clause->data.filter_offset;
3988                                         tblock->in_scount = 1;
3989                                         tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
3990                                         tblock->stack_state = mono_mempool_alloc (cfg->mempool, sizeof (MonoStackSlot));
3991                                         tblock->stack_state [0].type = STACK_OBJ;
3992                                         /* FIXME? */
3993                                         tblock->stack_state [0].klass = mono_defaults.object_class;
3994
3995                                         /* The filter block shares the exvar with the handler block */
3996                                         tblock->in_stack [0] = mono_create_exvar_for_offset (cfg, clause->handler_offset);
3997                                         MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
3998                                         MONO_ADD_INS (tblock, ins);
3999                                 }
4000                         }
4001                 }
4002         } else {
4003                 arg_array = alloca (sizeof (MonoInst *) * num_args);
4004                 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
4005         }
4006
4007         /* FIRST CODE BLOCK */
4008         bblock = NEW_BBLOCK (cfg);
4009         bblock->cil_code = ip;
4010
4011         ADD_BBLOCK (cfg, bblock);
4012
4013         if (cfg->method == method) {
4014                 breakpoint_id = mono_debugger_method_has_breakpoint (method);
4015                 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
4016                         MONO_INST_NEW (cfg, ins, OP_BREAK);
4017                         MONO_ADD_INS (bblock, ins);
4018                 }
4019         }
4020
4021         if (mono_use_security_manager)
4022                 secman = mono_security_manager_get_methods ();
4023
4024         security = (secman && mono_method_has_declsec (method));
4025         /* at this point having security doesn't mean we have any code to generate */
4026         if (security && (cfg->method == method)) {
4027                 /* Only Demand, NonCasDemand and DemandChoice requires code generation.
4028                  * And we do not want to enter the next section (with allocation) if we
4029                  * have nothing to generate */
4030                 security = mono_declsec_get_demands (method, &actions);
4031         }
4032
4033         /* we must Demand SecurityPermission.Unmanaged before P/Invoking */
4034         pinvoke = (secman && (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE));
4035         if (pinvoke) {
4036                 MonoMethod *wrapped = mono_marshal_method_from_wrapper (method);
4037                 if (wrapped && (wrapped->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
4038                         MonoCustomAttrInfo* custom = mono_custom_attrs_from_method (wrapped);
4039
4040                         /* unless the method or it's class has the [SuppressUnmanagedCodeSecurity] attribute */
4041                         if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
4042                                 pinvoke = FALSE;
4043                         }
4044
4045                         if (pinvoke) {
4046                                 custom = mono_custom_attrs_from_class (wrapped->klass);
4047                                 if (custom && mono_custom_attrs_has_attr (custom, secman->suppressunmanagedcodesecurity)) {
4048                                         pinvoke = FALSE;
4049                                 }
4050                         }
4051                 } else {
4052                         /* not a P/Invoke after all */
4053                         pinvoke = FALSE;
4054                 }
4055         }
4056         
4057         if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || cfg->compile_aot || security || pinvoke) {
4058                 /* we use a separate basic block for the initialization code */
4059                 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
4060                 init_localsbb->real_offset = real_offset;
4061                 start_bblock->next_bb = init_localsbb;
4062                 init_localsbb->next_bb = bblock;
4063                 link_bblock (cfg, start_bblock, init_localsbb);
4064                 link_bblock (cfg, init_localsbb, bblock);
4065                 init_localsbb->block_num = cfg->num_bblocks++;
4066         } else {
4067                 start_bblock->next_bb = bblock;
4068                 link_bblock (cfg, start_bblock, bblock);
4069         }
4070
4071         /* at this point we know, if security is TRUE, that some code needs to be generated */
4072         if (security && (cfg->method == method)) {
4073                 MonoInst *args [2];
4074
4075                 mono_jit_stats.cas_demand_generation++;
4076
4077                 if (actions.demand.blob) {
4078                         /* Add code for SecurityAction.Demand */
4079                         NEW_DECLSECCONST (cfg, args[0], image, actions.demand);
4080                         NEW_ICONST (cfg, args [1], actions.demand.size);
4081                         /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
4082                         mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
4083                 }
4084                 if (actions.noncasdemand.blob) {
4085                         /* CLR 1.x uses a .noncasdemand (but 2.x doesn't) */
4086                         /* For Mono we re-route non-CAS Demand to Demand (as the managed code must deal with it anyway) */
4087                         NEW_DECLSECCONST (cfg, args[0], image, actions.noncasdemand);
4088                         NEW_ICONST (cfg, args [1], actions.noncasdemand.size);
4089                         /* Calls static void SecurityManager.InternalDemand (byte* permissions, int size); */
4090                         mono_emit_method_call_spilled (cfg, init_localsbb, secman->demand, mono_method_signature (secman->demand), args, ip, NULL);
4091                 }
4092                 if (actions.demandchoice.blob) {
4093                         /* New in 2.0, Demand must succeed for one of the permissions (i.e. not all) */
4094                         NEW_DECLSECCONST (cfg, args[0], image, actions.demandchoice);
4095                         NEW_ICONST (cfg, args [1], actions.demandchoice.size);
4096                         /* Calls static void SecurityManager.InternalDemandChoice (byte* permissions, int size); */
4097                         mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandchoice, mono_method_signature (secman->demandchoice), args, ip, NULL);
4098                 }
4099         }
4100
4101         /* we must Demand SecurityPermission.Unmanaged before p/invoking */
4102         if (pinvoke) {
4103                 mono_emit_method_call_spilled (cfg, init_localsbb, secman->demandunmanaged, mono_method_signature (secman->demandunmanaged), NULL, ip, NULL);
4104         }
4105
4106         if (get_basic_blocks (cfg, header, real_offset, ip, end, &err_pos)) {
4107                 ip = err_pos;
4108                 UNVERIFIED;
4109         }
4110
4111         if (cfg->method == method)
4112                 mono_debug_init_method (cfg, bblock, breakpoint_id);
4113
4114         param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * num_args);
4115         if (sig->hasthis)
4116                 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
4117         for (n = 0; n < sig->param_count; ++n)
4118                 param_types [n + sig->hasthis] = sig->params [n];
4119         for (n = 0; n < header->num_locals; ++n) {
4120                 if (header->locals [n]->type == MONO_TYPE_VOID && !header->locals [n]->byref)
4121                         UNVERIFIED;
4122         }
4123         class_inits = NULL;
4124
4125         /* do this somewhere outside - not here */
4126         NEW_ICONST (cfg, zero_int32, 0);
4127         NEW_ICONST (cfg, zero_int64, 0);
4128         zero_int64->type = STACK_I8;
4129         NEW_PCONST (cfg, zero_ptr, 0);
4130         NEW_PCONST (cfg, zero_obj, 0);
4131         zero_obj->type = STACK_OBJ;
4132
4133         MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
4134         zero_r8->type = STACK_R8;
4135         zero_r8->inst_p0 = &r8_0;
4136
4137         /* add a check for this != NULL to inlined methods */
4138         if (is_virtual_call) {
4139                 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
4140                 NEW_ARGLOAD (cfg, ins->inst_left, 0);
4141                 ins->cil_code = ip;
4142                 MONO_ADD_INS (bblock, ins);
4143         }
4144
4145         /* we use a spare stack slot in SWITCH and NEWOBJ and others */
4146         stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
4147
4148         ins_flag = 0;
4149         start_new_bblock = 0;
4150         while (ip < end) {
4151
4152                 if (cfg->method == method)
4153                         real_offset = ip - header->code;
4154                 else
4155                         real_offset = inline_offset;
4156
4157                 if (start_new_bblock) {
4158                         bblock->cil_length = ip - bblock->cil_code;
4159                         if (start_new_bblock == 2) {
4160                                 g_assert (ip == tblock->cil_code);
4161                         } else {
4162                                 GET_BBLOCK (cfg, tblock, ip);
4163                         }
4164                         bblock->next_bb = tblock;
4165                         bblock = tblock;
4166                         start_new_bblock = 0;
4167                         for (i = 0; i < bblock->in_scount; ++i) {
4168                                 if (cfg->verbose_level > 3)
4169                                         g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);                                           
4170                                 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
4171                                 *sp++ = ins;
4172                         }
4173                         g_slist_free (class_inits);
4174                         class_inits = NULL;
4175                 } else {
4176                         if ((tblock = cfg->cil_offset_to_bb [ip - cfg->cil_start]) && (tblock != bblock)) {
4177                                 link_bblock (cfg, bblock, tblock);
4178                                 if (sp != stack_start) {
4179                                         handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4180                                         sp = stack_start;
4181                                         CHECK_UNVERIFIABLE (cfg);
4182                                 }
4183                                 bblock->next_bb = tblock;
4184                                 bblock = tblock;
4185                                 for (i = 0; i < bblock->in_scount; ++i) {
4186                                         if (cfg->verbose_level > 3)
4187                                                 g_print ("loading %d from temp %d\n", i, (int)bblock->in_stack [i]->inst_c0);                                           
4188                                         NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
4189                                         *sp++ = ins;
4190                                 }
4191                                 g_slist_free (class_inits);
4192                                 class_inits = NULL;
4193                         }
4194                 }
4195
4196                 bblock->real_offset = real_offset;
4197
4198                 if ((cfg->method == method) && cfg->coverage_info) {
4199                         MonoInst *store, *one;
4200                         guint32 cil_offset = ip - header->code;
4201                         cfg->coverage_info->data [cil_offset].cil_code = ip;
4202
4203                         /* TODO: Use an increment here */
4204                         NEW_ICONST (cfg, one, 1);
4205                         one->cil_code = ip;
4206
4207                         NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
4208                         ins->cil_code = ip;
4209
4210                         MONO_INST_NEW (cfg, store, CEE_STIND_I);
4211                         store->cil_code = ip;
4212                         store->inst_left = ins;
4213                         store->inst_right = one;
4214
4215                         MONO_ADD_INS (bblock, store);
4216                 }
4217
4218                 if (cfg->verbose_level > 3)
4219                         g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, (int)(sp - stack_start), mono_disasm_code_one (NULL, method, ip, NULL));
4220
4221                 switch (*ip) {
4222                 case CEE_NOP:
4223                         MONO_INST_NEW (cfg, ins, OP_NOP);
4224                         ins->cil_code = ip++;
4225                         MONO_ADD_INS (bblock, ins);
4226                         break;
4227                 case CEE_BREAK:
4228                         MONO_INST_NEW (cfg, ins, OP_BREAK);
4229                         ins->cil_code = ip++;
4230                         MONO_ADD_INS (bblock, ins);
4231                         break;
4232                 case CEE_LDARG_0:
4233                 case CEE_LDARG_1:
4234                 case CEE_LDARG_2:
4235                 case CEE_LDARG_3:
4236                         CHECK_STACK_OVF (1);
4237                         n = (*ip)-CEE_LDARG_0;
4238                         CHECK_ARG (n);
4239                         NEW_ARGLOAD (cfg, ins, n);
4240                         LDARG_SOFT_FLOAT (cfg, ins, n, ip);
4241                         ins->cil_code = ip++;
4242                         *sp++ = ins;
4243                         break;
4244                 case CEE_LDLOC_0:
4245                 case CEE_LDLOC_1:
4246                 case CEE_LDLOC_2:
4247                 case CEE_LDLOC_3:
4248                         CHECK_STACK_OVF (1);
4249                         n = (*ip)-CEE_LDLOC_0;
4250                         CHECK_LOCAL (n);
4251                         NEW_LOCLOAD (cfg, ins, n);
4252                         LDLOC_SOFT_FLOAT (cfg, ins, n, ip);
4253                         ins->cil_code = ip++;
4254                         *sp++ = ins;
4255                         break;
4256                 case CEE_STLOC_0:
4257                 case CEE_STLOC_1:
4258                 case CEE_STLOC_2:
4259                 case CEE_STLOC_3:
4260                         CHECK_STACK (1);
4261                         n = (*ip)-CEE_STLOC_0;
4262                         CHECK_LOCAL (n);
4263                         --sp;
4264                         handle_loaded_temps (cfg, bblock, stack_start, sp);
4265                         NEW_LOCSTORE (cfg, ins, n, *sp);
4266                         ins->cil_code = ip;
4267                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
4268                                 UNVERIFIED;
4269                         STLOC_SOFT_FLOAT (cfg, ins, n, ip);
4270                         if (ins->opcode == CEE_STOBJ) {
4271                                 NEW_LOCLOADA (cfg, ins, n);
4272                                 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
4273                         } else
4274                                 MONO_ADD_INS (bblock, ins);
4275                         ++ip;
4276                         inline_costs += 1;
4277                         break;
4278                 case CEE_LDARG_S:
4279                         CHECK_OPSIZE (2);
4280                         CHECK_STACK_OVF (1);
4281                         CHECK_ARG (ip [1]);
4282                         NEW_ARGLOAD (cfg, ins, ip [1]);
4283                         LDARG_SOFT_FLOAT (cfg, ins, ip [1], ip);
4284                         ins->cil_code = ip;
4285                         *sp++ = ins;
4286                         ip += 2;
4287                         break;
4288                 case CEE_LDARGA_S:
4289                         CHECK_OPSIZE (2);
4290                         CHECK_STACK_OVF (1);
4291                         CHECK_ARG (ip [1]);
4292                         NEW_ARGLOADA (cfg, ins, ip [1]);
4293                         ins->cil_code = ip;
4294                         *sp++ = ins;
4295                         ip += 2;
4296                         break;
4297                 case CEE_STARG_S:
4298                         CHECK_OPSIZE (2);
4299                         CHECK_STACK (1);
4300                         --sp;
4301                         CHECK_ARG (ip [1]);
4302                         NEW_ARGSTORE (cfg, ins, ip [1], *sp);
4303                         handle_loaded_temps (cfg, bblock, stack_start, sp);
4304                         ins->cil_code = ip;
4305                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [ip [1]], *sp))
4306                                 UNVERIFIED;
4307                         STARG_SOFT_FLOAT (cfg, ins, ip [1], ip);
4308                         if (ins->opcode == CEE_STOBJ) {
4309                                 NEW_ARGLOADA (cfg, ins, ip [1]);
4310                                 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
4311                         } else
4312                                 MONO_ADD_INS (bblock, ins);
4313                         ip += 2;
4314                         break;
4315                 case CEE_LDLOC_S:
4316                         CHECK_OPSIZE (2);
4317                         CHECK_STACK_OVF (1);
4318                         CHECK_LOCAL (ip [1]);
4319                         NEW_LOCLOAD (cfg, ins, ip [1]);
4320                         LDLOC_SOFT_FLOAT (cfg, ins, ip [1], ip);
4321                         ins->cil_code = ip;
4322                         *sp++ = ins;
4323                         ip += 2;
4324                         break;
4325                 case CEE_LDLOCA_S:
4326                         CHECK_OPSIZE (2);
4327                         CHECK_STACK_OVF (1);
4328                         CHECK_LOCAL (ip [1]);
4329                         NEW_LOCLOADA (cfg, ins, ip [1]);
4330                         ins->cil_code = ip;
4331                         *sp++ = ins;
4332                         ip += 2;
4333                         break;
4334                 case CEE_STLOC_S:
4335                         CHECK_OPSIZE (2);
4336                         CHECK_STACK (1);
4337                         --sp;
4338                         handle_loaded_temps (cfg, bblock, stack_start, sp);
4339                         CHECK_LOCAL (ip [1]);
4340                         NEW_LOCSTORE (cfg, ins, ip [1], *sp);
4341                         ins->cil_code = ip;
4342                         if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [ip [1]], *sp))
4343                                 UNVERIFIED;
4344                         STLOC_SOFT_FLOAT (cfg, ins, ip [1], ip);
4345                         if (ins->opcode == CEE_STOBJ) {
4346                                 NEW_LOCLOADA (cfg, ins, ip [1]);
4347                                 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
4348                         } else
4349                                 MONO_ADD_INS (bblock, ins);
4350                         ip += 2;
4351                         inline_costs += 1;
4352                         break;
4353                 case CEE_LDNULL:
4354                         CHECK_STACK_OVF (1);
4355                         NEW_PCONST (cfg, ins, NULL);
4356                         ins->cil_code = ip;
4357                         ins->type = STACK_OBJ;
4358                         ++ip;
4359                         *sp++ = ins;
4360                         break;
4361                 case CEE_LDC_I4_M1:
4362                         CHECK_STACK_OVF (1);
4363                         NEW_ICONST (cfg, ins, -1);
4364                         ins->cil_code = ip;
4365                         ++ip;
4366                         *sp++ = ins;
4367                         break;
4368                 case CEE_LDC_I4_0:
4369                 case CEE_LDC_I4_1:
4370                 case CEE_LDC_I4_2:
4371                 case CEE_LDC_I4_3:
4372                 case CEE_LDC_I4_4:
4373                 case CEE_LDC_I4_5:
4374                 case CEE_LDC_I4_6:
4375                 case CEE_LDC_I4_7:
4376                 case CEE_LDC_I4_8:
4377                         CHECK_STACK_OVF (1);
4378                         NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
4379                         ins->cil_code = ip;
4380                         ++ip;
4381                         *sp++ = ins;
4382                         break;
4383                 case CEE_LDC_I4_S:
4384                         CHECK_OPSIZE (2);
4385                         CHECK_STACK_OVF (1);
4386                         ++ip;
4387                         NEW_ICONST (cfg, ins, *((signed char*)ip));
4388                         ins->cil_code = ip;
4389                         ++ip;
4390                         *sp++ = ins;
4391                         break;
4392                 case CEE_LDC_I4:
4393                         CHECK_OPSIZE (5);
4394                         CHECK_STACK_OVF (1);
4395                         NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
4396                         ins->cil_code = ip;
4397                         ip += 5;
4398                         *sp++ = ins;
4399                         break;
4400                 case CEE_LDC_I8:
4401                         CHECK_OPSIZE (9);
4402                         CHECK_STACK_OVF (1);
4403                         MONO_INST_NEW (cfg, ins, OP_I8CONST);
4404                         ins->cil_code = ip;
4405                         ins->type = STACK_I8;
4406                         ++ip;
4407                         ins->inst_l = (gint64)read64 (ip);
4408                         ip += 8;
4409                         *sp++ = ins;
4410                         break;
4411                 case CEE_LDC_R4: {
4412                         float *f;
4413                         /* we should really allocate this only late in the compilation process */
4414                         mono_domain_lock (cfg->domain);
4415                         f = mono_mempool_alloc (cfg->domain->mp, sizeof (float));
4416                         mono_domain_unlock (cfg->domain);
4417                         CHECK_OPSIZE (5);
4418                         CHECK_STACK_OVF (1);
4419                         MONO_INST_NEW (cfg, ins, OP_R4CONST);
4420                         ins->type = STACK_R8;
4421                         ++ip;
4422                         readr4 (ip, f);
4423                         ins->inst_p0 = f;
4424
4425                         ip += 4;
4426                         *sp++ = ins;                    
4427                         break;
4428                 }
4429                 case CEE_LDC_R8: {
4430                         double *d;
4431                         mono_domain_lock (cfg->domain);
4432                         d = mono_mempool_alloc (cfg->domain->mp, sizeof (double));
4433                         mono_domain_unlock (cfg->domain);
4434                         CHECK_OPSIZE (9);
4435                         CHECK_STACK_OVF (1);
4436                         MONO_INST_NEW (cfg, ins, OP_R8CONST);
4437                         ins->type = STACK_R8;
4438                         ++ip;
4439                         readr8 (ip, d);
4440                         ins->inst_p0 = d;
4441
4442                         ip += 8;
4443                         *sp++ = ins;                    
4444                         break;
4445                 }
4446                 case CEE_DUP: {
4447                         MonoInst *temp, *store;
4448                         CHECK_STACK (1);
4449                         CHECK_STACK_OVF (1);
4450                         sp--;
4451                         ins = *sp;
4452                 
4453                         /* 
4454                          * small optimization: if the loaded value was from a local already,
4455                          * just load it twice.
4456                          */
4457                         if (ins->ssa_op == MONO_SSA_LOAD && 
4458                             (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
4459                                 sp++;
4460                                 MONO_INST_NEW (cfg, temp, 0);
4461                                 *temp = *ins;
4462                                 temp->cil_code = ip;
4463                                 *sp++ = temp;
4464                         } else {
4465                                 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
4466                                 temp->flags |= MONO_INST_IS_TEMP;
4467                                 temp->cil_code = ip;
4468                                 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4469                                 store->cil_code = ip;
4470                                 if (store->opcode == CEE_STOBJ) {
4471                                         NEW_TEMPLOADA (cfg, store, temp->inst_c0);
4472                                         handle_stobj (cfg, bblock, store, sp [0], sp [0]->cil_code, store->klass, TRUE, FALSE, FALSE);
4473                                 } else {
4474                                         MONO_ADD_INS (bblock, store);
4475                                 }
4476                                 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
4477                                 *sp++ = ins;
4478                                 ins->cil_code = ip;
4479                                 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
4480                                 *sp++ = ins;
4481                                 ins->cil_code = ip;
4482                         }
4483                         ++ip;
4484                         inline_costs += 2;
4485                         break;
4486                 }
4487                 case CEE_POP:
4488                         CHECK_STACK (1);
4489                         MONO_INST_NEW (cfg, ins, CEE_POP);
4490                         MONO_ADD_INS (bblock, ins);
4491                         ins->cil_code = ip++;
4492                         --sp;
4493                         ins->inst_i0 = *sp;
4494                         break;
4495                 case CEE_JMP:
4496                         CHECK_OPSIZE (5);
4497                         if (stack_start != sp)
4498                                 UNVERIFIED;
4499                         MONO_INST_NEW (cfg, ins, OP_JMP);
4500                         token = read32 (ip + 1);
4501                         /* FIXME: check the signature matches */
4502                         cmethod = mini_get_method (method, token, NULL, generic_context);
4503
4504                         if (!cmethod)
4505                                 goto load_error;
4506
4507                         if (mono_use_security_manager) {
4508                                 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
4509                                         INLINE_FAILURE;
4510                         }
4511
4512                         ins->inst_p0 = cmethod;
4513                         MONO_ADD_INS (bblock, ins);
4514                         ip += 5;
4515                         start_new_bblock = 1;
4516                         break;
4517                 case CEE_CALLI:
4518                 case CEE_CALL:
4519                 case CEE_CALLVIRT: {
4520                         MonoInst *addr = NULL;
4521                         MonoMethodSignature *fsig = NULL;
4522                         int temp, array_rank = 0;
4523                         int virtual = *ip == CEE_CALLVIRT;
4524
4525                         CHECK_OPSIZE (5);
4526                         token = read32 (ip + 1);
4527
4528                         if (*ip == CEE_CALLI) {
4529                                 cmethod = NULL;
4530                                 CHECK_STACK (1);
4531                                 --sp;
4532                                 addr = *sp;
4533                                 if (method->wrapper_type != MONO_WRAPPER_NONE)
4534                                         fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
4535                                 else
4536                                         fsig = mono_metadata_parse_signature (image, token);
4537
4538                                 n = fsig->param_count + fsig->hasthis;
4539                         } else {
4540                                 MonoMethod *cil_method;
4541                                 
4542                                 if (method->wrapper_type != MONO_WRAPPER_NONE) {
4543                                         cmethod =  (MonoMethod *)mono_method_get_wrapper_data (method, token);
4544                                         cil_method = cmethod;
4545                                 } else if (constrained_call) {
4546                                         cmethod = mono_get_method_constrained (image, token, constrained_call, generic_context, &cil_method);
4547                                         cmethod = mono_get_inflated_method (cmethod);
4548                                         cil_method = cmethod;
4549                                 } else {
4550                                         cmethod = mini_get_method (method, token, NULL, generic_context);
4551                                         cil_method = cmethod;
4552                                 }
4553
4554                                 if (!cmethod)
4555                                         goto load_error;
4556                                 if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_method (method, cil_method))
4557                                         UNVERIFIED;
4558
4559                                 if (!virtual && (cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT))
4560                                         /* MS.NET seems to silently convert this to a callvirt */
4561                                         virtual = 1;
4562
4563                                 if (!cmethod->klass->inited){
4564                                         if (!mono_class_init (cmethod->klass))
4565                                                 goto load_error;
4566                                 }
4567
4568                                 if (mono_method_signature (cmethod)->pinvoke) {
4569                                         MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
4570                                         fsig = mono_method_signature (wrapper);
4571                                 } else if (constrained_call) {
4572                                         fsig = mono_method_signature (cmethod);
4573                                 } else {
4574                                         fsig = mono_method_get_signature_full (cmethod, image, token, generic_context);
4575                                 }
4576
4577                                 mono_save_token_info (cfg, image, token, cmethod);
4578
4579                                 n = fsig->param_count + fsig->hasthis;
4580
4581                                 if (mono_use_security_manager) {
4582                                         if (check_linkdemand (cfg, method, cmethod, bblock, ip))
4583                                                 INLINE_FAILURE;
4584                                 }
4585
4586                                 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
4587                                     mini_class_is_system_array (cmethod->klass)) {
4588                                         array_rank = cmethod->klass->rank;
4589                                 }
4590
4591                                 if (cmethod->string_ctor)
4592                                         g_assert_not_reached ();
4593
4594                         }
4595
4596                         if (cmethod && cmethod->klass->generic_container)
4597                                 UNVERIFIED;
4598
4599                         CHECK_STACK (n);
4600
4601                         //g_assert (!virtual || fsig->hasthis);
4602
4603                         sp -= n;
4604
4605                         if (constrained_call) {
4606                                 /*
4607                                  * We have the `constrained.' prefix opcode.
4608                                  */
4609                                 if (constrained_call->valuetype && !cmethod->klass->valuetype) {
4610                                         MonoInst *load;
4611                                         /*
4612                                          * The type parameter is instantiated as a valuetype,
4613                                          * but that type doesn't override the method we're
4614                                          * calling, so we need to box `this'.
4615                                          * sp [0] is a pointer to the data: we need the value
4616                                          * in handle_box (), so load it here.
4617                                          */
4618                                         MONO_INST_NEW (cfg, load, mono_type_to_ldind (&constrained_call->byval_arg));
4619                                         type_to_eval_stack_type (&constrained_call->byval_arg, load);
4620                                         load->cil_code = ip;
4621                                         load->inst_left = sp [0];
4622                                         sp [0] = handle_box (cfg, bblock, load, ip, constrained_call);
4623                                 } else if (!constrained_call->valuetype) {
4624                                         MonoInst *ins;
4625
4626                                         /*
4627                                          * The type parameter is instantiated as a reference
4628                                          * type.  We have a managed pointer on the stack, so
4629                                          * we need to dereference it here.
4630                                          */
4631
4632                                         MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
4633                                         ins->cil_code = ip;
4634                                         ins->inst_i0 = sp [0];
4635                                         ins->type = STACK_OBJ;
4636                                         ins->klass = mono_class_from_mono_type (&constrained_call->byval_arg);
4637                                         sp [0] = ins;
4638                                 } else if (cmethod->klass->valuetype)
4639                                         virtual = 0;
4640                                 constrained_call = NULL;
4641                         }
4642
4643                         if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
4644                                 UNVERIFIED;
4645
4646                         if (cmethod && virtual && 
4647                             (cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) && 
4648                             !((cmethod->flags & METHOD_ATTRIBUTE_FINAL) && 
4649                               cmethod->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) &&
4650                             mono_method_signature (cmethod)->generic_param_count) {
4651                                 MonoInst *this_temp, *this_arg_temp, *store;
4652                                 MonoInst *iargs [4];
4653
4654                                 g_assert (mono_method_signature (cmethod)->is_inflated);
4655                                 /* Prevent inlining of methods that contain indirect calls */
4656                                 INLINE_FAILURE;
4657
4658                                 this_temp = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4659                                 this_temp->cil_code = ip;
4660                                 NEW_TEMPSTORE (cfg, store, this_temp->inst_c0, sp [0]);
4661
4662                                 store->cil_code = ip;
4663                                 MONO_ADD_INS (bblock, store);
4664
4665                                 /* FIXME: This should be a managed pointer */
4666                                 this_arg_temp = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
4667                                 this_arg_temp->cil_code = ip;
4668
4669                                 /* Because of the PCONST below */
4670                                 cfg->disable_aot = TRUE;
4671                                 NEW_TEMPLOAD (cfg, iargs [0], this_temp->inst_c0);
4672                                 NEW_METHODCONST (cfg, iargs [1], cmethod);
4673                                 NEW_PCONST (cfg, iargs [2], mono_method_get_context (cmethod));
4674                                 NEW_TEMPLOADA (cfg, iargs [3], this_arg_temp->inst_c0);
4675                                 temp = mono_emit_jit_icall (cfg, bblock, mono_helper_compile_generic_method, iargs, ip);
4676
4677                                 NEW_TEMPLOAD (cfg, addr, temp);
4678                                 NEW_TEMPLOAD (cfg, sp [0], this_arg_temp->inst_c0);
4679
4680                                 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4681                                         NEW_TEMPLOAD (cfg, *sp, temp);
4682                                         sp++;
4683                                 }
4684
4685                                 ip += 5;
4686                                 ins_flag = 0;
4687                                 break;
4688                         }
4689
4690                         if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL) &&
4691                                  (mono_metadata_signature_equal (mono_method_signature (method), mono_method_signature (cmethod)))) {
4692                                 int i;
4693
4694                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
4695                                 INLINE_FAILURE;
4696                                 /* FIXME: This assumes the two methods has the same number and type of arguments */
4697                                 /*
4698                                  * We implement tail calls by storing the actual arguments into the 
4699                                  * argument variables, then emitting a OP_JMP. Since the actual arguments
4700                                  * can refer to the arg variables, we have to spill them.
4701                                  */
4702                                 handle_loaded_temps (cfg, bblock, sp, sp + n);
4703                                 for (i = 0; i < n; ++i) {
4704                                         /* Prevent argument from being register allocated */
4705                                         arg_array [i]->flags |= MONO_INST_VOLATILE;
4706
4707                                         /* Check if argument is the same */
4708                                         /* 
4709                                          * FIXME: This loses liveness info, so it can only be done if the
4710                                          * argument is not register allocated.
4711                                          */
4712                                         NEW_ARGLOAD (cfg, ins, i);
4713                                         if ((ins->opcode == sp [i]->opcode) && (ins->inst_i0 == sp [i]->inst_i0))
4714                                                 continue;
4715
4716                                         NEW_ARGSTORE (cfg, ins, i, sp [i]);
4717                                         ins->cil_code = ip;
4718                                         if (ins->opcode == CEE_STOBJ) {
4719                                                 NEW_ARGLOADA (cfg, ins, i);
4720                                                 handle_stobj (cfg, bblock, ins, sp [i], sp [i]->cil_code, ins->klass, FALSE, FALSE, FALSE);
4721                                         }
4722                                         else
4723                                                 MONO_ADD_INS (bblock, ins);
4724                                 }
4725                                 MONO_INST_NEW (cfg, ins, OP_JMP);
4726                                 ins->cil_code = ip;
4727                                 ins->inst_p0 = cmethod;
4728                                 ins->inst_p1 = arg_array [0];
4729                                 MONO_ADD_INS (bblock, ins);
4730                                 link_bblock (cfg, bblock, end_bblock);                  
4731                                 start_new_bblock = 1;
4732                                 /* skip CEE_RET as well */
4733                                 ip += 6;
4734                                 ins_flag = 0;
4735                                 break;
4736                         }
4737                         if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_inst_for_method (cfg, cmethod, fsig, sp))) {
4738                                 ins->cil_code = ip;
4739
4740                                 if (MONO_TYPE_IS_VOID (fsig->ret)) {
4741                                         MONO_ADD_INS (bblock, ins);
4742                                 } else {
4743                                         type_to_eval_stack_type (fsig->ret, ins);
4744                                         *sp = ins;
4745                                         sp++;
4746                                 }
4747
4748                                 ip += 5;
4749                                 ins_flag = 0;
4750                                 break;
4751                         }
4752
4753                         handle_loaded_temps (cfg, bblock, stack_start, sp);
4754
4755                         if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
4756                             (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) && 
4757                             mono_method_check_inlining (cfg, cmethod) &&
4758                                  !g_list_find (dont_inline, cmethod)) {
4759                                 int costs;
4760                                 MonoBasicBlock *ebblock;
4761                                 gboolean allways = FALSE;
4762
4763                                 if ((cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
4764                                         (cmethod->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
4765                                         /* Prevent inlining of methods that call wrappers */
4766                                         INLINE_FAILURE;
4767                                         cmethod = mono_marshal_get_native_wrapper (cmethod);
4768                                         allways = TRUE;
4769                                 }
4770
4771                                 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, allways))) {
4772                                         ip += 5;
4773                                         real_offset += 5;
4774
4775                                         GET_BBLOCK (cfg, bblock, ip);
4776                                         ebblock->next_bb = bblock;
4777                                         link_bblock (cfg, ebblock, bblock);
4778
4779                                         if (!MONO_TYPE_IS_VOID (fsig->ret))
4780                                                 sp++;
4781
4782                                         /* indicates start of a new block, and triggers a load of all 
4783                                            stack arguments at bb boundarie */
4784                                         bblock = ebblock;
4785
4786                                         inline_costs += costs;
4787                                         ins_flag = 0;
4788                                         break;
4789                                 }
4790                         }
4791                         
4792                         inline_costs += 10 * num_calls++;
4793
4794                         /* tail recursion elimination */
4795                         if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == method && ip [5] == CEE_RET) {
4796                                 gboolean has_vtargs = FALSE;
4797                                 int i;
4798                                 
4799                                 /* Prevent inlining of methods with tail calls (the call stack would be altered) */
4800                                 INLINE_FAILURE;
4801                                 /* keep it simple */
4802                                 for (i =  fsig->param_count - 1; i >= 0; i--) {
4803                                         if (MONO_TYPE_ISSTRUCT (mono_method_signature (cmethod)->params [i])) 
4804                                                 has_vtargs = TRUE;
4805                                 }
4806
4807                                 if (!has_vtargs) {
4808                                         for (i = 0; i < n; ++i) {
4809                                                 NEW_ARGSTORE (cfg, ins, i, sp [i]);
4810                                                 ins->cil_code = ip;
4811                                                 MONO_ADD_INS (bblock, ins);
4812                                         }
4813                                         MONO_INST_NEW (cfg, ins, OP_BR);
4814                                         ins->cil_code = ip;
4815                                         MONO_ADD_INS (bblock, ins);
4816                                         tblock = start_bblock->out_bb [0];
4817                                         link_bblock (cfg, bblock, tblock);
4818                                         ins->inst_target_bb = tblock;
4819                                         start_new_bblock = 1;
4820
4821                                         /* skip the CEE_RET, too */
4822                                         if (ip_in_bb (cfg, bblock, ip + 5))
4823                                                 ip += 6;
4824                                         else
4825                                                 ip += 5;
4826                                         ins_flag = 0;
4827                                         break;
4828                                 }
4829                         }
4830
4831                         if (*ip == CEE_CALLI) {
4832                                 /* Prevent inlining of methods with indirect calls */
4833                                 INLINE_FAILURE;
4834                                 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
4835                                         NEW_TEMPLOAD (cfg, *sp, temp);
4836                                         sp++;
4837                                 }                                       
4838                         } else if (array_rank) {
4839                                 MonoInst *addr;
4840
4841                                 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */ 
4842                                         if (sp [fsig->param_count]->type == STACK_OBJ) {
4843                                                 MonoInst *iargs [2];
4844                                                 MonoInst *array, *to_store, *store;
4845
4846                                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
4847                                                 
4848                                                 array = mono_compile_create_var (cfg, type_from_stack_type (sp [0]), OP_LOCAL);
4849                                                 NEW_TEMPSTORE (cfg, store, array->inst_c0, sp [0]);
4850                                                 store->cil_code = ip;
4851                                                 MONO_ADD_INS (bblock, store);
4852                                                 NEW_TEMPLOAD (cfg, iargs [0], array->inst_c0);
4853
4854                                                 to_store = mono_compile_create_var (cfg, type_from_stack_type (sp [fsig->param_count]), OP_LOCAL);
4855                                                 NEW_TEMPSTORE (cfg, store, to_store->inst_c0, sp [fsig->param_count]);
4856                                                 store->cil_code = ip;
4857                                                 MONO_ADD_INS (bblock, store);
4858                                                 NEW_TEMPLOAD (cfg, iargs [1], to_store->inst_c0);
4859
4860                                                 /*
4861                                                  * We first save the args for the call so that the args are copied to the stack
4862                                                  * and a new instruction tree for them is created. If we don't do this,
4863                                                  * the same MonoInst is added to two different trees and this is not 
4864                                                  * allowed by burg.
4865                                                  */
4866                                                 mono_emit_jit_icall (cfg, bblock, mono_helper_stelem_ref_check, iargs, ip);
4867
4868                                                 NEW_TEMPLOAD (cfg, sp [0], array->inst_c0);
4869                                                 NEW_TEMPLOAD (cfg, sp [fsig->param_count], to_store->inst_c0);
4870                                         }
4871
4872                                         addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
4873                                         NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
4874                                         ins->cil_code = ip;
4875                                         if (ins->opcode == CEE_STOBJ) {
4876                                                 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE, TRUE);
4877                                         } else {
4878                                                 MONO_ADD_INS (bblock, ins);
4879                                         }
4880
4881                                 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
4882                                         addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4883                                         NEW_INDLOAD (cfg, ins, addr, fsig->ret);
4884                                         ins->cil_code = ip;
4885
4886                                         *sp++ = ins;
4887                                 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
4888                                         addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
4889                                         *sp++ = addr;
4890                                 } else {
4891                                         g_assert_not_reached ();
4892                                 }
4893
4894                         } else {
4895                                 /* Prevent inlining of methods which call other methods */
4896                                 INLINE_FAILURE;
4897                                 if (ip_in_bb (cfg, bblock, ip + 5) 
4898                                     && (!MONO_TYPE_ISSTRUCT (fsig->ret))
4899                                     && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)
4900                                     && (CODE_IS_STLOC (ip + 5) || ip [5] == CEE_POP || ip [5] == CEE_RET)) {
4901                                         /* no need to spill */
4902                                         ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
4903                                         *sp++ = ins;
4904                                 } else {
4905                                         if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
4906                                                 NEW_TEMPLOAD (cfg, *sp, temp);
4907                                                 sp++;
4908                                         }
4909                                 }
4910                         }
4911
4912                         ip += 5;
4913                         ins_flag = 0;
4914                         break;
4915                 }
4916                 case CEE_RET:
4917                         if (cfg->method != method) {
4918                                 /* return from inlined methode */
4919                                 if (return_var) {
4920                                         MonoInst *store;
4921                                         CHECK_STACK (1);
4922                                         --sp;
4923                                         //g_assert (returnvar != -1);
4924                                         NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
4925                                         store->cil_code = sp [0]->cil_code;
4926                                         if (store->opcode == CEE_STOBJ) {
4927                                                 g_assert_not_reached ();
4928                                                 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
4929                                                 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
4930                                                 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE, FALSE);
4931                                         } else
4932                                                 MONO_ADD_INS (bblock, store);
4933                                 } 
4934                         } else {
4935                                 if (cfg->ret) {
4936                                         g_assert (!return_var);
4937                                         CHECK_STACK (1);
4938                                         --sp;
4939                                         MONO_INST_NEW (cfg, ins, OP_NOP);
4940                                         ins->opcode = mono_type_to_stind (mono_method_signature (method)->ret);
4941                                         if (ins->opcode == CEE_STOBJ) {
4942                                                 NEW_RETLOADA (cfg, ins);
4943                                                 /* FIXME: it is possible some optimization will pass the a heap pointer for the struct address, so we'll need the write barrier */
4944                                                 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
4945                                         } else {
4946                                                 ins->opcode = OP_SETRET;
4947                                                 ins->cil_code = ip;
4948                                                 ins->inst_i0 = *sp;;
4949                                                 ins->inst_i1 = NULL;
4950                                                 MONO_ADD_INS (bblock, ins);
4951                                         }
4952                                 }
4953                         }
4954                         if (sp != stack_start)
4955                                 UNVERIFIED;
4956                         MONO_INST_NEW (cfg, ins, OP_BR);
4957                         ins->cil_code = ip++;
4958                         ins->inst_target_bb = end_bblock;
4959                         MONO_ADD_INS (bblock, ins);
4960                         link_bblock (cfg, bblock, end_bblock);
4961                         start_new_bblock = 1;
4962                         break;
4963                 case CEE_BR_S:
4964                         CHECK_OPSIZE (2);
4965                         MONO_INST_NEW (cfg, ins, OP_BR);
4966                         ins->cil_code = ip++;
4967                         MONO_ADD_INS (bblock, ins);
4968                         target = ip + 1 + (signed char)(*ip);
4969                         ++ip;
4970                         GET_BBLOCK (cfg, tblock, target);
4971                         link_bblock (cfg, bblock, tblock);
4972                         CHECK_BBLOCK (target, ip, tblock);
4973                         ins->inst_target_bb = tblock;
4974                         if (sp != stack_start) {
4975                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4976                                 sp = stack_start;
4977                                 CHECK_UNVERIFIABLE (cfg);
4978                         }
4979                         start_new_bblock = 1;
4980                         inline_costs += BRANCH_COST;
4981                         break;
4982                 case CEE_BRFALSE_S:
4983                 case CEE_BRTRUE_S:
4984                         CHECK_OPSIZE (2);
4985                         CHECK_STACK (1);
4986                         if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
4987                                 UNVERIFIED;
4988                         MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
4989                         ins->cil_code = ip++;
4990                         target = ip + 1 + *(signed char*)ip;
4991                         ip++;
4992                         ADD_UNCOND (ins->opcode == CEE_BRTRUE);
4993                         if (sp != stack_start) {
4994                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
4995                                 sp = stack_start;
4996                                 CHECK_UNVERIFIABLE (cfg);
4997                         }
4998                         inline_costs += BRANCH_COST;
4999                         break;
5000                 case CEE_BEQ_S:
5001                 case CEE_BGE_S:
5002                 case CEE_BGT_S:
5003                 case CEE_BLE_S:
5004                 case CEE_BLT_S:
5005                 case CEE_BNE_UN_S:
5006                 case CEE_BGE_UN_S:
5007                 case CEE_BGT_UN_S:
5008                 case CEE_BLE_UN_S:
5009                 case CEE_BLT_UN_S:
5010                         CHECK_OPSIZE (2);
5011                         CHECK_STACK (2);
5012                         MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
5013                         ins->cil_code = ip++;
5014                         target = ip + 1 + *(signed char*)ip;
5015                         ip++;
5016 #ifdef MONO_ARCH_SOFT_FLOAT
5017                         if (sp [-1]->type == STACK_R8 || sp [-2]->type == STACK_R8) {
5018                                 ins->opcode = condbr_to_fp_br (ins->opcode);
5019                                 sp -= 2;
5020                                 ins->inst_left = sp [0];
5021                                 ins->inst_right = sp [1];
5022                                 ins->type = STACK_I4;
5023                                 *sp++ = emit_tree (cfg, bblock, ins, ins->cil_code);
5024                                 MONO_INST_NEW (cfg, ins, CEE_BRTRUE);
5025                                 ADD_UNCOND (TRUE);
5026                         } else {
5027                                 ADD_BINCOND (NULL);
5028                         }
5029 #else
5030                         ADD_BINCOND (NULL);
5031 #endif
5032                         if (sp != stack_start) {
5033                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
5034                                 sp = stack_start;
5035                                 CHECK_UNVERIFIABLE (cfg);
5036                         }
5037                         inline_costs += BRANCH_COST;
5038                         break;
5039                 case CEE_BR:
5040                         CHECK_OPSIZE (5);
5041                         MONO_INST_NEW (cfg, ins, OP_BR);
5042                         ins->cil_code = ip++;
5043                         MONO_ADD_INS (bblock, ins);
5044                         target = ip + 4 + (gint32)read32(ip);
5045                         ip += 4;
5046                         GET_BBLOCK (cfg, tblock, target);
5047                         link_bblock (cfg, bblock, tblock);
5048                         CHECK_BBLOCK (target, ip, tblock);
5049                         ins->inst_target_bb = tblock;
5050                         if (sp != stack_start) {
5051                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
5052                                 sp = stack_start;
5053                                 CHECK_UNVERIFIABLE (cfg);
5054                         }
5055                         start_new_bblock = 1;
5056                         inline_costs += BRANCH_COST;
5057                         break;
5058                 case CEE_BRFALSE:
5059                 case CEE_BRTRUE:
5060                         CHECK_OPSIZE (5);
5061                         CHECK_STACK (1);
5062                         if (sp [-1]->type == STACK_VTYPE || sp [-1]->type == STACK_R8)
5063                                 UNVERIFIED;
5064                         MONO_INST_NEW (cfg, ins, *ip);
5065                         ins->cil_code = ip++;
5066                         target = ip + 4 + (gint32)read32(ip);
5067                         ip += 4;
5068                         ADD_UNCOND(ins->opcode == CEE_BRTRUE);
5069                         if (sp != stack_start) {
5070                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
5071                                 sp = stack_start;
5072                                 CHECK_UNVERIFIABLE (cfg);
5073                         }
5074                         inline_costs += BRANCH_COST;
5075                         break;
5076                 case CEE_BEQ:
5077                 case CEE_BGE:
5078                 case CEE_BGT:
5079                 case CEE_BLE:
5080                 case CEE_BLT:
5081                 case CEE_BNE_UN:
5082                 case CEE_BGE_UN:
5083                 case CEE_BGT_UN:
5084                 case CEE_BLE_UN:
5085                 case CEE_BLT_UN:
5086                         CHECK_OPSIZE (5);
5087                         CHECK_STACK (2);
5088                         MONO_INST_NEW (cfg, ins, *ip);
5089                         ins->cil_code = ip++;
5090                         target = ip + 4 + (gint32)read32(ip);
5091                         ip += 4;
5092 #ifdef MONO_ARCH_SOFT_FLOAT
5093                         if (sp [-1]->type == STACK_R8 || sp [-2]->type == STACK_R8) {
5094                                 ins->opcode = condbr_to_fp_br (ins->opcode);
5095                                 sp -= 2;
5096                                 ins->inst_left = sp [0];
5097                                 ins->inst_right = sp [1];
5098                                 ins->type = STACK_I4;
5099                                 *sp++ = emit_tree (cfg, bblock, ins, ins->cil_code);
5100                                 MONO_INST_NEW (cfg, ins, CEE_BRTRUE);
5101                                 ADD_UNCOND (TRUE);
5102                         } else {
5103                                 ADD_BINCOND (NULL);
5104                         }
5105 #else
5106                         ADD_BINCOND (NULL);
5107 #endif
5108                         if (sp != stack_start) {
5109                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
5110                                 sp = stack_start;
5111                                 CHECK_UNVERIFIABLE (cfg);
5112                         }
5113                         inline_costs += BRANCH_COST;
5114                         break;
5115                 case CEE_SWITCH:
5116                         CHECK_OPSIZE (5);
5117                         CHECK_STACK (1);
5118                         n = read32 (ip + 1);
5119                         MONO_INST_NEW (cfg, ins, *ip);
5120                         --sp;
5121                         ins->inst_left = *sp;
5122                         if ((ins->inst_left->type != STACK_I4) && (ins->inst_left->type != STACK_PTR)) 
5123                                 UNVERIFIED;
5124                         ins->cil_code = ip;
5125                         ip += 5;
5126                         CHECK_OPSIZE (n * sizeof (guint32));
5127                         target = ip + n * sizeof (guint32);
5128                         MONO_ADD_INS (bblock, ins);
5129                         GET_BBLOCK (cfg, tblock, target);
5130                         link_bblock (cfg, bblock, tblock);
5131                         ins->klass = GUINT_TO_POINTER (n);
5132                         ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
5133                         ins->inst_many_bb [n] = tblock;
5134
5135                         for (i = 0; i < n; ++i) {
5136                                 GET_BBLOCK (cfg, tblock, target + (gint32)read32(ip));
5137                                 link_bblock (cfg, bblock, tblock);
5138                                 ins->inst_many_bb [i] = tblock;
5139                                 ip += 4;
5140                         }
5141                         if (sp != stack_start) {
5142                                 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
5143                                 sp = stack_start;
5144                                 CHECK_UNVERIFIABLE (cfg);
5145                         }
5146                         /* Needed by the code generated in inssel.brg */
5147                         mono_get_got_var (cfg);
5148                         inline_costs += (BRANCH_COST * 2);
5149                         break;
5150                 case CEE_LDIND_I1:
5151                 case CEE_LDIND_U1:
5152                 case CEE_LDIND_I2:
5153                 case CEE_LDIND_U2:
5154                 case CEE_LDIND_I4:
5155                 case CEE_LDIND_U4:
5156                 case CEE_LDIND_I8:
5157                 case CEE_LDIND_I:
5158                 case CEE_LDIND_R4:
5159                 case CEE_LDIND_R8:
5160                 case CEE_LDIND_REF:
5161                         CHECK_STACK (1);
5162                         MONO_INST_NEW (cfg, ins, *ip);
5163                         ins->cil_code = ip;
5164                         --sp;
5165                         ins->inst_i0 = *sp;
5166                         *sp++ = ins;
5167                         ins->type = ldind_type [*ip - CEE_LDIND_I1];
5168                         ins->flags |= ins_flag;
5169                         ins_flag = 0;
5170                         if (ins->type == STACK_OBJ)
5171                                 ins->klass = mono_defaults.object_class;
5172 #ifdef MONO_ARCH_SOFT_FLOAT
5173                         if (*ip == CEE_LDIND_R4) {
5174                                 int temp;
5175                                 --sp;
5176                                 temp = handle_load_float (cfg, bblock, ins->inst_i0, ip);
5177                                 NEW_TEMPLOAD (cfg, *sp, temp);
5178                                 sp++;
5179                         }
5180 #endif
5181                         ++ip;
5182                         break;
5183                 case CEE_STIND_REF:
5184                 case CEE_STIND_I1:
5185                 case CEE_STIND_I2:
5186                 case CEE_STIND_I4:
5187                 case CEE_STIND_I8:
5188                 case CEE_STIND_R4:
5189                 case CEE_STIND_R8:
5190                         CHECK_STACK (2);
5191 #ifdef MONO_ARCH_SOFT_FLOAT
5192                         if (*ip == CEE_STIND_R4) {
5193                                 sp -= 2;
5194                                 handle_store_float (cfg, bblock, sp [0], sp [1], ip);
5195                                 ip++;
5196                                 break;
5197                         }
5198 #endif
5199 #if HAVE_WRITE_BARRIERS
5200                         if (*ip == CEE_STIND_REF && method->wrapper_type != MONO_WRAPPER_WRITE_BARRIER) {
5201                                 /* insert call to write barrier */
5202                                 MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
5203                                 sp -= 2;
5204                                 mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), sp, ip, NULL);
5205                                 ip++;
5206                                 break;
5207                         }
5208 #endif
5209                         MONO_INST_NEW (cfg, ins, *ip);
5210                         ins->cil_code = ip++;
5211                         sp -= 2;
5212                         handle_loaded_temps (cfg, bblock, stack_start, sp);
5213                         MONO_ADD_INS (bblock, ins);
5214                         ins->inst_i0 = sp [0];
5215                         ins->inst_i1 = sp [1];
5216                         ins->flags |= ins_flag;
5217                         ins_flag = 0;
5218                         inline_costs += 1;
5219                         break;
5220                 case CEE_MUL:
5221                         CHECK_STACK (2);
5222                         ADD_BINOP (*ip);
5223
5224 #ifdef MONO_ARCH_NO_EMULATE_MUL_IMM
5225                         /* FIXME: This breaks with ssapre (mono -O=ssapre loader.exe) */
5226                         if ((ins->inst_right->opcode == OP_ICONST) && !(cfg->opt & MONO_OPT_SSAPRE)) {
5227                                 switch (ins->opcode) {
5228                                 case CEE_MUL:
5229                                         ins->opcode = OP_IMUL_IMM;
5230                                         ins->inst_imm = ins->inst_right->inst_c0;
5231                                         break;
5232                                 case OP_LMUL:
5233                                         ins->opcode = OP_LMUL_IMM;
5234                                         ins->inst_imm = ins->inst_right->inst_c0;
5235                                         break;
5236                                 default:
5237                                         g_assert_not_reached ();
5238                                 }
5239                         }
5240 #endif
5241
5242                         if (mono_find_jit_opcode_emulation (ins->opcode)) {
5243                                 --sp;
5244                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5245                         }
5246                         ip++;
5247                         break;
5248                 case CEE_ADD:
5249                 case CEE_SUB:
5250                 case CEE_DIV:
5251                 case CEE_DIV_UN:
5252                 case CEE_REM:
5253                 case CEE_REM_UN:
5254                 case CEE_AND:
5255                 case CEE_OR:
5256                 case CEE_XOR:
5257                 case CEE_SHL:
5258                 case CEE_SHR:
5259                 case CEE_SHR_UN:
5260                         CHECK_STACK (2);
5261                         ADD_BINOP (*ip);
5262                         /* special case that gives a nice speedup and happens to workaorund a ppc jit but (for the release)
5263                          * later apply the speedup to the left shift as well
5264                          * See BUG# 57957.
5265                          */
5266                         if ((ins->opcode == OP_LSHR_UN) && (ins->type == STACK_I8) 
5267                                         && (ins->inst_right->opcode == OP_ICONST) && (ins->inst_right->inst_c0 == 32)) {
5268                                 ins->opcode = OP_LONG_SHRUN_32;
5269                                 /*g_print ("applied long shr speedup to %s\n", cfg->method->name);*/
5270                                 ip++;
5271                                 break;
5272                         }
5273                         if (mono_find_jit_opcode_emulation (ins->opcode)) {
5274                                 --sp;
5275                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5276                         }
5277                         ip++;
5278                         break;
5279                 case CEE_NEG:
5280                 case CEE_NOT:
5281                 case CEE_CONV_I1:
5282                 case CEE_CONV_I2:
5283                 case CEE_CONV_I4:
5284                 case CEE_CONV_R4:
5285                 case CEE_CONV_R8:
5286                 case CEE_CONV_U4:
5287                 case CEE_CONV_I8:
5288                 case CEE_CONV_U8:
5289                 case CEE_CONV_OVF_I8:
5290                 case CEE_CONV_OVF_U8:
5291                 case CEE_CONV_R_UN:
5292                         CHECK_STACK (1);
5293                         ADD_UNOP (*ip);
5294                         if (mono_find_jit_opcode_emulation (ins->opcode)) {
5295                                 --sp;
5296                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
5297                         }
5298                         ip++;                   
5299                         break;
5300                 case CEE_CONV_OVF_I4:
5301                 case CEE_CONV_OVF_I1:
5302                 case CEE_CONV_OVF_I2:
5303                 case CEE_CONV_OVF_I:
5304                 case CEE_CONV_OVF_U:
5305                         CHECK_STACK (1);
5306
5307                         if (sp [-1]->type == STACK_R8) {
5308                                 ADD_UNOP (CEE_CONV_OVF_I8);
5309                                 ADD_UNOP (*ip);
5310                         } else {
5311                                 ADD_UNOP (*ip);
5312                         }
5313
5314                         ip++;
5315                         break;
5316                 case CEE_CONV_OVF_U1:
5317                 case CEE_CONV_OVF_U2:
5318                 case CEE_CONV_OVF_U4:
5319                         CHECK_STACK (1);
5320
5321                         if (sp [-1]->type == STACK_R8) {
5322                                 ADD_UNOP (CEE_CONV_OVF_U8);
5323                                 ADD_UNOP (*ip);
5324                         } else {
5325                                 ADD_UNOP (*ip);
5326                         }
5327
5328                         ip++;
5329                         break;
5330                 case CEE_CONV_OVF_I1_UN:
5331                 case CEE_CONV_OVF_I2_UN:
5332                 case CEE_CONV_OVF_I4_UN:
5333                 case CEE_CONV_OVF_I8_UN:
5334                 case CEE_CONV_OVF_U1_UN:
5335                 case CEE_CONV_OVF_U2_UN:
5336                 case CEE_CONV_OVF_U4_UN:
5337                 case CEE_CONV_OVF_U8_UN:
5338                 case CEE_CONV_OVF_I_UN:
5339                 case CEE_CONV_OVF_U_UN:
5340                         CHECK_STACK (1);
5341                         ADD_UNOP (*ip);
5342                         ip++;
5343                         break;
5344                 case CEE_CPOBJ:
5345                         CHECK_OPSIZE (5);
5346                         CHECK_STACK (2);
5347                         token = read32 (ip + 1);
5348                         klass = mini_get_class (method, token, generic_context);
5349                         CHECK_TYPELOAD (klass);
5350                         sp -= 2;
5351                         if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5352                                 MonoInst *store, *load;
5353                                 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
5354                                 load->cil_code = ip;
5355                                 load->inst_i0 = sp [1];
5356                                 load->type = STACK_OBJ;
5357                                 load->klass = klass;
5358                                 load->flags |= ins_flag;
5359                                 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5360                                 store->cil_code = ip;
5361                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
5362                                 MONO_ADD_INS (bblock, store);
5363                                 store->inst_i0 = sp [0];
5364                                 store->inst_i1 = load;
5365                                 store->flags |= ins_flag;
5366                         } else {
5367                                 n = mono_class_value_size (klass, NULL);
5368                                 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
5369                                         MonoInst *copy;
5370                                         MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5371                                         copy->inst_left = sp [0];
5372                                         copy->inst_right = sp [1];
5373                                         copy->cil_code = ip;
5374                                         copy->backend.size = n;
5375                                         MONO_ADD_INS (bblock, copy);
5376                                 } else {
5377                                         MonoMethod *memcpy_method = get_memcpy_method ();
5378                                         MonoInst *iargs [3];
5379                                         iargs [0] = sp [0];
5380                                         iargs [1] = sp [1];
5381                                         NEW_ICONST (cfg, iargs [2], n);
5382                                         iargs [2]->cil_code = ip;
5383
5384                                         mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
5385                                 }
5386                         }
5387                         ins_flag = 0;
5388                         ip += 5;
5389                         break;
5390                 case CEE_LDOBJ: {
5391                         MonoInst *iargs [3];
5392                         int loc_index = -1;
5393                         int stloc_len = 0;
5394                         CHECK_OPSIZE (5);
5395                         CHECK_STACK (1);
5396                         --sp;
5397                         token = read32 (ip + 1);
5398                         klass = mini_get_class (method, token, generic_context);
5399                         CHECK_TYPELOAD (klass);
5400                         if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5401                                 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
5402                                 ins->cil_code = ip;
5403                                 ins->inst_i0 = sp [0];
5404                                 ins->type = STACK_OBJ;
5405                                 ins->klass = klass;
5406                                 ins->flags |= ins_flag;
5407                                 ins_flag = 0;
5408                                 *sp++ = ins;
5409                                 ip += 5;
5410                                 break;
5411                         }
5412
5413                         /* Optimize the common ldobj+stloc combination */
5414                         switch (ip [5]) {
5415                         case CEE_STLOC_S:
5416                                 loc_index = ip [6];
5417                                 stloc_len = 2;
5418                                 break;
5419                         case CEE_STLOC_0:
5420                         case CEE_STLOC_1:
5421                         case CEE_STLOC_2:
5422                         case CEE_STLOC_3:
5423                                 loc_index = ip [5] - CEE_STLOC_0;
5424                                 stloc_len = 1;
5425                                 break;
5426                         default:
5427                                 break;
5428                         }
5429
5430                         if ((loc_index != -1) && ip_in_bb (cfg, bblock, ip + 5)) {
5431                                 CHECK_LOCAL (loc_index);
5432                                 NEW_LOCSTORE (cfg, ins, loc_index, *sp);
5433
5434                                 if (ins->opcode == CEE_STOBJ) {
5435                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
5436                                         ins->cil_code = ip;
5437                                         g_assert (ins->opcode == CEE_STOBJ);
5438                                         NEW_LOCLOADA (cfg, ins, loc_index);
5439                                         handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
5440                                         ip += 5;
5441                                         ip += stloc_len;
5442                                         break;
5443                                 }
5444                         }
5445
5446                         n = mono_class_value_size (klass, NULL);
5447                         ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
5448                         NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
5449                         if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
5450                                 MonoInst *copy;
5451                                 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5452                                 copy->inst_left = iargs [0];
5453                                 copy->inst_right = *sp;
5454                                 copy->cil_code = ip;
5455                                 copy->backend.size = n;
5456                                 MONO_ADD_INS (bblock, copy);
5457                         } else {
5458                                 MonoMethod *memcpy_method = get_memcpy_method ();
5459                                 iargs [1] = *sp;
5460                                 NEW_ICONST (cfg, iargs [2], n);
5461                                 iargs [2]->cil_code = ip;
5462
5463                                 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
5464                         }
5465                         NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
5466                         ++sp;
5467                         ip += 5;
5468                         ins_flag = 0;
5469                         inline_costs += 1;
5470                         break;
5471                 }
5472                 case CEE_LDSTR:
5473                         CHECK_STACK_OVF (1);
5474                         CHECK_OPSIZE (5);
5475                         n = read32 (ip + 1);
5476
5477                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
5478                                 /* FIXME: moving GC */
5479                                 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, n));
5480                                 ins->cil_code = ip;
5481                                 ins->type = STACK_OBJ;
5482                                 ins->klass = mono_defaults.string_class;
5483                                 *sp = ins;
5484                         }
5485                         else if (method->wrapper_type != MONO_WRAPPER_NONE) {
5486                                 int temp;
5487                                 MonoInst *iargs [1];
5488
5489                                 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));                          
5490                                 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
5491                                 NEW_TEMPLOAD (cfg, *sp, temp);
5492
5493                         } else {
5494
5495                                 if (cfg->opt & MONO_OPT_SHARED) {
5496                                         int temp;
5497                                         MonoInst *iargs [3];
5498                                         MonoInst* domain_var;
5499                                         
5500                                         if (cfg->compile_aot) {
5501                                                 /* FIXME: bug when inlining methods from different assemblies (n is a token valid just in one). */
5502                                                 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, GINT_TO_POINTER (n));
5503                                         }
5504                                         /* avoid depending on undefined C behavior in sequence points */
5505                                         domain_var = mono_get_domainvar (cfg);
5506                                         NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
5507                                         NEW_IMAGECONST (cfg, iargs [1], image);
5508                                         NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
5509                                         temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
5510                                         NEW_TEMPLOAD (cfg, *sp, temp);
5511                                         mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
5512                                 } else {
5513                                         if (bblock->out_of_line) {
5514                                                 MonoInst *iargs [2];
5515                                                 int temp;
5516
5517                                                 if (cfg->compile_aot && cfg->method->klass->image == mono_defaults.corlib) {
5518                                                         /* 
5519                                                          * Avoid relocations by using a version of helper_ldstr
5520                                                          * specialized to mscorlib.
5521                                                          */
5522                                                         NEW_ICONST (cfg, iargs [0], mono_metadata_token_index (n));
5523                                                         temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr_mscorlib, iargs, ip);
5524                                                 } else {
5525                                                         /* Avoid creating the string object */
5526                                                         NEW_IMAGECONST (cfg, iargs [0], image);
5527                                                         NEW_ICONST (cfg, iargs [1], mono_metadata_token_index (n));
5528                                                         temp = mono_emit_jit_icall (cfg, bblock, mono_helper_ldstr, iargs, ip);
5529                                                 }
5530                                                 NEW_TEMPLOAD (cfg, *sp, temp);
5531                                         } 
5532                                         else
5533                                         if (cfg->compile_aot) {
5534                                                 NEW_LDSTRCONST (cfg, ins, image, n);
5535                                                 *sp = ins;
5536                                         } 
5537                                         else {
5538                                                 NEW_PCONST (cfg, ins, NULL);
5539                                                 ins->cil_code = ip;
5540                                                 ins->type = STACK_OBJ;
5541                                                 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
5542                                                 ins->klass = mono_defaults.string_class;
5543                                                 *sp = ins;
5544                                         }
5545                                 }
5546                         }
5547
5548                         sp++;
5549                         ip += 5;
5550                         break;
5551                 case CEE_NEWOBJ: {
5552                         MonoInst *iargs [2];
5553                         MonoMethodSignature *fsig;
5554                         int temp;
5555                         
5556                         CHECK_OPSIZE (5);
5557                         token = read32 (ip + 1);
5558                         cmethod = mini_get_method (method, token, NULL, generic_context);
5559                         if (!cmethod)
5560                                 goto load_error;
5561                         fsig = mono_method_get_signature (cmethod, image, token);
5562
5563                         mono_save_token_info (cfg, image, token, cmethod);
5564
5565                         if (!mono_class_init (cmethod->klass))
5566                                 goto load_error;
5567
5568                         if (mono_use_security_manager) {
5569                                 if (check_linkdemand (cfg, method, cmethod, bblock, ip))
5570                                         INLINE_FAILURE;
5571                         }
5572
5573                         n = fsig->param_count;
5574                         CHECK_STACK (n);
5575
5576                         /* move the args to allow room for 'this' in the first position */
5577                         while (n--) {
5578                                 --sp;
5579                                 sp [1] = sp [0];
5580                         }
5581
5582                         handle_loaded_temps (cfg, bblock, stack_start, sp);
5583
5584                         if (mini_class_is_system_array (cmethod->klass)) {
5585                                 NEW_METHODCONST (cfg, *sp, cmethod);
5586                                 temp = handle_array_new (cfg, bblock, fsig->param_count, sp, ip);
5587                         } else if (cmethod->string_ctor) {
5588                                 /* we simply pass a null pointer */
5589                                 NEW_PCONST (cfg, *sp, NULL); 
5590                                 /* now call the string ctor */
5591                                 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
5592                         } else {
5593                                 MonoInst* callvirt_this_arg = NULL;
5594                                 
5595                                 if (cmethod->klass->valuetype) {
5596                                         iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
5597                                         temp = iargs [0]->inst_c0;
5598
5599                                         NEW_TEMPLOADA (cfg, *sp, temp);
5600
5601                                         handle_initobj (cfg, bblock, *sp, NULL, cmethod->klass, stack_start, sp);
5602
5603                                         NEW_TEMPLOADA (cfg, *sp, temp);
5604
5605                                         /* 
5606                                          * The code generated by mini_emit_virtual_call () expects
5607                                          * iargs [0] to be a boxed instance, but luckily the vcall
5608                                          * will be transformed into a normal call there.
5609                                          */
5610                                 } else {
5611                                         temp = handle_alloc (cfg, bblock, cmethod->klass, FALSE, ip);
5612                                         NEW_TEMPLOAD (cfg, *sp, temp);
5613                                 }
5614
5615                                 /* Avoid virtual calls to ctors if possible */
5616                                 if (cmethod->klass->marshalbyref)
5617                                         callvirt_this_arg = sp [0];
5618                                 
5619                                 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
5620                                     mono_method_check_inlining (cfg, cmethod) &&
5621                                     !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
5622                                     !g_list_find (dont_inline, cmethod)) {
5623                                         int costs;
5624                                         MonoBasicBlock *ebblock;
5625                                         if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock, FALSE))) {
5626
5627                                                 ip += 5;
5628                                                 real_offset += 5;
5629                                                 
5630                                                 GET_BBLOCK (cfg, bblock, ip);
5631                                                 ebblock->next_bb = bblock;
5632                                                 link_bblock (cfg, ebblock, bblock);
5633
5634                                                 NEW_TEMPLOAD (cfg, *sp, temp);
5635                                                 sp++;
5636
5637                                                 /* indicates start of a new block, and triggers a load 
5638                                                    of all stack arguments at bb boundarie */
5639                                                 bblock = ebblock;
5640
5641                                                 inline_costs += costs;
5642                                                 break;
5643                                                 
5644                                         } else {
5645                                                 /* Prevent inlining of methods which call other methods */
5646                                                 INLINE_FAILURE;
5647                                                 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
5648                                         }
5649                                 } else {
5650                                         /* Prevent inlining of methods which call other methods */
5651                                         INLINE_FAILURE;
5652                                         /* now call the actual ctor */
5653                                         mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, callvirt_this_arg);
5654                                 }
5655                         }
5656
5657                         NEW_TEMPLOAD (cfg, *sp, temp);
5658                         sp++;
5659                         
5660                         ip += 5;
5661                         inline_costs += 5;
5662                         break;
5663                 }
5664                 case CEE_ISINST:
5665                         CHECK_STACK (1);
5666                         --sp;
5667                         CHECK_OPSIZE (5);
5668                         token = read32 (ip + 1);
5669                         klass = mini_get_class (method, token, generic_context);
5670                         CHECK_TYPELOAD (klass);
5671                         if (sp [0]->type != STACK_OBJ)
5672                                 UNVERIFIED;
5673
5674                         /* Needed by the code generated in inssel.brg */
5675                         mono_get_got_var (cfg);
5676
5677                         if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5678                         
5679                                 MonoMethod *mono_isinst;
5680                                 MonoInst *iargs [1];
5681                                 MonoBasicBlock *ebblock;
5682                                 int costs;
5683                                 int temp;
5684                                 
5685                                 mono_isinst = mono_marshal_get_isinst (klass); 
5686                                 iargs [0] = sp [0];
5687                                 
5688                                 costs = inline_method (cfg, mono_isinst, mono_method_signature (mono_isinst), bblock, 
5689                                                            iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5690                         
5691                                 g_assert (costs > 0);
5692                                 
5693                                 ip += 5;
5694                                 real_offset += 5;
5695                         
5696                                 GET_BBLOCK (cfg, bblock, ip);
5697                                 ebblock->next_bb = bblock;
5698                                 link_bblock (cfg, ebblock, bblock);
5699
5700                                 temp = iargs [0]->inst_i0->inst_c0;
5701                                 NEW_TEMPLOAD (cfg, *sp, temp);
5702                                 
5703                                 sp++;
5704                                 bblock = ebblock;
5705                                 inline_costs += costs;
5706                         } else {
5707                                 MONO_INST_NEW (cfg, ins, *ip);
5708                                 ins->type = STACK_OBJ;
5709                                 ins->inst_left = *sp;
5710                                 ins->inst_newa_class = klass;
5711                                 ins->klass = klass;
5712                                 ins->cil_code = ip;
5713                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5714                                 ip += 5;
5715                         }
5716                         break;
5717                 case CEE_UNBOX_ANY: {
5718                         MonoInst *add, *vtoffset;
5719                         MonoInst *iargs [3];
5720
5721                         CHECK_STACK (1);
5722                         --sp;
5723                         CHECK_OPSIZE (5);
5724                         token = read32 (ip + 1);
5725                         klass = mini_get_class (method, token, generic_context);
5726                         CHECK_TYPELOAD (klass);
5727
5728                         if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5729                                 /* CASTCLASS */
5730                                 if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5731                                         MonoMethod *mono_castclass;
5732                                         MonoInst *iargs [1];
5733                                         MonoBasicBlock *ebblock;
5734                                         int costs;
5735                                         int temp;
5736                                         
5737                                         mono_castclass = mono_marshal_get_castclass (klass); 
5738                                         iargs [0] = sp [0];
5739                                         
5740                                         costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock, 
5741                                                                    iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5742                                 
5743                                         g_assert (costs > 0);
5744                                         
5745                                         ip += 5;
5746                                         real_offset += 5;
5747                                 
5748                                         GET_BBLOCK (cfg, bblock, ip);
5749                                         ebblock->next_bb = bblock;
5750                                         link_bblock (cfg, ebblock, bblock);
5751         
5752                                         temp = iargs [0]->inst_i0->inst_c0;
5753                                         NEW_TEMPLOAD (cfg, *sp, temp);
5754                                         
5755                                         sp++;
5756                                         bblock = ebblock;
5757                                         inline_costs += costs;                          
5758                                 } else {
5759                                         MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
5760                                         ins->type = STACK_OBJ;
5761                                         ins->inst_left = *sp;
5762                                         ins->klass = klass;
5763                                         ins->inst_newa_class = klass;
5764                                         ins->cil_code = ip;
5765                                         *sp++ = ins;
5766                                         ip += 5;
5767                                 }
5768                                 break;
5769                         }
5770
5771                         if (mono_class_is_nullable (klass)) {
5772                                 int v = handle_unbox_nullable (cfg, bblock, *sp, ip, klass);
5773                                 NEW_TEMPLOAD (cfg, *sp, v);
5774                                 sp ++;
5775                                 ip += 5;
5776                                 break;
5777                         }
5778
5779                         MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5780                         ins->type = STACK_OBJ;
5781                         ins->inst_left = *sp;
5782                         ins->klass = klass;
5783                         ins->inst_newa_class = klass;
5784                         ins->cil_code = ip;
5785
5786                         MONO_INST_NEW (cfg, add, OP_PADD);
5787                         NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5788                         add->inst_left = ins;
5789                         add->inst_right = vtoffset;
5790                         add->type = STACK_MP;
5791                         add->klass = mono_defaults.object_class;
5792                         *sp = add;
5793                         ip += 5;
5794                         /* LDOBJ impl */
5795                         n = mono_class_value_size (klass, NULL);
5796                         ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
5797                         NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
5798                         if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
5799                                 MonoInst *copy;
5800                                 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
5801                                 copy->inst_left = iargs [0];
5802                                 copy->inst_right = *sp;
5803                                 copy->cil_code = ip;
5804                                 copy->backend.size = n;
5805                                 MONO_ADD_INS (bblock, copy);
5806                         } else {
5807                                 MonoMethod *memcpy_method = get_memcpy_method ();
5808                                 iargs [1] = *sp;
5809                                 NEW_ICONST (cfg, iargs [2], n);
5810                                 iargs [2]->cil_code = ip;
5811
5812                                 mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
5813                         }
5814                         NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
5815                         ++sp;
5816                         inline_costs += 2;
5817                         break;
5818                 }
5819                 case CEE_UNBOX: {
5820                         MonoInst *add, *vtoffset;
5821
5822                         CHECK_STACK (1);
5823                         --sp;
5824                         CHECK_OPSIZE (5);
5825                         token = read32 (ip + 1);
5826                         klass = mini_get_class (method, token, generic_context);
5827                         CHECK_TYPELOAD (klass);
5828
5829                         if (mono_class_is_nullable (klass)) {
5830                                 int v = handle_unbox_nullable (cfg, bblock, *sp, ip, klass);
5831                                 NEW_TEMPLOAD (cfg, *sp, v);
5832                                 sp ++;
5833                                 ip += 5;
5834                                 break;
5835                         }
5836
5837                         /* Needed by the code generated in inssel.brg */
5838                         mono_get_got_var (cfg);
5839
5840                         MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
5841                         ins->type = STACK_OBJ;
5842                         ins->inst_left = *sp;
5843                         ins->klass = klass;
5844                         ins->inst_newa_class = klass;
5845                         ins->cil_code = ip;
5846
5847                         MONO_INST_NEW (cfg, add, OP_PADD);
5848                         NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
5849                         add->inst_left = ins;
5850                         add->inst_right = vtoffset;
5851                         add->type = STACK_MP;
5852                         add->klass = klass;
5853                         *sp++ = add;
5854                         ip += 5;
5855                         inline_costs += 2;
5856                         break;
5857                 }
5858                 case CEE_CASTCLASS:
5859                         CHECK_STACK (1);
5860                         --sp;
5861                         CHECK_OPSIZE (5);
5862                         token = read32 (ip + 1);
5863                         klass = mini_get_class (method, token, generic_context);
5864                         CHECK_TYPELOAD (klass);
5865                         if (sp [0]->type != STACK_OBJ)
5866                                 UNVERIFIED;
5867
5868                         /* Needed by the code generated in inssel.brg */
5869                         mono_get_got_var (cfg);
5870                 
5871                         if (klass->marshalbyref || klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
5872                                 
5873                                 MonoMethod *mono_castclass;
5874                                 MonoInst *iargs [1];
5875                                 MonoBasicBlock *ebblock;
5876                                 int costs;
5877                                 int temp;
5878                                 
5879                                 mono_castclass = mono_marshal_get_castclass (klass); 
5880                                 iargs [0] = sp [0];
5881                                 
5882                                 costs = inline_method (cfg, mono_castclass, mono_method_signature (mono_castclass), bblock, 
5883                                                            iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5884                         
5885                                 g_assert (costs > 0);
5886                                 
5887                                 ip += 5;
5888                                 real_offset += 5;
5889                         
5890                                 GET_BBLOCK (cfg, bblock, ip);
5891                                 ebblock->next_bb = bblock;
5892                                 link_bblock (cfg, ebblock, bblock);
5893
5894                                 temp = iargs [0]->inst_i0->inst_c0;
5895                                 NEW_TEMPLOAD (cfg, *sp, temp);
5896                                 
5897                                 sp++;
5898                                 bblock = ebblock;
5899                                 inline_costs += costs;
5900                         } else {
5901                                 MONO_INST_NEW (cfg, ins, *ip);
5902                                 ins->type = STACK_OBJ;
5903                                 ins->inst_left = *sp;
5904                                 ins->klass = klass;
5905                                 ins->inst_newa_class = klass;
5906                                 ins->cil_code = ip;
5907                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 5);
5908                                 ip += 5;
5909                         }
5910                         break;
5911                 case CEE_THROW:
5912                         CHECK_STACK (1);
5913                         MONO_INST_NEW (cfg, ins, OP_THROW);
5914                         --sp;
5915                         ins->inst_left = *sp;
5916                         ins->cil_code = ip++;
5917                         bblock->out_of_line = TRUE;
5918                         MONO_ADD_INS (bblock, ins);
5919                         MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
5920                         ins->cil_code = ip - 1;
5921                         MONO_ADD_INS (bblock, ins);
5922                         sp = stack_start;
5923                         
5924                         link_bblock (cfg, bblock, end_bblock);
5925                         start_new_bblock = 1;
5926                         break;
5927                 case CEE_LDFLD:
5928                 case CEE_LDFLDA:
5929                 case CEE_STFLD: {
5930                         MonoInst *offset_ins;
5931                         MonoClassField *field;
5932                         MonoBasicBlock *ebblock;
5933                         int costs;
5934                         guint foffset;
5935
5936                         if (*ip == CEE_STFLD) {
5937                                 CHECK_STACK (2);
5938                                 sp -= 2;
5939                         } else {
5940                                 CHECK_STACK (1);
5941                                 --sp;
5942                         }
5943                         if (sp [0]->type == STACK_I4 || sp [0]->type == STACK_I8 || sp [0]->type == STACK_R8)
5944                                 UNVERIFIED;
5945                         if (*ip != CEE_LDFLD && sp [0]->type == STACK_VTYPE)
5946                                 UNVERIFIED;
5947                         CHECK_OPSIZE (5);
5948                         token = read32 (ip + 1);
5949                         if (method->wrapper_type != MONO_WRAPPER_NONE) {
5950                                 field = mono_method_get_wrapper_data (method, token);
5951                                 klass = field->parent;
5952                         } else {
5953                                 field = mono_field_from_token (image, token, &klass, generic_context);
5954                         }
5955                         if (!field)
5956                                 goto load_error;
5957                         mono_class_init (klass);
5958                         if (!dont_verify && !cfg->skip_visibility && !mono_method_can_access_field (method, field))
5959                                 UNVERIFIED;
5960
5961                         foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
5962                         /* FIXME: mark instructions for use in SSA */
5963                         if (*ip == CEE_STFLD) {
5964                                 if (target_type_is_incompatible (cfg, field->type, sp [1]))
5965                                         UNVERIFIED;
5966                                 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
5967                                         MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type); 
5968                                         MonoInst *iargs [5];
5969
5970                                         iargs [0] = sp [0];
5971                                         NEW_CLASSCONST (cfg, iargs [1], klass);
5972                                         NEW_FIELDCONST (cfg, iargs [2], field);
5973                                         NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : 
5974                                                     field->offset);
5975                                         iargs [4] = sp [1];
5976
5977                                         if (cfg->opt & MONO_OPT_INLINE) {
5978                                                 costs = inline_method (cfg, stfld_wrapper, mono_method_signature (stfld_wrapper), bblock, 
5979                                                                        iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
5980                                                 g_assert (costs > 0);
5981                                                       
5982                                                 ip += 5;
5983                                                 real_offset += 5;
5984
5985                                                 GET_BBLOCK (cfg, bblock, ip);
5986                                                 ebblock->next_bb = bblock;
5987                                                 link_bblock (cfg, ebblock, bblock);
5988
5989                                                 /* indicates start of a new block, and triggers a load 
5990                                                    of all stack arguments at bb boundarie */
5991                                                 bblock = ebblock;
5992
5993                                                 inline_costs += costs;
5994                                                 break;
5995                                         } else {
5996                                                 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, mono_method_signature (stfld_wrapper), iargs, ip, NULL);
5997                                         }
5998 #if HAVE_WRITE_BARRIERS
5999                                 } else if (mono_type_to_stind (field->type) == CEE_STIND_REF) {
6000                                         /* insert call to write barrier */
6001                                         MonoMethod *write_barrier = mono_marshal_get_write_barrier ();
6002                                         MonoInst *iargs [2];
6003                                         NEW_ICONST (cfg, offset_ins, foffset);
6004                                         MONO_INST_NEW (cfg, ins, OP_PADD);
6005                                         ins->cil_code = ip;
6006                                         ins->inst_left = *sp;
6007                                         ins->inst_right = offset_ins;
6008                                         ins->type = STACK_MP;
6009                                         ins->klass = mono_defaults.object_class;
6010                                         iargs [0] = ins;
6011                                         iargs [1] = sp [1];
6012                                         mono_emit_method_call_spilled (cfg, bblock, write_barrier, mono_method_signature (write_barrier), iargs, ip, NULL);
6013 #endif
6014 #ifdef MONO_ARCH_SOFT_FLOAT
6015                                 } else if (mono_type_to_stind (field->type) == CEE_STIND_R4) {
6016                                         NEW_ICONST (cfg, offset_ins, foffset);
6017                                         MONO_INST_NEW (cfg, ins, OP_PADD);
6018                                         ins->cil_code = ip;
6019                                         ins->inst_left = *sp;
6020                                         ins->inst_right = offset_ins;
6021                                         ins->type = STACK_MP;
6022                                         ins->klass = mono_defaults.object_class;
6023                                         handle_store_float (cfg, bblock, ins, sp [1], ip);
6024 #endif
6025                                 } else {
6026                                         MonoInst *store;
6027                                         NEW_ICONST (cfg, offset_ins, foffset);
6028                                         MONO_INST_NEW (cfg, ins, OP_PADD);
6029                                         ins->cil_code = ip;
6030                                         ins->inst_left = *sp;
6031                                         ins->inst_right = offset_ins;
6032                                         ins->type = STACK_MP;
6033
6034                                         MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
6035                                         store->cil_code = ip;
6036                                         store->inst_left = ins;
6037                                         store->inst_right = sp [1];
6038                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
6039                                         store->flags |= ins_flag;
6040                                         ins_flag = 0;
6041                                         if (store->opcode == CEE_STOBJ) {
6042                                                 handle_stobj (cfg, bblock, ins, sp [1], ip, 
6043                                                               mono_class_from_mono_type (field->type), FALSE, FALSE, TRUE);
6044                                         } else
6045                                                 MONO_ADD_INS (bblock, store);
6046                                 }
6047                         } else {
6048                                 if ((klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) || klass->contextbound || klass == mono_defaults.marshalbyrefobject_class) {
6049                                         MonoMethod *wrapper = (*ip == CEE_LDFLDA) ? mono_marshal_get_ldflda_wrapper (field->type) : mono_marshal_get_ldfld_wrapper (field->type); 
6050                                         MonoInst *iargs [4];
6051                                         int temp;
6052                                         
6053                                         iargs [0] = sp [0];
6054                                         NEW_CLASSCONST (cfg, iargs [1], klass);
6055                                         NEW_FIELDCONST (cfg, iargs [2], field);
6056                                         NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
6057                                         if ((cfg->opt & MONO_OPT_INLINE) && !MONO_TYPE_ISSTRUCT (mono_method_signature (wrapper)->ret)) {
6058                                                 costs = inline_method (cfg, wrapper, mono_method_signature (wrapper), bblock, 
6059                                                                        iargs, ip, real_offset, dont_inline, &ebblock, TRUE);
6060                                                 g_assert (costs > 0);
6061                                                       
6062                                                 ip += 5;
6063                                                 real_offset += 5;
6064
6065                                                 GET_BBLOCK (cfg, bblock, ip);
6066                                                 ebblock->next_bb = bblock;
6067                                                 link_bblock (cfg, ebblock, bblock);
6068
6069                                                 temp = iargs [0]->inst_i0->inst_c0;
6070
6071                                                 NEW_TEMPLOAD (cfg, *sp, temp);
6072                                                 sp++;
6073
6074                                                 /* indicates start of a new block, and triggers a load of
6075                                                    all stack arguments at bb boundarie */
6076                                                 bblock = ebblock;
6077                                                 
6078                                                 inline_costs += costs;
6079                                                 break;
6080                                         } else {
6081                                                 temp = mono_emit_method_call_spilled (cfg, bblock, wrapper, mono_method_signature (wrapper), iargs, ip, NULL);
6082                                                 NEW_TEMPLOAD (cfg, *sp, temp);
6083                                                 sp++;
6084                                         }
6085                                 } else {
6086                                         NEW_ICONST (cfg, offset_ins, foffset);
6087                                         MONO_INST_NEW (cfg, ins, OP_PADD);
6088                                         ins->cil_code = ip;
6089                                         ins->inst_left = *sp;
6090                                         ins->inst_right = offset_ins;
6091                                         ins->type = STACK_MP;
6092
6093                                         if (*ip == CEE_LDFLDA) {
6094                                                 ins->klass = mono_class_from_mono_type (field->type);
6095                                                 *sp++ = ins;
6096                                         } else {
6097                                                 MonoInst *load;
6098                                                 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
6099                                                 type_to_eval_stack_type (field->type, load);
6100                                                 load->cil_code = ip;
6101                                                 load->inst_left = ins;
6102                                                 load->flags |= ins_flag;
6103                                                 ins_flag = 0;
6104 #ifdef MONO_ARCH_SOFT_FLOAT
6105                                                 if (mono_type_to_ldind (field->type) == CEE_LDIND_R4) {
6106                                                         int temp;
6107                                                         temp = handle_load_float (cfg, bblock, ins, ip);
6108                                                         NEW_TEMPLOAD (cfg, *sp, temp);
6109                                                         sp++;
6110                                                 } else
6111 #endif
6112                                                 *sp++ = load;
6113                                         }
6114                                 }
6115                         }
6116                         ip += 5;
6117                         break;
6118                 }
6119                 case CEE_LDSFLD:
6120                 case CEE_LDSFLDA:
6121                 case CEE_STSFLD: {
6122                         MonoClassField *field;
6123                         gpointer addr = NULL;
6124
6125                         CHECK_OPSIZE (5);
6126                         token = read32 (ip + 1);
6127                         if (method->wrapper_type != MONO_WRAPPER_NONE) {
6128                                 field = mono_method_get_wrapper_data (method, token);
6129                                 klass = field->parent;
6130                         }
6131                         else
6132                                 field = mono_field_from_token (image, token, &klass, generic_context);
6133                         if (!field)
6134                                 goto load_error;
6135                         mono_class_init (klass);
6136
6137                         g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_LITERAL));
6138
6139                         if ((*ip) == CEE_STSFLD)
6140                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
6141
6142                         /* The special_static_fields field is init'd in mono_class_vtable, so it needs
6143                          * to be called here.
6144                          */
6145                         if (!(cfg->opt & MONO_OPT_SHARED))
6146                                 mono_class_vtable (cfg->domain, klass);
6147                         mono_domain_lock (cfg->domain);
6148                         if (cfg->domain->special_static_fields)
6149                                 addr = g_hash_table_lookup (cfg->domain->special_static_fields, field);
6150                         mono_domain_unlock (cfg->domain);
6151
6152                         if ((cfg->opt & MONO_OPT_SHARED) || (cfg->compile_aot && addr)) {
6153                                 int temp;
6154                                 MonoInst *iargs [2];
6155                                 MonoInst *domain_var;
6156                                 
6157                                 g_assert (field->parent);
6158                                 /* avoid depending on undefined C behavior in sequence points */
6159                                 domain_var = mono_get_domainvar (cfg);
6160                                 NEW_TEMPLOAD (cfg, iargs [0], domain_var->inst_c0);
6161                                 NEW_FIELDCONST (cfg, iargs [1], field);
6162                                 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
6163                                 NEW_TEMPLOAD (cfg, ins, temp);
6164                         } else {
6165                                 MonoVTable *vtable;
6166                                 vtable = mono_class_vtable (cfg->domain, klass);
6167                                 if (!addr) {
6168                                         if (mini_field_access_needs_cctor_run (cfg, method, vtable) && !(g_slist_find (class_inits, vtable))) {
6169                                                 guint8 *tramp = mono_create_class_init_trampoline (vtable);
6170                                                 mono_emit_native_call (cfg, bblock, tramp, 
6171                                                                                            helper_sig_class_init_trampoline,
6172                                                                                            NULL, ip, FALSE, FALSE);
6173                                                 if (cfg->verbose_level > 2)
6174                                                         g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
6175                                                 class_inits = g_slist_prepend (class_inits, vtable);
6176                                         } else {
6177                                                 if (cfg->run_cctors) {
6178                                                         /* This makes so that inline cannot trigger */
6179                                                         /* .cctors: too many apps depend on them */
6180                                                         /* running with a specific order... */
6181                                                         if (! vtable->initialized)
6182                                                                 INLINE_FAILURE;
6183                                                         mono_runtime_class_init (vtable);
6184                                                 }
6185                                         }
6186                                         addr = (char*)vtable->data + field->offset;
6187
6188                                         if (cfg->compile_aot)
6189                                                 NEW_SFLDACONST (cfg, ins, field);
6190                                         else
6191                                                 NEW_PCONST (cfg, ins, addr);
6192                                         ins->cil_code = ip;
6193                                 } else {
6194                                         /* 
6195                                          * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr)) 
6196                                          * This could be later optimized to do just a couple of
6197                                          * memory dereferences with constant offsets.
6198                                          */
6199                                         int temp;
6200                                         MonoInst *iargs [1];
6201                                         NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
6202                                         temp = mono_emit_jit_icall (cfg, bblock, mono_get_special_static_data, iargs, ip);
6203                                         NEW_TEMPLOAD (cfg, ins, temp);
6204                                 }
6205                         }
6206
6207                         /* FIXME: mark instructions for use in SSA */
6208                         if (*ip == CEE_LDSFLDA) {
6209                                 ins->klass = mono_class_from_mono_type (field->type);
6210                                 *sp++ = ins;
6211                         } else if (*ip == CEE_STSFLD) {
6212                                 MonoInst *store;
6213                                 CHECK_STACK (1);
6214                                 sp--;
6215                                 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
6216                                 store->cil_code = ip;
6217                                 store->inst_left = ins;
6218                                 store->inst_right = sp [0];
6219                                 store->flags |= ins_flag;
6220                                 ins_flag = 0;
6221
6222                                 if (store->opcode == CEE_STOBJ) {
6223                                         handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE, FALSE);
6224                                 } else
6225                                         MONO_ADD_INS (bblock, store);
6226                         } else {
6227                                 gboolean is_const = FALSE;
6228                                 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
6229                                 if (!((cfg->opt & MONO_OPT_SHARED) || cfg->compile_aot) && 
6230                                     vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
6231                                         gpointer addr = (char*)vtable->data + field->offset;
6232                                         int ro_type = field->type->type;
6233                                         if (ro_type == MONO_TYPE_VALUETYPE && field->type->data.klass->enumtype) {
6234                                                 ro_type = field->type->data.klass->enum_basetype->type;
6235                                         }
6236                                         /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
6237                                         is_const = TRUE;
6238                                         switch (ro_type) {
6239                                         case MONO_TYPE_BOOLEAN:
6240                                         case MONO_TYPE_U1:
6241                                                 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
6242                                                 sp++;
6243                                                 break;
6244                                         case MONO_TYPE_I1:
6245                                                 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
6246                                                 sp++;
6247                                                 break;                                          
6248                                         case MONO_TYPE_CHAR:
6249                                         case MONO_TYPE_U2:
6250                                                 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
6251                                                 sp++;
6252                                                 break;
6253                                         case MONO_TYPE_I2:
6254                                                 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
6255                                                 sp++;
6256                                                 break;
6257                                                 break;
6258                                         case MONO_TYPE_I4:
6259                                                 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
6260                                                 sp++;
6261                                                 break;                                          
6262                                         case MONO_TYPE_U4:
6263                                                 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
6264                                                 sp++;
6265                                                 break;
6266 #ifndef HAVE_MOVING_COLLECTOR
6267                                         case MONO_TYPE_I:
6268                                         case MONO_TYPE_U:
6269                                         case MONO_TYPE_STRING:
6270                                         case MONO_TYPE_OBJECT:
6271                                         case MONO_TYPE_CLASS:
6272                                         case MONO_TYPE_SZARRAY:
6273                                         case MONO_TYPE_PTR:
6274                                         case MONO_TYPE_FNPTR:
6275                                         case MONO_TYPE_ARRAY:
6276                                                 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
6277                                                 type_to_eval_stack_type (field->type, *sp);
6278                                                 sp++;
6279                                                 break;
6280 #endif
6281                                         case MONO_TYPE_I8:
6282                                         case MONO_TYPE_U8:
6283                                                 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
6284                                                 sp [0]->type = STACK_I8;
6285                                                 sp [0]->inst_l = *((gint64 *)addr);
6286                                                 sp++;
6287                                                 break;
6288                                         case MONO_TYPE_R4:
6289                                         case MONO_TYPE_R8:
6290                                         case MONO_TYPE_VALUETYPE:
6291                                         default:
6292                                                 is_const = FALSE;
6293                                                 break;
6294                                         }
6295                                 }
6296
6297                                 if (!is_const) {
6298                                         MonoInst *load;
6299                                         CHECK_STACK_OVF (1);
6300                                         MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
6301                                         type_to_eval_stack_type (field->type, load);
6302                                         load->cil_code = ip;
6303                                         load->inst_left = ins;
6304                                         *sp++ = load;
6305                                         load->flags |= ins_flag;
6306                                         ins_flag = 0;
6307                                         /* fixme: dont see the problem why this does not work */
6308                                         //cfg->disable_aot = TRUE;
6309                                 }
6310                         }
6311                         ip += 5;
6312                         break;
6313                 }
6314                 case CEE_STOBJ:
6315                         CHECK_STACK (2);
6316                         sp -= 2;
6317                         CHECK_OPSIZE (5);
6318                         token = read32 (ip + 1);
6319                         klass = mini_get_class (method, token, generic_context);
6320                         CHECK_TYPELOAD (klass);
6321                         n = mono_type_to_stind (&klass->byval_arg);
6322                         if (n == CEE_STOBJ) {
6323                                 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE, TRUE);
6324                         } else {
6325                                 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
6326                                 MonoInst *store;
6327                                 MONO_INST_NEW (cfg, store, n);
6328                                 store->cil_code = ip;
6329                                 store->inst_left = sp [0];
6330                                 store->inst_right = sp [1];
6331                                 store->flags |= ins_flag;
6332                                 MONO_ADD_INS (bblock, store);
6333                         }
6334                         ins_flag = 0;
6335                         ip += 5;
6336                         inline_costs += 1;
6337                         break;
6338                 case CEE_BOX: {
6339                         MonoInst *val;
6340                         CHECK_STACK (1);
6341                         --sp;
6342                         val = *sp;
6343                         CHECK_OPSIZE (5);
6344                         token = read32 (ip + 1);
6345                         klass = mini_get_class (method, token, generic_context);
6346                         CHECK_TYPELOAD (klass);
6347
6348                         if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
6349                                 *sp++ = val;
6350                                 ip += 5;
6351                                 break;
6352                         }
6353                         if (klass == mono_defaults.void_class)
6354                                 UNVERIFIED;
6355                         if (target_type_is_incompatible (cfg, &klass->byval_arg, *sp))
6356                                 UNVERIFIED;
6357                         /* frequent check in generic code: box (struct), brtrue */
6358                         if (!mono_class_is_nullable (klass) &&
6359                             ip + 5 < end && ip_in_bb (cfg, bblock, ip + 5) && (ip [5] == CEE_BRTRUE || ip [5] == CEE_BRTRUE_S)) {
6360                                 /*g_print ("box-brtrue opt at 0x%04x in %s\n", real_offset, method->name);*/
6361                                 MONO_INST_NEW (cfg, ins, CEE_POP);
6362                                 MONO_ADD_INS (bblock, ins);
6363                                 ins->cil_code = ip;
6364                                 ins->inst_i0 = *sp;
6365                                 ip += 5;
6366                                 MONO_INST_NEW (cfg, ins, OP_BR);
6367                                 ins->cil_code = ip;
6368                                 MONO_ADD_INS (bblock, ins);
6369                                 if (*ip == CEE_BRTRUE_S) {
6370                                         CHECK_OPSIZE (2);
6371                                         ip++;
6372                                         target = ip + 1 + (signed char)(*ip);
6373                                         ip++;
6374                                 } else {
6375                                         CHECK_OPSIZE (5);
6376                                         ip++;
6377                                         target = ip + 4 + (gint)(read32 (ip));
6378                                         ip += 4;
6379                                 }
6380                                 GET_BBLOCK (cfg, tblock, target);
6381                                 link_bblock (cfg, bblock, tblock);
6382                                 CHECK_BBLOCK (target, ip, tblock);
6383                                 ins->inst_target_bb = tblock;
6384                                 GET_BBLOCK (cfg, tblock, ip);
6385                                 link_bblock (cfg, bblock, tblock);
6386                                 if (sp != stack_start) {
6387                                         handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
6388                                         sp = stack_start;
6389                                         CHECK_UNVERIFIABLE (cfg);
6390                                 }
6391                                 start_new_bblock = 1;
6392                                 break;
6393                         }
6394                         *sp++ = handle_box (cfg, bblock, val, ip, klass);
6395                         ip += 5;
6396                         inline_costs += 1;
6397                         break;
6398                 }
6399                 case CEE_NEWARR:
6400                         CHECK_STACK (1);
6401                         MONO_INST_NEW (cfg, ins, *ip);
6402                         ins->cil_code = ip;
6403                         --sp;
6404
6405                         CHECK_OPSIZE (5);
6406                         token = read32 (ip + 1);
6407
6408                         /* allocate the domainvar - becaus this is used in decompose_foreach */
6409                         if (cfg->opt & MONO_OPT_SHARED) {
6410                                 mono_get_domainvar (cfg);
6411                                 /* LAME-IR: Mark it as used since otherwise it will be optimized away */
6412                                 cfg->domainvar->flags |= MONO_INST_VOLATILE;
6413                         }
6414
6415                         /* Ditto */
6416                         mono_get_got_var (cfg);
6417
6418                         klass = mini_get_class (method, token, generic_context);
6419                         CHECK_TYPELOAD (klass);
6420                         ins->inst_newa_class = klass;
6421                         ins->inst_newa_len = *sp;
6422                         ins->type = STACK_OBJ;
6423                         ins->klass = mono_array_class_get (klass, 1);
6424                         ip += 5;
6425                         *sp++ = ins;
6426                         /* 
6427                          * we store the object so calls to create the array are not interleaved
6428                          * with the arguments of other calls.
6429                          */
6430                         if (1) {
6431                                 MonoInst *store, *temp, *load;
6432                                 const char *data_ptr;
6433                                 int data_size = 0;
6434                                 --sp;
6435                                 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
6436                                 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
6437                                 store->cil_code = ins->cil_code;
6438                                 MONO_ADD_INS (bblock, store);
6439                                 /* 
6440                                  * we inline/optimize the initialization sequence if possible.
6441                                  * we should also allocate the array as not cleared, since we spend as much time clearing to 0 as initializing
6442                                  * for small sizes open code the memcpy
6443                                  * ensure the rva field is big enough
6444                                  */
6445                                 if ((cfg->opt & MONO_OPT_INTRINS) && ip_in_bb (cfg, bblock, ip + 6) && (data_ptr = initialize_array_data (method, cfg->compile_aot, ip, ins, &data_size))) {
6446                                         MonoMethod *memcpy_method = get_memcpy_method ();
6447                                         MonoInst *data_offset, *add;
6448                                         MonoInst *iargs [3];
6449                                         NEW_ICONST (cfg, iargs [2], data_size);
6450                                         NEW_TEMPLOAD (cfg, load, temp->inst_c0);
6451                                         load->cil_code = ins->cil_code;
6452                                         NEW_ICONST (cfg, data_offset, G_STRUCT_OFFSET (MonoArray, vector));
6453                                         MONO_INST_NEW (cfg, add, OP_PADD);
6454                                         add->inst_left = load;
6455                                         add->inst_right = data_offset;
6456                                         add->cil_code = ip;
6457                                         iargs [0] = add;
6458                                         if (cfg->compile_aot) {
6459                                                 NEW_AOTCONST_TOKEN (cfg, iargs [1], MONO_PATCH_INFO_RVA, method->klass->image, GPOINTER_TO_UINT(data_ptr), STACK_PTR, NULL);
6460                                         } else {
6461                                                 NEW_PCONST (cfg, iargs [1], (char*)data_ptr);
6462                                         }
6463                                         mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
6464                                         ip += 11;
6465                                 }
6466                                 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
6467                                 load->cil_code = ins->cil_code;
6468                                 *sp++ = load;
6469                         }
6470                         inline_costs += 1;
6471                         break;
6472                 case CEE_LDLEN:
6473                         CHECK_STACK (1);
6474                         --sp;
6475                         if (sp [0]->type != STACK_OBJ)
6476                                 UNVERIFIED;
6477                         MONO_INST_NEW (cfg, ins, *ip);
6478                         ins->cil_code = ip++;
6479                         ins->inst_left = *sp;
6480                         ins->type = STACK_PTR;
6481                         *sp++ = ins;
6482                         break;
6483                 case CEE_LDELEMA:
6484                         CHECK_STACK (2);
6485                         sp -= 2;
6486                         CHECK_OPSIZE (5);
6487                         if (sp [0]->type != STACK_OBJ)
6488                                 UNVERIFIED;
6489
6490                         klass = mini_get_class (method, read32 (ip + 1), generic_context);
6491                         CHECK_TYPELOAD (klass);
6492                         /* we need to make sure that this array is exactly the type it needs
6493                          * to be for correctness. the wrappers are lax with their usage
6494                          * so we need to ignore them here
6495                          */
6496                         if (!klass->valuetype && method->wrapper_type == MONO_WRAPPER_NONE) {
6497                                 MonoInst* check;
6498
6499                                 /* Needed by the code generated in inssel.brg */
6500                                 mono_get_got_var (cfg);
6501
6502                                 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
6503                                 check->cil_code = ip;
6504                                 check->klass = klass;
6505                                 check->inst_left = sp [0];
6506                                 check->type = STACK_OBJ;
6507                                 check->klass = klass;
6508                                 sp [0] = check;
6509                         }
6510                         
6511                         mono_class_init (klass);
6512                         NEW_LDELEMA (cfg, ins, sp, klass);
6513                         ins->cil_code = ip;
6514                         *sp++ = ins;
6515                         ip += 5;
6516                         break;
6517                 case CEE_LDELEM_ANY: {
6518                         MonoInst *load;
6519                         CHECK_STACK (2);
6520                         sp -= 2;
6521                         if (sp [0]->type != STACK_OBJ)
6522                                 UNVERIFIED;
6523                         CHECK_OPSIZE (5);
6524                         token = read32 (ip + 1);
6525                         klass = mono_class_get_full (image, token, generic_context);
6526                         CHECK_TYPELOAD (klass);
6527                         mono_class_init (klass);
6528                         NEW_LDELEMA (cfg, load, sp, klass);
6529                         load->cil_code = ip;
6530                         MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
6531                         ins->cil_code = ip;
6532                         ins->inst_left = load;
6533                         *sp++ = ins;
6534                         type_to_eval_stack_type (&klass->byval_arg, ins);
6535                         ip += 5;
6536                         break;
6537                 }
6538                 case CEE_LDELEM_I1:
6539                 case CEE_LDELEM_U1:
6540                 case CEE_LDELEM_I2:
6541                 case CEE_LDELEM_U2:
6542                 case CEE_LDELEM_I4:
6543                 case CEE_LDELEM_U4:
6544                 case CEE_LDELEM_I8:
6545                 case CEE_LDELEM_I:
6546                 case CEE_LDELEM_R4:
6547                 case CEE_LDELEM_R8:
6548                 case CEE_LDELEM_REF: {
6549                         MonoInst *load;
6550                         /*
6551                          * translate to:
6552                          * ldind.x (ldelema (array, index))
6553                          * ldelema does the bounds check
6554                          */
6555                         CHECK_STACK (2);
6556                         sp -= 2;
6557                         if (sp [0]->type != STACK_OBJ)
6558                                 UNVERIFIED;
6559                         klass = array_access_to_klass (*ip, sp [0]);
6560                         NEW_LDELEMA (cfg, load, sp, klass);
6561                         load->cil_code = ip;
6562 #ifdef MONO_ARCH_SOFT_FLOAT
6563                         if (*ip == CEE_LDELEM_R4) {
6564                                 int temp;
6565                                 temp = handle_load_float (cfg, bblock, load, ip);
6566                                 NEW_TEMPLOAD (cfg, *sp, temp);
6567                                 sp++;
6568                                 ++ip;
6569                                 break;
6570                         }
6571 #endif
6572                         MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
6573                         ins->cil_code = ip;
6574                         ins->inst_left = load;
6575                         *sp++ = ins;
6576                         ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
6577                         ins->klass = klass;
6578                         ++ip;
6579                         break;
6580                 }
6581                 case CEE_STELEM_I:
6582                 case CEE_STELEM_I1:
6583                 case CEE_STELEM_I2:
6584                 case CEE_STELEM_I4:
6585                 case CEE_STELEM_I8:
6586                 case CEE_STELEM_R4:
6587                 case CEE_STELEM_R8: {
6588                         MonoInst *load;
6589                         /*
6590                          * translate to:
6591                          * stind.x (ldelema (array, index), val)
6592                          * ldelema does the bounds check
6593                          */
6594                         CHECK_STACK (3);
6595                         sp -= 3;
6596                         if (sp [0]->type != STACK_OBJ)
6597                                 UNVERIFIED;
6598                         klass = array_access_to_klass (*ip, sp [0]);
6599                         NEW_LDELEMA (cfg, load, sp, klass);
6600                         load->cil_code = ip;
6601 #ifdef MONO_ARCH_SOFT_FLOAT
6602                         if (*ip == CEE_STELEM_R4) {
6603                                 handle_store_float (cfg, bblock, load, sp [2], ip);
6604                                 ip++;
6605                                 break;
6606                         }
6607 #endif
6608                         MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
6609                         ins->cil_code = ip;
6610                         ins->inst_left = load;
6611                         ins->inst_right = sp [2];
6612                         ++ip;
6613                         handle_loaded_temps (cfg, bblock, stack_start, sp);
6614                         MONO_ADD_INS (bblock, ins);
6615                         inline_costs += 1;
6616                         break;
6617                 }
6618                 case CEE_STELEM_ANY: {
6619                         MonoInst *load;
6620                         /*
6621                          * translate to:
6622                          * stind.x (ldelema (array, index), val)
6623                          * ldelema does the bounds check
6624                          */
6625                         CHECK_STACK (3);
6626                         sp -= 3;
6627                         if (sp [0]->type != STACK_OBJ)
6628                                 UNVERIFIED;
6629                         CHECK_OPSIZE (5);
6630                         token = read32 (ip + 1);
6631                         klass = mono_class_get_full (image, token, generic_context);
6632                         CHECK_TYPELOAD (klass);
6633                         mono_class_init (klass);
6634                         if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
6635                                 /* storing a NULL doesn't need any of the complex checks in stelemref */
6636                                 if (sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL) {
6637                                         MonoInst *load;
6638                                         NEW_LDELEMA (cfg, load, sp, mono_defaults.object_class);
6639                                         load->cil_code = ip;
6640                                         MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
6641                                         ins->cil_code = ip;
6642                                         ins->inst_left = load;
6643                                         ins->inst_right = sp [2];
6644                                         MONO_ADD_INS (bblock, ins);
6645                                 } else {
6646                                         MonoMethod* helper = mono_marshal_get_stelemref ();
6647                                         MonoInst *iargs [3];
6648                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
6649
6650                                         iargs [2] = sp [2];
6651                                         iargs [1] = sp [1];
6652                                         iargs [0] = sp [0];
6653
6654                                         mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
6655                                 }
6656                         } else {
6657                                 NEW_LDELEMA (cfg, load, sp, klass);
6658                                 load->cil_code = ip;
6659
6660                                 n = mono_type_to_stind (&klass->byval_arg);
6661                                 if (n == CEE_STOBJ)
6662                                         handle_stobj (cfg, bblock, load, sp [2], ip, klass, FALSE, FALSE, TRUE);
6663                                 else {
6664                                         MONO_INST_NEW (cfg, ins, n);
6665                                         ins->cil_code = ip;
6666                                         ins->inst_left = load;
6667                                         ins->inst_right = sp [2];
6668                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
6669                                         MONO_ADD_INS (bblock, ins);
6670                                 }
6671                         }
6672                         ip += 5;
6673                         inline_costs += 1;
6674                         break;
6675                 }
6676                 case CEE_STELEM_REF: {
6677                         MonoInst *iargs [3];
6678                         MonoMethod* helper = mono_marshal_get_stelemref ();
6679
6680                         CHECK_STACK (3);
6681                         sp -= 3;
6682                         if (sp [0]->type != STACK_OBJ)
6683                                 UNVERIFIED;
6684                         if (sp [2]->type != STACK_OBJ)
6685                                 UNVERIFIED;
6686
6687                         handle_loaded_temps (cfg, bblock, stack_start, sp);
6688
6689                         /* storing a NULL doesn't need any of the complex checks in stelemref */
6690                         if (sp [2]->opcode == OP_PCONST && sp [2]->inst_p0 == NULL) {
6691                                 MonoInst *load;
6692                                 NEW_LDELEMA (cfg, load, sp, mono_defaults.object_class);
6693                                 load->cil_code = ip;
6694                                 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
6695                                 ins->cil_code = ip;
6696                                 ins->inst_left = load;
6697                                 ins->inst_right = sp [2];
6698                                 MONO_ADD_INS (bblock, ins);
6699                         } else {
6700                                 iargs [2] = sp [2];
6701                                 iargs [1] = sp [1];
6702                                 iargs [0] = sp [0];
6703                         
6704                                 mono_emit_method_call_spilled (cfg, bblock, helper, mono_method_signature (helper), iargs, ip, NULL);
6705                                 inline_costs += 1;
6706                         }
6707
6708                         ++ip;
6709                         break;
6710                 }
6711                 case CEE_CKFINITE: {
6712                         MonoInst *store, *temp;
6713                         CHECK_STACK (1);
6714
6715                         /* this instr. can throw exceptions as side effect,
6716                          * so we cant eliminate dead code which contains CKFINITE opdodes.
6717                          * Spilling to memory makes sure that we always perform
6718                          * this check */
6719
6720                         
6721                         MONO_INST_NEW (cfg, ins, OP_CKFINITE);
6722                         ins->cil_code = ip;
6723                         ins->inst_left = sp [-1];
6724                         temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
6725
6726                         NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
6727                         store->cil_code = ip;
6728                         MONO_ADD_INS (bblock, store);
6729
6730                         NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
6731                        
6732                         ++ip;
6733                         break;
6734                 }
6735                 case CEE_REFANYVAL:
6736                         CHECK_STACK (1);
6737                         MONO_INST_NEW (cfg, ins, *ip);
6738                         --sp;
6739                         CHECK_OPSIZE (5);
6740                         klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
6741                         CHECK_TYPELOAD (klass);
6742                         mono_class_init (klass);
6743                         ins->type = STACK_MP;
6744                         ins->inst_left = *sp;
6745                         ins->klass = klass;
6746                         ins->inst_newa_class = klass;
6747                         ins->cil_code = ip;
6748                         ip += 5;
6749                         *sp++ = ins;
6750                         break;
6751                 case CEE_MKREFANY: {
6752                         MonoInst *loc, *klassconst;
6753
6754                         CHECK_STACK (1);
6755                         MONO_INST_NEW (cfg, ins, *ip);
6756                         --sp;
6757                         CHECK_OPSIZE (5);
6758                         klass = mono_class_get_full (image, read32 (ip + 1), generic_context);
6759                         CHECK_TYPELOAD (klass);
6760                         mono_class_init (klass);
6761                         ins->cil_code = ip;
6762
6763                         loc = mono_compile_create_var (cfg, &mono_defaults.typed_reference_class->byval_arg, OP_LOCAL);
6764                         NEW_TEMPLOADA (cfg, ins->inst_right, loc->inst_c0);
6765
6766                         NEW_PCONST (cfg, klassconst, klass);
6767                         NEW_GROUP (cfg, ins->inst_left, *sp, klassconst);
6768                         
6769                         MONO_ADD_INS (bblock, ins);
6770
6771                         NEW_TEMPLOAD (cfg, *sp, loc->inst_c0);
6772                         ++sp;
6773                         ip += 5;
6774                         break;
6775                 }
6776                 case CEE_LDTOKEN: {
6777                         gpointer handle;
6778                         MonoClass *handle_class;
6779
6780                         CHECK_STACK_OVF (1);
6781
6782                         CHECK_OPSIZE (5);
6783                         n = read32 (ip + 1);
6784
6785                         if (method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD) {
6786                                 handle = mono_method_get_wrapper_data (method, n);
6787                                 handle_class = mono_method_get_wrapper_data (method, n + 1);
6788                                 if (handle_class == mono_defaults.typehandle_class)
6789                                         handle = &((MonoClass*)handle)->byval_arg;
6790                         }
6791                         else {
6792                                 handle = mono_ldtoken (image, n, &handle_class, generic_context);
6793                         }
6794                         if (!handle)
6795                                 goto load_error;
6796                         mono_class_init (handle_class);
6797
6798                         if (cfg->opt & MONO_OPT_SHARED) {
6799                                 int temp;
6800                                 MonoInst *res, *store, *addr, *vtvar, *iargs [3];
6801
6802                                 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL); 
6803
6804                                 NEW_IMAGECONST (cfg, iargs [0], image);
6805                                 NEW_ICONST (cfg, iargs [1], n);
6806                                 NEW_PCONST (cfg, iargs [2], generic_context);
6807                                 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
6808                                 NEW_TEMPLOAD (cfg, res, temp);
6809                                 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
6810                                 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
6811                                 MONO_ADD_INS (bblock, store);
6812                                 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
6813                         } else {
6814                                 if ((ip [5] == CEE_CALL) && (cmethod = mini_get_method (method, read32 (ip + 6), NULL, generic_context)) &&
6815                                                 (cmethod->klass == mono_defaults.monotype_class->parent) &&
6816                                                 (strcmp (cmethod->name, "GetTypeFromHandle") == 0) && ip_in_bb (cfg, bblock, ip + 5)) {
6817                                         MonoClass *tclass = mono_class_from_mono_type (handle);
6818                                         mono_class_init (tclass);
6819                                         if (cfg->compile_aot)
6820                                                 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, image, n);
6821                                         else
6822                                                 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
6823                                         ins->type = STACK_OBJ;
6824                                         ins->klass = cmethod->klass;
6825                                         ip += 5;
6826                                 } else {
6827                                         MonoInst *store, *addr, *vtvar;
6828
6829                                         if (cfg->compile_aot)
6830                                                 NEW_LDTOKENCONST (cfg, ins, image, n);
6831                                         else
6832                                                 NEW_PCONST (cfg, ins, handle);
6833                                         vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
6834                                         NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
6835                                         NEW_INDSTORE (cfg, store, addr, ins, &mono_defaults.int_class->byval_arg);
6836                                         MONO_ADD_INS (bblock, store);
6837                                         NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
6838                                 }
6839                         }
6840
6841                         *sp++ = ins;
6842                         ip += 5;
6843                         break;
6844                 }
6845                 case CEE_CONV_U2:
6846                 case CEE_CONV_U1:
6847                 case CEE_CONV_I:
6848                         CHECK_STACK (1);
6849                         ADD_UNOP (*ip);
6850                         ip++;
6851                         break;
6852                 case CEE_ADD_OVF:
6853                 case CEE_ADD_OVF_UN:
6854                 case CEE_MUL_OVF:
6855                 case CEE_MUL_OVF_UN:
6856                 case CEE_SUB_OVF:
6857                 case CEE_SUB_OVF_UN:
6858                         CHECK_STACK (2);
6859                         ADD_BINOP (*ip);
6860                         if (mono_find_jit_opcode_emulation (ins->opcode)) {
6861                                 --sp;
6862                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 1);
6863                         }
6864                         ip++;
6865                         break;
6866                 case CEE_ENDFINALLY:
6867                         MONO_INST_NEW (cfg, ins, OP_ENDFINALLY);
6868                         MONO_ADD_INS (bblock, ins);
6869                         ins->cil_code = ip++;
6870                         start_new_bblock = 1;
6871
6872                         /*
6873                          * Control will leave the method so empty the stack, otherwise
6874                          * the next basic block will start with a nonempty stack.
6875                          */
6876                         while (sp != stack_start) {
6877                                 MONO_INST_NEW (cfg, ins, CEE_POP);
6878                                 ins->cil_code = ip;
6879                                 sp--;
6880                                 ins->inst_i0 = *sp;
6881                                 MONO_ADD_INS (bblock, ins);
6882                         }
6883                         break;
6884                 case CEE_LEAVE:
6885                 case CEE_LEAVE_S: {
6886                         GList *handlers;
6887
6888                         if (*ip == CEE_LEAVE) {
6889                                 CHECK_OPSIZE (5);
6890                                 target = ip + 5 + (gint32)read32(ip + 1);
6891                         } else {
6892                                 CHECK_OPSIZE (2);
6893                                 target = ip + 2 + (signed char)(ip [1]);
6894                         }
6895
6896                         /* empty the stack */
6897                         while (sp != stack_start) {
6898                                 MONO_INST_NEW (cfg, ins, CEE_POP);
6899                                 ins->cil_code = ip;
6900                                 sp--;
6901                                 ins->inst_i0 = *sp;
6902                                 MONO_ADD_INS (bblock, ins);
6903                         }
6904
6905                         /* 
6906                          * If this leave statement is in a catch block, check for a
6907                          * pending exception, and rethrow it if necessary.
6908                          */
6909                         for (i = 0; i < header->num_clauses; ++i) {
6910                                 MonoExceptionClause *clause = &header->clauses [i];
6911
6912                                 /* 
6913                                  * Use <= in the final comparison to handle clauses with multiple
6914                                  * leave statements, like in bug #78024.
6915                                  * The ordering of the exception clauses guarantees that we find the
6916                                  * innermost clause.
6917                                  */
6918                                 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)) {
6919                                         int temp;
6920                                         MonoInst *load;
6921
6922                                         NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, clause->handler_offset)->inst_c0);
6923                                         load->cil_code = ip;
6924
6925                                         temp = mono_emit_jit_icall (cfg, bblock, mono_thread_get_pending_exception, NULL, ip);
6926                                         NEW_TEMPLOAD (cfg, *sp, temp);
6927                                 
6928                                         MONO_INST_NEW (cfg, ins, OP_THROW_OR_NULL);
6929                                         ins->inst_left = *sp;
6930                                         ins->inst_right = load;
6931                                         ins->cil_code = ip;
6932                                         MONO_ADD_INS (bblock, ins);
6933                                 }
6934                         }
6935
6936                         if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
6937                                 GList *tmp;
6938                                 for (tmp = handlers; tmp; tmp = tmp->next) {
6939                                         tblock = tmp->data;
6940                                         link_bblock (cfg, bblock, tblock);
6941                                         MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
6942                                         ins->cil_code = ip;
6943                                         ins->inst_target_bb = tblock;
6944                                         MONO_ADD_INS (bblock, ins);
6945                                 }
6946                                 g_list_free (handlers);
6947                         } 
6948
6949                         MONO_INST_NEW (cfg, ins, OP_BR);
6950                         ins->cil_code = ip;
6951                         MONO_ADD_INS (bblock, ins);
6952                         GET_BBLOCK (cfg, tblock, target);
6953                         link_bblock (cfg, bblock, tblock);
6954                         CHECK_BBLOCK (target, ip, tblock);
6955                         ins->inst_target_bb = tblock;
6956                         start_new_bblock = 1;
6957
6958                         if (*ip == CEE_LEAVE)
6959                                 ip += 5;
6960                         else
6961                                 ip += 2;
6962
6963                         break;
6964                 }
6965                 case CEE_STIND_I:
6966                         CHECK_STACK (2);
6967                         MONO_INST_NEW (cfg, ins, *ip);
6968                         sp -= 2;
6969                         handle_loaded_temps (cfg, bblock, stack_start, sp);
6970                         MONO_ADD_INS (bblock, ins);
6971                         ins->cil_code = ip++;
6972                         ins->inst_i0 = sp [0];
6973                         ins->inst_i1 = sp [1];
6974                         inline_costs += 1;
6975                         break;
6976                 case CEE_CONV_U:
6977                         CHECK_STACK (1);
6978                         ADD_UNOP (*ip);
6979                         ip++;
6980                         break;
6981                 /* trampoline mono specific opcodes */
6982                 case MONO_CUSTOM_PREFIX: {
6983
6984                         g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
6985
6986                         CHECK_OPSIZE (2);
6987                         switch (ip [1]) {
6988
6989                         case CEE_MONO_ICALL: {
6990                                 int temp;
6991                                 gpointer func;
6992                                 MonoJitICallInfo *info;
6993
6994                                 token = read32 (ip + 2);
6995                                 func = mono_method_get_wrapper_data (method, token);
6996                                 info = mono_find_jit_icall_by_addr (func);
6997                                 if (info == NULL){
6998                                         g_error ("An attempt has been made to perform an icall to address %p, "
6999                                                  "but the address has not been registered as an icall\n", info);
7000                                         g_assert_not_reached ();
7001                                 }
7002
7003                                 CHECK_STACK (info->sig->param_count);
7004                                 sp -= info->sig->param_count;
7005
7006                                 temp = mono_emit_jit_icall (cfg, bblock, info->func, sp, ip);
7007                                 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
7008                                         NEW_TEMPLOAD (cfg, *sp, temp);
7009                                         sp++;
7010                                 }
7011
7012                                 ip += 6;
7013                                 inline_costs += 10 * num_calls++;
7014
7015                                 break;
7016                         }
7017                         case CEE_MONO_LDPTR:
7018                                 CHECK_STACK_OVF (1);
7019                                 CHECK_OPSIZE (6);
7020                                 token = read32 (ip + 2);
7021                                 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
7022                                 ins->cil_code = ip;
7023                                 *sp++ = ins;
7024                                 ip += 6;
7025                                 inline_costs += 10 * num_calls++;
7026                                 /* Can't embed random pointers into AOT code */
7027                                 cfg->disable_aot = 1;
7028                                 break;
7029                         case CEE_MONO_VTADDR:
7030                                 CHECK_STACK (1);
7031                                 --sp;
7032                                 MONO_INST_NEW (cfg, ins, OP_VTADDR);
7033                                 ins->cil_code = ip;
7034                                 ins->type = STACK_MP;
7035                                 ins->inst_left = *sp;
7036                                 *sp++ = ins;
7037                                 ip += 2;
7038                                 break;
7039                         case CEE_MONO_NEWOBJ: {
7040                                 MonoInst *iargs [2];
7041                                 int temp;
7042                                 CHECK_STACK_OVF (1);
7043                                 CHECK_OPSIZE (6);
7044                                 token = read32 (ip + 2);
7045                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
7046                                 mono_class_init (klass);
7047                                 NEW_DOMAINCONST (cfg, iargs [0]);
7048                                 NEW_CLASSCONST (cfg, iargs [1], klass);
7049                                 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
7050                                 NEW_TEMPLOAD (cfg, *sp, temp);
7051                                 sp++;
7052                                 ip += 6;
7053                                 inline_costs += 10 * num_calls++;
7054                                 break;
7055                         }
7056                         case CEE_MONO_OBJADDR:
7057                                 CHECK_STACK (1);
7058                                 --sp;
7059                                 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
7060                                 ins->cil_code = ip;
7061                                 ins->type = STACK_MP;
7062                                 ins->inst_left = *sp;
7063                                 *sp++ = ins;
7064                                 ip += 2;
7065                                 break;
7066                         case CEE_MONO_LDNATIVEOBJ:
7067                                 CHECK_STACK (1);
7068                                 CHECK_OPSIZE (6);
7069                                 token = read32 (ip + 2);
7070                                 klass = mono_method_get_wrapper_data (method, token);
7071                                 g_assert (klass->valuetype);
7072                                 mono_class_init (klass);
7073                                 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
7074                                 sp [-1] = ins;
7075                                 ip += 6;
7076                                 break;
7077                         case CEE_MONO_RETOBJ:
7078                                 g_assert (cfg->ret);
7079                                 g_assert (mono_method_signature (method)->pinvoke); 
7080                                 CHECK_STACK (1);
7081                                 --sp;
7082                                 
7083                                 CHECK_OPSIZE (6);
7084                                 token = read32 (ip + 2);    
7085                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
7086
7087                                 NEW_RETLOADA (cfg, ins);
7088                                 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE, FALSE);
7089                                 
7090                                 if (sp != stack_start)
7091                                         UNVERIFIED;
7092                                 
7093                                 MONO_INST_NEW (cfg, ins, OP_BR);
7094                                 ins->cil_code = ip;
7095                                 ins->inst_target_bb = end_bblock;
7096                                 MONO_ADD_INS (bblock, ins);
7097                                 link_bblock (cfg, bblock, end_bblock);
7098                                 start_new_bblock = 1;
7099                                 ip += 6;
7100                                 break;
7101                         case CEE_MONO_CISINST:
7102                         case CEE_MONO_CCASTCLASS: {
7103                                 int token;
7104                                 CHECK_STACK (1);
7105                                 --sp;
7106                                 CHECK_OPSIZE (6);
7107                                 token = read32 (ip + 2);
7108                                 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
7109                                 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_CISINST) ? OP_CISINST : OP_CCASTCLASS);
7110                                 ins->type = STACK_I4;
7111                                 ins->inst_left = *sp;
7112                                 ins->inst_newa_class = klass;
7113                                 ins->cil_code = ip;
7114                                 *sp++ = emit_tree (cfg, bblock, ins, ip + 6);
7115                                 ip += 6;
7116                                 break;
7117                         }
7118                         case CEE_MONO_SAVE_LMF:
7119                         case CEE_MONO_RESTORE_LMF:
7120 #ifdef MONO_ARCH_HAVE_LMF_OPS
7121                                 MONO_INST_NEW (cfg, ins, (ip [1] == CEE_MONO_SAVE_LMF) ? OP_SAVE_LMF : OP_RESTORE_LMF);
7122                                 MONO_ADD_INS (bblock, ins);
7123                                 cfg->need_lmf_area = TRUE;
7124 #endif
7125                                 ip += 2;
7126                                 break;
7127                         case CEE_MONO_CLASSCONST:
7128                                 CHECK_STACK_OVF (1);
7129                                 CHECK_OPSIZE (6);
7130                                 token = read32 (ip + 2);
7131                                 NEW_CLASSCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
7132                                 ins->cil_code = ip;
7133                                 *sp++ = ins;
7134                                 ip += 6;
7135                                 inline_costs += 10 * num_calls++;
7136                                 break;
7137                         case CEE_MONO_NOT_TAKEN:
7138                                 bblock->out_of_line = TRUE;
7139                                 ip += 2;
7140                                 break;
7141                         default:
7142                                 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
7143                                 break;
7144                         }
7145                         break;
7146                 }
7147                 case CEE_PREFIX1: {
7148                         CHECK_OPSIZE (2);
7149                         switch (ip [1]) {
7150                         case CEE_ARGLIST: {
7151                                 /* somewhat similar to LDTOKEN */
7152                                 MonoInst *addr, *vtvar;
7153                                 CHECK_STACK_OVF (1);
7154                                 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL); 
7155
7156                                 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
7157                                 addr->cil_code = ip;
7158                                 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
7159                                 ins->cil_code = ip;
7160                                 ins->inst_left = addr;
7161                                 MONO_ADD_INS (bblock, ins);
7162                                 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
7163                                 ins->cil_code = ip;
7164                                 *sp++ = ins;
7165                                 ip += 2;
7166                                 break;
7167                         }
7168                         case CEE_CEQ:
7169                         case CEE_CGT:
7170                         case CEE_CGT_UN:
7171                         case CEE_CLT:
7172                         case CEE_CLT_UN: {
7173                                 MonoInst *cmp;
7174                                 CHECK_STACK (2);
7175                                 /*
7176                                  * The following transforms:
7177                                  *    CEE_CEQ    into OP_CEQ
7178                                  *    CEE_CGT    into OP_CGT
7179                                  *    CEE_CGT_UN into OP_CGT_UN
7180                                  *    CEE_CLT    into OP_CLT
7181                                  *    CEE_CLT_UN into OP_CLT_UN
7182                                  */
7183                                 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
7184                                 
7185                                 MONO_INST_NEW (cfg, ins, cmp->opcode);
7186                                 sp -= 2;
7187                                 cmp->inst_i0 = sp [0];
7188                                 cmp->inst_i1 = sp [1];
7189                                 cmp->cil_code = ip;
7190                                 type_from_op (cmp);
7191                                 CHECK_TYPE (cmp);
7192                                 ins->cil_code = ip;
7193                                 ins->type = STACK_I4;
7194                                 ins->inst_i0 = cmp;
7195 #if MONO_ARCH_SOFT_FLOAT
7196                                 if (sp [0]->type == STACK_R8) {
7197                                         cmp->type = STACK_I4;
7198                                         *sp++ = emit_tree (cfg, bblock, cmp, ip + 2);
7199                                         ip += 2;
7200                                         break;
7201                                 }
7202 #endif
7203                                 if ((sp [0]->type == STACK_I8) || ((sizeof (gpointer) == 8) && ((sp [0]->type == STACK_PTR) || (sp [0]->type == STACK_OBJ) || (sp [0]->type == STACK_MP))))
7204                                         cmp->opcode = OP_LCOMPARE;
7205                                 else
7206                                         cmp->opcode = OP_COMPARE;
7207                                 *sp++ = ins;
7208                                 /* spill it to reduce the expression complexity
7209                                  * and workaround bug 54209 
7210                                  */
7211                                 if (cmp->inst_left->type == STACK_I8) {
7212                                         --sp;
7213                                         *sp++ = emit_tree (cfg, bblock, ins, ip + 2);
7214                                 }
7215                                 ip += 2;
7216                                 break;
7217                         }
7218                         case CEE_LDFTN: {
7219                                 MonoInst *argconst;
7220                                 int temp;
7221
7222                                 CHECK_STACK_OVF (1);
7223                                 CHECK_OPSIZE (6);
7224                                 n = read32 (ip + 2);
7225                                 cmethod = mini_get_method (method, n, NULL, generic_context);
7226                                 if (!cmethod)
7227                                         goto load_error;
7228                                 mono_class_init (cmethod->klass);
7229
7230                                 if (mono_use_security_manager) {
7231                                         if (check_linkdemand (cfg, method, cmethod, bblock, ip))
7232                                                 INLINE_FAILURE;
7233                                 }
7234
7235                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
7236
7237                                 NEW_METHODCONST (cfg, argconst, cmethod);
7238                                 if (method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
7239                                         temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
7240                                 else
7241                                         temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn_nosync, &argconst, ip);
7242                                 NEW_TEMPLOAD (cfg, *sp, temp);
7243                                 sp ++;
7244                                 
7245                                 ip += 6;
7246                                 inline_costs += 10 * num_calls++;
7247                                 break;
7248                         }
7249                         case CEE_LDVIRTFTN: {
7250                                 MonoInst *args [2];
7251                                 int temp;
7252
7253                                 CHECK_STACK (1);
7254                                 CHECK_OPSIZE (6);
7255                                 n = read32 (ip + 2);
7256                                 cmethod = mini_get_method (method, n, NULL, generic_context);
7257                                 if (!cmethod)
7258                                         goto load_error;
7259                                 mono_class_init (cmethod->klass);
7260
7261                                 if (mono_use_security_manager) {
7262                                         if (check_linkdemand (cfg, method, cmethod, bblock, ip))
7263                                                 INLINE_FAILURE;
7264                                 }
7265
7266                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
7267
7268                                 --sp;
7269                                 args [0] = *sp;
7270                                 NEW_METHODCONST (cfg, args [1], cmethod);
7271                                 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
7272                                 NEW_TEMPLOAD (cfg, *sp, temp);
7273                                 sp ++;
7274
7275                                 ip += 6;
7276                                 inline_costs += 10 * num_calls++;
7277                                 break;
7278                         }
7279                         case CEE_LDARG:
7280                                 CHECK_STACK_OVF (1);
7281                                 CHECK_OPSIZE (4);
7282                                 n = read16 (ip + 2);
7283                                 CHECK_ARG (n);
7284                                 NEW_ARGLOAD (cfg, ins, n);
7285                                 LDARG_SOFT_FLOAT (cfg, ins, n, ip);
7286                                 ins->cil_code = ip;
7287                                 *sp++ = ins;
7288                                 ip += 4;
7289                                 break;
7290                         case CEE_LDARGA:
7291                                 CHECK_STACK_OVF (1);
7292                                 CHECK_OPSIZE (4);
7293                                 n = read16 (ip + 2);
7294                                 CHECK_ARG (n);
7295                                 NEW_ARGLOADA (cfg, ins, n);
7296                                 ins->cil_code = ip;
7297                                 *sp++ = ins;
7298                                 ip += 4;
7299                                 break;
7300                         case CEE_STARG:
7301                                 CHECK_STACK (1);
7302                                 --sp;
7303                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
7304                                 CHECK_OPSIZE (4);
7305                                 n = read16 (ip + 2);
7306                                 CHECK_ARG (n);
7307                                 NEW_ARGSTORE (cfg, ins, n, *sp);
7308                                 ins->cil_code = ip;
7309                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, param_types [n], *sp))
7310                                         UNVERIFIED;
7311                                 STARG_SOFT_FLOAT (cfg, ins, n, ip);
7312                                 if (ins->opcode == CEE_STOBJ) {
7313                                         NEW_ARGLOADA (cfg, ins, n);
7314                                         handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
7315                                 } else
7316                                         MONO_ADD_INS (bblock, ins);
7317                                 ip += 4;
7318                                 break;
7319                         case CEE_LDLOC:
7320                                 CHECK_STACK_OVF (1);
7321                                 CHECK_OPSIZE (4);
7322                                 n = read16 (ip + 2);
7323                                 CHECK_LOCAL (n);
7324                                 NEW_LOCLOAD (cfg, ins, n);
7325                                 LDLOC_SOFT_FLOAT (cfg, ins, n, ip);
7326                                 ins->cil_code = ip;
7327                                 *sp++ = ins;
7328                                 ip += 4;
7329                                 break;
7330                         case CEE_LDLOCA:
7331                                 CHECK_STACK_OVF (1);
7332                                 CHECK_OPSIZE (4);
7333                                 n = read16 (ip + 2);
7334                                 CHECK_LOCAL (n);
7335                                 NEW_LOCLOADA (cfg, ins, n);
7336                                 ins->cil_code = ip;
7337                                 *sp++ = ins;
7338                                 ip += 4;
7339                                 break;
7340                         case CEE_STLOC:
7341                                 CHECK_STACK (1);
7342                                 --sp;
7343                                 CHECK_OPSIZE (4);
7344                                 n = read16 (ip + 2);
7345                                 CHECK_LOCAL (n);
7346                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
7347                                 NEW_LOCSTORE (cfg, ins, n, *sp);
7348                                 if (!dont_verify_stloc && target_type_is_incompatible (cfg, header->locals [n], *sp))
7349                                         UNVERIFIED;
7350                                 ins->cil_code = ip;
7351                                 STLOC_SOFT_FLOAT (cfg, ins, n, ip);
7352                                 if (ins->opcode == CEE_STOBJ) {
7353                                         NEW_LOCLOADA (cfg, ins, n);
7354                                         handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE, FALSE);
7355                                 } else
7356                                         MONO_ADD_INS (bblock, ins);
7357                                 ip += 4;
7358                                 inline_costs += 1;
7359                                 break;
7360                         case CEE_LOCALLOC:
7361                                 CHECK_STACK (1);
7362                                 --sp;
7363                                 if (sp != stack_start) 
7364                                         UNVERIFIED;
7365                                 if (cfg->method != method) 
7366                                         /* 
7367                                          * Inlining this into a loop in a parent could lead to 
7368                                          * stack overflows which is different behavior than the
7369                                          * non-inlined case, thus disable inlining in this case.
7370                                          */
7371                                         goto inline_failure;
7372                                 MONO_INST_NEW (cfg, ins, OP_LOCALLOC);
7373                                 ins->inst_left = *sp;
7374                                 ins->cil_code = ip;
7375                                 ins->type = STACK_PTR;
7376
7377                                 cfg->flags |= MONO_CFG_HAS_ALLOCA;
7378                                 if (header->init_locals)
7379                                         ins->flags |= MONO_INST_INIT;
7380
7381                                 *sp++ = ins;
7382                                 ip += 2;
7383                                 /* FIXME: set init flag if locals init is set in this method */
7384                                 break;
7385                         case CEE_ENDFILTER: {
7386                                 MonoExceptionClause *clause, *nearest;
7387                                 int cc, nearest_num;
7388
7389                                 CHECK_STACK (1);
7390                                 --sp;
7391                                 if ((sp != stack_start) || (sp [0]->type != STACK_I4)) 
7392                                         UNVERIFIED;
7393                                 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
7394                                 ins->inst_left = *sp;
7395                                 ins->cil_code = ip;
7396                                 MONO_ADD_INS (bblock, ins);
7397                                 start_new_bblock = 1;
7398                                 ip += 2;
7399
7400                                 nearest = NULL;
7401                                 nearest_num = 0;
7402                                 for (cc = 0; cc < header->num_clauses; ++cc) {
7403                                         clause = &header->clauses [cc];
7404                                         if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
7405                                                 ((ip - header->code) > clause->data.filter_offset && (ip - header->code) <= clause->handler_offset) &&
7406                                             (!nearest || (clause->data.filter_offset < nearest->data.filter_offset))) {
7407                                                 nearest = clause;
7408                                                 nearest_num = cc;
7409                                         }
7410                                 }
7411                                 g_assert (nearest);
7412                                 if ((ip - header->code) != nearest->handler_offset)
7413                                         UNVERIFIED;
7414
7415                                 break;
7416                         }
7417                         case CEE_UNALIGNED_:
7418                                 ins_flag |= MONO_INST_UNALIGNED;
7419                                 /* FIXME: record alignment? we can assume 1 for now */
7420                                 CHECK_OPSIZE (3);
7421                                 ip += 3;
7422                                 break;
7423                         case CEE_VOLATILE_:
7424                                 ins_flag |= MONO_INST_VOLATILE;
7425                                 ip += 2;
7426                                 break;
7427                         case CEE_TAIL_:
7428                                 ins_flag   |= MONO_INST_TAILCALL;
7429                                 cfg->flags |= MONO_CFG_HAS_TAIL;
7430                                 /* Can't inline tail calls at this time */
7431                                 inline_costs += 100000;
7432                                 ip += 2;
7433                                 break;
7434                         case CEE_INITOBJ:
7435                                 CHECK_STACK (1);
7436                                 --sp;
7437                                 CHECK_OPSIZE (6);
7438                                 token = read32 (ip + 2);
7439                                 klass = mini_get_class (method, token, generic_context);
7440                                 CHECK_TYPELOAD (klass);
7441                                 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
7442                                         MonoInst *store, *load;
7443                                         NEW_PCONST (cfg, load, NULL);
7444                                         load->cil_code = ip;
7445                                         load->type = STACK_OBJ;
7446                                         load->klass = klass;
7447                                         MONO_INST_NEW (cfg, store, CEE_STIND_REF);
7448                                         store->cil_code = ip;
7449                                         handle_loaded_temps (cfg, bblock, stack_start, sp);
7450                                         MONO_ADD_INS (bblock, store);
7451                                         store->inst_i0 = sp [0];
7452                                         store->inst_i1 = load;
7453                                 } else {
7454                                         handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
7455                                 }
7456                                 ip += 6;
7457                                 inline_costs += 1;
7458                                 break;
7459                         case CEE_CONSTRAINED_:
7460                                 /* FIXME: implement */
7461                                 CHECK_OPSIZE (6);
7462                                 token = read32 (ip + 2);
7463                                 constrained_call = mono_class_get_full (image, token, generic_context);
7464                                 CHECK_TYPELOAD (constrained_call);
7465                                 ip += 6;
7466                                 break;
7467                         case CEE_CPBLK:
7468                         case CEE_INITBLK: {
7469                                 MonoInst *iargs [3];
7470                                 CHECK_STACK (3);
7471                                 sp -= 3;
7472                                 if ((cfg->opt & MONO_OPT_INTRINS) && (ip [1] == CEE_CPBLK) && (sp [2]->opcode == OP_ICONST) && ((n = sp [2]->inst_c0) <= sizeof (gpointer) * 5)) {
7473                                         MonoInst *copy;
7474                                         MONO_INST_NEW (cfg, copy, OP_MEMCPY);
7475                                         copy->inst_left = sp [0];
7476                                         copy->inst_right = sp [1];
7477                                         copy->cil_code = ip;
7478                                         copy->backend.size = n;
7479                                         MONO_ADD_INS (bblock, copy);
7480                                         ip += 2;
7481                                         break;
7482                                 }
7483                                 iargs [0] = sp [0];
7484                                 iargs [1] = sp [1];
7485                                 iargs [2] = sp [2];
7486                                 handle_loaded_temps (cfg, bblock, stack_start, sp);
7487                                 if (ip [1] == CEE_CPBLK) {
7488                                         MonoMethod *memcpy_method = get_memcpy_method ();
7489                                         mono_emit_method_call_spilled (cfg, bblock, memcpy_method, memcpy_method->signature, iargs, ip, NULL);
7490                                 } else {
7491                                         MonoMethod *memset_method = get_memset_method ();
7492                                         mono_emit_method_call_spilled (cfg, bblock, memset_method, memset_method->signature, iargs, ip, NULL);
7493                                 }
7494                                 ip += 2;
7495                                 inline_costs += 1;
7496                                 break;
7497                         }
7498                         case CEE_NO_:
7499                                 CHECK_OPSIZE (3);
7500                                 if (ip [2] & 0x1)
7501                                         ins_flag |= MONO_INST_NOTYPECHECK;
7502                                 if (ip [2] & 0x2)
7503                                         ins_flag |= MONO_INST_NORANGECHECK;
7504                                 /* we ignore the no-nullcheck for now since we
7505                                  * really do it explicitly only when doing callvirt->call
7506                                  */
7507                                 ip += 3;
7508                                 break;
7509                         case CEE_RETHROW: {
7510                                 MonoInst *load;
7511                                 int handler_offset = -1;
7512
7513                                 for (i = 0; i < header->num_clauses; ++i) {
7514                                         MonoExceptionClause *clause = &header->clauses [i];
7515                                         if (MONO_OFFSET_IN_HANDLER (clause, ip - header->code) && !(clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY))
7516                                                 handler_offset = clause->handler_offset;
7517                                 }
7518
7519                                 bblock->flags |= BB_EXCEPTION_UNSAFE;
7520
7521                                 g_assert (handler_offset != -1);
7522
7523                                 NEW_TEMPLOAD (cfg, load, mono_find_exvar_for_offset (cfg, handler_offset)->inst_c0);
7524                                 load->cil_code = ip;
7525                                 MONO_INST_NEW (cfg, ins, OP_RETHROW);
7526                                 ins->inst_left = load;
7527                                 ins->cil_code = ip;
7528                                 MONO_ADD_INS (bblock, ins);
7529                                 sp = stack_start;
7530                                 link_bblock (cfg, bblock, end_bblock);
7531                                 start_new_bblock = 1;
7532                                 ip += 2;
7533                                 break;
7534                         }
7535                         case CEE_SIZEOF:
7536                                 CHECK_STACK_OVF (1);
7537                                 CHECK_OPSIZE (6);
7538                                 token = read32 (ip + 2);
7539                                 /* FIXXME: handle generics. */
7540                                 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
7541                                         MonoType *type = mono_type_create_from_typespec (image, token);
7542                                         token = mono_type_size (type, &ialign);
7543                                 } else {
7544                                         MonoClass *klass = mono_class_get_full (image, token, generic_context);
7545                                         CHECK_TYPELOAD (klass);
7546                                         mono_class_init (klass);
7547                                         token = mono_class_value_size (klass, &align);
7548                                 }
7549                                 NEW_ICONST (cfg, ins, token);
7550                                 ins->cil_code = ip;
7551                                 *sp++= ins;
7552                                 ip += 6;
7553                                 break;
7554                         case CEE_REFANYTYPE:
7555                                 CHECK_STACK (1);
7556                                 MONO_INST_NEW (cfg, ins, OP_REFANYTYPE);
7557                                 --sp;
7558                                 ins->type = STACK_MP;
7559                                 ins->inst_left = *sp;
7560                                 ins->type = STACK_VTYPE;
7561                                 ins->klass = mono_defaults.typehandle_class;
7562                                 ins->cil_code = ip;
7563                                 ip += 2;
7564                                 *sp++ = ins;
7565                                 break;
7566                         case CEE_READONLY_:
7567                                 ip += 2;
7568                                 break;
7569                         default:
7570                                 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
7571                         }
7572                         break;
7573                 }
7574                 default:
7575                         g_error ("opcode 0x%02x not handled", *ip);
7576                 }
7577         }
7578         if (start_new_bblock != 1)
7579                 UNVERIFIED;
7580
7581         bblock->cil_length = ip - bblock->cil_code;
7582         bblock->next_bb = end_bblock;
7583
7584         if (cfg->method == method && cfg->domainvar) {
7585                 MonoInst *store;
7586                 MonoInst *get_domain;
7587                 
7588                 if (! (get_domain = mono_arch_get_domain_intrinsic (cfg))) {
7589                         MonoCallInst *call;
7590                         
7591                         MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
7592                         call->signature = helper_sig_domain_get;
7593                         call->inst.type = STACK_PTR;
7594                         call->fptr = mono_domain_get;
7595                         get_domain = (MonoInst*)call;
7596                 }
7597                 
7598                 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, get_domain);
7599                 MONO_ADD_INS (init_localsbb, store);
7600         }
7601
7602         if (cfg->method == method && cfg->got_var)
7603                 mono_emit_load_got_addr (cfg);
7604
7605         if (header->init_locals) {
7606                 MonoInst *store;
7607                 for (i = 0; i < header->num_locals; ++i) {
7608                         MonoType *ptype = header->locals [i];
7609                         int t = ptype->type;
7610                         if (t == MONO_TYPE_VALUETYPE && ptype->data.klass->enumtype)
7611                                 t = ptype->data.klass->enum_basetype->type;
7612                         if (ptype->byref) {
7613                                 NEW_PCONST (cfg, ins, NULL);
7614                                 NEW_LOCSTORE (cfg, store, i, ins);
7615                                 MONO_ADD_INS (init_localsbb, store);
7616                         } else if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
7617                                 NEW_ICONST (cfg, ins, 0);
7618                                 NEW_LOCSTORE (cfg, store, i, ins);
7619                                 MONO_ADD_INS (init_localsbb, store);
7620                         } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
7621                                 MONO_INST_NEW (cfg, ins, OP_I8CONST);
7622                                 ins->type = STACK_I8;
7623                                 ins->inst_l = 0;
7624                                 NEW_LOCSTORE (cfg, store, i, ins);
7625                                 MONO_ADD_INS (init_localsbb, store);
7626                         } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
7627 #ifdef MONO_ARCH_SOFT_FLOAT
7628                                 /* FIXME: handle init of R4 */
7629 #else
7630                                 MONO_INST_NEW (cfg, ins, OP_R8CONST);
7631                                 ins->type = STACK_R8;
7632                                 ins->inst_p0 = (void*)&r8_0;
7633                                 NEW_LOCSTORE (cfg, store, i, ins);
7634                                 MONO_ADD_INS (init_localsbb, store);
7635 #endif
7636                         } else if ((t == MONO_TYPE_VALUETYPE) || (t == MONO_TYPE_TYPEDBYREF) ||
7637                                    ((t == MONO_TYPE_GENERICINST) && mono_type_generic_inst_is_valuetype (ptype))) {
7638                                 NEW_LOCLOADA (cfg, ins, i);
7639                                 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (ptype), NULL, NULL);
7640                         } else {
7641                                 NEW_PCONST (cfg, ins, NULL);
7642                                 NEW_LOCSTORE (cfg, store, i, ins);
7643                                 MONO_ADD_INS (init_localsbb, store);
7644                         }
7645                 }
7646         }
7647
7648         /* resolve backward branches in the middle of an existing basic block */
7649         for (tmp = bb_recheck; tmp; tmp = tmp->next) {
7650                 bblock = tmp->data;
7651                 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
7652                 tblock = find_previous (cfg->cil_offset_to_bb, header->code_size, start_bblock, bblock->cil_code);
7653                 if (tblock != start_bblock) {
7654                         int l;
7655                         split_bblock (cfg, tblock, bblock);
7656                         l = bblock->cil_code - header->code;
7657                         bblock->cil_length = tblock->cil_length - l;
7658                         tblock->cil_length = l;
7659                 } else {
7660                         g_print ("recheck failed.\n");
7661                 }
7662         }
7663
7664         if (cfg->method == method) {
7665                 MonoBasicBlock *bb;
7666                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
7667                         bb->region = mono_find_block_region (cfg, bb->real_offset);
7668                         if (cfg->spvars)
7669                                 mono_create_spvar_for_region (cfg, bb->region);
7670                         if (cfg->verbose_level > 2)
7671                                 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
7672                 }
7673         }
7674
7675         g_slist_free (class_inits);
7676         dont_inline = g_list_remove (dont_inline, method);
7677
7678         if (inline_costs < 0) {
7679                 char *mname;
7680
7681                 /* Method is too large */
7682                 mname = mono_method_full_name (method, TRUE);
7683                 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
7684                 cfg->exception_message = g_strdup_printf ("Method %s is too complex.", mname);
7685                 g_free (mname);
7686                 return -1;
7687         }
7688
7689         return inline_costs;
7690
7691  inline_failure:
7692         g_slist_free (class_inits);
7693         dont_inline = g_list_remove (dont_inline, method);
7694         return -1;
7695
7696  load_error:
7697         g_slist_free (class_inits);
7698         dont_inline = g_list_remove (dont_inline, method);
7699         cfg->exception_type = MONO_EXCEPTION_TYPE_LOAD;
7700         return -1;
7701
7702  unverified:
7703         g_slist_free (class_inits);
7704         dont_inline = g_list_remove (dont_inline, method);
7705         cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
7706         cfg->exception_message = g_strdup_printf ("Invalid IL code in %s: %s\n",
7707                  mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
7708         return -1;
7709 }
7710
7711 void
7712 mono_print_tree (MonoInst *tree) {
7713         int arity;
7714
7715         if (!tree)
7716                 return;
7717
7718         arity = mono_burg_arity [tree->opcode];
7719
7720         printf (" %s%s", arity?"(":"",  mono_inst_name (tree->opcode));
7721
7722         switch (tree->opcode) {
7723         case OP_ICONST:
7724                 printf ("[%d]", (int)tree->inst_c0);
7725                 break;
7726         case OP_I8CONST:
7727                 printf ("[%lld]", (long long)tree->inst_l);
7728                 break;
7729         case OP_R8CONST:
7730                 printf ("[%f]", *(double*)tree->inst_p0);
7731                 break;
7732         case OP_R4CONST:
7733                 printf ("[%f]", *(float*)tree->inst_p0);
7734                 break;
7735         case OP_ARG:
7736         case OP_LOCAL:
7737                 printf ("[%d]", (int)tree->inst_c0);
7738                 break;
7739         case OP_REGOFFSET:
7740                 if (tree->inst_offset < 0)
7741                         printf ("[-0x%x(%s)]", (int)(-tree->inst_offset), mono_arch_regname (tree->inst_basereg));
7742                 else
7743                         printf ("[0x%x(%s)]", (int)(tree->inst_offset), mono_arch_regname (tree->inst_basereg));
7744                 break;
7745         case OP_REGVAR:
7746                 printf ("[%s]", mono_arch_regname (tree->dreg));
7747                 break;
7748         case CEE_NEWARR:
7749                 printf ("[%s]",  tree->inst_newa_class->name);
7750                 mono_print_tree (tree->inst_newa_len);
7751                 break;
7752         case CEE_CALL:
7753         case CEE_CALLVIRT:
7754         case OP_FCALL:
7755         case OP_FCALLVIRT:
7756         case OP_LCALL:
7757         case OP_LCALLVIRT:
7758         case OP_VCALL:
7759         case OP_VCALLVIRT:
7760         case OP_VOIDCALL:
7761         case OP_VOIDCALLVIRT: {
7762                 MonoCallInst *call = (MonoCallInst*)tree;
7763                 if (call->method)
7764                         printf ("[%s]", call->method->name);
7765                 else if (call->fptr) {
7766                         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
7767                         if (info)
7768                                 printf ("[%s]", info->name);
7769                 }
7770                 break;
7771         }
7772         case OP_PHI: {
7773                 int i;
7774                 printf ("[%d (", (int)tree->inst_c0);
7775                 for (i = 0; i < tree->inst_phi_args [0]; i++) {
7776                         if (i)
7777                                 printf (", ");
7778                         printf ("%d", tree->inst_phi_args [i + 1]);
7779                 }
7780                 printf (")]");
7781                 break;
7782         }
7783         case OP_RENAME:
7784         case OP_RETARG:
7785         case OP_NOP:
7786         case OP_JMP:
7787         case OP_BREAK:
7788                 break;
7789         case OP_LOAD_MEMBASE:
7790         case OP_LOADI4_MEMBASE:
7791         case OP_LOADU4_MEMBASE:
7792         case OP_LOADU1_MEMBASE:
7793         case OP_LOADI1_MEMBASE:
7794         case OP_LOADU2_MEMBASE:
7795         case OP_LOADI2_MEMBASE:
7796                 printf ("[%s] <- [%s + 0x%x]", mono_arch_regname (tree->dreg), mono_arch_regname (tree->inst_basereg), (int)tree->inst_offset);
7797                 break;
7798         case OP_BR:
7799         case OP_CALL_HANDLER:
7800                 printf ("[B%d]", tree->inst_target_bb->block_num);
7801                 break;
7802         case CEE_SWITCH:
7803         case CEE_ISINST:
7804         case CEE_CASTCLASS:
7805         case OP_OUTARG:
7806         case OP_CALL_REG:
7807         case OP_FCALL_REG:
7808         case OP_LCALL_REG:
7809         case OP_VCALL_REG:
7810         case OP_VOIDCALL_REG:
7811                 mono_print_tree (tree->inst_left);
7812                 break;
7813         case CEE_BNE_UN:
7814         case CEE_BEQ:
7815         case CEE_BLT:
7816         case CEE_BLT_UN:
7817         case CEE_BGT:
7818         case CEE_BGT_UN:
7819         case CEE_BGE:
7820         case CEE_BGE_UN:
7821         case CEE_BLE:
7822         case CEE_BLE_UN:
7823                 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
7824                 mono_print_tree (tree->inst_left);
7825                 break;
7826         default:
7827                 if (!mono_arch_print_tree(tree, arity)) {
7828                         if (arity) {
7829                                 mono_print_tree (tree->inst_left);
7830                                 if (arity > 1)
7831                                         mono_print_tree (tree->inst_right);
7832                         }
7833                 }
7834                 break;
7835         }
7836
7837         if (arity)
7838                 printf (")");
7839 }
7840
7841 void
7842 mono_print_tree_nl (MonoInst *tree)
7843 {
7844         mono_print_tree (tree);
7845         printf ("\n");
7846 }
7847
7848 static void
7849 create_helper_signature (void)
7850 {
7851         helper_sig_domain_get = mono_create_icall_signature ("ptr");
7852         helper_sig_class_init_trampoline = mono_create_icall_signature ("void");
7853 }
7854
7855 gconstpointer
7856 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
7857 {
7858         char *name;
7859         MonoMethod *wrapper;
7860         gconstpointer trampoline;
7861         MonoDomain *domain = mono_get_root_domain ();
7862         
7863         if (callinfo->wrapper) {
7864                 return callinfo->wrapper;
7865         }
7866
7867         if (callinfo->trampoline)
7868                 return callinfo->trampoline;
7869
7870         /* 
7871          * We use the lock on the root domain instead of the JIT lock to protect 
7872          * callinfo->trampoline, since we do a lot of stuff inside the critical section.
7873          */
7874         mono_domain_lock (domain);
7875
7876         if (callinfo->trampoline) {
7877                 mono_domain_unlock (domain);
7878                 return callinfo->trampoline;
7879         }
7880
7881         name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
7882         wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
7883         g_free (name);
7884
7885         trampoline = mono_create_ftnptr (domain, mono_create_jit_trampoline_in_domain (domain, wrapper));
7886         mono_register_jit_icall_wrapper (callinfo, trampoline);
7887
7888         callinfo->trampoline = trampoline;
7889
7890         mono_domain_unlock (domain);
7891         
7892         return callinfo->trampoline;
7893 }
7894
7895 static void
7896 mono_init_trampolines (void)
7897 {
7898         mono_trampoline_code [MONO_TRAMPOLINE_GENERIC] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
7899         mono_trampoline_code [MONO_TRAMPOLINE_JUMP] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_JUMP);
7900         mono_trampoline_code [MONO_TRAMPOLINE_CLASS_INIT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
7901 #ifdef MONO_ARCH_HAVE_PIC_AOT
7902         mono_trampoline_code [MONO_TRAMPOLINE_AOT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT);
7903         mono_trampoline_code [MONO_TRAMPOLINE_AOT_PLT] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_AOT_PLT);
7904 #endif
7905 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
7906         mono_trampoline_code [MONO_TRAMPOLINE_DELEGATE] = mono_arch_create_trampoline_code (MONO_TRAMPOLINE_DELEGATE);
7907 #endif
7908 }
7909
7910 static void
7911 mono_init_exceptions (void)
7912 {
7913 #ifndef CUSTOM_EXCEPTION_HANDLING
7914         mono_arch_get_restore_context ();
7915         mono_arch_get_call_filter ();
7916         mono_arch_get_throw_exception ();
7917         mono_arch_get_rethrow_exception ();
7918 #endif
7919 }
7920
7921 guint8 *
7922 mono_get_trampoline_code (MonoTrampolineType tramp_type)
7923 {
7924         return mono_trampoline_code [tramp_type];
7925 }
7926
7927 gpointer
7928 mono_create_class_init_trampoline (MonoVTable *vtable)
7929 {
7930         gpointer code, ptr;
7931
7932         /* previously created trampoline code */
7933         mono_domain_lock (vtable->domain);
7934         ptr = 
7935                 g_hash_table_lookup (vtable->domain->class_init_trampoline_hash,
7936                                                                   vtable);
7937         mono_domain_unlock (vtable->domain);
7938         if (ptr)
7939                 return ptr;
7940
7941 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7942         code = mono_arch_create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain, NULL);
7943 #else
7944         code = mono_arch_create_class_init_trampoline (vtable);
7945 #endif
7946
7947         ptr = mono_create_ftnptr (vtable->domain, code);
7948
7949         /* store trampoline address */
7950         mono_domain_lock (vtable->domain);
7951         g_hash_table_insert (vtable->domain->class_init_trampoline_hash,
7952                                                           vtable, ptr);
7953         mono_domain_unlock (vtable->domain);
7954
7955         mono_jit_lock ();
7956         if (!class_init_hash_addr)
7957                 class_init_hash_addr = g_hash_table_new (NULL, NULL);
7958         g_hash_table_insert (class_init_hash_addr, ptr, vtable);
7959         mono_jit_unlock ();
7960
7961         return ptr;
7962 }
7963
7964 gpointer
7965 mono_create_jump_trampoline (MonoDomain *domain, MonoMethod *method, 
7966                                                          gboolean add_sync_wrapper)
7967 {
7968         MonoJitInfo *ji;
7969         gpointer code;
7970 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7971         guint32 code_size;
7972 #endif
7973
7974         if (add_sync_wrapper && method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
7975                 return mono_create_jump_trampoline (domain, mono_marshal_get_synchronized_wrapper (method), FALSE);
7976
7977         code = mono_jit_find_compiled_method (domain, method);
7978         if (code)
7979                 return code;
7980
7981         mono_domain_lock (domain);
7982         code = g_hash_table_lookup (domain->jump_trampoline_hash, method);
7983         mono_domain_unlock (domain);
7984         if (code)
7985                 return code;
7986
7987 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
7988         code = mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get (), &code_size);
7989
7990         mono_domain_lock (domain);
7991         ji = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
7992         mono_domain_unlock (domain);
7993         ji->code_start = code;
7994         ji->code_size = code_size;
7995         ji->method = method;
7996 #else
7997         ji = mono_arch_create_jump_trampoline (method);
7998 #endif
7999
8000         /*
8001          * mono_delegate_ctor needs to find the method metadata from the 
8002          * trampoline address, so we save it here.
8003          */
8004
8005         mono_jit_info_table_add (domain, ji);
8006
8007         mono_domain_lock (domain);
8008         g_hash_table_insert (domain->jump_trampoline_hash, method, ji->code_start);
8009         mono_domain_unlock (domain);
8010
8011         return ji->code_start;
8012 }
8013
8014 static gpointer
8015 mono_create_jit_trampoline_in_domain (MonoDomain *domain, MonoMethod *method)
8016 {
8017         gpointer tramp;
8018
8019         mono_domain_lock (domain);
8020         tramp = g_hash_table_lookup (domain->jit_trampoline_hash, method);
8021         mono_domain_unlock (domain);
8022         if (tramp)
8023                 return tramp;
8024
8025         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
8026                 return mono_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
8027
8028 #ifdef MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE
8029         tramp =  mono_arch_create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, domain, NULL);
8030 #else
8031         tramp = mono_arch_create_jit_trampoline (method);
8032 #endif
8033         
8034         mono_domain_lock (domain);
8035         g_hash_table_insert (domain->jit_trampoline_hash, method, tramp);
8036         mono_domain_unlock (domain);
8037
8038         mono_jit_stats.method_trampolines++;
8039
8040         return tramp;
8041 }       
8042
8043 gpointer
8044 mono_create_jit_trampoline (MonoMethod *method)
8045 {
8046         return mono_create_jit_trampoline_in_domain (mono_domain_get (), method);
8047 }
8048
8049 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
8050 gpointer
8051 mono_create_jit_trampoline_from_token (MonoImage *image, guint32 token)
8052 {
8053         gpointer tramp;
8054
8055         MonoDomain *domain = mono_domain_get ();
8056         guint8 *buf, *start;
8057
8058         mono_domain_lock (domain);
8059         buf = start = mono_code_manager_reserve (domain->code_mp, 2 * sizeof (gpointer));
8060         mono_domain_unlock (domain);
8061
8062         *(gpointer*)(gpointer)buf = image;
8063         buf += sizeof (gpointer);
8064         *(guint32*)(gpointer)buf = token;
8065
8066         tramp = mono_arch_create_specific_trampoline (start, MONO_TRAMPOLINE_AOT, domain, NULL);
8067
8068         mono_jit_stats.method_trampolines++;
8069
8070         return tramp;
8071 }       
8072 #endif
8073
8074 static gpointer
8075 mono_create_delegate_trampoline (MonoClass *klass)
8076 {
8077 #ifdef MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE
8078         MonoDomain *domain = mono_domain_get ();
8079         gpointer code, ptr;
8080         guint32 code_size;
8081
8082         mono_domain_lock (domain);
8083         ptr = g_hash_table_lookup (domain->delegate_trampoline_hash, klass);
8084         mono_domain_unlock (domain);
8085         if (ptr)
8086                 return ptr;
8087
8088     code = mono_arch_create_specific_trampoline (klass, MONO_TRAMPOLINE_DELEGATE, mono_domain_get (), &code_size);
8089
8090         ptr = mono_create_ftnptr (domain, code);
8091
8092         /* store trampoline address */
8093         mono_domain_lock (domain);
8094         g_hash_table_insert (domain->delegate_trampoline_hash,
8095                                                           klass, ptr);
8096         mono_domain_unlock (domain);
8097
8098         return ptr;
8099 #else
8100         return NULL;
8101 #endif
8102 }
8103
8104 MonoVTable*
8105 mono_find_class_init_trampoline_by_addr (gconstpointer addr)
8106 {
8107         MonoVTable *res;
8108
8109         mono_jit_lock ();
8110         if (class_init_hash_addr)
8111                 res = g_hash_table_lookup (class_init_hash_addr, addr);
8112         else
8113                 res = NULL;
8114         mono_jit_unlock ();
8115         return res;
8116 }
8117
8118 static void
8119 mono_dynamic_code_hash_insert (MonoDomain *domain, MonoMethod *method, MonoJitDynamicMethodInfo *ji)
8120 {
8121         if (!domain->dynamic_code_hash)
8122                 domain->dynamic_code_hash = g_hash_table_new (NULL, NULL);
8123         g_hash_table_insert (domain->dynamic_code_hash, method, ji);
8124 }
8125
8126 static MonoJitDynamicMethodInfo*
8127 mono_dynamic_code_hash_lookup (MonoDomain *domain, MonoMethod *method)
8128 {
8129         MonoJitDynamicMethodInfo *res;
8130
8131         if (domain->dynamic_code_hash)
8132                 res = g_hash_table_lookup (domain->dynamic_code_hash, method);
8133         else
8134                 res = NULL;
8135         return res;
8136 }
8137
8138 typedef struct {
8139         MonoClass *vtype;
8140         GList *active;
8141         GSList *slots;
8142 } StackSlotInfo;
8143
8144 static inline GSList*
8145 g_slist_prepend_mempool (MonoMemPool *mp, GSList   *list,
8146                                                  gpointer  data)
8147 {
8148   GSList *new_list;
8149
8150   new_list = mono_mempool_alloc (mp, sizeof (GSList));
8151   new_list->data = data;
8152   new_list->next = list;
8153
8154   return new_list;
8155 }
8156
8157 /*
8158  *  mono_allocate_stack_slots_full:
8159  *
8160  *  Allocate stack slots for all non register allocated variables using a
8161  * linear scan algorithm.
8162  * Returns: an array of stack offsets.
8163  * STACK_SIZE is set to the amount of stack space needed.
8164  * STACK_ALIGN is set to the alignment needed by the locals area.
8165  */
8166 gint32*
8167 mono_allocate_stack_slots_full (MonoCompile *m, gboolean backward, guint32 *stack_size, guint32 *stack_align)
8168 {
8169         int i, slot, offset, size;
8170         guint32 align;
8171         MonoMethodVar *vmv;
8172         MonoInst *inst;
8173         gint32 *offsets;
8174         GList *vars = NULL, *l;
8175         StackSlotInfo *scalar_stack_slots, *vtype_stack_slots, *slot_info;
8176         MonoType *t;
8177         int nvtypes;
8178
8179         scalar_stack_slots = mono_mempool_alloc0 (m->mempool, sizeof (StackSlotInfo) * MONO_TYPE_PINNED);
8180         vtype_stack_slots = NULL;
8181         nvtypes = 0;
8182
8183         offsets = mono_mempool_alloc (m->mempool, sizeof (gint32) * m->num_varinfo);
8184         for (i = 0; i < m->num_varinfo; ++i)
8185                 offsets [i] = -1;
8186
8187         for (i = m->locals_start; i < m->num_varinfo; i++) {
8188                 inst = m->varinfo [i];
8189                 vmv = MONO_VARINFO (m, i);
8190
8191                 if ((inst->flags & MONO_INST_IS_DEAD) || inst->opcode == OP_REGVAR || inst->opcode == OP_REGOFFSET)
8192                         continue;
8193
8194                 vars = g_list_prepend (vars, vmv);
8195         }
8196
8197         vars = mono_varlist_sort (m, vars, 0);
8198         offset = 0;
8199         *stack_align = 0;
8200         for (l = vars; l; l = l->next) {
8201                 vmv = l->data;
8202                 inst = m->varinfo [vmv->idx];
8203
8204                 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
8205                 * pinvoke wrappers when they call functions returning structures */
8206                 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
8207                         size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
8208                 else {
8209                         int ialign;
8210
8211                         size = mono_type_size (inst->inst_vtype, &ialign);
8212                         align = ialign;
8213                 }
8214
8215                 t = mono_type_get_underlying_type (inst->inst_vtype);
8216                 if (t->byref) {
8217                         slot_info = &scalar_stack_slots [MONO_TYPE_I];
8218                 } else {
8219                         switch (t->type) {
8220                         case MONO_TYPE_GENERICINST:
8221                                 if (!mono_type_generic_inst_is_valuetype (t)) {
8222                                         slot_info = &scalar_stack_slots [t->type];
8223                                         break;
8224                                 }
8225                                 /* Fall through */
8226                         case MONO_TYPE_VALUETYPE:
8227                                 if (!vtype_stack_slots)
8228                                         vtype_stack_slots = mono_mempool_alloc0 (m->mempool, sizeof (StackSlotInfo) * 256);
8229                                 for (i = 0; i < nvtypes; ++i)
8230                                         if (t->data.klass == vtype_stack_slots [i].vtype)
8231                                                 break;
8232                                 if (i < nvtypes)
8233                                         slot_info = &vtype_stack_slots [i];
8234                                 else {
8235                                         g_assert (nvtypes < 256);
8236                                         vtype_stack_slots [nvtypes].vtype = t->data.klass;
8237                                         slot_info = &vtype_stack_slots [nvtypes];
8238                                         nvtypes ++;
8239                                 }
8240                                 break;
8241                         case MONO_TYPE_CLASS:
8242                         case MONO_TYPE_OBJECT:
8243                         case MONO_TYPE_ARRAY:
8244                         case MONO_TYPE_SZARRAY:
8245                         case MONO_TYPE_STRING:
8246                         case MONO_TYPE_PTR:
8247                         case MONO_TYPE_I:
8248                         case MONO_TYPE_U:
8249 #if SIZEOF_VOID_P == 4
8250                         case MONO_TYPE_I4:
8251 #else
8252                         case MONO_TYPE_I8:
8253 #endif
8254                                 /* Share non-float stack slots of the same size */
8255                                 slot_info = &scalar_stack_slots [MONO_TYPE_CLASS];
8256                                 break;
8257                         default:
8258                                 slot_info = &scalar_stack_slots [t->type];
8259                         }
8260                 }
8261
8262                 slot = 0xffffff;
8263                 if (m->comp_done & MONO_COMP_LIVENESS) {
8264                         //printf ("START  %2d %08x %08x\n",  vmv->idx, vmv->range.first_use.abs_pos, vmv->range.last_use.abs_pos);
8265                         
8266                         /* expire old intervals in active */
8267                         while (slot_info->active) {
8268                                 MonoMethodVar *amv = (MonoMethodVar *)slot_info->active->data;
8269
8270                                 if (amv->range.last_use.abs_pos > vmv->range.first_use.abs_pos)
8271                                         break;
8272
8273                                 //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);
8274
8275                                 slot_info->active = g_list_delete_link (slot_info->active, slot_info->active);
8276                                 slot_info->slots = g_slist_prepend_mempool (m->mempool, slot_info->slots, GINT_TO_POINTER (offsets [amv->idx]));
8277                         }
8278
8279                         /* 
8280                          * This also handles the case when the variable is used in an
8281                          * exception region, as liveness info is not computed there.
8282                          */
8283                         /* 
8284                          * FIXME: All valuetypes are marked as INDIRECT because of LDADDR
8285                          * opcodes.
8286                          */
8287                         if (! (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
8288                                 if (slot_info->slots) {
8289                                         slot = GPOINTER_TO_INT (slot_info->slots->data);
8290
8291                                         slot_info->slots = slot_info->slots->next;
8292                                 }
8293
8294                                 slot_info->active = mono_varlist_insert_sorted (m, slot_info->active, vmv, TRUE);
8295                         }
8296                 }
8297
8298                 {
8299                         static int count = 0;
8300                         count ++;
8301
8302                         /*
8303                         if (count == atoi (getenv ("COUNT")))
8304                                 printf ("LAST: %s\n", mono_method_full_name (m->method, TRUE));
8305                         if (count > atoi (getenv ("COUNT")))
8306                                 slot = 0xffffff;
8307                         else {
8308                                 mono_print_tree_nl (inst);
8309                                 }
8310                         */
8311                 }
8312                 if (slot == 0xffffff) {
8313                         /*
8314                          * Allways allocate valuetypes to sizeof (gpointer) to allow more
8315                          * efficient copying (and to work around the fact that OP_MEMCPY
8316                          * and OP_MEMSET ignores alignment).
8317                          */
8318                         if (MONO_TYPE_ISSTRUCT (t))
8319                                 align = sizeof (gpointer);
8320
8321                         if (backward) {
8322                                 offset += size;
8323                                 offset += align - 1;
8324                                 offset &= ~(align - 1);
8325                                 slot = offset;
8326                         }
8327                         else {
8328                                 offset += align - 1;
8329                                 offset &= ~(align - 1);
8330                                 slot = offset;
8331                                 offset += size;
8332                         }
8333
8334                         if (*stack_align == 0)
8335                                 *stack_align = align;
8336                 }
8337
8338                 offsets [vmv->idx] = slot;
8339         }
8340         g_list_free (vars);
8341         for (i = 0; i < MONO_TYPE_PINNED; ++i) {
8342                 if (scalar_stack_slots [i].active)
8343                         g_list_free (scalar_stack_slots [i].active);
8344         }
8345         for (i = 0; i < nvtypes; ++i) {
8346                 if (vtype_stack_slots [i].active)
8347                         g_list_free (vtype_stack_slots [i].active);
8348         }
8349
8350         mono_jit_stats.locals_stack_size += offset;
8351
8352         *stack_size = offset;
8353         return offsets;
8354 }
8355
8356 gint32*
8357 mono_allocate_stack_slots (MonoCompile *m, guint32 *stack_size, guint32 *stack_align)
8358 {
8359         return mono_allocate_stack_slots_full (m, TRUE, stack_size, stack_align);
8360 }
8361
8362 void
8363 mono_register_opcode_emulation (int opcode, const char *name, const char *sigstr, gpointer func, gboolean no_throw)
8364 {
8365         MonoJitICallInfo *info;
8366         MonoMethodSignature *sig = mono_create_icall_signature (sigstr);
8367
8368         if (!emul_opcode_map)
8369                 emul_opcode_map = g_new0 (MonoJitICallInfo*, OP_LAST + 1);
8370
8371         g_assert (!sig->hasthis);
8372         g_assert (sig->param_count < 3);
8373
8374         info = mono_register_jit_icall (func, name, sig, no_throw);
8375
8376         emul_opcode_map [opcode] = info;
8377 }
8378
8379 static void
8380 register_icall (gpointer func, const char *name, const char *sigstr, gboolean save)
8381 {
8382         MonoMethodSignature *sig;
8383
8384         if (sigstr)
8385                 sig = mono_create_icall_signature (sigstr);
8386         else
8387                 sig = NULL;
8388
8389         mono_register_jit_icall (func, name, sig, save);
8390 }
8391
8392 static void
8393 decompose_foreach (MonoInst *tree, gpointer data) 
8394 {
8395         static MonoJitICallInfo *newarr_info = NULL;
8396         static MonoJitICallInfo *newarr_specific_info = NULL;
8397         MonoJitICallInfo *info;
8398         int i;
8399
8400         switch (tree->opcode) {
8401         case CEE_NEWARR: {
8402                 MonoCompile *cfg = data;
8403                 MonoInst *iargs [3];
8404
8405                 if (!newarr_info) {
8406                         newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
8407                         g_assert (newarr_info);
8408                         newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
8409                         g_assert (newarr_specific_info);
8410                 }
8411
8412                 if (cfg->opt & MONO_OPT_SHARED) {
8413                         NEW_DOMAINCONST (cfg, iargs [0]);
8414                         NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
8415                         iargs [2] = tree->inst_newa_len;
8416
8417                         info = newarr_info;
8418                 }
8419                 else {
8420                         MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
8421
8422                         NEW_VTABLECONST (cfg, iargs [0], vtable);
8423                         iargs [1] = tree->inst_newa_len;
8424
8425                         info = newarr_specific_info;
8426                 }
8427
8428                 mono_emulate_opcode (cfg, tree, iargs, info);
8429
8430                 /* Need to decompose arguments after the the opcode is decomposed */
8431                 for (i = 0; i < info->sig->param_count; ++i)
8432                         dec_foreach (iargs [i], cfg);
8433                 break;
8434         }
8435 #ifdef MONO_ARCH_SOFT_FLOAT
8436         case OP_FBEQ:
8437         case OP_FBGE:
8438         case OP_FBGT:
8439         case OP_FBLE:
8440         case OP_FBLT:
8441         case OP_FBNE_UN:
8442         case OP_FBGE_UN:
8443         case OP_FBGT_UN:
8444         case OP_FBLE_UN:
8445         case OP_FBLT_UN: {
8446                 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
8447                         MonoCompile *cfg = data;
8448                         MonoInst *iargs [2];
8449                 
8450                         iargs [0] = tree->inst_i0;
8451                         iargs [1] = tree->inst_i1;
8452                 
8453                         mono_emulate_opcode (cfg, tree, iargs, info);
8454
8455                         dec_foreach (iargs [0], cfg);
8456                         dec_foreach (iargs [1], cfg);
8457                         break;
8458                 } else {
8459                         g_assert_not_reached ();
8460                 }
8461                 break;
8462         }
8463         case OP_FCEQ:
8464         case OP_FCGT:
8465         case OP_FCGT_UN:
8466         case OP_FCLT:
8467         case OP_FCLT_UN: {
8468                 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
8469                         MonoCompile *cfg = data;
8470                         MonoInst *iargs [2];
8471
8472                         /* the args are in the compare opcode ... */
8473                         iargs [0] = tree->inst_i0;
8474                         iargs [1] = tree->inst_i1;
8475                 
8476                         mono_emulate_opcode (cfg, tree, iargs, info);
8477
8478                         dec_foreach (iargs [0], cfg);
8479                         dec_foreach (iargs [1], cfg);
8480                         break;
8481                 } else {
8482                         g_assert_not_reached ();
8483                 }
8484                 break;
8485         }
8486 #endif
8487
8488         default:
8489                 break;
8490         }
8491 }
8492
8493 void
8494 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
8495
8496         switch (mono_burg_arity [tree->opcode]) {
8497         case 0: break;
8498         case 1: 
8499                 mono_inst_foreach (tree->inst_left, func, data);
8500                 break;
8501         case 2: 
8502                 mono_inst_foreach (tree->inst_left, func, data);
8503                 mono_inst_foreach (tree->inst_right, func, data);
8504                 break;
8505         default:
8506                 g_assert_not_reached ();
8507         }
8508         func (tree, data);
8509 }
8510
8511 G_GNUC_UNUSED
8512 static void
8513 mono_print_bb_code (MonoBasicBlock *bb) {
8514         if (bb->code) {
8515                 MonoInst *c = bb->code;
8516                 while (c) {
8517                         mono_print_tree (c);
8518                         g_print ("\n");
8519                         c = c->next;
8520                 }
8521         }
8522 }
8523
8524 static void
8525 print_dfn (MonoCompile *cfg) {
8526         int i, j;
8527         char *code;
8528         MonoBasicBlock *bb;
8529
8530         g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
8531
8532         for (i = 0; i < cfg->num_bblocks; ++i) {
8533                 bb = cfg->bblocks [i];
8534                 /*if (bb->cil_code) {
8535                         char* code1, *code2;
8536                         code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
8537                         if (bb->last_ins->cil_code)
8538                                 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
8539                         else
8540                                 code2 = g_strdup ("");
8541
8542                         code1 [strlen (code1) - 1] = 0;
8543                         code = g_strdup_printf ("%s -> %s", code1, code2);
8544                         g_free (code1);
8545                         g_free (code2);
8546                 } else*/
8547                         code = g_strdup ("\n");
8548                 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
8549                 if (bb->code) {
8550                         MonoInst *c = bb->code;
8551                         while (c) {
8552                                 mono_print_tree (c);
8553                                 g_print ("\n");
8554                                 c = c->next;
8555                         }
8556                 } else {
8557
8558                 }
8559
8560                 g_print ("\tprev:");
8561                 for (j = 0; j < bb->in_count; ++j) {
8562                         g_print (" BB%d", bb->in_bb [j]->block_num);
8563                 }
8564                 g_print ("\t\tsucc:");
8565                 for (j = 0; j < bb->out_count; ++j) {
8566                         g_print (" BB%d", bb->out_bb [j]->block_num);
8567                 }
8568                 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
8569
8570                 if (bb->idom)
8571                         g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
8572
8573                 if (bb->dominators)
8574                         mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
8575                 if (bb->dfrontier)
8576                         mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
8577                 g_free (code);
8578         }
8579
8580         g_print ("\n");
8581 }
8582
8583 void
8584 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
8585 {
8586         inst->next = NULL;
8587         if (bb->last_ins) {
8588                 g_assert (bb->code);
8589                 bb->last_ins->next = inst;
8590                 bb->last_ins = inst;
8591         } else {
8592                 bb->last_ins = bb->code = inst;
8593         }
8594 }
8595
8596 void
8597 mono_destroy_compile (MonoCompile *cfg)
8598 {
8599         //mono_mempool_stats (cfg->mempool);
8600         mono_free_loop_info (cfg);
8601         if (cfg->rs)
8602                 mono_regstate_free (cfg->rs);
8603         if (cfg->spvars)
8604                 g_hash_table_destroy (cfg->spvars);
8605         if (cfg->exvars)
8606                 g_hash_table_destroy (cfg->exvars);
8607         mono_mempool_destroy (cfg->mempool);
8608         g_list_free (cfg->ldstr_list);
8609         g_hash_table_destroy (cfg->token_info_hash);
8610
8611         g_free (cfg->varinfo);
8612         g_free (cfg->vars);
8613         g_free (cfg->exception_message);
8614         g_free (cfg);
8615 }
8616
8617 #ifdef HAVE_KW_THREAD
8618 static __thread gpointer mono_lmf_addr MONO_TLS_FAST;
8619 #ifdef MONO_ARCH_ENABLE_MONO_LMF_VAR
8620 /* 
8621  * When this is defined, the current lmf is stored in this tls variable instead of in 
8622  * jit_tls->lmf.
8623  */
8624 static __thread gpointer mono_lmf MONO_TLS_FAST;
8625 #endif
8626 #endif
8627
8628 guint32
8629 mono_get_jit_tls_key (void)
8630 {
8631         return mono_jit_tls_id;
8632 }
8633
8634 gint32
8635 mono_get_lmf_tls_offset (void)
8636 {
8637 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
8638         int offset;
8639         MONO_THREAD_VAR_OFFSET(mono_lmf,offset);
8640         return offset;
8641 #else
8642         return -1;
8643 #endif
8644 }
8645
8646 gint32
8647 mono_get_lmf_addr_tls_offset (void)
8648 {
8649         int offset;
8650         MONO_THREAD_VAR_OFFSET(mono_lmf_addr,offset);
8651         return offset;
8652 }
8653
8654 MonoLMF *
8655 mono_get_lmf (void)
8656 {
8657 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
8658         return mono_lmf;
8659 #else
8660         MonoJitTlsData *jit_tls;
8661
8662         if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
8663                 return jit_tls->lmf;
8664
8665         g_assert_not_reached ();
8666         return NULL;
8667 #endif
8668 }
8669
8670 MonoLMF **
8671 mono_get_lmf_addr (void)
8672 {
8673 #ifdef HAVE_KW_THREAD
8674         return mono_lmf_addr;
8675 #else
8676         MonoJitTlsData *jit_tls;
8677
8678         if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
8679                 return &jit_tls->lmf;
8680
8681         g_assert_not_reached ();
8682         return NULL;
8683 #endif
8684 }
8685
8686 /* Called by native->managed wrappers */
8687 void
8688 mono_jit_thread_attach (MonoDomain *domain)
8689 {
8690 #ifdef HAVE_KW_THREAD
8691         if (!mono_lmf_addr) {
8692                 mono_thread_attach (domain);
8693         }
8694 #else
8695         if (!TlsGetValue (mono_jit_tls_id))
8696                 mono_thread_attach (domain);
8697 #endif
8698 }       
8699
8700 /**
8701  * mono_thread_abort:
8702  * @obj: exception object
8703  *
8704  * abort the thread, print exception information and stack trace
8705  */
8706 static void
8707 mono_thread_abort (MonoObject *obj)
8708 {
8709         /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
8710         
8711         /* handle_remove should be eventually called for this thread, too
8712         g_free (jit_tls);*/
8713
8714         mono_thread_exit ();
8715 }
8716
8717 static void*
8718 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
8719 {
8720         MonoJitTlsData *jit_tls;
8721         MonoLMF *lmf;
8722
8723         jit_tls = TlsGetValue (mono_jit_tls_id);
8724         if (jit_tls)
8725                 return jit_tls;
8726
8727         jit_tls = g_new0 (MonoJitTlsData, 1);
8728
8729         TlsSetValue (mono_jit_tls_id, jit_tls);
8730
8731         jit_tls->abort_func = abort_func;
8732         jit_tls->end_of_stack = stack_start;
8733
8734         lmf = g_new0 (MonoLMF, 1);
8735         lmf->ebp = -1;
8736
8737         jit_tls->first_lmf = lmf;
8738
8739 #if defined(HAVE_KW_THREAD) && defined(MONO_ARCH_ENABLE_MONO_LMF_VAR)
8740         /* jit_tls->lmf is unused */
8741         mono_lmf = lmf;
8742         mono_lmf_addr = &mono_lmf;
8743 #else
8744 #if defined(HAVE_KW_THREAD)
8745         mono_lmf_addr = &jit_tls->lmf;  
8746 #endif
8747
8748         jit_tls->lmf = lmf;
8749 #endif
8750
8751         mono_arch_setup_jit_tls_data (jit_tls);
8752
8753 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8754         mono_setup_altstack (jit_tls);
8755 #endif
8756
8757         return jit_tls;
8758 }
8759
8760 static void
8761 mono_thread_start_cb (gsize tid, gpointer stack_start, gpointer func)
8762 {
8763         MonoThread *thread;
8764         void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
8765         thread = mono_thread_current ();
8766         if (thread)
8767                 thread->jit_data = jit_tls;
8768 }
8769
8770 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
8771
8772 static void
8773 mono_thread_abort_dummy (MonoObject *obj)
8774 {
8775   if (mono_thread_attach_aborted_cb)
8776     mono_thread_attach_aborted_cb (obj);
8777   else
8778     mono_thread_abort (obj);
8779 }
8780
8781 static void
8782 mono_thread_attach_cb (gsize tid, gpointer stack_start)
8783 {
8784         MonoThread *thread;
8785         void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
8786         thread = mono_thread_current ();
8787         if (thread)
8788                 thread->jit_data = jit_tls;
8789         if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
8790                 setup_stat_profiler ();
8791 }
8792
8793 static void
8794 mini_thread_cleanup (MonoThread *thread)
8795 {
8796         MonoJitTlsData *jit_tls = thread->jit_data;
8797
8798         if (jit_tls) {
8799                 mono_arch_free_jit_tls_data (jit_tls);
8800
8801 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
8802                 mono_free_altstack (jit_tls);
8803 #endif
8804                 g_free (jit_tls->first_lmf);
8805                 g_free (jit_tls);
8806                 thread->jit_data = NULL;
8807                 TlsSetValue (mono_jit_tls_id, NULL);
8808         }
8809 }
8810
8811 void
8812 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
8813 {
8814         MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
8815
8816         ji->ip.i = ip;
8817         ji->type = type;
8818         ji->data.target = target;
8819         ji->next = cfg->patch_info;
8820
8821         cfg->patch_info = ji;
8822 }
8823
8824 void
8825 mono_remove_patch_info (MonoCompile *cfg, int ip)
8826 {
8827         MonoJumpInfo **ji = &cfg->patch_info;
8828
8829         while (*ji) {
8830                 if ((*ji)->ip.i == ip)
8831                         *ji = (*ji)->next;
8832                 else
8833                         ji = &((*ji)->next);
8834         }
8835 }
8836
8837 /**
8838  * mono_patch_info_dup_mp:
8839  *
8840  * Make a copy of PATCH_INFO, allocating memory from the mempool MP.
8841  */
8842 MonoJumpInfo*
8843 mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
8844 {
8845         MonoJumpInfo *res = mono_mempool_alloc (mp, sizeof (MonoJumpInfo));
8846         memcpy (res, patch_info, sizeof (MonoJumpInfo));
8847
8848         switch (patch_info->type) {
8849         case MONO_PATCH_INFO_RVA:
8850         case MONO_PATCH_INFO_LDSTR:
8851         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
8852         case MONO_PATCH_INFO_LDTOKEN:
8853         case MONO_PATCH_INFO_DECLSEC:
8854                 res->data.token = mono_mempool_alloc (mp, sizeof (MonoJumpInfoToken));
8855                 memcpy (res->data.token, patch_info->data.token, sizeof (MonoJumpInfoToken));
8856                 break;
8857         case MONO_PATCH_INFO_SWITCH:
8858                 res->data.table = mono_mempool_alloc (mp, sizeof (MonoJumpInfoBBTable));
8859                 memcpy (res->data.table, patch_info->data.table, sizeof (MonoJumpInfoBBTable));
8860                 break;
8861         default:
8862                 break;
8863         }
8864
8865         return res;
8866 }
8867
8868 guint
8869 mono_patch_info_hash (gconstpointer data)
8870 {
8871         const MonoJumpInfo *ji = (MonoJumpInfo*)data;
8872
8873         switch (ji->type) {
8874         case MONO_PATCH_INFO_RVA:
8875         case MONO_PATCH_INFO_LDSTR:
8876         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
8877         case MONO_PATCH_INFO_LDTOKEN:
8878         case MONO_PATCH_INFO_DECLSEC:
8879                 return (ji->type << 8) | ji->data.token->token;
8880         default:
8881                 return (ji->type << 8);
8882         }
8883 }
8884
8885 /* 
8886  * mono_patch_info_equal:
8887  * 
8888  * This might fail to recognize equivalent patches, i.e. floats, so its only
8889  * usable in those cases where this is not a problem, i.e. sharing GOT slots
8890  * in AOT.
8891  */
8892 gint
8893 mono_patch_info_equal (gconstpointer ka, gconstpointer kb)
8894 {
8895         const MonoJumpInfo *ji1 = (MonoJumpInfo*)ka;
8896         const MonoJumpInfo *ji2 = (MonoJumpInfo*)kb;
8897
8898         if (ji1->type != ji2->type)
8899                 return 0;
8900
8901         switch (ji1->type) {
8902         case MONO_PATCH_INFO_RVA:
8903         case MONO_PATCH_INFO_LDSTR:
8904         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
8905         case MONO_PATCH_INFO_LDTOKEN:
8906         case MONO_PATCH_INFO_DECLSEC:
8907                 if ((ji1->data.token->image != ji2->data.token->image) ||
8908                         (ji1->data.token->token != ji2->data.token->token))
8909                         return 0;
8910                 break;
8911         default:
8912                 if (ji1->data.name != ji2->data.name)
8913                         return 0;
8914                 break;
8915         }
8916
8917         return 1;
8918 }
8919
8920 gpointer
8921 mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *patch_info, gboolean run_cctors)
8922 {
8923         unsigned char *ip = patch_info->ip.i + code;
8924         gconstpointer target = NULL;
8925
8926         switch (patch_info->type) {
8927         case MONO_PATCH_INFO_BB:
8928                 target = patch_info->data.bb->native_offset + code;
8929                 break;
8930         case MONO_PATCH_INFO_ABS:
8931                 target = patch_info->data.target;
8932                 break;
8933         case MONO_PATCH_INFO_LABEL:
8934                 target = patch_info->data.inst->inst_c0 + code;
8935                 break;
8936         case MONO_PATCH_INFO_IP:
8937                 target = ip;
8938                 break;
8939         case MONO_PATCH_INFO_METHOD_REL:
8940                 target = code + patch_info->data.offset;
8941                 break;
8942         case MONO_PATCH_INFO_INTERNAL_METHOD: {
8943                 MonoJitICallInfo *mi = mono_find_jit_icall_by_name (patch_info->data.name);
8944                 if (!mi) {
8945                         g_warning ("unknown MONO_PATCH_INFO_INTERNAL_METHOD %s", patch_info->data.name);
8946                         g_assert_not_reached ();
8947                 }
8948                 target = mono_icall_get_wrapper (mi);
8949                 break;
8950         }
8951         case MONO_PATCH_INFO_METHOD_JUMP: {
8952                 GSList *list;
8953
8954                 /* get the trampoline to the method from the domain */
8955                 target = mono_create_jump_trampoline (domain, patch_info->data.method, TRUE);
8956                 if (!domain->jump_target_hash)
8957                         domain->jump_target_hash = g_hash_table_new (NULL, NULL);
8958                 list = g_hash_table_lookup (domain->jump_target_hash, patch_info->data.method);
8959                 list = g_slist_prepend (list, ip);
8960                 g_hash_table_insert (domain->jump_target_hash, patch_info->data.method, list);
8961                 break;
8962         }
8963         case MONO_PATCH_INFO_METHOD:
8964                 if (patch_info->data.method == method) {
8965                         target = code;
8966                 } else
8967                         /* get the trampoline to the method from the domain */
8968                         target = mono_create_jit_trampoline (patch_info->data.method);
8969                 break;
8970         case MONO_PATCH_INFO_SWITCH: {
8971                 gpointer *jump_table;
8972                 int i;
8973
8974                 if (method && method->dynamic) {
8975                         jump_table = mono_code_manager_reserve (mono_dynamic_code_hash_lookup (domain, method)->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8976                 } else {
8977                         mono_domain_lock (domain);
8978                         jump_table = mono_code_manager_reserve (domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
8979                         mono_domain_unlock (domain);
8980                 }
8981
8982                 for (i = 0; i < patch_info->data.table->table_size; i++) {
8983                         jump_table [i] = code + GPOINTER_TO_INT (patch_info->data.table->table [i]);
8984                 }
8985                 target = jump_table;
8986                 break;
8987         }
8988         case MONO_PATCH_INFO_METHODCONST:
8989         case MONO_PATCH_INFO_CLASS:
8990         case MONO_PATCH_INFO_IMAGE:
8991         case MONO_PATCH_INFO_FIELD:
8992                 target = patch_info->data.target;
8993                 break;
8994         case MONO_PATCH_INFO_IID:
8995                 mono_class_init (patch_info->data.klass);
8996                 target = GINT_TO_POINTER ((int)patch_info->data.klass->interface_id);
8997                 break;
8998         case MONO_PATCH_INFO_ADJUSTED_IID:
8999                 mono_class_init (patch_info->data.klass);
9000                 target = GINT_TO_POINTER ((int)(-((patch_info->data.klass->interface_id + 1) * SIZEOF_VOID_P)));
9001                 break;
9002         case MONO_PATCH_INFO_VTABLE:
9003                 target = mono_class_vtable (domain, patch_info->data.klass);
9004                 break;
9005         case MONO_PATCH_INFO_CLASS_INIT:
9006                 target = mono_create_class_init_trampoline (mono_class_vtable (domain, patch_info->data.klass));
9007                 break;
9008         case MONO_PATCH_INFO_SFLDA: {
9009                 MonoVTable *vtable = mono_class_vtable (domain, patch_info->data.field->parent);
9010                 if (!vtable->initialized && !(vtable->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && (method && mono_class_needs_cctor_run (vtable->klass, method)))
9011                         /* Done by the generated code */
9012                         ;
9013                 else {
9014                         if (run_cctors)
9015                                 mono_runtime_class_init (vtable);
9016                 }
9017                 target = (char*)vtable->data + patch_info->data.field->offset;
9018                 break;
9019         }
9020         case MONO_PATCH_INFO_RVA:
9021                 target = mono_image_rva_map (patch_info->data.token->image, patch_info->data.token->token);
9022                 break;
9023         case MONO_PATCH_INFO_R4:
9024         case MONO_PATCH_INFO_R8:
9025                 target = patch_info->data.target;
9026                 break;
9027         case MONO_PATCH_INFO_EXC_NAME:
9028                 target = patch_info->data.name;
9029                 break;
9030         case MONO_PATCH_INFO_LDSTR:
9031                 target =
9032                         mono_ldstr (domain, patch_info->data.token->image, 
9033                                                 mono_metadata_token_index (patch_info->data.token->token));
9034                 break;
9035         case MONO_PATCH_INFO_TYPE_FROM_HANDLE: {
9036                 gpointer handle;
9037                 MonoClass *handle_class;
9038
9039                 handle = mono_ldtoken (patch_info->data.token->image, 
9040                                        patch_info->data.token->token, &handle_class, NULL);
9041                 mono_class_init (handle_class);
9042                 mono_class_init (mono_class_from_mono_type (handle));
9043
9044                 target =
9045                         mono_type_get_object (domain, handle);
9046                 break;
9047         }
9048         case MONO_PATCH_INFO_LDTOKEN: {
9049                 gpointer handle;
9050                 MonoClass *handle_class;
9051                 
9052                 handle = mono_ldtoken (patch_info->data.token->image,
9053                                        patch_info->data.token->token, &handle_class, NULL);
9054                 mono_class_init (handle_class);
9055                 
9056                 target = handle;
9057                 break;
9058         }
9059         case MONO_PATCH_INFO_DECLSEC:
9060                 target = (mono_metadata_blob_heap (patch_info->data.token->image, patch_info->data.token->token) + 2);
9061                 break;
9062         case MONO_PATCH_INFO_BB_OVF:
9063         case MONO_PATCH_INFO_EXC_OVF:
9064         case MONO_PATCH_INFO_GOT_OFFSET:
9065         case MONO_PATCH_INFO_NONE:
9066                 break;
9067         default:
9068                 g_assert_not_reached ();
9069         }
9070
9071         return (gpointer)target;
9072 }
9073
9074 static void
9075 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
9076         MonoJitICallInfo *info;
9077
9078         decompose_foreach (tree, cfg);
9079
9080         switch (mono_burg_arity [tree->opcode]) {
9081         case 0: break;
9082         case 1: 
9083                 dec_foreach (tree->inst_left, cfg);
9084
9085                 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
9086                         MonoInst *iargs [2];
9087                 
9088                         iargs [0] = tree->inst_left;
9089
9090                         mono_emulate_opcode (cfg, tree, iargs, info);
9091                         return;
9092                 }
9093
9094                 break;
9095         case 2:
9096 #ifdef MONO_ARCH_BIGMUL_INTRINS
9097                 if (tree->opcode == OP_LMUL
9098                                 && (cfg->opt & MONO_OPT_INTRINS)
9099                                 && (tree->inst_left->opcode == CEE_CONV_I8 
9100                                         || tree->inst_left->opcode == CEE_CONV_U8)
9101                                 && tree->inst_left->inst_left->type == STACK_I4
9102                                 && (tree->inst_right->opcode == CEE_CONV_I8 
9103                                         || tree->inst_right->opcode == CEE_CONV_U8)
9104                                 && tree->inst_right->inst_left->type == STACK_I4
9105                                 && tree->inst_left->opcode == tree->inst_right->opcode) {
9106                         tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
9107                         tree->inst_left = tree->inst_left->inst_left;
9108                         tree->inst_right = tree->inst_right->inst_left;
9109                         dec_foreach (tree, cfg);
9110                 } else 
9111 #endif
9112                         if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
9113                         MonoInst *iargs [2];
9114                 
9115                         iargs [0] = tree->inst_i0;
9116                         iargs [1] = tree->inst_i1;
9117                 
9118                         mono_emulate_opcode (cfg, tree, iargs, info);
9119
9120                         dec_foreach (iargs [0], cfg);
9121                         dec_foreach (iargs [1], cfg);
9122                         return;
9123                 } else {
9124                         dec_foreach (tree->inst_left, cfg);
9125                         dec_foreach (tree->inst_right, cfg);
9126                 }
9127                 break;
9128         default:
9129                 g_assert_not_reached ();
9130         }
9131 }
9132
9133 static void
9134 decompose_pass (MonoCompile *cfg) {
9135         MonoBasicBlock *bb;
9136
9137         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9138                 MonoInst *tree;
9139                 cfg->cbb = bb;
9140                 cfg->prev_ins = NULL;
9141                 for (tree = cfg->cbb->code; tree; tree = tree->next) {
9142                         dec_foreach (tree, cfg);
9143                         cfg->prev_ins = tree;
9144                 }
9145         }
9146 }
9147
9148 static void
9149 nullify_basic_block (MonoBasicBlock *bb) 
9150 {
9151         bb->in_count = 0;
9152         bb->out_count = 0;
9153         bb->in_bb = NULL;
9154         bb->out_bb = NULL;
9155         bb->next_bb = NULL;
9156         bb->code = bb->last_ins = NULL;
9157         bb->cil_code = NULL;
9158 }
9159
9160 static void 
9161 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig,  MonoBasicBlock *repl)
9162 {
9163         int i;
9164
9165         for (i = 0; i < bb->out_count; i++) {
9166                 MonoBasicBlock *ob = bb->out_bb [i];
9167                 if (ob == orig) {
9168                         if (!repl) {
9169                                 if (bb->out_count > 1) {
9170                                         bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
9171                                 }
9172                                 bb->out_count--;
9173                         } else {
9174                                 bb->out_bb [i] = repl;
9175                         }
9176                 }
9177         }
9178 }
9179
9180 static void 
9181 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
9182 {
9183         int i;
9184
9185         for (i = 0; i < bb->in_count; i++) {
9186                 MonoBasicBlock *ib = bb->in_bb [i];
9187                 if (ib == orig) {
9188                         if (!repl) {
9189                                 if (bb->in_count > 1) {
9190                                         bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
9191                                 }
9192                                 bb->in_count--;
9193                         } else {
9194                                 bb->in_bb [i] = repl;
9195                         }
9196                 }
9197         }
9198 }
9199
9200 static void
9201 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
9202         MonoInst *inst;
9203         
9204         for (inst = bb->code; inst != NULL; inst = inst->next) {
9205                 if (inst->opcode == OP_CALL_HANDLER) {
9206                         if (inst->inst_target_bb == orig) {
9207                                 inst->inst_target_bb = repl;
9208                         }
9209                 }
9210         }
9211         if (bb->last_ins != NULL) {
9212                 switch (bb->last_ins->opcode) {
9213                 case OP_BR:
9214                         if (bb->last_ins->inst_target_bb == orig) {
9215                                 bb->last_ins->inst_target_bb = repl;
9216                         }
9217                         break;
9218                 case CEE_SWITCH: {
9219                         int i;
9220                         int n = GPOINTER_TO_INT (bb->last_ins->klass);
9221                         for (i = 0; i < n; i++ ) {
9222                                 if (bb->last_ins->inst_many_bb [i] == orig) {
9223                                         bb->last_ins->inst_many_bb [i] = repl;
9224                                 }
9225                         }
9226                         break;
9227                 }
9228                 case CEE_BNE_UN:
9229                 case CEE_BEQ:
9230                 case CEE_BLT:
9231                 case CEE_BLT_UN:
9232                 case CEE_BGT:
9233                 case CEE_BGT_UN:
9234                 case CEE_BGE:
9235                 case CEE_BGE_UN:
9236                 case CEE_BLE:
9237                 case CEE_BLE_UN:
9238                         if (bb->last_ins->inst_true_bb == orig) {
9239                                 bb->last_ins->inst_true_bb = repl;
9240                         }
9241                         if (bb->last_ins->inst_false_bb == orig) {
9242                                 bb->last_ins->inst_false_bb = repl;
9243                         }
9244                         break;
9245                 default:
9246                         break;
9247                 }
9248         }
9249 }
9250
9251 static void 
9252 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig,  MonoBasicBlock *repl)
9253 {
9254         int i, j;
9255
9256         for (i = 0; i < bb->out_count; i++) {
9257                 MonoBasicBlock *ob = bb->out_bb [i];
9258                 for (j = 0; j < ob->in_count; j++) {
9259                         if (ob->in_bb [j] == orig) {
9260                                 ob->in_bb [j] = repl;
9261                         }
9262                 }
9263         }
9264
9265 }
9266
9267 /**
9268   * Check if a bb is useless (is just made of NOPs and ends with an
9269   * unconditional branch, or nothing).
9270   * If it is so, unlink it from the CFG and nullify it, and return TRUE.
9271   * Otherwise, return FALSE;
9272   */
9273 static gboolean
9274 remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *previous_bb) {
9275         MonoBasicBlock *target_bb = NULL;
9276         MonoInst *inst;
9277         
9278         /* Do not touch handlers */
9279         if (bb->region != -1) {
9280                 bb->not_useless = TRUE;
9281                 return FALSE;
9282         }
9283         
9284         for (inst = bb->code; inst != NULL; inst = inst->next) {
9285                 switch (inst->opcode) {
9286                 case OP_NOP:
9287                         break;
9288                 case OP_BR:
9289                         target_bb = inst->inst_target_bb;
9290                         break;
9291                 default:
9292                         bb->not_useless = TRUE;
9293                         return FALSE;
9294                 }
9295         }
9296         
9297         if (target_bb == NULL) {
9298                 if ((bb->out_count == 1) && (bb->out_bb [0] == bb->next_bb)) {
9299                         target_bb = bb->next_bb;
9300                 } else {
9301                         /* Do not touch empty BBs that do not "fall through" to their next BB (like the exit BB) */
9302                         return FALSE;
9303                 }
9304         }
9305         
9306         /* Do not touch BBs following a switch (they are the "default" branch) */
9307         if ((previous_bb->last_ins != NULL) && (previous_bb->last_ins->opcode == CEE_SWITCH)) {
9308                 return FALSE;
9309         }
9310         
9311         /* Do not touch BBs following the entry BB and jumping to something that is not */
9312         /* thiry "next" bb (the entry BB cannot contain the branch) */
9313         if ((previous_bb == cfg->bb_entry) && (bb->next_bb != target_bb)) {
9314                 return FALSE;
9315         }
9316
9317         /* 
9318          * Do not touch BBs following a try block as the code in 
9319          * mini_method_compile needs them to compute the length of the try block.
9320          */
9321         if (MONO_BBLOCK_IS_IN_REGION (previous_bb, MONO_REGION_TRY))
9322                 return FALSE;
9323         
9324         /* Check that there is a target BB, and that bb is not an empty loop (Bug 75061) */
9325         if ((target_bb != NULL) && (target_bb != bb)) {
9326                 int i;
9327
9328                 if (cfg->verbose_level > 1) {
9329                         printf ("remove_block_if_useless %s, removed BB%d\n", mono_method_full_name (cfg->method, TRUE), bb->block_num);
9330                 }
9331                 
9332                 /* unlink_bblock () modifies the bb->in_bb array so can't use a for loop here */
9333                 while (bb->in_count) {
9334                         MonoBasicBlock *in_bb = bb->in_bb [0];
9335                         mono_unlink_bblock (cfg, in_bb, bb);
9336                         link_bblock (cfg, in_bb, target_bb);
9337                         replace_out_block_in_code (in_bb, bb, target_bb);
9338                 }
9339                 
9340                 mono_unlink_bblock (cfg, bb, target_bb);
9341                 
9342                 if ((previous_bb != cfg->bb_entry) &&
9343                                 (previous_bb->region == bb->region) &&
9344                                 ((previous_bb->last_ins == NULL) ||
9345                                 ((previous_bb->last_ins->opcode != OP_BR) &&
9346                                 (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
9347                                 (previous_bb->last_ins->opcode != CEE_SWITCH)))) {
9348                         for (i = 0; i < previous_bb->out_count; i++) {
9349                                 if (previous_bb->out_bb [i] == target_bb) {
9350                                         MonoInst *jump;
9351                                         MONO_INST_NEW (cfg, jump, OP_BR);
9352                                         MONO_ADD_INS (previous_bb, jump);
9353                                         jump->cil_code = previous_bb->cil_code;
9354                                         jump->inst_target_bb = target_bb;
9355                                         break;
9356                                 }
9357                         }
9358                 }
9359                 
9360                 previous_bb->next_bb = bb->next_bb;
9361                 nullify_basic_block (bb);
9362                 
9363                 return TRUE;
9364         } else {
9365                 return FALSE;
9366         }
9367 }
9368
9369 static void
9370 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn) 
9371 {
9372         bb->out_count = bbn->out_count;
9373         bb->out_bb = bbn->out_bb;
9374
9375         replace_basic_block (bb, bbn, bb);
9376
9377         /* Nullify branch at the end of bb */
9378         if (bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) {
9379                 bb->last_ins->opcode = OP_NOP;
9380         }               
9381
9382         if (bb->last_ins) {
9383                 if (bbn->code) {
9384                         bb->last_ins->next = bbn->code;
9385                         bb->last_ins = bbn->last_ins;
9386                 }
9387         } else {
9388                 bb->code = bbn->code;
9389                 bb->last_ins = bbn->last_ins;
9390         }
9391         bb->next_bb = bbn->next_bb;
9392         nullify_basic_block (bbn);
9393 }
9394
9395 static void
9396 move_basic_block_to_end (MonoCompile *cfg, MonoBasicBlock *bb)
9397 {
9398         MonoBasicBlock *bbn, *next;
9399
9400         next = bb->next_bb;
9401
9402         /* Find the previous */
9403         for (bbn = cfg->bb_entry; bbn->next_bb && bbn->next_bb != bb; bbn = bbn->next_bb)
9404                 ;
9405         if (bbn->next_bb) {
9406                 bbn->next_bb = bb->next_bb;
9407         }
9408
9409         /* Find the last */
9410         for (bbn = cfg->bb_entry; bbn->next_bb; bbn = bbn->next_bb)
9411                 ;
9412         bbn->next_bb = bb;
9413         bb->next_bb = NULL;
9414
9415         /* Add a branch */
9416         if (next && (!bb->last_ins || (bb->last_ins->opcode != OP_NOT_REACHED))) {
9417                 MonoInst *ins;
9418
9419                 MONO_INST_NEW (cfg, ins, OP_BR);
9420                 MONO_ADD_INS (bb, ins);
9421                 link_bblock (cfg, bb, next);
9422                 ins->inst_target_bb = next;
9423         }               
9424 }
9425
9426 /* checks that a and b represent the same instructions, conservatively,
9427  * it can return FALSE also for two trees that are equal.
9428  * FIXME: also make sure there are no side effects.
9429  */
9430 static int
9431 same_trees (MonoInst *a, MonoInst *b)
9432 {
9433         int arity;
9434         if (a->opcode != b->opcode)
9435                 return FALSE;
9436         arity = mono_burg_arity [a->opcode];
9437         if (arity == 1) {
9438                 if (a->ssa_op == b->ssa_op && a->ssa_op == MONO_SSA_LOAD && a->inst_i0 == b->inst_i0)
9439                         return TRUE;
9440                 return same_trees (a->inst_left, b->inst_left);
9441         } else if (arity == 2) {
9442                 return same_trees (a->inst_left, b->inst_left) && same_trees (a->inst_right, b->inst_right);
9443         } else if (arity == 0) {
9444                 switch (a->opcode) {
9445                 case OP_ICONST:
9446                         return a->inst_c0 == b->inst_c0;
9447                 default:
9448                         return FALSE;
9449                 }
9450         }
9451         return FALSE;
9452 }
9453
9454 static int
9455 get_unsigned_condbranch (int opcode)
9456 {
9457         switch (opcode) {
9458         case CEE_BLE: return CEE_BLE_UN;
9459         case CEE_BLT: return CEE_BLT_UN;
9460         case CEE_BGE: return CEE_BGE_UN;
9461         case CEE_BGT: return CEE_BGT_UN;
9462         }
9463         g_assert_not_reached ();
9464         return 0;
9465 }
9466
9467 static int
9468 tree_is_unsigned (MonoInst* ins) {
9469         switch (ins->opcode) {
9470         case OP_ICONST:
9471                 return (int)ins->inst_c0 >= 0;
9472         /* array lengths are positive as are string sizes */
9473         case CEE_LDLEN:
9474         case OP_STRLEN:
9475                 return TRUE;
9476         case CEE_CONV_U1:
9477         case CEE_CONV_U2:
9478         case CEE_CONV_U4:
9479         case CEE_CONV_OVF_U1:
9480         case CEE_CONV_OVF_U2:
9481         case CEE_CONV_OVF_U4:
9482                 return TRUE;
9483         case CEE_LDIND_U1:
9484         case CEE_LDIND_U2:
9485         case CEE_LDIND_U4:
9486                 return TRUE;
9487         default:
9488                 return FALSE;
9489         }
9490 }
9491
9492 /* check if an unsigned compare can be used instead of two signed compares
9493  * for (val < 0 || val > limit) conditionals.
9494  * Returns TRUE if the optimization has been applied.
9495  * Note that this can't be applied if the second arg is not positive...
9496  */
9497 static int
9498 try_unsigned_compare (MonoCompile *cfg, MonoBasicBlock *bb)
9499 {
9500         MonoBasicBlock *truet, *falset;
9501         MonoInst *cmp_inst = bb->last_ins->inst_left;
9502         MonoInst *condb;
9503         if (!cmp_inst->inst_right->inst_c0 == 0)
9504                 return FALSE;
9505         truet = bb->last_ins->inst_true_bb;
9506         falset = bb->last_ins->inst_false_bb;
9507         if (falset->in_count != 1)
9508                 return FALSE;
9509         condb = falset->last_ins;
9510         /* target bb must have one instruction */
9511         if (!condb || (condb != falset->code))
9512                 return FALSE;
9513         if ((((condb->opcode == CEE_BLE || condb->opcode == CEE_BLT) && (condb->inst_false_bb == truet))
9514                         || ((condb->opcode == CEE_BGE || condb->opcode == CEE_BGT) && (condb->inst_true_bb == truet)))
9515                         && same_trees (cmp_inst->inst_left, condb->inst_left->inst_left)) {
9516                 if (!tree_is_unsigned (condb->inst_left->inst_right))
9517                         return FALSE;
9518                 condb->opcode = get_unsigned_condbranch (condb->opcode);
9519                 /* change the original condbranch to just point to the new unsigned check */
9520                 bb->last_ins->opcode = OP_BR;
9521                 bb->last_ins->inst_target_bb = falset;
9522                 replace_out_block (bb, truet, NULL);
9523                 replace_in_block (truet, bb, NULL);
9524                 return TRUE;
9525         }
9526         return FALSE;
9527 }
9528
9529 /*
9530  * Optimizes the branches on the Control Flow Graph
9531  *
9532  */
9533 static void
9534 optimize_branches (MonoCompile *cfg)
9535 {
9536         int i, changed = FALSE;
9537         MonoBasicBlock *bb, *bbn;
9538         guint32 niterations;
9539
9540         /*
9541          * Some crazy loops could cause the code below to go into an infinite
9542          * loop, see bug #53003 for an example. To prevent this, we put an upper
9543          * bound on the number of iterations.
9544          */
9545         if (cfg->num_bblocks > 1000)
9546                 niterations = cfg->num_bblocks * 2;
9547         else
9548                 niterations = 1000;
9549
9550         do {
9551                 MonoBasicBlock *previous_bb;
9552                 changed = FALSE;
9553                 niterations --;
9554
9555                 /* we skip the entry block (exit is handled specially instead ) */
9556                 for (previous_bb = cfg->bb_entry, bb = cfg->bb_entry->next_bb; bb; previous_bb = bb, bb = bb->next_bb) {
9557
9558                         /* dont touch code inside exception clauses */
9559                         if (bb->region != -1)
9560                                 continue;
9561
9562                         if (!bb->not_useless && remove_block_if_useless (cfg, bb, previous_bb)) {
9563                                 changed = TRUE;
9564                                 continue;
9565                         }
9566
9567                         if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
9568                                 if (cfg->verbose_level > 2)
9569                                         g_print ("nullify block triggered %d\n", bbn->block_num);
9570
9571                                 bb->next_bb = bbn->next_bb;
9572
9573                                 for (i = 0; i < bbn->out_count; i++)
9574                                         replace_in_block (bbn->out_bb [i], bbn, NULL);
9575
9576                                 nullify_basic_block (bbn);                      
9577                                 changed = TRUE;
9578                         }
9579
9580                         if (bb->out_count == 1) {
9581                                 bbn = bb->out_bb [0];
9582
9583                                 /* conditional branches where true and false targets are the same can be also replaced with OP_BR */
9584                                 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
9585                                         MonoInst *pop;
9586                                         MONO_INST_NEW (cfg, pop, CEE_POP);
9587                                         pop->inst_left = bb->last_ins->inst_left->inst_left;
9588                                         mono_add_ins_to_end (bb, pop);
9589                                         MONO_INST_NEW (cfg, pop, CEE_POP);
9590                                         pop->inst_left = bb->last_ins->inst_left->inst_right;
9591                                         mono_add_ins_to_end (bb, pop);
9592                                         bb->last_ins->opcode = OP_BR;
9593                                         bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
9594                                         changed = TRUE;
9595                                         if (cfg->verbose_level > 2)
9596                                                 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
9597                                 }
9598
9599                                 if (bb->region == bbn->region && bb->next_bb == bbn) {
9600                                         /* the block are in sequence anyway ... */
9601
9602                                         /* branches to the following block can be removed */
9603                                         if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
9604                                                 bb->last_ins->opcode = OP_NOP;
9605                                                 changed = TRUE;
9606                                                 if (cfg->verbose_level > 2)
9607                                                         g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
9608                                         }
9609
9610                                         if (bbn->in_count == 1) {
9611
9612                                                 if (bbn != cfg->bb_exit) {
9613                                                         if (cfg->verbose_level > 2)
9614                                                                 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
9615                                                         merge_basic_blocks (bb, bbn);
9616                                                         changed = TRUE;
9617                                                         continue;
9618                                                 }
9619
9620                                                 //mono_print_bb_code (bb);
9621                                         }
9622                                 }
9623                         }
9624                         if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
9625                                 if (cfg->verbose_level > 2) {
9626                                         g_print ("nullify block triggered %d\n", bbn->block_num);
9627                                 }
9628                                 bb->next_bb = bbn->next_bb;
9629
9630                                 for (i = 0; i < bbn->out_count; i++)
9631                                         replace_in_block (bbn->out_bb [i], bbn, NULL);
9632
9633                                 nullify_basic_block (bbn);                      
9634                                 changed = TRUE;
9635                                 continue;
9636                         }
9637
9638                         if (bb->out_count == 1) {
9639                                 bbn = bb->out_bb [0];
9640
9641                                 if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
9642                                         bbn = bb->last_ins->inst_target_bb;
9643                                         if (bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
9644                                             bbn->code->inst_target_bb->region == bb->region) {
9645                                                 
9646                                                 if (cfg->verbose_level > 2)
9647                                                         g_print ("in %s branch to branch triggered %d -> %d -> %d\n", cfg->method->name, 
9648                                                                  bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num);
9649
9650                                                 replace_in_block (bbn, bb, NULL);
9651                                                 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
9652                                                 link_bblock (cfg, bb, bbn->code->inst_target_bb);
9653                                                 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
9654                                                 changed = TRUE;
9655                                                 continue;
9656                                         }
9657                                 }
9658                         } else if (bb->out_count == 2) {
9659                                 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
9660                                         int branch_result = mono_eval_cond_branch (bb->last_ins);
9661                                         MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
9662                                         if (branch_result == BRANCH_TAKEN) {
9663                                                 taken_branch_target = bb->last_ins->inst_true_bb;
9664                                                 untaken_branch_target = bb->last_ins->inst_false_bb;
9665                                         } else if (branch_result == BRANCH_NOT_TAKEN) {
9666                                                 taken_branch_target = bb->last_ins->inst_false_bb;
9667                                                 untaken_branch_target = bb->last_ins->inst_true_bb;
9668                                         }
9669                                         if (taken_branch_target) {
9670                                                 /* if mono_eval_cond_branch () is ever taken to handle 
9671                                                  * non-constant values to compare, issue a pop here.
9672                                                  */
9673                                                 bb->last_ins->opcode = OP_BR;
9674                                                 bb->last_ins->inst_target_bb = taken_branch_target;
9675                                                 mono_unlink_bblock (cfg, bb, untaken_branch_target);
9676                                                 changed = TRUE;
9677                                                 continue;
9678                                         }
9679                                         bbn = bb->last_ins->inst_true_bb;
9680                                         if (bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
9681                                             bbn->code->inst_target_bb->region == bb->region) {
9682                                                 if (cfg->verbose_level > 2)             
9683                                                         g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n", 
9684                                                                  bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num, 
9685                                                                  bbn->code->opcode);
9686
9687                                                 /* 
9688                                                  * Unlink, then relink bblocks to avoid various
9689                                                  * tricky situations when the two targets of the branch
9690                                                  * are equal, or will become equal after the change.
9691                                                  */
9692                                                 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
9693                                                 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
9694
9695                                                 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
9696
9697                                                 link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
9698                                                 link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
9699
9700                                                 changed = TRUE;
9701                                                 continue;
9702                                         }
9703
9704                                         bbn = bb->last_ins->inst_false_bb;
9705                                         if (bb->region == bbn->region && bbn->code && bbn->code->opcode == OP_BR &&
9706                                             bbn->code->inst_target_bb->region == bb->region) {
9707                                                 if (cfg->verbose_level > 2)
9708                                                         g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n", 
9709                                                                  bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num, 
9710                                                                  bbn->code->opcode);
9711
9712                                                 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_true_bb);
9713                                                 mono_unlink_bblock (cfg, bb, bb->last_ins->inst_false_bb);
9714
9715                                                 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
9716
9717                                                 link_bblock (cfg, bb, bb->last_ins->inst_true_bb);
9718                                                 link_bblock (cfg, bb, bb->last_ins->inst_false_bb);
9719
9720                                                 changed = TRUE;
9721                                                 continue;
9722                                         }
9723                                 }
9724
9725                                 /* detect and optimize to unsigned compares checks like: if (v < 0 || v > limit */
9726                                 if (bb->last_ins && bb->last_ins->opcode == CEE_BLT && bb->last_ins->inst_left->inst_right->opcode == OP_ICONST) {
9727                                         if (try_unsigned_compare (cfg, bb)) {
9728                                                 /*g_print ("applied in bb %d (->%d) %s\n", bb->block_num, bb->last_ins->inst_target_bb->block_num, mono_method_full_name (cfg->method, TRUE));*/
9729                                                 changed = TRUE;
9730                                                 continue;
9731                                         }
9732                                 }
9733
9734                                 if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
9735                                         if (bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region)) {
9736                                                 /* Reverse the branch */
9737                                                 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
9738                                                 bbn = bb->last_ins->inst_false_bb;
9739                                                 bb->last_ins->inst_false_bb = bb->last_ins->inst_true_bb;
9740                                                 bb->last_ins->inst_true_bb = bbn;
9741
9742                                                 move_basic_block_to_end (cfg, bb->last_ins->inst_true_bb);
9743                                                 if (cfg->verbose_level > 2)
9744                                                         g_print ("cbranch to throw block triggered %d.\n", 
9745                                                                          bb->block_num);
9746                                         }
9747                                 }
9748                         }
9749                 }
9750         } while (changed && (niterations > 0));
9751
9752 }
9753
9754 static void
9755 mono_compile_create_vars (MonoCompile *cfg)
9756 {
9757         MonoMethodSignature *sig;
9758         MonoMethodHeader *header;
9759         int i;
9760
9761         header = mono_method_get_header (cfg->method);
9762
9763         sig = mono_method_signature (cfg->method);
9764         
9765         if (!MONO_TYPE_IS_VOID (sig->ret)) {
9766                 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
9767                 cfg->ret->opcode = OP_RETARG;
9768                 cfg->ret->inst_vtype = sig->ret;
9769                 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
9770         }
9771         if (cfg->verbose_level > 2)
9772                 g_print ("creating vars\n");
9773
9774         cfg->args = mono_mempool_alloc0 (cfg->mempool, (sig->param_count + sig->hasthis) * sizeof (MonoInst*));
9775
9776         if (sig->hasthis)
9777                 cfg->args [0] = mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
9778
9779         for (i = 0; i < sig->param_count; ++i) {
9780                 cfg->args [i + sig->hasthis] = mono_compile_create_var (cfg, sig->params [i], OP_ARG);
9781                 if (sig->params [i]->byref) {
9782                         cfg->disable_ssa = TRUE;
9783                 }
9784         }
9785
9786         cfg->locals_start = cfg->num_varinfo;
9787
9788         if (cfg->verbose_level > 2)
9789                 g_print ("creating locals\n");
9790         for (i = 0; i < header->num_locals; ++i)
9791                 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
9792         if (cfg->verbose_level > 2)
9793                 g_print ("locals done\n");
9794
9795 #ifdef MONO_ARCH_HAVE_CREATE_VARS
9796         mono_arch_create_vars (cfg);
9797 #endif
9798 }
9799
9800 void
9801 mono_print_code (MonoCompile *cfg)
9802 {
9803         MonoBasicBlock *bb;
9804         
9805         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9806                 MonoInst *tree = bb->code;      
9807
9808                 if (!tree)
9809                         continue;
9810                 
9811                 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
9812
9813                 for (; tree; tree = tree->next) {
9814                         mono_print_tree (tree);
9815                         g_print ("\n");
9816                 }
9817
9818                 if (bb->last_ins)
9819                         bb->last_ins->next = NULL;
9820         }
9821 }
9822
9823 extern const char * const mono_burg_rule_string [];
9824
9825 static void
9826 emit_state (MonoCompile *cfg, MBState *state, int goal)
9827 {
9828         MBState *kids [10];
9829         int ern = mono_burg_rule (state, goal);
9830         const guint16 *nts = mono_burg_nts_data + mono_burg_nts [ern];
9831
9832         //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
9833         switch (goal) {
9834         case MB_NTERM_reg:
9835                 //if (state->reg2)
9836                 //      state->reg1 = state->reg2; /* chain rule */
9837                 //else
9838 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
9839                 if (!state->reg1)
9840 #endif
9841                         state->reg1 = mono_regstate_next_int (cfg->rs);
9842                 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
9843                 break;
9844         case MB_NTERM_lreg:
9845                 state->reg1 = mono_regstate_next_int (cfg->rs);
9846                 state->reg2 = mono_regstate_next_int (cfg->rs);
9847                 break;
9848         case MB_NTERM_freg:
9849 #ifdef MONO_ARCH_SOFT_FLOAT
9850                 state->reg1 = mono_regstate_next_int (cfg->rs);
9851                 state->reg2 = mono_regstate_next_int (cfg->rs);
9852 #else
9853                 state->reg1 = mono_regstate_next_float (cfg->rs);
9854 #endif
9855                 break;
9856         default:
9857 #ifdef MONO_ARCH_ENABLE_EMIT_STATE_OPT
9858                 /*
9859                  * Enabling this might cause bugs to surface in the local register
9860                  * allocators on some architectures like x86.
9861                  */
9862                 if ((state->tree->ssa_op == MONO_SSA_STORE) && (state->left->tree->opcode == OP_REGVAR)) {
9863                         /* Do not optimize away reg-reg moves */
9864                         if (! ((state->right->tree->ssa_op == MONO_SSA_LOAD) && (state->right->left->tree->opcode == OP_REGVAR))) {
9865                                 state->right->reg1 = state->left->tree->dreg;
9866                         }
9867                 }
9868 #endif
9869
9870                 /* do nothing */
9871                 break;
9872         }
9873         if (nts [0]) {
9874                 mono_burg_kids (state, ern, kids);
9875
9876                 emit_state (cfg, kids [0], nts [0]);
9877                 if (nts [1]) {
9878                         emit_state (cfg, kids [1], nts [1]);
9879                         if (nts [2]) {
9880                                 g_assert (!nts [3]);
9881                                 emit_state (cfg, kids [2], nts [2]);
9882                         }
9883                 }
9884         }
9885
9886 //      g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
9887         mono_burg_emit (ern, state, state->tree, cfg);
9888 }
9889
9890 #define DEBUG_SELECTION
9891
9892 static void 
9893 mini_select_instructions (MonoCompile *cfg)
9894 {
9895         MonoBasicBlock *bb;
9896         
9897         cfg->state_pool = mono_mempool_new ();
9898         cfg->rs = mono_regstate_new ();
9899
9900         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9901                 if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins) &&
9902                     bb->next_bb != bb->last_ins->inst_false_bb) {
9903
9904                         /* we are careful when inverting, since bugs like #59580
9905                          * could show up when dealing with NaNs.
9906                          */
9907                         if (MONO_IS_COND_BRANCH_NOFP(bb->last_ins) && bb->next_bb == bb->last_ins->inst_true_bb) {
9908                                 MonoBasicBlock *tmp =  bb->last_ins->inst_true_bb;
9909                                 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
9910                                 bb->last_ins->inst_false_bb = tmp;
9911
9912                                 bb->last_ins->opcode = reverse_branch_op (bb->last_ins->opcode);
9913                         } else {                        
9914                                 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
9915                                 inst->opcode = OP_BR;
9916                                 inst->inst_target_bb = bb->last_ins->inst_false_bb;
9917                                 mono_bblock_add_inst (bb, inst);
9918                         }
9919                 }
9920         }
9921
9922 #ifdef DEBUG_SELECTION
9923         if (cfg->verbose_level >= 4) {
9924         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9925                 MonoInst *tree = bb->code;      
9926                 g_print ("DUMP BLOCK %d:\n", bb->block_num);
9927                 if (!tree)
9928                         continue;
9929                 for (; tree; tree = tree->next) {
9930                         mono_print_tree (tree);
9931                         g_print ("\n");
9932                 }
9933         }
9934         }
9935 #endif
9936
9937         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9938                 MonoInst *tree = bb->code, *next;       
9939                 MBState *mbstate;
9940
9941                 if (!tree)
9942                         continue;
9943                 bb->code = NULL;
9944                 bb->last_ins = NULL;
9945                 
9946                 cfg->cbb = bb;
9947                 mono_regstate_reset (cfg->rs);
9948
9949 #ifdef DEBUG_SELECTION
9950                 if (cfg->verbose_level >= 3)
9951                         g_print ("LABEL BLOCK %d:\n", bb->block_num);
9952 #endif
9953                 for (; tree; tree = next) {
9954                         next = tree->next;
9955 #ifdef DEBUG_SELECTION
9956                         if (cfg->verbose_level >= 3) {
9957                                 mono_print_tree (tree);
9958                                 g_print ("\n");
9959                         }
9960 #endif
9961
9962                         if (!(mbstate = mono_burg_label (tree, cfg))) {
9963                                 g_warning ("unable to label tree %p", tree);
9964                                 mono_print_tree (tree);
9965                                 g_print ("\n");                         
9966                                 g_assert_not_reached ();
9967                         }
9968                         emit_state (cfg, mbstate, MB_NTERM_stmt);
9969                 }
9970                 bb->max_vreg = cfg->rs->next_vreg;
9971
9972                 if (bb->last_ins)
9973                         bb->last_ins->next = NULL;
9974
9975                 mono_mempool_empty (cfg->state_pool); 
9976         }
9977         mono_mempool_destroy (cfg->state_pool); 
9978 }
9979
9980 void
9981 mono_codegen (MonoCompile *cfg)
9982 {
9983         MonoJumpInfo *patch_info;
9984         MonoBasicBlock *bb;
9985         int i, max_epilog_size;
9986         guint8 *code;
9987
9988         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
9989                 cfg->spill_count = 0;
9990                 /* we reuse dfn here */
9991                 /* bb->dfn = bb_count++; */
9992                 mono_arch_local_regalloc (cfg, bb);
9993         }
9994
9995         if (cfg->prof_options & MONO_PROFILE_COVERAGE)
9996                 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
9997
9998         code = mono_arch_emit_prolog (cfg);
9999
10000         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
10001                 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
10002
10003         cfg->code_len = code - cfg->native_code;
10004         cfg->prolog_end = cfg->code_len;
10005
10006         mono_debug_open_method (cfg);
10007
10008         /* emit code all basic blocks */
10009         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
10010                 bb->native_offset = cfg->code_len;
10011                 mono_arch_output_basic_block (cfg, bb);
10012
10013                 if (bb == cfg->bb_exit) {
10014                         cfg->epilog_begin = cfg->code_len;
10015
10016                         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE) {
10017                                 code = cfg->native_code + cfg->code_len;
10018                                 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
10019                                 cfg->code_len = code - cfg->native_code;
10020                                 g_assert (cfg->code_len < cfg->code_size);
10021                         }
10022
10023                         mono_arch_emit_epilog (cfg);
10024                 }
10025         }
10026
10027         mono_arch_emit_exceptions (cfg);
10028
10029         max_epilog_size = 0;
10030
10031         code = cfg->native_code + cfg->code_len;
10032
10033         /* we always allocate code in cfg->domain->code_mp to increase locality */
10034         cfg->code_size = cfg->code_len + max_epilog_size;
10035         /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
10036
10037         if (cfg->method->dynamic) {
10038                 /* Allocate the code into a separate memory pool so it can be freed */
10039                 cfg->dynamic_info = g_new0 (MonoJitDynamicMethodInfo, 1);
10040                 cfg->dynamic_info->code_mp = mono_code_manager_new_dynamic ();
10041                 mono_domain_lock (cfg->domain);
10042                 mono_dynamic_code_hash_insert (cfg->domain, cfg->method, cfg->dynamic_info);
10043                 mono_domain_unlock (cfg->domain);
10044
10045                 code = mono_code_manager_reserve (cfg->dynamic_info->code_mp, cfg->code_size);
10046         } else {
10047                 mono_domain_lock (cfg->domain);
10048                 code = mono_code_manager_reserve (cfg->domain->code_mp, cfg->code_size);
10049                 mono_domain_unlock (cfg->domain);
10050         }
10051
10052         memcpy (code, cfg->native_code, cfg->code_len);
10053         g_free (cfg->native_code);
10054         cfg->native_code = code;
10055         code = cfg->native_code + cfg->code_len;
10056   
10057         /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
10058         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
10059                 switch (patch_info->type) {
10060                 case MONO_PATCH_INFO_ABS: {
10061                         MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
10062                         if (info) {
10063                                 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
10064                                 if ((cfg->method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && 
10065                                         strstr (cfg->method->name, info->name))
10066                                         /*
10067                                          * This is an icall wrapper, and this is a call to the
10068                                          * wrapped function.
10069                                          */
10070                                         ;
10071                                 else {
10072                                         /* for these array methods we currently register the same function pointer
10073                                          * since it's a vararg function. But this means that mono_find_jit_icall_by_addr ()
10074                                          * will return the incorrect one depending on the order they are registered.
10075                                          * See tests/test-arr.cs
10076                                          */
10077                                         if (strstr (info->name, "ves_array_new_va_") == NULL && strstr (info->name, "ves_array_element_address_") == NULL) {
10078                                                 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
10079                                                 patch_info->data.name = info->name;
10080                                         }
10081                                 }
10082                         }
10083                         else {
10084                                 MonoVTable *vtable = mono_find_class_init_trampoline_by_addr (patch_info->data.target);
10085                                 if (vtable) {
10086                                         patch_info->type = MONO_PATCH_INFO_CLASS_INIT;
10087                                         patch_info->data.klass = vtable->klass;
10088                                 }
10089                         }
10090                         break;
10091                 }
10092                 case MONO_PATCH_INFO_SWITCH: {
10093                         gpointer *table;
10094                         if (cfg->method->dynamic) {
10095                                 table = mono_code_manager_reserve (cfg->dynamic_info->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
10096                         } else {
10097                                 mono_domain_lock (cfg->domain);
10098                                 table = mono_code_manager_reserve (cfg->domain->code_mp, sizeof (gpointer) * patch_info->data.table->table_size);
10099                                 mono_domain_unlock (cfg->domain);
10100                         }
10101
10102                         if (!cfg->compile_aot)
10103                                 /* In the aot case, the patch already points to the correct location */
10104                                 patch_info->ip.i = patch_info->ip.label->inst_c0;
10105                         for (i = 0; i < patch_info->data.table->table_size; i++) {
10106                                 table [i] = GINT_TO_POINTER (patch_info->data.table->table [i]->native_offset);
10107                         }
10108                         patch_info->data.table->table = (MonoBasicBlock**)table;
10109                         break;
10110                 }
10111                 default:
10112                         /* do nothing */
10113                         break;
10114                 }
10115         }
10116
10117 #ifdef VALGRIND_JIT_REGISTER_MAP
10118 if (valgrind_register){
10119                 char* nm = mono_method_full_name (cfg->method, TRUE);
10120                 VALGRIND_JIT_REGISTER_MAP (nm, cfg->native_code, cfg->native_code + cfg->code_len);
10121                 g_free (nm);
10122         }
10123 #endif
10124  
10125         if (cfg->verbose_level > 0) {
10126                 char* nm = mono_method_full_name (cfg->method, TRUE);
10127                 g_print ("Method %s emitted at %p to %p (code length %d) [%s]\n", 
10128                                  nm, 
10129                                  cfg->native_code, cfg->native_code + cfg->code_len, cfg->code_len, cfg->domain->friendly_name);
10130                 g_free (nm);
10131         }
10132
10133 #ifdef MONO_ARCH_HAVE_SAVE_UNWIND_INFO
10134         mono_arch_save_unwind_info (cfg);
10135 #endif
10136         
10137         mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info, cfg->run_cctors);
10138
10139         if (cfg->method->dynamic) {
10140                 mono_code_manager_commit (cfg->dynamic_info->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
10141         } else {
10142                 mono_domain_lock (cfg->domain);
10143                 mono_code_manager_commit (cfg->domain->code_mp, cfg->native_code, cfg->code_size, cfg->code_len);
10144                 mono_domain_unlock (cfg->domain);
10145         }
10146         
10147         mono_arch_flush_icache (cfg->native_code, cfg->code_len);
10148
10149         mono_debug_close_method (cfg);
10150 }
10151
10152
10153
10154 static void
10155 remove_critical_edges (MonoCompile *cfg) {
10156         MonoBasicBlock *bb;
10157         MonoBasicBlock *previous_bb;
10158         
10159         if (cfg->verbose_level > 3) {
10160                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
10161                         int i;
10162                         printf ("remove_critical_edges %s, BEFORE BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
10163                         for (i = 0; i < bb->in_count; i++) {
10164                                 printf (" %d", bb->in_bb [i]->block_num);
10165                         }
10166                         printf (") (out:");
10167                         for (i = 0; i < bb->out_count; i++) {
10168                                 printf (" %d", bb->out_bb [i]->block_num);
10169                         }
10170                         printf (")");
10171                         if (bb->last_ins != NULL) {
10172                                 printf (" ");
10173                                 mono_print_tree (bb->last_ins);
10174                         }
10175                         printf ("\n");
10176                 }
10177         }
10178         
10179         for (previous_bb = cfg->bb_entry, bb = previous_bb->next_bb; bb != NULL; previous_bb = previous_bb->next_bb, bb = bb->next_bb) {
10180                 if (bb->in_count > 1) {
10181                         int in_bb_index;
10182                         for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
10183                                 MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
10184                                 if (in_bb->out_count > 1) {
10185                                         MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
10186                                         new_bb->block_num = cfg->num_bblocks++;
10187 //                                      new_bb->real_offset = bb->real_offset;
10188                                         new_bb->region = bb->region;
10189                                         
10190                                         /* Do not alter the CFG while altering the BB list */
10191                                         if (previous_bb->region == bb->region) {
10192                                                 if (previous_bb != cfg->bb_entry) {
10193                                                         /* If previous_bb "followed through" to bb, */
10194                                                         /* keep it linked with a OP_BR */
10195                                                         if ((previous_bb->last_ins == NULL) ||
10196                                                                         ((previous_bb->last_ins->opcode != OP_BR) &&
10197                                                                         (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
10198                                                                         (previous_bb->last_ins->opcode != CEE_SWITCH))) {
10199                                                                 int i;
10200                                                                 /* Make sure previous_bb really falls through bb */
10201                                                                 for (i = 0; i < previous_bb->out_count; i++) {
10202                                                                         if (previous_bb->out_bb [i] == bb) {
10203                                                                                 MonoInst *jump;
10204                                                                                 MONO_INST_NEW (cfg, jump, OP_BR);
10205                                                                                 MONO_ADD_INS (previous_bb, jump);
10206                                                                                 jump->cil_code = previous_bb->cil_code;
10207                                                                                 jump->inst_target_bb = bb;
10208                                                                                 break;
10209                                                                         }
10210                                                                 }
10211                                                         }
10212                                                 } else {
10213                                                         /* We cannot add any inst to the entry BB, so we must */
10214                                                         /* put a new BB in the middle to hold the OP_BR */
10215                                                         MonoInst *jump;
10216                                                         MonoBasicBlock *new_bb_after_entry = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
10217                                                         new_bb_after_entry->block_num = cfg->num_bblocks++;
10218 //                                                      new_bb_after_entry->real_offset = bb->real_offset;
10219                                                         new_bb_after_entry->region = bb->region;
10220                                                         
10221                                                         MONO_INST_NEW (cfg, jump, OP_BR);
10222                                                         MONO_ADD_INS (new_bb_after_entry, jump);
10223                                                         jump->cil_code = bb->cil_code;
10224                                                         jump->inst_target_bb = bb;
10225                                                         
10226                                                         previous_bb->next_bb = new_bb_after_entry;
10227                                                         previous_bb = new_bb_after_entry;
10228                                                         
10229                                                         if (cfg->verbose_level > 2) {
10230                                                                 printf ("remove_critical_edges %s, added helper BB%d jumping to BB%d\n", mono_method_full_name (cfg->method, TRUE), new_bb_after_entry->block_num, bb->block_num);
10231                                                         }
10232                                                 }
10233                                         }
10234                                         
10235                                         /* Insert new_bb in the BB list */
10236                                         previous_bb->next_bb = new_bb;
10237                                         new_bb->next_bb = bb;
10238                                         previous_bb = new_bb;
10239                                         
10240                                         /* Setup in_bb and out_bb */
10241                                         new_bb->in_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
10242                                         new_bb->in_bb [0] = in_bb;
10243                                         new_bb->in_count = 1;
10244                                         new_bb->out_bb = mono_mempool_alloc ((cfg)->mempool, sizeof (MonoBasicBlock*));
10245                                         new_bb->out_bb [0] = bb;
10246                                         new_bb->out_count = 1;
10247                                         
10248                                         /* Relink in_bb and bb to (from) new_bb */
10249                                         replace_out_block (in_bb, bb, new_bb);
10250                                         replace_out_block_in_code (in_bb, bb, new_bb);
10251                                         replace_in_block (bb, in_bb, new_bb);
10252                                         
10253                                         if (cfg->verbose_level > 2) {
10254                                                 printf ("remove_critical_edges %s, removed critical edge from BB%d to BB%d (added BB%d)\n", mono_method_full_name (cfg->method, TRUE), in_bb->block_num, bb->block_num, new_bb->block_num);
10255                                         }
10256                                 }
10257                         }
10258                 }
10259         }
10260         
10261         if (cfg->verbose_level > 3) {
10262                 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
10263                         int i;
10264                         printf ("remove_critical_edges %s, AFTER BB%d (in:", mono_method_full_name (cfg->method, TRUE), bb->block_num);
10265                         for (i = 0; i < bb->in_count; i++) {
10266                                 printf (" %d", bb->in_bb [i]->block_num);
10267                         }
10268                         printf (") (out:");
10269                         for (i = 0; i < bb->out_count; i++) {
10270                                 printf (" %d", bb->out_bb [i]->block_num);
10271                         }
10272                         printf (")");
10273                         if (bb->last_ins != NULL) {
10274                                 printf (" ");
10275                                 mono_print_tree (bb->last_ins);
10276                         }
10277                         printf ("\n");
10278                 }
10279         }
10280 }
10281
10282 /*
10283  * mini_method_compile:
10284  * @method: the method to compile
10285  * @opts: the optimization flags to use
10286  * @domain: the domain where the method will be compiled in
10287  * @run_cctors: whether we should run type ctors if possible
10288  * @compile_aot: whether this is an AOT compilation
10289  * @parts: debug flag
10290  *
10291  * Returns: a MonoCompile* pointer. Caller must check the exception_type
10292  * field in the returned struct to see if compilation succeded.
10293  */
10294 MonoCompile*
10295 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, gboolean run_cctors, gboolean compile_aot, int parts)
10296 {
10297         MonoMethodHeader *header = mono_method_get_header (method);
10298         guint8 *ip;
10299         MonoCompile *cfg;
10300         MonoJitInfo *jinfo;
10301         int dfn = 0, i, code_size_ratio;
10302         gboolean deadce_has_run = FALSE;
10303
10304         mono_jit_stats.methods_compiled++;
10305         if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
10306                 mono_profiler_method_jit (method);
10307
10308         cfg = g_new0 (MonoCompile, 1);
10309         cfg->method = method;
10310         cfg->mempool = mono_mempool_new ();
10311         cfg->opt = opts;
10312         cfg->prof_options = mono_profiler_get_events ();
10313         cfg->run_cctors = run_cctors;
10314         cfg->domain = domain;
10315         cfg->verbose_level = mini_verbose;
10316         cfg->compile_aot = compile_aot;
10317         cfg->skip_visibility = method->skip_visibility;
10318         cfg->token_info_hash = g_hash_table_new (NULL, NULL);
10319         if (!header) {
10320                 cfg->exception_type = MONO_EXCEPTION_INVALID_PROGRAM;
10321                 cfg->exception_message = g_strdup_printf ("Missing or incorrect header for method %s", cfg->method->name);
10322                 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
10323                         mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
10324                 return cfg;
10325         }
10326
10327         ip = (guint8 *)header->code;
10328
10329         cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX * header->max_stack);
10330         cfg->aliasing_info = NULL;
10331         
10332         if (cfg->verbose_level > 2)
10333                 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
10334
10335         /*
10336          * create MonoInst* which represents arguments and local variables
10337          */
10338         mono_compile_create_vars (cfg);
10339
10340         if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
10341                 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
10342                         mono_profiler_method_end_jit (method, NULL, MONO_PROFILE_FAILED);
10343                 /* cfg contains the details of the failure, so let the caller cleanup */
10344                 return cfg;
10345         }
10346
10347         mono_jit_stats.basic_blocks += cfg->num_bblocks;
10348         mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
10349
10350         if ((cfg->num_varinfo > 2000) && !cfg->compile_aot) {
10351                 /* 
10352                  * we disable some optimizations if there are too many variables
10353                  * because JIT time may become too expensive. The actual number needs 
10354                  * to be tweaked and eventually the non-linear algorithms should be fixed.
10355                  */
10356                 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
10357                 cfg->disable_ssa = TRUE;
10358         }
10359
10360         /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
10361
10362         if (cfg->opt & MONO_OPT_BRANCH)
10363                 optimize_branches (cfg);
10364
10365         if (cfg->opt & MONO_OPT_SSAPRE) {
10366                 remove_critical_edges (cfg);
10367         }
10368
10369         /* Depth-first ordering on basic blocks */
10370         cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
10371
10372         df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
10373         if (cfg->num_bblocks != dfn + 1) {
10374                 MonoBasicBlock *bb;
10375
10376                 cfg->num_bblocks = dfn + 1;
10377
10378                 if (!header->clauses) {
10379                         /* remove unreachable code, because the code in them may be 
10380                          * inconsistent  (access to dead variables for example) */
10381                         for (bb = cfg->bb_entry; bb;) {
10382                                 MonoBasicBlock *bbn = bb->next_bb;
10383
10384                                 if (bbn && bbn->region == -1 && !bbn->dfn) {
10385                                         if (cfg->verbose_level > 1)
10386                                                 g_print ("found unreachable code in BB%d\n", bbn->block_num);
10387                                         bb->next_bb = bbn->next_bb;
10388                                         nullify_basic_block (bbn);                      
10389                                 } else {
10390                                         bb = bb->next_bb;
10391                                 }
10392                         }
10393                 }
10394         }
10395
10396         if (cfg->opt & MONO_OPT_LOOP) {
10397                 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
10398                 mono_compute_natural_loops (cfg);
10399         }
10400
10401         /* after method_to_ir */
10402         if (parts == 1)
10403                 return cfg;
10404
10405 //#define DEBUGSSA "logic_run"
10406 #define DEBUGSSA_CLASS "Tests"
10407 #ifdef DEBUGSSA
10408
10409         if (!header->num_clauses && !cfg->disable_ssa) {
10410                 mono_local_cprop (cfg);
10411                 mono_ssa_compute (cfg);
10412         }
10413 #else 
10414
10415         /* fixme: add all optimizations which requires SSA */
10416         if (cfg->opt & (MONO_OPT_SSA | MONO_OPT_ABCREM | MONO_OPT_SSAPRE)) {
10417                 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
10418                         mono_local_cprop (cfg);
10419                         mono_ssa_compute (cfg);
10420
10421                         if (cfg->verbose_level >= 2) {
10422                                 print_dfn (cfg);
10423                         }
10424                 }
10425         }
10426 #endif
10427
10428         /* after SSA translation */
10429         if (parts == 2)
10430                 return cfg;
10431
10432         if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
10433                 if (cfg->comp_done & MONO_COMP_SSA) {
10434                         mono_ssa_cprop (cfg);
10435                 } else {
10436                         mono_local_cprop (cfg);
10437                 }
10438         }
10439
10440         if (cfg->comp_done & MONO_COMP_SSA) {                   
10441                 //mono_ssa_deadce (cfg);
10442
10443                 //mono_ssa_strength_reduction (cfg);
10444
10445                 if (cfg->opt & MONO_OPT_SSAPRE) {
10446                         mono_perform_ssapre (cfg);
10447                         //mono_local_cprop (cfg);
10448                 }
10449                 
10450                 if (cfg->opt & MONO_OPT_DEADCE) {
10451                         mono_ssa_deadce (cfg);
10452                         deadce_has_run = TRUE;
10453                 }
10454                 
10455                 if ((cfg->flags & MONO_CFG_HAS_LDELEMA) && (cfg->opt & MONO_OPT_ABCREM))
10456                         mono_perform_abc_removal (cfg);
10457                 
10458                 mono_ssa_remove (cfg);
10459
10460                 if (cfg->opt & MONO_OPT_BRANCH)
10461                         optimize_branches (cfg);
10462         }
10463
10464         /* after SSA removal */
10465         if (parts == 3)
10466                 return cfg;
10467
10468         if (cfg->verbose_level > 4) {
10469                 printf ("BEFORE DECOMPSE START\n");
10470                 mono_print_code (cfg);
10471                 printf ("BEFORE DECOMPSE END\n");
10472         }
10473         
10474         decompose_pass (cfg);
10475
10476         if (cfg->got_var) {
10477                 GList *regs;
10478
10479                 g_assert (cfg->got_var_allocated);
10480
10481                 /* 
10482                  * Allways allocate the GOT var to a register, because keeping it
10483                  * in memory will increase the number of live temporaries in some
10484                  * code created by inssel.brg, leading to the well known spills+
10485                  * branches problem. Testcase: mcs crash in 
10486                  * System.MonoCustomAttrs:GetCustomAttributes.
10487                  */
10488                 regs = mono_arch_get_global_int_regs (cfg);
10489                 g_assert (regs);
10490                 cfg->got_var->opcode = OP_REGVAR;
10491                 cfg->got_var->dreg = GPOINTER_TO_INT (regs->data);
10492                 cfg->used_int_regs |= 1LL << cfg->got_var->dreg;
10493                 
10494                 g_list_free (regs);
10495         }
10496
10497         /* todo: remove code when we have verified that the liveness for try/catch blocks
10498          * works perfectly 
10499          */
10500         /* 
10501          * Currently, this can't be commented out since exception blocks are not
10502          * processed during liveness analysis.
10503          */
10504         mono_liveness_handle_exception_clauses (cfg);
10505
10506         if (cfg->opt & MONO_OPT_LINEARS) {
10507                 GList *vars, *regs;
10508                 
10509                 /* For now, compute aliasing info only if needed for deadce... */
10510                 if ((cfg->opt & MONO_OPT_DEADCE) && (! deadce_has_run) && (header->num_clauses == 0)) {
10511                         cfg->aliasing_info = mono_build_aliasing_information (cfg);
10512                 }
10513
10514                 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
10515                 cfg->comp_done &= ~MONO_COMP_LIVENESS;
10516                 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
10517                         mono_analyze_liveness (cfg);
10518
10519                 if (cfg->aliasing_info != NULL) {
10520                         mono_aliasing_deadce (cfg->aliasing_info);
10521                         deadce_has_run = TRUE;
10522                 }
10523                 
10524                 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
10525                         regs = mono_arch_get_global_int_regs (cfg);
10526                         if (cfg->got_var)
10527                                 regs = g_list_delete_link (regs, regs);
10528                         mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
10529                 }
10530                 
10531                 if (cfg->aliasing_info != NULL) {
10532                         mono_destroy_aliasing_information (cfg->aliasing_info);
10533                         cfg->aliasing_info = NULL;
10534                 }
10535         }
10536
10537         //mono_print_code (cfg);
10538
10539     //print_dfn (cfg);
10540         
10541         /* variables are allocated after decompose, since decompose could create temps */
10542         mono_arch_allocate_vars (cfg);
10543
10544         if (cfg->opt & MONO_OPT_CFOLD)
10545                 mono_constant_fold (cfg);
10546
10547         mini_select_instructions (cfg);
10548
10549         mono_codegen (cfg);
10550         if (cfg->verbose_level >= 2) {
10551                 char *id =  mono_method_full_name (cfg->method, FALSE);
10552                 mono_disassemble_code (cfg, cfg->native_code, cfg->code_len, id + 3);
10553                 g_free (id);
10554         }
10555         
10556         if (cfg->method->dynamic) {
10557                 jinfo = g_malloc0 (sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
10558         } else {
10559                 /* we access cfg->domain->mp */
10560                 mono_domain_lock (cfg->domain);
10561                 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo) + (header->num_clauses * sizeof (MonoJitExceptionInfo)));
10562                 mono_domain_unlock (cfg->domain);
10563         }
10564
10565         jinfo->method = method;
10566         jinfo->code_start = cfg->native_code;
10567         jinfo->code_size = cfg->code_len;
10568         jinfo->used_regs = cfg->used_int_regs;
10569         jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
10570         jinfo->cas_inited = FALSE; /* initialization delayed at the first stalk walk using this method */
10571
10572         if (header->num_clauses) {
10573                 int i;
10574
10575                 jinfo->num_clauses = header->num_clauses;
10576
10577                 for (i = 0; i < header->num_clauses; i++) {
10578                         MonoExceptionClause *ec = &header->clauses [i];
10579                         MonoJitExceptionInfo *ei = &jinfo->clauses [i];
10580                         MonoBasicBlock *tblock;
10581                         MonoInst *exvar;
10582
10583                         ei->flags = ec->flags;
10584
10585                         exvar = mono_find_exvar_for_offset (cfg, ec->handler_offset);
10586                         ei->exvar_offset = exvar ? exvar->inst_offset : 0;
10587
10588                         if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
10589                                 tblock = cfg->cil_offset_to_bb [ec->data.filter_offset];
10590                                 g_assert (tblock);
10591                                 ei->data.filter = cfg->native_code + tblock->native_offset;
10592                         } else {
10593                                 ei->data.catch_class = ec->data.catch_class;
10594                         }
10595
10596                         tblock = cfg->cil_offset_to_bb [ec->try_offset];
10597                         g_assert (tblock);
10598                         ei->try_start = cfg->native_code + tblock->native_offset;
10599                         g_assert (tblock->native_offset);
10600                         tblock = cfg->cil_offset_to_bb [ec->try_offset + ec->try_len];
10601                         g_assert (tblock);
10602                         ei->try_end = cfg->native_code + tblock->native_offset;
10603                         g_assert (tblock->native_offset);
10604                         tblock = cfg->cil_offset_to_bb [ec->handler_offset];
10605                         g_assert (tblock);
10606                         ei->handler_start = cfg->native_code + tblock->native_offset;
10607                 }
10608         }
10609
10610         cfg->jit_info = jinfo;
10611 #if defined(__arm__)
10612         mono_arch_fixup_jinfo (cfg);
10613 #endif
10614
10615         mono_domain_lock (cfg->domain);
10616         mono_jit_info_table_add (cfg->domain, jinfo);
10617
10618         if (cfg->method->dynamic)
10619                 mono_dynamic_code_hash_lookup (cfg->domain, cfg->method)->ji = jinfo;
10620         mono_domain_unlock (cfg->domain);
10621
10622         /* collect statistics */
10623         mono_jit_stats.allocated_code_size += cfg->code_len;
10624         code_size_ratio = cfg->code_len;
10625         if (code_size_ratio > mono_jit_stats.biggest_method_size) {
10626                         mono_jit_stats.biggest_method_size = code_size_ratio;
10627                         mono_jit_stats.biggest_method = method;
10628         }
10629         code_size_ratio = (code_size_ratio * 100) / mono_method_get_header (method)->code_size;
10630         if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
10631                 mono_jit_stats.max_code_size_ratio = code_size_ratio;
10632                 mono_jit_stats.max_ratio_method = method;
10633         }
10634         mono_jit_stats.native_code_size += cfg->code_len;
10635
10636         if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
10637                 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
10638
10639         return cfg;
10640 }
10641
10642 static gpointer
10643 mono_jit_compile_method_inner (MonoMethod *method, MonoDomain *target_domain, int opt)
10644 {
10645         MonoCompile *cfg;
10646         gpointer code = NULL;
10647         MonoJitInfo *info;
10648
10649         method = mono_get_inflated_method (method);
10650
10651 #ifdef MONO_USE_AOT_COMPILER
10652         if ((opt & MONO_OPT_AOT) && !(mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)) {
10653                 MonoDomain *domain = mono_domain_get ();
10654
10655                 mono_class_init (method->klass);
10656
10657                 mono_domain_lock (domain);
10658                 if ((code = mono_aot_get_method (domain, method))) {
10659                         mono_domain_unlock (domain);
10660                         mono_runtime_class_init (mono_class_vtable (domain, method->klass));
10661                         return code;
10662                 }
10663
10664                 mono_domain_unlock (domain);
10665         }
10666 #endif
10667
10668         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
10669             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
10670                 MonoMethod *nm;
10671                 MonoMethodPInvoke* piinfo = (MonoMethodPInvoke *) method;
10672
10673                 if (method->iflags & METHOD_IMPL_ATTRIBUTE_NATIVE && !MONO_CLASS_IS_IMPORT(method->klass))
10674                         g_error ("Method '%s' in assembly '%s' contains native code and mono can't run it. The assembly was probably created by Managed C++.\n", mono_method_full_name (method, TRUE), method->klass->image->name);
10675
10676                 if (!piinfo->addr) {
10677                         if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
10678                                 piinfo->addr = mono_lookup_internal_call (method);
10679                         else
10680                                 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
10681                                         mono_lookup_pinvoke_call (method, NULL, NULL);
10682                 }
10683                         nm = mono_marshal_get_native_wrapper (method);
10684                         return mono_get_addr_from_ftnptr (mono_compile_method (nm));
10685
10686                         //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE) 
10687                         //mono_debug_add_wrapper (method, nm);
10688         } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
10689                 const char *name = method->name;
10690                 MonoMethod *nm;
10691
10692                 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
10693                         if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
10694                                 MonoJitICallInfo *mi = mono_find_jit_icall_by_name ("mono_delegate_ctor");
10695                                 g_assert (mi);
10696                                 return mono_get_addr_from_ftnptr ((gpointer)mono_icall_get_wrapper (mi));
10697                         } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
10698                                 nm = mono_marshal_get_delegate_invoke (method);
10699                                         return mono_get_addr_from_ftnptr (mono_compile_method (nm));
10700                         } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
10701                                 nm = mono_marshal_get_delegate_begin_invoke (method);
10702                                 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
10703                         } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
10704                                 nm = mono_marshal_get_delegate_end_invoke (method);
10705                                 return mono_get_addr_from_ftnptr (mono_compile_method (nm));
10706                         }
10707                 }
10708                 return NULL;
10709         }
10710
10711         cfg = mini_method_compile (method, opt, target_domain, TRUE, FALSE, 0);
10712
10713         switch (cfg->exception_type) {
10714         case MONO_EXCEPTION_NONE: break;
10715         case MONO_EXCEPTION_TYPE_LOAD:
10716         case MONO_EXCEPTION_MISSING_FIELD:
10717         case MONO_EXCEPTION_MISSING_METHOD: {
10718                 /* Throw a type load exception if needed */
10719                 MonoLoaderError *error = mono_loader_get_last_error ();
10720                 MonoException *ex;
10721
10722                 if (error) {
10723                         ex = mono_loader_error_prepare_exception (error);
10724                 } else {
10725                         if (cfg->exception_ptr) {
10726                                 ex = mono_class_get_exception_for_failure (cfg->exception_ptr);
10727                         } else {
10728                                 if (cfg->exception_type == MONO_EXCEPTION_MISSING_FIELD)
10729                                         ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", cfg->exception_message);
10730                                 else if (cfg->exception_type == MONO_EXCEPTION_MISSING_METHOD)
10731                                         ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", cfg->exception_message);
10732                                 else if (cfg->exception_type == MONO_EXCEPTION_TYPE_LOAD)
10733                                         ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", cfg->exception_message);
10734                                 else
10735                                         g_assert_not_reached ();
10736                         }
10737                 }
10738                 mono_destroy_compile (cfg);
10739                 mono_raise_exception (ex);
10740                 break;
10741         }
10742         case MONO_EXCEPTION_INVALID_PROGRAM: {
10743                 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", cfg->exception_message);
10744                 mono_destroy_compile (cfg);
10745                 mono_raise_exception (ex);
10746                 break;
10747         }
10748         case MONO_EXCEPTION_UNVERIFIABLE_IL: {
10749                 MonoException *ex = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", cfg->exception_message);
10750                 mono_destroy_compile (cfg);
10751                 mono_raise_exception (ex);
10752                 break;
10753         }
10754         /* this can only be set if the security manager is active */
10755         case MONO_EXCEPTION_SECURITY_LINKDEMAND: {
10756                 MonoAssembly *assembly = mono_image_get_assembly (method->klass->image);
10757                 MonoReflectionAssembly *refass = (MonoReflectionAssembly*) mono_assembly_get_object (target_domain, assembly);
10758                 MonoReflectionMethod *refmet = mono_method_get_object (target_domain, method, NULL);
10759                 MonoSecurityManager* secman = mono_security_manager_get_methods ();
10760                 MonoObject *exc = NULL;
10761                 gpointer args [3];
10762
10763                 args [0] = &cfg->exception_data;
10764                 args [1] = refass;
10765                 args [2] = refmet;
10766                 mono_runtime_invoke (secman->linkdemandsecurityexception, NULL, args, &exc);
10767
10768                 mono_destroy_compile (cfg);
10769                 cfg = NULL;
10770
10771                 mono_raise_exception ((MonoException*)exc);
10772         }
10773         default:
10774                 g_assert_not_reached ();
10775         }
10776
10777         mono_domain_lock (target_domain);
10778
10779         /* Check if some other thread already did the job. In this case, we can
10780        discard the code this thread generated. */
10781
10782         if ((info = mono_internal_hash_table_lookup (&target_domain->jit_code_hash, method))) {
10783                 /* We can't use a domain specific method in another domain */
10784                 if ((target_domain == mono_domain_get ()) || info->domain_neutral) {
10785                         code = info->code_start;
10786 //                      printf("Discarding code for method %s\n", method->name);
10787                 }
10788         }
10789         
10790         if (code == NULL) {
10791                 mono_internal_hash_table_insert (&target_domain->jit_code_hash, method, cfg->jit_info);
10792                 code = cfg->native_code;
10793         }
10794
10795         mono_destroy_compile (cfg);
10796
10797         if (target_domain->jump_target_hash) {
10798                 MonoJumpInfo patch_info;
10799                 GSList *list, *tmp;
10800                 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
10801                 if (list) {
10802                         patch_info.next = NULL;
10803                         patch_info.ip.i = 0;
10804                         patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
10805                         patch_info.data.method = method;
10806                         g_hash_table_remove (target_domain->jump_target_hash, method);
10807                 }
10808                 for (tmp = list; tmp; tmp = tmp->next)
10809                         mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info, TRUE);
10810                 g_slist_free (list);
10811         }
10812
10813         mono_domain_unlock (target_domain);
10814
10815         mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
10816         return code;
10817 }
10818
10819 static gpointer
10820 mono_jit_compile_method_with_opt (MonoMethod *method, guint32 opt)
10821 {
10822         MonoDomain *target_domain, *domain = mono_domain_get ();
10823         MonoJitInfo *info;
10824         gpointer p;
10825         MonoJitICallInfo *callinfo = NULL;
10826
10827         /*
10828          * ICALL wrappers are handled specially, since there is only one copy of them
10829          * shared by all appdomains.
10830          */
10831         if ((method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE) && (strstr (method->name, "__icall_wrapper_") == method->name)) {
10832                 const char *icall_name;
10833
10834                 icall_name = method->name + strlen ("__icall_wrapper_");
10835                 g_assert (icall_name);
10836                 callinfo = mono_find_jit_icall_by_name (icall_name);
10837                 g_assert (callinfo);
10838
10839                 /* Must be domain neutral since there is only one copy */
10840                 opt |= MONO_OPT_SHARED;
10841         }
10842
10843         if (opt & MONO_OPT_SHARED)
10844                 target_domain = mono_get_root_domain ();
10845         else 
10846                 target_domain = domain;
10847
10848         mono_domain_lock (target_domain);
10849
10850         if ((info = mono_internal_hash_table_lookup (&target_domain->jit_code_hash, method))) {
10851                 /* We can't use a domain specific method in another domain */
10852                 if (! ((domain != target_domain) && !info->domain_neutral)) {
10853                         mono_domain_unlock (target_domain);
10854                         mono_jit_stats.methods_lookups++;
10855                         mono_runtime_class_init (mono_class_vtable (domain, method->klass));
10856                         return mono_create_ftnptr (target_domain, info->code_start);
10857                 }
10858         }
10859
10860         mono_domain_unlock (target_domain);
10861         p = mono_create_ftnptr (target_domain, mono_jit_compile_method_inner (method, target_domain, opt));
10862
10863         if (callinfo) {
10864                 mono_jit_lock ();
10865                 if (!callinfo->wrapper) {
10866                         callinfo->wrapper = p;
10867                         mono_register_jit_icall_wrapper (callinfo, p);
10868                         mono_debug_add_icall_wrapper (method, callinfo);
10869                 }
10870                 mono_jit_unlock ();
10871         }
10872
10873         return p;
10874 }
10875
10876 static gpointer
10877 mono_jit_compile_method (MonoMethod *method)
10878 {
10879         return mono_jit_compile_method_with_opt (method, default_opt);
10880 }
10881
10882 static void
10883 invalidated_delegate_trampoline (char *desc)
10884 {
10885         g_error ("Unmanaged code called delegate of type %s which was already garbage collected.\n"
10886                  "See http://www.go-mono.com/delegate.html for an explanation and ways to fix this.",
10887                  desc);
10888 }
10889
10890 /*
10891  * mono_jit_free_method:
10892  *
10893  *  Free all memory allocated by the JIT for METHOD.
10894  */
10895 static void
10896 mono_jit_free_method (MonoDomain *domain, MonoMethod *method)
10897 {
10898         MonoJitDynamicMethodInfo *ji;
10899         gboolean destroy = TRUE;
10900
10901         g_assert (method->dynamic);
10902
10903         mono_domain_lock (domain);
10904         ji = mono_dynamic_code_hash_lookup (domain, method);
10905         mono_domain_unlock (domain);
10906
10907         if (!ji)
10908                 return;
10909         mono_domain_lock (domain);
10910         g_hash_table_remove (domain->dynamic_code_hash, method);
10911         mono_internal_hash_table_remove (&domain->jit_code_hash, method);
10912         g_hash_table_remove (domain->jump_trampoline_hash, method);
10913         mono_domain_unlock (domain);
10914
10915 #ifdef MONO_ARCH_HAVE_INVALIDATE_METHOD
10916         if (debug_options.keep_delegates && method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
10917                 /*
10918                  * Instead of freeing the code, change it to call an error routine
10919                  * so people can fix their code.
10920                  */
10921                 char *type = mono_type_full_name (&method->klass->byval_arg);
10922                 char *type_and_method = g_strdup_printf ("%s.%s", type, method->name);
10923
10924                 g_free (type);
10925                 mono_arch_invalidate_method (ji->ji, invalidated_delegate_trampoline, type_and_method);
10926                 destroy = FALSE;
10927         }
10928 #endif
10929
10930         /* 
10931          * This needs to be done before freeing code_mp, since the code address is the
10932          * key in the table, so if we the code_mp first, another thread can grab the
10933          * same code address and replace our entry in the table.
10934          */
10935         mono_jit_info_table_remove (domain, ji->ji);
10936
10937         if (destroy)
10938                 mono_code_manager_destroy (ji->code_mp);
10939         g_free (ji->ji);
10940         g_free (ji);
10941 }
10942
10943 static gpointer
10944 mono_jit_find_compiled_method (MonoDomain *domain, MonoMethod *method)
10945 {
10946         MonoDomain *target_domain;
10947         MonoJitInfo *info;
10948
10949         if (default_opt & MONO_OPT_SHARED)
10950                 target_domain = mono_get_root_domain ();
10951         else 
10952                 target_domain = domain;
10953
10954         mono_domain_lock (target_domain);
10955
10956         if ((info = mono_internal_hash_table_lookup (&target_domain->jit_code_hash, method))) {
10957                 /* We can't use a domain specific method in another domain */
10958                 if (! ((domain != target_domain) && !info->domain_neutral)) {
10959                         mono_domain_unlock (target_domain);
10960                         mono_jit_stats.methods_lookups++;
10961                         return info->code_start;
10962                 }
10963         }
10964
10965         mono_domain_unlock (target_domain);
10966
10967         return NULL;
10968 }
10969
10970 /**
10971  * mono_jit_runtime_invoke:
10972  * @method: the method to invoke
10973  * @obj: this pointer
10974  * @params: array of parameter values.
10975  * @exc: used to catch exceptions objects
10976  */
10977 static MonoObject*
10978 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
10979 {
10980         MonoMethod *invoke;
10981         MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc, void* compiled_method);
10982         void* compiled_method;
10983
10984         if (obj == NULL && !(method->flags & METHOD_ATTRIBUTE_STATIC) && !method->string_ctor && (method->wrapper_type == 0)) {
10985                 g_warning ("Ignoring invocation of an instance method on a NULL instance.\n");
10986                 return NULL;
10987         }
10988
10989         method = mono_get_inflated_method (method);
10990         invoke = mono_marshal_get_runtime_invoke (method);
10991         runtime_invoke = mono_jit_compile_method (invoke);
10992         
10993         /* We need this here becuase mono_marshal_get_runtime_invoke can be place 
10994          * the helper method in System.Object and not the target class
10995          */
10996         mono_runtime_class_init (mono_class_vtable (mono_domain_get (), method->klass));
10997
10998         compiled_method = mono_jit_compile_method (method);
10999         return runtime_invoke (obj, params, exc, compiled_method);
11000 }
11001
11002 #ifdef MONO_GET_CONTEXT
11003 #define GET_CONTEXT MONO_GET_CONTEXT
11004 #endif
11005
11006 #ifndef GET_CONTEXT
11007 #ifdef PLATFORM_WIN32
11008 #define GET_CONTEXT \
11009         struct sigcontext *ctx = (struct sigcontext*)_dummy;
11010 #else
11011 #ifdef MONO_ARCH_USE_SIGACTION
11012 #define GET_CONTEXT \
11013     void *ctx = context;
11014 #elif defined(__sparc__)
11015 #define GET_CONTEXT \
11016     void *ctx = sigctx;
11017 #else
11018 #define GET_CONTEXT \
11019         void **_p = (void **)&_dummy; \
11020         struct sigcontext *ctx = (struct sigcontext *)++_p;
11021 #endif
11022 #endif
11023 #endif
11024
11025 #ifdef MONO_ARCH_USE_SIGACTION
11026 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, siginfo_t *info, void *context)
11027 #elif defined(__sparc__)
11028 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy, void *sigctx)
11029 #else
11030 #define SIG_HANDLER_SIGNATURE(ftn) ftn (int _dummy)
11031 #endif
11032
11033 static void
11034 SIG_HANDLER_SIGNATURE (sigfpe_signal_handler)
11035 {
11036         MonoException *exc = NULL;
11037 #ifndef MONO_ARCH_USE_SIGACTION
11038         void *info = NULL;
11039 #endif
11040         GET_CONTEXT;
11041
11042 #if defined(MONO_ARCH_HAVE_IS_INT_OVERFLOW)
11043         if (mono_arch_is_int_overflow (ctx, info))
11044                 exc = mono_get_exception_arithmetic ();
11045         else
11046                 exc = mono_get_exception_divide_by_zero ();
11047 #else
11048         exc = mono_get_exception_divide_by_zero ();
11049 #endif
11050         
11051         mono_arch_handle_exception (ctx, exc, FALSE);
11052 }
11053
11054 static void
11055 SIG_HANDLER_SIGNATURE (sigill_signal_handler)
11056 {
11057         MonoException *exc;
11058         GET_CONTEXT;
11059
11060         exc = mono_get_exception_execution_engine ("SIGILL");
11061         
11062         mono_arch_handle_exception (ctx, exc, FALSE);
11063 }
11064
11065 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
11066
11067 #ifndef MONO_ARCH_USE_SIGACTION
11068 #error "Can't use sigaltstack without sigaction"
11069 #endif
11070
11071 #endif
11072
11073 static void
11074 SIG_HANDLER_SIGNATURE (sigsegv_signal_handler)
11075 {
11076         MonoException *exc = NULL;
11077         MonoJitInfo *ji;
11078
11079 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
11080         MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
11081 #endif
11082         GET_CONTEXT;
11083
11084 #ifdef MONO_ARCH_USE_SIGACTION
11085         if (debug_options.collect_pagefault_stats) {
11086                 if (mono_raw_buffer_is_pagefault (info->si_addr)) {
11087                         mono_raw_buffer_handle_pagefault (info->si_addr);
11088                         return;
11089                 }
11090                 if (mono_aot_is_pagefault (info->si_addr)) {
11091                         mono_aot_handle_pagefault (info->si_addr);
11092                         return;
11093                 }
11094         }
11095 #endif
11096
11097 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
11098         /* Can't allocate memory using Boehm GC on altstack */
11099         if (jit_tls->stack_size && 
11100                 ((guint8*)info->si_addr >= (guint8*)jit_tls->end_of_stack - jit_tls->stack_size) &&
11101                 ((guint8*)info->si_addr < (guint8*)jit_tls->end_of_stack))
11102                 exc = mono_domain_get ()->stack_overflow_ex;
11103         else
11104                 exc = mono_domain_get ()->null_reference_ex;
11105 #endif
11106
11107         ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
11108         if (!ji) {
11109                 mono_handle_native_sigsegv (SIGSEGV, ctx);
11110         }
11111                         
11112         mono_arch_handle_exception (ctx, exc, FALSE);
11113 }
11114
11115 #ifndef PLATFORM_WIN32
11116
11117 static void
11118 SIG_HANDLER_SIGNATURE (sigabrt_signal_handler)
11119 {
11120         MonoJitInfo *ji;
11121         GET_CONTEXT;
11122
11123         ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
11124         if (!ji) {
11125                 mono_handle_native_sigsegv (SIGABRT, ctx);
11126         }
11127 }
11128
11129 static void
11130 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
11131 {
11132         gboolean running_managed;
11133         MonoException *exc;
11134         MonoThread *thread = mono_thread_current ();
11135         void *ji;
11136         
11137         GET_CONTEXT;
11138
11139         if (thread->thread_dump_requested) {
11140                 thread->thread_dump_requested = FALSE;
11141
11142                 mono_print_thread_dump (ctx);
11143         }
11144
11145         /*
11146          * FIXME:
11147          * This is an async signal, so the code below must not call anything which
11148          * is not async safe. That includes the pthread locking functions. If we
11149          * know that we interrupted managed code, then locking is safe.
11150          */
11151         ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
11152         running_managed = ji != NULL;
11153         
11154         exc = mono_thread_request_interruption (running_managed); 
11155         if (!exc) return;
11156
11157         mono_arch_handle_exception (ctx, exc, FALSE);
11158 }
11159
11160 static void
11161 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
11162 {
11163         GET_CONTEXT;
11164
11165         mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
11166 }
11167
11168 static void
11169 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
11170 {
11171         GET_CONTEXT;
11172
11173         printf ("Full thread dump:\n");
11174
11175         mono_threads_request_thread_dump ();
11176
11177         /*
11178          * print_thread_dump () skips the current thread, since sending a signal
11179          * to it would invoke the signal handler below the sigquit signal handler,
11180          * and signal handlers don't create an lmf, so the stack walk could not
11181          * be performed.
11182          */
11183         mono_print_thread_dump (ctx);
11184 }
11185
11186 static void
11187 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
11188 {
11189         gboolean enabled = mono_trace_is_enabled ();
11190
11191         mono_trace_enable (!enabled);
11192 }
11193
11194 #endif
11195
11196 static void
11197 SIG_HANDLER_SIGNATURE (sigint_signal_handler)
11198 {
11199         MonoException *exc;
11200         GET_CONTEXT;
11201
11202         exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
11203         
11204         mono_arch_handle_exception (ctx, exc, FALSE);
11205 }
11206
11207 #ifdef PLATFORM_MACOSX
11208
11209 /*
11210  * This code disables the CrashReporter of MacOS X by installing
11211  * a dummy Mach exception handler.
11212  */
11213
11214 /*
11215  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
11216  */
11217 extern
11218 boolean_t
11219 exc_server (mach_msg_header_t *request_msg,
11220             mach_msg_header_t *reply_msg);
11221
11222 /*
11223  * The exception message
11224  */
11225 typedef struct {
11226         mach_msg_base_t msg;  /* common mach message header */
11227         char payload [1024];  /* opaque */
11228 } mach_exception_msg_t;
11229
11230 /* The exception port */
11231 static mach_port_t mach_exception_port = VM_MAP_NULL;
11232
11233 /*
11234  * Implicitly called by exc_server. Must be public.
11235  *
11236  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
11237  */
11238 kern_return_t
11239 catch_exception_raise (
11240         mach_port_t exception_port,
11241         mach_port_t thread,
11242         mach_port_t task,
11243         exception_type_t exception,
11244         exception_data_t code,
11245         mach_msg_type_number_t code_count)
11246 {
11247         /* consume the exception */
11248         return KERN_FAILURE;
11249 }
11250
11251 /*
11252  * Exception thread handler.
11253  */
11254 static
11255 void *
11256 mach_exception_thread (void *arg)
11257 {
11258         for (;;) {
11259                 mach_exception_msg_t request;
11260                 mach_exception_msg_t reply;
11261                 mach_msg_return_t result;
11262
11263                 /* receive from "mach_exception_port" */
11264                 result = mach_msg (&request.msg.header,
11265                                    MACH_RCV_MSG | MACH_RCV_LARGE,
11266                                    0,
11267                                    sizeof (request),
11268                                    mach_exception_port,
11269                                    MACH_MSG_TIMEOUT_NONE,
11270                                    MACH_PORT_NULL);
11271
11272                 g_assert (result == MACH_MSG_SUCCESS);
11273
11274                 /* dispatch to catch_exception_raise () */
11275                 exc_server (&request.msg.header, &reply.msg.header);
11276
11277                 /* send back to sender */
11278                 result = mach_msg (&reply.msg.header,
11279                                    MACH_SEND_MSG,
11280                                    reply.msg.header.msgh_size,
11281                                    0,
11282                                    MACH_PORT_NULL,
11283                                    MACH_MSG_TIMEOUT_NONE,
11284                                    MACH_PORT_NULL);
11285
11286                 g_assert (result == MACH_MSG_SUCCESS);
11287         }
11288         return NULL;
11289 }
11290
11291 static void
11292 macosx_register_exception_handler ()
11293 {
11294         mach_port_t task;
11295         pthread_attr_t attr;
11296         pthread_t thread;
11297
11298         if (mach_exception_port != VM_MAP_NULL)
11299                 return;
11300
11301         task = mach_task_self ();
11302
11303         /* create the "mach_exception_port" with send & receive rights */
11304         g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
11305                                       &mach_exception_port) == KERN_SUCCESS);
11306         g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
11307                                           MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
11308
11309         /* create the exception handler thread */
11310         g_assert (!pthread_attr_init (&attr));
11311         g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
11312         g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
11313         pthread_attr_destroy (&attr);
11314
11315         /*
11316          * register "mach_exception_port" as a receiver for the
11317          * EXC_BAD_ACCESS exception
11318          *
11319          * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
11320          */
11321         g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
11322                                             mach_exception_port,
11323                                             EXCEPTION_DEFAULT,
11324                                             MACHINE_THREAD_STATE) == KERN_SUCCESS);
11325 }
11326 #endif
11327
11328 #ifndef PLATFORM_WIN32
11329 static void
11330 add_signal_handler (int signo, gpointer handler)
11331 {
11332         struct sigaction sa;
11333
11334 #ifdef MONO_ARCH_USE_SIGACTION
11335         sa.sa_sigaction = handler;
11336         sigemptyset (&sa.sa_mask);
11337         sa.sa_flags = SA_SIGINFO;
11338 #else
11339         sa.sa_handler = handler;
11340         sigemptyset (&sa.sa_mask);
11341         sa.sa_flags = 0;
11342 #endif
11343         g_assert (sigaction (signo, &sa, NULL) != -1);
11344 }
11345
11346 static void
11347 remove_signal_handler (int signo)
11348 {
11349         struct sigaction sa;
11350
11351         sa.sa_handler = SIG_DFL;
11352         sigemptyset (&sa.sa_mask);
11353         sa.sa_flags = 0;
11354
11355         g_assert (sigaction (signo, &sa, NULL) != -1);
11356 }
11357 #endif
11358
11359 static void
11360 mono_runtime_install_handlers (void)
11361 {
11362 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
11363         struct sigaction sa;
11364 #endif
11365
11366 #ifdef PLATFORM_WIN32
11367         win32_seh_init();
11368         win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
11369         win32_seh_set_handler(SIGILL, sigill_signal_handler);
11370         win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
11371         if (debug_options.handle_sigint)
11372                 win32_seh_set_handler(SIGINT, sigint_signal_handler);
11373
11374 #else /* !PLATFORM_WIN32 */
11375
11376
11377 #ifdef PLATFORM_MACOSX
11378         macosx_register_exception_handler ();
11379 #endif
11380
11381         if (debug_options.handle_sigint)
11382                 add_signal_handler (SIGINT, sigint_signal_handler);
11383
11384         add_signal_handler (SIGFPE, sigfpe_signal_handler);
11385         add_signal_handler (SIGQUIT, sigquit_signal_handler);
11386         add_signal_handler (SIGILL, sigill_signal_handler);
11387         add_signal_handler (SIGBUS, sigsegv_signal_handler);
11388         if (mono_jit_trace_calls != NULL)
11389                 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
11390
11391         add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
11392         signal (SIGPIPE, SIG_IGN);
11393
11394         add_signal_handler (SIGABRT, sigabrt_signal_handler);
11395
11396         /* catch SIGSEGV */
11397 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
11398         sa.sa_sigaction = sigsegv_signal_handler;
11399         sigemptyset (&sa.sa_mask);
11400         sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
11401         g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
11402 #else
11403         add_signal_handler (SIGSEGV, sigsegv_signal_handler);
11404 #endif
11405 #endif /* PLATFORM_WIN32 */
11406 }
11407
11408 static void
11409 mono_runtime_cleanup_handlers (void)
11410 {
11411 #ifdef PLATFORM_WIN32
11412         win32_seh_cleanup();
11413 #else
11414         if (debug_options.handle_sigint)
11415                 remove_signal_handler (SIGINT);
11416
11417         remove_signal_handler (SIGFPE);
11418         remove_signal_handler (SIGQUIT);
11419         remove_signal_handler (SIGILL);
11420         remove_signal_handler (SIGBUS);
11421         if (mono_jit_trace_calls != NULL)
11422                 remove_signal_handler (SIGUSR2);
11423
11424         remove_signal_handler (mono_thread_get_abort_signal ());
11425
11426         remove_signal_handler (SIGABRT);
11427
11428         remove_signal_handler (SIGSEGV);
11429 #endif /* PLATFORM_WIN32 */
11430 }
11431
11432
11433 #ifdef HAVE_LINUX_RTC_H
11434 #include <linux/rtc.h>
11435 #include <sys/ioctl.h>
11436 #include <fcntl.h>
11437 static int rtc_fd = -1;
11438
11439 static int
11440 enable_rtc_timer (gboolean enable)
11441 {
11442         int flags;
11443         flags = fcntl (rtc_fd, F_GETFL);
11444         if (flags < 0) {
11445                 perror ("getflags");
11446                 return 0;
11447         }
11448         if (enable)
11449                 flags |= FASYNC;
11450         else
11451                 flags &= ~FASYNC;
11452         if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
11453                 perror ("setflags");
11454                 return 0;
11455         }
11456         return 1;
11457 }
11458 #endif
11459
11460 #ifdef PLATFORM_WIN32
11461 static HANDLE win32_main_thread;
11462 static MMRESULT win32_timer;
11463
11464 static void CALLBACK
11465 win32_time_proc (UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
11466 {
11467         CONTEXT context;
11468
11469         context.ContextFlags = CONTEXT_CONTROL;
11470         if (GetThreadContext (win32_main_thread, &context)) {
11471 #ifdef _WIN64
11472                 mono_profiler_stat_hit ((guchar *) context.Rip, &context);
11473 #else
11474                 mono_profiler_stat_hit ((guchar *) context.Eip, &context);
11475 #endif
11476         }
11477 }
11478 #endif
11479
11480 static void
11481 setup_stat_profiler (void)
11482 {
11483 #ifdef ITIMER_PROF
11484         struct itimerval itval;
11485         static int inited = 0;
11486 #ifdef HAVE_LINUX_RTC_H
11487         const char *rtc_freq;
11488         if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
11489                 int freq = 0;
11490                 inited = 1;
11491                 if (*rtc_freq)
11492                         freq = atoi (rtc_freq);
11493                 if (!freq)
11494                         freq = 1024;
11495                 rtc_fd = open ("/dev/rtc", O_RDONLY);
11496                 if (rtc_fd == -1) {
11497                         perror ("open /dev/rtc");
11498                         return;
11499                 }
11500                 add_signal_handler (SIGPROF, sigprof_signal_handler);
11501                 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
11502                         perror ("set rtc freq");
11503                         return;
11504                 }
11505                 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
11506                         perror ("start rtc");
11507                         return;
11508                 }
11509                 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
11510                         perror ("setsig");
11511                         return;
11512                 }
11513                 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
11514                         perror ("setown");
11515                         return;
11516                 }
11517                 enable_rtc_timer (TRUE);
11518                 return;
11519         }
11520         if (rtc_fd >= 0)
11521                 return;
11522 #endif
11523
11524         itval.it_interval.tv_usec = 999;
11525         itval.it_interval.tv_sec = 0;
11526         itval.it_value = itval.it_interval;
11527         setitimer (ITIMER_PROF, &itval, NULL);
11528         if (inited)
11529                 return;
11530         inited = 1;
11531         add_signal_handler (SIGPROF, sigprof_signal_handler);
11532 #elif defined (PLATFORM_WIN32)
11533         static int inited = 0;
11534         TIMECAPS timecaps;
11535
11536         if (inited)
11537                 return;
11538
11539         inited = 1;
11540         if (timeGetDevCaps (&timecaps, sizeof (timecaps)) != TIMERR_NOERROR)
11541                 return;
11542
11543         if ((win32_main_thread = OpenThread (READ_CONTROL | THREAD_GET_CONTEXT, FALSE, GetCurrentThreadId ())) == NULL)
11544                 return;
11545
11546         if (timeBeginPeriod (1) != TIMERR_NOERROR)
11547                 return;
11548
11549         if ((win32_timer = timeSetEvent (1, 0, win32_time_proc, 0, TIME_PERIODIC)) == 0) {
11550                 timeEndPeriod (1);
11551                 return;
11552         }
11553 #endif
11554 }
11555
11556 /* mono_jit_create_remoting_trampoline:
11557  * @method: pointer to the method info
11558  *
11559  * Creates a trampoline which calls the remoting functions. This
11560  * is used in the vtable of transparent proxies.
11561  * 
11562  * Returns: a pointer to the newly created code 
11563  */
11564 static gpointer
11565 mono_jit_create_remoting_trampoline (MonoMethod *method, MonoRemotingTarget target)
11566 {
11567         MonoMethod *nm;
11568         guint8 *addr = NULL;
11569
11570         if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || 
11571             (mono_method_signature (method)->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
11572                 nm = mono_marshal_get_remoting_invoke_for_target (method, target);
11573                 addr = mono_compile_method (nm);
11574         } else {
11575                 addr = mono_compile_method (method);
11576         }
11577         return mono_get_addr_from_ftnptr (addr);
11578 }
11579
11580 static void
11581 mini_parse_debug_options (void)
11582 {
11583         char *options = getenv ("MONO_DEBUG");
11584         gchar **args, **ptr;
11585         
11586         if (!options)
11587                 return;
11588
11589         args = g_strsplit (options, ",", -1);
11590
11591         for (ptr = args; ptr && *ptr; ptr++) {
11592                 const char *arg = *ptr;
11593
11594                 if (!strcmp (arg, "handle-sigint"))
11595                         debug_options.handle_sigint = TRUE;
11596                 else if (!strcmp (arg, "keep-delegates"))
11597                         debug_options.keep_delegates = TRUE;
11598                 else if (!strcmp (arg, "collect-pagefault-stats"))
11599                         debug_options.collect_pagefault_stats = TRUE;
11600                 else if (!strcmp (arg, "break-on-unverified"))
11601                         debug_options.break_on_unverified = TRUE;
11602                 else {
11603                         fprintf (stderr, "Invalid option for the MONO_DEBUG env variable: %s\n", arg);
11604                         fprintf (stderr, "Available options: 'handle-sigint', 'keep-delegates', 'collect-pagefault-stats', 'break-on-unverified'\n");
11605                         exit (1);
11606                 }
11607         }
11608 }
11609
11610 MonoDomain *
11611 mini_init (const char *filename, const char *runtime_version)
11612 {
11613         MonoDomain *domain;
11614
11615 #ifdef __linux__
11616         if (access ("/proc/self/maps", F_OK) != 0) {
11617                 g_print ("Mono requires /proc to be mounted.\n");
11618                 exit (1);
11619         }
11620 #endif
11621
11622         /* Happens when using the embedding interface */
11623         if (!default_opt_set)
11624                 default_opt = mono_parse_default_optimizations (NULL);
11625
11626         InitializeCriticalSection (&jit_mutex);
11627
11628         global_codeman = mono_code_manager_new ();
11629         jit_icall_name_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
11630
11631         mono_arch_cpu_init ();
11632
11633         mono_init_trampolines ();
11634
11635         mono_init_exceptions ();
11636
11637         if (!g_thread_supported ())
11638                 g_thread_init (NULL);
11639
11640         if (getenv ("MONO_DEBUG") != NULL)
11641                 mini_parse_debug_options ();
11642
11643         /*
11644          * Handle the case when we are called from a thread different from the main thread,
11645          * confusing libgc.
11646          * FIXME: Move this to libgc where it belongs.
11647          *
11648          * we used to do this only when running on valgrind,
11649          * but it happens also in other setups.
11650          */
11651 #if defined(HAVE_BOEHM_GC)
11652 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK)
11653         {
11654                 size_t size;
11655                 void *sstart;
11656                 pthread_attr_t attr;
11657                 pthread_getattr_np (pthread_self (), &attr);
11658                 pthread_attr_getstack (&attr, &sstart, &size);
11659                 pthread_attr_destroy (&attr); 
11660                 /*g_print ("stackbottom pth is: %p\n", (char*)sstart + size);*/
11661 #ifdef __ia64__
11662                 /*
11663                  * The calculation above doesn't seem to work on ia64, also we need to set
11664                  * GC_register_stackbottom as well, but don't know how.
11665                  */
11666 #else
11667                 /* apparently with some linuxthreads implementations sstart can be NULL,
11668                  * fallback to the more imprecise method (bug# 78096).
11669                  */
11670                 if (sstart) {
11671                         GC_stackbottom = (char*)sstart + size;
11672                 } else {
11673                         gsize stack_bottom = (gsize)&domain;
11674                         stack_bottom += 4095;
11675                         stack_bottom &= ~4095;
11676                         GC_stackbottom = (char*)stack_bottom;
11677                 }
11678 #endif
11679         }
11680 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
11681                 GC_stackbottom = (char*)pthread_get_stackaddr_np (pthread_self ());
11682 #else
11683         {
11684                 gsize stack_bottom = (gsize)&domain;
11685                 stack_bottom += 4095;
11686                 stack_bottom &= ~4095;
11687                 /*g_print ("stackbottom is: %p\n", (char*)stack_bottom);*/
11688                 GC_stackbottom = (char*)stack_bottom;
11689         }
11690 #endif
11691 #endif
11692         MONO_GC_PRE_INIT ();
11693
11694         mono_jit_tls_id = TlsAlloc ();
11695         setup_jit_tls_data ((gpointer)-1, mono_thread_abort);
11696
11697         mono_burg_init ();
11698
11699         if (default_opt & MONO_OPT_AOT)
11700                 mono_aot_init ();
11701
11702         mono_runtime_install_handlers ();
11703         mono_threads_install_cleanup (mini_thread_cleanup);
11704
11705 #define JIT_TRAMPOLINES_WORK
11706 #ifdef JIT_TRAMPOLINES_WORK
11707         mono_install_compile_method (mono_jit_compile_method);
11708         mono_install_free_method (mono_jit_free_method);
11709         mono_install_trampoline (mono_create_jit_trampoline);
11710         mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
11711         mono_install_delegate_trampoline (mono_create_delegate_trampoline);
11712 #endif
11713 #define JIT_INVOKE_WORKS
11714 #ifdef JIT_INVOKE_WORKS
11715         mono_install_runtime_invoke (mono_jit_runtime_invoke);
11716         mono_install_handler (mono_arch_get_throw_exception ());
11717 #endif
11718         mono_install_stack_walk (mono_jit_walk_stack);
11719         mono_install_init_vtable (mono_aot_init_vtable);
11720         mono_install_get_cached_class_info (mono_aot_get_cached_class_info);
11721         mono_install_get_class_from_name (mono_aot_get_class_from_name);
11722         mono_install_jit_info_find_in_aot (mono_aot_find_jit_info);
11723
11724 #ifdef MONO_ARCH_HAVE_IMT
11725         mono_install_imt_thunk_builder (mono_arch_build_imt_thunk);
11726 #endif
11727         if (debug_options.collect_pagefault_stats) {
11728                 mono_raw_buffer_set_make_unreadable (TRUE);
11729                 mono_aot_set_make_unreadable (TRUE);
11730         }
11731
11732         if (runtime_version)
11733                 domain = mono_init_version (filename, runtime_version);
11734         else
11735                 domain = mono_init_from_assembly (filename, filename);
11736         mono_icall_init ();
11737
11738         mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info", 
11739                                 ves_icall_get_frame_info);
11740         mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace", 
11741                                 ves_icall_get_trace);
11742         mono_add_internal_call ("System.Exception::get_trace", 
11743                                 ves_icall_System_Exception_get_trace);
11744         mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityFrame",
11745                                 ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
11746         mono_add_internal_call ("System.Security.SecurityFrame::_GetSecurityStack",
11747                                 ves_icall_System_Security_SecurityFrame_GetSecurityStack);
11748         mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers", 
11749                                 mono_runtime_install_handlers);
11750
11751
11752         create_helper_signature ();
11753
11754 #define JIT_CALLS_WORK
11755 #ifdef JIT_CALLS_WORK
11756         /* Needs to be called here since register_jit_icall depends on it */
11757         mono_marshal_init ();
11758
11759         mono_arch_register_lowlevel_calls ();
11760         register_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
11761         register_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
11762         register_icall (mono_trace_enter_method, "mono_trace_enter_method", NULL, TRUE);
11763         register_icall (mono_trace_leave_method, "mono_trace_leave_method", NULL, TRUE);
11764         register_icall (mono_get_lmf_addr, "mono_get_lmf_addr", "ptr", TRUE);
11765         register_icall (mono_jit_thread_attach, "mono_jit_thread_attach", "void", TRUE);
11766         register_icall (mono_domain_get, "mono_domain_get", "ptr", TRUE);
11767
11768         register_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", "void object", TRUE);
11769         register_icall (mono_arch_get_rethrow_exception (), "mono_arch_rethrow_exception", "void object", TRUE);
11770         register_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name", "void ptr", TRUE); 
11771 #if MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION
11772         register_icall (mono_arch_get_throw_corlib_exception (), "mono_arch_throw_corlib_exception", 
11773                                  "void ptr", TRUE);
11774 #endif
11775         register_icall (mono_thread_get_pending_exception, "mono_thread_get_pending_exception", "object", FALSE);
11776         register_icall (mono_thread_interruption_checkpoint, "mono_thread_interruption_checkpoint", "void", FALSE);
11777         register_icall (mono_thread_force_interruption_checkpoint, "mono_thread_force_interruption_checkpoint", "void", FALSE);
11778         register_icall (mono_load_remote_field_new, "mono_load_remote_field_new", "object object ptr ptr", FALSE);
11779         register_icall (mono_store_remote_field_new, "mono_store_remote_field_new", "void object ptr ptr object", FALSE);
11780
11781         /* 
11782          * NOTE, NOTE, NOTE, NOTE:
11783          * when adding emulation for some opcodes, remember to also add a dummy
11784          * rule to the burg files, because we need the arity information to be correct.
11785          */
11786 #ifndef MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
11787         mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", "long long long", mono_llmult, TRUE);
11788         mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", "long long long", mono_lldiv, FALSE);
11789         mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", "long long long", mono_lldiv_un, FALSE);
11790         mono_register_opcode_emulation (OP_LREM, "__emul_lrem", "long long long", mono_llrem, FALSE);
11791         mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", "long long long", mono_llrem_un, FALSE);
11792         mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", "long long long", mono_llmult_ovf_un, FALSE);
11793         mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", "long long long", mono_llmult_ovf, FALSE);
11794 #endif
11795
11796 #ifndef MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
11797         mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", "long long int32", mono_lshl, TRUE);
11798         mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", "long long int32", mono_lshr, TRUE);
11799         mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", "long long int32", mono_lshr_un, TRUE);
11800 #endif
11801
11802 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_EMULATE_DIV)
11803         mono_register_opcode_emulation (CEE_DIV, "__emul_idiv", "int32 int32 int32", mono_idiv, FALSE);
11804         mono_register_opcode_emulation (CEE_DIV_UN, "__emul_idiv_un", "int32 int32 int32", mono_idiv_un, FALSE);
11805         mono_register_opcode_emulation (CEE_REM, "__emul_irem", "int32 int32 int32", mono_irem, FALSE);
11806         mono_register_opcode_emulation (CEE_REM_UN, "__emul_irem_un", "int32 int32 int32", mono_irem_un, FALSE);
11807 #endif
11808
11809 #ifdef MONO_ARCH_EMULATE_MUL_DIV
11810         mono_register_opcode_emulation (CEE_MUL_OVF, "__emul_imul_ovf", "int32 int32 int32", mono_imul_ovf, FALSE);
11811         mono_register_opcode_emulation (CEE_MUL_OVF_UN, "__emul_imul_ovf_un", "int32 int32 int32", mono_imul_ovf_un, FALSE);
11812         mono_register_opcode_emulation (CEE_MUL, "__emul_imul", "int32 int32 int32", mono_imul, TRUE);
11813 #endif
11814 #if defined(MONO_ARCH_EMULATE_MUL_DIV) || defined(MONO_ARCH_SOFT_FLOAT)
11815         mono_register_opcode_emulation (OP_FDIV, "__emul_fdiv", "double double double", mono_fdiv, FALSE);
11816 #endif
11817
11818         mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", "ulong double", mono_fconv_u8, FALSE);
11819         mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", "uint32 double", mono_fconv_u4, FALSE);
11820         mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", "long double", mono_fconv_ovf_i8, FALSE);
11821         mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", "ulong double", mono_fconv_ovf_u8, FALSE);
11822
11823 #ifdef MONO_ARCH_EMULATE_FCONV_TO_I8
11824         mono_register_opcode_emulation (OP_FCONV_TO_I8, "__emul_fconv_to_i8", "long double", mono_fconv_i8, FALSE);
11825 #endif
11826 #ifdef MONO_ARCH_EMULATE_CONV_R8_UN
11827         mono_register_opcode_emulation (CEE_CONV_R_UN, "__emul_conv_r_un", "double int32", mono_conv_to_r8_un, FALSE);
11828 #endif
11829 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8
11830         mono_register_opcode_emulation (OP_LCONV_TO_R8, "__emul_lconv_to_r8", "double long", mono_lconv_to_r8, FALSE);
11831 #endif
11832 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R4
11833         mono_register_opcode_emulation (OP_LCONV_TO_R4, "__emul_lconv_to_r4", "float long", mono_lconv_to_r4, FALSE);
11834 #endif
11835 #ifdef MONO_ARCH_EMULATE_LCONV_TO_R8_UN
11836         mono_register_opcode_emulation (OP_LCONV_TO_R_UN, "__emul_lconv_to_r8_un", "double long", mono_lconv_to_r8_un, FALSE);
11837 #endif
11838 #ifdef MONO_ARCH_EMULATE_FREM
11839         mono_register_opcode_emulation (OP_FREM, "__emul_frem", "double double double", fmod, FALSE);
11840 #endif
11841
11842 #ifdef MONO_ARCH_SOFT_FLOAT
11843         mono_register_opcode_emulation (OP_FSUB, "__emul_fsub", "double double double", mono_fsub, FALSE);
11844         mono_register_opcode_emulation (OP_FADD, "__emul_fadd", "double double double", mono_fadd, FALSE);
11845         mono_register_opcode_emulation (OP_FMUL, "__emul_fmul", "double double double", mono_fmul, FALSE);
11846         mono_register_opcode_emulation (OP_FNEG, "__emul_fneg", "double double", mono_fneg, FALSE);
11847         mono_register_opcode_emulation (CEE_CONV_R8, "__emul_conv_r8", "double int32", mono_conv_to_r8, FALSE);
11848         mono_register_opcode_emulation (CEE_CONV_R4, "__emul_conv_r4", "double int32", mono_conv_to_r4, FALSE);
11849         mono_register_opcode_emulation (OP_FCONV_TO_R4, "__emul_fconv_to_r4", "double double", mono_fconv_r4, FALSE);
11850         mono_register_opcode_emulation (OP_FCONV_TO_I1, "__emul_fconv_to_i1", "int8 double", mono_fconv_i1, FALSE);
11851         mono_register_opcode_emulation (OP_FCONV_TO_I2, "__emul_fconv_to_i2", "int16 double", mono_fconv_i2, FALSE);
11852         mono_register_opcode_emulation (OP_FCONV_TO_I4, "__emul_fconv_to_i4", "int32 double", mono_fconv_i4, FALSE);
11853         mono_register_opcode_emulation (OP_FCONV_TO_U1, "__emul_fconv_to_u1", "uint8 double", mono_fconv_u1, FALSE);
11854         mono_register_opcode_emulation (OP_FCONV_TO_U2, "__emul_fconv_to_u2", "uint16 double", mono_fconv_u2, FALSE);
11855
11856         mono_register_opcode_emulation (OP_FBEQ, "__emul_fcmp_eq", "uint32 double double", mono_fcmp_eq, FALSE);
11857         mono_register_opcode_emulation (OP_FBLT, "__emul_fcmp_lt", "uint32 double double", mono_fcmp_lt, FALSE);
11858         mono_register_opcode_emulation (OP_FBGT, "__emul_fcmp_gt", "uint32 double double", mono_fcmp_gt, FALSE);
11859         mono_register_opcode_emulation (OP_FBLE, "__emul_fcmp_le", "uint32 double double", mono_fcmp_le, FALSE);
11860         mono_register_opcode_emulation (OP_FBGE, "__emul_fcmp_ge", "uint32 double double", mono_fcmp_ge, FALSE);
11861         mono_register_opcode_emulation (OP_FBNE_UN, "__emul_fcmp_ne_un", "uint32 double double", mono_fcmp_ne_un, FALSE);
11862         mono_register_opcode_emulation (OP_FBLT_UN, "__emul_fcmp_lt_un", "uint32 double double", mono_fcmp_lt_un, FALSE);
11863         mono_register_opcode_emulation (OP_FBGT_UN, "__emul_fcmp_gt_un", "uint32 double double", mono_fcmp_gt_un, FALSE);
11864         mono_register_opcode_emulation (OP_FBLE_UN, "__emul_fcmp_le_un", "uint32 double double", mono_fcmp_le_un, FALSE);
11865         mono_register_opcode_emulation (OP_FBGE_UN, "__emul_fcmp_ge_un", "uint32 double double", mono_fcmp_ge_un, FALSE);
11866
11867         mono_register_opcode_emulation (OP_FCEQ, "__emul_fcmp_ceq", "uint32 double double", mono_fceq, FALSE);
11868         mono_register_opcode_emulation (OP_FCGT, "__emul_fcmp_cgt", "uint32 double double", mono_fcgt, FALSE);
11869         mono_register_opcode_emulation (OP_FCGT_UN, "__emul_fcmp_cgt_un", "uint32 double double", mono_fcgt_un, FALSE);
11870         mono_register_opcode_emulation (OP_FCLT, "__emul_fcmp_clt", "uint32 double double", mono_fclt, FALSE);
11871         mono_register_opcode_emulation (OP_FCLT_UN, "__emul_fcmp_clt_un", "uint32 double double", mono_fclt_un, FALSE);
11872
11873         register_icall (mono_fload_r4, "mono_fload_r4", "double ptr", FALSE);
11874         register_icall (mono_fstore_r4, "mono_fstore_r4", "void double ptr", FALSE);
11875         register_icall (mono_fload_r4_arg, "mono_fload_r4_arg", "uint32 double", FALSE);
11876 #endif
11877
11878 #if SIZEOF_VOID_P == 4
11879         mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", "uint32 double", mono_fconv_u4, TRUE);
11880 #endif
11881
11882         /* other jit icalls */
11883         register_icall (mono_delegate_ctor, "mono_delegate_ctor", "void object object ptr", FALSE);
11884         register_icall (mono_class_static_field_address , "mono_class_static_field_address", 
11885                                  "ptr ptr ptr", FALSE);
11886         register_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", "ptr ptr ptr ptr", FALSE);
11887         register_icall (mono_get_special_static_data, "mono_get_special_static_data", "ptr int", FALSE);
11888         register_icall (mono_ldstr, "mono_ldstr", "object ptr ptr int32", FALSE);
11889         register_icall (mono_helper_stelem_ref_check, "helper_stelem_ref_check", "void object object", FALSE);
11890         register_icall (mono_object_new, "mono_object_new", "object ptr ptr", FALSE);
11891         register_icall (mono_object_new_specific, "mono_object_new_specific", "object ptr", FALSE);
11892         register_icall (mono_array_new, "mono_array_new", "object ptr ptr int32", FALSE);
11893         register_icall (mono_array_new_specific, "mono_array_new_specific", "object ptr int32", FALSE);
11894         register_icall (mono_runtime_class_init, "mono_runtime_class_init", "void ptr", FALSE);
11895         register_icall (mono_ldftn, "mono_ldftn", "ptr ptr", FALSE);
11896         register_icall (mono_ldftn_nosync, "mono_ldftn_nosync", "ptr ptr", FALSE);
11897         register_icall (mono_ldvirtfn, "mono_ldvirtfn", "ptr object ptr", FALSE);
11898         register_icall (mono_helper_compile_generic_method, "compile_generic_method", "ptr object ptr ptr ptr", FALSE);
11899         register_icall (mono_helper_ldstr, "helper_ldstr", "object ptr int", FALSE);
11900         register_icall (mono_helper_ldstr_mscorlib, "helper_ldstr_mscorlib", "object int", FALSE);
11901         register_icall (mono_helper_newobj_mscorlib, "helper_newobj_mscorlib", "object int", FALSE);
11902         register_icall (mono_value_copy, "mono_value_copy", "void ptr ptr ptr", FALSE);
11903 #endif
11904
11905 #define JIT_RUNTIME_WORKS
11906 #ifdef JIT_RUNTIME_WORKS
11907         mono_install_runtime_cleanup ((MonoDomainFunc)mini_cleanup);
11908         mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
11909 #endif
11910
11911         mono_thread_attach (domain);
11912         return domain;
11913 }
11914
11915 MonoJitStats mono_jit_stats = {0};
11916
11917 static void 
11918 print_jit_stats (void)
11919 {
11920         if (mono_jit_stats.enabled) {
11921                 g_print ("Mono Jit statistics\n");
11922                 g_print ("Compiled methods:       %ld\n", mono_jit_stats.methods_compiled);
11923                 g_print ("Methods from AOT:       %ld\n", mono_jit_stats.methods_aot);
11924                 g_print ("Methods cache lookup:   %ld\n", mono_jit_stats.methods_lookups);
11925                 g_print ("Method trampolines:     %ld\n", mono_jit_stats.method_trampolines);
11926                 g_print ("Basic blocks:           %ld\n", mono_jit_stats.basic_blocks);
11927                 g_print ("Max basic blocks:       %ld\n", mono_jit_stats.max_basic_blocks);
11928                 g_print ("Allocated vars:         %ld\n", mono_jit_stats.allocate_var);
11929                 g_print ("Analyze stack repeat:   %ld\n", mono_jit_stats.analyze_stack_repeat);
11930                 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
11931                 g_print ("Native code size:       %ld\n", mono_jit_stats.native_code_size);
11932                 g_print ("Max code size ratio:    %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
11933                                 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
11934                 g_print ("Biggest method:         %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
11935                                 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
11936                 g_print ("Code reallocs:          %ld\n", mono_jit_stats.code_reallocs);
11937                 g_print ("Allocated code size:    %ld\n", mono_jit_stats.allocated_code_size);
11938                 g_print ("Inlineable methods:     %ld\n", mono_jit_stats.inlineable_methods);
11939                 g_print ("Inlined methods:        %ld\n", mono_jit_stats.inlined_methods);
11940                 g_print ("Locals stack size:      %ld\n", mono_jit_stats.locals_stack_size);
11941
11942                 g_print ("\nCreated object count:   %ld\n", mono_stats.new_object_count);
11943                 g_print ("Initialized classes:    %ld\n", mono_stats.initialized_class_count);
11944                 g_print ("Used classes:           %ld\n", mono_stats.used_class_count);
11945                 g_print ("Generic vtables:        %ld\n", mono_stats.generic_vtable_count);
11946                 g_print ("Methods:                %ld\n", mono_stats.method_count);
11947                 g_print ("Static data size:       %ld\n", mono_stats.class_static_data_size);
11948                 g_print ("VTable data size:       %ld\n", mono_stats.class_vtable_size);
11949                 g_print ("Mscorlib mempool size:  %d\n", mono_mempool_get_allocated (mono_defaults.corlib->mempool));
11950
11951                 g_print ("\nGeneric instances:      %ld\n", mono_stats.generic_instance_count);
11952                 g_print ("Initialized classes:    %ld\n", mono_stats.generic_class_count);
11953                 g_print ("Inflated methods:       %ld / %ld\n", mono_stats.inflated_method_count_2,
11954                          mono_stats.inflated_method_count);
11955                 g_print ("Inflated types:         %ld\n", mono_stats.inflated_type_count);
11956                 g_print ("Generics metadata size: %ld\n", mono_stats.generics_metadata_size);
11957                 g_print ("Generics virtual invokes: %ld\n", mono_jit_stats.generic_virtual_invocations);
11958
11959                 g_print ("Dynamic code allocs:    %ld\n", mono_stats.dynamic_code_alloc_count);
11960                 g_print ("Dynamic code bytes:     %ld\n", mono_stats.dynamic_code_bytes_count);
11961                 g_print ("Dynamic code frees:     %ld\n", mono_stats.dynamic_code_frees_count);
11962
11963                 g_print ("IMT tables size:        %ld\n", mono_stats.imt_tables_size);
11964                 g_print ("IMT number of tables:   %ld\n", mono_stats.imt_number_of_tables);
11965                 g_print ("IMT number of methods:  %ld\n", mono_stats.imt_number_of_methods);
11966                 g_print ("IMT used slots:         %ld\n", mono_stats.imt_used_slots);
11967                 g_print ("IMT colliding slots:    %ld\n", mono_stats.imt_slots_with_collisions);
11968                 g_print ("IMT max collisions:     %ld\n", mono_stats.imt_max_collisions_in_slot);
11969                 g_print ("IMT methods at max col: %ld\n", mono_stats.imt_method_count_when_max_collisions);
11970                 g_print ("IMT thunks size:        %ld\n", mono_stats.imt_thunks_size);
11971
11972                 if (mono_use_security_manager) {
11973                         g_print ("\nDecl security check   : %ld\n", mono_jit_stats.cas_declsec_check);
11974                         g_print ("LinkDemand (user)     : %ld\n", mono_jit_stats.cas_linkdemand);
11975                         g_print ("LinkDemand (icall)    : %ld\n", mono_jit_stats.cas_linkdemand_icall);
11976                         g_print ("LinkDemand (pinvoke)  : %ld\n", mono_jit_stats.cas_linkdemand_pinvoke);
11977                         g_print ("LinkDemand (aptc)     : %ld\n", mono_jit_stats.cas_linkdemand_aptc);
11978                         g_print ("Demand (code gen)     : %ld\n", mono_jit_stats.cas_demand_generation);
11979                 }
11980                 if (debug_options.collect_pagefault_stats) {
11981                         g_print ("Metadata pagefaults   : %d\n", mono_raw_buffer_get_n_pagefaults ());
11982                         g_print ("AOT pagefaults        : %d\n", mono_aot_get_n_pagefaults ());
11983                 }
11984         }
11985 }
11986
11987 void
11988 mini_cleanup (MonoDomain *domain)
11989 {
11990 #ifdef HAVE_LINUX_RTC_H
11991         if (rtc_fd >= 0)
11992                 enable_rtc_timer (FALSE);
11993 #endif
11994
11995         /* 
11996          * mono_runtime_cleanup() and mono_domain_finalize () need to
11997          * be called early since they need the execution engine still
11998          * fully working (mono_domain_finalize may invoke managed finalizers
11999          * and mono_runtime_cleanup will wait for other threads to finish).
12000          */
12001         mono_domain_finalize (domain, 2000);
12002
12003         /* This accesses metadata so needs to be called before runtime shutdown */
12004         print_jit_stats ();
12005
12006         mono_runtime_cleanup (domain);
12007
12008         mono_profiler_shutdown ();
12009
12010         mono_debug_cleanup ();
12011
12012         mono_icall_cleanup ();
12013
12014         mono_runtime_cleanup_handlers ();
12015
12016         mono_domain_free (domain, TRUE);
12017
12018         mono_code_manager_destroy (global_codeman);
12019         g_hash_table_destroy (jit_icall_name_hash);
12020         if (class_init_hash_addr)
12021                 g_hash_table_destroy (class_init_hash_addr);
12022         g_free (emul_opcode_map);
12023
12024         mono_cleanup ();
12025
12026         mono_trace_cleanup ();
12027
12028         mono_counters_dump (-1, stdout);
12029
12030         TlsFree(mono_jit_tls_id);
12031
12032         DeleteCriticalSection (&jit_mutex);
12033
12034         DeleteCriticalSection (&mono_delegate_section);
12035 }
12036
12037 void
12038 mono_set_defaults (int verbose_level, guint32 opts)
12039 {
12040         mini_verbose = verbose_level;
12041         default_opt = opts;
12042         default_opt_set = TRUE;
12043 }
12044
12045 static void
12046 mono_precompile_assembly (MonoAssembly *ass, void *user_data)
12047 {
12048         GHashTable *assemblies = (GHashTable*)user_data;
12049         MonoImage *image = mono_assembly_get_image (ass);
12050         MonoMethod *method, *invoke;
12051         int i, count = 0;
12052
12053         if (g_hash_table_lookup (assemblies, ass))
12054                 return;
12055
12056         g_hash_table_insert (assemblies, ass, ass);
12057
12058         if (mini_verbose > 0)
12059                 printf ("PRECOMPILE: %s.\n", mono_image_get_filename (image));
12060
12061         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_METHOD); ++i) {
12062                 method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | (i + 1), NULL);
12063                 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
12064                         continue;
12065
12066                 count++;
12067                 if (mini_verbose > 1) {
12068                         char * desc = mono_method_full_name (method, TRUE);
12069                         g_print ("Compiling %d %s\n", count, desc);
12070                         g_free (desc);
12071                 }
12072                 mono_compile_method (method);
12073                 if (strcmp (method->name, "Finalize") == 0) {
12074                         invoke = mono_marshal_get_runtime_invoke (method);
12075                         mono_compile_method (invoke);
12076                 }
12077                 if (method->klass->marshalbyref && mono_method_signature (method)->hasthis) {
12078                         invoke = mono_marshal_get_remoting_invoke_with_check (method);
12079                         mono_compile_method (invoke);
12080                 }
12081         }
12082
12083         /* Load and precompile referenced assemblies as well */
12084         for (i = 0; i < mono_image_get_table_rows (image, MONO_TABLE_ASSEMBLYREF); ++i) {
12085                 mono_assembly_load_reference (image, i);
12086                 if (image->references [i])
12087                         mono_precompile_assembly (image->references [i], assemblies);
12088         }
12089 }
12090
12091 void mono_precompile_assemblies ()
12092 {
12093         GHashTable *assemblies = g_hash_table_new (NULL, NULL);
12094
12095         mono_assembly_foreach ((GFunc)mono_precompile_assembly, assemblies);
12096
12097         g_hash_table_destroy (assemblies);
12098 }