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