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