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