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