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:
3450 token = read32 (ip + 1);
3451 if (method->wrapper_type != MONO_WRAPPER_NONE)
3452 klass = mono_method_get_wrapper_data (method, token);
3454 klass = mono_class_get (image, token);
3456 mono_class_init (klass);
3457 if (klass->byval_arg.type == MONO_TYPE_VAR)
3458 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3460 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3461 MonoInst *store, *load;
3462 MONO_INST_NEW (cfg, load, CEE_LDIND_REF);
3463 load->cil_code = ip;
3464 load->inst_i0 = sp [1];
3465 load->type = ldind_type [CEE_LDIND_REF];
3466 load->flags |= ins_flag;
3467 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
3468 store->cil_code = ip;
3469 handle_loaded_temps (cfg, bblock, stack_start, sp);
3470 MONO_ADD_INS (bblock, store);
3471 store->inst_i0 = sp [0];
3472 store->inst_i1 = load;
3473 store->flags |= ins_flag;
3475 n = mono_class_value_size (klass, NULL);
3476 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3478 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3479 copy->inst_left = sp [0];
3480 copy->inst_right = sp [1];
3481 copy->cil_code = ip;
3483 MONO_ADD_INS (bblock, copy);
3485 MonoInst *iargs [3];
3488 NEW_ICONST (cfg, iargs [2], n);
3489 iargs [2]->cil_code = ip;
3491 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3498 MonoInst *iargs [3];
3501 token = read32 (ip + 1);
3502 if (method->wrapper_type != MONO_WRAPPER_NONE)
3503 klass = mono_method_get_wrapper_data (method, token);
3505 klass = mono_class_get (image, token);
3507 mono_class_init (klass);
3508 if (klass->byval_arg.type == MONO_TYPE_VAR)
3509 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3510 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3511 MONO_INST_NEW (cfg, ins, CEE_LDIND_REF);
3513 ins->inst_i0 = sp [0];
3514 ins->type = ldind_type [CEE_LDIND_REF];
3515 ins->flags |= ins_flag;
3521 n = mono_class_value_size (klass, NULL);
3522 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3523 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3524 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3526 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3527 copy->inst_left = iargs [0];
3528 copy->inst_right = *sp;
3529 copy->cil_code = ip;
3531 MONO_ADD_INS (bblock, copy);
3534 NEW_ICONST (cfg, iargs [2], n);
3535 iargs [2]->cil_code = ip;
3537 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3539 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3547 CHECK_STACK_OVF (1);
3548 n = read32 (ip + 1);
3550 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3552 MonoInst *iargs [1];
3554 NEW_PCONST (cfg, iargs [0], mono_method_get_wrapper_data (method, n));
3555 temp = mono_emit_jit_icall (cfg, bblock, mono_string_new_wrapper, iargs, ip);
3556 NEW_TEMPLOAD (cfg, *sp, temp);
3560 if (mono_compile_aot) {
3561 cfg->ldstr_list = g_list_prepend (cfg->ldstr_list, (gpointer)n);
3564 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3566 MonoInst *iargs [3];
3567 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3568 NEW_IMAGECONST (cfg, iargs [1], image);
3569 NEW_ICONST (cfg, iargs [2], mono_metadata_token_index (n));
3570 temp = mono_emit_jit_icall (cfg, bblock, mono_ldstr, iargs, ip);
3571 NEW_TEMPLOAD (cfg, *sp, temp);
3572 mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3574 NEW_PCONST (cfg, ins, NULL);
3576 ins->type = STACK_OBJ;
3577 ins->inst_p0 = mono_ldstr (cfg->domain, image, mono_metadata_token_index (n));
3586 MonoInst *iargs [2];
3587 MonoMethodSignature *fsig;
3590 token = read32 (ip + 1);
3591 if (method->wrapper_type != MONO_WRAPPER_NONE) {
3592 cmethod = mono_method_get_wrapper_data (method, token);
3594 cmethod = mono_get_method (image, token, NULL);
3595 fsig = mono_method_get_signature (cmethod, image, token);
3597 mono_class_init (cmethod->klass);
3599 n = fsig->param_count;
3602 /* move the args to allow room for 'this' in the first position */
3608 handle_loaded_temps (cfg, bblock, stack_start, sp);
3611 if (cmethod->klass->parent == mono_defaults.array_class) {
3612 NEW_METHODCONST (cfg, *sp, cmethod);
3613 temp = mono_emit_native_call (cfg, bblock, mono_array_new_va, fsig, sp, ip, FALSE);
3615 } else if (cmethod->string_ctor) {
3616 /* we simply pass a null pointer */
3617 NEW_PCONST (cfg, *sp, NULL);
3618 /* now call the string ctor */
3619 temp = mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, NULL);
3621 if (cmethod->klass->valuetype) {
3622 iargs [0] = mono_compile_create_var (cfg, &cmethod->klass->byval_arg, OP_LOCAL);
3623 temp = iargs [0]->inst_c0;
3624 NEW_TEMPLOADA (cfg, *sp, temp);
3626 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
3627 NEW_DOMAINCONST (cfg, iargs [0]);
3628 NEW_CLASSCONST (cfg, iargs [1], cmethod->klass);
3630 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
3632 MonoVTable *vtable = mono_class_vtable (cfg->domain, cmethod->klass);
3633 NEW_PCONST (cfg, iargs [0], vtable);
3634 if (cmethod->klass->has_finalize || cmethod->klass->marshalbyref || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
3635 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
3637 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
3639 NEW_TEMPLOAD (cfg, *sp, temp);
3642 if ((cfg->opt & MONO_OPT_INLINE) && cmethod &&
3643 mono_method_check_inlining (cfg, cmethod) &&
3644 !mono_class_is_subclass_of (cmethod->klass, mono_defaults.exception_class, FALSE) &&
3645 !g_list_find (dont_inline, cmethod)) {
3647 MonoBasicBlock *ebblock;
3648 if ((costs = inline_method (cfg, cmethod, fsig, bblock, sp, ip, real_offset, dont_inline, &ebblock))) {
3653 GET_BBLOCK (cfg, bbhash, bblock, ip);
3654 ebblock->next_bb = bblock;
3655 link_bblock (cfg, ebblock, bblock);
3657 NEW_TEMPLOAD (cfg, *sp, temp);
3660 /* indicates start of a new block, and triggers a load
3661 of all stack arguments at bb boundarie */
3664 inline_costs += costs;
3668 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3671 /* now call the actual ctor */
3672 mono_emit_method_call_spilled (cfg, bblock, cmethod, fsig, sp, ip, sp[0]);
3676 NEW_TEMPLOAD (cfg, *sp, temp);
3685 MONO_INST_NEW (cfg, ins, *ip);
3687 klass = mono_class_get (image, read32 (ip + 1));
3688 mono_class_init (klass);
3689 ins->type = STACK_OBJ;
3690 ins->inst_left = *sp;
3691 ins->inst_newa_class = klass;
3696 case CEE_UNBOX_ANY: {
3697 MonoInst *add, *vtoffset;
3698 MonoInst *iargs [3];
3702 token = read32 (ip + 1);
3703 if (method->wrapper_type != MONO_WRAPPER_NONE)
3704 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3706 klass = mono_class_get (image, token);
3707 mono_class_init (klass);
3709 if (klass->byval_arg.type == MONO_TYPE_VAR)
3710 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3712 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
3714 MONO_INST_NEW (cfg, ins, CEE_CASTCLASS);
3715 ins->type = STACK_OBJ;
3716 ins->inst_left = *sp;
3718 ins->inst_newa_class = klass;
3725 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3726 ins->type = STACK_OBJ;
3727 ins->inst_left = *sp;
3729 ins->inst_newa_class = klass;
3732 MONO_INST_NEW (cfg, add, CEE_ADD);
3733 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3734 add->inst_left = ins;
3735 add->inst_right = vtoffset;
3736 add->type = STACK_MP;
3740 n = mono_class_value_size (klass, NULL);
3741 ins = mono_compile_create_var (cfg, &klass->byval_arg, OP_LOCAL);
3742 NEW_TEMPLOADA (cfg, iargs [0], ins->inst_c0);
3743 if ((cfg->opt & MONO_OPT_INTRINS) && n <= sizeof (gpointer) * 5) {
3745 MONO_INST_NEW (cfg, copy, OP_MEMCPY);
3746 copy->inst_left = iargs [0];
3747 copy->inst_right = *sp;
3748 copy->cil_code = ip;
3750 MONO_ADD_INS (bblock, copy);
3753 NEW_ICONST (cfg, iargs [2], n);
3754 iargs [2]->cil_code = ip;
3756 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
3758 NEW_TEMPLOAD (cfg, *sp, ins->inst_c0);
3764 MonoInst *add, *vtoffset;
3768 token = read32 (ip + 1);
3769 if (method->wrapper_type != MONO_WRAPPER_NONE)
3770 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
3772 klass = mono_class_get (image, token);
3773 mono_class_init (klass);
3775 if (klass->byval_arg.type == MONO_TYPE_VAR)
3776 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3778 MONO_INST_NEW (cfg, ins, OP_UNBOXCAST);
3779 ins->type = STACK_OBJ;
3780 ins->inst_left = *sp;
3782 ins->inst_newa_class = klass;
3785 MONO_INST_NEW (cfg, add, CEE_ADD);
3786 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
3787 add->inst_left = ins;
3788 add->inst_right = vtoffset;
3789 add->type = STACK_MP;
3797 MONO_INST_NEW (cfg, ins, *ip);
3799 klass = mono_class_get (image, read32 (ip + 1));
3800 mono_class_init (klass);
3801 if (klass->byval_arg.type == MONO_TYPE_VAR)
3802 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
3803 ins->type = STACK_OBJ;
3804 ins->inst_left = *sp;
3806 ins->inst_newa_class = klass;
3813 MONO_INST_NEW (cfg, ins, *ip);
3815 ins->inst_left = *sp;
3816 ins->cil_code = ip++;
3817 MONO_ADD_INS (bblock, ins);
3819 start_new_bblock = 1;
3824 MonoInst *offset_ins;
3825 MonoClassField *field;
3826 MonoBasicBlock *ebblock;
3830 if (*ip == CEE_STFLD) {
3837 // FIXME: enable this test later.
3838 //if (sp [0]->type != STACK_OBJ && sp [0]->type != STACK_MP)
3840 token = read32 (ip + 1);
3841 field = mono_field_from_token (image, token, &klass);
3842 if (field->parent->gen_params)
3843 field = get_generic_field_inst (field, method->klass, &klass);
3844 mono_class_init (klass);
3846 foffset = klass->valuetype? field->offset - sizeof (MonoObject): field->offset;
3847 /* FIXME: mark instructions for use in SSA */
3848 if (*ip == CEE_STFLD) {
3849 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3850 MonoMethod *stfld_wrapper = mono_marshal_get_stfld_wrapper (field->type);
3851 MonoInst *iargs [5];
3854 NEW_CLASSCONST (cfg, iargs [1], klass);
3855 NEW_FIELDCONST (cfg, iargs [2], field);
3856 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) :
3860 if (cfg->opt & MONO_OPT_INLINE) {
3861 costs = inline_method (cfg, stfld_wrapper, stfld_wrapper->signature, bblock,
3862 iargs, ip, real_offset, dont_inline, &ebblock);
3863 g_assert (costs > 0);
3868 GET_BBLOCK (cfg, bbhash, bblock, ip);
3869 ebblock->next_bb = bblock;
3870 link_bblock (cfg, ebblock, bblock);
3872 /* indicates start of a new block, and triggers a load
3873 of all stack arguments at bb boundarie */
3876 inline_costs += costs;
3879 mono_emit_method_call_spilled (cfg, bblock, stfld_wrapper, stfld_wrapper->signature, iargs, ip, NULL);
3883 NEW_ICONST (cfg, offset_ins, foffset);
3884 MONO_INST_NEW (cfg, ins, CEE_ADD);
3886 ins->inst_left = *sp;
3887 ins->inst_right = offset_ins;
3888 ins->type = STACK_MP;
3890 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
3891 store->cil_code = ip;
3892 store->inst_left = ins;
3893 store->inst_right = sp [1];
3894 handle_loaded_temps (cfg, bblock, stack_start, sp);
3895 store->flags |= ins_flag;
3897 if (store->opcode == CEE_STOBJ) {
3898 handle_stobj (cfg, bblock, ins, sp [1], ip,
3899 mono_class_from_mono_type (field->type), FALSE, FALSE);
3901 MONO_ADD_INS (bblock, store);
3904 if (klass->marshalbyref && !MONO_CHECK_THIS (sp [0])) {
3905 /* fixme: we need to inline that call somehow */
3906 MonoMethod *ldfld_wrapper = mono_marshal_get_ldfld_wrapper (field->type);
3907 MonoInst *iargs [4];
3911 NEW_CLASSCONST (cfg, iargs [1], klass);
3912 NEW_FIELDCONST (cfg, iargs [2], field);
3913 NEW_ICONST (cfg, iargs [3], klass->valuetype ? field->offset - sizeof (MonoObject) : field->offset);
3914 if (cfg->opt & MONO_OPT_INLINE) {
3915 costs = inline_method (cfg, ldfld_wrapper, ldfld_wrapper->signature, bblock,
3916 iargs, ip, real_offset, dont_inline, &ebblock);
3917 g_assert (costs > 0);
3922 GET_BBLOCK (cfg, bbhash, bblock, ip);
3923 ebblock->next_bb = bblock;
3924 link_bblock (cfg, ebblock, bblock);
3926 temp = iargs [0]->inst_i0->inst_c0;
3928 if (*ip == CEE_LDFLDA) {
3929 /* not sure howto handle this */
3930 NEW_TEMPLOADA (cfg, *sp, temp);
3932 NEW_TEMPLOAD (cfg, *sp, temp);
3936 /* indicates start of a new block, and triggers a load of
3937 all stack arguments at bb boundarie */
3940 inline_costs += costs;
3943 temp = mono_emit_method_call_spilled (cfg, bblock, ldfld_wrapper, ldfld_wrapper->signature, iargs, ip, NULL);
3944 if (*ip == CEE_LDFLDA) {
3945 /* not sure howto handle this */
3946 NEW_TEMPLOADA (cfg, *sp, temp);
3948 NEW_TEMPLOAD (cfg, *sp, temp);
3953 NEW_ICONST (cfg, offset_ins, foffset);
3954 MONO_INST_NEW (cfg, ins, CEE_ADD);
3956 ins->inst_left = *sp;
3957 ins->inst_right = offset_ins;
3958 ins->type = STACK_MP;
3960 if (*ip == CEE_LDFLDA) {
3964 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
3965 type_to_eval_stack_type (field->type, load);
3966 load->cil_code = ip;
3967 load->inst_left = ins;
3968 load->flags |= ins_flag;
3980 MonoClassField *field;
3982 token = read32 (ip + 1);
3984 field = mono_field_from_token (image, token, &klass);
3985 mono_class_init (klass);
3987 handle_loaded_temps (cfg, bblock, stack_start, sp);
3989 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
3991 MonoInst *iargs [2];
3992 g_assert (field->parent);
3993 NEW_TEMPLOAD (cfg, iargs [0], mono_get_domainvar (cfg)->inst_c0);
3994 NEW_FIELDCONST (cfg, iargs [1], field);
3995 temp = mono_emit_jit_icall (cfg, bblock, mono_class_static_field_address, iargs, ip);
3996 NEW_TEMPLOAD (cfg, ins, temp);
4000 vtable = mono_class_vtable (cfg->domain, klass);
4001 if (!cfg->domain->thread_static_fields || !(addr = g_hash_table_lookup (cfg->domain->thread_static_fields, field))) {
4002 if (!vtable->initialized && !(klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT) && needs_cctor_run (klass, method)) {
4003 MonoInst *iargs [1];
4004 NEW_PCONST (cfg, iargs [0], vtable);
4005 mono_emit_jit_icall (cfg, bblock, mono_runtime_class_init, iargs, ip);
4006 if (cfg->verbose_level > 2)
4007 g_print ("class %s.%s needs init call for %s\n", klass->name_space, klass->name, field->name);
4009 mono_runtime_class_init (vtable);
4011 addr = (char*)vtable->data + field->offset;
4012 NEW_PCONST (cfg, ins, addr);
4016 * insert call to mono_threads_get_static_data (GPOINTER_TO_UINT (addr))
4017 * This could be later optimized to do just a couple of
4018 * memory dereferences with constant offsets.
4021 MonoInst *iargs [1];
4022 NEW_ICONST (cfg, iargs [0], GPOINTER_TO_UINT (addr));
4023 temp = mono_emit_jit_icall (cfg, bblock, mono_threads_get_static_data, iargs, ip);
4024 NEW_TEMPLOAD (cfg, ins, temp);
4028 /* FIXME: mark instructions for use in SSA */
4029 if (*ip == CEE_LDSFLDA) {
4031 } else if (*ip == CEE_STSFLD) {
4035 MONO_INST_NEW (cfg, store, mono_type_to_stind (field->type));
4036 store->cil_code = ip;
4037 store->inst_left = ins;
4038 store->inst_right = sp [0];
4039 store->flags |= ins_flag;
4042 if (store->opcode == CEE_STOBJ) {
4043 handle_stobj (cfg, bblock, ins, sp [0], ip, mono_class_from_mono_type (field->type), FALSE, FALSE);
4045 MONO_ADD_INS (bblock, store);
4047 gboolean is_const = FALSE;
4048 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4049 if (!((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) &&
4050 vtable->initialized && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY)) {
4051 gpointer addr = (char*)vtable->data + field->offset;
4052 /* g_print ("RO-FIELD %s.%s:%s\n", klass->name_space, klass->name, field->name);*/
4054 switch (field->type->type) {
4055 case MONO_TYPE_BOOLEAN:
4057 NEW_ICONST (cfg, *sp, *((guint8 *)addr));
4061 NEW_ICONST (cfg, *sp, *((gint8 *)addr));
4064 case MONO_TYPE_CHAR:
4066 NEW_ICONST (cfg, *sp, *((guint16 *)addr));
4070 NEW_ICONST (cfg, *sp, *((gint16 *)addr));
4075 NEW_ICONST (cfg, *sp, *((gint32 *)addr));
4079 NEW_ICONST (cfg, *sp, *((guint32 *)addr));
4084 case MONO_TYPE_STRING:
4085 case MONO_TYPE_OBJECT:
4086 case MONO_TYPE_CLASS:
4087 case MONO_TYPE_SZARRAY:
4089 case MONO_TYPE_FNPTR:
4090 case MONO_TYPE_ARRAY:
4091 NEW_PCONST (cfg, *sp, *((gpointer *)addr));
4092 type_to_eval_stack_type (field->type, *sp);
4097 MONO_INST_NEW (cfg, *sp, OP_I8CONST);
4098 sp [0]->type = STACK_I8;
4099 sp [0]->inst_l = *((gint64 *)addr);
4104 case MONO_TYPE_VALUETYPE:
4113 CHECK_STACK_OVF (1);
4114 MONO_INST_NEW (cfg, load, mono_type_to_ldind (field->type));
4115 type_to_eval_stack_type (field->type, load);
4116 load->cil_code = ip;
4117 load->inst_left = ins;
4119 load->flags |= ins_flag;
4121 /* fixme: dont see the problem why this does not work */
4122 //cfg->disable_aot = TRUE;
4131 token = read32 (ip + 1);
4132 if (method->wrapper_type != MONO_WRAPPER_NONE)
4133 klass = mono_method_get_wrapper_data (method, token);
4135 klass = mono_class_get (image, token);
4136 mono_class_init (klass);
4137 if (klass->byval_arg.type == MONO_TYPE_VAR)
4138 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4139 n = mono_type_to_stind (&klass->byval_arg);
4140 if (n == CEE_STOBJ) {
4141 handle_stobj (cfg, bblock, sp [0], sp [1], ip, klass, FALSE, FALSE);
4143 /* FIXME: should check item at sp [1] is compatible with the type of the store. */
4145 MONO_INST_NEW (cfg, store, n);
4146 store->cil_code = ip;
4147 store->inst_left = sp [0];
4148 store->inst_right = sp [1];
4149 store->flags |= ins_flag;
4150 MONO_ADD_INS (bblock, store);
4157 MonoInst *iargs [2];
4158 MonoInst *load, *vtoffset, *add, *val, *vstore;
4163 token = read32 (ip + 1);
4164 if (method->wrapper_type != MONO_WRAPPER_NONE)
4165 klass = mono_method_get_wrapper_data (method, token);
4167 klass = mono_class_get (image, token);
4168 mono_class_init (klass);
4169 if (klass->byval_arg.type == MONO_TYPE_VAR)
4170 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4172 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4177 /* much like NEWOBJ */
4178 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
4179 NEW_DOMAINCONST (cfg, iargs [0]);
4180 NEW_CLASSCONST (cfg, iargs [1], klass);
4182 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4184 MonoVTable *vtable = mono_class_vtable (cfg->domain, klass);
4185 NEW_PCONST (cfg, iargs [0], vtable);
4186 if (1 || klass->has_finalize || (cfg->prof_options & MONO_PROFILE_ALLOCATIONS))
4187 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_specific, iargs, ip);
4189 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new_fast, iargs, ip);
4191 NEW_TEMPLOAD (cfg, load, temp);
4192 NEW_ICONST (cfg, vtoffset, sizeof (MonoObject));
4193 MONO_INST_NEW (cfg, add, CEE_ADD);
4194 add->inst_left = load;
4195 add->inst_right = vtoffset;
4198 MONO_INST_NEW (cfg, vstore, CEE_STIND_I);
4199 vstore->opcode = mono_type_to_stind (&klass->byval_arg);
4200 vstore->cil_code = ip;
4201 vstore->inst_left = add;
4202 vstore->inst_right = val;
4204 if (vstore->opcode == CEE_STOBJ) {
4205 handle_stobj (cfg, bblock, add, val, ip, klass, FALSE, FALSE);
4207 MONO_ADD_INS (bblock, vstore);
4209 NEW_TEMPLOAD (cfg, load, temp);
4217 MONO_INST_NEW (cfg, ins, *ip);
4221 token = read32 (ip + 1);
4223 /* allocate the domainvar - becaus this is used in decompose_foreach */
4224 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)
4225 mono_get_domainvar (cfg);
4227 if (method->wrapper_type != MONO_WRAPPER_NONE)
4228 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4230 klass = mono_class_get (image, token);
4232 mono_class_init (klass);
4233 if (klass->byval_arg.type == MONO_TYPE_VAR)
4234 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4235 ins->inst_newa_class = klass;
4236 ins->inst_newa_len = *sp;
4237 ins->type = STACK_OBJ;
4244 MONO_INST_NEW (cfg, ins, *ip);
4245 ins->cil_code = ip++;
4247 ins->inst_left = *sp;
4248 ins->type = STACK_PTR;
4254 klass = mono_class_get (image, read32 (ip + 1));
4255 mono_class_init (klass);
4256 if (klass->byval_arg.type == MONO_TYPE_VAR)
4257 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4258 NEW_LDELEMA (cfg, ins, sp, klass);
4267 token = read32 (ip + 1);
4268 klass = mono_class_get (image, token);
4269 mono_class_init (klass);
4270 if (klass->byval_arg.type == MONO_TYPE_VAR)
4271 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4272 NEW_LDELEMA (cfg, load, sp, klass);
4273 load->cil_code = ip;
4274 MONO_INST_NEW (cfg, ins, mono_type_to_ldind (&klass->byval_arg));
4276 ins->inst_left = load;
4278 type_to_eval_stack_type (&klass->byval_arg, ins);
4292 case CEE_LDELEM_REF: {
4296 * ldind.x (ldelema (array, index))
4297 * ldelema does the bounds check
4301 klass = array_access_to_klass (*ip);
4302 NEW_LDELEMA (cfg, load, sp, klass);
4303 load->cil_code = ip;
4304 MONO_INST_NEW (cfg, ins, ldelem_to_ldind [*ip - CEE_LDELEM_I1]);
4306 ins->inst_left = load;
4308 ins->type = ldind_type [ins->opcode - CEE_LDIND_I1];
4318 case CEE_STELEM_R8: {
4322 * stind.x (ldelema (array, index), val)
4323 * ldelema does the bounds check
4327 klass = array_access_to_klass (*ip);
4328 NEW_LDELEMA (cfg, load, sp, klass);
4329 load->cil_code = ip;
4330 MONO_INST_NEW (cfg, ins, stelem_to_stind [*ip - CEE_STELEM_I]);
4332 ins->inst_left = load;
4333 ins->inst_right = sp [2];
4335 handle_loaded_temps (cfg, bblock, stack_start, sp);
4336 MONO_ADD_INS (bblock, ins);
4338 cfg->disable_ssa = TRUE;
4345 * stind.x (ldelema (array, index), val)
4346 * ldelema does the bounds check
4350 token = read32 (ip + 1);
4351 klass = mono_class_get (image, token);
4352 mono_class_init (klass);
4353 if (klass->byval_arg.type == MONO_TYPE_VAR)
4354 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4355 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4356 MonoInst *iargs [3];
4357 handle_loaded_temps (cfg, bblock, stack_start, sp);
4363 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4365 NEW_LDELEMA (cfg, load, sp, klass);
4366 load->cil_code = ip;
4367 MONO_INST_NEW (cfg, ins, mono_type_to_stind (&klass->byval_arg));
4369 ins->inst_left = load;
4370 ins->inst_right = sp [2];
4371 handle_loaded_temps (cfg, bblock, stack_start, sp);
4372 MONO_ADD_INS (bblock, ins);
4376 cfg->disable_ssa = TRUE;
4379 case CEE_STELEM_REF: {
4380 MonoInst *iargs [3];
4385 handle_loaded_temps (cfg, bblock, stack_start, sp);
4391 mono_emit_jit_icall (cfg, bblock, helper_stelem_ref, iargs, ip);
4395 NEW_GROUP (cfg, group, sp [0], sp [1]);
4396 MONO_INST_NEW (cfg, ins, CEE_STELEM_REF);
4398 ins->inst_left = group;
4399 ins->inst_right = sp [2];
4400 MONO_ADD_INS (bblock, ins);
4405 cfg->disable_ssa = TRUE;
4408 case CEE_CKFINITE: {
4409 MonoInst *store, *temp;
4412 /* this instr. can throw exceptions as side effect,
4413 * so we cant eliminate dead code which contains CKFINITE opdodes.
4414 * Spilling to memory makes sure that we always perform
4418 MONO_INST_NEW (cfg, ins, CEE_CKFINITE);
4420 ins->inst_left = sp [-1];
4421 temp = mono_compile_create_var (cfg, &mono_defaults.double_class->byval_arg, OP_LOCAL);
4423 NEW_TEMPSTORE (cfg, store, temp->inst_c0, ins);
4424 store->cil_code = ip;
4425 MONO_ADD_INS (bblock, store);
4427 NEW_TEMPLOAD (cfg, sp [-1], temp->inst_c0);
4434 g_error ("opcode 0x%02x not handled", *ip);
4438 MonoClass *handle_class;
4440 CHECK_STACK_OVF (1);
4442 n = read32 (ip + 1);
4444 handle = mono_ldtoken (image, n, &handle_class);
4445 mono_class_init (handle_class);
4447 if (((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot)) {
4449 MonoInst *res, *store, *addr, *vtvar, *iargs [2];
4451 vtvar = mono_compile_create_var (cfg, &handle_class->byval_arg, OP_LOCAL);
4453 NEW_IMAGECONST (cfg, iargs [0], image);
4454 NEW_ICONST (cfg, iargs [1], n);
4455 temp = mono_emit_jit_icall (cfg, bblock, mono_ldtoken_wrapper, iargs, ip);
4456 NEW_TEMPLOAD (cfg, res, temp);
4457 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4458 NEW_INDSTORE (cfg, store, addr, res, &mono_defaults.int_class->byval_arg);
4459 MONO_ADD_INS (bblock, store);
4460 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4462 if ((ip [5] == CEE_CALL) && (cmethod = mono_get_method (image, read32 (ip + 6), NULL)) &&
4463 (cmethod->klass == mono_defaults.monotype_class->parent) &&
4464 (strcmp (cmethod->name, "GetTypeFromHandle") == 0)) {
4465 MonoClass *tclass = mono_class_from_mono_type (handle);
4466 mono_class_init (tclass);
4467 NEW_PCONST (cfg, ins, mono_type_get_object (cfg->domain, handle));
4468 ins->type = STACK_OBJ;
4469 ins->klass = cmethod->klass;
4472 NEW_PCONST (cfg, ins, handle);
4473 ins->type = STACK_VTYPE;
4474 ins->klass = handle_class;
4490 case CEE_ADD_OVF_UN:
4492 case CEE_MUL_OVF_UN:
4494 case CEE_SUB_OVF_UN:
4499 case CEE_ENDFINALLY:
4500 /* FIXME: check stack state */
4501 MONO_INST_NEW (cfg, ins, *ip);
4502 MONO_ADD_INS (bblock, ins);
4503 ins->cil_code = ip++;
4504 start_new_bblock = 1;
4509 if (*ip == CEE_LEAVE) {
4510 target = ip + 5 + (gint32)read32(ip + 1);
4512 target = ip + 2 + (signed char)(ip [1]);
4515 /* empty the stack */
4516 while (sp != stack_start) {
4517 MONO_INST_NEW (cfg, ins, CEE_POP);
4521 MONO_ADD_INS (bblock, ins);
4524 /* fixme: call fault handler ? */
4526 if ((handlers = mono_find_final_block (cfg, ip, target, MONO_EXCEPTION_CLAUSE_FINALLY))) {
4528 for (tmp = handlers; tmp; tmp = tmp->next) {
4530 link_bblock (cfg, bblock, tblock);
4531 MONO_INST_NEW (cfg, ins, OP_CALL_HANDLER);
4533 ins->inst_target_bb = tblock;
4534 MONO_ADD_INS (bblock, ins);
4536 g_list_free (handlers);
4539 MONO_INST_NEW (cfg, ins, CEE_BR);
4541 MONO_ADD_INS (bblock, ins);
4542 GET_BBLOCK (cfg, bbhash, tblock, target);
4543 link_bblock (cfg, bblock, tblock);
4544 CHECK_BBLOCK (target, ip, tblock);
4545 ins->inst_target_bb = tblock;
4546 start_new_bblock = 1;
4548 if (*ip == CEE_LEAVE)
4557 MONO_INST_NEW (cfg, ins, *ip);
4559 handle_loaded_temps (cfg, bblock, stack_start, sp);
4560 MONO_ADD_INS (bblock, ins);
4561 ins->cil_code = ip++;
4562 ins->inst_i0 = sp [0];
4563 ins->inst_i1 = sp [1];
4571 /* trampoline mono specific opcodes */
4572 case MONO_CUSTOM_PREFIX: {
4574 g_assert (method->wrapper_type != MONO_WRAPPER_NONE);
4578 case CEE_MONO_FUNC1: {
4580 gpointer func = NULL;
4585 case MONO_MARSHAL_CONV_STR_LPWSTR:
4586 func = mono_string_to_utf16;
4588 case MONO_MARSHAL_CONV_LPWSTR_STR:
4589 func = mono_string_from_utf16;
4591 case MONO_MARSHAL_CONV_LPSTR_STR:
4592 func = mono_string_new_wrapper;
4594 case MONO_MARSHAL_CONV_STR_LPTSTR:
4595 case MONO_MARSHAL_CONV_STR_LPSTR:
4596 func = mono_string_to_utf8;
4598 case MONO_MARSHAL_CONV_STR_BSTR:
4599 func = mono_string_to_bstr;
4601 case MONO_MARSHAL_CONV_STR_TBSTR:
4602 case MONO_MARSHAL_CONV_STR_ANSIBSTR:
4603 func = mono_string_to_ansibstr;
4605 case MONO_MARSHAL_CONV_SB_LPSTR:
4606 func = mono_string_builder_to_utf8;
4608 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
4609 func = mono_array_to_savearray;
4611 case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
4612 func = mono_array_to_lparray;
4614 case MONO_MARSHAL_CONV_DEL_FTN:
4615 func = mono_delegate_to_ftnptr;
4617 case MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY:
4618 func = mono_marshal_string_array;
4621 g_warning ("unknown conversion %d\n", ip [2]);
4622 g_assert_not_reached ();
4625 temp = mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4626 NEW_TEMPLOAD (cfg, *sp, temp);
4630 inline_costs += 10 * num_calls++;
4633 case CEE_MONO_PROC2: {
4634 gpointer func = NULL;
4639 case MONO_MARSHAL_CONV_LPSTR_SB:
4640 func = mono_string_utf8_to_builder;
4642 case MONO_MARSHAL_FREE_ARRAY:
4643 func = mono_marshal_free_array;
4646 g_assert_not_reached ();
4649 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4651 inline_costs += 10 * num_calls++;
4654 case CEE_MONO_PROC3: {
4655 gpointer func = NULL;
4660 case MONO_MARSHAL_CONV_STR_BYVALSTR:
4661 func = mono_string_to_byvalstr;
4663 case MONO_MARSHAL_CONV_STR_BYVALWSTR:
4664 func = mono_string_to_byvalwstr;
4667 g_assert_not_reached ();
4670 mono_emit_jit_icall (cfg, bblock, func, sp, ip);
4672 inline_costs += 10 * num_calls++;
4678 mono_emit_jit_icall (cfg, bblock, g_free, sp, ip);
4680 inline_costs += 10 * num_calls++;
4682 case CEE_MONO_LDPTR:
4683 CHECK_STACK_OVF (1);
4684 token = read32 (ip + 2);
4685 NEW_PCONST (cfg, ins, mono_method_get_wrapper_data (method, token));
4689 inline_costs += 10 * num_calls++;
4691 case CEE_MONO_VTADDR:
4694 MONO_INST_NEW (cfg, ins, OP_VTADDR);
4696 ins->type = STACK_MP;
4697 ins->inst_left = *sp;
4701 case CEE_MONO_NEWOBJ: {
4702 MonoInst *iargs [2];
4704 CHECK_STACK_OVF (1);
4705 token = read32 (ip + 2);
4706 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4707 mono_class_init (klass);
4708 NEW_DOMAINCONST (cfg, iargs [0]);
4709 NEW_CLASSCONST (cfg, iargs [1], klass);
4710 temp = mono_emit_jit_icall (cfg, bblock, mono_object_new, iargs, ip);
4711 NEW_TEMPLOAD (cfg, *sp, temp);
4714 inline_costs += 10 * num_calls++;
4717 case CEE_MONO_OBJADDR:
4720 MONO_INST_NEW (cfg, ins, OP_OBJADDR);
4722 ins->type = STACK_MP;
4723 ins->inst_left = *sp;
4727 case CEE_MONO_LDNATIVEOBJ:
4729 token = read32 (ip + 2);
4730 klass = mono_method_get_wrapper_data (method, token);
4731 g_assert (klass->valuetype);
4732 mono_class_init (klass);
4733 NEW_INDLOAD (cfg, ins, sp [-1], &klass->byval_arg);
4737 case CEE_MONO_RETOBJ:
4738 g_assert (cfg->ret);
4739 g_assert (method->signature->pinvoke);
4743 token = read32 (ip + 2);
4744 klass = (MonoClass *)mono_method_get_wrapper_data (method, token);
4746 NEW_RETLOADA (cfg, ins);
4747 handle_stobj (cfg, bblock, ins, *sp, ip, klass, FALSE, TRUE);
4749 if (sp != stack_start)
4752 MONO_INST_NEW (cfg, ins, CEE_BR);
4754 ins->inst_target_bb = end_bblock;
4755 MONO_ADD_INS (bblock, ins);
4756 link_bblock (cfg, bblock, end_bblock);
4757 start_new_bblock = 1;
4761 g_error ("opcode 0x%02x 0x%02x not handled", MONO_CUSTOM_PREFIX, ip [1]);
4769 /* somewhat similar to LDTOKEN */
4770 MonoInst *addr, *vtvar;
4771 CHECK_STACK_OVF (1);
4772 vtvar = mono_compile_create_var (cfg, &mono_defaults.argumenthandle_class->byval_arg, OP_LOCAL);
4774 NEW_TEMPLOADA (cfg, addr, vtvar->inst_c0);
4775 addr->cil_code = ip;
4776 MONO_INST_NEW (cfg, ins, OP_ARGLIST);
4778 ins->inst_left = addr;
4779 MONO_ADD_INS (bblock, ins);
4780 NEW_TEMPLOAD (cfg, ins, vtvar->inst_c0);
4793 MONO_INST_NEW (cfg, cmp, 256 + ip [1]);
4794 MONO_INST_NEW (cfg, ins, cmp->opcode);
4796 cmp->inst_i0 = sp [0];
4797 cmp->inst_i1 = sp [1];
4801 cmp->opcode = OP_COMPARE;
4803 ins->type = STACK_I4;
4813 CHECK_STACK_OVF (1);
4814 n = read32 (ip + 2);
4815 if (method->wrapper_type != MONO_WRAPPER_NONE)
4816 cmethod = mono_method_get_wrapper_data (method, n);
4818 cmethod = mono_get_method (image, n, NULL);
4821 * We can't do this in mono_ldftn, since it is used in
4822 * the synchronized wrapper, leading to an infinite loop.
4824 if (cmethod->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
4825 cmethod = mono_marshal_get_synchronized_wrapper (cmethod);
4828 mono_class_init (cmethod->klass);
4829 handle_loaded_temps (cfg, bblock, stack_start, sp);
4831 NEW_METHODCONST (cfg, argconst, cmethod);
4832 temp = mono_emit_jit_icall (cfg, bblock, mono_ldftn, &argconst, ip);
4833 NEW_TEMPLOAD (cfg, *sp, temp);
4837 inline_costs += 10 * num_calls++;
4840 case CEE_LDVIRTFTN: {
4845 n = read32 (ip + 2);
4846 if (method->wrapper_type != MONO_WRAPPER_NONE)
4847 cmethod = mono_method_get_wrapper_data (method, n);
4849 cmethod = mono_get_method (image, n, NULL);
4851 mono_class_init (cmethod->klass);
4852 handle_loaded_temps (cfg, bblock, stack_start, sp);
4856 NEW_METHODCONST (cfg, args [1], cmethod);
4857 temp = mono_emit_jit_icall (cfg, bblock, mono_ldvirtfn, args, ip);
4858 NEW_TEMPLOAD (cfg, *sp, temp);
4862 inline_costs += 10 * num_calls++;
4866 CHECK_STACK_OVF (1);
4867 NEW_ARGLOAD (cfg, ins, read16 (ip + 2));
4873 CHECK_STACK_OVF (1);
4874 NEW_ARGLOADA (cfg, ins, read16 (ip + 2));
4882 handle_loaded_temps (cfg, bblock, stack_start, sp);
4883 n = read16 (ip + 2);
4884 NEW_ARGSTORE (cfg, ins, n, *sp);
4886 if (ins->opcode == CEE_STOBJ) {
4887 NEW_ARGLOADA (cfg, ins, n);
4888 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4890 MONO_ADD_INS (bblock, ins);
4894 CHECK_STACK_OVF (1);
4895 NEW_LOCLOAD (cfg, ins, read16 (ip + 2));
4901 CHECK_STACK_OVF (1);
4902 NEW_LOCLOADA (cfg, ins, read16 (ip + 2));
4910 n = read16 (ip + 2);
4911 handle_loaded_temps (cfg, bblock, stack_start, sp);
4912 NEW_LOCSTORE (cfg, ins, n, *sp);
4914 if (ins->opcode == CEE_STOBJ) {
4915 NEW_LOCLOADA (cfg, ins, n);
4916 handle_stobj (cfg, bblock, ins, *sp, ip, ins->klass, FALSE, FALSE);
4918 MONO_ADD_INS (bblock, ins);
4925 if (sp != stack_start)
4927 MONO_INST_NEW (cfg, ins, 256 + ip [1]);
4928 ins->inst_left = *sp;
4931 if (header->init_locals)
4932 ins->flags |= MONO_INST_INIT;
4936 /* FIXME: set init flag if locals init is set in this method */
4938 case CEE_ENDFILTER: {
4939 MonoExceptionClause *clause, *nearest;
4940 int cc, nearest_num;
4944 if ((sp != stack_start) || (sp [0]->type != STACK_I4))
4946 MONO_INST_NEW (cfg, ins, OP_ENDFILTER);
4947 ins->inst_left = *sp;
4949 MONO_ADD_INS (bblock, ins);
4950 start_new_bblock = 1;
4954 for (cc = 0; cc < header->num_clauses; ++cc) {
4955 clause = &header->clauses [cc];
4956 if ((clause->flags & MONO_EXCEPTION_CLAUSE_FILTER) &&
4957 (!nearest || (clause->token_or_filter > nearest->token_or_filter))) {
4963 filter_lengths [nearest_num] = (ip - header->code) - nearest->token_or_filter;
4967 case CEE_UNALIGNED_:
4968 ins_flag |= MONO_INST_UNALIGNED;
4972 ins_flag |= MONO_INST_VOLATILE;
4976 ins_flag |= MONO_INST_TAILCALL;
4982 token = read32 (ip + 2);
4983 if (method->wrapper_type != MONO_WRAPPER_NONE)
4984 klass = mono_method_get_wrapper_data (method, token);
4986 klass = mono_class_get (image, token);
4987 if (klass->byval_arg.type == MONO_TYPE_VAR)
4988 klass = TYPE_PARAM_TO_CLASS (klass->byval_arg.data.type_param);
4989 if (MONO_TYPE_IS_REFERENCE (&klass->byval_arg)) {
4990 MonoInst *store, *load;
4991 NEW_PCONST (cfg, load, NULL);
4992 load->cil_code = ip;
4993 load->type = STACK_OBJ;
4994 MONO_INST_NEW (cfg, store, CEE_STIND_REF);
4995 store->cil_code = ip;
4996 handle_loaded_temps (cfg, bblock, stack_start, sp);
4997 MONO_ADD_INS (bblock, store);
4998 store->inst_i0 = sp [0];
4999 store->inst_i1 = load;
5002 handle_initobj (cfg, bblock, *sp, NULL, klass, stack_start, sp);
5007 case CEE_CONSTRAINED_:
5008 /* FIXME: implement */
5009 token = read32 (ip + 2);
5014 MonoInst *iargs [3];
5020 handle_loaded_temps (cfg, bblock, stack_start, sp);
5021 if (ip [1] == CEE_CPBLK) {
5022 mono_emit_jit_icall (cfg, bblock, helper_memcpy, iargs, ip);
5024 mono_emit_jit_icall (cfg, bblock, helper_memset, iargs, ip);
5032 ins_flag |= MONO_INST_NOTYPECHECK;
5034 ins_flag |= MONO_INST_NORANGECHECK;
5035 /* we ignore the no-nullcheck for now since we
5036 * really do it explicitly only when doing callvirt->call
5042 /* FIXME: check we are in a catch handler */
5043 NEW_TEMPLOAD (cfg, load, cfg->exvar->inst_c0);
5044 load->cil_code = ip;
5045 MONO_INST_NEW (cfg, ins, CEE_THROW);
5046 ins->inst_left = load;
5048 MONO_ADD_INS (bblock, ins);
5050 start_new_bblock = 1;
5055 CHECK_STACK_OVF (1);
5056 token = read32 (ip + 2);
5057 /* FIXXME: handle generics. */
5058 if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
5059 MonoType *type = mono_type_create_from_typespec (image, token);
5060 token = mono_type_size (type, &align);
5061 mono_metadata_free_type (type);
5063 MonoClass *szclass = mono_class_get (image, token);
5064 mono_class_init (szclass);
5065 token = mono_class_value_size (szclass, &align);
5067 NEW_ICONST (cfg, ins, token);
5072 case CEE_REFANYTYPE:
5073 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5079 g_error ("opcode 0xfe 0x%02x not handled", ip [1]);
5084 g_error ("opcode 0x%02x not handled", *ip);
5087 if (start_new_bblock != 1)
5090 bblock->cil_length = ip - bblock->cil_code;
5091 bblock->next_bb = end_bblock;
5092 link_bblock (cfg, bblock, end_bblock);
5094 if (cfg->method == method && cfg->domainvar) {
5098 MONO_INST_NEW_CALL (cfg, call, CEE_CALL);
5099 call->signature = helper_sig_domain_get;
5100 call->inst.type = STACK_PTR;
5101 call->fptr = mono_domain_get;
5102 NEW_TEMPSTORE (cfg, store, cfg->domainvar->inst_c0, (MonoInst*)call);
5104 MONO_ADD_INS (init_localsbb, store);
5107 if (header->init_locals) {
5109 for (i = 0; i < header->num_locals; ++i) {
5110 int t = header->locals [i]->type;
5111 if (t == MONO_TYPE_VALUETYPE && header->locals [i]->data.klass->enumtype)
5112 t = header->locals [i]->data.klass->enum_basetype->type;
5113 /* FIXME: use initobj for valuetypes, handle pointers, long, float. */
5114 if (t >= MONO_TYPE_BOOLEAN && t <= MONO_TYPE_U4) {
5115 NEW_ICONST (cfg, ins, 0);
5116 NEW_LOCSTORE (cfg, store, i, ins);
5117 MONO_ADD_INS (init_localsbb, store);
5118 } else if (t == MONO_TYPE_I8 || t == MONO_TYPE_U8) {
5119 MONO_INST_NEW (cfg, ins, OP_I8CONST);
5120 ins->type = STACK_I8;
5122 NEW_LOCSTORE (cfg, store, i, ins);
5123 MONO_ADD_INS (init_localsbb, store);
5124 } else if (t == MONO_TYPE_R4 || t == MONO_TYPE_R8) {
5125 MONO_INST_NEW (cfg, ins, OP_R8CONST);
5126 ins->type = STACK_R8;
5127 ins->inst_p0 = (void*)&r8_0;
5128 NEW_LOCSTORE (cfg, store, i, ins);
5129 MONO_ADD_INS (init_localsbb, store);
5130 } else if (t == MONO_TYPE_VALUETYPE) {
5131 NEW_LOCLOADA (cfg, ins, i);
5132 handle_initobj (cfg, init_localsbb, ins, NULL, mono_class_from_mono_type (header->locals [i]), NULL, NULL);
5135 NEW_PCONST (cfg, ins, NULL);
5136 NEW_LOCSTORE (cfg, store, i, ins);
5137 MONO_ADD_INS (init_localsbb, store);
5143 /* resolve backward branches in the middle of an existing basic block */
5144 for (tmp = bb_recheck; tmp; tmp = tmp->next) {
5146 /*g_print ("need recheck in %s at IL_%04x\n", method->name, bblock->cil_code - header->code);*/
5147 tblock = find_previous (bbhash, start_bblock, bblock->cil_code);
5148 if (tblock != start_bblock) {
5150 split_bblock (cfg, tblock, bblock);
5151 l = bblock->cil_code - header->code;
5152 bblock->cil_length = tblock->cil_length - l;
5153 tblock->cil_length = l;
5155 g_print ("recheck failed.\n");
5159 /* we compute regions here, because the length of filter clauses is not known in advance.
5160 * It is computed in the CEE_ENDFILTER case in the above switch statement*/
5161 if (cfg->method == method) {
5163 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5164 bb->region = mono_find_block_region (cfg, bb->real_offset, filter_lengths);
5165 if (cfg->verbose_level > 2)
5166 g_print ("REGION BB%d IL_%04x ID_%08X\n", bb->block_num, bb->real_offset, bb->region);
5169 g_hash_table_destroy (bbhash);
5172 dont_inline = g_list_remove (dont_inline, method);
5173 return inline_costs;
5176 if (cfg->method != method)
5177 g_hash_table_destroy (bbhash);
5178 dont_inline = g_list_remove (dont_inline, method);
5182 if (cfg->method != method)
5183 g_hash_table_destroy (bbhash);
5184 g_error ("Invalid IL code at IL%04x in %s: %s\n", ip - header->code,
5185 mono_method_full_name (method, TRUE), mono_disasm_code_one (NULL, method, ip, NULL));
5186 dont_inline = g_list_remove (dont_inline, method);
5191 mono_print_tree (MonoInst *tree) {
5197 arity = mono_burg_arity [tree->opcode];
5199 printf (" %s%s", arity?"(":"", mono_inst_name (tree->opcode));
5201 switch (tree->opcode) {
5203 printf ("[%d]", tree->inst_c0);
5206 printf ("[%lld]", tree->inst_l);
5209 printf ("[%f]", *(double*)tree->inst_p0);
5212 printf ("[%f]", *(float*)tree->inst_p0);
5216 printf ("[%d]", tree->inst_c0);
5219 printf ("[0x%x(%s)]", tree->inst_offset, mono_arch_regname (tree->inst_basereg));
5222 printf ("[%s]", mono_arch_regname (tree->dreg));
5225 printf ("[%s]", tree->inst_newa_class->name);
5226 mono_print_tree (tree->inst_newa_len);
5237 case OP_VOIDCALLVIRT: {
5238 MonoCallInst *call = (MonoCallInst*)tree;
5240 printf ("[%s]", call->method->name);
5245 printf ("[%d (", tree->inst_c0);
5246 for (i = 0; i < tree->inst_phi_args [0]; i++) {
5249 printf ("%d", tree->inst_phi_args [i + 1]);
5261 printf ("[B%d]", tree->inst_target_bb->block_num);
5271 case OP_VOIDCALL_REG:
5272 mono_print_tree (tree->inst_left);
5284 printf ("[B%dB%d]", tree->inst_true_bb->block_num, tree->inst_false_bb->block_num);
5285 mono_print_tree (tree->inst_left);
5289 mono_print_tree (tree->inst_left);
5291 mono_print_tree (tree->inst_right);
5301 create_helper_signature (void)
5303 /* FIXME: set call conv */
5304 /* MonoArray * mono_array_new (MonoDomain *domain, MonoClass *klass, gint32 len) */
5305 helper_sig_newarr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5306 helper_sig_newarr->params [0] = helper_sig_newarr->params [1] = &mono_defaults.int_class->byval_arg;
5307 helper_sig_newarr->ret = &mono_defaults.object_class->byval_arg;
5308 helper_sig_newarr->params [2] = &mono_defaults.int32_class->byval_arg;
5309 helper_sig_newarr->pinvoke = 1;
5311 /* MonoArray * mono_array_new_specific (MonoVTable *vtable, guint32 len) */
5312 helper_sig_newarr_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5313 helper_sig_newarr_specific->params [0] = &mono_defaults.int_class->byval_arg;
5314 helper_sig_newarr_specific->params [1] = &mono_defaults.int32_class->byval_arg;
5315 helper_sig_newarr_specific->ret = &mono_defaults.object_class->byval_arg;
5316 helper_sig_newarr_specific->pinvoke = 1;
5318 /* MonoObject * mono_object_new (MonoDomain *domain, MonoClass *klass) */
5319 helper_sig_object_new = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5320 helper_sig_object_new->params [0] = helper_sig_object_new->params [1] = &mono_defaults.int_class->byval_arg;
5321 helper_sig_object_new->ret = &mono_defaults.object_class->byval_arg;
5322 helper_sig_object_new->pinvoke = 1;
5324 /* MonoObject * mono_object_new_specific (MonoVTable *vtable) */
5325 helper_sig_object_new_specific = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5326 helper_sig_object_new_specific->params [0] = &mono_defaults.int_class->byval_arg;
5327 helper_sig_object_new_specific->ret = &mono_defaults.object_class->byval_arg;
5328 helper_sig_object_new_specific->pinvoke = 1;
5330 /* void* mono_method_compile (MonoMethod*) */
5331 helper_sig_compile = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5332 helper_sig_compile->params [0] = helper_sig_compile->ret = &mono_defaults.int_class->byval_arg;
5333 helper_sig_compile->pinvoke = 1;
5335 /* void* mono_ldvirtfn (MonoObject *, MonoMethod*) */
5336 helper_sig_compile_virt = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5337 helper_sig_compile_virt->params [0] = &mono_defaults.object_class->byval_arg;
5338 helper_sig_compile_virt->params [1] = helper_sig_compile_virt->ret = &mono_defaults.int_class->byval_arg;
5339 helper_sig_compile_virt->pinvoke = 1;
5341 /* MonoString* mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 str_index) */
5342 helper_sig_ldstr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5343 helper_sig_ldstr->params [0] = helper_sig_ldstr->params [1] = &mono_defaults.int_class->byval_arg;
5344 helper_sig_ldstr->params [2] = &mono_defaults.int32_class->byval_arg;
5345 helper_sig_ldstr->ret = &mono_defaults.object_class->byval_arg;
5346 helper_sig_ldstr->pinvoke = 1;
5348 /* MonoDomain *mono_domain_get (void) */
5349 helper_sig_domain_get = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5350 helper_sig_domain_get->ret = &mono_defaults.int_class->byval_arg;
5351 helper_sig_domain_get->pinvoke = 1;
5353 /* void* stelem_ref (MonoArray *, int index, MonoObject *) */
5354 helper_sig_stelem_ref = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5355 helper_sig_stelem_ref->params [0] = &mono_defaults.array_class->byval_arg;
5356 helper_sig_stelem_ref->params [1] = &mono_defaults.int32_class->byval_arg;
5357 helper_sig_stelem_ref->params [2] = &mono_defaults.object_class->byval_arg;
5358 helper_sig_stelem_ref->ret = &mono_defaults.void_class->byval_arg;
5359 helper_sig_stelem_ref->pinvoke = 1;
5361 /* long amethod (long, long) */
5362 helper_sig_long_long_long = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5363 helper_sig_long_long_long->params [0] = helper_sig_long_long_long->params [1] =
5364 &mono_defaults.int64_class->byval_arg;
5365 helper_sig_long_long_long->ret = &mono_defaults.int64_class->byval_arg;
5366 helper_sig_long_long_long->pinvoke = 1;
5368 /* object amethod (intptr) */
5369 helper_sig_obj_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5370 helper_sig_obj_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5371 helper_sig_obj_ptr->ret = &mono_defaults.object_class->byval_arg;
5372 helper_sig_obj_ptr->pinvoke = 1;
5374 /* void amethod (intptr) */
5375 helper_sig_void_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5376 helper_sig_void_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5377 helper_sig_void_ptr->ret = &mono_defaults.void_class->byval_arg;
5378 helper_sig_void_ptr->pinvoke = 1;
5380 /* void amethod (MonoObject *obj) */
5381 helper_sig_void_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5382 helper_sig_void_obj->params [0] = &mono_defaults.object_class->byval_arg;
5383 helper_sig_void_obj->ret = &mono_defaults.void_class->byval_arg;
5384 helper_sig_void_obj->pinvoke = 1;
5386 /* intptr amethod (void) */
5387 helper_sig_ptr_void = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
5388 helper_sig_ptr_void->ret = &mono_defaults.int_class->byval_arg;
5389 helper_sig_ptr_void->pinvoke = 1;
5391 /* void amethod (intptr, intptr) */
5392 helper_sig_void_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5393 helper_sig_void_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5394 helper_sig_void_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5395 helper_sig_void_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5396 helper_sig_void_ptr_ptr->pinvoke = 1;
5398 /* void amethod (intptr, intptr, intptr) */
5399 helper_sig_void_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5400 helper_sig_void_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5401 helper_sig_void_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5402 helper_sig_void_ptr_ptr_ptr->params [2] = &mono_defaults.int_class->byval_arg;
5403 helper_sig_void_ptr_ptr_ptr->ret = &mono_defaults.void_class->byval_arg;
5404 helper_sig_void_ptr_ptr_ptr->pinvoke = 1;
5406 /* intptr amethod (intptr, intptr) */
5407 helper_sig_ptr_ptr_ptr = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5408 helper_sig_ptr_ptr_ptr->params [0] = &mono_defaults.int_class->byval_arg;
5409 helper_sig_ptr_ptr_ptr->params [1] = &mono_defaults.int_class->byval_arg;
5410 helper_sig_ptr_ptr_ptr->ret = &mono_defaults.int_class->byval_arg;
5411 helper_sig_ptr_ptr_ptr->pinvoke = 1;
5413 /* IntPtr amethod (object) */
5414 helper_sig_ptr_obj = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5415 helper_sig_ptr_obj->params [0] = &mono_defaults.object_class->byval_arg;
5416 helper_sig_ptr_obj->ret = &mono_defaults.int_class->byval_arg;
5417 helper_sig_ptr_obj->pinvoke = 1;
5419 /* IntPtr amethod (int) */
5420 helper_sig_ptr_int = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5421 helper_sig_ptr_int->params [0] = &mono_defaults.int32_class->byval_arg;
5422 helper_sig_ptr_int->ret = &mono_defaults.int_class->byval_arg;
5423 helper_sig_ptr_int->pinvoke = 1;
5425 /* long amethod (long, guint32) */
5426 helper_sig_long_long_int = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5427 helper_sig_long_long_int->params [0] = &mono_defaults.int64_class->byval_arg;
5428 helper_sig_long_long_int->params [1] = &mono_defaults.int32_class->byval_arg;
5429 helper_sig_long_long_int->ret = &mono_defaults.int64_class->byval_arg;
5430 helper_sig_long_long_int->pinvoke = 1;
5432 /* ulong amethod (double) */
5433 helper_sig_ulong_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5434 helper_sig_ulong_double->params [0] = &mono_defaults.double_class->byval_arg;
5435 helper_sig_ulong_double->ret = &mono_defaults.uint64_class->byval_arg;
5436 helper_sig_ulong_double->pinvoke = 1;
5438 /* long amethod (double) */
5439 helper_sig_long_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5440 helper_sig_long_double->params [0] = &mono_defaults.double_class->byval_arg;
5441 helper_sig_long_double->ret = &mono_defaults.int64_class->byval_arg;
5442 helper_sig_long_double->pinvoke = 1;
5444 /* uint amethod (double) */
5445 helper_sig_uint_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5446 helper_sig_uint_double->params [0] = &mono_defaults.double_class->byval_arg;
5447 helper_sig_uint_double->ret = &mono_defaults.uint32_class->byval_arg;
5448 helper_sig_uint_double->pinvoke = 1;
5450 /* int amethod (double) */
5451 helper_sig_int_double = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5452 helper_sig_int_double->params [0] = &mono_defaults.double_class->byval_arg;
5453 helper_sig_int_double->ret = &mono_defaults.int32_class->byval_arg;
5454 helper_sig_int_double->pinvoke = 1;
5456 /* void initobj (intptr, int size) */
5457 helper_sig_initobj = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
5458 helper_sig_initobj->params [0] = &mono_defaults.int_class->byval_arg;
5459 helper_sig_initobj->params [1] = &mono_defaults.int32_class->byval_arg;
5460 helper_sig_initobj->ret = &mono_defaults.void_class->byval_arg;
5461 helper_sig_initobj->pinvoke = 1;
5463 /* void memcpy (intptr, intptr, int size) */
5464 helper_sig_memcpy = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5465 helper_sig_memcpy->params [0] = &mono_defaults.int_class->byval_arg;
5466 helper_sig_memcpy->params [1] = &mono_defaults.int_class->byval_arg;
5467 helper_sig_memcpy->params [2] = &mono_defaults.int32_class->byval_arg;
5468 helper_sig_memcpy->ret = &mono_defaults.void_class->byval_arg;
5469 helper_sig_memcpy->pinvoke = 1;
5471 /* void memset (intptr, int val, int size) */
5472 helper_sig_memset = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5473 helper_sig_memset->params [0] = &mono_defaults.int_class->byval_arg;
5474 helper_sig_memset->params [1] = &mono_defaults.int32_class->byval_arg;
5475 helper_sig_memset->params [2] = &mono_defaults.int32_class->byval_arg;
5476 helper_sig_memset->ret = &mono_defaults.void_class->byval_arg;
5477 helper_sig_memset->pinvoke = 1;
5480 static GHashTable *jit_icall_hash_name = NULL;
5481 static GHashTable *jit_icall_hash_addr = NULL;
5484 mono_find_jit_icall_by_name (const char *name)
5486 g_assert (jit_icall_hash_name);
5488 //printf ("lookup addr %s %p\n", name, g_hash_table_lookup (jit_icall_hash_name, name));
5489 return g_hash_table_lookup (jit_icall_hash_name, name);
5493 mono_find_jit_icall_by_addr (gconstpointer addr)
5495 g_assert (jit_icall_hash_addr);
5497 return g_hash_table_lookup (jit_icall_hash_addr, (gpointer)addr);
5501 mono_icall_get_wrapper (MonoJitICallInfo* callinfo)
5504 MonoMethod *wrapper;
5506 if (callinfo->wrapper)
5507 return callinfo->wrapper;
5508 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
5509 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func);
5510 callinfo->wrapper = mono_jit_compile_method (wrapper);
5512 return callinfo->wrapper;
5516 mono_register_jit_icall (gconstpointer func, const char *name, MonoMethodSignature *sig, gboolean is_save)
5518 MonoJitICallInfo *info;
5523 if (!jit_icall_hash_name) {
5524 jit_icall_hash_name = g_hash_table_new (g_str_hash, g_str_equal);
5525 jit_icall_hash_addr = g_hash_table_new (NULL, NULL);
5528 if (g_hash_table_lookup (jit_icall_hash_name, name)) {
5529 g_warning ("jit icall already defined \"%s\"\n", name);
5530 g_assert_not_reached ();
5533 info = g_new (MonoJitICallInfo, 1);
5540 #ifdef MONO_USE_EXC_TABLES
5541 || mono_arch_has_unwind_info (func)
5544 info->wrapper = func;
5546 info->wrapper = NULL;
5547 mono_icall_get_wrapper (info);
5550 g_hash_table_insert (jit_icall_hash_name, (gpointer)info->name, info);
5551 g_hash_table_insert (jit_icall_hash_addr, (gpointer)func, info);
5552 if (func != info->wrapper)
5553 g_hash_table_insert (jit_icall_hash_addr, (gpointer)info->wrapper, info);
5558 static GHashTable *emul_opcode_hash = NULL;
5560 static MonoJitICallInfo *
5561 mono_find_jit_opcode_emulation (int opcode)
5563 if (emul_opcode_hash)
5564 return g_hash_table_lookup (emul_opcode_hash, (gpointer)opcode);
5570 mono_register_opcode_emulation (int opcode, const char *name, MonoMethodSignature *sig, gpointer func, gboolean no_throw)
5572 MonoJitICallInfo *info;
5574 if (!emul_opcode_hash)
5575 emul_opcode_hash = g_hash_table_new (NULL, NULL);
5577 g_assert (!sig->hasthis);
5578 g_assert (sig->param_count < 3);
5580 info = mono_register_jit_icall (func, name, sig, no_throw);
5582 g_hash_table_insert (emul_opcode_hash, (gpointer)opcode, info);
5586 decompose_foreach (MonoInst *tree, gpointer data)
5588 static MonoJitICallInfo *newarr_info = NULL;
5589 static MonoJitICallInfo *newarr_specific_info = NULL;
5590 MonoJitICallInfo *info;
5592 switch (tree->opcode) {
5594 MonoCompile *cfg = data;
5595 MonoInst *iargs [3];
5598 newarr_info = mono_find_jit_icall_by_addr (mono_array_new);
5599 g_assert (newarr_info);
5600 newarr_specific_info = mono_find_jit_icall_by_addr (mono_array_new_specific);
5601 g_assert (newarr_specific_info);
5604 if ((cfg->opt & MONO_OPT_SHARED) || mono_compile_aot) {
5605 NEW_DOMAINCONST (cfg, iargs [0]);
5606 NEW_CLASSCONST (cfg, iargs [1], tree->inst_newa_class);
5607 iargs [2] = tree->inst_newa_len;
5612 MonoVTable *vtable = mono_class_vtable (cfg->domain, mono_array_class_get (tree->inst_newa_class, 1));
5614 NEW_PCONST (cfg, iargs [0], vtable);
5615 iargs [1] = tree->inst_newa_len;
5617 info = newarr_specific_info;
5620 mono_emulate_opcode (cfg, tree, iargs, info);
5630 mono_inst_foreach (MonoInst *tree, MonoInstFunc func, gpointer data) {
5632 switch (mono_burg_arity [tree->opcode]) {
5635 mono_inst_foreach (tree->inst_left, func, data);
5638 mono_inst_foreach (tree->inst_left, func, data);
5639 mono_inst_foreach (tree->inst_right, func, data);
5642 g_assert_not_reached ();
5649 mono_print_bb_code (MonoBasicBlock *bb) {
5651 MonoInst *c = bb->code;
5653 mono_print_tree (c);
5662 print_dfn (MonoCompile *cfg) {
5667 g_print ("IR code for method %s\n", mono_method_full_name (cfg->method, TRUE));
5669 for (i = 0; i < cfg->num_bblocks; ++i) {
5670 bb = cfg->bblocks [i];
5672 char* code1, *code2;
5673 code1 = mono_disasm_code_one (NULL, cfg->method, bb->cil_code, NULL);
5674 if (bb->last_ins->cil_code)
5675 code2 = mono_disasm_code_one (NULL, cfg->method, bb->last_ins->cil_code, NULL);
5677 code2 = g_strdup ("");
5679 code1 [strlen (code1) - 1] = 0;
5680 code = g_strdup_printf ("%s -> %s", code1, code2);
5684 code = g_strdup ("\n");
5685 g_print ("\nBB%d DFN%d (len: %d): %s", bb->block_num, i, bb->cil_length, code);
5687 MonoInst *c = bb->code;
5689 mono_print_tree (c);
5697 g_print ("\tprev:");
5698 for (j = 0; j < bb->in_count; ++j) {
5699 g_print (" BB%d", bb->in_bb [j]->block_num);
5701 g_print ("\t\tsucc:");
5702 for (j = 0; j < bb->out_count; ++j) {
5703 g_print (" BB%d", bb->out_bb [j]->block_num);
5705 g_print ("\n\tidom: BB%d\n", bb->idom? bb->idom->block_num: -1);
5708 g_assert (mono_bitset_test_fast (bb->dominators, bb->idom->dfn));
5711 mono_blockset_print (cfg, bb->dominators, "\tdominators", bb->idom? bb->idom->dfn: -1);
5713 mono_blockset_print (cfg, bb->dfrontier, "\tdfrontier", -1);
5721 * returns the offset used by spillvar. It allocates a new
5722 * spill variable if necessary.
5725 mono_spillvar_offset (MonoCompile *cfg, int spillvar)
5727 MonoSpillInfo **si, *info;
5730 si = &cfg->spill_info;
5732 while (i <= spillvar) {
5735 *si = info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo));
5737 cfg->stack_offset -= sizeof (gpointer);
5738 info->offset = cfg->stack_offset;
5742 return (*si)->offset;
5748 g_assert_not_reached ();
5753 mono_bblock_add_inst (MonoBasicBlock *bb, MonoInst *inst)
5757 g_assert (bb->code);
5758 bb->last_ins->next = inst;
5759 bb->last_ins = inst;
5761 bb->last_ins = bb->code = inst;
5766 mono_destroy_compile (MonoCompile *cfg)
5768 //mono_mempool_stats (cfg->mempool);
5769 g_hash_table_destroy (cfg->bb_hash);
5771 mono_regstate_free (cfg->rs);
5772 mono_mempool_destroy (cfg->mempool);
5773 g_list_free (cfg->ldstr_list);
5775 g_free (cfg->varinfo);
5781 mono_get_lmf_addr (void)
5783 MonoJitTlsData *jit_tls;
5785 if ((jit_tls = TlsGetValue (mono_jit_tls_id)))
5786 return &jit_tls->lmf;
5788 g_assert_not_reached ();
5793 * mono_thread_abort:
5794 * @obj: exception object
5796 * abort the thread, print exception information and stack trace
5799 mono_thread_abort (MonoObject *obj)
5801 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
5803 /* handle_remove should be eventually called for this thread, too
5810 setup_jit_tls_data (gpointer stack_start, gpointer abort_func)
5812 MonoJitTlsData *jit_tls;
5816 jit_tls = g_new0 (MonoJitTlsData, 1);
5818 TlsSetValue (mono_jit_tls_id, jit_tls);
5820 jit_tls->abort_func = abort_func;
5821 jit_tls->end_of_stack = stack_start;
5823 lmf = g_new0 (MonoLMF, 1);
5826 jit_tls->lmf = jit_tls->first_lmf = lmf;
5828 thread = mono_thread_current ();
5830 thread->jit_data = jit_tls;
5834 mono_thread_start_cb (guint32 tid, gpointer stack_start, gpointer func)
5836 setup_jit_tls_data (stack_start, mono_thread_abort);
5839 void (*mono_thread_attach_aborted_cb ) (MonoObject *obj) = NULL;
5842 mono_thread_abort_dummy (MonoObject *obj)
5844 if (mono_thread_attach_aborted_cb)
5845 mono_thread_attach_aborted_cb (obj);
5847 mono_thread_abort (obj);
5851 mono_thread_attach_cb (guint32 tid, gpointer stack_start)
5853 setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
5857 mini_thread_cleanup (MonoThread *thread)
5859 MonoJitTlsData *jit_tls = thread->jit_data;
5862 g_free (jit_tls->first_lmf);
5864 thread->jit_data = NULL;
5869 mono_add_patch_info (MonoCompile *cfg, int ip, MonoJumpInfoType type, gconstpointer target)
5871 MonoJumpInfo *ji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
5875 ji->data.target = target;
5876 ji->next = cfg->patch_info;
5878 cfg->patch_info = ji;
5882 mono_remove_patch_info (MonoCompile *cfg, int ip)
5884 MonoJumpInfo **ji = &cfg->patch_info;
5887 if ((*ji)->ip.i == ip)
5890 ji = &((*ji)->next);
5895 dec_foreach (MonoInst *tree, MonoCompile *cfg) {
5896 MonoJitICallInfo *info;
5898 switch (mono_burg_arity [tree->opcode]) {
5901 dec_foreach (tree->inst_left, cfg);
5903 if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5904 MonoInst *iargs [2];
5906 iargs [0] = tree->inst_left;
5908 mono_emulate_opcode (cfg, tree, iargs, info);
5914 if (tree->opcode == OP_LMUL
5915 && (cfg->opt & MONO_OPT_INTRINS)
5916 && (tree->inst_left->opcode == CEE_CONV_I8
5917 || tree->inst_left->opcode == CEE_CONV_U8)
5918 && tree->inst_left->inst_left->type == STACK_I4
5919 && (tree->inst_right->opcode == CEE_CONV_I8
5920 || tree->inst_right->opcode == CEE_CONV_U8)
5921 && tree->inst_right->inst_left->type == STACK_I4) {
5922 tree->opcode = (tree->inst_left->opcode == CEE_CONV_I8 ? OP_BIGMUL: OP_BIGMUL_UN);
5923 tree->inst_left = tree->inst_left->inst_left;
5924 tree->inst_right = tree->inst_right->inst_left;
5925 dec_foreach (tree, cfg);
5926 } else if ((info = mono_find_jit_opcode_emulation (tree->opcode))) {
5927 MonoInst *iargs [2];
5929 iargs [0] = tree->inst_i0;
5930 iargs [1] = tree->inst_i1;
5932 mono_emulate_opcode (cfg, tree, iargs, info);
5934 dec_foreach (iargs [0], cfg);
5935 dec_foreach (iargs [1], cfg);
5938 dec_foreach (tree->inst_left, cfg);
5939 dec_foreach (tree->inst_right, cfg);
5943 g_assert_not_reached ();
5945 decompose_foreach (tree, cfg);
5949 decompose_pass (MonoCompile *cfg) {
5952 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
5955 cfg->prev_ins = NULL;
5956 for (tree = cfg->cbb->code; tree; tree = tree->next) {
5957 dec_foreach (tree, cfg);
5958 cfg->prev_ins = tree;
5964 nullify_basic_block (MonoBasicBlock *bb)
5971 bb->code = bb->last_ins = NULL;
5972 bb->cil_code = NULL;
5976 replace_out_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
5980 for (i = 0; i < bb->out_count; i++) {
5981 MonoBasicBlock *ob = bb->out_bb [i];
5984 if (bb->out_count > 1) {
5985 bb->out_bb [i] = bb->out_bb [bb->out_count - 1];
5989 bb->out_bb [i] = repl;
5996 replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6000 for (i = 0; i < bb->in_count; i++) {
6001 MonoBasicBlock *ib = bb->in_bb [i];
6004 if (bb->in_count > 1) {
6005 bb->in_bb [i] = bb->in_bb [bb->in_count - 1];
6009 bb->in_bb [i] = repl;
6016 replace_basic_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl)
6020 for (i = 0; i < bb->out_count; i++) {
6021 MonoBasicBlock *ob = bb->out_bb [i];
6022 for (j = 0; j < ob->in_count; j++) {
6023 if (ob->in_bb [j] == orig) {
6024 ob->in_bb [j] = repl;
6033 merge_basic_blocks (MonoBasicBlock *bb, MonoBasicBlock *bbn)
6035 bb->out_count = bbn->out_count;
6036 bb->out_bb = bbn->out_bb;
6038 replace_basic_block (bb, bbn, bb);
6042 bb->last_ins->next = bbn->code;
6043 bb->last_ins = bbn->last_ins;
6046 bb->code = bbn->code;
6047 bb->last_ins = bbn->last_ins;
6049 bb->next_bb = bbn->next_bb;
6050 nullify_basic_block (bbn);
6054 optimize_branches (MonoCompile *cfg) {
6055 int i, changed = FALSE;
6056 MonoBasicBlock *bb, *bbn;
6061 /* we skip the entry block (exit is handled specially instead ) */
6062 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6064 /* dont touch code inside exception clauses */
6065 if (bb->region != -1)
6068 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6069 if (cfg->verbose_level > 2)
6070 g_print ("nullify block triggered %d\n", bbn->block_num);
6072 bb->next_bb = bbn->next_bb;
6074 for (i = 0; i < bbn->out_count; i++)
6075 replace_in_block (bbn->out_bb [i], bbn, NULL);
6077 nullify_basic_block (bbn);
6081 if (bb->out_count == 1) {
6082 bbn = bb->out_bb [0];
6084 /* conditional branches where true and false targets are the same can be also replaced with CEE_BR */
6085 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6086 bb->last_ins->opcode = CEE_BR;
6087 bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
6089 if (cfg->verbose_level > 2)
6090 g_print ("cond branch removal triggered in %d %d\n", bb->block_num, bb->out_count);
6093 if (bb->region == bbn->region && bb->next_bb == bbn) {
6094 /* the block are in sequence anyway ... */
6096 /* branches to the following block can be removed */
6097 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6098 bb->last_ins->opcode = CEE_NOP;
6100 if (cfg->verbose_level > 2)
6101 g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
6104 if (bbn->in_count == 1) {
6106 if (bbn != cfg->bb_exit) {
6107 if (cfg->verbose_level > 2)
6108 g_print ("block merge triggered %d -> %d\n", bb->block_num, bbn->block_num);
6109 merge_basic_blocks (bb, bbn);
6113 //mono_print_bb_code (bb);
6123 /* we skip the entry block (exit is handled specially instead ) */
6124 for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
6126 /* dont touch code inside exception clauses */
6127 if (bb->region != -1)
6130 if ((bbn = bb->next_bb) && bbn->in_count == 0 && bb->region == bbn->region) {
6131 if (cfg->verbose_level > 2) {
6132 g_print ("nullify block triggered %d\n", bbn->block_num);
6134 bb->next_bb = bbn->next_bb;
6136 for (i = 0; i < bbn->out_count; i++)
6137 replace_in_block (bbn->out_bb [i], bbn, NULL);
6139 nullify_basic_block (bbn);
6145 if (bb->out_count == 1) {
6146 bbn = bb->out_bb [0];
6148 if (bb->last_ins && bb->last_ins->opcode == CEE_BR) {
6149 bbn = bb->last_ins->inst_target_bb;
6150 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6151 bbn->code->inst_target_bb->region == bb->region) {
6153 if (cfg->verbose_level > 2)
6154 g_print ("in %s branch to branch triggered %d -> %d\n", cfg->method->name,
6155 bb->block_num, bbn->block_num);
6157 replace_basic_block (bb, bb->out_bb [0], bbn->code->inst_target_bb);
6158 bb->last_ins->inst_target_bb = bbn->code->inst_target_bb;
6163 } else if (bb->out_count == 2) {
6164 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode)) {
6165 bbn = bb->last_ins->inst_true_bb;
6166 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6167 bbn->code->inst_target_bb->region == bb->region) {
6168 if (cfg->verbose_level > 2)
6169 g_print ("cbranch1 to branch triggered %d -> (%d) %d (0x%02x)\n",
6170 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6173 bb->last_ins->inst_true_bb = bbn->code->inst_target_bb;
6175 replace_in_block (bbn, bb, NULL);
6177 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6178 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6180 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6186 bbn = bb->last_ins->inst_false_bb;
6187 if (bb->region == bbn->region && bbn->code && bbn->code->opcode == CEE_BR &&
6188 bbn->code->inst_target_bb->region == bb->region) {
6189 if (cfg->verbose_level > 2)
6190 g_print ("cbranch2 to branch triggered %d -> (%d) %d (0x%02x)\n",
6191 bb->block_num, bbn->block_num, bbn->code->inst_target_bb->block_num,
6194 bb->last_ins->inst_false_bb = bbn->code->inst_target_bb;
6196 replace_in_block (bbn, bb, NULL);
6198 replace_in_block (bbn->code->inst_target_bb, bbn, bb);
6199 replace_out_block (bb, bbn, bbn->code->inst_target_bb);
6201 link_bblock (cfg, bb, bbn->code->inst_target_bb);
6214 mono_compile_create_vars (MonoCompile *cfg)
6216 MonoMethodSignature *sig;
6217 MonoMethodHeader *header;
6220 header = ((MonoMethodNormal *)cfg->method)->header;
6222 sig = cfg->method->signature;
6224 if (!MONO_TYPE_IS_VOID (sig->ret)) {
6225 cfg->ret = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6226 cfg->ret->opcode = OP_RETARG;
6227 cfg->ret->inst_vtype = sig->ret;
6228 cfg->ret->klass = mono_class_from_mono_type (sig->ret);
6230 if (cfg->verbose_level > 2)
6231 g_print ("creating vars\n");
6234 mono_compile_create_var (cfg, &cfg->method->klass->this_arg, OP_ARG);
6236 for (i = 0; i < sig->param_count; ++i)
6237 mono_compile_create_var (cfg, sig->params [i], OP_ARG);
6239 cfg->locals_start = cfg->num_varinfo;
6241 if (cfg->verbose_level > 2)
6242 g_print ("creating locals\n");
6243 for (i = 0; i < header->num_locals; ++i)
6244 mono_compile_create_var (cfg, header->locals [i], OP_LOCAL);
6245 if (cfg->verbose_level > 2)
6246 g_print ("locals done\n");
6251 mono_print_code (MonoCompile *cfg)
6255 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6256 MonoInst *tree = bb->code;
6261 g_print ("CODE BLOCK %d (nesting %d):\n", bb->block_num, bb->nesting);
6263 for (; tree; tree = tree->next) {
6264 mono_print_tree (tree);
6269 bb->last_ins->next = NULL;
6274 extern const char * const mono_burg_rule_string [];
6277 emit_state (MonoCompile *cfg, MBState *state, int goal)
6280 int ern = mono_burg_rule (state, goal);
6281 const guint16 *nts = mono_burg_nts [ern];
6284 //g_print ("rule: %s\n", mono_burg_rule_string [ern]);
6288 // state->reg1 = state->reg2; /* chain rule */
6290 state->reg1 = mono_regstate_next_int (cfg->rs);
6291 //g_print ("alloc symbolic R%d (reg2: R%d) in block %d\n", state->reg1, state->reg2, cfg->cbb->block_num);
6294 state->reg1 = mono_regstate_next_int (cfg->rs);
6295 state->reg2 = mono_regstate_next_int (cfg->rs);
6298 state->reg1 = mono_regstate_next_float (cfg->rs);
6305 mono_burg_kids (state, ern, kids);
6307 emit_state (cfg, kids [0], nts [0]);
6309 emit_state (cfg, kids [1], nts [1]);
6311 g_assert (!nts [3]);
6312 emit_state (cfg, kids [2], nts [2]);
6317 // g_print ("emit: %s (%p)\n", mono_burg_rule_string [ern], state);
6318 if ((emit = mono_burg_func [ern]))
6319 emit (state, state->tree, cfg);
6322 #define DEBUG_SELECTION
6325 mini_select_instructions (MonoCompile *cfg)
6327 static int reverse_map [] = {
6328 CEE_BNE_UN, CEE_BLT, CEE_BLE, CEE_BGT, CEE_BGE,
6329 CEE_BEQ, CEE_BLT_UN, CEE_BLE_UN, CEE_BGT_UN, CEE_BGE_UN
6331 static int reverse_fmap [] = {
6332 OP_FBNE_UN, OP_FBLT, OP_FBLE, OP_FBGT, OP_FBGE,
6333 OP_FBEQ, OP_FBLT_UN, OP_FBLE_UN, OP_FBGT_UN, OP_FBGE_UN
6335 static int reverse_lmap [] = {
6336 OP_LBNE_UN, OP_LBLT, OP_LBLE, OP_LBGT, OP_LBGE,
6337 OP_LBEQ, OP_LBLT_UN, OP_LBLE_UN, OP_LBGT_UN, OP_LBGE_UN
6342 cfg->state_pool = mono_mempool_new ();
6343 cfg->rs = mono_regstate_new ();
6345 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6346 if (bb->last_ins && MONO_IS_COND_BRANCH (bb->last_ins->opcode) &&
6347 bb->next_bb != bb->last_ins->inst_false_bb) {
6349 if (bb->next_bb == bb->last_ins->inst_true_bb) {
6350 MonoBasicBlock *tmp = bb->last_ins->inst_true_bb;
6351 bb->last_ins->inst_true_bb = bb->last_ins->inst_false_bb;
6352 bb->last_ins->inst_false_bb = tmp;
6354 if (bb->last_ins->opcode >= CEE_BEQ && bb->last_ins->opcode <= CEE_BLT_UN) {
6355 bb->last_ins->opcode = reverse_map [bb->last_ins->opcode - CEE_BEQ];
6356 } else if (bb->last_ins->opcode >= OP_FBEQ && bb->last_ins->opcode <= OP_FBLT_UN) {
6357 bb->last_ins->opcode = reverse_fmap [bb->last_ins->opcode - OP_FBEQ];
6358 } else if (bb->last_ins->opcode >= OP_LBEQ && bb->last_ins->opcode <= OP_LBLT_UN) {
6359 bb->last_ins->opcode = reverse_lmap [bb->last_ins->opcode - OP_LBEQ];
6362 MonoInst *inst = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoInst));
6363 inst->opcode = CEE_BR;
6364 inst->inst_target_bb = bb->last_ins->inst_false_bb;
6365 mono_bblock_add_inst (bb, inst);
6370 #ifdef DEBUG_SELECTION
6371 if (cfg->verbose_level >= 4) {
6372 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6373 MonoInst *tree = bb->code;
6374 g_print ("DUMP BLOCK %d:\n", bb->block_num);
6377 for (; tree; tree = tree->next) {
6378 mono_print_tree (tree);
6385 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6386 MonoInst *tree = bb->code, *next;
6392 bb->last_ins = NULL;
6395 mono_regstate_reset (cfg->rs);
6397 #ifdef DEBUG_SELECTION
6398 if (cfg->verbose_level >= 3)
6399 g_print ("LABEL BLOCK %d:\n", bb->block_num);
6401 for (; tree; tree = next) {
6403 #ifdef DEBUG_SELECTION
6404 if (cfg->verbose_level >= 3) {
6405 mono_print_tree (tree);
6410 if (!(mbstate = mono_burg_label (tree, cfg))) {
6411 g_warning ("unabled to label tree %p", tree);
6412 mono_print_tree (tree);
6414 g_assert_not_reached ();
6416 emit_state (cfg, mbstate, MB_NTERM_stmt);
6418 bb->max_ireg = cfg->rs->next_vireg;
6419 bb->max_freg = cfg->rs->next_vfreg;
6422 bb->last_ins->next = NULL;
6424 mono_mempool_empty (cfg->state_pool);
6426 mono_mempool_destroy (cfg->state_pool);
6430 mono_codegen (MonoCompile *cfg)
6432 MonoJumpInfo *patch_info;
6434 int i, max_epilog_size;
6437 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6438 cfg->spill_count = 0;
6439 /* we reuse dfn here */
6440 /* bb->dfn = bb_count++; */
6441 mono_arch_local_regalloc (cfg, bb);
6444 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
6445 cfg->coverage_info = mono_profiler_coverage_alloc (cfg->method, cfg->num_bblocks);
6447 code = mono_arch_emit_prolog (cfg);
6449 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6450 code = mono_arch_instrument_prolog (cfg, mono_profiler_method_enter, code, FALSE);
6452 cfg->code_len = code - cfg->native_code;
6453 cfg->prolog_end = cfg->code_len;
6455 mono_debug_open_method (cfg);
6457 /* emit code all basic blocks */
6458 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6459 bb->native_offset = cfg->code_len;
6460 mono_arch_output_basic_block (cfg, bb);
6462 cfg->bb_exit->native_offset = cfg->code_len;
6464 code = cfg->native_code + cfg->code_len;
6466 max_epilog_size = mono_arch_max_epilog_size (cfg);
6468 /* we always allocate code in cfg->domain->code_mp to increase locality */
6469 cfg->code_size = cfg->code_len + max_epilog_size;
6470 /* fixme: align to MONO_ARCH_CODE_ALIGNMENT */
6471 code = mono_mempool_alloc (cfg->domain->code_mp, cfg->code_size);
6472 memcpy (code, cfg->native_code, cfg->code_len);
6473 g_free (cfg->native_code);
6474 cfg->native_code = code;
6475 code = cfg->native_code + cfg->code_len;
6477 /* g_assert (((int)cfg->native_code & (MONO_ARCH_CODE_ALIGNMENT - 1)) == 0); */
6479 cfg->epilog_begin = cfg->code_len;
6481 if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
6482 code = mono_arch_instrument_epilog (cfg, mono_profiler_method_leave, code, FALSE);
6484 cfg->code_len = code - cfg->native_code;
6486 mono_arch_emit_epilog (cfg);
6488 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6489 switch (patch_info->type) {
6490 case MONO_PATCH_INFO_ABS: {
6491 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (patch_info->data.target);
6493 //printf ("TEST %s %p\n", info->name, patch_info->data.target);
6494 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
6495 patch_info->data.name = info->name;
6499 case MONO_PATCH_INFO_SWITCH: {
6500 gpointer *table = g_new (gpointer, patch_info->table_size);
6501 patch_info->ip.i = patch_info->ip.label->inst_c0;
6502 for (i = 0; i < patch_info->table_size; i++) {
6503 table [i] = (gpointer)patch_info->data.table [i]->native_offset;
6505 patch_info->data.target = table;
6514 if (cfg->verbose_level > 1)
6515 g_print ("Method %s::%s emmitted at %p to %p\n", cfg->method->klass->name,
6516 cfg->method->name, cfg->native_code, cfg->native_code + cfg->code_len);
6518 mono_arch_patch_code (cfg->method, cfg->domain, cfg->native_code, cfg->patch_info);
6520 mono_debug_close_method (cfg);
6524 mono_cprop_copy_values (MonoCompile *cfg, MonoInst *tree, MonoInst **acp)
6529 if (tree->ssa_op == MONO_SSA_LOAD && (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG) &&
6530 (cp = acp [tree->inst_i0->inst_c0]) && !tree->inst_i0->flags) {
6532 if (cp->opcode == OP_ICONST) {
6533 if (cfg->opt & MONO_OPT_CONSPROP) {
6534 //{ static int c = 0; printf ("CCOPY %d %d %s\n", c++, cp->inst_c0, mono_method_full_name (cfg->method, TRUE)); }
6538 if (tree->inst_i0->inst_vtype->type == cp->inst_vtype->type) {
6539 if (cfg->opt & MONO_OPT_COPYPROP) {
6540 //{ static int c = 0; printf ("VCOPY %d\n", ++c); }
6546 arity = mono_burg_arity [tree->opcode];
6549 mono_cprop_copy_values (cfg, tree->inst_i0, acp);
6550 if (cfg->opt & MONO_OPT_CFOLD)
6551 mono_constant_fold_inst (tree, NULL);
6553 mono_cprop_copy_values (cfg, tree->inst_i1, acp);
6554 if (cfg->opt & MONO_OPT_CFOLD)
6555 mono_constant_fold_inst (tree, NULL);
6557 mono_constant_fold_inst (tree, NULL);
6563 mono_cprop_invalidate_values (MonoInst *tree, MonoInst **acp, int acp_size)
6567 switch (tree->opcode) {
6577 if (tree->ssa_op == MONO_SSA_NOP) {
6578 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6595 case OP_VOIDCALL_REG:
6596 case OP_VOIDCALLVIRT:
6598 MonoCallInst *call = (MonoCallInst *)tree;
6599 MonoMethodSignature *sig = call->signature;
6600 int i, byref = FALSE;
6602 for (i = 0; i < sig->param_count; i++) {
6603 if (sig->params [i]->byref) {
6610 memset (acp, 0, sizeof (MonoInst *) * acp_size);
6618 arity = mono_burg_arity [tree->opcode];
6624 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6627 mono_cprop_invalidate_values (tree->inst_i0, acp, acp_size);
6628 mono_cprop_invalidate_values (tree->inst_i1, acp, acp_size);
6631 g_assert_not_reached ();
6636 mono_local_cprop_bb (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **acp, int acp_size)
6638 MonoInst *tree = bb->code;
6644 for (; tree; tree = tree->next) {
6646 mono_cprop_copy_values (cfg, tree, acp);
6648 mono_cprop_invalidate_values (tree, acp, acp_size);
6650 if (tree->ssa_op == MONO_SSA_STORE &&
6651 (tree->inst_i0->opcode == OP_LOCAL || tree->inst_i0->opcode == OP_ARG)) {
6652 MonoInst *i1 = tree->inst_i1;
6654 acp [tree->inst_i0->inst_c0] = NULL;
6656 for (i = 0; i < acp_size; i++) {
6657 if (acp [i] && acp [i]->opcode != OP_ICONST &&
6658 acp [i]->inst_c0 == tree->inst_i0->inst_c0) {
6663 if (i1->opcode == OP_ICONST) {
6664 acp [tree->inst_i0->inst_c0] = i1;
6665 //printf ("DEF1 BB%d %d\n", bb->block_num,tree->inst_i0->inst_c0);
6667 if (i1->ssa_op == MONO_SSA_LOAD &&
6668 (i1->inst_i0->opcode == OP_LOCAL || i1->inst_i0->opcode == OP_ARG) &&
6669 (i1->inst_i0->inst_c0 != tree->inst_i0->inst_c0)) {
6670 acp [tree->inst_i0->inst_c0] = i1->inst_i0;
6671 //printf ("DEF2 BB%d %d %d\n", bb->block_num,tree->inst_i0->inst_c0,i1->inst_i0->inst_c0);
6676 if (tree->opcode == CEE_BEQ) {
6677 g_assert (tree->inst_i0->opcode == OP_COMPARE);
6678 if (tree->inst_i0->inst_i0->opcode == OP_ICONST &&
6679 tree->inst_i0->inst_i1->opcode == OP_ICONST) {
6681 tree->opcode = CEE_BR;
6682 if (tree->inst_i0->inst_i0->opcode == tree->inst_i0->inst_i1->opcode) {
6683 tree->inst_target_bb = tree->inst_true_bb;
6685 tree->inst_target_bb = tree->inst_false_bb;
6694 mono_local_cprop (MonoCompile *cfg)
6699 acp = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
6701 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
6702 memset (acp, 0, sizeof (MonoInst *) * cfg->num_varinfo);
6703 mono_local_cprop_bb (cfg, bb, acp, cfg->num_varinfo);
6708 mini_method_compile (MonoMethod *method, guint32 opts, MonoDomain *domain, int parts)
6710 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
6711 guint8 *ip = (guint8 *)header->code;
6714 int dfn = 0, i, code_size_ratio;
6716 mono_jit_stats.methods_compiled++;
6717 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION)
6718 mono_profiler_method_jit (method);
6720 cfg = g_new0 (MonoCompile, 1);
6721 cfg->method = method;
6722 cfg->mempool = mono_mempool_new ();
6724 cfg->prof_options = mono_profiler_get_events ();
6725 cfg->bb_hash = g_hash_table_new (g_direct_hash, NULL);
6726 cfg->domain = domain;
6727 cfg->verbose_level = mini_verbose;
6728 cfg->intvars = mono_mempool_alloc0 (cfg->mempool, sizeof (guint16) * STACK_MAX *
6729 ((MonoMethodNormal *)method)->header->max_stack);
6731 if (cfg->verbose_level > 2)
6732 g_print ("converting method %s\n", mono_method_full_name (method, TRUE));
6735 * create MonoInst* which represents arguments and local variables
6737 mono_compile_create_vars (cfg);
6739 if ((i = mono_method_to_ir (cfg, method, NULL, NULL, cfg->locals_start, NULL, NULL, NULL, 0, FALSE)) < 0) {
6740 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6741 mono_profiler_method_end_jit (method, MONO_PROFILE_FAILED);
6742 mono_destroy_compile (cfg);
6746 mono_jit_stats.basic_blocks += cfg->num_bblocks;
6747 mono_jit_stats.max_basic_blocks = MAX (cfg->num_bblocks, mono_jit_stats.max_basic_blocks);
6749 if (cfg->num_varinfo > 2000) {
6751 * we disable some optimizations if there are too many variables
6752 * because JIT time may become too expensive. The actual number needs
6753 * to be tweaked and eventually the non-linear algorithms should be fixed.
6755 cfg->opt &= ~ (MONO_OPT_LINEARS | MONO_OPT_COPYPROP | MONO_OPT_CONSPROP);
6756 cfg->disable_ssa = TRUE;
6758 /*g_print ("numblocks = %d\n", cfg->num_bblocks);*/
6760 /* Depth-first ordering on basic blocks */
6761 cfg->bblocks = mono_mempool_alloc (cfg->mempool, sizeof (MonoBasicBlock*) * (cfg->num_bblocks + 1));
6763 if (cfg->opt & MONO_OPT_BRANCH)
6764 optimize_branches (cfg);
6766 df_visit (cfg->bb_entry, &dfn, cfg->bblocks);
6767 if (cfg->num_bblocks != dfn + 1) {
6770 cfg->num_bblocks = dfn + 1;
6772 if (!header->clauses) {
6773 /* remove unreachable code, because the code in them may be
6774 * inconsistent (access to dead variables for example) */
6775 for (bb = cfg->bb_entry; bb;) {
6776 MonoBasicBlock *bbn = bb->next_bb;
6778 if (bbn && bbn->region == -1 && !bbn->dfn) {
6779 if (cfg->verbose_level > 1)
6780 g_print ("found unreachabel code in BB%d\n", bbn->block_num);
6781 bb->next_bb = bbn->next_bb;
6782 nullify_basic_block (bbn);
6790 if (cfg->opt & MONO_OPT_LOOP) {
6791 mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM);
6792 mono_compute_natural_loops (cfg);
6796 /* after method_to_ir */
6800 //#define DEBUGSSA "logic_run"
6801 #define DEBUGSSA_CLASS "Tests"
6805 if (!header->num_clauses && !cfg->disable_ssa) {
6806 mono_local_cprop (cfg);
6807 mono_ssa_compute (cfg);
6811 /* fixme: add all optimizations which requires SSA */
6812 if (cfg->opt & (MONO_OPT_DEADCE)) {
6813 if (!(cfg->comp_done & MONO_COMP_SSA) && !header->num_clauses && !cfg->disable_ssa) {
6814 mono_local_cprop (cfg);
6815 mono_ssa_compute (cfg);
6817 if (cfg->verbose_level >= 2) {
6824 /* after SSA translation */
6828 if ((cfg->opt & MONO_OPT_CONSPROP) || (cfg->opt & MONO_OPT_COPYPROP)) {
6829 if (cfg->comp_done & MONO_COMP_SSA) {
6830 mono_ssa_cprop (cfg);
6832 mono_local_cprop (cfg);
6836 if (cfg->comp_done & MONO_COMP_SSA) {
6837 mono_ssa_deadce (cfg);
6839 //mono_ssa_strength_reduction (cfg);
6841 mono_ssa_remove (cfg);
6843 if (cfg->opt & MONO_OPT_BRANCH)
6844 optimize_branches (cfg);
6847 /* after SSA removal */
6851 decompose_pass (cfg);
6853 /* FIXME: disabled with exception clauses: bug #42136 */
6854 if ((!header->num_clauses) && (cfg->opt & MONO_OPT_LINEARS)) {
6857 /* fixme: maybe we can avoid to compute livenesss here if already computed ? */
6858 cfg->comp_done &= ~MONO_COMP_LIVENESS;
6859 if (!(cfg->comp_done & MONO_COMP_LIVENESS))
6860 mono_analyze_liveness (cfg);
6862 if ((vars = mono_arch_get_allocatable_int_vars (cfg))) {
6863 regs = mono_arch_get_global_int_regs (cfg);
6864 mono_linear_scan (cfg, vars, regs, &cfg->used_int_regs);
6868 //mono_print_code (cfg);
6872 /* variables are allocated after decompose, since decompose could create temps */
6873 mono_arch_allocate_vars (cfg);
6875 if (cfg->opt & MONO_OPT_CFOLD)
6876 mono_constant_fold (cfg);
6878 mini_select_instructions (cfg);
6881 if (cfg->verbose_level >= 2) {
6882 char *id = mono_method_full_name (cfg->method, FALSE);
6883 mono_disassemble_code (cfg->native_code, cfg->code_len, id + 3);
6887 jinfo = mono_mempool_alloc0 (cfg->domain->mp, sizeof (MonoJitInfo));
6889 jinfo = g_new0 (MonoJitInfo, 1);
6890 jinfo->method = method;
6891 jinfo->code_start = cfg->native_code;
6892 jinfo->code_size = cfg->code_len;
6893 jinfo->used_regs = cfg->used_int_regs;
6895 if (header->num_clauses) {
6898 jinfo->exvar_offset = cfg->exvar? cfg->exvar->inst_offset: 0;
6899 jinfo->num_clauses = header->num_clauses;
6900 jinfo->clauses = mono_mempool_alloc0 (cfg->domain->mp,
6901 sizeof (MonoJitExceptionInfo) * header->num_clauses);
6903 for (i = 0; i < header->num_clauses; i++) {
6904 MonoExceptionClause *ec = &header->clauses [i];
6905 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
6906 MonoBasicBlock *tblock;
6908 ei->flags = ec->flags;
6910 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
6911 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->token_or_filter);
6913 ei->data.filter = cfg->native_code + tblock->native_offset;
6915 ei->data.token = ec->token_or_filter;
6918 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset);
6920 ei->try_start = cfg->native_code + tblock->native_offset;
6921 g_assert (tblock->native_offset);
6922 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->try_offset + ec->try_len);
6924 ei->try_end = cfg->native_code + tblock->native_offset;
6925 g_assert (tblock->native_offset);
6926 tblock = g_hash_table_lookup (cfg->bb_hash, ip + ec->handler_offset);
6928 ei->handler_start = cfg->native_code + tblock->native_offset;
6932 mono_jit_info_table_add (cfg->domain, jinfo);
6934 /* collect statistics */
6935 mono_jit_stats.allocated_code_size += cfg->code_len;
6936 code_size_ratio = cfg->code_len;
6937 if (code_size_ratio > mono_jit_stats.biggest_method_size) {
6938 mono_jit_stats.biggest_method_size = code_size_ratio;
6939 mono_jit_stats.biggest_method = method;
6941 code_size_ratio = (code_size_ratio * 100) / ((MonoMethodNormal *)method)->header->code_size;
6942 if (code_size_ratio > mono_jit_stats.max_code_size_ratio) {
6943 mono_jit_stats.max_code_size_ratio = code_size_ratio;
6944 mono_jit_stats.max_ratio_method = method;
6946 mono_jit_stats.native_code_size += cfg->code_len;
6948 if (cfg->prof_options & MONO_PROFILE_JIT_COMPILATION)
6949 mono_profiler_method_end_jit (method, MONO_PROFILE_OK);
6955 mono_jit_compile_method (MonoMethod *method)
6957 /* FIXME: later copy the code from mono */
6958 MonoDomain *target_domain, *domain = mono_domain_get ();
6960 GHashTable *jit_code_hash;
6963 if (default_opt & MONO_OPT_SHARED)
6964 target_domain = mono_root_domain;
6966 target_domain = domain;
6968 jit_code_hash = target_domain->jit_code_hash;
6970 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
6971 mono_jit_stats.methods_lookups++;
6975 #ifdef MONO_USE_AOT_COMPILER
6976 if (!mono_compile_aot) {
6977 mono_class_init (method->klass);
6978 if ((code = mono_aot_get_method (method))) {
6979 g_hash_table_insert (jit_code_hash, method, code);
6985 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
6986 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
6987 if (!method->info) {
6990 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
6991 mono_lookup_pinvoke_call (method);
6992 #ifdef MONO_USE_EXC_TABLES
6993 if (mono_method_blittable (method)) {
6994 method->info = method->addr;
6997 nm = mono_marshal_get_native_wrapper (method);
6998 method->info = mono_compile_method (nm);
7000 //if (mono_debug_format != MONO_DEBUG_FORMAT_NONE)
7001 //mono_debug_add_wrapper (method, nm);
7002 #ifdef MONO_USE_EXC_TABLES
7006 return method->info;
7007 } else if ((method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
7008 const char *name = method->name;
7011 if (method->klass->parent == mono_defaults.multicastdelegate_class) {
7012 if (*name == '.' && (strcmp (name, ".ctor") == 0)) {
7013 /* FIXME: uhm, we need a wrapper to handle exceptions? */
7014 return (gpointer)mono_delegate_ctor;
7015 } else if (*name == 'I' && (strcmp (name, "Invoke") == 0)) {
7016 nm = mono_marshal_get_delegate_invoke (method);
7017 return mono_jit_compile_method (nm);
7018 } else if (*name == 'B' && (strcmp (name, "BeginInvoke") == 0)) {
7019 nm = mono_marshal_get_delegate_begin_invoke (method);
7020 return mono_jit_compile_method (nm);
7021 } else if (*name == 'E' && (strcmp (name, "EndInvoke") == 0)) {
7022 nm = mono_marshal_get_delegate_end_invoke (method);
7023 return mono_jit_compile_method (nm);
7029 cfg = mini_method_compile (method, default_opt, target_domain, 0);
7030 code = cfg->native_code;
7031 mono_destroy_compile (cfg);
7033 g_hash_table_insert (jit_code_hash, method, code);
7035 if (target_domain->jump_target_hash) {
7036 MonoJumpInfo patch_info;
7038 list = g_hash_table_lookup (target_domain->jump_target_hash, method);
7040 patch_info.next = NULL;
7041 patch_info.ip.i = 0;
7042 patch_info.type = MONO_PATCH_INFO_METHOD_JUMP;
7043 patch_info.data.method = method;
7044 g_hash_table_remove (target_domain->jump_target_hash, method);
7046 for (tmp = list; tmp; tmp = tmp->next)
7047 mono_arch_patch_code (NULL, target_domain, tmp->data, &patch_info);
7048 g_slist_free (list);
7050 /* make sure runtime_init is called */
7051 mono_runtime_class_init (mono_class_vtable (target_domain, method->klass));
7057 * mono_jit_runtime_invoke:
7058 * @method: the method to invoke
7059 * @obj: this pointer
7060 * @params: array of parameter values.
7061 * @exc: used to catch exceptions objects
7064 mono_jit_runtime_invoke (MonoMethod *method, void *obj, void **params, MonoObject **exc)
7067 MonoObject *(*runtime_invoke) (MonoObject *this, void **params, MonoObject **exc);
7068 invoke = mono_marshal_get_runtime_invoke (method);
7069 runtime_invoke = mono_jit_compile_method (invoke);
7070 return runtime_invoke (obj, params, exc);
7073 #ifdef PLATFORM_WIN32
7074 #define GET_CONTEXT \
7075 struct sigcontext *ctx = (struct sigcontext*)_dummy;
7077 #define GET_CONTEXT \
7078 void **_p = (void **)&_dummy; \
7079 struct sigcontext *ctx = (struct sigcontext *)++_p;
7083 sigfpe_signal_handler (int _dummy)
7088 exc = mono_get_exception_divide_by_zero ();
7090 mono_arch_handle_exception (ctx, exc, FALSE);
7094 sigill_signal_handler (int _dummy)
7098 exc = mono_get_exception_execution_engine ("SIGILL");
7100 mono_arch_handle_exception (ctx, exc, FALSE);
7104 sigsegv_signal_handler (int _dummy)
7109 exc = mono_get_exception_null_reference ();
7111 mono_arch_handle_exception (ctx, exc, FALSE);
7115 sigusr1_signal_handler (int _dummy)
7120 thread = mono_thread_current ();
7122 g_assert (thread->abort_exc);
7124 mono_arch_handle_exception (ctx, thread->abort_exc, FALSE);
7128 sigquit_signal_handler (int _dummy)
7133 exc = mono_get_exception_execution_engine ("Interrupted (SIGQUIT).");
7135 mono_arch_handle_exception (ctx, exc, FALSE);
7140 mono_runtime_install_handlers (void)
7142 #ifndef PLATFORM_WIN32
7143 struct sigaction sa;
7146 #ifdef PLATFORM_WIN32
7148 win32_seh_set_handler(SIGFPE, sigfpe_signal_handler);
7149 win32_seh_set_handler(SIGILL, sigill_signal_handler);
7150 win32_seh_set_handler(SIGSEGV, sigsegv_signal_handler);
7151 #else /* !PLATFORM_WIN32 */
7153 /* libpthreads has its own implementation of sigaction(),
7154 * but it seems to work well with our current exception
7155 * handlers. If not we must call syscall directly instead
7159 sa.sa_handler = sigfpe_signal_handler;
7160 sigemptyset (&sa.sa_mask);
7162 //g_assert (syscall (SYS_sigaction, SIGFPE, &sa, NULL) != -1);
7163 g_assert (sigaction (SIGFPE, &sa, NULL) != -1);
7166 sa.sa_handler = sigquit_signal_handler;
7167 sigemptyset (&sa.sa_mask);
7169 g_assert (sigaction (SIGQUIT, &sa, NULL) != -1);
7172 sa.sa_handler = sigill_signal_handler;
7173 sigemptyset (&sa.sa_mask);
7175 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7176 g_assert (sigaction (SIGILL, &sa, NULL) != -1);
7178 /* catch thread abort signal */
7179 sa.sa_handler = sigusr1_signal_handler;
7180 sigemptyset (&sa.sa_mask);
7182 //g_assert (syscall (SYS_sigaction, SIGILL, &sa, NULL) != -1);
7183 g_assert (sigaction (mono_thread_get_abort_signal (), &sa, NULL) != -1);
7187 sa.sa_handler = sigsegv_signal_handler;
7188 sigemptyset (&sa.sa_mask);
7190 //g_assert (syscall (SYS_sigaction, SIGSEGV, &sa, NULL) != -1);
7191 g_assert (sigaction (SIGSEGV, &sa, NULL) != -1);
7193 #endif /* PLATFORM_WIN32 */
7196 /* mono_jit_create_remoting_trampoline:
7197 * @method: pointer to the method info
7199 * Creates a trampoline which calls the remoting functions. This
7200 * is used in the vtable of transparent proxies.
7202 * Returns: a pointer to the newly created code
7205 mono_jit_create_remoting_trampoline (MonoMethod *method)
7208 guint8 *addr = NULL;
7210 if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
7211 (method->signature->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class))) {
7212 nm = mono_marshal_get_remoting_invoke (method);
7213 addr = mono_compile_method (nm);
7215 addr = mono_compile_method (method);
7220 static CRITICAL_SECTION ms;
7223 mini_init (const char *filename)
7227 mono_arch_cpu_init ();
7229 metadata_section = &ms;
7230 InitializeCriticalSection (metadata_section);
7232 mono_jit_tls_id = TlsAlloc ();
7233 mono_thread_start_cb (GetCurrentThreadId (), (gpointer)-1, NULL);
7237 mono_runtime_install_handlers ();
7238 mono_threads_install_cleanup (mini_thread_cleanup);
7240 mono_install_compile_method (mono_jit_compile_method);
7241 mono_install_trampoline (mono_arch_create_jit_trampoline);
7242 mono_install_remoting_trampoline (mono_jit_create_remoting_trampoline);
7243 mono_install_runtime_invoke (mono_jit_runtime_invoke);
7244 mono_install_handler (mono_arch_get_throw_exception ());
7245 mono_install_stack_walk (mono_jit_walk_stack);
7246 mono_install_get_config_dir ();
7248 domain = mono_init (filename);
7251 mono_add_internal_call ("System.Diagnostics.StackFrame::get_frame_info",
7252 ves_icall_get_frame_info);
7253 mono_add_internal_call ("System.Diagnostics.StackTrace::get_trace",
7254 ves_icall_get_trace);
7255 mono_add_internal_call ("Mono.Runtime::mono_runtime_install_handlers",
7256 mono_runtime_install_handlers);
7259 create_helper_signature ();
7261 mono_arch_register_lowlevel_calls ();
7262 mono_register_jit_icall (mono_profiler_method_enter, "mono_profiler_method_enter", NULL, TRUE);
7263 mono_register_jit_icall (mono_profiler_method_leave, "mono_profiler_method_leave", NULL, TRUE);
7265 mono_register_jit_icall (mono_get_lmf_addr, "mono_get_lmf_addr", helper_sig_ptr_void, TRUE);
7266 mono_register_jit_icall (mono_domain_get, "mono_domain_get", helper_sig_domain_get, TRUE);
7268 /* fixme: we cant hanlde vararg methods this way, because the signature is not constant */
7269 //mono_register_jit_icall (ves_array_element_address, "ves_array_element_address", NULL);
7270 //mono_register_jit_icall (mono_array_new_va, "mono_array_new_va", NULL);
7272 mono_register_jit_icall (mono_arch_get_throw_exception (), "mono_arch_throw_exception", helper_sig_void_obj, TRUE);
7273 mono_register_jit_icall (mono_arch_get_throw_exception_by_name (), "mono_arch_throw_exception_by_name",
7274 helper_sig_void_ptr, TRUE);
7277 * NOTE, NOTE, NOTE, NOTE:
7278 * when adding emulation for some opcodes, remember to also add a dummy
7279 * rule to the burg files, because we need the arity information to be correct.
7281 mono_register_opcode_emulation (OP_LMUL, "__emul_lmul", helper_sig_long_long_long, mono_llmult, TRUE);
7282 mono_register_opcode_emulation (OP_LMUL_OVF_UN, "__emul_lmul_ovf_un", helper_sig_long_long_long, mono_llmult_ovf_un, FALSE);
7283 mono_register_opcode_emulation (OP_LMUL_OVF, "__emul_lmul_ovf", helper_sig_long_long_long, mono_llmult_ovf, FALSE);
7284 mono_register_opcode_emulation (OP_LDIV, "__emul_ldiv", helper_sig_long_long_long, mono_lldiv, FALSE);
7285 mono_register_opcode_emulation (OP_LDIV_UN, "__emul_ldiv_un", helper_sig_long_long_long, mono_lldiv_un, FALSE);
7286 mono_register_opcode_emulation (OP_LREM, "__emul_lrem", helper_sig_long_long_long, mono_llrem, FALSE);
7287 mono_register_opcode_emulation (OP_LREM_UN, "__emul_lrem_un", helper_sig_long_long_long, mono_llrem_un, FALSE);
7289 mono_register_opcode_emulation (OP_LSHL, "__emul_lshl", helper_sig_long_long_int, mono_lshl, TRUE);
7290 mono_register_opcode_emulation (OP_LSHR, "__emul_lshr", helper_sig_long_long_int, mono_lshr, TRUE);
7291 mono_register_opcode_emulation (OP_LSHR_UN, "__emul_lshr_un", helper_sig_long_long_int, mono_lshr_un, TRUE);
7293 mono_register_opcode_emulation (OP_FCONV_TO_U8, "__emul_fconv_to_u8", helper_sig_ulong_double, mono_fconv_u8, FALSE);
7294 mono_register_opcode_emulation (OP_FCONV_TO_U4, "__emul_fconv_to_u4", helper_sig_uint_double, mono_fconv_u4, FALSE);
7295 mono_register_opcode_emulation (OP_FCONV_TO_OVF_I8, "__emul_fconv_to_ovf_i8", helper_sig_long_double, mono_fconv_ovf_i8, TRUE);
7296 mono_register_opcode_emulation (OP_FCONV_TO_OVF_U8, "__emul_fconv_to_ovf_u8", helper_sig_ulong_double, mono_fconv_ovf_u8, TRUE);
7298 #if SIZEOF_VOID_P == 4
7299 mono_register_opcode_emulation (OP_FCONV_TO_U, "__emul_fconv_to_u", helper_sig_uint_double, mono_fconv_u4, TRUE);
7301 #warning "fixme: add opcode emulation"
7304 /* other jit icalls */
7305 mono_register_jit_icall (mono_class_static_field_address , "mono_class_static_field_address",
7306 helper_sig_ptr_ptr_ptr, FALSE);
7307 mono_register_jit_icall (mono_ldtoken_wrapper, "mono_ldtoken_wrapper", helper_sig_ptr_ptr_ptr, FALSE);
7308 mono_register_jit_icall (mono_threads_get_static_data, "mono_threads_get_static_data", helper_sig_ptr_int, FALSE);
7309 mono_register_jit_icall (mono_ldstr, "mono_ldstr", helper_sig_ldstr, FALSE);
7310 mono_register_jit_icall (helper_memcpy, "helper_memcpy", helper_sig_memcpy, FALSE);
7311 mono_register_jit_icall (helper_memset, "helper_memset", helper_sig_memset, FALSE);
7312 mono_register_jit_icall (helper_initobj, "helper_initobj", helper_sig_initobj, FALSE);
7313 mono_register_jit_icall (helper_stelem_ref, "helper_stelem_ref", helper_sig_stelem_ref, FALSE);
7314 mono_register_jit_icall (mono_object_new, "mono_object_new", helper_sig_object_new, FALSE);
7315 mono_register_jit_icall (mono_object_new_specific, "mono_object_new_specific", helper_sig_object_new_specific, FALSE);
7316 mono_register_jit_icall (mono_object_new_fast, "mono_object_new_fast", helper_sig_object_new_specific, FALSE);
7317 mono_register_jit_icall (mono_array_new, "mono_array_new", helper_sig_newarr, FALSE);
7318 mono_register_jit_icall (mono_array_new_specific, "mono_array_new_specific", helper_sig_newarr_specific, FALSE);
7319 mono_register_jit_icall (mono_string_to_utf16, "mono_string_to_utf16", helper_sig_ptr_obj, FALSE);
7320 mono_register_jit_icall (mono_string_from_utf16, "mono_string_from_utf16", helper_sig_obj_ptr, FALSE);
7321 mono_register_jit_icall (mono_string_new_wrapper, "mono_string_new_wrapper", helper_sig_obj_ptr, FALSE);
7322 mono_register_jit_icall (mono_string_to_utf8, "mono_string_to_utf8", helper_sig_ptr_obj, FALSE);
7323 mono_register_jit_icall (mono_string_to_bstr, "mono_string_to_bstr", helper_sig_ptr_obj, FALSE);
7324 mono_register_jit_icall (mono_string_to_ansibstr, "mono_string_to_ansibstr", helper_sig_ptr_obj, FALSE);
7325 mono_register_jit_icall (mono_string_builder_to_utf8, "mono_string_builder_to_utf8", helper_sig_ptr_obj, FALSE);
7326 mono_register_jit_icall (mono_array_to_savearray, "mono_array_to_savearray", helper_sig_ptr_obj, FALSE);
7327 mono_register_jit_icall (mono_array_to_lparray, "mono_array_to_lparray", helper_sig_ptr_obj, FALSE);
7328 mono_register_jit_icall (mono_delegate_to_ftnptr, "mono_delegate_to_ftnptr", helper_sig_ptr_obj, FALSE);
7329 mono_register_jit_icall (mono_marshal_string_array, "mono_marshal_string_array", helper_sig_ptr_obj, FALSE);
7330 mono_register_jit_icall (mono_string_utf8_to_builder, "mono_string_utf8_to_builder", helper_sig_void_ptr_ptr, FALSE);
7331 mono_register_jit_icall (mono_marshal_free_array, "mono_marshal_free_array", helper_sig_void_ptr_ptr, FALSE);
7332 mono_register_jit_icall (mono_string_to_byvalstr, "mono_string_to_byvalstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7333 mono_register_jit_icall (mono_string_to_byvalwstr, "mono_string_to_byvalwstr", helper_sig_void_ptr_ptr_ptr, FALSE);
7334 mono_register_jit_icall (g_free, "g_free", helper_sig_void_ptr, FALSE);
7335 mono_register_jit_icall (mono_runtime_class_init, "mono_runtime_class_init", helper_sig_void_ptr, FALSE);
7336 mono_register_jit_icall (mono_ldftn, "mono_ldftn", helper_sig_compile, FALSE);
7337 mono_register_jit_icall (mono_ldvirtfn, "mono_ldvirtfn", helper_sig_compile_virt, FALSE);
7339 mono_runtime_install_cleanup ((MonoDomainFunc)mini_cleanup);
7340 mono_runtime_init (domain, mono_thread_start_cb, mono_thread_attach_cb);
7342 //mono_thread_attach (domain);
7346 MonoJitStats mono_jit_stats = {0};
7349 print_jit_stats (void)
7351 if (mono_jit_stats.enabled) {
7352 g_print ("Mono Jit statistics\n");
7353 g_print ("Compiled methods: %ld\n", mono_jit_stats.methods_compiled);
7354 g_print ("Methods from AOT: %ld\n", mono_jit_stats.methods_aot);
7355 g_print ("Methods cache lookup: %ld\n", mono_jit_stats.methods_lookups);
7356 g_print ("Method trampolines: %ld\n", mono_jit_stats.method_trampolines);
7357 g_print ("Basic blocks: %ld\n", mono_jit_stats.basic_blocks);
7358 g_print ("Max basic blocks: %ld\n", mono_jit_stats.max_basic_blocks);
7359 g_print ("Allocated vars: %ld\n", mono_jit_stats.allocate_var);
7360 g_print ("Analyze stack repeat: %ld\n", mono_jit_stats.analyze_stack_repeat);
7361 g_print ("Compiled CIL code size: %ld\n", mono_jit_stats.cil_code_size);
7362 g_print ("Native code size: %ld\n", mono_jit_stats.native_code_size);
7363 g_print ("Max code size ratio: %.2f (%s::%s)\n", mono_jit_stats.max_code_size_ratio/100.0,
7364 mono_jit_stats.max_ratio_method->klass->name, mono_jit_stats.max_ratio_method->name);
7365 g_print ("Biggest method: %ld (%s::%s)\n", mono_jit_stats.biggest_method_size,
7366 mono_jit_stats.biggest_method->klass->name, mono_jit_stats.biggest_method->name);
7367 g_print ("Code reallocs: %ld\n", mono_jit_stats.code_reallocs);
7368 g_print ("Allocated code size: %ld\n", mono_jit_stats.allocated_code_size);
7369 g_print ("Inlineable methods: %ld\n", mono_jit_stats.inlineable_methods);
7370 g_print ("Inlined methods: %ld\n", mono_jit_stats.inlined_methods);
7372 g_print ("\nCreated object count: %ld\n", mono_stats.new_object_count);
7373 g_print ("Initialized classes: %ld\n", mono_stats.initialized_class_count);
7374 g_print ("Used classes: %ld\n", mono_stats.used_class_count);
7375 g_print ("Static data size: %ld\n", mono_stats.class_static_data_size);
7376 g_print ("VTable data size: %ld\n", mono_stats.class_vtable_size);
7381 mini_cleanup (MonoDomain *domain)
7384 * mono_runtime_cleanup() and mono_domain_finalize () need to
7385 * be called early since they need the execution engine still
7386 * fully working (mono_domain_finalize may invoke managed finalizers
7387 * and mono_runtime_cleanup will wait for other threads to finish).
7389 mono_domain_finalize (domain);
7391 mono_runtime_cleanup (domain);
7393 mono_profiler_shutdown ();
7395 mono_debug_cleanup ();
7397 #ifdef PLATFORM_WIN32
7398 win32_seh_cleanup();
7401 mono_domain_unload (domain, TRUE);
7404 DeleteCriticalSection (metadata_section);
7408 mono_set_defaults (int verbose_level, guint32 opts)
7410 mini_verbose = verbose_level;