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