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