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 static gpointer mono_jit_compile_method (MonoMethod *method);
50 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
51 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
53 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
54 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
57 extern guint8 mono_burg_arity [];
58 /* helper methods signature */
59 static MonoMethodSignature *helper_sig_long_long_long = NULL;
60 static MonoMethodSignature *helper_sig_long_long_int = NULL;
61 static MonoMethodSignature *helper_sig_newarr = NULL;
62 static MonoMethodSignature *helper_sig_ldstr = NULL;
63 static MonoMethodSignature *helper_sig_domain_get = NULL;
64 static MonoMethodSignature *helper_sig_object_new = NULL;
65 static MonoMethodSignature *helper_sig_compile = NULL;
66 static MonoMethodSignature *helper_sig_compile_virt = NULL;
67 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
68 static MonoMethodSignature *helper_sig_ptr_void = NULL;
69 static MonoMethodSignature *helper_sig_void_ptr = NULL;
70 static MonoMethodSignature *helper_sig_void_obj = NULL;
71 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
72 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
73 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
74 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
75 static MonoMethodSignature *helper_sig_initobj = NULL;
76 static MonoMethodSignature *helper_sig_memcpy = NULL;
77 static MonoMethodSignature *helper_sig_memset = NULL;
78 static MonoMethodSignature *helper_sig_ulong_double = NULL;
79 static MonoMethodSignature *helper_sig_long_double = NULL;
80 static MonoMethodSignature *helper_sig_uint_double = NULL;
81 static MonoMethodSignature *helper_sig_int_double = NULL;
82 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
84 static guint32 default_opt = MONO_OPT_PEEPHOLE;
86 guint32 mono_jit_tls_id = 0;
87 gboolean mono_jit_trace_calls = FALSE;
88 gboolean mono_break_on_exc = FALSE;
89 gboolean mono_compile_aot = FALSE;
90 gboolean mono_trace_coverage = FALSE;
91 gboolean mono_jit_profile = FALSE;
92 MonoDebugFormat mono_debug_format = MONO_DEBUG_FORMAT_NONE;
94 CRITICAL_SECTION *metadata_section = NULL;
96 static int mini_verbose = 0;
98 #ifdef MONO_USE_EXC_TABLES
100 mono_type_blittable (MonoType *type)
119 case MONO_TYPE_OBJECT:
121 case MONO_TYPE_VALUETYPE:
122 case MONO_TYPE_CLASS:
123 return type->data.klass->blittable;
133 mono_method_blittable (MonoMethod *method)
135 MonoMethodSignature *sig;
141 if (!mono_arch_has_unwind_info (method->addr)) {
145 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
148 sig = method->signature;
150 if (!mono_type_blittable (sig->ret))
153 for (i = 0; i < sig->param_count; i++)
154 if (!mono_type_blittable (sig->params [i]))
163 print_method_from_ip (void *ip)
168 ji = mono_jit_info_table_find (mono_domain_get (), ip);
170 g_print ("No method at %p\n", ip);
173 method = mono_method_full_name (ji->method, TRUE);
174 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);
179 #define MONO_INIT_VARINFO(vi,id) do { \
180 (vi)->range.first_use.pos.bid = 0xffff; \
186 * Basic blocks have two numeric identifiers:
187 * dfn: Depth First Number
188 * block_num: unique ID assigned at bblock creation
190 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
191 #define ADD_BBLOCK(cfg,bbhash,b) do { \
192 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
193 (b)->block_num = cfg->num_bblocks++; \
194 (b)->real_offset = real_offset; \
197 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
198 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
200 if ((ip) >= end || (ip) < header->code) goto unverified; \
201 (tblock) = NEW_BBLOCK (cfg); \
202 (tblock)->cil_code = (ip); \
203 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
205 (tblock)->real_offset = real_offset; \
208 #define CHECK_BBLOCK(target,ip,tblock) do { \
209 if ((target) < (ip) && !(tblock)->code) { \
210 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
211 if (cfg->verbose_level > 2) g_print ("queued block %d for check at IL%04x from IL%04x\n", (tblock)->block_num, (target) - header->code, (ip) - header->code); \
215 #define NEW_ICONST(cfg,dest,val) do { \
216 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
217 (dest)->opcode = OP_ICONST; \
218 (dest)->inst_c0 = (val); \
219 (dest)->type = STACK_I4; \
222 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
223 #define NEW_PCONST(cfg,dest,val) do { \
224 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
225 (dest)->opcode = OP_ICONST; \
226 (dest)->inst_p0 = (val); \
227 (dest)->type = STACK_PTR; \
230 #define NEW_CLASSCONST(cfg,dest,val) do { \
231 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
232 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
233 (dest)->inst_p0 = (val); \
234 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
235 (dest)->type = STACK_PTR; \
238 #define NEW_IMAGECONST(cfg,dest,val) do { \
239 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
240 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
241 (dest)->inst_p0 = (val); \
242 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
243 (dest)->type = STACK_PTR; \
246 #define NEW_FIELDCONST(cfg,dest,field) do { \
247 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
248 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
249 (dest)->inst_p0 = (field); \
250 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
251 (dest)->type = STACK_PTR; \
254 #define NEW_METHODCONST(cfg,dest,val) do { \
255 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
256 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
257 (dest)->inst_p0 = (val); \
258 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
259 (dest)->type = STACK_PTR; \
262 #define NEW_DOMAINCONST(cfg,dest) do { \
263 if ((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot) { \
264 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
266 NEW_PCONST (cfg, dest, (cfg)->domain); \
270 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
272 #define NEW_ARGLOAD(cfg,dest,num) do { \
273 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
274 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
275 (dest)->ssa_op = MONO_SSA_LOAD; \
276 (dest)->inst_i0 = arg_array [(num)]; \
277 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
278 type_to_eval_stack_type (param_types [(num)], (dest)); \
279 (dest)->klass = (dest)->inst_i0->klass; \
282 #define NEW_LOCLOAD(cfg,dest,num) do { \
283 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
284 (dest)->ssa_op = MONO_SSA_LOAD; \
285 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
286 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
287 type_to_eval_stack_type (header->locals [(num)], (dest)); \
288 (dest)->klass = (dest)->inst_i0->klass; \
291 #define NEW_LOCLOADA(cfg,dest,num) do { \
292 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
293 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
294 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
295 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
296 (dest)->opcode = OP_LDADDR; \
297 (dest)->type = STACK_MP; \
298 (dest)->klass = (dest)->inst_i0->klass; \
299 (cfg)->disable_ssa = TRUE; \
302 #define NEW_RETLOADA(cfg,dest) do { \
303 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
304 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
305 (dest)->inst_i0 = (cfg)->ret; \
306 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
307 (dest)->opcode = CEE_LDIND_I; \
308 (dest)->type = STACK_MP; \
309 (dest)->klass = (dest)->inst_i0->klass; \
310 (cfg)->disable_ssa = TRUE; \
313 #define NEW_ARGLOADA(cfg,dest,num) do { \
314 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
315 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
316 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
317 (dest)->inst_i0 = arg_array [(num)]; \
318 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
319 (dest)->opcode = OP_LDADDR; \
320 (dest)->type = STACK_MP; \
321 (dest)->klass = (dest)->inst_i0->klass; \
322 (cfg)->disable_ssa = TRUE; \
325 #define NEW_TEMPLOAD(cfg,dest,num) do { \
326 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
327 (dest)->ssa_op = MONO_SSA_LOAD; \
328 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
329 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
330 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
331 (dest)->klass = (dest)->inst_i0->klass; \
334 #define NEW_TEMPLOADA(cfg,dest,num) do { \
335 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
336 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
337 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
338 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
339 (dest)->opcode = OP_LDADDR; \
340 (dest)->type = STACK_MP; \
341 (dest)->klass = (dest)->inst_i0->klass; \
342 (cfg)->disable_ssa = TRUE; \
346 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
347 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
348 (dest)->inst_left = addr; \
349 (dest)->opcode = mono_type_to_ldind (vtype); \
350 type_to_eval_stack_type (vtype, (dest)); \
351 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
354 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
355 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
356 (dest)->inst_i0 = addr; \
357 (dest)->opcode = mono_type_to_stind (vtype); \
358 (dest)->inst_i1 = (value); \
359 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
362 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
363 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
364 (dest)->ssa_op = MONO_SSA_STORE; \
365 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
366 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
367 (dest)->inst_i1 = (inst); \
368 (dest)->klass = (dest)->inst_i0->klass; \
371 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
372 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
373 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
374 (dest)->ssa_op = MONO_SSA_STORE; \
375 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
376 (dest)->inst_i1 = (inst); \
377 (dest)->klass = (dest)->inst_i0->klass; \
380 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
381 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
382 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
383 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
384 (dest)->ssa_op = MONO_SSA_STORE; \
385 (dest)->inst_i0 = arg_array [(num)]; \
386 (dest)->inst_i1 = (inst); \
387 (dest)->klass = (dest)->inst_i0->klass; \
390 #define ADD_BINOP(op) do { \
391 MONO_INST_NEW (cfg, ins, (op)); \
392 ins->cil_code = ip; \
394 ins->inst_i0 = sp [0]; \
395 ins->inst_i1 = sp [1]; \
397 type_from_op (ins); \
401 #define ADD_UNOP(op) do { \
402 MONO_INST_NEW (cfg, ins, (op)); \
403 ins->cil_code = ip; \
405 ins->inst_i0 = sp [0]; \
407 type_from_op (ins); \
411 #define ADD_BINCOND(next_block) do { \
413 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
415 cmp->inst_i0 = sp [0]; \
416 cmp->inst_i1 = sp [1]; \
417 cmp->cil_code = ins->cil_code; \
418 type_from_op (cmp); \
420 ins->inst_i0 = cmp; \
421 MONO_ADD_INS (bblock, ins); \
422 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
423 GET_BBLOCK (cfg, bbhash, tblock, target); \
424 link_bblock (cfg, bblock, tblock); \
425 ins->inst_true_bb = tblock; \
426 CHECK_BBLOCK (target, ip, tblock); \
427 if ((next_block)) { \
428 link_bblock (cfg, bblock, (next_block)); \
429 ins->inst_false_bb = (next_block); \
430 start_new_bblock = 1; \
432 GET_BBLOCK (cfg, bbhash, tblock, ip); \
433 link_bblock (cfg, bblock, tblock); \
434 ins->inst_false_bb = tblock; \
435 start_new_bblock = 2; \
439 /* FIXME: handle float, long ... */
440 #define ADD_UNCOND(istrue) do { \
442 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
444 cmp->inst_i0 = sp [0]; \
445 switch (cmp->inst_i0->type) { \
447 cmp->inst_i1 = zero_int64; break; \
449 cmp->inst_i1 = zero_r8; break; \
452 cmp->inst_i1 = zero_ptr; break; \
454 cmp->inst_i1 = zero_obj; break; \
456 cmp->inst_i1 = zero_int32; \
458 cmp->cil_code = ins->cil_code; \
459 type_from_op (cmp); \
461 ins->inst_i0 = cmp; \
462 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
463 MONO_ADD_INS (bblock, ins); \
464 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
465 GET_BBLOCK (cfg, bbhash, tblock, target); \
466 link_bblock (cfg, bblock, tblock); \
467 ins->inst_true_bb = tblock; \
468 CHECK_BBLOCK (target, ip, tblock); \
469 GET_BBLOCK (cfg, bbhash, tblock, ip); \
470 link_bblock (cfg, bblock, tblock); \
471 ins->inst_false_bb = tblock; \
472 start_new_bblock = 2; \
475 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
476 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
477 (dest)->opcode = CEE_LDELEMA; \
478 (dest)->inst_left = (sp) [0]; \
479 (dest)->inst_right = (sp) [1]; \
480 (dest)->type = STACK_MP; \
481 (dest)->klass = (k); \
484 #define NEW_GROUP(cfg,dest,el1,el2) do { \
485 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
486 (dest)->opcode = OP_GROUP; \
487 (dest)->inst_left = (el1); \
488 (dest)->inst_right = (el2); \
491 static GHashTable *coverage_hash = NULL;
494 mono_allocate_coverage_info (MonoMethod *method, int size)
496 MonoCoverageInfo *res;
499 coverage_hash = g_hash_table_new (NULL, NULL);
501 res = g_malloc0 (sizeof (MonoCoverageInfo) + sizeof (int) * size * 2);
505 g_hash_table_insert (coverage_hash, method, res);
511 mono_get_coverage_info (MonoMethod *method)
516 return g_hash_table_lookup (coverage_hash, method);
521 compare_bblock (gconstpointer a, gconstpointer b)
523 const MonoBasicBlock *b1 = a;
524 const MonoBasicBlock *b2 = b;
526 return b2->cil_code - b1->cil_code;
531 * link_bblock: Links two basic blocks
533 * links two basic blocks in the control flow graph, the 'from'
534 * argument is the starting block and the 'to' argument is the block
535 * the control flow ends to after 'from'.
538 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
540 MonoBasicBlock **newa;
544 if (from->cil_code) {
546 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
548 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
551 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
553 g_print ("edge from entry to exit\n");
557 for (i = 0; i < from->out_count; ++i) {
558 if (to == from->out_bb [i]) {
564 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
565 for (i = 0; i < from->out_count; ++i) {
566 newa [i] = from->out_bb [i];
574 for (i = 0; i < to->in_count; ++i) {
575 if (from == to->in_bb [i]) {
581 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
582 for (i = 0; i < to->in_count; ++i) {
583 newa [i] = to->in_bb [i];
592 * We mark each basic block with a region ID. We use that to avoid BB
593 * optimizations when blocks are in different regions.
596 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
598 MonoMethod *method = cfg->method;
599 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
600 MonoExceptionClause *clause;
603 /* first search for handlers and filters */
604 for (i = 0; i < header->num_clauses; ++i) {
605 clause = &header->clauses [i];
606 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
607 (offset < (clause->token_or_filter + filter_lengths [i])))
608 return (i << 8) | 128 | clause->flags;
610 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
611 return (i << 8) | 64 | clause->flags;
615 /* search the try blocks */
616 for (i = 0; i < header->num_clauses; ++i) {
617 clause = &header->clauses [i];
618 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
619 return (i << 8) | clause->flags;
625 static MonoBasicBlock *
626 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
628 MonoMethod *method = cfg->method;
629 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
630 MonoExceptionClause *clause;
631 MonoBasicBlock *handler;
634 for (i = 0; i < header->num_clauses; ++i) {
635 clause = &header->clauses [i];
636 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
637 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
638 if (clause->flags & type) {
639 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
650 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
654 array [*dfn] = start;
655 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
656 for (i = 0; i < start->out_count; ++i) {
657 if (start->out_bb [i]->dfn)
660 start->out_bb [i]->dfn = *dfn;
661 start->out_bb [i]->df_parent = start;
662 array [*dfn] = start->out_bb [i];
663 df_visit (start->out_bb [i], dfn, array);
669 MonoBasicBlock *best;
673 previous_foreach (gconstpointer key, gpointer val, gpointer data)
675 PrevStruct *p = data;
676 MonoBasicBlock *bb = val;
677 //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,
678 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
680 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
684 static MonoBasicBlock*
685 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
691 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
696 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
705 * FIXME: take into account all the details:
706 * second may have been the target of more than one bblock
708 second->out_count = first->out_count;
709 second->out_bb = first->out_bb;
711 for (i = 0; i < first->out_count; ++i) {
712 bb = first->out_bb [i];
713 for (j = 0; j < bb->in_count; ++j) {
714 if (bb->in_bb [j] == first)
715 bb->in_bb [j] = second;
719 first->out_count = 0;
720 first->out_bb = NULL;
721 link_bblock (cfg, first, second);
723 second->last_ins = first->last_ins;
725 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
726 for (inst = first->code; inst && inst->next; inst = inst->next) {
727 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
728 g_print ("found %p: %s", inst->next->cil_code, code);
730 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
731 second->code = inst->next;
733 first->last_ins = inst;
734 second->next_bb = first->next_bb;
735 first->next_bb = second;
740 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
746 mono_type_to_ldind (MonoType *type)
758 case MONO_TYPE_BOOLEAN:
772 case MONO_TYPE_FNPTR:
774 case MONO_TYPE_CLASS:
775 case MONO_TYPE_STRING:
776 case MONO_TYPE_OBJECT:
777 case MONO_TYPE_SZARRAY:
778 case MONO_TYPE_ARRAY:
779 return CEE_LDIND_REF;
787 case MONO_TYPE_VALUETYPE:
788 if (type->data.klass->enumtype) {
789 t = type->data.klass->enum_basetype->type;
794 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
800 mono_type_to_stind (MonoType *type)
811 case MONO_TYPE_BOOLEAN:
823 case MONO_TYPE_FNPTR:
825 case MONO_TYPE_CLASS:
826 case MONO_TYPE_STRING:
827 case MONO_TYPE_OBJECT:
828 case MONO_TYPE_SZARRAY:
829 case MONO_TYPE_ARRAY:
830 return CEE_STIND_REF;
838 case MONO_TYPE_VALUETYPE:
839 if (type->data.klass->enumtype) {
840 t = type->data.klass->enum_basetype->type;
846 g_error ("unknown type %02x in type_to_stind", type->type);
852 * Returns the type used in the eval stack when @type is loaded.
853 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
856 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
860 inst->type = STACK_MP;
868 case MONO_TYPE_BOOLEAN:
874 inst->type = STACK_I4;
879 case MONO_TYPE_FNPTR:
880 inst->type = STACK_PTR;
882 case MONO_TYPE_CLASS:
883 case MONO_TYPE_STRING:
884 case MONO_TYPE_OBJECT:
885 case MONO_TYPE_SZARRAY:
886 case MONO_TYPE_ARRAY:
887 inst->type = STACK_OBJ;
891 inst->type = STACK_I8;
895 inst->type = STACK_R8;
897 case MONO_TYPE_VALUETYPE:
898 if (type->data.klass->enumtype) {
899 t = type->data.klass->enum_basetype->type;
902 inst->klass = type->data.klass;
903 inst->type = STACK_VTYPE;
907 g_error ("unknown type 0x%02x in eval stack type", type->type);
912 * The following tables are used to quickly validate the IL code in type_from_op ().
915 bin_num_table [STACK_MAX] [STACK_MAX] = {
916 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
917 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
918 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
919 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
920 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
921 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
922 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
923 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
928 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
931 /* reduce the size of this table */
933 bin_int_table [STACK_MAX] [STACK_MAX] = {
934 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
935 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
936 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
937 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, 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},
939 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
940 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
941 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
945 bin_comp_table [STACK_MAX] [STACK_MAX] = {
947 {0, 1, 0, 1, 0, 0, 4, 0},
948 {0, 0, 1, 0, 0, 0, 0, 0},
949 {0, 1, 0, 1, 0, 2, 4, 0},
950 {0, 0, 0, 0, 1, 0, 0, 0},
951 {0, 0, 0, 2, 0, 1, 0, 0},
952 {0, 4, 0, 4, 0, 0, 3, 0},
953 {0, 0, 0, 0, 0, 0, 0, 0},
956 /* reduce the size of this table */
958 shift_table [STACK_MAX] [STACK_MAX] = {
959 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
960 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
961 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
962 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
963 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
964 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
965 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
966 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
970 * Tables to map from the non-specific opcode to the matching
971 * type-specific opcode.
973 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
975 binops_op_map [STACK_MAX] = {
976 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
979 /* handles from CEE_NEG to CEE_CONV_U8 */
981 unops_op_map [STACK_MAX] = {
982 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
985 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
987 ovfops_op_map [STACK_MAX] = {
988 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
991 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
993 ovf2ops_op_map [STACK_MAX] = {
994 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
997 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
999 ovf3ops_op_map [STACK_MAX] = {
1000 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
1003 /* handles from CEE_CEQ to CEE_CLT_UN */
1004 static const guint16
1005 ceqops_op_map [STACK_MAX] = {
1006 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1010 * Sets ins->type (the type on the eval stack) according to the
1011 * type of the opcode and the arguments to it.
1012 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1014 * FIXME: this function sets ins->type unconditionally in some cases, but
1015 * it should set it to invalid for some types (a conv.x on an object)
1018 type_from_op (MonoInst *ins) {
1019 switch (ins->opcode) {
1026 /* FIXME: check unverifiable args for STACK_MP */
1027 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1028 ins->opcode += binops_op_map [ins->type];
1035 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1036 ins->opcode += binops_op_map [ins->type];
1041 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1042 ins->opcode += binops_op_map [ins->type];
1045 /* FIXME: handle some specifics with ins->next->type */
1046 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1050 case 256+CEE_CGT_UN:
1052 case 256+CEE_CLT_UN:
1053 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1054 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1058 ins->type = neg_table [ins->inst_i0->type];
1059 ins->opcode += unops_op_map [ins->type];
1062 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1063 ins->type = ins->inst_i0->type;
1065 ins->type = STACK_INV;
1066 ins->opcode += unops_op_map [ins->type];
1072 ins->type = STACK_I4;
1073 ins->opcode += unops_op_map [ins->inst_i0->type];
1076 ins->type = STACK_R8;
1077 switch (ins->inst_i0->type) {
1082 ins->opcode = OP_LCONV_TO_R_UN;
1086 case CEE_CONV_OVF_I1:
1087 case CEE_CONV_OVF_U1:
1088 case CEE_CONV_OVF_I2:
1089 case CEE_CONV_OVF_U2:
1090 case CEE_CONV_OVF_I4:
1091 case CEE_CONV_OVF_U4:
1092 ins->type = STACK_I4;
1093 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1095 case CEE_CONV_OVF_I_UN:
1096 case CEE_CONV_OVF_U_UN:
1097 ins->type = STACK_PTR;
1098 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1100 case CEE_CONV_OVF_I1_UN:
1101 case CEE_CONV_OVF_I2_UN:
1102 case CEE_CONV_OVF_I4_UN:
1103 case CEE_CONV_OVF_U1_UN:
1104 case CEE_CONV_OVF_U2_UN:
1105 case CEE_CONV_OVF_U4_UN:
1106 ins->type = STACK_I4;
1107 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1110 ins->type = STACK_PTR;
1111 switch (ins->inst_i0->type) {
1117 ins->opcode = OP_LCONV_TO_U;
1120 ins->opcode = OP_FCONV_TO_U;
1126 ins->type = STACK_I8;
1127 ins->opcode += unops_op_map [ins->inst_i0->type];
1129 case CEE_CONV_OVF_I8:
1130 case CEE_CONV_OVF_U8:
1131 ins->type = STACK_I8;
1132 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1134 case CEE_CONV_OVF_U8_UN:
1135 case CEE_CONV_OVF_I8_UN:
1136 ins->type = STACK_I8;
1137 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1141 ins->type = STACK_R8;
1142 ins->opcode += unops_op_map [ins->inst_i0->type];
1145 ins->type = STACK_R8;
1149 ins->type = STACK_I4;
1150 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1153 case CEE_CONV_OVF_I:
1154 case CEE_CONV_OVF_U:
1155 ins->type = STACK_PTR;
1156 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1159 case CEE_ADD_OVF_UN:
1161 case CEE_MUL_OVF_UN:
1163 case CEE_SUB_OVF_UN:
1164 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1165 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1168 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1175 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1178 /* map ldelem.x to the matching ldind.x opcode */
1180 ldelem_to_ldind [] = {
1194 /* map stelem.x to the matching stind.x opcode */
1196 stelem_to_stind [] = {
1210 param_table [STACK_MAX] [STACK_MAX] = {
1215 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1219 switch (args->type) {
1229 for (i = 0; i < sig->param_count; ++i) {
1230 switch (args [i].type) {
1234 if (!sig->params [i]->byref)
1238 if (sig->params [i]->byref)
1240 switch (sig->params [i]->type) {
1241 case MONO_TYPE_CLASS:
1242 case MONO_TYPE_STRING:
1243 case MONO_TYPE_OBJECT:
1244 case MONO_TYPE_SZARRAY:
1245 case MONO_TYPE_ARRAY:
1252 if (sig->params [i]->byref)
1254 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1263 /*if (!param_table [args [i].type] [sig->params [i]->type])
1271 * When we need a pointer to the current domain many times in a method, we
1272 * call mono_domain_get() once and we store the result in a local variable.
1273 * This function returns the variable that represents the MonoDomain*.
1275 inline static MonoInst *
1276 mono_get_domainvar (MonoCompile *cfg)
1278 if (!cfg->domainvar)
1279 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1280 return cfg->domainvar;
1284 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1287 int num = cfg->num_varinfo;
1289 if ((num + 1) >= cfg->varinfo_count) {
1290 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1291 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1292 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1295 mono_jit_stats.allocate_var++;
1297 MONO_INST_NEW (cfg, inst, opcode);
1298 inst->inst_c0 = num;
1299 inst->inst_vtype = type;
1300 inst->klass = mono_class_from_mono_type (type);
1301 /* if set to 1 the variable is native */
1304 cfg->varinfo [num] = inst;
1306 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1307 MONO_INIT_VARINFO (cfg->vars [num], num);
1310 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1315 type_from_stack_type (MonoInst *ins) {
1316 switch (ins->type) {
1317 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1318 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1319 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1320 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1321 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1322 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1323 case STACK_VTYPE: return &ins->klass->byval_arg;
1325 g_error ("stack type %d to montype not handled\n", ins->type);
1331 array_access_to_klass (int opcode)
1335 return mono_defaults.byte_class;
1337 return mono_defaults.uint16_class;
1340 return mono_defaults.int_class;
1343 return mono_defaults.sbyte_class;
1346 return mono_defaults.int16_class;
1349 return mono_defaults.int32_class;
1351 return mono_defaults.uint32_class;
1354 return mono_defaults.int64_class;
1357 return mono_defaults.single_class;
1360 return mono_defaults.double_class;
1361 case CEE_LDELEM_REF:
1362 case CEE_STELEM_REF:
1363 return mono_defaults.object_class;
1365 g_assert_not_reached ();
1371 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1375 MONO_ADD_INS (bb, inst);
1378 switch (bb->last_ins->opcode) {
1391 while (prev->next && prev->next != bb->last_ins)
1393 if (prev == bb->code) {
1394 if (bb->last_ins == bb->code) {
1395 inst->next = bb->code;
1398 inst->next = prev->next;
1402 inst->next = bb->last_ins;
1406 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1408 MONO_ADD_INS (bb, inst);
1414 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1416 MonoInst *inst, *load;
1418 NEW_TEMPLOAD (cfg, load, src);
1420 NEW_TEMPSTORE (cfg, inst, dest, load);
1421 if (inst->opcode == CEE_STOBJ) {
1422 NEW_TEMPLOADA (cfg, inst, dest);
1423 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1425 inst->cil_code = NULL;
1426 mono_add_ins_to_end (bb, inst);
1431 * This function is called to handle items that are left on the evaluation stack
1432 * at basic block boundaries. What happens is that we save the values to local variables
1433 * and we reload them later when first entering the target basic block (with the
1434 * handle_loaded_temps () function).
1435 * A single joint point will use the same variables (stored in the array bb->out_stack or
1436 * bb->in_stack, if the basic block is before or after the joint point).
1439 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1441 MonoBasicBlock *outb;
1442 MonoInst *inst, **locals;
1446 if (cfg->verbose_level > 3)
1447 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1448 if (!bb->out_scount) {
1450 bb->out_scount = count;
1451 //g_print ("bblock %d has out:", bb->block_num);
1452 for (i = 0; i < bb->out_count; ++i) {
1453 outb = bb->out_bb [i];
1454 //g_print (" %d", outb->block_num);
1455 if (outb->in_stack) {
1457 bb->out_stack = outb->in_stack;
1463 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1464 for (i = 0; i < count; ++i) {
1466 * dietmar suggests that we can reuse temps already allocated
1467 * for this purpouse, if they occupy the same stack slot and if
1468 * they are of the same type.
1470 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1474 locals = bb->out_stack;
1475 for (i = 0; i < count; ++i) {
1476 /* add store ops at the end of the bb, before the branch */
1477 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1478 if (inst->opcode == CEE_STOBJ) {
1479 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1480 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1482 inst->cil_code = sp [i]->cil_code;
1483 mono_add_ins_to_end (bb, inst);
1485 if (cfg->verbose_level > 3)
1486 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1489 for (i = 0; i < bb->out_count; ++i) {
1490 outb = bb->out_bb [i];
1491 if (outb->in_scount)
1492 continue; /* check they are the same locals */
1493 outb->in_scount = count;
1494 outb->in_stack = locals;
1500 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1505 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1509 case MONO_TYPE_VOID:
1510 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1513 case MONO_TYPE_BOOLEAN:
1516 case MONO_TYPE_CHAR:
1519 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1523 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1524 case MONO_TYPE_CLASS:
1525 case MONO_TYPE_STRING:
1526 case MONO_TYPE_OBJECT:
1527 case MONO_TYPE_SZARRAY:
1528 case MONO_TYPE_ARRAY:
1529 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1532 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1535 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1536 case MONO_TYPE_VALUETYPE:
1537 if (type->data.klass->enumtype) {
1538 t = type->data.klass->enum_basetype->type;
1541 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1543 g_error ("unknown type %02x in ret_type_to_call_opcode", type->type);
1549 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1551 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1553 ji->ip.label = label;
1554 ji->type = MONO_PATCH_INFO_SWITCH;
1555 ji->data.table = bbs;
1556 ji->next = cfg->patch_info;
1557 ji->table_size = num_blocks;
1558 cfg->patch_info = ji;
1562 * When we add a tree of instructions, we need to ensure the instructions currently
1563 * on the stack are executed before (like, if we load a value from a local).
1564 * We ensure this by saving the currently loaded values to temps and rewriting the
1565 * instructions to load the values.
1566 * This is not done for opcodes that terminate a basic block (because it's handled already
1567 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1570 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1572 MonoInst *load, *store, *temp, *ins;
1574 while (stack < sp) {
1576 /* handle also other constants */
1577 if (ins->opcode != OP_ICONST) {
1578 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1579 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1580 store->cil_code = ins->cil_code;
1581 if (store->opcode == CEE_STOBJ) {
1582 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1583 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1585 MONO_ADD_INS (bblock, store);
1586 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1587 load->cil_code = ins->cil_code;
1595 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1596 const guint8 *ip, gboolean to_end)
1598 MonoInst *temp, *store, *ins = (MonoInst*)call;
1599 MonoType *ret = sig->ret;
1601 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1603 call->inst.type = STACK_OBJ;
1604 call->inst.opcode = CEE_CALL;
1605 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1607 type_to_eval_stack_type (ret, ins);
1608 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1611 if (MONO_TYPE_ISSTRUCT (ret)) {
1614 /* we use this to allocate native sized structs */
1615 temp->unused = sig->pinvoke;
1617 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1618 if (call->inst.opcode == OP_VCALL)
1619 ins->inst_left = loada;
1621 ins->inst_right = loada; /* a virtual or indirect call */
1624 mono_add_ins_to_end (bblock, ins);
1626 MONO_ADD_INS (bblock, ins);
1628 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1629 store->cil_code = ip;
1631 mono_add_ins_to_end (bblock, store);
1633 MONO_ADD_INS (bblock, store);
1635 return temp->inst_c0;
1638 mono_add_ins_to_end (bblock, ins);
1640 MONO_ADD_INS (bblock, ins);
1645 inline static MonoCallInst *
1646 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1647 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1652 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1654 call->inst.cil_code = ip;
1656 call->signature = sig;
1657 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1659 for (i = 0; i < (sig->param_count + sig->hasthis); ++i) {
1660 if (call->args [i]) {
1662 mono_add_ins_to_end (bblock, call->args [i]);
1664 MONO_ADD_INS (bblock, call->args [i]);
1671 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1672 MonoInst **args, MonoInst *addr, const guint8 *ip)
1674 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1676 call->inst.inst_i0 = addr;
1678 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1681 static MonoCallInst*
1682 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1683 MonoInst **args, const guint8 *ip, MonoInst *this)
1685 gboolean virtual = this != NULL;
1688 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1690 if (this && sig->hasthis &&
1691 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1692 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1693 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1695 call->method = method;
1697 call->inst.flags |= MONO_INST_HAS_METHOD;
1698 call->inst.inst_left = this;
1704 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1705 MonoInst **args, const guint8 *ip, MonoInst *this)
1707 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, method->signature, args, ip, this);
1709 return mono_spill_call (cfg, bblock, call, method->signature, method->string_ctor, ip, FALSE);
1713 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1714 MonoInst **args, const guint8 *ip, gboolean to_end)
1720 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1722 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1726 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1728 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1731 g_warning ("unregistered JIT ICall");
1732 g_assert_not_reached ();
1735 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1739 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1741 MonoInst *ins, *temp = NULL, *store, *load;
1745 /*g_print ("emulating: ");
1746 mono_print_tree (tree);
1748 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1749 ins = (MonoInst*)call;
1751 call->inst.cil_code = tree->cil_code;
1753 call->signature = info->sig;
1755 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1757 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1758 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1759 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1760 store->cil_code = tree->cil_code;
1765 nargs = info->sig->param_count + info->sig->hasthis;
1767 for (i = 1; i < nargs; i++) {
1768 call->args [i - 1]->next = call->args [i];
1772 call->args [nargs - 1]->next = store;
1774 if (cfg->prev_ins) {
1775 store->next = cfg->prev_ins->next;
1777 cfg->prev_ins->next = call->args [0];
1779 cfg->prev_ins->next = store;
1781 store->next = cfg->cbb->code;
1783 cfg->cbb->code = call->args [0];
1785 cfg->cbb->code = store;
1789 call->fptr = info->wrapper;
1791 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1792 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1797 static MonoMethodSignature *
1798 mono_get_element_address_signature (int arity)
1800 static GHashTable *sighash = NULL;
1801 MonoMethodSignature *res;
1805 sighash = g_hash_table_new (NULL, NULL);
1808 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1811 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1813 res->params [0] = &mono_defaults.array_class->byval_arg;
1815 for (i = 1; i <= arity; i++)
1816 res->params [i] = &mono_defaults.int_class->byval_arg;
1818 res->ret = &mono_defaults.int_class->byval_arg;
1820 g_hash_table_insert (sighash, (gpointer)arity, res);
1826 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1827 MonoInst *iargs [3];
1832 * This check breaks with spilled vars... need to handle it during verification anyway.
1833 * g_assert (klass && klass == src->klass && klass == dest->klass);
1837 n = mono_class_native_size (klass, NULL);
1839 n = mono_class_value_size (klass, NULL);
1843 NEW_ICONST (cfg, iargs [2], n);
1845 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
1849 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
1851 MonoInst *iargs [2];
1852 MonoInst *ins, *zero_int32;
1855 NEW_ICONST (cfg, zero_int32, 0);
1857 mono_class_init (klass);
1858 n = mono_class_value_size (klass, NULL);
1859 MONO_INST_NEW (cfg, ins, 0);
1861 ins->inst_left = dest;
1862 ins->inst_right = zero_int32;
1865 ins->opcode = CEE_STIND_I1;
1866 MONO_ADD_INS (bblock, ins);
1869 ins->opcode = CEE_STIND_I2;
1870 MONO_ADD_INS (bblock, ins);
1873 ins->opcode = CEE_STIND_I4;
1874 MONO_ADD_INS (bblock, ins);
1877 handle_loaded_temps (cfg, bblock, stack_start, sp);
1878 NEW_ICONST (cfg, ins, n);
1881 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
1886 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
1889 mono_method_check_inlining (MonoMethod *method)
1891 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
1892 MonoMethodSignature *signature = method->signature;
1895 /* fixme: we should inline wrappers */
1896 if (method->wrapper_type != MONO_WRAPPER_NONE)
1899 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1900 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1901 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
1902 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
1903 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1904 (method->klass->marshalbyref) ||
1905 !header || header->num_clauses ||
1906 /* fixme: why cant we inline valuetype returns? */
1907 MONO_TYPE_ISSTRUCT (signature->ret))
1910 /* its not worth to inline methods with valuetype arguments?? */
1911 for (i = 0; i < signature->param_count; i++) {
1912 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
1917 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
1919 /* also consider num_locals? */
1920 if (header->code_size < 20)
1927 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
1932 if (cmethod->klass == mono_defaults.string_class) {
1933 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
1936 } else if (cmethod->klass == mono_defaults.math_class) {
1937 if (strcmp (cmethod->name, "Sin") == 0)
1939 else if (strcmp (cmethod->name, "Cos") == 0)
1941 else if (strcmp (cmethod->name, "Tan") == 0)
1943 else if (strcmp (cmethod->name, "Atan") == 0)
1945 else if (strcmp (cmethod->name, "Sqrt") == 0)
1947 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
1954 pc = fsig->param_count + fsig->hasthis;
1955 MONO_INST_NEW (cfg, ins, op);
1958 ins->inst_i0 = args [0];
1960 ins->inst_i1 = args [1];
1967 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
1969 MonoInst *store, *temp;
1972 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
1974 if (!sig->hasthis && sig->param_count == 0)
1978 if (sp [0]->opcode == OP_ICONST) {
1981 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
1983 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
1984 store->cil_code = sp [0]->cil_code;
1985 MONO_ADD_INS (bblock, store);
1990 for (i = 0; i < sig->param_count; ++i) {
1991 if (sp [0]->opcode == OP_ICONST) {
1994 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
1996 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
1997 store->cil_code = sp [0]->cil_code;
1998 if (store->opcode == CEE_STOBJ) {
1999 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2000 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2002 MONO_ADD_INS (bblock, store);
2010 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2011 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2013 MonoInst *ins, *rvar = NULL;
2014 MonoMethodHeader *cheader;
2015 MonoBasicBlock *ebblock, *sbblock;
2016 int i, costs, new_locals_offset;
2018 if (cfg->verbose_level > 2)
2019 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2021 cheader = ((MonoMethodNormal *)cmethod)->header;
2023 if (!cmethod->inline_info) {
2024 mono_jit_stats.inlineable_methods++;
2025 cmethod->inline_info = 1;
2027 /* allocate space to store the return value */
2028 if (!MONO_TYPE_IS_VOID (fsig->ret))
2029 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2031 /* allocate local variables */
2032 new_locals_offset = cfg->num_varinfo;
2033 for (i = 0; i < cheader->num_locals; ++i)
2034 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2036 /* allocate starte and end blocks */
2037 sbblock = NEW_BBLOCK (cfg);
2038 sbblock->block_num = cfg->num_bblocks++;
2039 sbblock->real_offset = real_offset;
2041 ebblock = NEW_BBLOCK (cfg);
2042 ebblock->block_num = cfg->num_bblocks++;
2043 ebblock->real_offset = real_offset;
2045 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset);
2047 if (costs >= 0 && costs < 60) {
2048 if (cfg->verbose_level > 2)
2049 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2051 mono_jit_stats.inlined_methods++;
2053 /* always add some code to avoid block split failures */
2054 MONO_INST_NEW (cfg, ins, CEE_NOP);
2055 MONO_ADD_INS (bblock, ins);
2058 bblock->next_bb = sbblock;
2059 link_bblock (cfg, bblock, sbblock);
2062 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2068 if (cfg->verbose_level > 2)
2069 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2075 * Some of these comments may well be out-of-date.
2076 * Design decisions: we do a single pass over the IL code (and we do bblock
2077 * splitting/merging in the few cases when it's required: a back jump to an IL
2078 * address that was not already seen as bblock starting point).
2079 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2080 * Complex operations are decomposed in simpler ones right away. We need to let the
2081 * arch-specific code peek and poke inside this process somehow (except when the
2082 * optimizations can take advantage of the full semantic info of coarse opcodes).
2083 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2084 * MonoInst->opcode initially is the IL opcode or some simplification of that
2085 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2086 * opcode with value bigger than OP_LAST.
2087 * At this point the IR can be handed over to an interpreter, a dumb code generator
2088 * or to the optimizing code generator that will translate it to SSA form.
2090 * Profiling directed optimizations.
2091 * We may compile by default with few or no optimizations and instrument the code
2092 * or the user may indicate what methods to optimize the most either in a config file
2093 * or through repeated runs where the compiler applies offline the optimizations to
2094 * each method and then decides if it was worth it.
2097 * * consider using an array instead of an hash table (bb_hash)
2100 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2101 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2102 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2104 /* offset from br.s -> br like opcodes */
2105 #define BIG_BRANCH_OFFSET 13
2108 * mono_method_to_ir: translates IL into basic blocks containing trees
2111 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2112 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2113 guint inline_offset)
2115 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2116 MonoInst *ins, **sp, **stack_start;
2117 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2119 MonoMethod *cmethod;
2120 MonoInst **arg_array;
2121 MonoMethodHeader *header;
2123 guint32 token, ins_flag;
2125 unsigned char *ip, *end, *target;
2126 static double r8_0 = 0.0;
2127 MonoMethodSignature *sig;
2128 MonoType **param_types;
2129 GList *bb_recheck = NULL, *tmp;
2130 int i, n, start_new_bblock, align;
2131 int num_calls = 0, inline_costs = 0;
2132 int *filter_lengths = NULL;
2133 int breakpoint_id = 0;
2136 image = method->klass->image;
2137 header = ((MonoMethodNormal *)method)->header;
2138 sig = method->signature;
2139 ip = (unsigned char*)header->code;
2140 end = ip + header->code_size;
2141 mono_jit_stats.cil_code_size += header->code_size;
2143 if (cfg->method == method) {
2145 bbhash = cfg->bb_hash;
2147 real_offset = inline_offset;
2148 bbhash = g_hash_table_new (g_direct_hash, NULL);
2151 dont_inline = g_list_prepend (dont_inline, method);
2152 if (cfg->method == method) {
2155 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2156 start_bblock->cil_code = NULL;
2157 start_bblock->cil_length = 0;
2158 start_bblock->block_num = cfg->num_bblocks++;
2161 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2162 end_bblock->cil_code = NULL;
2163 end_bblock->cil_length = 0;
2164 end_bblock->block_num = cfg->num_bblocks++;
2165 g_assert (cfg->num_bblocks == 2);
2167 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2168 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2169 arg_array [i] = cfg->varinfo [i];
2171 if (mono_compile_aot)
2172 cfg->opt |= MONO_OPT_SAHRED;
2174 if (header->num_clauses) {
2175 int size = sizeof (int) * header->num_clauses;
2176 filter_lengths = alloca (size);
2177 memset (filter_lengths, 0, size);
2179 /* handle exception clauses */
2180 for (i = 0; i < header->num_clauses; ++i) {
2181 //unsigned char *p = ip;
2182 MonoExceptionClause *clause = &header->clauses [i];
2183 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2184 tblock->real_offset = clause->try_offset;
2185 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2186 tblock->real_offset = clause->handler_offset;
2187 /*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);
2189 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2191 /* catch and filter blocks get the exception object on the stack */
2192 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2193 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2194 /* mostly like handle_stack_args (), but just sets the input args */
2195 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2197 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2198 /* prevent it from being register allocated */
2199 cfg->exvar->flags |= MONO_INST_INDIRECT;
2201 tblock->in_scount = 1;
2202 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2203 tblock->in_stack [0] = cfg->exvar;
2204 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2205 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2206 tblock->real_offset = clause->token_or_filter;
2207 tblock->in_scount = 1;
2208 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2209 tblock->in_stack [0] = cfg->exvar;
2215 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2216 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2219 /* FIRST CODE BLOCK */
2220 bblock = NEW_BBLOCK (cfg);
2221 bblock->cil_code = ip;
2223 ADD_BBLOCK (cfg, bbhash, bblock);
2225 if (cfg->method == method) {
2226 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2227 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2228 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2229 MONO_ADD_INS (bblock, ins);
2233 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SAHRED)))) {
2234 /* we use a separate basic block for the initialization code */
2235 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2236 init_localsbb->real_offset = real_offset;
2237 start_bblock->next_bb = init_localsbb;
2238 init_localsbb->next_bb = bblock;
2239 link_bblock (cfg, start_bblock, init_localsbb);
2240 link_bblock (cfg, init_localsbb, bblock);
2241 init_localsbb->block_num = cfg->num_bblocks++;
2243 start_bblock->next_bb = bblock;
2244 link_bblock (cfg, start_bblock, bblock);
2247 mono_debug_init_method (cfg, bblock, breakpoint_id);
2249 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2251 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2252 for (n = 0; n < sig->param_count; ++n)
2253 param_types [n + sig->hasthis] = sig->params [n];
2255 /* do this somewhere outside - not here */
2256 NEW_ICONST (cfg, zero_int32, 0);
2257 NEW_ICONST (cfg, zero_int64, 0);
2258 zero_int64->type = STACK_I8;
2259 NEW_PCONST (cfg, zero_ptr, 0);
2260 NEW_PCONST (cfg, zero_obj, 0);
2261 zero_obj->type = STACK_OBJ;
2263 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2264 zero_r8->type = STACK_R8;
2265 zero_r8->inst_p0 = &r8_0;
2267 /* add a check for this != NULL to inlined methods */
2268 if (cfg->method != method && sig->hasthis) {
2269 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2270 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2272 MONO_ADD_INS (bblock, ins);
2275 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2276 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2279 start_new_bblock = 0;
2282 if (cfg->method == method)
2283 real_offset = ip - header->code;
2285 real_offset = inline_offset;
2287 if (start_new_bblock) {
2288 bblock->cil_length = ip - bblock->cil_code;
2289 if (start_new_bblock == 2) {
2290 g_assert (ip == tblock->cil_code);
2292 GET_BBLOCK (cfg, bbhash, tblock, ip);
2294 bblock->next_bb = tblock;
2296 start_new_bblock = 0;
2297 for (i = 0; i < bblock->in_scount; ++i) {
2298 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2302 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2303 link_bblock (cfg, bblock, tblock);
2304 if (sp != stack_start) {
2305 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2308 bblock->next_bb = tblock;
2310 for (i = 0; i < bblock->in_scount; ++i) {
2311 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2317 if (cfg->verbose_level > 3)
2318 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2325 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2326 ins->cil_code = ip++;
2327 MONO_ADD_INS (bblock, ins);
2333 CHECK_STACK_OVF (1);
2334 n = (*ip)-CEE_LDARG_0;
2335 NEW_ARGLOAD (cfg, ins, n);
2336 ins->cil_code = ip++;
2343 CHECK_STACK_OVF (1);
2344 n = (*ip)-CEE_LDLOC_0;
2345 NEW_LOCLOAD (cfg, ins, n);
2346 ins->cil_code = ip++;
2354 n = (*ip)-CEE_STLOC_0;
2356 handle_loaded_temps (cfg, bblock, stack_start, sp);
2357 NEW_LOCSTORE (cfg, ins, n, *sp);
2359 if (ins->opcode == CEE_STOBJ) {
2360 NEW_LOCLOADA (cfg, ins, n);
2361 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2363 MONO_ADD_INS (bblock, ins);
2368 CHECK_STACK_OVF (1);
2369 NEW_ARGLOAD (cfg, ins, ip [1]);
2375 CHECK_STACK_OVF (1);
2376 NEW_ARGLOADA (cfg, ins, ip [1]);
2384 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2385 handle_loaded_temps (cfg, bblock, stack_start, sp);
2387 if (ins->opcode == CEE_STOBJ) {
2388 NEW_ARGLOADA (cfg, ins, ip [1]);
2389 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2391 MONO_ADD_INS (bblock, ins);
2395 CHECK_STACK_OVF (1);
2396 NEW_LOCLOAD (cfg, ins, ip [1]);
2402 CHECK_STACK_OVF (1);
2403 NEW_LOCLOADA (cfg, ins, ip [1]);
2411 handle_loaded_temps (cfg, bblock, stack_start, sp);
2412 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2414 if (ins->opcode == CEE_STOBJ) {
2415 NEW_LOCLOADA (cfg, ins, ip [1]);
2416 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2418 MONO_ADD_INS (bblock, ins);
2423 CHECK_STACK_OVF (1);
2424 NEW_PCONST (cfg, ins, NULL);
2426 ins->type = STACK_OBJ;
2431 CHECK_STACK_OVF (1);
2432 NEW_ICONST (cfg, ins, -1);
2446 CHECK_STACK_OVF (1);
2447 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2453 CHECK_STACK_OVF (1);
2455 NEW_ICONST (cfg, ins, *((signed char*)ip));
2461 CHECK_STACK_OVF (1);
2462 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2468 CHECK_STACK_OVF (1);
2469 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2471 ins->type = STACK_I8;
2473 ins->inst_l = (gint64)read64 (ip);
2478 float *f = g_malloc (sizeof (float));
2479 CHECK_STACK_OVF (1);
2480 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2481 ins->type = STACK_R8;
2490 double *d = g_malloc (sizeof (double));
2491 CHECK_STACK_OVF (1);
2492 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2493 ins->type = STACK_R8;
2502 MonoInst *temp, *store;
2504 CHECK_STACK_OVF (1);
2509 * small optimization: if the loaded value was from a local already,
2510 * just load it twice.
2512 if (ins->ssa_op == MONO_SSA_LOAD &&
2513 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2515 MONO_INST_NEW (cfg, temp, 0);
2517 temp->cil_code = ip;
2520 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2521 temp->cil_code = ip;
2522 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2523 store->cil_code = ip;
2524 MONO_ADD_INS (bblock, store);
2525 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2528 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2538 MONO_INST_NEW (cfg, ins, CEE_POP);
2539 MONO_ADD_INS (bblock, ins);
2540 ins->cil_code = ip++;
2545 if (stack_start != sp)
2547 MONO_INST_NEW (cfg, ins, CEE_JMP);
2548 token = read32 (ip + 1);
2549 /* FIXME: check the signature matches */
2550 cmethod = mono_get_method (image, token, NULL);
2552 * The current magic trampoline can't handle this
2553 * apparently, so we compile the method right away.
2554 * Later, we may need to fix the trampoline or use a different one.
2556 ins->inst_p0 = mono_compile_method (cmethod);
2557 MONO_ADD_INS (bblock, ins);
2559 start_new_bblock = 1;
2563 case CEE_CALLVIRT: {
2564 MonoInst *addr = NULL;
2565 MonoMethodSignature *fsig = NULL;
2566 int temp, array_rank = 0;
2567 int virtual = *ip == CEE_CALLVIRT;
2569 token = read32 (ip + 1);
2571 if (*ip == CEE_CALLI) {
2576 if (method->wrapper_type != MONO_WRAPPER_NONE)
2577 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2579 fsig = mono_metadata_parse_signature (image, token);
2581 n = fsig->param_count + fsig->hasthis;
2584 cmethod = mono_get_method (image, token, NULL);
2586 if (!cmethod->klass->inited)
2587 mono_class_init (cmethod->klass);
2589 if (cmethod->signature->pinvoke) {
2590 #ifdef MONO_USE_EXC_TABLES
2591 if (mono_method_blittable (cmethod)) {
2592 fsig = cmethod->signature;
2595 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2596 fsig = wrapper->signature;
2597 #ifdef MONO_USE_EXC_TABLES
2601 fsig = cmethod->signature;
2604 n = fsig->param_count + fsig->hasthis;
2606 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2607 cmethod->klass->parent == mono_defaults.array_class) {
2608 array_rank = cmethod->klass->rank;
2611 if (cmethod->string_ctor)
2612 g_assert_not_reached ();
2618 //g_assert (!virtual || fsig->hasthis);
2622 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
2625 if (MONO_TYPE_IS_VOID (fsig->ret)) {
2626 MONO_ADD_INS (bblock, ins);
2628 type_to_eval_stack_type (fsig->ret, ins);
2637 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
2638 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
2639 mono_method_check_inlining (cmethod) &&
2640 !g_list_find (dont_inline, cmethod)) {
2642 MonoBasicBlock *ebblock;
2644 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
2648 GET_BBLOCK (cfg, bbhash, bblock, ip);
2649 ebblock->next_bb = bblock;
2650 link_bblock (cfg, ebblock, bblock);
2651 if (!MONO_TYPE_IS_VOID (fsig->ret))
2654 if (sp != stack_start) {
2655 handle_stack_args (cfg, ebblock, stack_start, sp - stack_start);
2658 start_new_bblock = 1;
2660 inline_costs += costs;
2665 inline_costs += 10 * num_calls++;
2666 handle_loaded_temps (cfg, bblock, stack_start, sp);
2668 /* tail recursion elimination */
2669 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
2670 gboolean has_vtargs = FALSE;
2673 /* keep it simple */
2674 for (i = fsig->param_count - 1; i >= 0; i--) {
2675 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
2680 for (i = 0; i < n; ++i) {
2681 NEW_ARGSTORE (cfg, ins, i, sp [i]);
2683 MONO_ADD_INS (bblock, ins);
2685 MONO_INST_NEW (cfg, ins, CEE_BR);
2687 MONO_ADD_INS (bblock, ins);
2688 tblock = start_bblock->out_bb [0];
2689 link_bblock (cfg, bblock, tblock);
2690 ins->inst_target_bb = tblock;
2691 start_new_bblock = 1;
2694 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2695 /* just create a dummy - the value is never used */
2696 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2697 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
2705 if (*ip == CEE_CALLI) {
2707 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
2708 NEW_TEMPLOAD (cfg, *sp, temp);
2712 } else if (array_rank) {
2713 MonoMethodSignature *esig;
2716 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
2717 esig = mono_get_element_address_signature (fsig->param_count - 1);
2719 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2720 NEW_TEMPLOAD (cfg, addr, temp);
2721 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
2723 if (ins->opcode == CEE_STOBJ) {
2724 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
2726 MONO_ADD_INS (bblock, ins);
2729 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
2730 esig = mono_get_element_address_signature (fsig->param_count);
2732 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2733 NEW_TEMPLOAD (cfg, addr, temp);
2734 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
2738 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
2740 esig = mono_get_element_address_signature (fsig->param_count);
2742 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2743 NEW_TEMPLOAD (cfg, *sp, temp);
2746 g_assert_not_reached ();
2750 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
2751 /* no need to spill */
2752 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
2755 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, virtual ? sp [0] : NULL)) != -1) {
2756 NEW_TEMPLOAD (cfg, *sp, temp);
2766 if (cfg->method != method) {
2767 /* return from inlined methode */
2772 //g_assert (returnvar != -1);
2773 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
2774 store->cil_code = sp [0]->cil_code;
2775 if (store->opcode == CEE_STOBJ) {
2776 g_assert_not_reached ();
2777 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
2778 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
2780 MONO_ADD_INS (bblock, store);
2784 g_assert (!return_var);
2787 MONO_INST_NEW (cfg, ins, CEE_NOP);
2788 ins->opcode = mono_type_to_stind (method->signature->ret);
2789 if (ins->opcode == CEE_STOBJ) {
2790 NEW_RETLOADA (cfg, ins);
2791 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2793 ins->opcode = OP_SETRET;
2795 ins->inst_i0 = *sp;;
2796 ins->inst_i1 = NULL;
2797 MONO_ADD_INS (bblock, ins);
2801 if (sp != stack_start)
2803 MONO_INST_NEW (cfg, ins, CEE_BR);
2804 ins->cil_code = ip++;
2805 ins->inst_target_bb = end_bblock;
2806 MONO_ADD_INS (bblock, ins);
2807 link_bblock (cfg, bblock, end_bblock);
2808 start_new_bblock = 1;
2811 MONO_INST_NEW (cfg, ins, CEE_BR);
2812 ins->cil_code = ip++;
2813 MONO_ADD_INS (bblock, ins);
2814 target = ip + 1 + (signed char)(*ip);
2816 GET_BBLOCK (cfg, bbhash, tblock, target);
2817 link_bblock (cfg, bblock, tblock);
2818 CHECK_BBLOCK (target, ip, tblock);
2819 ins->inst_target_bb = tblock;
2820 if (sp != stack_start) {
2821 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2824 start_new_bblock = 1;
2830 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
2831 ins->cil_code = ip++;
2832 target = ip + 1 + *(signed char*)ip;
2834 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
2835 if (sp != stack_start) {
2836 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2852 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
2853 ins->cil_code = ip++;
2854 target = ip + 1 + *(signed char*)ip;
2857 if (sp != stack_start) {
2858 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2864 MONO_INST_NEW (cfg, ins, CEE_BR);
2865 ins->cil_code = ip++;
2866 MONO_ADD_INS (bblock, ins);
2867 target = ip + 4 + (gint32)read32(ip);
2869 GET_BBLOCK (cfg, bbhash, tblock, target);
2870 link_bblock (cfg, bblock, tblock);
2871 CHECK_BBLOCK (target, ip, tblock);
2872 ins->inst_target_bb = tblock;
2873 if (sp != stack_start) {
2874 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2877 start_new_bblock = 1;
2883 MONO_INST_NEW (cfg, ins, *ip);
2884 ins->cil_code = ip++;
2885 target = ip + 4 + (gint32)read32(ip);
2887 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
2888 if (sp != stack_start) {
2889 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2905 MONO_INST_NEW (cfg, ins, *ip);
2906 ins->cil_code = ip++;
2907 target = ip + 4 + (gint32)read32(ip);
2910 if (sp != stack_start) {
2911 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2918 n = read32 (ip + 1);
2919 MONO_INST_NEW (cfg, ins, *ip);
2921 ins->inst_left = *sp;
2922 if (ins->inst_left->type != STACK_I4) goto unverified;
2925 target = ip + n * sizeof (guint32);
2926 MONO_ADD_INS (bblock, ins);
2927 GET_BBLOCK (cfg, bbhash, tblock, target);
2928 link_bblock (cfg, bblock, tblock);
2929 ins->klass = GUINT_TO_POINTER (n);
2930 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
2931 ins->inst_many_bb [n] = tblock;
2933 for (i = 0; i < n; ++i) {
2934 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
2935 link_bblock (cfg, bblock, tblock);
2936 ins->inst_many_bb [i] = tblock;
2939 /* FIXME: handle stack args */
2954 MONO_INST_NEW (cfg, ins, *ip);
2959 ins->type = ldind_type [*ip - CEE_LDIND_I1];
2960 ins->flags |= ins_flag;
2972 MONO_INST_NEW (cfg, ins, *ip);
2973 ins->cil_code = ip++;
2975 handle_loaded_temps (cfg, bblock, stack_start, sp);
2976 MONO_ADD_INS (bblock, ins);
2977 ins->inst_i0 = sp [0];
2978 ins->inst_i1 = sp [1];
2979 ins->flags |= ins_flag;
3010 case CEE_CONV_OVF_I8:
3011 case CEE_CONV_OVF_U8:
3017 case CEE_CONV_OVF_I4:
3018 case CEE_CONV_OVF_I1:
3019 case CEE_CONV_OVF_I2:
3020 case CEE_CONV_OVF_I:
3021 case CEE_CONV_OVF_U:
3024 if (sp [-1]->type == STACK_R8) {
3025 ADD_UNOP (CEE_CONV_OVF_I8);
3033 case CEE_CONV_OVF_U1:
3034 case CEE_CONV_OVF_U2:
3035 case CEE_CONV_OVF_U4:
3038 if (sp [-1]->type == STACK_R8) {
3039 ADD_UNOP (CEE_CONV_OVF_U8);
3047 case CEE_CONV_OVF_I1_UN:
3048 case CEE_CONV_OVF_I2_UN:
3049 case CEE_CONV_OVF_I4_UN:
3050 case CEE_CONV_OVF_I8_UN:
3051 case CEE_CONV_OVF_U1_UN:
3052 case CEE_CONV_OVF_U2_UN:
3053 case CEE_CONV_OVF_U4_UN:
3054 case CEE_CONV_OVF_U8_UN:
3055 case CEE_CONV_OVF_I_UN:
3056 case CEE_CONV_OVF_U_UN:
3062 g_error ("opcode 0x%02x not handled", *ip);
3065 MonoInst *iargs [3];
3068 token = read32 (ip + 1);
3069 if (method->wrapper_type != MONO_WRAPPER_NONE)
3070 klass = mono_method_get_wrapper_data (method, token);
3072 klass = mono_class_get (image, token);
3074 mono_class_init (klass);
3075 n = mono_class_value_size (klass, NULL);
3076 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3077 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3079 NEW_ICONST (cfg, iargs [2], n);
3080 iargs [2]->cil_code = ip;
3082 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3083 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3090 CHECK_STACK_OVF (1);
3091 n = read32 (ip + 1);
3093 if (mono_compile_aot) {
3094 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3097 if ((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot) {
3099 MonoInst *iargs [3];
3100 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3101 NEW_IMAGECONST (cfg, iargs [1], image);
3102 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3103 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3104 NEW_TEMPLOAD (cfg, *sp, temp);
3105 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3107 NEW_PCONST (cfg, ins, NULL);
3109 ins->type = STACK_OBJ;
3110 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3117 MonoInst *iargs [2];
3120 token = read32 (ip + 1);
3121 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3122 cmethod = mono_method_get_wrapper_data (method, token);
3124 cmethod = mono_get_method (image, token, NULL);
3126 mono_class_init (cmethod->klass);
3128 n = cmethod->signature->param_count;
3131 /* move the args to allow room for 'this' in the first position */
3137 handle_loaded_temps (cfg, bblock, stack_start, sp);
3140 if (cmethod->klass->parent == mono_defaults.array_class) {
3141 NEW_METHODCONST (cfg, *sp, cmethod);
3142 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, cmethod->signature, sp, ip, FALSE);
3144 } else if (cmethod->string_ctor) {
3145 /* we simply pass a null pointer */
3146 NEW_PCONST (cfg, *sp, NULL);
3147 /* now call the string ctor */
3148 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL);
3150 if (cmethod->klass->valuetype) {
3151 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3152 temp = iargs [0]->inst_c0;
3153 NEW_TEMPLOADA (cfg, *sp, temp);
3155 NEW_DOMAINCONST (cfg, iargs [0]);
3156 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3158 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3159 NEW_TEMPLOAD (cfg, *sp, temp);
3162 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3163 mono_method_check_inlining (cmethod) &&
3164 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3165 !g_list_find (dont_inline, cmethod)) {
3167 MonoBasicBlock *ebblock;
3168 if ((costs = inline_method (cfg, cmethod, cmethod->signature, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3170 GET_BBLOCK (cfg, bbhash, bblock, ip + 5);
3171 ebblock->next_bb = bblock;
3172 link_bblock (cfg, ebblock, bblock);
3174 /*if (sp != stack_start) {
3175 handle_stack_args (cfg, ebblock, stack_start, sp - stack_start);
3178 start_new_bblock = 1;*/
3180 inline_costs += costs;
3181 /*g_print ("inlined newobj for %s\n", cmethod->klass->name);*/
3184 /* now call the actual ctor */
3185 mono_emit_method_call_spilled (cfg, bblock, cmethod, sp, ip, NULL);
3189 NEW_TEMPLOAD (cfg, *sp, temp);
3198 MONO_INST_NEW (cfg, ins, *ip);
3200 klass = mono_class_get (image, read32 (ip + 1));
3201 mono_class_init (klass);
3202 ins->type = STACK_OBJ;
3203 ins->inst_left = *sp;
3204 ins->inst_newa_class = klass;
3210 MonoInst *add, *vtoffset;
3211 /* FIXME: need to check class: move to inssel.brg? */
3214 token = read32 (ip + 1);
3215 if (method->wrapper_type != MONO_WRAPPER_NONE)
3216 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3218 klass = mono_class_get (image, token);
3219 mono_class_init (klass);
3220 MONO_INST_NEW (cfg, add, CEE_ADD);
3221 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3222 add->inst_left = *sp;
3223 add->inst_right = vtoffset;
3224 add->type = STACK_MP;
3232 MONO_INST_NEW (cfg, ins, *ip);
3234 klass = mono_class_get (image, read32 (ip + 1));
3235 mono_class_init (klass);
3236 ins->type = STACK_OBJ;
3237 ins->inst_left = *sp;
3239 ins->inst_newa_class = klass;
3246 MONO_INST_NEW (cfg, ins, *ip);
3248 ins->inst_left = *sp;
3249 ins->cil_code = ip++;
3250 MONO_ADD_INS (bblock, ins);
3252 start_new_bblock = 1;
3257 MonoInst *offset_ins;
3258 MonoClassField *field;
3261 if (*ip == CEE_STFLD) {
3268 // FIXME: enable this test later.
3269 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3271 token = read32 (ip + 1);
3272 field = mono_field_from_token (image, token, &klass);
3273 mono_class_init (klass);
3274 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3275 /* FIXME: mark instructions for use in SSA */
3276 if (*ip == CEE_STFLD) {
3277 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3278 /* fixme: we need to inline that call somehow */
3279 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3280 MonoInst *iargs [5];
3282 NEW_CLASSCONST (cfg, iargs [1], klass);
3283 NEW_FIELDCONST (cfg, iargs [2], field);
3284 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3286 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, iargs, ip, NULL);
3289 NEW_ICONST (cfg, offset_ins, foffset);
3290 MONO_INST_NEW (cfg, ins, CEE_ADD);
3292 ins->inst_left = *sp;
3293 ins->inst_right = offset_ins;
3294 ins->type = STACK_MP;
3296 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3297 store->cil_code = ip;
3298 store->inst_left = ins;
3299 store->inst_right = sp [1];
3300 handle_loaded_temps (cfg, bblock, stack_start, sp);
3301 store->flags |= ins_flag;
3303 if (store->opcode == CEE_STOBJ) {
3304 handle_stobj (cfg, bblock, ins, sp [1], ip,
3305 mono_class_from_mono_type (field->type), FALSE, FALSE);
3307 MONO_ADD_INS (bblock, store);
3310 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3311 /* fixme: we need to inline that call somehow */
3312 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3313 MonoInst *iargs [4];
3316 NEW_CLASSCONST (cfg, iargs [1], klass);
3317 NEW_FIELDCONST (cfg, iargs [2], field);
3318 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3319 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, iargs, ip, NULL);
3320 if (*ip == CEE_LDFLDA) {
3321 /* not sure howto handle this */
3322 NEW_TEMPLOADA (cfg, *sp, temp);
3324 NEW_TEMPLOAD (cfg, *sp, temp);
3328 NEW_ICONST (cfg, offset_ins, foffset);
3329 MONO_INST_NEW (cfg, ins, CEE_ADD);
3331 ins->inst_left = *sp;
3332 ins->inst_right = offset_ins;
3333 ins->type = STACK_MP;
3335 if (*ip == CEE_LDFLDA) {
3339 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3340 type_to_eval_stack_type (field->type, load);
3341 load->cil_code = ip;
3342 load->inst_left = ins;
3343 load->flags |= ins_flag;
3355 MonoClassField *field;
3358 token = read32 (ip + 1);
3360 field = mono_field_from_token (image, token, &klass);
3361 mono_class_init (klass);
3363 handle_loaded_temps (cfg, bblock, stack_start, sp);
3365 if (((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot)) {
3367 MonoInst *iargs [2];
3368 g_assert (field->parent);
3369 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3370 NEW_FIELDCONST (cfg, iargs [1], field);
3371 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3372 NEW_TEMPLOAD (cfg, ins, temp);
3374 vtable = mono_class_vtable (cfg->domain, klass);
3375 NEW_PCONST (cfg, ins, (char*)vtable->data + field->offset);
3379 /* FIXME: mark instructions for use in SSA */
3380 if (*ip == CEE_LDSFLDA) {
3382 } else if (*ip == CEE_STSFLD) {
3386 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3387 store->cil_code = ip;
3388 store->inst_left = ins;
3389 store->inst_right = sp [0];
3390 store->flags |= ins_flag;
3393 if (store->opcode == CEE_STOBJ) {
3394 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3396 MONO_ADD_INS (bblock, store);
3399 CHECK_STACK_OVF (1);
3400 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3401 type_to_eval_stack_type (field->type, load);
3402 load->cil_code = ip;
3403 load->inst_left = ins;
3405 load->flags |= ins_flag;
3407 /* fixme: dont see the problem why this does not work */
3408 //cfg->disable_aot = TRUE;
3416 token = read32 (ip + 1);
3417 if (method->wrapper_type != MONO_WRAPPER_NONE)
3418 klass = mono_method_get_wrapper_data (method, token);
3420 klass = mono_class_get (image, token);
3421 mono_class_init (klass);
3422 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
3427 MonoInst *iargs [2];
3428 MonoInst *load, *vtoffset, *add, *val, *vstore;
3433 token = read32 (ip + 1);
3434 if (method->wrapper_type != MONO_WRAPPER_NONE)
3435 klass = mono_method_get_wrapper_data (method, token);
3437 klass = mono_class_get (image, token);
3438 mono_class_init (klass);
3440 /* much like NEWOBJ */
3441 NEW_DOMAINCONST (cfg, iargs [0]);
3442 NEW_CLASSCONST (cfg, iargs [1], klass);
3444 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3445 NEW_TEMPLOAD (cfg, load, temp);
3446 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3447 MONO_INST_NEW (cfg, add, CEE_ADD);
3448 add->inst_left = load;
3449 add->inst_right = vtoffset;
3452 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
3453 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
3454 vstore->cil_code = ip;
3455 vstore->inst_left = add;
3456 vstore->inst_right = val;
3458 if (vstore->opcode == CEE_STOBJ) {
3459 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
3461 MONO_ADD_INS (bblock, vstore);
3463 NEW_TEMPLOAD (cfg, load, temp);
3471 MONO_INST_NEW (cfg, ins, *ip);
3475 token = read32 (ip + 1);
3477 /* allocate the domainvar - becaus this is used in decompose_foreach */
3478 if ((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot)
3479 mono_get_domainvar (cfg);
3481 if (method->wrapper_type != MONO_WRAPPER_NONE)
3482 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3484 klass = mono_class_get (image, token);
3486 mono_class_init (klass);
3487 ins->inst_newa_class = klass;
3488 ins->inst_newa_len = *sp;
3489 ins->type = STACK_OBJ;
3496 MONO_INST_NEW (cfg, ins, *ip);
3497 ins->cil_code = ip++;
3499 ins->inst_left = *sp;
3500 ins->type = STACK_PTR;
3506 klass = mono_class_get (image, read32 (ip + 1));
3507 mono_class_init (klass);
3508 NEW_LDELEMA (cfg, ins, sp, klass);
3523 case CEE_LDELEM_REF: {
3527 * ldind.x (ldelema (array, index))
3528 * ldelema does the bounds check
3532 klass = array_access_to_klass (*ip);
3533 NEW_LDELEMA (cfg, load, sp, klass);
3534 load->cil_code = ip;
3535 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
3537 ins->inst_left = load;
3539 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
3549 case CEE_STELEM_R8: {
3553 * stind.x (ldelema (array, index), val)
3554 * ldelema does the bounds check
3558 klass = array_access_to_klass (*ip);
3559 NEW_LDELEMA (cfg, load, sp, klass);
3560 load->cil_code = ip;
3561 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
3563 ins->inst_left = load;
3564 ins->inst_right = sp [2];
3566 handle_loaded_temps (cfg, bblock, stack_start, sp);
3567 MONO_ADD_INS (bblock, ins);
3568 /* FIXME: add the implicit STELEM_REF castclass */
3570 cfg->disable_ssa = TRUE;
3573 case CEE_STELEM_REF: {
3574 MonoInst *iargs [3];
3579 handle_loaded_temps (cfg, bblock, stack_start, sp);
3585 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
3589 NEW_GROUP (cfg, group, sp [0], sp [1]);
3590 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
3592 ins->inst_left = group;
3593 ins->inst_right = sp [2];
3594 MONO_ADD_INS (bblock, ins);
3599 cfg->disable_ssa = TRUE;
3602 case CEE_CKFINITE: {
3603 MonoInst *store, *temp;
3606 /* this instr. can throw exceptions as side effect,
3607 * so we cant eliminate dead code which contains CKFINITE opdodes.
3608 * Spilling to memory makes sure that we always perform
3612 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
3614 ins->inst_left = sp [-1];
3615 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
3617 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
3618 store->cil_code = ip;
3619 MONO_ADD_INS (bblock, store);
3621 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
3628 g_error ("opcode 0x%02x not handled", *ip);
3632 MonoClass *handle_class;
3634 CHECK_STACK_OVF (1);
3636 n = read32 (ip + 1);
3638 handle = mono_ldtoken (image, n, &handle_class);
3639 mono_class_init (handle_class);
3641 if (((cfg->opt & MONO_OPT_SAHRED) || mono_compile_aot)) {
3643 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
3645 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
3647 NEW_IMAGECONST (cfg, iargs [0], image);
3648 NEW_ICONST (cfg, iargs [1], n);
3649 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
3650 NEW_TEMPLOAD (cfg, res, temp);
3651 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
3652 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
3653 MONO_ADD_INS (bblock, store);
3654 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
3656 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
3657 (cmethod->klass == mono_defaults.monotype_class->parent) &&
3658 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
3659 MonoClass *tclass = mono_class_from_mono_type (handle);
3660 mono_class_init (tclass);
3661 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
3662 ins->type = STACK_OBJ;
3663 ins->klass = cmethod->klass;
3666 NEW_PCONST (cfg, ins, handle);
3667 ins->type = STACK_VTYPE;
3668 ins->klass = handle_class;
3684 case CEE_ADD_OVF_UN:
3686 case CEE_MUL_OVF_UN:
3688 case CEE_SUB_OVF_UN:
3693 case CEE_ENDFINALLY:
3694 /* FIXME: check stack state */
3695 MONO_INST_NEW (cfg, ins, *ip);
3696 MONO_ADD_INS (bblock, ins);
3697 ins->cil_code = ip++;
3698 start_new_bblock = 1;
3702 if (*ip == CEE_LEAVE) {
3703 target = ip + 5 + (gint32)read32(ip + 1);
3705 target = ip + 2 + (signed char)(ip [1]);
3708 /* empty the stack */
3709 while (sp != stack_start) {
3710 MONO_INST_NEW (cfg, ins, CEE_POP);
3714 MONO_ADD_INS (bblock, ins);
3717 /* fixme: call fault handler ? */
3719 if ((tblock = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
3720 link_bblock (cfg, bblock, tblock);
3721 MONO_INST_NEW (cfg, ins, OP_HANDLER);
3723 ins->inst_target_bb = tblock;
3724 MONO_ADD_INS (bblock, ins);
3728 MONO_INST_NEW (cfg, ins, CEE_BR);
3730 MONO_ADD_INS (bblock, ins);
3731 GET_BBLOCK (cfg, bbhash, tblock, target);
3732 link_bblock (cfg, bblock, tblock);
3733 CHECK_BBLOCK (target, ip, tblock);
3734 ins->inst_target_bb = tblock;
3735 start_new_bblock = 1;
3737 if (*ip == CEE_LEAVE)
3746 MONO_INST_NEW (cfg, ins, *ip);
3748 handle_loaded_temps (cfg, bblock, stack_start, sp);
3749 MONO_ADD_INS (bblock, ins);
3750 ins->cil_code = ip++;
3751 ins->inst_i0 = sp [0];
3752 ins->inst_i1 = sp [1];
3760 /* trampoline mono specific opcodes */
3761 case MONO_CUSTOM_PREFIX: {
3763 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
3767 case CEE_MONO_FUNC1: {
3769 gpointer func = NULL;
3774 case MONO_MARSHAL_CONV_STR_LPWSTR:
3775 func = mono_string_to_utf16;
3777 case MONO_MARSHAL_CONV_LPWSTR_STR:
3778 func = mono_string_from_utf16;
3780 case MONO_MARSHAL_CONV_LPSTR_STR:
3781 func = mono_string_new_wrapper;
3783 case MONO_MARSHAL_CONV_STR_LPTSTR:
3784 case MONO_MARSHAL_CONV_STR_LPSTR:
3785 func = mono_string_to_utf8;
3787 case MONO_MARSHAL_CONV_STR_BSTR:
3788 func = mono_string_to_bstr;
3790 case MONO_MARSHAL_CONV_STR_TBSTR:
3791 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
3792 func = mono_string_to_ansibstr;
3794 case MONO_MARSHAL_CONV_SB_LPSTR:
3795 func = mono_string_builder_to_utf8;
3797 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
3798 func = mono_array_to_savearray;
3800 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
3801 func = mono_array_to_lparray;
3803 case MONO_MARSHAL_CONV_DEL_FTN:
3804 func = mono_delegate_to_ftnptr;
3806 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
3807 func = mono_marshal_string_array;
3810 g_warning ("unknown conversion %d\n", ip [2]);
3811 g_assert_not_reached ();
3814 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3815 NEW_TEMPLOAD (cfg, *sp, temp);
3819 inline_costs += 10 * num_calls++;
3822 case CEE_MONO_PROC2: {
3823 gpointer func = NULL;
3828 case MONO_MARSHAL_CONV_LPSTR_SB:
3829 func = mono_string_utf8_to_builder;
3831 case MONO_MARSHAL_FREE_ARRAY:
3832 func = mono_marshal_free_array;
3835 g_assert_not_reached ();
3838 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3840 inline_costs += 10 * num_calls++;
3843 case CEE_MONO_PROC3: {
3844 gpointer func = NULL;
3849 case MONO_MARSHAL_CONV_STR_BYVALSTR:
3850 func = mono_string_to_byvalstr;
3852 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
3853 func = mono_string_to_byvalwstr;
3856 g_assert_not_reached ();
3859 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
3861 inline_costs += 10 * num_calls++;
3867 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
3869 inline_costs += 10 * num_calls++;
3871 case CEE_MONO_LDPTR:
3872 CHECK_STACK_OVF (1);
3873 token = read32 (ip + 2);
3874 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
3878 inline_costs += 10 * num_calls++;
3880 case CEE_MONO_VTADDR:
3883 MONO_INST_NEW (cfg, ins, OP_VTADDR);
3885 ins->type = STACK_MP;
3886 ins->inst_left = *sp;
3890 case CEE_MONO_NEWOBJ: {
3891 MonoInst *iargs [2];
3893 CHECK_STACK_OVF (1);
3894 token = read32 (ip + 2);
3895 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3896 mono_class_init (klass);
3897 NEW_DOMAINCONST (cfg, iargs [0]);
3898 NEW_CLASSCONST (cfg, iargs [1], klass);
3899 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3900 NEW_TEMPLOAD (cfg, *sp, temp);
3903 inline_costs += 10 * num_calls++;
3906 case CEE_MONO_OBJADDR:
3909 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
3911 ins->type = STACK_MP;
3912 ins->inst_left = *sp;
3916 case CEE_MONO_LDNATIVEOBJ:
3918 token = read32 (ip + 2);
3919 klass = mono_method_get_wrapper_data (method, token);
3920 g_assert (klass->valuetype);
3921 mono_class_init (klass);
3922 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
3926 case CEE_MONO_RETOBJ:
3927 g_assert (cfg->ret);
3928 g_assert (method->signature->pinvoke);
3932 token = read32 (ip + 2);
3933 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3935 NEW_RETLOADA (cfg, ins);
3936 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
3938 if (sp != stack_start)
3941 MONO_INST_NEW (cfg, ins, CEE_BR);
3943 ins->inst_target_bb = end_bblock;
3944 MONO_ADD_INS (bblock, ins);
3945 link_bblock (cfg, bblock, end_bblock);
3946 start_new_bblock = 1;
3950 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
3958 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
3967 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
3968 MONO_INST_NEW (cfg, ins, cmp->opcode);
3970 cmp->inst_i0 = sp [0];
3971 cmp->inst_i1 = sp [1];
3975 cmp->opcode = OP_COMPARE;
3977 ins->type = STACK_I4;
3987 CHECK_STACK_OVF (1);
3988 n = read32 (ip + 2);
3989 if (method->wrapper_type != MONO_WRAPPER_NONE)
3990 cmethod = mono_method_get_wrapper_data (method, n);
3992 cmethod = mono_get_method (image, n, NULL);
3995 * We can't do this in mono_ldftn, since it is used in
3996 * the synchronized wrapper, leading to an infinite loop.
3998 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
3999 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4002 mono_class_init (cmethod->klass);
4003 handle_loaded_temps (cfg, bblock, stack_start, sp);
4005 NEW_METHODCONST (cfg, argconst, cmethod);
4006 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4007 NEW_TEMPLOAD (cfg, *sp, temp);
4011 inline_costs += 10 * num_calls++;
4014 case CEE_LDVIRTFTN: {
4019 n = read32 (ip + 2);
4020 if (method->wrapper_type != MONO_WRAPPER_NONE)
4021 cmethod = mono_method_get_wrapper_data (method, n);
4023 cmethod = mono_get_method (image, n, NULL);
4025 mono_class_init (cmethod->klass);
4026 handle_loaded_temps (cfg, bblock, stack_start, sp);
4030 NEW_METHODCONST (cfg, args [1], cmethod);
4031 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4032 NEW_TEMPLOAD (cfg, *sp, temp);
4036 inline_costs += 10 * num_calls++;
4040 CHECK_STACK_OVF (1);
4041 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4047 CHECK_STACK_OVF (1);
4048 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4056 handle_loaded_temps (cfg, bblock, stack_start, sp);
4057 n = read16 (ip + 2);
4058 NEW_ARGSTORE (cfg, ins, n, *sp);
4060 if (ins->opcode == CEE_STOBJ) {
4061 NEW_ARGLOADA (cfg, ins, n);
4062 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4064 MONO_ADD_INS (bblock, ins);
4068 CHECK_STACK_OVF (1);
4069 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4075 CHECK_STACK_OVF (1);
4076 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4084 n = read16 (ip + 2);
4085 handle_loaded_temps (cfg, bblock, stack_start, sp);
4086 NEW_LOCSTORE (cfg, ins, n, *sp);
4088 if (ins->opcode == CEE_STOBJ) {
4089 NEW_LOCLOADA (cfg, ins, n);
4090 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4092 MONO_ADD_INS (bblock, ins);
4099 if (sp != stack_start)
4101 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4102 ins->inst_left = *sp;
4105 if (header->init_locals)
4106 ins->flags |= MONO_INST_INIT;
4110 /* FIXME: set init flag if locals init is set in this method */
4112 case CEE_ENDFILTER: {
4113 MonoExceptionClause *clause, *nearest;
4114 int cc, nearest_num;
4118 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4120 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4121 ins->inst_left = *sp;
4123 MONO_ADD_INS (bblock, ins);
4124 start_new_bblock = 1;
4128 for (cc = 0; cc < header->num_clauses; ++cc) {
4129 clause = &header->clauses [cc];
4130 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4131 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4137 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4141 case CEE_UNALIGNED_:
4142 ins_flag |= MONO_INST_UNALIGNED;
4146 ins_flag |= MONO_INST_VOLATILE;
4150 ins_flag |= MONO_INST_TAILCALL;
4156 token = read32 (ip + 2);
4157 if (method->wrapper_type != MONO_WRAPPER_NONE)
4158 klass = mono_method_get_wrapper_data (method, token);
4160 klass = mono_class_get (image, token);
4161 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4167 MonoInst *iargs [3];
4173 handle_loaded_temps (cfg, bblock, stack_start, sp);
4174 if (ip [1] == CEE_CPBLK) {
4175 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4177 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4185 /* FIXME: check we are in a catch handler */
4186 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4187 load->cil_code = ip;
4188 MONO_INST_NEW (cfg, ins, CEE_THROW);
4189 ins->inst_left = load;
4191 MONO_ADD_INS (bblock, ins);
4193 start_new_bblock = 1;
4198 CHECK_STACK_OVF (1);
4199 token = read32 (ip + 2);
4200 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4201 MonoType *type = mono_type_create_from_typespec (image, token);
4202 token = mono_type_size (type, &align);
4203 mono_metadata_free_type (type);
4205 MonoClass *szclass = mono_class_get (image, token);
4206 mono_class_init (szclass);
4207 token = mono_class_value_size (szclass, &align);
4209 NEW_ICONST (cfg, ins, token);
4214 case CEE_REFANYTYPE:
4215 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4218 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4223 g_error ("opcode 0x%02x not handled", *ip);
4226 if (start_new_bblock != 1)
4229 bblock->cil_length = ip - bblock->cil_code;
4230 bblock->next_bb = end_bblock;
4231 link_bblock (cfg, bblock, end_bblock);
4233 if (cfg->method == method && cfg->domainvar) {
4237 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4238 call->signature = helper_sig_domain_get;
4239 call->inst.type = STACK_PTR;
4240 call->fptr = mono_domain_get;
4241 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4243 MONO_ADD_INS (init_localsbb, store);
4246 if (header->init_locals) {
4248 for (i = 0; i < header->num_locals; ++i) {
4249 int t = header->locals [i]->type;
4250 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4251 t = header->locals [i]->data.klass->enum_basetype->type;
4252 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4253 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4254 NEW_ICONST (cfg, ins, 0);
4255 NEW_LOCSTORE (cfg, store, i, ins);
4256 MONO_ADD_INS (init_localsbb, store);
4257 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4258 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4259 ins->type = STACK_I8;
4261 NEW_LOCSTORE (cfg, store, i, ins);
4262 MONO_ADD_INS (init_localsbb, store);
4263 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4264 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4265 ins->type = STACK_R8;
4266 ins->inst_p0 = (void*)&r8_0;
4267 NEW_LOCSTORE (cfg, store, i, ins);
4268 MONO_ADD_INS (init_localsbb, store);
4269 } else if (t == MONO_TYPE_VALUETYPE) {
4270 NEW_LOCLOADA (cfg, ins, i);
4271 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4274 NEW_PCONST (cfg, ins, NULL);
4275 NEW_LOCSTORE (cfg, store, i, ins);
4276 MONO_ADD_INS (init_localsbb, store);
4282 /* resolve backward branches in the middle of an existing basic block */
4283 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
4285 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
4286 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
4287 if (tblock != start_bblock) {
4289 split_bblock (cfg, tblock, bblock);
4290 l = bblock->cil_code - header->code;
4291 bblock->cil_length = tblock->cil_length - l;
4292 tblock->cil_length = l;
4294 g_print ("recheck failed.\n");
4298 /* we compute regions here, because the length of filter clauses is not known in advance.
4299 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
4300 if (cfg->method == method) {
4302 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
4303 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
4304 if (cfg->verbose_level > 2)
4305 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
4308 g_hash_table_destroy (bbhash);
4311 dont_inline = g_list_remove (dont_inline, method);
4312 return inline_costs;
4315 if (cfg->method != method)
4316 g_hash_table_destroy (bbhash);
4317 dont_inline = g_list_remove (dont_inline, method);
4321 if (cfg->method != method)
4322 g_hash_table_destroy (bbhash);
4323 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
4324 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
4325 dont_inline = g_list_remove (dont_inline, method);
4330 mono_print_tree (MonoInst *tree) {
4336 arity = mono_burg_arity [tree->opcode];
4338 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
4340 switch (tree->opcode) {
4342 printf ("[%d]", tree->inst_c0);
4345 printf ("[%lld]", tree->inst_l);
4348 printf ("[%f]", *(double*)tree->inst_p0);
4351 printf ("[%f]", *(float*)tree->inst_p0);
4355 printf ("[%d]", tree->inst_c0);
4358 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
4361 printf ("[%s]", mono_arch_regname (tree->dreg));
4364 printf ("[%s]", tree->inst_newa_class->name);
4365 mono_print_tree (tree->inst_newa_len);
4376 case OP_VOIDCALLVIRT: {
4377 MonoCallInst *call = (MonoCallInst*)tree;
4379 printf ("[%s]", call->method->name);
4384 printf ("[%d (", tree->inst_c0);
4385 for (i = 0; i < tree->inst_phi_args [0]; i++) {
4388 printf ("%d", tree->inst_phi_args [i + 1]);
4400 printf ("[B%d]", tree->inst_target_bb->block_num);
4410 case OP_VOIDCALL_REG:
4411 mono_print_tree (tree->inst_left);
4423 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
4424 mono_print_tree (tree->inst_left);
4428 mono_print_tree (tree->inst_left);
4430 mono_print_tree (tree->inst_right);
4440 create_helper_signature (void)
4442 /* FIXME: set call conv */
4443 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
4444 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4445 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
4446 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
4447 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
4448 helper_sig_newarr->pinvoke = 1;
4450 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
4451 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4452 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
4453 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
4454 helper_sig_object_new->pinvoke = 1;
4456 /* void* mono_method_compile (MonoMethod*) */
4457 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4458 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
4459 helper_sig_compile->pinvoke = 1;
4461 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
4462 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4463 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
4464 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
4465 helper_sig_compile_virt->pinvoke = 1;
4467 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
4468 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4469 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
4470 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
4471 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
4472 helper_sig_ldstr->pinvoke = 1;
4474 /* MonoDomain *mono_domain_get (void) */
4475 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4476 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
4477 helper_sig_domain_get->pinvoke = 1;
4479 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
4480 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4481 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
4482 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
4483 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
4484 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
4485 helper_sig_stelem_ref->pinvoke = 1;
4487 /* long amethod (long, long) */
4488 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4489 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
4490 &mono_defaults.int64_class->byval_arg;
4491 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
4492 helper_sig_long_long_long->pinvoke = 1;
4494 /* object amethod (intptr) */
4495 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4496 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4497 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
4498 helper_sig_obj_ptr->pinvoke = 1;
4500 /* void amethod (intptr) */
4501 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4502 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4503 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
4504 helper_sig_void_ptr->pinvoke = 1;
4506 /* void amethod (MonoObject *obj) */
4507 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4508 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
4509 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
4510 helper_sig_void_obj->pinvoke = 1;
4512 /* intptr amethod (void) */
4513 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4514 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
4515 helper_sig_ptr_void->pinvoke = 1;
4517 /* void amethod (intptr, intptr) */
4518 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4519 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4520 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4521 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4522 helper_sig_void_ptr_ptr->pinvoke = 1;
4524 /* void amethod (intptr, intptr, intptr) */
4525 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4526 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4527 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4528 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
4529 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
4530 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
4532 /* intptr amethod (intptr, intptr) */
4533 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4534 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
4535 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
4536 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
4537 helper_sig_ptr_ptr_ptr->pinvoke = 1;
4539 /* IntPtr amethod (object) */
4540 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4541 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
4542 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
4543 helper_sig_ptr_obj->pinvoke = 1;
4545 /* long amethod (long, guint32) */
4546 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4547 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
4548 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
4549 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
4550 helper_sig_long_long_int->pinvoke = 1;
4552 /* ulong amethod (double) */
4553 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4554 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
4555 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
4556 helper_sig_ulong_double->pinvoke = 1;
4558 /* long amethod (double) */
4559 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4560 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
4561 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
4562 helper_sig_long_double->pinvoke = 1;
4564 /* uint amethod (double) */
4565 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4566 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
4567 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
4568 helper_sig_uint_double->pinvoke = 1;
4570 /* int amethod (double) */
4571 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4572 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
4573 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
4574 helper_sig_int_double->pinvoke = 1;
4576 /* void initobj (intptr, int size) */
4577 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4578 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
4579 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
4580 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
4581 helper_sig_initobj->pinvoke = 1;
4583 /* void memcpy (intptr, intptr, int size) */
4584 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4585 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
4586 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
4587 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
4588 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
4589 helper_sig_memcpy->pinvoke = 1;
4591 /* void memset (intptr, int val, int size) */
4592 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4593 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
4594 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
4595 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
4596 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
4597 helper_sig_memset->pinvoke = 1;
4600 static GHashTable *jit_icall_hash_name = NULL;
4601 static GHashTable *jit_icall_hash_addr = NULL;
4604 mono_find_jit_icall_by_name (const char *name)
4606 g_assert (jit_icall_hash_name);
4608 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
4609 return g_hash_table_lookup (jit_icall_hash_name, name);
4613 mono_find_jit_icall_by_addr (gconstpointer addr)
4615 g_assert (jit_icall_hash_addr);
4617 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
4621 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
4623 MonoJitICallInfo *info;
4624 MonoMethod *wrapper;
4630 if (!jit_icall_hash_name) {
4631 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
4632 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
4635 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
4636 g_warning ("jit icall already defined \"%s\"\n", name);
4637 g_assert_not_reached ();
4640 info = g_new (MonoJitICallInfo, 1);
4642 info->name = g_strdup (name);
4647 #ifdef MONO_USE_EXC_TABLES
4648 || mono_arch_has_unwind_info (func)
4651 info->wrapper = func;
4654 n = g_strdup_printf ("__icall_wrapper_%s", name);
4655 wrapper = mono_marshal_get_icall_wrapper (sig, n, func);
4656 info->wrapper = mono_jit_compile_method (wrapper);
4660 g_hash_table_insert (jit_icall_hash_name, info->name, info);
4661 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
4662 if (func != info->wrapper)
4663 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
4668 static GHashTable *emul_opcode_hash = NULL;
4670 static MonoJitICallInfo *
4671 mono_find_jit_opcode_emulation (int opcode)
4673 if (emul_opcode_hash)
4674 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
4680 mono_register_opcode_emulation (int opcode, MonoMethodSignature *sig, gpointer func)
4682 MonoJitICallInfo *info;
4685 if (!emul_opcode_hash)
4686 emul_opcode_hash = g_hash_table_new (NULL, NULL);
4688 g_assert (!sig->hasthis);
4689 g_assert (sig->param_count < 3);
4691 name = g_strdup_printf ("__emulate_%s", mono_inst_name (opcode));
4693 info = mono_register_jit_icall (func, name, sig, FALSE);
4697 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
4701 decompose_foreach (MonoInst *tree, gpointer data)
4703 static MonoJitICallInfo *newarr_info = NULL;
4705 switch (tree->opcode) {
4707 MonoCompile *cfg = data;
4708 MonoInst *iargs [3];
4710 NEW_DOMAINCONST (cfg, iargs [0]);
4711 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
4712 iargs [2] = tree->inst_newa_len;
4715 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
4716 g_assert (newarr_info);
4719 mono_emulate_opcode (cfg, tree, iargs, newarr_info);
4729 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
4731 switch (mono_burg_arity [tree->opcode]) {
4734 mono_inst_foreach (tree->inst_left, func, data);
4737 mono_inst_foreach (tree->inst_left, func, data);
4738 mono_inst_foreach (tree->inst_right, func, data);
4741 g_assert_not_reached ();
4748 mono_print_bb_code (MonoBasicBlock *bb) {
4750 MonoInst *c = bb->code;
4752 mono_print_tree (c);
4761 print_dfn (MonoCompile *cfg) {
4766 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
4768 for (i = 0; i < cfg->num_bblocks; ++i) {
4769 bb = cfg->bblocks [i];
4771 char* code1, *code2;
4772 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
4773 if (bb->last_ins->cil_code)
4774 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
4776 code2 = g_strdup ("");
4778 code1 [strlen (code1) - 1] = 0;
4779 code = g_strdup_printf ("%s -> %s", code1, code2);
4783 code = g_strdup ("\n");
4784 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
4786 MonoInst *c = bb->code;
4788 mono_print_tree (c);
4796 g_print ("\tprev:");
4797 for (j = 0; j < bb->in_count; ++j) {
4798 g_print (" BB%d", bb->in_bb [j]->block_num);
4800 g_print ("\t\tsucc:");
4801 for (j = 0; j < bb->out_count; ++j) {
4802 g_print (" BB%d", bb->out_bb [j]->block_num);
4804 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
4807 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
4810 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
4812 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
4820 * returns the offset used by spillvar. It allocates a new
4821 * spill variable if necessary.
4824 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
4826 MonoSpillInfo **si, *info;
4829 si = &cfg->spill_info;
4831 while (i <= spillvar) {
4834 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
4836 cfg->stack_offset -= sizeof (gpointer);
4837 info->offset = cfg->stack_offset;
4841 return (*si)->offset;
4847 g_assert_not_reached ();
4852 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
4856 g_assert (bb->code);
4857 bb->last_ins->next = inst;
4858 bb->last_ins = inst;
4860 bb->last_ins = bb->code = inst;
4865 mono_destroy_compile (MonoCompile *cfg)
4867 //mono_mempool_stats (cfg->mempool);
4868 g_hash_table_destroy (cfg->bb_hash);
4870 mono_regstate_free (cfg->rs);
4871 mono_mempool_destroy (cfg->mempool);
4872 g_list_free (cfg->ldstr_list);
4874 g_free (cfg->varinfo);
4880 mono_get_lmf_addr (void)
4882 MonoJitTlsData *jit_tls;
4884 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
4885 return &jit_tls->lmf;
4887 g_assert_not_reached ();
4892 * mono_thread_abort:
4893 * @obj: exception object
4895 * abort the thread, print exception information and stack trace
4898 mono_thread_abort (MonoObject *obj)
4900 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
4908 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
4910 MonoJitTlsData *jit_tls;
4913 jit_tls = g_new0 (MonoJitTlsData, 1);
4915 TlsSetValue (mono_jit_tls_id, jit_tls);
4917 jit_tls->abort_func = mono_thread_abort;
4918 jit_tls->end_of_stack = stack_start;
4920 lmf = g_new0 (MonoLMF, 1);
4926 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
4929 mono_thread_abort_dummy (MonoObject *obj)
4931 if (mono_thread_attach_aborted_cb)
4932 mono_thread_attach_aborted_cb (obj);
4934 mono_thread_abort (obj);
4938 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
4940 MonoJitTlsData *jit_tls;
4943 jit_tls = g_new0 (MonoJitTlsData, 1);
4945 TlsSetValue (mono_jit_tls_id, jit_tls);
4947 jit_tls->abort_func = mono_thread_abort_dummy;
4948 jit_tls->end_of_stack = stack_start;
4950 lmf = g_new0 (MonoLMF, 1);
4957 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
4959 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
4963 ji->data.target = target;
4964 ji->next = cfg->patch_info;
4966 cfg->patch_info = ji;
4970 mono_remove_patch_info (MonoCompile *cfg, int ip)
4972 MonoJumpInfo **ji = &cfg->patch_info;
4975 if ((*ji)->ip.i == ip)
4978 ji = &((*ji)->next);
4983 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
4984 MonoJitICallInfo *info;
4986 switch (mono_burg_arity [tree->opcode]) {
4989 dec_foreach (tree->inst_left, cfg);
4991 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
4992 MonoInst *iargs [2];
4994 iargs [0] = tree->inst_left;
4996 mono_emulate_opcode (cfg, tree, iargs, info);
5002 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5003 MonoInst *iargs [2];
5005 iargs [0] = tree->inst_i0;
5006 iargs [1] = tree->inst_i1;
5008 mono_emulate_opcode (cfg, tree, iargs, info);
5010 dec_foreach (iargs [0], cfg);
5011 dec_foreach (iargs [1], cfg);
5014 dec_foreach (tree->inst_left, cfg);
5015 dec_foreach (tree->inst_right, cfg);
5019 g_assert_not_reached ();
5021 decompose_foreach (tree, cfg);
5025 decompose_pass (MonoCompile *cfg) {
5028 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5031 cfg->prev_ins = NULL;
5032 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5033 dec_foreach (tree, cfg);
5034 cfg->prev_ins = tree;
5040 nullify_basic_block (MonoBasicBlock *bb)
5047 bb->code = bb->last_ins = NULL;
5051 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5055 for (i = 0; i < bb->out_count; i++) {
5056 MonoBasicBlock *ob = bb->out_bb [i];
5057 for (j = 0; j < ob->in_count; j++) {
5058 if (ob->in_bb [j] == orig)
5059 ob->in_bb [j] = repl;
5066 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5068 bb->out_count = bbn->out_count;
5069 bb->out_bb = bbn->out_bb;
5071 replace_basic_block (bb, bbn, bb);
5075 bb->last_ins->next = bbn->code;
5076 bb->last_ins = bbn->last_ins;
5079 bb->code = bbn->code;
5080 bb->last_ins = bbn->last_ins;
5082 bb->next_bb = bbn->next_bb;
5083 nullify_basic_block (bbn);
5087 optimize_branches (MonoCompile *cfg) {
5088 int changed = FALSE;
5089 MonoBasicBlock *bb, *bbn;
5094 /* we skip the entry block (exit is handled specially instead ) */
5095 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5097 if (bb->out_count == 1) {
5098 bbn = bb->out_bb [0];
5100 if (bb->region == bbn->region && bb->next_bb == bbn) {
5101 /* the block are in sequence anyway ... */
5104 * miguel: I do not understand what the test below does, could we
5105 * use a macro, or a comment here? opcode > CEE_BEQ && <= BLT_UN
5107 * It could also test for bb->last_in only once, and the value
5108 * could be cached (last_ins->opcode)
5110 if (bb->last_ins && (bb->last_ins->opcode == CEE_BR || (
5111 (bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN)))) {
5112 bb->last_ins->opcode = CEE_NOP;
5114 if (cfg->verbose_level > 2)
5115 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5117 /* fixme: this causes problems with inlining */
5118 if (bbn->in_count == 1) {
5120 if (bbn != cfg->bb_exit) {
5121 if (cfg->verbose_level > 2)
5122 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5123 merge_basic_blocks (bb, bbn);
5127 //mono_print_bb_code (bb);
5130 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5131 bbn = bb->last_ins->inst_target_bb;
5132 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR) {
5134 if (cfg->verbose_level > 2)
5135 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name, bb->block_num, bbn->block_num);
5136 bb->out_bb [0] = bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
5141 } else if (bb->out_count == 2) {
5142 /* fixme: this does not correctly unlink the blocks, so we get serious problems in idom code */
5143 if (0 && bb->last_ins && bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
5144 bbn = bb->last_ins->inst_true_bb;
5145 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR) {
5146 if (cfg->verbose_level > 2)
5147 g_print ("cbranch to branch triggered %d -> %d (0x%02x)\n", bb->block_num,
5148 bbn->block_num, bbn->code->opcode);
5150 if (bb->out_bb [0] == bbn) {
5151 bb->out_bb [0] = bbn->code->inst_target_bb;
5152 } else if (bb->out_bb [1] == bbn) {
5153 bb->out_bb [1] = bbn->code->inst_target_bb;
5155 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
5165 mono_compile_create_vars (MonoCompile *cfg)
5167 MonoMethodSignature *sig;
5168 MonoMethodHeader *header;
5171 header = ((MonoMethodNormal *)cfg->method)->header;
5173 sig = cfg->method->signature;
5175 if (!MONO_TYPE_IS_VOID (sig->ret)) {
5176 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
5177 cfg->ret->opcode = OP_RETARG;
5178 cfg->ret->inst_vtype = sig->ret;
5179 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
5183 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
5185 for (i = 0; i < sig->param_count; ++i)
5186 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
5188 cfg->locals_start = cfg->num_varinfo;
5190 for (i = 0; i < header->num_locals; ++i)
5191 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
5196 mono_print_code (MonoCompile *cfg)
5200 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5201 MonoInst *tree = bb->code;
5206 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
5208 for (; tree; tree = tree->next) {
5209 mono_print_tree (tree);
5214 bb->last_ins->next = NULL;
5219 extern const char * const mono_burg_rule_string [];
5222 emit_state (MonoCompile *cfg, MBState *state, int goal)
5225 int ern = mono_burg_rule (state, goal);
5226 const guint16 *nts = mono_burg_nts [ern];
5229 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
5233 // state->reg1 = state->reg2; /* chain rule */
5235 state->reg1 = mono_regstate_next_int (cfg->rs);
5236 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
5239 state->reg1 = mono_regstate_next_int (cfg->rs);
5240 state->reg2 = mono_regstate_next_int (cfg->rs);
5243 state->reg1 = mono_regstate_next_float (cfg->rs);
5250 mono_burg_kids (state, ern, kids);
5252 emit_state (cfg, kids [0], nts [0]);
5254 emit_state (cfg, kids [1], nts [1]);
5256 g_assert (!nts [3]);
5257 emit_state (cfg, kids [2], nts [2]);
5262 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
5263 if ((emit = mono_burg_func [ern]))
5264 emit (state, state->tree, cfg);
5267 #define DEBUG_SELECTION
5270 mini_select_instructions (MonoCompile *cfg)
5274 cfg->state_pool = mono_mempool_new ();
5275 cfg->rs = mono_regstate_new ();
5277 #ifdef DEBUG_SELECTION
5278 if (cfg->verbose_level >= 4) {
5279 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5280 MonoInst *tree = bb->code;
5281 g_print ("DUMP BLOCK %d:\n", bb->block_num);
5284 for (; tree; tree = tree->next) {
5285 mono_print_tree (tree);
5292 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5293 MonoInst *tree = bb->code, *next;
5299 bb->last_ins = NULL;
5302 mono_regstate_reset (cfg->rs);
5304 #ifdef DEBUG_SELECTION
5305 if (cfg->verbose_level >= 3)
5306 g_print ("LABEL BLOCK %d:\n", bb->block_num);
5308 for (; tree; tree = next) {
5310 #ifdef DEBUG_SELECTION
5311 if (cfg->verbose_level >= 3) {
5312 mono_print_tree (tree);
5317 if (!(mbstate = mono_burg_label (tree, cfg))) {
5318 g_warning ("unabled to label tree %p", tree);
5319 mono_print_tree (tree);
5321 g_assert_not_reached ();
5323 emit_state (cfg, mbstate, MB_NTERM_stmt);
5325 bb->max_ireg = cfg->rs->next_vireg;
5326 bb->max_freg = cfg->rs->next_vfreg;
5329 bb->last_ins->next = NULL;
5331 mono_mempool_empty (cfg->state_pool);
5333 mono_mempool_destroy (cfg->state_pool);
5337 mono_codegen (MonoCompile *cfg)
5339 MonoJumpInfo *patch_info;
5341 int i, max_epilog_size;
5344 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5345 cfg->spill_count = 0;
5346 /* we reuse dfn here */
5347 /* bb->dfn = bb_count++; */
5348 mono_arch_local_regalloc (cfg, bb);
5351 if (mono_trace_coverage)
5352 mono_allocate_coverage_info (cfg->method, cfg->num_bblocks);
5354 code = mono_arch_emit_prolog (cfg);
5356 if (mono_jit_profile)
5357 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
5359 cfg->code_len = code - cfg->native_code;
5360 cfg->prolog_end = cfg->code_len;
5362 mono_debug_open_method (cfg);
5364 /* emit code all basic blocks */
5365 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5366 bb->native_offset = cfg->code_len;
5367 mono_arch_output_basic_block (cfg, bb);
5369 cfg->bb_exit->native_offset = cfg->code_len;
5371 code = cfg->native_code + cfg->code_len;
5373 max_epilog_size = mono_arch_max_epilog_size (cfg);
5375 /* we always allocate code in cfg->domain->code_mp to increase locality */
5376 cfg->code_size = cfg->code_len + max_epilog_size;
5377 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
5378 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
5379 memcpy (code, cfg->native_code, cfg->code_len);
5380 g_free (cfg->native_code);
5381 cfg->native_code = code;
5382 code = cfg->native_code + cfg->code_len;
5384 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
5386 cfg->epilog_begin = cfg->code_len;
5388 if (mono_jit_profile)
5389 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
5391 cfg->code_len = code - cfg->native_code;
5393 mono_arch_emit_epilog (cfg);
5395 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5396 switch (patch_info->type) {
5397 case MONO_PATCH_INFO_ABS: {
5398 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
5400 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
5401 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
5402 patch_info->data.name = info->name;
5406 case MONO_PATCH_INFO_SWITCH: {
5407 gpointer *table = g_new (gpointer, patch_info->table_size);
5408 patch_info->ip.i = patch_info->ip.label->inst_c0;
5409 for (i = 0; i < patch_info->table_size; i++) {
5410 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
5412 patch_info->data.target = table;
5421 if (cfg->verbose_level > 1)
5422 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
5423 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
5425 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
5427 mono_debug_close_method (cfg);
5431 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
5436 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
5437 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
5439 if (cp->opcode == OP_ICONST) {
5440 if (cfg->opt & MONO_OPT_CONSPROP) {
5441 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
5445 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
5446 if (cfg->opt & MONO_OPT_COPYPROP) {
5447 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
5453 arity = mono_burg_arity [tree->opcode];
5456 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
5457 if (cfg->opt & MONO_OPT_CFOLD)
5458 mono_constant_fold_inst (tree, NULL);
5460 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
5461 if (cfg->opt & MONO_OPT_CFOLD)
5462 mono_constant_fold_inst (tree, NULL);
5464 mono_constant_fold_inst (tree, NULL);
5470 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
5474 switch (tree->opcode) {
5484 if (tree->ssa_op == MONO_SSA_NOP) {
5485 memset (acp, 0, sizeof (MonoInst *) * acp_size);
5502 case OP_VOIDCALL_REG:
5503 case OP_VOIDCALLVIRT:
5505 MonoCallInst *call = (MonoCallInst *)tree;
5506 MonoMethodSignature *sig = call->signature;
5507 int i, byref = FALSE;
5509 for (i = 0; i < sig->param_count; i++) {
5510 if (sig->params [i]->byref) {
5517 memset (acp, 0, sizeof (MonoInst *) * acp_size);
5525 arity = mono_burg_arity [tree->opcode];
5531 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
5534 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
5535 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
5538 g_assert_not_reached ();
5543 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
5545 MonoInst *tree = bb->code;
5551 for (; tree; tree = tree->next) {
5553 mono_cprop_copy_values (cfg, tree, acp);
5555 mono_cprop_invalidate_values (tree, acp, acp_size);
5557 if (tree->ssa_op == MONO_SSA_STORE &&
5558 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
5559 MonoInst *i1 = tree->inst_i1;
5561 acp [tree->inst_i0->inst_c0] = NULL;
5563 for (i = 0; i < acp_size; i++) {
5564 if (acp [i] && acp [i]->opcode != OP_ICONST &&
5565 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
5570 if (i1->opcode == OP_ICONST) {
5571 acp [tree->inst_i0->inst_c0] = i1;
5572 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
5574 if (i1->ssa_op == MONO_SSA_LOAD &&
5575 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
5576 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
5577 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
5578 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
5583 if (tree->opcode == CEE_BEQ) {
5584 g_assert (tree->inst_i0->opcode == OP_COMPARE);
5585 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
5586 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
5588 tree->opcode = CEE_BR;
5589 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
5590 tree->inst_target_bb = tree->inst_true_bb;
5592 tree->inst_target_bb = tree->inst_false_bb;
5601 mono_local_cprop (MonoCompile *cfg)
5606 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
5608 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5609 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
5610 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
5615 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
5617 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
5618 guint8 *ip = (guint8 *)header->code;
5621 int dfn = 0, i, code_size_ratio;
5623 mono_jit_stats.methods_compiled++;
5624 if (mono_jit_profile)
5625 mono_profiler_method_jit (method);
5627 cfg = g_new0 (MonoCompile, 1);
5628 cfg->method = method;
5629 cfg->mempool = mono_mempool_new ();
5631 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
5632 cfg->domain = domain;
5633 cfg->verbose_level = mini_verbose;
5636 * create MonoInst* which represents arguments and local variables
5638 mono_compile_create_vars (cfg);
5640 if (cfg->verbose_level > 2)
5641 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
5643 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0)) < 0) {
5644 mono_destroy_compile (cfg);
5645 if (mono_jit_profile)
5646 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
5650 mono_jit_stats.basic_blocks += cfg->num_bblocks;
5651 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
5653 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
5655 /* Depth-first ordering on basic blocks */
5656 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
5658 if (cfg->opt & MONO_OPT_BRANCH)
5659 optimize_branches (cfg);
5661 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
5662 if (cfg->num_bblocks != dfn + 1) {
5663 if (cfg->verbose_level > 1)
5664 g_print ("unreachable code?\n");
5665 cfg->num_bblocks = dfn + 1;
5668 if (cfg->opt & MONO_OPT_LOOP) {
5669 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
5670 mono_compute_natural_loops (cfg);
5674 /* after method_to_ir */
5678 //#define DEBUGSSA "logic_run"
5679 #define DEBUGSSA_CLASS "Tests"
5683 if (!header->num_clauses && !cfg->disable_ssa) {
5684 mono_local_cprop (cfg);
5685 mono_ssa_compute (cfg);
5689 /* fixme: add all optimizations which requires SSA */
5690 if (cfg->opt & (MONO_OPT_DEADCE)) {
5691 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
5692 mono_local_cprop (cfg);
5693 mono_ssa_compute (cfg);
5695 if (cfg->verbose_level >= 2) {
5702 /* after SSA translation */
5706 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
5707 if (cfg->comp_done & MONO_COMP_SSA) {
5708 mono_ssa_cprop (cfg);
5710 mono_local_cprop (cfg);
5714 if (cfg->comp_done & MONO_COMP_SSA) {
5715 mono_ssa_deadce (cfg);
5717 //mono_ssa_strength_reduction (cfg);
5719 mono_ssa_remove (cfg);
5721 if (cfg->opt & MONO_OPT_BRANCH)
5722 optimize_branches (cfg);
5725 /* after SSA removal */
5729 decompose_pass (cfg);
5731 if (cfg->opt & MONO_OPT_LINEARS) {
5734 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
5735 cfg->comp_done &= ~MONO_COMP_LIVENESS;
5736 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
5737 mono_analyze_liveness (cfg);
5739 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
5740 regs = mono_arch_get_global_int_regs (cfg);
5741 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
5745 //mono_print_code (cfg);
5749 /* variables are allocated after decompose, since decompose could create temps */
5750 mono_arch_allocate_vars (cfg);
5752 if (cfg->opt & MONO_OPT_CFOLD)
5753 mono_constant_fold (cfg);
5755 mini_select_instructions (cfg);
5758 if (cfg->verbose_level >= 2) {
5759 char *id = mono_method_full_name (cfg->method, FALSE);
5760 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
5764 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
5766 jinfo = g_new0 (MonoJitInfo, 1);
5767 jinfo->method = method;
5768 jinfo->code_start = cfg->native_code;
5769 jinfo->code_size = cfg->code_len;
5770 jinfo->used_regs = cfg->used_int_regs;
5772 if (header->num_clauses) {
5775 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
5776 jinfo->num_clauses = header->num_clauses;
5777 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
5778 sizeof (MonoJitExceptionInfo) * header->num_clauses);
5780 for (i = 0; i < header->num_clauses; i++) {
5781 MonoExceptionClause *ec = &header->clauses [i];
5782 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
5783 MonoBasicBlock *tblock;
5785 ei->flags = ec->flags;
5787 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5788 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
5790 ei->data.filter = cfg->native_code + tblock->native_offset;
5792 ei->data.token = ec->token_or_filter;
5795 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
5797 ei->try_start = cfg->native_code + tblock->native_offset;
5798 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
5800 ei->try_end = cfg->native_code + tblock->native_offset;
5801 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
5803 ei->handler_start = cfg->native_code + tblock->native_offset;
5808 mono_jit_info_table_add (cfg->domain, jinfo);
5810 /* collect statistics */
5811 mono_jit_stats.allocated_code_size += cfg->code_len;
5812 code_size_ratio = cfg->code_len;
5813 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
5814 mono_jit_stats.biggest_method_size = code_size_ratio;
5815 mono_jit_stats.biggest_method = method;
5817 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
5818 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
5819 mono_jit_stats.max_code_size_ratio = code_size_ratio;
5820 mono_jit_stats.max_ratio_method = method;
5822 mono_jit_stats.native_code_size += cfg->code_len;
5824 if (mono_jit_profile)
5825 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
5831 mono_jit_compile_method (MonoMethod *method)
5833 /* FIXME: later copy the code from mono */
5834 MonoDomain *target_domain, *domain = mono_domain_get ();
5836 GHashTable *jit_code_hash;
5839 if (default_opt & MONO_OPT_SAHRED)
5840 target_domain = mono_root_domain;
5842 target_domain = domain;
5844 jit_code_hash = target_domain->jit_code_hash;
5846 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
5847 mono_jit_stats.methods_lookups++;
5851 #ifdef MONO_USE_AOT_COMPILER
5852 if (!mono_compile_aot) {
5853 mono_class_init (method->klass);
5854 if ((code = mono_aot_get_method (method))) {
5855 g_hash_table_insert (jit_code_hash, method, code);
5861 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
5862 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5863 if (!method->info) {
5866 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
5867 mono_lookup_pinvoke_call (method);
5868 #ifdef MONO_USE_EXC_TABLES
5869 if (mono_method_blittable (method)) {
5870 method->info = method->addr;
5873 nm = mono_marshal_get_native_wrapper (method);
5874 method->info = mono_compile_method (nm);
5876 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
5877 //mono_debug_add_wrapper (method, nm);
5878 #ifdef MONO_USE_EXC_TABLES
5882 return method->info;
5883 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
5884 const char *name = method->name;
5887 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
5888 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
5889 /* FIXME: uhm, we need a wrapper to handle exceptions? */
5890 return (gpointer)mono_delegate_ctor;
5891 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
5892 nm = mono_marshal_get_delegate_invoke (method);
5893 return mono_jit_compile_method (nm);
5894 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
5895 nm = mono_marshal_get_delegate_begin_invoke (method);
5896 return mono_jit_compile_method (nm);
5897 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
5898 nm = mono_marshal_get_delegate_end_invoke (method);
5899 return mono_jit_compile_method (nm);
5905 cfg = mini_method_compile (method, default_opt, target_domain, 0);
5906 code = cfg->native_code;
5907 mono_destroy_compile (cfg);
5909 g_hash_table_insert (jit_code_hash, method, code);
5911 /* make sure runtime_init is called */
5912 mono_class_vtable (target_domain, method->klass);
5918 * mono_jit_runtime_invoke:
5919 * @method: the method to invoke
5920 * @obj: this pointer
5921 * @params: array of parameter values.
5922 * @exc: used to catch exceptions objects
5925 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
5928 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
5929 invoke = mono_marshal_get_runtime_invoke (method);
5930 runtime_invoke = mono_jit_compile_method (invoke);
5931 return runtime_invoke (obj, params, exc);
5934 #ifdef PLATFORM_WIN32
5935 #define GET_CONTEXT \
5936 struct sigcontext *ctx = (struct sigcontext*)_dummy;
5938 #define GET_CONTEXT \
5939 void **_p = (void **)&_dummy; \
5940 struct sigcontext *ctx = (struct sigcontext *)++_p;
5944 sigfpe_signal_handler (int _dummy)
5949 exc = mono_get_exception_divide_by_zero ();
5951 mono_arch_handle_exception (ctx, exc, FALSE);
5955 sigill_signal_handler (int _dummy)
5959 exc = mono_get_exception_execution_engine ("SIGILL");
5961 mono_arch_handle_exception (ctx, exc, FALSE);
5965 sigsegv_signal_handler (int _dummy)
5970 exc = mono_get_exception_null_reference ();
5972 mono_arch_handle_exception (ctx, exc, FALSE);
5976 sigusr1_signal_handler (int _dummy)
5981 thread = mono_thread_current ();
5983 g_assert (thread->abort_exc);
5985 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
5989 mono_runtime_install_handlers (void)
5991 #ifndef PLATFORM_WIN32
5992 struct sigaction sa;
5995 #ifdef PLATFORM_WIN32
5997 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
5998 win32_seh_set_handler(SIGILL, sigill_signal_handler);
5999 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6000 #else /* !PLATFORM_WIN32 */
6002 /* libpthreads has its own implementation of sigaction(),
6003 * but it seems to work well with our current exception
6004 * handlers. If not we must call syscall directly instead
6008 sa.sa_handler = sigfpe_signal_handler;
6009 sigemptyset (&sa.sa_mask);
6011 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
6012 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
6015 sa.sa_handler = sigill_signal_handler;
6016 sigemptyset (&sa.sa_mask);
6018 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6019 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
6021 /* catch thread abort signal */
6022 sa.sa_handler = sigusr1_signal_handler;
6023 sigemptyset (&sa.sa_mask);
6025 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
6026 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
6030 sa.sa_handler = sigsegv_signal_handler;
6031 sigemptyset (&sa.sa_mask);
6033 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
6034 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
6036 #endif /* PLATFORM_WIN32 */
6039 /* mono_jit_create_remoting_trampoline:
6040 * @method: pointer to the method info
6042 * Creates a trampoline which calls the remoting functions. This
6043 * is used in the vtable of transparent proxies.
6045 * Returns: a pointer to the newly created code
6048 mono_jit_create_remoting_trampoline (MonoMethod *method)
6051 guint8 *addr = NULL;
6053 if (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
6054 nm = mono_marshal_get_remoting_invoke (method);
6055 addr = mono_compile_method (nm);
6057 addr = mono_compile_method (method);
6062 static CRITICAL_SECTION ms;
6065 mini_init (const char *filename)
6069 metadata_section = &ms;
6070 InitializeCriticalSection (metadata_section);
6072 mono_jit_tls_id = TlsAlloc ();
6073 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
6077 mono_runtime_install_handlers ();
6079 mono_install_compile_method (mono_jit_compile_method);
6080 mono_install_trampoline (mono_arch_create_jit_trampoline);
6081 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
6082 mono_install_runtime_invoke (mono_jit_runtime_invoke);
6083 mono_install_handler (mono_arch_get_throw_exception ());
6084 mono_install_stack_walk (mono_jit_walk_stack);
6085 mono_install_get_config_dir ();
6087 domain = mono_init (filename);
6090 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
6091 ves_icall_get_frame_info);
6092 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
6093 ves_icall_get_trace);
6094 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
6095 mono_runtime_install_handlers);
6098 create_helper_signature ();
6100 mono_arch_register_lowlevel_calls ();
6101 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
6102 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
6104 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
6105 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
6107 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
6108 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
6109 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
6111 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
6112 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
6113 helper_sig_void_ptr, TRUE);
6116 * NOTE, NOTE, NOTE, NOTE:
6117 * when adding emulation for some opcodes, remember to also add a dummy
6118 * rule to the burg files, because we need the arity information to be correct.
6120 mono_register_opcode_emulation (OP_LMUL, helper_sig_long_long_long, mono_llmult);
6121 mono_register_opcode_emulation (OP_LMUL_OVF_UN, helper_sig_long_long_long, mono_llmult_ovf_un);
6122 mono_register_opcode_emulation (OP_LMUL_OVF, helper_sig_long_long_long, mono_llmult_ovf);
6123 mono_register_opcode_emulation (OP_LDIV, helper_sig_long_long_long, mono_lldiv);
6124 mono_register_opcode_emulation (OP_LDIV_UN, helper_sig_long_long_long, mono_lldiv_un);
6125 mono_register_opcode_emulation (OP_LREM, helper_sig_long_long_long, mono_llrem);
6126 mono_register_opcode_emulation (OP_LREM_UN, helper_sig_long_long_long, mono_llrem_un);
6128 mono_register_opcode_emulation (OP_LSHL, helper_sig_long_long_int, mono_lshl);
6129 mono_register_opcode_emulation (OP_LSHR, helper_sig_long_long_int, mono_lshr);
6130 mono_register_opcode_emulation (OP_LSHR_UN, helper_sig_long_long_int, mono_lshr_un);
6132 mono_register_opcode_emulation (OP_FCONV_TO_U8, helper_sig_ulong_double, mono_fconv_u8);
6133 mono_register_opcode_emulation (OP_FCONV_TO_U4, helper_sig_uint_double, mono_fconv_u4);
6134 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, helper_sig_long_double, mono_fconv_ovf_i8);
6135 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, helper_sig_ulong_double, mono_fconv_ovf_u8);
6137 #if SIZEOF_VOID_P == 4
6138 mono_register_opcode_emulation (OP_FCONV_TO_U, helper_sig_uint_double, mono_fconv_u4);
6140 #warning "fixme: add opcode emulation"
6143 /* other jit icalls */
6144 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
6145 helper_sig_ptr_ptr_ptr, FALSE);
6146 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
6147 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
6148 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
6149 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
6150 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
6151 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
6152 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
6153 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
6154 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
6155 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
6156 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
6157 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
6158 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
6159 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
6160 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
6161 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
6162 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
6163 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
6164 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
6165 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
6166 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
6167 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6168 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
6169 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
6170 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
6171 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
6173 mono_runtime_init (domain, mono_thread_start_cb,
6174 mono_thread_attach_cb);
6176 //mono_thread_attach (domain);
6180 MonoJitStats mono_jit_stats = {0};
6183 print_jit_stats (void)
6185 if (mono_jit_stats.enabled) {
6186 g_print ("Mono Jit statistics\n");
6187 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
6188 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
6189 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
6190 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
6191 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
6192 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
6193 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
6194 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
6195 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
6196 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
6197 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
6198 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
6199 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
6200 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
6201 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
6202 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
6203 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
6204 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
6206 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
6207 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
6208 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
6209 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
6210 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
6215 mini_cleanup (MonoDomain *domain)
6218 * mono_runtime_cleanup() and mono_domain_finalize () need to
6219 * be called early since they need the execution engine still
6220 * fully working (mono_domain_finalize may invoke managed finalizers
6221 * and mono_runtime_cleanup will wait for other threads to finish).
6223 mono_domain_finalize (domain);
6225 mono_runtime_cleanup (domain);
6227 mono_profiler_shutdown ();
6229 mono_debug_cleanup ();
6230 #ifdef PLATFORM_WIN32
6231 win32_seh_cleanup();
6234 mono_domain_unload (domain, TRUE);
6237 DeleteCriticalSection (metadata_section);
6241 mini_set_defaults (int verbose_level, guint32 opts)
6243 mini_verbose = verbose_level;