2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
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>
44 #include "jit-icalls.c"
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))
48 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
50 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
51 static gpointer mono_jit_compile_method (MonoMethod *method);
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);
56 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
57 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
58 guint inline_offset, gboolean is_virtual_call);
60 extern guint8 mono_burg_arity [];
61 /* helper methods signature */
62 static MonoMethodSignature *helper_sig_long_long_long = NULL;
63 static MonoMethodSignature *helper_sig_long_long_int = NULL;
64 static MonoMethodSignature *helper_sig_newarr = NULL;
65 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
66 static MonoMethodSignature *helper_sig_ldstr = NULL;
67 static MonoMethodSignature *helper_sig_domain_get = NULL;
68 static MonoMethodSignature *helper_sig_object_new = NULL;
69 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
70 static MonoMethodSignature *helper_sig_compile = NULL;
71 static MonoMethodSignature *helper_sig_compile_virt = NULL;
72 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
73 static MonoMethodSignature *helper_sig_ptr_void = NULL;
74 static MonoMethodSignature *helper_sig_void_ptr = NULL;
75 static MonoMethodSignature *helper_sig_void_obj = NULL;
76 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
77 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
78 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
79 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
80 static MonoMethodSignature *helper_sig_ptr_int = NULL;
81 static MonoMethodSignature *helper_sig_initobj = NULL;
82 static MonoMethodSignature *helper_sig_memcpy = NULL;
83 static MonoMethodSignature *helper_sig_memset = NULL;
84 static MonoMethodSignature *helper_sig_ulong_double = NULL;
85 static MonoMethodSignature *helper_sig_long_double = NULL;
86 static MonoMethodSignature *helper_sig_uint_double = NULL;
87 static MonoMethodSignature *helper_sig_int_double = NULL;
88 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
90 static guint32 default_opt = MONO_OPT_PEEPHOLE;
92 guint32 mono_jit_tls_id = -1;
93 gboolean mono_jit_trace_calls = FALSE;
94 gboolean mono_break_on_exc = FALSE;
95 gboolean mono_compile_aot = FALSE;
96 gboolean mono_no_aot = FALSE;
98 static int mini_verbose = 0;
100 #ifdef MONO_USE_EXC_TABLES
102 mono_type_blittable (MonoType *type)
122 case MONO_TYPE_VALUETYPE:
123 case MONO_TYPE_CLASS:
124 case MONO_TYPE_OBJECT:
125 return type->data.klass->blittable;
135 mono_method_blittable (MonoMethod *method)
137 MonoMethodSignature *sig;
143 if (!mono_arch_has_unwind_info (method->addr)) {
147 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
150 sig = method->signature;
152 if (!mono_type_blittable (sig->ret))
155 for (i = 0; i < sig->param_count; i++)
156 if (!mono_type_blittable (sig->params [i]))
159 if (mono_method_has_marshal_info (method))
167 G_GNUC_UNUSED static void
168 print_method_from_ip (void *ip)
173 MonoDomain *domain = mono_domain_get ();
175 ji = mono_jit_info_table_find (domain, ip);
177 g_print ("No method at %p\n", ip);
180 method = mono_method_full_name (ji->method, TRUE);
181 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
183 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);
186 g_print ("%s\n", source);
193 #define MONO_INIT_VARINFO(vi,id) do { \
194 (vi)->range.first_use.pos.bid = 0xffff; \
200 * Basic blocks have two numeric identifiers:
201 * dfn: Depth First Number
202 * block_num: unique ID assigned at bblock creation
204 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
205 #define ADD_BBLOCK(cfg,bbhash,b) do { \
206 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
207 (b)->block_num = cfg->num_bblocks++; \
208 (b)->real_offset = real_offset; \
211 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
212 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
214 if ((ip) >= end || (ip) < header->code) goto unverified; \
215 (tblock) = NEW_BBLOCK (cfg); \
216 (tblock)->cil_code = (ip); \
217 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
219 (tblock)->real_offset = real_offset; \
222 #define CHECK_BBLOCK(target,ip,tblock) do { \
223 if ((target) < (ip) && !(tblock)->code) { \
224 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
225 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); \
229 #define NEW_ICONST(cfg,dest,val) do { \
230 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
231 (dest)->opcode = OP_ICONST; \
232 (dest)->inst_c0 = (val); \
233 (dest)->type = STACK_I4; \
236 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
237 #define NEW_PCONST(cfg,dest,val) do { \
238 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
239 (dest)->opcode = OP_ICONST; \
240 (dest)->inst_p0 = (val); \
241 (dest)->type = STACK_PTR; \
244 #define NEW_CLASSCONST(cfg,dest,val) do { \
245 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
246 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
247 (dest)->inst_p0 = (val); \
248 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
249 (dest)->type = STACK_PTR; \
252 #define NEW_IMAGECONST(cfg,dest,val) do { \
253 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
254 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
255 (dest)->inst_p0 = (val); \
256 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
257 (dest)->type = STACK_PTR; \
260 #define NEW_FIELDCONST(cfg,dest,field) do { \
261 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
262 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
263 (dest)->inst_p0 = (field); \
264 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
265 (dest)->type = STACK_PTR; \
268 #define NEW_METHODCONST(cfg,dest,val) do { \
269 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
270 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
271 (dest)->inst_p0 = (val); \
272 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
273 (dest)->type = STACK_PTR; \
276 #define NEW_VTABLECONST(cfg,dest,vtable) do { \
277 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
278 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
279 (dest)->inst_p0 = mono_compile_aot ? (gpointer)((vtable)->klass) : (vtable); \
280 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_VTABLE; \
281 (dest)->type = STACK_PTR; \
284 #define NEW_SFLDACONST(cfg,dest,field) do { \
285 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
286 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
287 (dest)->inst_p0 = (field); \
288 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_SFLDA; \
289 (dest)->type = STACK_PTR; \
292 #define NEW_LDSTRCONST(cfg,dest,token) do { \
293 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
294 (dest)->opcode = OP_AOTCONST; \
295 (dest)->inst_p0 = (gpointer)(token); \
296 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDSTR; \
297 (dest)->type = STACK_OBJ; \
300 #define NEW_TYPE_FROM_HANDLE_CONST(cfg,dest,token) do { \
301 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
302 (dest)->opcode = OP_AOTCONST; \
303 (dest)->inst_p0 = (gpointer)(token); \
304 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_TYPE_FROM_HANDLE; \
305 (dest)->type = STACK_OBJ; \
308 #define NEW_LDTOKENCONST(cfg,dest,token) do { \
309 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
310 (dest)->opcode = OP_AOTCONST; \
311 (dest)->inst_p0 = (gpointer)(token); \
312 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_LDTOKEN; \
313 (dest)->type = STACK_PTR; \
316 #define NEW_DOMAINCONST(cfg,dest) do { \
317 if (cfg->opt & MONO_OPT_SHARED) { \
318 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
320 NEW_PCONST (cfg, dest, (cfg)->domain); \
324 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
326 #define NEW_ARGLOAD(cfg,dest,num) do { \
327 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
328 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
329 (dest)->ssa_op = MONO_SSA_LOAD; \
330 (dest)->inst_i0 = arg_array [(num)]; \
331 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
332 type_to_eval_stack_type (param_types [(num)], (dest)); \
333 (dest)->klass = (dest)->inst_i0->klass; \
336 #define NEW_LOCLOAD(cfg,dest,num) do { \
337 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
338 (dest)->ssa_op = MONO_SSA_LOAD; \
339 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
340 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
341 type_to_eval_stack_type (header->locals [(num)], (dest)); \
342 (dest)->klass = (dest)->inst_i0->klass; \
345 #define NEW_LOCLOADA(cfg,dest,num) do { \
346 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
347 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
348 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
349 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
350 (dest)->opcode = OP_LDADDR; \
351 (dest)->type = STACK_MP; \
352 (dest)->klass = (dest)->inst_i0->klass; \
353 (cfg)->disable_ssa = TRUE; \
356 #define NEW_RETLOADA(cfg,dest) do { \
357 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
358 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
359 (dest)->inst_i0 = (cfg)->ret; \
360 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
361 (dest)->opcode = CEE_LDIND_I; \
362 (dest)->type = STACK_MP; \
363 (dest)->klass = (dest)->inst_i0->klass; \
364 (cfg)->disable_ssa = TRUE; \
367 #define NEW_ARGLOADA(cfg,dest,num) do { \
368 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
369 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
370 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
371 (dest)->inst_i0 = arg_array [(num)]; \
372 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
373 (dest)->opcode = OP_LDADDR; \
374 (dest)->type = STACK_MP; \
375 (dest)->klass = (dest)->inst_i0->klass; \
376 (cfg)->disable_ssa = TRUE; \
379 #define NEW_TEMPLOAD(cfg,dest,num) do { \
380 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
381 (dest)->ssa_op = MONO_SSA_LOAD; \
382 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
383 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
384 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
385 (dest)->klass = (dest)->inst_i0->klass; \
388 #define NEW_TEMPLOADA(cfg,dest,num) do { \
389 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
390 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
391 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
392 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
393 (dest)->opcode = OP_LDADDR; \
394 (dest)->type = STACK_MP; \
395 (dest)->klass = (dest)->inst_i0->klass; \
396 (cfg)->disable_ssa = TRUE; \
400 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
401 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
402 (dest)->inst_left = addr; \
403 (dest)->opcode = mono_type_to_ldind (vtype); \
404 type_to_eval_stack_type (vtype, (dest)); \
405 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
408 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
409 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
410 (dest)->inst_i0 = addr; \
411 (dest)->opcode = mono_type_to_stind (vtype); \
412 (dest)->inst_i1 = (value); \
413 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
416 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
417 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
418 (dest)->ssa_op = MONO_SSA_STORE; \
419 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
420 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
421 (dest)->inst_i1 = (inst); \
422 (dest)->klass = (dest)->inst_i0->klass; \
425 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
426 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
427 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
428 (dest)->ssa_op = MONO_SSA_STORE; \
429 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
430 (dest)->inst_i1 = (inst); \
431 (dest)->klass = (dest)->inst_i0->klass; \
434 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
435 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
436 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
437 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
438 (dest)->ssa_op = MONO_SSA_STORE; \
439 (dest)->inst_i0 = arg_array [(num)]; \
440 (dest)->inst_i1 = (inst); \
441 (dest)->klass = (dest)->inst_i0->klass; \
444 #define ADD_BINOP(op) do { \
445 MONO_INST_NEW (cfg, ins, (op)); \
446 ins->cil_code = ip; \
448 ins->inst_i0 = sp [0]; \
449 ins->inst_i1 = sp [1]; \
451 type_from_op (ins); \
455 #define ADD_UNOP(op) do { \
456 MONO_INST_NEW (cfg, ins, (op)); \
457 ins->cil_code = ip; \
459 ins->inst_i0 = sp [0]; \
461 type_from_op (ins); \
465 #define ADD_BINCOND(next_block) do { \
467 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
469 cmp->inst_i0 = sp [0]; \
470 cmp->inst_i1 = sp [1]; \
471 cmp->cil_code = ins->cil_code; \
472 type_from_op (cmp); \
474 ins->inst_i0 = cmp; \
475 MONO_ADD_INS (bblock, ins); \
476 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
477 GET_BBLOCK (cfg, bbhash, tblock, target); \
478 link_bblock (cfg, bblock, tblock); \
479 ins->inst_true_bb = tblock; \
480 CHECK_BBLOCK (target, ip, tblock); \
481 if ((next_block)) { \
482 link_bblock (cfg, bblock, (next_block)); \
483 ins->inst_false_bb = (next_block); \
484 start_new_bblock = 1; \
486 GET_BBLOCK (cfg, bbhash, tblock, ip); \
487 link_bblock (cfg, bblock, tblock); \
488 ins->inst_false_bb = tblock; \
489 start_new_bblock = 2; \
493 /* FIXME: handle float, long ... */
494 #define ADD_UNCOND(istrue) do { \
496 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
498 cmp->inst_i0 = sp [0]; \
499 switch (cmp->inst_i0->type) { \
501 cmp->inst_i1 = zero_int64; break; \
503 cmp->inst_i1 = zero_r8; break; \
506 cmp->inst_i1 = zero_ptr; break; \
508 cmp->inst_i1 = zero_obj; break; \
510 cmp->inst_i1 = zero_int32; \
512 cmp->cil_code = ins->cil_code; \
513 type_from_op (cmp); \
515 ins->inst_i0 = cmp; \
516 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
517 MONO_ADD_INS (bblock, ins); \
518 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
519 GET_BBLOCK (cfg, bbhash, tblock, target); \
520 link_bblock (cfg, bblock, tblock); \
521 ins->inst_true_bb = tblock; \
522 CHECK_BBLOCK (target, ip, tblock); \
523 GET_BBLOCK (cfg, bbhash, tblock, ip); \
524 link_bblock (cfg, bblock, tblock); \
525 ins->inst_false_bb = tblock; \
526 start_new_bblock = 2; \
529 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
530 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
531 (dest)->opcode = CEE_LDELEMA; \
532 (dest)->inst_left = (sp) [0]; \
533 (dest)->inst_right = (sp) [1]; \
534 (dest)->type = STACK_MP; \
535 (dest)->klass = (k); \
538 #define NEW_GROUP(cfg,dest,el1,el2) do { \
539 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
540 (dest)->opcode = OP_GROUP; \
541 (dest)->inst_left = (el1); \
542 (dest)->inst_right = (el2); \
547 compare_bblock (gconstpointer a, gconstpointer b)
549 const MonoBasicBlock *b1 = a;
550 const MonoBasicBlock *b2 = b;
552 return b2->cil_code - b1->cil_code;
557 * link_bblock: Links two basic blocks
559 * links two basic blocks in the control flow graph, the 'from'
560 * argument is the starting block and the 'to' argument is the block
561 * the control flow ends to after 'from'.
564 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
566 MonoBasicBlock **newa;
570 if (from->cil_code) {
572 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
574 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
577 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
579 g_print ("edge from entry to exit\n");
583 for (i = 0; i < from->out_count; ++i) {
584 if (to == from->out_bb [i]) {
590 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
591 for (i = 0; i < from->out_count; ++i) {
592 newa [i] = from->out_bb [i];
600 for (i = 0; i < to->in_count; ++i) {
601 if (from == to->in_bb [i]) {
607 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
608 for (i = 0; i < to->in_count; ++i) {
609 newa [i] = to->in_bb [i];
618 * We mark each basic block with a region ID. We use that to avoid BB
619 * optimizations when blocks are in different regions.
622 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
624 MonoMethod *method = cfg->method;
625 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
626 MonoExceptionClause *clause;
629 /* first search for handlers and filters */
630 for (i = 0; i < header->num_clauses; ++i) {
631 clause = &header->clauses [i];
632 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
633 (offset < (clause->token_or_filter + filter_lengths [i])))
634 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
636 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
637 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
638 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
640 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
644 /* search the try blocks */
645 for (i = 0; i < header->num_clauses; ++i) {
646 clause = &header->clauses [i];
647 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
648 return ((i + 1) << 8) | clause->flags;
655 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
657 MonoMethod *method = cfg->method;
658 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
659 MonoExceptionClause *clause;
660 MonoBasicBlock *handler;
664 for (i = 0; i < header->num_clauses; ++i) {
665 clause = &header->clauses [i];
666 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
667 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
668 if (clause->flags == type) {
669 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
671 res = g_list_append (res, handler);
680 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
684 array [*dfn] = start;
685 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
686 for (i = 0; i < start->out_count; ++i) {
687 if (start->out_bb [i]->dfn)
690 start->out_bb [i]->dfn = *dfn;
691 start->out_bb [i]->df_parent = start;
692 array [*dfn] = start->out_bb [i];
693 df_visit (start->out_bb [i], dfn, array);
699 MonoBasicBlock *best;
703 previous_foreach (gconstpointer key, gpointer val, gpointer data)
705 PrevStruct *p = data;
706 MonoBasicBlock *bb = val;
707 //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,
708 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
710 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
714 static MonoBasicBlock*
715 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
721 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
726 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
735 * FIXME: take into account all the details:
736 * second may have been the target of more than one bblock
738 second->out_count = first->out_count;
739 second->out_bb = first->out_bb;
741 for (i = 0; i < first->out_count; ++i) {
742 bb = first->out_bb [i];
743 for (j = 0; j < bb->in_count; ++j) {
744 if (bb->in_bb [j] == first)
745 bb->in_bb [j] = second;
749 first->out_count = 0;
750 first->out_bb = NULL;
751 link_bblock (cfg, first, second);
753 second->last_ins = first->last_ins;
755 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
756 for (inst = first->code; inst && inst->next; inst = inst->next) {
757 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
758 g_print ("found %p: %s", inst->next->cil_code, code);
760 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
761 second->code = inst->next;
763 first->last_ins = inst;
764 second->next_bb = first->next_bb;
765 first->next_bb = second;
770 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
776 mono_type_to_ldind (MonoType *type)
788 case MONO_TYPE_BOOLEAN:
802 case MONO_TYPE_FNPTR:
804 case MONO_TYPE_CLASS:
805 case MONO_TYPE_STRING:
806 case MONO_TYPE_OBJECT:
807 case MONO_TYPE_SZARRAY:
808 case MONO_TYPE_ARRAY:
809 return CEE_LDIND_REF;
817 case MONO_TYPE_VALUETYPE:
818 if (type->data.klass->enumtype) {
819 t = type->data.klass->enum_basetype->type;
823 case MONO_TYPE_TYPEDBYREF:
825 case MONO_TYPE_GENERICINST:
826 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
828 return CEE_LDIND_REF;
830 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
836 mono_type_to_stind (MonoType *type)
847 case MONO_TYPE_BOOLEAN:
859 case MONO_TYPE_FNPTR:
861 case MONO_TYPE_CLASS:
862 case MONO_TYPE_STRING:
863 case MONO_TYPE_OBJECT:
864 case MONO_TYPE_SZARRAY:
865 case MONO_TYPE_ARRAY:
866 return CEE_STIND_REF;
874 case MONO_TYPE_VALUETYPE:
875 if (type->data.klass->enumtype) {
876 t = type->data.klass->enum_basetype->type;
880 case MONO_TYPE_TYPEDBYREF:
882 case MONO_TYPE_GENERICINST:
883 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
885 return CEE_STIND_REF;
887 g_error ("unknown type 0x%02x in type_to_stind", type->type);
893 * Returns the type used in the eval stack when @type is loaded.
894 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
897 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
901 inst->type = STACK_MP;
909 case MONO_TYPE_BOOLEAN:
915 inst->type = STACK_I4;
920 case MONO_TYPE_FNPTR:
921 inst->type = STACK_PTR;
923 case MONO_TYPE_CLASS:
924 case MONO_TYPE_STRING:
925 case MONO_TYPE_OBJECT:
926 case MONO_TYPE_SZARRAY:
927 case MONO_TYPE_ARRAY:
928 inst->type = STACK_OBJ;
932 inst->type = STACK_I8;
936 inst->type = STACK_R8;
938 case MONO_TYPE_VALUETYPE:
939 if (type->data.klass->enumtype) {
940 t = type->data.klass->enum_basetype->type;
943 inst->klass = type->data.klass;
944 inst->type = STACK_VTYPE;
947 case MONO_TYPE_TYPEDBYREF:
948 inst->klass = mono_defaults.typed_reference_class;
949 inst->type = STACK_VTYPE;
951 case MONO_TYPE_GENERICINST:
952 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
953 inst->klass = mono_class_from_mono_type (type);
954 inst->type = STACK_VTYPE;
956 inst->type = STACK_OBJ;
960 g_error ("unknown type 0x%02x in eval stack type", type->type);
965 * The following tables are used to quickly validate the IL code in type_from_op ().
968 bin_num_table [STACK_MAX] [STACK_MAX] = {
969 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
970 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
971 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
972 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
973 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
974 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
975 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
976 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
981 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
984 /* reduce the size of this table */
986 bin_int_table [STACK_MAX] [STACK_MAX] = {
987 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
988 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
989 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
990 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
991 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
992 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
993 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
994 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
998 bin_comp_table [STACK_MAX] [STACK_MAX] = {
1000 {0, 1, 0, 1, 0, 0, 4, 0},
1001 {0, 0, 1, 0, 0, 0, 0, 0},
1002 {0, 1, 0, 1, 0, 2, 4, 0},
1003 {0, 0, 0, 0, 1, 0, 0, 0},
1004 {0, 0, 0, 2, 0, 1, 0, 0},
1005 {0, 4, 0, 4, 0, 0, 3, 0},
1006 {0, 0, 0, 0, 0, 0, 0, 0},
1009 /* reduce the size of this table */
1011 shift_table [STACK_MAX] [STACK_MAX] = {
1012 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1013 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1014 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1015 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1016 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1017 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1018 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
1019 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
1023 * Tables to map from the non-specific opcode to the matching
1024 * type-specific opcode.
1026 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
1027 static const guint16
1028 binops_op_map [STACK_MAX] = {
1029 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
1032 /* handles from CEE_NEG to CEE_CONV_U8 */
1033 static const guint16
1034 unops_op_map [STACK_MAX] = {
1035 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1038 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1039 static const guint16
1040 ovfops_op_map [STACK_MAX] = {
1041 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1044 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1045 static const guint16
1046 ovf2ops_op_map [STACK_MAX] = {
1047 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
1050 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1051 static const guint16
1052 ovf3ops_op_map [STACK_MAX] = {
1053 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
1056 /* handles from CEE_CEQ to CEE_CLT_UN */
1057 static const guint16
1058 ceqops_op_map [STACK_MAX] = {
1059 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1063 * Sets ins->type (the type on the eval stack) according to the
1064 * type of the opcode and the arguments to it.
1065 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1067 * FIXME: this function sets ins->type unconditionally in some cases, but
1068 * it should set it to invalid for some types (a conv.x on an object)
1071 type_from_op (MonoInst *ins) {
1072 switch (ins->opcode) {
1079 /* FIXME: check unverifiable args for STACK_MP */
1080 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1081 ins->opcode += binops_op_map [ins->type];
1088 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1089 ins->opcode += binops_op_map [ins->type];
1094 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1095 ins->opcode += binops_op_map [ins->type];
1098 /* FIXME: handle some specifics with ins->next->type */
1099 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1103 case 256+CEE_CGT_UN:
1105 case 256+CEE_CLT_UN:
1106 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1107 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1111 ins->type = neg_table [ins->inst_i0->type];
1112 ins->opcode += unops_op_map [ins->type];
1115 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1116 ins->type = ins->inst_i0->type;
1118 ins->type = STACK_INV;
1119 ins->opcode += unops_op_map [ins->type];
1125 ins->type = STACK_I4;
1126 ins->opcode += unops_op_map [ins->inst_i0->type];
1129 ins->type = STACK_R8;
1130 switch (ins->inst_i0->type) {
1135 ins->opcode = OP_LCONV_TO_R_UN;
1139 case CEE_CONV_OVF_I1:
1140 case CEE_CONV_OVF_U1:
1141 case CEE_CONV_OVF_I2:
1142 case CEE_CONV_OVF_U2:
1143 case CEE_CONV_OVF_I4:
1144 case CEE_CONV_OVF_U4:
1145 ins->type = STACK_I4;
1146 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1148 case CEE_CONV_OVF_I_UN:
1149 case CEE_CONV_OVF_U_UN:
1150 ins->type = STACK_PTR;
1151 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1153 case CEE_CONV_OVF_I1_UN:
1154 case CEE_CONV_OVF_I2_UN:
1155 case CEE_CONV_OVF_I4_UN:
1156 case CEE_CONV_OVF_U1_UN:
1157 case CEE_CONV_OVF_U2_UN:
1158 case CEE_CONV_OVF_U4_UN:
1159 ins->type = STACK_I4;
1160 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1163 ins->type = STACK_PTR;
1164 switch (ins->inst_i0->type) {
1170 ins->opcode = OP_LCONV_TO_U;
1173 ins->opcode = OP_FCONV_TO_U;
1179 ins->type = STACK_I8;
1180 ins->opcode += unops_op_map [ins->inst_i0->type];
1182 case CEE_CONV_OVF_I8:
1183 case CEE_CONV_OVF_U8:
1184 ins->type = STACK_I8;
1185 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1187 case CEE_CONV_OVF_U8_UN:
1188 case CEE_CONV_OVF_I8_UN:
1189 ins->type = STACK_I8;
1190 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1194 ins->type = STACK_R8;
1195 ins->opcode += unops_op_map [ins->inst_i0->type];
1198 ins->type = STACK_R8;
1202 ins->type = STACK_I4;
1203 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1206 case CEE_CONV_OVF_I:
1207 case CEE_CONV_OVF_U:
1208 ins->type = STACK_PTR;
1209 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1212 case CEE_ADD_OVF_UN:
1214 case CEE_MUL_OVF_UN:
1216 case CEE_SUB_OVF_UN:
1217 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1218 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1221 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1228 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1231 /* map ldelem.x to the matching ldind.x opcode */
1233 ldelem_to_ldind [] = {
1247 /* map stelem.x to the matching stind.x opcode */
1249 stelem_to_stind [] = {
1263 param_table [STACK_MAX] [STACK_MAX] = {
1268 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1272 switch (args->type) {
1282 for (i = 0; i < sig->param_count; ++i) {
1283 switch (args [i].type) {
1287 if (!sig->params [i]->byref)
1291 if (sig->params [i]->byref)
1293 switch (sig->params [i]->type) {
1294 case MONO_TYPE_CLASS:
1295 case MONO_TYPE_STRING:
1296 case MONO_TYPE_OBJECT:
1297 case MONO_TYPE_SZARRAY:
1298 case MONO_TYPE_ARRAY:
1305 if (sig->params [i]->byref)
1307 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1316 /*if (!param_table [args [i].type] [sig->params [i]->type])
1324 * When we need a pointer to the current domain many times in a method, we
1325 * call mono_domain_get() once and we store the result in a local variable.
1326 * This function returns the variable that represents the MonoDomain*.
1328 inline static MonoInst *
1329 mono_get_domainvar (MonoCompile *cfg)
1331 if (!cfg->domainvar)
1332 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1333 return cfg->domainvar;
1337 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1340 int num = cfg->num_varinfo;
1342 if ((num + 1) >= cfg->varinfo_count) {
1343 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1344 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1345 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1348 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1349 mono_jit_stats.allocate_var++;
1351 MONO_INST_NEW (cfg, inst, opcode);
1352 inst->inst_c0 = num;
1353 inst->inst_vtype = type;
1354 inst->klass = mono_class_from_mono_type (type);
1355 /* if set to 1 the variable is native */
1358 cfg->varinfo [num] = inst;
1360 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1361 MONO_INIT_VARINFO (cfg->vars [num], num);
1364 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1369 type_from_stack_type (MonoInst *ins) {
1370 switch (ins->type) {
1371 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1372 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1373 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1374 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1375 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1376 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1377 case STACK_VTYPE: return &ins->klass->byval_arg;
1379 g_error ("stack type %d to montype not handled\n", ins->type);
1385 array_access_to_klass (int opcode)
1389 return mono_defaults.byte_class;
1391 return mono_defaults.uint16_class;
1394 return mono_defaults.int_class;
1397 return mono_defaults.sbyte_class;
1400 return mono_defaults.int16_class;
1403 return mono_defaults.int32_class;
1405 return mono_defaults.uint32_class;
1408 return mono_defaults.int64_class;
1411 return mono_defaults.single_class;
1414 return mono_defaults.double_class;
1415 case CEE_LDELEM_REF:
1416 case CEE_STELEM_REF:
1417 return mono_defaults.object_class;
1419 g_assert_not_reached ();
1425 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1429 MONO_ADD_INS (bb, inst);
1432 switch (bb->last_ins->opcode) {
1445 while (prev->next && prev->next != bb->last_ins)
1447 if (prev == bb->code) {
1448 if (bb->last_ins == bb->code) {
1449 inst->next = bb->code;
1452 inst->next = prev->next;
1456 inst->next = bb->last_ins;
1460 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1462 MONO_ADD_INS (bb, inst);
1468 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1470 MonoInst *inst, *load;
1472 NEW_TEMPLOAD (cfg, load, src);
1474 NEW_TEMPSTORE (cfg, inst, dest, load);
1475 if (inst->opcode == CEE_STOBJ) {
1476 NEW_TEMPLOADA (cfg, inst, dest);
1477 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1479 inst->cil_code = NULL;
1480 mono_add_ins_to_end (bb, inst);
1485 * We try to share variables when possible
1488 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1493 /* inlining can result in deeper stacks */
1494 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1495 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1497 pos = ins->type - 1 + slot * STACK_MAX;
1499 switch (ins->type) {
1506 if ((vnum = cfg->intvars [pos]))
1507 return cfg->varinfo [vnum];
1508 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1509 cfg->intvars [pos] = res->inst_c0;
1512 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1518 * This function is called to handle items that are left on the evaluation stack
1519 * at basic block boundaries. What happens is that we save the values to local variables
1520 * and we reload them later when first entering the target basic block (with the
1521 * handle_loaded_temps () function).
1522 * A single joint point will use the same variables (stored in the array bb->out_stack or
1523 * bb->in_stack, if the basic block is before or after the joint point).
1526 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1528 MonoBasicBlock *outb;
1529 MonoInst *inst, **locals;
1533 if (cfg->verbose_level > 3)
1534 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1535 if (!bb->out_scount) {
1537 bb->out_scount = count;
1538 //g_print ("bblock %d has out:", bb->block_num);
1539 for (i = 0; i < bb->out_count; ++i) {
1540 outb = bb->out_bb [i];
1541 //g_print (" %d", outb->block_num);
1542 if (outb->in_stack) {
1544 bb->out_stack = outb->in_stack;
1550 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1551 for (i = 0; i < count; ++i) {
1553 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1554 * stack slot and if they are of the same type. */
1555 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1557 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1562 locals = bb->out_stack;
1563 for (i = 0; i < count; ++i) {
1564 /* add store ops at the end of the bb, before the branch */
1565 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1566 if (inst->opcode == CEE_STOBJ) {
1567 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1568 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1570 inst->cil_code = sp [i]->cil_code;
1571 mono_add_ins_to_end (bb, inst);
1573 if (cfg->verbose_level > 3)
1574 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1577 for (i = 0; i < bb->out_count; ++i) {
1578 outb = bb->out_bb [i];
1579 if (outb->in_scount)
1580 continue; /* check they are the same locals */
1581 outb->in_scount = count;
1582 outb->in_stack = locals;
1588 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1593 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1597 case MONO_TYPE_VOID:
1598 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1601 case MONO_TYPE_BOOLEAN:
1604 case MONO_TYPE_CHAR:
1607 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1611 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1612 case MONO_TYPE_CLASS:
1613 case MONO_TYPE_STRING:
1614 case MONO_TYPE_OBJECT:
1615 case MONO_TYPE_SZARRAY:
1616 case MONO_TYPE_ARRAY:
1617 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1620 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1623 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1624 case MONO_TYPE_VALUETYPE:
1625 if (type->data.klass->enumtype) {
1626 t = type->data.klass->enum_basetype->type;
1629 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1630 case MONO_TYPE_TYPEDBYREF:
1631 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1633 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1639 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1641 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1643 ji->ip.label = label;
1644 ji->type = MONO_PATCH_INFO_SWITCH;
1645 ji->data.table = bbs;
1646 ji->next = cfg->patch_info;
1647 ji->table_size = num_blocks;
1648 cfg->patch_info = ji;
1652 * When we add a tree of instructions, we need to ensure the instructions currently
1653 * on the stack are executed before (like, if we load a value from a local).
1654 * We ensure this by saving the currently loaded values to temps and rewriting the
1655 * instructions to load the values.
1656 * This is not done for opcodes that terminate a basic block (because it's handled already
1657 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1660 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1662 MonoInst *load, *store, *temp, *ins;
1664 while (stack < sp) {
1666 /* handle also other constants */
1667 if (ins->opcode != OP_ICONST) {
1668 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1669 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1670 store->cil_code = ins->cil_code;
1671 if (store->opcode == CEE_STOBJ) {
1672 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1673 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1675 MONO_ADD_INS (bblock, store);
1676 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1677 load->cil_code = ins->cil_code;
1685 * Prepare arguments for passing to a function call.
1686 * Return a non-zero value if the arguments can't be passed to the given
1688 * The type checks are not yet complete and some conversions may need
1689 * casts on 32 or 64 bit architectures.
1692 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1697 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1701 for (i = 0; i < sig->param_count; ++i) {
1702 if (sig->params [i]->byref) {
1704 * check the result of ldelema is only passed as an argument if the byref
1705 * type matches exactly the array element type.
1706 * FIXME: if the argument as been saved on the stack as part of the
1707 * interface variable code (the value was on the stack at a basic block boundary)
1708 * we need to add the check in that case, too.
1710 if (args [i]->opcode == CEE_LDELEMA) {
1712 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1713 if (!exact_class->valuetype) {
1714 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1715 check->cil_code = args [i]->cil_code;
1716 check->klass = exact_class;
1717 check->inst_left = args [i]->inst_left;
1718 check->type = STACK_OBJ;
1719 args [i]->inst_left = check;
1722 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1726 simple_type = sig->params [i]->type;
1728 switch (simple_type) {
1729 case MONO_TYPE_VOID:
1734 case MONO_TYPE_BOOLEAN:
1737 case MONO_TYPE_CHAR:
1740 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1746 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1749 case MONO_TYPE_CLASS:
1750 case MONO_TYPE_STRING:
1751 case MONO_TYPE_OBJECT:
1752 case MONO_TYPE_SZARRAY:
1753 case MONO_TYPE_ARRAY:
1754 if (args [i]->type != STACK_OBJ)
1759 if (args [i]->type != STACK_I8)
1764 if (args [i]->type != STACK_R8)
1767 case MONO_TYPE_VALUETYPE:
1768 if (sig->params [i]->data.klass->enumtype) {
1769 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1772 if (args [i]->type != STACK_VTYPE)
1775 case MONO_TYPE_TYPEDBYREF:
1776 if (args [i]->type != STACK_VTYPE)
1779 case MONO_TYPE_GENERICINST:
1780 simple_type = sig->params [i]->data.generic_inst->generic_type->type;
1784 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1791 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1792 const guint8 *ip, gboolean to_end)
1794 MonoInst *temp, *store, *ins = (MonoInst*)call;
1795 MonoType *ret = sig->ret;
1797 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1799 call->inst.type = STACK_OBJ;
1800 call->inst.opcode = CEE_CALL;
1801 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1803 type_to_eval_stack_type (ret, ins);
1804 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1807 if (MONO_TYPE_ISSTRUCT (ret)) {
1810 /* we use this to allocate native sized structs */
1811 temp->unused = sig->pinvoke;
1813 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1814 if (call->inst.opcode == OP_VCALL)
1815 ins->inst_left = loada;
1817 ins->inst_right = loada; /* a virtual or indirect call */
1820 mono_add_ins_to_end (bblock, ins);
1822 MONO_ADD_INS (bblock, ins);
1824 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1825 store->cil_code = ip;
1827 mono_add_ins_to_end (bblock, store);
1829 MONO_ADD_INS (bblock, store);
1831 return temp->inst_c0;
1834 mono_add_ins_to_end (bblock, ins);
1836 MONO_ADD_INS (bblock, ins);
1841 inline static MonoCallInst *
1842 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1843 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1848 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1850 call->inst.cil_code = ip;
1852 call->signature = sig;
1853 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1855 for (arg = call->out_args; arg;) {
1856 MonoInst *narg = arg->next;
1861 mono_add_ins_to_end (bblock, arg);
1863 MONO_ADD_INS (bblock, arg);
1870 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1871 MonoInst **args, MonoInst *addr, const guint8 *ip)
1873 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1875 call->inst.inst_i0 = addr;
1877 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1880 static MonoCallInst*
1881 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1882 MonoInst **args, const guint8 *ip, MonoInst *this)
1884 gboolean virtual = this != NULL;
1887 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1889 if (this && sig->hasthis &&
1890 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1891 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1892 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1894 call->method = method;
1896 call->inst.flags |= MONO_INST_HAS_METHOD;
1897 call->inst.inst_left = this;
1903 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1904 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1906 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1908 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1912 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1913 MonoInst **args, const guint8 *ip, gboolean to_end)
1919 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1921 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1925 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1927 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1930 g_warning ("unregistered JIT ICall");
1931 g_assert_not_reached ();
1934 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1938 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1940 MonoInst *ins, *temp = NULL, *store, *load, *begin;
1941 MonoInst *last_arg = NULL;
1945 /*g_print ("emulating: ");
1946 mono_print_tree (tree);
1948 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1949 ins = (MonoInst*)call;
1951 call->inst.cil_code = tree->cil_code;
1953 call->signature = info->sig;
1955 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1957 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1958 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1959 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1960 store->cil_code = tree->cil_code;
1965 nargs = info->sig->param_count + info->sig->hasthis;
1967 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
1970 last_arg->next = store;
1973 begin = call->out_args;
1977 if (cfg->prev_ins) {
1978 store->next = cfg->prev_ins->next;
1979 cfg->prev_ins->next = begin;
1981 store->next = cfg->cbb->code;
1982 cfg->cbb->code = begin;
1985 call->fptr = info->wrapper;
1987 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1988 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1993 static MonoMethodSignature *
1994 mono_get_element_address_signature (int arity)
1996 static GHashTable *sighash = NULL;
1997 MonoMethodSignature *res;
2001 sighash = g_hash_table_new (NULL, NULL);
2004 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
2007 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
2009 res->params [0] = &mono_defaults.array_class->byval_arg;
2011 for (i = 1; i <= arity; i++)
2012 res->params [i] = &mono_defaults.int_class->byval_arg;
2014 res->ret = &mono_defaults.int_class->byval_arg;
2016 g_hash_table_insert (sighash, (gpointer)arity, res);
2022 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
2023 MonoInst *iargs [3];
2029 * This check breaks with spilled vars... need to handle it during verification anyway.
2030 * g_assert (klass && klass == src->klass && klass == dest->klass);
2034 n = mono_class_native_size (klass, &align);
2036 n = mono_class_value_size (klass, &align);
2038 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
2040 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2041 inst->inst_left = dest;
2042 inst->inst_right = src;
2043 inst->cil_code = ip;
2045 MONO_ADD_INS (bblock, inst);
2050 NEW_ICONST (cfg, iargs [2], n);
2052 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2056 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2058 MonoInst *iargs [2];
2059 MonoInst *ins, *zero_int32;
2062 NEW_ICONST (cfg, zero_int32, 0);
2064 mono_class_init (klass);
2065 n = mono_class_value_size (klass, NULL);
2066 MONO_INST_NEW (cfg, ins, 0);
2068 ins->inst_left = dest;
2069 ins->inst_right = zero_int32;
2072 ins->opcode = CEE_STIND_I1;
2073 MONO_ADD_INS (bblock, ins);
2076 ins->opcode = CEE_STIND_I2;
2077 MONO_ADD_INS (bblock, ins);
2080 ins->opcode = CEE_STIND_I4;
2081 MONO_ADD_INS (bblock, ins);
2084 if (n <= sizeof (gpointer) * 5) {
2085 ins->opcode = OP_MEMSET;
2088 MONO_ADD_INS (bblock, ins);
2091 handle_loaded_temps (cfg, bblock, stack_start, sp);
2092 NEW_ICONST (cfg, ins, n);
2095 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2100 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2103 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2105 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2106 MonoMethodSignature *signature = method->signature;
2110 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2111 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2112 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2113 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2114 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2115 (method->klass->marshalbyref) ||
2116 !header || header->num_clauses ||
2117 /* fixme: why cant we inline valuetype returns? */
2118 MONO_TYPE_ISSTRUCT (signature->ret))
2121 /* its not worth to inline methods with valuetype arguments?? */
2122 for (i = 0; i < signature->param_count; i++) {
2123 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2129 * if we can initialize the class of the method right away, we do,
2130 * otherwise we don't allow inlining if the class needs initialization,
2131 * since it would mean inserting a call to mono_runtime_class_init()
2132 * inside the inlined code
2134 if (!(cfg->opt & MONO_OPT_SHARED)) {
2135 vtable = mono_class_vtable (cfg->domain, method->klass);
2136 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2137 mono_runtime_class_init (vtable);
2138 else if (!vtable->initialized && mono_class_needs_cctor_run (method->klass, NULL))
2142 * If we're compiling for shared code
2143 * the cctor will need to be run at aot method load time, for example,
2144 * or at the end of the compilation of the inlining method.
2146 if (mono_class_needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2149 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2151 /* also consider num_locals? */
2152 if (header->code_size < 20)
2159 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2163 MonoMethodSignature *esig;
2165 rank = cmethod->signature->param_count - (is_set? 1: 0);
2167 * FIXME: handle TypeMismatch for set or use the slow path
2170 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2172 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2173 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2174 addr->inst_left = sp [0];
2175 addr->inst_right = indexes;
2176 addr->cil_code = ip;
2177 addr->type = STACK_MP;
2178 addr->klass = cmethod->klass;
2181 esig = mono_get_element_address_signature (rank);
2182 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2183 NEW_TEMPLOAD (cfg, addr, temp);
2188 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2193 if (cmethod->klass == mono_defaults.string_class) {
2194 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2197 } else if (cmethod->klass == mono_defaults.math_class) {
2198 if (strcmp (cmethod->name, "Sin") == 0)
2200 else if (strcmp (cmethod->name, "Cos") == 0)
2202 else if (strcmp (cmethod->name, "Tan") == 0)
2204 else if (strcmp (cmethod->name, "Atan") == 0)
2206 else if (strcmp (cmethod->name, "Sqrt") == 0)
2208 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2211 /* OP_FREM is not IEEE compatible */
2212 else if (strcmp (cmethod->name, "IEEERemainder") == 0)
2217 } else if (cmethod->klass == mono_defaults.array_class) {
2218 if (strcmp (cmethod->name, "get_Rank") == 0)
2220 else if (strcmp (cmethod->name, "get_Length") == 0)
2227 pc = fsig->param_count + fsig->hasthis;
2228 MONO_INST_NEW (cfg, ins, op);
2231 ins->inst_i0 = args [0];
2233 ins->inst_i1 = args [1];
2240 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2242 MonoInst *store, *temp;
2245 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2247 if (!sig->hasthis && sig->param_count == 0)
2251 if (sp [0]->opcode == OP_ICONST) {
2254 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2256 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2257 store->cil_code = sp [0]->cil_code;
2258 MONO_ADD_INS (bblock, store);
2263 for (i = 0; i < sig->param_count; ++i) {
2264 if (sp [0]->opcode == OP_ICONST) {
2267 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2269 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2270 store->cil_code = sp [0]->cil_code;
2271 if (store->opcode == CEE_STOBJ) {
2272 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2273 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2275 MONO_ADD_INS (bblock, store);
2283 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2284 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2286 MonoInst *ins, *rvar = NULL;
2287 MonoMethodHeader *cheader;
2288 MonoBasicBlock *ebblock, *sbblock;
2289 int i, costs, new_locals_offset;
2291 if (cfg->verbose_level > 2)
2292 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2294 cheader = ((MonoMethodNormal *)cmethod)->header;
2296 if (!cmethod->inline_info) {
2297 mono_jit_stats.inlineable_methods++;
2298 cmethod->inline_info = 1;
2300 /* allocate space to store the return value */
2301 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2302 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2305 /* allocate local variables */
2306 new_locals_offset = cfg->num_varinfo;
2307 for (i = 0; i < cheader->num_locals; ++i)
2308 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2310 /* allocate starte and end blocks */
2311 sbblock = NEW_BBLOCK (cfg);
2312 sbblock->block_num = cfg->num_bblocks++;
2313 sbblock->real_offset = real_offset;
2315 ebblock = NEW_BBLOCK (cfg);
2316 ebblock->block_num = cfg->num_bblocks++;
2317 ebblock->real_offset = real_offset;
2319 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2321 if (costs >= 0 && costs < 60) {
2322 if (cfg->verbose_level > 2)
2323 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2325 mono_jit_stats.inlined_methods++;
2327 /* always add some code to avoid block split failures */
2328 MONO_INST_NEW (cfg, ins, CEE_NOP);
2329 MONO_ADD_INS (bblock, ins);
2332 bblock->next_bb = sbblock;
2333 link_bblock (cfg, bblock, sbblock);
2336 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2342 if (cfg->verbose_level > 2)
2343 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2349 * Some of these comments may well be out-of-date.
2350 * Design decisions: we do a single pass over the IL code (and we do bblock
2351 * splitting/merging in the few cases when it's required: a back jump to an IL
2352 * address that was not already seen as bblock starting point).
2353 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2354 * Complex operations are decomposed in simpler ones right away. We need to let the
2355 * arch-specific code peek and poke inside this process somehow (except when the
2356 * optimizations can take advantage of the full semantic info of coarse opcodes).
2357 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2358 * MonoInst->opcode initially is the IL opcode or some simplification of that
2359 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2360 * opcode with value bigger than OP_LAST.
2361 * At this point the IR can be handed over to an interpreter, a dumb code generator
2362 * or to the optimizing code generator that will translate it to SSA form.
2364 * Profiling directed optimizations.
2365 * We may compile by default with few or no optimizations and instrument the code
2366 * or the user may indicate what methods to optimize the most either in a config file
2367 * or through repeated runs where the compiler applies offline the optimizations to
2368 * each method and then decides if it was worth it.
2371 * * consider using an array instead of an hash table (bb_hash)
2374 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2375 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2376 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2378 #define TYPE_PARAM_TO_TYPE(num) (method->klass->generic_inst->data.generic_inst->type_argv [(num)])
2379 #define TYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (TYPE_PARAM_TO_TYPE ((num))))
2381 /* offset from br.s -> br like opcodes */
2382 #define BIG_BRANCH_OFFSET 13
2385 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end, unsigned char **pos)
2387 unsigned char *ip = start;
2388 unsigned char *target;
2391 MonoBasicBlock *bblock;
2392 const MonoOpcode *opcode;
2395 cli_addr = ip - start;
2396 i = mono_opcode_value ((const guint8 **)&ip);
2397 opcode = &mono_opcodes [i];
2398 switch (opcode->argument) {
2399 case MonoInlineNone:
2402 case MonoInlineString:
2403 case MonoInlineType:
2404 case MonoInlineField:
2405 case MonoInlineMethod:
2408 case MonoShortInlineR:
2415 case MonoShortInlineVar:
2416 case MonoShortInlineI:
2419 case MonoShortInlineBrTarget:
2420 target = start + cli_addr + 2 + (signed char)ip [1];
2421 GET_BBLOCK (cfg, bbhash, bblock, target);
2424 case MonoInlineBrTarget:
2425 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2426 GET_BBLOCK (cfg, bbhash, bblock, target);
2429 case MonoInlineSwitch: {
2430 guint32 n = read32 (ip + 1);
2433 cli_addr += 5 + 4 * n;
2434 target = start + cli_addr;
2435 GET_BBLOCK (cfg, bbhash, bblock, target);
2437 for (j = 0; j < n; ++j) {
2438 target = start + cli_addr + (gint32)read32 (ip);
2439 GET_BBLOCK (cfg, bbhash, bblock, target);
2449 g_assert_not_reached ();
2458 static MonoClassField *
2459 get_generic_field_inst (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2462 for (i = 0; i < field->parent->field.count; ++i) {
2463 if (field == &field->parent->fields [i]) {
2465 return &klass->fields [i];
2472 * mono_method_to_ir: translates IL into basic blocks containing trees
2475 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2476 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2477 guint inline_offset, gboolean is_virtual_call)
2479 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2480 MonoInst *ins, **sp, **stack_start;
2481 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2483 MonoMethod *cmethod;
2484 MonoInst **arg_array;
2485 MonoMethodHeader *header;
2487 guint32 token, ins_flag;
2489 unsigned char *ip, *end, *target, *err_pos;
2490 static double r8_0 = 0.0;
2491 MonoMethodSignature *sig;
2492 MonoType **param_types;
2493 GList *bb_recheck = NULL, *tmp;
2494 int i, n, start_new_bblock, align;
2495 int num_calls = 0, inline_costs = 0;
2496 int *filter_lengths = NULL;
2497 int breakpoint_id = 0;
2500 image = method->klass->image;
2501 header = ((MonoMethodNormal *)method)->header;
2502 sig = method->signature;
2503 ip = (unsigned char*)header->code;
2504 end = ip + header->code_size;
2505 mono_jit_stats.cil_code_size += header->code_size;
2507 if (cfg->method == method) {
2509 bbhash = cfg->bb_hash;
2511 real_offset = inline_offset;
2512 bbhash = g_hash_table_new (g_direct_hash, NULL);
2515 if (cfg->verbose_level > 2)
2516 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2518 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2519 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2521 dont_inline = g_list_prepend (dont_inline, method);
2522 if (cfg->method == method) {
2525 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2526 start_bblock->cil_code = NULL;
2527 start_bblock->cil_length = 0;
2528 start_bblock->block_num = cfg->num_bblocks++;
2531 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2532 end_bblock->cil_code = NULL;
2533 end_bblock->cil_length = 0;
2534 end_bblock->block_num = cfg->num_bblocks++;
2535 g_assert (cfg->num_bblocks == 2);
2537 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2538 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2539 arg_array [i] = cfg->varinfo [i];
2541 if (header->num_clauses) {
2542 int size = sizeof (int) * header->num_clauses;
2543 filter_lengths = alloca (size);
2544 memset (filter_lengths, 0, size);
2546 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2547 /* prevent it from being register allocated */
2548 cfg->spvar->flags |= MONO_INST_INDIRECT;
2550 /* handle exception clauses */
2551 for (i = 0; i < header->num_clauses; ++i) {
2552 //unsigned char *p = ip;
2553 MonoExceptionClause *clause = &header->clauses [i];
2554 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2555 tblock->real_offset = clause->try_offset;
2556 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2557 tblock->real_offset = clause->handler_offset;
2559 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2560 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2561 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2562 MONO_ADD_INS (tblock, ins);
2565 /*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);
2567 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2569 /* catch and filter blocks get the exception object on the stack */
2570 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2571 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2572 /* mostly like handle_stack_args (), but just sets the input args */
2573 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2575 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2576 /* prevent it from being register allocated */
2577 cfg->exvar->flags |= MONO_INST_INDIRECT;
2579 tblock->in_scount = 1;
2580 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2581 tblock->in_stack [0] = cfg->exvar;
2583 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2584 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2585 tblock->real_offset = clause->token_or_filter;
2586 tblock->in_scount = 1;
2587 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2588 tblock->in_stack [0] = cfg->exvar;
2589 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2590 MONO_ADD_INS (tblock, ins);
2596 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2597 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2600 /* FIRST CODE BLOCK */
2601 bblock = NEW_BBLOCK (cfg);
2602 bblock->cil_code = ip;
2604 ADD_BBLOCK (cfg, bbhash, bblock);
2606 if (cfg->method == method) {
2607 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2608 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2609 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2610 MONO_ADD_INS (bblock, ins);
2614 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED))) || mono_compile_aot) {
2615 /* we use a separate basic block for the initialization code */
2616 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2617 init_localsbb->real_offset = real_offset;
2618 start_bblock->next_bb = init_localsbb;
2619 init_localsbb->next_bb = bblock;
2620 link_bblock (cfg, start_bblock, init_localsbb);
2621 link_bblock (cfg, init_localsbb, bblock);
2622 init_localsbb->block_num = cfg->num_bblocks++;
2624 start_bblock->next_bb = bblock;
2625 link_bblock (cfg, start_bblock, bblock);
2628 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end, &err_pos)) {
2633 mono_debug_init_method (cfg, bblock, breakpoint_id);
2635 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2637 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2638 for (n = 0; n < sig->param_count; ++n)
2639 param_types [n + sig->hasthis] = sig->params [n];
2641 /* do this somewhere outside - not here */
2642 NEW_ICONST (cfg, zero_int32, 0);
2643 NEW_ICONST (cfg, zero_int64, 0);
2644 zero_int64->type = STACK_I8;
2645 NEW_PCONST (cfg, zero_ptr, 0);
2646 NEW_PCONST (cfg, zero_obj, 0);
2647 zero_obj->type = STACK_OBJ;
2649 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2650 zero_r8->type = STACK_R8;
2651 zero_r8->inst_p0 = &r8_0;
2653 /* add a check for this != NULL to inlined methods */
2654 if (is_virtual_call) {
2655 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2656 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2658 MONO_ADD_INS (bblock, ins);
2661 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2662 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2665 start_new_bblock = 0;
2668 if (cfg->method == method)
2669 real_offset = ip - header->code;
2671 real_offset = inline_offset;
2673 if (start_new_bblock) {
2674 bblock->cil_length = ip - bblock->cil_code;
2675 if (start_new_bblock == 2) {
2676 g_assert (ip == tblock->cil_code);
2678 GET_BBLOCK (cfg, bbhash, tblock, ip);
2680 bblock->next_bb = tblock;
2682 start_new_bblock = 0;
2683 for (i = 0; i < bblock->in_scount; ++i) {
2684 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2688 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2689 link_bblock (cfg, bblock, tblock);
2690 if (sp != stack_start) {
2691 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2694 bblock->next_bb = tblock;
2696 for (i = 0; i < bblock->in_scount; ++i) {
2697 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2703 if (cfg->coverage_info) {
2704 MonoInst *store, *one;
2705 guint32 cil_offset = ip - header->code;
2706 cfg->coverage_info->data [cil_offset].cil_code = ip;
2708 /* TODO: Use an increment here */
2709 NEW_ICONST (cfg, one, 1);
2712 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2715 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2716 store->cil_code = ip;
2717 store->inst_left = ins;
2718 store->inst_right = one;
2720 MONO_ADD_INS (bblock, store);
2723 if (cfg->verbose_level > 3)
2724 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2731 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2732 ins->cil_code = ip++;
2733 MONO_ADD_INS (bblock, ins);
2739 CHECK_STACK_OVF (1);
2740 n = (*ip)-CEE_LDARG_0;
2741 NEW_ARGLOAD (cfg, ins, n);
2742 ins->cil_code = ip++;
2749 CHECK_STACK_OVF (1);
2750 n = (*ip)-CEE_LDLOC_0;
2751 NEW_LOCLOAD (cfg, ins, n);
2752 ins->cil_code = ip++;
2760 n = (*ip)-CEE_STLOC_0;
2762 handle_loaded_temps (cfg, bblock, stack_start, sp);
2763 NEW_LOCSTORE (cfg, ins, n, *sp);
2765 if (ins->opcode == CEE_STOBJ) {
2766 NEW_LOCLOADA (cfg, ins, n);
2767 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2769 MONO_ADD_INS (bblock, ins);
2774 CHECK_STACK_OVF (1);
2775 NEW_ARGLOAD (cfg, ins, ip [1]);
2781 CHECK_STACK_OVF (1);
2782 NEW_ARGLOADA (cfg, ins, ip [1]);
2790 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2791 handle_loaded_temps (cfg, bblock, stack_start, sp);
2793 if (ins->opcode == CEE_STOBJ) {
2794 NEW_ARGLOADA (cfg, ins, ip [1]);
2795 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2797 MONO_ADD_INS (bblock, ins);
2801 CHECK_STACK_OVF (1);
2802 NEW_LOCLOAD (cfg, ins, ip [1]);
2808 CHECK_STACK_OVF (1);
2809 NEW_LOCLOADA (cfg, ins, ip [1]);
2817 handle_loaded_temps (cfg, bblock, stack_start, sp);
2818 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2820 if (ins->opcode == CEE_STOBJ) {
2821 NEW_LOCLOADA (cfg, ins, ip [1]);
2822 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2824 MONO_ADD_INS (bblock, ins);
2829 CHECK_STACK_OVF (1);
2830 NEW_PCONST (cfg, ins, NULL);
2832 ins->type = STACK_OBJ;
2837 CHECK_STACK_OVF (1);
2838 NEW_ICONST (cfg, ins, -1);
2852 CHECK_STACK_OVF (1);
2853 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2859 CHECK_STACK_OVF (1);
2861 NEW_ICONST (cfg, ins, *((signed char*)ip));
2867 CHECK_STACK_OVF (1);
2868 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2874 CHECK_STACK_OVF (1);
2875 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2877 ins->type = STACK_I8;
2879 ins->inst_l = (gint64)read64 (ip);
2884 float *f = g_malloc (sizeof (float));
2885 CHECK_STACK_OVF (1);
2886 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2887 ins->type = STACK_R8;
2896 double *d = g_malloc (sizeof (double));
2897 CHECK_STACK_OVF (1);
2898 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2899 ins->type = STACK_R8;
2908 MonoInst *temp, *store;
2910 CHECK_STACK_OVF (1);
2915 * small optimization: if the loaded value was from a local already,
2916 * just load it twice.
2918 if (ins->ssa_op == MONO_SSA_LOAD &&
2919 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2921 MONO_INST_NEW (cfg, temp, 0);
2923 temp->cil_code = ip;
2926 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2927 temp->cil_code = ip;
2928 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2929 store->cil_code = ip;
2930 MONO_ADD_INS (bblock, store);
2931 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2934 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2944 MONO_INST_NEW (cfg, ins, CEE_POP);
2945 MONO_ADD_INS (bblock, ins);
2946 ins->cil_code = ip++;
2951 if (stack_start != sp)
2953 MONO_INST_NEW (cfg, ins, CEE_JMP);
2954 token = read32 (ip + 1);
2955 /* FIXME: check the signature matches */
2956 cmethod = mono_get_method (image, token, NULL);
2957 ins->inst_p0 = cmethod;
2958 MONO_ADD_INS (bblock, ins);
2960 start_new_bblock = 1;
2964 case CEE_CALLVIRT: {
2965 MonoInst *addr = NULL;
2966 MonoMethodSignature *fsig = NULL;
2967 int temp, array_rank = 0;
2968 int virtual = *ip == CEE_CALLVIRT;
2970 token = read32 (ip + 1);
2972 if (*ip == CEE_CALLI) {
2977 if (method->wrapper_type != MONO_WRAPPER_NONE)
2978 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2980 fsig = mono_metadata_parse_signature (image, token);
2982 n = fsig->param_count + fsig->hasthis;
2985 if (method->wrapper_type != MONO_WRAPPER_NONE) {
2986 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2988 cmethod = mono_get_method (image, token, NULL);
2991 if (!cmethod->klass->inited)
2992 mono_class_init (cmethod->klass);
2994 if (cmethod->signature->pinvoke) {
2995 #ifdef MONO_USE_EXC_TABLES
2996 if (mono_method_blittable (cmethod)) {
2997 fsig = cmethod->signature;
3000 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
3001 fsig = wrapper->signature;
3002 #ifdef MONO_USE_EXC_TABLES
3006 fsig = mono_method_get_signature (cmethod, image, token);
3009 n = fsig->param_count + fsig->hasthis;
3011 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
3012 cmethod->klass->parent == mono_defaults.array_class) {
3013 array_rank = cmethod->klass->rank;
3016 if (cmethod->string_ctor)
3017 g_assert_not_reached ();
3023 //g_assert (!virtual || fsig->hasthis);
3027 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3030 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
3032 for (i = 0; i < n; ++i) {
3033 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3035 MONO_ADD_INS (bblock, ins);
3037 MONO_INST_NEW (cfg, ins, CEE_JMP);
3039 ins->inst_p0 = cmethod;
3040 MONO_ADD_INS (bblock, ins);
3041 start_new_bblock = 1;
3042 /* skip CEE_RET as well */
3047 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3050 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3051 MONO_ADD_INS (bblock, ins);
3053 type_to_eval_stack_type (fsig->ret, ins);
3062 handle_loaded_temps (cfg, bblock, stack_start, sp);
3064 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3065 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3066 mono_method_check_inlining (cfg, cmethod) &&
3067 !g_list_find (dont_inline, cmethod)) {
3069 MonoBasicBlock *ebblock;
3071 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3075 GET_BBLOCK (cfg, bbhash, bblock, ip);
3076 ebblock->next_bb = bblock;
3077 link_bblock (cfg, ebblock, bblock);
3079 if (!MONO_TYPE_IS_VOID (fsig->ret))
3082 /* indicates start of a new block, and triggers a load of all
3083 stack arguments at bb boundarie */
3086 inline_costs += costs;
3091 inline_costs += 10 * num_calls++;
3093 /* tail recursion elimination */
3094 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
3095 gboolean has_vtargs = FALSE;
3098 /* keep it simple */
3099 for (i = fsig->param_count - 1; i >= 0; i--) {
3100 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3105 for (i = 0; i < n; ++i) {
3106 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3108 MONO_ADD_INS (bblock, ins);
3110 MONO_INST_NEW (cfg, ins, CEE_BR);
3112 MONO_ADD_INS (bblock, ins);
3113 tblock = start_bblock->out_bb [0];
3114 link_bblock (cfg, bblock, tblock);
3115 ins->inst_target_bb = tblock;
3116 start_new_bblock = 1;
3119 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3120 /* just create a dummy - the value is never used */
3121 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3122 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3130 if (*ip == CEE_CALLI) {
3132 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3133 NEW_TEMPLOAD (cfg, *sp, temp);
3137 } else if (array_rank) {
3140 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3141 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3142 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3144 if (ins->opcode == CEE_STOBJ) {
3145 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3147 MONO_ADD_INS (bblock, ins);
3150 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3151 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3152 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3156 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3157 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3160 g_assert_not_reached ();
3164 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3165 /* no need to spill */
3166 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3169 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3170 NEW_TEMPLOAD (cfg, *sp, temp);
3180 if (cfg->method != method) {
3181 /* return from inlined methode */
3186 //g_assert (returnvar != -1);
3187 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3188 store->cil_code = sp [0]->cil_code;
3189 if (store->opcode == CEE_STOBJ) {
3190 g_assert_not_reached ();
3191 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3192 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3194 MONO_ADD_INS (bblock, store);
3198 g_assert (!return_var);
3201 MONO_INST_NEW (cfg, ins, CEE_NOP);
3202 ins->opcode = mono_type_to_stind (method->signature->ret);
3203 if (ins->opcode == CEE_STOBJ) {
3204 NEW_RETLOADA (cfg, ins);
3205 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3207 ins->opcode = OP_SETRET;
3209 ins->inst_i0 = *sp;;
3210 ins->inst_i1 = NULL;
3211 MONO_ADD_INS (bblock, ins);
3215 if (sp != stack_start)
3217 MONO_INST_NEW (cfg, ins, CEE_BR);
3218 ins->cil_code = ip++;
3219 ins->inst_target_bb = end_bblock;
3220 MONO_ADD_INS (bblock, ins);
3221 link_bblock (cfg, bblock, end_bblock);
3222 start_new_bblock = 1;
3225 MONO_INST_NEW (cfg, ins, CEE_BR);
3226 ins->cil_code = ip++;
3227 MONO_ADD_INS (bblock, ins);
3228 target = ip + 1 + (signed char)(*ip);
3230 GET_BBLOCK (cfg, bbhash, tblock, target);
3231 link_bblock (cfg, bblock, tblock);
3232 CHECK_BBLOCK (target, ip, tblock);
3233 ins->inst_target_bb = tblock;
3234 if (sp != stack_start) {
3235 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3238 start_new_bblock = 1;
3244 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3245 ins->cil_code = ip++;
3246 target = ip + 1 + *(signed char*)ip;
3248 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3249 if (sp != stack_start) {
3250 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3266 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3267 ins->cil_code = ip++;
3268 target = ip + 1 + *(signed char*)ip;
3271 if (sp != stack_start) {
3272 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3278 MONO_INST_NEW (cfg, ins, CEE_BR);
3279 ins->cil_code = ip++;
3280 MONO_ADD_INS (bblock, ins);
3281 target = ip + 4 + (gint32)read32(ip);
3283 GET_BBLOCK (cfg, bbhash, tblock, target);
3284 link_bblock (cfg, bblock, tblock);
3285 CHECK_BBLOCK (target, ip, tblock);
3286 ins->inst_target_bb = tblock;
3287 if (sp != stack_start) {
3288 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3291 start_new_bblock = 1;
3297 MONO_INST_NEW (cfg, ins, *ip);
3298 ins->cil_code = ip++;
3299 target = ip + 4 + (gint32)read32(ip);
3301 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3302 if (sp != stack_start) {
3303 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3319 MONO_INST_NEW (cfg, ins, *ip);
3320 ins->cil_code = ip++;
3321 target = ip + 4 + (gint32)read32(ip);
3324 if (sp != stack_start) {
3325 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3332 n = read32 (ip + 1);
3333 MONO_INST_NEW (cfg, ins, *ip);
3335 ins->inst_left = *sp;
3336 if (ins->inst_left->type != STACK_I4) goto unverified;
3339 target = ip + n * sizeof (guint32);
3340 MONO_ADD_INS (bblock, ins);
3341 GET_BBLOCK (cfg, bbhash, tblock, target);
3342 link_bblock (cfg, bblock, tblock);
3343 ins->klass = GUINT_TO_POINTER (n);
3344 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3345 ins->inst_many_bb [n] = tblock;
3347 for (i = 0; i < n; ++i) {
3348 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3349 link_bblock (cfg, bblock, tblock);
3350 ins->inst_many_bb [i] = tblock;
3353 /* FIXME: handle stack args */
3368 MONO_INST_NEW (cfg, ins, *ip);
3373 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3374 ins->flags |= ins_flag;
3386 MONO_INST_NEW (cfg, ins, *ip);
3387 ins->cil_code = ip++;
3389 handle_loaded_temps (cfg, bblock, stack_start, sp);
3390 MONO_ADD_INS (bblock, ins);
3391 ins->inst_i0 = sp [0];
3392 ins->inst_i1 = sp [1];
3393 ins->flags |= ins_flag;
3424 case CEE_CONV_OVF_I8:
3425 case CEE_CONV_OVF_U8:
3431 case CEE_CONV_OVF_I4:
3432 case CEE_CONV_OVF_I1:
3433 case CEE_CONV_OVF_I2:
3434 case CEE_CONV_OVF_I:
3435 case CEE_CONV_OVF_U:
3438 if (sp [-1]->type == STACK_R8) {
3439 ADD_UNOP (CEE_CONV_OVF_I8);
3447 case CEE_CONV_OVF_U1:
3448 case CEE_CONV_OVF_U2:
3449 case CEE_CONV_OVF_U4:
3452 if (sp [-1]->type == STACK_R8) {
3453 ADD_UNOP (CEE_CONV_OVF_U8);
3461 case CEE_CONV_OVF_I1_UN:
3462 case CEE_CONV_OVF_I2_UN:
3463 case CEE_CONV_OVF_I4_UN:
3464 case CEE_CONV_OVF_I8_UN:
3465 case CEE_CONV_OVF_U1_UN:
3466 case CEE_CONV_OVF_U2_UN:
3467 case CEE_CONV_OVF_U4_UN:
3468 case CEE_CONV_OVF_U8_UN:
3469 case CEE_CONV_OVF_I_UN:
3470 case CEE_CONV_OVF_U_UN:
3477 token = read32 (ip + 1);
3478 if (method->wrapper_type != MONO_WRAPPER_NONE)
3479 klass = mono_method_get_wrapper_data (method, token);
3481 klass = mono_class_get (image, token);
3483 mono_class_init (klass);
3484 if (klass->byval_arg.type == MONO_TYPE_VAR)
3485 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3487 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3488 MonoInst *store, *load;
3489 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3490 load->cil_code = ip;
3491 load->inst_i0 = sp [1];
3492 load->type = ldind_type [CEE_LDIND_REF];
3493 load->flags |= ins_flag;
3494 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3495 store->cil_code = ip;
3496 handle_loaded_temps (cfg, bblock, stack_start, sp);
3497 MONO_ADD_INS (bblock, store);
3498 store->inst_i0 = sp [0];
3499 store->inst_i1 = load;
3500 store->flags |= ins_flag;
3502 n = mono_class_value_size (klass, NULL);
3503 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3505 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3506 copy->inst_left = sp [0];
3507 copy->inst_right = sp [1];
3508 copy->cil_code = ip;
3510 MONO_ADD_INS (bblock, copy);
3512 MonoInst *iargs [3];
3515 NEW_ICONST (cfg, iargs [2], n);
3516 iargs [2]->cil_code = ip;
3518 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3525 MonoInst *iargs [3];
3528 token = read32 (ip + 1);
3529 if (method->wrapper_type != MONO_WRAPPER_NONE)
3530 klass = mono_method_get_wrapper_data (method, token);
3532 klass = mono_class_get (image, token);
3534 mono_class_init (klass);
3535 if (klass->byval_arg.type == MONO_TYPE_VAR)
3536 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3537 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3538 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3540 ins->inst_i0 = sp [0];
3541 ins->type = ldind_type [CEE_LDIND_REF];
3542 ins->flags |= ins_flag;
3548 n = mono_class_value_size (klass, NULL);
3549 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3550 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3551 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3553 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3554 copy->inst_left = iargs [0];
3555 copy->inst_right = *sp;
3556 copy->cil_code = ip;
3558 MONO_ADD_INS (bblock, copy);
3561 NEW_ICONST (cfg, iargs [2], n);
3562 iargs [2]->cil_code = ip;
3564 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3566 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3574 CHECK_STACK_OVF (1);
3575 n = read32 (ip + 1);
3577 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3579 MonoInst *iargs [1];
3581 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3582 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3583 NEW_TEMPLOAD (cfg, *sp, temp);
3587 if (cfg->opt & MONO_OPT_SHARED) {
3589 MonoInst *iargs [3];
3591 if (mono_compile_aot) {
3592 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3595 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3596 NEW_IMAGECONST (cfg, iargs [1], image);
3597 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3598 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3599 NEW_TEMPLOAD (cfg, *sp, temp);
3600 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3602 if (mono_compile_aot)
3603 NEW_LDSTRCONST (cfg, ins, n);
3605 NEW_PCONST (cfg, ins, NULL);
3607 ins->type = STACK_OBJ;
3608 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3618 MonoInst *iargs [2];
3619 MonoMethodSignature *fsig;
3622 token = read32 (ip + 1);
3623 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3624 cmethod = mono_method_get_wrapper_data (method, token);
3626 cmethod = mono_get_method (image, token, NULL);
3627 fsig = mono_method_get_signature (cmethod, image, token);
3629 mono_class_init (cmethod->klass);
3631 n = fsig->param_count;
3634 /* move the args to allow room for 'this' in the first position */
3640 handle_loaded_temps (cfg, bblock, stack_start, sp);
3643 if (cmethod->klass->parent == mono_defaults.array_class) {
3644 NEW_METHODCONST (cfg, *sp, cmethod);
3645 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3647 } else if (cmethod->string_ctor) {
3648 /* we simply pass a null pointer */
3649 NEW_PCONST (cfg, *sp, NULL);
3650 /* now call the string ctor */
3651 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3653 if (cmethod->klass->valuetype) {
3654 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3655 temp = iargs [0]->inst_c0;
3656 NEW_TEMPLOADA (cfg, *sp, temp);
3658 if (cfg->opt & MONO_OPT_SHARED) {
3659 NEW_DOMAINCONST (cfg, iargs [0]);
3660 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3662 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3664 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3665 NEW_VTABLECONST (cfg, iargs [0], vtable);
3666 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3667 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3669 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3671 NEW_TEMPLOAD (cfg, *sp, temp);
3674 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3675 mono_method_check_inlining (cfg, cmethod) &&
3676 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3677 !g_list_find (dont_inline, cmethod)) {
3679 MonoBasicBlock *ebblock;
3680 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3685 GET_BBLOCK (cfg, bbhash, bblock, ip);
3686 ebblock->next_bb = bblock;
3687 link_bblock (cfg, ebblock, bblock);
3689 NEW_TEMPLOAD (cfg, *sp, temp);
3692 /* indicates start of a new block, and triggers a load
3693 of all stack arguments at bb boundarie */
3696 inline_costs += costs;
3700 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3703 /* now call the actual ctor */
3704 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3708 NEW_TEMPLOAD (cfg, *sp, temp);
3717 MONO_INST_NEW (cfg, ins, *ip);
3719 klass = mono_class_get (image, read32 (ip + 1));
3720 mono_class_init (klass);
3721 ins->type = STACK_OBJ;
3722 ins->inst_left = *sp;
3723 ins->inst_newa_class = klass;
3728 case CEE_UNBOX_ANY: {
3729 MonoInst *add, *vtoffset;
3730 MonoInst *iargs [3];
3734 token = read32 (ip + 1);
3735 if (method->wrapper_type != MONO_WRAPPER_NONE)
3736 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3738 klass = mono_class_get (image, token);
3739 mono_class_init (klass);
3741 if (klass->byval_arg.type == MONO_TYPE_VAR)
3742 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3744 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3746 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
3747 ins->type = STACK_OBJ;
3748 ins->inst_left = *sp;
3750 ins->inst_newa_class = klass;
3757 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3758 ins->type = STACK_OBJ;
3759 ins->inst_left = *sp;
3761 ins->inst_newa_class = klass;
3764 MONO_INST_NEW (cfg, add, CEE_ADD);
3765 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3766 add->inst_left = ins;
3767 add->inst_right = vtoffset;
3768 add->type = STACK_MP;
3772 n = mono_class_value_size (klass, NULL);
3773 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3774 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3775 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3777 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3778 copy->inst_left = iargs [0];
3779 copy->inst_right = *sp;
3780 copy->cil_code = ip;
3782 MONO_ADD_INS (bblock, copy);
3785 NEW_ICONST (cfg, iargs [2], n);
3786 iargs [2]->cil_code = ip;
3788 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3790 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3796 MonoInst *add, *vtoffset;
3800 token = read32 (ip + 1);
3801 if (method->wrapper_type != MONO_WRAPPER_NONE)
3802 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3804 klass = mono_class_get (image, token);
3805 mono_class_init (klass);
3807 if (klass->byval_arg.type == MONO_TYPE_VAR)
3808 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3810 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3811 ins->type = STACK_OBJ;
3812 ins->inst_left = *sp;
3814 ins->inst_newa_class = klass;
3817 MONO_INST_NEW (cfg, add, CEE_ADD);
3818 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3819 add->inst_left = ins;
3820 add->inst_right = vtoffset;
3821 add->type = STACK_MP;
3829 MONO_INST_NEW (cfg, ins, *ip);
3831 klass = mono_class_get (image, read32 (ip + 1));
3832 mono_class_init (klass);
3833 if (klass->byval_arg.type == MONO_TYPE_VAR)
3834 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3835 ins->type = STACK_OBJ;
3836 ins->inst_left = *sp;
3838 ins->inst_newa_class = klass;
3845 MONO_INST_NEW (cfg, ins, *ip);
3847 ins->inst_left = *sp;
3848 ins->cil_code = ip++;
3849 MONO_ADD_INS (bblock, ins);
3851 start_new_bblock = 1;
3856 MonoInst *offset_ins;
3857 MonoClassField *field;
3858 MonoBasicBlock *ebblock;
3862 if (*ip == CEE_STFLD) {
3869 // FIXME: enable this test later.
3870 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3872 token = read32 (ip + 1);
3873 field = mono_field_from_token (image, token, &klass);
3874 if (field->parent->gen_params)
3875 field = get_generic_field_inst (field, method->klass, &klass);
3876 mono_class_init (klass);
3878 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3879 /* FIXME: mark instructions for use in SSA */
3880 if (*ip == CEE_STFLD) {
3881 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3882 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3883 MonoInst *iargs [5];
3886 NEW_CLASSCONST (cfg, iargs [1], klass);
3887 NEW_FIELDCONST (cfg, iargs [2], field);
3888 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3892 if (cfg->opt & MONO_OPT_INLINE) {
3893 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3894 iargs, ip, real_offset, dont_inline, &ebblock);
3895 g_assert (costs > 0);
3900 GET_BBLOCK (cfg, bbhash, bblock, ip);
3901 ebblock->next_bb = bblock;
3902 link_bblock (cfg, ebblock, bblock);
3904 /* indicates start of a new block, and triggers a load
3905 of all stack arguments at bb boundarie */
3908 inline_costs += costs;
3911 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3915 NEW_ICONST (cfg, offset_ins, foffset);
3916 MONO_INST_NEW (cfg, ins, CEE_ADD);
3918 ins->inst_left = *sp;
3919 ins->inst_right = offset_ins;
3920 ins->type = STACK_MP;
3922 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3923 store->cil_code = ip;
3924 store->inst_left = ins;
3925 store->inst_right = sp [1];
3926 handle_loaded_temps (cfg, bblock, stack_start, sp);
3927 store->flags |= ins_flag;
3929 if (store->opcode == CEE_STOBJ) {
3930 handle_stobj (cfg, bblock, ins, sp [1], ip,
3931 mono_class_from_mono_type (field->type), FALSE, FALSE);
3933 MONO_ADD_INS (bblock, store);
3936 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3937 /* fixme: we need to inline that call somehow */
3938 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3939 MonoInst *iargs [4];
3943 NEW_CLASSCONST (cfg, iargs [1], klass);
3944 NEW_FIELDCONST (cfg, iargs [2], field);
3945 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3946 if (cfg->opt & MONO_OPT_INLINE) {
3947 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3948 iargs, ip, real_offset, dont_inline, &ebblock);
3949 g_assert (costs > 0);
3954 GET_BBLOCK (cfg, bbhash, bblock, ip);
3955 ebblock->next_bb = bblock;
3956 link_bblock (cfg, ebblock, bblock);
3958 temp = iargs [0]->inst_i0->inst_c0;
3960 if (*ip == CEE_LDFLDA) {
3961 /* not sure howto handle this */
3962 NEW_TEMPLOADA (cfg, *sp, temp);
3964 NEW_TEMPLOAD (cfg, *sp, temp);
3968 /* indicates start of a new block, and triggers a load of
3969 all stack arguments at bb boundarie */
3972 inline_costs += costs;
3975 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3976 if (*ip == CEE_LDFLDA) {
3977 /* not sure howto handle this */
3978 NEW_TEMPLOADA (cfg, *sp, temp);
3980 NEW_TEMPLOAD (cfg, *sp, temp);
3985 NEW_ICONST (cfg, offset_ins, foffset);
3986 MONO_INST_NEW (cfg, ins, CEE_ADD);
3988 ins->inst_left = *sp;
3989 ins->inst_right = offset_ins;
3990 ins->type = STACK_MP;
3992 if (*ip == CEE_LDFLDA) {
3996 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3997 type_to_eval_stack_type (field->type, load);
3998 load->cil_code = ip;
3999 load->inst_left = ins;
4000 load->flags |= ins_flag;
4012 MonoClassField *field;
4013 gpointer addr = NULL;
4015 token = read32 (ip + 1);
4017 field = mono_field_from_token (image, token, &klass);
4018 mono_class_init (klass);
4020 handle_loaded_temps (cfg, bblock, stack_start, sp);
4022 if (cfg->domain->thread_static_fields)
4023 addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field);
4025 if ((cfg->opt & MONO_OPT_SHARED) || (mono_compile_aot && addr)) {
4027 MonoInst *iargs [2];
4028 g_assert (field->parent);
4029 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
4030 NEW_FIELDCONST (cfg, iargs [1], field);
4031 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
4032 NEW_TEMPLOAD (cfg, ins, temp);
4035 vtable = mono_class_vtable (cfg->domain, klass);
4037 if ((!vtable->initialized || mono_compile_aot) && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && mono_class_needs_cctor_run (klass, method)) {
4038 MonoInst *iargs [1];
4039 NEW_VTABLECONST (cfg, iargs [0], vtable);
4040 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
4041 if (cfg->verbose_level > 2)
4042 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4044 mono_runtime_class_init (vtable);
4046 addr = (char*)vtable->data + field->offset;
4048 if (mono_compile_aot)
4049 NEW_SFLDACONST (cfg, ins, field);
4051 NEW_PCONST (cfg, ins, addr);
4055 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4056 * This could be later optimized to do just a couple of
4057 * memory dereferences with constant offsets.
4060 MonoInst *iargs [1];
4061 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4062 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
4063 NEW_TEMPLOAD (cfg, ins, temp);
4067 /* FIXME: mark instructions for use in SSA */
4068 if (*ip == CEE_LDSFLDA) {
4070 } else if (*ip == CEE_STSFLD) {
4074 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4075 store->cil_code = ip;
4076 store->inst_left = ins;
4077 store->inst_right = sp [0];
4078 store->flags |= ins_flag;
4081 if (store->opcode == CEE_STOBJ) {
4082 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4084 MONO_ADD_INS (bblock, store);
4086 gboolean is_const = FALSE;
4087 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4088 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4089 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4090 gpointer addr = (char*)vtable->data + field->offset;
4091 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4093 switch (field->type->type) {
4094 case MONO_TYPE_BOOLEAN:
4096 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4100 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4103 case MONO_TYPE_CHAR:
4105 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4109 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4114 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4118 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4123 case MONO_TYPE_STRING:
4124 case MONO_TYPE_OBJECT:
4125 case MONO_TYPE_CLASS:
4126 case MONO_TYPE_SZARRAY:
4128 case MONO_TYPE_FNPTR:
4129 case MONO_TYPE_ARRAY:
4130 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4131 type_to_eval_stack_type (field->type, *sp);
4136 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4137 sp [0]->type = STACK_I8;
4138 sp [0]->inst_l = *((gint64 *)addr);
4143 case MONO_TYPE_VALUETYPE:
4152 CHECK_STACK_OVF (1);
4153 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4154 type_to_eval_stack_type (field->type, load);
4155 load->cil_code = ip;
4156 load->inst_left = ins;
4158 load->flags |= ins_flag;
4160 /* fixme: dont see the problem why this does not work */
4161 //cfg->disable_aot = TRUE;
4170 token = read32 (ip + 1);
4171 if (method->wrapper_type != MONO_WRAPPER_NONE)
4172 klass = mono_method_get_wrapper_data (method, token);
4174 klass = mono_class_get (image, token);
4175 mono_class_init (klass);
4176 if (klass->byval_arg.type == MONO_TYPE_VAR)
4177 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4178 n = mono_type_to_stind (&klass->byval_arg);
4179 if (n == CEE_STOBJ) {
4180 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4182 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4184 MONO_INST_NEW (cfg, store, n);
4185 store->cil_code = ip;
4186 store->inst_left = sp [0];
4187 store->inst_right = sp [1];
4188 store->flags |= ins_flag;
4189 MONO_ADD_INS (bblock, store);
4196 MonoInst *iargs [2];
4197 MonoInst *load, *vtoffset, *add, *val, *vstore;
4202 token = read32 (ip + 1);
4203 if (method->wrapper_type != MONO_WRAPPER_NONE)
4204 klass = mono_method_get_wrapper_data (method, token);
4206 klass = mono_class_get (image, token);
4207 mono_class_init (klass);
4208 if (klass->byval_arg.type == MONO_TYPE_VAR)
4209 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4211 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4216 /* much like NEWOBJ */
4217 if (cfg->opt & MONO_OPT_SHARED) {
4218 NEW_DOMAINCONST (cfg, iargs [0]);
4219 NEW_CLASSCONST (cfg, iargs [1], klass);
4221 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4223 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4224 NEW_VTABLECONST (cfg, iargs [0], vtable);
4225 if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4226 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4228 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4230 NEW_TEMPLOAD (cfg, load, temp);
4231 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4232 MONO_INST_NEW (cfg, add, CEE_ADD);
4233 add->inst_left = load;
4234 add->inst_right = vtoffset;
4237 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4238 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4239 vstore->cil_code = ip;
4240 vstore->inst_left = add;
4241 vstore->inst_right = val;
4243 if (vstore->opcode == CEE_STOBJ) {
4244 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4246 MONO_ADD_INS (bblock, vstore);
4248 NEW_TEMPLOAD (cfg, load, temp);
4256 MONO_INST_NEW (cfg, ins, *ip);
4260 token = read32 (ip + 1);
4262 /* allocate the domainvar - becaus this is used in decompose_foreach */
4263 if (cfg->opt & MONO_OPT_SHARED)
4264 mono_get_domainvar (cfg);
4266 if (method->wrapper_type != MONO_WRAPPER_NONE)
4267 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4269 klass = mono_class_get (image, token);
4271 mono_class_init (klass);
4272 if (klass->byval_arg.type == MONO_TYPE_VAR)
4273 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4274 ins->inst_newa_class = klass;
4275 ins->inst_newa_len = *sp;
4276 ins->type = STACK_OBJ;
4283 MONO_INST_NEW (cfg, ins, *ip);
4284 ins->cil_code = ip++;
4286 ins->inst_left = *sp;
4287 ins->type = STACK_PTR;
4293 klass = mono_class_get (image, read32 (ip + 1));
4294 mono_class_init (klass);
4295 if (klass->byval_arg.type == MONO_TYPE_VAR)
4296 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4297 NEW_LDELEMA (cfg, ins, sp, klass);
4306 token = read32 (ip + 1);
4307 klass = mono_class_get (image, token);
4308 mono_class_init (klass);
4309 if (klass->byval_arg.type == MONO_TYPE_VAR)
4310 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4311 NEW_LDELEMA (cfg, load, sp, klass);
4312 load->cil_code = ip;
4313 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4315 ins->inst_left = load;
4317 type_to_eval_stack_type (&klass->byval_arg, ins);
4331 case CEE_LDELEM_REF: {
4335 * ldind.x (ldelema (array, index))
4336 * ldelema does the bounds check
4340 klass = array_access_to_klass (*ip);
4341 NEW_LDELEMA (cfg, load, sp, klass);
4342 load->cil_code = ip;
4343 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4345 ins->inst_left = load;
4347 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4357 case CEE_STELEM_R8: {
4361 * stind.x (ldelema (array, index), val)
4362 * ldelema does the bounds check
4366 klass = array_access_to_klass (*ip);
4367 NEW_LDELEMA (cfg, load, sp, klass);
4368 load->cil_code = ip;
4369 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4371 ins->inst_left = load;
4372 ins->inst_right = sp [2];
4374 handle_loaded_temps (cfg, bblock, stack_start, sp);
4375 MONO_ADD_INS (bblock, ins);
4377 cfg->disable_ssa = TRUE;
4384 * stind.x (ldelema (array, index), val)
4385 * ldelema does the bounds check
4389 token = read32 (ip + 1);
4390 klass = mono_class_get (image, token);
4391 mono_class_init (klass);
4392 if (klass->byval_arg.type == MONO_TYPE_VAR)
4393 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4394 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4395 MonoInst *iargs [3];
4396 handle_loaded_temps (cfg, bblock, stack_start, sp);
4402 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4404 NEW_LDELEMA (cfg, load, sp, klass);
4405 load->cil_code = ip;
4406 MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg));
4408 ins->inst_left = load;
4409 ins->inst_right = sp [2];
4410 handle_loaded_temps (cfg, bblock, stack_start, sp);
4411 MONO_ADD_INS (bblock, ins);
4415 cfg->disable_ssa = TRUE;
4418 case CEE_STELEM_REF: {
4419 MonoInst *iargs [3];
4424 handle_loaded_temps (cfg, bblock, stack_start, sp);
4430 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4434 NEW_GROUP (cfg, group, sp [0], sp [1]);
4435 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4437 ins->inst_left = group;
4438 ins->inst_right = sp [2];
4439 MONO_ADD_INS (bblock, ins);
4444 cfg->disable_ssa = TRUE;
4447 case CEE_CKFINITE: {
4448 MonoInst *store, *temp;
4451 /* this instr. can throw exceptions as side effect,
4452 * so we cant eliminate dead code which contains CKFINITE opdodes.
4453 * Spilling to memory makes sure that we always perform
4457 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4459 ins->inst_left = sp [-1];
4460 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4462 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4463 store->cil_code = ip;
4464 MONO_ADD_INS (bblock, store);
4466 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4473 g_error ("opcode 0x%02x not handled", *ip);
4477 MonoClass *handle_class;
4479 CHECK_STACK_OVF (1);
4481 n = read32 (ip + 1);
4483 handle = mono_ldtoken (image, n, &handle_class);
4484 mono_class_init (handle_class);
4486 if (cfg->opt & MONO_OPT_SHARED) {
4488 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4490 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4492 NEW_IMAGECONST (cfg, iargs [0], image);
4493 NEW_ICONST (cfg, iargs [1], n);
4494 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4495 NEW_TEMPLOAD (cfg, res, temp);
4496 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4497 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4498 MONO_ADD_INS (bblock, store);
4499 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4501 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4502 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4503 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4504 MonoClass *tclass = mono_class_from_mono_type (handle);
4505 mono_class_init (tclass);
4506 if (mono_compile_aot)
4507 NEW_TYPE_FROM_HANDLE_CONST (cfg, ins, n);
4509 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4510 ins->type = STACK_OBJ;
4511 ins->klass = cmethod->klass;
4514 if (mono_compile_aot)
4515 NEW_LDTOKENCONST (cfg, ins, n);
4517 NEW_PCONST (cfg, ins, handle);
4518 ins->type = STACK_VTYPE;
4519 ins->klass = handle_class;
4535 case CEE_ADD_OVF_UN:
4537 case CEE_MUL_OVF_UN:
4539 case CEE_SUB_OVF_UN:
4544 case CEE_ENDFINALLY:
4545 /* FIXME: check stack state */
4546 MONO_INST_NEW (cfg, ins, *ip);
4547 MONO_ADD_INS (bblock, ins);
4548 ins->cil_code = ip++;
4549 start_new_bblock = 1;
4554 if (*ip == CEE_LEAVE) {
4555 target = ip + 5 + (gint32)read32(ip + 1);
4557 target = ip + 2 + (signed char)(ip [1]);
4560 /* empty the stack */
4561 while (sp != stack_start) {
4562 MONO_INST_NEW (cfg, ins, CEE_POP);
4566 MONO_ADD_INS (bblock, ins);
4569 /* fixme: call fault handler ? */
4571 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4573 for (tmp = handlers; tmp; tmp = tmp->next) {
4575 link_bblock (cfg, bblock, tblock);
4576 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4578 ins->inst_target_bb = tblock;
4579 MONO_ADD_INS (bblock, ins);
4581 g_list_free (handlers);
4584 MONO_INST_NEW (cfg, ins, CEE_BR);
4586 MONO_ADD_INS (bblock, ins);
4587 GET_BBLOCK (cfg, bbhash, tblock, target);
4588 link_bblock (cfg, bblock, tblock);
4589 CHECK_BBLOCK (target, ip, tblock);
4590 ins->inst_target_bb = tblock;
4591 start_new_bblock = 1;
4593 if (*ip == CEE_LEAVE)
4602 MONO_INST_NEW (cfg, ins, *ip);
4604 handle_loaded_temps (cfg, bblock, stack_start, sp);
4605 MONO_ADD_INS (bblock, ins);
4606 ins->cil_code = ip++;
4607 ins->inst_i0 = sp [0];
4608 ins->inst_i1 = sp [1];
4616 /* trampoline mono specific opcodes */
4617 case MONO_CUSTOM_PREFIX: {
4619 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4623 case CEE_MONO_FUNC1: {
4625 gpointer func = NULL;
4630 case MONO_MARSHAL_CONV_STR_LPWSTR:
4631 func = mono_string_to_utf16;
4633 case MONO_MARSHAL_CONV_LPWSTR_STR:
4634 func = mono_string_from_utf16;
4636 case MONO_MARSHAL_CONV_LPSTR_STR:
4637 func = mono_string_new_wrapper;
4639 case MONO_MARSHAL_CONV_STR_LPTSTR:
4640 case MONO_MARSHAL_CONV_STR_LPSTR:
4641 func = mono_string_to_utf8;
4643 case MONO_MARSHAL_CONV_STR_BSTR:
4644 func = mono_string_to_bstr;
4646 case MONO_MARSHAL_CONV_STR_TBSTR:
4647 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4648 func = mono_string_to_ansibstr;
4650 case MONO_MARSHAL_CONV_SB_LPSTR:
4651 func = mono_string_builder_to_utf8;
4653 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4654 func = mono_array_to_savearray;
4656 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4657 func = mono_array_to_lparray;
4659 case MONO_MARSHAL_CONV_DEL_FTN:
4660 func = mono_delegate_to_ftnptr;
4662 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4663 func = mono_marshal_string_array;
4666 g_warning ("unknown conversion %d\n", ip [2]);
4667 g_assert_not_reached ();
4670 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4671 NEW_TEMPLOAD (cfg, *sp, temp);
4675 inline_costs += 10 * num_calls++;
4678 case CEE_MONO_PROC2: {
4679 gpointer func = NULL;
4684 case MONO_MARSHAL_CONV_LPSTR_SB:
4685 func = mono_string_utf8_to_builder;
4687 case MONO_MARSHAL_FREE_ARRAY:
4688 func = mono_marshal_free_array;
4691 g_assert_not_reached ();
4694 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4696 inline_costs += 10 * num_calls++;
4699 case CEE_MONO_PROC3: {
4700 gpointer func = NULL;
4705 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4706 func = mono_string_to_byvalstr;
4708 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4709 func = mono_string_to_byvalwstr;
4712 g_assert_not_reached ();
4715 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4717 inline_costs += 10 * num_calls++;
4723 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4725 inline_costs += 10 * num_calls++;
4727 case CEE_MONO_LDPTR:
4728 CHECK_STACK_OVF (1);
4729 token = read32 (ip + 2);
4730 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4734 inline_costs += 10 * num_calls++;
4736 case CEE_MONO_VTADDR:
4739 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4741 ins->type = STACK_MP;
4742 ins->inst_left = *sp;
4746 case CEE_MONO_NEWOBJ: {
4747 MonoInst *iargs [2];
4749 CHECK_STACK_OVF (1);
4750 token = read32 (ip + 2);
4751 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4752 mono_class_init (klass);
4753 NEW_DOMAINCONST (cfg, iargs [0]);
4754 NEW_CLASSCONST (cfg, iargs [1], klass);
4755 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4756 NEW_TEMPLOAD (cfg, *sp, temp);
4759 inline_costs += 10 * num_calls++;
4762 case CEE_MONO_OBJADDR:
4765 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4767 ins->type = STACK_MP;
4768 ins->inst_left = *sp;
4772 case CEE_MONO_LDNATIVEOBJ:
4774 token = read32 (ip + 2);
4775 klass = mono_method_get_wrapper_data (method, token);
4776 g_assert (klass->valuetype);
4777 mono_class_init (klass);
4778 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4782 case CEE_MONO_RETOBJ:
4783 g_assert (cfg->ret);
4784 g_assert (method->signature->pinvoke);
4788 token = read32 (ip + 2);
4789 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4791 NEW_RETLOADA (cfg, ins);
4792 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4794 if (sp != stack_start)
4797 MONO_INST_NEW (cfg, ins, CEE_BR);
4799 ins->inst_target_bb = end_bblock;
4800 MONO_ADD_INS (bblock, ins);
4801 link_bblock (cfg, bblock, end_bblock);
4802 start_new_bblock = 1;
4806 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4814 /* somewhat similar to LDTOKEN */
4815 MonoInst *addr, *vtvar;
4816 CHECK_STACK_OVF (1);
4817 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4819 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4820 addr->cil_code = ip;
4821 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4823 ins->inst_left = addr;
4824 MONO_ADD_INS (bblock, ins);
4825 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4838 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4839 MONO_INST_NEW (cfg, ins, cmp->opcode);
4841 cmp->inst_i0 = sp [0];
4842 cmp->inst_i1 = sp [1];
4846 cmp->opcode = OP_COMPARE;
4848 ins->type = STACK_I4;
4858 CHECK_STACK_OVF (1);
4859 n = read32 (ip + 2);
4860 if (method->wrapper_type != MONO_WRAPPER_NONE)
4861 cmethod = mono_method_get_wrapper_data (method, n);
4863 cmethod = mono_get_method (image, n, NULL);
4866 * We can't do this in mono_ldftn, since it is used in
4867 * the synchronized wrapper, leading to an infinite loop.
4869 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4870 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4873 mono_class_init (cmethod->klass);
4874 handle_loaded_temps (cfg, bblock, stack_start, sp);
4876 NEW_METHODCONST (cfg, argconst, cmethod);
4877 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4878 NEW_TEMPLOAD (cfg, *sp, temp);
4882 inline_costs += 10 * num_calls++;
4885 case CEE_LDVIRTFTN: {
4890 n = read32 (ip + 2);
4891 if (method->wrapper_type != MONO_WRAPPER_NONE)
4892 cmethod = mono_method_get_wrapper_data (method, n);
4894 cmethod = mono_get_method (image, n, NULL);
4896 mono_class_init (cmethod->klass);
4897 handle_loaded_temps (cfg, bblock, stack_start, sp);
4901 NEW_METHODCONST (cfg, args [1], cmethod);
4902 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4903 NEW_TEMPLOAD (cfg, *sp, temp);
4907 inline_costs += 10 * num_calls++;
4911 CHECK_STACK_OVF (1);
4912 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4918 CHECK_STACK_OVF (1);
4919 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4927 handle_loaded_temps (cfg, bblock, stack_start, sp);
4928 n = read16 (ip + 2);
4929 NEW_ARGSTORE (cfg, ins, n, *sp);
4931 if (ins->opcode == CEE_STOBJ) {
4932 NEW_ARGLOADA (cfg, ins, n);
4933 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4935 MONO_ADD_INS (bblock, ins);
4939 CHECK_STACK_OVF (1);
4940 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4946 CHECK_STACK_OVF (1);
4947 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4955 n = read16 (ip + 2);
4956 handle_loaded_temps (cfg, bblock, stack_start, sp);
4957 NEW_LOCSTORE (cfg, ins, n, *sp);
4959 if (ins->opcode == CEE_STOBJ) {
4960 NEW_LOCLOADA (cfg, ins, n);
4961 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4963 MONO_ADD_INS (bblock, ins);
4970 if (sp != stack_start)
4972 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4973 ins->inst_left = *sp;
4976 if (header->init_locals)
4977 ins->flags |= MONO_INST_INIT;
4981 /* FIXME: set init flag if locals init is set in this method */
4983 case CEE_ENDFILTER: {
4984 MonoExceptionClause *clause, *nearest;
4985 int cc, nearest_num;
4989 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4991 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4992 ins->inst_left = *sp;
4994 MONO_ADD_INS (bblock, ins);
4995 start_new_bblock = 1;
4999 for (cc = 0; cc < header->num_clauses; ++cc) {
5000 clause = &header->clauses [cc];
5001 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
5002 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
5008 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
5012 case CEE_UNALIGNED_:
5013 ins_flag |= MONO_INST_UNALIGNED;
5017 ins_flag |= MONO_INST_VOLATILE;
5021 ins_flag |= MONO_INST_TAILCALL;
5027 token = read32 (ip + 2);
5028 if (method->wrapper_type != MONO_WRAPPER_NONE)
5029 klass = mono_method_get_wrapper_data (method, token);
5031 klass = mono_class_get (image, token);
5032 if (klass->byval_arg.type == MONO_TYPE_VAR)
5033 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
5034 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
5035 MonoInst *store, *load;
5036 NEW_PCONST (cfg, load, NULL);
5037 load->cil_code = ip;
5038 load->type = STACK_OBJ;
5039 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
5040 store->cil_code = ip;
5041 handle_loaded_temps (cfg, bblock, stack_start, sp);
5042 MONO_ADD_INS (bblock, store);
5043 store->inst_i0 = sp [0];
5044 store->inst_i1 = load;
5047 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5052 case CEE_CONSTRAINED_:
5053 /* FIXME: implement */
5054 token = read32 (ip + 2);
5059 MonoInst *iargs [3];
5065 handle_loaded_temps (cfg, bblock, stack_start, sp);
5066 if (ip [1] == CEE_CPBLK) {
5067 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5069 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5077 ins_flag |= MONO_INST_NOTYPECHECK;
5079 ins_flag |= MONO_INST_NORANGECHECK;
5080 /* we ignore the no-nullcheck for now since we
5081 * really do it explicitly only when doing callvirt->call
5087 /* FIXME: check we are in a catch handler */
5088 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5089 load->cil_code = ip;
5090 MONO_INST_NEW (cfg, ins, CEE_THROW);
5091 ins->inst_left = load;
5093 MONO_ADD_INS (bblock, ins);
5095 start_new_bblock = 1;
5100 CHECK_STACK_OVF (1);
5101 token = read32 (ip + 2);
5102 /* FIXXME: handle generics. */
5103 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5104 MonoType *type = mono_type_create_from_typespec (image, token);
5105 token = mono_type_size (type, &align);
5106 mono_metadata_free_type (type);
5108 MonoClass *szclass = mono_class_get (image, token);
5109 mono_class_init (szclass);
5110 token = mono_class_value_size (szclass, &align);
5112 NEW_ICONST (cfg, ins, token);
5117 case CEE_REFANYTYPE:
5118 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5124 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5129 g_error ("opcode 0x%02x not handled", *ip);
5132 if (start_new_bblock != 1)
5135 bblock->cil_length = ip - bblock->cil_code;
5136 bblock->next_bb = end_bblock;
5137 link_bblock (cfg, bblock, end_bblock);
5139 if (cfg->method == method && cfg->domainvar) {
5143 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5144 call->signature = helper_sig_domain_get;
5145 call->inst.type = STACK_PTR;
5146 call->fptr = mono_domain_get;
5147 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5149 MONO_ADD_INS (init_localsbb, store);
5152 if (header->init_locals) {
5154 for (i = 0; i < header->num_locals; ++i) {
5155 int t = header->locals [i]->type;
5156 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5157 t = header->locals [i]->data.klass->enum_basetype->type;
5158 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5159 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5160 NEW_ICONST (cfg, ins, 0);
5161 NEW_LOCSTORE (cfg, store, i, ins);
5162 MONO_ADD_INS (init_localsbb, store);
5163 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5164 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5165 ins->type = STACK_I8;
5167 NEW_LOCSTORE (cfg, store, i, ins);
5168 MONO_ADD_INS (init_localsbb, store);
5169 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5170 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5171 ins->type = STACK_R8;
5172 ins->inst_p0 = (void*)&r8_0;
5173 NEW_LOCSTORE (cfg, store, i, ins);
5174 MONO_ADD_INS (init_localsbb, store);
5175 } else if (t == MONO_TYPE_VALUETYPE) {
5176 NEW_LOCLOADA (cfg, ins, i);
5177 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5180 NEW_PCONST (cfg, ins, NULL);
5181 NEW_LOCSTORE (cfg, store, i, ins);
5182 MONO_ADD_INS (init_localsbb, store);
5188 /* resolve backward branches in the middle of an existing basic block */
5189 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5191 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5192 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5193 if (tblock != start_bblock) {
5195 split_bblock (cfg, tblock, bblock);
5196 l = bblock->cil_code - header->code;
5197 bblock->cil_length = tblock->cil_length - l;
5198 tblock->cil_length = l;
5200 g_print ("recheck failed.\n");
5204 /* we compute regions here, because the length of filter clauses is not known in advance.
5205 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
5206 if (cfg->method == method) {
5208 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5209 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5210 if (cfg->verbose_level > 2)
5211 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5214 g_hash_table_destroy (bbhash);
5217 dont_inline = g_list_remove (dont_inline, method);
5218 return inline_costs;
5221 if (cfg->method != method)
5222 g_hash_table_destroy (bbhash);
5223 dont_inline = g_list_remove (dont_inline, method);
5227 if (cfg->method != method)
5228 g_hash_table_destroy (bbhash);
5229 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5230 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5231 dont_inline = g_list_remove (dont_inline, method);
5236 mono_print_tree (MonoInst *tree) {
5242 arity = mono_burg_arity [tree->opcode];
5244 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5246 switch (tree->opcode) {
5248 printf ("[%d]", tree->inst_c0);
5251 printf ("[%lld]", tree->inst_l);
5254 printf ("[%f]", *(double*)tree->inst_p0);
5257 printf ("[%f]", *(float*)tree->inst_p0);
5261 printf ("[%d]", tree->inst_c0);
5264 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5267 printf ("[%s]", mono_arch_regname (tree->dreg));
5270 printf ("[%s]", tree->inst_newa_class->name);
5271 mono_print_tree (tree->inst_newa_len);
5282 case OP_VOIDCALLVIRT: {
5283 MonoCallInst *call = (MonoCallInst*)tree;
5285 printf ("[%s]", call->method->name);
5290 printf ("[%d (", tree->inst_c0);
5291 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5294 printf ("%d", tree->inst_phi_args [i + 1]);
5306 printf ("[B%d]", tree->inst_target_bb->block_num);
5316 case OP_VOIDCALL_REG:
5317 mono_print_tree (tree->inst_left);
5329 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5330 mono_print_tree (tree->inst_left);
5334 mono_print_tree (tree->inst_left);
5336 mono_print_tree (tree->inst_right);
5346 mono_print_tree_nl (MonoInst *tree)
5348 mono_print_tree (tree);
5353 create_helper_signature (void)
5355 /* FIXME: set call conv */
5356 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5357 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5358 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5359 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5360 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5361 helper_sig_newarr->pinvoke = 1;
5363 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5364 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5365 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5366 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5367 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5368 helper_sig_newarr_specific->pinvoke = 1;
5370 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5371 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5372 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5373 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5374 helper_sig_object_new->pinvoke = 1;
5376 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5377 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5378 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5379 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5380 helper_sig_object_new_specific->pinvoke = 1;
5382 /* void* mono_method_compile (MonoMethod*) */
5383 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5384 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5385 helper_sig_compile->pinvoke = 1;
5387 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5388 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5389 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5390 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5391 helper_sig_compile_virt->pinvoke = 1;
5393 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5394 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5395 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5396 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5397 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5398 helper_sig_ldstr->pinvoke = 1;
5400 /* MonoDomain *mono_domain_get (void) */
5401 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5402 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5403 helper_sig_domain_get->pinvoke = 1;
5405 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5406 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5407 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5408 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5409 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5410 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5411 helper_sig_stelem_ref->pinvoke = 1;
5413 /* long amethod (long, long) */
5414 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5415 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5416 &mono_defaults.int64_class->byval_arg;
5417 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5418 helper_sig_long_long_long->pinvoke = 1;
5420 /* object amethod (intptr) */
5421 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5422 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5423 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5424 helper_sig_obj_ptr->pinvoke = 1;
5426 /* void amethod (intptr) */
5427 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5428 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5429 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5430 helper_sig_void_ptr->pinvoke = 1;
5432 /* void amethod (MonoObject *obj) */
5433 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5434 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5435 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5436 helper_sig_void_obj->pinvoke = 1;
5438 /* intptr amethod (void) */
5439 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5440 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5441 helper_sig_ptr_void->pinvoke = 1;
5443 /* void amethod (intptr, intptr) */
5444 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5445 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5446 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5447 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5448 helper_sig_void_ptr_ptr->pinvoke = 1;
5450 /* void amethod (intptr, intptr, intptr) */
5451 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5452 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5453 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5454 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5455 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5456 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5458 /* intptr amethod (intptr, intptr) */
5459 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5460 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5461 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5462 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5463 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5465 /* IntPtr amethod (object) */
5466 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5467 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5468 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5469 helper_sig_ptr_obj->pinvoke = 1;
5471 /* IntPtr amethod (int) */
5472 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5473 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5474 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5475 helper_sig_ptr_int->pinvoke = 1;
5477 /* long amethod (long, guint32) */
5478 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5479 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5480 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5481 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5482 helper_sig_long_long_int->pinvoke = 1;
5484 /* ulong amethod (double) */
5485 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5486 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5487 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5488 helper_sig_ulong_double->pinvoke = 1;
5490 /* long amethod (double) */
5491 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5492 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5493 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5494 helper_sig_long_double->pinvoke = 1;
5496 /* uint amethod (double) */
5497 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5498 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5499 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5500 helper_sig_uint_double->pinvoke = 1;
5502 /* int amethod (double) */
5503 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5504 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5505 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5506 helper_sig_int_double->pinvoke = 1;
5508 /* void initobj (intptr, int size) */
5509 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5510 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5511 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5512 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5513 helper_sig_initobj->pinvoke = 1;
5515 /* void memcpy (intptr, intptr, int size) */
5516 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5517 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5518 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5519 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5520 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5521 helper_sig_memcpy->pinvoke = 1;
5523 /* void memset (intptr, int val, int size) */
5524 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5525 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5526 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5527 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5528 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5529 helper_sig_memset->pinvoke = 1;
5532 static GHashTable *jit_icall_hash_name = NULL;
5533 static GHashTable *jit_icall_hash_addr = NULL;
5536 mono_find_jit_icall_by_name (const char *name)
5538 g_assert (jit_icall_hash_name);
5540 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5541 return g_hash_table_lookup (jit_icall_hash_name, name);
5545 mono_find_jit_icall_by_addr (gconstpointer addr)
5547 g_assert (jit_icall_hash_addr);
5549 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5553 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5556 MonoMethod *wrapper;
5558 if (callinfo->wrapper)
5559 return callinfo->wrapper;
5560 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5561 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5562 callinfo->wrapper = mono_jit_compile_method (wrapper);
5564 return callinfo->wrapper;
5568 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5570 MonoJitICallInfo *info;
5575 if (!jit_icall_hash_name) {
5576 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5577 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5580 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5581 g_warning ("jit icall already defined \"%s\"\n", name);
5582 g_assert_not_reached ();
5585 info = g_new (MonoJitICallInfo, 1);
5592 #ifdef MONO_USE_EXC_TABLES
5593 || mono_arch_has_unwind_info (func)
5596 info->wrapper = func;
5598 info->wrapper = NULL;
5599 mono_icall_get_wrapper (info);
5602 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5603 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5604 if (func != info->wrapper)
5605 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5610 static GHashTable *emul_opcode_hash = NULL;
5612 static MonoJitICallInfo *
5613 mono_find_jit_opcode_emulation (int opcode)
5615 if (emul_opcode_hash)
5616 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5622 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
5624 MonoJitICallInfo *info;
5626 if (!emul_opcode_hash)
5627 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5629 g_assert (!sig->hasthis);
5630 g_assert (sig->param_count < 3);
5632 info = mono_register_jit_icall (func, name, sig, no_throw);
5634 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5638 decompose_foreach (MonoInst *tree, gpointer data)
5640 static MonoJitICallInfo *newarr_info = NULL;
5641 static MonoJitICallInfo *newarr_specific_info = NULL;
5642 MonoJitICallInfo *info;
5644 switch (tree->opcode) {
5646 MonoCompile *cfg = data;
5647 MonoInst *iargs [3];
5650 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5651 g_assert (newarr_info);
5652 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5653 g_assert (newarr_specific_info);
5656 if (cfg->opt & MONO_OPT_SHARED) {
5657 NEW_DOMAINCONST (cfg, iargs [0]);
5658 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5659 iargs [2] = tree->inst_newa_len;
5664 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
5666 NEW_VTABLECONST (cfg, iargs [0], vtable);
5667 iargs [1] = tree->inst_newa_len;
5669 info = newarr_specific_info;
5672 mono_emulate_opcode (cfg, tree, iargs, info);
5682 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5684 switch (mono_burg_arity [tree->opcode]) {
5687 mono_inst_foreach (tree->inst_left, func, data);
5690 mono_inst_foreach (tree->inst_left, func, data);
5691 mono_inst_foreach (tree->inst_right, func, data);
5694 g_assert_not_reached ();
5701 mono_print_bb_code (MonoBasicBlock *bb) {
5703 MonoInst *c = bb->code;
5705 mono_print_tree (c);
5714 print_dfn (MonoCompile *cfg) {
5719 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5721 for (i = 0; i < cfg->num_bblocks; ++i) {
5722 bb = cfg->bblocks [i];
5724 char* code1, *code2;
5725 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5726 if (bb->last_ins->cil_code)
5727 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5729 code2 = g_strdup ("");
5731 code1 [strlen (code1) - 1] = 0;
5732 code = g_strdup_printf ("%s -> %s", code1, code2);
5736 code = g_strdup ("\n");
5737 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5739 MonoInst *c = bb->code;
5741 mono_print_tree (c);
5749 g_print ("\tprev:");
5750 for (j = 0; j < bb->in_count; ++j) {
5751 g_print (" BB%d", bb->in_bb [j]->block_num);
5753 g_print ("\t\tsucc:");
5754 for (j = 0; j < bb->out_count; ++j) {
5755 g_print (" BB%d", bb->out_bb [j]->block_num);
5757 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5760 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5763 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5765 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5773 * returns the offset used by spillvar. It allocates a new
5774 * spill variable if necessary.
5777 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5779 MonoSpillInfo **si, *info;
5782 si = &cfg->spill_info;
5784 while (i <= spillvar) {
5787 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5789 cfg->stack_offset -= sizeof (gpointer);
5790 info->offset = cfg->stack_offset;
5794 return (*si)->offset;
5800 g_assert_not_reached ();
5805 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5809 g_assert (bb->code);
5810 bb->last_ins->next = inst;
5811 bb->last_ins = inst;
5813 bb->last_ins = bb->code = inst;
5818 mono_destroy_compile (MonoCompile *cfg)
5820 //mono_mempool_stats (cfg->mempool);
5821 g_hash_table_destroy (cfg->bb_hash);
5823 mono_regstate_free (cfg->rs);
5824 mono_mempool_destroy (cfg->mempool);
5825 g_list_free (cfg->ldstr_list);
5827 g_free (cfg->varinfo);
5833 mono_get_lmf_addr (void)
5835 MonoJitTlsData *jit_tls;
5837 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5838 return &jit_tls->lmf;
5840 g_assert_not_reached ();
5845 * mono_thread_abort:
5846 * @obj: exception object
5848 * abort the thread, print exception information and stack trace
5851 mono_thread_abort (MonoObject *obj)
5853 /* MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id); */
5855 /* handle_remove should be eventually called for this thread, too
5862 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
5864 MonoJitTlsData *jit_tls;
5867 jit_tls = g_new0 (MonoJitTlsData, 1);
5869 TlsSetValue (mono_jit_tls_id, jit_tls);
5871 jit_tls->abort_func = abort_func;
5872 jit_tls->end_of_stack = stack_start;
5874 lmf = g_new0 (MonoLMF, 1);
5877 jit_tls->lmf = jit_tls->first_lmf = lmf;
5882 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5885 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
5886 thread = mono_thread_current ();
5888 thread->jit_data = jit_tls;
5891 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5894 mono_thread_abort_dummy (MonoObject *obj)
5896 if (mono_thread_attach_aborted_cb)
5897 mono_thread_attach_aborted_cb (obj);
5899 mono_thread_abort (obj);
5903 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5906 void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
5907 thread = mono_thread_current ();
5909 thread->jit_data = jit_tls;
5913 mini_thread_cleanup (MonoThread *thread)
5915 MonoJitTlsData *jit_tls = thread->jit_data;
5918 g_free (jit_tls->first_lmf);
5920 thread->jit_data = NULL;
5925 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5927 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5931 ji->data.target = target;
5932 ji->next = cfg->patch_info;
5934 cfg->patch_info = ji;
5938 mono_remove_patch_info (MonoCompile *cfg, int ip)
5940 MonoJumpInfo **ji = &cfg->patch_info;
5943 if ((*ji)->ip.i == ip)
5946 ji = &((*ji)->next);
5951 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5952 MonoJitICallInfo *info;
5954 switch (mono_burg_arity [tree->opcode]) {
5957 dec_foreach (tree->inst_left, cfg);
5959 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5960 MonoInst *iargs [2];
5962 iargs [0] = tree->inst_left;
5964 mono_emulate_opcode (cfg, tree, iargs, info);
5970 if (tree->opcode == OP_LMUL
5971 && (cfg->opt & MONO_OPT_INTRINS)
5972 && (tree->inst_left->opcode == CEE_CONV_I8
5973 || tree->inst_left->opcode == CEE_CONV_U8)
5974 && tree->inst_left->inst_left->type == STACK_I4
5975 && (tree->inst_right->opcode == CEE_CONV_I8
5976 || tree->inst_right->opcode == CEE_CONV_U8)
5977 && tree->inst_right->inst_left->type == STACK_I4) {
5978 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
5979 tree->inst_left = tree->inst_left->inst_left;
5980 tree->inst_right = tree->inst_right->inst_left;
5981 dec_foreach (tree, cfg);
5982 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5983 MonoInst *iargs [2];
5985 iargs [0] = tree->inst_i0;
5986 iargs [1] = tree->inst_i1;
5988 mono_emulate_opcode (cfg, tree, iargs, info);
5990 dec_foreach (iargs [0], cfg);
5991 dec_foreach (iargs [1], cfg);
5994 dec_foreach (tree->inst_left, cfg);
5995 dec_foreach (tree->inst_right, cfg);
5999 g_assert_not_reached ();
6001 decompose_foreach (tree, cfg);
6005 decompose_pass (MonoCompile *cfg) {
6008 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6011 cfg->prev_ins = NULL;
6012 for (tree = cfg->cbb->code; tree; tree = tree->next) {
6013 dec_foreach (tree, cfg);
6014 cfg->prev_ins = tree;
6020 nullify_basic_block (MonoBasicBlock *bb)
6027 bb->code = bb->last_ins = NULL;
6028 bb->cil_code = NULL;
6032 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6036 for (i = 0; i < bb->out_count; i++) {
6037 MonoBasicBlock *ob = bb->out_bb [i];
6040 if (bb->out_count > 1) {
6041 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
6045 bb->out_bb [i] = repl;
6052 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6056 for (i = 0; i < bb->in_count; i++) {
6057 MonoBasicBlock *ib = bb->in_bb [i];
6060 if (bb->in_count > 1) {
6061 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6065 bb->in_bb [i] = repl;
6072 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6076 for (i = 0; i < bb->out_count; i++) {
6077 MonoBasicBlock *ob = bb->out_bb [i];
6078 for (j = 0; j < ob->in_count; j++) {
6079 if (ob->in_bb [j] == orig) {
6080 ob->in_bb [j] = repl;
6089 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6091 bb->out_count = bbn->out_count;
6092 bb->out_bb = bbn->out_bb;
6094 replace_basic_block (bb, bbn, bb);
6098 bb->last_ins->next = bbn->code;
6099 bb->last_ins = bbn->last_ins;
6102 bb->code = bbn->code;
6103 bb->last_ins = bbn->last_ins;
6105 bb->next_bb = bbn->next_bb;
6106 nullify_basic_block (bbn);
6110 optimize_branches (MonoCompile *cfg) {
6111 int i, changed = FALSE;
6112 MonoBasicBlock *bb, *bbn;
6117 /* we skip the entry block (exit is handled specially instead ) */
6118 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6120 /* dont touch code inside exception clauses */
6121 if (bb->region != -1)
6124 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6125 if (cfg->verbose_level > 2)
6126 g_print ("nullify block triggered %d\n", bbn->block_num);
6128 bb->next_bb = bbn->next_bb;
6130 for (i = 0; i < bbn->out_count; i++)
6131 replace_in_block (bbn->out_bb [i], bbn, NULL);
6133 nullify_basic_block (bbn);
6137 if (bb->out_count == 1) {
6138 bbn = bb->out_bb [0];
6140 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6141 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6142 bb->last_ins->opcode = CEE_BR;
6143 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6145 if (cfg->verbose_level > 2)
6146 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6149 if (bb->region == bbn->region && bb->next_bb == bbn) {
6150 /* the block are in sequence anyway ... */
6152 /* branches to the following block can be removed */
6153 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6154 bb->last_ins->opcode = CEE_NOP;
6156 if (cfg->verbose_level > 2)
6157 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6160 if (bbn->in_count == 1) {
6162 if (bbn != cfg->bb_exit) {
6163 if (cfg->verbose_level > 2)
6164 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6165 merge_basic_blocks (bb, bbn);
6169 //mono_print_bb_code (bb);
6179 /* we skip the entry block (exit is handled specially instead ) */
6180 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6182 /* dont touch code inside exception clauses */
6183 if (bb->region != -1)
6186 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6187 if (cfg->verbose_level > 2) {
6188 g_print ("nullify block triggered %d\n", bbn->block_num);
6190 bb->next_bb = bbn->next_bb;
6192 for (i = 0; i < bbn->out_count; i++)
6193 replace_in_block (bbn->out_bb [i], bbn, NULL);
6195 nullify_basic_block (bbn);
6201 if (bb->out_count == 1) {
6202 bbn = bb->out_bb [0];
6204 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6205 bbn = bb->last_ins->inst_target_bb;
6206 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6207 bbn->code->inst_target_bb->region == bb->region) {
6209 if (cfg->verbose_level > 2)
6210 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
6211 bb->block_num, bbn->block_num);
6213 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
6214 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
6219 } else if (bb->out_count == 2) {
6220 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6221 bbn = bb->last_ins->inst_true_bb;
6222 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6223 bbn->code->inst_target_bb->region == bb->region) {
6224 if (cfg->verbose_level > 2)
6225 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
6226 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6229 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
6231 replace_in_block (bbn, bb, NULL);
6233 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6234 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6236 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6242 bbn = bb->last_ins->inst_false_bb;
6243 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6244 bbn->code->inst_target_bb->region == bb->region) {
6245 if (cfg->verbose_level > 2)
6246 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
6247 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6250 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
6252 replace_in_block (bbn, bb, NULL);
6254 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6255 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6257 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6270 mono_compile_create_vars (MonoCompile *cfg)
6272 MonoMethodSignature *sig;
6273 MonoMethodHeader *header;
6276 header = ((MonoMethodNormal *)cfg->method)->header;
6278 sig = cfg->method->signature;
6280 if (!MONO_TYPE_IS_VOID (sig->ret)) {
6281 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6282 cfg->ret->opcode = OP_RETARG;
6283 cfg->ret->inst_vtype = sig->ret;
6284 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
6286 if (cfg->verbose_level > 2)
6287 g_print ("creating vars\n");
6290 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
6292 for (i = 0; i < sig->param_count; ++i)
6293 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
6295 cfg->locals_start = cfg->num_varinfo;
6297 if (cfg->verbose_level > 2)
6298 g_print ("creating locals\n");
6299 for (i = 0; i < header->num_locals; ++i)
6300 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
6301 if (cfg->verbose_level > 2)
6302 g_print ("locals done\n");
6306 mono_print_code (MonoCompile *cfg)
6310 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6311 MonoInst *tree = bb->code;
6316 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
6318 for (; tree; tree = tree->next) {
6319 mono_print_tree (tree);
6324 bb->last_ins->next = NULL;
6328 extern const char * const mono_burg_rule_string [];
6331 emit_state (MonoCompile *cfg, MBState *state, int goal)
6334 int ern = mono_burg_rule (state, goal);
6335 const guint16 *nts = mono_burg_nts [ern];
6338 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
6342 // state->reg1 = state->reg2; /* chain rule */
6344 state->reg1 = mono_regstate_next_int (cfg->rs);
6345 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
6348 state->reg1 = mono_regstate_next_int (cfg->rs);
6349 state->reg2 = mono_regstate_next_int (cfg->rs);
6352 state->reg1 = mono_regstate_next_float (cfg->rs);
6359 mono_burg_kids (state, ern, kids);
6361 emit_state (cfg, kids [0], nts [0]);
6363 emit_state (cfg, kids [1], nts [1]);
6365 g_assert (!nts [3]);
6366 emit_state (cfg, kids [2], nts [2]);
6371 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6372 if ((emit = mono_burg_func [ern]))
6373 emit (state, state->tree, cfg);
6376 #define DEBUG_SELECTION
6379 mini_select_instructions (MonoCompile *cfg)
6381 static int reverse_map [] = {
6382 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6383 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6385 static int reverse_fmap [] = {
6386 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6387 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6389 static int reverse_lmap [] = {
6390 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6391 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6396 cfg->state_pool = mono_mempool_new ();
6397 cfg->rs = mono_regstate_new ();
6399 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6400 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6401 bb->next_bb != bb->last_ins->inst_false_bb) {
6403 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6404 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6405 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6406 bb->last_ins->inst_false_bb = tmp;
6408 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6409 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6410 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6411 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6412 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6413 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6416 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6417 inst->opcode = CEE_BR;
6418 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6419 mono_bblock_add_inst (bb, inst);
6424 #ifdef DEBUG_SELECTION
6425 if (cfg->verbose_level >= 4) {
6426 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6427 MonoInst *tree = bb->code;
6428 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6431 for (; tree; tree = tree->next) {
6432 mono_print_tree (tree);
6439 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6440 MonoInst *tree = bb->code, *next;
6446 bb->last_ins = NULL;
6449 mono_regstate_reset (cfg->rs);
6451 #ifdef DEBUG_SELECTION
6452 if (cfg->verbose_level >= 3)
6453 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6455 for (; tree; tree = next) {
6457 #ifdef DEBUG_SELECTION
6458 if (cfg->verbose_level >= 3) {
6459 mono_print_tree (tree);
6464 if (!(mbstate = mono_burg_label (tree, cfg))) {
6465 g_warning ("unable to label tree %p", tree);
6466 mono_print_tree (tree);
6468 g_assert_not_reached ();
6470 emit_state (cfg, mbstate, MB_NTERM_stmt);
6472 bb->max_ireg = cfg->rs->next_vireg;
6473 bb->max_freg = cfg->rs->next_vfreg;
6476 bb->last_ins->next = NULL;
6478 mono_mempool_empty (cfg->state_pool);
6480 mono_mempool_destroy (cfg->state_pool);
6484 mono_codegen (MonoCompile *cfg)
6486 MonoJumpInfo *patch_info;
6488 int i, max_epilog_size;
6491 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6492 cfg->spill_count = 0;
6493 /* we reuse dfn here */
6494 /* bb->dfn = bb_count++; */
6495 mono_arch_local_regalloc (cfg, bb);
6498 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6499 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6501 code = mono_arch_emit_prolog (cfg);
6503 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6504 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6506 cfg->code_len = code - cfg->native_code;
6507 cfg->prolog_end = cfg->code_len;
6509 mono_debug_open_method (cfg);
6511 /* emit code all basic blocks */
6512 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6513 bb->native_offset = cfg->code_len;
6514 mono_arch_output_basic_block (cfg, bb);
6516 cfg->bb_exit->native_offset = cfg->code_len;
6518 code = cfg->native_code + cfg->code_len;
6520 max_epilog_size = mono_arch_max_epilog_size (cfg);
6522 /* we always allocate code in cfg->domain->code_mp to increase locality */
6523 cfg->code_size = cfg->code_len + max_epilog_size;
6524 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6525 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6526 memcpy (code, cfg->native_code, cfg->code_len);
6527 g_free (cfg->native_code);
6528 cfg->native_code = code;
6529 code = cfg->native_code + cfg->code_len;
6531 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6533 cfg->epilog_begin = cfg->code_len;
6535 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6536 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6538 cfg->code_len = code - cfg->native_code;
6540 mono_arch_emit_epilog (cfg);
6542 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6543 switch (patch_info->type) {
6544 case MONO_PATCH_INFO_ABS: {
6545 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6547 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6548 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6549 patch_info->data.name = info->name;
6553 case MONO_PATCH_INFO_SWITCH: {
6554 gpointer *table = mono_mempool_alloc (cfg->domain->code_mp, sizeof (gpointer) * patch_info->table_size);
6555 patch_info->ip.i = patch_info->ip.label->inst_c0;
6556 for (i = 0; i < patch_info->table_size; i++) {
6557 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6559 patch_info->data.target = table;
6568 if (cfg->verbose_level > 1)
6569 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6570 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6572 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6574 mono_debug_close_method (cfg);
6578 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6583 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6584 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6586 if (cp->opcode == OP_ICONST) {
6587 if (cfg->opt & MONO_OPT_CONSPROP) {
6588 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6592 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6593 if (cfg->opt & MONO_OPT_COPYPROP) {
6594 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6600 arity = mono_burg_arity [tree->opcode];
6603 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6604 if (cfg->opt & MONO_OPT_CFOLD)
6605 mono_constant_fold_inst (tree, NULL);
6607 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6608 if (cfg->opt & MONO_OPT_CFOLD)
6609 mono_constant_fold_inst (tree, NULL);
6611 mono_constant_fold_inst (tree, NULL);
6617 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6621 switch (tree->opcode) {
6631 if (tree->ssa_op == MONO_SSA_NOP) {
6632 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6649 case OP_VOIDCALL_REG:
6650 case OP_VOIDCALLVIRT:
6652 MonoCallInst *call = (MonoCallInst *)tree;
6653 MonoMethodSignature *sig = call->signature;
6654 int i, byref = FALSE;
6656 for (i = 0; i < sig->param_count; i++) {
6657 if (sig->params [i]->byref) {
6664 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6672 arity = mono_burg_arity [tree->opcode];
6678 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6681 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6682 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6685 g_assert_not_reached ();
6690 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6692 MonoInst *tree = bb->code;
6698 for (; tree; tree = tree->next) {
6700 mono_cprop_copy_values (cfg, tree, acp);
6702 mono_cprop_invalidate_values (tree, acp, acp_size);
6704 if (tree->ssa_op == MONO_SSA_STORE &&
6705 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6706 MonoInst *i1 = tree->inst_i1;
6708 acp [tree->inst_i0->inst_c0] = NULL;
6710 for (i = 0; i < acp_size; i++) {
6711 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6712 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6717 if (i1->opcode == OP_ICONST) {
6718 acp [tree->inst_i0->inst_c0] = i1;
6719 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6721 if (i1->ssa_op == MONO_SSA_LOAD &&
6722 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6723 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6724 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6725 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6730 if (tree->opcode == CEE_BEQ) {
6731 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6732 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6733 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6735 tree->opcode = CEE_BR;
6736 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6737 tree->inst_target_bb = tree->inst_true_bb;
6739 tree->inst_target_bb = tree->inst_false_bb;
6748 mono_local_cprop (MonoCompile *cfg)
6753 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6755 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6756 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6757 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6762 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6764 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6765 guint8 *ip = (guint8 *)header->code;
6768 int dfn = 0, i, code_size_ratio;
6770 mono_jit_stats.methods_compiled++;
6771 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
6772 mono_profiler_method_jit (method);
6774 cfg = g_new0 (MonoCompile, 1);
6775 cfg->method = method;
6776 cfg->mempool = mono_mempool_new ();
6778 cfg->prof_options = mono_profiler_get_events ();
6779 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6780 cfg->domain = domain;
6781 cfg->verbose_level = mini_verbose;
6782 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6783 ((MonoMethodNormal *)method)->header->max_stack);
6785 if (cfg->verbose_level > 2)
6786 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6789 * create MonoInst* which represents arguments and local variables
6791 mono_compile_create_vars (cfg);
6793 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6794 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6795 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6796 mono_destroy_compile (cfg);
6800 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6801 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6803 if (cfg->num_varinfo > 2000) {
6805 * we disable some optimizations if there are too many variables
6806 * because JIT time may become too expensive. The actual number needs
6807 * to be tweaked and eventually the non-linear algorithms should be fixed.
6809 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
6810 cfg->disable_ssa = TRUE;
6812 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6814 /* Depth-first ordering on basic blocks */
6815 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6817 if (cfg->opt & MONO_OPT_BRANCH)
6818 optimize_branches (cfg);
6820 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6821 if (cfg->num_bblocks != dfn + 1) {
6824 cfg->num_bblocks = dfn + 1;
6826 if (!header->clauses) {
6827 /* remove unreachable code, because the code in them may be
6828 * inconsistent (access to dead variables for example) */
6829 for (bb = cfg->bb_entry; bb;) {
6830 MonoBasicBlock *bbn = bb->next_bb;
6832 if (bbn && bbn->region == -1 && !bbn->dfn) {
6833 if (cfg->verbose_level > 1)
6834 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6835 bb->next_bb = bbn->next_bb;
6836 nullify_basic_block (bbn);
6844 if (cfg->opt & MONO_OPT_LOOP) {
6845 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6846 mono_compute_natural_loops (cfg);
6850 /* after method_to_ir */
6854 //#define DEBUGSSA "logic_run"
6855 #define DEBUGSSA_CLASS "Tests"
6858 if (!header->num_clauses && !cfg->disable_ssa) {
6859 mono_local_cprop (cfg);
6860 mono_ssa_compute (cfg);
6864 /* fixme: add all optimizations which requires SSA */
6865 if (cfg->opt & (MONO_OPT_DEADCE)) {
6866 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6867 mono_local_cprop (cfg);
6868 mono_ssa_compute (cfg);
6870 if (cfg->verbose_level >= 2) {
6877 /* after SSA translation */
6881 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6882 if (cfg->comp_done & MONO_COMP_SSA) {
6883 mono_ssa_cprop (cfg);
6885 mono_local_cprop (cfg);
6889 if (cfg->comp_done & MONO_COMP_SSA) {
6890 mono_ssa_deadce (cfg);
6892 //mono_ssa_strength_reduction (cfg);
6894 mono_ssa_remove (cfg);
6896 if (cfg->opt & MONO_OPT_BRANCH)
6897 optimize_branches (cfg);
6900 /* after SSA removal */
6904 decompose_pass (cfg);
6906 /* FIXME: disabled with exception clauses: bug #42136 */
6907 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6910 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6911 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6912 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6913 mono_analyze_liveness (cfg);
6915 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6916 regs = mono_arch_get_global_int_regs (cfg);
6917 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6921 //mono_print_code (cfg);
6925 /* variables are allocated after decompose, since decompose could create temps */
6926 mono_arch_allocate_vars (cfg);
6928 if (cfg->opt & MONO_OPT_CFOLD)
6929 mono_constant_fold (cfg);
6931 mini_select_instructions (cfg);
6934 if (cfg->verbose_level >= 2) {
6935 char *id = mono_method_full_name (cfg->method, FALSE);
6936 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6940 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6942 jinfo = g_new0 (MonoJitInfo, 1);
6943 jinfo->method = method;
6944 jinfo->code_start = cfg->native_code;
6945 jinfo->code_size = cfg->code_len;
6946 jinfo->used_regs = cfg->used_int_regs;
6947 jinfo->domain_neutral = (cfg->opt & MONO_OPT_SHARED) != 0;
6949 if (header->num_clauses) {
6952 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6953 jinfo->num_clauses = header->num_clauses;
6954 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6955 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6957 for (i = 0; i < header->num_clauses; i++) {
6958 MonoExceptionClause *ec = &header->clauses [i];
6959 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6960 MonoBasicBlock *tblock;
6962 ei->flags = ec->flags;
6964 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6965 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6967 ei->data.filter = cfg->native_code + tblock->native_offset;
6969 ei->data.token = ec->token_or_filter;
6972 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6974 ei->try_start = cfg->native_code + tblock->native_offset;
6975 g_assert (tblock->native_offset);
6976 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6978 ei->try_end = cfg->native_code + tblock->native_offset;
6979 g_assert (tblock->native_offset);
6980 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6982 ei->handler_start = cfg->native_code + tblock->native_offset;
6986 cfg->jit_info = jinfo;
6988 mono_jit_info_table_add (cfg->domain, jinfo);
6990 /* collect statistics */
6991 mono_jit_stats.allocated_code_size += cfg->code_len;
6992 code_size_ratio = cfg->code_len;
6993 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6994 mono_jit_stats.biggest_method_size = code_size_ratio;
6995 mono_jit_stats.biggest_method = method;
6997 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6998 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6999 mono_jit_stats.max_code_size_ratio = code_size_ratio;
7000 mono_jit_stats.max_ratio_method = method;
7002 mono_jit_stats.native_code_size += cfg->code_len;
7004 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
7005 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
7011 mono_jit_compile_method (MonoMethod *method)
7013 /* FIXME: later copy the code from mono */
7014 MonoDomain *target_domain, *domain = mono_domain_get ();
7016 GHashTable *jit_code_hash;
7019 if (default_opt & MONO_OPT_SHARED)
7020 target_domain = mono_root_domain;
7022 target_domain = domain;
7024 jit_code_hash = target_domain->jit_code_hash;
7026 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
7027 mono_jit_stats.methods_lookups++;
7031 #ifdef MONO_USE_AOT_COMPILER
7032 if (!mono_compile_aot && !mono_no_aot) {
7033 mono_class_init (method->klass);
7034 if ((code = mono_aot_get_method (target_domain, method))) {
7035 g_hash_table_insert (jit_code_hash, method, code);
7037 /* make sure runtime_init is called */
7038 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7045 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
7046 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
7047 if (!method->info) {
7050 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
7051 mono_lookup_pinvoke_call (method);
7052 #ifdef MONO_USE_EXC_TABLES
7053 if (mono_method_blittable (method)) {
7054 method->info = method->addr;
7057 nm = mono_marshal_get_native_wrapper (method);
7058 method->info = mono_compile_method (nm);
7060 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7061 //mono_debug_add_wrapper (method, nm);
7062 #ifdef MONO_USE_EXC_TABLES
7066 return method->info;
7067 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7068 const char *name = method->name;
7071 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7072 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7073 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7074 return (gpointer)mono_delegate_ctor;
7075 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7076 nm = mono_marshal_get_delegate_invoke (method);
7077 return mono_jit_compile_method (nm);
7078 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7079 nm = mono_marshal_get_delegate_begin_invoke (method);
7080 return mono_jit_compile_method (nm);
7081 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7082 nm = mono_marshal_get_delegate_end_invoke (method);
7083 return mono_jit_compile_method (nm);
7089 cfg = mini_method_compile (method, default_opt, target_domain, 0);
7090 code = cfg->native_code;
7091 mono_destroy_compile (cfg);
7093 g_hash_table_insert (jit_code_hash, method, code);
7095 if (target_domain->jump_target_hash) {
7096 MonoJumpInfo patch_info;
7098 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7100 patch_info.next = NULL;
7101 patch_info.ip.i = 0;
7102 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7103 patch_info.data.method = method;
7104 g_hash_table_remove (target_domain->jump_target_hash, method);
7106 for (tmp = list; tmp; tmp = tmp->next)
7107 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
7108 g_slist_free (list);
7110 /* make sure runtime_init is called */
7111 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7117 * mono_jit_runtime_invoke:
7118 * @method: the method to invoke
7119 * @obj: this pointer
7120 * @params: array of parameter values.
7121 * @exc: used to catch exceptions objects
7124 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
7127 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
7128 invoke = mono_marshal_get_runtime_invoke (method);
7129 runtime_invoke = mono_jit_compile_method (invoke);
7130 return runtime_invoke (obj, params, exc);
7133 #ifdef PLATFORM_WIN32
7134 #define GET_CONTEXT \
7135 struct sigcontext *ctx = (struct sigcontext*)_dummy;
7137 #define GET_CONTEXT \
7138 void **_p = (void **)&_dummy; \
7139 struct sigcontext *ctx = (struct sigcontext *)++_p;
7143 sigfpe_signal_handler (int _dummy)
7148 exc = mono_get_exception_divide_by_zero ();
7150 mono_arch_handle_exception (ctx, exc, FALSE);
7154 sigill_signal_handler (int _dummy)
7158 exc = mono_get_exception_execution_engine ("SIGILL");
7160 mono_arch_handle_exception (ctx, exc, FALSE);
7164 sigsegv_signal_handler (int _dummy)
7169 exc = mono_get_exception_null_reference ();
7171 mono_arch_handle_exception (ctx, exc, FALSE);
7175 sigusr1_signal_handler (int _dummy)
7180 thread = mono_thread_current ();
7182 g_assert (thread->abort_exc);
7184 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
7188 sigquit_signal_handler (int _dummy)
7193 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
7195 mono_arch_handle_exception (ctx, exc, FALSE);
7200 sigint_signal_handler (int _dummy)
7205 exc = mono_get_exception_execution_engine ("Interrupted (SIGINT).");
7207 mono_arch_handle_exception (ctx, exc, FALSE);
7211 mono_runtime_install_handlers (void)
7213 #ifndef PLATFORM_WIN32
7214 struct sigaction sa;
7217 #ifdef PLATFORM_WIN32
7219 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
7220 win32_seh_set_handler(SIGILL, sigill_signal_handler);
7221 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
7222 if (getenv ("MONO_DEBUG"))
7223 win32_seh_set_handler(SIGINT, sigint_signal_handler);
7224 #else /* !PLATFORM_WIN32 */
7226 /* libpthreads has its own implementation of sigaction(),
7227 * but it seems to work well with our current exception
7228 * handlers. If not we must call syscall directly instead
7231 if (getenv ("MONO_DEBUG")) {
7233 sa.sa_handler = sigint_signal_handler;
7234 sigemptyset (&sa.sa_mask);
7236 //g_assert (syscall (SYS_sigaction, SIGINT, &sa, NULL) != -1);
7237 g_assert (sigaction (SIGINT, &sa, NULL) != -1);
7241 sa.sa_handler = sigfpe_signal_handler;
7242 sigemptyset (&sa.sa_mask);
7244 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
7245 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
7248 sa.sa_handler = sigquit_signal_handler;
7249 sigemptyset (&sa.sa_mask);
7251 g_assert (sigaction (SIGQUIT, &sa, NULL) != -1);
7254 sa.sa_handler = sigill_signal_handler;
7255 sigemptyset (&sa.sa_mask);
7257 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7258 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
7260 /* catch thread abort signal */
7261 sa.sa_handler = sigusr1_signal_handler;
7262 sigemptyset (&sa.sa_mask);
7264 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7265 if (!getenv ("MONO_VALGRIND"))
7266 /* valgrind 20030725 and earlier aborts on this call so we skip it */
7267 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
7271 sa.sa_handler = sigsegv_signal_handler;
7272 sigemptyset (&sa.sa_mask);
7274 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
7275 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
7277 #endif /* PLATFORM_WIN32 */
7280 /* mono_jit_create_remoting_trampoline:
7281 * @method: pointer to the method info
7283 * Creates a trampoline which calls the remoting functions. This
7284 * is used in the vtable of transparent proxies.
7286 * Returns: a pointer to the newly created code
7289 mono_jit_create_remoting_trampoline (MonoMethod *method)
7292 guint8 *addr = NULL;
7294 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
7295 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
7296 nm = mono_marshal_get_remoting_invoke (method);
7297 addr = mono_compile_method (nm);
7299 addr = mono_compile_method (method);
7305 mini_init (const char *filename)
7309 mono_arch_cpu_init ();
7311 g_thread_init (NULL);
7313 mono_jit_tls_id = TlsAlloc ();
7314 setup_jit_tls_data ((gpointer)-1, NULL);
7320 mono_runtime_install_handlers ();
7321 mono_threads_install_cleanup (mini_thread_cleanup);
7323 mono_install_compile_method (mono_jit_compile_method);
7324 mono_install_trampoline (mono_arch_create_jit_trampoline);
7325 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
7326 mono_install_runtime_invoke (mono_jit_runtime_invoke);
7327 mono_install_handler (mono_arch_get_throw_exception ());
7328 mono_install_stack_walk (mono_jit_walk_stack);
7329 mono_install_get_config_dir ();
7331 domain = mono_init (filename);
7334 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
7335 ves_icall_get_frame_info);
7336 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
7337 ves_icall_get_trace);
7338 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
7339 mono_runtime_install_handlers);
7342 create_helper_signature ();
7344 /* Needs to be called here since register_jit_icall depends on it */
7345 mono_marshal_init ();
7347 mono_arch_register_lowlevel_calls ();
7348 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
7349 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
7351 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
7352 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
7354 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
7355 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
7356 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
7358 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
7359 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
7360 helper_sig_void_ptr, TRUE);
7363 * NOTE, NOTE, NOTE, NOTE:
7364 * when adding emulation for some opcodes, remember to also add a dummy
7365 * rule to the burg files, because we need the arity information to be correct.
7367 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
7368 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
7369 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
7370 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
7371 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
7372 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
7373 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
7375 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
7376 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
7377 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
7379 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
7380 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
7381 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
7382 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
7384 #if SIZEOF_VOID_P == 4
7385 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
7387 #warning "fixme: add opcode emulation"
7390 /* other jit icalls */
7391 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
7392 helper_sig_ptr_ptr_ptr, FALSE);
7393 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
7394 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
7395 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
7396 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
7397 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
7398 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
7399 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
7400 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
7401 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
7402 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
7403 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
7404 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
7405 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
7406 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
7407 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
7408 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
7409 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
7410 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
7411 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
7412 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
7413 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
7414 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
7415 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
7416 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
7417 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
7418 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7419 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7420 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
7421 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
7422 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
7423 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
7425 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
7426 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
7428 //mono_thread_attach (domain);
7432 MonoJitStats mono_jit_stats = {0};
7435 print_jit_stats (void)
7437 if (mono_jit_stats.enabled) {
7438 g_print ("Mono Jit statistics\n");
7439 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7440 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7441 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
7442 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
7443 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
7444 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
7445 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
7446 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
7447 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
7448 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
7449 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
7450 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
7451 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
7452 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
7453 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7454 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7455 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7456 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7458 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7459 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7460 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7461 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7462 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7467 mini_cleanup (MonoDomain *domain)
7470 * mono_runtime_cleanup() and mono_domain_finalize () need to
7471 * be called early since they need the execution engine still
7472 * fully working (mono_domain_finalize may invoke managed finalizers
7473 * and mono_runtime_cleanup will wait for other threads to finish).
7475 mono_domain_finalize (domain);
7477 mono_runtime_cleanup (domain);
7479 mono_profiler_shutdown ();
7481 mono_debug_cleanup ();
7483 #ifdef PLATFORM_WIN32
7484 win32_seh_cleanup();
7487 mono_domain_unload (domain, TRUE);
7493 mono_set_defaults (int verbose_level, guint32 opts)
7495 mini_verbose = verbose_level;