2 * mini.c: The new Mono code generator.
5 * Paolo Molaro (lupus@ximian.com)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2002 Ximian, Inc.
15 #include <mono/metadata/assembly.h>
16 #include <mono/metadata/loader.h>
17 #include <mono/metadata/cil-coff.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/metadata/opcodes.h>
23 #include <mono/metadata/mono-endian.h>
24 #include <mono/metadata/tokentype.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/marshal.h>
28 #include <mono/metadata/socket-io.h>
29 #include <mono/metadata/appdomain.h>
30 #include <mono/metadata/debug-helpers.h>
31 #include <mono/io-layer/io-layer.h>
32 #include "mono/metadata/profiler.h"
33 #include <mono/metadata/profiler-private.h>
34 #include <mono/metadata/mono-config.h>
35 #include <mono/metadata/environment.h>
36 #include <mono/metadata/mono-debug.h>
37 #include <mono/metadata/mono-debug-debugger.h>
44 #include "jit-icalls.c"
46 #define MONO_IS_COND_BRANCH(op) ((op >= CEE_BEQ && op <= CEE_BLT_UN) || (op >= OP_LBEQ && op <= OP_LBLT_UN) || (op >= OP_FBEQ && op <= OP_FBLT_UN))
48 #define MONO_CHECK_THIS(ins) (cfg->method->signature->hasthis && (ins)->ssa_op == MONO_SSA_LOAD && (ins)->inst_left->inst_c0 == 0)
50 gboolean mono_arch_handle_exception (struct sigcontext *ctx, gpointer obj, gboolean test_only);
51 static gpointer mono_jit_compile_method (MonoMethod *method);
53 static void handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src,
54 const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native);
56 static int mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
57 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
58 guint inline_offset, gboolean is_virtual_call);
60 extern guint8 mono_burg_arity [];
61 /* helper methods signature */
62 static MonoMethodSignature *helper_sig_long_long_long = NULL;
63 static MonoMethodSignature *helper_sig_long_long_int = NULL;
64 static MonoMethodSignature *helper_sig_newarr = NULL;
65 static MonoMethodSignature *helper_sig_newarr_specific = NULL;
66 static MonoMethodSignature *helper_sig_ldstr = NULL;
67 static MonoMethodSignature *helper_sig_domain_get = NULL;
68 static MonoMethodSignature *helper_sig_object_new = NULL;
69 static MonoMethodSignature *helper_sig_object_new_specific = NULL;
70 static MonoMethodSignature *helper_sig_compile = NULL;
71 static MonoMethodSignature *helper_sig_compile_virt = NULL;
72 static MonoMethodSignature *helper_sig_obj_ptr = NULL;
73 static MonoMethodSignature *helper_sig_ptr_void = NULL;
74 static MonoMethodSignature *helper_sig_void_ptr = NULL;
75 static MonoMethodSignature *helper_sig_void_obj = NULL;
76 static MonoMethodSignature *helper_sig_void_ptr_ptr = NULL;
77 static MonoMethodSignature *helper_sig_void_ptr_ptr_ptr = NULL;
78 static MonoMethodSignature *helper_sig_ptr_ptr_ptr = NULL;
79 static MonoMethodSignature *helper_sig_ptr_obj = NULL;
80 static MonoMethodSignature *helper_sig_ptr_int = NULL;
81 static MonoMethodSignature *helper_sig_initobj = NULL;
82 static MonoMethodSignature *helper_sig_memcpy = NULL;
83 static MonoMethodSignature *helper_sig_memset = NULL;
84 static MonoMethodSignature *helper_sig_ulong_double = NULL;
85 static MonoMethodSignature *helper_sig_long_double = NULL;
86 static MonoMethodSignature *helper_sig_uint_double = NULL;
87 static MonoMethodSignature *helper_sig_int_double = NULL;
88 static MonoMethodSignature *helper_sig_stelem_ref = NULL;
90 static guint32 default_opt = MONO_OPT_PEEPHOLE;
92 guint32 mono_jit_tls_id = 0;
93 gboolean mono_jit_trace_calls = FALSE;
94 gboolean mono_break_on_exc = FALSE;
95 gboolean mono_compile_aot = FALSE;
97 static int mini_verbose = 0;
99 #ifdef MONO_USE_EXC_TABLES
101 mono_type_blittable (MonoType *type)
120 case MONO_TYPE_OBJECT:
122 case MONO_TYPE_VALUETYPE:
123 case MONO_TYPE_CLASS:
124 return type->data.klass->blittable;
134 mono_method_blittable (MonoMethod *method)
136 MonoMethodSignature *sig;
142 if (!mono_arch_has_unwind_info (method->addr)) {
146 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
149 sig = method->signature;
151 if (!mono_type_blittable (sig->ret))
154 for (i = 0; i < sig->param_count; i++)
155 if (!mono_type_blittable (sig->params [i]))
163 G_GNUC_UNUSED static void
164 print_method_from_ip (void *ip)
169 MonoDomain *domain = mono_domain_get ();
171 ji = mono_jit_info_table_find (domain, ip);
173 g_print ("No method at %p\n", ip);
176 method = mono_method_full_name (ji->method, TRUE);
177 source = mono_debug_source_location_from_address (ji->method, (int) ip, NULL, domain);
179 g_print ("IP %p at offset 0x%x of method %s (%p %p)\n", ip, (char*)ip - (char*)ji->code_start, method, ji->code_start, (char*)ji->code_start + ji->code_size);
182 g_print ("%s\n", source);
189 #define MONO_INIT_VARINFO(vi,id) do { \
190 (vi)->range.first_use.pos.bid = 0xffff; \
196 * Basic blocks have two numeric identifiers:
197 * dfn: Depth First Number
198 * block_num: unique ID assigned at bblock creation
200 #define NEW_BBLOCK(cfg) (mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock)))
201 #define ADD_BBLOCK(cfg,bbhash,b) do { \
202 g_hash_table_insert (bbhash, (b)->cil_code, (b)); \
203 (b)->block_num = cfg->num_bblocks++; \
204 (b)->real_offset = real_offset; \
207 #define GET_BBLOCK(cfg,bbhash,tblock,ip) do { \
208 (tblock) = g_hash_table_lookup (bbhash, (ip)); \
210 if ((ip) >= end || (ip) < header->code) goto unverified; \
211 (tblock) = NEW_BBLOCK (cfg); \
212 (tblock)->cil_code = (ip); \
213 ADD_BBLOCK (cfg, (bbhash), (tblock)); \
215 (tblock)->real_offset = real_offset; \
218 #define CHECK_BBLOCK(target,ip,tblock) do { \
219 if ((target) < (ip) && !(tblock)->code) { \
220 bb_recheck = g_list_prepend (bb_recheck, (tblock)); \
221 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); \
225 #define NEW_ICONST(cfg,dest,val) do { \
226 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
227 (dest)->opcode = OP_ICONST; \
228 (dest)->inst_c0 = (val); \
229 (dest)->type = STACK_I4; \
232 /* FIXME: have a different definition of NEW_PCONST for 64 bit systems */
233 #define NEW_PCONST(cfg,dest,val) do { \
234 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
235 (dest)->opcode = OP_ICONST; \
236 (dest)->inst_p0 = (val); \
237 (dest)->type = STACK_PTR; \
240 #define NEW_CLASSCONST(cfg,dest,val) do { \
241 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
242 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
243 (dest)->inst_p0 = (val); \
244 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_CLASS; \
245 (dest)->type = STACK_PTR; \
248 #define NEW_IMAGECONST(cfg,dest,val) do { \
249 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
250 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
251 (dest)->inst_p0 = (val); \
252 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_IMAGE; \
253 (dest)->type = STACK_PTR; \
256 #define NEW_FIELDCONST(cfg,dest,field) do { \
257 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
258 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
259 (dest)->inst_p0 = (field); \
260 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_FIELD; \
261 (dest)->type = STACK_PTR; \
264 #define NEW_METHODCONST(cfg,dest,val) do { \
265 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
266 (dest)->opcode = mono_compile_aot ? OP_AOTCONST : OP_ICONST; \
267 (dest)->inst_p0 = (val); \
268 (dest)->inst_i1 = (gpointer)MONO_PATCH_INFO_METHODCONST; \
269 (dest)->type = STACK_PTR; \
272 #define NEW_DOMAINCONST(cfg,dest) do { \
273 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) { \
274 NEW_TEMPLOAD (cfg, dest, mono_get_domainvar (cfg)->inst_c0); \
276 NEW_PCONST (cfg, dest, (cfg)->domain); \
280 #define GET_VARINFO_INST(cfg,num) ((cfg)->varinfo [(num)]->inst)
282 #define NEW_ARGLOAD(cfg,dest,num) do { \
283 if (arg_array [(num)]->opcode == OP_ICONST) (dest) = arg_array [(num)]; else { \
284 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
285 (dest)->ssa_op = MONO_SSA_LOAD; \
286 (dest)->inst_i0 = arg_array [(num)]; \
287 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
288 type_to_eval_stack_type (param_types [(num)], (dest)); \
289 (dest)->klass = (dest)->inst_i0->klass; \
292 #define NEW_LOCLOAD(cfg,dest,num) do { \
293 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
294 (dest)->ssa_op = MONO_SSA_LOAD; \
295 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
296 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
297 type_to_eval_stack_type (header->locals [(num)], (dest)); \
298 (dest)->klass = (dest)->inst_i0->klass; \
301 #define NEW_LOCLOADA(cfg,dest,num) do { \
302 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
303 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
304 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
305 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
306 (dest)->opcode = OP_LDADDR; \
307 (dest)->type = STACK_MP; \
308 (dest)->klass = (dest)->inst_i0->klass; \
309 (cfg)->disable_ssa = TRUE; \
312 #define NEW_RETLOADA(cfg,dest) do { \
313 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
314 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
315 (dest)->inst_i0 = (cfg)->ret; \
316 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
317 (dest)->opcode = CEE_LDIND_I; \
318 (dest)->type = STACK_MP; \
319 (dest)->klass = (dest)->inst_i0->klass; \
320 (cfg)->disable_ssa = TRUE; \
323 #define NEW_ARGLOADA(cfg,dest,num) do { \
324 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
325 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
326 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
327 (dest)->inst_i0 = arg_array [(num)]; \
328 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
329 (dest)->opcode = OP_LDADDR; \
330 (dest)->type = STACK_MP; \
331 (dest)->klass = (dest)->inst_i0->klass; \
332 (cfg)->disable_ssa = TRUE; \
335 #define NEW_TEMPLOAD(cfg,dest,num) do { \
336 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
337 (dest)->ssa_op = MONO_SSA_LOAD; \
338 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
339 (dest)->opcode = mono_type_to_ldind ((dest)->inst_i0->inst_vtype); \
340 type_to_eval_stack_type ((dest)->inst_i0->inst_vtype, (dest)); \
341 (dest)->klass = (dest)->inst_i0->klass; \
344 #define NEW_TEMPLOADA(cfg,dest,num) do { \
345 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
346 (dest)->ssa_op = MONO_SSA_MAYBE_LOAD; \
347 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
348 (dest)->inst_i0->flags |= MONO_INST_INDIRECT; \
349 (dest)->opcode = OP_LDADDR; \
350 (dest)->type = STACK_MP; \
351 (dest)->klass = (dest)->inst_i0->klass; \
352 (cfg)->disable_ssa = TRUE; \
356 #define NEW_INDLOAD(cfg,dest,addr,vtype) do { \
357 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
358 (dest)->inst_left = addr; \
359 (dest)->opcode = mono_type_to_ldind (vtype); \
360 type_to_eval_stack_type (vtype, (dest)); \
361 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
364 #define NEW_INDSTORE(cfg,dest,addr,value,vtype) do { \
365 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
366 (dest)->inst_i0 = addr; \
367 (dest)->opcode = mono_type_to_stind (vtype); \
368 (dest)->inst_i1 = (value); \
369 /* FIXME: (dest)->klass = (dest)->inst_i0->klass;*/ \
372 #define NEW_TEMPSTORE(cfg,dest,num,inst) do { \
373 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
374 (dest)->ssa_op = MONO_SSA_STORE; \
375 (dest)->inst_i0 = (cfg)->varinfo [(num)]; \
376 (dest)->opcode = mono_type_to_stind ((dest)->inst_i0->inst_vtype); \
377 (dest)->inst_i1 = (inst); \
378 (dest)->klass = (dest)->inst_i0->klass; \
381 #define NEW_LOCSTORE(cfg,dest,num,inst) do { \
382 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
383 (dest)->opcode = mono_type_to_stind (header->locals [(num)]); \
384 (dest)->ssa_op = MONO_SSA_STORE; \
385 (dest)->inst_i0 = (cfg)->varinfo [locals_offset + (num)]; \
386 (dest)->inst_i1 = (inst); \
387 (dest)->klass = (dest)->inst_i0->klass; \
390 #define NEW_ARGSTORE(cfg,dest,num,inst) do { \
391 if (arg_array [(num)]->opcode == OP_ICONST) goto inline_failure; \
392 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
393 (dest)->opcode = mono_type_to_stind (param_types [(num)]); \
394 (dest)->ssa_op = MONO_SSA_STORE; \
395 (dest)->inst_i0 = arg_array [(num)]; \
396 (dest)->inst_i1 = (inst); \
397 (dest)->klass = (dest)->inst_i0->klass; \
400 #define ADD_BINOP(op) do { \
401 MONO_INST_NEW (cfg, ins, (op)); \
402 ins->cil_code = ip; \
404 ins->inst_i0 = sp [0]; \
405 ins->inst_i1 = sp [1]; \
407 type_from_op (ins); \
411 #define ADD_UNOP(op) do { \
412 MONO_INST_NEW (cfg, ins, (op)); \
413 ins->cil_code = ip; \
415 ins->inst_i0 = sp [0]; \
417 type_from_op (ins); \
421 #define ADD_BINCOND(next_block) do { \
423 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
425 cmp->inst_i0 = sp [0]; \
426 cmp->inst_i1 = sp [1]; \
427 cmp->cil_code = ins->cil_code; \
428 type_from_op (cmp); \
430 ins->inst_i0 = cmp; \
431 MONO_ADD_INS (bblock, ins); \
432 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
433 GET_BBLOCK (cfg, bbhash, tblock, target); \
434 link_bblock (cfg, bblock, tblock); \
435 ins->inst_true_bb = tblock; \
436 CHECK_BBLOCK (target, ip, tblock); \
437 if ((next_block)) { \
438 link_bblock (cfg, bblock, (next_block)); \
439 ins->inst_false_bb = (next_block); \
440 start_new_bblock = 1; \
442 GET_BBLOCK (cfg, bbhash, tblock, ip); \
443 link_bblock (cfg, bblock, tblock); \
444 ins->inst_false_bb = tblock; \
445 start_new_bblock = 2; \
449 /* FIXME: handle float, long ... */
450 #define ADD_UNCOND(istrue) do { \
452 MONO_INST_NEW(cfg, cmp, OP_COMPARE); \
454 cmp->inst_i0 = sp [0]; \
455 switch (cmp->inst_i0->type) { \
457 cmp->inst_i1 = zero_int64; break; \
459 cmp->inst_i1 = zero_r8; break; \
462 cmp->inst_i1 = zero_ptr; break; \
464 cmp->inst_i1 = zero_obj; break; \
466 cmp->inst_i1 = zero_int32; \
468 cmp->cil_code = ins->cil_code; \
469 type_from_op (cmp); \
471 ins->inst_i0 = cmp; \
472 ins->opcode = (istrue)? CEE_BNE_UN: CEE_BEQ; \
473 MONO_ADD_INS (bblock, ins); \
474 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof(gpointer)*2); \
475 GET_BBLOCK (cfg, bbhash, tblock, target); \
476 link_bblock (cfg, bblock, tblock); \
477 ins->inst_true_bb = tblock; \
478 CHECK_BBLOCK (target, ip, tblock); \
479 GET_BBLOCK (cfg, bbhash, tblock, ip); \
480 link_bblock (cfg, bblock, tblock); \
481 ins->inst_false_bb = tblock; \
482 start_new_bblock = 2; \
485 #define NEW_LDELEMA(cfg,dest,sp,k) do { \
486 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
487 (dest)->opcode = CEE_LDELEMA; \
488 (dest)->inst_left = (sp) [0]; \
489 (dest)->inst_right = (sp) [1]; \
490 (dest)->type = STACK_MP; \
491 (dest)->klass = (k); \
494 #define NEW_GROUP(cfg,dest,el1,el2) do { \
495 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst)); \
496 (dest)->opcode = OP_GROUP; \
497 (dest)->inst_left = (el1); \
498 (dest)->inst_right = (el2); \
503 compare_bblock (gconstpointer a, gconstpointer b)
505 const MonoBasicBlock *b1 = a;
506 const MonoBasicBlock *b2 = b;
508 return b2->cil_code - b1->cil_code;
513 * link_bblock: Links two basic blocks
515 * links two basic blocks in the control flow graph, the 'from'
516 * argument is the starting block and the 'to' argument is the block
517 * the control flow ends to after 'from'.
520 link_bblock (MonoCompile *cfg, MonoBasicBlock *from, MonoBasicBlock* to)
522 MonoBasicBlock **newa;
526 if (from->cil_code) {
528 g_print ("edge from IL%04x to IL_%04x\n", from->cil_code - cfg->cil_code, to->cil_code - cfg->cil_code);
530 g_print ("edge from IL%04x to exit\n", from->cil_code - cfg->cil_code);
533 g_print ("edge from entry to IL_%04x\n", to->cil_code - cfg->cil_code);
535 g_print ("edge from entry to exit\n");
539 for (i = 0; i < from->out_count; ++i) {
540 if (to == from->out_bb [i]) {
546 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (from->out_count + 1));
547 for (i = 0; i < from->out_count; ++i) {
548 newa [i] = from->out_bb [i];
556 for (i = 0; i < to->in_count; ++i) {
557 if (from == to->in_bb [i]) {
563 newa = mono_mempool_alloc (cfg->mempool, sizeof (gpointer) * (to->in_count + 1));
564 for (i = 0; i < to->in_count; ++i) {
565 newa [i] = to->in_bb [i];
574 * We mark each basic block with a region ID. We use that to avoid BB
575 * optimizations when blocks are in different regions.
578 mono_find_block_region (MonoCompile *cfg, int offset, int *filter_lengths)
580 MonoMethod *method = cfg->method;
581 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
582 MonoExceptionClause *clause;
585 /* first search for handlers and filters */
586 for (i = 0; i < header->num_clauses; ++i) {
587 clause = &header->clauses [i];
588 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) && (offset >= clause->token_or_filter) &&
589 (offset < (clause->token_or_filter + filter_lengths [i])))
590 return ((i + 1) << 8) | MONO_REGION_FILTER | clause->flags;
592 if (MONO_OFFSET_IN_HANDLER (clause, offset)) {
593 if (clause->flags & MONO_EXCEPTION_CLAUSE_FINALLY)
594 return ((i + 1) << 8) | MONO_REGION_FINALLY | clause->flags;
596 return ((i + 1) << 8) | MONO_REGION_CATCH | clause->flags;
600 /* search the try blocks */
601 for (i = 0; i < header->num_clauses; ++i) {
602 clause = &header->clauses [i];
603 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
604 return ((i + 1) << 8) | clause->flags;
611 mono_find_final_block (MonoCompile *cfg, unsigned char *ip, unsigned char *target, int type)
613 MonoMethod *method = cfg->method;
614 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
615 MonoExceptionClause *clause;
616 MonoBasicBlock *handler;
620 for (i = 0; i < header->num_clauses; ++i) {
621 clause = &header->clauses [i];
622 if (MONO_OFFSET_IN_CLAUSE (clause, (ip - header->code)) &&
623 (!MONO_OFFSET_IN_CLAUSE (clause, (target - header->code)))) {
624 if (clause->flags == type) {
625 handler = g_hash_table_lookup (cfg->bb_hash, header->code + clause->handler_offset);
627 res = g_list_append (res, handler);
636 df_visit (MonoBasicBlock *start, int *dfn, MonoBasicBlock **array)
640 array [*dfn] = start;
641 /*g_print ("visit %d at %p\n", *dfn, start->cil_code);*/
642 for (i = 0; i < start->out_count; ++i) {
643 if (start->out_bb [i]->dfn)
646 start->out_bb [i]->dfn = *dfn;
647 start->out_bb [i]->df_parent = start;
648 array [*dfn] = start->out_bb [i];
649 df_visit (start->out_bb [i], dfn, array);
655 MonoBasicBlock *best;
659 previous_foreach (gconstpointer key, gpointer val, gpointer data)
661 PrevStruct *p = data;
662 MonoBasicBlock *bb = val;
663 //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,
664 //bb->method == p->best->method, bb->cil_code < p->code, bb->cil_code > p->best->cil_code);
666 if (bb->cil_code && bb->cil_code < p->code && bb->cil_code > p->best->cil_code)
670 static MonoBasicBlock*
671 find_previous (GHashTable *bb_hash, MonoBasicBlock *start, const guchar *code) {
677 g_hash_table_foreach (bb_hash, (GHFunc)previous_foreach, &p);
682 split_bblock (MonoCompile *cfg, MonoBasicBlock *first, MonoBasicBlock *second) {
691 * FIXME: take into account all the details:
692 * second may have been the target of more than one bblock
694 second->out_count = first->out_count;
695 second->out_bb = first->out_bb;
697 for (i = 0; i < first->out_count; ++i) {
698 bb = first->out_bb [i];
699 for (j = 0; j < bb->in_count; ++j) {
700 if (bb->in_bb [j] == first)
701 bb->in_bb [j] = second;
705 first->out_count = 0;
706 first->out_bb = NULL;
707 link_bblock (cfg, first, second);
709 second->last_ins = first->last_ins;
711 /*g_print ("start search at %p for %p\n", first->cil_code, second->cil_code);*/
712 for (inst = first->code; inst && inst->next; inst = inst->next) {
713 /*char *code = mono_disasm_code_one (NULL, cfg->method, inst->next->cil_code, NULL);
714 g_print ("found %p: %s", inst->next->cil_code, code);
716 if (inst->cil_code < second->cil_code && inst->next->cil_code >= second->cil_code) {
717 second->code = inst->next;
719 first->last_ins = inst;
720 second->next_bb = first->next_bb;
721 first->next_bb = second;
726 g_warning ("bblock split failed in %s::%s\n", cfg->method->klass->name, cfg->method->name);
732 mono_type_to_ldind (MonoType *type)
744 case MONO_TYPE_BOOLEAN:
758 case MONO_TYPE_FNPTR:
760 case MONO_TYPE_CLASS:
761 case MONO_TYPE_STRING:
762 case MONO_TYPE_OBJECT:
763 case MONO_TYPE_SZARRAY:
764 case MONO_TYPE_ARRAY:
765 return CEE_LDIND_REF;
773 case MONO_TYPE_VALUETYPE:
774 if (type->data.klass->enumtype) {
775 t = type->data.klass->enum_basetype->type;
779 case MONO_TYPE_TYPEDBYREF:
781 case MONO_TYPE_GENERICINST:
782 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
784 return CEE_LDIND_REF;
786 g_error ("unknown type 0x%02x in type_to_ldind", type->type);
792 mono_type_to_stind (MonoType *type)
803 case MONO_TYPE_BOOLEAN:
815 case MONO_TYPE_FNPTR:
817 case MONO_TYPE_CLASS:
818 case MONO_TYPE_STRING:
819 case MONO_TYPE_OBJECT:
820 case MONO_TYPE_SZARRAY:
821 case MONO_TYPE_ARRAY:
822 return CEE_STIND_REF;
830 case MONO_TYPE_VALUETYPE:
831 if (type->data.klass->enumtype) {
832 t = type->data.klass->enum_basetype->type;
836 case MONO_TYPE_TYPEDBYREF:
838 case MONO_TYPE_GENERICINST:
839 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE)
841 return CEE_STIND_REF;
843 g_error ("unknown type 0x%02x in type_to_stind", type->type);
849 * Returns the type used in the eval stack when @type is loaded.
850 * FIXME: return a MonoType/MonoClass for the byref and VALUETYPE cases.
853 type_to_eval_stack_type (MonoType *type, MonoInst *inst) {
857 inst->type = STACK_MP;
865 case MONO_TYPE_BOOLEAN:
871 inst->type = STACK_I4;
876 case MONO_TYPE_FNPTR:
877 inst->type = STACK_PTR;
879 case MONO_TYPE_CLASS:
880 case MONO_TYPE_STRING:
881 case MONO_TYPE_OBJECT:
882 case MONO_TYPE_SZARRAY:
883 case MONO_TYPE_ARRAY:
884 inst->type = STACK_OBJ;
888 inst->type = STACK_I8;
892 inst->type = STACK_R8;
894 case MONO_TYPE_VALUETYPE:
895 if (type->data.klass->enumtype) {
896 t = type->data.klass->enum_basetype->type;
899 inst->klass = type->data.klass;
900 inst->type = STACK_VTYPE;
903 case MONO_TYPE_TYPEDBYREF:
904 inst->klass = mono_defaults.typed_reference_class;
905 inst->type = STACK_VTYPE;
907 case MONO_TYPE_GENERICINST:
908 if (type->data.generic_inst->generic_type->type == MONO_TYPE_VALUETYPE) {
909 inst->klass = mono_class_from_mono_type (type);
910 inst->type = STACK_VTYPE;
912 inst->type = STACK_OBJ;
916 g_error ("unknown type 0x%02x in eval stack type", type->type);
921 * The following tables are used to quickly validate the IL code in type_from_op ().
924 bin_num_table [STACK_MAX] [STACK_MAX] = {
925 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
926 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
927 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
928 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_MP, STACK_INV, STACK_INV},
929 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_R8, STACK_INV, STACK_INV, STACK_INV},
930 {STACK_INV, STACK_MP, STACK_INV, STACK_MP, STACK_INV, STACK_PTR, STACK_INV, STACK_INV},
931 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
932 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
937 STACK_INV, STACK_I4, STACK_I8, STACK_PTR, STACK_R8, STACK_INV, STACK_INV, STACK_INV
940 /* reduce the size of this table */
942 bin_int_table [STACK_MAX] [STACK_MAX] = {
943 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
944 {STACK_INV, STACK_I4, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
945 {STACK_INV, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
946 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
947 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
948 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
949 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
950 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
954 bin_comp_table [STACK_MAX] [STACK_MAX] = {
956 {0, 1, 0, 1, 0, 0, 4, 0},
957 {0, 0, 1, 0, 0, 0, 0, 0},
958 {0, 1, 0, 1, 0, 2, 4, 0},
959 {0, 0, 0, 0, 1, 0, 0, 0},
960 {0, 0, 0, 2, 0, 1, 0, 0},
961 {0, 4, 0, 4, 0, 0, 3, 0},
962 {0, 0, 0, 0, 0, 0, 0, 0},
965 /* reduce the size of this table */
967 shift_table [STACK_MAX] [STACK_MAX] = {
968 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
969 {STACK_INV, STACK_I4, STACK_INV, STACK_I4, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
970 {STACK_INV, STACK_I8, STACK_INV, STACK_I8, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
971 {STACK_INV, STACK_PTR, STACK_INV, STACK_PTR, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
972 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
973 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
974 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV},
975 {STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV, STACK_INV}
979 * Tables to map from the non-specific opcode to the matching
980 * type-specific opcode.
982 /* handles from CEE_ADD to CEE_SHR_UN (CEE_REM_UN for floats) */
984 binops_op_map [STACK_MAX] = {
985 0, 0, OP_LADD-CEE_ADD, OP_PADD-CEE_ADD, OP_FADD-CEE_ADD, 0
988 /* handles from CEE_NEG to CEE_CONV_U8 */
990 unops_op_map [STACK_MAX] = {
991 0, 0, OP_LNEG-CEE_NEG, OP_PNEG-CEE_NEG, OP_FNEG-CEE_NEG, 0
994 /* handles from CEE_CONV_U2 to CEE_SUB_OVF_UN */
996 ovfops_op_map [STACK_MAX] = {
997 0, 0, OP_LCONV_TO_U2-CEE_CONV_U2, OP_PCONV_TO_U2-CEE_CONV_U2, OP_FCONV_TO_U2-CEE_CONV_U2, 0
1000 /* handles from CEE_CONV_OVF_I1_UN to CEE_CONV_OVF_U_UN */
1001 static const guint16
1002 ovf2ops_op_map [STACK_MAX] = {
1003 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
1006 /* handles from CEE_CONV_OVF_I1 to CEE_CONV_OVF_U8 */
1007 static const guint16
1008 ovf3ops_op_map [STACK_MAX] = {
1009 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
1012 /* handles from CEE_CEQ to CEE_CLT_UN */
1013 static const guint16
1014 ceqops_op_map [STACK_MAX] = {
1015 0, 0, OP_LCEQ-CEE_CEQ, OP_PCEQ-CEE_CEQ, OP_FCEQ-CEE_CEQ, 0
1019 * Sets ins->type (the type on the eval stack) according to the
1020 * type of the opcode and the arguments to it.
1021 * Invalid IL code is marked by setting ins->type to the invalid value STACK_INV.
1023 * FIXME: this function sets ins->type unconditionally in some cases, but
1024 * it should set it to invalid for some types (a conv.x on an object)
1027 type_from_op (MonoInst *ins) {
1028 switch (ins->opcode) {
1035 /* FIXME: check unverifiable args for STACK_MP */
1036 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1037 ins->opcode += binops_op_map [ins->type];
1044 ins->type = bin_int_table [ins->inst_i0->type] [ins->inst_i1->type];
1045 ins->opcode += binops_op_map [ins->type];
1050 ins->type = shift_table [ins->inst_i0->type] [ins->inst_i1->type];
1051 ins->opcode += binops_op_map [ins->type];
1054 /* FIXME: handle some specifics with ins->next->type */
1055 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1059 case 256+CEE_CGT_UN:
1061 case 256+CEE_CLT_UN:
1062 ins->type = bin_comp_table [ins->inst_i0->type] [ins->inst_i1->type] ? STACK_I4: STACK_INV;
1063 ins->opcode += ceqops_op_map [ins->inst_i0->type];
1067 ins->type = neg_table [ins->inst_i0->type];
1068 ins->opcode += unops_op_map [ins->type];
1071 if (ins->inst_i0->type >= STACK_I4 && ins->inst_i0->type <= STACK_PTR)
1072 ins->type = ins->inst_i0->type;
1074 ins->type = STACK_INV;
1075 ins->opcode += unops_op_map [ins->type];
1081 ins->type = STACK_I4;
1082 ins->opcode += unops_op_map [ins->inst_i0->type];
1085 ins->type = STACK_R8;
1086 switch (ins->inst_i0->type) {
1091 ins->opcode = OP_LCONV_TO_R_UN;
1095 case CEE_CONV_OVF_I1:
1096 case CEE_CONV_OVF_U1:
1097 case CEE_CONV_OVF_I2:
1098 case CEE_CONV_OVF_U2:
1099 case CEE_CONV_OVF_I4:
1100 case CEE_CONV_OVF_U4:
1101 ins->type = STACK_I4;
1102 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1104 case CEE_CONV_OVF_I_UN:
1105 case CEE_CONV_OVF_U_UN:
1106 ins->type = STACK_PTR;
1107 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1109 case CEE_CONV_OVF_I1_UN:
1110 case CEE_CONV_OVF_I2_UN:
1111 case CEE_CONV_OVF_I4_UN:
1112 case CEE_CONV_OVF_U1_UN:
1113 case CEE_CONV_OVF_U2_UN:
1114 case CEE_CONV_OVF_U4_UN:
1115 ins->type = STACK_I4;
1116 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1119 ins->type = STACK_PTR;
1120 switch (ins->inst_i0->type) {
1126 ins->opcode = OP_LCONV_TO_U;
1129 ins->opcode = OP_FCONV_TO_U;
1135 ins->type = STACK_I8;
1136 ins->opcode += unops_op_map [ins->inst_i0->type];
1138 case CEE_CONV_OVF_I8:
1139 case CEE_CONV_OVF_U8:
1140 ins->type = STACK_I8;
1141 ins->opcode += ovf3ops_op_map [ins->inst_i0->type];
1143 case CEE_CONV_OVF_U8_UN:
1144 case CEE_CONV_OVF_I8_UN:
1145 ins->type = STACK_I8;
1146 ins->opcode += ovf2ops_op_map [ins->inst_i0->type];
1150 ins->type = STACK_R8;
1151 ins->opcode += unops_op_map [ins->inst_i0->type];
1154 ins->type = STACK_R8;
1158 ins->type = STACK_I4;
1159 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1162 case CEE_CONV_OVF_I:
1163 case CEE_CONV_OVF_U:
1164 ins->type = STACK_PTR;
1165 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1168 case CEE_ADD_OVF_UN:
1170 case CEE_MUL_OVF_UN:
1172 case CEE_SUB_OVF_UN:
1173 ins->type = bin_num_table [ins->inst_i0->type] [ins->inst_i1->type];
1174 ins->opcode += ovfops_op_map [ins->inst_i0->type];
1177 g_error ("opcode 0x%04x not handled in type from op", ins->opcode);
1184 STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I4, STACK_I8, STACK_MP, STACK_R8, STACK_R8, STACK_OBJ
1187 /* map ldelem.x to the matching ldind.x opcode */
1189 ldelem_to_ldind [] = {
1203 /* map stelem.x to the matching stind.x opcode */
1205 stelem_to_stind [] = {
1219 param_table [STACK_MAX] [STACK_MAX] = {
1224 check_values_to_signature (MonoInst *args, MonoType *this, MonoMethodSignature *sig) {
1228 switch (args->type) {
1238 for (i = 0; i < sig->param_count; ++i) {
1239 switch (args [i].type) {
1243 if (!sig->params [i]->byref)
1247 if (sig->params [i]->byref)
1249 switch (sig->params [i]->type) {
1250 case MONO_TYPE_CLASS:
1251 case MONO_TYPE_STRING:
1252 case MONO_TYPE_OBJECT:
1253 case MONO_TYPE_SZARRAY:
1254 case MONO_TYPE_ARRAY:
1261 if (sig->params [i]->byref)
1263 if (sig->params [i]->type != MONO_TYPE_R4 && sig->params [i]->type != MONO_TYPE_R8)
1272 /*if (!param_table [args [i].type] [sig->params [i]->type])
1280 * When we need a pointer to the current domain many times in a method, we
1281 * call mono_domain_get() once and we store the result in a local variable.
1282 * This function returns the variable that represents the MonoDomain*.
1284 inline static MonoInst *
1285 mono_get_domainvar (MonoCompile *cfg)
1287 if (!cfg->domainvar)
1288 cfg->domainvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
1289 return cfg->domainvar;
1293 mono_compile_create_var (MonoCompile *cfg, MonoType *type, int opcode)
1296 int num = cfg->num_varinfo;
1298 if ((num + 1) >= cfg->varinfo_count) {
1299 cfg->varinfo_count = (cfg->varinfo_count + 2) * 2;
1300 cfg->varinfo = (MonoInst **)g_realloc (cfg->varinfo, sizeof (MonoInst*) * cfg->varinfo_count);
1301 cfg->vars = (MonoMethodVar **)g_realloc (cfg->vars, sizeof (MonoMethodVar*) * cfg->varinfo_count);
1304 /*g_print ("created temp %d of type 0x%x\n", num, type->type);*/
1305 mono_jit_stats.allocate_var++;
1307 MONO_INST_NEW (cfg, inst, opcode);
1308 inst->inst_c0 = num;
1309 inst->inst_vtype = type;
1310 inst->klass = mono_class_from_mono_type (type);
1311 /* if set to 1 the variable is native */
1314 cfg->varinfo [num] = inst;
1316 cfg->vars [num] = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoMethodVar));
1317 MONO_INIT_VARINFO (cfg->vars [num], num);
1320 //g_print ("created temp %d of type %s\n", num, mono_type_get_name (type));
1325 type_from_stack_type (MonoInst *ins) {
1326 switch (ins->type) {
1327 case STACK_I4: return &mono_defaults.int32_class->byval_arg;
1328 case STACK_I8: return &mono_defaults.int64_class->byval_arg;
1329 case STACK_PTR: return &mono_defaults.int_class->byval_arg;
1330 case STACK_R8: return &mono_defaults.double_class->byval_arg;
1331 case STACK_MP: return &mono_defaults.int_class->byval_arg;
1332 case STACK_OBJ: return &mono_defaults.object_class->byval_arg;
1333 case STACK_VTYPE: return &ins->klass->byval_arg;
1335 g_error ("stack type %d to montype not handled\n", ins->type);
1341 array_access_to_klass (int opcode)
1345 return mono_defaults.byte_class;
1347 return mono_defaults.uint16_class;
1350 return mono_defaults.int_class;
1353 return mono_defaults.sbyte_class;
1356 return mono_defaults.int16_class;
1359 return mono_defaults.int32_class;
1361 return mono_defaults.uint32_class;
1364 return mono_defaults.int64_class;
1367 return mono_defaults.single_class;
1370 return mono_defaults.double_class;
1371 case CEE_LDELEM_REF:
1372 case CEE_STELEM_REF:
1373 return mono_defaults.object_class;
1375 g_assert_not_reached ();
1381 mono_add_ins_to_end (MonoBasicBlock *bb, MonoInst *inst)
1385 MONO_ADD_INS (bb, inst);
1388 switch (bb->last_ins->opcode) {
1401 while (prev->next && prev->next != bb->last_ins)
1403 if (prev == bb->code) {
1404 if (bb->last_ins == bb->code) {
1405 inst->next = bb->code;
1408 inst->next = prev->next;
1412 inst->next = bb->last_ins;
1416 // g_warning ("handle conditional jump in add_ins_to_end ()\n");
1418 MONO_ADD_INS (bb, inst);
1424 mono_add_varcopy_to_end (MonoCompile *cfg, MonoBasicBlock *bb, int src, int dest)
1426 MonoInst *inst, *load;
1428 NEW_TEMPLOAD (cfg, load, src);
1430 NEW_TEMPSTORE (cfg, inst, dest, load);
1431 if (inst->opcode == CEE_STOBJ) {
1432 NEW_TEMPLOADA (cfg, inst, dest);
1433 handle_stobj (cfg, bb, inst, load, NULL, inst->klass, TRUE, FALSE);
1435 inst->cil_code = NULL;
1436 mono_add_ins_to_end (bb, inst);
1441 * We try to share variables when possible
1444 mono_compile_get_interface_var (MonoCompile *cfg, int slot, MonoInst *ins)
1449 /* inlining can result in deeper stacks */
1450 if (slot >= ((MonoMethodNormal *)cfg->method)->header->max_stack)
1451 return mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1453 pos = ins->type - 1 + slot * STACK_MAX;
1455 switch (ins->type) {
1462 if ((vnum = cfg->intvars [pos]))
1463 return cfg->varinfo [vnum];
1464 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1465 cfg->intvars [pos] = res->inst_c0;
1468 res = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1474 * This function is called to handle items that are left on the evaluation stack
1475 * at basic block boundaries. What happens is that we save the values to local variables
1476 * and we reload them later when first entering the target basic block (with the
1477 * handle_loaded_temps () function).
1478 * A single joint point will use the same variables (stored in the array bb->out_stack or
1479 * bb->in_stack, if the basic block is before or after the joint point).
1482 handle_stack_args (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **sp, int count) {
1484 MonoBasicBlock *outb;
1485 MonoInst *inst, **locals;
1489 if (cfg->verbose_level > 3)
1490 g_print ("%d item(s) on exit from B%d\n", count, bb->block_num);
1491 if (!bb->out_scount) {
1493 bb->out_scount = count;
1494 //g_print ("bblock %d has out:", bb->block_num);
1495 for (i = 0; i < bb->out_count; ++i) {
1496 outb = bb->out_bb [i];
1497 //g_print (" %d", outb->block_num);
1498 if (outb->in_stack) {
1500 bb->out_stack = outb->in_stack;
1506 bb->out_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * count);
1507 for (i = 0; i < count; ++i) {
1509 /* try to reuse temps already allocated for this purpouse, if they occupy the same
1510 * stack slot and if they are of the same type. */
1511 bb->out_stack [i] = mono_compile_get_interface_var (cfg, i, sp [i]);
1513 bb->out_stack [i] = mono_compile_create_var (cfg, type_from_stack_type (sp [i]), OP_LOCAL);
1518 locals = bb->out_stack;
1519 for (i = 0; i < count; ++i) {
1520 /* add store ops at the end of the bb, before the branch */
1521 NEW_TEMPSTORE (cfg, inst, locals [i]->inst_c0, sp [i]);
1522 if (inst->opcode == CEE_STOBJ) {
1523 NEW_TEMPLOADA (cfg, inst, locals [i]->inst_c0);
1524 handle_stobj (cfg, bb, inst, sp [i], sp [i]->cil_code, inst->klass, TRUE, FALSE);
1526 inst->cil_code = sp [i]->cil_code;
1527 mono_add_ins_to_end (bb, inst);
1529 if (cfg->verbose_level > 3)
1530 g_print ("storing %d to temp %d\n", i, locals [i]->inst_c0);
1533 for (i = 0; i < bb->out_count; ++i) {
1534 outb = bb->out_bb [i];
1535 if (outb->in_scount)
1536 continue; /* check they are the same locals */
1537 outb->in_scount = count;
1538 outb->in_stack = locals;
1544 ret_type_to_call_opcode (MonoType *type, int calli, int virt)
1549 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1553 case MONO_TYPE_VOID:
1554 return calli? OP_VOIDCALL_REG: virt? OP_VOIDCALLVIRT: OP_VOIDCALL;
1557 case MONO_TYPE_BOOLEAN:
1560 case MONO_TYPE_CHAR:
1563 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1567 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1568 case MONO_TYPE_CLASS:
1569 case MONO_TYPE_STRING:
1570 case MONO_TYPE_OBJECT:
1571 case MONO_TYPE_SZARRAY:
1572 case MONO_TYPE_ARRAY:
1573 return calli? OP_CALL_REG: virt? CEE_CALLVIRT: CEE_CALL;
1576 return calli? OP_LCALL_REG: virt? OP_LCALLVIRT: OP_LCALL;
1579 return calli? OP_FCALL_REG: virt? OP_FCALLVIRT: OP_FCALL;
1580 case MONO_TYPE_VALUETYPE:
1581 if (type->data.klass->enumtype) {
1582 t = type->data.klass->enum_basetype->type;
1585 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1586 case MONO_TYPE_TYPEDBYREF:
1587 return calli? OP_VCALL_REG: virt? OP_VCALLVIRT: OP_VCALL;
1589 g_error ("unknown type 0x%02x in ret_type_to_call_opcode", type->type);
1595 mono_create_jump_table (MonoCompile *cfg, MonoInst *label, MonoBasicBlock **bbs, int num_blocks)
1597 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
1599 ji->ip.label = label;
1600 ji->type = MONO_PATCH_INFO_SWITCH;
1601 ji->data.table = bbs;
1602 ji->next = cfg->patch_info;
1603 ji->table_size = num_blocks;
1604 cfg->patch_info = ji;
1608 * When we add a tree of instructions, we need to ensure the instructions currently
1609 * on the stack are executed before (like, if we load a value from a local).
1610 * We ensure this by saving the currently loaded values to temps and rewriting the
1611 * instructions to load the values.
1612 * This is not done for opcodes that terminate a basic block (because it's handled already
1613 * by handle_stack_args ()) and for opcodes that can't change values, like POP.
1616 handle_loaded_temps (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst **stack, MonoInst **sp)
1618 MonoInst *load, *store, *temp, *ins;
1620 while (stack < sp) {
1622 /* handle also other constants */
1623 if (ins->opcode != OP_ICONST) {
1624 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
1625 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1626 store->cil_code = ins->cil_code;
1627 if (store->opcode == CEE_STOBJ) {
1628 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
1629 handle_stobj (cfg, bblock, store, ins, ins->cil_code, temp->klass, FALSE, FALSE);
1631 MONO_ADD_INS (bblock, store);
1632 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1633 load->cil_code = ins->cil_code;
1641 * Prepare arguments for passing to a function call.
1642 * Return a non-zero value if the arguments can't be passed to the given
1644 * The type checks are not yet complete and some conversions may need
1645 * casts on 32 or 64 bit architectures.
1648 check_call_signature (MonoCompile *cfg, MonoMethodSignature *sig, MonoInst **args)
1653 if (args [0]->type != STACK_OBJ && args [0]->type != STACK_MP && args [0]->type != STACK_PTR)
1657 for (i = 0; i < sig->param_count; ++i) {
1658 if (sig->params [i]->byref) {
1660 * check the result of ldelema is only passed as an argument if the byref
1661 * type matches exactly the array element type.
1662 * FIXME: if the argument as been saved on the stack as part of the
1663 * interface variable code (the value was on the stack at a basic block boundary)
1664 * we need to add the check in that case, too.
1666 if (args [i]->opcode == CEE_LDELEMA) {
1668 MonoClass *exact_class = mono_class_from_mono_type (sig->params [i]);
1669 if (!exact_class->valuetype) {
1670 MONO_INST_NEW (cfg, check, OP_CHECK_ARRAY_TYPE);
1671 check->cil_code = args [i]->cil_code;
1672 check->klass = exact_class;
1673 check->inst_left = args [i]->inst_left;
1674 check->type = STACK_OBJ;
1675 args [i]->inst_left = check;
1678 if (args [i]->type != STACK_MP && args [i]->type != STACK_PTR)
1682 simple_type = sig->params [i]->type;
1684 switch (simple_type) {
1685 case MONO_TYPE_VOID:
1690 case MONO_TYPE_BOOLEAN:
1693 case MONO_TYPE_CHAR:
1696 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR)
1702 if (args [i]->type != STACK_I4 && args [i]->type != STACK_PTR && args [i]->type != STACK_MP && args [i]->type != STACK_OBJ)
1705 case MONO_TYPE_CLASS:
1706 case MONO_TYPE_STRING:
1707 case MONO_TYPE_OBJECT:
1708 case MONO_TYPE_SZARRAY:
1709 case MONO_TYPE_ARRAY:
1710 if (args [i]->type != STACK_OBJ)
1715 if (args [i]->type != STACK_I8)
1720 if (args [i]->type != STACK_R8)
1723 case MONO_TYPE_VALUETYPE:
1724 if (sig->params [i]->data.klass->enumtype) {
1725 simple_type = sig->params [i]->data.klass->enum_basetype->type;
1728 if (args [i]->type != STACK_VTYPE)
1731 case MONO_TYPE_TYPEDBYREF:
1732 if (args [i]->type != STACK_VTYPE)
1736 g_error ("unknown type 0x%02x in check_call_signature", simple_type);
1743 mono_spill_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoCallInst *call, MonoMethodSignature *sig, gboolean ret_object,
1744 const guint8 *ip, gboolean to_end)
1746 MonoInst *temp, *store, *ins = (MonoInst*)call;
1747 MonoType *ret = sig->ret;
1749 if (!MONO_TYPE_IS_VOID (ret) || ret_object) {
1751 call->inst.type = STACK_OBJ;
1752 call->inst.opcode = CEE_CALL;
1753 temp = mono_compile_create_var (cfg, &mono_defaults.string_class->byval_arg, OP_LOCAL);
1755 type_to_eval_stack_type (ret, ins);
1756 temp = mono_compile_create_var (cfg, ret, OP_LOCAL);
1759 if (MONO_TYPE_ISSTRUCT (ret)) {
1762 /* we use this to allocate native sized structs */
1763 temp->unused = sig->pinvoke;
1765 NEW_TEMPLOADA (cfg, loada, temp->inst_c0);
1766 if (call->inst.opcode == OP_VCALL)
1767 ins->inst_left = loada;
1769 ins->inst_right = loada; /* a virtual or indirect call */
1772 mono_add_ins_to_end (bblock, ins);
1774 MONO_ADD_INS (bblock, ins);
1776 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1777 store->cil_code = ip;
1779 mono_add_ins_to_end (bblock, store);
1781 MONO_ADD_INS (bblock, store);
1783 return temp->inst_c0;
1786 mono_add_ins_to_end (bblock, ins);
1788 MONO_ADD_INS (bblock, ins);
1793 inline static MonoCallInst *
1794 mono_emit_call_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1795 MonoInst **args, int calli, int virtual, const guint8 *ip, gboolean to_end)
1801 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (sig->ret, calli, virtual));
1803 call->inst.cil_code = ip;
1805 call->signature = sig;
1806 call = mono_arch_call_opcode (cfg, bblock, call, virtual);
1808 for (arg = call->out_args; arg;) {
1809 MonoInst *narg = arg->next;
1814 mono_add_ins_to_end (bblock, arg);
1816 MONO_ADD_INS (bblock, arg);
1823 mono_emit_calli (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig,
1824 MonoInst **args, MonoInst *addr, const guint8 *ip)
1826 MonoCallInst *call = mono_emit_call_args (cfg, bblock, sig, args, TRUE, FALSE, ip, FALSE);
1828 call->inst.inst_i0 = addr;
1830 return mono_spill_call (cfg, bblock, call, sig, FALSE, ip, FALSE);
1833 static MonoCallInst*
1834 mono_emit_method_call (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method, MonoMethodSignature *sig,
1835 MonoInst **args, const guint8 *ip, MonoInst *this)
1837 gboolean virtual = this != NULL;
1840 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, virtual, ip, FALSE);
1842 if (this && sig->hasthis &&
1843 (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1844 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !MONO_CHECK_THIS (this)) {
1845 call->method = mono_marshal_get_remoting_invoke_with_check (method);
1847 call->method = method;
1849 call->inst.flags |= MONO_INST_HAS_METHOD;
1850 call->inst.inst_left = this;
1856 mono_emit_method_call_spilled (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *method,
1857 MonoMethodSignature *signature, MonoInst **args, const guint8 *ip, MonoInst *this)
1859 MonoCallInst *call = mono_emit_method_call (cfg, bblock, method, signature, args, ip, this);
1861 return mono_spill_call (cfg, bblock, call, signature, method->string_ctor, ip, FALSE);
1865 mono_emit_native_call (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoMethodSignature *sig,
1866 MonoInst **args, const guint8 *ip, gboolean to_end)
1872 call = mono_emit_call_args (cfg, bblock, sig, args, FALSE, FALSE, ip, to_end);
1874 return mono_spill_call (cfg, bblock, call, sig, func == mono_array_new_va, ip, to_end);
1878 mono_emit_jit_icall (MonoCompile *cfg, MonoBasicBlock *bblock, gconstpointer func, MonoInst **args, const guint8 *ip)
1880 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (func);
1883 g_warning ("unregistered JIT ICall");
1884 g_assert_not_reached ();
1887 return mono_emit_native_call (cfg, bblock, info->wrapper, info->sig, args, ip, FALSE);
1891 mono_emulate_opcode (MonoCompile *cfg, MonoInst *tree, MonoInst **iargs, MonoJitICallInfo *info)
1893 MonoInst *ins, *temp = NULL, *store, *load;
1894 MonoInst *last_arg = NULL;
1898 /*g_print ("emulating: ");
1899 mono_print_tree (tree);
1901 MONO_INST_NEW_CALL (cfg, call, ret_type_to_call_opcode (info->sig->ret, FALSE, FALSE));
1902 ins = (MonoInst*)call;
1904 call->inst.cil_code = tree->cil_code;
1906 call->signature = info->sig;
1908 call = mono_arch_call_opcode (cfg, cfg->cbb, call, FALSE);
1910 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1911 temp = mono_compile_create_var (cfg, info->sig->ret, OP_LOCAL);
1912 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
1913 store->cil_code = tree->cil_code;
1918 nargs = info->sig->param_count + info->sig->hasthis;
1920 for (last_arg = call->out_args; last_arg && last_arg->next; last_arg = last_arg->next) ;
1923 last_arg->next = store;
1925 if (cfg->prev_ins) {
1926 store->next = cfg->prev_ins->next;
1928 cfg->prev_ins->next = call->out_args;
1930 cfg->prev_ins->next = store;
1932 store->next = cfg->cbb->code;
1934 cfg->cbb->code = call->out_args;
1936 cfg->cbb->code = store;
1940 call->fptr = info->wrapper;
1942 if (!MONO_TYPE_IS_VOID (info->sig->ret)) {
1943 NEW_TEMPLOAD (cfg, load, temp->inst_c0);
1948 static MonoMethodSignature *
1949 mono_get_element_address_signature (int arity)
1951 static GHashTable *sighash = NULL;
1952 MonoMethodSignature *res;
1956 sighash = g_hash_table_new (NULL, NULL);
1959 if ((res = g_hash_table_lookup (sighash, (gpointer)arity)))
1962 res = mono_metadata_signature_alloc (mono_defaults.corlib, arity + 1);
1964 res->params [0] = &mono_defaults.array_class->byval_arg;
1966 for (i = 1; i <= arity; i++)
1967 res->params [i] = &mono_defaults.int_class->byval_arg;
1969 res->ret = &mono_defaults.int_class->byval_arg;
1971 g_hash_table_insert (sighash, (gpointer)arity, res);
1977 handle_stobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, MonoInst *src, const unsigned char *ip, MonoClass *klass, gboolean to_end, gboolean native) {
1978 MonoInst *iargs [3];
1984 * This check breaks with spilled vars... need to handle it during verification anyway.
1985 * g_assert (klass && klass == src->klass && klass == dest->klass);
1989 n = mono_class_native_size (klass, &align);
1991 n = mono_class_value_size (klass, &align);
1993 if ((cfg->opt & MONO_OPT_INTRINS) && !to_end && n <= sizeof (gpointer) * 5) {
1995 MONO_INST_NEW (cfg, inst, OP_MEMCPY);
1996 inst->inst_left = dest;
1997 inst->inst_right = src;
1998 inst->cil_code = ip;
2000 MONO_ADD_INS (bblock, inst);
2005 NEW_ICONST (cfg, iargs [2], n);
2007 mono_emit_native_call (cfg, bblock, helper_memcpy, helper_sig_memcpy, iargs, ip, to_end);
2011 handle_initobj (MonoCompile *cfg, MonoBasicBlock *bblock, MonoInst *dest, const guchar *ip, MonoClass *klass, MonoInst **stack_start, MonoInst **sp)
2013 MonoInst *iargs [2];
2014 MonoInst *ins, *zero_int32;
2017 NEW_ICONST (cfg, zero_int32, 0);
2019 mono_class_init (klass);
2020 n = mono_class_value_size (klass, NULL);
2021 MONO_INST_NEW (cfg, ins, 0);
2023 ins->inst_left = dest;
2024 ins->inst_right = zero_int32;
2027 ins->opcode = CEE_STIND_I1;
2028 MONO_ADD_INS (bblock, ins);
2031 ins->opcode = CEE_STIND_I2;
2032 MONO_ADD_INS (bblock, ins);
2035 ins->opcode = CEE_STIND_I4;
2036 MONO_ADD_INS (bblock, ins);
2039 if (n <= sizeof (gpointer) * 5) {
2040 ins->opcode = OP_MEMSET;
2043 MONO_ADD_INS (bblock, ins);
2046 handle_loaded_temps (cfg, bblock, stack_start, sp);
2047 NEW_ICONST (cfg, ins, n);
2050 mono_emit_jit_icall (cfg, bblock, helper_initobj, iargs, ip);
2055 #define CODE_IS_STLOC(ip) (((ip) [0] >= CEE_STLOC_0 && (ip) [0] <= CEE_STLOC_3) || ((ip) [0] == CEE_STLOC_S))
2058 needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2063 for (i = 0; i < klass->method.count; ++i) {
2064 method = klass->methods [i];
2065 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2066 (strcmp (".cctor", method->name) == 0)) {
2067 if (caller == method)
2076 mono_method_check_inlining (MonoCompile *cfg, MonoMethod *method)
2078 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
2079 MonoMethodSignature *signature = method->signature;
2083 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2084 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2085 (method->iflags & METHOD_IMPL_ATTRIBUTE_NOINLINING) ||
2086 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) ||
2087 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2088 (method->klass->marshalbyref) ||
2089 !header || header->num_clauses ||
2090 /* fixme: why cant we inline valuetype returns? */
2091 MONO_TYPE_ISSTRUCT (signature->ret))
2094 /* its not worth to inline methods with valuetype arguments?? */
2095 for (i = 0; i < signature->param_count; i++) {
2096 if (MONO_TYPE_ISSTRUCT (signature->params [i])) {
2102 * if we can initialize the class of the method right away, we do,
2103 * otherwise we don't allow inlining if the class needs initialization,
2104 * since it would mean inserting a call to mono_runtime_class_init()
2105 * inside the inlined code
2107 if (!(cfg->opt & MONO_OPT_SHARED)) {
2108 vtable = mono_class_vtable (cfg->domain, method->klass);
2109 if (method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
2110 mono_runtime_class_init (vtable);
2111 else if (!vtable->initialized && needs_cctor_run (method->klass, NULL))
2115 * If we're compiling for shared code
2116 * the cctor will need to be run at aot method load time, for example,
2117 * or at the end of the compilation of the inlining method.
2119 if (needs_cctor_run (method->klass, NULL) && !((method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2122 //if (!MONO_TYPE_IS_VOID (signature->ret)) return FALSE;
2124 /* also consider num_locals? */
2125 if (header->code_size < 20)
2132 mini_get_ldelema_ins (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethod *cmethod, MonoInst **sp, unsigned char *ip, gboolean is_set)
2136 MonoMethodSignature *esig;
2138 rank = cmethod->signature->param_count - (is_set? 1: 0);
2140 * FIXME: handle TypeMismatch for set or use the slow path
2143 if (rank == 2 && (cfg->opt & MONO_OPT_INTRINS)) {
2145 NEW_GROUP (cfg, indexes, sp [1], sp [2]);
2146 MONO_INST_NEW (cfg, addr, OP_LDELEMA2D);
2147 addr->inst_left = sp [0];
2148 addr->inst_right = indexes;
2149 addr->cil_code = ip;
2150 addr->type = STACK_MP;
2151 addr->klass = cmethod->klass;
2154 esig = mono_get_element_address_signature (rank);
2155 temp = mono_emit_native_call (cfg, bblock, ves_array_element_address, esig, sp, ip, FALSE);
2156 NEW_TEMPLOAD (cfg, addr, temp);
2161 mini_get_opcode_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2166 if (cmethod->klass == mono_defaults.string_class) {
2167 if (cmethod->name [0] != 'g' || strcmp (cmethod->name, "get_Chars"))
2170 } else if (cmethod->klass == mono_defaults.math_class) {
2171 if (strcmp (cmethod->name, "Sin") == 0)
2173 else if (strcmp (cmethod->name, "Cos") == 0)
2175 else if (strcmp (cmethod->name, "Tan") == 0)
2177 else if (strcmp (cmethod->name, "Atan") == 0)
2179 else if (strcmp (cmethod->name, "Sqrt") == 0)
2181 else if (strcmp (cmethod->name, "Abs") == 0 && fsig->params [0]->type == MONO_TYPE_R8)
2184 /* OP_FREM is not IEEE compatible */
2185 else if (strcmp (cmethod->name, "IEEERemainder") == 0)
2190 } else if (cmethod->klass == mono_defaults.array_class) {
2191 if (strcmp (cmethod->name, "get_Rank") == 0)
2193 else if (strcmp (cmethod->name, "get_Length") == 0)
2200 pc = fsig->param_count + fsig->hasthis;
2201 MONO_INST_NEW (cfg, ins, op);
2204 ins->inst_i0 = args [0];
2206 ins->inst_i1 = args [1];
2213 mono_save_args (MonoCompile *cfg, MonoBasicBlock *bblock, MonoMethodSignature *sig, MonoInst **sp, MonoInst **args)
2215 MonoInst *store, *temp;
2218 g_assert (!MONO_TYPE_ISSTRUCT (sig->ret));
2220 if (!sig->hasthis && sig->param_count == 0)
2224 if (sp [0]->opcode == OP_ICONST) {
2227 temp = mono_compile_create_var (cfg, type_from_stack_type (*sp), OP_LOCAL);
2229 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2230 store->cil_code = sp [0]->cil_code;
2231 MONO_ADD_INS (bblock, store);
2236 for (i = 0; i < sig->param_count; ++i) {
2237 if (sp [0]->opcode == OP_ICONST) {
2240 temp = mono_compile_create_var (cfg, sig->params [i], OP_LOCAL);
2242 NEW_TEMPSTORE (cfg, store, temp->inst_c0, *sp);
2243 store->cil_code = sp [0]->cil_code;
2244 if (store->opcode == CEE_STOBJ) {
2245 NEW_TEMPLOADA (cfg, store, temp->inst_c0);
2246 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, temp->klass, FALSE, FALSE);
2248 MONO_ADD_INS (bblock, store);
2256 inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoBasicBlock *bblock, MonoInst **sp,
2257 guchar *ip, guint real_offset, GList *dont_inline, MonoBasicBlock **last_b)
2259 MonoInst *ins, *rvar = NULL;
2260 MonoMethodHeader *cheader;
2261 MonoBasicBlock *ebblock, *sbblock;
2262 int i, costs, new_locals_offset;
2264 if (cfg->verbose_level > 2)
2265 g_print ("INLINE START %p %s\n", cmethod, mono_method_full_name (cmethod, TRUE));
2267 cheader = ((MonoMethodNormal *)cmethod)->header;
2269 if (!cmethod->inline_info) {
2270 mono_jit_stats.inlineable_methods++;
2271 cmethod->inline_info = 1;
2273 /* allocate space to store the return value */
2274 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
2275 rvar = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
2278 /* allocate local variables */
2279 new_locals_offset = cfg->num_varinfo;
2280 for (i = 0; i < cheader->num_locals; ++i)
2281 mono_compile_create_var (cfg, cheader->locals [i], OP_LOCAL);
2283 /* allocate starte and end blocks */
2284 sbblock = NEW_BBLOCK (cfg);
2285 sbblock->block_num = cfg->num_bblocks++;
2286 sbblock->real_offset = real_offset;
2288 ebblock = NEW_BBLOCK (cfg);
2289 ebblock->block_num = cfg->num_bblocks++;
2290 ebblock->real_offset = real_offset;
2292 costs = mono_method_to_ir (cfg, cmethod, sbblock, ebblock, new_locals_offset, rvar, dont_inline, sp, real_offset, *ip == CEE_CALLVIRT);
2294 if (costs >= 0 && costs < 60) {
2295 if (cfg->verbose_level > 2)
2296 g_print ("INLINE END %s\n", mono_method_full_name (cmethod, TRUE));
2298 mono_jit_stats.inlined_methods++;
2300 /* always add some code to avoid block split failures */
2301 MONO_INST_NEW (cfg, ins, CEE_NOP);
2302 MONO_ADD_INS (bblock, ins);
2305 bblock->next_bb = sbblock;
2306 link_bblock (cfg, bblock, sbblock);
2309 NEW_TEMPLOAD (cfg, ins, rvar->inst_c0);
2315 if (cfg->verbose_level > 2)
2316 g_print ("INLINE ABORTED %s\n", mono_method_full_name (cmethod, TRUE));
2322 * Some of these comments may well be out-of-date.
2323 * Design decisions: we do a single pass over the IL code (and we do bblock
2324 * splitting/merging in the few cases when it's required: a back jump to an IL
2325 * address that was not already seen as bblock starting point).
2326 * Code is validated as we go (full verification is still better left to metadata/verify.c).
2327 * Complex operations are decomposed in simpler ones right away. We need to let the
2328 * arch-specific code peek and poke inside this process somehow (except when the
2329 * optimizations can take advantage of the full semantic info of coarse opcodes).
2330 * All the opcodes of the form opcode.s are 'normalized' to opcode.
2331 * MonoInst->opcode initially is the IL opcode or some simplification of that
2332 * (OP_LOAD, OP_STORE). The arch-specific code may rearrange it to an arch-specific
2333 * opcode with value bigger than OP_LAST.
2334 * At this point the IR can be handed over to an interpreter, a dumb code generator
2335 * or to the optimizing code generator that will translate it to SSA form.
2337 * Profiling directed optimizations.
2338 * We may compile by default with few or no optimizations and instrument the code
2339 * or the user may indicate what methods to optimize the most either in a config file
2340 * or through repeated runs where the compiler applies offline the optimizations to
2341 * each method and then decides if it was worth it.
2344 * * consider using an array instead of an hash table (bb_hash)
2347 #define CHECK_TYPE(ins) if (!(ins)->type) goto unverified
2348 #define CHECK_STACK(num) if ((sp - stack_start) < (num)) goto unverified
2349 #define CHECK_STACK_OVF(num) if (((sp - stack_start) + (num)) > header->max_stack) goto unverified
2351 #define TYPE_PARAM_TO_TYPE(num) (method->klass->generic_inst->data.generic_inst->type_argv [(num)])
2352 #define TYPE_PARAM_TO_CLASS(num) (mono_class_from_mono_type (TYPE_PARAM_TO_TYPE ((num))))
2354 /* offset from br.s -> br like opcodes */
2355 #define BIG_BRANCH_OFFSET 13
2358 get_basic_blocks (MonoCompile *cfg, GHashTable *bbhash, MonoMethodHeader* header, guint real_offset, unsigned char *start, unsigned char *end)
2360 unsigned char *ip = start;
2361 unsigned char *target;
2364 MonoBasicBlock *bblock;
2365 const MonoOpcode *opcode;
2368 cli_addr = ip - start;
2369 i = mono_opcode_value ((const guint8 **)&ip);
2370 opcode = &mono_opcodes [i];
2371 switch (opcode->argument) {
2372 case MonoInlineNone:
2375 case MonoInlineString:
2376 case MonoInlineType:
2377 case MonoInlineField:
2378 case MonoInlineMethod:
2381 case MonoShortInlineR:
2388 case MonoShortInlineVar:
2389 case MonoShortInlineI:
2392 case MonoShortInlineBrTarget:
2393 target = start + cli_addr + 2 + (signed char)ip [1];
2394 GET_BBLOCK (cfg, bbhash, bblock, target);
2397 case MonoInlineBrTarget:
2398 target = start + cli_addr + 5 + (gint32)read32 (ip + 1);
2399 GET_BBLOCK (cfg, bbhash, bblock, target);
2402 case MonoInlineSwitch: {
2403 guint32 n = read32 (ip + 1);
2406 cli_addr += 5 + 4 * n;
2407 target = start + cli_addr;
2408 GET_BBLOCK (cfg, bbhash, bblock, target);
2410 for (j = 0; j < n; ++j) {
2411 target = start + cli_addr + (gint32)read32 (ip);
2412 GET_BBLOCK (cfg, bbhash, bblock, target);
2422 g_assert_not_reached ();
2430 static MonoClassField *
2431 get_generic_field_inst (MonoClassField *field, MonoClass *klass, MonoClass **retclass)
2434 for (i = 0; i < field->parent->field.count; ++i) {
2435 if (field == &field->parent->fields [i]) {
2437 return &klass->fields [i];
2444 * mono_method_to_ir: translates IL into basic blocks containing trees
2447 mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_bblock, MonoBasicBlock *end_bblock,
2448 int locals_offset, MonoInst *return_var, GList *dont_inline, MonoInst **inline_args,
2449 guint inline_offset, gboolean is_virtual_call)
2451 MonoInst *zero_int32, *zero_int64, *zero_ptr, *zero_obj, *zero_r8;
2452 MonoInst *ins, **sp, **stack_start;
2453 MonoBasicBlock *bblock, *tblock = NULL, *init_localsbb = NULL;
2455 MonoMethod *cmethod;
2456 MonoInst **arg_array;
2457 MonoMethodHeader *header;
2459 guint32 token, ins_flag;
2461 unsigned char *ip, *end, *target;
2462 static double r8_0 = 0.0;
2463 MonoMethodSignature *sig;
2464 MonoType **param_types;
2465 GList *bb_recheck = NULL, *tmp;
2466 int i, n, start_new_bblock, align;
2467 int num_calls = 0, inline_costs = 0;
2468 int *filter_lengths = NULL;
2469 int breakpoint_id = 0;
2472 image = method->klass->image;
2473 header = ((MonoMethodNormal *)method)->header;
2474 sig = method->signature;
2475 ip = (unsigned char*)header->code;
2476 end = ip + header->code_size;
2477 mono_jit_stats.cil_code_size += header->code_size;
2479 if (cfg->method == method) {
2481 bbhash = cfg->bb_hash;
2483 real_offset = inline_offset;
2484 bbhash = g_hash_table_new (g_direct_hash, NULL);
2487 if (cfg->verbose_level > 2)
2488 g_print ("method to IR %s\n", mono_method_full_name (method, TRUE));
2490 if (cfg->prof_options & MONO_PROFILE_INS_COVERAGE)
2491 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, header->code_size);
2493 dont_inline = g_list_prepend (dont_inline, method);
2494 if (cfg->method == method) {
2497 cfg->bb_entry = start_bblock = NEW_BBLOCK (cfg);
2498 start_bblock->cil_code = NULL;
2499 start_bblock->cil_length = 0;
2500 start_bblock->block_num = cfg->num_bblocks++;
2503 cfg->bb_exit = end_bblock = NEW_BBLOCK (cfg);
2504 end_bblock->cil_code = NULL;
2505 end_bblock->cil_length = 0;
2506 end_bblock->block_num = cfg->num_bblocks++;
2507 g_assert (cfg->num_bblocks == 2);
2509 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2510 for (i = sig->hasthis + sig->param_count - 1; i >= 0; i--)
2511 arg_array [i] = cfg->varinfo [i];
2513 if (mono_compile_aot)
2514 cfg->opt |= MONO_OPT_SHARED;
2516 if (header->num_clauses) {
2517 int size = sizeof (int) * header->num_clauses;
2518 filter_lengths = alloca (size);
2519 memset (filter_lengths, 0, size);
2521 cfg->spvar = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_LOCAL);
2522 /* prevent it from being register allocated */
2523 cfg->spvar->flags |= MONO_INST_INDIRECT;
2525 /* handle exception clauses */
2526 for (i = 0; i < header->num_clauses; ++i) {
2527 //unsigned char *p = ip;
2528 MonoExceptionClause *clause = &header->clauses [i];
2529 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->try_offset);
2530 tblock->real_offset = clause->try_offset;
2531 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->handler_offset);
2532 tblock->real_offset = clause->handler_offset;
2534 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY ||
2535 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2536 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2537 MONO_ADD_INS (tblock, ins);
2540 /*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);
2542 g_print ("%s", mono_disasm_code_one (NULL, method, p, &p));
2544 /* catch and filter blocks get the exception object on the stack */
2545 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE ||
2546 clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2547 /* mostly like handle_stack_args (), but just sets the input args */
2548 /* g_print ("handling clause at IL_%04x\n", clause->handler_offset); */
2550 cfg->exvar = mono_compile_create_var (cfg, &mono_defaults.object_class->byval_arg, OP_LOCAL);
2551 /* prevent it from being register allocated */
2552 cfg->exvar->flags |= MONO_INST_INDIRECT;
2554 tblock->in_scount = 1;
2555 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2556 tblock->in_stack [0] = cfg->exvar;
2558 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2559 GET_BBLOCK (cfg, bbhash, tblock, ip + clause->token_or_filter);
2560 tblock->real_offset = clause->token_or_filter;
2561 tblock->in_scount = 1;
2562 tblock->in_stack = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*));
2563 tblock->in_stack [0] = cfg->exvar;
2564 MONO_INST_NEW (cfg, ins, OP_START_HANDLER);
2565 MONO_ADD_INS (tblock, ins);
2571 arg_array = alloca (sizeof (MonoInst *) * (sig->hasthis + sig->param_count));
2572 mono_save_args (cfg, start_bblock, sig, inline_args, arg_array);
2575 /* FIRST CODE BLOCK */
2576 bblock = NEW_BBLOCK (cfg);
2577 bblock->cil_code = ip;
2579 ADD_BBLOCK (cfg, bbhash, bblock);
2581 if (cfg->method == method) {
2582 breakpoint_id = mono_debugger_method_has_breakpoint (method);
2583 if (breakpoint_id && (mono_debug_format != MONO_DEBUG_FORMAT_DEBUGGER)) {
2584 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2585 MONO_ADD_INS (bblock, ins);
2589 if ((header->init_locals || (cfg->method == method && (cfg->opt & MONO_OPT_SHARED)))) {
2590 /* we use a separate basic block for the initialization code */
2591 cfg->bb_init = init_localsbb = NEW_BBLOCK (cfg);
2592 init_localsbb->real_offset = real_offset;
2593 start_bblock->next_bb = init_localsbb;
2594 init_localsbb->next_bb = bblock;
2595 link_bblock (cfg, start_bblock, init_localsbb);
2596 link_bblock (cfg, init_localsbb, bblock);
2597 init_localsbb->block_num = cfg->num_bblocks++;
2599 start_bblock->next_bb = bblock;
2600 link_bblock (cfg, start_bblock, bblock);
2603 if (get_basic_blocks (cfg, bbhash, header, real_offset, ip, end))
2606 mono_debug_init_method (cfg, bblock, breakpoint_id);
2608 param_types = mono_mempool_alloc (cfg->mempool, sizeof (MonoType*) * (sig->hasthis + sig->param_count));
2610 param_types [0] = method->klass->valuetype?&method->klass->this_arg:&method->klass->byval_arg;
2611 for (n = 0; n < sig->param_count; ++n)
2612 param_types [n + sig->hasthis] = sig->params [n];
2614 /* do this somewhere outside - not here */
2615 NEW_ICONST (cfg, zero_int32, 0);
2616 NEW_ICONST (cfg, zero_int64, 0);
2617 zero_int64->type = STACK_I8;
2618 NEW_PCONST (cfg, zero_ptr, 0);
2619 NEW_PCONST (cfg, zero_obj, 0);
2620 zero_obj->type = STACK_OBJ;
2622 MONO_INST_NEW (cfg, zero_r8, OP_R8CONST);
2623 zero_r8->type = STACK_R8;
2624 zero_r8->inst_p0 = &r8_0;
2626 /* add a check for this != NULL to inlined methods */
2627 if (is_virtual_call) {
2628 MONO_INST_NEW (cfg, ins, OP_CHECK_THIS);
2629 NEW_ARGLOAD (cfg, ins->inst_left, 0);
2631 MONO_ADD_INS (bblock, ins);
2634 /* we use a spare stack slot in SWITCH and NEWOBJ and others */
2635 stack_start = sp = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst*) * (header->max_stack + 1));
2638 start_new_bblock = 0;
2641 if (cfg->method == method)
2642 real_offset = ip - header->code;
2644 real_offset = inline_offset;
2646 if (start_new_bblock) {
2647 bblock->cil_length = ip - bblock->cil_code;
2648 if (start_new_bblock == 2) {
2649 g_assert (ip == tblock->cil_code);
2651 GET_BBLOCK (cfg, bbhash, tblock, ip);
2653 bblock->next_bb = tblock;
2655 start_new_bblock = 0;
2656 for (i = 0; i < bblock->in_scount; ++i) {
2657 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2661 if ((tblock = g_hash_table_lookup (bbhash, ip)) && (tblock != bblock)) {
2662 link_bblock (cfg, bblock, tblock);
2663 if (sp != stack_start) {
2664 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
2667 bblock->next_bb = tblock;
2669 for (i = 0; i < bblock->in_scount; ++i) {
2670 NEW_TEMPLOAD (cfg, ins, bblock->in_stack [i]->inst_c0);
2676 if (cfg->coverage_info) {
2677 MonoInst *store, *one;
2678 guint32 cil_offset = ip - header->code;
2679 cfg->coverage_info->data [cil_offset].cil_code = ip;
2681 /* TODO: Use an increment here */
2682 NEW_ICONST (cfg, one, 1);
2685 NEW_PCONST (cfg, ins, &(cfg->coverage_info->data [cil_offset].count));
2688 MONO_INST_NEW (cfg, store, CEE_STIND_I);
2689 store->cil_code = ip;
2690 store->inst_left = ins;
2691 store->inst_right = one;
2693 MONO_ADD_INS (bblock, store);
2696 if (cfg->verbose_level > 3)
2697 g_print ("converting (in B%d: stack: %d) %s", bblock->block_num, sp-stack_start, mono_disasm_code_one (NULL, method, ip, NULL));
2704 MONO_INST_NEW (cfg, ins, CEE_BREAK);
2705 ins->cil_code = ip++;
2706 MONO_ADD_INS (bblock, ins);
2712 CHECK_STACK_OVF (1);
2713 n = (*ip)-CEE_LDARG_0;
2714 NEW_ARGLOAD (cfg, ins, n);
2715 ins->cil_code = ip++;
2722 CHECK_STACK_OVF (1);
2723 n = (*ip)-CEE_LDLOC_0;
2724 NEW_LOCLOAD (cfg, ins, n);
2725 ins->cil_code = ip++;
2733 n = (*ip)-CEE_STLOC_0;
2735 handle_loaded_temps (cfg, bblock, stack_start, sp);
2736 NEW_LOCSTORE (cfg, ins, n, *sp);
2738 if (ins->opcode == CEE_STOBJ) {
2739 NEW_LOCLOADA (cfg, ins, n);
2740 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2742 MONO_ADD_INS (bblock, ins);
2747 CHECK_STACK_OVF (1);
2748 NEW_ARGLOAD (cfg, ins, ip [1]);
2754 CHECK_STACK_OVF (1);
2755 NEW_ARGLOADA (cfg, ins, ip [1]);
2763 NEW_ARGSTORE (cfg, ins, ip [1], *sp);
2764 handle_loaded_temps (cfg, bblock, stack_start, sp);
2766 if (ins->opcode == CEE_STOBJ) {
2767 NEW_ARGLOADA (cfg, ins, ip [1]);
2768 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2770 MONO_ADD_INS (bblock, ins);
2774 CHECK_STACK_OVF (1);
2775 NEW_LOCLOAD (cfg, ins, ip [1]);
2781 CHECK_STACK_OVF (1);
2782 NEW_LOCLOADA (cfg, ins, ip [1]);
2790 handle_loaded_temps (cfg, bblock, stack_start, sp);
2791 NEW_LOCSTORE (cfg, ins, ip [1], *sp);
2793 if (ins->opcode == CEE_STOBJ) {
2794 NEW_LOCLOADA (cfg, ins, ip [1]);
2795 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
2797 MONO_ADD_INS (bblock, ins);
2802 CHECK_STACK_OVF (1);
2803 NEW_PCONST (cfg, ins, NULL);
2805 ins->type = STACK_OBJ;
2810 CHECK_STACK_OVF (1);
2811 NEW_ICONST (cfg, ins, -1);
2825 CHECK_STACK_OVF (1);
2826 NEW_ICONST (cfg, ins, (*ip) - CEE_LDC_I4_0);
2832 CHECK_STACK_OVF (1);
2834 NEW_ICONST (cfg, ins, *((signed char*)ip));
2840 CHECK_STACK_OVF (1);
2841 NEW_ICONST (cfg, ins, (gint32)read32 (ip + 1));
2847 CHECK_STACK_OVF (1);
2848 MONO_INST_NEW (cfg, ins, OP_I8CONST);
2850 ins->type = STACK_I8;
2852 ins->inst_l = (gint64)read64 (ip);
2857 float *f = g_malloc (sizeof (float));
2858 CHECK_STACK_OVF (1);
2859 MONO_INST_NEW (cfg, ins, OP_R4CONST);
2860 ins->type = STACK_R8;
2869 double *d = g_malloc (sizeof (double));
2870 CHECK_STACK_OVF (1);
2871 MONO_INST_NEW (cfg, ins, OP_R8CONST);
2872 ins->type = STACK_R8;
2881 MonoInst *temp, *store;
2883 CHECK_STACK_OVF (1);
2888 * small optimization: if the loaded value was from a local already,
2889 * just load it twice.
2891 if (ins->ssa_op == MONO_SSA_LOAD &&
2892 (ins->inst_i0->opcode == OP_LOCAL || ins->inst_i0->opcode == OP_ARG)) {
2894 MONO_INST_NEW (cfg, temp, 0);
2896 temp->cil_code = ip;
2899 temp = mono_compile_create_var (cfg, type_from_stack_type (ins), OP_LOCAL);
2900 temp->cil_code = ip;
2901 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
2902 store->cil_code = ip;
2903 MONO_ADD_INS (bblock, store);
2904 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2907 NEW_TEMPLOAD (cfg, ins, temp->inst_c0);
2917 MONO_INST_NEW (cfg, ins, CEE_POP);
2918 MONO_ADD_INS (bblock, ins);
2919 ins->cil_code = ip++;
2924 if (stack_start != sp)
2926 MONO_INST_NEW (cfg, ins, CEE_JMP);
2927 token = read32 (ip + 1);
2928 /* FIXME: check the signature matches */
2929 cmethod = mono_get_method (image, token, NULL);
2930 ins->inst_p0 = cmethod;
2931 MONO_ADD_INS (bblock, ins);
2933 start_new_bblock = 1;
2937 case CEE_CALLVIRT: {
2938 MonoInst *addr = NULL;
2939 MonoMethodSignature *fsig = NULL;
2940 int temp, array_rank = 0;
2941 int virtual = *ip == CEE_CALLVIRT;
2943 token = read32 (ip + 1);
2945 if (*ip == CEE_CALLI) {
2950 if (method->wrapper_type != MONO_WRAPPER_NONE)
2951 fsig = (MonoMethodSignature *)mono_method_get_wrapper_data (method, token);
2953 fsig = mono_metadata_parse_signature (image, token);
2955 n = fsig->param_count + fsig->hasthis;
2958 if (method->wrapper_type != MONO_WRAPPER_NONE) {
2959 cmethod = (MonoMethod *)mono_method_get_wrapper_data (method, token);
2961 cmethod = mono_get_method (image, token, NULL);
2964 if (!cmethod->klass->inited)
2965 mono_class_init (cmethod->klass);
2967 if (cmethod->signature->pinvoke) {
2968 #ifdef MONO_USE_EXC_TABLES
2969 if (mono_method_blittable (cmethod)) {
2970 fsig = cmethod->signature;
2973 MonoMethod *wrapper = mono_marshal_get_native_wrapper (cmethod);
2974 fsig = wrapper->signature;
2975 #ifdef MONO_USE_EXC_TABLES
2979 fsig = mono_method_get_signature (cmethod, image, token);
2982 n = fsig->param_count + fsig->hasthis;
2984 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL &&
2985 cmethod->klass->parent == mono_defaults.array_class) {
2986 array_rank = cmethod->klass->rank;
2989 if (cmethod->string_ctor)
2990 g_assert_not_reached ();
2996 //g_assert (!virtual || fsig->hasthis);
3000 if (*ip != CEE_CALLI && check_call_signature (cfg, fsig, sp))
3003 if ((ins_flag & MONO_INST_TAILCALL) && cmethod && (*ip == CEE_CALL)) {
3005 for (i = 0; i < n; ++i) {
3006 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3008 MONO_ADD_INS (bblock, ins);
3010 MONO_INST_NEW (cfg, ins, CEE_JMP);
3012 ins->inst_p0 = cmethod;
3013 MONO_ADD_INS (bblock, ins);
3014 start_new_bblock = 1;
3015 /* skip CEE_RET as well */
3020 if (cmethod && (cfg->opt & MONO_OPT_INTRINS) && (ins = mini_get_opcode_for_method (cfg, cmethod, fsig, sp))) {
3023 if (MONO_TYPE_IS_VOID (fsig->ret)) {
3024 MONO_ADD_INS (bblock, ins);
3026 type_to_eval_stack_type (fsig->ret, ins);
3035 handle_loaded_temps (cfg, bblock, stack_start, sp);
3037 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3038 (!virtual || !(cmethod->flags & METHOD_ATTRIBUTE_VIRTUAL) || (cmethod->flags & METHOD_ATTRIBUTE_FINAL)) &&
3039 mono_method_check_inlining (cfg, cmethod) &&
3040 !g_list_find (dont_inline, cmethod)) {
3042 MonoBasicBlock *ebblock;
3044 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3048 GET_BBLOCK (cfg, bbhash, bblock, ip);
3049 ebblock->next_bb = bblock;
3050 link_bblock (cfg, ebblock, bblock);
3052 if (!MONO_TYPE_IS_VOID (fsig->ret))
3055 /* indicates start of a new block, and triggers a load of all
3056 stack arguments at bb boundarie */
3059 inline_costs += costs;
3064 inline_costs += 10 * num_calls++;
3066 /* tail recursion elimination */
3067 if ((cfg->opt & MONO_OPT_TAILC) && *ip == CEE_CALL && cmethod == cfg->method && ip [5] == CEE_RET) {
3068 gboolean has_vtargs = FALSE;
3071 /* keep it simple */
3072 for (i = fsig->param_count - 1; i >= 0; i--) {
3073 if (MONO_TYPE_ISSTRUCT (cmethod->signature->params [i]))
3078 for (i = 0; i < n; ++i) {
3079 NEW_ARGSTORE (cfg, ins, i, sp [i]);
3081 MONO_ADD_INS (bblock, ins);
3083 MONO_INST_NEW (cfg, ins, CEE_BR);
3085 MONO_ADD_INS (bblock, ins);
3086 tblock = start_bblock->out_bb [0];
3087 link_bblock (cfg, bblock, tblock);
3088 ins->inst_target_bb = tblock;
3089 start_new_bblock = 1;
3092 if (!MONO_TYPE_IS_VOID (fsig->ret)) {
3093 /* just create a dummy - the value is never used */
3094 ins = mono_compile_create_var (cfg, fsig->ret, OP_LOCAL);
3095 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3103 if (*ip == CEE_CALLI) {
3105 if ((temp = mono_emit_calli (cfg, bblock, fsig, sp, addr, ip)) != -1) {
3106 NEW_TEMPLOAD (cfg, *sp, temp);
3110 } else if (array_rank) {
3113 if (strcmp (cmethod->name, "Set") == 0) { /* array Set */
3114 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, TRUE);
3115 NEW_INDSTORE (cfg, ins, addr, sp [fsig->param_count], fsig->params [fsig->param_count - 1]);
3117 if (ins->opcode == CEE_STOBJ) {
3118 handle_stobj (cfg, bblock, addr, sp [fsig->param_count], ip, mono_class_from_mono_type (fsig->params [fsig->param_count-1]), FALSE, FALSE);
3120 MONO_ADD_INS (bblock, ins);
3123 } else if (strcmp (cmethod->name, "Get") == 0) { /* array Get */
3124 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3125 NEW_INDLOAD (cfg, ins, addr, fsig->ret);
3129 } else if (strcmp (cmethod->name, "Address") == 0) { /* array Address */
3130 addr = mini_get_ldelema_ins (cfg, bblock, cmethod, sp, ip, FALSE);
3133 g_assert_not_reached ();
3137 if (0 && CODE_IS_STLOC (ip + 5) && (!MONO_TYPE_ISSTRUCT (fsig->ret)) && (!MONO_TYPE_IS_VOID (fsig->ret) || cmethod->string_ctor)) {
3138 /* no need to spill */
3139 ins = (MonoInst*)mono_emit_method_call (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL);
3142 if ((temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, virtual ? sp [0] : NULL)) != -1) {
3143 NEW_TEMPLOAD (cfg, *sp, temp);
3153 if (cfg->method != method) {
3154 /* return from inlined methode */
3159 //g_assert (returnvar != -1);
3160 NEW_TEMPSTORE (cfg, store, return_var->inst_c0, *sp);
3161 store->cil_code = sp [0]->cil_code;
3162 if (store->opcode == CEE_STOBJ) {
3163 g_assert_not_reached ();
3164 NEW_TEMPLOADA (cfg, store, return_var->inst_c0);
3165 handle_stobj (cfg, bblock, store, *sp, sp [0]->cil_code, return_var->klass, FALSE, FALSE);
3167 MONO_ADD_INS (bblock, store);
3171 g_assert (!return_var);
3174 MONO_INST_NEW (cfg, ins, CEE_NOP);
3175 ins->opcode = mono_type_to_stind (method->signature->ret);
3176 if (ins->opcode == CEE_STOBJ) {
3177 NEW_RETLOADA (cfg, ins);
3178 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
3180 ins->opcode = OP_SETRET;
3182 ins->inst_i0 = *sp;;
3183 ins->inst_i1 = NULL;
3184 MONO_ADD_INS (bblock, ins);
3188 if (sp != stack_start)
3190 MONO_INST_NEW (cfg, ins, CEE_BR);
3191 ins->cil_code = ip++;
3192 ins->inst_target_bb = end_bblock;
3193 MONO_ADD_INS (bblock, ins);
3194 link_bblock (cfg, bblock, end_bblock);
3195 start_new_bblock = 1;
3198 MONO_INST_NEW (cfg, ins, CEE_BR);
3199 ins->cil_code = ip++;
3200 MONO_ADD_INS (bblock, ins);
3201 target = ip + 1 + (signed char)(*ip);
3203 GET_BBLOCK (cfg, bbhash, tblock, target);
3204 link_bblock (cfg, bblock, tblock);
3205 CHECK_BBLOCK (target, ip, tblock);
3206 ins->inst_target_bb = tblock;
3207 if (sp != stack_start) {
3208 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3211 start_new_bblock = 1;
3217 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3218 ins->cil_code = ip++;
3219 target = ip + 1 + *(signed char*)ip;
3221 ADD_UNCOND (ins->opcode == CEE_BRTRUE);
3222 if (sp != stack_start) {
3223 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3239 MONO_INST_NEW (cfg, ins, *ip + BIG_BRANCH_OFFSET);
3240 ins->cil_code = ip++;
3241 target = ip + 1 + *(signed char*)ip;
3244 if (sp != stack_start) {
3245 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3251 MONO_INST_NEW (cfg, ins, CEE_BR);
3252 ins->cil_code = ip++;
3253 MONO_ADD_INS (bblock, ins);
3254 target = ip + 4 + (gint32)read32(ip);
3256 GET_BBLOCK (cfg, bbhash, tblock, target);
3257 link_bblock (cfg, bblock, tblock);
3258 CHECK_BBLOCK (target, ip, tblock);
3259 ins->inst_target_bb = tblock;
3260 if (sp != stack_start) {
3261 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3264 start_new_bblock = 1;
3270 MONO_INST_NEW (cfg, ins, *ip);
3271 ins->cil_code = ip++;
3272 target = ip + 4 + (gint32)read32(ip);
3274 ADD_UNCOND(ins->opcode == CEE_BRTRUE);
3275 if (sp != stack_start) {
3276 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3292 MONO_INST_NEW (cfg, ins, *ip);
3293 ins->cil_code = ip++;
3294 target = ip + 4 + (gint32)read32(ip);
3297 if (sp != stack_start) {
3298 handle_stack_args (cfg, bblock, stack_start, sp - stack_start);
3305 n = read32 (ip + 1);
3306 MONO_INST_NEW (cfg, ins, *ip);
3308 ins->inst_left = *sp;
3309 if (ins->inst_left->type != STACK_I4) goto unverified;
3312 target = ip + n * sizeof (guint32);
3313 MONO_ADD_INS (bblock, ins);
3314 GET_BBLOCK (cfg, bbhash, tblock, target);
3315 link_bblock (cfg, bblock, tblock);
3316 ins->klass = GUINT_TO_POINTER (n);
3317 ins->inst_many_bb = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (n + 1));
3318 ins->inst_many_bb [n] = tblock;
3320 for (i = 0; i < n; ++i) {
3321 GET_BBLOCK (cfg, bbhash, tblock, target + (gint32)read32(ip));
3322 link_bblock (cfg, bblock, tblock);
3323 ins->inst_many_bb [i] = tblock;
3326 /* FIXME: handle stack args */
3341 MONO_INST_NEW (cfg, ins, *ip);
3346 ins->type = ldind_type [*ip - CEE_LDIND_I1];
3347 ins->flags |= ins_flag;
3359 MONO_INST_NEW (cfg, ins, *ip);
3360 ins->cil_code = ip++;
3362 handle_loaded_temps (cfg, bblock, stack_start, sp);
3363 MONO_ADD_INS (bblock, ins);
3364 ins->inst_i0 = sp [0];
3365 ins->inst_i1 = sp [1];
3366 ins->flags |= ins_flag;
3397 case CEE_CONV_OVF_I8:
3398 case CEE_CONV_OVF_U8:
3404 case CEE_CONV_OVF_I4:
3405 case CEE_CONV_OVF_I1:
3406 case CEE_CONV_OVF_I2:
3407 case CEE_CONV_OVF_I:
3408 case CEE_CONV_OVF_U:
3411 if (sp [-1]->type == STACK_R8) {
3412 ADD_UNOP (CEE_CONV_OVF_I8);
3420 case CEE_CONV_OVF_U1:
3421 case CEE_CONV_OVF_U2:
3422 case CEE_CONV_OVF_U4:
3425 if (sp [-1]->type == STACK_R8) {
3426 ADD_UNOP (CEE_CONV_OVF_U8);
3434 case CEE_CONV_OVF_I1_UN:
3435 case CEE_CONV_OVF_I2_UN:
3436 case CEE_CONV_OVF_I4_UN:
3437 case CEE_CONV_OVF_I8_UN:
3438 case CEE_CONV_OVF_U1_UN:
3439 case CEE_CONV_OVF_U2_UN:
3440 case CEE_CONV_OVF_U4_UN:
3441 case CEE_CONV_OVF_U8_UN:
3442 case CEE_CONV_OVF_I_UN:
3443 case CEE_CONV_OVF_U_UN:
3449 g_error ("opcode 0x%02x not handled", *ip);
3452 MonoInst *iargs [3];
3455 token = read32 (ip + 1);
3456 if (method->wrapper_type != MONO_WRAPPER_NONE)
3457 klass = mono_method_get_wrapper_data (method, token);
3459 klass = mono_class_get (image, token);
3461 mono_class_init (klass);
3462 if (klass->byval_arg.type == MONO_TYPE_VAR)
3463 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3464 n = mono_class_value_size (klass, NULL);
3465 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3466 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3467 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3469 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3470 copy->inst_left = iargs [0];
3471 copy->inst_right = *sp;
3472 copy->cil_code = ip;
3474 MONO_ADD_INS (bblock, copy);
3477 NEW_ICONST (cfg, iargs [2], n);
3478 iargs [2]->cil_code = ip;
3480 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3482 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3489 CHECK_STACK_OVF (1);
3490 n = read32 (ip + 1);
3492 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3494 MonoInst *iargs [1];
3496 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3497 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3498 NEW_TEMPLOAD (cfg, *sp, temp);
3502 if (mono_compile_aot) {
3503 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3506 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3508 MonoInst *iargs [3];
3509 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3510 NEW_IMAGECONST (cfg, iargs [1], image);
3511 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3512 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3513 NEW_TEMPLOAD (cfg, *sp, temp);
3514 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3516 NEW_PCONST (cfg, ins, NULL);
3518 ins->type = STACK_OBJ;
3519 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3528 MonoInst *iargs [2];
3529 MonoMethodSignature *fsig;
3532 token = read32 (ip + 1);
3533 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3534 cmethod = mono_method_get_wrapper_data (method, token);
3536 cmethod = mono_get_method (image, token, NULL);
3537 fsig = mono_method_get_signature (cmethod, image, token);
3539 mono_class_init (cmethod->klass);
3541 n = fsig->param_count;
3544 /* move the args to allow room for 'this' in the first position */
3550 handle_loaded_temps (cfg, bblock, stack_start, sp);
3553 if (cmethod->klass->parent == mono_defaults.array_class) {
3554 NEW_METHODCONST (cfg, *sp, cmethod);
3555 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3557 } else if (cmethod->string_ctor) {
3558 /* we simply pass a null pointer */
3559 NEW_PCONST (cfg, *sp, NULL);
3560 /* now call the string ctor */
3561 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3563 if (cmethod->klass->valuetype) {
3564 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3565 temp = iargs [0]->inst_c0;
3566 NEW_TEMPLOADA (cfg, *sp, temp);
3568 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3569 NEW_DOMAINCONST (cfg, iargs [0]);
3570 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3572 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3574 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3575 NEW_PCONST (cfg, iargs [0], vtable);
3576 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3577 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3579 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3581 NEW_TEMPLOAD (cfg, *sp, temp);
3584 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3585 mono_method_check_inlining (cfg, cmethod) &&
3586 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3587 !g_list_find (dont_inline, cmethod)) {
3589 MonoBasicBlock *ebblock;
3590 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3595 GET_BBLOCK (cfg, bbhash, bblock, ip);
3596 ebblock->next_bb = bblock;
3597 link_bblock (cfg, ebblock, bblock);
3599 NEW_TEMPLOAD (cfg, *sp, temp);
3602 /* indicates start of a new block, and triggers a load
3603 of all stack arguments at bb boundarie */
3606 inline_costs += costs;
3610 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3613 /* now call the actual ctor */
3614 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3618 NEW_TEMPLOAD (cfg, *sp, temp);
3627 MONO_INST_NEW (cfg, ins, *ip);
3629 klass = mono_class_get (image, read32 (ip + 1));
3630 mono_class_init (klass);
3631 ins->type = STACK_OBJ;
3632 ins->inst_left = *sp;
3633 ins->inst_newa_class = klass;
3639 MonoInst *add, *vtoffset;
3643 token = read32 (ip + 1);
3644 if (method->wrapper_type != MONO_WRAPPER_NONE)
3645 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3647 klass = mono_class_get (image, token);
3648 mono_class_init (klass);
3650 if (klass->byval_arg.type == MONO_TYPE_VAR)
3651 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3653 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3654 ins->type = STACK_OBJ;
3655 ins->inst_left = *sp;
3657 ins->inst_newa_class = klass;
3660 MONO_INST_NEW (cfg, add, CEE_ADD);
3661 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3662 add->inst_left = ins;
3663 add->inst_right = vtoffset;
3664 add->type = STACK_MP;
3672 MONO_INST_NEW (cfg, ins, *ip);
3674 klass = mono_class_get (image, read32 (ip + 1));
3675 mono_class_init (klass);
3676 if (klass->byval_arg.type == MONO_TYPE_VAR)
3677 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3678 ins->type = STACK_OBJ;
3679 ins->inst_left = *sp;
3681 ins->inst_newa_class = klass;
3688 MONO_INST_NEW (cfg, ins, *ip);
3690 ins->inst_left = *sp;
3691 ins->cil_code = ip++;
3692 MONO_ADD_INS (bblock, ins);
3694 start_new_bblock = 1;
3699 MonoInst *offset_ins;
3700 MonoClassField *field;
3701 MonoBasicBlock *ebblock;
3705 if (*ip == CEE_STFLD) {
3712 // FIXME: enable this test later.
3713 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3715 token = read32 (ip + 1);
3716 field = mono_field_from_token (image, token, &klass);
3717 if (field->parent->gen_params)
3718 field = get_generic_field_inst (field, method->klass, &klass);
3719 mono_class_init (klass);
3721 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3722 /* FIXME: mark instructions for use in SSA */
3723 if (*ip == CEE_STFLD) {
3724 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3725 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3726 MonoInst *iargs [5];
3729 NEW_CLASSCONST (cfg, iargs [1], klass);
3730 NEW_FIELDCONST (cfg, iargs [2], field);
3731 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3735 if (cfg->opt & MONO_OPT_INLINE) {
3736 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3737 iargs, ip, real_offset, dont_inline, &ebblock);
3738 g_assert (costs > 0);
3743 GET_BBLOCK (cfg, bbhash, bblock, ip);
3744 ebblock->next_bb = bblock;
3745 link_bblock (cfg, ebblock, bblock);
3747 /* indicates start of a new block, and triggers a load
3748 of all stack arguments at bb boundarie */
3751 inline_costs += costs;
3754 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3758 NEW_ICONST (cfg, offset_ins, foffset);
3759 MONO_INST_NEW (cfg, ins, CEE_ADD);
3761 ins->inst_left = *sp;
3762 ins->inst_right = offset_ins;
3763 ins->type = STACK_MP;
3765 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3766 store->cil_code = ip;
3767 store->inst_left = ins;
3768 store->inst_right = sp [1];
3769 handle_loaded_temps (cfg, bblock, stack_start, sp);
3770 store->flags |= ins_flag;
3772 if (store->opcode == CEE_STOBJ) {
3773 handle_stobj (cfg, bblock, ins, sp [1], ip,
3774 mono_class_from_mono_type (field->type), FALSE, FALSE);
3776 MONO_ADD_INS (bblock, store);
3779 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3780 /* fixme: we need to inline that call somehow */
3781 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3782 MonoInst *iargs [4];
3786 NEW_CLASSCONST (cfg, iargs [1], klass);
3787 NEW_FIELDCONST (cfg, iargs [2], field);
3788 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3789 if (cfg->opt & MONO_OPT_INLINE) {
3790 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3791 iargs, ip, real_offset, dont_inline, &ebblock);
3792 g_assert (costs > 0);
3797 GET_BBLOCK (cfg, bbhash, bblock, ip);
3798 ebblock->next_bb = bblock;
3799 link_bblock (cfg, ebblock, bblock);
3801 temp = iargs [0]->inst_i0->inst_c0;
3803 if (*ip == CEE_LDFLDA) {
3804 /* not sure howto handle this */
3805 NEW_TEMPLOADA (cfg, *sp, temp);
3807 NEW_TEMPLOAD (cfg, *sp, temp);
3811 /* indicates start of a new block, and triggers a load of
3812 all stack arguments at bb boundarie */
3815 inline_costs += costs;
3818 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3819 if (*ip == CEE_LDFLDA) {
3820 /* not sure howto handle this */
3821 NEW_TEMPLOADA (cfg, *sp, temp);
3823 NEW_TEMPLOAD (cfg, *sp, temp);
3828 NEW_ICONST (cfg, offset_ins, foffset);
3829 MONO_INST_NEW (cfg, ins, CEE_ADD);
3831 ins->inst_left = *sp;
3832 ins->inst_right = offset_ins;
3833 ins->type = STACK_MP;
3835 if (*ip == CEE_LDFLDA) {
3839 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3840 type_to_eval_stack_type (field->type, load);
3841 load->cil_code = ip;
3842 load->inst_left = ins;
3843 load->flags |= ins_flag;
3855 MonoClassField *field;
3857 token = read32 (ip + 1);
3859 field = mono_field_from_token (image, token, &klass);
3860 mono_class_init (klass);
3862 handle_loaded_temps (cfg, bblock, stack_start, sp);
3864 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3866 MonoInst *iargs [2];
3867 g_assert (field->parent);
3868 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3869 NEW_FIELDCONST (cfg, iargs [1], field);
3870 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3871 NEW_TEMPLOAD (cfg, ins, temp);
3875 vtable = mono_class_vtable (cfg->domain, klass);
3876 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
3877 if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) {
3878 MonoInst *iargs [1];
3879 NEW_PCONST (cfg, iargs [0], vtable);
3880 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
3881 if (cfg->verbose_level > 2)
3882 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
3884 mono_runtime_class_init (vtable);
3886 addr = (char*)vtable->data + field->offset;
3887 NEW_PCONST (cfg, ins, addr);
3891 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
3892 * This could be later optimized to do just a couple of
3893 * memory dereferences with constant offsets.
3896 MonoInst *iargs [1];
3897 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
3898 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
3899 NEW_TEMPLOAD (cfg, ins, temp);
3903 /* FIXME: mark instructions for use in SSA */
3904 if (*ip == CEE_LDSFLDA) {
3906 } else if (*ip == CEE_STSFLD) {
3910 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3911 store->cil_code = ip;
3912 store->inst_left = ins;
3913 store->inst_right = sp [0];
3914 store->flags |= ins_flag;
3917 if (store->opcode == CEE_STOBJ) {
3918 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
3920 MONO_ADD_INS (bblock, store);
3922 gboolean is_const = FALSE;
3923 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
3924 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
3925 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
3926 gpointer addr = (char*)vtable->data + field->offset;
3927 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
3929 switch (field->type->type) {
3930 case MONO_TYPE_BOOLEAN:
3932 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
3936 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
3939 case MONO_TYPE_CHAR:
3941 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
3945 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
3950 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
3954 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
3959 case MONO_TYPE_STRING:
3960 case MONO_TYPE_OBJECT:
3961 case MONO_TYPE_CLASS:
3962 case MONO_TYPE_SZARRAY:
3964 case MONO_TYPE_FNPTR:
3965 case MONO_TYPE_ARRAY:
3966 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
3967 type_to_eval_stack_type (field->type, *sp);
3972 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
3973 sp [0]->type = STACK_I8;
3974 sp [0]->inst_l = *((gint64 *)addr);
3979 case MONO_TYPE_VALUETYPE:
3988 CHECK_STACK_OVF (1);
3989 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3990 type_to_eval_stack_type (field->type, load);
3991 load->cil_code = ip;
3992 load->inst_left = ins;
3994 load->flags |= ins_flag;
3996 /* fixme: dont see the problem why this does not work */
3997 //cfg->disable_aot = TRUE;
4006 token = read32 (ip + 1);
4007 if (method->wrapper_type != MONO_WRAPPER_NONE)
4008 klass = mono_method_get_wrapper_data (method, token);
4010 klass = mono_class_get (image, token);
4011 mono_class_init (klass);
4012 if (klass->byval_arg.type == MONO_TYPE_VAR)
4013 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4014 n = mono_type_to_stind (&klass->byval_arg);
4015 if (n == CEE_STOBJ) {
4016 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4018 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4020 MONO_INST_NEW (cfg, store, n);
4021 store->cil_code = ip;
4022 store->inst_left = sp [0];
4023 store->inst_right = sp [1];
4024 store->flags |= ins_flag;
4025 MONO_ADD_INS (bblock, store);
4032 MonoInst *iargs [2];
4033 MonoInst *load, *vtoffset, *add, *val, *vstore;
4038 token = read32 (ip + 1);
4039 if (method->wrapper_type != MONO_WRAPPER_NONE)
4040 klass = mono_method_get_wrapper_data (method, token);
4042 klass = mono_class_get (image, token);
4043 mono_class_init (klass);
4044 if (klass->byval_arg.type == MONO_TYPE_VAR)
4045 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4047 /* much like NEWOBJ */
4048 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
4049 NEW_DOMAINCONST (cfg, iargs [0]);
4050 NEW_CLASSCONST (cfg, iargs [1], klass);
4052 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4054 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4055 NEW_PCONST (cfg, iargs [0], vtable);
4056 if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4057 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4059 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4061 NEW_TEMPLOAD (cfg, load, temp);
4062 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4063 MONO_INST_NEW (cfg, add, CEE_ADD);
4064 add->inst_left = load;
4065 add->inst_right = vtoffset;
4068 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4069 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4070 vstore->cil_code = ip;
4071 vstore->inst_left = add;
4072 vstore->inst_right = val;
4074 if (vstore->opcode == CEE_STOBJ) {
4075 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4077 MONO_ADD_INS (bblock, vstore);
4079 NEW_TEMPLOAD (cfg, load, temp);
4087 MONO_INST_NEW (cfg, ins, *ip);
4091 token = read32 (ip + 1);
4093 /* allocate the domainvar - becaus this is used in decompose_foreach */
4094 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
4095 mono_get_domainvar (cfg);
4097 if (method->wrapper_type != MONO_WRAPPER_NONE)
4098 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4100 klass = mono_class_get (image, token);
4102 mono_class_init (klass);
4103 if (klass->byval_arg.type == MONO_TYPE_VAR)
4104 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4105 ins->inst_newa_class = klass;
4106 ins->inst_newa_len = *sp;
4107 ins->type = STACK_OBJ;
4114 MONO_INST_NEW (cfg, ins, *ip);
4115 ins->cil_code = ip++;
4117 ins->inst_left = *sp;
4118 ins->type = STACK_PTR;
4124 klass = mono_class_get (image, read32 (ip + 1));
4125 mono_class_init (klass);
4126 if (klass->byval_arg.type == MONO_TYPE_VAR)
4127 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4128 NEW_LDELEMA (cfg, ins, sp, klass);
4137 token = read32 (ip + 1);
4138 klass = mono_class_get (image, token);
4139 mono_class_init (klass);
4140 if (klass->byval_arg.type == MONO_TYPE_VAR)
4141 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4142 NEW_LDELEMA (cfg, load, sp, klass);
4143 load->cil_code = ip;
4144 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4146 ins->inst_left = load;
4148 type_to_eval_stack_type (&klass->byval_arg, ins);
4162 case CEE_LDELEM_REF: {
4166 * ldind.x (ldelema (array, index))
4167 * ldelema does the bounds check
4171 klass = array_access_to_klass (*ip);
4172 NEW_LDELEMA (cfg, load, sp, klass);
4173 load->cil_code = ip;
4174 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4176 ins->inst_left = load;
4178 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4188 case CEE_STELEM_R8: {
4192 * stind.x (ldelema (array, index), val)
4193 * ldelema does the bounds check
4197 klass = array_access_to_klass (*ip);
4198 NEW_LDELEMA (cfg, load, sp, klass);
4199 load->cil_code = ip;
4200 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4202 ins->inst_left = load;
4203 ins->inst_right = sp [2];
4205 handle_loaded_temps (cfg, bblock, stack_start, sp);
4206 MONO_ADD_INS (bblock, ins);
4208 cfg->disable_ssa = TRUE;
4215 * stind.x (ldelema (array, index), val)
4216 * ldelema does the bounds check
4220 token = read32 (ip + 1);
4221 klass = mono_class_get (image, token);
4222 mono_class_init (klass);
4223 if (klass->byval_arg.type == MONO_TYPE_VAR)
4224 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4225 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4226 MonoInst *iargs [3];
4227 handle_loaded_temps (cfg, bblock, stack_start, sp);
4233 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4235 NEW_LDELEMA (cfg, load, sp, klass);
4236 load->cil_code = ip;
4237 MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg));
4239 ins->inst_left = load;
4240 ins->inst_right = sp [2];
4241 handle_loaded_temps (cfg, bblock, stack_start, sp);
4242 MONO_ADD_INS (bblock, ins);
4246 cfg->disable_ssa = TRUE;
4249 case CEE_STELEM_REF: {
4250 MonoInst *iargs [3];
4255 handle_loaded_temps (cfg, bblock, stack_start, sp);
4261 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4265 NEW_GROUP (cfg, group, sp [0], sp [1]);
4266 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4268 ins->inst_left = group;
4269 ins->inst_right = sp [2];
4270 MONO_ADD_INS (bblock, ins);
4275 cfg->disable_ssa = TRUE;
4278 case CEE_CKFINITE: {
4279 MonoInst *store, *temp;
4282 /* this instr. can throw exceptions as side effect,
4283 * so we cant eliminate dead code which contains CKFINITE opdodes.
4284 * Spilling to memory makes sure that we always perform
4288 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4290 ins->inst_left = sp [-1];
4291 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4293 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4294 store->cil_code = ip;
4295 MONO_ADD_INS (bblock, store);
4297 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4304 g_error ("opcode 0x%02x not handled", *ip);
4308 MonoClass *handle_class;
4310 CHECK_STACK_OVF (1);
4312 n = read32 (ip + 1);
4314 handle = mono_ldtoken (image, n, &handle_class);
4315 mono_class_init (handle_class);
4317 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4319 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4321 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4323 NEW_IMAGECONST (cfg, iargs [0], image);
4324 NEW_ICONST (cfg, iargs [1], n);
4325 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4326 NEW_TEMPLOAD (cfg, res, temp);
4327 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4328 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4329 MONO_ADD_INS (bblock, store);
4330 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4332 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4333 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4334 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4335 MonoClass *tclass = mono_class_from_mono_type (handle);
4336 mono_class_init (tclass);
4337 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4338 ins->type = STACK_OBJ;
4339 ins->klass = cmethod->klass;
4342 NEW_PCONST (cfg, ins, handle);
4343 ins->type = STACK_VTYPE;
4344 ins->klass = handle_class;
4360 case CEE_ADD_OVF_UN:
4362 case CEE_MUL_OVF_UN:
4364 case CEE_SUB_OVF_UN:
4369 case CEE_ENDFINALLY:
4370 /* FIXME: check stack state */
4371 MONO_INST_NEW (cfg, ins, *ip);
4372 MONO_ADD_INS (bblock, ins);
4373 ins->cil_code = ip++;
4374 start_new_bblock = 1;
4379 if (*ip == CEE_LEAVE) {
4380 target = ip + 5 + (gint32)read32(ip + 1);
4382 target = ip + 2 + (signed char)(ip [1]);
4385 /* empty the stack */
4386 while (sp != stack_start) {
4387 MONO_INST_NEW (cfg, ins, CEE_POP);
4391 MONO_ADD_INS (bblock, ins);
4394 /* fixme: call fault handler ? */
4396 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4398 for (tmp = handlers; tmp; tmp = tmp->next) {
4400 link_bblock (cfg, bblock, tblock);
4401 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4403 ins->inst_target_bb = tblock;
4404 MONO_ADD_INS (bblock, ins);
4406 g_list_free (handlers);
4409 MONO_INST_NEW (cfg, ins, CEE_BR);
4411 MONO_ADD_INS (bblock, ins);
4412 GET_BBLOCK (cfg, bbhash, tblock, target);
4413 link_bblock (cfg, bblock, tblock);
4414 CHECK_BBLOCK (target, ip, tblock);
4415 ins->inst_target_bb = tblock;
4416 start_new_bblock = 1;
4418 if (*ip == CEE_LEAVE)
4427 MONO_INST_NEW (cfg, ins, *ip);
4429 handle_loaded_temps (cfg, bblock, stack_start, sp);
4430 MONO_ADD_INS (bblock, ins);
4431 ins->cil_code = ip++;
4432 ins->inst_i0 = sp [0];
4433 ins->inst_i1 = sp [1];
4441 /* trampoline mono specific opcodes */
4442 case MONO_CUSTOM_PREFIX: {
4444 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4448 case CEE_MONO_FUNC1: {
4450 gpointer func = NULL;
4455 case MONO_MARSHAL_CONV_STR_LPWSTR:
4456 func = mono_string_to_utf16;
4458 case MONO_MARSHAL_CONV_LPWSTR_STR:
4459 func = mono_string_from_utf16;
4461 case MONO_MARSHAL_CONV_LPSTR_STR:
4462 func = mono_string_new_wrapper;
4464 case MONO_MARSHAL_CONV_STR_LPTSTR:
4465 case MONO_MARSHAL_CONV_STR_LPSTR:
4466 func = mono_string_to_utf8;
4468 case MONO_MARSHAL_CONV_STR_BSTR:
4469 func = mono_string_to_bstr;
4471 case MONO_MARSHAL_CONV_STR_TBSTR:
4472 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4473 func = mono_string_to_ansibstr;
4475 case MONO_MARSHAL_CONV_SB_LPSTR:
4476 func = mono_string_builder_to_utf8;
4478 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4479 func = mono_array_to_savearray;
4481 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4482 func = mono_array_to_lparray;
4484 case MONO_MARSHAL_CONV_DEL_FTN:
4485 func = mono_delegate_to_ftnptr;
4487 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4488 func = mono_marshal_string_array;
4491 g_warning ("unknown conversion %d\n", ip [2]);
4492 g_assert_not_reached ();
4495 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4496 NEW_TEMPLOAD (cfg, *sp, temp);
4500 inline_costs += 10 * num_calls++;
4503 case CEE_MONO_PROC2: {
4504 gpointer func = NULL;
4509 case MONO_MARSHAL_CONV_LPSTR_SB:
4510 func = mono_string_utf8_to_builder;
4512 case MONO_MARSHAL_FREE_ARRAY:
4513 func = mono_marshal_free_array;
4516 g_assert_not_reached ();
4519 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4521 inline_costs += 10 * num_calls++;
4524 case CEE_MONO_PROC3: {
4525 gpointer func = NULL;
4530 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4531 func = mono_string_to_byvalstr;
4533 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4534 func = mono_string_to_byvalwstr;
4537 g_assert_not_reached ();
4540 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4542 inline_costs += 10 * num_calls++;
4548 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4550 inline_costs += 10 * num_calls++;
4552 case CEE_MONO_LDPTR:
4553 CHECK_STACK_OVF (1);
4554 token = read32 (ip + 2);
4555 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4559 inline_costs += 10 * num_calls++;
4561 case CEE_MONO_VTADDR:
4564 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4566 ins->type = STACK_MP;
4567 ins->inst_left = *sp;
4571 case CEE_MONO_NEWOBJ: {
4572 MonoInst *iargs [2];
4574 CHECK_STACK_OVF (1);
4575 token = read32 (ip + 2);
4576 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4577 mono_class_init (klass);
4578 NEW_DOMAINCONST (cfg, iargs [0]);
4579 NEW_CLASSCONST (cfg, iargs [1], klass);
4580 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4581 NEW_TEMPLOAD (cfg, *sp, temp);
4584 inline_costs += 10 * num_calls++;
4587 case CEE_MONO_OBJADDR:
4590 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4592 ins->type = STACK_MP;
4593 ins->inst_left = *sp;
4597 case CEE_MONO_LDNATIVEOBJ:
4599 token = read32 (ip + 2);
4600 klass = mono_method_get_wrapper_data (method, token);
4601 g_assert (klass->valuetype);
4602 mono_class_init (klass);
4603 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4607 case CEE_MONO_RETOBJ:
4608 g_assert (cfg->ret);
4609 g_assert (method->signature->pinvoke);
4613 token = read32 (ip + 2);
4614 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4616 NEW_RETLOADA (cfg, ins);
4617 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4619 if (sp != stack_start)
4622 MONO_INST_NEW (cfg, ins, CEE_BR);
4624 ins->inst_target_bb = end_bblock;
4625 MONO_ADD_INS (bblock, ins);
4626 link_bblock (cfg, bblock, end_bblock);
4627 start_new_bblock = 1;
4631 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4639 /* somewhat similar to LDTOKEN */
4640 MonoInst *addr, *vtvar;
4641 CHECK_STACK_OVF (1);
4642 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4644 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4645 addr->cil_code = ip;
4646 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4648 ins->inst_left = addr;
4649 MONO_ADD_INS (bblock, ins);
4650 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4663 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4664 MONO_INST_NEW (cfg, ins, cmp->opcode);
4666 cmp->inst_i0 = sp [0];
4667 cmp->inst_i1 = sp [1];
4671 cmp->opcode = OP_COMPARE;
4673 ins->type = STACK_I4;
4683 CHECK_STACK_OVF (1);
4684 n = read32 (ip + 2);
4685 if (method->wrapper_type != MONO_WRAPPER_NONE)
4686 cmethod = mono_method_get_wrapper_data (method, n);
4688 cmethod = mono_get_method (image, n, NULL);
4691 * We can't do this in mono_ldftn, since it is used in
4692 * the synchronized wrapper, leading to an infinite loop.
4694 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4695 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4698 mono_class_init (cmethod->klass);
4699 handle_loaded_temps (cfg, bblock, stack_start, sp);
4701 NEW_METHODCONST (cfg, argconst, cmethod);
4702 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4703 NEW_TEMPLOAD (cfg, *sp, temp);
4707 inline_costs += 10 * num_calls++;
4710 case CEE_LDVIRTFTN: {
4715 n = read32 (ip + 2);
4716 if (method->wrapper_type != MONO_WRAPPER_NONE)
4717 cmethod = mono_method_get_wrapper_data (method, n);
4719 cmethod = mono_get_method (image, n, NULL);
4721 mono_class_init (cmethod->klass);
4722 handle_loaded_temps (cfg, bblock, stack_start, sp);
4726 NEW_METHODCONST (cfg, args [1], cmethod);
4727 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4728 NEW_TEMPLOAD (cfg, *sp, temp);
4732 inline_costs += 10 * num_calls++;
4736 CHECK_STACK_OVF (1);
4737 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4743 CHECK_STACK_OVF (1);
4744 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4752 handle_loaded_temps (cfg, bblock, stack_start, sp);
4753 n = read16 (ip + 2);
4754 NEW_ARGSTORE (cfg, ins, n, *sp);
4756 if (ins->opcode == CEE_STOBJ) {
4757 NEW_ARGLOADA (cfg, ins, n);
4758 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4760 MONO_ADD_INS (bblock, ins);
4764 CHECK_STACK_OVF (1);
4765 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4771 CHECK_STACK_OVF (1);
4772 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4780 n = read16 (ip + 2);
4781 handle_loaded_temps (cfg, bblock, stack_start, sp);
4782 NEW_LOCSTORE (cfg, ins, n, *sp);
4784 if (ins->opcode == CEE_STOBJ) {
4785 NEW_LOCLOADA (cfg, ins, n);
4786 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4788 MONO_ADD_INS (bblock, ins);
4795 if (sp != stack_start)
4797 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4798 ins->inst_left = *sp;
4801 if (header->init_locals)
4802 ins->flags |= MONO_INST_INIT;
4806 /* FIXME: set init flag if locals init is set in this method */
4808 case CEE_ENDFILTER: {
4809 MonoExceptionClause *clause, *nearest;
4810 int cc, nearest_num;
4814 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4816 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4817 ins->inst_left = *sp;
4819 MONO_ADD_INS (bblock, ins);
4820 start_new_bblock = 1;
4824 for (cc = 0; cc < header->num_clauses; ++cc) {
4825 clause = &header->clauses [cc];
4826 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4827 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4833 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4837 case CEE_UNALIGNED_:
4838 ins_flag |= MONO_INST_UNALIGNED;
4842 ins_flag |= MONO_INST_VOLATILE;
4846 ins_flag |= MONO_INST_TAILCALL;
4852 token = read32 (ip + 2);
4853 if (method->wrapper_type != MONO_WRAPPER_NONE)
4854 klass = mono_method_get_wrapper_data (method, token);
4856 klass = mono_class_get (image, token);
4857 if (klass->byval_arg.type == MONO_TYPE_VAR)
4858 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4859 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
4863 case CEE_CONSTRAINED_:
4864 /* FIXME: implement */
4865 token = read32 (ip + 2);
4870 MonoInst *iargs [3];
4876 handle_loaded_temps (cfg, bblock, stack_start, sp);
4877 if (ip [1] == CEE_CPBLK) {
4878 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
4880 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
4888 ins_flag |= MONO_INST_NOTYPECHECK;
4890 ins_flag |= MONO_INST_NORANGECHECK;
4891 /* we ignore the no-nullcheck for now since we
4892 * really do it explicitly only when doing callvirt->call
4898 /* FIXME: check we are in a catch handler */
4899 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
4900 load->cil_code = ip;
4901 MONO_INST_NEW (cfg, ins, CEE_THROW);
4902 ins->inst_left = load;
4904 MONO_ADD_INS (bblock, ins);
4906 start_new_bblock = 1;
4911 CHECK_STACK_OVF (1);
4912 token = read32 (ip + 2);
4913 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
4914 MonoType *type = mono_type_create_from_typespec (image, token);
4915 token = mono_type_size (type, &align);
4916 mono_metadata_free_type (type);
4918 MonoClass *szclass = mono_class_get (image, token);
4919 mono_class_init (szclass);
4920 token = mono_class_value_size (szclass, &align);
4922 NEW_ICONST (cfg, ins, token);
4927 case CEE_REFANYTYPE:
4928 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4934 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
4939 g_error ("opcode 0x%02x not handled", *ip);
4942 if (start_new_bblock != 1)
4945 bblock->cil_length = ip - bblock->cil_code;
4946 bblock->next_bb = end_bblock;
4947 link_bblock (cfg, bblock, end_bblock);
4949 if (cfg->method == method && cfg->domainvar) {
4953 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
4954 call->signature = helper_sig_domain_get;
4955 call->inst.type = STACK_PTR;
4956 call->fptr = mono_domain_get;
4957 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
4959 MONO_ADD_INS (init_localsbb, store);
4962 if (header->init_locals) {
4964 for (i = 0; i < header->num_locals; ++i) {
4965 int t = header->locals [i]->type;
4966 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
4967 t = header->locals [i]->data.klass->enum_basetype->type;
4968 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
4969 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
4970 NEW_ICONST (cfg, ins, 0);
4971 NEW_LOCSTORE (cfg, store, i, ins);
4972 MONO_ADD_INS (init_localsbb, store);
4973 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
4974 MONO_INST_NEW (cfg, ins, OP_I8CONST);
4975 ins->type = STACK_I8;
4977 NEW_LOCSTORE (cfg, store, i, ins);
4978 MONO_ADD_INS (init_localsbb, store);
4979 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
4980 MONO_INST_NEW (cfg, ins, OP_R8CONST);
4981 ins->type = STACK_R8;
4982 ins->inst_p0 = (void*)&r8_0;
4983 NEW_LOCSTORE (cfg, store, i, ins);
4984 MONO_ADD_INS (init_localsbb, store);
4985 } else if (t == MONO_TYPE_VALUETYPE) {
4986 NEW_LOCLOADA (cfg, ins, i);
4987 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
4990 NEW_PCONST (cfg, ins, NULL);
4991 NEW_LOCSTORE (cfg, store, i, ins);
4992 MONO_ADD_INS (init_localsbb, store);
4998 /* resolve backward branches in the middle of an existing basic block */
4999 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5001 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5002 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5003 if (tblock != start_bblock) {
5005 split_bblock (cfg, tblock, bblock);
5006 l = bblock->cil_code - header->code;
5007 bblock->cil_length = tblock->cil_length - l;
5008 tblock->cil_length = l;
5010 g_print ("recheck failed.\n");
5014 /* we compute regions here, because the length of filter clauses is not known in advance.
5015 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
5016 if (cfg->method == method) {
5018 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5019 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5020 if (cfg->verbose_level > 2)
5021 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5024 g_hash_table_destroy (bbhash);
5027 dont_inline = g_list_remove (dont_inline, method);
5028 return inline_costs;
5031 if (cfg->method != method)
5032 g_hash_table_destroy (bbhash);
5033 dont_inline = g_list_remove (dont_inline, method);
5037 if (cfg->method != method)
5038 g_hash_table_destroy (bbhash);
5039 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5040 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5041 dont_inline = g_list_remove (dont_inline, method);
5046 mono_print_tree (MonoInst *tree) {
5052 arity = mono_burg_arity [tree->opcode];
5054 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5056 switch (tree->opcode) {
5058 printf ("[%d]", tree->inst_c0);
5061 printf ("[%lld]", tree->inst_l);
5064 printf ("[%f]", *(double*)tree->inst_p0);
5067 printf ("[%f]", *(float*)tree->inst_p0);
5071 printf ("[%d]", tree->inst_c0);
5074 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5077 printf ("[%s]", mono_arch_regname (tree->dreg));
5080 printf ("[%s]", tree->inst_newa_class->name);
5081 mono_print_tree (tree->inst_newa_len);
5092 case OP_VOIDCALLVIRT: {
5093 MonoCallInst *call = (MonoCallInst*)tree;
5095 printf ("[%s]", call->method->name);
5100 printf ("[%d (", tree->inst_c0);
5101 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5104 printf ("%d", tree->inst_phi_args [i + 1]);
5116 printf ("[B%d]", tree->inst_target_bb->block_num);
5126 case OP_VOIDCALL_REG:
5127 mono_print_tree (tree->inst_left);
5139 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5140 mono_print_tree (tree->inst_left);
5144 mono_print_tree (tree->inst_left);
5146 mono_print_tree (tree->inst_right);
5156 create_helper_signature (void)
5158 /* FIXME: set call conv */
5159 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5160 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5161 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5162 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5163 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5164 helper_sig_newarr->pinvoke = 1;
5166 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5167 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5168 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5169 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5170 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5171 helper_sig_newarr_specific->pinvoke = 1;
5173 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5174 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5175 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5176 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5177 helper_sig_object_new->pinvoke = 1;
5179 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5180 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5181 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5182 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5183 helper_sig_object_new_specific->pinvoke = 1;
5185 /* void* mono_method_compile (MonoMethod*) */
5186 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5187 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5188 helper_sig_compile->pinvoke = 1;
5190 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5191 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5192 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5193 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5194 helper_sig_compile_virt->pinvoke = 1;
5196 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5197 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5198 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5199 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5200 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5201 helper_sig_ldstr->pinvoke = 1;
5203 /* MonoDomain *mono_domain_get (void) */
5204 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5205 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5206 helper_sig_domain_get->pinvoke = 1;
5208 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5209 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5210 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5211 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5212 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5213 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5214 helper_sig_stelem_ref->pinvoke = 1;
5216 /* long amethod (long, long) */
5217 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5218 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5219 &mono_defaults.int64_class->byval_arg;
5220 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5221 helper_sig_long_long_long->pinvoke = 1;
5223 /* object amethod (intptr) */
5224 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5225 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5226 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5227 helper_sig_obj_ptr->pinvoke = 1;
5229 /* void amethod (intptr) */
5230 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5231 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5232 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5233 helper_sig_void_ptr->pinvoke = 1;
5235 /* void amethod (MonoObject *obj) */
5236 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5237 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5238 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5239 helper_sig_void_obj->pinvoke = 1;
5241 /* intptr amethod (void) */
5242 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5243 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5244 helper_sig_ptr_void->pinvoke = 1;
5246 /* void amethod (intptr, intptr) */
5247 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5248 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5249 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5250 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5251 helper_sig_void_ptr_ptr->pinvoke = 1;
5253 /* void amethod (intptr, intptr, intptr) */
5254 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5255 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5256 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5257 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5258 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5259 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5261 /* intptr amethod (intptr, intptr) */
5262 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5263 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5264 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5265 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5266 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5268 /* IntPtr amethod (object) */
5269 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5270 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5271 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5272 helper_sig_ptr_obj->pinvoke = 1;
5274 /* IntPtr amethod (int) */
5275 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5276 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5277 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5278 helper_sig_ptr_int->pinvoke = 1;
5280 /* long amethod (long, guint32) */
5281 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5282 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5283 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5284 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5285 helper_sig_long_long_int->pinvoke = 1;
5287 /* ulong amethod (double) */
5288 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5289 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5290 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5291 helper_sig_ulong_double->pinvoke = 1;
5293 /* long amethod (double) */
5294 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5295 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5296 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5297 helper_sig_long_double->pinvoke = 1;
5299 /* uint amethod (double) */
5300 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5301 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5302 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5303 helper_sig_uint_double->pinvoke = 1;
5305 /* int amethod (double) */
5306 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5307 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5308 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5309 helper_sig_int_double->pinvoke = 1;
5311 /* void initobj (intptr, int size) */
5312 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5313 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5314 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5315 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5316 helper_sig_initobj->pinvoke = 1;
5318 /* void memcpy (intptr, intptr, int size) */
5319 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5320 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5321 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5322 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5323 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5324 helper_sig_memcpy->pinvoke = 1;
5326 /* void memset (intptr, int val, int size) */
5327 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5328 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5329 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5330 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5331 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5332 helper_sig_memset->pinvoke = 1;
5335 static GHashTable *jit_icall_hash_name = NULL;
5336 static GHashTable *jit_icall_hash_addr = NULL;
5339 mono_find_jit_icall_by_name (const char *name)
5341 g_assert (jit_icall_hash_name);
5343 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5344 return g_hash_table_lookup (jit_icall_hash_name, name);
5348 mono_find_jit_icall_by_addr (gconstpointer addr)
5350 g_assert (jit_icall_hash_addr);
5352 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5356 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5359 MonoMethod *wrapper;
5361 if (callinfo->wrapper)
5362 return callinfo->wrapper;
5363 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5364 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5365 callinfo->wrapper = mono_jit_compile_method (wrapper);
5367 return callinfo->wrapper;
5371 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5373 MonoJitICallInfo *info;
5378 if (!jit_icall_hash_name) {
5379 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5380 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5383 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5384 g_warning ("jit icall already defined \"%s\"\n", name);
5385 g_assert_not_reached ();
5388 info = g_new (MonoJitICallInfo, 1);
5395 #ifdef MONO_USE_EXC_TABLES
5396 || mono_arch_has_unwind_info (func)
5399 info->wrapper = func;
5401 info->wrapper = NULL;
5402 mono_icall_get_wrapper (info);
5405 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5406 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5407 if (func != info->wrapper)
5408 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5413 static GHashTable *emul_opcode_hash = NULL;
5415 static MonoJitICallInfo *
5416 mono_find_jit_opcode_emulation (int opcode)
5418 if (emul_opcode_hash)
5419 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5425 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
5427 MonoJitICallInfo *info;
5429 if (!emul_opcode_hash)
5430 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5432 g_assert (!sig->hasthis);
5433 g_assert (sig->param_count < 3);
5435 info = mono_register_jit_icall (func, name, sig, no_throw);
5437 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5441 decompose_foreach (MonoInst *tree, gpointer data)
5443 static MonoJitICallInfo *newarr_info = NULL;
5444 static MonoJitICallInfo *newarr_specific_info = NULL;
5445 MonoJitICallInfo *info;
5447 switch (tree->opcode) {
5449 MonoCompile *cfg = data;
5450 MonoInst *iargs [3];
5453 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5454 g_assert (newarr_info);
5455 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5456 g_assert (newarr_specific_info);
5459 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5460 NEW_DOMAINCONST (cfg, iargs [0]);
5461 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5462 iargs [2] = tree->inst_newa_len;
5467 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
5469 NEW_PCONST (cfg, iargs [0], vtable);
5470 iargs [1] = tree->inst_newa_len;
5472 info = newarr_specific_info;
5475 mono_emulate_opcode (cfg, tree, iargs, info);
5485 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5487 switch (mono_burg_arity [tree->opcode]) {
5490 mono_inst_foreach (tree->inst_left, func, data);
5493 mono_inst_foreach (tree->inst_left, func, data);
5494 mono_inst_foreach (tree->inst_right, func, data);
5497 g_assert_not_reached ();
5504 mono_print_bb_code (MonoBasicBlock *bb) {
5506 MonoInst *c = bb->code;
5508 mono_print_tree (c);
5517 print_dfn (MonoCompile *cfg) {
5522 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5524 for (i = 0; i < cfg->num_bblocks; ++i) {
5525 bb = cfg->bblocks [i];
5527 char* code1, *code2;
5528 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5529 if (bb->last_ins->cil_code)
5530 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5532 code2 = g_strdup ("");
5534 code1 [strlen (code1) - 1] = 0;
5535 code = g_strdup_printf ("%s -> %s", code1, code2);
5539 code = g_strdup ("\n");
5540 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5542 MonoInst *c = bb->code;
5544 mono_print_tree (c);
5552 g_print ("\tprev:");
5553 for (j = 0; j < bb->in_count; ++j) {
5554 g_print (" BB%d", bb->in_bb [j]->block_num);
5556 g_print ("\t\tsucc:");
5557 for (j = 0; j < bb->out_count; ++j) {
5558 g_print (" BB%d", bb->out_bb [j]->block_num);
5560 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5563 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5566 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5568 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5576 * returns the offset used by spillvar. It allocates a new
5577 * spill variable if necessary.
5580 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5582 MonoSpillInfo **si, *info;
5585 si = &cfg->spill_info;
5587 while (i <= spillvar) {
5590 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5592 cfg->stack_offset -= sizeof (gpointer);
5593 info->offset = cfg->stack_offset;
5597 return (*si)->offset;
5603 g_assert_not_reached ();
5608 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5612 g_assert (bb->code);
5613 bb->last_ins->next = inst;
5614 bb->last_ins = inst;
5616 bb->last_ins = bb->code = inst;
5621 mono_destroy_compile (MonoCompile *cfg)
5623 //mono_mempool_stats (cfg->mempool);
5624 g_hash_table_destroy (cfg->bb_hash);
5626 mono_regstate_free (cfg->rs);
5627 mono_mempool_destroy (cfg->mempool);
5628 g_list_free (cfg->ldstr_list);
5630 g_free (cfg->varinfo);
5636 mono_get_lmf_addr (void)
5638 MonoJitTlsData *jit_tls;
5640 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5641 return &jit_tls->lmf;
5643 g_assert_not_reached ();
5648 * mono_thread_abort:
5649 * @obj: exception object
5651 * abort the thread, print exception information and stack trace
5654 mono_thread_abort (MonoObject *obj)
5656 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5664 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5666 MonoJitTlsData *jit_tls;
5669 jit_tls = g_new0 (MonoJitTlsData, 1);
5671 TlsSetValue (mono_jit_tls_id, jit_tls);
5673 jit_tls->abort_func = mono_thread_abort;
5674 jit_tls->end_of_stack = stack_start;
5676 lmf = g_new0 (MonoLMF, 1);
5682 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5685 mono_thread_abort_dummy (MonoObject *obj)
5687 if (mono_thread_attach_aborted_cb)
5688 mono_thread_attach_aborted_cb (obj);
5690 mono_thread_abort (obj);
5694 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5696 MonoJitTlsData *jit_tls;
5699 jit_tls = g_new0 (MonoJitTlsData, 1);
5701 TlsSetValue (mono_jit_tls_id, jit_tls);
5703 jit_tls->abort_func = mono_thread_abort_dummy;
5704 jit_tls->end_of_stack = stack_start;
5706 lmf = g_new0 (MonoLMF, 1);
5713 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5715 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5719 ji->data.target = target;
5720 ji->next = cfg->patch_info;
5722 cfg->patch_info = ji;
5726 mono_remove_patch_info (MonoCompile *cfg, int ip)
5728 MonoJumpInfo **ji = &cfg->patch_info;
5731 if ((*ji)->ip.i == ip)
5734 ji = &((*ji)->next);
5739 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5740 MonoJitICallInfo *info;
5742 switch (mono_burg_arity [tree->opcode]) {
5745 dec_foreach (tree->inst_left, cfg);
5747 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5748 MonoInst *iargs [2];
5750 iargs [0] = tree->inst_left;
5752 mono_emulate_opcode (cfg, tree, iargs, info);
5758 if (tree->opcode == OP_LMUL
5759 && (cfg->opt & MONO_OPT_INTRINS)
5760 && (tree->inst_left->opcode == CEE_CONV_I8
5761 || tree->inst_left->opcode == CEE_CONV_U8)
5762 && tree->inst_left->inst_left->type == STACK_I4
5763 && (tree->inst_right->opcode == CEE_CONV_I8
5764 || tree->inst_right->opcode == CEE_CONV_U8)
5765 && tree->inst_right->inst_left->type == STACK_I4) {
5766 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
5767 tree->inst_left = tree->inst_left->inst_left;
5768 tree->inst_right = tree->inst_right->inst_left;
5769 dec_foreach (tree, cfg);
5770 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5771 MonoInst *iargs [2];
5773 iargs [0] = tree->inst_i0;
5774 iargs [1] = tree->inst_i1;
5776 mono_emulate_opcode (cfg, tree, iargs, info);
5778 dec_foreach (iargs [0], cfg);
5779 dec_foreach (iargs [1], cfg);
5782 dec_foreach (tree->inst_left, cfg);
5783 dec_foreach (tree->inst_right, cfg);
5787 g_assert_not_reached ();
5789 decompose_foreach (tree, cfg);
5793 decompose_pass (MonoCompile *cfg) {
5796 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5799 cfg->prev_ins = NULL;
5800 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5801 dec_foreach (tree, cfg);
5802 cfg->prev_ins = tree;
5808 nullify_basic_block (MonoBasicBlock *bb)
5815 bb->code = bb->last_ins = NULL;
5816 bb->cil_code = NULL;
5820 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5824 for (i = 0; i < bb->out_count; i++) {
5825 MonoBasicBlock *ob = bb->out_bb [i];
5828 if (bb->out_count > 1) {
5829 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
5833 bb->out_bb [i] = repl;
5840 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5844 for (i = 0; i < bb->in_count; i++) {
5845 MonoBasicBlock *ib = bb->in_bb [i];
5848 if (bb->in_count > 1) {
5849 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
5853 bb->in_bb [i] = repl;
5860 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5864 for (i = 0; i < bb->out_count; i++) {
5865 MonoBasicBlock *ob = bb->out_bb [i];
5866 for (j = 0; j < ob->in_count; j++) {
5867 if (ob->in_bb [j] == orig) {
5868 ob->in_bb [j] = repl;
5877 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
5879 bb->out_count = bbn->out_count;
5880 bb->out_bb = bbn->out_bb;
5882 replace_basic_block (bb, bbn, bb);
5886 bb->last_ins->next = bbn->code;
5887 bb->last_ins = bbn->last_ins;
5890 bb->code = bbn->code;
5891 bb->last_ins = bbn->last_ins;
5893 bb->next_bb = bbn->next_bb;
5894 nullify_basic_block (bbn);
5898 optimize_branches (MonoCompile *cfg) {
5899 int i, changed = FALSE;
5900 MonoBasicBlock *bb, *bbn;
5905 /* we skip the entry block (exit is handled specially instead ) */
5906 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5908 /* dont touch code inside exception clauses */
5909 if (bb->region != -1)
5912 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5913 if (cfg->verbose_level > 2)
5914 g_print ("nullify block triggered %d\n", bbn->block_num);
5916 bb->next_bb = bbn->next_bb;
5918 for (i = 0; i < bbn->out_count; i++)
5919 replace_in_block (bbn->out_bb [i], bbn, NULL);
5921 nullify_basic_block (bbn);
5925 if (bb->out_count == 1) {
5926 bbn = bb->out_bb [0];
5928 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
5929 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
5930 bb->last_ins->opcode = CEE_BR;
5931 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
5933 if (cfg->verbose_level > 2)
5934 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
5937 if (bb->region == bbn->region && bb->next_bb == bbn) {
5938 /* the block are in sequence anyway ... */
5940 /* branches to the following block can be removed */
5941 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5942 bb->last_ins->opcode = CEE_NOP;
5944 if (cfg->verbose_level > 2)
5945 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
5948 if (bbn->in_count == 1) {
5950 if (bbn != cfg->bb_exit) {
5951 if (cfg->verbose_level > 2)
5952 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
5953 merge_basic_blocks (bb, bbn);
5957 //mono_print_bb_code (bb);
5967 /* we skip the entry block (exit is handled specially instead ) */
5968 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
5970 /* dont touch code inside exception clauses */
5971 if (bb->region != -1)
5974 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
5975 if (cfg->verbose_level > 2) {
5976 g_print ("nullify block triggered %d\n", bbn->block_num);
5978 bb->next_bb = bbn->next_bb;
5980 for (i = 0; i < bbn->out_count; i++)
5981 replace_in_block (bbn->out_bb [i], bbn, NULL);
5983 nullify_basic_block (bbn);
5989 if (bb->out_count == 1) {
5990 bbn = bb->out_bb [0];
5992 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
5993 bbn = bb->last_ins->inst_target_bb;
5994 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
5995 bbn->code->inst_target_bb->region == bb->region) {
5997 if (cfg->verbose_level > 2)
5998 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
5999 bb->block_num, bbn->block_num);
6001 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
6002 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
6007 } else if (bb->out_count == 2) {
6008 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6009 bbn = bb->last_ins->inst_true_bb;
6010 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6011 bbn->code->inst_target_bb->region == bb->region) {
6012 if (cfg->verbose_level > 2)
6013 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
6014 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6017 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
6019 replace_in_block (bbn, bb, NULL);
6021 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6022 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6024 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6030 bbn = bb->last_ins->inst_false_bb;
6031 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6032 bbn->code->inst_target_bb->region == bb->region) {
6033 if (cfg->verbose_level > 2)
6034 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
6035 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6038 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
6040 replace_in_block (bbn, bb, NULL);
6042 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6043 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6045 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6058 mono_compile_create_vars (MonoCompile *cfg)
6060 MonoMethodSignature *sig;
6061 MonoMethodHeader *header;
6064 header = ((MonoMethodNormal *)cfg->method)->header;
6066 sig = cfg->method->signature;
6068 if (!MONO_TYPE_IS_VOID (sig->ret)) {
6069 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6070 cfg->ret->opcode = OP_RETARG;
6071 cfg->ret->inst_vtype = sig->ret;
6072 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
6074 if (cfg->verbose_level > 2)
6075 g_print ("creating vars\n");
6078 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
6080 for (i = 0; i < sig->param_count; ++i)
6081 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
6083 cfg->locals_start = cfg->num_varinfo;
6085 if (cfg->verbose_level > 2)
6086 g_print ("creating locals\n");
6087 for (i = 0; i < header->num_locals; ++i)
6088 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
6089 if (cfg->verbose_level > 2)
6090 g_print ("locals done\n");
6095 mono_print_code (MonoCompile *cfg)
6099 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6100 MonoInst *tree = bb->code;
6105 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
6107 for (; tree; tree = tree->next) {
6108 mono_print_tree (tree);
6113 bb->last_ins->next = NULL;
6118 extern const char * const mono_burg_rule_string [];
6121 emit_state (MonoCompile *cfg, MBState *state, int goal)
6124 int ern = mono_burg_rule (state, goal);
6125 const guint16 *nts = mono_burg_nts [ern];
6128 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
6132 // state->reg1 = state->reg2; /* chain rule */
6134 state->reg1 = mono_regstate_next_int (cfg->rs);
6135 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
6138 state->reg1 = mono_regstate_next_int (cfg->rs);
6139 state->reg2 = mono_regstate_next_int (cfg->rs);
6142 state->reg1 = mono_regstate_next_float (cfg->rs);
6149 mono_burg_kids (state, ern, kids);
6151 emit_state (cfg, kids [0], nts [0]);
6153 emit_state (cfg, kids [1], nts [1]);
6155 g_assert (!nts [3]);
6156 emit_state (cfg, kids [2], nts [2]);
6161 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6162 if ((emit = mono_burg_func [ern]))
6163 emit (state, state->tree, cfg);
6166 #define DEBUG_SELECTION
6169 mini_select_instructions (MonoCompile *cfg)
6171 static int reverse_map [] = {
6172 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6173 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6175 static int reverse_fmap [] = {
6176 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6177 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6179 static int reverse_lmap [] = {
6180 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6181 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6186 cfg->state_pool = mono_mempool_new ();
6187 cfg->rs = mono_regstate_new ();
6189 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6190 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6191 bb->next_bb != bb->last_ins->inst_false_bb) {
6193 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6194 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6195 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6196 bb->last_ins->inst_false_bb = tmp;
6198 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6199 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6200 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6201 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6202 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6203 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6206 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6207 inst->opcode = CEE_BR;
6208 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6209 mono_bblock_add_inst (bb, inst);
6214 #ifdef DEBUG_SELECTION
6215 if (cfg->verbose_level >= 4) {
6216 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6217 MonoInst *tree = bb->code;
6218 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6221 for (; tree; tree = tree->next) {
6222 mono_print_tree (tree);
6229 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6230 MonoInst *tree = bb->code, *next;
6236 bb->last_ins = NULL;
6239 mono_regstate_reset (cfg->rs);
6241 #ifdef DEBUG_SELECTION
6242 if (cfg->verbose_level >= 3)
6243 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6245 for (; tree; tree = next) {
6247 #ifdef DEBUG_SELECTION
6248 if (cfg->verbose_level >= 3) {
6249 mono_print_tree (tree);
6254 if (!(mbstate = mono_burg_label (tree, cfg))) {
6255 g_warning ("unabled to label tree %p", tree);
6256 mono_print_tree (tree);
6258 g_assert_not_reached ();
6260 emit_state (cfg, mbstate, MB_NTERM_stmt);
6262 bb->max_ireg = cfg->rs->next_vireg;
6263 bb->max_freg = cfg->rs->next_vfreg;
6266 bb->last_ins->next = NULL;
6268 mono_mempool_empty (cfg->state_pool);
6270 mono_mempool_destroy (cfg->state_pool);
6274 mono_codegen (MonoCompile *cfg)
6276 MonoJumpInfo *patch_info;
6278 int i, max_epilog_size;
6281 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6282 cfg->spill_count = 0;
6283 /* we reuse dfn here */
6284 /* bb->dfn = bb_count++; */
6285 mono_arch_local_regalloc (cfg, bb);
6288 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6289 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6291 code = mono_arch_emit_prolog (cfg);
6293 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6294 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6296 cfg->code_len = code - cfg->native_code;
6297 cfg->prolog_end = cfg->code_len;
6299 mono_debug_open_method (cfg);
6301 /* emit code all basic blocks */
6302 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6303 bb->native_offset = cfg->code_len;
6304 mono_arch_output_basic_block (cfg, bb);
6306 cfg->bb_exit->native_offset = cfg->code_len;
6308 code = cfg->native_code + cfg->code_len;
6310 max_epilog_size = mono_arch_max_epilog_size (cfg);
6312 /* we always allocate code in cfg->domain->code_mp to increase locality */
6313 cfg->code_size = cfg->code_len + max_epilog_size;
6314 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6315 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6316 memcpy (code, cfg->native_code, cfg->code_len);
6317 g_free (cfg->native_code);
6318 cfg->native_code = code;
6319 code = cfg->native_code + cfg->code_len;
6321 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6323 cfg->epilog_begin = cfg->code_len;
6325 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6326 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6328 cfg->code_len = code - cfg->native_code;
6330 mono_arch_emit_epilog (cfg);
6332 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6333 switch (patch_info->type) {
6334 case MONO_PATCH_INFO_ABS: {
6335 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6337 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6338 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6339 patch_info->data.name = info->name;
6343 case MONO_PATCH_INFO_SWITCH: {
6344 gpointer *table = g_new (gpointer, patch_info->table_size);
6345 patch_info->ip.i = patch_info->ip.label->inst_c0;
6346 for (i = 0; i < patch_info->table_size; i++) {
6347 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6349 patch_info->data.target = table;
6358 if (cfg->verbose_level > 1)
6359 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6360 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6362 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6364 mono_debug_close_method (cfg);
6368 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6373 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6374 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6376 if (cp->opcode == OP_ICONST) {
6377 if (cfg->opt & MONO_OPT_CONSPROP) {
6378 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6382 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6383 if (cfg->opt & MONO_OPT_COPYPROP) {
6384 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6390 arity = mono_burg_arity [tree->opcode];
6393 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6394 if (cfg->opt & MONO_OPT_CFOLD)
6395 mono_constant_fold_inst (tree, NULL);
6397 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6398 if (cfg->opt & MONO_OPT_CFOLD)
6399 mono_constant_fold_inst (tree, NULL);
6401 mono_constant_fold_inst (tree, NULL);
6407 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6411 switch (tree->opcode) {
6421 if (tree->ssa_op == MONO_SSA_NOP) {
6422 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6439 case OP_VOIDCALL_REG:
6440 case OP_VOIDCALLVIRT:
6442 MonoCallInst *call = (MonoCallInst *)tree;
6443 MonoMethodSignature *sig = call->signature;
6444 int i, byref = FALSE;
6446 for (i = 0; i < sig->param_count; i++) {
6447 if (sig->params [i]->byref) {
6454 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6462 arity = mono_burg_arity [tree->opcode];
6468 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6471 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6472 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6475 g_assert_not_reached ();
6480 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6482 MonoInst *tree = bb->code;
6488 for (; tree; tree = tree->next) {
6490 mono_cprop_copy_values (cfg, tree, acp);
6492 mono_cprop_invalidate_values (tree, acp, acp_size);
6494 if (tree->ssa_op == MONO_SSA_STORE &&
6495 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6496 MonoInst *i1 = tree->inst_i1;
6498 acp [tree->inst_i0->inst_c0] = NULL;
6500 for (i = 0; i < acp_size; i++) {
6501 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6502 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6507 if (i1->opcode == OP_ICONST) {
6508 acp [tree->inst_i0->inst_c0] = i1;
6509 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6511 if (i1->ssa_op == MONO_SSA_LOAD &&
6512 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6513 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6514 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6515 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6520 if (tree->opcode == CEE_BEQ) {
6521 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6522 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6523 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6525 tree->opcode = CEE_BR;
6526 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6527 tree->inst_target_bb = tree->inst_true_bb;
6529 tree->inst_target_bb = tree->inst_false_bb;
6538 mono_local_cprop (MonoCompile *cfg)
6543 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6545 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6546 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6547 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6552 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6554 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6555 guint8 *ip = (guint8 *)header->code;
6558 int dfn = 0, i, code_size_ratio;
6560 mono_jit_stats.methods_compiled++;
6561 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
6562 mono_profiler_method_jit (method);
6564 cfg = g_new0 (MonoCompile, 1);
6565 cfg->method = method;
6566 cfg->mempool = mono_mempool_new ();
6568 cfg->prof_options = mono_profiler_get_events ();
6569 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6570 cfg->domain = domain;
6571 cfg->verbose_level = mini_verbose;
6572 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6573 ((MonoMethodNormal *)method)->header->max_stack);
6575 if (cfg->verbose_level > 2)
6576 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6579 * create MonoInst* which represents arguments and local variables
6581 mono_compile_create_vars (cfg);
6583 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6584 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6585 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6586 mono_destroy_compile (cfg);
6590 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6591 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6593 if (cfg->num_varinfo > 2000) {
6595 * we disable some optimizations if there are too many variables
6596 * because JIT time may become too expensive. The actual number needs
6597 * to be tweaked and eventually the non-linear algorithms should be fixed.
6599 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
6600 cfg->disable_ssa = TRUE;
6602 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6604 /* Depth-first ordering on basic blocks */
6605 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6607 if (cfg->opt & MONO_OPT_BRANCH)
6608 optimize_branches (cfg);
6610 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6611 if (cfg->num_bblocks != dfn + 1) {
6614 cfg->num_bblocks = dfn + 1;
6616 if (!header->clauses) {
6617 /* remove unreachable code, because the code in them may be
6618 * inconsistent (access to dead variables for example) */
6619 for (bb = cfg->bb_entry; bb;) {
6620 MonoBasicBlock *bbn = bb->next_bb;
6622 if (bbn && bbn->region == -1 && !bbn->dfn) {
6623 if (cfg->verbose_level > 1)
6624 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6625 bb->next_bb = bbn->next_bb;
6626 nullify_basic_block (bbn);
6634 if (cfg->opt & MONO_OPT_LOOP) {
6635 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6636 mono_compute_natural_loops (cfg);
6640 /* after method_to_ir */
6644 //#define DEBUGSSA "logic_run"
6645 #define DEBUGSSA_CLASS "Tests"
6649 if (!header->num_clauses && !cfg->disable_ssa) {
6650 mono_local_cprop (cfg);
6651 mono_ssa_compute (cfg);
6655 /* fixme: add all optimizations which requires SSA */
6656 if (cfg->opt & (MONO_OPT_DEADCE)) {
6657 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6658 mono_local_cprop (cfg);
6659 mono_ssa_compute (cfg);
6661 if (cfg->verbose_level >= 2) {
6668 /* after SSA translation */
6672 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6673 if (cfg->comp_done & MONO_COMP_SSA) {
6674 mono_ssa_cprop (cfg);
6676 mono_local_cprop (cfg);
6680 if (cfg->comp_done & MONO_COMP_SSA) {
6681 mono_ssa_deadce (cfg);
6683 //mono_ssa_strength_reduction (cfg);
6685 mono_ssa_remove (cfg);
6687 if (cfg->opt & MONO_OPT_BRANCH)
6688 optimize_branches (cfg);
6691 /* after SSA removal */
6695 decompose_pass (cfg);
6697 /* FIXME: disabled with exception clauses: bug #42136 */
6698 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6701 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6702 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6703 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6704 mono_analyze_liveness (cfg);
6706 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6707 regs = mono_arch_get_global_int_regs (cfg);
6708 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6712 //mono_print_code (cfg);
6716 /* variables are allocated after decompose, since decompose could create temps */
6717 mono_arch_allocate_vars (cfg);
6719 if (cfg->opt & MONO_OPT_CFOLD)
6720 mono_constant_fold (cfg);
6722 mini_select_instructions (cfg);
6725 if (cfg->verbose_level >= 2) {
6726 char *id = mono_method_full_name (cfg->method, FALSE);
6727 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6731 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6733 jinfo = g_new0 (MonoJitInfo, 1);
6734 jinfo->method = method;
6735 jinfo->code_start = cfg->native_code;
6736 jinfo->code_size = cfg->code_len;
6737 jinfo->used_regs = cfg->used_int_regs;
6739 if (header->num_clauses) {
6742 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6743 jinfo->num_clauses = header->num_clauses;
6744 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6745 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6747 for (i = 0; i < header->num_clauses; i++) {
6748 MonoExceptionClause *ec = &header->clauses [i];
6749 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6750 MonoBasicBlock *tblock;
6752 ei->flags = ec->flags;
6754 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6755 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6757 ei->data.filter = cfg->native_code + tblock->native_offset;
6759 ei->data.token = ec->token_or_filter;
6762 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6764 ei->try_start = cfg->native_code + tblock->native_offset;
6765 g_assert (tblock->native_offset);
6766 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6768 ei->try_end = cfg->native_code + tblock->native_offset;
6769 g_assert (tblock->native_offset);
6770 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6772 ei->handler_start = cfg->native_code + tblock->native_offset;
6776 mono_jit_info_table_add (cfg->domain, jinfo);
6778 /* collect statistics */
6779 mono_jit_stats.allocated_code_size += cfg->code_len;
6780 code_size_ratio = cfg->code_len;
6781 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6782 mono_jit_stats.biggest_method_size = code_size_ratio;
6783 mono_jit_stats.biggest_method = method;
6785 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6786 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6787 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6788 mono_jit_stats.max_ratio_method = method;
6790 mono_jit_stats.native_code_size += cfg->code_len;
6792 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6793 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6799 mono_jit_compile_method (MonoMethod *method)
6801 /* FIXME: later copy the code from mono */
6802 MonoDomain *target_domain, *domain = mono_domain_get ();
6804 GHashTable *jit_code_hash;
6807 if (default_opt & MONO_OPT_SHARED)
6808 target_domain = mono_root_domain;
6810 target_domain = domain;
6812 jit_code_hash = target_domain->jit_code_hash;
6814 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6815 mono_jit_stats.methods_lookups++;
6819 #ifdef MONO_USE_AOT_COMPILER
6820 if (!mono_compile_aot) {
6821 mono_class_init (method->klass);
6822 if ((code = mono_aot_get_method (method))) {
6823 g_hash_table_insert (jit_code_hash, method, code);
6829 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6830 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6831 if (!method->info) {
6834 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6835 mono_lookup_pinvoke_call (method);
6836 #ifdef MONO_USE_EXC_TABLES
6837 if (mono_method_blittable (method)) {
6838 method->info = method->addr;
6841 nm = mono_marshal_get_native_wrapper (method);
6842 method->info = mono_compile_method (nm);
6844 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
6845 //mono_debug_add_wrapper (method, nm);
6846 #ifdef MONO_USE_EXC_TABLES
6850 return method->info;
6851 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
6852 const char *name = method->name;
6855 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
6856 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
6857 /* FIXME: uhm, we need a wrapper to handle exceptions? */
6858 return (gpointer)mono_delegate_ctor;
6859 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
6860 nm = mono_marshal_get_delegate_invoke (method);
6861 return mono_jit_compile_method (nm);
6862 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
6863 nm = mono_marshal_get_delegate_begin_invoke (method);
6864 return mono_jit_compile_method (nm);
6865 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
6866 nm = mono_marshal_get_delegate_end_invoke (method);
6867 return mono_jit_compile_method (nm);
6873 cfg = mini_method_compile (method, default_opt, target_domain, 0);
6874 code = cfg->native_code;
6875 mono_destroy_compile (cfg);
6877 g_hash_table_insert (jit_code_hash, method, code);
6879 if (target_domain->jump_target_hash) {
6880 MonoJumpInfo patch_info;
6882 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
6884 patch_info.next = NULL;
6885 patch_info.ip.i = 0;
6886 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
6887 patch_info.data.method = method;
6888 g_hash_table_remove (target_domain->jump_target_hash, method);
6890 for (tmp = list; tmp; tmp = tmp->next)
6891 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
6892 g_slist_free (list);
6894 /* make sure runtime_init is called */
6895 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
6901 * mono_jit_runtime_invoke:
6902 * @method: the method to invoke
6903 * @obj: this pointer
6904 * @params: array of parameter values.
6905 * @exc: used to catch exceptions objects
6908 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
6911 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
6912 invoke = mono_marshal_get_runtime_invoke (method);
6913 runtime_invoke = mono_jit_compile_method (invoke);
6914 return runtime_invoke (obj, params, exc);
6917 #ifdef PLATFORM_WIN32
6918 #define GET_CONTEXT \
6919 struct sigcontext *ctx = (struct sigcontext*)_dummy;
6921 #define GET_CONTEXT \
6922 void **_p = (void **)&_dummy; \
6923 struct sigcontext *ctx = (struct sigcontext *)++_p;
6927 sigfpe_signal_handler (int _dummy)
6932 exc = mono_get_exception_divide_by_zero ();
6934 mono_arch_handle_exception (ctx, exc, FALSE);
6938 sigill_signal_handler (int _dummy)
6942 exc = mono_get_exception_execution_engine ("SIGILL");
6944 mono_arch_handle_exception (ctx, exc, FALSE);
6948 sigsegv_signal_handler (int _dummy)
6953 exc = mono_get_exception_null_reference ();
6955 mono_arch_handle_exception (ctx, exc, FALSE);
6959 sigusr1_signal_handler (int _dummy)
6964 thread = mono_thread_current ();
6966 g_assert (thread->abort_exc);
6968 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
6972 sigquit_signal_handler (int _dummy)
6977 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
6979 mono_arch_handle_exception (ctx, exc, FALSE);
6984 mono_runtime_install_handlers (void)
6986 #ifndef PLATFORM_WIN32
6987 struct sigaction sa;
6990 #ifdef PLATFORM_WIN32
6992 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
6993 win32_seh_set_handler(SIGILL, sigill_signal_handler);
6994 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
6995 #else /* !PLATFORM_WIN32 */
6997 /* libpthreads has its own implementation of sigaction(),
6998 * but it seems to work well with our current exception
6999 * handlers. If not we must call syscall directly instead
7003 sa.sa_handler = sigfpe_signal_handler;
7004 sigemptyset (&sa.sa_mask);
7006 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
7007 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
7010 sa.sa_handler = sigquit_signal_handler;
7011 sigemptyset (&sa.sa_mask);
7013 g_assert (sigaction (SIGQUIT, &sa, NULL) != -1);
7016 sa.sa_handler = sigill_signal_handler;
7017 sigemptyset (&sa.sa_mask);
7019 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7020 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
7022 /* catch thread abort signal */
7023 sa.sa_handler = sigusr1_signal_handler;
7024 sigemptyset (&sa.sa_mask);
7026 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7027 //g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
7031 sa.sa_handler = sigsegv_signal_handler;
7032 sigemptyset (&sa.sa_mask);
7034 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
7035 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
7037 #endif /* PLATFORM_WIN32 */
7040 /* mono_jit_create_remoting_trampoline:
7041 * @method: pointer to the method info
7043 * Creates a trampoline which calls the remoting functions. This
7044 * is used in the vtable of transparent proxies.
7046 * Returns: a pointer to the newly created code
7049 mono_jit_create_remoting_trampoline (MonoMethod *method)
7052 guint8 *addr = NULL;
7054 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
7055 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
7056 nm = mono_marshal_get_remoting_invoke (method);
7057 addr = mono_compile_method (nm);
7059 addr = mono_compile_method (method);
7064 static CRITICAL_SECTION ms;
7067 mini_init (const char *filename)
7071 mono_arch_cpu_init ();
7073 metadata_section = &ms;
7074 InitializeCriticalSection (metadata_section);
7076 mono_jit_tls_id = TlsAlloc ();
7077 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
7081 mono_runtime_install_handlers ();
7083 mono_install_compile_method (mono_jit_compile_method);
7084 mono_install_trampoline (mono_arch_create_jit_trampoline);
7085 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
7086 mono_install_runtime_invoke (mono_jit_runtime_invoke);
7087 mono_install_handler (mono_arch_get_throw_exception ());
7088 mono_install_stack_walk (mono_jit_walk_stack);
7089 mono_install_get_config_dir ();
7091 domain = mono_init (filename);
7094 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
7095 ves_icall_get_frame_info);
7096 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
7097 ves_icall_get_trace);
7098 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
7099 mono_runtime_install_handlers);
7102 create_helper_signature ();
7104 mono_arch_register_lowlevel_calls ();
7105 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
7106 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
7108 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
7109 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
7111 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
7112 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
7113 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
7115 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
7116 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
7117 helper_sig_void_ptr, TRUE);
7120 * NOTE, NOTE, NOTE, NOTE:
7121 * when adding emulation for some opcodes, remember to also add a dummy
7122 * rule to the burg files, because we need the arity information to be correct.
7124 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
7125 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
7126 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
7127 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
7128 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
7129 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
7130 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
7132 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
7133 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
7134 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
7136 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
7137 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
7138 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
7139 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
7141 #if SIZEOF_VOID_P == 4
7142 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
7144 #warning "fixme: add opcode emulation"
7147 /* other jit icalls */
7148 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
7149 helper_sig_ptr_ptr_ptr, FALSE);
7150 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
7151 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
7152 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
7153 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
7154 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
7155 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
7156 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
7157 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
7158 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
7159 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
7160 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
7161 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
7162 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
7163 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
7164 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
7165 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
7166 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
7167 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
7168 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
7169 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
7170 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
7171 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
7172 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
7173 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
7174 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
7175 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7176 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7177 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
7178 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
7179 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
7180 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
7182 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
7183 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
7185 //mono_thread_attach (domain);
7189 MonoJitStats mono_jit_stats = {0};
7192 print_jit_stats (void)
7194 if (mono_jit_stats.enabled) {
7195 g_print ("Mono Jit statistics\n");
7196 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7197 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7198 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
7199 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
7200 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
7201 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
7202 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
7203 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
7204 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
7205 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
7206 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
7207 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
7208 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
7209 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
7210 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7211 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7212 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7213 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7215 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7216 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7217 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7218 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7219 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7224 mini_cleanup (MonoDomain *domain)
7227 * mono_runtime_cleanup() and mono_domain_finalize () need to
7228 * be called early since they need the execution engine still
7229 * fully working (mono_domain_finalize may invoke managed finalizers
7230 * and mono_runtime_cleanup will wait for other threads to finish).
7232 mono_domain_finalize (domain);
7234 mono_runtime_cleanup (domain);
7236 mono_profiler_shutdown ();
7238 mono_debug_cleanup ();
7240 #ifdef PLATFORM_WIN32
7241 win32_seh_cleanup();
7244 mono_domain_unload (domain, TRUE);
7247 DeleteCriticalSection (metadata_section);
7251 mono_set_defaults (int verbose_level, guint32 opts)
7253 mini_verbose = verbose_level;