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