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_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
48 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
49 static gpointer mono_jit_compile_method (MonoMethod *method);
51 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
52 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
54 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
55 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
56 guint inline_offset, gboolean is_virtual_call);
58 extern guint8 mono_burg_arity [];
59 /* helper methods signature */
60 static MonoMethodSignature *helper_sig_long_long_long = NULL;
61 static MonoMethodSignature *helper_sig_long_long_int = NULL;
62 static MonoMethodSignature *helper_sig_newarr = NULL;
63 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
64 static MonoMethodSignature *helper_sig_ldstr = NULL;
65 static MonoMethodSignature *helper_sig_domain_get = NULL;
66 static MonoMethodSignature *helper_sig_object_new = NULL;
67 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
68 static MonoMethodSignature *helper_sig_compile = NULL;
69 static MonoMethodSignature *helper_sig_compile_virt = NULL;
70 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
71 static MonoMethodSignature *helper_sig_ptr_void = NULL;
72 static MonoMethodSignature *helper_sig_void_ptr = NULL;
73 static MonoMethodSignature *helper_sig_void_obj = NULL;
74 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
75 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
76 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
77 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
78 static MonoMethodSignature *helper_sig_ptr_int = NULL;
79 static MonoMethodSignature *helper_sig_initobj = NULL;
80 static MonoMethodSignature *helper_sig_memcpy = NULL;
81 static MonoMethodSignature *helper_sig_memset = NULL;
82 static MonoMethodSignature *helper_sig_ulong_double = NULL;
83 static MonoMethodSignature *helper_sig_long_double = NULL;
84 static MonoMethodSignature *helper_sig_uint_double = NULL;
85 static MonoMethodSignature *helper_sig_int_double = NULL;
86 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
88 static guint32 default_opt = MONO_OPT_PEEPHOLE;
90 guint32 mono_jit_tls_id = 0;
91 gboolean mono_jit_trace_calls = FALSE;
92 gboolean mono_break_on_exc = FALSE;
93 gboolean mono_compile_aot = FALSE;
94 gboolean mono_trace_coverage = FALSE;
95 gboolean mono_jit_profile = FALSE;
96 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
98 CRITICAL_SECTION *metadata_section = NULL;
100 static int mini_verbose = 0;
102 #ifdef MONO_USE_EXC_TABLES
104 mono_type_blittable (MonoType *type)
123 case MONO_TYPE_OBJECT:
125 case MONO_TYPE_VALUETYPE:
126 case MONO_TYPE_CLASS:
127 return type->data.klass->blittable;
137 mono_method_blittable (MonoMethod *method)
139 MonoMethodSignature *sig;
145 if (!mono_arch_has_unwind_info (method->addr)) {
149 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
152 sig = method->signature;
154 if (!mono_type_blittable (sig->ret))
157 for (i = 0; i < sig->param_count; i++)
158 if (!mono_type_blittable (sig->params [i]))
167 print_method_from_ip (void *ip)
172 ji = mono_jit_info_table_find (mono_domain_get (), ip);
174 g_print ("No method at %p\n", ip);
177 method = mono_method_full_name (ji->method, TRUE);
178 g_print ("IP at offset 0x%x of method %s (%p %p)\n", (char*)ip - (char*)ji->code_start, method, ji->code_start, (char*)ji->code_start + ji->code_size);
183 #define MONO_INIT_VARINFO(vi,id) do { \
184 (vi)->range.first_use.pos.bid = 0xffff; \
190 * Basic blocks have two numeric identifiers:
191 * dfn: Depth First Number
192 * block_num: unique ID assigned at bblock creation
194 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
195 #define ADD_BBLOCK(cfg,bbhash,b) do { \
196 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
197 (b)->block_num = cfg->num_bblocks++; \
198 (b)->real_offset = real_offset; \
201 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
202 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
204 if ((ip) >= end || (ip) < header->code) goto unverified; \
205 (tblock) = NEW_BBLOCK (cfg); \
206 (tblock)->cil_code = (ip); \
207 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
209 (tblock)->real_offset = real_offset; \
212 #define CHECK_BBLOCK(target,ip,tblock) do { \
213 if ((target) < (ip) && !(tblock)->code) { \
214 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
215 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); \
219 #define NEW_ICONST(cfg,dest,val) do { \
220 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
221 (dest)->opcode = OP_ICONST; \
222 (dest)->inst_c0 = (val); \
223 (dest)->type = STACK_I4; \
226 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
227 #define NEW_PCONST(cfg,dest,val) do { \
228 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
229 (dest)->opcode = OP_ICONST; \
230 (dest)->inst_p0 = (val); \
231 (dest)->type = STACK_PTR; \
234 #define NEW_CLASSCONST(cfg,dest,val) do { \
235 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
236 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
237 (dest)->inst_p0 = (val); \
238 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
239 (dest)->type = STACK_PTR; \
242 #define NEW_IMAGECONST(cfg,dest,val) do { \
243 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
244 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
245 (dest)->inst_p0 = (val); \
246 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
247 (dest)->type = STACK_PTR; \
250 #define NEW_FIELDCONST(cfg,dest,field) do { \
251 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
252 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
253 (dest)->inst_p0 = (field); \
254 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
255 (dest)->type = STACK_PTR; \
258 #define NEW_METHODCONST(cfg,dest,val) do { \
259 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
260 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
261 (dest)->inst_p0 = (val); \
262 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
263 (dest)->type = STACK_PTR; \
266 #define NEW_DOMAINCONST(cfg,dest) do { \
267 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) { \
268 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
270 NEW_PCONST (cfg, dest, (cfg)->domain); \
274 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
276 #define NEW_ARGLOAD(cfg,dest,num) do { \
277 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
278 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
279 (dest)->ssa_op = MONO_SSA_LOAD; \
280 (dest)->inst_i0 = arg_array [(num)]; \
281 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
282 type_to_eval_stack_type (param_types [(num)], (dest)); \
283 (dest)->klass = (dest)->inst_i0->klass; \
286 #define NEW_LOCLOAD(cfg,dest,num) do { \
287 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
288 (dest)->ssa_op = MONO_SSA_LOAD; \
289 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
290 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
291 type_to_eval_stack_type (header->locals [(num)], (dest)); \
292 (dest)->klass = (dest)->inst_i0->klass; \
295 #define NEW_LOCLOADA(cfg,dest,num) do { \
296 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
297 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
298 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
299 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
300 (dest)->opcode = OP_LDADDR; \
301 (dest)->type = STACK_MP; \
302 (dest)->klass = (dest)->inst_i0->klass; \
303 (cfg)->disable_ssa = TRUE; \
306 #define NEW_RETLOADA(cfg,dest) do { \
307 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
308 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
309 (dest)->inst_i0 = (cfg)->ret; \
310 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
311 (dest)->opcode = CEE_LDIND_I; \
312 (dest)->type = STACK_MP; \
313 (dest)->klass = (dest)->inst_i0->klass; \
314 (cfg)->disable_ssa = TRUE; \
317 #define NEW_ARGLOADA(cfg,dest,num) do { \
318 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
319 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
320 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
321 (dest)->inst_i0 = arg_array [(num)]; \
322 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
323 (dest)->opcode = OP_LDADDR; \
324 (dest)->type = STACK_MP; \
325 (dest)->klass = (dest)->inst_i0->klass; \
326 (cfg)->disable_ssa = TRUE; \
329 #define NEW_TEMPLOAD(cfg,dest,num) do { \
330 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
331 (dest)->ssa_op = MONO_SSA_LOAD; \
332 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
333 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
334 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
335 (dest)->klass = (dest)->inst_i0->klass; \
338 #define NEW_TEMPLOADA(cfg,dest,num) do { \
339 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
340 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
341 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
342 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
343 (dest)->opcode = OP_LDADDR; \
344 (dest)->type = STACK_MP; \
345 (dest)->klass = (dest)->inst_i0->klass; \
346 (cfg)->disable_ssa = TRUE; \
350 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
351 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
352 (dest)->inst_left = addr; \
353 (dest)->opcode = mono_type_to_ldind (vtype); \
354 type_to_eval_stack_type (vtype, (dest)); \
355 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
358 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
359 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
360 (dest)->inst_i0 = addr; \
361 (dest)->opcode = mono_type_to_stind (vtype); \
362 (dest)->inst_i1 = (value); \
363 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
366 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
367 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
368 (dest)->ssa_op = MONO_SSA_STORE; \
369 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
370 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
371 (dest)->inst_i1 = (inst); \
372 (dest)->klass = (dest)->inst_i0->klass; \
375 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
376 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
377 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
378 (dest)->ssa_op = MONO_SSA_STORE; \
379 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
380 (dest)->inst_i1 = (inst); \
381 (dest)->klass = (dest)->inst_i0->klass; \
384 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
385 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
386 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
387 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
388 (dest)->ssa_op = MONO_SSA_STORE; \
389 (dest)->inst_i0 = arg_array [(num)]; \
390 (dest)->inst_i1 = (inst); \
391 (dest)->klass = (dest)->inst_i0->klass; \
394 #define ADD_BINOP(op) do { \
395 MONO_INST_NEW (cfg, ins, (op)); \
396 ins->cil_code = ip; \
398 ins->inst_i0 = sp [0]; \
399 ins->inst_i1 = sp [1]; \
401 type_from_op (ins); \
405 #define ADD_UNOP(op) do { \
406 MONO_INST_NEW (cfg, ins, (op)); \
407 ins->cil_code = ip; \
409 ins->inst_i0 = sp [0]; \
411 type_from_op (ins); \
415 #define ADD_BINCOND(next_block) do { \
417 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
419 cmp->inst_i0 = sp [0]; \
420 cmp->inst_i1 = sp [1]; \
421 cmp->cil_code = ins->cil_code; \
422 type_from_op (cmp); \
424 ins->inst_i0 = cmp; \
425 MONO_ADD_INS (bblock, ins); \
426 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
427 GET_BBLOCK (cfg, bbhash, tblock, target); \
428 link_bblock (cfg, bblock, tblock); \
429 ins->inst_true_bb = tblock; \
430 CHECK_BBLOCK (target, ip, tblock); \
431 if ((next_block)) { \
432 link_bblock (cfg, bblock, (next_block)); \
433 ins->inst_false_bb = (next_block); \
434 start_new_bblock = 1; \
436 GET_BBLOCK (cfg, bbhash, tblock, ip); \
437 link_bblock (cfg, bblock, tblock); \
438 ins->inst_false_bb = tblock; \
439 start_new_bblock = 2; \
443 /* FIXME: handle float, long ... */
444 #define ADD_UNCOND(istrue) do { \
446 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
448 cmp->inst_i0 = sp [0]; \
449 switch (cmp->inst_i0->type) { \
451 cmp->inst_i1 = zero_int64; break; \
453 cmp->inst_i1 = zero_r8; break; \
456 cmp->inst_i1 = zero_ptr; break; \
458 cmp->inst_i1 = zero_obj; break; \
460 cmp->inst_i1 = zero_int32; \
462 cmp->cil_code = ins->cil_code; \
463 type_from_op (cmp); \
465 ins->inst_i0 = cmp; \
466 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
467 MONO_ADD_INS (bblock, ins); \
468 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
469 GET_BBLOCK (cfg, bbhash, tblock, target); \
470 link_bblock (cfg, bblock, tblock); \
471 ins->inst_true_bb = tblock; \
472 CHECK_BBLOCK (target, ip, tblock); \
473 GET_BBLOCK (cfg, bbhash, tblock, ip); \
474 link_bblock (cfg, bblock, tblock); \
475 ins->inst_false_bb = tblock; \
476 start_new_bblock = 2; \
479 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
480 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
481 (dest)->opcode = CEE_LDELEMA; \
482 (dest)->inst_left = (sp) [0]; \
483 (dest)->inst_right = (sp) [1]; \
484 (dest)->type = STACK_MP; \
485 (dest)->klass = (k); \
488 #define NEW_GROUP(cfg,dest,el1,el2) do { \
489 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
490 (dest)->opcode = OP_GROUP; \
491 (dest)->inst_left = (el1); \
492 (dest)->inst_right = (el2); \
495 static GHashTable *coverage_hash = NULL;
498 mono_allocate_coverage_info (MonoMethod *method, int size)
500 MonoCoverageInfo *res;
503 coverage_hash = g_hash_table_new (NULL, NULL);
505 res = g_malloc0 (sizeof (MonoCoverageInfo) + sizeof (int) * size * 2);
509 g_hash_table_insert (coverage_hash, method, res);
515 mono_get_coverage_info (MonoMethod *method)
520 return g_hash_table_lookup (coverage_hash, method);
525 compare_bblock (gconstpointer a, gconstpointer b)
527 const MonoBasicBlock *b1 = a;
528 const MonoBasicBlock *b2 = b;
530 return b2->cil_code - b1->cil_code;
535 * link_bblock: Links two basic blocks
537 * links two basic blocks in the control flow graph, the 'from'
538 * argument is the starting block and the 'to' argument is the block
539 * the control flow ends to after 'from'.
542 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
544 MonoBasicBlock **newa;
548 if (from->cil_code) {
550 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
552 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
555 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
557 g_print ("edge from entry to exit\n");
561 for (i = 0; i < from->out_count; ++i) {
562 if (to == from->out_bb [i]) {
568 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
569 for (i = 0; i < from->out_count; ++i) {
570 newa [i] = from->out_bb [i];
578 for (i = 0; i < to->in_count; ++i) {
579 if (from == to->in_bb [i]) {
585 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
586 for (i = 0; i < to->in_count; ++i) {
587 newa [i] = to->in_bb [i];
596 * We mark each basic block with a region ID. We use that to avoid BB
597 * optimizations when blocks are in different regions.
600 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
602 MonoMethod *method = cfg->method;
603 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
604 MonoExceptionClause *clause;
607 /* first search for handlers and filters */
608 for (i = 0; i < header->num_clauses; ++i) {
609 clause = &header->clauses [i];
610 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
611 (offset < (clause->token_or_filter + filter_lengths [i])))
612 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
614 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
615 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
616 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
618 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
622 /* search the try blocks */
623 for (i = 0; i < header->num_clauses; ++i) {
624 clause = &header->clauses [i];
625 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
626 return ((i + 1) << 8) | clause->flags;
633 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
635 MonoMethod *method = cfg->method;
636 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
637 MonoExceptionClause *clause;
638 MonoBasicBlock *handler;
642 for (i = 0; i < header->num_clauses; ++i) {
643 clause = &header->clauses [i];
644 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
645 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
646 if (clause->flags == type) {
647 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
649 res = g_list_append (res, handler);
658 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
662 array [*dfn] = start;
663 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
664 for (i = 0; i < start->out_count; ++i) {
665 if (start->out_bb [i]->dfn)
668 start->out_bb [i]->dfn = *dfn;
669 start->out_bb [i]->df_parent = start;
670 array [*dfn] = start->out_bb [i];
671 df_visit (start->out_bb [i], dfn, array);
677 MonoBasicBlock *best;
681 previous_foreach (gconstpointer key, gpointer val, gpointer data)
683 PrevStruct *p = data;
684 MonoBasicBlock *bb = val;
685 //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,
686 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
688 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
692 static MonoBasicBlock*
693 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
699 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
704 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
713 * FIXME: take into account all the details:
714 * second may have been the target of more than one bblock
716 second->out_count = first->out_count;
717 second->out_bb = first->out_bb;
719 for (i = 0; i < first->out_count; ++i) {
720 bb = first->out_bb [i];
721 for (j = 0; j < bb->in_count; ++j) {
722 if (bb->in_bb [j] == first)
723 bb->in_bb [j] = second;
727 first->out_count = 0;
728 first->out_bb = NULL;
729 link_bblock (cfg, first, second);
731 second->last_ins = first->last_ins;
733 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
734 for (inst = first->code; inst && inst->next; inst = inst->next) {
735 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
736 g_print ("found %p: %s", inst->next->cil_code, code);
738 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
739 second->code = inst->next;
741 first->last_ins = inst;
742 second->next_bb = first->next_bb;
743 first->next_bb = second;
748 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
754 mono_type_to_ldind (MonoType *type)
766 case MONO_TYPE_BOOLEAN:
780 case MONO_TYPE_FNPTR:
782 case MONO_TYPE_CLASS:
783 case MONO_TYPE_STRING:
784 case MONO_TYPE_OBJECT:
785 case MONO_TYPE_SZARRAY:
786 case MONO_TYPE_ARRAY:
787 return CEE_LDIND_REF;
795 case MONO_TYPE_VALUETYPE:
796 if (type->data.klass->enumtype) {
797 t = type->data.klass->enum_basetype->type;
801 case MONO_TYPE_TYPEDBYREF:
804 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
810 mono_type_to_stind (MonoType *type)
821 case MONO_TYPE_BOOLEAN:
833 case MONO_TYPE_FNPTR:
835 case MONO_TYPE_CLASS:
836 case MONO_TYPE_STRING:
837 case MONO_TYPE_OBJECT:
838 case MONO_TYPE_SZARRAY:
839 case MONO_TYPE_ARRAY:
840 return CEE_STIND_REF;
848 case MONO_TYPE_VALUETYPE:
849 if (type->data.klass->enumtype) {
850 t = type->data.klass->enum_basetype->type;
854 case MONO_TYPE_TYPEDBYREF:
857 g_error ("unknown type 0x%02x in type_to_stind", type->type);
863 * Returns the type used in the eval stack when @type is loaded.
864 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
867 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
871 inst->type = STACK_MP;
879 case MONO_TYPE_BOOLEAN:
885 inst->type = STACK_I4;
890 case MONO_TYPE_FNPTR:
891 inst->type = STACK_PTR;
893 case MONO_TYPE_CLASS:
894 case MONO_TYPE_STRING:
895 case MONO_TYPE_OBJECT:
896 case MONO_TYPE_SZARRAY:
897 case MONO_TYPE_ARRAY:
898 inst->type = STACK_OBJ;
902 inst->type = STACK_I8;
906 inst->type = STACK_R8;
908 case MONO_TYPE_VALUETYPE:
909 if (type->data.klass->enumtype) {
910 t = type->data.klass->enum_basetype->type;
913 inst->klass = type->data.klass;
914 inst->type = STACK_VTYPE;
917 case MONO_TYPE_TYPEDBYREF:
918 inst->klass = mono_defaults.typed_reference_class;
919 inst->type = STACK_VTYPE;
922 g_error ("unknown type 0x%02x in eval stack type", type->type);
927 * The following tables are used to quickly validate the IL code in type_from_op ().
930 bin_num_table [STACK_MAX] [STACK_MAX] = {
931 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
932 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
933 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
934 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
935 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
936 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
937 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
938 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
943 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
946 /* reduce the size of this table */
948 bin_int_table [STACK_MAX] [STACK_MAX] = {
949 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
950 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
951 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
952 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
953 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
954 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
955 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
956 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
960 bin_comp_table [STACK_MAX] [STACK_MAX] = {
962 {0, 1, 0, 1, 0, 0, 4, 0},
963 {0, 0, 1, 0, 0, 0, 0, 0},
964 {0, 1, 0, 1, 0, 2, 4, 0},
965 {0, 0, 0, 0, 1, 0, 0, 0},
966 {0, 0, 0, 2, 0, 1, 0, 0},
967 {0, 4, 0, 4, 0, 0, 3, 0},
968 {0, 0, 0, 0, 0, 0, 0, 0},
971 /* reduce the size of this table */
973 shift_table [STACK_MAX] [STACK_MAX] = {
974 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
975 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
976 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
977 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
978 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
979 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
980 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
981 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
985 * Tables to map from the non-specific opcode to the matching
986 * type-specific opcode.
988 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
990 binops_op_map [STACK_MAX] = {
991 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
994 /* handles from CEE_NEG to CEE_CONV_U8 */
996 unops_op_map [STACK_MAX] = {
997 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
1000 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
1001 static const guint16
1002 ovfops_op_map [STACK_MAX] = {
1003 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1006 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1007 static const guint16
1008 ovf2ops_op_map [STACK_MAX] = {
1009 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
1012 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1013 static const guint16
1014 ovf3ops_op_map [STACK_MAX] = {
1015 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
1018 /* handles from CEE_CEQ to CEE_CLT_UN */
1019 static const guint16
1020 ceqops_op_map [STACK_MAX] = {
1021 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1025 * Sets ins->type (the type on the eval stack) according to the
1026 * type of the opcode and the arguments to it.
1027 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1029 * FIXME: this function sets ins->type unconditionally in some cases, but
1030 * it should set it to invalid for some types (a conv.x on an object)
1033 type_from_op (MonoInst *ins) {
1034 switch (ins->opcode) {
1041 /* FIXME: check unverifiable args for STACK_MP */
1042 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1043 ins->opcode += binops_op_map [ins->type];
1050 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1051 ins->opcode += binops_op_map [ins->type];
1056 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1057 ins->opcode += binops_op_map [ins->type];
1060 /* FIXME: handle some specifics with ins->next->type */
1061 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1065 case 256+CEE_CGT_UN:
1067 case 256+CEE_CLT_UN:
1068 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1069 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1073 ins->type = neg_table [ins->inst_i0->type];
1074 ins->opcode += unops_op_map [ins->type];
1077 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1078 ins->type = ins->inst_i0->type;
1080 ins->type = STACK_INV;
1081 ins->opcode += unops_op_map [ins->type];
1087 ins->type = STACK_I4;
1088 ins->opcode += unops_op_map [ins->inst_i0->type];
1091 ins->type = STACK_R8;
1092 switch (ins->inst_i0->type) {
1097 ins->opcode = OP_LCONV_TO_R_UN;
1101 case CEE_CONV_OVF_I1:
1102 case CEE_CONV_OVF_U1:
1103 case CEE_CONV_OVF_I2:
1104 case CEE_CONV_OVF_U2:
1105 case CEE_CONV_OVF_I4:
1106 case CEE_CONV_OVF_U4:
1107 ins->type = STACK_I4;
1108 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1110 case CEE_CONV_OVF_I_UN:
1111 case CEE_CONV_OVF_U_UN:
1112 ins->type = STACK_PTR;
1113 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1115 case CEE_CONV_OVF_I1_UN:
1116 case CEE_CONV_OVF_I2_UN:
1117 case CEE_CONV_OVF_I4_UN:
1118 case CEE_CONV_OVF_U1_UN:
1119 case CEE_CONV_OVF_U2_UN:
1120 case CEE_CONV_OVF_U4_UN:
1121 ins->type = STACK_I4;
1122 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1125 ins->type = STACK_PTR;
1126 switch (ins->inst_i0->type) {
1132 ins->opcode = OP_LCONV_TO_U;
1135 ins->opcode = OP_FCONV_TO_U;
1141 ins->type = STACK_I8;
1142 ins->opcode += unops_op_map [ins->inst_i0->type];
1144 case CEE_CONV_OVF_I8:
1145 case CEE_CONV_OVF_U8:
1146 ins->type = STACK_I8;
1147 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1149 case CEE_CONV_OVF_U8_UN:
1150 case CEE_CONV_OVF_I8_UN:
1151 ins->type = STACK_I8;
1152 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1156 ins->type = STACK_R8;
1157 ins->opcode += unops_op_map [ins->inst_i0->type];
1160 ins->type = STACK_R8;
1164 ins->type = STACK_I4;
1165 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1168 case CEE_CONV_OVF_I:
1169 case CEE_CONV_OVF_U:
1170 ins->type = STACK_PTR;
1171 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1174 case CEE_ADD_OVF_UN:
1176 case CEE_MUL_OVF_UN:
1178 case CEE_SUB_OVF_UN:
1179 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1180 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1183 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1190 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1193 /* map ldelem.x to the matching ldind.x opcode */
1195 ldelem_to_ldind [] = {
1209 /* map stelem.x to the matching stind.x opcode */
1211 stelem_to_stind [] = {
1225 param_table [STACK_MAX] [STACK_MAX] = {
1230 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1234 switch (args->type) {
1244 for (i = 0; i < sig->param_count; ++i) {
1245 switch (args [i].type) {
1249 if (!sig->params [i]->byref)
1253 if (sig->params [i]->byref)
1255 switch (sig->params [i]->type) {
1256 case MONO_TYPE_CLASS:
1257 case MONO_TYPE_STRING:
1258 case MONO_TYPE_OBJECT:
1259 case MONO_TYPE_SZARRAY:
1260 case MONO_TYPE_ARRAY:
1267 if (sig->params [i]->byref)
1269 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1278 /*if (!param_table [args [i].type] [sig->params [i]->type])
1286 * When we need a pointer to the current domain many times in a method, we
1287 * call mono_domain_get() once and we store the result in a local variable.
1288 * This function returns the variable that represents the MonoDomain*.
1290 inline static MonoInst *
1291 mono_get_domainvar (MonoCompile *cfg)
1293 if (!cfg->domainvar)
1294 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1295 return cfg->domainvar;
1299 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1302 int num = cfg->num_varinfo;
1304 if ((num + 1) >= cfg->varinfo_count) {
1305 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1306 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1307 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1310 mono_jit_stats.allocate_var++;
1312 MONO_INST_NEW (cfg, inst, opcode);
1313 inst->inst_c0 = num;
1314 inst->inst_vtype = type;
1315 inst->klass = mono_class_from_mono_type (type);
1316 /* if set to 1 the variable is native */
1319 cfg->varinfo [num] = inst;
1321 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1322 MONO_INIT_VARINFO (cfg->vars [num], num);
1325 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1330 type_from_stack_type (MonoInst *ins) {
1331 switch (ins->type) {
1332 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1333 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1334 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1335 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1336 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1337 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1338 case STACK_VTYPE: return &ins->klass->byval_arg;
1340 g_error ("stack type %d to montype not handled\n", ins->type);
1346 array_access_to_klass (int opcode)
1350 return mono_defaults.byte_class;
1352 return mono_defaults.uint16_class;
1355 return mono_defaults.int_class;
1358 return mono_defaults.sbyte_class;
1361 return mono_defaults.int16_class;
1364 return mono_defaults.int32_class;
1366 return mono_defaults.uint32_class;
1369 return mono_defaults.int64_class;
1372 return mono_defaults.single_class;
1375 return mono_defaults.double_class;
1376 case CEE_LDELEM_REF:
1377 case CEE_STELEM_REF:
1378 return mono_defaults.object_class;
1380 g_assert_not_reached ();
1386 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1390 MONO_ADD_INS (bb, inst);
1393 switch (bb->last_ins->opcode) {
1406 while (prev->next && prev->next != bb->last_ins)
1408 if (prev == bb->code) {
1409 if (bb->last_ins == bb->code) {
1410 inst->next = bb->code;
1413 inst->next = prev->next;
1417 inst->next = bb->last_ins;
1421 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1423 MONO_ADD_INS (bb, inst);
1429 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1431 MonoInst *inst, *load;
1433 NEW_TEMPLOAD (cfg, load, src);
1435 NEW_TEMPSTORE (cfg, inst, dest, load);
1436 if (inst->opcode == CEE_STOBJ) {
1437 NEW_TEMPLOADA (cfg, inst, dest);
1438 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1440 inst->cil_code = NULL;
1441 mono_add_ins_to_end (bb, inst);
1446 * We try to share variables when possible
1449 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1454 /* inlining can result in deeper stacks */
1455 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1456 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1458 pos = ins->type - 1 + slot * STACK_MAX;
1460 switch (ins->type) {
1467 if ((vnum = cfg->intvars [pos]))
1468 return cfg->varinfo [vnum];
1469 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1470 cfg->intvars [pos] = res->inst_c0;
1473 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1479 * This function is called to handle items that are left on the evaluation stack
1480 * at basic block boundaries. What happens is that we save the values to local variables
1481 * and we reload them later when first entering the target basic block (with the
1482 * handle_loaded_temps () function).
1483 * A single joint point will use the same variables (stored in the array bb->out_stack or
1484 * bb->in_stack, if the basic block is before or after the joint point).
1487 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1489 MonoBasicBlock *outb;
1490 MonoInst *inst, **locals;
1494 if (cfg->verbose_level > 3)
1495 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1496 if (!bb->out_scount) {
1498 bb->out_scount = count;
1499 //g_print ("bblock %d has out:", bb->block_num);
1500 for (i = 0; i < bb->out_count; ++i) {
1501 outb = bb->out_bb [i];
1502 //g_print (" %d", outb->block_num);
1503 if (outb->in_stack) {
1505 bb->out_stack = outb->in_stack;
1511 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1512 for (i = 0; i < count; ++i) {
1514 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1515 * stack slot and if they are of the same type. */
1516 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1518 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1523 locals = bb->out_stack;
1524 for (i = 0; i < count; ++i) {
1525 /* add store ops at the end of the bb, before the branch */
1526 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1527 if (inst->opcode == CEE_STOBJ) {
1528 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1529 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1531 inst->cil_code = sp [i]->cil_code;
1532 mono_add_ins_to_end (bb, inst);
1534 if (cfg->verbose_level > 3)
1535 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1538 for (i = 0; i < bb->out_count; ++i) {
1539 outb = bb->out_bb [i];
1540 if (outb->in_scount)
1541 continue; /* check they are the same locals */
1542 outb->in_scount = count;
1543 outb->in_stack = locals;
1549 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1554 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1558 case MONO_TYPE_VOID:
1559 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1562 case MONO_TYPE_BOOLEAN:
1565 case MONO_TYPE_CHAR:
1568 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1572 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1573 case MONO_TYPE_CLASS:
1574 case MONO_TYPE_STRING:
1575 case MONO_TYPE_OBJECT:
1576 case MONO_TYPE_SZARRAY:
1577 case MONO_TYPE_ARRAY:
1578 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1581 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1584 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1585 case MONO_TYPE_VALUETYPE:
1586 if (type->data.klass->enumtype) {
1587 t = type->data.klass->enum_basetype->type;
1590 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1591 case MONO_TYPE_TYPEDBYREF:
1592 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1594 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1600 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1602 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1604 ji->ip.label = label;
1605 ji->type = MONO_PATCH_INFO_SWITCH;
1606 ji->data.table = bbs;
1607 ji->next = cfg->patch_info;
1608 ji->table_size = num_blocks;
1609 cfg->patch_info = ji;
1613 * When we add a tree of instructions, we need to ensure the instructions currently
1614 * on the stack are executed before (like, if we load a value from a local).
1615 * We ensure this by saving the currently loaded values to temps and rewriting the
1616 * instructions to load the values.
1617 * This is not done for opcodes that terminate a basic block (because it's handled already
1618 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1621 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1623 MonoInst *load, *store, *temp, *ins;
1625 while (stack < sp) {
1627 /* handle also other constants */
1628 if (ins->opcode != OP_ICONST) {
1629 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1630 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1631 store->cil_code = ins->cil_code;
1632 if (store->opcode == CEE_STOBJ) {
1633 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1634 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1636 MONO_ADD_INS (bblock, store);
1637 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1638 load->cil_code = ins->cil_code;
1646 * Prepare arguments for passing to a function call.
1647 * Return a non-zero value if the arguments can't be passed to the given
1649 * The type checks are not yet complete and some conversions may need
1650 * casts on 32 or 64 bit architectures.
1653 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1658 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1662 for (i = 0; i < sig->param_count; ++i) {
1663 if (sig->params [i]->byref) {
1665 * check the result of ldelema is only passed as an argument if the byref
1666 * type matches exactly the array element type.
1667 * FIXME: if the argument as been saved on the stack as part of the
1668 * interface variable code (the value was on the stack at a basic block boundary)
1669 * we need to add the check in that case, too.
1671 if (args [i]->opcode == CEE_LDELEMA) {
1673 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1674 if (!exact_class->valuetype) {
1675 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1676 check->cil_code = args [i]->cil_code;
1677 check->klass = exact_class;
1678 check->inst_left = args [i]->inst_left;
1679 check->type = STACK_OBJ;
1680 args [i]->inst_left = check;
1683 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1687 simple_type = sig->params [i]->type;
1689 switch (simple_type) {
1690 case MONO_TYPE_VOID:
1695 case MONO_TYPE_BOOLEAN:
1698 case MONO_TYPE_CHAR:
1701 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1707 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1710 case MONO_TYPE_CLASS:
1711 case MONO_TYPE_STRING:
1712 case MONO_TYPE_OBJECT:
1713 case MONO_TYPE_SZARRAY:
1714 case MONO_TYPE_ARRAY:
1715 if (args [i]->type != STACK_OBJ)
1720 if (args [i]->type != STACK_I8)
1725 if (args [i]->type != STACK_R8)
1728 case MONO_TYPE_VALUETYPE:
1729 if (sig->params [i]->data.klass->enumtype) {
1730 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1733 if (args [i]->type != STACK_VTYPE)
1736 case MONO_TYPE_TYPEDBYREF:
1737 if (args [i]->type != STACK_VTYPE)
1741 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1748 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1749 const guint8 *ip, gboolean to_end)
1751 MonoInst *temp, *store, *ins = (MonoInst*)call;
1752 MonoType *ret = sig->ret;
1754 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1756 call->inst.type = STACK_OBJ;
1757 call->inst.opcode = CEE_CALL;
1758 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1760 type_to_eval_stack_type (ret, ins);
1761 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1764 if (MONO_TYPE_ISSTRUCT (ret)) {
1767 /* we use this to allocate native sized structs */
1768 temp->unused = sig->pinvoke;
1770 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1771 if (call->inst.opcode == OP_VCALL)
1772 ins->inst_left = loada;
1774 ins->inst_right = loada; /* a virtual or indirect call */
1777 mono_add_ins_to_end (bblock, ins);
1779 MONO_ADD_INS (bblock, ins);
1781 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1782 store->cil_code = ip;
1784 mono_add_ins_to_end (bblock, store);
1786 MONO_ADD_INS (bblock, store);
1788 return temp->inst_c0;
1791 mono_add_ins_to_end (bblock, ins);
1793 MONO_ADD_INS (bblock, ins);
1798 inline static MonoCallInst *
1799 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1800 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1805 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1807 call->inst.cil_code = ip;
1809 call->signature = sig;
1810 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1812 for (i = 0; i < (sig->param_count + sig->hasthis); ++i) {
1813 if (call->args [i]) {
1814 if (!call->args [i]->cil_code)
1815 call->args [i]->cil_code = ip;
1817 mono_add_ins_to_end (bblock, call->args [i]);
1819 MONO_ADD_INS (bblock, call->args [i]);
1826 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1827 MonoInst **args, MonoInst *addr, const guint8 *ip)
1829 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1831 call->inst.inst_i0 = addr;
1833 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1836 static MonoCallInst*
1837 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1838 MonoInst **args, const guint8 *ip, MonoInst *this)
1840 gboolean virtual = this != NULL;
1843 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1845 if (this && sig->hasthis &&
1846 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1847 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1848 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1850 call->method = method;
1852 call->inst.flags |= MONO_INST_HAS_METHOD;
1853 call->inst.inst_left = this;
1859 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1860 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1862 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1864 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1868 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1869 MonoInst **args, const guint8 *ip, gboolean to_end)
1875 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1877 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1881 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1883 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1886 g_warning ("unregistered JIT ICall");
1887 g_assert_not_reached ();
1890 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1894 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1896 MonoInst *ins, *temp = NULL, *store, *load;
1900 /*g_print ("emulating: ");
1901 mono_print_tree (tree);
1903 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1904 ins = (MonoInst*)call;
1906 call->inst.cil_code = tree->cil_code;
1908 call->signature = info->sig;
1910 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1912 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1913 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1914 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1915 store->cil_code = tree->cil_code;
1920 nargs = info->sig->param_count + info->sig->hasthis;
1922 for (i = 1; i < nargs; i++) {
1923 call->args [i - 1]->next = call->args [i];
1927 call->args [nargs - 1]->next = store;
1929 if (cfg->prev_ins) {
1930 store->next = cfg->prev_ins->next;
1932 cfg->prev_ins->next = call->args [0];
1934 cfg->prev_ins->next = store;
1936 store->next = cfg->cbb->code;
1938 cfg->cbb->code = call->args [0];
1940 cfg->cbb->code = store;
1944 call->fptr = info->wrapper;
1946 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1947 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1952 static MonoMethodSignature *
1953 mono_get_element_address_signature (int arity)
1955 static GHashTable *sighash = NULL;
1956 MonoMethodSignature *res;
1960 sighash = g_hash_table_new (NULL, NULL);
1963 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1966 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1968 res->params [0] = &mono_defaults.array_class->byval_arg;
1970 for (i = 1; i <= arity; i++)
1971 res->params [i] = &mono_defaults.int_class->byval_arg;
1973 res->ret = &mono_defaults.int_class->byval_arg;
1975 g_hash_table_insert (sighash, (gpointer)arity, res);
1981 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1982 MonoInst *iargs [3];
1988 * This check breaks with spilled vars... need to handle it during verification anyway.
1989 * g_assert (klass && klass == src->klass && klass == dest->klass);
1993 n = mono_class_native_size (klass, &align);
1995 n = mono_class_value_size (klass, &align);
1997 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
1999 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
2000 inst->inst_left = dest;
2001 inst->inst_right = src;
2002 inst->cil_code = ip;
2004 MONO_ADD_INS (bblock, inst);
2009 NEW_ICONST (cfg, iargs [2], n);
2011 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2015 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2017 MonoInst *iargs [2];
2018 MonoInst *ins, *zero_int32;
2021 NEW_ICONST (cfg, zero_int32, 0);
2023 mono_class_init (klass);
2024 n = mono_class_value_size (klass, NULL);
2025 MONO_INST_NEW (cfg, ins, 0);
2027 ins->inst_left = dest;
2028 ins->inst_right = zero_int32;
2031 ins->opcode = CEE_STIND_I1;
2032 MONO_ADD_INS (bblock, ins);
2035 ins->opcode = CEE_STIND_I2;
2036 MONO_ADD_INS (bblock, ins);
2039 ins->opcode = CEE_STIND_I4;
2040 MONO_ADD_INS (bblock, ins);
2043 handle_loaded_temps (cfg, bblock, stack_start, sp);
2044 NEW_ICONST (cfg, ins, n);
2047 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2052 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2055 needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2060 for (i = 0; i < klass->method.count; ++i) {
2061 method = klass->methods [i];
2062 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2063 (strcmp (".cctor", method->name) == 0)) {
2064 if (caller == method)
2073 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2075 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2076 MonoMethodSignature *signature = method->signature;
2080 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2081 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2082 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2083 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2084 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2085 (method->klass->marshalbyref) ||
2086 !header || header->num_clauses ||
2087 /* fixme: why cant we inline valuetype returns? */
2088 MONO_TYPE_ISSTRUCT (signature->ret))
2091 /* its not worth to inline methods with valuetype arguments?? */
2092 for (i = 0; i < signature->param_count; i++) {
2093 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2099 * if we can initialize the class of the method right away, we do,
2100 * otherwise we don't allow inlining if the class needs initialization,
2101 * since it would mean inserting a call to mono_runtime_class_init()
2102 * inside the inlined code
2104 if (!(cfg->opt & MONO_OPT_SHARED)) {
2105 vtable = mono_class_vtable (cfg->domain, method->klass);
2106 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2107 mono_runtime_class_init (vtable);
2108 else if (!vtable->initialized && needs_cctor_run (method->klass, NULL))
2112 * If we're compiling for shared code
2113 * the cctor will need to be run at aot method load time, for example,
2114 * or at the end of the compilation of the inlining method.
2116 if (needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2119 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2121 /* also consider num_locals? */
2122 if (header->code_size < 20)
2129 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2133 MonoMethodSignature *esig;
2135 rank = cmethod->signature->param_count - (is_set? 1: 0);
2137 * FIXME: handle TypeMismatch for set or use the slow path
2140 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2142 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2143 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2144 addr->inst_left = sp [0];
2145 addr->inst_right = indexes;
2146 addr->cil_code = ip;
2147 addr->type = STACK_MP;
2148 addr->klass = cmethod->klass;
2151 esig = mono_get_element_address_signature (rank);
2152 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2153 NEW_TEMPLOAD (cfg, addr, temp);
2158 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2163 if (cmethod->klass == mono_defaults.string_class) {
2164 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2167 } else if (cmethod->klass == mono_defaults.math_class) {
2168 if (strcmp (cmethod->name, "Sin") == 0)
2170 else if (strcmp (cmethod->name, "Cos") == 0)
2172 else if (strcmp (cmethod->name, "Tan") == 0)
2174 else if (strcmp (cmethod->name, "Atan") == 0)
2176 else if (strcmp (cmethod->name, "Sqrt") == 0)
2178 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2185 pc = fsig->param_count + fsig->hasthis;
2186 MONO_INST_NEW (cfg, ins, op);
2189 ins->inst_i0 = args [0];
2191 ins->inst_i1 = args [1];
2198 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2200 MonoInst *store, *temp;
2203 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2205 if (!sig->hasthis && sig->param_count == 0)
2209 if (sp [0]->opcode == OP_ICONST) {
2212 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2214 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2215 store->cil_code = sp [0]->cil_code;
2216 MONO_ADD_INS (bblock, store);
2221 for (i = 0; i < sig->param_count; ++i) {
2222 if (sp [0]->opcode == OP_ICONST) {
2225 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2227 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2228 store->cil_code = sp [0]->cil_code;
2229 if (store->opcode == CEE_STOBJ) {
2230 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2231 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2233 MONO_ADD_INS (bblock, store);
2241 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2242 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2244 MonoInst *ins, *rvar = NULL;
2245 MonoMethodHeader *cheader;
2246 MonoBasicBlock *ebblock, *sbblock;
2247 int i, costs, new_locals_offset;
2249 if (cfg->verbose_level > 2)
2250 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2252 cheader = ((MonoMethodNormal *)cmethod)->header;
2254 if (!cmethod->inline_info) {
2255 mono_jit_stats.inlineable_methods++;
2256 cmethod->inline_info = 1;
2258 /* allocate space to store the return value */
2259 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2260 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2263 /* allocate local variables */
2264 new_locals_offset = cfg->num_varinfo;
2265 for (i = 0; i < cheader->num_locals; ++i)
2266 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2268 /* allocate starte and end blocks */
2269 sbblock = NEW_BBLOCK (cfg);
2270 sbblock->block_num = cfg->num_bblocks++;
2271 sbblock->real_offset = real_offset;
2273 ebblock = NEW_BBLOCK (cfg);
2274 ebblock->block_num = cfg->num_bblocks++;
2275 ebblock->real_offset = real_offset;
2277 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2279 if (costs >= 0 && costs < 60) {
2280 if (cfg->verbose_level > 2)
2281 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2283 mono_jit_stats.inlined_methods++;
2285 /* always add some code to avoid block split failures */
2286 MONO_INST_NEW (cfg, ins, CEE_NOP);
2287 MONO_ADD_INS (bblock, ins);
2290 bblock->next_bb = sbblock;
2291 link_bblock (cfg, bblock, sbblock);
2294 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2300 if (cfg->verbose_level > 2)
2301 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2307 * Some of these comments may well be out-of-date.
2308 * Design decisions: we do a single pass over the IL code (and we do bblock
2309 * splitting/merging in the few cases when it's required: a back jump to an IL
2310 * address that was not already seen as bblock starting point).
2311 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2312 * Complex operations are decomposed in simpler ones right away. We need to let the
2313 * arch-specific code peek and poke inside this process somehow (except when the
2314 * optimizations can take advantage of the full semantic info of coarse opcodes).
2315 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2316 * MonoInst->opcode initially is the IL opcode or some simplification of that
2317 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2318 * opcode with value bigger than OP_LAST.
2319 * At this point the IR can be handed over to an interpreter, a dumb code generator
2320 * or to the optimizing code generator that will translate it to SSA form.
2322 * Profiling directed optimizations.
2323 * We may compile by default with few or no optimizations and instrument the code
2324 * or the user may indicate what methods to optimize the most either in a config file
2325 * or through repeated runs where the compiler applies offline the optimizations to
2326 * each method and then decides if it was worth it.
2329 * * consider using an array instead of an hash table (bb_hash)
2332 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2333 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2334 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2336 /* offset from br.s -> br like opcodes */
2337 #define BIG_BRANCH_OFFSET 13
2340 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end)
2342 unsigned char *ip = start;
2343 unsigned char *target;
2346 MonoBasicBlock *bblock;
2347 const MonoOpcode *opcode;
2350 cli_addr = ip - start;
2351 i = mono_opcode_value (&ip);
2352 opcode = &mono_opcodes [i];
2353 switch (opcode->argument) {
2354 case MonoInlineNone:
2357 case MonoInlineString:
2358 case MonoInlineType:
2359 case MonoInlineField:
2360 case MonoInlineMethod:
2363 case MonoShortInlineR:
2370 case MonoShortInlineVar:
2371 case MonoShortInlineI:
2374 case MonoShortInlineBrTarget:
2375 target = start + cli_addr + 2 + (signed char)ip [1];
2376 GET_BBLOCK (cfg, bbhash, bblock, target);
2379 case MonoInlineBrTarget:
2380 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2381 GET_BBLOCK (cfg, bbhash, bblock, target);
2384 case MonoInlineSwitch: {
2385 guint32 n = read32 (ip + 1);
2388 cli_addr += 5 + 4 * n;
2389 target = start + cli_addr;
2390 GET_BBLOCK (cfg, bbhash, bblock, target);
2392 for (j = 0; j < n; ++j) {
2393 target = start + cli_addr + (gint32)read32 (ip);
2394 GET_BBLOCK (cfg, bbhash, bblock, target);
2404 g_assert_not_reached ();
2413 * mono_method_to_ir: translates IL into basic blocks containing trees
2416 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2417 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2418 guint inline_offset, gboolean is_virtual_call)
2420 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2421 MonoInst *ins, **sp, **stack_start;
2422 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2424 MonoMethod *cmethod;
2425 MonoInst **arg_array;
2426 MonoMethodHeader *header;
2428 guint32 token, ins_flag;
2430 unsigned char *ip, *end, *target;
2431 static double r8_0 = 0.0;
2432 MonoMethodSignature *sig;
2433 MonoType **param_types;
2434 GList *bb_recheck = NULL, *tmp;
2435 int i, n, start_new_bblock, align;
2436 int num_calls = 0, inline_costs = 0;
2437 int *filter_lengths = NULL;
2438 int breakpoint_id = 0;
2441 image = method->klass->image;
2442 header = ((MonoMethodNormal *)method)->header;
2443 sig = method->signature;
2444 ip = (unsigned char*)header->code;
2445 end = ip + header->code_size;
2446 mono_jit_stats.cil_code_size += header->code_size;
2448 if (cfg->method == method) {
2450 bbhash = cfg->bb_hash;
2452 real_offset = inline_offset;
2453 bbhash = g_hash_table_new (g_direct_hash, NULL);
2456 dont_inline = g_list_prepend (dont_inline, method);
2457 if (cfg->method == method) {
2460 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2461 start_bblock->cil_code = NULL;
2462 start_bblock->cil_length = 0;
2463 start_bblock->block_num = cfg->num_bblocks++;
2466 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2467 end_bblock->cil_code = NULL;
2468 end_bblock->cil_length = 0;
2469 end_bblock->block_num = cfg->num_bblocks++;
2470 g_assert (cfg->num_bblocks == 2);
2472 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2473 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2474 arg_array [i] = cfg->varinfo [i];
2476 if (mono_compile_aot)
2477 cfg->opt |= MONO_OPT_SHARED;
2479 if (header->num_clauses) {
2480 int size = sizeof (int) * header->num_clauses;
2481 filter_lengths = alloca (size);
2482 memset (filter_lengths, 0, size);
2484 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2485 /* prevent it from being register allocated */
2486 cfg->spvar->flags |= MONO_INST_INDIRECT;
2488 /* handle exception clauses */
2489 for (i = 0; i < header->num_clauses; ++i) {
2490 //unsigned char *p = ip;
2491 MonoExceptionClause *clause = &header->clauses [i];
2492 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2493 tblock->real_offset = clause->try_offset;
2494 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2495 tblock->real_offset = clause->handler_offset;
2497 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2498 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2499 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2500 MONO_ADD_INS (tblock, ins);
2503 /*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);
2505 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2507 /* catch and filter blocks get the exception object on the stack */
2508 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2509 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2510 /* mostly like handle_stack_args (), but just sets the input args */
2511 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2513 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2514 /* prevent it from being register allocated */
2515 cfg->exvar->flags |= MONO_INST_INDIRECT;
2517 tblock->in_scount = 1;
2518 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2519 tblock->in_stack [0] = cfg->exvar;
2521 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2522 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2523 tblock->real_offset = clause->token_or_filter;
2524 tblock->in_scount = 1;
2525 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2526 tblock->in_stack [0] = cfg->exvar;
2527 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2528 MONO_ADD_INS (tblock, ins);
2534 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2535 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2538 /* FIRST CODE BLOCK */
2539 bblock = NEW_BBLOCK (cfg);
2540 bblock->cil_code = ip;
2542 ADD_BBLOCK (cfg, bbhash, bblock);
2544 if (cfg->method == method) {
2545 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2546 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2547 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2548 MONO_ADD_INS (bblock, ins);
2552 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED)))) {
2553 /* we use a separate basic block for the initialization code */
2554 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2555 init_localsbb->real_offset = real_offset;
2556 start_bblock->next_bb = init_localsbb;
2557 init_localsbb->next_bb = bblock;
2558 link_bblock (cfg, start_bblock, init_localsbb);
2559 link_bblock (cfg, init_localsbb, bblock);
2560 init_localsbb->block_num = cfg->num_bblocks++;
2562 start_bblock->next_bb = bblock;
2563 link_bblock (cfg, start_bblock, bblock);
2566 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end))
2569 mono_debug_init_method (cfg, bblock, breakpoint_id);
2571 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2573 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2574 for (n = 0; n < sig->param_count; ++n)
2575 param_types [n + sig->hasthis] = sig->params [n];
2577 /* do this somewhere outside - not here */
2578 NEW_ICONST (cfg, zero_int32, 0);
2579 NEW_ICONST (cfg, zero_int64, 0);
2580 zero_int64->type = STACK_I8;
2581 NEW_PCONST (cfg, zero_ptr, 0);
2582 NEW_PCONST (cfg, zero_obj, 0);
2583 zero_obj->type = STACK_OBJ;
2585 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2586 zero_r8->type = STACK_R8;
2587 zero_r8->inst_p0 = &r8_0;
2589 /* add a check for this != NULL to inlined methods */
2590 if (is_virtual_call) {
2591 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2592 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2594 MONO_ADD_INS (bblock, ins);
2597 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2598 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2601 start_new_bblock = 0;
2604 if (cfg->method == method)
2605 real_offset = ip - header->code;
2607 real_offset = inline_offset;
2609 if (start_new_bblock) {
2610 bblock->cil_length = ip - bblock->cil_code;
2611 if (start_new_bblock == 2) {
2612 g_assert (ip == tblock->cil_code);
2614 GET_BBLOCK (cfg, bbhash, tblock, ip);
2616 bblock->next_bb = tblock;
2618 start_new_bblock = 0;
2619 for (i = 0; i < bblock->in_scount; ++i) {
2620 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2624 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2625 link_bblock (cfg, bblock, tblock);
2626 if (sp != stack_start) {
2627 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2630 bblock->next_bb = tblock;
2632 for (i = 0; i < bblock->in_scount; ++i) {
2633 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2639 if (cfg->verbose_level > 3)
2640 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2647 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2648 ins->cil_code = ip++;
2649 MONO_ADD_INS (bblock, ins);
2655 CHECK_STACK_OVF (1);
2656 n = (*ip)-CEE_LDARG_0;
2657 NEW_ARGLOAD (cfg, ins, n);
2658 ins->cil_code = ip++;
2665 CHECK_STACK_OVF (1);
2666 n = (*ip)-CEE_LDLOC_0;
2667 NEW_LOCLOAD (cfg, ins, n);
2668 ins->cil_code = ip++;
2676 n = (*ip)-CEE_STLOC_0;
2678 handle_loaded_temps (cfg, bblock, stack_start, sp);
2679 NEW_LOCSTORE (cfg, ins, n, *sp);
2681 if (ins->opcode == CEE_STOBJ) {
2682 NEW_LOCLOADA (cfg, ins, n);
2683 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2685 MONO_ADD_INS (bblock, ins);
2690 CHECK_STACK_OVF (1);
2691 NEW_ARGLOAD (cfg, ins, ip [1]);
2697 CHECK_STACK_OVF (1);
2698 NEW_ARGLOADA (cfg, ins, ip [1]);
2706 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2707 handle_loaded_temps (cfg, bblock, stack_start, sp);
2709 if (ins->opcode == CEE_STOBJ) {
2710 NEW_ARGLOADA (cfg, ins, ip [1]);
2711 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2713 MONO_ADD_INS (bblock, ins);
2717 CHECK_STACK_OVF (1);
2718 NEW_LOCLOAD (cfg, ins, ip [1]);
2724 CHECK_STACK_OVF (1);
2725 NEW_LOCLOADA (cfg, ins, ip [1]);
2733 handle_loaded_temps (cfg, bblock, stack_start, sp);
2734 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2736 if (ins->opcode == CEE_STOBJ) {
2737 NEW_LOCLOADA (cfg, ins, ip [1]);
2738 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2740 MONO_ADD_INS (bblock, ins);
2745 CHECK_STACK_OVF (1);
2746 NEW_PCONST (cfg, ins, NULL);
2748 ins->type = STACK_OBJ;
2753 CHECK_STACK_OVF (1);
2754 NEW_ICONST (cfg, ins, -1);
2768 CHECK_STACK_OVF (1);
2769 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2775 CHECK_STACK_OVF (1);
2777 NEW_ICONST (cfg, ins, *((signed char*)ip));
2783 CHECK_STACK_OVF (1);
2784 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2790 CHECK_STACK_OVF (1);
2791 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2793 ins->type = STACK_I8;
2795 ins->inst_l = (gint64)read64 (ip);
2800 float *f = g_malloc (sizeof (float));
2801 CHECK_STACK_OVF (1);
2802 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2803 ins->type = STACK_R8;
2812 double *d = g_malloc (sizeof (double));
2813 CHECK_STACK_OVF (1);
2814 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2815 ins->type = STACK_R8;
2824 MonoInst *temp, *store;
2826 CHECK_STACK_OVF (1);
2831 * small optimization: if the loaded value was from a local already,
2832 * just load it twice.
2834 if (ins->ssa_op == MONO_SSA_LOAD &&
2835 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2837 MONO_INST_NEW (cfg, temp, 0);
2839 temp->cil_code = ip;
2842 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2843 temp->cil_code = ip;
2844 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2845 store->cil_code = ip;
2846 MONO_ADD_INS (bblock, store);
2847 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2850 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2860 MONO_INST_NEW (cfg, ins, CEE_POP);
2861 MONO_ADD_INS (bblock, ins);
2862 ins->cil_code = ip++;
2867 if (stack_start != sp)
2869 MONO_INST_NEW (cfg, ins, CEE_JMP);
2870 token = read32 (ip + 1);
2871 /* FIXME: check the signature matches */
2872 cmethod = mono_get_method (image, token, NULL);
2873 ins->inst_p0 = cmethod;
2874 MONO_ADD_INS (bblock, ins);
2876 start_new_bblock = 1;
2880 case CEE_CALLVIRT: {
2881 MonoInst *addr = NULL;
2882 MonoMethodSignature *fsig = NULL;
2883 int temp, array_rank = 0;
2884 int virtual = *ip == CEE_CALLVIRT;
2886 token = read32 (ip + 1);
2888 if (*ip == CEE_CALLI) {
2893 if (method->wrapper_type != MONO_WRAPPER_NONE)
2894 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2896 fsig = mono_metadata_parse_signature (image, token);
2898 n = fsig->param_count + fsig->hasthis;
2901 if (method->wrapper_type != MONO_WRAPPER_NONE) {
2902 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2904 cmethod = mono_get_method (image, token, NULL);
2907 if (!cmethod->klass->inited)
2908 mono_class_init (cmethod->klass);
2910 if (cmethod->signature->pinvoke) {
2911 #ifdef MONO_USE_EXC_TABLES
2912 if (mono_method_blittable (cmethod)) {
2913 fsig = cmethod->signature;
2916 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2917 fsig = wrapper->signature;
2918 #ifdef MONO_USE_EXC_TABLES
2922 fsig = mono_method_get_signature (cmethod, image, token);
2925 n = fsig->param_count + fsig->hasthis;
2927 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2928 cmethod->klass->parent == mono_defaults.array_class) {
2929 array_rank = cmethod->klass->rank;
2932 if (cmethod->string_ctor)
2933 g_assert_not_reached ();
2939 //g_assert (!virtual || fsig->hasthis);
2943 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
2946 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
2948 for (i = 0; i < n; ++i) {
2949 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2951 MONO_ADD_INS (bblock, ins);
2953 MONO_INST_NEW (cfg, ins, CEE_JMP);
2955 ins->inst_p0 = cmethod;
2956 MONO_ADD_INS (bblock, ins);
2957 start_new_bblock = 1;
2958 /* skip CEE_RET as well */
2963 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2966 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2967 MONO_ADD_INS (bblock, ins);
2969 type_to_eval_stack_type (fsig->ret, ins);
2978 handle_loaded_temps (cfg, bblock, stack_start, sp);
2980 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
2981 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2982 mono_method_check_inlining (cfg, cmethod) &&
2983 !g_list_find (dont_inline, cmethod)) {
2985 MonoBasicBlock *ebblock;
2987 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
2991 GET_BBLOCK (cfg, bbhash, bblock, ip);
2992 ebblock->next_bb = bblock;
2993 link_bblock (cfg, ebblock, bblock);
2995 if (!MONO_TYPE_IS_VOID (fsig->ret))
2998 /* indicates start of a new block, and triggers a load of all
2999 stack arguments at bb boundarie */
3002 inline_costs += costs;
3007 inline_costs += 10 * num_calls++;
3009 /* tail recursion elimination */
3010 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
3011 gboolean has_vtargs = FALSE;
3014 /* keep it simple */
3015 for (i = fsig->param_count - 1; i >= 0; i--) {
3016 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3021 for (i = 0; i < n; ++i) {
3022 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3024 MONO_ADD_INS (bblock, ins);
3026 MONO_INST_NEW (cfg, ins, CEE_BR);
3028 MONO_ADD_INS (bblock, ins);
3029 tblock = start_bblock->out_bb [0];
3030 link_bblock (cfg, bblock, tblock);
3031 ins->inst_target_bb = tblock;
3032 start_new_bblock = 1;
3035 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3036 /* just create a dummy - the value is never used */
3037 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3038 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3046 if (*ip == CEE_CALLI) {
3048 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3049 NEW_TEMPLOAD (cfg, *sp, temp);
3053 } else if (array_rank) {
3056 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3057 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3058 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3060 if (ins->opcode == CEE_STOBJ) {
3061 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3063 MONO_ADD_INS (bblock, ins);
3066 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3067 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3068 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3072 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3073 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3076 g_assert_not_reached ();
3080 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3081 /* no need to spill */
3082 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3085 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3086 NEW_TEMPLOAD (cfg, *sp, temp);
3096 if (cfg->method != method) {
3097 /* return from inlined methode */
3102 //g_assert (returnvar != -1);
3103 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3104 store->cil_code = sp [0]->cil_code;
3105 if (store->opcode == CEE_STOBJ) {
3106 g_assert_not_reached ();
3107 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3108 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3110 MONO_ADD_INS (bblock, store);
3114 g_assert (!return_var);
3117 MONO_INST_NEW (cfg, ins, CEE_NOP);
3118 ins->opcode = mono_type_to_stind (method->signature->ret);
3119 if (ins->opcode == CEE_STOBJ) {
3120 NEW_RETLOADA (cfg, ins);
3121 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3123 ins->opcode = OP_SETRET;
3125 ins->inst_i0 = *sp;;
3126 ins->inst_i1 = NULL;
3127 MONO_ADD_INS (bblock, ins);
3131 if (sp != stack_start)
3133 MONO_INST_NEW (cfg, ins, CEE_BR);
3134 ins->cil_code = ip++;
3135 ins->inst_target_bb = end_bblock;
3136 MONO_ADD_INS (bblock, ins);
3137 link_bblock (cfg, bblock, end_bblock);
3138 start_new_bblock = 1;
3141 MONO_INST_NEW (cfg, ins, CEE_BR);
3142 ins->cil_code = ip++;
3143 MONO_ADD_INS (bblock, ins);
3144 target = ip + 1 + (signed char)(*ip);
3146 GET_BBLOCK (cfg, bbhash, tblock, target);
3147 link_bblock (cfg, bblock, tblock);
3148 CHECK_BBLOCK (target, ip, tblock);
3149 ins->inst_target_bb = tblock;
3150 if (sp != stack_start) {
3151 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3154 start_new_bblock = 1;
3160 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3161 ins->cil_code = ip++;
3162 target = ip + 1 + *(signed char*)ip;
3164 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3165 if (sp != stack_start) {
3166 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3182 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3183 ins->cil_code = ip++;
3184 target = ip + 1 + *(signed char*)ip;
3187 if (sp != stack_start) {
3188 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3194 MONO_INST_NEW (cfg, ins, CEE_BR);
3195 ins->cil_code = ip++;
3196 MONO_ADD_INS (bblock, ins);
3197 target = ip + 4 + (gint32)read32(ip);
3199 GET_BBLOCK (cfg, bbhash, tblock, target);
3200 link_bblock (cfg, bblock, tblock);
3201 CHECK_BBLOCK (target, ip, tblock);
3202 ins->inst_target_bb = tblock;
3203 if (sp != stack_start) {
3204 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3207 start_new_bblock = 1;
3213 MONO_INST_NEW (cfg, ins, *ip);
3214 ins->cil_code = ip++;
3215 target = ip + 4 + (gint32)read32(ip);
3217 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3218 if (sp != stack_start) {
3219 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3235 MONO_INST_NEW (cfg, ins, *ip);
3236 ins->cil_code = ip++;
3237 target = ip + 4 + (gint32)read32(ip);
3240 if (sp != stack_start) {
3241 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3248 n = read32 (ip + 1);
3249 MONO_INST_NEW (cfg, ins, *ip);
3251 ins->inst_left = *sp;
3252 if (ins->inst_left->type != STACK_I4) goto unverified;
3255 target = ip + n * sizeof (guint32);
3256 MONO_ADD_INS (bblock, ins);
3257 GET_BBLOCK (cfg, bbhash, tblock, target);
3258 link_bblock (cfg, bblock, tblock);
3259 ins->klass = GUINT_TO_POINTER (n);
3260 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3261 ins->inst_many_bb [n] = tblock;
3263 for (i = 0; i < n; ++i) {
3264 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3265 link_bblock (cfg, bblock, tblock);
3266 ins->inst_many_bb [i] = tblock;
3269 /* FIXME: handle stack args */
3284 MONO_INST_NEW (cfg, ins, *ip);
3289 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3290 ins->flags |= ins_flag;
3302 MONO_INST_NEW (cfg, ins, *ip);
3303 ins->cil_code = ip++;
3305 handle_loaded_temps (cfg, bblock, stack_start, sp);
3306 MONO_ADD_INS (bblock, ins);
3307 ins->inst_i0 = sp [0];
3308 ins->inst_i1 = sp [1];
3309 ins->flags |= ins_flag;
3340 case CEE_CONV_OVF_I8:
3341 case CEE_CONV_OVF_U8:
3347 case CEE_CONV_OVF_I4:
3348 case CEE_CONV_OVF_I1:
3349 case CEE_CONV_OVF_I2:
3350 case CEE_CONV_OVF_I:
3351 case CEE_CONV_OVF_U:
3354 if (sp [-1]->type == STACK_R8) {
3355 ADD_UNOP (CEE_CONV_OVF_I8);
3363 case CEE_CONV_OVF_U1:
3364 case CEE_CONV_OVF_U2:
3365 case CEE_CONV_OVF_U4:
3368 if (sp [-1]->type == STACK_R8) {
3369 ADD_UNOP (CEE_CONV_OVF_U8);
3377 case CEE_CONV_OVF_I1_UN:
3378 case CEE_CONV_OVF_I2_UN:
3379 case CEE_CONV_OVF_I4_UN:
3380 case CEE_CONV_OVF_I8_UN:
3381 case CEE_CONV_OVF_U1_UN:
3382 case CEE_CONV_OVF_U2_UN:
3383 case CEE_CONV_OVF_U4_UN:
3384 case CEE_CONV_OVF_U8_UN:
3385 case CEE_CONV_OVF_I_UN:
3386 case CEE_CONV_OVF_U_UN:
3392 g_error ("opcode 0x%02x not handled", *ip);
3395 MonoInst *iargs [3];
3398 token = read32 (ip + 1);
3399 if (method->wrapper_type != MONO_WRAPPER_NONE)
3400 klass = mono_method_get_wrapper_data (method, token);
3402 klass = mono_class_get (image, token);
3404 mono_class_init (klass);
3405 n = mono_class_value_size (klass, NULL);
3406 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3407 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3408 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3410 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3411 copy->inst_left = iargs [0];
3412 copy->inst_right = *sp;
3413 copy->cil_code = ip;
3415 MONO_ADD_INS (bblock, copy);
3418 NEW_ICONST (cfg, iargs [2], n);
3419 iargs [2]->cil_code = ip;
3421 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3423 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3430 CHECK_STACK_OVF (1);
3431 n = read32 (ip + 1);
3433 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3435 MonoInst *iargs [1];
3437 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3438 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3439 NEW_TEMPLOAD (cfg, *sp, temp);
3443 if (mono_compile_aot) {
3444 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3447 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3449 MonoInst *iargs [3];
3450 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3451 NEW_IMAGECONST (cfg, iargs [1], image);
3452 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3453 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3454 NEW_TEMPLOAD (cfg, *sp, temp);
3455 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3457 NEW_PCONST (cfg, ins, NULL);
3459 ins->type = STACK_OBJ;
3460 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3469 MonoInst *iargs [2];
3470 MonoMethodSignature *fsig;
3473 token = read32 (ip + 1);
3474 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3475 cmethod = mono_method_get_wrapper_data (method, token);
3477 cmethod = mono_get_method (image, token, NULL);
3478 fsig = mono_method_get_signature (cmethod, image, token);
3480 mono_class_init (cmethod->klass);
3482 n = fsig->param_count;
3485 /* move the args to allow room for 'this' in the first position */
3491 handle_loaded_temps (cfg, bblock, stack_start, sp);
3494 if (cmethod->klass->parent == mono_defaults.array_class) {
3495 NEW_METHODCONST (cfg, *sp, cmethod);
3496 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3498 } else if (cmethod->string_ctor) {
3499 /* we simply pass a null pointer */
3500 NEW_PCONST (cfg, *sp, NULL);
3501 /* now call the string ctor */
3502 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3504 if (cmethod->klass->valuetype) {
3505 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3506 temp = iargs [0]->inst_c0;
3507 NEW_TEMPLOADA (cfg, *sp, temp);
3509 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3510 NEW_DOMAINCONST (cfg, iargs [0]);
3511 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3513 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3515 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3516 NEW_PCONST (cfg, iargs [0], vtable);
3517 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3519 NEW_TEMPLOAD (cfg, *sp, temp);
3522 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3523 mono_method_check_inlining (cfg, cmethod) &&
3524 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3525 !g_list_find (dont_inline, cmethod)) {
3527 MonoBasicBlock *ebblock;
3528 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3533 GET_BBLOCK (cfg, bbhash, bblock, ip);
3534 ebblock->next_bb = bblock;
3535 link_bblock (cfg, ebblock, bblock);
3537 NEW_TEMPLOAD (cfg, *sp, temp);
3540 /* indicates start of a new block, and triggers a load
3541 of all stack arguments at bb boundarie */
3544 inline_costs += costs;
3548 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3551 /* now call the actual ctor */
3552 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3556 NEW_TEMPLOAD (cfg, *sp, temp);
3565 MONO_INST_NEW (cfg, ins, *ip);
3567 klass = mono_class_get (image, read32 (ip + 1));
3568 mono_class_init (klass);
3569 ins->type = STACK_OBJ;
3570 ins->inst_left = *sp;
3571 ins->inst_newa_class = klass;
3577 MonoInst *add, *vtoffset;
3581 token = read32 (ip + 1);
3582 if (method->wrapper_type != MONO_WRAPPER_NONE)
3583 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3585 klass = mono_class_get (image, token);
3586 mono_class_init (klass);
3589 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3590 ins->type = STACK_OBJ;
3591 ins->inst_left = *sp;
3593 ins->inst_newa_class = klass;
3596 MONO_INST_NEW (cfg, add, CEE_ADD);
3597 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3598 add->inst_left = ins;
3599 add->inst_right = vtoffset;
3600 add->type = STACK_MP;
3608 MONO_INST_NEW (cfg, ins, *ip);
3610 klass = mono_class_get (image, read32 (ip + 1));
3611 mono_class_init (klass);
3612 ins->type = STACK_OBJ;
3613 ins->inst_left = *sp;
3615 ins->inst_newa_class = klass;
3622 MONO_INST_NEW (cfg, ins, *ip);
3624 ins->inst_left = *sp;
3625 ins->cil_code = ip++;
3626 MONO_ADD_INS (bblock, ins);
3628 start_new_bblock = 1;
3633 MonoInst *offset_ins;
3634 MonoClassField *field;
3635 MonoBasicBlock *ebblock;
3639 if (*ip == CEE_STFLD) {
3646 // FIXME: enable this test later.
3647 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3649 token = read32 (ip + 1);
3650 field = mono_field_from_token (image, token, &klass);
3651 mono_class_init (klass);
3653 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3654 /* FIXME: mark instructions for use in SSA */
3655 if (*ip == CEE_STFLD) {
3656 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3657 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3658 MonoInst *iargs [5];
3661 NEW_CLASSCONST (cfg, iargs [1], klass);
3662 NEW_FIELDCONST (cfg, iargs [2], field);
3663 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3667 if (cfg->opt & MONO_OPT_INLINE) {
3668 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3669 iargs, ip, real_offset, dont_inline, &ebblock);
3670 g_assert (costs > 0);
3675 GET_BBLOCK (cfg, bbhash, bblock, ip);
3676 ebblock->next_bb = bblock;
3677 link_bblock (cfg, ebblock, bblock);
3679 /* indicates start of a new block, and triggers a load
3680 of all stack arguments at bb boundarie */
3683 inline_costs += costs;
3686 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3690 NEW_ICONST (cfg, offset_ins, foffset);
3691 MONO_INST_NEW (cfg, ins, CEE_ADD);
3693 ins->inst_left = *sp;
3694 ins->inst_right = offset_ins;
3695 ins->type = STACK_MP;
3697 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3698 store->cil_code = ip;
3699 store->inst_left = ins;
3700 store->inst_right = sp [1];
3701 handle_loaded_temps (cfg, bblock, stack_start, sp);
3702 store->flags |= ins_flag;
3704 if (store->opcode == CEE_STOBJ) {
3705 handle_stobj (cfg, bblock, ins, sp [1], ip,
3706 mono_class_from_mono_type (field->type), FALSE, FALSE);
3708 MONO_ADD_INS (bblock, store);
3711 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3712 /* fixme: we need to inline that call somehow */
3713 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3714 MonoInst *iargs [4];
3718 NEW_CLASSCONST (cfg, iargs [1], klass);
3719 NEW_FIELDCONST (cfg, iargs [2], field);
3720 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3721 if (cfg->opt & MONO_OPT_INLINE) {
3722 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3723 iargs, ip, real_offset, dont_inline, &ebblock);
3724 g_assert (costs > 0);
3729 GET_BBLOCK (cfg, bbhash, bblock, ip);
3730 ebblock->next_bb = bblock;
3731 link_bblock (cfg, ebblock, bblock);
3733 temp = iargs [0]->inst_i0->inst_c0;
3735 if (*ip == CEE_LDFLDA) {
3736 /* not sure howto handle this */
3737 NEW_TEMPLOADA (cfg, *sp, temp);
3739 NEW_TEMPLOAD (cfg, *sp, temp);
3743 /* indicates start of a new block, and triggers a load of
3744 all stack arguments at bb boundarie */
3747 inline_costs += costs;
3750 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3751 if (*ip == CEE_LDFLDA) {
3752 /* not sure howto handle this */
3753 NEW_TEMPLOADA (cfg, *sp, temp);
3755 NEW_TEMPLOAD (cfg, *sp, temp);
3760 NEW_ICONST (cfg, offset_ins, foffset);
3761 MONO_INST_NEW (cfg, ins, CEE_ADD);
3763 ins->inst_left = *sp;
3764 ins->inst_right = offset_ins;
3765 ins->type = STACK_MP;
3767 if (*ip == CEE_LDFLDA) {
3771 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3772 type_to_eval_stack_type (field->type, load);
3773 load->cil_code = ip;
3774 load->inst_left = ins;
3775 load->flags |= ins_flag;
3787 MonoClassField *field;
3789 token = read32 (ip + 1);
3791 field = mono_field_from_token (image, token, &klass);
3792 mono_class_init (klass);
3794 handle_loaded_temps (cfg, bblock, stack_start, sp);
3796 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3798 MonoInst *iargs [2];
3799 g_assert (field->parent);
3800 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3801 NEW_FIELDCONST (cfg, iargs [1], field);
3802 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3803 NEW_TEMPLOAD (cfg, ins, temp);
3807 vtable = mono_class_vtable (cfg->domain, klass);
3808 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3809 if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) {
3810 MonoInst *iargs [1];
3811 NEW_PCONST (cfg, iargs [0], vtable);
3812 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
3813 if (cfg->verbose_level > 2)
3814 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
3816 mono_runtime_class_init (vtable);
3818 addr = (char*)vtable->data + field->offset;
3819 NEW_PCONST (cfg, ins, addr);
3823 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
3824 * This could be later optimized to do just a couple of
3825 * memory dereferences with constant offsets.
3828 MonoInst *iargs [1];
3829 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
3830 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
3831 NEW_TEMPLOAD (cfg, ins, temp);
3835 /* FIXME: mark instructions for use in SSA */
3836 if (*ip == CEE_LDSFLDA) {
3838 } else if (*ip == CEE_STSFLD) {
3842 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3843 store->cil_code = ip;
3844 store->inst_left = ins;
3845 store->inst_right = sp [0];
3846 store->flags |= ins_flag;
3849 if (store->opcode == CEE_STOBJ) {
3850 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3852 MONO_ADD_INS (bblock, store);
3855 CHECK_STACK_OVF (1);
3856 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3857 type_to_eval_stack_type (field->type, load);
3858 load->cil_code = ip;
3859 load->inst_left = ins;
3861 load->flags |= ins_flag;
3863 /* fixme: dont see the problem why this does not work */
3864 //cfg->disable_aot = TRUE;
3872 token = read32 (ip + 1);
3873 if (method->wrapper_type != MONO_WRAPPER_NONE)
3874 klass = mono_method_get_wrapper_data (method, token);
3876 klass = mono_class_get (image, token);
3877 mono_class_init (klass);
3878 n = mono_type_to_stind (&klass->byval_arg);
3879 if (n == CEE_STOBJ) {
3880 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3882 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
3884 MONO_INST_NEW (cfg, store, n);
3885 store->cil_code = ip;
3886 store->inst_left = sp [0];
3887 store->inst_right = sp [1];
3888 store->flags |= ins_flag;
3889 MONO_ADD_INS (bblock, store);
3896 MonoInst *iargs [2];
3897 MonoInst *load, *vtoffset, *add, *val, *vstore;
3902 token = read32 (ip + 1);
3903 if (method->wrapper_type != MONO_WRAPPER_NONE)
3904 klass = mono_method_get_wrapper_data (method, token);
3906 klass = mono_class_get (image, token);
3907 mono_class_init (klass);
3909 /* much like NEWOBJ */
3910 NEW_DOMAINCONST (cfg, iargs [0]);
3911 NEW_CLASSCONST (cfg, iargs [1], klass);
3913 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3914 NEW_TEMPLOAD (cfg, load, temp);
3915 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3916 MONO_INST_NEW (cfg, add, CEE_ADD);
3917 add->inst_left = load;
3918 add->inst_right = vtoffset;
3921 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3922 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3923 vstore->cil_code = ip;
3924 vstore->inst_left = add;
3925 vstore->inst_right = val;
3927 if (vstore->opcode == CEE_STOBJ) {
3928 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
3930 MONO_ADD_INS (bblock, vstore);
3932 NEW_TEMPLOAD (cfg, load, temp);
3940 MONO_INST_NEW (cfg, ins, *ip);
3944 token = read32 (ip + 1);
3946 /* allocate the domainvar - becaus this is used in decompose_foreach */
3947 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
3948 mono_get_domainvar (cfg);
3950 if (method->wrapper_type != MONO_WRAPPER_NONE)
3951 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3953 klass = mono_class_get (image, token);
3955 mono_class_init (klass);
3956 ins->inst_newa_class = klass;
3957 ins->inst_newa_len = *sp;
3958 ins->type = STACK_OBJ;
3965 MONO_INST_NEW (cfg, ins, *ip);
3966 ins->cil_code = ip++;
3968 ins->inst_left = *sp;
3969 ins->type = STACK_PTR;
3975 klass = mono_class_get (image, read32 (ip + 1));
3976 mono_class_init (klass);
3977 NEW_LDELEMA (cfg, ins, sp, klass);
3992 case CEE_LDELEM_REF: {
3996 * ldind.x (ldelema (array, index))
3997 * ldelema does the bounds check
4001 klass = array_access_to_klass (*ip);
4002 NEW_LDELEMA (cfg, load, sp, klass);
4003 load->cil_code = ip;
4004 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4006 ins->inst_left = load;
4008 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4018 case CEE_STELEM_R8: {
4022 * stind.x (ldelema (array, index), val)
4023 * ldelema does the bounds check
4027 klass = array_access_to_klass (*ip);
4028 NEW_LDELEMA (cfg, load, sp, klass);
4029 load->cil_code = ip;
4030 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4032 ins->inst_left = load;
4033 ins->inst_right = sp [2];
4035 handle_loaded_temps (cfg, bblock, stack_start, sp);
4036 MONO_ADD_INS (bblock, ins);
4037 /* FIXME: add the implicit STELEM_REF castclass */
4039 cfg->disable_ssa = TRUE;
4042 case CEE_STELEM_REF: {
4043 MonoInst *iargs [3];
4048 handle_loaded_temps (cfg, bblock, stack_start, sp);
4054 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4058 NEW_GROUP (cfg, group, sp [0], sp [1]);
4059 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4061 ins->inst_left = group;
4062 ins->inst_right = sp [2];
4063 MONO_ADD_INS (bblock, ins);
4068 cfg->disable_ssa = TRUE;
4071 case CEE_CKFINITE: {
4072 MonoInst *store, *temp;
4075 /* this instr. can throw exceptions as side effect,
4076 * so we cant eliminate dead code which contains CKFINITE opdodes.
4077 * Spilling to memory makes sure that we always perform
4081 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4083 ins->inst_left = sp [-1];
4084 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4086 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4087 store->cil_code = ip;
4088 MONO_ADD_INS (bblock, store);
4090 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4097 g_error ("opcode 0x%02x not handled", *ip);
4101 MonoClass *handle_class;
4103 CHECK_STACK_OVF (1);
4105 n = read32 (ip + 1);
4107 handle = mono_ldtoken (image, n, &handle_class);
4108 mono_class_init (handle_class);
4110 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4112 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4114 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4116 NEW_IMAGECONST (cfg, iargs [0], image);
4117 NEW_ICONST (cfg, iargs [1], n);
4118 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4119 NEW_TEMPLOAD (cfg, res, temp);
4120 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4121 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4122 MONO_ADD_INS (bblock, store);
4123 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4125 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4126 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4127 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4128 MonoClass *tclass = mono_class_from_mono_type (handle);
4129 mono_class_init (tclass);
4130 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4131 ins->type = STACK_OBJ;
4132 ins->klass = cmethod->klass;
4135 NEW_PCONST (cfg, ins, handle);
4136 ins->type = STACK_VTYPE;
4137 ins->klass = handle_class;
4153 case CEE_ADD_OVF_UN:
4155 case CEE_MUL_OVF_UN:
4157 case CEE_SUB_OVF_UN:
4162 case CEE_ENDFINALLY:
4163 /* FIXME: check stack state */
4164 MONO_INST_NEW (cfg, ins, *ip);
4165 MONO_ADD_INS (bblock, ins);
4166 ins->cil_code = ip++;
4167 start_new_bblock = 1;
4172 if (*ip == CEE_LEAVE) {
4173 target = ip + 5 + (gint32)read32(ip + 1);
4175 target = ip + 2 + (signed char)(ip [1]);
4178 /* empty the stack */
4179 while (sp != stack_start) {
4180 MONO_INST_NEW (cfg, ins, CEE_POP);
4184 MONO_ADD_INS (bblock, ins);
4187 /* fixme: call fault handler ? */
4189 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4191 for (tmp = handlers; tmp; tmp = tmp->next) {
4193 link_bblock (cfg, bblock, tblock);
4194 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4196 ins->inst_target_bb = tblock;
4197 MONO_ADD_INS (bblock, ins);
4199 g_list_free (handlers);
4202 MONO_INST_NEW (cfg, ins, CEE_BR);
4204 MONO_ADD_INS (bblock, ins);
4205 GET_BBLOCK (cfg, bbhash, tblock, target);
4206 link_bblock (cfg, bblock, tblock);
4207 CHECK_BBLOCK (target, ip, tblock);
4208 ins->inst_target_bb = tblock;
4209 start_new_bblock = 1;
4211 if (*ip == CEE_LEAVE)
4220 MONO_INST_NEW (cfg, ins, *ip);
4222 handle_loaded_temps (cfg, bblock, stack_start, sp);
4223 MONO_ADD_INS (bblock, ins);
4224 ins->cil_code = ip++;
4225 ins->inst_i0 = sp [0];
4226 ins->inst_i1 = sp [1];
4234 /* trampoline mono specific opcodes */
4235 case MONO_CUSTOM_PREFIX: {
4237 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4241 case CEE_MONO_FUNC1: {
4243 gpointer func = NULL;
4248 case MONO_MARSHAL_CONV_STR_LPWSTR:
4249 func = mono_string_to_utf16;
4251 case MONO_MARSHAL_CONV_LPWSTR_STR:
4252 func = mono_string_from_utf16;
4254 case MONO_MARSHAL_CONV_LPSTR_STR:
4255 func = mono_string_new_wrapper;
4257 case MONO_MARSHAL_CONV_STR_LPTSTR:
4258 case MONO_MARSHAL_CONV_STR_LPSTR:
4259 func = mono_string_to_utf8;
4261 case MONO_MARSHAL_CONV_STR_BSTR:
4262 func = mono_string_to_bstr;
4264 case MONO_MARSHAL_CONV_STR_TBSTR:
4265 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4266 func = mono_string_to_ansibstr;
4268 case MONO_MARSHAL_CONV_SB_LPSTR:
4269 func = mono_string_builder_to_utf8;
4271 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4272 func = mono_array_to_savearray;
4274 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4275 func = mono_array_to_lparray;
4277 case MONO_MARSHAL_CONV_DEL_FTN:
4278 func = mono_delegate_to_ftnptr;
4280 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4281 func = mono_marshal_string_array;
4284 g_warning ("unknown conversion %d\n", ip [2]);
4285 g_assert_not_reached ();
4288 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4289 NEW_TEMPLOAD (cfg, *sp, temp);
4293 inline_costs += 10 * num_calls++;
4296 case CEE_MONO_PROC2: {
4297 gpointer func = NULL;
4302 case MONO_MARSHAL_CONV_LPSTR_SB:
4303 func = mono_string_utf8_to_builder;
4305 case MONO_MARSHAL_FREE_ARRAY:
4306 func = mono_marshal_free_array;
4309 g_assert_not_reached ();
4312 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4314 inline_costs += 10 * num_calls++;
4317 case CEE_MONO_PROC3: {
4318 gpointer func = NULL;
4323 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4324 func = mono_string_to_byvalstr;
4326 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4327 func = mono_string_to_byvalwstr;
4330 g_assert_not_reached ();
4333 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4335 inline_costs += 10 * num_calls++;
4341 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4343 inline_costs += 10 * num_calls++;
4345 case CEE_MONO_LDPTR:
4346 CHECK_STACK_OVF (1);
4347 token = read32 (ip + 2);
4348 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4352 inline_costs += 10 * num_calls++;
4354 case CEE_MONO_VTADDR:
4357 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4359 ins->type = STACK_MP;
4360 ins->inst_left = *sp;
4364 case CEE_MONO_NEWOBJ: {
4365 MonoInst *iargs [2];
4367 CHECK_STACK_OVF (1);
4368 token = read32 (ip + 2);
4369 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4370 mono_class_init (klass);
4371 NEW_DOMAINCONST (cfg, iargs [0]);
4372 NEW_CLASSCONST (cfg, iargs [1], klass);
4373 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4374 NEW_TEMPLOAD (cfg, *sp, temp);
4377 inline_costs += 10 * num_calls++;
4380 case CEE_MONO_OBJADDR:
4383 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4385 ins->type = STACK_MP;
4386 ins->inst_left = *sp;
4390 case CEE_MONO_LDNATIVEOBJ:
4392 token = read32 (ip + 2);
4393 klass = mono_method_get_wrapper_data (method, token);
4394 g_assert (klass->valuetype);
4395 mono_class_init (klass);
4396 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4400 case CEE_MONO_RETOBJ:
4401 g_assert (cfg->ret);
4402 g_assert (method->signature->pinvoke);
4406 token = read32 (ip + 2);
4407 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4409 NEW_RETLOADA (cfg, ins);
4410 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4412 if (sp != stack_start)
4415 MONO_INST_NEW (cfg, ins, CEE_BR);
4417 ins->inst_target_bb = end_bblock;
4418 MONO_ADD_INS (bblock, ins);
4419 link_bblock (cfg, bblock, end_bblock);
4420 start_new_bblock = 1;
4424 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4432 /* somewhat similar to LDTOKEN */
4433 MonoInst *addr, *vtvar;
4435 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4437 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4438 addr->cil_code = ip;
4439 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4441 ins->inst_left = addr;
4442 MONO_ADD_INS (bblock, ins);
4443 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4456 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4457 MONO_INST_NEW (cfg, ins, cmp->opcode);
4459 cmp->inst_i0 = sp [0];
4460 cmp->inst_i1 = sp [1];
4464 cmp->opcode = OP_COMPARE;
4466 ins->type = STACK_I4;
4476 CHECK_STACK_OVF (1);
4477 n = read32 (ip + 2);
4478 if (method->wrapper_type != MONO_WRAPPER_NONE)
4479 cmethod = mono_method_get_wrapper_data (method, n);
4481 cmethod = mono_get_method (image, n, NULL);
4484 * We can't do this in mono_ldftn, since it is used in
4485 * the synchronized wrapper, leading to an infinite loop.
4487 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4488 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4491 mono_class_init (cmethod->klass);
4492 handle_loaded_temps (cfg, bblock, stack_start, sp);
4494 NEW_METHODCONST (cfg, argconst, cmethod);
4495 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4496 NEW_TEMPLOAD (cfg, *sp, temp);
4500 inline_costs += 10 * num_calls++;
4503 case CEE_LDVIRTFTN: {
4508 n = read32 (ip + 2);
4509 if (method->wrapper_type != MONO_WRAPPER_NONE)
4510 cmethod = mono_method_get_wrapper_data (method, n);
4512 cmethod = mono_get_method (image, n, NULL);
4514 mono_class_init (cmethod->klass);
4515 handle_loaded_temps (cfg, bblock, stack_start, sp);
4519 NEW_METHODCONST (cfg, args [1], cmethod);
4520 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4521 NEW_TEMPLOAD (cfg, *sp, temp);
4525 inline_costs += 10 * num_calls++;
4529 CHECK_STACK_OVF (1);
4530 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4536 CHECK_STACK_OVF (1);
4537 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4545 handle_loaded_temps (cfg, bblock, stack_start, sp);
4546 n = read16 (ip + 2);
4547 NEW_ARGSTORE (cfg, ins, n, *sp);
4549 if (ins->opcode == CEE_STOBJ) {
4550 NEW_ARGLOADA (cfg, ins, n);
4551 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4553 MONO_ADD_INS (bblock, ins);
4557 CHECK_STACK_OVF (1);
4558 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4564 CHECK_STACK_OVF (1);
4565 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4573 n = read16 (ip + 2);
4574 handle_loaded_temps (cfg, bblock, stack_start, sp);
4575 NEW_LOCSTORE (cfg, ins, n, *sp);
4577 if (ins->opcode == CEE_STOBJ) {
4578 NEW_LOCLOADA (cfg, ins, n);
4579 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4581 MONO_ADD_INS (bblock, ins);
4588 if (sp != stack_start)
4590 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4591 ins->inst_left = *sp;
4594 if (header->init_locals)
4595 ins->flags |= MONO_INST_INIT;
4599 /* FIXME: set init flag if locals init is set in this method */
4601 case CEE_ENDFILTER: {
4602 MonoExceptionClause *clause, *nearest;
4603 int cc, nearest_num;
4607 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4609 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4610 ins->inst_left = *sp;
4612 MONO_ADD_INS (bblock, ins);
4613 start_new_bblock = 1;
4617 for (cc = 0; cc < header->num_clauses; ++cc) {
4618 clause = &header->clauses [cc];
4619 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4620 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4626 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4630 case CEE_UNALIGNED_:
4631 ins_flag |= MONO_INST_UNALIGNED;
4635 ins_flag |= MONO_INST_VOLATILE;
4639 ins_flag |= MONO_INST_TAILCALL;
4645 token = read32 (ip + 2);
4646 if (method->wrapper_type != MONO_WRAPPER_NONE)
4647 klass = mono_method_get_wrapper_data (method, token);
4649 klass = mono_class_get (image, token);
4650 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4656 MonoInst *iargs [3];
4662 handle_loaded_temps (cfg, bblock, stack_start, sp);
4663 if (ip [1] == CEE_CPBLK) {
4664 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4666 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4674 /* FIXME: check we are in a catch handler */
4675 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4676 load->cil_code = ip;
4677 MONO_INST_NEW (cfg, ins, CEE_THROW);
4678 ins->inst_left = load;
4680 MONO_ADD_INS (bblock, ins);
4682 start_new_bblock = 1;
4687 CHECK_STACK_OVF (1);
4688 token = read32 (ip + 2);
4689 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4690 MonoType *type = mono_type_create_from_typespec (image, token);
4691 token = mono_type_size (type, &align);
4692 mono_metadata_free_type (type);
4694 MonoClass *szclass = mono_class_get (image, token);
4695 mono_class_init (szclass);
4696 token = mono_class_value_size (szclass, &align);
4698 NEW_ICONST (cfg, ins, token);
4703 case CEE_REFANYTYPE:
4704 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4707 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4712 g_error ("opcode 0x%02x not handled", *ip);
4715 if (start_new_bblock != 1)
4718 bblock->cil_length = ip - bblock->cil_code;
4719 bblock->next_bb = end_bblock;
4720 link_bblock (cfg, bblock, end_bblock);
4722 if (cfg->method == method && cfg->domainvar) {
4726 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4727 call->signature = helper_sig_domain_get;
4728 call->inst.type = STACK_PTR;
4729 call->fptr = mono_domain_get;
4730 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4732 MONO_ADD_INS (init_localsbb, store);
4735 if (header->init_locals) {
4737 for (i = 0; i < header->num_locals; ++i) {
4738 int t = header->locals [i]->type;
4739 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4740 t = header->locals [i]->data.klass->enum_basetype->type;
4741 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4742 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4743 NEW_ICONST (cfg, ins, 0);
4744 NEW_LOCSTORE (cfg, store, i, ins);
4745 MONO_ADD_INS (init_localsbb, store);
4746 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4747 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4748 ins->type = STACK_I8;
4750 NEW_LOCSTORE (cfg, store, i, ins);
4751 MONO_ADD_INS (init_localsbb, store);
4752 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4753 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4754 ins->type = STACK_R8;
4755 ins->inst_p0 = (void*)&r8_0;
4756 NEW_LOCSTORE (cfg, store, i, ins);
4757 MONO_ADD_INS (init_localsbb, store);
4758 } else if (t == MONO_TYPE_VALUETYPE) {
4759 NEW_LOCLOADA (cfg, ins, i);
4760 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4763 NEW_PCONST (cfg, ins, NULL);
4764 NEW_LOCSTORE (cfg, store, i, ins);
4765 MONO_ADD_INS (init_localsbb, store);
4771 /* resolve backward branches in the middle of an existing basic block */
4772 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4774 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4775 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4776 if (tblock != start_bblock) {
4778 split_bblock (cfg, tblock, bblock);
4779 l = bblock->cil_code - header->code;
4780 bblock->cil_length = tblock->cil_length - l;
4781 tblock->cil_length = l;
4783 g_print ("recheck failed.\n");
4787 /* we compute regions here, because the length of filter clauses is not known in advance.
4788 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4789 if (cfg->method == method) {
4791 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4792 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4793 if (cfg->verbose_level > 2)
4794 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4797 g_hash_table_destroy (bbhash);
4800 dont_inline = g_list_remove (dont_inline, method);
4801 return inline_costs;
4804 if (cfg->method != method)
4805 g_hash_table_destroy (bbhash);
4806 dont_inline = g_list_remove (dont_inline, method);
4810 if (cfg->method != method)
4811 g_hash_table_destroy (bbhash);
4812 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4813 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4814 dont_inline = g_list_remove (dont_inline, method);
4819 mono_print_tree (MonoInst *tree) {
4825 arity = mono_burg_arity [tree->opcode];
4827 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4829 switch (tree->opcode) {
4831 printf ("[%d]", tree->inst_c0);
4834 printf ("[%lld]", tree->inst_l);
4837 printf ("[%f]", *(double*)tree->inst_p0);
4840 printf ("[%f]", *(float*)tree->inst_p0);
4844 printf ("[%d]", tree->inst_c0);
4847 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4850 printf ("[%s]", mono_arch_regname (tree->dreg));
4853 printf ("[%s]", tree->inst_newa_class->name);
4854 mono_print_tree (tree->inst_newa_len);
4865 case OP_VOIDCALLVIRT: {
4866 MonoCallInst *call = (MonoCallInst*)tree;
4868 printf ("[%s]", call->method->name);
4873 printf ("[%d (", tree->inst_c0);
4874 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4877 printf ("%d", tree->inst_phi_args [i + 1]);
4889 printf ("[B%d]", tree->inst_target_bb->block_num);
4899 case OP_VOIDCALL_REG:
4900 mono_print_tree (tree->inst_left);
4912 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4913 mono_print_tree (tree->inst_left);
4917 mono_print_tree (tree->inst_left);
4919 mono_print_tree (tree->inst_right);
4929 create_helper_signature (void)
4931 /* FIXME: set call conv */
4932 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
4933 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4934 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
4935 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
4936 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
4937 helper_sig_newarr->pinvoke = 1;
4939 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
4940 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4941 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
4942 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
4943 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
4944 helper_sig_newarr_specific->pinvoke = 1;
4946 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
4947 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4948 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
4949 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
4950 helper_sig_object_new->pinvoke = 1;
4952 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
4953 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4954 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
4955 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
4956 helper_sig_object_new_specific->pinvoke = 1;
4958 /* void* mono_method_compile (MonoMethod*) */
4959 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4960 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
4961 helper_sig_compile->pinvoke = 1;
4963 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
4964 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4965 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
4966 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
4967 helper_sig_compile_virt->pinvoke = 1;
4969 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
4970 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4971 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
4972 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
4973 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
4974 helper_sig_ldstr->pinvoke = 1;
4976 /* MonoDomain *mono_domain_get (void) */
4977 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4978 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
4979 helper_sig_domain_get->pinvoke = 1;
4981 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
4982 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4983 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
4984 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
4985 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
4986 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
4987 helper_sig_stelem_ref->pinvoke = 1;
4989 /* long amethod (long, long) */
4990 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4991 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
4992 &mono_defaults.int64_class->byval_arg;
4993 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
4994 helper_sig_long_long_long->pinvoke = 1;
4996 /* object amethod (intptr) */
4997 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4998 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4999 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5000 helper_sig_obj_ptr->pinvoke = 1;
5002 /* void amethod (intptr) */
5003 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5004 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5005 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5006 helper_sig_void_ptr->pinvoke = 1;
5008 /* void amethod (MonoObject *obj) */
5009 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5010 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5011 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5012 helper_sig_void_obj->pinvoke = 1;
5014 /* intptr amethod (void) */
5015 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5016 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5017 helper_sig_ptr_void->pinvoke = 1;
5019 /* void amethod (intptr, intptr) */
5020 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5021 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5022 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5023 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5024 helper_sig_void_ptr_ptr->pinvoke = 1;
5026 /* void amethod (intptr, intptr, intptr) */
5027 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5028 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5029 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5030 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5031 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5032 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5034 /* intptr amethod (intptr, intptr) */
5035 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5036 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5037 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5038 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5039 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5041 /* IntPtr amethod (object) */
5042 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5043 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5044 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5045 helper_sig_ptr_obj->pinvoke = 1;
5047 /* IntPtr amethod (int) */
5048 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5049 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5050 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5051 helper_sig_ptr_int->pinvoke = 1;
5053 /* long amethod (long, guint32) */
5054 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5055 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5056 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5057 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5058 helper_sig_long_long_int->pinvoke = 1;
5060 /* ulong amethod (double) */
5061 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5062 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5063 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5064 helper_sig_ulong_double->pinvoke = 1;
5066 /* long amethod (double) */
5067 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5068 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5069 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5070 helper_sig_long_double->pinvoke = 1;
5072 /* uint amethod (double) */
5073 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5074 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5075 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5076 helper_sig_uint_double->pinvoke = 1;
5078 /* int amethod (double) */
5079 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5080 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5081 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5082 helper_sig_int_double->pinvoke = 1;
5084 /* void initobj (intptr, int size) */
5085 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5086 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5087 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5088 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5089 helper_sig_initobj->pinvoke = 1;
5091 /* void memcpy (intptr, intptr, int size) */
5092 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5093 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5094 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5095 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5096 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5097 helper_sig_memcpy->pinvoke = 1;
5099 /* void memset (intptr, int val, int size) */
5100 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5101 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5102 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5103 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5104 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5105 helper_sig_memset->pinvoke = 1;
5108 static GHashTable *jit_icall_hash_name = NULL;
5109 static GHashTable *jit_icall_hash_addr = NULL;
5112 mono_find_jit_icall_by_name (const char *name)
5114 g_assert (jit_icall_hash_name);
5116 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5117 return g_hash_table_lookup (jit_icall_hash_name, name);
5121 mono_find_jit_icall_by_addr (gconstpointer addr)
5123 g_assert (jit_icall_hash_addr);
5125 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5129 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5132 MonoMethod *wrapper;
5134 if (callinfo->wrapper)
5135 return callinfo->wrapper;
5136 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5137 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5138 callinfo->wrapper = mono_jit_compile_method (wrapper);
5140 return callinfo->wrapper;
5144 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5146 MonoJitICallInfo *info;
5151 if (!jit_icall_hash_name) {
5152 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5153 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5156 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5157 g_warning ("jit icall already defined \"%s\"\n", name);
5158 g_assert_not_reached ();
5161 info = g_new (MonoJitICallInfo, 1);
5168 #ifdef MONO_USE_EXC_TABLES
5169 || mono_arch_has_unwind_info (func)
5172 info->wrapper = func;
5174 info->wrapper = NULL;
5175 mono_icall_get_wrapper (info);
5178 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5179 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5180 if (func != info->wrapper)
5181 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5186 static GHashTable *emul_opcode_hash = NULL;
5188 static MonoJitICallInfo *
5189 mono_find_jit_opcode_emulation (int opcode)
5191 if (emul_opcode_hash)
5192 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5198 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func)
5200 MonoJitICallInfo *info;
5202 if (!emul_opcode_hash)
5203 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5205 g_assert (!sig->hasthis);
5206 g_assert (sig->param_count < 3);
5208 info = mono_register_jit_icall (func, name, sig, FALSE);
5210 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5214 decompose_foreach (MonoInst *tree, gpointer data)
5216 static MonoJitICallInfo *newarr_info = NULL;
5217 static MonoJitICallInfo *newarr_specific_info = NULL;
5218 MonoJitICallInfo *info;
5220 switch (tree->opcode) {
5222 MonoCompile *cfg = data;
5223 MonoInst *iargs [3];
5226 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5227 g_assert (newarr_info);
5228 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5229 g_assert (newarr_specific_info);
5232 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5233 NEW_DOMAINCONST (cfg, iargs [0]);
5234 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5235 iargs [2] = tree->inst_newa_len;
5240 MonoVTable *vtable = mono_class_vtable (cfg->domain,
5241 mono_array_class_get (&tree->inst_newa_class->byval_arg, 1));
5243 NEW_PCONST (cfg, iargs [0], vtable);
5244 iargs [1] = tree->inst_newa_len;
5246 info = newarr_specific_info;
5249 mono_emulate_opcode (cfg, tree, iargs, info);
5259 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5261 switch (mono_burg_arity [tree->opcode]) {
5264 mono_inst_foreach (tree->inst_left, func, data);
5267 mono_inst_foreach (tree->inst_left, func, data);
5268 mono_inst_foreach (tree->inst_right, func, data);
5271 g_assert_not_reached ();
5278 mono_print_bb_code (MonoBasicBlock *bb) {
5280 MonoInst *c = bb->code;
5282 mono_print_tree (c);
5291 print_dfn (MonoCompile *cfg) {
5296 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5298 for (i = 0; i < cfg->num_bblocks; ++i) {
5299 bb = cfg->bblocks [i];
5301 char* code1, *code2;
5302 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5303 if (bb->last_ins->cil_code)
5304 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5306 code2 = g_strdup ("");
5308 code1 [strlen (code1) - 1] = 0;
5309 code = g_strdup_printf ("%s -> %s", code1, code2);
5313 code = g_strdup ("\n");
5314 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5316 MonoInst *c = bb->code;
5318 mono_print_tree (c);
5326 g_print ("\tprev:");
5327 for (j = 0; j < bb->in_count; ++j) {
5328 g_print (" BB%d", bb->in_bb [j]->block_num);
5330 g_print ("\t\tsucc:");
5331 for (j = 0; j < bb->out_count; ++j) {
5332 g_print (" BB%d", bb->out_bb [j]->block_num);
5334 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5337 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5340 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5342 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5350 * returns the offset used by spillvar. It allocates a new
5351 * spill variable if necessary.
5354 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5356 MonoSpillInfo **si, *info;
5359 si = &cfg->spill_info;
5361 while (i <= spillvar) {
5364 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5366 cfg->stack_offset -= sizeof (gpointer);
5367 info->offset = cfg->stack_offset;
5371 return (*si)->offset;
5377 g_assert_not_reached ();
5382 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5386 g_assert (bb->code);
5387 bb->last_ins->next = inst;
5388 bb->last_ins = inst;
5390 bb->last_ins = bb->code = inst;
5395 mono_destroy_compile (MonoCompile *cfg)
5397 //mono_mempool_stats (cfg->mempool);
5398 g_hash_table_destroy (cfg->bb_hash);
5400 mono_regstate_free (cfg->rs);
5401 mono_mempool_destroy (cfg->mempool);
5402 g_list_free (cfg->ldstr_list);
5404 g_free (cfg->varinfo);
5410 mono_get_lmf_addr (void)
5412 MonoJitTlsData *jit_tls;
5414 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5415 return &jit_tls->lmf;
5417 g_assert_not_reached ();
5422 * mono_thread_abort:
5423 * @obj: exception object
5425 * abort the thread, print exception information and stack trace
5428 mono_thread_abort (MonoObject *obj)
5430 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5438 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5440 MonoJitTlsData *jit_tls;
5443 jit_tls = g_new0 (MonoJitTlsData, 1);
5445 TlsSetValue (mono_jit_tls_id, jit_tls);
5447 jit_tls->abort_func = mono_thread_abort;
5448 jit_tls->end_of_stack = stack_start;
5450 lmf = g_new0 (MonoLMF, 1);
5456 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5459 mono_thread_abort_dummy (MonoObject *obj)
5461 if (mono_thread_attach_aborted_cb)
5462 mono_thread_attach_aborted_cb (obj);
5464 mono_thread_abort (obj);
5468 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5470 MonoJitTlsData *jit_tls;
5473 jit_tls = g_new0 (MonoJitTlsData, 1);
5475 TlsSetValue (mono_jit_tls_id, jit_tls);
5477 jit_tls->abort_func = mono_thread_abort_dummy;
5478 jit_tls->end_of_stack = stack_start;
5480 lmf = g_new0 (MonoLMF, 1);
5487 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5489 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5493 ji->data.target = target;
5494 ji->next = cfg->patch_info;
5496 cfg->patch_info = ji;
5500 mono_remove_patch_info (MonoCompile *cfg, int ip)
5502 MonoJumpInfo **ji = &cfg->patch_info;
5505 if ((*ji)->ip.i == ip)
5508 ji = &((*ji)->next);
5513 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5514 MonoJitICallInfo *info;
5516 switch (mono_burg_arity [tree->opcode]) {
5519 dec_foreach (tree->inst_left, cfg);
5521 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5522 MonoInst *iargs [2];
5524 iargs [0] = tree->inst_left;
5526 mono_emulate_opcode (cfg, tree, iargs, info);
5532 if (tree->opcode == OP_LMUL
5533 && (cfg->opt & MONO_OPT_INTRINS)
5534 && (tree->inst_left->opcode == CEE_CONV_I8
5535 || tree->inst_left->opcode == CEE_CONV_U8)
5536 && tree->inst_left->inst_left->type == STACK_I4
5537 && (tree->inst_right->opcode == CEE_CONV_I8
5538 || tree->inst_right->opcode == CEE_CONV_U8)
5539 && tree->inst_right->inst_left->type == STACK_I4) {
5540 tree->opcode = OP_BIGMUL;
5541 tree->inst_left = tree->inst_left->inst_left;
5542 tree->inst_right = tree->inst_right->inst_left;
5543 dec_foreach (tree, cfg);
5544 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5545 MonoInst *iargs [2];
5547 iargs [0] = tree->inst_i0;
5548 iargs [1] = tree->inst_i1;
5550 mono_emulate_opcode (cfg, tree, iargs, info);
5552 dec_foreach (iargs [0], cfg);
5553 dec_foreach (iargs [1], cfg);
5556 dec_foreach (tree->inst_left, cfg);
5557 dec_foreach (tree->inst_right, cfg);
5561 g_assert_not_reached ();
5563 decompose_foreach (tree, cfg);
5567 decompose_pass (MonoCompile *cfg) {
5570 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5573 cfg->prev_ins = NULL;
5574 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5575 dec_foreach (tree, cfg);
5576 cfg->prev_ins = tree;
5582 nullify_basic_block (MonoBasicBlock *bb)
5589 bb->code = bb->last_ins = NULL;
5593 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5597 for (i = 0; i < bb->out_count; i++) {
5598 MonoBasicBlock *ob = bb->out_bb [i];
5599 for (j = 0; j < ob->in_count; j++) {
5600 if (ob->in_bb [j] == orig)
5601 ob->in_bb [j] = repl;
5608 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5610 bb->out_count = bbn->out_count;
5611 bb->out_bb = bbn->out_bb;
5613 replace_basic_block (bb, bbn, bb);
5617 bb->last_ins->next = bbn->code;
5618 bb->last_ins = bbn->last_ins;
5621 bb->code = bbn->code;
5622 bb->last_ins = bbn->last_ins;
5624 bb->next_bb = bbn->next_bb;
5625 nullify_basic_block (bbn);
5629 optimize_branches (MonoCompile *cfg) {
5630 int changed = FALSE;
5631 MonoBasicBlock *bb, *bbn;
5636 /* we skip the entry block (exit is handled specially instead ) */
5637 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5639 /* dont touch code inside exception clauses */
5640 if (bb->region != -1)
5643 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5644 if (cfg->verbose_level > 2)
5645 g_print ("nullify block triggered %d\n", bbn->block_num);
5647 bb->next_bb = bbn->next_bb;
5648 nullify_basic_block (bbn);
5652 if (bb->out_count == 1) {
5653 bbn = bb->out_bb [0];
5655 if (bb->region == bbn->region && bb->next_bb == bbn) {
5656 /* the block are in sequence anyway ... */
5659 * miguel: I do not understand what the test below does, could we
5660 * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN
5662 * It could also test for bb->last_in only once, and the value
5663 * could be cached (last_ins->opcode)
5665 if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || (
5666 (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) {
5667 bb->last_ins->opcode = CEE_NOP;
5669 if (cfg->verbose_level > 2)
5670 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5672 if (bbn->in_count == 1) {
5674 if (bbn != cfg->bb_exit) {
5675 if (cfg->verbose_level > 2)
5676 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5677 merge_basic_blocks (bb, bbn);
5681 //mono_print_bb_code (bb);
5691 /* we skip the entry block (exit is handled specially instead ) */
5692 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5694 /* dont touch code inside exception clauses */
5695 if (bb->region != -1)
5698 if (bb->out_count == 1) {
5699 bbn = bb->out_bb [0];
5701 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5702 bbn = bb->last_ins->inst_target_bb;
5703 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5704 bbn->code->inst_target_bb->region == bb->region) {
5706 if (cfg->verbose_level > 2)
5707 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
5708 bb->block_num, bbn->block_num);
5710 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5711 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5716 } else if (bb->out_count == 2) {
5717 /* fixme: this does not correctly - no idea whats wrong */
5718 if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
5719 bbn = bb->last_ins->inst_true_bb;
5720 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5721 bbn->code->inst_target_bb->region == bb->region) {
5722 if (cfg->verbose_level > 2)
5723 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5724 bbn->block_num, bbn->code->opcode);
5726 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5727 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5731 bbn = bb->last_ins->inst_false_bb;
5732 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5733 bbn->code->inst_target_bb->region == bb->region) {
5734 if (cfg->verbose_level > 2)
5735 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5736 bbn->block_num, bbn->code->opcode);
5738 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
5739 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
5751 mono_compile_create_vars (MonoCompile *cfg)
5753 MonoMethodSignature *sig;
5754 MonoMethodHeader *header;
5757 header = ((MonoMethodNormal *)cfg->method)->header;
5759 sig = cfg->method->signature;
5761 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5762 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5763 cfg->ret->opcode = OP_RETARG;
5764 cfg->ret->inst_vtype = sig->ret;
5765 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5769 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5771 for (i = 0; i < sig->param_count; ++i)
5772 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5774 cfg->locals_start = cfg->num_varinfo;
5776 for (i = 0; i < header->num_locals; ++i)
5777 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5782 mono_print_code (MonoCompile *cfg)
5786 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5787 MonoInst *tree = bb->code;
5792 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5794 for (; tree; tree = tree->next) {
5795 mono_print_tree (tree);
5800 bb->last_ins->next = NULL;
5805 extern const char * const mono_burg_rule_string [];
5808 emit_state (MonoCompile *cfg, MBState *state, int goal)
5811 int ern = mono_burg_rule (state, goal);
5812 const guint16 *nts = mono_burg_nts [ern];
5815 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5819 // state->reg1 = state->reg2; /* chain rule */
5821 state->reg1 = mono_regstate_next_int (cfg->rs);
5822 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5825 state->reg1 = mono_regstate_next_int (cfg->rs);
5826 state->reg2 = mono_regstate_next_int (cfg->rs);
5829 state->reg1 = mono_regstate_next_float (cfg->rs);
5836 mono_burg_kids (state, ern, kids);
5838 emit_state (cfg, kids [0], nts [0]);
5840 emit_state (cfg, kids [1], nts [1]);
5842 g_assert (!nts [3]);
5843 emit_state (cfg, kids [2], nts [2]);
5848 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5849 if ((emit = mono_burg_func [ern]))
5850 emit (state, state->tree, cfg);
5853 #define DEBUG_SELECTION
5856 mini_select_instructions (MonoCompile *cfg)
5860 cfg->state_pool = mono_mempool_new ();
5861 cfg->rs = mono_regstate_new ();
5863 #ifdef DEBUG_SELECTION
5864 if (cfg->verbose_level >= 4) {
5865 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5866 MonoInst *tree = bb->code;
5867 g_print ("DUMP BLOCK %d:\n", bb->block_num);
5870 for (; tree; tree = tree->next) {
5871 mono_print_tree (tree);
5878 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5879 MonoInst *tree = bb->code, *next;
5885 bb->last_ins = NULL;
5888 mono_regstate_reset (cfg->rs);
5890 #ifdef DEBUG_SELECTION
5891 if (cfg->verbose_level >= 3)
5892 g_print ("LABEL BLOCK %d:\n", bb->block_num);
5894 for (; tree; tree = next) {
5896 #ifdef DEBUG_SELECTION
5897 if (cfg->verbose_level >= 3) {
5898 mono_print_tree (tree);
5903 if (!(mbstate = mono_burg_label (tree, cfg))) {
5904 g_warning ("unabled to label tree %p", tree);
5905 mono_print_tree (tree);
5907 g_assert_not_reached ();
5909 emit_state (cfg, mbstate, MB_NTERM_stmt);
5911 bb->max_ireg = cfg->rs->next_vireg;
5912 bb->max_freg = cfg->rs->next_vfreg;
5915 bb->last_ins->next = NULL;
5917 mono_mempool_empty (cfg->state_pool);
5919 mono_mempool_destroy (cfg->state_pool);
5923 mono_codegen (MonoCompile *cfg)
5925 MonoJumpInfo *patch_info;
5927 int i, max_epilog_size;
5930 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5931 cfg->spill_count = 0;
5932 /* we reuse dfn here */
5933 /* bb->dfn = bb_count++; */
5934 mono_arch_local_regalloc (cfg, bb);
5937 if (mono_trace_coverage)
5938 mono_allocate_coverage_info (cfg->method, cfg->num_bblocks);
5940 code = mono_arch_emit_prolog (cfg);
5942 if (mono_jit_profile)
5943 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
5945 cfg->code_len = code - cfg->native_code;
5946 cfg->prolog_end = cfg->code_len;
5948 mono_debug_open_method (cfg);
5950 /* emit code all basic blocks */
5951 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5952 bb->native_offset = cfg->code_len;
5953 mono_arch_output_basic_block (cfg, bb);
5955 cfg->bb_exit->native_offset = cfg->code_len;
5957 code = cfg->native_code + cfg->code_len;
5959 max_epilog_size = mono_arch_max_epilog_size (cfg);
5961 /* we always allocate code in cfg->domain->code_mp to increase locality */
5962 cfg->code_size = cfg->code_len + max_epilog_size;
5963 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
5964 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
5965 memcpy (code, cfg->native_code, cfg->code_len);
5966 g_free (cfg->native_code);
5967 cfg->native_code = code;
5968 code = cfg->native_code + cfg->code_len;
5970 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
5972 cfg->epilog_begin = cfg->code_len;
5974 if (mono_jit_profile)
5975 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
5977 cfg->code_len = code - cfg->native_code;
5979 mono_arch_emit_epilog (cfg);
5981 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5982 switch (patch_info->type) {
5983 case MONO_PATCH_INFO_ABS: {
5984 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
5986 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
5987 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5988 patch_info->data.name = info->name;
5992 case MONO_PATCH_INFO_SWITCH: {
5993 gpointer *table = g_new (gpointer, patch_info->table_size);
5994 patch_info->ip.i = patch_info->ip.label->inst_c0;
5995 for (i = 0; i < patch_info->table_size; i++) {
5996 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
5998 patch_info->data.target = table;
6007 if (cfg->verbose_level > 1)
6008 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6009 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6011 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6013 mono_debug_close_method (cfg);
6017 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6022 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6023 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6025 if (cp->opcode == OP_ICONST) {
6026 if (cfg->opt & MONO_OPT_CONSPROP) {
6027 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6031 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6032 if (cfg->opt & MONO_OPT_COPYPROP) {
6033 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6039 arity = mono_burg_arity [tree->opcode];
6042 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6043 if (cfg->opt & MONO_OPT_CFOLD)
6044 mono_constant_fold_inst (tree, NULL);
6046 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6047 if (cfg->opt & MONO_OPT_CFOLD)
6048 mono_constant_fold_inst (tree, NULL);
6050 mono_constant_fold_inst (tree, NULL);
6056 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6060 switch (tree->opcode) {
6070 if (tree->ssa_op == MONO_SSA_NOP) {
6071 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6088 case OP_VOIDCALL_REG:
6089 case OP_VOIDCALLVIRT:
6091 MonoCallInst *call = (MonoCallInst *)tree;
6092 MonoMethodSignature *sig = call->signature;
6093 int i, byref = FALSE;
6095 for (i = 0; i < sig->param_count; i++) {
6096 if (sig->params [i]->byref) {
6103 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6111 arity = mono_burg_arity [tree->opcode];
6117 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6120 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6121 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6124 g_assert_not_reached ();
6129 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6131 MonoInst *tree = bb->code;
6137 for (; tree; tree = tree->next) {
6139 mono_cprop_copy_values (cfg, tree, acp);
6141 mono_cprop_invalidate_values (tree, acp, acp_size);
6143 if (tree->ssa_op == MONO_SSA_STORE &&
6144 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6145 MonoInst *i1 = tree->inst_i1;
6147 acp [tree->inst_i0->inst_c0] = NULL;
6149 for (i = 0; i < acp_size; i++) {
6150 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6151 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6156 if (i1->opcode == OP_ICONST) {
6157 acp [tree->inst_i0->inst_c0] = i1;
6158 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6160 if (i1->ssa_op == MONO_SSA_LOAD &&
6161 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6162 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6163 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6164 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6169 if (tree->opcode == CEE_BEQ) {
6170 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6171 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6172 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6174 tree->opcode = CEE_BR;
6175 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6176 tree->inst_target_bb = tree->inst_true_bb;
6178 tree->inst_target_bb = tree->inst_false_bb;
6187 mono_local_cprop (MonoCompile *cfg)
6192 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6194 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6195 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6196 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6201 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6203 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6204 guint8 *ip = (guint8 *)header->code;
6207 int dfn = 0, i, code_size_ratio;
6209 mono_jit_stats.methods_compiled++;
6210 if (mono_jit_profile)
6211 mono_profiler_method_jit (method);
6213 cfg = g_new0 (MonoCompile, 1);
6214 cfg->method = method;
6215 cfg->mempool = mono_mempool_new ();
6217 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6218 cfg->domain = domain;
6219 cfg->verbose_level = mini_verbose;
6220 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6221 ((MonoMethodNormal *)method)->header->max_stack);
6224 * create MonoInst* which represents arguments and local variables
6226 mono_compile_create_vars (cfg);
6228 if (cfg->verbose_level > 2)
6229 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6231 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6232 mono_destroy_compile (cfg);
6233 if (mono_jit_profile)
6234 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6238 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6239 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6241 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6243 /* Depth-first ordering on basic blocks */
6244 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6246 if (cfg->opt & MONO_OPT_BRANCH)
6247 optimize_branches (cfg);
6249 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6250 if (cfg->num_bblocks != dfn + 1) {
6253 cfg->num_bblocks = dfn + 1;
6255 if (!header->clauses) {
6256 /* remove unreachable code, because the code in them may be
6257 * inconsistent (access to dead variables for example) */
6258 for (bb = cfg->bb_entry; bb;) {
6259 MonoBasicBlock *bbn = bb->next_bb;
6261 if (bbn && bbn->region == -1 && !bbn->dfn) {
6262 if (cfg->verbose_level > 1)
6263 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6264 bb->next_bb = bbn->next_bb;
6265 nullify_basic_block (bbn);
6273 if (cfg->opt & MONO_OPT_LOOP) {
6274 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6275 mono_compute_natural_loops (cfg);
6279 /* after method_to_ir */
6283 //#define DEBUGSSA "logic_run"
6284 #define DEBUGSSA_CLASS "Tests"
6288 if (!header->num_clauses && !cfg->disable_ssa) {
6289 mono_local_cprop (cfg);
6290 mono_ssa_compute (cfg);
6294 /* fixme: add all optimizations which requires SSA */
6295 if (cfg->opt & (MONO_OPT_DEADCE)) {
6296 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6297 mono_local_cprop (cfg);
6298 mono_ssa_compute (cfg);
6300 if (cfg->verbose_level >= 2) {
6307 /* after SSA translation */
6311 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6312 if (cfg->comp_done & MONO_COMP_SSA) {
6313 mono_ssa_cprop (cfg);
6315 mono_local_cprop (cfg);
6319 if (cfg->comp_done & MONO_COMP_SSA) {
6320 mono_ssa_deadce (cfg);
6322 //mono_ssa_strength_reduction (cfg);
6324 mono_ssa_remove (cfg);
6326 if (cfg->opt & MONO_OPT_BRANCH)
6327 optimize_branches (cfg);
6330 /* after SSA removal */
6334 decompose_pass (cfg);
6336 /* FIXME: disabled with exception clauses: bug #42136 */
6337 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6340 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6341 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6342 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6343 mono_analyze_liveness (cfg);
6345 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6346 regs = mono_arch_get_global_int_regs (cfg);
6347 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6351 //mono_print_code (cfg);
6355 /* variables are allocated after decompose, since decompose could create temps */
6356 mono_arch_allocate_vars (cfg);
6358 if (cfg->opt & MONO_OPT_CFOLD)
6359 mono_constant_fold (cfg);
6361 mini_select_instructions (cfg);
6364 if (cfg->verbose_level >= 2) {
6365 char *id = mono_method_full_name (cfg->method, FALSE);
6366 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6370 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6372 jinfo = g_new0 (MonoJitInfo, 1);
6373 jinfo->method = method;
6374 jinfo->code_start = cfg->native_code;
6375 jinfo->code_size = cfg->code_len;
6376 jinfo->used_regs = cfg->used_int_regs;
6378 if (header->num_clauses) {
6381 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6382 jinfo->num_clauses = header->num_clauses;
6383 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6384 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6386 for (i = 0; i < header->num_clauses; i++) {
6387 MonoExceptionClause *ec = &header->clauses [i];
6388 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6389 MonoBasicBlock *tblock;
6391 ei->flags = ec->flags;
6393 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6394 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6396 ei->data.filter = cfg->native_code + tblock->native_offset;
6398 ei->data.token = ec->token_or_filter;
6401 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6403 ei->try_start = cfg->native_code + tblock->native_offset;
6404 g_assert (tblock->native_offset);
6405 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6407 ei->try_end = cfg->native_code + tblock->native_offset;
6408 g_assert (tblock->native_offset);
6409 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6411 ei->handler_start = cfg->native_code + tblock->native_offset;
6415 mono_jit_info_table_add (cfg->domain, jinfo);
6417 /* collect statistics */
6418 mono_jit_stats.allocated_code_size += cfg->code_len;
6419 code_size_ratio = cfg->code_len;
6420 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6421 mono_jit_stats.biggest_method_size = code_size_ratio;
6422 mono_jit_stats.biggest_method = method;
6424 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6425 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6426 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6427 mono_jit_stats.max_ratio_method = method;
6429 mono_jit_stats.native_code_size += cfg->code_len;
6431 if (mono_jit_profile)
6432 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6438 mono_jit_compile_method (MonoMethod *method)
6440 /* FIXME: later copy the code from mono */
6441 MonoDomain *target_domain, *domain = mono_domain_get ();
6443 GHashTable *jit_code_hash;
6446 if (default_opt & MONO_OPT_SHARED)
6447 target_domain = mono_root_domain;
6449 target_domain = domain;
6451 jit_code_hash = target_domain->jit_code_hash;
6453 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6454 mono_jit_stats.methods_lookups++;
6458 #ifdef MONO_USE_AOT_COMPILER
6459 if (!mono_compile_aot) {
6460 mono_class_init (method->klass);
6461 if ((code = mono_aot_get_method (method))) {
6462 g_hash_table_insert (jit_code_hash, method, code);
6468 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6469 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6470 if (!method->info) {
6473 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6474 mono_lookup_pinvoke_call (method);
6475 #ifdef MONO_USE_EXC_TABLES
6476 if (mono_method_blittable (method)) {
6477 method->info = method->addr;
6480 nm = mono_marshal_get_native_wrapper (method);
6481 method->info = mono_compile_method (nm);
6483 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
6484 //mono_debug_add_wrapper (method, nm);
6485 #ifdef MONO_USE_EXC_TABLES
6489 return method->info;
6490 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
6491 const char *name = method->name;
6494 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
6495 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
6496 /* FIXME: uhm, we need a wrapper to handle exceptions? */
6497 return (gpointer)mono_delegate_ctor;
6498 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
6499 nm = mono_marshal_get_delegate_invoke (method);
6500 return mono_jit_compile_method (nm);
6501 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
6502 nm = mono_marshal_get_delegate_begin_invoke (method);
6503 return mono_jit_compile_method (nm);
6504 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
6505 nm = mono_marshal_get_delegate_end_invoke (method);
6506 return mono_jit_compile_method (nm);
6512 cfg = mini_method_compile (method, default_opt, target_domain, 0);
6513 code = cfg->native_code;
6514 mono_destroy_compile (cfg);
6516 g_hash_table_insert (jit_code_hash, method, code);
6518 if (target_domain->jump_target_hash) {
6519 MonoJumpInfo patch_info;
6521 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
6523 patch_info.next = NULL;
6524 patch_info.ip.i = 0;
6525 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
6526 patch_info.data.method = method;
6527 g_hash_table_remove (target_domain->jump_target_hash, method);
6529 for (tmp = list; tmp; tmp = tmp->next)
6530 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
6531 g_slist_free (list);
6533 /* make sure runtime_init is called */
6534 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
6540 * mono_jit_runtime_invoke:
6541 * @method: the method to invoke
6542 * @obj: this pointer
6543 * @params: array of parameter values.
6544 * @exc: used to catch exceptions objects
6547 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6550 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6551 invoke = mono_marshal_get_runtime_invoke (method);
6552 runtime_invoke = mono_jit_compile_method (invoke);
6553 return runtime_invoke (obj, params, exc);
6556 #ifdef PLATFORM_WIN32
6557 #define GET_CONTEXT \
6558 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6560 #define GET_CONTEXT \
6561 void **_p = (void **)&_dummy; \
6562 struct sigcontext *ctx = (struct sigcontext *)++_p;
6566 sigfpe_signal_handler (int _dummy)
6571 exc = mono_get_exception_divide_by_zero ();
6573 mono_arch_handle_exception (ctx, exc, FALSE);
6577 sigill_signal_handler (int _dummy)
6581 exc = mono_get_exception_execution_engine ("SIGILL");
6583 mono_arch_handle_exception (ctx, exc, FALSE);
6587 sigsegv_signal_handler (int _dummy)
6592 exc = mono_get_exception_null_reference ();
6594 mono_arch_handle_exception (ctx, exc, FALSE);
6598 sigusr1_signal_handler (int _dummy)
6603 thread = mono_thread_current ();
6605 g_assert (thread->abort_exc);
6607 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6611 mono_runtime_install_handlers (void)
6613 #ifndef PLATFORM_WIN32
6614 struct sigaction sa;
6617 #ifdef PLATFORM_WIN32
6619 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6620 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6621 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6622 #else /* !PLATFORM_WIN32 */
6624 /* libpthreads has its own implementation of sigaction(),
6625 * but it seems to work well with our current exception
6626 * handlers. If not we must call syscall directly instead
6630 sa.sa_handler = sigfpe_signal_handler;
6631 sigemptyset (&sa.sa_mask);
6633 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6634 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6637 sa.sa_handler = sigill_signal_handler;
6638 sigemptyset (&sa.sa_mask);
6640 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6641 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6643 /* catch thread abort signal */
6644 sa.sa_handler = sigusr1_signal_handler;
6645 sigemptyset (&sa.sa_mask);
6647 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6648 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6652 sa.sa_handler = sigsegv_signal_handler;
6653 sigemptyset (&sa.sa_mask);
6655 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6656 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6658 #endif /* PLATFORM_WIN32 */
6661 /* mono_jit_create_remoting_trampoline:
6662 * @method: pointer to the method info
6664 * Creates a trampoline which calls the remoting functions. This
6665 * is used in the vtable of transparent proxies.
6667 * Returns: a pointer to the newly created code
6670 mono_jit_create_remoting_trampoline (MonoMethod *method)
6673 guint8 *addr = NULL;
6675 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6676 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
6677 nm = mono_marshal_get_remoting_invoke (method);
6678 addr = mono_compile_method (nm);
6680 addr = mono_compile_method (method);
6685 static CRITICAL_SECTION ms;
6688 mini_init (const char *filename)
6692 metadata_section = &ms;
6693 InitializeCriticalSection (metadata_section);
6695 mono_jit_tls_id = TlsAlloc ();
6696 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6700 mono_runtime_install_handlers ();
6702 mono_install_compile_method (mono_jit_compile_method);
6703 mono_install_trampoline (mono_arch_create_jit_trampoline);
6704 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6705 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6706 mono_install_handler (mono_arch_get_throw_exception ());
6707 mono_install_stack_walk (mono_jit_walk_stack);
6708 mono_install_get_config_dir ();
6710 domain = mono_init (filename);
6713 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6714 ves_icall_get_frame_info);
6715 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6716 ves_icall_get_trace);
6717 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6718 mono_runtime_install_handlers);
6721 create_helper_signature ();
6723 mono_arch_register_lowlevel_calls ();
6724 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6725 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6727 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6728 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6730 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6731 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6732 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6734 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6735 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6736 helper_sig_void_ptr, TRUE);
6739 * NOTE, NOTE, NOTE, NOTE:
6740 * when adding emulation for some opcodes, remember to also add a dummy
6741 * rule to the burg files, because we need the arity information to be correct.
6743 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult);
6744 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un);
6745 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf);
6746 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv);
6747 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un);
6748 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem);
6749 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un);
6751 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl);
6752 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr);
6753 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un);
6755 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8);
6756 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4);
6757 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8);
6758 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8);
6760 #if SIZEOF_VOID_P == 4
6761 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4);
6763 #warning "fixme: add opcode emulation"
6766 /* other jit icalls */
6767 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6768 helper_sig_ptr_ptr_ptr, FALSE);
6769 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6770 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
6771 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6772 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6773 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6774 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6775 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6776 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6777 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
6778 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6779 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
6780 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6781 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6782 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6783 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6784 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6785 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6786 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6787 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6788 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6789 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6790 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6791 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6792 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6793 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6794 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6795 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6796 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
6797 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6798 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6800 mono_runtime_init (domain, mono_thread_start_cb,
6801 mono_thread_attach_cb);
6803 //mono_thread_attach (domain);
6807 MonoJitStats mono_jit_stats = {0};
6810 print_jit_stats (void)
6812 if (mono_jit_stats.enabled) {
6813 g_print ("Mono Jit statistics\n");
6814 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6815 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6816 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6817 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6818 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6819 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6820 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6821 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6822 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6823 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6824 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6825 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6826 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6827 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6828 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
6829 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
6830 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
6831 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
6833 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
6834 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
6835 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
6836 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
6837 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
6842 mini_cleanup (MonoDomain *domain)
6845 * mono_runtime_cleanup() and mono_domain_finalize () need to
6846 * be called early since they need the execution engine still
6847 * fully working (mono_domain_finalize may invoke managed finalizers
6848 * and mono_runtime_cleanup will wait for other threads to finish).
6850 mono_domain_finalize (domain);
6852 mono_runtime_cleanup (domain);
6854 mono_profiler_shutdown ();
6856 mono_debug_cleanup ();
6857 #ifdef PLATFORM_WIN32
6858 win32_seh_cleanup();
6861 mono_domain_unload (domain, TRUE);
6864 DeleteCriticalSection (metadata_section);
6868 mono_set_defaults (int verbose_level, guint32 opts)
6870 mini_verbose = verbose_level;