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